PowerPC warning on symbol+offset@got
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright (C) 1993-2019 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 /* Arrays of operands for each instruction. */
816 #define MAX_OPERANDS 6
817 struct mips_operand_array
818 {
819 const struct mips_operand *operand[MAX_OPERANDS];
820 };
821 static struct mips_operand_array *mips_operands;
822 static struct mips_operand_array *mips16_operands;
823 static struct mips_operand_array *micromips_operands;
824
825 /* Nop instructions used by emit_nop. */
826 static struct mips_cl_insn nop_insn;
827 static struct mips_cl_insn mips16_nop_insn;
828 static struct mips_cl_insn micromips_nop16_insn;
829 static struct mips_cl_insn micromips_nop32_insn;
830
831 /* Sync instructions used by insert sync. */
832 static struct mips_cl_insn sync_insn;
833
834 /* The appropriate nop for the current mode. */
835 #define NOP_INSN (mips_opts.mips16 \
836 ? &mips16_nop_insn \
837 : (mips_opts.micromips \
838 ? (mips_opts.insn32 \
839 ? &micromips_nop32_insn \
840 : &micromips_nop16_insn) \
841 : &nop_insn))
842
843 /* The size of NOP_INSN in bytes. */
844 #define NOP_INSN_SIZE ((mips_opts.mips16 \
845 || (mips_opts.micromips && !mips_opts.insn32)) \
846 ? 2 : 4)
847
848 /* If this is set, it points to a frag holding nop instructions which
849 were inserted before the start of a noreorder section. If those
850 nops turn out to be unnecessary, the size of the frag can be
851 decreased. */
852 static fragS *prev_nop_frag;
853
854 /* The number of nop instructions we created in prev_nop_frag. */
855 static int prev_nop_frag_holds;
856
857 /* The number of nop instructions that we know we need in
858 prev_nop_frag. */
859 static int prev_nop_frag_required;
860
861 /* The number of instructions we've seen since prev_nop_frag. */
862 static int prev_nop_frag_since;
863
864 /* Relocations against symbols are sometimes done in two parts, with a HI
865 relocation and a LO relocation. Each relocation has only 16 bits of
866 space to store an addend. This means that in order for the linker to
867 handle carries correctly, it must be able to locate both the HI and
868 the LO relocation. This means that the relocations must appear in
869 order in the relocation table.
870
871 In order to implement this, we keep track of each unmatched HI
872 relocation. We then sort them so that they immediately precede the
873 corresponding LO relocation. */
874
875 struct mips_hi_fixup
876 {
877 /* Next HI fixup. */
878 struct mips_hi_fixup *next;
879 /* This fixup. */
880 fixS *fixp;
881 /* The section this fixup is in. */
882 segT seg;
883 };
884
885 /* The list of unmatched HI relocs. */
886
887 static struct mips_hi_fixup *mips_hi_fixup_list;
888
889 /* The frag containing the last explicit relocation operator.
890 Null if explicit relocations have not been used. */
891
892 static fragS *prev_reloc_op_frag;
893
894 /* Map mips16 register numbers to normal MIPS register numbers. */
895
896 static const unsigned int mips16_to_32_reg_map[] =
897 {
898 16, 17, 2, 3, 4, 5, 6, 7
899 };
900
901 /* Map microMIPS register numbers to normal MIPS register numbers. */
902
903 #define micromips_to_32_reg_d_map mips16_to_32_reg_map
904
905 /* The microMIPS registers with type h. */
906 static const unsigned int micromips_to_32_reg_h_map1[] =
907 {
908 5, 5, 6, 4, 4, 4, 4, 4
909 };
910 static const unsigned int micromips_to_32_reg_h_map2[] =
911 {
912 6, 7, 7, 21, 22, 5, 6, 7
913 };
914
915 /* The microMIPS registers with type m. */
916 static const unsigned int micromips_to_32_reg_m_map[] =
917 {
918 0, 17, 2, 3, 16, 18, 19, 20
919 };
920
921 #define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
922
923 /* Classifies the kind of instructions we're interested in when
924 implementing -mfix-vr4120. */
925 enum fix_vr4120_class
926 {
927 FIX_VR4120_MACC,
928 FIX_VR4120_DMACC,
929 FIX_VR4120_MULT,
930 FIX_VR4120_DMULT,
931 FIX_VR4120_DIV,
932 FIX_VR4120_MTHILO,
933 NUM_FIX_VR4120_CLASSES
934 };
935
936 /* ...likewise -mfix-loongson2f-jump. */
937 static bfd_boolean mips_fix_loongson2f_jump;
938
939 /* ...likewise -mfix-loongson2f-nop. */
940 static bfd_boolean mips_fix_loongson2f_nop;
941
942 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
943 static bfd_boolean mips_fix_loongson2f;
944
945 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
946 there must be at least one other instruction between an instruction
947 of type X and an instruction of type Y. */
948 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
949
950 /* True if -mfix-vr4120 is in force. */
951 static int mips_fix_vr4120;
952
953 /* ...likewise -mfix-vr4130. */
954 static int mips_fix_vr4130;
955
956 /* ...likewise -mfix-24k. */
957 static int mips_fix_24k;
958
959 /* ...likewise -mfix-rm7000 */
960 static int mips_fix_rm7000;
961
962 /* ...likewise -mfix-cn63xxp1 */
963 static bfd_boolean mips_fix_cn63xxp1;
964
965 /* ...likewise -mfix-r5900 */
966 static bfd_boolean mips_fix_r5900;
967 static bfd_boolean mips_fix_r5900_explicit;
968
969 /* ...likewise -mfix-loongson3-llsc. */
970 static bfd_boolean mips_fix_loongson3_llsc = DEFAULT_MIPS_FIX_LOONGSON3_LLSC;
971
972 /* We don't relax branches by default, since this causes us to expand
973 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
974 fail to compute the offset before expanding the macro to the most
975 efficient expansion. */
976
977 static int mips_relax_branch;
978
979 /* TRUE if checks are suppressed for invalid branches between ISA modes.
980 Needed for broken assembly produced by some GCC versions and some
981 sloppy code out there, where branches to data labels are present. */
982 static bfd_boolean mips_ignore_branch_isa;
983 \f
984 /* The expansion of many macros depends on the type of symbol that
985 they refer to. For example, when generating position-dependent code,
986 a macro that refers to a symbol may have two different expansions,
987 one which uses GP-relative addresses and one which uses absolute
988 addresses. When generating SVR4-style PIC, a macro may have
989 different expansions for local and global symbols.
990
991 We handle these situations by generating both sequences and putting
992 them in variant frags. In position-dependent code, the first sequence
993 will be the GP-relative one and the second sequence will be the
994 absolute one. In SVR4 PIC, the first sequence will be for global
995 symbols and the second will be for local symbols.
996
997 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
998 SECOND are the lengths of the two sequences in bytes. These fields
999 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
1000 the subtype has the following flags:
1001
1002 RELAX_PIC
1003 Set if generating PIC code.
1004
1005 RELAX_USE_SECOND
1006 Set if it has been decided that we should use the second
1007 sequence instead of the first.
1008
1009 RELAX_SECOND_LONGER
1010 Set in the first variant frag if the macro's second implementation
1011 is longer than its first. This refers to the macro as a whole,
1012 not an individual relaxation.
1013
1014 RELAX_NOMACRO
1015 Set in the first variant frag if the macro appeared in a .set nomacro
1016 block and if one alternative requires a warning but the other does not.
1017
1018 RELAX_DELAY_SLOT
1019 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
1020 delay slot.
1021
1022 RELAX_DELAY_SLOT_16BIT
1023 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
1024 16-bit instruction.
1025
1026 RELAX_DELAY_SLOT_SIZE_FIRST
1027 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
1028 the macro is of the wrong size for the branch delay slot.
1029
1030 RELAX_DELAY_SLOT_SIZE_SECOND
1031 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1032 the macro is of the wrong size for the branch delay slot.
1033
1034 The frag's "opcode" points to the first fixup for relaxable code.
1035
1036 Relaxable macros are generated using a sequence such as:
1037
1038 relax_start (SYMBOL);
1039 ... generate first expansion ...
1040 relax_switch ();
1041 ... generate second expansion ...
1042 relax_end ();
1043
1044 The code and fixups for the unwanted alternative are discarded
1045 by md_convert_frag. */
1046 #define RELAX_ENCODE(FIRST, SECOND, PIC) \
1047 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
1048
1049 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1050 #define RELAX_SECOND(X) ((X) & 0xff)
1051 #define RELAX_PIC(X) (((X) & 0x10000) != 0)
1052 #define RELAX_USE_SECOND 0x20000
1053 #define RELAX_SECOND_LONGER 0x40000
1054 #define RELAX_NOMACRO 0x80000
1055 #define RELAX_DELAY_SLOT 0x100000
1056 #define RELAX_DELAY_SLOT_16BIT 0x200000
1057 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1058 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
1059
1060 /* Branch without likely bit. If label is out of range, we turn:
1061
1062 beq reg1, reg2, label
1063 delay slot
1064
1065 into
1066
1067 bne reg1, reg2, 0f
1068 nop
1069 j label
1070 0: delay slot
1071
1072 with the following opcode replacements:
1073
1074 beq <-> bne
1075 blez <-> bgtz
1076 bltz <-> bgez
1077 bc1f <-> bc1t
1078
1079 bltzal <-> bgezal (with jal label instead of j label)
1080
1081 Even though keeping the delay slot instruction in the delay slot of
1082 the branch would be more efficient, it would be very tricky to do
1083 correctly, because we'd have to introduce a variable frag *after*
1084 the delay slot instruction, and expand that instead. Let's do it
1085 the easy way for now, even if the branch-not-taken case now costs
1086 one additional instruction. Out-of-range branches are not supposed
1087 to be common, anyway.
1088
1089 Branch likely. If label is out of range, we turn:
1090
1091 beql reg1, reg2, label
1092 delay slot (annulled if branch not taken)
1093
1094 into
1095
1096 beql reg1, reg2, 1f
1097 nop
1098 beql $0, $0, 2f
1099 nop
1100 1: j[al] label
1101 delay slot (executed only if branch taken)
1102 2:
1103
1104 It would be possible to generate a shorter sequence by losing the
1105 likely bit, generating something like:
1106
1107 bne reg1, reg2, 0f
1108 nop
1109 j[al] label
1110 delay slot (executed only if branch taken)
1111 0:
1112
1113 beql -> bne
1114 bnel -> beq
1115 blezl -> bgtz
1116 bgtzl -> blez
1117 bltzl -> bgez
1118 bgezl -> bltz
1119 bc1fl -> bc1t
1120 bc1tl -> bc1f
1121
1122 bltzall -> bgezal (with jal label instead of j label)
1123 bgezall -> bltzal (ditto)
1124
1125
1126 but it's not clear that it would actually improve performance. */
1127 #define RELAX_BRANCH_ENCODE(at, pic, \
1128 uncond, likely, link, toofar) \
1129 ((relax_substateT) \
1130 (0xc0000000 \
1131 | ((at) & 0x1f) \
1132 | ((pic) ? 0x20 : 0) \
1133 | ((toofar) ? 0x40 : 0) \
1134 | ((link) ? 0x80 : 0) \
1135 | ((likely) ? 0x100 : 0) \
1136 | ((uncond) ? 0x200 : 0)))
1137 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1138 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1139 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1140 #define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1141 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1142 #define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
1143 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1144
1145 /* For mips16 code, we use an entirely different form of relaxation.
1146 mips16 supports two versions of most instructions which take
1147 immediate values: a small one which takes some small value, and a
1148 larger one which takes a 16 bit value. Since branches also follow
1149 this pattern, relaxing these values is required.
1150
1151 We can assemble both mips16 and normal MIPS code in a single
1152 object. Therefore, we need to support this type of relaxation at
1153 the same time that we support the relaxation described above. We
1154 use the high bit of the subtype field to distinguish these cases.
1155
1156 The information we store for this type of relaxation is the
1157 argument code found in the opcode file for this relocation, whether
1158 the user explicitly requested a small or extended form, and whether
1159 the relocation is in a jump or jal delay slot. That tells us the
1160 size of the value, and how it should be stored. We also store
1161 whether the fragment is considered to be extended or not. We also
1162 store whether this is known to be a branch to a different section,
1163 whether we have tried to relax this frag yet, and whether we have
1164 ever extended a PC relative fragment because of a shift count. */
1165 #define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro, \
1166 small, ext, \
1167 dslot, jal_dslot) \
1168 (0x80000000 \
1169 | ((type) & 0xff) \
1170 | ((e2) ? 0x100 : 0) \
1171 | ((pic) ? 0x200 : 0) \
1172 | ((sym32) ? 0x400 : 0) \
1173 | ((nomacro) ? 0x800 : 0) \
1174 | ((small) ? 0x1000 : 0) \
1175 | ((ext) ? 0x2000 : 0) \
1176 | ((dslot) ? 0x4000 : 0) \
1177 | ((jal_dslot) ? 0x8000 : 0))
1178
1179 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1180 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1181 #define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1182 #define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1183 #define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1184 #define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1185 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1186 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1187 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1188 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1189
1190 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1191 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1192 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1193 #define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1194 #define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1195 #define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1196 #define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1197 #define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1198 #define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
1199
1200 /* For microMIPS code, we use relaxation similar to one we use for
1201 MIPS16 code. Some instructions that take immediate values support
1202 two encodings: a small one which takes some small value, and a
1203 larger one which takes a 16 bit value. As some branches also follow
1204 this pattern, relaxing these values is required.
1205
1206 We can assemble both microMIPS and normal MIPS code in a single
1207 object. Therefore, we need to support this type of relaxation at
1208 the same time that we support the relaxation described above. We
1209 use one of the high bits of the subtype field to distinguish these
1210 cases.
1211
1212 The information we store for this type of relaxation is the argument
1213 code found in the opcode file for this relocation, the register
1214 selected as the assembler temporary, whether in the 32-bit
1215 instruction mode, whether the branch is unconditional, whether it is
1216 compact, whether there is no delay-slot instruction available to fill
1217 in, whether it stores the link address implicitly in $ra, whether
1218 relaxation of out-of-range 32-bit branches to a sequence of
1219 instructions is enabled, and whether the displacement of a branch is
1220 too large to fit as an immediate argument of a 16-bit and a 32-bit
1221 branch, respectively. */
1222 #define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
1223 uncond, compact, link, nods, \
1224 relax32, toofar16, toofar32) \
1225 (0x40000000 \
1226 | ((type) & 0xff) \
1227 | (((at) & 0x1f) << 8) \
1228 | ((insn32) ? 0x2000 : 0) \
1229 | ((pic) ? 0x4000 : 0) \
1230 | ((uncond) ? 0x8000 : 0) \
1231 | ((compact) ? 0x10000 : 0) \
1232 | ((link) ? 0x20000 : 0) \
1233 | ((nods) ? 0x40000 : 0) \
1234 | ((relax32) ? 0x80000 : 0) \
1235 | ((toofar16) ? 0x100000 : 0) \
1236 | ((toofar32) ? 0x200000 : 0))
1237 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1238 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1239 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1240 #define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
1241 #define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1242 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1243 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1244 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1245 #define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1246 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1247
1248 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1249 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1250 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1251 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1252 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1253 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
1254
1255 /* Sign-extend 16-bit value X. */
1256 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1257
1258 /* Is the given value a sign-extended 32-bit value? */
1259 #define IS_SEXT_32BIT_NUM(x) \
1260 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1261 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1262
1263 /* Is the given value a sign-extended 16-bit value? */
1264 #define IS_SEXT_16BIT_NUM(x) \
1265 (((x) &~ (offsetT) 0x7fff) == 0 \
1266 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1267
1268 /* Is the given value a sign-extended 12-bit value? */
1269 #define IS_SEXT_12BIT_NUM(x) \
1270 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1271
1272 /* Is the given value a sign-extended 9-bit value? */
1273 #define IS_SEXT_9BIT_NUM(x) \
1274 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1275
1276 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
1277 #define IS_ZEXT_32BIT_NUM(x) \
1278 (((x) &~ (offsetT) 0xffffffff) == 0 \
1279 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1280
1281 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1282 SHIFT places. */
1283 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1284 (((STRUCT) >> (SHIFT)) & (MASK))
1285
1286 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
1287 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1288 (!(MICROMIPS) \
1289 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1290 : EXTRACT_BITS ((INSN).insn_opcode, \
1291 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1292 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1293 EXTRACT_BITS ((INSN).insn_opcode, \
1294 MIPS16OP_MASK_##FIELD, \
1295 MIPS16OP_SH_##FIELD)
1296
1297 /* The MIPS16 EXTEND opcode, shifted left 16 places. */
1298 #define MIPS16_EXTEND (0xf000U << 16)
1299 \f
1300 /* Whether or not we are emitting a branch-likely macro. */
1301 static bfd_boolean emit_branch_likely_macro = FALSE;
1302
1303 /* Global variables used when generating relaxable macros. See the
1304 comment above RELAX_ENCODE for more details about how relaxation
1305 is used. */
1306 static struct {
1307 /* 0 if we're not emitting a relaxable macro.
1308 1 if we're emitting the first of the two relaxation alternatives.
1309 2 if we're emitting the second alternative. */
1310 int sequence;
1311
1312 /* The first relaxable fixup in the current frag. (In other words,
1313 the first fixup that refers to relaxable code.) */
1314 fixS *first_fixup;
1315
1316 /* sizes[0] says how many bytes of the first alternative are stored in
1317 the current frag. Likewise sizes[1] for the second alternative. */
1318 unsigned int sizes[2];
1319
1320 /* The symbol on which the choice of sequence depends. */
1321 symbolS *symbol;
1322 } mips_relax;
1323 \f
1324 /* Global variables used to decide whether a macro needs a warning. */
1325 static struct {
1326 /* True if the macro is in a branch delay slot. */
1327 bfd_boolean delay_slot_p;
1328
1329 /* Set to the length in bytes required if the macro is in a delay slot
1330 that requires a specific length of instruction, otherwise zero. */
1331 unsigned int delay_slot_length;
1332
1333 /* For relaxable macros, sizes[0] is the length of the first alternative
1334 in bytes and sizes[1] is the length of the second alternative.
1335 For non-relaxable macros, both elements give the length of the
1336 macro in bytes. */
1337 unsigned int sizes[2];
1338
1339 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1340 instruction of the first alternative in bytes and first_insn_sizes[1]
1341 is the length of the first instruction of the second alternative.
1342 For non-relaxable macros, both elements give the length of the first
1343 instruction in bytes.
1344
1345 Set to zero if we haven't yet seen the first instruction. */
1346 unsigned int first_insn_sizes[2];
1347
1348 /* For relaxable macros, insns[0] is the number of instructions for the
1349 first alternative and insns[1] is the number of instructions for the
1350 second alternative.
1351
1352 For non-relaxable macros, both elements give the number of
1353 instructions for the macro. */
1354 unsigned int insns[2];
1355
1356 /* The first variant frag for this macro. */
1357 fragS *first_frag;
1358 } mips_macro_warning;
1359 \f
1360 /* Prototypes for static functions. */
1361
1362 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1363
1364 static void append_insn
1365 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1366 bfd_boolean expansionp);
1367 static void mips_no_prev_insn (void);
1368 static void macro_build (expressionS *, const char *, const char *, ...);
1369 static void mips16_macro_build
1370 (expressionS *, const char *, const char *, va_list *);
1371 static void load_register (int, expressionS *, int);
1372 static void macro_start (void);
1373 static void macro_end (void);
1374 static void macro (struct mips_cl_insn *ip, char *str);
1375 static void mips16_macro (struct mips_cl_insn * ip);
1376 static void mips_ip (char *str, struct mips_cl_insn * ip);
1377 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1378 static unsigned long mips16_immed_extend (offsetT, unsigned int);
1379 static void mips16_immed
1380 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1381 unsigned int, unsigned long *);
1382 static size_t my_getSmallExpression
1383 (expressionS *, bfd_reloc_code_real_type *, char *);
1384 static void my_getExpression (expressionS *, char *);
1385 static void s_align (int);
1386 static void s_change_sec (int);
1387 static void s_change_section (int);
1388 static void s_cons (int);
1389 static void s_float_cons (int);
1390 static void s_mips_globl (int);
1391 static void s_option (int);
1392 static void s_mipsset (int);
1393 static void s_abicalls (int);
1394 static void s_cpload (int);
1395 static void s_cpsetup (int);
1396 static void s_cplocal (int);
1397 static void s_cprestore (int);
1398 static void s_cpreturn (int);
1399 static void s_dtprelword (int);
1400 static void s_dtpreldword (int);
1401 static void s_tprelword (int);
1402 static void s_tpreldword (int);
1403 static void s_gpvalue (int);
1404 static void s_gpword (int);
1405 static void s_gpdword (int);
1406 static void s_ehword (int);
1407 static void s_cpadd (int);
1408 static void s_insn (int);
1409 static void s_nan (int);
1410 static void s_module (int);
1411 static void s_mips_ent (int);
1412 static void s_mips_end (int);
1413 static void s_mips_frame (int);
1414 static void s_mips_mask (int reg_type);
1415 static void s_mips_stab (int);
1416 static void s_mips_weakext (int);
1417 static void s_mips_file (int);
1418 static void s_mips_loc (int);
1419 static bfd_boolean pic_need_relax (symbolS *);
1420 static int relaxed_branch_length (fragS *, asection *, int);
1421 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1422 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1423 static void file_mips_check_options (void);
1424
1425 /* Table and functions used to map between CPU/ISA names, and
1426 ISA levels, and CPU numbers. */
1427
1428 struct mips_cpu_info
1429 {
1430 const char *name; /* CPU or ISA name. */
1431 int flags; /* MIPS_CPU_* flags. */
1432 int ase; /* Set of ASEs implemented by the CPU. */
1433 int isa; /* ISA level. */
1434 int cpu; /* CPU number (default CPU if ISA). */
1435 };
1436
1437 #define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1438
1439 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1440 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1441 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1442 \f
1443 /* Command-line options. */
1444 const char *md_shortopts = "O::g::G:";
1445
1446 enum options
1447 {
1448 OPTION_MARCH = OPTION_MD_BASE,
1449 OPTION_MTUNE,
1450 OPTION_MIPS1,
1451 OPTION_MIPS2,
1452 OPTION_MIPS3,
1453 OPTION_MIPS4,
1454 OPTION_MIPS5,
1455 OPTION_MIPS32,
1456 OPTION_MIPS64,
1457 OPTION_MIPS32R2,
1458 OPTION_MIPS32R3,
1459 OPTION_MIPS32R5,
1460 OPTION_MIPS32R6,
1461 OPTION_MIPS64R2,
1462 OPTION_MIPS64R3,
1463 OPTION_MIPS64R5,
1464 OPTION_MIPS64R6,
1465 OPTION_MIPS16,
1466 OPTION_NO_MIPS16,
1467 OPTION_MIPS3D,
1468 OPTION_NO_MIPS3D,
1469 OPTION_MDMX,
1470 OPTION_NO_MDMX,
1471 OPTION_DSP,
1472 OPTION_NO_DSP,
1473 OPTION_MT,
1474 OPTION_NO_MT,
1475 OPTION_VIRT,
1476 OPTION_NO_VIRT,
1477 OPTION_MSA,
1478 OPTION_NO_MSA,
1479 OPTION_SMARTMIPS,
1480 OPTION_NO_SMARTMIPS,
1481 OPTION_DSPR2,
1482 OPTION_NO_DSPR2,
1483 OPTION_DSPR3,
1484 OPTION_NO_DSPR3,
1485 OPTION_EVA,
1486 OPTION_NO_EVA,
1487 OPTION_XPA,
1488 OPTION_NO_XPA,
1489 OPTION_MICROMIPS,
1490 OPTION_NO_MICROMIPS,
1491 OPTION_MCU,
1492 OPTION_NO_MCU,
1493 OPTION_MIPS16E2,
1494 OPTION_NO_MIPS16E2,
1495 OPTION_CRC,
1496 OPTION_NO_CRC,
1497 OPTION_M4650,
1498 OPTION_NO_M4650,
1499 OPTION_M4010,
1500 OPTION_NO_M4010,
1501 OPTION_M4100,
1502 OPTION_NO_M4100,
1503 OPTION_M3900,
1504 OPTION_NO_M3900,
1505 OPTION_M7000_HILO_FIX,
1506 OPTION_MNO_7000_HILO_FIX,
1507 OPTION_FIX_24K,
1508 OPTION_NO_FIX_24K,
1509 OPTION_FIX_RM7000,
1510 OPTION_NO_FIX_RM7000,
1511 OPTION_FIX_LOONGSON3_LLSC,
1512 OPTION_NO_FIX_LOONGSON3_LLSC,
1513 OPTION_FIX_LOONGSON2F_JUMP,
1514 OPTION_NO_FIX_LOONGSON2F_JUMP,
1515 OPTION_FIX_LOONGSON2F_NOP,
1516 OPTION_NO_FIX_LOONGSON2F_NOP,
1517 OPTION_FIX_VR4120,
1518 OPTION_NO_FIX_VR4120,
1519 OPTION_FIX_VR4130,
1520 OPTION_NO_FIX_VR4130,
1521 OPTION_FIX_CN63XXP1,
1522 OPTION_NO_FIX_CN63XXP1,
1523 OPTION_FIX_R5900,
1524 OPTION_NO_FIX_R5900,
1525 OPTION_TRAP,
1526 OPTION_BREAK,
1527 OPTION_EB,
1528 OPTION_EL,
1529 OPTION_FP32,
1530 OPTION_GP32,
1531 OPTION_CONSTRUCT_FLOATS,
1532 OPTION_NO_CONSTRUCT_FLOATS,
1533 OPTION_FP64,
1534 OPTION_FPXX,
1535 OPTION_GP64,
1536 OPTION_RELAX_BRANCH,
1537 OPTION_NO_RELAX_BRANCH,
1538 OPTION_IGNORE_BRANCH_ISA,
1539 OPTION_NO_IGNORE_BRANCH_ISA,
1540 OPTION_INSN32,
1541 OPTION_NO_INSN32,
1542 OPTION_MSHARED,
1543 OPTION_MNO_SHARED,
1544 OPTION_MSYM32,
1545 OPTION_MNO_SYM32,
1546 OPTION_SOFT_FLOAT,
1547 OPTION_HARD_FLOAT,
1548 OPTION_SINGLE_FLOAT,
1549 OPTION_DOUBLE_FLOAT,
1550 OPTION_32,
1551 OPTION_CALL_SHARED,
1552 OPTION_CALL_NONPIC,
1553 OPTION_NON_SHARED,
1554 OPTION_XGOT,
1555 OPTION_MABI,
1556 OPTION_N32,
1557 OPTION_64,
1558 OPTION_MDEBUG,
1559 OPTION_NO_MDEBUG,
1560 OPTION_PDR,
1561 OPTION_NO_PDR,
1562 OPTION_MVXWORKS_PIC,
1563 OPTION_NAN,
1564 OPTION_ODD_SPREG,
1565 OPTION_NO_ODD_SPREG,
1566 OPTION_GINV,
1567 OPTION_NO_GINV,
1568 OPTION_LOONGSON_MMI,
1569 OPTION_NO_LOONGSON_MMI,
1570 OPTION_LOONGSON_CAM,
1571 OPTION_NO_LOONGSON_CAM,
1572 OPTION_LOONGSON_EXT,
1573 OPTION_NO_LOONGSON_EXT,
1574 OPTION_LOONGSON_EXT2,
1575 OPTION_NO_LOONGSON_EXT2,
1576 OPTION_END_OF_ENUM
1577 };
1578
1579 struct option md_longopts[] =
1580 {
1581 /* Options which specify architecture. */
1582 {"march", required_argument, NULL, OPTION_MARCH},
1583 {"mtune", required_argument, NULL, OPTION_MTUNE},
1584 {"mips0", no_argument, NULL, OPTION_MIPS1},
1585 {"mips1", no_argument, NULL, OPTION_MIPS1},
1586 {"mips2", no_argument, NULL, OPTION_MIPS2},
1587 {"mips3", no_argument, NULL, OPTION_MIPS3},
1588 {"mips4", no_argument, NULL, OPTION_MIPS4},
1589 {"mips5", no_argument, NULL, OPTION_MIPS5},
1590 {"mips32", no_argument, NULL, OPTION_MIPS32},
1591 {"mips64", no_argument, NULL, OPTION_MIPS64},
1592 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1593 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1594 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
1595 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
1596 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1597 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1598 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
1599 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
1600
1601 /* Options which specify Application Specific Extensions (ASEs). */
1602 {"mips16", no_argument, NULL, OPTION_MIPS16},
1603 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1604 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1605 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1606 {"mdmx", no_argument, NULL, OPTION_MDMX},
1607 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1608 {"mdsp", no_argument, NULL, OPTION_DSP},
1609 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1610 {"mmt", no_argument, NULL, OPTION_MT},
1611 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1612 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1613 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1614 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1615 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1616 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1617 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
1618 {"meva", no_argument, NULL, OPTION_EVA},
1619 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1620 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1621 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1622 {"mmcu", no_argument, NULL, OPTION_MCU},
1623 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1624 {"mvirt", no_argument, NULL, OPTION_VIRT},
1625 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1626 {"mmsa", no_argument, NULL, OPTION_MSA},
1627 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
1628 {"mxpa", no_argument, NULL, OPTION_XPA},
1629 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
1630 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1631 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
1632 {"mcrc", no_argument, NULL, OPTION_CRC},
1633 {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
1634 {"mginv", no_argument, NULL, OPTION_GINV},
1635 {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
1636 {"mloongson-mmi", no_argument, NULL, OPTION_LOONGSON_MMI},
1637 {"mno-loongson-mmi", no_argument, NULL, OPTION_NO_LOONGSON_MMI},
1638 {"mloongson-cam", no_argument, NULL, OPTION_LOONGSON_CAM},
1639 {"mno-loongson-cam", no_argument, NULL, OPTION_NO_LOONGSON_CAM},
1640 {"mloongson-ext", no_argument, NULL, OPTION_LOONGSON_EXT},
1641 {"mno-loongson-ext", no_argument, NULL, OPTION_NO_LOONGSON_EXT},
1642 {"mloongson-ext2", no_argument, NULL, OPTION_LOONGSON_EXT2},
1643 {"mno-loongson-ext2", no_argument, NULL, OPTION_NO_LOONGSON_EXT2},
1644
1645 /* Old-style architecture options. Don't add more of these. */
1646 {"m4650", no_argument, NULL, OPTION_M4650},
1647 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1648 {"m4010", no_argument, NULL, OPTION_M4010},
1649 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1650 {"m4100", no_argument, NULL, OPTION_M4100},
1651 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1652 {"m3900", no_argument, NULL, OPTION_M3900},
1653 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1654
1655 /* Options which enable bug fixes. */
1656 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1657 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1658 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1659 {"mfix-loongson3-llsc", no_argument, NULL, OPTION_FIX_LOONGSON3_LLSC},
1660 {"mno-fix-loongson3-llsc", no_argument, NULL, OPTION_NO_FIX_LOONGSON3_LLSC},
1661 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1662 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1663 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1664 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1665 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1666 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1667 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1668 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1669 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1670 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1671 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1672 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
1673 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1674 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1675 {"mfix-r5900", no_argument, NULL, OPTION_FIX_R5900},
1676 {"mno-fix-r5900", no_argument, NULL, OPTION_NO_FIX_R5900},
1677
1678 /* Miscellaneous options. */
1679 {"trap", no_argument, NULL, OPTION_TRAP},
1680 {"no-break", no_argument, NULL, OPTION_TRAP},
1681 {"break", no_argument, NULL, OPTION_BREAK},
1682 {"no-trap", no_argument, NULL, OPTION_BREAK},
1683 {"EB", no_argument, NULL, OPTION_EB},
1684 {"EL", no_argument, NULL, OPTION_EL},
1685 {"mfp32", no_argument, NULL, OPTION_FP32},
1686 {"mgp32", no_argument, NULL, OPTION_GP32},
1687 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1688 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1689 {"mfp64", no_argument, NULL, OPTION_FP64},
1690 {"mfpxx", no_argument, NULL, OPTION_FPXX},
1691 {"mgp64", no_argument, NULL, OPTION_GP64},
1692 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1693 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
1694 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1695 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
1696 {"minsn32", no_argument, NULL, OPTION_INSN32},
1697 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
1698 {"mshared", no_argument, NULL, OPTION_MSHARED},
1699 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1700 {"msym32", no_argument, NULL, OPTION_MSYM32},
1701 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1702 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1703 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1704 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1705 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1706 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1707 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
1708
1709 /* Strictly speaking this next option is ELF specific,
1710 but we allow it for other ports as well in order to
1711 make testing easier. */
1712 {"32", no_argument, NULL, OPTION_32},
1713
1714 /* ELF-specific options. */
1715 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1716 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1717 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1718 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1719 {"xgot", no_argument, NULL, OPTION_XGOT},
1720 {"mabi", required_argument, NULL, OPTION_MABI},
1721 {"n32", no_argument, NULL, OPTION_N32},
1722 {"64", no_argument, NULL, OPTION_64},
1723 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1724 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1725 {"mpdr", no_argument, NULL, OPTION_PDR},
1726 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1727 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
1728 {"mnan", required_argument, NULL, OPTION_NAN},
1729
1730 {NULL, no_argument, NULL, 0}
1731 };
1732 size_t md_longopts_size = sizeof (md_longopts);
1733 \f
1734 /* Information about either an Application Specific Extension or an
1735 optional architecture feature that, for simplicity, we treat in the
1736 same way as an ASE. */
1737 struct mips_ase
1738 {
1739 /* The name of the ASE, used in both the command-line and .set options. */
1740 const char *name;
1741
1742 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1743 and 64-bit architectures, the flags here refer to the subset that
1744 is available on both. */
1745 unsigned int flags;
1746
1747 /* The ASE_* flag used for instructions that are available on 64-bit
1748 architectures but that are not included in FLAGS. */
1749 unsigned int flags64;
1750
1751 /* The command-line options that turn the ASE on and off. */
1752 int option_on;
1753 int option_off;
1754
1755 /* The minimum required architecture revisions for MIPS32, MIPS64,
1756 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1757 int mips32_rev;
1758 int mips64_rev;
1759 int micromips32_rev;
1760 int micromips64_rev;
1761
1762 /* The architecture where the ASE was removed or -1 if the extension has not
1763 been removed. */
1764 int rem_rev;
1765 };
1766
1767 /* A table of all supported ASEs. */
1768 static const struct mips_ase mips_ases[] = {
1769 { "dsp", ASE_DSP, ASE_DSP64,
1770 OPTION_DSP, OPTION_NO_DSP,
1771 2, 2, 2, 2,
1772 -1 },
1773
1774 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1775 OPTION_DSPR2, OPTION_NO_DSPR2,
1776 2, 2, 2, 2,
1777 -1 },
1778
1779 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1780 OPTION_DSPR3, OPTION_NO_DSPR3,
1781 6, 6, -1, -1,
1782 -1 },
1783
1784 { "eva", ASE_EVA, 0,
1785 OPTION_EVA, OPTION_NO_EVA,
1786 2, 2, 2, 2,
1787 -1 },
1788
1789 { "mcu", ASE_MCU, 0,
1790 OPTION_MCU, OPTION_NO_MCU,
1791 2, 2, 2, 2,
1792 -1 },
1793
1794 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1795 { "mdmx", ASE_MDMX, 0,
1796 OPTION_MDMX, OPTION_NO_MDMX,
1797 -1, 1, -1, -1,
1798 6 },
1799
1800 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1801 { "mips3d", ASE_MIPS3D, 0,
1802 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1803 2, 1, -1, -1,
1804 6 },
1805
1806 { "mt", ASE_MT, 0,
1807 OPTION_MT, OPTION_NO_MT,
1808 2, 2, -1, -1,
1809 -1 },
1810
1811 { "smartmips", ASE_SMARTMIPS, 0,
1812 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1813 1, -1, -1, -1,
1814 6 },
1815
1816 { "virt", ASE_VIRT, ASE_VIRT64,
1817 OPTION_VIRT, OPTION_NO_VIRT,
1818 2, 2, 2, 2,
1819 -1 },
1820
1821 { "msa", ASE_MSA, ASE_MSA64,
1822 OPTION_MSA, OPTION_NO_MSA,
1823 2, 2, 2, 2,
1824 -1 },
1825
1826 { "xpa", ASE_XPA, 0,
1827 OPTION_XPA, OPTION_NO_XPA,
1828 2, 2, 2, 2,
1829 -1 },
1830
1831 { "mips16e2", ASE_MIPS16E2, 0,
1832 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1833 2, 2, -1, -1,
1834 6 },
1835
1836 { "crc", ASE_CRC, ASE_CRC64,
1837 OPTION_CRC, OPTION_NO_CRC,
1838 6, 6, -1, -1,
1839 -1 },
1840
1841 { "ginv", ASE_GINV, 0,
1842 OPTION_GINV, OPTION_NO_GINV,
1843 6, 6, 6, 6,
1844 -1 },
1845
1846 { "loongson-mmi", ASE_LOONGSON_MMI, 0,
1847 OPTION_LOONGSON_MMI, OPTION_NO_LOONGSON_MMI,
1848 0, 0, -1, -1,
1849 -1 },
1850
1851 { "loongson-cam", ASE_LOONGSON_CAM, 0,
1852 OPTION_LOONGSON_CAM, OPTION_NO_LOONGSON_CAM,
1853 0, 0, -1, -1,
1854 -1 },
1855
1856 { "loongson-ext", ASE_LOONGSON_EXT, 0,
1857 OPTION_LOONGSON_EXT, OPTION_NO_LOONGSON_EXT,
1858 0, 0, -1, -1,
1859 -1 },
1860
1861 { "loongson-ext2", ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2, 0,
1862 OPTION_LOONGSON_EXT2, OPTION_NO_LOONGSON_EXT2,
1863 0, 0, -1, -1,
1864 -1 },
1865 };
1866
1867 /* The set of ASEs that require -mfp64. */
1868 #define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
1869
1870 /* Groups of ASE_* flags that represent different revisions of an ASE. */
1871 static const unsigned int mips_ase_groups[] = {
1872 ASE_DSP | ASE_DSPR2 | ASE_DSPR3,
1873 ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2
1874 };
1875 \f
1876 /* Pseudo-op table.
1877
1878 The following pseudo-ops from the Kane and Heinrich MIPS book
1879 should be defined here, but are currently unsupported: .alias,
1880 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1881
1882 The following pseudo-ops from the Kane and Heinrich MIPS book are
1883 specific to the type of debugging information being generated, and
1884 should be defined by the object format: .aent, .begin, .bend,
1885 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1886 .vreg.
1887
1888 The following pseudo-ops from the Kane and Heinrich MIPS book are
1889 not MIPS CPU specific, but are also not specific to the object file
1890 format. This file is probably the best place to define them, but
1891 they are not currently supported: .asm0, .endr, .lab, .struct. */
1892
1893 static const pseudo_typeS mips_pseudo_table[] =
1894 {
1895 /* MIPS specific pseudo-ops. */
1896 {"option", s_option, 0},
1897 {"set", s_mipsset, 0},
1898 {"rdata", s_change_sec, 'r'},
1899 {"sdata", s_change_sec, 's'},
1900 {"livereg", s_ignore, 0},
1901 {"abicalls", s_abicalls, 0},
1902 {"cpload", s_cpload, 0},
1903 {"cpsetup", s_cpsetup, 0},
1904 {"cplocal", s_cplocal, 0},
1905 {"cprestore", s_cprestore, 0},
1906 {"cpreturn", s_cpreturn, 0},
1907 {"dtprelword", s_dtprelword, 0},
1908 {"dtpreldword", s_dtpreldword, 0},
1909 {"tprelword", s_tprelword, 0},
1910 {"tpreldword", s_tpreldword, 0},
1911 {"gpvalue", s_gpvalue, 0},
1912 {"gpword", s_gpword, 0},
1913 {"gpdword", s_gpdword, 0},
1914 {"ehword", s_ehword, 0},
1915 {"cpadd", s_cpadd, 0},
1916 {"insn", s_insn, 0},
1917 {"nan", s_nan, 0},
1918 {"module", s_module, 0},
1919
1920 /* Relatively generic pseudo-ops that happen to be used on MIPS
1921 chips. */
1922 {"asciiz", stringer, 8 + 1},
1923 {"bss", s_change_sec, 'b'},
1924 {"err", s_err, 0},
1925 {"half", s_cons, 1},
1926 {"dword", s_cons, 3},
1927 {"weakext", s_mips_weakext, 0},
1928 {"origin", s_org, 0},
1929 {"repeat", s_rept, 0},
1930
1931 /* For MIPS this is non-standard, but we define it for consistency. */
1932 {"sbss", s_change_sec, 'B'},
1933
1934 /* These pseudo-ops are defined in read.c, but must be overridden
1935 here for one reason or another. */
1936 {"align", s_align, 0},
1937 {"byte", s_cons, 0},
1938 {"data", s_change_sec, 'd'},
1939 {"double", s_float_cons, 'd'},
1940 {"float", s_float_cons, 'f'},
1941 {"globl", s_mips_globl, 0},
1942 {"global", s_mips_globl, 0},
1943 {"hword", s_cons, 1},
1944 {"int", s_cons, 2},
1945 {"long", s_cons, 2},
1946 {"octa", s_cons, 4},
1947 {"quad", s_cons, 3},
1948 {"section", s_change_section, 0},
1949 {"short", s_cons, 1},
1950 {"single", s_float_cons, 'f'},
1951 {"stabd", s_mips_stab, 'd'},
1952 {"stabn", s_mips_stab, 'n'},
1953 {"stabs", s_mips_stab, 's'},
1954 {"text", s_change_sec, 't'},
1955 {"word", s_cons, 2},
1956
1957 { "extern", ecoff_directive_extern, 0},
1958
1959 { NULL, NULL, 0 },
1960 };
1961
1962 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1963 {
1964 /* These pseudo-ops should be defined by the object file format.
1965 However, a.out doesn't support them, so we have versions here. */
1966 {"aent", s_mips_ent, 1},
1967 {"bgnb", s_ignore, 0},
1968 {"end", s_mips_end, 0},
1969 {"endb", s_ignore, 0},
1970 {"ent", s_mips_ent, 0},
1971 {"file", s_mips_file, 0},
1972 {"fmask", s_mips_mask, 'F'},
1973 {"frame", s_mips_frame, 0},
1974 {"loc", s_mips_loc, 0},
1975 {"mask", s_mips_mask, 'R'},
1976 {"verstamp", s_ignore, 0},
1977 { NULL, NULL, 0 },
1978 };
1979
1980 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1981 purpose of the `.dc.a' internal pseudo-op. */
1982
1983 int
1984 mips_address_bytes (void)
1985 {
1986 file_mips_check_options ();
1987 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1988 }
1989
1990 extern void pop_insert (const pseudo_typeS *);
1991
1992 void
1993 mips_pop_insert (void)
1994 {
1995 pop_insert (mips_pseudo_table);
1996 if (! ECOFF_DEBUGGING)
1997 pop_insert (mips_nonecoff_pseudo_table);
1998 }
1999 \f
2000 /* Symbols labelling the current insn. */
2001
2002 struct insn_label_list
2003 {
2004 struct insn_label_list *next;
2005 symbolS *label;
2006 };
2007
2008 static struct insn_label_list *free_insn_labels;
2009 #define label_list tc_segment_info_data.labels
2010
2011 static void mips_clear_insn_labels (void);
2012 static void mips_mark_labels (void);
2013 static void mips_compressed_mark_labels (void);
2014
2015 static inline void
2016 mips_clear_insn_labels (void)
2017 {
2018 struct insn_label_list **pl;
2019 segment_info_type *si;
2020
2021 if (now_seg)
2022 {
2023 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
2024 ;
2025
2026 si = seg_info (now_seg);
2027 *pl = si->label_list;
2028 si->label_list = NULL;
2029 }
2030 }
2031
2032 /* Mark instruction labels in MIPS16/microMIPS mode. */
2033
2034 static inline void
2035 mips_mark_labels (void)
2036 {
2037 if (HAVE_CODE_COMPRESSION)
2038 mips_compressed_mark_labels ();
2039 }
2040 \f
2041 static char *expr_end;
2042
2043 /* An expression in a macro instruction. This is set by mips_ip and
2044 mips16_ip and when populated is always an O_constant. */
2045
2046 static expressionS imm_expr;
2047
2048 /* The relocatable field in an instruction and the relocs associated
2049 with it. These variables are used for instructions like LUI and
2050 JAL as well as true offsets. They are also used for address
2051 operands in macros. */
2052
2053 static expressionS offset_expr;
2054 static bfd_reloc_code_real_type offset_reloc[3]
2055 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2056
2057 /* This is set to the resulting size of the instruction to be produced
2058 by mips16_ip if an explicit extension is used or by mips_ip if an
2059 explicit size is supplied. */
2060
2061 static unsigned int forced_insn_length;
2062
2063 /* True if we are assembling an instruction. All dot symbols defined during
2064 this time should be treated as code labels. */
2065
2066 static bfd_boolean mips_assembling_insn;
2067
2068 /* The pdr segment for per procedure frame/regmask info. Not used for
2069 ECOFF debugging. */
2070
2071 static segT pdr_seg;
2072
2073 /* The default target format to use. */
2074
2075 #if defined (TE_FreeBSD)
2076 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2077 #elif defined (TE_TMIPS)
2078 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2079 #else
2080 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2081 #endif
2082
2083 const char *
2084 mips_target_format (void)
2085 {
2086 switch (OUTPUT_FLAVOR)
2087 {
2088 case bfd_target_elf_flavour:
2089 #ifdef TE_VXWORKS
2090 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2091 return (target_big_endian
2092 ? "elf32-bigmips-vxworks"
2093 : "elf32-littlemips-vxworks");
2094 #endif
2095 return (target_big_endian
2096 ? (HAVE_64BIT_OBJECTS
2097 ? ELF_TARGET ("elf64-", "big")
2098 : (HAVE_NEWABI
2099 ? ELF_TARGET ("elf32-n", "big")
2100 : ELF_TARGET ("elf32-", "big")))
2101 : (HAVE_64BIT_OBJECTS
2102 ? ELF_TARGET ("elf64-", "little")
2103 : (HAVE_NEWABI
2104 ? ELF_TARGET ("elf32-n", "little")
2105 : ELF_TARGET ("elf32-", "little"))));
2106 default:
2107 abort ();
2108 return NULL;
2109 }
2110 }
2111
2112 /* Return the ISA revision that is currently in use, or 0 if we are
2113 generating code for MIPS V or below. */
2114
2115 static int
2116 mips_isa_rev (void)
2117 {
2118 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2119 return 2;
2120
2121 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2122 return 3;
2123
2124 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2125 return 5;
2126
2127 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2128 return 6;
2129
2130 /* microMIPS implies revision 2 or above. */
2131 if (mips_opts.micromips)
2132 return 2;
2133
2134 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2135 return 1;
2136
2137 return 0;
2138 }
2139
2140 /* Return the mask of all ASEs that are revisions of those in FLAGS. */
2141
2142 static unsigned int
2143 mips_ase_mask (unsigned int flags)
2144 {
2145 unsigned int i;
2146
2147 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2148 if (flags & mips_ase_groups[i])
2149 flags |= mips_ase_groups[i];
2150 return flags;
2151 }
2152
2153 /* Check whether the current ISA supports ASE. Issue a warning if
2154 appropriate. */
2155
2156 static void
2157 mips_check_isa_supports_ase (const struct mips_ase *ase)
2158 {
2159 const char *base;
2160 int min_rev, size;
2161 static unsigned int warned_isa;
2162 static unsigned int warned_fp32;
2163
2164 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2165 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2166 else
2167 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2168 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2169 && (warned_isa & ase->flags) != ase->flags)
2170 {
2171 warned_isa |= ase->flags;
2172 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2173 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2174 if (min_rev < 0)
2175 as_warn (_("the %d-bit %s architecture does not support the"
2176 " `%s' extension"), size, base, ase->name);
2177 else
2178 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
2179 ase->name, base, size, min_rev);
2180 }
2181 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2182 && (warned_isa & ase->flags) != ase->flags)
2183 {
2184 warned_isa |= ase->flags;
2185 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2186 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2187 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2188 ase->name, base, size, ase->rem_rev);
2189 }
2190
2191 if ((ase->flags & FP64_ASES)
2192 && mips_opts.fp != 64
2193 && (warned_fp32 & ase->flags) != ase->flags)
2194 {
2195 warned_fp32 |= ase->flags;
2196 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
2197 }
2198 }
2199
2200 /* Check all enabled ASEs to see whether they are supported by the
2201 chosen architecture. */
2202
2203 static void
2204 mips_check_isa_supports_ases (void)
2205 {
2206 unsigned int i, mask;
2207
2208 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2209 {
2210 mask = mips_ase_mask (mips_ases[i].flags);
2211 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2212 mips_check_isa_supports_ase (&mips_ases[i]);
2213 }
2214 }
2215
2216 /* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2217 that were affected. */
2218
2219 static unsigned int
2220 mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2221 bfd_boolean enabled_p)
2222 {
2223 unsigned int mask;
2224
2225 mask = mips_ase_mask (ase->flags);
2226 opts->ase &= ~mask;
2227
2228 /* Clear combination ASE flags, which need to be recalculated based on
2229 updated regular ASE settings. */
2230 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT | ASE_EVA_R6);
2231
2232 if (enabled_p)
2233 opts->ase |= ase->flags;
2234
2235 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2236 instructions which are only valid when both ASEs are enabled.
2237 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2238 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2239 {
2240 opts->ase |= ASE_XPA_VIRT;
2241 mask |= ASE_XPA_VIRT;
2242 }
2243 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2244 {
2245 opts->ase |= ASE_MIPS16E2_MT;
2246 mask |= ASE_MIPS16E2_MT;
2247 }
2248
2249 /* The EVA Extension has instructions which are only valid when the R6 ISA
2250 is enabled. This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
2251 present. */
2252 if (((opts->ase & ASE_EVA) != 0) && ISA_IS_R6 (opts->isa))
2253 {
2254 opts->ase |= ASE_EVA_R6;
2255 mask |= ASE_EVA_R6;
2256 }
2257
2258 return mask;
2259 }
2260
2261 /* Return the ASE called NAME, or null if none. */
2262
2263 static const struct mips_ase *
2264 mips_lookup_ase (const char *name)
2265 {
2266 unsigned int i;
2267
2268 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2269 if (strcmp (name, mips_ases[i].name) == 0)
2270 return &mips_ases[i];
2271 return NULL;
2272 }
2273
2274 /* Return the length of a microMIPS instruction in bytes. If bits of
2275 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2276 otherwise it is a 32-bit instruction. */
2277
2278 static inline unsigned int
2279 micromips_insn_length (const struct mips_opcode *mo)
2280 {
2281 return mips_opcode_32bit_p (mo) ? 4 : 2;
2282 }
2283
2284 /* Return the length of MIPS16 instruction OPCODE. */
2285
2286 static inline unsigned int
2287 mips16_opcode_length (unsigned long opcode)
2288 {
2289 return (opcode >> 16) == 0 ? 2 : 4;
2290 }
2291
2292 /* Return the length of instruction INSN. */
2293
2294 static inline unsigned int
2295 insn_length (const struct mips_cl_insn *insn)
2296 {
2297 if (mips_opts.micromips)
2298 return micromips_insn_length (insn->insn_mo);
2299 else if (mips_opts.mips16)
2300 return mips16_opcode_length (insn->insn_opcode);
2301 else
2302 return 4;
2303 }
2304
2305 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2306
2307 static void
2308 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2309 {
2310 size_t i;
2311
2312 insn->insn_mo = mo;
2313 insn->insn_opcode = mo->match;
2314 insn->frag = NULL;
2315 insn->where = 0;
2316 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2317 insn->fixp[i] = NULL;
2318 insn->fixed_p = (mips_opts.noreorder > 0);
2319 insn->noreorder_p = (mips_opts.noreorder > 0);
2320 insn->mips16_absolute_jump_p = 0;
2321 insn->complete_p = 0;
2322 insn->cleared_p = 0;
2323 }
2324
2325 /* Get a list of all the operands in INSN. */
2326
2327 static const struct mips_operand_array *
2328 insn_operands (const struct mips_cl_insn *insn)
2329 {
2330 if (insn->insn_mo >= &mips_opcodes[0]
2331 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2332 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2333
2334 if (insn->insn_mo >= &mips16_opcodes[0]
2335 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2336 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2337
2338 if (insn->insn_mo >= &micromips_opcodes[0]
2339 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2340 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2341
2342 abort ();
2343 }
2344
2345 /* Get a description of operand OPNO of INSN. */
2346
2347 static const struct mips_operand *
2348 insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2349 {
2350 const struct mips_operand_array *operands;
2351
2352 operands = insn_operands (insn);
2353 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2354 abort ();
2355 return operands->operand[opno];
2356 }
2357
2358 /* Install UVAL as the value of OPERAND in INSN. */
2359
2360 static inline void
2361 insn_insert_operand (struct mips_cl_insn *insn,
2362 const struct mips_operand *operand, unsigned int uval)
2363 {
2364 if (mips_opts.mips16
2365 && operand->type == OP_INT && operand->lsb == 0
2366 && mips_opcode_32bit_p (insn->insn_mo))
2367 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2368 else
2369 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2370 }
2371
2372 /* Extract the value of OPERAND from INSN. */
2373
2374 static inline unsigned
2375 insn_extract_operand (const struct mips_cl_insn *insn,
2376 const struct mips_operand *operand)
2377 {
2378 return mips_extract_operand (operand, insn->insn_opcode);
2379 }
2380
2381 /* Record the current MIPS16/microMIPS mode in now_seg. */
2382
2383 static void
2384 mips_record_compressed_mode (void)
2385 {
2386 segment_info_type *si;
2387
2388 si = seg_info (now_seg);
2389 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2390 si->tc_segment_info_data.mips16 = mips_opts.mips16;
2391 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2392 si->tc_segment_info_data.micromips = mips_opts.micromips;
2393 }
2394
2395 /* Read a standard MIPS instruction from BUF. */
2396
2397 static unsigned long
2398 read_insn (char *buf)
2399 {
2400 if (target_big_endian)
2401 return bfd_getb32 ((bfd_byte *) buf);
2402 else
2403 return bfd_getl32 ((bfd_byte *) buf);
2404 }
2405
2406 /* Write standard MIPS instruction INSN to BUF. Return a pointer to
2407 the next byte. */
2408
2409 static char *
2410 write_insn (char *buf, unsigned int insn)
2411 {
2412 md_number_to_chars (buf, insn, 4);
2413 return buf + 4;
2414 }
2415
2416 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
2417 has length LENGTH. */
2418
2419 static unsigned long
2420 read_compressed_insn (char *buf, unsigned int length)
2421 {
2422 unsigned long insn;
2423 unsigned int i;
2424
2425 insn = 0;
2426 for (i = 0; i < length; i += 2)
2427 {
2428 insn <<= 16;
2429 if (target_big_endian)
2430 insn |= bfd_getb16 ((char *) buf);
2431 else
2432 insn |= bfd_getl16 ((char *) buf);
2433 buf += 2;
2434 }
2435 return insn;
2436 }
2437
2438 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2439 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2440
2441 static char *
2442 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2443 {
2444 unsigned int i;
2445
2446 for (i = 0; i < length; i += 2)
2447 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2448 return buf + length;
2449 }
2450
2451 /* Install INSN at the location specified by its "frag" and "where" fields. */
2452
2453 static void
2454 install_insn (const struct mips_cl_insn *insn)
2455 {
2456 char *f = insn->frag->fr_literal + insn->where;
2457 if (HAVE_CODE_COMPRESSION)
2458 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
2459 else
2460 write_insn (f, insn->insn_opcode);
2461 mips_record_compressed_mode ();
2462 }
2463
2464 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2465 and install the opcode in the new location. */
2466
2467 static void
2468 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2469 {
2470 size_t i;
2471
2472 insn->frag = frag;
2473 insn->where = where;
2474 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2475 if (insn->fixp[i] != NULL)
2476 {
2477 insn->fixp[i]->fx_frag = frag;
2478 insn->fixp[i]->fx_where = where;
2479 }
2480 install_insn (insn);
2481 }
2482
2483 /* Add INSN to the end of the output. */
2484
2485 static void
2486 add_fixed_insn (struct mips_cl_insn *insn)
2487 {
2488 char *f = frag_more (insn_length (insn));
2489 move_insn (insn, frag_now, f - frag_now->fr_literal);
2490 }
2491
2492 /* Start a variant frag and move INSN to the start of the variant part,
2493 marking it as fixed. The other arguments are as for frag_var. */
2494
2495 static void
2496 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2497 relax_substateT subtype, symbolS *symbol, offsetT offset)
2498 {
2499 frag_grow (max_chars);
2500 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2501 insn->fixed_p = 1;
2502 frag_var (rs_machine_dependent, max_chars, var,
2503 subtype, symbol, offset, NULL);
2504 }
2505
2506 /* Insert N copies of INSN into the history buffer, starting at
2507 position FIRST. Neither FIRST nor N need to be clipped. */
2508
2509 static void
2510 insert_into_history (unsigned int first, unsigned int n,
2511 const struct mips_cl_insn *insn)
2512 {
2513 if (mips_relax.sequence != 2)
2514 {
2515 unsigned int i;
2516
2517 for (i = ARRAY_SIZE (history); i-- > first;)
2518 if (i >= first + n)
2519 history[i] = history[i - n];
2520 else
2521 history[i] = *insn;
2522 }
2523 }
2524
2525 /* Clear the error in insn_error. */
2526
2527 static void
2528 clear_insn_error (void)
2529 {
2530 memset (&insn_error, 0, sizeof (insn_error));
2531 }
2532
2533 /* Possibly record error message MSG for the current instruction.
2534 If the error is about a particular argument, ARGNUM is the 1-based
2535 number of that argument, otherwise it is 0. FORMAT is the format
2536 of MSG. Return true if MSG was used, false if the current message
2537 was kept. */
2538
2539 static bfd_boolean
2540 set_insn_error_format (int argnum, enum mips_insn_error_format format,
2541 const char *msg)
2542 {
2543 if (argnum == 0)
2544 {
2545 /* Give priority to errors against specific arguments, and to
2546 the first whole-instruction message. */
2547 if (insn_error.msg)
2548 return FALSE;
2549 }
2550 else
2551 {
2552 /* Keep insn_error if it is against a later argument. */
2553 if (argnum < insn_error.min_argnum)
2554 return FALSE;
2555
2556 /* If both errors are against the same argument but are different,
2557 give up on reporting a specific error for this argument.
2558 See the comment about mips_insn_error for details. */
2559 if (argnum == insn_error.min_argnum
2560 && insn_error.msg
2561 && strcmp (insn_error.msg, msg) != 0)
2562 {
2563 insn_error.msg = 0;
2564 insn_error.min_argnum += 1;
2565 return FALSE;
2566 }
2567 }
2568 insn_error.min_argnum = argnum;
2569 insn_error.format = format;
2570 insn_error.msg = msg;
2571 return TRUE;
2572 }
2573
2574 /* Record an instruction error with no % format fields. ARGNUM and MSG are
2575 as for set_insn_error_format. */
2576
2577 static void
2578 set_insn_error (int argnum, const char *msg)
2579 {
2580 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2581 }
2582
2583 /* Record an instruction error with one %d field I. ARGNUM and MSG are
2584 as for set_insn_error_format. */
2585
2586 static void
2587 set_insn_error_i (int argnum, const char *msg, int i)
2588 {
2589 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2590 insn_error.u.i = i;
2591 }
2592
2593 /* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2594 are as for set_insn_error_format. */
2595
2596 static void
2597 set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2598 {
2599 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2600 {
2601 insn_error.u.ss[0] = s1;
2602 insn_error.u.ss[1] = s2;
2603 }
2604 }
2605
2606 /* Report the error in insn_error, which is against assembly code STR. */
2607
2608 static void
2609 report_insn_error (const char *str)
2610 {
2611 const char *msg = concat (insn_error.msg, " `%s'", NULL);
2612
2613 switch (insn_error.format)
2614 {
2615 case ERR_FMT_PLAIN:
2616 as_bad (msg, str);
2617 break;
2618
2619 case ERR_FMT_I:
2620 as_bad (msg, insn_error.u.i, str);
2621 break;
2622
2623 case ERR_FMT_SS:
2624 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2625 break;
2626 }
2627
2628 free ((char *) msg);
2629 }
2630
2631 /* Initialize vr4120_conflicts. There is a bit of duplication here:
2632 the idea is to make it obvious at a glance that each errata is
2633 included. */
2634
2635 static void
2636 init_vr4120_conflicts (void)
2637 {
2638 #define CONFLICT(FIRST, SECOND) \
2639 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2640
2641 /* Errata 21 - [D]DIV[U] after [D]MACC */
2642 CONFLICT (MACC, DIV);
2643 CONFLICT (DMACC, DIV);
2644
2645 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2646 CONFLICT (DMULT, DMULT);
2647 CONFLICT (DMULT, DMACC);
2648 CONFLICT (DMACC, DMULT);
2649 CONFLICT (DMACC, DMACC);
2650
2651 /* Errata 24 - MT{LO,HI} after [D]MACC */
2652 CONFLICT (MACC, MTHILO);
2653 CONFLICT (DMACC, MTHILO);
2654
2655 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2656 instruction is executed immediately after a MACC or DMACC
2657 instruction, the result of [either instruction] is incorrect." */
2658 CONFLICT (MACC, MULT);
2659 CONFLICT (MACC, DMULT);
2660 CONFLICT (DMACC, MULT);
2661 CONFLICT (DMACC, DMULT);
2662
2663 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2664 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2665 DDIV or DDIVU instruction, the result of the MACC or
2666 DMACC instruction is incorrect.". */
2667 CONFLICT (DMULT, MACC);
2668 CONFLICT (DMULT, DMACC);
2669 CONFLICT (DIV, MACC);
2670 CONFLICT (DIV, DMACC);
2671
2672 #undef CONFLICT
2673 }
2674
2675 struct regname {
2676 const char *name;
2677 unsigned int num;
2678 };
2679
2680 #define RNUM_MASK 0x00000ff
2681 #define RTYPE_MASK 0x0ffff00
2682 #define RTYPE_NUM 0x0000100
2683 #define RTYPE_FPU 0x0000200
2684 #define RTYPE_FCC 0x0000400
2685 #define RTYPE_VEC 0x0000800
2686 #define RTYPE_GP 0x0001000
2687 #define RTYPE_CP0 0x0002000
2688 #define RTYPE_PC 0x0004000
2689 #define RTYPE_ACC 0x0008000
2690 #define RTYPE_CCC 0x0010000
2691 #define RTYPE_VI 0x0020000
2692 #define RTYPE_VF 0x0040000
2693 #define RTYPE_R5900_I 0x0080000
2694 #define RTYPE_R5900_Q 0x0100000
2695 #define RTYPE_R5900_R 0x0200000
2696 #define RTYPE_R5900_ACC 0x0400000
2697 #define RTYPE_MSA 0x0800000
2698 #define RWARN 0x8000000
2699
2700 #define GENERIC_REGISTER_NUMBERS \
2701 {"$0", RTYPE_NUM | 0}, \
2702 {"$1", RTYPE_NUM | 1}, \
2703 {"$2", RTYPE_NUM | 2}, \
2704 {"$3", RTYPE_NUM | 3}, \
2705 {"$4", RTYPE_NUM | 4}, \
2706 {"$5", RTYPE_NUM | 5}, \
2707 {"$6", RTYPE_NUM | 6}, \
2708 {"$7", RTYPE_NUM | 7}, \
2709 {"$8", RTYPE_NUM | 8}, \
2710 {"$9", RTYPE_NUM | 9}, \
2711 {"$10", RTYPE_NUM | 10}, \
2712 {"$11", RTYPE_NUM | 11}, \
2713 {"$12", RTYPE_NUM | 12}, \
2714 {"$13", RTYPE_NUM | 13}, \
2715 {"$14", RTYPE_NUM | 14}, \
2716 {"$15", RTYPE_NUM | 15}, \
2717 {"$16", RTYPE_NUM | 16}, \
2718 {"$17", RTYPE_NUM | 17}, \
2719 {"$18", RTYPE_NUM | 18}, \
2720 {"$19", RTYPE_NUM | 19}, \
2721 {"$20", RTYPE_NUM | 20}, \
2722 {"$21", RTYPE_NUM | 21}, \
2723 {"$22", RTYPE_NUM | 22}, \
2724 {"$23", RTYPE_NUM | 23}, \
2725 {"$24", RTYPE_NUM | 24}, \
2726 {"$25", RTYPE_NUM | 25}, \
2727 {"$26", RTYPE_NUM | 26}, \
2728 {"$27", RTYPE_NUM | 27}, \
2729 {"$28", RTYPE_NUM | 28}, \
2730 {"$29", RTYPE_NUM | 29}, \
2731 {"$30", RTYPE_NUM | 30}, \
2732 {"$31", RTYPE_NUM | 31}
2733
2734 #define FPU_REGISTER_NAMES \
2735 {"$f0", RTYPE_FPU | 0}, \
2736 {"$f1", RTYPE_FPU | 1}, \
2737 {"$f2", RTYPE_FPU | 2}, \
2738 {"$f3", RTYPE_FPU | 3}, \
2739 {"$f4", RTYPE_FPU | 4}, \
2740 {"$f5", RTYPE_FPU | 5}, \
2741 {"$f6", RTYPE_FPU | 6}, \
2742 {"$f7", RTYPE_FPU | 7}, \
2743 {"$f8", RTYPE_FPU | 8}, \
2744 {"$f9", RTYPE_FPU | 9}, \
2745 {"$f10", RTYPE_FPU | 10}, \
2746 {"$f11", RTYPE_FPU | 11}, \
2747 {"$f12", RTYPE_FPU | 12}, \
2748 {"$f13", RTYPE_FPU | 13}, \
2749 {"$f14", RTYPE_FPU | 14}, \
2750 {"$f15", RTYPE_FPU | 15}, \
2751 {"$f16", RTYPE_FPU | 16}, \
2752 {"$f17", RTYPE_FPU | 17}, \
2753 {"$f18", RTYPE_FPU | 18}, \
2754 {"$f19", RTYPE_FPU | 19}, \
2755 {"$f20", RTYPE_FPU | 20}, \
2756 {"$f21", RTYPE_FPU | 21}, \
2757 {"$f22", RTYPE_FPU | 22}, \
2758 {"$f23", RTYPE_FPU | 23}, \
2759 {"$f24", RTYPE_FPU | 24}, \
2760 {"$f25", RTYPE_FPU | 25}, \
2761 {"$f26", RTYPE_FPU | 26}, \
2762 {"$f27", RTYPE_FPU | 27}, \
2763 {"$f28", RTYPE_FPU | 28}, \
2764 {"$f29", RTYPE_FPU | 29}, \
2765 {"$f30", RTYPE_FPU | 30}, \
2766 {"$f31", RTYPE_FPU | 31}
2767
2768 #define FPU_CONDITION_CODE_NAMES \
2769 {"$fcc0", RTYPE_FCC | 0}, \
2770 {"$fcc1", RTYPE_FCC | 1}, \
2771 {"$fcc2", RTYPE_FCC | 2}, \
2772 {"$fcc3", RTYPE_FCC | 3}, \
2773 {"$fcc4", RTYPE_FCC | 4}, \
2774 {"$fcc5", RTYPE_FCC | 5}, \
2775 {"$fcc6", RTYPE_FCC | 6}, \
2776 {"$fcc7", RTYPE_FCC | 7}
2777
2778 #define COPROC_CONDITION_CODE_NAMES \
2779 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2780 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2781 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2782 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2783 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2784 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2785 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2786 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2787
2788 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2789 {"$a4", RTYPE_GP | 8}, \
2790 {"$a5", RTYPE_GP | 9}, \
2791 {"$a6", RTYPE_GP | 10}, \
2792 {"$a7", RTYPE_GP | 11}, \
2793 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2794 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2795 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2796 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2797 {"$t0", RTYPE_GP | 12}, \
2798 {"$t1", RTYPE_GP | 13}, \
2799 {"$t2", RTYPE_GP | 14}, \
2800 {"$t3", RTYPE_GP | 15}
2801
2802 #define O32_SYMBOLIC_REGISTER_NAMES \
2803 {"$t0", RTYPE_GP | 8}, \
2804 {"$t1", RTYPE_GP | 9}, \
2805 {"$t2", RTYPE_GP | 10}, \
2806 {"$t3", RTYPE_GP | 11}, \
2807 {"$t4", RTYPE_GP | 12}, \
2808 {"$t5", RTYPE_GP | 13}, \
2809 {"$t6", RTYPE_GP | 14}, \
2810 {"$t7", RTYPE_GP | 15}, \
2811 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2812 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2813 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2814 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2815
2816 /* Remaining symbolic register names. */
2817 #define SYMBOLIC_REGISTER_NAMES \
2818 {"$zero", RTYPE_GP | 0}, \
2819 {"$at", RTYPE_GP | 1}, \
2820 {"$AT", RTYPE_GP | 1}, \
2821 {"$v0", RTYPE_GP | 2}, \
2822 {"$v1", RTYPE_GP | 3}, \
2823 {"$a0", RTYPE_GP | 4}, \
2824 {"$a1", RTYPE_GP | 5}, \
2825 {"$a2", RTYPE_GP | 6}, \
2826 {"$a3", RTYPE_GP | 7}, \
2827 {"$s0", RTYPE_GP | 16}, \
2828 {"$s1", RTYPE_GP | 17}, \
2829 {"$s2", RTYPE_GP | 18}, \
2830 {"$s3", RTYPE_GP | 19}, \
2831 {"$s4", RTYPE_GP | 20}, \
2832 {"$s5", RTYPE_GP | 21}, \
2833 {"$s6", RTYPE_GP | 22}, \
2834 {"$s7", RTYPE_GP | 23}, \
2835 {"$t8", RTYPE_GP | 24}, \
2836 {"$t9", RTYPE_GP | 25}, \
2837 {"$k0", RTYPE_GP | 26}, \
2838 {"$kt0", RTYPE_GP | 26}, \
2839 {"$k1", RTYPE_GP | 27}, \
2840 {"$kt1", RTYPE_GP | 27}, \
2841 {"$gp", RTYPE_GP | 28}, \
2842 {"$sp", RTYPE_GP | 29}, \
2843 {"$s8", RTYPE_GP | 30}, \
2844 {"$fp", RTYPE_GP | 30}, \
2845 {"$ra", RTYPE_GP | 31}
2846
2847 #define MIPS16_SPECIAL_REGISTER_NAMES \
2848 {"$pc", RTYPE_PC | 0}
2849
2850 #define MDMX_VECTOR_REGISTER_NAMES \
2851 /* {"$v0", RTYPE_VEC | 0}, Clash with REG 2 above. */ \
2852 /* {"$v1", RTYPE_VEC | 1}, Clash with REG 3 above. */ \
2853 {"$v2", RTYPE_VEC | 2}, \
2854 {"$v3", RTYPE_VEC | 3}, \
2855 {"$v4", RTYPE_VEC | 4}, \
2856 {"$v5", RTYPE_VEC | 5}, \
2857 {"$v6", RTYPE_VEC | 6}, \
2858 {"$v7", RTYPE_VEC | 7}, \
2859 {"$v8", RTYPE_VEC | 8}, \
2860 {"$v9", RTYPE_VEC | 9}, \
2861 {"$v10", RTYPE_VEC | 10}, \
2862 {"$v11", RTYPE_VEC | 11}, \
2863 {"$v12", RTYPE_VEC | 12}, \
2864 {"$v13", RTYPE_VEC | 13}, \
2865 {"$v14", RTYPE_VEC | 14}, \
2866 {"$v15", RTYPE_VEC | 15}, \
2867 {"$v16", RTYPE_VEC | 16}, \
2868 {"$v17", RTYPE_VEC | 17}, \
2869 {"$v18", RTYPE_VEC | 18}, \
2870 {"$v19", RTYPE_VEC | 19}, \
2871 {"$v20", RTYPE_VEC | 20}, \
2872 {"$v21", RTYPE_VEC | 21}, \
2873 {"$v22", RTYPE_VEC | 22}, \
2874 {"$v23", RTYPE_VEC | 23}, \
2875 {"$v24", RTYPE_VEC | 24}, \
2876 {"$v25", RTYPE_VEC | 25}, \
2877 {"$v26", RTYPE_VEC | 26}, \
2878 {"$v27", RTYPE_VEC | 27}, \
2879 {"$v28", RTYPE_VEC | 28}, \
2880 {"$v29", RTYPE_VEC | 29}, \
2881 {"$v30", RTYPE_VEC | 30}, \
2882 {"$v31", RTYPE_VEC | 31}
2883
2884 #define R5900_I_NAMES \
2885 {"$I", RTYPE_R5900_I | 0}
2886
2887 #define R5900_Q_NAMES \
2888 {"$Q", RTYPE_R5900_Q | 0}
2889
2890 #define R5900_R_NAMES \
2891 {"$R", RTYPE_R5900_R | 0}
2892
2893 #define R5900_ACC_NAMES \
2894 {"$ACC", RTYPE_R5900_ACC | 0 }
2895
2896 #define MIPS_DSP_ACCUMULATOR_NAMES \
2897 {"$ac0", RTYPE_ACC | 0}, \
2898 {"$ac1", RTYPE_ACC | 1}, \
2899 {"$ac2", RTYPE_ACC | 2}, \
2900 {"$ac3", RTYPE_ACC | 3}
2901
2902 static const struct regname reg_names[] = {
2903 GENERIC_REGISTER_NUMBERS,
2904 FPU_REGISTER_NAMES,
2905 FPU_CONDITION_CODE_NAMES,
2906 COPROC_CONDITION_CODE_NAMES,
2907
2908 /* The $txx registers depends on the abi,
2909 these will be added later into the symbol table from
2910 one of the tables below once mips_abi is set after
2911 parsing of arguments from the command line. */
2912 SYMBOLIC_REGISTER_NAMES,
2913
2914 MIPS16_SPECIAL_REGISTER_NAMES,
2915 MDMX_VECTOR_REGISTER_NAMES,
2916 R5900_I_NAMES,
2917 R5900_Q_NAMES,
2918 R5900_R_NAMES,
2919 R5900_ACC_NAMES,
2920 MIPS_DSP_ACCUMULATOR_NAMES,
2921 {0, 0}
2922 };
2923
2924 static const struct regname reg_names_o32[] = {
2925 O32_SYMBOLIC_REGISTER_NAMES,
2926 {0, 0}
2927 };
2928
2929 static const struct regname reg_names_n32n64[] = {
2930 N32N64_SYMBOLIC_REGISTER_NAMES,
2931 {0, 0}
2932 };
2933
2934 /* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2935 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2936 of these register symbols, return the associated vector register,
2937 otherwise return SYMVAL itself. */
2938
2939 static unsigned int
2940 mips_prefer_vec_regno (unsigned int symval)
2941 {
2942 if ((symval & -2) == (RTYPE_GP | 2))
2943 return RTYPE_VEC | (symval & 1);
2944 return symval;
2945 }
2946
2947 /* Return true if string [S, E) is a valid register name, storing its
2948 symbol value in *SYMVAL_PTR if so. */
2949
2950 static bfd_boolean
2951 mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
2952 {
2953 char save_c;
2954 symbolS *symbol;
2955
2956 /* Terminate name. */
2957 save_c = *e;
2958 *e = '\0';
2959
2960 /* Look up the name. */
2961 symbol = symbol_find (s);
2962 *e = save_c;
2963
2964 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2965 return FALSE;
2966
2967 *symval_ptr = S_GET_VALUE (symbol);
2968 return TRUE;
2969 }
2970
2971 /* Return true if the string at *SPTR is a valid register name. Allow it
2972 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2973 is nonnull.
2974
2975 When returning true, move *SPTR past the register, store the
2976 register's symbol value in *SYMVAL_PTR and the channel mask in
2977 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2978 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2979 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2980
2981 static bfd_boolean
2982 mips_parse_register (char **sptr, unsigned int *symval_ptr,
2983 unsigned int *channels_ptr)
2984 {
2985 char *s, *e, *m;
2986 const char *q;
2987 unsigned int channels, symval, bit;
2988
2989 /* Find end of name. */
2990 s = e = *sptr;
2991 if (is_name_beginner (*e))
2992 ++e;
2993 while (is_part_of_name (*e))
2994 ++e;
2995
2996 channels = 0;
2997 if (!mips_parse_register_1 (s, e, &symval))
2998 {
2999 if (!channels_ptr)
3000 return FALSE;
3001
3002 /* Eat characters from the end of the string that are valid
3003 channel suffixes. The preceding register must be $ACC or
3004 end with a digit, so there is no ambiguity. */
3005 bit = 1;
3006 m = e;
3007 for (q = "wzyx"; *q; q++, bit <<= 1)
3008 if (m > s && m[-1] == *q)
3009 {
3010 --m;
3011 channels |= bit;
3012 }
3013
3014 if (channels == 0
3015 || !mips_parse_register_1 (s, m, &symval)
3016 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
3017 return FALSE;
3018 }
3019
3020 *sptr = e;
3021 *symval_ptr = symval;
3022 if (channels_ptr)
3023 *channels_ptr = channels;
3024 return TRUE;
3025 }
3026
3027 /* Check if SPTR points at a valid register specifier according to TYPES.
3028 If so, then return 1, advance S to consume the specifier and store
3029 the register's number in REGNOP, otherwise return 0. */
3030
3031 static int
3032 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
3033 {
3034 unsigned int regno;
3035
3036 if (mips_parse_register (s, &regno, NULL))
3037 {
3038 if (types & RTYPE_VEC)
3039 regno = mips_prefer_vec_regno (regno);
3040 if (regno & types)
3041 regno &= RNUM_MASK;
3042 else
3043 regno = ~0;
3044 }
3045 else
3046 {
3047 if (types & RWARN)
3048 as_warn (_("unrecognized register name `%s'"), *s);
3049 regno = ~0;
3050 }
3051 if (regnop)
3052 *regnop = regno;
3053 return regno <= RNUM_MASK;
3054 }
3055
3056 /* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
3057 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
3058
3059 static char *
3060 mips_parse_vu0_channels (char *s, unsigned int *channels)
3061 {
3062 unsigned int i;
3063
3064 *channels = 0;
3065 for (i = 0; i < 4; i++)
3066 if (*s == "xyzw"[i])
3067 {
3068 *channels |= 1 << (3 - i);
3069 ++s;
3070 }
3071 return s;
3072 }
3073
3074 /* Token types for parsed operand lists. */
3075 enum mips_operand_token_type {
3076 /* A plain register, e.g. $f2. */
3077 OT_REG,
3078
3079 /* A 4-bit XYZW channel mask. */
3080 OT_CHANNELS,
3081
3082 /* A constant vector index, e.g. [1]. */
3083 OT_INTEGER_INDEX,
3084
3085 /* A register vector index, e.g. [$2]. */
3086 OT_REG_INDEX,
3087
3088 /* A continuous range of registers, e.g. $s0-$s4. */
3089 OT_REG_RANGE,
3090
3091 /* A (possibly relocated) expression. */
3092 OT_INTEGER,
3093
3094 /* A floating-point value. */
3095 OT_FLOAT,
3096
3097 /* A single character. This can be '(', ')' or ',', but '(' only appears
3098 before OT_REGs. */
3099 OT_CHAR,
3100
3101 /* A doubled character, either "--" or "++". */
3102 OT_DOUBLE_CHAR,
3103
3104 /* The end of the operand list. */
3105 OT_END
3106 };
3107
3108 /* A parsed operand token. */
3109 struct mips_operand_token
3110 {
3111 /* The type of token. */
3112 enum mips_operand_token_type type;
3113 union
3114 {
3115 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
3116 unsigned int regno;
3117
3118 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3119 unsigned int channels;
3120
3121 /* The integer value of an OT_INTEGER_INDEX. */
3122 addressT index;
3123
3124 /* The two register symbol values involved in an OT_REG_RANGE. */
3125 struct {
3126 unsigned int regno1;
3127 unsigned int regno2;
3128 } reg_range;
3129
3130 /* The value of an OT_INTEGER. The value is represented as an
3131 expression and the relocation operators that were applied to
3132 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3133 relocation operators were used. */
3134 struct {
3135 expressionS value;
3136 bfd_reloc_code_real_type relocs[3];
3137 } integer;
3138
3139 /* The binary data for an OT_FLOAT constant, and the number of bytes
3140 in the constant. */
3141 struct {
3142 unsigned char data[8];
3143 int length;
3144 } flt;
3145
3146 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
3147 char ch;
3148 } u;
3149 };
3150
3151 /* An obstack used to construct lists of mips_operand_tokens. */
3152 static struct obstack mips_operand_tokens;
3153
3154 /* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3155
3156 static void
3157 mips_add_token (struct mips_operand_token *token,
3158 enum mips_operand_token_type type)
3159 {
3160 token->type = type;
3161 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3162 }
3163
3164 /* Check whether S is '(' followed by a register name. Add OT_CHAR
3165 and OT_REG tokens for them if so, and return a pointer to the first
3166 unconsumed character. Return null otherwise. */
3167
3168 static char *
3169 mips_parse_base_start (char *s)
3170 {
3171 struct mips_operand_token token;
3172 unsigned int regno, channels;
3173 bfd_boolean decrement_p;
3174
3175 if (*s != '(')
3176 return 0;
3177
3178 ++s;
3179 SKIP_SPACE_TABS (s);
3180
3181 /* Only match "--" as part of a base expression. In other contexts "--X"
3182 is a double negative. */
3183 decrement_p = (s[0] == '-' && s[1] == '-');
3184 if (decrement_p)
3185 {
3186 s += 2;
3187 SKIP_SPACE_TABS (s);
3188 }
3189
3190 /* Allow a channel specifier because that leads to better error messages
3191 than treating something like "$vf0x++" as an expression. */
3192 if (!mips_parse_register (&s, &regno, &channels))
3193 return 0;
3194
3195 token.u.ch = '(';
3196 mips_add_token (&token, OT_CHAR);
3197
3198 if (decrement_p)
3199 {
3200 token.u.ch = '-';
3201 mips_add_token (&token, OT_DOUBLE_CHAR);
3202 }
3203
3204 token.u.regno = regno;
3205 mips_add_token (&token, OT_REG);
3206
3207 if (channels)
3208 {
3209 token.u.channels = channels;
3210 mips_add_token (&token, OT_CHANNELS);
3211 }
3212
3213 /* For consistency, only match "++" as part of base expressions too. */
3214 SKIP_SPACE_TABS (s);
3215 if (s[0] == '+' && s[1] == '+')
3216 {
3217 s += 2;
3218 token.u.ch = '+';
3219 mips_add_token (&token, OT_DOUBLE_CHAR);
3220 }
3221
3222 return s;
3223 }
3224
3225 /* Parse one or more tokens from S. Return a pointer to the first
3226 unconsumed character on success. Return null if an error was found
3227 and store the error text in insn_error. FLOAT_FORMAT is as for
3228 mips_parse_arguments. */
3229
3230 static char *
3231 mips_parse_argument_token (char *s, char float_format)
3232 {
3233 char *end, *save_in;
3234 const char *err;
3235 unsigned int regno1, regno2, channels;
3236 struct mips_operand_token token;
3237
3238 /* First look for "($reg", since we want to treat that as an
3239 OT_CHAR and OT_REG rather than an expression. */
3240 end = mips_parse_base_start (s);
3241 if (end)
3242 return end;
3243
3244 /* Handle other characters that end up as OT_CHARs. */
3245 if (*s == ')' || *s == ',')
3246 {
3247 token.u.ch = *s;
3248 mips_add_token (&token, OT_CHAR);
3249 ++s;
3250 return s;
3251 }
3252
3253 /* Handle tokens that start with a register. */
3254 if (mips_parse_register (&s, &regno1, &channels))
3255 {
3256 if (channels)
3257 {
3258 /* A register and a VU0 channel suffix. */
3259 token.u.regno = regno1;
3260 mips_add_token (&token, OT_REG);
3261
3262 token.u.channels = channels;
3263 mips_add_token (&token, OT_CHANNELS);
3264 return s;
3265 }
3266
3267 SKIP_SPACE_TABS (s);
3268 if (*s == '-')
3269 {
3270 /* A register range. */
3271 ++s;
3272 SKIP_SPACE_TABS (s);
3273 if (!mips_parse_register (&s, &regno2, NULL))
3274 {
3275 set_insn_error (0, _("invalid register range"));
3276 return 0;
3277 }
3278
3279 token.u.reg_range.regno1 = regno1;
3280 token.u.reg_range.regno2 = regno2;
3281 mips_add_token (&token, OT_REG_RANGE);
3282 return s;
3283 }
3284
3285 /* Add the register itself. */
3286 token.u.regno = regno1;
3287 mips_add_token (&token, OT_REG);
3288
3289 /* Check for a vector index. */
3290 if (*s == '[')
3291 {
3292 ++s;
3293 SKIP_SPACE_TABS (s);
3294 if (mips_parse_register (&s, &token.u.regno, NULL))
3295 mips_add_token (&token, OT_REG_INDEX);
3296 else
3297 {
3298 expressionS element;
3299
3300 my_getExpression (&element, s);
3301 if (element.X_op != O_constant)
3302 {
3303 set_insn_error (0, _("vector element must be constant"));
3304 return 0;
3305 }
3306 s = expr_end;
3307 token.u.index = element.X_add_number;
3308 mips_add_token (&token, OT_INTEGER_INDEX);
3309 }
3310 SKIP_SPACE_TABS (s);
3311 if (*s != ']')
3312 {
3313 set_insn_error (0, _("missing `]'"));
3314 return 0;
3315 }
3316 ++s;
3317 }
3318 return s;
3319 }
3320
3321 if (float_format)
3322 {
3323 /* First try to treat expressions as floats. */
3324 save_in = input_line_pointer;
3325 input_line_pointer = s;
3326 err = md_atof (float_format, (char *) token.u.flt.data,
3327 &token.u.flt.length);
3328 end = input_line_pointer;
3329 input_line_pointer = save_in;
3330 if (err && *err)
3331 {
3332 set_insn_error (0, err);
3333 return 0;
3334 }
3335 if (s != end)
3336 {
3337 mips_add_token (&token, OT_FLOAT);
3338 return end;
3339 }
3340 }
3341
3342 /* Treat everything else as an integer expression. */
3343 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3344 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3345 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3346 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3347 s = expr_end;
3348 mips_add_token (&token, OT_INTEGER);
3349 return s;
3350 }
3351
3352 /* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3353 if expressions should be treated as 32-bit floating-point constants,
3354 'd' if they should be treated as 64-bit floating-point constants,
3355 or 0 if they should be treated as integer expressions (the usual case).
3356
3357 Return a list of tokens on success, otherwise return 0. The caller
3358 must obstack_free the list after use. */
3359
3360 static struct mips_operand_token *
3361 mips_parse_arguments (char *s, char float_format)
3362 {
3363 struct mips_operand_token token;
3364
3365 SKIP_SPACE_TABS (s);
3366 while (*s)
3367 {
3368 s = mips_parse_argument_token (s, float_format);
3369 if (!s)
3370 {
3371 obstack_free (&mips_operand_tokens,
3372 obstack_finish (&mips_operand_tokens));
3373 return 0;
3374 }
3375 SKIP_SPACE_TABS (s);
3376 }
3377 mips_add_token (&token, OT_END);
3378 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
3379 }
3380
3381 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3382 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
3383
3384 static bfd_boolean
3385 is_opcode_valid (const struct mips_opcode *mo)
3386 {
3387 int isa = mips_opts.isa;
3388 int ase = mips_opts.ase;
3389 int fp_s, fp_d;
3390 unsigned int i;
3391
3392 if (ISA_HAS_64BIT_REGS (isa))
3393 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3394 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3395 ase |= mips_ases[i].flags64;
3396
3397 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
3398 return FALSE;
3399
3400 /* Check whether the instruction or macro requires single-precision or
3401 double-precision floating-point support. Note that this information is
3402 stored differently in the opcode table for insns and macros. */
3403 if (mo->pinfo == INSN_MACRO)
3404 {
3405 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3406 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3407 }
3408 else
3409 {
3410 fp_s = mo->pinfo & FP_S;
3411 fp_d = mo->pinfo & FP_D;
3412 }
3413
3414 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3415 return FALSE;
3416
3417 if (fp_s && mips_opts.soft_float)
3418 return FALSE;
3419
3420 return TRUE;
3421 }
3422
3423 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
3424 selected ISA and architecture. */
3425
3426 static bfd_boolean
3427 is_opcode_valid_16 (const struct mips_opcode *mo)
3428 {
3429 int isa = mips_opts.isa;
3430 int ase = mips_opts.ase;
3431 unsigned int i;
3432
3433 if (ISA_HAS_64BIT_REGS (isa))
3434 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3435 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3436 ase |= mips_ases[i].flags64;
3437
3438 return opcode_is_member (mo, isa, ase, mips_opts.arch);
3439 }
3440
3441 /* Return TRUE if the size of the microMIPS opcode MO matches one
3442 explicitly requested. Always TRUE in the standard MIPS mode.
3443 Use is_size_valid_16 for MIPS16 opcodes. */
3444
3445 static bfd_boolean
3446 is_size_valid (const struct mips_opcode *mo)
3447 {
3448 if (!mips_opts.micromips)
3449 return TRUE;
3450
3451 if (mips_opts.insn32)
3452 {
3453 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3454 return FALSE;
3455 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3456 return FALSE;
3457 }
3458 if (!forced_insn_length)
3459 return TRUE;
3460 if (mo->pinfo == INSN_MACRO)
3461 return FALSE;
3462 return forced_insn_length == micromips_insn_length (mo);
3463 }
3464
3465 /* Return TRUE if the size of the MIPS16 opcode MO matches one
3466 explicitly requested. */
3467
3468 static bfd_boolean
3469 is_size_valid_16 (const struct mips_opcode *mo)
3470 {
3471 if (!forced_insn_length)
3472 return TRUE;
3473 if (mo->pinfo == INSN_MACRO)
3474 return FALSE;
3475 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3476 return FALSE;
3477 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3478 return FALSE;
3479 return TRUE;
3480 }
3481
3482 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
3483 of the preceding instruction. Always TRUE in the standard MIPS mode.
3484
3485 We don't accept macros in 16-bit delay slots to avoid a case where
3486 a macro expansion fails because it relies on a preceding 32-bit real
3487 instruction to have matched and does not handle the operands correctly.
3488 The only macros that may expand to 16-bit instructions are JAL that
3489 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3490 and BGT (that likewise cannot be placed in a delay slot) that decay to
3491 a NOP. In all these cases the macros precede any corresponding real
3492 instruction definitions in the opcode table, so they will match in the
3493 second pass where the size of the delay slot is ignored and therefore
3494 produce correct code. */
3495
3496 static bfd_boolean
3497 is_delay_slot_valid (const struct mips_opcode *mo)
3498 {
3499 if (!mips_opts.micromips)
3500 return TRUE;
3501
3502 if (mo->pinfo == INSN_MACRO)
3503 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
3504 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3505 && micromips_insn_length (mo) != 4)
3506 return FALSE;
3507 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3508 && micromips_insn_length (mo) != 2)
3509 return FALSE;
3510
3511 return TRUE;
3512 }
3513
3514 /* For consistency checking, verify that all bits of OPCODE are specified
3515 either by the match/mask part of the instruction definition, or by the
3516 operand list. Also build up a list of operands in OPERANDS.
3517
3518 INSN_BITS says which bits of the instruction are significant.
3519 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3520 provides the mips_operand description of each operand. DECODE_OPERAND
3521 is null for MIPS16 instructions. */
3522
3523 static int
3524 validate_mips_insn (const struct mips_opcode *opcode,
3525 unsigned long insn_bits,
3526 const struct mips_operand *(*decode_operand) (const char *),
3527 struct mips_operand_array *operands)
3528 {
3529 const char *s;
3530 unsigned long used_bits, doubled, undefined, opno, mask;
3531 const struct mips_operand *operand;
3532
3533 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3534 if ((mask & opcode->match) != opcode->match)
3535 {
3536 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3537 opcode->name, opcode->args);
3538 return 0;
3539 }
3540 used_bits = 0;
3541 opno = 0;
3542 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3543 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
3544 for (s = opcode->args; *s; ++s)
3545 switch (*s)
3546 {
3547 case ',':
3548 case '(':
3549 case ')':
3550 break;
3551
3552 case '#':
3553 s++;
3554 break;
3555
3556 default:
3557 if (!decode_operand)
3558 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
3559 else
3560 operand = decode_operand (s);
3561 if (!operand && opcode->pinfo != INSN_MACRO)
3562 {
3563 as_bad (_("internal: unknown operand type: %s %s"),
3564 opcode->name, opcode->args);
3565 return 0;
3566 }
3567 gas_assert (opno < MAX_OPERANDS);
3568 operands->operand[opno] = operand;
3569 if (!decode_operand && operand
3570 && operand->type == OP_INT && operand->lsb == 0
3571 && mips_opcode_32bit_p (opcode))
3572 used_bits |= mips16_immed_extend (-1, operand->size);
3573 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
3574 {
3575 used_bits = mips_insert_operand (operand, used_bits, -1);
3576 if (operand->type == OP_MDMX_IMM_REG)
3577 /* Bit 5 is the format selector (OB vs QH). The opcode table
3578 has separate entries for each format. */
3579 used_bits &= ~(1 << (operand->lsb + 5));
3580 if (operand->type == OP_ENTRY_EXIT_LIST)
3581 used_bits &= ~(mask & 0x700);
3582 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3583 operand field that cannot be fully described with LSB/SIZE. */
3584 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3585 used_bits &= ~0x6000;
3586 }
3587 /* Skip prefix characters. */
3588 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
3589 ++s;
3590 opno += 1;
3591 break;
3592 }
3593 doubled = used_bits & mask & insn_bits;
3594 if (doubled)
3595 {
3596 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3597 " %s %s"), doubled, opcode->name, opcode->args);
3598 return 0;
3599 }
3600 used_bits |= mask;
3601 undefined = ~used_bits & insn_bits;
3602 if (opcode->pinfo != INSN_MACRO && undefined)
3603 {
3604 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3605 undefined, opcode->name, opcode->args);
3606 return 0;
3607 }
3608 used_bits &= ~insn_bits;
3609 if (used_bits)
3610 {
3611 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3612 used_bits, opcode->name, opcode->args);
3613 return 0;
3614 }
3615 return 1;
3616 }
3617
3618 /* The MIPS16 version of validate_mips_insn. */
3619
3620 static int
3621 validate_mips16_insn (const struct mips_opcode *opcode,
3622 struct mips_operand_array *operands)
3623 {
3624 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
3625
3626 return validate_mips_insn (opcode, insn_bits, 0, operands);
3627 }
3628
3629 /* The microMIPS version of validate_mips_insn. */
3630
3631 static int
3632 validate_micromips_insn (const struct mips_opcode *opc,
3633 struct mips_operand_array *operands)
3634 {
3635 unsigned long insn_bits;
3636 unsigned long major;
3637 unsigned int length;
3638
3639 if (opc->pinfo == INSN_MACRO)
3640 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3641 operands);
3642
3643 length = micromips_insn_length (opc);
3644 if (length != 2 && length != 4)
3645 {
3646 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
3647 "%s %s"), length, opc->name, opc->args);
3648 return 0;
3649 }
3650 major = opc->match >> (10 + 8 * (length - 2));
3651 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3652 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3653 {
3654 as_bad (_("internal error: bad microMIPS opcode "
3655 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3656 return 0;
3657 }
3658
3659 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3660 insn_bits = 1 << 4 * length;
3661 insn_bits <<= 4 * length;
3662 insn_bits -= 1;
3663 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3664 operands);
3665 }
3666
3667 /* This function is called once, at assembler startup time. It should set up
3668 all the tables, etc. that the MD part of the assembler will need. */
3669
3670 void
3671 md_begin (void)
3672 {
3673 const char *retval = NULL;
3674 int i = 0;
3675 int broken = 0;
3676
3677 if (mips_pic != NO_PIC)
3678 {
3679 if (g_switch_seen && g_switch_value != 0)
3680 as_bad (_("-G may not be used in position-independent code"));
3681 g_switch_value = 0;
3682 }
3683 else if (mips_abicalls)
3684 {
3685 if (g_switch_seen && g_switch_value != 0)
3686 as_bad (_("-G may not be used with abicalls"));
3687 g_switch_value = 0;
3688 }
3689
3690 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
3691 as_warn (_("could not set architecture and machine"));
3692
3693 op_hash = hash_new ();
3694
3695 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
3696 for (i = 0; i < NUMOPCODES;)
3697 {
3698 const char *name = mips_opcodes[i].name;
3699
3700 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
3701 if (retval != NULL)
3702 {
3703 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3704 mips_opcodes[i].name, retval);
3705 /* Probably a memory allocation problem? Give up now. */
3706 as_fatal (_("broken assembler, no assembly attempted"));
3707 }
3708 do
3709 {
3710 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3711 decode_mips_operand, &mips_operands[i]))
3712 broken = 1;
3713
3714 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3715 {
3716 create_insn (&nop_insn, mips_opcodes + i);
3717 if (mips_fix_loongson2f_nop)
3718 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3719 nop_insn.fixed_p = 1;
3720 }
3721
3722 if (sync_insn.insn_mo == NULL && strcmp (name, "sync") == 0)
3723 create_insn (&sync_insn, mips_opcodes + i);
3724
3725 ++i;
3726 }
3727 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3728 }
3729
3730 mips16_op_hash = hash_new ();
3731 mips16_operands = XCNEWVEC (struct mips_operand_array,
3732 bfd_mips16_num_opcodes);
3733
3734 i = 0;
3735 while (i < bfd_mips16_num_opcodes)
3736 {
3737 const char *name = mips16_opcodes[i].name;
3738
3739 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
3740 if (retval != NULL)
3741 as_fatal (_("internal: can't hash `%s': %s"),
3742 mips16_opcodes[i].name, retval);
3743 do
3744 {
3745 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3746 broken = 1;
3747 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3748 {
3749 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3750 mips16_nop_insn.fixed_p = 1;
3751 }
3752 ++i;
3753 }
3754 while (i < bfd_mips16_num_opcodes
3755 && strcmp (mips16_opcodes[i].name, name) == 0);
3756 }
3757
3758 micromips_op_hash = hash_new ();
3759 micromips_operands = XCNEWVEC (struct mips_operand_array,
3760 bfd_micromips_num_opcodes);
3761
3762 i = 0;
3763 while (i < bfd_micromips_num_opcodes)
3764 {
3765 const char *name = micromips_opcodes[i].name;
3766
3767 retval = hash_insert (micromips_op_hash, name,
3768 (void *) &micromips_opcodes[i]);
3769 if (retval != NULL)
3770 as_fatal (_("internal: can't hash `%s': %s"),
3771 micromips_opcodes[i].name, retval);
3772 do
3773 {
3774 struct mips_cl_insn *micromips_nop_insn;
3775
3776 if (!validate_micromips_insn (&micromips_opcodes[i],
3777 &micromips_operands[i]))
3778 broken = 1;
3779
3780 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3781 {
3782 if (micromips_insn_length (micromips_opcodes + i) == 2)
3783 micromips_nop_insn = &micromips_nop16_insn;
3784 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3785 micromips_nop_insn = &micromips_nop32_insn;
3786 else
3787 continue;
3788
3789 if (micromips_nop_insn->insn_mo == NULL
3790 && strcmp (name, "nop") == 0)
3791 {
3792 create_insn (micromips_nop_insn, micromips_opcodes + i);
3793 micromips_nop_insn->fixed_p = 1;
3794 }
3795 }
3796 }
3797 while (++i < bfd_micromips_num_opcodes
3798 && strcmp (micromips_opcodes[i].name, name) == 0);
3799 }
3800
3801 if (broken)
3802 as_fatal (_("broken assembler, no assembly attempted"));
3803
3804 /* We add all the general register names to the symbol table. This
3805 helps us detect invalid uses of them. */
3806 for (i = 0; reg_names[i].name; i++)
3807 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
3808 reg_names[i].num, /* & RNUM_MASK, */
3809 &zero_address_frag));
3810 if (HAVE_NEWABI)
3811 for (i = 0; reg_names_n32n64[i].name; i++)
3812 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
3813 reg_names_n32n64[i].num, /* & RNUM_MASK, */
3814 &zero_address_frag));
3815 else
3816 for (i = 0; reg_names_o32[i].name; i++)
3817 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
3818 reg_names_o32[i].num, /* & RNUM_MASK, */
3819 &zero_address_frag));
3820
3821 for (i = 0; i < 32; i++)
3822 {
3823 char regname[6];
3824
3825 /* R5900 VU0 floating-point register. */
3826 sprintf (regname, "$vf%d", i);
3827 symbol_table_insert (symbol_new (regname, reg_section,
3828 RTYPE_VF | i, &zero_address_frag));
3829
3830 /* R5900 VU0 integer register. */
3831 sprintf (regname, "$vi%d", i);
3832 symbol_table_insert (symbol_new (regname, reg_section,
3833 RTYPE_VI | i, &zero_address_frag));
3834
3835 /* MSA register. */
3836 sprintf (regname, "$w%d", i);
3837 symbol_table_insert (symbol_new (regname, reg_section,
3838 RTYPE_MSA | i, &zero_address_frag));
3839 }
3840
3841 obstack_init (&mips_operand_tokens);
3842
3843 mips_no_prev_insn ();
3844
3845 mips_gprmask = 0;
3846 mips_cprmask[0] = 0;
3847 mips_cprmask[1] = 0;
3848 mips_cprmask[2] = 0;
3849 mips_cprmask[3] = 0;
3850
3851 /* set the default alignment for the text section (2**2) */
3852 record_alignment (text_section, 2);
3853
3854 bfd_set_gp_size (stdoutput, g_switch_value);
3855
3856 /* On a native system other than VxWorks, sections must be aligned
3857 to 16 byte boundaries. When configured for an embedded ELF
3858 target, we don't bother. */
3859 if (strncmp (TARGET_OS, "elf", 3) != 0
3860 && strncmp (TARGET_OS, "vxworks", 7) != 0)
3861 {
3862 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3863 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3864 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3865 }
3866
3867 /* Create a .reginfo section for register masks and a .mdebug
3868 section for debugging information. */
3869 {
3870 segT seg;
3871 subsegT subseg;
3872 flagword flags;
3873 segT sec;
3874
3875 seg = now_seg;
3876 subseg = now_subseg;
3877
3878 /* The ABI says this section should be loaded so that the
3879 running program can access it. However, we don't load it
3880 if we are configured for an embedded target. */
3881 flags = SEC_READONLY | SEC_DATA;
3882 if (strncmp (TARGET_OS, "elf", 3) != 0)
3883 flags |= SEC_ALLOC | SEC_LOAD;
3884
3885 if (mips_abi != N64_ABI)
3886 {
3887 sec = subseg_new (".reginfo", (subsegT) 0);
3888
3889 bfd_set_section_flags (stdoutput, sec, flags);
3890 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
3891
3892 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3893 }
3894 else
3895 {
3896 /* The 64-bit ABI uses a .MIPS.options section rather than
3897 .reginfo section. */
3898 sec = subseg_new (".MIPS.options", (subsegT) 0);
3899 bfd_set_section_flags (stdoutput, sec, flags);
3900 bfd_set_section_alignment (stdoutput, sec, 3);
3901
3902 /* Set up the option header. */
3903 {
3904 Elf_Internal_Options opthdr;
3905 char *f;
3906
3907 opthdr.kind = ODK_REGINFO;
3908 opthdr.size = (sizeof (Elf_External_Options)
3909 + sizeof (Elf64_External_RegInfo));
3910 opthdr.section = 0;
3911 opthdr.info = 0;
3912 f = frag_more (sizeof (Elf_External_Options));
3913 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3914 (Elf_External_Options *) f);
3915
3916 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3917 }
3918 }
3919
3920 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3921 bfd_set_section_flags (stdoutput, sec,
3922 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3923 bfd_set_section_alignment (stdoutput, sec, 3);
3924 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3925
3926 if (ECOFF_DEBUGGING)
3927 {
3928 sec = subseg_new (".mdebug", (subsegT) 0);
3929 (void) bfd_set_section_flags (stdoutput, sec,
3930 SEC_HAS_CONTENTS | SEC_READONLY);
3931 (void) bfd_set_section_alignment (stdoutput, sec, 2);
3932 }
3933 else if (mips_flag_pdr)
3934 {
3935 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3936 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3937 SEC_READONLY | SEC_RELOC
3938 | SEC_DEBUGGING);
3939 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3940 }
3941
3942 subseg_set (seg, subseg);
3943 }
3944
3945 if (mips_fix_vr4120)
3946 init_vr4120_conflicts ();
3947 }
3948
3949 static inline void
3950 fpabi_incompatible_with (int fpabi, const char *what)
3951 {
3952 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3953 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3954 }
3955
3956 static inline void
3957 fpabi_requires (int fpabi, const char *what)
3958 {
3959 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3960 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3961 }
3962
3963 /* Check -mabi and register sizes against the specified FP ABI. */
3964 static void
3965 check_fpabi (int fpabi)
3966 {
3967 switch (fpabi)
3968 {
3969 case Val_GNU_MIPS_ABI_FP_DOUBLE:
3970 if (file_mips_opts.soft_float)
3971 fpabi_incompatible_with (fpabi, "softfloat");
3972 else if (file_mips_opts.single_float)
3973 fpabi_incompatible_with (fpabi, "singlefloat");
3974 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3975 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3976 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3977 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
3978 break;
3979
3980 case Val_GNU_MIPS_ABI_FP_XX:
3981 if (mips_abi != O32_ABI)
3982 fpabi_requires (fpabi, "-mabi=32");
3983 else if (file_mips_opts.soft_float)
3984 fpabi_incompatible_with (fpabi, "softfloat");
3985 else if (file_mips_opts.single_float)
3986 fpabi_incompatible_with (fpabi, "singlefloat");
3987 else if (file_mips_opts.fp != 0)
3988 fpabi_requires (fpabi, "fp=xx");
3989 break;
3990
3991 case Val_GNU_MIPS_ABI_FP_64A:
3992 case Val_GNU_MIPS_ABI_FP_64:
3993 if (mips_abi != O32_ABI)
3994 fpabi_requires (fpabi, "-mabi=32");
3995 else if (file_mips_opts.soft_float)
3996 fpabi_incompatible_with (fpabi, "softfloat");
3997 else if (file_mips_opts.single_float)
3998 fpabi_incompatible_with (fpabi, "singlefloat");
3999 else if (file_mips_opts.fp != 64)
4000 fpabi_requires (fpabi, "fp=64");
4001 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
4002 fpabi_incompatible_with (fpabi, "nooddspreg");
4003 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
4004 fpabi_requires (fpabi, "nooddspreg");
4005 break;
4006
4007 case Val_GNU_MIPS_ABI_FP_SINGLE:
4008 if (file_mips_opts.soft_float)
4009 fpabi_incompatible_with (fpabi, "softfloat");
4010 else if (!file_mips_opts.single_float)
4011 fpabi_requires (fpabi, "singlefloat");
4012 break;
4013
4014 case Val_GNU_MIPS_ABI_FP_SOFT:
4015 if (!file_mips_opts.soft_float)
4016 fpabi_requires (fpabi, "softfloat");
4017 break;
4018
4019 case Val_GNU_MIPS_ABI_FP_OLD_64:
4020 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
4021 Tag_GNU_MIPS_ABI_FP, fpabi);
4022 break;
4023
4024 case Val_GNU_MIPS_ABI_FP_NAN2008:
4025 /* Silently ignore compatibility value. */
4026 break;
4027
4028 default:
4029 as_warn (_(".gnu_attribute %d,%d is not a recognized"
4030 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
4031 break;
4032 }
4033 }
4034
4035 /* Perform consistency checks on the current options. */
4036
4037 static void
4038 mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
4039 {
4040 /* Check the size of integer registers agrees with the ABI and ISA. */
4041 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
4042 as_bad (_("`gp=64' used with a 32-bit processor"));
4043 else if (abi_checks
4044 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
4045 as_bad (_("`gp=32' used with a 64-bit ABI"));
4046 else if (abi_checks
4047 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
4048 as_bad (_("`gp=64' used with a 32-bit ABI"));
4049
4050 /* Check the size of the float registers agrees with the ABI and ISA. */
4051 switch (opts->fp)
4052 {
4053 case 0:
4054 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
4055 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
4056 else if (opts->single_float == 1)
4057 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
4058 break;
4059 case 64:
4060 if (!ISA_HAS_64BIT_FPRS (opts->isa))
4061 as_bad (_("`fp=64' used with a 32-bit fpu"));
4062 else if (abi_checks
4063 && ABI_NEEDS_32BIT_REGS (mips_abi)
4064 && !ISA_HAS_MXHC1 (opts->isa))
4065 as_warn (_("`fp=64' used with a 32-bit ABI"));
4066 break;
4067 case 32:
4068 if (abi_checks
4069 && ABI_NEEDS_64BIT_REGS (mips_abi))
4070 as_warn (_("`fp=32' used with a 64-bit ABI"));
4071 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
4072 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
4073 break;
4074 default:
4075 as_bad (_("Unknown size of floating point registers"));
4076 break;
4077 }
4078
4079 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
4080 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
4081
4082 if (opts->micromips == 1 && opts->mips16 == 1)
4083 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
4084 else if (ISA_IS_R6 (opts->isa)
4085 && (opts->micromips == 1
4086 || opts->mips16 == 1))
4087 as_fatal (_("`%s' cannot be used with `%s'"),
4088 opts->micromips ? "micromips" : "mips16",
4089 mips_cpu_info_from_isa (opts->isa)->name);
4090
4091 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4092 as_fatal (_("branch relaxation is not supported in `%s'"),
4093 mips_cpu_info_from_isa (opts->isa)->name);
4094 }
4095
4096 /* Perform consistency checks on the module level options exactly once.
4097 This is a deferred check that happens:
4098 at the first .set directive
4099 or, at the first pseudo op that generates code (inc .dc.a)
4100 or, at the first instruction
4101 or, at the end. */
4102
4103 static void
4104 file_mips_check_options (void)
4105 {
4106 if (file_mips_opts_checked)
4107 return;
4108
4109 /* The following code determines the register size.
4110 Similar code was added to GCC 3.3 (see override_options() in
4111 config/mips/mips.c). The GAS and GCC code should be kept in sync
4112 as much as possible. */
4113
4114 if (file_mips_opts.gp < 0)
4115 {
4116 /* Infer the integer register size from the ABI and processor.
4117 Restrict ourselves to 32-bit registers if that's all the
4118 processor has, or if the ABI cannot handle 64-bit registers. */
4119 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4120 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4121 ? 32 : 64;
4122 }
4123
4124 if (file_mips_opts.fp < 0)
4125 {
4126 /* No user specified float register size.
4127 ??? GAS treats single-float processors as though they had 64-bit
4128 float registers (although it complains when double-precision
4129 instructions are used). As things stand, saying they have 32-bit
4130 registers would lead to spurious "register must be even" messages.
4131 So here we assume float registers are never smaller than the
4132 integer ones. */
4133 if (file_mips_opts.gp == 64)
4134 /* 64-bit integer registers implies 64-bit float registers. */
4135 file_mips_opts.fp = 64;
4136 else if ((file_mips_opts.ase & FP64_ASES)
4137 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4138 /* Handle ASEs that require 64-bit float registers, if possible. */
4139 file_mips_opts.fp = 64;
4140 else if (ISA_IS_R6 (mips_opts.isa))
4141 /* R6 implies 64-bit float registers. */
4142 file_mips_opts.fp = 64;
4143 else
4144 /* 32-bit float registers. */
4145 file_mips_opts.fp = 32;
4146 }
4147
4148 /* Disable operations on odd-numbered floating-point registers by default
4149 when using the FPXX ABI. */
4150 if (file_mips_opts.oddspreg < 0)
4151 {
4152 if (file_mips_opts.fp == 0)
4153 file_mips_opts.oddspreg = 0;
4154 else
4155 file_mips_opts.oddspreg = 1;
4156 }
4157
4158 /* End of GCC-shared inference code. */
4159
4160 /* This flag is set when we have a 64-bit capable CPU but use only
4161 32-bit wide registers. Note that EABI does not use it. */
4162 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4163 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4164 || mips_abi == O32_ABI))
4165 mips_32bitmode = 1;
4166
4167 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4168 as_bad (_("trap exception not supported at ISA 1"));
4169
4170 /* If the selected architecture includes support for ASEs, enable
4171 generation of code for them. */
4172 if (file_mips_opts.mips16 == -1)
4173 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4174 if (file_mips_opts.micromips == -1)
4175 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4176 ? 1 : 0;
4177
4178 if (mips_nan2008 == -1)
4179 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4180 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4181 as_fatal (_("`%s' does not support legacy NaN"),
4182 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4183
4184 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4185 being selected implicitly. */
4186 if (file_mips_opts.fp != 64)
4187 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4188
4189 /* If the user didn't explicitly select or deselect a particular ASE,
4190 use the default setting for the CPU. */
4191 file_mips_opts.ase |= (file_mips_opts.init_ase & ~file_ase_explicit);
4192
4193 /* Set up the current options. These may change throughout assembly. */
4194 mips_opts = file_mips_opts;
4195
4196 mips_check_isa_supports_ases ();
4197 mips_check_options (&file_mips_opts, TRUE);
4198 file_mips_opts_checked = TRUE;
4199
4200 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4201 as_warn (_("could not set architecture and machine"));
4202 }
4203
4204 void
4205 md_assemble (char *str)
4206 {
4207 struct mips_cl_insn insn;
4208 bfd_reloc_code_real_type unused_reloc[3]
4209 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
4210
4211 file_mips_check_options ();
4212
4213 imm_expr.X_op = O_absent;
4214 offset_expr.X_op = O_absent;
4215 offset_reloc[0] = BFD_RELOC_UNUSED;
4216 offset_reloc[1] = BFD_RELOC_UNUSED;
4217 offset_reloc[2] = BFD_RELOC_UNUSED;
4218
4219 mips_mark_labels ();
4220 mips_assembling_insn = TRUE;
4221 clear_insn_error ();
4222
4223 if (mips_opts.mips16)
4224 mips16_ip (str, &insn);
4225 else
4226 {
4227 mips_ip (str, &insn);
4228 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4229 str, insn.insn_opcode));
4230 }
4231
4232 if (insn_error.msg)
4233 report_insn_error (str);
4234 else if (insn.insn_mo->pinfo == INSN_MACRO)
4235 {
4236 macro_start ();
4237 if (mips_opts.mips16)
4238 mips16_macro (&insn);
4239 else
4240 macro (&insn, str);
4241 macro_end ();
4242 }
4243 else
4244 {
4245 if (offset_expr.X_op != O_absent)
4246 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
4247 else
4248 append_insn (&insn, NULL, unused_reloc, FALSE);
4249 }
4250
4251 mips_assembling_insn = FALSE;
4252 }
4253
4254 /* Convenience functions for abstracting away the differences between
4255 MIPS16 and non-MIPS16 relocations. */
4256
4257 static inline bfd_boolean
4258 mips16_reloc_p (bfd_reloc_code_real_type reloc)
4259 {
4260 switch (reloc)
4261 {
4262 case BFD_RELOC_MIPS16_JMP:
4263 case BFD_RELOC_MIPS16_GPREL:
4264 case BFD_RELOC_MIPS16_GOT16:
4265 case BFD_RELOC_MIPS16_CALL16:
4266 case BFD_RELOC_MIPS16_HI16_S:
4267 case BFD_RELOC_MIPS16_HI16:
4268 case BFD_RELOC_MIPS16_LO16:
4269 case BFD_RELOC_MIPS16_16_PCREL_S1:
4270 return TRUE;
4271
4272 default:
4273 return FALSE;
4274 }
4275 }
4276
4277 static inline bfd_boolean
4278 micromips_reloc_p (bfd_reloc_code_real_type reloc)
4279 {
4280 switch (reloc)
4281 {
4282 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4283 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4284 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4285 case BFD_RELOC_MICROMIPS_GPREL16:
4286 case BFD_RELOC_MICROMIPS_JMP:
4287 case BFD_RELOC_MICROMIPS_HI16:
4288 case BFD_RELOC_MICROMIPS_HI16_S:
4289 case BFD_RELOC_MICROMIPS_LO16:
4290 case BFD_RELOC_MICROMIPS_LITERAL:
4291 case BFD_RELOC_MICROMIPS_GOT16:
4292 case BFD_RELOC_MICROMIPS_CALL16:
4293 case BFD_RELOC_MICROMIPS_GOT_HI16:
4294 case BFD_RELOC_MICROMIPS_GOT_LO16:
4295 case BFD_RELOC_MICROMIPS_CALL_HI16:
4296 case BFD_RELOC_MICROMIPS_CALL_LO16:
4297 case BFD_RELOC_MICROMIPS_SUB:
4298 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4299 case BFD_RELOC_MICROMIPS_GOT_OFST:
4300 case BFD_RELOC_MICROMIPS_GOT_DISP:
4301 case BFD_RELOC_MICROMIPS_HIGHEST:
4302 case BFD_RELOC_MICROMIPS_HIGHER:
4303 case BFD_RELOC_MICROMIPS_SCN_DISP:
4304 case BFD_RELOC_MICROMIPS_JALR:
4305 return TRUE;
4306
4307 default:
4308 return FALSE;
4309 }
4310 }
4311
4312 static inline bfd_boolean
4313 jmp_reloc_p (bfd_reloc_code_real_type reloc)
4314 {
4315 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4316 }
4317
4318 static inline bfd_boolean
4319 b_reloc_p (bfd_reloc_code_real_type reloc)
4320 {
4321 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4322 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4323 || reloc == BFD_RELOC_16_PCREL_S2
4324 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
4325 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4326 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4327 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4328 }
4329
4330 static inline bfd_boolean
4331 got16_reloc_p (bfd_reloc_code_real_type reloc)
4332 {
4333 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
4334 || reloc == BFD_RELOC_MICROMIPS_GOT16);
4335 }
4336
4337 static inline bfd_boolean
4338 hi16_reloc_p (bfd_reloc_code_real_type reloc)
4339 {
4340 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
4341 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
4342 }
4343
4344 static inline bfd_boolean
4345 lo16_reloc_p (bfd_reloc_code_real_type reloc)
4346 {
4347 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
4348 || reloc == BFD_RELOC_MICROMIPS_LO16);
4349 }
4350
4351 static inline bfd_boolean
4352 jalr_reloc_p (bfd_reloc_code_real_type reloc)
4353 {
4354 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
4355 }
4356
4357 static inline bfd_boolean
4358 gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4359 {
4360 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4361 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4362 }
4363
4364 /* Return true if RELOC is a PC-relative relocation that does not have
4365 full address range. */
4366
4367 static inline bfd_boolean
4368 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4369 {
4370 switch (reloc)
4371 {
4372 case BFD_RELOC_16_PCREL_S2:
4373 case BFD_RELOC_MIPS16_16_PCREL_S1:
4374 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4375 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4376 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4377 case BFD_RELOC_MIPS_21_PCREL_S2:
4378 case BFD_RELOC_MIPS_26_PCREL_S2:
4379 case BFD_RELOC_MIPS_18_PCREL_S3:
4380 case BFD_RELOC_MIPS_19_PCREL_S2:
4381 return TRUE;
4382
4383 case BFD_RELOC_32_PCREL:
4384 case BFD_RELOC_HI16_S_PCREL:
4385 case BFD_RELOC_LO16_PCREL:
4386 return HAVE_64BIT_ADDRESSES;
4387
4388 default:
4389 return FALSE;
4390 }
4391 }
4392
4393 /* Return true if the given relocation might need a matching %lo().
4394 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4395 need a matching %lo() when applied to local symbols. */
4396
4397 static inline bfd_boolean
4398 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
4399 {
4400 return (HAVE_IN_PLACE_ADDENDS
4401 && (hi16_reloc_p (reloc)
4402 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4403 all GOT16 relocations evaluate to "G". */
4404 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4405 }
4406
4407 /* Return the type of %lo() reloc needed by RELOC, given that
4408 reloc_needs_lo_p. */
4409
4410 static inline bfd_reloc_code_real_type
4411 matching_lo_reloc (bfd_reloc_code_real_type reloc)
4412 {
4413 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4414 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4415 : BFD_RELOC_LO16));
4416 }
4417
4418 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
4419 relocation. */
4420
4421 static inline bfd_boolean
4422 fixup_has_matching_lo_p (fixS *fixp)
4423 {
4424 return (fixp->fx_next != NULL
4425 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
4426 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4427 && fixp->fx_offset == fixp->fx_next->fx_offset);
4428 }
4429
4430 /* Move all labels in LABELS to the current insertion point. TEXT_P
4431 says whether the labels refer to text or data. */
4432
4433 static void
4434 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
4435 {
4436 struct insn_label_list *l;
4437 valueT val;
4438
4439 for (l = labels; l != NULL; l = l->next)
4440 {
4441 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
4442 symbol_set_frag (l->label, frag_now);
4443 val = (valueT) frag_now_fix ();
4444 /* MIPS16/microMIPS text labels are stored as odd.
4445 We just carry the ISA mode bit forward. */
4446 if (text_p && HAVE_CODE_COMPRESSION)
4447 val |= (S_GET_VALUE (l->label) & 0x1);
4448 S_SET_VALUE (l->label, val);
4449 }
4450 }
4451
4452 /* Move all labels in insn_labels to the current insertion point
4453 and treat them as text labels. */
4454
4455 static void
4456 mips_move_text_labels (void)
4457 {
4458 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4459 }
4460
4461 /* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4462
4463 static bfd_boolean
4464 s_is_linkonce (symbolS *sym, segT from_seg)
4465 {
4466 bfd_boolean linkonce = FALSE;
4467 segT symseg = S_GET_SEGMENT (sym);
4468
4469 if (symseg != from_seg && !S_IS_LOCAL (sym))
4470 {
4471 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4472 linkonce = TRUE;
4473 /* The GNU toolchain uses an extension for ELF: a section
4474 beginning with the magic string .gnu.linkonce is a
4475 linkonce section. */
4476 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4477 sizeof ".gnu.linkonce" - 1) == 0)
4478 linkonce = TRUE;
4479 }
4480 return linkonce;
4481 }
4482
4483 /* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
4484 linker to handle them specially, such as generating jalx instructions
4485 when needed. We also make them odd for the duration of the assembly,
4486 in order to generate the right sort of code. We will make them even
4487 in the adjust_symtab routine, while leaving them marked. This is
4488 convenient for the debugger and the disassembler. The linker knows
4489 to make them odd again. */
4490
4491 static void
4492 mips_compressed_mark_label (symbolS *label)
4493 {
4494 gas_assert (HAVE_CODE_COMPRESSION);
4495
4496 if (mips_opts.mips16)
4497 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4498 else
4499 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
4500 if ((S_GET_VALUE (label) & 1) == 0
4501 /* Don't adjust the address if the label is global or weak, or
4502 in a link-once section, since we'll be emitting symbol reloc
4503 references to it which will be patched up by the linker, and
4504 the final value of the symbol may or may not be MIPS16/microMIPS. */
4505 && !S_IS_WEAK (label)
4506 && !S_IS_EXTERNAL (label)
4507 && !s_is_linkonce (label, now_seg))
4508 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4509 }
4510
4511 /* Mark preceding MIPS16 or microMIPS instruction labels. */
4512
4513 static void
4514 mips_compressed_mark_labels (void)
4515 {
4516 struct insn_label_list *l;
4517
4518 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4519 mips_compressed_mark_label (l->label);
4520 }
4521
4522 /* End the current frag. Make it a variant frag and record the
4523 relaxation info. */
4524
4525 static void
4526 relax_close_frag (void)
4527 {
4528 mips_macro_warning.first_frag = frag_now;
4529 frag_var (rs_machine_dependent, 0, 0,
4530 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4531 mips_pic != NO_PIC),
4532 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4533
4534 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4535 mips_relax.first_fixup = 0;
4536 }
4537
4538 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
4539 See the comment above RELAX_ENCODE for more details. */
4540
4541 static void
4542 relax_start (symbolS *symbol)
4543 {
4544 gas_assert (mips_relax.sequence == 0);
4545 mips_relax.sequence = 1;
4546 mips_relax.symbol = symbol;
4547 }
4548
4549 /* Start generating the second version of a relaxable sequence.
4550 See the comment above RELAX_ENCODE for more details. */
4551
4552 static void
4553 relax_switch (void)
4554 {
4555 gas_assert (mips_relax.sequence == 1);
4556 mips_relax.sequence = 2;
4557 }
4558
4559 /* End the current relaxable sequence. */
4560
4561 static void
4562 relax_end (void)
4563 {
4564 gas_assert (mips_relax.sequence == 2);
4565 relax_close_frag ();
4566 mips_relax.sequence = 0;
4567 }
4568
4569 /* Return true if IP is a delayed branch or jump. */
4570
4571 static inline bfd_boolean
4572 delayed_branch_p (const struct mips_cl_insn *ip)
4573 {
4574 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4575 | INSN_COND_BRANCH_DELAY
4576 | INSN_COND_BRANCH_LIKELY)) != 0;
4577 }
4578
4579 /* Return true if IP is a compact branch or jump. */
4580
4581 static inline bfd_boolean
4582 compact_branch_p (const struct mips_cl_insn *ip)
4583 {
4584 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4585 | INSN2_COND_BRANCH)) != 0;
4586 }
4587
4588 /* Return true if IP is an unconditional branch or jump. */
4589
4590 static inline bfd_boolean
4591 uncond_branch_p (const struct mips_cl_insn *ip)
4592 {
4593 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
4594 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
4595 }
4596
4597 /* Return true if IP is a branch-likely instruction. */
4598
4599 static inline bfd_boolean
4600 branch_likely_p (const struct mips_cl_insn *ip)
4601 {
4602 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4603 }
4604
4605 /* Return the type of nop that should be used to fill the delay slot
4606 of delayed branch IP. */
4607
4608 static struct mips_cl_insn *
4609 get_delay_slot_nop (const struct mips_cl_insn *ip)
4610 {
4611 if (mips_opts.micromips
4612 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4613 return &micromips_nop32_insn;
4614 return NOP_INSN;
4615 }
4616
4617 /* Return a mask that has bit N set if OPCODE reads the register(s)
4618 in operand N. */
4619
4620 static unsigned int
4621 insn_read_mask (const struct mips_opcode *opcode)
4622 {
4623 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4624 }
4625
4626 /* Return a mask that has bit N set if OPCODE writes to the register(s)
4627 in operand N. */
4628
4629 static unsigned int
4630 insn_write_mask (const struct mips_opcode *opcode)
4631 {
4632 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4633 }
4634
4635 /* Return a mask of the registers specified by operand OPERAND of INSN.
4636 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4637 is set. */
4638
4639 static unsigned int
4640 operand_reg_mask (const struct mips_cl_insn *insn,
4641 const struct mips_operand *operand,
4642 unsigned int type_mask)
4643 {
4644 unsigned int uval, vsel;
4645
4646 switch (operand->type)
4647 {
4648 case OP_INT:
4649 case OP_MAPPED_INT:
4650 case OP_MSB:
4651 case OP_PCREL:
4652 case OP_PERF_REG:
4653 case OP_ADDIUSP_INT:
4654 case OP_ENTRY_EXIT_LIST:
4655 case OP_REPEAT_DEST_REG:
4656 case OP_REPEAT_PREV_REG:
4657 case OP_PC:
4658 case OP_VU0_SUFFIX:
4659 case OP_VU0_MATCH_SUFFIX:
4660 case OP_IMM_INDEX:
4661 abort ();
4662
4663 case OP_REG28:
4664 return 1 << 28;
4665
4666 case OP_REG:
4667 case OP_OPTIONAL_REG:
4668 {
4669 const struct mips_reg_operand *reg_op;
4670
4671 reg_op = (const struct mips_reg_operand *) operand;
4672 if (!(type_mask & (1 << reg_op->reg_type)))
4673 return 0;
4674 uval = insn_extract_operand (insn, operand);
4675 return 1 << mips_decode_reg_operand (reg_op, uval);
4676 }
4677
4678 case OP_REG_PAIR:
4679 {
4680 const struct mips_reg_pair_operand *pair_op;
4681
4682 pair_op = (const struct mips_reg_pair_operand *) operand;
4683 if (!(type_mask & (1 << pair_op->reg_type)))
4684 return 0;
4685 uval = insn_extract_operand (insn, operand);
4686 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4687 }
4688
4689 case OP_CLO_CLZ_DEST:
4690 if (!(type_mask & (1 << OP_REG_GP)))
4691 return 0;
4692 uval = insn_extract_operand (insn, operand);
4693 return (1 << (uval & 31)) | (1 << (uval >> 5));
4694
4695 case OP_SAME_RS_RT:
4696 if (!(type_mask & (1 << OP_REG_GP)))
4697 return 0;
4698 uval = insn_extract_operand (insn, operand);
4699 gas_assert ((uval & 31) == (uval >> 5));
4700 return 1 << (uval & 31);
4701
4702 case OP_CHECK_PREV:
4703 case OP_NON_ZERO_REG:
4704 if (!(type_mask & (1 << OP_REG_GP)))
4705 return 0;
4706 uval = insn_extract_operand (insn, operand);
4707 return 1 << (uval & 31);
4708
4709 case OP_LWM_SWM_LIST:
4710 abort ();
4711
4712 case OP_SAVE_RESTORE_LIST:
4713 abort ();
4714
4715 case OP_MDMX_IMM_REG:
4716 if (!(type_mask & (1 << OP_REG_VEC)))
4717 return 0;
4718 uval = insn_extract_operand (insn, operand);
4719 vsel = uval >> 5;
4720 if ((vsel & 0x18) == 0x18)
4721 return 0;
4722 return 1 << (uval & 31);
4723
4724 case OP_REG_INDEX:
4725 if (!(type_mask & (1 << OP_REG_GP)))
4726 return 0;
4727 return 1 << insn_extract_operand (insn, operand);
4728 }
4729 abort ();
4730 }
4731
4732 /* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4733 where bit N of OPNO_MASK is set if operand N should be included.
4734 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4735 is set. */
4736
4737 static unsigned int
4738 insn_reg_mask (const struct mips_cl_insn *insn,
4739 unsigned int type_mask, unsigned int opno_mask)
4740 {
4741 unsigned int opno, reg_mask;
4742
4743 opno = 0;
4744 reg_mask = 0;
4745 while (opno_mask != 0)
4746 {
4747 if (opno_mask & 1)
4748 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4749 opno_mask >>= 1;
4750 opno += 1;
4751 }
4752 return reg_mask;
4753 }
4754
4755 /* Return the mask of core registers that IP reads. */
4756
4757 static unsigned int
4758 gpr_read_mask (const struct mips_cl_insn *ip)
4759 {
4760 unsigned long pinfo, pinfo2;
4761 unsigned int mask;
4762
4763 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4764 pinfo = ip->insn_mo->pinfo;
4765 pinfo2 = ip->insn_mo->pinfo2;
4766 if (pinfo & INSN_UDI)
4767 {
4768 /* UDI instructions have traditionally been assumed to read RS
4769 and RT. */
4770 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4771 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4772 }
4773 if (pinfo & INSN_READ_GPR_24)
4774 mask |= 1 << 24;
4775 if (pinfo2 & INSN2_READ_GPR_16)
4776 mask |= 1 << 16;
4777 if (pinfo2 & INSN2_READ_SP)
4778 mask |= 1 << SP;
4779 if (pinfo2 & INSN2_READ_GPR_31)
4780 mask |= 1 << 31;
4781 /* Don't include register 0. */
4782 return mask & ~1;
4783 }
4784
4785 /* Return the mask of core registers that IP writes. */
4786
4787 static unsigned int
4788 gpr_write_mask (const struct mips_cl_insn *ip)
4789 {
4790 unsigned long pinfo, pinfo2;
4791 unsigned int mask;
4792
4793 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4794 pinfo = ip->insn_mo->pinfo;
4795 pinfo2 = ip->insn_mo->pinfo2;
4796 if (pinfo & INSN_WRITE_GPR_24)
4797 mask |= 1 << 24;
4798 if (pinfo & INSN_WRITE_GPR_31)
4799 mask |= 1 << 31;
4800 if (pinfo & INSN_UDI)
4801 /* UDI instructions have traditionally been assumed to write to RD. */
4802 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4803 if (pinfo2 & INSN2_WRITE_SP)
4804 mask |= 1 << SP;
4805 /* Don't include register 0. */
4806 return mask & ~1;
4807 }
4808
4809 /* Return the mask of floating-point registers that IP reads. */
4810
4811 static unsigned int
4812 fpr_read_mask (const struct mips_cl_insn *ip)
4813 {
4814 unsigned long pinfo;
4815 unsigned int mask;
4816
4817 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4818 | (1 << OP_REG_MSA)),
4819 insn_read_mask (ip->insn_mo));
4820 pinfo = ip->insn_mo->pinfo;
4821 /* Conservatively treat all operands to an FP_D instruction are doubles.
4822 (This is overly pessimistic for things like cvt.d.s.) */
4823 if (FPR_SIZE != 64 && (pinfo & FP_D))
4824 mask |= mask << 1;
4825 return mask;
4826 }
4827
4828 /* Return the mask of floating-point registers that IP writes. */
4829
4830 static unsigned int
4831 fpr_write_mask (const struct mips_cl_insn *ip)
4832 {
4833 unsigned long pinfo;
4834 unsigned int mask;
4835
4836 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4837 | (1 << OP_REG_MSA)),
4838 insn_write_mask (ip->insn_mo));
4839 pinfo = ip->insn_mo->pinfo;
4840 /* Conservatively treat all operands to an FP_D instruction are doubles.
4841 (This is overly pessimistic for things like cvt.s.d.) */
4842 if (FPR_SIZE != 64 && (pinfo & FP_D))
4843 mask |= mask << 1;
4844 return mask;
4845 }
4846
4847 /* Operand OPNUM of INSN is an odd-numbered floating-point register.
4848 Check whether that is allowed. */
4849
4850 static bfd_boolean
4851 mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4852 {
4853 const char *s = insn->name;
4854 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4855 || FPR_SIZE == 64)
4856 && mips_opts.oddspreg;
4857
4858 if (insn->pinfo == INSN_MACRO)
4859 /* Let a macro pass, we'll catch it later when it is expanded. */
4860 return TRUE;
4861
4862 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4863 otherwise it depends on oddspreg. */
4864 if ((insn->pinfo & FP_S)
4865 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
4866 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
4867 return FPR_SIZE == 32 || oddspreg;
4868
4869 /* Allow odd registers for single-precision ops and double-precision if the
4870 floating-point registers are 64-bit wide. */
4871 switch (insn->pinfo & (FP_S | FP_D))
4872 {
4873 case FP_S:
4874 case 0:
4875 return oddspreg;
4876 case FP_D:
4877 return FPR_SIZE == 64;
4878 default:
4879 break;
4880 }
4881
4882 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4883 s = strchr (insn->name, '.');
4884 if (s != NULL && opnum == 2)
4885 s = strchr (s + 1, '.');
4886 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4887 return oddspreg;
4888
4889 return FPR_SIZE == 64;
4890 }
4891
4892 /* Information about an instruction argument that we're trying to match. */
4893 struct mips_arg_info
4894 {
4895 /* The instruction so far. */
4896 struct mips_cl_insn *insn;
4897
4898 /* The first unconsumed operand token. */
4899 struct mips_operand_token *token;
4900
4901 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4902 int opnum;
4903
4904 /* The 1-based argument number, for error reporting. This does not
4905 count elided optional registers, etc.. */
4906 int argnum;
4907
4908 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4909 unsigned int last_regno;
4910
4911 /* If the first operand was an OP_REG, this is the register that it
4912 specified, otherwise it is ILLEGAL_REG. */
4913 unsigned int dest_regno;
4914
4915 /* The value of the last OP_INT operand. Only used for OP_MSB,
4916 where it gives the lsb position. */
4917 unsigned int last_op_int;
4918
4919 /* If true, match routines should assume that no later instruction
4920 alternative matches and should therefore be as accommodating as
4921 possible. Match routines should not report errors if something
4922 is only invalid for !LAX_MATCH. */
4923 bfd_boolean lax_match;
4924
4925 /* True if a reference to the current AT register was seen. */
4926 bfd_boolean seen_at;
4927 };
4928
4929 /* Record that the argument is out of range. */
4930
4931 static void
4932 match_out_of_range (struct mips_arg_info *arg)
4933 {
4934 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4935 }
4936
4937 /* Record that the argument isn't constant but needs to be. */
4938
4939 static void
4940 match_not_constant (struct mips_arg_info *arg)
4941 {
4942 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4943 arg->argnum);
4944 }
4945
4946 /* Try to match an OT_CHAR token for character CH. Consume the token
4947 and return true on success, otherwise return false. */
4948
4949 static bfd_boolean
4950 match_char (struct mips_arg_info *arg, char ch)
4951 {
4952 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4953 {
4954 ++arg->token;
4955 if (ch == ',')
4956 arg->argnum += 1;
4957 return TRUE;
4958 }
4959 return FALSE;
4960 }
4961
4962 /* Try to get an expression from the next tokens in ARG. Consume the
4963 tokens and return true on success, storing the expression value in
4964 VALUE and relocation types in R. */
4965
4966 static bfd_boolean
4967 match_expression (struct mips_arg_info *arg, expressionS *value,
4968 bfd_reloc_code_real_type *r)
4969 {
4970 /* If the next token is a '(' that was parsed as being part of a base
4971 expression, assume we have an elided offset. The later match will fail
4972 if this turns out to be wrong. */
4973 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4974 {
4975 value->X_op = O_constant;
4976 value->X_add_number = 0;
4977 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
4978 return TRUE;
4979 }
4980
4981 /* Reject register-based expressions such as "0+$2" and "(($2))".
4982 For plain registers the default error seems more appropriate. */
4983 if (arg->token->type == OT_INTEGER
4984 && arg->token->u.integer.value.X_op == O_register)
4985 {
4986 set_insn_error (arg->argnum, _("register value used as expression"));
4987 return FALSE;
4988 }
4989
4990 if (arg->token->type == OT_INTEGER)
4991 {
4992 *value = arg->token->u.integer.value;
4993 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4994 ++arg->token;
4995 return TRUE;
4996 }
4997
4998 set_insn_error_i
4999 (arg->argnum, _("operand %d must be an immediate expression"),
5000 arg->argnum);
5001 return FALSE;
5002 }
5003
5004 /* Try to get a constant expression from the next tokens in ARG. Consume
5005 the tokens and return true on success, storing the constant value
5006 in *VALUE. */
5007
5008 static bfd_boolean
5009 match_const_int (struct mips_arg_info *arg, offsetT *value)
5010 {
5011 expressionS ex;
5012 bfd_reloc_code_real_type r[3];
5013
5014 if (!match_expression (arg, &ex, r))
5015 return FALSE;
5016
5017 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
5018 *value = ex.X_add_number;
5019 else
5020 {
5021 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
5022 match_out_of_range (arg);
5023 else
5024 match_not_constant (arg);
5025 return FALSE;
5026 }
5027 return TRUE;
5028 }
5029
5030 /* Return the RTYPE_* flags for a register operand of type TYPE that
5031 appears in instruction OPCODE. */
5032
5033 static unsigned int
5034 convert_reg_type (const struct mips_opcode *opcode,
5035 enum mips_reg_operand_type type)
5036 {
5037 switch (type)
5038 {
5039 case OP_REG_GP:
5040 return RTYPE_NUM | RTYPE_GP;
5041
5042 case OP_REG_FP:
5043 /* Allow vector register names for MDMX if the instruction is a 64-bit
5044 FPR load, store or move (including moves to and from GPRs). */
5045 if ((mips_opts.ase & ASE_MDMX)
5046 && (opcode->pinfo & FP_D)
5047 && (opcode->pinfo & (INSN_COPROC_MOVE
5048 | INSN_COPROC_MEMORY_DELAY
5049 | INSN_LOAD_COPROC
5050 | INSN_LOAD_MEMORY
5051 | INSN_STORE_MEMORY)))
5052 return RTYPE_FPU | RTYPE_VEC;
5053 return RTYPE_FPU;
5054
5055 case OP_REG_CCC:
5056 if (opcode->pinfo & (FP_D | FP_S))
5057 return RTYPE_CCC | RTYPE_FCC;
5058 return RTYPE_CCC;
5059
5060 case OP_REG_VEC:
5061 if (opcode->membership & INSN_5400)
5062 return RTYPE_FPU;
5063 return RTYPE_FPU | RTYPE_VEC;
5064
5065 case OP_REG_ACC:
5066 return RTYPE_ACC;
5067
5068 case OP_REG_COPRO:
5069 if (opcode->name[strlen (opcode->name) - 1] == '0')
5070 return RTYPE_NUM | RTYPE_CP0;
5071 return RTYPE_NUM;
5072
5073 case OP_REG_HW:
5074 return RTYPE_NUM;
5075
5076 case OP_REG_VI:
5077 return RTYPE_NUM | RTYPE_VI;
5078
5079 case OP_REG_VF:
5080 return RTYPE_NUM | RTYPE_VF;
5081
5082 case OP_REG_R5900_I:
5083 return RTYPE_R5900_I;
5084
5085 case OP_REG_R5900_Q:
5086 return RTYPE_R5900_Q;
5087
5088 case OP_REG_R5900_R:
5089 return RTYPE_R5900_R;
5090
5091 case OP_REG_R5900_ACC:
5092 return RTYPE_R5900_ACC;
5093
5094 case OP_REG_MSA:
5095 return RTYPE_MSA;
5096
5097 case OP_REG_MSA_CTRL:
5098 return RTYPE_NUM;
5099 }
5100 abort ();
5101 }
5102
5103 /* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
5104
5105 static void
5106 check_regno (struct mips_arg_info *arg,
5107 enum mips_reg_operand_type type, unsigned int regno)
5108 {
5109 if (AT && type == OP_REG_GP && regno == AT)
5110 arg->seen_at = TRUE;
5111
5112 if (type == OP_REG_FP
5113 && (regno & 1) != 0
5114 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
5115 {
5116 /* This was a warning prior to introducing O32 FPXX and FP64 support
5117 so maintain a warning for FP32 but raise an error for the new
5118 cases. */
5119 if (FPR_SIZE == 32)
5120 as_warn (_("float register should be even, was %d"), regno);
5121 else
5122 as_bad (_("float register should be even, was %d"), regno);
5123 }
5124
5125 if (type == OP_REG_CCC)
5126 {
5127 const char *name;
5128 size_t length;
5129
5130 name = arg->insn->insn_mo->name;
5131 length = strlen (name);
5132 if ((regno & 1) != 0
5133 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5134 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
5135 as_warn (_("condition code register should be even for %s, was %d"),
5136 name, regno);
5137
5138 if ((regno & 3) != 0
5139 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
5140 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
5141 name, regno);
5142 }
5143 }
5144
5145 /* ARG is a register with symbol value SYMVAL. Try to interpret it as
5146 a register of type TYPE. Return true on success, storing the register
5147 number in *REGNO and warning about any dubious uses. */
5148
5149 static bfd_boolean
5150 match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5151 unsigned int symval, unsigned int *regno)
5152 {
5153 if (type == OP_REG_VEC)
5154 symval = mips_prefer_vec_regno (symval);
5155 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5156 return FALSE;
5157
5158 *regno = symval & RNUM_MASK;
5159 check_regno (arg, type, *regno);
5160 return TRUE;
5161 }
5162
5163 /* Try to interpret the next token in ARG as a register of type TYPE.
5164 Consume the token and return true on success, storing the register
5165 number in *REGNO. Return false on failure. */
5166
5167 static bfd_boolean
5168 match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5169 unsigned int *regno)
5170 {
5171 if (arg->token->type == OT_REG
5172 && match_regno (arg, type, arg->token->u.regno, regno))
5173 {
5174 ++arg->token;
5175 return TRUE;
5176 }
5177 return FALSE;
5178 }
5179
5180 /* Try to interpret the next token in ARG as a range of registers of type TYPE.
5181 Consume the token and return true on success, storing the register numbers
5182 in *REGNO1 and *REGNO2. Return false on failure. */
5183
5184 static bfd_boolean
5185 match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5186 unsigned int *regno1, unsigned int *regno2)
5187 {
5188 if (match_reg (arg, type, regno1))
5189 {
5190 *regno2 = *regno1;
5191 return TRUE;
5192 }
5193 if (arg->token->type == OT_REG_RANGE
5194 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5195 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5196 && *regno1 <= *regno2)
5197 {
5198 ++arg->token;
5199 return TRUE;
5200 }
5201 return FALSE;
5202 }
5203
5204 /* OP_INT matcher. */
5205
5206 static bfd_boolean
5207 match_int_operand (struct mips_arg_info *arg,
5208 const struct mips_operand *operand_base)
5209 {
5210 const struct mips_int_operand *operand;
5211 unsigned int uval;
5212 int min_val, max_val, factor;
5213 offsetT sval;
5214
5215 operand = (const struct mips_int_operand *) operand_base;
5216 factor = 1 << operand->shift;
5217 min_val = mips_int_operand_min (operand);
5218 max_val = mips_int_operand_max (operand);
5219
5220 if (operand_base->lsb == 0
5221 && operand_base->size == 16
5222 && operand->shift == 0
5223 && operand->bias == 0
5224 && (operand->max_val == 32767 || operand->max_val == 65535))
5225 {
5226 /* The operand can be relocated. */
5227 if (!match_expression (arg, &offset_expr, offset_reloc))
5228 return FALSE;
5229
5230 if (offset_expr.X_op == O_big)
5231 {
5232 match_out_of_range (arg);
5233 return FALSE;
5234 }
5235
5236 if (offset_reloc[0] != BFD_RELOC_UNUSED)
5237 /* Relocation operators were used. Accept the argument and
5238 leave the relocation value in offset_expr and offset_relocs
5239 for the caller to process. */
5240 return TRUE;
5241
5242 if (offset_expr.X_op != O_constant)
5243 {
5244 /* Accept non-constant operands if no later alternative matches,
5245 leaving it for the caller to process. */
5246 if (!arg->lax_match)
5247 {
5248 match_not_constant (arg);
5249 return FALSE;
5250 }
5251 offset_reloc[0] = BFD_RELOC_LO16;
5252 return TRUE;
5253 }
5254
5255 /* Clear the global state; we're going to install the operand
5256 ourselves. */
5257 sval = offset_expr.X_add_number;
5258 offset_expr.X_op = O_absent;
5259
5260 /* For compatibility with older assemblers, we accept
5261 0x8000-0xffff as signed 16-bit numbers when only
5262 signed numbers are allowed. */
5263 if (sval > max_val)
5264 {
5265 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5266 if (!arg->lax_match && sval <= max_val)
5267 {
5268 match_out_of_range (arg);
5269 return FALSE;
5270 }
5271 }
5272 }
5273 else
5274 {
5275 if (!match_const_int (arg, &sval))
5276 return FALSE;
5277 }
5278
5279 arg->last_op_int = sval;
5280
5281 if (sval < min_val || sval > max_val || sval % factor)
5282 {
5283 match_out_of_range (arg);
5284 return FALSE;
5285 }
5286
5287 uval = (unsigned int) sval >> operand->shift;
5288 uval -= operand->bias;
5289
5290 /* Handle -mfix-cn63xxp1. */
5291 if (arg->opnum == 1
5292 && mips_fix_cn63xxp1
5293 && !mips_opts.micromips
5294 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5295 switch (uval)
5296 {
5297 case 5:
5298 case 25:
5299 case 26:
5300 case 27:
5301 case 28:
5302 case 29:
5303 case 30:
5304 case 31:
5305 /* These are ok. */
5306 break;
5307
5308 default:
5309 /* The rest must be changed to 28. */
5310 uval = 28;
5311 break;
5312 }
5313
5314 insn_insert_operand (arg->insn, operand_base, uval);
5315 return TRUE;
5316 }
5317
5318 /* OP_MAPPED_INT matcher. */
5319
5320 static bfd_boolean
5321 match_mapped_int_operand (struct mips_arg_info *arg,
5322 const struct mips_operand *operand_base)
5323 {
5324 const struct mips_mapped_int_operand *operand;
5325 unsigned int uval, num_vals;
5326 offsetT sval;
5327
5328 operand = (const struct mips_mapped_int_operand *) operand_base;
5329 if (!match_const_int (arg, &sval))
5330 return FALSE;
5331
5332 num_vals = 1 << operand_base->size;
5333 for (uval = 0; uval < num_vals; uval++)
5334 if (operand->int_map[uval] == sval)
5335 break;
5336 if (uval == num_vals)
5337 {
5338 match_out_of_range (arg);
5339 return FALSE;
5340 }
5341
5342 insn_insert_operand (arg->insn, operand_base, uval);
5343 return TRUE;
5344 }
5345
5346 /* OP_MSB matcher. */
5347
5348 static bfd_boolean
5349 match_msb_operand (struct mips_arg_info *arg,
5350 const struct mips_operand *operand_base)
5351 {
5352 const struct mips_msb_operand *operand;
5353 int min_val, max_val, max_high;
5354 offsetT size, sval, high;
5355
5356 operand = (const struct mips_msb_operand *) operand_base;
5357 min_val = operand->bias;
5358 max_val = min_val + (1 << operand_base->size) - 1;
5359 max_high = operand->opsize;
5360
5361 if (!match_const_int (arg, &size))
5362 return FALSE;
5363
5364 high = size + arg->last_op_int;
5365 sval = operand->add_lsb ? high : size;
5366
5367 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5368 {
5369 match_out_of_range (arg);
5370 return FALSE;
5371 }
5372 insn_insert_operand (arg->insn, operand_base, sval - min_val);
5373 return TRUE;
5374 }
5375
5376 /* OP_REG matcher. */
5377
5378 static bfd_boolean
5379 match_reg_operand (struct mips_arg_info *arg,
5380 const struct mips_operand *operand_base)
5381 {
5382 const struct mips_reg_operand *operand;
5383 unsigned int regno, uval, num_vals;
5384
5385 operand = (const struct mips_reg_operand *) operand_base;
5386 if (!match_reg (arg, operand->reg_type, &regno))
5387 return FALSE;
5388
5389 if (operand->reg_map)
5390 {
5391 num_vals = 1 << operand->root.size;
5392 for (uval = 0; uval < num_vals; uval++)
5393 if (operand->reg_map[uval] == regno)
5394 break;
5395 if (num_vals == uval)
5396 return FALSE;
5397 }
5398 else
5399 uval = regno;
5400
5401 arg->last_regno = regno;
5402 if (arg->opnum == 1)
5403 arg->dest_regno = regno;
5404 insn_insert_operand (arg->insn, operand_base, uval);
5405 return TRUE;
5406 }
5407
5408 /* OP_REG_PAIR matcher. */
5409
5410 static bfd_boolean
5411 match_reg_pair_operand (struct mips_arg_info *arg,
5412 const struct mips_operand *operand_base)
5413 {
5414 const struct mips_reg_pair_operand *operand;
5415 unsigned int regno1, regno2, uval, num_vals;
5416
5417 operand = (const struct mips_reg_pair_operand *) operand_base;
5418 if (!match_reg (arg, operand->reg_type, &regno1)
5419 || !match_char (arg, ',')
5420 || !match_reg (arg, operand->reg_type, &regno2))
5421 return FALSE;
5422
5423 num_vals = 1 << operand_base->size;
5424 for (uval = 0; uval < num_vals; uval++)
5425 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5426 break;
5427 if (uval == num_vals)
5428 return FALSE;
5429
5430 insn_insert_operand (arg->insn, operand_base, uval);
5431 return TRUE;
5432 }
5433
5434 /* OP_PCREL matcher. The caller chooses the relocation type. */
5435
5436 static bfd_boolean
5437 match_pcrel_operand (struct mips_arg_info *arg)
5438 {
5439 bfd_reloc_code_real_type r[3];
5440
5441 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
5442 }
5443
5444 /* OP_PERF_REG matcher. */
5445
5446 static bfd_boolean
5447 match_perf_reg_operand (struct mips_arg_info *arg,
5448 const struct mips_operand *operand)
5449 {
5450 offsetT sval;
5451
5452 if (!match_const_int (arg, &sval))
5453 return FALSE;
5454
5455 if (sval != 0
5456 && (sval != 1
5457 || (mips_opts.arch == CPU_R5900
5458 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5459 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5460 {
5461 set_insn_error (arg->argnum, _("invalid performance register"));
5462 return FALSE;
5463 }
5464
5465 insn_insert_operand (arg->insn, operand, sval);
5466 return TRUE;
5467 }
5468
5469 /* OP_ADDIUSP matcher. */
5470
5471 static bfd_boolean
5472 match_addiusp_operand (struct mips_arg_info *arg,
5473 const struct mips_operand *operand)
5474 {
5475 offsetT sval;
5476 unsigned int uval;
5477
5478 if (!match_const_int (arg, &sval))
5479 return FALSE;
5480
5481 if (sval % 4)
5482 {
5483 match_out_of_range (arg);
5484 return FALSE;
5485 }
5486
5487 sval /= 4;
5488 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
5489 {
5490 match_out_of_range (arg);
5491 return FALSE;
5492 }
5493
5494 uval = (unsigned int) sval;
5495 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5496 insn_insert_operand (arg->insn, operand, uval);
5497 return TRUE;
5498 }
5499
5500 /* OP_CLO_CLZ_DEST matcher. */
5501
5502 static bfd_boolean
5503 match_clo_clz_dest_operand (struct mips_arg_info *arg,
5504 const struct mips_operand *operand)
5505 {
5506 unsigned int regno;
5507
5508 if (!match_reg (arg, OP_REG_GP, &regno))
5509 return FALSE;
5510
5511 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5512 return TRUE;
5513 }
5514
5515 /* OP_CHECK_PREV matcher. */
5516
5517 static bfd_boolean
5518 match_check_prev_operand (struct mips_arg_info *arg,
5519 const struct mips_operand *operand_base)
5520 {
5521 const struct mips_check_prev_operand *operand;
5522 unsigned int regno;
5523
5524 operand = (const struct mips_check_prev_operand *) operand_base;
5525
5526 if (!match_reg (arg, OP_REG_GP, &regno))
5527 return FALSE;
5528
5529 if (!operand->zero_ok && regno == 0)
5530 return FALSE;
5531
5532 if ((operand->less_than_ok && regno < arg->last_regno)
5533 || (operand->greater_than_ok && regno > arg->last_regno)
5534 || (operand->equal_ok && regno == arg->last_regno))
5535 {
5536 arg->last_regno = regno;
5537 insn_insert_operand (arg->insn, operand_base, regno);
5538 return TRUE;
5539 }
5540
5541 return FALSE;
5542 }
5543
5544 /* OP_SAME_RS_RT matcher. */
5545
5546 static bfd_boolean
5547 match_same_rs_rt_operand (struct mips_arg_info *arg,
5548 const struct mips_operand *operand)
5549 {
5550 unsigned int regno;
5551
5552 if (!match_reg (arg, OP_REG_GP, &regno))
5553 return FALSE;
5554
5555 if (regno == 0)
5556 {
5557 set_insn_error (arg->argnum, _("the source register must not be $0"));
5558 return FALSE;
5559 }
5560
5561 arg->last_regno = regno;
5562
5563 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5564 return TRUE;
5565 }
5566
5567 /* OP_LWM_SWM_LIST matcher. */
5568
5569 static bfd_boolean
5570 match_lwm_swm_list_operand (struct mips_arg_info *arg,
5571 const struct mips_operand *operand)
5572 {
5573 unsigned int reglist, sregs, ra, regno1, regno2;
5574 struct mips_arg_info reset;
5575
5576 reglist = 0;
5577 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5578 return FALSE;
5579 do
5580 {
5581 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5582 {
5583 reglist |= 1 << FP;
5584 regno2 = S7;
5585 }
5586 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5587 reset = *arg;
5588 }
5589 while (match_char (arg, ',')
5590 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5591 *arg = reset;
5592
5593 if (operand->size == 2)
5594 {
5595 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5596
5597 s0, ra
5598 s0, s1, ra, s2, s3
5599 s0-s2, ra
5600
5601 and any permutations of these. */
5602 if ((reglist & 0xfff1ffff) != 0x80010000)
5603 return FALSE;
5604
5605 sregs = (reglist >> 17) & 7;
5606 ra = 0;
5607 }
5608 else
5609 {
5610 /* The list must include at least one of ra and s0-sN,
5611 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5612 which are $23 and $30 respectively.) E.g.:
5613
5614 ra
5615 s0
5616 ra, s0, s1, s2
5617 s0-s8
5618 s0-s5, ra
5619
5620 and any permutations of these. */
5621 if ((reglist & 0x3f00ffff) != 0)
5622 return FALSE;
5623
5624 ra = (reglist >> 27) & 0x10;
5625 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5626 }
5627 sregs += 1;
5628 if ((sregs & -sregs) != sregs)
5629 return FALSE;
5630
5631 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
5632 return TRUE;
5633 }
5634
5635 /* OP_ENTRY_EXIT_LIST matcher. */
5636
5637 static unsigned int
5638 match_entry_exit_operand (struct mips_arg_info *arg,
5639 const struct mips_operand *operand)
5640 {
5641 unsigned int mask;
5642 bfd_boolean is_exit;
5643
5644 /* The format is the same for both ENTRY and EXIT, but the constraints
5645 are different. */
5646 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5647 mask = (is_exit ? 7 << 3 : 0);
5648 do
5649 {
5650 unsigned int regno1, regno2;
5651 bfd_boolean is_freg;
5652
5653 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5654 is_freg = FALSE;
5655 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
5656 is_freg = TRUE;
5657 else
5658 return FALSE;
5659
5660 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5661 {
5662 mask &= ~(7 << 3);
5663 mask |= (5 + regno2) << 3;
5664 }
5665 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5666 mask |= (regno2 - 3) << 3;
5667 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5668 mask |= (regno2 - 15) << 1;
5669 else if (regno1 == RA && regno2 == RA)
5670 mask |= 1;
5671 else
5672 return FALSE;
5673 }
5674 while (match_char (arg, ','));
5675
5676 insn_insert_operand (arg->insn, operand, mask);
5677 return TRUE;
5678 }
5679
5680 /* Encode regular MIPS SAVE/RESTORE instruction operands according to
5681 the argument register mask AMASK, the number of static registers
5682 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5683 respectively, and the frame size FRAME_SIZE. */
5684
5685 static unsigned int
5686 mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5687 unsigned int ra, unsigned int s0, unsigned int s1,
5688 unsigned int frame_size)
5689 {
5690 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5691 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5692 }
5693
5694 /* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5695 argument register mask AMASK, the number of static registers saved
5696 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5697 respectively, and the frame size FRAME_SIZE. */
5698
5699 static unsigned int
5700 mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5701 unsigned int ra, unsigned int s0, unsigned int s1,
5702 unsigned int frame_size)
5703 {
5704 unsigned int args;
5705
5706 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5707 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5708 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5709 | ((frame_size & 0xf0) << 16));
5710 return args;
5711 }
5712
5713 /* OP_SAVE_RESTORE_LIST matcher. */
5714
5715 static bfd_boolean
5716 match_save_restore_list_operand (struct mips_arg_info *arg)
5717 {
5718 unsigned int opcode, args, statics, sregs;
5719 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
5720 unsigned int arg_mask, ra, s0, s1;
5721 offsetT frame_size;
5722
5723 opcode = arg->insn->insn_opcode;
5724 frame_size = 0;
5725 num_frame_sizes = 0;
5726 args = 0;
5727 statics = 0;
5728 sregs = 0;
5729 ra = 0;
5730 s0 = 0;
5731 s1 = 0;
5732 do
5733 {
5734 unsigned int regno1, regno2;
5735
5736 if (arg->token->type == OT_INTEGER)
5737 {
5738 /* Handle the frame size. */
5739 if (!match_const_int (arg, &frame_size))
5740 return FALSE;
5741 num_frame_sizes += 1;
5742 }
5743 else
5744 {
5745 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5746 return FALSE;
5747
5748 while (regno1 <= regno2)
5749 {
5750 if (regno1 >= 4 && regno1 <= 7)
5751 {
5752 if (num_frame_sizes == 0)
5753 /* args $a0-$a3 */
5754 args |= 1 << (regno1 - 4);
5755 else
5756 /* statics $a0-$a3 */
5757 statics |= 1 << (regno1 - 4);
5758 }
5759 else if (regno1 >= 16 && regno1 <= 23)
5760 /* $s0-$s7 */
5761 sregs |= 1 << (regno1 - 16);
5762 else if (regno1 == 30)
5763 /* $s8 */
5764 sregs |= 1 << 8;
5765 else if (regno1 == 31)
5766 /* Add $ra to insn. */
5767 ra = 1;
5768 else
5769 return FALSE;
5770 regno1 += 1;
5771 if (regno1 == 24)
5772 regno1 = 30;
5773 }
5774 }
5775 }
5776 while (match_char (arg, ','));
5777
5778 /* Encode args/statics combination. */
5779 if (args & statics)
5780 return FALSE;
5781 else if (args == 0xf)
5782 /* All $a0-$a3 are args. */
5783 arg_mask = MIPS_SVRS_ALL_ARGS;
5784 else if (statics == 0xf)
5785 /* All $a0-$a3 are statics. */
5786 arg_mask = MIPS_SVRS_ALL_STATICS;
5787 else
5788 {
5789 /* Count arg registers. */
5790 num_args = 0;
5791 while (args & 0x1)
5792 {
5793 args >>= 1;
5794 num_args += 1;
5795 }
5796 if (args != 0)
5797 return FALSE;
5798
5799 /* Count static registers. */
5800 num_statics = 0;
5801 while (statics & 0x8)
5802 {
5803 statics = (statics << 1) & 0xf;
5804 num_statics += 1;
5805 }
5806 if (statics != 0)
5807 return FALSE;
5808
5809 /* Encode args/statics. */
5810 arg_mask = (num_args << 2) | num_statics;
5811 }
5812
5813 /* Encode $s0/$s1. */
5814 if (sregs & (1 << 0)) /* $s0 */
5815 s0 = 1;
5816 if (sregs & (1 << 1)) /* $s1 */
5817 s1 = 1;
5818 sregs >>= 2;
5819
5820 /* Encode $s2-$s8. */
5821 num_sregs = 0;
5822 while (sregs & 1)
5823 {
5824 sregs >>= 1;
5825 num_sregs += 1;
5826 }
5827 if (sregs != 0)
5828 return FALSE;
5829
5830 /* Encode frame size. */
5831 if (num_frame_sizes == 0)
5832 {
5833 set_insn_error (arg->argnum, _("missing frame size"));
5834 return FALSE;
5835 }
5836 if (num_frame_sizes > 1)
5837 {
5838 set_insn_error (arg->argnum, _("frame size specified twice"));
5839 return FALSE;
5840 }
5841 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5842 {
5843 set_insn_error (arg->argnum, _("invalid frame size"));
5844 return FALSE;
5845 }
5846 frame_size /= 8;
5847
5848 /* Finally build the instruction. */
5849 if (mips_opts.mips16)
5850 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5851 frame_size);
5852 else if (!mips_opts.micromips)
5853 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5854 frame_size);
5855 else
5856 abort ();
5857
5858 arg->insn->insn_opcode = opcode;
5859 return TRUE;
5860 }
5861
5862 /* OP_MDMX_IMM_REG matcher. */
5863
5864 static bfd_boolean
5865 match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
5866 const struct mips_operand *operand)
5867 {
5868 unsigned int regno, uval;
5869 bfd_boolean is_qh;
5870 const struct mips_opcode *opcode;
5871
5872 /* The mips_opcode records whether this is an octobyte or quadhalf
5873 instruction. Start out with that bit in place. */
5874 opcode = arg->insn->insn_mo;
5875 uval = mips_extract_operand (operand, opcode->match);
5876 is_qh = (uval != 0);
5877
5878 if (arg->token->type == OT_REG)
5879 {
5880 if ((opcode->membership & INSN_5400)
5881 && strcmp (opcode->name, "rzu.ob") == 0)
5882 {
5883 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5884 arg->argnum);
5885 return FALSE;
5886 }
5887
5888 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5889 return FALSE;
5890 ++arg->token;
5891
5892 /* Check whether this is a vector register or a broadcast of
5893 a single element. */
5894 if (arg->token->type == OT_INTEGER_INDEX)
5895 {
5896 if (arg->token->u.index > (is_qh ? 3 : 7))
5897 {
5898 set_insn_error (arg->argnum, _("invalid element selector"));
5899 return FALSE;
5900 }
5901 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5902 ++arg->token;
5903 }
5904 else
5905 {
5906 /* A full vector. */
5907 if ((opcode->membership & INSN_5400)
5908 && (strcmp (opcode->name, "sll.ob") == 0
5909 || strcmp (opcode->name, "srl.ob") == 0))
5910 {
5911 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5912 arg->argnum);
5913 return FALSE;
5914 }
5915
5916 if (is_qh)
5917 uval |= MDMX_FMTSEL_VEC_QH << 5;
5918 else
5919 uval |= MDMX_FMTSEL_VEC_OB << 5;
5920 }
5921 uval |= regno;
5922 }
5923 else
5924 {
5925 offsetT sval;
5926
5927 if (!match_const_int (arg, &sval))
5928 return FALSE;
5929 if (sval < 0 || sval > 31)
5930 {
5931 match_out_of_range (arg);
5932 return FALSE;
5933 }
5934 uval |= (sval & 31);
5935 if (is_qh)
5936 uval |= MDMX_FMTSEL_IMM_QH << 5;
5937 else
5938 uval |= MDMX_FMTSEL_IMM_OB << 5;
5939 }
5940 insn_insert_operand (arg->insn, operand, uval);
5941 return TRUE;
5942 }
5943
5944 /* OP_IMM_INDEX matcher. */
5945
5946 static bfd_boolean
5947 match_imm_index_operand (struct mips_arg_info *arg,
5948 const struct mips_operand *operand)
5949 {
5950 unsigned int max_val;
5951
5952 if (arg->token->type != OT_INTEGER_INDEX)
5953 return FALSE;
5954
5955 max_val = (1 << operand->size) - 1;
5956 if (arg->token->u.index > max_val)
5957 {
5958 match_out_of_range (arg);
5959 return FALSE;
5960 }
5961 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5962 ++arg->token;
5963 return TRUE;
5964 }
5965
5966 /* OP_REG_INDEX matcher. */
5967
5968 static bfd_boolean
5969 match_reg_index_operand (struct mips_arg_info *arg,
5970 const struct mips_operand *operand)
5971 {
5972 unsigned int regno;
5973
5974 if (arg->token->type != OT_REG_INDEX)
5975 return FALSE;
5976
5977 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5978 return FALSE;
5979
5980 insn_insert_operand (arg->insn, operand, regno);
5981 ++arg->token;
5982 return TRUE;
5983 }
5984
5985 /* OP_PC matcher. */
5986
5987 static bfd_boolean
5988 match_pc_operand (struct mips_arg_info *arg)
5989 {
5990 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5991 {
5992 ++arg->token;
5993 return TRUE;
5994 }
5995 return FALSE;
5996 }
5997
5998 /* OP_REG28 matcher. */
5999
6000 static bfd_boolean
6001 match_reg28_operand (struct mips_arg_info *arg)
6002 {
6003 unsigned int regno;
6004
6005 if (arg->token->type == OT_REG
6006 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
6007 && regno == GP)
6008 {
6009 ++arg->token;
6010 return TRUE;
6011 }
6012 return FALSE;
6013 }
6014
6015 /* OP_NON_ZERO_REG matcher. */
6016
6017 static bfd_boolean
6018 match_non_zero_reg_operand (struct mips_arg_info *arg,
6019 const struct mips_operand *operand)
6020 {
6021 unsigned int regno;
6022
6023 if (!match_reg (arg, OP_REG_GP, &regno))
6024 return FALSE;
6025
6026 if (regno == 0)
6027 {
6028 set_insn_error (arg->argnum, _("the source register must not be $0"));
6029 return FALSE;
6030 }
6031
6032 arg->last_regno = regno;
6033 insn_insert_operand (arg->insn, operand, regno);
6034 return TRUE;
6035 }
6036
6037 /* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
6038 register that we need to match. */
6039
6040 static bfd_boolean
6041 match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
6042 {
6043 unsigned int regno;
6044
6045 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
6046 }
6047
6048 /* Try to match a floating-point constant from ARG for LI.S or LI.D.
6049 LENGTH is the length of the value in bytes (4 for float, 8 for double)
6050 and USING_GPRS says whether the destination is a GPR rather than an FPR.
6051
6052 Return the constant in IMM and OFFSET as follows:
6053
6054 - If the constant should be loaded via memory, set IMM to O_absent and
6055 OFFSET to the memory address.
6056
6057 - Otherwise, if the constant should be loaded into two 32-bit registers,
6058 set IMM to the O_constant to load into the high register and OFFSET
6059 to the corresponding value for the low register.
6060
6061 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
6062
6063 These constants only appear as the last operand in an instruction,
6064 and every instruction that accepts them in any variant accepts them
6065 in all variants. This means we don't have to worry about backing out
6066 any changes if the instruction does not match. We just match
6067 unconditionally and report an error if the constant is invalid. */
6068
6069 static bfd_boolean
6070 match_float_constant (struct mips_arg_info *arg, expressionS *imm,
6071 expressionS *offset, int length, bfd_boolean using_gprs)
6072 {
6073 char *p;
6074 segT seg, new_seg;
6075 subsegT subseg;
6076 const char *newname;
6077 unsigned char *data;
6078
6079 /* Where the constant is placed is based on how the MIPS assembler
6080 does things:
6081
6082 length == 4 && using_gprs -- immediate value only
6083 length == 8 && using_gprs -- .rdata or immediate value
6084 length == 4 && !using_gprs -- .lit4 or immediate value
6085 length == 8 && !using_gprs -- .lit8 or immediate value
6086
6087 The .lit4 and .lit8 sections are only used if permitted by the
6088 -G argument. */
6089 if (arg->token->type != OT_FLOAT)
6090 {
6091 set_insn_error (arg->argnum, _("floating-point expression required"));
6092 return FALSE;
6093 }
6094
6095 gas_assert (arg->token->u.flt.length == length);
6096 data = arg->token->u.flt.data;
6097 ++arg->token;
6098
6099 /* Handle 32-bit constants for which an immediate value is best. */
6100 if (length == 4
6101 && (using_gprs
6102 || g_switch_value < 4
6103 || (data[0] == 0 && data[1] == 0)
6104 || (data[2] == 0 && data[3] == 0)))
6105 {
6106 imm->X_op = O_constant;
6107 if (!target_big_endian)
6108 imm->X_add_number = bfd_getl32 (data);
6109 else
6110 imm->X_add_number = bfd_getb32 (data);
6111 offset->X_op = O_absent;
6112 return TRUE;
6113 }
6114
6115 /* Handle 64-bit constants for which an immediate value is best. */
6116 if (length == 8
6117 && !mips_disable_float_construction
6118 /* Constants can only be constructed in GPRs and copied to FPRs if the
6119 GPRs are at least as wide as the FPRs or MTHC1 is available.
6120 Unlike most tests for 32-bit floating-point registers this check
6121 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6122 permit 64-bit moves without MXHC1.
6123 Force the constant into memory otherwise. */
6124 && (using_gprs
6125 || GPR_SIZE == 64
6126 || ISA_HAS_MXHC1 (mips_opts.isa)
6127 || FPR_SIZE == 32)
6128 && ((data[0] == 0 && data[1] == 0)
6129 || (data[2] == 0 && data[3] == 0))
6130 && ((data[4] == 0 && data[5] == 0)
6131 || (data[6] == 0 && data[7] == 0)))
6132 {
6133 /* The value is simple enough to load with a couple of instructions.
6134 If using 32-bit registers, set IMM to the high order 32 bits and
6135 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6136 64 bit constant. */
6137 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
6138 {
6139 imm->X_op = O_constant;
6140 offset->X_op = O_constant;
6141 if (!target_big_endian)
6142 {
6143 imm->X_add_number = bfd_getl32 (data + 4);
6144 offset->X_add_number = bfd_getl32 (data);
6145 }
6146 else
6147 {
6148 imm->X_add_number = bfd_getb32 (data);
6149 offset->X_add_number = bfd_getb32 (data + 4);
6150 }
6151 if (offset->X_add_number == 0)
6152 offset->X_op = O_absent;
6153 }
6154 else
6155 {
6156 imm->X_op = O_constant;
6157 if (!target_big_endian)
6158 imm->X_add_number = bfd_getl64 (data);
6159 else
6160 imm->X_add_number = bfd_getb64 (data);
6161 offset->X_op = O_absent;
6162 }
6163 return TRUE;
6164 }
6165
6166 /* Switch to the right section. */
6167 seg = now_seg;
6168 subseg = now_subseg;
6169 if (length == 4)
6170 {
6171 gas_assert (!using_gprs && g_switch_value >= 4);
6172 newname = ".lit4";
6173 }
6174 else
6175 {
6176 if (using_gprs || g_switch_value < 8)
6177 newname = RDATA_SECTION_NAME;
6178 else
6179 newname = ".lit8";
6180 }
6181
6182 new_seg = subseg_new (newname, (subsegT) 0);
6183 bfd_set_section_flags (stdoutput, new_seg,
6184 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6185 frag_align (length == 4 ? 2 : 3, 0, 0);
6186 if (strncmp (TARGET_OS, "elf", 3) != 0)
6187 record_alignment (new_seg, 4);
6188 else
6189 record_alignment (new_seg, length == 4 ? 2 : 3);
6190 if (seg == now_seg)
6191 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
6192
6193 /* Set the argument to the current address in the section. */
6194 imm->X_op = O_absent;
6195 offset->X_op = O_symbol;
6196 offset->X_add_symbol = symbol_temp_new_now ();
6197 offset->X_add_number = 0;
6198
6199 /* Put the floating point number into the section. */
6200 p = frag_more (length);
6201 memcpy (p, data, length);
6202
6203 /* Switch back to the original section. */
6204 subseg_set (seg, subseg);
6205 return TRUE;
6206 }
6207
6208 /* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6209 them. */
6210
6211 static bfd_boolean
6212 match_vu0_suffix_operand (struct mips_arg_info *arg,
6213 const struct mips_operand *operand,
6214 bfd_boolean match_p)
6215 {
6216 unsigned int uval;
6217
6218 /* The operand can be an XYZW mask or a single 2-bit channel index
6219 (with X being 0). */
6220 gas_assert (operand->size == 2 || operand->size == 4);
6221
6222 /* The suffix can be omitted when it is already part of the opcode. */
6223 if (arg->token->type != OT_CHANNELS)
6224 return match_p;
6225
6226 uval = arg->token->u.channels;
6227 if (operand->size == 2)
6228 {
6229 /* Check that a single bit is set and convert it into a 2-bit index. */
6230 if ((uval & -uval) != uval)
6231 return FALSE;
6232 uval = 4 - ffs (uval);
6233 }
6234
6235 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6236 return FALSE;
6237
6238 ++arg->token;
6239 if (!match_p)
6240 insn_insert_operand (arg->insn, operand, uval);
6241 return TRUE;
6242 }
6243
6244 /* Try to match a token from ARG against OPERAND. Consume the token
6245 and return true on success, otherwise return false. */
6246
6247 static bfd_boolean
6248 match_operand (struct mips_arg_info *arg,
6249 const struct mips_operand *operand)
6250 {
6251 switch (operand->type)
6252 {
6253 case OP_INT:
6254 return match_int_operand (arg, operand);
6255
6256 case OP_MAPPED_INT:
6257 return match_mapped_int_operand (arg, operand);
6258
6259 case OP_MSB:
6260 return match_msb_operand (arg, operand);
6261
6262 case OP_REG:
6263 case OP_OPTIONAL_REG:
6264 return match_reg_operand (arg, operand);
6265
6266 case OP_REG_PAIR:
6267 return match_reg_pair_operand (arg, operand);
6268
6269 case OP_PCREL:
6270 return match_pcrel_operand (arg);
6271
6272 case OP_PERF_REG:
6273 return match_perf_reg_operand (arg, operand);
6274
6275 case OP_ADDIUSP_INT:
6276 return match_addiusp_operand (arg, operand);
6277
6278 case OP_CLO_CLZ_DEST:
6279 return match_clo_clz_dest_operand (arg, operand);
6280
6281 case OP_LWM_SWM_LIST:
6282 return match_lwm_swm_list_operand (arg, operand);
6283
6284 case OP_ENTRY_EXIT_LIST:
6285 return match_entry_exit_operand (arg, operand);
6286
6287 case OP_SAVE_RESTORE_LIST:
6288 return match_save_restore_list_operand (arg);
6289
6290 case OP_MDMX_IMM_REG:
6291 return match_mdmx_imm_reg_operand (arg, operand);
6292
6293 case OP_REPEAT_DEST_REG:
6294 return match_tied_reg_operand (arg, arg->dest_regno);
6295
6296 case OP_REPEAT_PREV_REG:
6297 return match_tied_reg_operand (arg, arg->last_regno);
6298
6299 case OP_PC:
6300 return match_pc_operand (arg);
6301
6302 case OP_REG28:
6303 return match_reg28_operand (arg);
6304
6305 case OP_VU0_SUFFIX:
6306 return match_vu0_suffix_operand (arg, operand, FALSE);
6307
6308 case OP_VU0_MATCH_SUFFIX:
6309 return match_vu0_suffix_operand (arg, operand, TRUE);
6310
6311 case OP_IMM_INDEX:
6312 return match_imm_index_operand (arg, operand);
6313
6314 case OP_REG_INDEX:
6315 return match_reg_index_operand (arg, operand);
6316
6317 case OP_SAME_RS_RT:
6318 return match_same_rs_rt_operand (arg, operand);
6319
6320 case OP_CHECK_PREV:
6321 return match_check_prev_operand (arg, operand);
6322
6323 case OP_NON_ZERO_REG:
6324 return match_non_zero_reg_operand (arg, operand);
6325 }
6326 abort ();
6327 }
6328
6329 /* ARG is the state after successfully matching an instruction.
6330 Issue any queued-up warnings. */
6331
6332 static void
6333 check_completed_insn (struct mips_arg_info *arg)
6334 {
6335 if (arg->seen_at)
6336 {
6337 if (AT == ATREG)
6338 as_warn (_("used $at without \".set noat\""));
6339 else
6340 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
6341 }
6342 }
6343
6344 /* Return true if modifying general-purpose register REG needs a delay. */
6345
6346 static bfd_boolean
6347 reg_needs_delay (unsigned int reg)
6348 {
6349 unsigned long prev_pinfo;
6350
6351 prev_pinfo = history[0].insn_mo->pinfo;
6352 if (!mips_opts.noreorder
6353 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
6354 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
6355 && (gpr_write_mask (&history[0]) & (1 << reg)))
6356 return TRUE;
6357
6358 return FALSE;
6359 }
6360
6361 /* Classify an instruction according to the FIX_VR4120_* enumeration.
6362 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6363 by VR4120 errata. */
6364
6365 static unsigned int
6366 classify_vr4120_insn (const char *name)
6367 {
6368 if (strncmp (name, "macc", 4) == 0)
6369 return FIX_VR4120_MACC;
6370 if (strncmp (name, "dmacc", 5) == 0)
6371 return FIX_VR4120_DMACC;
6372 if (strncmp (name, "mult", 4) == 0)
6373 return FIX_VR4120_MULT;
6374 if (strncmp (name, "dmult", 5) == 0)
6375 return FIX_VR4120_DMULT;
6376 if (strstr (name, "div"))
6377 return FIX_VR4120_DIV;
6378 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6379 return FIX_VR4120_MTHILO;
6380 return NUM_FIX_VR4120_CLASSES;
6381 }
6382
6383 #define INSN_ERET 0x42000018
6384 #define INSN_DERET 0x4200001f
6385 #define INSN_DMULT 0x1c
6386 #define INSN_DMULTU 0x1d
6387
6388 /* Return the number of instructions that must separate INSN1 and INSN2,
6389 where INSN1 is the earlier instruction. Return the worst-case value
6390 for any INSN2 if INSN2 is null. */
6391
6392 static unsigned int
6393 insns_between (const struct mips_cl_insn *insn1,
6394 const struct mips_cl_insn *insn2)
6395 {
6396 unsigned long pinfo1, pinfo2;
6397 unsigned int mask;
6398
6399 /* If INFO2 is null, pessimistically assume that all flags are set for
6400 the second instruction. */
6401 pinfo1 = insn1->insn_mo->pinfo;
6402 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
6403
6404 /* For most targets, write-after-read dependencies on the HI and LO
6405 registers must be separated by at least two instructions. */
6406 if (!hilo_interlocks)
6407 {
6408 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6409 return 2;
6410 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6411 return 2;
6412 }
6413
6414 /* If we're working around r7000 errata, there must be two instructions
6415 between an mfhi or mflo and any instruction that uses the result. */
6416 if (mips_7000_hilo_fix
6417 && !mips_opts.micromips
6418 && MF_HILO_INSN (pinfo1)
6419 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
6420 return 2;
6421
6422 /* If we're working around 24K errata, one instruction is required
6423 if an ERET or DERET is followed by a branch instruction. */
6424 if (mips_fix_24k && !mips_opts.micromips)
6425 {
6426 if (insn1->insn_opcode == INSN_ERET
6427 || insn1->insn_opcode == INSN_DERET)
6428 {
6429 if (insn2 == NULL
6430 || insn2->insn_opcode == INSN_ERET
6431 || insn2->insn_opcode == INSN_DERET
6432 || delayed_branch_p (insn2))
6433 return 1;
6434 }
6435 }
6436
6437 /* If we're working around PMC RM7000 errata, there must be three
6438 nops between a dmult and a load instruction. */
6439 if (mips_fix_rm7000 && !mips_opts.micromips)
6440 {
6441 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6442 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6443 {
6444 if (pinfo2 & INSN_LOAD_MEMORY)
6445 return 3;
6446 }
6447 }
6448
6449 /* If working around VR4120 errata, check for combinations that need
6450 a single intervening instruction. */
6451 if (mips_fix_vr4120 && !mips_opts.micromips)
6452 {
6453 unsigned int class1, class2;
6454
6455 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6456 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
6457 {
6458 if (insn2 == NULL)
6459 return 1;
6460 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6461 if (vr4120_conflicts[class1] & (1 << class2))
6462 return 1;
6463 }
6464 }
6465
6466 if (!HAVE_CODE_COMPRESSION)
6467 {
6468 /* Check for GPR or coprocessor load delays. All such delays
6469 are on the RT register. */
6470 /* Itbl support may require additional care here. */
6471 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
6472 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
6473 {
6474 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
6475 return 1;
6476 }
6477
6478 /* Check for generic coprocessor hazards.
6479
6480 This case is not handled very well. There is no special
6481 knowledge of CP0 handling, and the coprocessors other than
6482 the floating point unit are not distinguished at all. */
6483 /* Itbl support may require additional care here. FIXME!
6484 Need to modify this to include knowledge about
6485 user specified delays! */
6486 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
6487 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6488 {
6489 /* Handle cases where INSN1 writes to a known general coprocessor
6490 register. There must be a one instruction delay before INSN2
6491 if INSN2 reads that register, otherwise no delay is needed. */
6492 mask = fpr_write_mask (insn1);
6493 if (mask != 0)
6494 {
6495 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
6496 return 1;
6497 }
6498 else
6499 {
6500 /* Read-after-write dependencies on the control registers
6501 require a two-instruction gap. */
6502 if ((pinfo1 & INSN_WRITE_COND_CODE)
6503 && (pinfo2 & INSN_READ_COND_CODE))
6504 return 2;
6505
6506 /* We don't know exactly what INSN1 does. If INSN2 is
6507 also a coprocessor instruction, assume there must be
6508 a one instruction gap. */
6509 if (pinfo2 & INSN_COP)
6510 return 1;
6511 }
6512 }
6513
6514 /* Check for read-after-write dependencies on the coprocessor
6515 control registers in cases where INSN1 does not need a general
6516 coprocessor delay. This means that INSN1 is a floating point
6517 comparison instruction. */
6518 /* Itbl support may require additional care here. */
6519 else if (!cop_interlocks
6520 && (pinfo1 & INSN_WRITE_COND_CODE)
6521 && (pinfo2 & INSN_READ_COND_CODE))
6522 return 1;
6523 }
6524
6525 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6526 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6527 and pause. */
6528 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6529 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6530 || (insn2 && delayed_branch_p (insn2))))
6531 return 1;
6532
6533 return 0;
6534 }
6535
6536 /* Return the number of nops that would be needed to work around the
6537 VR4130 mflo/mfhi errata if instruction INSN immediately followed
6538 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6539 that are contained within the first IGNORE instructions of HIST. */
6540
6541 static int
6542 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
6543 const struct mips_cl_insn *insn)
6544 {
6545 int i, j;
6546 unsigned int mask;
6547
6548 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6549 are not affected by the errata. */
6550 if (insn != 0
6551 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6552 || strcmp (insn->insn_mo->name, "mtlo") == 0
6553 || strcmp (insn->insn_mo->name, "mthi") == 0))
6554 return 0;
6555
6556 /* Search for the first MFLO or MFHI. */
6557 for (i = 0; i < MAX_VR4130_NOPS; i++)
6558 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
6559 {
6560 /* Extract the destination register. */
6561 mask = gpr_write_mask (&hist[i]);
6562
6563 /* No nops are needed if INSN reads that register. */
6564 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
6565 return 0;
6566
6567 /* ...or if any of the intervening instructions do. */
6568 for (j = 0; j < i; j++)
6569 if (gpr_read_mask (&hist[j]) & mask)
6570 return 0;
6571
6572 if (i >= ignore)
6573 return MAX_VR4130_NOPS - i;
6574 }
6575 return 0;
6576 }
6577
6578 #define BASE_REG_EQ(INSN1, INSN2) \
6579 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
6580 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6581
6582 /* Return the minimum alignment for this store instruction. */
6583
6584 static int
6585 fix_24k_align_to (const struct mips_opcode *mo)
6586 {
6587 if (strcmp (mo->name, "sh") == 0)
6588 return 2;
6589
6590 if (strcmp (mo->name, "swc1") == 0
6591 || strcmp (mo->name, "swc2") == 0
6592 || strcmp (mo->name, "sw") == 0
6593 || strcmp (mo->name, "sc") == 0
6594 || strcmp (mo->name, "s.s") == 0)
6595 return 4;
6596
6597 if (strcmp (mo->name, "sdc1") == 0
6598 || strcmp (mo->name, "sdc2") == 0
6599 || strcmp (mo->name, "s.d") == 0)
6600 return 8;
6601
6602 /* sb, swl, swr */
6603 return 1;
6604 }
6605
6606 struct fix_24k_store_info
6607 {
6608 /* Immediate offset, if any, for this store instruction. */
6609 short off;
6610 /* Alignment required by this store instruction. */
6611 int align_to;
6612 /* True for register offsets. */
6613 int register_offset;
6614 };
6615
6616 /* Comparison function used by qsort. */
6617
6618 static int
6619 fix_24k_sort (const void *a, const void *b)
6620 {
6621 const struct fix_24k_store_info *pos1 = a;
6622 const struct fix_24k_store_info *pos2 = b;
6623
6624 return (pos1->off - pos2->off);
6625 }
6626
6627 /* INSN is a store instruction. Try to record the store information
6628 in STINFO. Return false if the information isn't known. */
6629
6630 static bfd_boolean
6631 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
6632 const struct mips_cl_insn *insn)
6633 {
6634 /* The instruction must have a known offset. */
6635 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6636 return FALSE;
6637
6638 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6639 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6640 return TRUE;
6641 }
6642
6643 /* Return the number of nops that would be needed to work around the 24k
6644 "lost data on stores during refill" errata if instruction INSN
6645 immediately followed the 2 instructions described by HIST.
6646 Ignore hazards that are contained within the first IGNORE
6647 instructions of HIST.
6648
6649 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6650 for the data cache refills and store data. The following describes
6651 the scenario where the store data could be lost.
6652
6653 * A data cache miss, due to either a load or a store, causing fill
6654 data to be supplied by the memory subsystem
6655 * The first three doublewords of fill data are returned and written
6656 into the cache
6657 * A sequence of four stores occurs in consecutive cycles around the
6658 final doubleword of the fill:
6659 * Store A
6660 * Store B
6661 * Store C
6662 * Zero, One or more instructions
6663 * Store D
6664
6665 The four stores A-D must be to different doublewords of the line that
6666 is being filled. The fourth instruction in the sequence above permits
6667 the fill of the final doubleword to be transferred from the FSB into
6668 the cache. In the sequence above, the stores may be either integer
6669 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6670 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6671 different doublewords on the line. If the floating point unit is
6672 running in 1:2 mode, it is not possible to create the sequence above
6673 using only floating point store instructions.
6674
6675 In this case, the cache line being filled is incorrectly marked
6676 invalid, thereby losing the data from any store to the line that
6677 occurs between the original miss and the completion of the five
6678 cycle sequence shown above.
6679
6680 The workarounds are:
6681
6682 * Run the data cache in write-through mode.
6683 * Insert a non-store instruction between
6684 Store A and Store B or Store B and Store C. */
6685
6686 static int
6687 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
6688 const struct mips_cl_insn *insn)
6689 {
6690 struct fix_24k_store_info pos[3];
6691 int align, i, base_offset;
6692
6693 if (ignore >= 2)
6694 return 0;
6695
6696 /* If the previous instruction wasn't a store, there's nothing to
6697 worry about. */
6698 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6699 return 0;
6700
6701 /* If the instructions after the previous one are unknown, we have
6702 to assume the worst. */
6703 if (!insn)
6704 return 1;
6705
6706 /* Check whether we are dealing with three consecutive stores. */
6707 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6708 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6709 return 0;
6710
6711 /* If we don't know the relationship between the store addresses,
6712 assume the worst. */
6713 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
6714 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6715 return 1;
6716
6717 if (!fix_24k_record_store_info (&pos[0], insn)
6718 || !fix_24k_record_store_info (&pos[1], &hist[0])
6719 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6720 return 1;
6721
6722 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6723
6724 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6725 X bytes and such that the base register + X is known to be aligned
6726 to align bytes. */
6727
6728 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6729 align = 8;
6730 else
6731 {
6732 align = pos[0].align_to;
6733 base_offset = pos[0].off;
6734 for (i = 1; i < 3; i++)
6735 if (align < pos[i].align_to)
6736 {
6737 align = pos[i].align_to;
6738 base_offset = pos[i].off;
6739 }
6740 for (i = 0; i < 3; i++)
6741 pos[i].off -= base_offset;
6742 }
6743
6744 pos[0].off &= ~align + 1;
6745 pos[1].off &= ~align + 1;
6746 pos[2].off &= ~align + 1;
6747
6748 /* If any two stores write to the same chunk, they also write to the
6749 same doubleword. The offsets are still sorted at this point. */
6750 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6751 return 0;
6752
6753 /* A range of at least 9 bytes is needed for the stores to be in
6754 non-overlapping doublewords. */
6755 if (pos[2].off - pos[0].off <= 8)
6756 return 0;
6757
6758 if (pos[2].off - pos[1].off >= 24
6759 || pos[1].off - pos[0].off >= 24
6760 || pos[2].off - pos[0].off >= 32)
6761 return 0;
6762
6763 return 1;
6764 }
6765
6766 /* Return the number of nops that would be needed if instruction INSN
6767 immediately followed the MAX_NOPS instructions given by HIST,
6768 where HIST[0] is the most recent instruction. Ignore hazards
6769 between INSN and the first IGNORE instructions in HIST.
6770
6771 If INSN is null, return the worse-case number of nops for any
6772 instruction. */
6773
6774 static int
6775 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
6776 const struct mips_cl_insn *insn)
6777 {
6778 int i, nops, tmp_nops;
6779
6780 nops = 0;
6781 for (i = ignore; i < MAX_DELAY_NOPS; i++)
6782 {
6783 tmp_nops = insns_between (hist + i, insn) - i;
6784 if (tmp_nops > nops)
6785 nops = tmp_nops;
6786 }
6787
6788 if (mips_fix_vr4130 && !mips_opts.micromips)
6789 {
6790 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
6791 if (tmp_nops > nops)
6792 nops = tmp_nops;
6793 }
6794
6795 if (mips_fix_24k && !mips_opts.micromips)
6796 {
6797 tmp_nops = nops_for_24k (ignore, hist, insn);
6798 if (tmp_nops > nops)
6799 nops = tmp_nops;
6800 }
6801
6802 return nops;
6803 }
6804
6805 /* The variable arguments provide NUM_INSNS extra instructions that
6806 might be added to HIST. Return the largest number of nops that
6807 would be needed after the extended sequence, ignoring hazards
6808 in the first IGNORE instructions. */
6809
6810 static int
6811 nops_for_sequence (int num_insns, int ignore,
6812 const struct mips_cl_insn *hist, ...)
6813 {
6814 va_list args;
6815 struct mips_cl_insn buffer[MAX_NOPS];
6816 struct mips_cl_insn *cursor;
6817 int nops;
6818
6819 va_start (args, hist);
6820 cursor = buffer + num_insns;
6821 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
6822 while (cursor > buffer)
6823 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6824
6825 nops = nops_for_insn (ignore, buffer, NULL);
6826 va_end (args);
6827 return nops;
6828 }
6829
6830 /* Like nops_for_insn, but if INSN is a branch, take into account the
6831 worst-case delay for the branch target. */
6832
6833 static int
6834 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
6835 const struct mips_cl_insn *insn)
6836 {
6837 int nops, tmp_nops;
6838
6839 nops = nops_for_insn (ignore, hist, insn);
6840 if (delayed_branch_p (insn))
6841 {
6842 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
6843 hist, insn, get_delay_slot_nop (insn));
6844 if (tmp_nops > nops)
6845 nops = tmp_nops;
6846 }
6847 else if (compact_branch_p (insn))
6848 {
6849 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
6850 if (tmp_nops > nops)
6851 nops = tmp_nops;
6852 }
6853 return nops;
6854 }
6855
6856 /* Fix NOP issue: Replace nops by "or at,at,zero". */
6857
6858 static void
6859 fix_loongson2f_nop (struct mips_cl_insn * ip)
6860 {
6861 gas_assert (!HAVE_CODE_COMPRESSION);
6862 if (strcmp (ip->insn_mo->name, "nop") == 0)
6863 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6864 }
6865
6866 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6867 jr target pc &= 'hffff_ffff_cfff_ffff. */
6868
6869 static void
6870 fix_loongson2f_jump (struct mips_cl_insn * ip)
6871 {
6872 gas_assert (!HAVE_CODE_COMPRESSION);
6873 if (strcmp (ip->insn_mo->name, "j") == 0
6874 || strcmp (ip->insn_mo->name, "jr") == 0
6875 || strcmp (ip->insn_mo->name, "jalr") == 0)
6876 {
6877 int sreg;
6878 expressionS ep;
6879
6880 if (! mips_opts.at)
6881 return;
6882
6883 sreg = EXTRACT_OPERAND (0, RS, *ip);
6884 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6885 return;
6886
6887 ep.X_op = O_constant;
6888 ep.X_add_number = 0xcfff0000;
6889 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6890 ep.X_add_number = 0xffff;
6891 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6892 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6893 }
6894 }
6895
6896 static void
6897 fix_loongson2f (struct mips_cl_insn * ip)
6898 {
6899 if (mips_fix_loongson2f_nop)
6900 fix_loongson2f_nop (ip);
6901
6902 if (mips_fix_loongson2f_jump)
6903 fix_loongson2f_jump (ip);
6904 }
6905
6906 /* Fix loongson3 llsc errata: Insert sync before ll/lld. */
6907
6908 static void
6909 fix_loongson3_llsc (struct mips_cl_insn * ip)
6910 {
6911 gas_assert (!HAVE_CODE_COMPRESSION);
6912
6913 /* If is an local label and the insn is not sync,
6914 look forward that whether an branch between ll/sc jump to here
6915 if so, insert a sync. */
6916 if (seg_info (now_seg)->label_list
6917 && S_IS_LOCAL (seg_info (now_seg)->label_list->label)
6918 && (strcmp (ip->insn_mo->name, "sync") != 0))
6919 {
6920 const char *label_name = S_GET_NAME (seg_info (now_seg)->label_list->label);
6921 unsigned long lookback = ARRAY_SIZE (history);
6922 unsigned long i;
6923
6924 for (i = 0; i < lookback; i++)
6925 {
6926 if (streq (history[i].insn_mo->name, "ll")
6927 || streq (history[i].insn_mo->name, "lld"))
6928 break;
6929
6930 if (streq (history[i].insn_mo->name, "sc")
6931 || streq (history[i].insn_mo->name, "scd"))
6932 {
6933 unsigned long j;
6934
6935 for (j = i + 1; j < lookback; j++)
6936 {
6937 if (streq (history[i].insn_mo->name, "ll")
6938 || streq (history[i].insn_mo->name, "lld"))
6939 break;
6940
6941 if (delayed_branch_p (&history[j]))
6942 {
6943 if (streq (history[j].target, label_name))
6944 {
6945 add_fixed_insn (&sync_insn);
6946 insert_into_history (0, 1, &sync_insn);
6947 i = lookback;
6948 break;
6949 }
6950 }
6951 }
6952 }
6953 }
6954 }
6955 /* If we find a sc, we look forward to look for an branch insn,
6956 and see whether it jump back and out of ll/sc. */
6957 else if (streq(ip->insn_mo->name, "sc") || streq(ip->insn_mo->name, "scd"))
6958 {
6959 unsigned long lookback = ARRAY_SIZE (history) - 1;
6960 unsigned long i;
6961
6962 for (i = 0; i < lookback; i++)
6963 {
6964 if (streq (history[i].insn_mo->name, "ll")
6965 || streq (history[i].insn_mo->name, "lld"))
6966 break;
6967
6968 if (delayed_branch_p (&history[i]))
6969 {
6970 unsigned long j;
6971
6972 for (j = i + 1; j < lookback; j++)
6973 {
6974 if (streq (history[j].insn_mo->name, "ll")
6975 || streq (history[i].insn_mo->name, "lld"))
6976 break;
6977 }
6978
6979 for (; j < lookback; j++)
6980 {
6981 if (history[j].label[0] != '\0'
6982 && streq (history[j].label, history[i].target)
6983 && strcmp (history[j+1].insn_mo->name, "sync") != 0)
6984 {
6985 add_fixed_insn (&sync_insn);
6986 insert_into_history (++j, 1, &sync_insn);
6987 }
6988 }
6989 }
6990 }
6991 }
6992
6993 /* Skip if there is a sync before ll/lld. */
6994 if ((strcmp (ip->insn_mo->name, "ll") == 0
6995 || strcmp (ip->insn_mo->name, "lld") == 0)
6996 && (strcmp (history[0].insn_mo->name, "sync") != 0))
6997 {
6998 add_fixed_insn (&sync_insn);
6999 insert_into_history (0, 1, &sync_insn);
7000 }
7001 }
7002
7003 /* IP is a branch that has a delay slot, and we need to fill it
7004 automatically. Return true if we can do that by swapping IP
7005 with the previous instruction.
7006 ADDRESS_EXPR is an operand of the instruction to be used with
7007 RELOC_TYPE. */
7008
7009 static bfd_boolean
7010 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
7011 bfd_reloc_code_real_type *reloc_type)
7012 {
7013 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
7014 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
7015 unsigned int fpr_read, prev_fpr_write;
7016
7017 /* -O2 and above is required for this optimization. */
7018 if (mips_optimize < 2)
7019 return FALSE;
7020
7021 /* If we have seen .set volatile or .set nomove, don't optimize. */
7022 if (mips_opts.nomove)
7023 return FALSE;
7024
7025 /* We can't swap if the previous instruction's position is fixed. */
7026 if (history[0].fixed_p)
7027 return FALSE;
7028
7029 /* If the previous previous insn was in a .set noreorder, we can't
7030 swap. Actually, the MIPS assembler will swap in this situation.
7031 However, gcc configured -with-gnu-as will generate code like
7032
7033 .set noreorder
7034 lw $4,XXX
7035 .set reorder
7036 INSN
7037 bne $4,$0,foo
7038
7039 in which we can not swap the bne and INSN. If gcc is not configured
7040 -with-gnu-as, it does not output the .set pseudo-ops. */
7041 if (history[1].noreorder_p)
7042 return FALSE;
7043
7044 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
7045 This means that the previous instruction was a 4-byte one anyhow. */
7046 if (mips_opts.mips16 && history[0].fixp[0])
7047 return FALSE;
7048
7049 /* If the branch is itself the target of a branch, we can not swap.
7050 We cheat on this; all we check for is whether there is a label on
7051 this instruction. If there are any branches to anything other than
7052 a label, users must use .set noreorder. */
7053 if (seg_info (now_seg)->label_list)
7054 return FALSE;
7055
7056 /* If the previous instruction is in a variant frag other than this
7057 branch's one, we cannot do the swap. This does not apply to
7058 MIPS16 code, which uses variant frags for different purposes. */
7059 if (!mips_opts.mips16
7060 && history[0].frag
7061 && history[0].frag->fr_type == rs_machine_dependent)
7062 return FALSE;
7063
7064 /* We do not swap with instructions that cannot architecturally
7065 be placed in a branch delay slot, such as SYNC or ERET. We
7066 also refrain from swapping with a trap instruction, since it
7067 complicates trap handlers to have the trap instruction be in
7068 a delay slot. */
7069 prev_pinfo = history[0].insn_mo->pinfo;
7070 if (prev_pinfo & INSN_NO_DELAY_SLOT)
7071 return FALSE;
7072
7073 /* Check for conflicts between the branch and the instructions
7074 before the candidate delay slot. */
7075 if (nops_for_insn (0, history + 1, ip) > 0)
7076 return FALSE;
7077
7078 /* Check for conflicts between the swapped sequence and the
7079 target of the branch. */
7080 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
7081 return FALSE;
7082
7083 /* If the branch reads a register that the previous
7084 instruction sets, we can not swap. */
7085 gpr_read = gpr_read_mask (ip);
7086 prev_gpr_write = gpr_write_mask (&history[0]);
7087 if (gpr_read & prev_gpr_write)
7088 return FALSE;
7089
7090 fpr_read = fpr_read_mask (ip);
7091 prev_fpr_write = fpr_write_mask (&history[0]);
7092 if (fpr_read & prev_fpr_write)
7093 return FALSE;
7094
7095 /* If the branch writes a register that the previous
7096 instruction sets, we can not swap. */
7097 gpr_write = gpr_write_mask (ip);
7098 if (gpr_write & prev_gpr_write)
7099 return FALSE;
7100
7101 /* If the branch writes a register that the previous
7102 instruction reads, we can not swap. */
7103 prev_gpr_read = gpr_read_mask (&history[0]);
7104 if (gpr_write & prev_gpr_read)
7105 return FALSE;
7106
7107 /* If one instruction sets a condition code and the
7108 other one uses a condition code, we can not swap. */
7109 pinfo = ip->insn_mo->pinfo;
7110 if ((pinfo & INSN_READ_COND_CODE)
7111 && (prev_pinfo & INSN_WRITE_COND_CODE))
7112 return FALSE;
7113 if ((pinfo & INSN_WRITE_COND_CODE)
7114 && (prev_pinfo & INSN_READ_COND_CODE))
7115 return FALSE;
7116
7117 /* If the previous instruction uses the PC, we can not swap. */
7118 prev_pinfo2 = history[0].insn_mo->pinfo2;
7119 if (prev_pinfo2 & INSN2_READ_PC)
7120 return FALSE;
7121
7122 /* If the previous instruction has an incorrect size for a fixed
7123 branch delay slot in microMIPS mode, we cannot swap. */
7124 pinfo2 = ip->insn_mo->pinfo2;
7125 if (mips_opts.micromips
7126 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
7127 && insn_length (history) != 2)
7128 return FALSE;
7129 if (mips_opts.micromips
7130 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
7131 && insn_length (history) != 4)
7132 return FALSE;
7133
7134 /* On the R5900 short loops need to be fixed by inserting a NOP in the
7135 branch delay slot.
7136
7137 The short loop bug under certain conditions causes loops to execute
7138 only once or twice. We must ensure that the assembler never
7139 generates loops that satisfy all of the following conditions:
7140
7141 - a loop consists of less than or equal to six instructions
7142 (including the branch delay slot);
7143 - a loop contains only one conditional branch instruction at the end
7144 of the loop;
7145 - a loop does not contain any other branch or jump instructions;
7146 - a branch delay slot of the loop is not NOP (EE 2.9 or later).
7147
7148 We need to do this because of a hardware bug in the R5900 chip. */
7149 if (mips_fix_r5900
7150 /* Check if instruction has a parameter, ignore "j $31". */
7151 && (address_expr != NULL)
7152 /* Parameter must be 16 bit. */
7153 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
7154 /* Branch to same segment. */
7155 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
7156 /* Branch to same code fragment. */
7157 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
7158 /* Can only calculate branch offset if value is known. */
7159 && symbol_constant_p (address_expr->X_add_symbol)
7160 /* Check if branch is really conditional. */
7161 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
7162 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
7163 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
7164 {
7165 int distance;
7166 /* Check if loop is shorter than or equal to 6 instructions
7167 including branch and delay slot. */
7168 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
7169 if (distance <= 20)
7170 {
7171 int i;
7172 int rv;
7173
7174 rv = FALSE;
7175 /* When the loop includes branches or jumps,
7176 it is not a short loop. */
7177 for (i = 0; i < (distance / 4); i++)
7178 {
7179 if ((history[i].cleared_p)
7180 || delayed_branch_p (&history[i]))
7181 {
7182 rv = TRUE;
7183 break;
7184 }
7185 }
7186 if (!rv)
7187 {
7188 /* Insert nop after branch to fix short loop. */
7189 return FALSE;
7190 }
7191 }
7192 }
7193
7194 return TRUE;
7195 }
7196
7197 /* Decide how we should add IP to the instruction stream.
7198 ADDRESS_EXPR is an operand of the instruction to be used with
7199 RELOC_TYPE. */
7200
7201 static enum append_method
7202 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
7203 bfd_reloc_code_real_type *reloc_type)
7204 {
7205 /* The relaxed version of a macro sequence must be inherently
7206 hazard-free. */
7207 if (mips_relax.sequence == 2)
7208 return APPEND_ADD;
7209
7210 /* We must not dabble with instructions in a ".set noreorder" block. */
7211 if (mips_opts.noreorder)
7212 return APPEND_ADD;
7213
7214 /* Otherwise, it's our responsibility to fill branch delay slots. */
7215 if (delayed_branch_p (ip))
7216 {
7217 if (!branch_likely_p (ip)
7218 && can_swap_branch_p (ip, address_expr, reloc_type))
7219 return APPEND_SWAP;
7220
7221 if (mips_opts.mips16
7222 && ISA_SUPPORTS_MIPS16E
7223 && gpr_read_mask (ip) != 0)
7224 return APPEND_ADD_COMPACT;
7225
7226 if (mips_opts.micromips
7227 && ((ip->insn_opcode & 0xffe0) == 0x4580
7228 || (!forced_insn_length
7229 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7230 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7231 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7232 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7233 return APPEND_ADD_COMPACT;
7234
7235 return APPEND_ADD_WITH_NOP;
7236 }
7237
7238 return APPEND_ADD;
7239 }
7240
7241 /* IP is an instruction whose opcode we have just changed, END points
7242 to the end of the opcode table processed. Point IP->insn_mo to the
7243 new opcode's definition. */
7244
7245 static void
7246 find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
7247 {
7248 const struct mips_opcode *mo;
7249
7250 for (mo = ip->insn_mo; mo < end; mo++)
7251 if (mo->pinfo != INSN_MACRO
7252 && (ip->insn_opcode & mo->mask) == mo->match)
7253 {
7254 ip->insn_mo = mo;
7255 return;
7256 }
7257 abort ();
7258 }
7259
7260 /* IP is a MIPS16 instruction whose opcode we have just changed.
7261 Point IP->insn_mo to the new opcode's definition. */
7262
7263 static void
7264 find_altered_mips16_opcode (struct mips_cl_insn *ip)
7265 {
7266 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7267 }
7268
7269 /* IP is a microMIPS instruction whose opcode we have just changed.
7270 Point IP->insn_mo to the new opcode's definition. */
7271
7272 static void
7273 find_altered_micromips_opcode (struct mips_cl_insn *ip)
7274 {
7275 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7276 }
7277
7278 /* For microMIPS macros, we need to generate a local number label
7279 as the target of branches. */
7280 #define MICROMIPS_LABEL_CHAR '\037'
7281 static unsigned long micromips_target_label;
7282 static char micromips_target_name[32];
7283
7284 static char *
7285 micromips_label_name (void)
7286 {
7287 char *p = micromips_target_name;
7288 char symbol_name_temporary[24];
7289 unsigned long l;
7290 int i;
7291
7292 if (*p)
7293 return p;
7294
7295 i = 0;
7296 l = micromips_target_label;
7297 #ifdef LOCAL_LABEL_PREFIX
7298 *p++ = LOCAL_LABEL_PREFIX;
7299 #endif
7300 *p++ = 'L';
7301 *p++ = MICROMIPS_LABEL_CHAR;
7302 do
7303 {
7304 symbol_name_temporary[i++] = l % 10 + '0';
7305 l /= 10;
7306 }
7307 while (l != 0);
7308 while (i > 0)
7309 *p++ = symbol_name_temporary[--i];
7310 *p = '\0';
7311
7312 return micromips_target_name;
7313 }
7314
7315 static void
7316 micromips_label_expr (expressionS *label_expr)
7317 {
7318 label_expr->X_op = O_symbol;
7319 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7320 label_expr->X_add_number = 0;
7321 }
7322
7323 static void
7324 micromips_label_inc (void)
7325 {
7326 micromips_target_label++;
7327 *micromips_target_name = '\0';
7328 }
7329
7330 static void
7331 micromips_add_label (void)
7332 {
7333 symbolS *s;
7334
7335 s = colon (micromips_label_name ());
7336 micromips_label_inc ();
7337 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
7338 }
7339
7340 /* If assembling microMIPS code, then return the microMIPS reloc
7341 corresponding to the requested one if any. Otherwise return
7342 the reloc unchanged. */
7343
7344 static bfd_reloc_code_real_type
7345 micromips_map_reloc (bfd_reloc_code_real_type reloc)
7346 {
7347 static const bfd_reloc_code_real_type relocs[][2] =
7348 {
7349 /* Keep sorted incrementally by the left-hand key. */
7350 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7351 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7352 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7353 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7354 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7355 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7356 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7357 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7358 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7359 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7360 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7361 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7362 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7363 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7364 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7365 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7366 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7367 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7368 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7369 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7370 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7371 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7372 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7373 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7374 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7375 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7376 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7377 };
7378 bfd_reloc_code_real_type r;
7379 size_t i;
7380
7381 if (!mips_opts.micromips)
7382 return reloc;
7383 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7384 {
7385 r = relocs[i][0];
7386 if (r > reloc)
7387 return reloc;
7388 if (r == reloc)
7389 return relocs[i][1];
7390 }
7391 return reloc;
7392 }
7393
7394 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7395 Return true on success, storing the resolved value in RESULT. */
7396
7397 static bfd_boolean
7398 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7399 offsetT *result)
7400 {
7401 switch (reloc)
7402 {
7403 case BFD_RELOC_MIPS_HIGHEST:
7404 case BFD_RELOC_MICROMIPS_HIGHEST:
7405 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7406 return TRUE;
7407
7408 case BFD_RELOC_MIPS_HIGHER:
7409 case BFD_RELOC_MICROMIPS_HIGHER:
7410 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7411 return TRUE;
7412
7413 case BFD_RELOC_HI16_S:
7414 case BFD_RELOC_HI16_S_PCREL:
7415 case BFD_RELOC_MICROMIPS_HI16_S:
7416 case BFD_RELOC_MIPS16_HI16_S:
7417 *result = ((operand + 0x8000) >> 16) & 0xffff;
7418 return TRUE;
7419
7420 case BFD_RELOC_HI16:
7421 case BFD_RELOC_MICROMIPS_HI16:
7422 case BFD_RELOC_MIPS16_HI16:
7423 *result = (operand >> 16) & 0xffff;
7424 return TRUE;
7425
7426 case BFD_RELOC_LO16:
7427 case BFD_RELOC_LO16_PCREL:
7428 case BFD_RELOC_MICROMIPS_LO16:
7429 case BFD_RELOC_MIPS16_LO16:
7430 *result = operand & 0xffff;
7431 return TRUE;
7432
7433 case BFD_RELOC_UNUSED:
7434 *result = operand;
7435 return TRUE;
7436
7437 default:
7438 return FALSE;
7439 }
7440 }
7441
7442 /* Output an instruction. IP is the instruction information.
7443 ADDRESS_EXPR is an operand of the instruction to be used with
7444 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7445 a macro expansion. */
7446
7447 static void
7448 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
7449 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
7450 {
7451 unsigned long prev_pinfo2, pinfo;
7452 bfd_boolean relaxed_branch = FALSE;
7453 enum append_method method;
7454 bfd_boolean relax32;
7455 int branch_disp;
7456
7457 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
7458 fix_loongson2f (ip);
7459
7460 ip->target[0] = '\0';
7461 if (offset_expr.X_op == O_symbol)
7462 strncpy (ip->target, S_GET_NAME (offset_expr.X_add_symbol), 15);
7463 ip->label[0] = '\0';
7464 if (seg_info (now_seg)->label_list)
7465 strncpy (ip->label, S_GET_NAME (seg_info (now_seg)->label_list->label), 15);
7466 if (mips_fix_loongson3_llsc && !HAVE_CODE_COMPRESSION)
7467 fix_loongson3_llsc (ip);
7468
7469 file_ase_mips16 |= mips_opts.mips16;
7470 file_ase_micromips |= mips_opts.micromips;
7471
7472 prev_pinfo2 = history[0].insn_mo->pinfo2;
7473 pinfo = ip->insn_mo->pinfo;
7474
7475 /* Don't raise alarm about `nods' frags as they'll fill in the right
7476 kind of nop in relaxation if required. */
7477 if (mips_opts.micromips
7478 && !expansionp
7479 && !(history[0].frag
7480 && history[0].frag->fr_type == rs_machine_dependent
7481 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7482 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
7483 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7484 && micromips_insn_length (ip->insn_mo) != 2)
7485 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7486 && micromips_insn_length (ip->insn_mo) != 4)))
7487 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
7488 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
7489
7490 if (address_expr == NULL)
7491 ip->complete_p = 1;
7492 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7493 && reloc_type[1] == BFD_RELOC_UNUSED
7494 && reloc_type[2] == BFD_RELOC_UNUSED
7495 && address_expr->X_op == O_constant)
7496 {
7497 switch (*reloc_type)
7498 {
7499 case BFD_RELOC_MIPS_JMP:
7500 {
7501 int shift;
7502
7503 /* Shift is 2, unusually, for microMIPS JALX. */
7504 shift = (mips_opts.micromips
7505 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
7506 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7507 as_bad (_("jump to misaligned address (0x%lx)"),
7508 (unsigned long) address_expr->X_add_number);
7509 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7510 & 0x3ffffff);
7511 ip->complete_p = 1;
7512 }
7513 break;
7514
7515 case BFD_RELOC_MIPS16_JMP:
7516 if ((address_expr->X_add_number & 3) != 0)
7517 as_bad (_("jump to misaligned address (0x%lx)"),
7518 (unsigned long) address_expr->X_add_number);
7519 ip->insn_opcode |=
7520 (((address_expr->X_add_number & 0x7c0000) << 3)
7521 | ((address_expr->X_add_number & 0xf800000) >> 7)
7522 | ((address_expr->X_add_number & 0x3fffc) >> 2));
7523 ip->complete_p = 1;
7524 break;
7525
7526 case BFD_RELOC_16_PCREL_S2:
7527 {
7528 int shift;
7529
7530 shift = mips_opts.micromips ? 1 : 2;
7531 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7532 as_bad (_("branch to misaligned address (0x%lx)"),
7533 (unsigned long) address_expr->X_add_number);
7534 if (!mips_relax_branch)
7535 {
7536 if ((address_expr->X_add_number + (1 << (shift + 15)))
7537 & ~((1 << (shift + 16)) - 1))
7538 as_bad (_("branch address range overflow (0x%lx)"),
7539 (unsigned long) address_expr->X_add_number);
7540 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7541 & 0xffff);
7542 }
7543 }
7544 break;
7545
7546 case BFD_RELOC_MIPS_21_PCREL_S2:
7547 {
7548 int shift;
7549
7550 shift = 2;
7551 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7552 as_bad (_("branch to misaligned address (0x%lx)"),
7553 (unsigned long) address_expr->X_add_number);
7554 if ((address_expr->X_add_number + (1 << (shift + 20)))
7555 & ~((1 << (shift + 21)) - 1))
7556 as_bad (_("branch address range overflow (0x%lx)"),
7557 (unsigned long) address_expr->X_add_number);
7558 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7559 & 0x1fffff);
7560 }
7561 break;
7562
7563 case BFD_RELOC_MIPS_26_PCREL_S2:
7564 {
7565 int shift;
7566
7567 shift = 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 ((address_expr->X_add_number + (1 << (shift + 25)))
7572 & ~((1 << (shift + 26)) - 1))
7573 as_bad (_("branch address range overflow (0x%lx)"),
7574 (unsigned long) address_expr->X_add_number);
7575 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7576 & 0x3ffffff);
7577 }
7578 break;
7579
7580 default:
7581 {
7582 offsetT value;
7583
7584 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7585 &value))
7586 {
7587 ip->insn_opcode |= value & 0xffff;
7588 ip->complete_p = 1;
7589 }
7590 }
7591 break;
7592 }
7593 }
7594
7595 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7596 {
7597 /* There are a lot of optimizations we could do that we don't.
7598 In particular, we do not, in general, reorder instructions.
7599 If you use gcc with optimization, it will reorder
7600 instructions and generally do much more optimization then we
7601 do here; repeating all that work in the assembler would only
7602 benefit hand written assembly code, and does not seem worth
7603 it. */
7604 int nops = (mips_optimize == 0
7605 ? nops_for_insn (0, history, NULL)
7606 : nops_for_insn_or_target (0, history, ip));
7607 if (nops > 0)
7608 {
7609 fragS *old_frag;
7610 unsigned long old_frag_offset;
7611 int i;
7612
7613 old_frag = frag_now;
7614 old_frag_offset = frag_now_fix ();
7615
7616 for (i = 0; i < nops; i++)
7617 add_fixed_insn (NOP_INSN);
7618 insert_into_history (0, nops, NOP_INSN);
7619
7620 if (listing)
7621 {
7622 listing_prev_line ();
7623 /* We may be at the start of a variant frag. In case we
7624 are, make sure there is enough space for the frag
7625 after the frags created by listing_prev_line. The
7626 argument to frag_grow here must be at least as large
7627 as the argument to all other calls to frag_grow in
7628 this file. We don't have to worry about being in the
7629 middle of a variant frag, because the variants insert
7630 all needed nop instructions themselves. */
7631 frag_grow (40);
7632 }
7633
7634 mips_move_text_labels ();
7635
7636 #ifndef NO_ECOFF_DEBUGGING
7637 if (ECOFF_DEBUGGING)
7638 ecoff_fix_loc (old_frag, old_frag_offset);
7639 #endif
7640 }
7641 }
7642 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7643 {
7644 int nops;
7645
7646 /* Work out how many nops in prev_nop_frag are needed by IP,
7647 ignoring hazards generated by the first prev_nop_frag_since
7648 instructions. */
7649 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
7650 gas_assert (nops <= prev_nop_frag_holds);
7651
7652 /* Enforce NOPS as a minimum. */
7653 if (nops > prev_nop_frag_required)
7654 prev_nop_frag_required = nops;
7655
7656 if (prev_nop_frag_holds == prev_nop_frag_required)
7657 {
7658 /* Settle for the current number of nops. Update the history
7659 accordingly (for the benefit of any future .set reorder code). */
7660 prev_nop_frag = NULL;
7661 insert_into_history (prev_nop_frag_since,
7662 prev_nop_frag_holds, NOP_INSN);
7663 }
7664 else
7665 {
7666 /* Allow this instruction to replace one of the nops that was
7667 tentatively added to prev_nop_frag. */
7668 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
7669 prev_nop_frag_holds--;
7670 prev_nop_frag_since++;
7671 }
7672 }
7673
7674 method = get_append_method (ip, address_expr, reloc_type);
7675 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
7676
7677 dwarf2_emit_insn (0);
7678 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7679 so "move" the instruction address accordingly.
7680
7681 Also, it doesn't seem appropriate for the assembler to reorder .loc
7682 entries. If this instruction is a branch that we are going to swap
7683 with the previous instruction, the two instructions should be
7684 treated as a unit, and the debug information for both instructions
7685 should refer to the start of the branch sequence. Using the
7686 current position is certainly wrong when swapping a 32-bit branch
7687 and a 16-bit delay slot, since the current position would then be
7688 in the middle of a branch. */
7689 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
7690
7691 relax32 = (mips_relax_branch
7692 /* Don't try branch relaxation within .set nomacro, or within
7693 .set noat if we use $at for PIC computations. If it turns
7694 out that the branch was out-of-range, we'll get an error. */
7695 && !mips_opts.warn_about_macros
7696 && (mips_opts.at || mips_pic == NO_PIC)
7697 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7698 as they have no complementing branches. */
7699 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
7700
7701 if (!HAVE_CODE_COMPRESSION
7702 && address_expr
7703 && relax32
7704 && *reloc_type == BFD_RELOC_16_PCREL_S2
7705 && delayed_branch_p (ip))
7706 {
7707 relaxed_branch = TRUE;
7708 add_relaxed_insn (ip, (relaxed_branch_length
7709 (NULL, NULL,
7710 uncond_branch_p (ip) ? -1
7711 : branch_likely_p (ip) ? 1
7712 : 0)), 4,
7713 RELAX_BRANCH_ENCODE
7714 (AT, mips_pic != NO_PIC,
7715 uncond_branch_p (ip),
7716 branch_likely_p (ip),
7717 pinfo & INSN_WRITE_GPR_31,
7718 0),
7719 address_expr->X_add_symbol,
7720 address_expr->X_add_number);
7721 *reloc_type = BFD_RELOC_UNUSED;
7722 }
7723 else if (mips_opts.micromips
7724 && address_expr
7725 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7726 || *reloc_type > BFD_RELOC_UNUSED)
7727 && (delayed_branch_p (ip) || compact_branch_p (ip))
7728 /* Don't try branch relaxation when users specify
7729 16-bit/32-bit instructions. */
7730 && !forced_insn_length)
7731 {
7732 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7733 && *reloc_type > BFD_RELOC_UNUSED);
7734 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
7735 int uncond = uncond_branch_p (ip) ? -1 : 0;
7736 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7737 int nods = method == APPEND_ADD_WITH_NOP;
7738 int al = pinfo & INSN_WRITE_GPR_31;
7739 int length32 = nods ? 8 : 4;
7740
7741 gas_assert (address_expr != NULL);
7742 gas_assert (!mips_relax.sequence);
7743
7744 relaxed_branch = TRUE;
7745 if (nods)
7746 method = APPEND_ADD;
7747 if (relax32)
7748 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7749 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
7750 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
7751 mips_pic != NO_PIC,
7752 uncond, compact, al, nods,
7753 relax32, 0, 0),
7754 address_expr->X_add_symbol,
7755 address_expr->X_add_number);
7756 *reloc_type = BFD_RELOC_UNUSED;
7757 }
7758 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
7759 {
7760 bfd_boolean require_unextended;
7761 bfd_boolean require_extended;
7762 symbolS *symbol;
7763 offsetT offset;
7764
7765 if (forced_insn_length != 0)
7766 {
7767 require_unextended = forced_insn_length == 2;
7768 require_extended = forced_insn_length == 4;
7769 }
7770 else
7771 {
7772 require_unextended = (mips_opts.noautoextend
7773 && !mips_opcode_32bit_p (ip->insn_mo));
7774 require_extended = 0;
7775 }
7776
7777 /* We need to set up a variant frag. */
7778 gas_assert (address_expr != NULL);
7779 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7780 symbol created by `make_expr_symbol' may not get a necessary
7781 external relocation produced. */
7782 if (address_expr->X_op == O_symbol)
7783 {
7784 symbol = address_expr->X_add_symbol;
7785 offset = address_expr->X_add_number;
7786 }
7787 else
7788 {
7789 symbol = make_expr_symbol (address_expr);
7790 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
7791 offset = 0;
7792 }
7793 add_relaxed_insn (ip, 12, 0,
7794 RELAX_MIPS16_ENCODE
7795 (*reloc_type - BFD_RELOC_UNUSED,
7796 mips_opts.ase & ASE_MIPS16E2,
7797 mips_pic != NO_PIC,
7798 HAVE_32BIT_SYMBOLS,
7799 mips_opts.warn_about_macros,
7800 require_unextended, require_extended,
7801 delayed_branch_p (&history[0]),
7802 history[0].mips16_absolute_jump_p),
7803 symbol, offset);
7804 }
7805 else if (mips_opts.mips16 && insn_length (ip) == 2)
7806 {
7807 if (!delayed_branch_p (ip))
7808 /* Make sure there is enough room to swap this instruction with
7809 a following jump instruction. */
7810 frag_grow (6);
7811 add_fixed_insn (ip);
7812 }
7813 else
7814 {
7815 if (mips_opts.mips16
7816 && mips_opts.noreorder
7817 && delayed_branch_p (&history[0]))
7818 as_warn (_("extended instruction in delay slot"));
7819
7820 if (mips_relax.sequence)
7821 {
7822 /* If we've reached the end of this frag, turn it into a variant
7823 frag and record the information for the instructions we've
7824 written so far. */
7825 if (frag_room () < 4)
7826 relax_close_frag ();
7827 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
7828 }
7829
7830 if (mips_relax.sequence != 2)
7831 {
7832 if (mips_macro_warning.first_insn_sizes[0] == 0)
7833 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7834 mips_macro_warning.sizes[0] += insn_length (ip);
7835 mips_macro_warning.insns[0]++;
7836 }
7837 if (mips_relax.sequence != 1)
7838 {
7839 if (mips_macro_warning.first_insn_sizes[1] == 0)
7840 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7841 mips_macro_warning.sizes[1] += insn_length (ip);
7842 mips_macro_warning.insns[1]++;
7843 }
7844
7845 if (mips_opts.mips16)
7846 {
7847 ip->fixed_p = 1;
7848 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7849 }
7850 add_fixed_insn (ip);
7851 }
7852
7853 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
7854 {
7855 bfd_reloc_code_real_type final_type[3];
7856 reloc_howto_type *howto0;
7857 reloc_howto_type *howto;
7858 int i;
7859
7860 /* Perform any necessary conversion to microMIPS relocations
7861 and find out how many relocations there actually are. */
7862 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7863 final_type[i] = micromips_map_reloc (reloc_type[i]);
7864
7865 /* In a compound relocation, it is the final (outermost)
7866 operator that determines the relocated field. */
7867 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
7868 if (!howto)
7869 abort ();
7870
7871 if (i > 1)
7872 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
7873 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7874 bfd_get_reloc_size (howto),
7875 address_expr,
7876 howto0 && howto0->pc_relative,
7877 final_type[0]);
7878 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7879 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
7880
7881 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
7882 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
7883 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7884
7885 /* These relocations can have an addend that won't fit in
7886 4 octets for 64bit assembly. */
7887 if (GPR_SIZE == 64
7888 && ! howto->partial_inplace
7889 && (reloc_type[0] == BFD_RELOC_16
7890 || reloc_type[0] == BFD_RELOC_32
7891 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7892 || reloc_type[0] == BFD_RELOC_GPREL16
7893 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7894 || reloc_type[0] == BFD_RELOC_GPREL32
7895 || reloc_type[0] == BFD_RELOC_64
7896 || reloc_type[0] == BFD_RELOC_CTOR
7897 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7898 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7899 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7900 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7901 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7902 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7903 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7904 || hi16_reloc_p (reloc_type[0])
7905 || lo16_reloc_p (reloc_type[0])))
7906 ip->fixp[0]->fx_no_overflow = 1;
7907
7908 /* These relocations can have an addend that won't fit in 2 octets. */
7909 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7910 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7911 ip->fixp[0]->fx_no_overflow = 1;
7912
7913 if (mips_relax.sequence)
7914 {
7915 if (mips_relax.first_fixup == 0)
7916 mips_relax.first_fixup = ip->fixp[0];
7917 }
7918 else if (reloc_needs_lo_p (*reloc_type))
7919 {
7920 struct mips_hi_fixup *hi_fixup;
7921
7922 /* Reuse the last entry if it already has a matching %lo. */
7923 hi_fixup = mips_hi_fixup_list;
7924 if (hi_fixup == 0
7925 || !fixup_has_matching_lo_p (hi_fixup->fixp))
7926 {
7927 hi_fixup = XNEW (struct mips_hi_fixup);
7928 hi_fixup->next = mips_hi_fixup_list;
7929 mips_hi_fixup_list = hi_fixup;
7930 }
7931 hi_fixup->fixp = ip->fixp[0];
7932 hi_fixup->seg = now_seg;
7933 }
7934
7935 /* Add fixups for the second and third relocations, if given.
7936 Note that the ABI allows the second relocation to be
7937 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7938 moment we only use RSS_UNDEF, but we could add support
7939 for the others if it ever becomes necessary. */
7940 for (i = 1; i < 3; i++)
7941 if (reloc_type[i] != BFD_RELOC_UNUSED)
7942 {
7943 ip->fixp[i] = fix_new (ip->frag, ip->where,
7944 ip->fixp[0]->fx_size, NULL, 0,
7945 FALSE, final_type[i]);
7946
7947 /* Use fx_tcbit to mark compound relocs. */
7948 ip->fixp[0]->fx_tcbit = 1;
7949 ip->fixp[i]->fx_tcbit = 1;
7950 }
7951 }
7952
7953 /* Update the register mask information. */
7954 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7955 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
7956
7957 switch (method)
7958 {
7959 case APPEND_ADD:
7960 insert_into_history (0, 1, ip);
7961 break;
7962
7963 case APPEND_ADD_WITH_NOP:
7964 {
7965 struct mips_cl_insn *nop;
7966
7967 insert_into_history (0, 1, ip);
7968 nop = get_delay_slot_nop (ip);
7969 add_fixed_insn (nop);
7970 insert_into_history (0, 1, nop);
7971 if (mips_relax.sequence)
7972 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7973 }
7974 break;
7975
7976 case APPEND_ADD_COMPACT:
7977 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7978 if (mips_opts.mips16)
7979 {
7980 ip->insn_opcode |= 0x0080;
7981 find_altered_mips16_opcode (ip);
7982 }
7983 /* Convert microMIPS instructions. */
7984 else if (mips_opts.micromips)
7985 {
7986 /* jr16->jrc */
7987 if ((ip->insn_opcode & 0xffe0) == 0x4580)
7988 ip->insn_opcode |= 0x0020;
7989 /* b16->bc */
7990 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7991 ip->insn_opcode = 0x40e00000;
7992 /* beqz16->beqzc, bnez16->bnezc */
7993 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7994 {
7995 unsigned long regno;
7996
7997 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7998 regno &= MICROMIPSOP_MASK_MD;
7999 regno = micromips_to_32_reg_d_map[regno];
8000 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
8001 | (regno << MICROMIPSOP_SH_RS)
8002 | 0x40a00000) ^ 0x00400000;
8003 }
8004 /* beqz->beqzc, bnez->bnezc */
8005 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
8006 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
8007 | ((ip->insn_opcode >> 7) & 0x00400000)
8008 | 0x40a00000) ^ 0x00400000;
8009 /* beq $0->beqzc, bne $0->bnezc */
8010 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
8011 ip->insn_opcode = (((ip->insn_opcode >>
8012 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
8013 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
8014 | ((ip->insn_opcode >> 7) & 0x00400000)
8015 | 0x40a00000) ^ 0x00400000;
8016 else
8017 abort ();
8018 find_altered_micromips_opcode (ip);
8019 }
8020 else
8021 abort ();
8022 install_insn (ip);
8023 insert_into_history (0, 1, ip);
8024 break;
8025
8026 case APPEND_SWAP:
8027 {
8028 struct mips_cl_insn delay = history[0];
8029
8030 if (relaxed_branch || delay.frag != ip->frag)
8031 {
8032 /* Add the delay slot instruction to the end of the
8033 current frag and shrink the fixed part of the
8034 original frag. If the branch occupies the tail of
8035 the latter, move it backwards to cover the gap. */
8036 delay.frag->fr_fix -= branch_disp;
8037 if (delay.frag == ip->frag)
8038 move_insn (ip, ip->frag, ip->where - branch_disp);
8039 add_fixed_insn (&delay);
8040 }
8041 else
8042 {
8043 /* If this is not a relaxed branch and we are in the
8044 same frag, then just swap the instructions. */
8045 move_insn (ip, delay.frag, delay.where);
8046 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
8047 }
8048 history[0] = *ip;
8049 delay.fixed_p = 1;
8050 insert_into_history (0, 1, &delay);
8051 }
8052 break;
8053 }
8054
8055 /* If we have just completed an unconditional branch, clear the history. */
8056 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
8057 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
8058 {
8059 unsigned int i;
8060
8061 mips_no_prev_insn ();
8062
8063 for (i = 0; i < ARRAY_SIZE (history); i++)
8064 history[i].cleared_p = 1;
8065 }
8066
8067 /* We need to emit a label at the end of branch-likely macros. */
8068 if (emit_branch_likely_macro)
8069 {
8070 emit_branch_likely_macro = FALSE;
8071 micromips_add_label ();
8072 }
8073
8074 /* We just output an insn, so the next one doesn't have a label. */
8075 mips_clear_insn_labels ();
8076 }
8077
8078 /* Forget that there was any previous instruction or label.
8079 When BRANCH is true, the branch history is also flushed. */
8080
8081 static void
8082 mips_no_prev_insn (void)
8083 {
8084 prev_nop_frag = NULL;
8085 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
8086 mips_clear_insn_labels ();
8087 }
8088
8089 /* This function must be called before we emit something other than
8090 instructions. It is like mips_no_prev_insn except that it inserts
8091 any NOPS that might be needed by previous instructions. */
8092
8093 void
8094 mips_emit_delays (void)
8095 {
8096 if (! mips_opts.noreorder)
8097 {
8098 int nops = nops_for_insn (0, history, NULL);
8099 if (nops > 0)
8100 {
8101 while (nops-- > 0)
8102 add_fixed_insn (NOP_INSN);
8103 mips_move_text_labels ();
8104 }
8105 }
8106 mips_no_prev_insn ();
8107 }
8108
8109 /* Start a (possibly nested) noreorder block. */
8110
8111 static void
8112 start_noreorder (void)
8113 {
8114 if (mips_opts.noreorder == 0)
8115 {
8116 unsigned int i;
8117 int nops;
8118
8119 /* None of the instructions before the .set noreorder can be moved. */
8120 for (i = 0; i < ARRAY_SIZE (history); i++)
8121 history[i].fixed_p = 1;
8122
8123 /* Insert any nops that might be needed between the .set noreorder
8124 block and the previous instructions. We will later remove any
8125 nops that turn out not to be needed. */
8126 nops = nops_for_insn (0, history, NULL);
8127 if (nops > 0)
8128 {
8129 if (mips_optimize != 0)
8130 {
8131 /* Record the frag which holds the nop instructions, so
8132 that we can remove them if we don't need them. */
8133 frag_grow (nops * NOP_INSN_SIZE);
8134 prev_nop_frag = frag_now;
8135 prev_nop_frag_holds = nops;
8136 prev_nop_frag_required = 0;
8137 prev_nop_frag_since = 0;
8138 }
8139
8140 for (; nops > 0; --nops)
8141 add_fixed_insn (NOP_INSN);
8142
8143 /* Move on to a new frag, so that it is safe to simply
8144 decrease the size of prev_nop_frag. */
8145 frag_wane (frag_now);
8146 frag_new (0);
8147 mips_move_text_labels ();
8148 }
8149 mips_mark_labels ();
8150 mips_clear_insn_labels ();
8151 }
8152 mips_opts.noreorder++;
8153 mips_any_noreorder = 1;
8154 }
8155
8156 /* End a nested noreorder block. */
8157
8158 static void
8159 end_noreorder (void)
8160 {
8161 mips_opts.noreorder--;
8162 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
8163 {
8164 /* Commit to inserting prev_nop_frag_required nops and go back to
8165 handling nop insertion the .set reorder way. */
8166 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
8167 * NOP_INSN_SIZE);
8168 insert_into_history (prev_nop_frag_since,
8169 prev_nop_frag_required, NOP_INSN);
8170 prev_nop_frag = NULL;
8171 }
8172 }
8173
8174 /* Sign-extend 32-bit mode constants that have bit 31 set and all
8175 higher bits unset. */
8176
8177 static void
8178 normalize_constant_expr (expressionS *ex)
8179 {
8180 if (ex->X_op == O_constant
8181 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8182 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8183 - 0x80000000);
8184 }
8185
8186 /* Sign-extend 32-bit mode address offsets that have bit 31 set and
8187 all higher bits unset. */
8188
8189 static void
8190 normalize_address_expr (expressionS *ex)
8191 {
8192 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
8193 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
8194 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8195 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8196 - 0x80000000);
8197 }
8198
8199 /* Try to match TOKENS against OPCODE, storing the result in INSN.
8200 Return true if the match was successful.
8201
8202 OPCODE_EXTRA is a value that should be ORed into the opcode
8203 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
8204 there are more alternatives after OPCODE and SOFT_MATCH is
8205 as for mips_arg_info. */
8206
8207 static bfd_boolean
8208 match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8209 struct mips_operand_token *tokens, unsigned int opcode_extra,
8210 bfd_boolean lax_match, bfd_boolean complete_p)
8211 {
8212 const char *args;
8213 struct mips_arg_info arg;
8214 const struct mips_operand *operand;
8215 char c;
8216
8217 imm_expr.X_op = O_absent;
8218 offset_expr.X_op = O_absent;
8219 offset_reloc[0] = BFD_RELOC_UNUSED;
8220 offset_reloc[1] = BFD_RELOC_UNUSED;
8221 offset_reloc[2] = BFD_RELOC_UNUSED;
8222
8223 create_insn (insn, opcode);
8224 /* When no opcode suffix is specified, assume ".xyzw". */
8225 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8226 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8227 else
8228 insn->insn_opcode |= opcode_extra;
8229 memset (&arg, 0, sizeof (arg));
8230 arg.insn = insn;
8231 arg.token = tokens;
8232 arg.argnum = 1;
8233 arg.last_regno = ILLEGAL_REG;
8234 arg.dest_regno = ILLEGAL_REG;
8235 arg.lax_match = lax_match;
8236 for (args = opcode->args;; ++args)
8237 {
8238 if (arg.token->type == OT_END)
8239 {
8240 /* Handle unary instructions in which only one operand is given.
8241 The source is then the same as the destination. */
8242 if (arg.opnum == 1 && *args == ',')
8243 {
8244 operand = (mips_opts.micromips
8245 ? decode_micromips_operand (args + 1)
8246 : decode_mips_operand (args + 1));
8247 if (operand && mips_optional_operand_p (operand))
8248 {
8249 arg.token = tokens;
8250 arg.argnum = 1;
8251 continue;
8252 }
8253 }
8254
8255 /* Treat elided base registers as $0. */
8256 if (strcmp (args, "(b)") == 0)
8257 args += 3;
8258
8259 if (args[0] == '+')
8260 switch (args[1])
8261 {
8262 case 'K':
8263 case 'N':
8264 /* The register suffix is optional. */
8265 args += 2;
8266 break;
8267 }
8268
8269 /* Fail the match if there were too few operands. */
8270 if (*args)
8271 return FALSE;
8272
8273 /* Successful match. */
8274 if (!complete_p)
8275 return TRUE;
8276 clear_insn_error ();
8277 if (arg.dest_regno == arg.last_regno
8278 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
8279 {
8280 if (arg.opnum == 2)
8281 set_insn_error
8282 (0, _("source and destination must be different"));
8283 else if (arg.last_regno == 31)
8284 set_insn_error
8285 (0, _("a destination register must be supplied"));
8286 }
8287 else if (arg.last_regno == 31
8288 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
8289 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
8290 set_insn_error (0, _("the source register must not be $31"));
8291 check_completed_insn (&arg);
8292 return TRUE;
8293 }
8294
8295 /* Fail the match if the line has too many operands. */
8296 if (*args == 0)
8297 return FALSE;
8298
8299 /* Handle characters that need to match exactly. */
8300 if (*args == '(' || *args == ')' || *args == ',')
8301 {
8302 if (match_char (&arg, *args))
8303 continue;
8304 return FALSE;
8305 }
8306 if (*args == '#')
8307 {
8308 ++args;
8309 if (arg.token->type == OT_DOUBLE_CHAR
8310 && arg.token->u.ch == *args)
8311 {
8312 ++arg.token;
8313 continue;
8314 }
8315 return FALSE;
8316 }
8317
8318 /* Handle special macro operands. Work out the properties of
8319 other operands. */
8320 arg.opnum += 1;
8321 switch (*args)
8322 {
8323 case '-':
8324 switch (args[1])
8325 {
8326 case 'A':
8327 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8328 break;
8329
8330 case 'B':
8331 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8332 break;
8333 }
8334 break;
8335
8336 case '+':
8337 switch (args[1])
8338 {
8339 case 'i':
8340 *offset_reloc = BFD_RELOC_MIPS_JMP;
8341 break;
8342
8343 case '\'':
8344 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8345 break;
8346
8347 case '\"':
8348 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8349 break;
8350 }
8351 break;
8352
8353 case 'I':
8354 if (!match_const_int (&arg, &imm_expr.X_add_number))
8355 return FALSE;
8356 imm_expr.X_op = O_constant;
8357 if (GPR_SIZE == 32)
8358 normalize_constant_expr (&imm_expr);
8359 continue;
8360
8361 case 'A':
8362 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8363 {
8364 /* Assume that the offset has been elided and that what
8365 we saw was a base register. The match will fail later
8366 if that assumption turns out to be wrong. */
8367 offset_expr.X_op = O_constant;
8368 offset_expr.X_add_number = 0;
8369 }
8370 else
8371 {
8372 if (!match_expression (&arg, &offset_expr, offset_reloc))
8373 return FALSE;
8374 normalize_address_expr (&offset_expr);
8375 }
8376 continue;
8377
8378 case 'F':
8379 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8380 8, TRUE))
8381 return FALSE;
8382 continue;
8383
8384 case 'L':
8385 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8386 8, FALSE))
8387 return FALSE;
8388 continue;
8389
8390 case 'f':
8391 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8392 4, TRUE))
8393 return FALSE;
8394 continue;
8395
8396 case 'l':
8397 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8398 4, FALSE))
8399 return FALSE;
8400 continue;
8401
8402 case 'p':
8403 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8404 break;
8405
8406 case 'a':
8407 *offset_reloc = BFD_RELOC_MIPS_JMP;
8408 break;
8409
8410 case 'm':
8411 gas_assert (mips_opts.micromips);
8412 c = args[1];
8413 switch (c)
8414 {
8415 case 'D':
8416 case 'E':
8417 if (!forced_insn_length)
8418 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8419 else if (c == 'D')
8420 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8421 else
8422 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8423 break;
8424 }
8425 break;
8426 }
8427
8428 operand = (mips_opts.micromips
8429 ? decode_micromips_operand (args)
8430 : decode_mips_operand (args));
8431 if (!operand)
8432 abort ();
8433
8434 /* Skip prefixes. */
8435 if (*args == '+' || *args == 'm' || *args == '-')
8436 args++;
8437
8438 if (mips_optional_operand_p (operand)
8439 && args[1] == ','
8440 && (arg.token[0].type != OT_REG
8441 || arg.token[1].type == OT_END))
8442 {
8443 /* Assume that the register has been elided and is the
8444 same as the first operand. */
8445 arg.token = tokens;
8446 arg.argnum = 1;
8447 }
8448
8449 if (!match_operand (&arg, operand))
8450 return FALSE;
8451 }
8452 }
8453
8454 /* Like match_insn, but for MIPS16. */
8455
8456 static bfd_boolean
8457 match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8458 struct mips_operand_token *tokens)
8459 {
8460 const char *args;
8461 const struct mips_operand *operand;
8462 const struct mips_operand *ext_operand;
8463 bfd_boolean pcrel = FALSE;
8464 int required_insn_length;
8465 struct mips_arg_info arg;
8466 int relax_char;
8467
8468 if (forced_insn_length)
8469 required_insn_length = forced_insn_length;
8470 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8471 required_insn_length = 2;
8472 else
8473 required_insn_length = 0;
8474
8475 create_insn (insn, opcode);
8476 imm_expr.X_op = O_absent;
8477 offset_expr.X_op = O_absent;
8478 offset_reloc[0] = BFD_RELOC_UNUSED;
8479 offset_reloc[1] = BFD_RELOC_UNUSED;
8480 offset_reloc[2] = BFD_RELOC_UNUSED;
8481 relax_char = 0;
8482
8483 memset (&arg, 0, sizeof (arg));
8484 arg.insn = insn;
8485 arg.token = tokens;
8486 arg.argnum = 1;
8487 arg.last_regno = ILLEGAL_REG;
8488 arg.dest_regno = ILLEGAL_REG;
8489 relax_char = 0;
8490 for (args = opcode->args;; ++args)
8491 {
8492 int c;
8493
8494 if (arg.token->type == OT_END)
8495 {
8496 offsetT value;
8497
8498 /* Handle unary instructions in which only one operand is given.
8499 The source is then the same as the destination. */
8500 if (arg.opnum == 1 && *args == ',')
8501 {
8502 operand = decode_mips16_operand (args[1], FALSE);
8503 if (operand && mips_optional_operand_p (operand))
8504 {
8505 arg.token = tokens;
8506 arg.argnum = 1;
8507 continue;
8508 }
8509 }
8510
8511 /* Fail the match if there were too few operands. */
8512 if (*args)
8513 return FALSE;
8514
8515 /* Successful match. Stuff the immediate value in now, if
8516 we can. */
8517 clear_insn_error ();
8518 if (opcode->pinfo == INSN_MACRO)
8519 {
8520 gas_assert (relax_char == 0 || relax_char == 'p');
8521 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8522 }
8523 else if (relax_char
8524 && offset_expr.X_op == O_constant
8525 && !pcrel
8526 && calculate_reloc (*offset_reloc,
8527 offset_expr.X_add_number,
8528 &value))
8529 {
8530 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
8531 required_insn_length, &insn->insn_opcode);
8532 offset_expr.X_op = O_absent;
8533 *offset_reloc = BFD_RELOC_UNUSED;
8534 }
8535 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8536 {
8537 if (required_insn_length == 2)
8538 set_insn_error (0, _("invalid unextended operand value"));
8539 else if (!mips_opcode_32bit_p (opcode))
8540 {
8541 forced_insn_length = 4;
8542 insn->insn_opcode |= MIPS16_EXTEND;
8543 }
8544 }
8545 else if (relax_char)
8546 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8547
8548 check_completed_insn (&arg);
8549 return TRUE;
8550 }
8551
8552 /* Fail the match if the line has too many operands. */
8553 if (*args == 0)
8554 return FALSE;
8555
8556 /* Handle characters that need to match exactly. */
8557 if (*args == '(' || *args == ')' || *args == ',')
8558 {
8559 if (match_char (&arg, *args))
8560 continue;
8561 return FALSE;
8562 }
8563
8564 arg.opnum += 1;
8565 c = *args;
8566 switch (c)
8567 {
8568 case 'p':
8569 case 'q':
8570 case 'A':
8571 case 'B':
8572 case 'E':
8573 case 'V':
8574 case 'u':
8575 relax_char = c;
8576 break;
8577
8578 case 'I':
8579 if (!match_const_int (&arg, &imm_expr.X_add_number))
8580 return FALSE;
8581 imm_expr.X_op = O_constant;
8582 if (GPR_SIZE == 32)
8583 normalize_constant_expr (&imm_expr);
8584 continue;
8585
8586 case 'a':
8587 case 'i':
8588 *offset_reloc = BFD_RELOC_MIPS16_JMP;
8589 break;
8590 }
8591
8592 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
8593 if (!operand)
8594 abort ();
8595
8596 if (operand->type == OP_PCREL)
8597 pcrel = TRUE;
8598 else
8599 {
8600 ext_operand = decode_mips16_operand (c, TRUE);
8601 if (operand != ext_operand)
8602 {
8603 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8604 {
8605 offset_expr.X_op = O_constant;
8606 offset_expr.X_add_number = 0;
8607 relax_char = c;
8608 continue;
8609 }
8610
8611 if (!match_expression (&arg, &offset_expr, offset_reloc))
8612 return FALSE;
8613
8614 /* '8' is used for SLTI(U) and has traditionally not
8615 been allowed to take relocation operators. */
8616 if (offset_reloc[0] != BFD_RELOC_UNUSED
8617 && (ext_operand->size != 16 || c == '8'))
8618 {
8619 match_not_constant (&arg);
8620 return FALSE;
8621 }
8622
8623 if (offset_expr.X_op == O_big)
8624 {
8625 match_out_of_range (&arg);
8626 return FALSE;
8627 }
8628
8629 relax_char = c;
8630 continue;
8631 }
8632 }
8633
8634 if (mips_optional_operand_p (operand)
8635 && args[1] == ','
8636 && (arg.token[0].type != OT_REG
8637 || arg.token[1].type == OT_END))
8638 {
8639 /* Assume that the register has been elided and is the
8640 same as the first operand. */
8641 arg.token = tokens;
8642 arg.argnum = 1;
8643 }
8644
8645 if (!match_operand (&arg, operand))
8646 return FALSE;
8647 }
8648 }
8649
8650 /* Record that the current instruction is invalid for the current ISA. */
8651
8652 static void
8653 match_invalid_for_isa (void)
8654 {
8655 set_insn_error_ss
8656 (0, _("opcode not supported on this processor: %s (%s)"),
8657 mips_cpu_info_from_arch (mips_opts.arch)->name,
8658 mips_cpu_info_from_isa (mips_opts.isa)->name);
8659 }
8660
8661 /* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8662 Return true if a definite match or failure was found, storing any match
8663 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8664 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8665 tried and failed to match under normal conditions and now want to try a
8666 more relaxed match. */
8667
8668 static bfd_boolean
8669 match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8670 const struct mips_opcode *past, struct mips_operand_token *tokens,
8671 int opcode_extra, bfd_boolean lax_match)
8672 {
8673 const struct mips_opcode *opcode;
8674 const struct mips_opcode *invalid_delay_slot;
8675 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8676
8677 /* Search for a match, ignoring alternatives that don't satisfy the
8678 current ISA or forced_length. */
8679 invalid_delay_slot = 0;
8680 seen_valid_for_isa = FALSE;
8681 seen_valid_for_size = FALSE;
8682 opcode = first;
8683 do
8684 {
8685 gas_assert (strcmp (opcode->name, first->name) == 0);
8686 if (is_opcode_valid (opcode))
8687 {
8688 seen_valid_for_isa = TRUE;
8689 if (is_size_valid (opcode))
8690 {
8691 bfd_boolean delay_slot_ok;
8692
8693 seen_valid_for_size = TRUE;
8694 delay_slot_ok = is_delay_slot_valid (opcode);
8695 if (match_insn (insn, opcode, tokens, opcode_extra,
8696 lax_match, delay_slot_ok))
8697 {
8698 if (!delay_slot_ok)
8699 {
8700 if (!invalid_delay_slot)
8701 invalid_delay_slot = opcode;
8702 }
8703 else
8704 return TRUE;
8705 }
8706 }
8707 }
8708 ++opcode;
8709 }
8710 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8711
8712 /* If the only matches we found had the wrong length for the delay slot,
8713 pick the first such match. We'll issue an appropriate warning later. */
8714 if (invalid_delay_slot)
8715 {
8716 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8717 lax_match, TRUE))
8718 return TRUE;
8719 abort ();
8720 }
8721
8722 /* Handle the case where we didn't try to match an instruction because
8723 all the alternatives were incompatible with the current ISA. */
8724 if (!seen_valid_for_isa)
8725 {
8726 match_invalid_for_isa ();
8727 return TRUE;
8728 }
8729
8730 /* Handle the case where we didn't try to match an instruction because
8731 all the alternatives were of the wrong size. */
8732 if (!seen_valid_for_size)
8733 {
8734 if (mips_opts.insn32)
8735 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
8736 else
8737 set_insn_error_i
8738 (0, _("unrecognized %d-bit version of microMIPS opcode"),
8739 8 * forced_insn_length);
8740 return TRUE;
8741 }
8742
8743 return FALSE;
8744 }
8745
8746 /* Like match_insns, but for MIPS16. */
8747
8748 static bfd_boolean
8749 match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8750 struct mips_operand_token *tokens)
8751 {
8752 const struct mips_opcode *opcode;
8753 bfd_boolean seen_valid_for_isa;
8754 bfd_boolean seen_valid_for_size;
8755
8756 /* Search for a match, ignoring alternatives that don't satisfy the
8757 current ISA. There are no separate entries for extended forms so
8758 we deal with forced_length later. */
8759 seen_valid_for_isa = FALSE;
8760 seen_valid_for_size = FALSE;
8761 opcode = first;
8762 do
8763 {
8764 gas_assert (strcmp (opcode->name, first->name) == 0);
8765 if (is_opcode_valid_16 (opcode))
8766 {
8767 seen_valid_for_isa = TRUE;
8768 if (is_size_valid_16 (opcode))
8769 {
8770 seen_valid_for_size = TRUE;
8771 if (match_mips16_insn (insn, opcode, tokens))
8772 return TRUE;
8773 }
8774 }
8775 ++opcode;
8776 }
8777 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8778 && strcmp (opcode->name, first->name) == 0);
8779
8780 /* Handle the case where we didn't try to match an instruction because
8781 all the alternatives were incompatible with the current ISA. */
8782 if (!seen_valid_for_isa)
8783 {
8784 match_invalid_for_isa ();
8785 return TRUE;
8786 }
8787
8788 /* Handle the case where we didn't try to match an instruction because
8789 all the alternatives were of the wrong size. */
8790 if (!seen_valid_for_size)
8791 {
8792 if (forced_insn_length == 2)
8793 set_insn_error
8794 (0, _("unrecognized unextended version of MIPS16 opcode"));
8795 else
8796 set_insn_error
8797 (0, _("unrecognized extended version of MIPS16 opcode"));
8798 return TRUE;
8799 }
8800
8801 return FALSE;
8802 }
8803
8804 /* Set up global variables for the start of a new macro. */
8805
8806 static void
8807 macro_start (void)
8808 {
8809 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
8810 memset (&mips_macro_warning.first_insn_sizes, 0,
8811 sizeof (mips_macro_warning.first_insn_sizes));
8812 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
8813 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
8814 && delayed_branch_p (&history[0]));
8815 if (history[0].frag
8816 && history[0].frag->fr_type == rs_machine_dependent
8817 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8818 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8819 mips_macro_warning.delay_slot_length = 0;
8820 else
8821 switch (history[0].insn_mo->pinfo2
8822 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8823 {
8824 case INSN2_BRANCH_DELAY_32BIT:
8825 mips_macro_warning.delay_slot_length = 4;
8826 break;
8827 case INSN2_BRANCH_DELAY_16BIT:
8828 mips_macro_warning.delay_slot_length = 2;
8829 break;
8830 default:
8831 mips_macro_warning.delay_slot_length = 0;
8832 break;
8833 }
8834 mips_macro_warning.first_frag = NULL;
8835 }
8836
8837 /* Given that a macro is longer than one instruction or of the wrong size,
8838 return the appropriate warning for it. Return null if no warning is
8839 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8840 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8841 and RELAX_NOMACRO. */
8842
8843 static const char *
8844 macro_warning (relax_substateT subtype)
8845 {
8846 if (subtype & RELAX_DELAY_SLOT)
8847 return _("macro instruction expanded into multiple instructions"
8848 " in a branch delay slot");
8849 else if (subtype & RELAX_NOMACRO)
8850 return _("macro instruction expanded into multiple instructions");
8851 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8852 | RELAX_DELAY_SLOT_SIZE_SECOND))
8853 return ((subtype & RELAX_DELAY_SLOT_16BIT)
8854 ? _("macro instruction expanded into a wrong size instruction"
8855 " in a 16-bit branch delay slot")
8856 : _("macro instruction expanded into a wrong size instruction"
8857 " in a 32-bit branch delay slot"));
8858 else
8859 return 0;
8860 }
8861
8862 /* Finish up a macro. Emit warnings as appropriate. */
8863
8864 static void
8865 macro_end (void)
8866 {
8867 /* Relaxation warning flags. */
8868 relax_substateT subtype = 0;
8869
8870 /* Check delay slot size requirements. */
8871 if (mips_macro_warning.delay_slot_length == 2)
8872 subtype |= RELAX_DELAY_SLOT_16BIT;
8873 if (mips_macro_warning.delay_slot_length != 0)
8874 {
8875 if (mips_macro_warning.delay_slot_length
8876 != mips_macro_warning.first_insn_sizes[0])
8877 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8878 if (mips_macro_warning.delay_slot_length
8879 != mips_macro_warning.first_insn_sizes[1])
8880 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8881 }
8882
8883 /* Check instruction count requirements. */
8884 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8885 {
8886 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
8887 subtype |= RELAX_SECOND_LONGER;
8888 if (mips_opts.warn_about_macros)
8889 subtype |= RELAX_NOMACRO;
8890 if (mips_macro_warning.delay_slot_p)
8891 subtype |= RELAX_DELAY_SLOT;
8892 }
8893
8894 /* If both alternatives fail to fill a delay slot correctly,
8895 emit the warning now. */
8896 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8897 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8898 {
8899 relax_substateT s;
8900 const char *msg;
8901
8902 s = subtype & (RELAX_DELAY_SLOT_16BIT
8903 | RELAX_DELAY_SLOT_SIZE_FIRST
8904 | RELAX_DELAY_SLOT_SIZE_SECOND);
8905 msg = macro_warning (s);
8906 if (msg != NULL)
8907 as_warn ("%s", msg);
8908 subtype &= ~s;
8909 }
8910
8911 /* If both implementations are longer than 1 instruction, then emit the
8912 warning now. */
8913 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8914 {
8915 relax_substateT s;
8916 const char *msg;
8917
8918 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8919 msg = macro_warning (s);
8920 if (msg != NULL)
8921 as_warn ("%s", msg);
8922 subtype &= ~s;
8923 }
8924
8925 /* If any flags still set, then one implementation might need a warning
8926 and the other either will need one of a different kind or none at all.
8927 Pass any remaining flags over to relaxation. */
8928 if (mips_macro_warning.first_frag != NULL)
8929 mips_macro_warning.first_frag->fr_subtype |= subtype;
8930 }
8931
8932 /* Instruction operand formats used in macros that vary between
8933 standard MIPS and microMIPS code. */
8934
8935 static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
8936 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8937 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8938 static const char * const lui_fmt[2] = { "t,u", "s,u" };
8939 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
8940 static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
8941 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8942 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8943
8944 #define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
8945 #define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8946 : cop12_fmt[mips_opts.micromips])
8947 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
8948 #define LUI_FMT (lui_fmt[mips_opts.micromips])
8949 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
8950 #define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8951 : mem12_fmt[mips_opts.micromips])
8952 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
8953 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
8954 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
8955
8956 /* Read a macro's relocation codes from *ARGS and store them in *R.
8957 The first argument in *ARGS will be either the code for a single
8958 relocation or -1 followed by the three codes that make up a
8959 composite relocation. */
8960
8961 static void
8962 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8963 {
8964 int i, next;
8965
8966 next = va_arg (*args, int);
8967 if (next >= 0)
8968 r[0] = (bfd_reloc_code_real_type) next;
8969 else
8970 {
8971 for (i = 0; i < 3; i++)
8972 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8973 /* This function is only used for 16-bit relocation fields.
8974 To make the macro code simpler, treat an unrelocated value
8975 in the same way as BFD_RELOC_LO16. */
8976 if (r[0] == BFD_RELOC_UNUSED)
8977 r[0] = BFD_RELOC_LO16;
8978 }
8979 }
8980
8981 /* Build an instruction created by a macro expansion. This is passed
8982 a pointer to the count of instructions created so far, an
8983 expression, the name of the instruction to build, an operand format
8984 string, and corresponding arguments. */
8985
8986 static void
8987 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
8988 {
8989 const struct mips_opcode *mo = NULL;
8990 bfd_reloc_code_real_type r[3];
8991 const struct mips_opcode *amo;
8992 const struct mips_operand *operand;
8993 struct hash_control *hash;
8994 struct mips_cl_insn insn;
8995 va_list args;
8996 unsigned int uval;
8997
8998 va_start (args, fmt);
8999
9000 if (mips_opts.mips16)
9001 {
9002 mips16_macro_build (ep, name, fmt, &args);
9003 va_end (args);
9004 return;
9005 }
9006
9007 r[0] = BFD_RELOC_UNUSED;
9008 r[1] = BFD_RELOC_UNUSED;
9009 r[2] = BFD_RELOC_UNUSED;
9010 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
9011 amo = (struct mips_opcode *) hash_find (hash, name);
9012 gas_assert (amo);
9013 gas_assert (strcmp (name, amo->name) == 0);
9014
9015 do
9016 {
9017 /* Search until we get a match for NAME. It is assumed here that
9018 macros will never generate MDMX, MIPS-3D, or MT instructions.
9019 We try to match an instruction that fulfills the branch delay
9020 slot instruction length requirement (if any) of the previous
9021 instruction. While doing this we record the first instruction
9022 seen that matches all the other conditions and use it anyway
9023 if the requirement cannot be met; we will issue an appropriate
9024 warning later on. */
9025 if (strcmp (fmt, amo->args) == 0
9026 && amo->pinfo != INSN_MACRO
9027 && is_opcode_valid (amo)
9028 && is_size_valid (amo))
9029 {
9030 if (is_delay_slot_valid (amo))
9031 {
9032 mo = amo;
9033 break;
9034 }
9035 else if (!mo)
9036 mo = amo;
9037 }
9038
9039 ++amo;
9040 gas_assert (amo->name);
9041 }
9042 while (strcmp (name, amo->name) == 0);
9043
9044 gas_assert (mo);
9045 create_insn (&insn, mo);
9046 for (; *fmt; ++fmt)
9047 {
9048 switch (*fmt)
9049 {
9050 case ',':
9051 case '(':
9052 case ')':
9053 case 'z':
9054 break;
9055
9056 case 'i':
9057 case 'j':
9058 macro_read_relocs (&args, r);
9059 gas_assert (*r == BFD_RELOC_GPREL16
9060 || *r == BFD_RELOC_MIPS_HIGHER
9061 || *r == BFD_RELOC_HI16_S
9062 || *r == BFD_RELOC_LO16
9063 || *r == BFD_RELOC_MIPS_GOT_OFST
9064 || (mips_opts.micromips
9065 && (*r == BFD_RELOC_16
9066 || *r == BFD_RELOC_MIPS_GOT16
9067 || *r == BFD_RELOC_MIPS_CALL16
9068 || *r == BFD_RELOC_MIPS_GOT_HI16
9069 || *r == BFD_RELOC_MIPS_GOT_LO16
9070 || *r == BFD_RELOC_MIPS_CALL_HI16
9071 || *r == BFD_RELOC_MIPS_CALL_LO16
9072 || *r == BFD_RELOC_MIPS_SUB
9073 || *r == BFD_RELOC_MIPS_GOT_PAGE
9074 || *r == BFD_RELOC_MIPS_HIGHEST
9075 || *r == BFD_RELOC_MIPS_GOT_DISP
9076 || *r == BFD_RELOC_MIPS_TLS_GD
9077 || *r == BFD_RELOC_MIPS_TLS_LDM
9078 || *r == BFD_RELOC_MIPS_TLS_DTPREL_HI16
9079 || *r == BFD_RELOC_MIPS_TLS_DTPREL_LO16
9080 || *r == BFD_RELOC_MIPS_TLS_GOTTPREL
9081 || *r == BFD_RELOC_MIPS_TLS_TPREL_HI16
9082 || *r == BFD_RELOC_MIPS_TLS_TPREL_LO16)));
9083 break;
9084
9085 case 'o':
9086 macro_read_relocs (&args, r);
9087 break;
9088
9089 case 'u':
9090 macro_read_relocs (&args, r);
9091 gas_assert (ep != NULL
9092 && (ep->X_op == O_constant
9093 || (ep->X_op == O_symbol
9094 && (*r == BFD_RELOC_MIPS_HIGHEST
9095 || *r == BFD_RELOC_HI16_S
9096 || *r == BFD_RELOC_HI16
9097 || *r == BFD_RELOC_GPREL16
9098 || *r == BFD_RELOC_MIPS_GOT_HI16
9099 || *r == BFD_RELOC_MIPS_CALL_HI16))));
9100 break;
9101
9102 case 'p':
9103 gas_assert (ep != NULL);
9104
9105 /*
9106 * This allows macro() to pass an immediate expression for
9107 * creating short branches without creating a symbol.
9108 *
9109 * We don't allow branch relaxation for these branches, as
9110 * they should only appear in ".set nomacro" anyway.
9111 */
9112 if (ep->X_op == O_constant)
9113 {
9114 /* For microMIPS we always use relocations for branches.
9115 So we should not resolve immediate values. */
9116 gas_assert (!mips_opts.micromips);
9117
9118 if ((ep->X_add_number & 3) != 0)
9119 as_bad (_("branch to misaligned address (0x%lx)"),
9120 (unsigned long) ep->X_add_number);
9121 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
9122 as_bad (_("branch address range overflow (0x%lx)"),
9123 (unsigned long) ep->X_add_number);
9124 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
9125 ep = NULL;
9126 }
9127 else
9128 *r = BFD_RELOC_16_PCREL_S2;
9129 break;
9130
9131 case 'a':
9132 gas_assert (ep != NULL);
9133 *r = BFD_RELOC_MIPS_JMP;
9134 break;
9135
9136 default:
9137 operand = (mips_opts.micromips
9138 ? decode_micromips_operand (fmt)
9139 : decode_mips_operand (fmt));
9140 if (!operand)
9141 abort ();
9142
9143 uval = va_arg (args, int);
9144 if (operand->type == OP_CLO_CLZ_DEST)
9145 uval |= (uval << 5);
9146 insn_insert_operand (&insn, operand, uval);
9147
9148 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
9149 ++fmt;
9150 break;
9151 }
9152 }
9153 va_end (args);
9154 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
9155
9156 append_insn (&insn, ep, r, TRUE);
9157 }
9158
9159 static void
9160 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
9161 va_list *args)
9162 {
9163 struct mips_opcode *mo;
9164 struct mips_cl_insn insn;
9165 const struct mips_operand *operand;
9166 bfd_reloc_code_real_type r[3]
9167 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
9168
9169 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9170 gas_assert (mo);
9171 gas_assert (strcmp (name, mo->name) == 0);
9172
9173 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
9174 {
9175 ++mo;
9176 gas_assert (mo->name);
9177 gas_assert (strcmp (name, mo->name) == 0);
9178 }
9179
9180 create_insn (&insn, mo);
9181 for (; *fmt; ++fmt)
9182 {
9183 int c;
9184
9185 c = *fmt;
9186 switch (c)
9187 {
9188 case ',':
9189 case '(':
9190 case ')':
9191 break;
9192
9193 case '.':
9194 case 'S':
9195 case 'P':
9196 case 'R':
9197 break;
9198
9199 case '<':
9200 case '5':
9201 case 'F':
9202 case 'H':
9203 case 'W':
9204 case 'D':
9205 case 'j':
9206 case '8':
9207 case 'V':
9208 case 'C':
9209 case 'U':
9210 case 'k':
9211 case 'K':
9212 case 'p':
9213 case 'q':
9214 {
9215 offsetT value;
9216
9217 gas_assert (ep != NULL);
9218
9219 if (ep->X_op != O_constant)
9220 *r = (int) BFD_RELOC_UNUSED + c;
9221 else if (calculate_reloc (*r, ep->X_add_number, &value))
9222 {
9223 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
9224 ep = NULL;
9225 *r = BFD_RELOC_UNUSED;
9226 }
9227 }
9228 break;
9229
9230 default:
9231 operand = decode_mips16_operand (c, FALSE);
9232 if (!operand)
9233 abort ();
9234
9235 insn_insert_operand (&insn, operand, va_arg (*args, int));
9236 break;
9237 }
9238 }
9239
9240 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
9241
9242 append_insn (&insn, ep, r, TRUE);
9243 }
9244
9245 /*
9246 * Generate a "jalr" instruction with a relocation hint to the called
9247 * function. This occurs in NewABI PIC code.
9248 */
9249 static void
9250 macro_build_jalr (expressionS *ep, int cprestore)
9251 {
9252 static const bfd_reloc_code_real_type jalr_relocs[2]
9253 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9254 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9255 const char *jalr;
9256 char *f = NULL;
9257
9258 if (MIPS_JALR_HINT_P (ep))
9259 {
9260 frag_grow (8);
9261 f = frag_more (0);
9262 }
9263 if (mips_opts.micromips)
9264 {
9265 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9266 ? "jalr" : "jalrs");
9267 if (MIPS_JALR_HINT_P (ep)
9268 || mips_opts.insn32
9269 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9270 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9271 else
9272 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9273 }
9274 else
9275 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
9276 if (MIPS_JALR_HINT_P (ep))
9277 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
9278 }
9279
9280 /*
9281 * Generate a "lui" instruction.
9282 */
9283 static void
9284 macro_build_lui (expressionS *ep, int regnum)
9285 {
9286 gas_assert (! mips_opts.mips16);
9287
9288 if (ep->X_op != O_constant)
9289 {
9290 gas_assert (ep->X_op == O_symbol);
9291 /* _gp_disp is a special case, used from s_cpload.
9292 __gnu_local_gp is used if mips_no_shared. */
9293 gas_assert (mips_pic == NO_PIC
9294 || (! HAVE_NEWABI
9295 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9296 || (! mips_in_shared
9297 && strcmp (S_GET_NAME (ep->X_add_symbol),
9298 "__gnu_local_gp") == 0));
9299 }
9300
9301 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
9302 }
9303
9304 /* Generate a sequence of instructions to do a load or store from a constant
9305 offset off of a base register (breg) into/from a target register (treg),
9306 using AT if necessary. */
9307 static void
9308 macro_build_ldst_constoffset (expressionS *ep, const char *op,
9309 int treg, int breg, int dbl)
9310 {
9311 gas_assert (ep->X_op == O_constant);
9312
9313 /* Sign-extending 32-bit constants makes their handling easier. */
9314 if (!dbl)
9315 normalize_constant_expr (ep);
9316
9317 /* Right now, this routine can only handle signed 32-bit constants. */
9318 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
9319 as_warn (_("operand overflow"));
9320
9321 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9322 {
9323 /* Signed 16-bit offset will fit in the op. Easy! */
9324 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
9325 }
9326 else
9327 {
9328 /* 32-bit offset, need multiple instructions and AT, like:
9329 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9330 addu $tempreg,$tempreg,$breg
9331 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9332 to handle the complete offset. */
9333 macro_build_lui (ep, AT);
9334 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9335 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
9336
9337 if (!mips_opts.at)
9338 as_bad (_("macro used $at after \".set noat\""));
9339 }
9340 }
9341
9342 /* set_at()
9343 * Generates code to set the $at register to true (one)
9344 * if reg is less than the immediate expression.
9345 */
9346 static void
9347 set_at (int reg, int unsignedp)
9348 {
9349 if (imm_expr.X_add_number >= -0x8000
9350 && imm_expr.X_add_number < 0x8000)
9351 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9352 AT, reg, BFD_RELOC_LO16);
9353 else
9354 {
9355 load_register (AT, &imm_expr, GPR_SIZE == 64);
9356 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
9357 }
9358 }
9359
9360 /* Count the leading zeroes by performing a binary chop. This is a
9361 bulky bit of source, but performance is a LOT better for the
9362 majority of values than a simple loop to count the bits:
9363 for (lcnt = 0; (lcnt < 32); lcnt++)
9364 if ((v) & (1 << (31 - lcnt)))
9365 break;
9366 However it is not code size friendly, and the gain will drop a bit
9367 on certain cached systems.
9368 */
9369 #define COUNT_TOP_ZEROES(v) \
9370 (((v) & ~0xffff) == 0 \
9371 ? ((v) & ~0xff) == 0 \
9372 ? ((v) & ~0xf) == 0 \
9373 ? ((v) & ~0x3) == 0 \
9374 ? ((v) & ~0x1) == 0 \
9375 ? !(v) \
9376 ? 32 \
9377 : 31 \
9378 : 30 \
9379 : ((v) & ~0x7) == 0 \
9380 ? 29 \
9381 : 28 \
9382 : ((v) & ~0x3f) == 0 \
9383 ? ((v) & ~0x1f) == 0 \
9384 ? 27 \
9385 : 26 \
9386 : ((v) & ~0x7f) == 0 \
9387 ? 25 \
9388 : 24 \
9389 : ((v) & ~0xfff) == 0 \
9390 ? ((v) & ~0x3ff) == 0 \
9391 ? ((v) & ~0x1ff) == 0 \
9392 ? 23 \
9393 : 22 \
9394 : ((v) & ~0x7ff) == 0 \
9395 ? 21 \
9396 : 20 \
9397 : ((v) & ~0x3fff) == 0 \
9398 ? ((v) & ~0x1fff) == 0 \
9399 ? 19 \
9400 : 18 \
9401 : ((v) & ~0x7fff) == 0 \
9402 ? 17 \
9403 : 16 \
9404 : ((v) & ~0xffffff) == 0 \
9405 ? ((v) & ~0xfffff) == 0 \
9406 ? ((v) & ~0x3ffff) == 0 \
9407 ? ((v) & ~0x1ffff) == 0 \
9408 ? 15 \
9409 : 14 \
9410 : ((v) & ~0x7ffff) == 0 \
9411 ? 13 \
9412 : 12 \
9413 : ((v) & ~0x3fffff) == 0 \
9414 ? ((v) & ~0x1fffff) == 0 \
9415 ? 11 \
9416 : 10 \
9417 : ((v) & ~0x7fffff) == 0 \
9418 ? 9 \
9419 : 8 \
9420 : ((v) & ~0xfffffff) == 0 \
9421 ? ((v) & ~0x3ffffff) == 0 \
9422 ? ((v) & ~0x1ffffff) == 0 \
9423 ? 7 \
9424 : 6 \
9425 : ((v) & ~0x7ffffff) == 0 \
9426 ? 5 \
9427 : 4 \
9428 : ((v) & ~0x3fffffff) == 0 \
9429 ? ((v) & ~0x1fffffff) == 0 \
9430 ? 3 \
9431 : 2 \
9432 : ((v) & ~0x7fffffff) == 0 \
9433 ? 1 \
9434 : 0)
9435
9436 /* load_register()
9437 * This routine generates the least number of instructions necessary to load
9438 * an absolute expression value into a register.
9439 */
9440 static void
9441 load_register (int reg, expressionS *ep, int dbl)
9442 {
9443 int freg;
9444 expressionS hi32, lo32;
9445
9446 if (ep->X_op != O_big)
9447 {
9448 gas_assert (ep->X_op == O_constant);
9449
9450 /* Sign-extending 32-bit constants makes their handling easier. */
9451 if (!dbl)
9452 normalize_constant_expr (ep);
9453
9454 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
9455 {
9456 /* We can handle 16 bit signed values with an addiu to
9457 $zero. No need to ever use daddiu here, since $zero and
9458 the result are always correct in 32 bit mode. */
9459 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9460 return;
9461 }
9462 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9463 {
9464 /* We can handle 16 bit unsigned values with an ori to
9465 $zero. */
9466 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9467 return;
9468 }
9469 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
9470 {
9471 /* 32 bit values require an lui. */
9472 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9473 if ((ep->X_add_number & 0xffff) != 0)
9474 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9475 return;
9476 }
9477 }
9478
9479 /* The value is larger than 32 bits. */
9480
9481 if (!dbl || GPR_SIZE == 32)
9482 {
9483 char value[32];
9484
9485 sprintf_vma (value, ep->X_add_number);
9486 as_bad (_("number (0x%s) larger than 32 bits"), value);
9487 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9488 return;
9489 }
9490
9491 if (ep->X_op != O_big)
9492 {
9493 hi32 = *ep;
9494 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9495 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9496 hi32.X_add_number &= 0xffffffff;
9497 lo32 = *ep;
9498 lo32.X_add_number &= 0xffffffff;
9499 }
9500 else
9501 {
9502 gas_assert (ep->X_add_number > 2);
9503 if (ep->X_add_number == 3)
9504 generic_bignum[3] = 0;
9505 else if (ep->X_add_number > 4)
9506 as_bad (_("number larger than 64 bits"));
9507 lo32.X_op = O_constant;
9508 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9509 hi32.X_op = O_constant;
9510 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9511 }
9512
9513 if (hi32.X_add_number == 0)
9514 freg = 0;
9515 else
9516 {
9517 int shift, bit;
9518 unsigned long hi, lo;
9519
9520 if (hi32.X_add_number == (offsetT) 0xffffffff)
9521 {
9522 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9523 {
9524 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9525 return;
9526 }
9527 if (lo32.X_add_number & 0x80000000)
9528 {
9529 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9530 if (lo32.X_add_number & 0xffff)
9531 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9532 return;
9533 }
9534 }
9535
9536 /* Check for 16bit shifted constant. We know that hi32 is
9537 non-zero, so start the mask on the first bit of the hi32
9538 value. */
9539 shift = 17;
9540 do
9541 {
9542 unsigned long himask, lomask;
9543
9544 if (shift < 32)
9545 {
9546 himask = 0xffff >> (32 - shift);
9547 lomask = (0xffff << shift) & 0xffffffff;
9548 }
9549 else
9550 {
9551 himask = 0xffff << (shift - 32);
9552 lomask = 0;
9553 }
9554 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9555 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9556 {
9557 expressionS tmp;
9558
9559 tmp.X_op = O_constant;
9560 if (shift < 32)
9561 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9562 | (lo32.X_add_number >> shift));
9563 else
9564 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
9565 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9566 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9567 reg, reg, (shift >= 32) ? shift - 32 : shift);
9568 return;
9569 }
9570 ++shift;
9571 }
9572 while (shift <= (64 - 16));
9573
9574 /* Find the bit number of the lowest one bit, and store the
9575 shifted value in hi/lo. */
9576 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9577 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9578 if (lo != 0)
9579 {
9580 bit = 0;
9581 while ((lo & 1) == 0)
9582 {
9583 lo >>= 1;
9584 ++bit;
9585 }
9586 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9587 hi >>= bit;
9588 }
9589 else
9590 {
9591 bit = 32;
9592 while ((hi & 1) == 0)
9593 {
9594 hi >>= 1;
9595 ++bit;
9596 }
9597 lo = hi;
9598 hi = 0;
9599 }
9600
9601 /* Optimize if the shifted value is a (power of 2) - 1. */
9602 if ((hi == 0 && ((lo + 1) & lo) == 0)
9603 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
9604 {
9605 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
9606 if (shift != 0)
9607 {
9608 expressionS tmp;
9609
9610 /* This instruction will set the register to be all
9611 ones. */
9612 tmp.X_op = O_constant;
9613 tmp.X_add_number = (offsetT) -1;
9614 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9615 if (bit != 0)
9616 {
9617 bit += shift;
9618 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9619 reg, reg, (bit >= 32) ? bit - 32 : bit);
9620 }
9621 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
9622 reg, reg, (shift >= 32) ? shift - 32 : shift);
9623 return;
9624 }
9625 }
9626
9627 /* Sign extend hi32 before calling load_register, because we can
9628 generally get better code when we load a sign extended value. */
9629 if ((hi32.X_add_number & 0x80000000) != 0)
9630 hi32.X_add_number |= ~(offsetT) 0xffffffff;
9631 load_register (reg, &hi32, 0);
9632 freg = reg;
9633 }
9634 if ((lo32.X_add_number & 0xffff0000) == 0)
9635 {
9636 if (freg != 0)
9637 {
9638 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
9639 freg = reg;
9640 }
9641 }
9642 else
9643 {
9644 expressionS mid16;
9645
9646 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
9647 {
9648 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9649 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
9650 return;
9651 }
9652
9653 if (freg != 0)
9654 {
9655 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
9656 freg = reg;
9657 }
9658 mid16 = lo32;
9659 mid16.X_add_number >>= 16;
9660 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9661 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9662 freg = reg;
9663 }
9664 if ((lo32.X_add_number & 0xffff) != 0)
9665 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9666 }
9667
9668 static inline void
9669 load_delay_nop (void)
9670 {
9671 if (!gpr_interlocks)
9672 macro_build (NULL, "nop", "");
9673 }
9674
9675 /* Load an address into a register. */
9676
9677 static void
9678 load_address (int reg, expressionS *ep, int *used_at)
9679 {
9680 if (ep->X_op != O_constant
9681 && ep->X_op != O_symbol)
9682 {
9683 as_bad (_("expression too complex"));
9684 ep->X_op = O_constant;
9685 }
9686
9687 if (ep->X_op == O_constant)
9688 {
9689 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
9690 return;
9691 }
9692
9693 if (mips_pic == NO_PIC)
9694 {
9695 /* If this is a reference to a GP relative symbol, we want
9696 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
9697 Otherwise we want
9698 lui $reg,<sym> (BFD_RELOC_HI16_S)
9699 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9700 If we have an addend, we always use the latter form.
9701
9702 With 64bit address space and a usable $at we want
9703 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9704 lui $at,<sym> (BFD_RELOC_HI16_S)
9705 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9706 daddiu $at,<sym> (BFD_RELOC_LO16)
9707 dsll32 $reg,0
9708 daddu $reg,$reg,$at
9709
9710 If $at is already in use, we use a path which is suboptimal
9711 on superscalar processors.
9712 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9713 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9714 dsll $reg,16
9715 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9716 dsll $reg,16
9717 daddiu $reg,<sym> (BFD_RELOC_LO16)
9718
9719 For GP relative symbols in 64bit address space we can use
9720 the same sequence as in 32bit address space. */
9721 if (HAVE_64BIT_SYMBOLS)
9722 {
9723 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9724 && !nopic_need_relax (ep->X_add_symbol, 1))
9725 {
9726 relax_start (ep->X_add_symbol);
9727 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9728 mips_gp_register, BFD_RELOC_GPREL16);
9729 relax_switch ();
9730 }
9731
9732 if (*used_at == 0 && mips_opts.at)
9733 {
9734 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9735 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
9736 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9737 BFD_RELOC_MIPS_HIGHER);
9738 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
9739 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
9740 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
9741 *used_at = 1;
9742 }
9743 else
9744 {
9745 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9746 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9747 BFD_RELOC_MIPS_HIGHER);
9748 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9749 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
9750 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9751 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
9752 }
9753
9754 if (mips_relax.sequence)
9755 relax_end ();
9756 }
9757 else
9758 {
9759 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9760 && !nopic_need_relax (ep->X_add_symbol, 1))
9761 {
9762 relax_start (ep->X_add_symbol);
9763 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9764 mips_gp_register, BFD_RELOC_GPREL16);
9765 relax_switch ();
9766 }
9767 macro_build_lui (ep, reg);
9768 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9769 reg, reg, BFD_RELOC_LO16);
9770 if (mips_relax.sequence)
9771 relax_end ();
9772 }
9773 }
9774 else if (!mips_big_got)
9775 {
9776 expressionS ex;
9777
9778 /* If this is a reference to an external symbol, we want
9779 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9780 Otherwise we want
9781 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9782 nop
9783 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9784 If there is a constant, it must be added in after.
9785
9786 If we have NewABI, we want
9787 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9788 unless we're referencing a global symbol with a non-zero
9789 offset, in which case cst must be added separately. */
9790 if (HAVE_NEWABI)
9791 {
9792 if (ep->X_add_number)
9793 {
9794 ex.X_add_number = ep->X_add_number;
9795 ep->X_add_number = 0;
9796 relax_start (ep->X_add_symbol);
9797 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9798 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9799 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9800 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9801 ex.X_op = O_constant;
9802 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9803 reg, reg, BFD_RELOC_LO16);
9804 ep->X_add_number = ex.X_add_number;
9805 relax_switch ();
9806 }
9807 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9808 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9809 if (mips_relax.sequence)
9810 relax_end ();
9811 }
9812 else
9813 {
9814 ex.X_add_number = ep->X_add_number;
9815 ep->X_add_number = 0;
9816 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9817 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9818 load_delay_nop ();
9819 relax_start (ep->X_add_symbol);
9820 relax_switch ();
9821 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9822 BFD_RELOC_LO16);
9823 relax_end ();
9824
9825 if (ex.X_add_number != 0)
9826 {
9827 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9828 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9829 ex.X_op = O_constant;
9830 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9831 reg, reg, BFD_RELOC_LO16);
9832 }
9833 }
9834 }
9835 else if (mips_big_got)
9836 {
9837 expressionS ex;
9838
9839 /* This is the large GOT case. If this is a reference to an
9840 external symbol, we want
9841 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9842 addu $reg,$reg,$gp
9843 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
9844
9845 Otherwise, for a reference to a local symbol in old ABI, we want
9846 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9847 nop
9848 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9849 If there is a constant, it must be added in after.
9850
9851 In the NewABI, for local symbols, with or without offsets, we want:
9852 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9853 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
9854 */
9855 if (HAVE_NEWABI)
9856 {
9857 ex.X_add_number = ep->X_add_number;
9858 ep->X_add_number = 0;
9859 relax_start (ep->X_add_symbol);
9860 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9861 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9862 reg, reg, mips_gp_register);
9863 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9864 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9865 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9866 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9867 else if (ex.X_add_number)
9868 {
9869 ex.X_op = O_constant;
9870 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9871 BFD_RELOC_LO16);
9872 }
9873
9874 ep->X_add_number = ex.X_add_number;
9875 relax_switch ();
9876 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9877 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9878 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9879 BFD_RELOC_MIPS_GOT_OFST);
9880 relax_end ();
9881 }
9882 else
9883 {
9884 ex.X_add_number = ep->X_add_number;
9885 ep->X_add_number = 0;
9886 relax_start (ep->X_add_symbol);
9887 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9888 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9889 reg, reg, mips_gp_register);
9890 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9891 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9892 relax_switch ();
9893 if (reg_needs_delay (mips_gp_register))
9894 {
9895 /* We need a nop before loading from $gp. This special
9896 check is required because the lui which starts the main
9897 instruction stream does not refer to $gp, and so will not
9898 insert the nop which may be required. */
9899 macro_build (NULL, "nop", "");
9900 }
9901 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9902 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9903 load_delay_nop ();
9904 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9905 BFD_RELOC_LO16);
9906 relax_end ();
9907
9908 if (ex.X_add_number != 0)
9909 {
9910 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9911 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9912 ex.X_op = O_constant;
9913 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9914 BFD_RELOC_LO16);
9915 }
9916 }
9917 }
9918 else
9919 abort ();
9920
9921 if (!mips_opts.at && *used_at == 1)
9922 as_bad (_("macro used $at after \".set noat\""));
9923 }
9924
9925 /* Move the contents of register SOURCE into register DEST. */
9926
9927 static void
9928 move_register (int dest, int source)
9929 {
9930 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9931 instruction specifically requires a 32-bit one. */
9932 if (mips_opts.micromips
9933 && !mips_opts.insn32
9934 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9935 macro_build (NULL, "move", "mp,mj", dest, source);
9936 else
9937 macro_build (NULL, "or", "d,v,t", dest, source, 0);
9938 }
9939
9940 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
9941 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9942 The two alternatives are:
9943
9944 Global symbol Local symbol
9945 ------------- ------------
9946 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9947 ... ...
9948 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9949
9950 load_got_offset emits the first instruction and add_got_offset
9951 emits the second for a 16-bit offset or add_got_offset_hilo emits
9952 a sequence to add a 32-bit offset using a scratch register. */
9953
9954 static void
9955 load_got_offset (int dest, expressionS *local)
9956 {
9957 expressionS global;
9958
9959 global = *local;
9960 global.X_add_number = 0;
9961
9962 relax_start (local->X_add_symbol);
9963 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9964 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9965 relax_switch ();
9966 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9967 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9968 relax_end ();
9969 }
9970
9971 static void
9972 add_got_offset (int dest, expressionS *local)
9973 {
9974 expressionS global;
9975
9976 global.X_op = O_constant;
9977 global.X_op_symbol = NULL;
9978 global.X_add_symbol = NULL;
9979 global.X_add_number = local->X_add_number;
9980
9981 relax_start (local->X_add_symbol);
9982 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
9983 dest, dest, BFD_RELOC_LO16);
9984 relax_switch ();
9985 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
9986 relax_end ();
9987 }
9988
9989 static void
9990 add_got_offset_hilo (int dest, expressionS *local, int tmp)
9991 {
9992 expressionS global;
9993 int hold_mips_optimize;
9994
9995 global.X_op = O_constant;
9996 global.X_op_symbol = NULL;
9997 global.X_add_symbol = NULL;
9998 global.X_add_number = local->X_add_number;
9999
10000 relax_start (local->X_add_symbol);
10001 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
10002 relax_switch ();
10003 /* Set mips_optimize around the lui instruction to avoid
10004 inserting an unnecessary nop after the lw. */
10005 hold_mips_optimize = mips_optimize;
10006 mips_optimize = 2;
10007 macro_build_lui (&global, tmp);
10008 mips_optimize = hold_mips_optimize;
10009 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
10010 relax_end ();
10011
10012 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
10013 }
10014
10015 /* Emit a sequence of instructions to emulate a branch likely operation.
10016 BR is an ordinary branch corresponding to one to be emulated. BRNEG
10017 is its complementing branch with the original condition negated.
10018 CALL is set if the original branch specified the link operation.
10019 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
10020
10021 Code like this is produced in the noreorder mode:
10022
10023 BRNEG <args>, 1f
10024 nop
10025 b <sym>
10026 delay slot (executed only if branch taken)
10027 1:
10028
10029 or, if CALL is set:
10030
10031 BRNEG <args>, 1f
10032 nop
10033 bal <sym>
10034 delay slot (executed only if branch taken)
10035 1:
10036
10037 In the reorder mode the delay slot would be filled with a nop anyway,
10038 so code produced is simply:
10039
10040 BR <args>, <sym>
10041 nop
10042
10043 This function is used when producing code for the microMIPS ASE that
10044 does not implement branch likely instructions in hardware. */
10045
10046 static void
10047 macro_build_branch_likely (const char *br, const char *brneg,
10048 int call, expressionS *ep, const char *fmt,
10049 unsigned int sreg, unsigned int treg)
10050 {
10051 int noreorder = mips_opts.noreorder;
10052 expressionS expr1;
10053
10054 gas_assert (mips_opts.micromips);
10055 start_noreorder ();
10056 if (noreorder)
10057 {
10058 micromips_label_expr (&expr1);
10059 macro_build (&expr1, brneg, fmt, sreg, treg);
10060 macro_build (NULL, "nop", "");
10061 macro_build (ep, call ? "bal" : "b", "p");
10062
10063 /* Set to true so that append_insn adds a label. */
10064 emit_branch_likely_macro = TRUE;
10065 }
10066 else
10067 {
10068 macro_build (ep, br, fmt, sreg, treg);
10069 macro_build (NULL, "nop", "");
10070 }
10071 end_noreorder ();
10072 }
10073
10074 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
10075 the condition code tested. EP specifies the branch target. */
10076
10077 static void
10078 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
10079 {
10080 const int call = 0;
10081 const char *brneg;
10082 const char *br;
10083
10084 switch (type)
10085 {
10086 case M_BC1FL:
10087 br = "bc1f";
10088 brneg = "bc1t";
10089 break;
10090 case M_BC1TL:
10091 br = "bc1t";
10092 brneg = "bc1f";
10093 break;
10094 case M_BC2FL:
10095 br = "bc2f";
10096 brneg = "bc2t";
10097 break;
10098 case M_BC2TL:
10099 br = "bc2t";
10100 brneg = "bc2f";
10101 break;
10102 default:
10103 abort ();
10104 }
10105 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
10106 }
10107
10108 /* Emit a two-argument branch macro specified by TYPE, using SREG as
10109 the register tested. EP specifies the branch target. */
10110
10111 static void
10112 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
10113 {
10114 const char *brneg = NULL;
10115 const char *br;
10116 int call = 0;
10117
10118 switch (type)
10119 {
10120 case M_BGEZ:
10121 br = "bgez";
10122 break;
10123 case M_BGEZL:
10124 br = mips_opts.micromips ? "bgez" : "bgezl";
10125 brneg = "bltz";
10126 break;
10127 case M_BGEZALL:
10128 gas_assert (mips_opts.micromips);
10129 br = mips_opts.insn32 ? "bgezal" : "bgezals";
10130 brneg = "bltz";
10131 call = 1;
10132 break;
10133 case M_BGTZ:
10134 br = "bgtz";
10135 break;
10136 case M_BGTZL:
10137 br = mips_opts.micromips ? "bgtz" : "bgtzl";
10138 brneg = "blez";
10139 break;
10140 case M_BLEZ:
10141 br = "blez";
10142 break;
10143 case M_BLEZL:
10144 br = mips_opts.micromips ? "blez" : "blezl";
10145 brneg = "bgtz";
10146 break;
10147 case M_BLTZ:
10148 br = "bltz";
10149 break;
10150 case M_BLTZL:
10151 br = mips_opts.micromips ? "bltz" : "bltzl";
10152 brneg = "bgez";
10153 break;
10154 case M_BLTZALL:
10155 gas_assert (mips_opts.micromips);
10156 br = mips_opts.insn32 ? "bltzal" : "bltzals";
10157 brneg = "bgez";
10158 call = 1;
10159 break;
10160 default:
10161 abort ();
10162 }
10163 if (mips_opts.micromips && brneg)
10164 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
10165 else
10166 macro_build (ep, br, "s,p", sreg);
10167 }
10168
10169 /* Emit a three-argument branch macro specified by TYPE, using SREG and
10170 TREG as the registers tested. EP specifies the branch target. */
10171
10172 static void
10173 macro_build_branch_rsrt (int type, expressionS *ep,
10174 unsigned int sreg, unsigned int treg)
10175 {
10176 const char *brneg = NULL;
10177 const int call = 0;
10178 const char *br;
10179
10180 switch (type)
10181 {
10182 case M_BEQ:
10183 case M_BEQ_I:
10184 br = "beq";
10185 break;
10186 case M_BEQL:
10187 case M_BEQL_I:
10188 br = mips_opts.micromips ? "beq" : "beql";
10189 brneg = "bne";
10190 break;
10191 case M_BNE:
10192 case M_BNE_I:
10193 br = "bne";
10194 break;
10195 case M_BNEL:
10196 case M_BNEL_I:
10197 br = mips_opts.micromips ? "bne" : "bnel";
10198 brneg = "beq";
10199 break;
10200 default:
10201 abort ();
10202 }
10203 if (mips_opts.micromips && brneg)
10204 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
10205 else
10206 macro_build (ep, br, "s,t,p", sreg, treg);
10207 }
10208
10209 /* Return the high part that should be loaded in order to make the low
10210 part of VALUE accessible using an offset of OFFBITS bits. */
10211
10212 static offsetT
10213 offset_high_part (offsetT value, unsigned int offbits)
10214 {
10215 offsetT bias;
10216 addressT low_mask;
10217
10218 if (offbits == 0)
10219 return value;
10220 bias = 1 << (offbits - 1);
10221 low_mask = bias * 2 - 1;
10222 return (value + bias) & ~low_mask;
10223 }
10224
10225 /* Return true if the value stored in offset_expr and offset_reloc
10226 fits into a signed offset of OFFBITS bits. RANGE is the maximum
10227 amount that the caller wants to add without inducing overflow
10228 and ALIGN is the known alignment of the value in bytes. */
10229
10230 static bfd_boolean
10231 small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10232 {
10233 if (offbits == 16)
10234 {
10235 /* Accept any relocation operator if overflow isn't a concern. */
10236 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
10237 return TRUE;
10238
10239 /* These relocations are guaranteed not to overflow in correct links. */
10240 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10241 || gprel16_reloc_p (*offset_reloc))
10242 return TRUE;
10243 }
10244 if (offset_expr.X_op == O_constant
10245 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10246 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10247 return TRUE;
10248 return FALSE;
10249 }
10250
10251 /*
10252 * Build macros
10253 * This routine implements the seemingly endless macro or synthesized
10254 * instructions and addressing modes in the mips assembly language. Many
10255 * of these macros are simple and are similar to each other. These could
10256 * probably be handled by some kind of table or grammar approach instead of
10257 * this verbose method. Others are not simple macros but are more like
10258 * optimizing code generation.
10259 * One interesting optimization is when several store macros appear
10260 * consecutively that would load AT with the upper half of the same address.
10261 * The ensuing load upper instructions are omitted. This implies some kind
10262 * of global optimization. We currently only optimize within a single macro.
10263 * For many of the load and store macros if the address is specified as a
10264 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10265 * first load register 'at' with zero and use it as the base register. The
10266 * mips assembler simply uses register $zero. Just one tiny optimization
10267 * we're missing.
10268 */
10269 static void
10270 macro (struct mips_cl_insn *ip, char *str)
10271 {
10272 const struct mips_operand_array *operands;
10273 unsigned int breg, i;
10274 unsigned int tempreg;
10275 int mask;
10276 int used_at = 0;
10277 expressionS label_expr;
10278 expressionS expr1;
10279 expressionS *ep;
10280 const char *s;
10281 const char *s2;
10282 const char *fmt;
10283 int likely = 0;
10284 int coproc = 0;
10285 int offbits = 16;
10286 int call = 0;
10287 int jals = 0;
10288 int dbl = 0;
10289 int imm = 0;
10290 int ust = 0;
10291 int lp = 0;
10292 int ll_sc_paired = 0;
10293 bfd_boolean large_offset;
10294 int off;
10295 int hold_mips_optimize;
10296 unsigned int align;
10297 unsigned int op[MAX_OPERANDS];
10298
10299 gas_assert (! mips_opts.mips16);
10300
10301 operands = insn_operands (ip);
10302 for (i = 0; i < MAX_OPERANDS; i++)
10303 if (operands->operand[i])
10304 op[i] = insn_extract_operand (ip, operands->operand[i]);
10305 else
10306 op[i] = -1;
10307
10308 mask = ip->insn_mo->mask;
10309
10310 label_expr.X_op = O_constant;
10311 label_expr.X_op_symbol = NULL;
10312 label_expr.X_add_symbol = NULL;
10313 label_expr.X_add_number = 0;
10314
10315 expr1.X_op = O_constant;
10316 expr1.X_op_symbol = NULL;
10317 expr1.X_add_symbol = NULL;
10318 expr1.X_add_number = 1;
10319 align = 1;
10320
10321 switch (mask)
10322 {
10323 case M_DABS:
10324 dbl = 1;
10325 /* Fall through. */
10326 case M_ABS:
10327 /* bgez $a0,1f
10328 move v0,$a0
10329 sub v0,$zero,$a0
10330 1:
10331 */
10332
10333 start_noreorder ();
10334
10335 if (mips_opts.micromips)
10336 micromips_label_expr (&label_expr);
10337 else
10338 label_expr.X_add_number = 8;
10339 macro_build (&label_expr, "bgez", "s,p", op[1]);
10340 if (op[0] == op[1])
10341 macro_build (NULL, "nop", "");
10342 else
10343 move_register (op[0], op[1]);
10344 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
10345 if (mips_opts.micromips)
10346 micromips_add_label ();
10347
10348 end_noreorder ();
10349 break;
10350
10351 case M_ADD_I:
10352 s = "addi";
10353 s2 = "add";
10354 if (ISA_IS_R6 (mips_opts.isa))
10355 goto do_addi_i;
10356 else
10357 goto do_addi;
10358 case M_ADDU_I:
10359 s = "addiu";
10360 s2 = "addu";
10361 goto do_addi;
10362 case M_DADD_I:
10363 dbl = 1;
10364 s = "daddi";
10365 s2 = "dadd";
10366 if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
10367 goto do_addi;
10368 if (imm_expr.X_add_number >= -0x200
10369 && imm_expr.X_add_number < 0x200
10370 && !ISA_IS_R6 (mips_opts.isa))
10371 {
10372 macro_build (NULL, s, "t,r,.", op[0], op[1],
10373 (int) imm_expr.X_add_number);
10374 break;
10375 }
10376 goto do_addi_i;
10377 case M_DADDU_I:
10378 dbl = 1;
10379 s = "daddiu";
10380 s2 = "daddu";
10381 do_addi:
10382 if (imm_expr.X_add_number >= -0x8000
10383 && imm_expr.X_add_number < 0x8000)
10384 {
10385 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
10386 break;
10387 }
10388 do_addi_i:
10389 used_at = 1;
10390 load_register (AT, &imm_expr, dbl);
10391 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10392 break;
10393
10394 case M_AND_I:
10395 s = "andi";
10396 s2 = "and";
10397 goto do_bit;
10398 case M_OR_I:
10399 s = "ori";
10400 s2 = "or";
10401 goto do_bit;
10402 case M_NOR_I:
10403 s = "";
10404 s2 = "nor";
10405 goto do_bit;
10406 case M_XOR_I:
10407 s = "xori";
10408 s2 = "xor";
10409 do_bit:
10410 if (imm_expr.X_add_number >= 0
10411 && imm_expr.X_add_number < 0x10000)
10412 {
10413 if (mask != M_NOR_I)
10414 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
10415 else
10416 {
10417 macro_build (&imm_expr, "ori", "t,r,i",
10418 op[0], op[1], BFD_RELOC_LO16);
10419 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
10420 }
10421 break;
10422 }
10423
10424 used_at = 1;
10425 load_register (AT, &imm_expr, GPR_SIZE == 64);
10426 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10427 break;
10428
10429 case M_BALIGN:
10430 switch (imm_expr.X_add_number)
10431 {
10432 case 0:
10433 macro_build (NULL, "nop", "");
10434 break;
10435 case 2:
10436 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
10437 break;
10438 case 1:
10439 case 3:
10440 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
10441 (int) imm_expr.X_add_number);
10442 break;
10443 default:
10444 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10445 (unsigned long) imm_expr.X_add_number);
10446 break;
10447 }
10448 break;
10449
10450 case M_BC1FL:
10451 case M_BC1TL:
10452 case M_BC2FL:
10453 case M_BC2TL:
10454 gas_assert (mips_opts.micromips);
10455 macro_build_branch_ccl (mask, &offset_expr,
10456 EXTRACT_OPERAND (1, BCC, *ip));
10457 break;
10458
10459 case M_BEQ_I:
10460 case M_BEQL_I:
10461 case M_BNE_I:
10462 case M_BNEL_I:
10463 if (imm_expr.X_add_number == 0)
10464 op[1] = 0;
10465 else
10466 {
10467 op[1] = AT;
10468 used_at = 1;
10469 load_register (op[1], &imm_expr, GPR_SIZE == 64);
10470 }
10471 /* Fall through. */
10472 case M_BEQL:
10473 case M_BNEL:
10474 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
10475 break;
10476
10477 case M_BGEL:
10478 likely = 1;
10479 /* Fall through. */
10480 case M_BGE:
10481 if (op[1] == 0)
10482 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10483 else if (op[0] == 0)
10484 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
10485 else
10486 {
10487 used_at = 1;
10488 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10489 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10490 &offset_expr, AT, ZERO);
10491 }
10492 break;
10493
10494 case M_BGEZL:
10495 case M_BGEZALL:
10496 case M_BGTZL:
10497 case M_BLEZL:
10498 case M_BLTZL:
10499 case M_BLTZALL:
10500 macro_build_branch_rs (mask, &offset_expr, op[0]);
10501 break;
10502
10503 case M_BGTL_I:
10504 likely = 1;
10505 /* Fall through. */
10506 case M_BGT_I:
10507 /* Check for > max integer. */
10508 if (imm_expr.X_add_number >= GPR_SMAX)
10509 {
10510 do_false:
10511 /* Result is always false. */
10512 if (! likely)
10513 macro_build (NULL, "nop", "");
10514 else
10515 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
10516 break;
10517 }
10518 ++imm_expr.X_add_number;
10519 /* Fall through. */
10520 case M_BGE_I:
10521 case M_BGEL_I:
10522 if (mask == M_BGEL_I)
10523 likely = 1;
10524 if (imm_expr.X_add_number == 0)
10525 {
10526 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
10527 &offset_expr, op[0]);
10528 break;
10529 }
10530 if (imm_expr.X_add_number == 1)
10531 {
10532 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
10533 &offset_expr, op[0]);
10534 break;
10535 }
10536 if (imm_expr.X_add_number <= GPR_SMIN)
10537 {
10538 do_true:
10539 /* Result is always true. */
10540 as_warn (_("branch %s is always true"), ip->insn_mo->name);
10541 macro_build (&offset_expr, "b", "p");
10542 break;
10543 }
10544 used_at = 1;
10545 set_at (op[0], 0);
10546 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10547 &offset_expr, AT, ZERO);
10548 break;
10549
10550 case M_BGEUL:
10551 likely = 1;
10552 /* Fall through. */
10553 case M_BGEU:
10554 if (op[1] == 0)
10555 goto do_true;
10556 else if (op[0] == 0)
10557 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10558 &offset_expr, ZERO, op[1]);
10559 else
10560 {
10561 used_at = 1;
10562 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10563 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10564 &offset_expr, AT, ZERO);
10565 }
10566 break;
10567
10568 case M_BGTUL_I:
10569 likely = 1;
10570 /* Fall through. */
10571 case M_BGTU_I:
10572 if (op[0] == 0
10573 || (GPR_SIZE == 32
10574 && imm_expr.X_add_number == -1))
10575 goto do_false;
10576 ++imm_expr.X_add_number;
10577 /* Fall through. */
10578 case M_BGEU_I:
10579 case M_BGEUL_I:
10580 if (mask == M_BGEUL_I)
10581 likely = 1;
10582 if (imm_expr.X_add_number == 0)
10583 goto do_true;
10584 else if (imm_expr.X_add_number == 1)
10585 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10586 &offset_expr, op[0], ZERO);
10587 else
10588 {
10589 used_at = 1;
10590 set_at (op[0], 1);
10591 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10592 &offset_expr, AT, ZERO);
10593 }
10594 break;
10595
10596 case M_BGTL:
10597 likely = 1;
10598 /* Fall through. */
10599 case M_BGT:
10600 if (op[1] == 0)
10601 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10602 else if (op[0] == 0)
10603 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
10604 else
10605 {
10606 used_at = 1;
10607 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10608 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10609 &offset_expr, AT, ZERO);
10610 }
10611 break;
10612
10613 case M_BGTUL:
10614 likely = 1;
10615 /* Fall through. */
10616 case M_BGTU:
10617 if (op[1] == 0)
10618 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10619 &offset_expr, op[0], ZERO);
10620 else if (op[0] == 0)
10621 goto do_false;
10622 else
10623 {
10624 used_at = 1;
10625 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10626 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10627 &offset_expr, AT, ZERO);
10628 }
10629 break;
10630
10631 case M_BLEL:
10632 likely = 1;
10633 /* Fall through. */
10634 case M_BLE:
10635 if (op[1] == 0)
10636 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10637 else if (op[0] == 0)
10638 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
10639 else
10640 {
10641 used_at = 1;
10642 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10643 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10644 &offset_expr, AT, ZERO);
10645 }
10646 break;
10647
10648 case M_BLEL_I:
10649 likely = 1;
10650 /* Fall through. */
10651 case M_BLE_I:
10652 if (imm_expr.X_add_number >= GPR_SMAX)
10653 goto do_true;
10654 ++imm_expr.X_add_number;
10655 /* Fall through. */
10656 case M_BLT_I:
10657 case M_BLTL_I:
10658 if (mask == M_BLTL_I)
10659 likely = 1;
10660 if (imm_expr.X_add_number == 0)
10661 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10662 else if (imm_expr.X_add_number == 1)
10663 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10664 else
10665 {
10666 used_at = 1;
10667 set_at (op[0], 0);
10668 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10669 &offset_expr, AT, ZERO);
10670 }
10671 break;
10672
10673 case M_BLEUL:
10674 likely = 1;
10675 /* Fall through. */
10676 case M_BLEU:
10677 if (op[1] == 0)
10678 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10679 &offset_expr, op[0], ZERO);
10680 else if (op[0] == 0)
10681 goto do_true;
10682 else
10683 {
10684 used_at = 1;
10685 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10686 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10687 &offset_expr, AT, ZERO);
10688 }
10689 break;
10690
10691 case M_BLEUL_I:
10692 likely = 1;
10693 /* Fall through. */
10694 case M_BLEU_I:
10695 if (op[0] == 0
10696 || (GPR_SIZE == 32
10697 && imm_expr.X_add_number == -1))
10698 goto do_true;
10699 ++imm_expr.X_add_number;
10700 /* Fall through. */
10701 case M_BLTU_I:
10702 case M_BLTUL_I:
10703 if (mask == M_BLTUL_I)
10704 likely = 1;
10705 if (imm_expr.X_add_number == 0)
10706 goto do_false;
10707 else if (imm_expr.X_add_number == 1)
10708 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10709 &offset_expr, op[0], ZERO);
10710 else
10711 {
10712 used_at = 1;
10713 set_at (op[0], 1);
10714 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10715 &offset_expr, AT, ZERO);
10716 }
10717 break;
10718
10719 case M_BLTL:
10720 likely = 1;
10721 /* Fall through. */
10722 case M_BLT:
10723 if (op[1] == 0)
10724 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10725 else if (op[0] == 0)
10726 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
10727 else
10728 {
10729 used_at = 1;
10730 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10731 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10732 &offset_expr, AT, ZERO);
10733 }
10734 break;
10735
10736 case M_BLTUL:
10737 likely = 1;
10738 /* Fall through. */
10739 case M_BLTU:
10740 if (op[1] == 0)
10741 goto do_false;
10742 else if (op[0] == 0)
10743 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10744 &offset_expr, ZERO, op[1]);
10745 else
10746 {
10747 used_at = 1;
10748 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10749 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10750 &offset_expr, AT, ZERO);
10751 }
10752 break;
10753
10754 case M_DDIV_3:
10755 dbl = 1;
10756 /* Fall through. */
10757 case M_DIV_3:
10758 s = "mflo";
10759 goto do_div3;
10760 case M_DREM_3:
10761 dbl = 1;
10762 /* Fall through. */
10763 case M_REM_3:
10764 s = "mfhi";
10765 do_div3:
10766 if (op[2] == 0)
10767 {
10768 as_warn (_("divide by zero"));
10769 if (mips_trap)
10770 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10771 else
10772 macro_build (NULL, "break", BRK_FMT, 7);
10773 break;
10774 }
10775
10776 start_noreorder ();
10777 if (mips_trap)
10778 {
10779 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10780 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10781 }
10782 else
10783 {
10784 if (mips_opts.micromips)
10785 micromips_label_expr (&label_expr);
10786 else
10787 label_expr.X_add_number = 8;
10788 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10789 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10790 macro_build (NULL, "break", BRK_FMT, 7);
10791 if (mips_opts.micromips)
10792 micromips_add_label ();
10793 }
10794 expr1.X_add_number = -1;
10795 used_at = 1;
10796 load_register (AT, &expr1, dbl);
10797 if (mips_opts.micromips)
10798 micromips_label_expr (&label_expr);
10799 else
10800 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
10801 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
10802 if (dbl)
10803 {
10804 expr1.X_add_number = 1;
10805 load_register (AT, &expr1, dbl);
10806 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
10807 }
10808 else
10809 {
10810 expr1.X_add_number = 0x80000000;
10811 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
10812 }
10813 if (mips_trap)
10814 {
10815 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
10816 /* We want to close the noreorder block as soon as possible, so
10817 that later insns are available for delay slot filling. */
10818 end_noreorder ();
10819 }
10820 else
10821 {
10822 if (mips_opts.micromips)
10823 micromips_label_expr (&label_expr);
10824 else
10825 label_expr.X_add_number = 8;
10826 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
10827 macro_build (NULL, "nop", "");
10828
10829 /* We want to close the noreorder block as soon as possible, so
10830 that later insns are available for delay slot filling. */
10831 end_noreorder ();
10832
10833 macro_build (NULL, "break", BRK_FMT, 6);
10834 }
10835 if (mips_opts.micromips)
10836 micromips_add_label ();
10837 macro_build (NULL, s, MFHL_FMT, op[0]);
10838 break;
10839
10840 case M_DIV_3I:
10841 s = "div";
10842 s2 = "mflo";
10843 goto do_divi;
10844 case M_DIVU_3I:
10845 s = "divu";
10846 s2 = "mflo";
10847 goto do_divi;
10848 case M_REM_3I:
10849 s = "div";
10850 s2 = "mfhi";
10851 goto do_divi;
10852 case M_REMU_3I:
10853 s = "divu";
10854 s2 = "mfhi";
10855 goto do_divi;
10856 case M_DDIV_3I:
10857 dbl = 1;
10858 s = "ddiv";
10859 s2 = "mflo";
10860 goto do_divi;
10861 case M_DDIVU_3I:
10862 dbl = 1;
10863 s = "ddivu";
10864 s2 = "mflo";
10865 goto do_divi;
10866 case M_DREM_3I:
10867 dbl = 1;
10868 s = "ddiv";
10869 s2 = "mfhi";
10870 goto do_divi;
10871 case M_DREMU_3I:
10872 dbl = 1;
10873 s = "ddivu";
10874 s2 = "mfhi";
10875 do_divi:
10876 if (imm_expr.X_add_number == 0)
10877 {
10878 as_warn (_("divide by zero"));
10879 if (mips_trap)
10880 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10881 else
10882 macro_build (NULL, "break", BRK_FMT, 7);
10883 break;
10884 }
10885 if (imm_expr.X_add_number == 1)
10886 {
10887 if (strcmp (s2, "mflo") == 0)
10888 move_register (op[0], op[1]);
10889 else
10890 move_register (op[0], ZERO);
10891 break;
10892 }
10893 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
10894 {
10895 if (strcmp (s2, "mflo") == 0)
10896 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
10897 else
10898 move_register (op[0], ZERO);
10899 break;
10900 }
10901
10902 used_at = 1;
10903 load_register (AT, &imm_expr, dbl);
10904 macro_build (NULL, s, "z,s,t", op[1], AT);
10905 macro_build (NULL, s2, MFHL_FMT, op[0]);
10906 break;
10907
10908 case M_DIVU_3:
10909 s = "divu";
10910 s2 = "mflo";
10911 goto do_divu3;
10912 case M_REMU_3:
10913 s = "divu";
10914 s2 = "mfhi";
10915 goto do_divu3;
10916 case M_DDIVU_3:
10917 s = "ddivu";
10918 s2 = "mflo";
10919 goto do_divu3;
10920 case M_DREMU_3:
10921 s = "ddivu";
10922 s2 = "mfhi";
10923 do_divu3:
10924 start_noreorder ();
10925 if (mips_trap)
10926 {
10927 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10928 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10929 /* We want to close the noreorder block as soon as possible, so
10930 that later insns are available for delay slot filling. */
10931 end_noreorder ();
10932 }
10933 else
10934 {
10935 if (mips_opts.micromips)
10936 micromips_label_expr (&label_expr);
10937 else
10938 label_expr.X_add_number = 8;
10939 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10940 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10941
10942 /* We want to close the noreorder block as soon as possible, so
10943 that later insns are available for delay slot filling. */
10944 end_noreorder ();
10945 macro_build (NULL, "break", BRK_FMT, 7);
10946 if (mips_opts.micromips)
10947 micromips_add_label ();
10948 }
10949 macro_build (NULL, s2, MFHL_FMT, op[0]);
10950 break;
10951
10952 case M_DLCA_AB:
10953 dbl = 1;
10954 /* Fall through. */
10955 case M_LCA_AB:
10956 call = 1;
10957 goto do_la;
10958 case M_DLA_AB:
10959 dbl = 1;
10960 /* Fall through. */
10961 case M_LA_AB:
10962 do_la:
10963 /* Load the address of a symbol into a register. If breg is not
10964 zero, we then add a base register to it. */
10965
10966 breg = op[2];
10967 if (dbl && GPR_SIZE == 32)
10968 as_warn (_("dla used to load 32-bit register; recommend using la "
10969 "instead"));
10970
10971 if (!dbl && HAVE_64BIT_OBJECTS)
10972 as_warn (_("la used to load 64-bit address; recommend using dla "
10973 "instead"));
10974
10975 if (small_offset_p (0, align, 16))
10976 {
10977 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
10978 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10979 break;
10980 }
10981
10982 if (mips_opts.at && (op[0] == breg))
10983 {
10984 tempreg = AT;
10985 used_at = 1;
10986 }
10987 else
10988 tempreg = op[0];
10989
10990 if (offset_expr.X_op != O_symbol
10991 && offset_expr.X_op != O_constant)
10992 {
10993 as_bad (_("expression too complex"));
10994 offset_expr.X_op = O_constant;
10995 }
10996
10997 if (offset_expr.X_op == O_constant)
10998 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
10999 else if (mips_pic == NO_PIC)
11000 {
11001 /* If this is a reference to a GP relative symbol, we want
11002 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
11003 Otherwise we want
11004 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11005 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11006 If we have a constant, we need two instructions anyhow,
11007 so we may as well always use the latter form.
11008
11009 With 64bit address space and a usable $at we want
11010 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11011 lui $at,<sym> (BFD_RELOC_HI16_S)
11012 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11013 daddiu $at,<sym> (BFD_RELOC_LO16)
11014 dsll32 $tempreg,0
11015 daddu $tempreg,$tempreg,$at
11016
11017 If $at is already in use, we use a path which is suboptimal
11018 on superscalar processors.
11019 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11020 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11021 dsll $tempreg,16
11022 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11023 dsll $tempreg,16
11024 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
11025
11026 For GP relative symbols in 64bit address space we can use
11027 the same sequence as in 32bit address space. */
11028 if (HAVE_64BIT_SYMBOLS)
11029 {
11030 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11031 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11032 {
11033 relax_start (offset_expr.X_add_symbol);
11034 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11035 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
11036 relax_switch ();
11037 }
11038
11039 if (used_at == 0 && mips_opts.at)
11040 {
11041 macro_build (&offset_expr, "lui", LUI_FMT,
11042 tempreg, BFD_RELOC_MIPS_HIGHEST);
11043 macro_build (&offset_expr, "lui", LUI_FMT,
11044 AT, BFD_RELOC_HI16_S);
11045 macro_build (&offset_expr, "daddiu", "t,r,j",
11046 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
11047 macro_build (&offset_expr, "daddiu", "t,r,j",
11048 AT, AT, BFD_RELOC_LO16);
11049 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
11050 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
11051 used_at = 1;
11052 }
11053 else
11054 {
11055 macro_build (&offset_expr, "lui", LUI_FMT,
11056 tempreg, BFD_RELOC_MIPS_HIGHEST);
11057 macro_build (&offset_expr, "daddiu", "t,r,j",
11058 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
11059 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
11060 macro_build (&offset_expr, "daddiu", "t,r,j",
11061 tempreg, tempreg, BFD_RELOC_HI16_S);
11062 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
11063 macro_build (&offset_expr, "daddiu", "t,r,j",
11064 tempreg, tempreg, BFD_RELOC_LO16);
11065 }
11066
11067 if (mips_relax.sequence)
11068 relax_end ();
11069 }
11070 else
11071 {
11072 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11073 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11074 {
11075 relax_start (offset_expr.X_add_symbol);
11076 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11077 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
11078 relax_switch ();
11079 }
11080 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
11081 as_bad (_("offset too large"));
11082 macro_build_lui (&offset_expr, tempreg);
11083 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11084 tempreg, tempreg, BFD_RELOC_LO16);
11085 if (mips_relax.sequence)
11086 relax_end ();
11087 }
11088 }
11089 else if (!mips_big_got && !HAVE_NEWABI)
11090 {
11091 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11092
11093 /* If this is a reference to an external symbol, and there
11094 is no constant, we want
11095 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11096 or for lca or if tempreg is PIC_CALL_REG
11097 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11098 For a local symbol, we want
11099 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11100 nop
11101 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11102
11103 If we have a small constant, and this is a reference to
11104 an external symbol, we want
11105 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11106 nop
11107 addiu $tempreg,$tempreg,<constant>
11108 For a local symbol, we want the same instruction
11109 sequence, but we output a BFD_RELOC_LO16 reloc on the
11110 addiu instruction.
11111
11112 If we have a large constant, and this is a reference to
11113 an external symbol, we want
11114 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11115 lui $at,<hiconstant>
11116 addiu $at,$at,<loconstant>
11117 addu $tempreg,$tempreg,$at
11118 For a local symbol, we want the same instruction
11119 sequence, but we output a BFD_RELOC_LO16 reloc on the
11120 addiu instruction.
11121 */
11122
11123 if (offset_expr.X_add_number == 0)
11124 {
11125 if (mips_pic == SVR4_PIC
11126 && breg == 0
11127 && (call || tempreg == PIC_CALL_REG))
11128 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
11129
11130 relax_start (offset_expr.X_add_symbol);
11131 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11132 lw_reloc_type, mips_gp_register);
11133 if (breg != 0)
11134 {
11135 /* We're going to put in an addu instruction using
11136 tempreg, so we may as well insert the nop right
11137 now. */
11138 load_delay_nop ();
11139 }
11140 relax_switch ();
11141 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11142 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
11143 load_delay_nop ();
11144 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11145 tempreg, tempreg, BFD_RELOC_LO16);
11146 relax_end ();
11147 /* FIXME: If breg == 0, and the next instruction uses
11148 $tempreg, then if this variant case is used an extra
11149 nop will be generated. */
11150 }
11151 else if (offset_expr.X_add_number >= -0x8000
11152 && offset_expr.X_add_number < 0x8000)
11153 {
11154 load_got_offset (tempreg, &offset_expr);
11155 load_delay_nop ();
11156 add_got_offset (tempreg, &offset_expr);
11157 }
11158 else
11159 {
11160 expr1.X_add_number = offset_expr.X_add_number;
11161 offset_expr.X_add_number =
11162 SEXT_16BIT (offset_expr.X_add_number);
11163 load_got_offset (tempreg, &offset_expr);
11164 offset_expr.X_add_number = expr1.X_add_number;
11165 /* If we are going to add in a base register, and the
11166 target register and the base register are the same,
11167 then we are using AT as a temporary register. Since
11168 we want to load the constant into AT, we add our
11169 current AT (from the global offset table) and the
11170 register into the register now, and pretend we were
11171 not using a base register. */
11172 if (breg == op[0])
11173 {
11174 load_delay_nop ();
11175 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11176 op[0], AT, breg);
11177 breg = 0;
11178 tempreg = op[0];
11179 }
11180 add_got_offset_hilo (tempreg, &offset_expr, AT);
11181 used_at = 1;
11182 }
11183 }
11184 else if (!mips_big_got && HAVE_NEWABI)
11185 {
11186 int add_breg_early = 0;
11187
11188 /* If this is a reference to an external, and there is no
11189 constant, or local symbol (*), with or without a
11190 constant, we want
11191 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11192 or for lca or if tempreg is PIC_CALL_REG
11193 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11194
11195 If we have a small constant, and this is a reference to
11196 an external symbol, we want
11197 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11198 addiu $tempreg,$tempreg,<constant>
11199
11200 If we have a large constant, and this is a reference to
11201 an external symbol, we want
11202 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11203 lui $at,<hiconstant>
11204 addiu $at,$at,<loconstant>
11205 addu $tempreg,$tempreg,$at
11206
11207 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
11208 local symbols, even though it introduces an additional
11209 instruction. */
11210
11211 if (offset_expr.X_add_number)
11212 {
11213 expr1.X_add_number = offset_expr.X_add_number;
11214 offset_expr.X_add_number = 0;
11215
11216 relax_start (offset_expr.X_add_symbol);
11217 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11218 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11219
11220 if (expr1.X_add_number >= -0x8000
11221 && expr1.X_add_number < 0x8000)
11222 {
11223 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11224 tempreg, tempreg, BFD_RELOC_LO16);
11225 }
11226 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11227 {
11228 unsigned int dreg;
11229
11230 /* If we are going to add in a base register, and the
11231 target register and the base register are the same,
11232 then we are using AT as a temporary register. Since
11233 we want to load the constant into AT, we add our
11234 current AT (from the global offset table) and the
11235 register into the register now, and pretend we were
11236 not using a base register. */
11237 if (breg != op[0])
11238 dreg = tempreg;
11239 else
11240 {
11241 gas_assert (tempreg == AT);
11242 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11243 op[0], AT, breg);
11244 dreg = op[0];
11245 add_breg_early = 1;
11246 }
11247
11248 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11249 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11250 dreg, dreg, AT);
11251
11252 used_at = 1;
11253 }
11254 else
11255 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11256
11257 relax_switch ();
11258 offset_expr.X_add_number = expr1.X_add_number;
11259
11260 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11261 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11262 if (add_breg_early)
11263 {
11264 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11265 op[0], tempreg, breg);
11266 breg = 0;
11267 tempreg = op[0];
11268 }
11269 relax_end ();
11270 }
11271 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
11272 {
11273 relax_start (offset_expr.X_add_symbol);
11274 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11275 BFD_RELOC_MIPS_CALL16, mips_gp_register);
11276 relax_switch ();
11277 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11278 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11279 relax_end ();
11280 }
11281 else
11282 {
11283 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11284 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11285 }
11286 }
11287 else if (mips_big_got && !HAVE_NEWABI)
11288 {
11289 int gpdelay;
11290 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11291 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11292 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11293
11294 /* This is the large GOT case. If this is a reference to an
11295 external symbol, and there is no constant, we want
11296 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11297 addu $tempreg,$tempreg,$gp
11298 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11299 or for lca or if tempreg is PIC_CALL_REG
11300 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11301 addu $tempreg,$tempreg,$gp
11302 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11303 For a local symbol, we want
11304 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11305 nop
11306 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11307
11308 If we have a small constant, and this is a reference to
11309 an external symbol, we want
11310 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11311 addu $tempreg,$tempreg,$gp
11312 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11313 nop
11314 addiu $tempreg,$tempreg,<constant>
11315 For a local symbol, we want
11316 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11317 nop
11318 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11319
11320 If we have a large constant, and this is a reference to
11321 an external symbol, we want
11322 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11323 addu $tempreg,$tempreg,$gp
11324 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11325 lui $at,<hiconstant>
11326 addiu $at,$at,<loconstant>
11327 addu $tempreg,$tempreg,$at
11328 For a local symbol, we want
11329 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11330 lui $at,<hiconstant>
11331 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11332 addu $tempreg,$tempreg,$at
11333 */
11334
11335 expr1.X_add_number = offset_expr.X_add_number;
11336 offset_expr.X_add_number = 0;
11337 relax_start (offset_expr.X_add_symbol);
11338 gpdelay = reg_needs_delay (mips_gp_register);
11339 if (expr1.X_add_number == 0 && breg == 0
11340 && (call || tempreg == PIC_CALL_REG))
11341 {
11342 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11343 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11344 }
11345 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11346 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11347 tempreg, tempreg, mips_gp_register);
11348 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11349 tempreg, lw_reloc_type, tempreg);
11350 if (expr1.X_add_number == 0)
11351 {
11352 if (breg != 0)
11353 {
11354 /* We're going to put in an addu instruction using
11355 tempreg, so we may as well insert the nop right
11356 now. */
11357 load_delay_nop ();
11358 }
11359 }
11360 else if (expr1.X_add_number >= -0x8000
11361 && expr1.X_add_number < 0x8000)
11362 {
11363 load_delay_nop ();
11364 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11365 tempreg, tempreg, BFD_RELOC_LO16);
11366 }
11367 else
11368 {
11369 unsigned int dreg;
11370
11371 /* If we are going to add in a base register, and the
11372 target register and the base register are the same,
11373 then we are using AT as a temporary register. Since
11374 we want to load the constant into AT, we add our
11375 current AT (from the global offset table) and the
11376 register into the register now, and pretend we were
11377 not using a base register. */
11378 if (breg != op[0])
11379 dreg = tempreg;
11380 else
11381 {
11382 gas_assert (tempreg == AT);
11383 load_delay_nop ();
11384 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11385 op[0], AT, breg);
11386 dreg = op[0];
11387 }
11388
11389 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11390 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11391
11392 used_at = 1;
11393 }
11394 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
11395 relax_switch ();
11396
11397 if (gpdelay)
11398 {
11399 /* This is needed because this instruction uses $gp, but
11400 the first instruction on the main stream does not. */
11401 macro_build (NULL, "nop", "");
11402 }
11403
11404 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11405 local_reloc_type, mips_gp_register);
11406 if (expr1.X_add_number >= -0x8000
11407 && expr1.X_add_number < 0x8000)
11408 {
11409 load_delay_nop ();
11410 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11411 tempreg, tempreg, BFD_RELOC_LO16);
11412 /* FIXME: If add_number is 0, and there was no base
11413 register, the external symbol case ended with a load,
11414 so if the symbol turns out to not be external, and
11415 the next instruction uses tempreg, an unnecessary nop
11416 will be inserted. */
11417 }
11418 else
11419 {
11420 if (breg == op[0])
11421 {
11422 /* We must add in the base register now, as in the
11423 external symbol case. */
11424 gas_assert (tempreg == AT);
11425 load_delay_nop ();
11426 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11427 op[0], AT, breg);
11428 tempreg = op[0];
11429 /* We set breg to 0 because we have arranged to add
11430 it in in both cases. */
11431 breg = 0;
11432 }
11433
11434 macro_build_lui (&expr1, AT);
11435 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11436 AT, AT, BFD_RELOC_LO16);
11437 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11438 tempreg, tempreg, AT);
11439 used_at = 1;
11440 }
11441 relax_end ();
11442 }
11443 else if (mips_big_got && HAVE_NEWABI)
11444 {
11445 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11446 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11447 int add_breg_early = 0;
11448
11449 /* This is the large GOT case. If this is a reference to an
11450 external symbol, and there is no constant, we want
11451 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11452 add $tempreg,$tempreg,$gp
11453 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11454 or for lca or if tempreg is PIC_CALL_REG
11455 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11456 add $tempreg,$tempreg,$gp
11457 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11458
11459 If we have a small constant, and this is a reference to
11460 an external symbol, we want
11461 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11462 add $tempreg,$tempreg,$gp
11463 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11464 addi $tempreg,$tempreg,<constant>
11465
11466 If we have a large constant, and this is a reference to
11467 an external symbol, we want
11468 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11469 addu $tempreg,$tempreg,$gp
11470 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11471 lui $at,<hiconstant>
11472 addi $at,$at,<loconstant>
11473 add $tempreg,$tempreg,$at
11474
11475 If we have NewABI, and we know it's a local symbol, we want
11476 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11477 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11478 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11479
11480 relax_start (offset_expr.X_add_symbol);
11481
11482 expr1.X_add_number = offset_expr.X_add_number;
11483 offset_expr.X_add_number = 0;
11484
11485 if (expr1.X_add_number == 0 && breg == 0
11486 && (call || tempreg == PIC_CALL_REG))
11487 {
11488 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11489 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11490 }
11491 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11492 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11493 tempreg, tempreg, mips_gp_register);
11494 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11495 tempreg, lw_reloc_type, tempreg);
11496
11497 if (expr1.X_add_number == 0)
11498 ;
11499 else if (expr1.X_add_number >= -0x8000
11500 && expr1.X_add_number < 0x8000)
11501 {
11502 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11503 tempreg, tempreg, BFD_RELOC_LO16);
11504 }
11505 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11506 {
11507 unsigned int dreg;
11508
11509 /* If we are going to add in a base register, and the
11510 target register and the base register are the same,
11511 then we are using AT as a temporary register. Since
11512 we want to load the constant into AT, we add our
11513 current AT (from the global offset table) and the
11514 register into the register now, and pretend we were
11515 not using a base register. */
11516 if (breg != op[0])
11517 dreg = tempreg;
11518 else
11519 {
11520 gas_assert (tempreg == AT);
11521 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11522 op[0], AT, breg);
11523 dreg = op[0];
11524 add_breg_early = 1;
11525 }
11526
11527 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11528 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11529
11530 used_at = 1;
11531 }
11532 else
11533 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11534
11535 relax_switch ();
11536 offset_expr.X_add_number = expr1.X_add_number;
11537 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11538 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11539 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11540 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11541 if (add_breg_early)
11542 {
11543 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11544 op[0], tempreg, breg);
11545 breg = 0;
11546 tempreg = op[0];
11547 }
11548 relax_end ();
11549 }
11550 else
11551 abort ();
11552
11553 if (breg != 0)
11554 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
11555 break;
11556
11557 case M_MSGSND:
11558 gas_assert (!mips_opts.micromips);
11559 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
11560 break;
11561
11562 case M_MSGLD:
11563 gas_assert (!mips_opts.micromips);
11564 macro_build (NULL, "c2", "C", 0x02);
11565 break;
11566
11567 case M_MSGLD_T:
11568 gas_assert (!mips_opts.micromips);
11569 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
11570 break;
11571
11572 case M_MSGWAIT:
11573 gas_assert (!mips_opts.micromips);
11574 macro_build (NULL, "c2", "C", 3);
11575 break;
11576
11577 case M_MSGWAIT_T:
11578 gas_assert (!mips_opts.micromips);
11579 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
11580 break;
11581
11582 case M_J_A:
11583 /* The j instruction may not be used in PIC code, since it
11584 requires an absolute address. We convert it to a b
11585 instruction. */
11586 if (mips_pic == NO_PIC)
11587 macro_build (&offset_expr, "j", "a");
11588 else
11589 macro_build (&offset_expr, "b", "p");
11590 break;
11591
11592 /* The jal instructions must be handled as macros because when
11593 generating PIC code they expand to multi-instruction
11594 sequences. Normally they are simple instructions. */
11595 case M_JALS_1:
11596 op[1] = op[0];
11597 op[0] = RA;
11598 /* Fall through. */
11599 case M_JALS_2:
11600 gas_assert (mips_opts.micromips);
11601 if (mips_opts.insn32)
11602 {
11603 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11604 break;
11605 }
11606 jals = 1;
11607 goto jal;
11608 case M_JAL_1:
11609 op[1] = op[0];
11610 op[0] = RA;
11611 /* Fall through. */
11612 case M_JAL_2:
11613 jal:
11614 if (mips_pic == NO_PIC)
11615 {
11616 s = jals ? "jalrs" : "jalr";
11617 if (mips_opts.micromips
11618 && !mips_opts.insn32
11619 && op[0] == RA
11620 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11621 macro_build (NULL, s, "mj", op[1]);
11622 else
11623 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11624 }
11625 else
11626 {
11627 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11628 && mips_cprestore_offset >= 0);
11629
11630 if (op[1] != PIC_CALL_REG)
11631 as_warn (_("MIPS PIC call to register other than $25"));
11632
11633 s = ((mips_opts.micromips
11634 && !mips_opts.insn32
11635 && (!mips_opts.noreorder || cprestore))
11636 ? "jalrs" : "jalr");
11637 if (mips_opts.micromips
11638 && !mips_opts.insn32
11639 && op[0] == RA
11640 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11641 macro_build (NULL, s, "mj", op[1]);
11642 else
11643 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11644 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
11645 {
11646 if (mips_cprestore_offset < 0)
11647 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11648 else
11649 {
11650 if (!mips_frame_reg_valid)
11651 {
11652 as_warn (_("no .frame pseudo-op used in PIC code"));
11653 /* Quiet this warning. */
11654 mips_frame_reg_valid = 1;
11655 }
11656 if (!mips_cprestore_valid)
11657 {
11658 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11659 /* Quiet this warning. */
11660 mips_cprestore_valid = 1;
11661 }
11662 if (mips_opts.noreorder)
11663 macro_build (NULL, "nop", "");
11664 expr1.X_add_number = mips_cprestore_offset;
11665 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11666 mips_gp_register,
11667 mips_frame_reg,
11668 HAVE_64BIT_ADDRESSES);
11669 }
11670 }
11671 }
11672
11673 break;
11674
11675 case M_JALS_A:
11676 gas_assert (mips_opts.micromips);
11677 if (mips_opts.insn32)
11678 {
11679 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11680 break;
11681 }
11682 jals = 1;
11683 /* Fall through. */
11684 case M_JAL_A:
11685 if (mips_pic == NO_PIC)
11686 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
11687 else if (mips_pic == SVR4_PIC)
11688 {
11689 /* If this is a reference to an external symbol, and we are
11690 using a small GOT, we want
11691 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11692 nop
11693 jalr $ra,$25
11694 nop
11695 lw $gp,cprestore($sp)
11696 The cprestore value is set using the .cprestore
11697 pseudo-op. If we are using a big GOT, we want
11698 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11699 addu $25,$25,$gp
11700 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11701 nop
11702 jalr $ra,$25
11703 nop
11704 lw $gp,cprestore($sp)
11705 If the symbol is not external, we want
11706 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11707 nop
11708 addiu $25,$25,<sym> (BFD_RELOC_LO16)
11709 jalr $ra,$25
11710 nop
11711 lw $gp,cprestore($sp)
11712
11713 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11714 sequences above, minus nops, unless the symbol is local,
11715 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11716 GOT_DISP. */
11717 if (HAVE_NEWABI)
11718 {
11719 if (!mips_big_got)
11720 {
11721 relax_start (offset_expr.X_add_symbol);
11722 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11723 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11724 mips_gp_register);
11725 relax_switch ();
11726 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11727 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
11728 mips_gp_register);
11729 relax_end ();
11730 }
11731 else
11732 {
11733 relax_start (offset_expr.X_add_symbol);
11734 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11735 BFD_RELOC_MIPS_CALL_HI16);
11736 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11737 PIC_CALL_REG, mips_gp_register);
11738 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11739 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11740 PIC_CALL_REG);
11741 relax_switch ();
11742 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11743 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11744 mips_gp_register);
11745 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11746 PIC_CALL_REG, PIC_CALL_REG,
11747 BFD_RELOC_MIPS_GOT_OFST);
11748 relax_end ();
11749 }
11750
11751 macro_build_jalr (&offset_expr, 0);
11752 }
11753 else
11754 {
11755 relax_start (offset_expr.X_add_symbol);
11756 if (!mips_big_got)
11757 {
11758 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11759 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11760 mips_gp_register);
11761 load_delay_nop ();
11762 relax_switch ();
11763 }
11764 else
11765 {
11766 int gpdelay;
11767
11768 gpdelay = reg_needs_delay (mips_gp_register);
11769 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11770 BFD_RELOC_MIPS_CALL_HI16);
11771 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11772 PIC_CALL_REG, mips_gp_register);
11773 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11774 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11775 PIC_CALL_REG);
11776 load_delay_nop ();
11777 relax_switch ();
11778 if (gpdelay)
11779 macro_build (NULL, "nop", "");
11780 }
11781 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11782 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
11783 mips_gp_register);
11784 load_delay_nop ();
11785 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11786 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
11787 relax_end ();
11788 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
11789
11790 if (mips_cprestore_offset < 0)
11791 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11792 else
11793 {
11794 if (!mips_frame_reg_valid)
11795 {
11796 as_warn (_("no .frame pseudo-op used in PIC code"));
11797 /* Quiet this warning. */
11798 mips_frame_reg_valid = 1;
11799 }
11800 if (!mips_cprestore_valid)
11801 {
11802 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11803 /* Quiet this warning. */
11804 mips_cprestore_valid = 1;
11805 }
11806 if (mips_opts.noreorder)
11807 macro_build (NULL, "nop", "");
11808 expr1.X_add_number = mips_cprestore_offset;
11809 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11810 mips_gp_register,
11811 mips_frame_reg,
11812 HAVE_64BIT_ADDRESSES);
11813 }
11814 }
11815 }
11816 else if (mips_pic == VXWORKS_PIC)
11817 as_bad (_("non-PIC jump used in PIC library"));
11818 else
11819 abort ();
11820
11821 break;
11822
11823 case M_LBUE_AB:
11824 s = "lbue";
11825 fmt = "t,+j(b)";
11826 offbits = 9;
11827 goto ld_st;
11828 case M_LHUE_AB:
11829 s = "lhue";
11830 fmt = "t,+j(b)";
11831 offbits = 9;
11832 goto ld_st;
11833 case M_LBE_AB:
11834 s = "lbe";
11835 fmt = "t,+j(b)";
11836 offbits = 9;
11837 goto ld_st;
11838 case M_LHE_AB:
11839 s = "lhe";
11840 fmt = "t,+j(b)";
11841 offbits = 9;
11842 goto ld_st;
11843 case M_LLE_AB:
11844 s = "lle";
11845 fmt = "t,+j(b)";
11846 offbits = 9;
11847 goto ld_st;
11848 case M_LWE_AB:
11849 s = "lwe";
11850 fmt = "t,+j(b)";
11851 offbits = 9;
11852 goto ld_st;
11853 case M_LWLE_AB:
11854 s = "lwle";
11855 fmt = "t,+j(b)";
11856 offbits = 9;
11857 goto ld_st;
11858 case M_LWRE_AB:
11859 s = "lwre";
11860 fmt = "t,+j(b)";
11861 offbits = 9;
11862 goto ld_st;
11863 case M_SBE_AB:
11864 s = "sbe";
11865 fmt = "t,+j(b)";
11866 offbits = 9;
11867 goto ld_st;
11868 case M_SCE_AB:
11869 s = "sce";
11870 fmt = "t,+j(b)";
11871 offbits = 9;
11872 goto ld_st;
11873 case M_SHE_AB:
11874 s = "she";
11875 fmt = "t,+j(b)";
11876 offbits = 9;
11877 goto ld_st;
11878 case M_SWE_AB:
11879 s = "swe";
11880 fmt = "t,+j(b)";
11881 offbits = 9;
11882 goto ld_st;
11883 case M_SWLE_AB:
11884 s = "swle";
11885 fmt = "t,+j(b)";
11886 offbits = 9;
11887 goto ld_st;
11888 case M_SWRE_AB:
11889 s = "swre";
11890 fmt = "t,+j(b)";
11891 offbits = 9;
11892 goto ld_st;
11893 case M_ACLR_AB:
11894 s = "aclr";
11895 fmt = "\\,~(b)";
11896 offbits = 12;
11897 goto ld_st;
11898 case M_ASET_AB:
11899 s = "aset";
11900 fmt = "\\,~(b)";
11901 offbits = 12;
11902 goto ld_st;
11903 case M_LB_AB:
11904 s = "lb";
11905 fmt = "t,o(b)";
11906 goto ld;
11907 case M_LBU_AB:
11908 s = "lbu";
11909 fmt = "t,o(b)";
11910 goto ld;
11911 case M_LH_AB:
11912 s = "lh";
11913 fmt = "t,o(b)";
11914 goto ld;
11915 case M_LHU_AB:
11916 s = "lhu";
11917 fmt = "t,o(b)";
11918 goto ld;
11919 case M_LW_AB:
11920 s = "lw";
11921 fmt = "t,o(b)";
11922 goto ld;
11923 case M_LWC0_AB:
11924 gas_assert (!mips_opts.micromips);
11925 s = "lwc0";
11926 fmt = "E,o(b)";
11927 /* Itbl support may require additional care here. */
11928 coproc = 1;
11929 goto ld_st;
11930 case M_LWC1_AB:
11931 s = "lwc1";
11932 fmt = "T,o(b)";
11933 /* Itbl support may require additional care here. */
11934 coproc = 1;
11935 goto ld_st;
11936 case M_LWC2_AB:
11937 s = "lwc2";
11938 fmt = COP12_FMT;
11939 offbits = (mips_opts.micromips ? 12
11940 : ISA_IS_R6 (mips_opts.isa) ? 11
11941 : 16);
11942 /* Itbl support may require additional care here. */
11943 coproc = 1;
11944 goto ld_st;
11945 case M_LWC3_AB:
11946 gas_assert (!mips_opts.micromips);
11947 s = "lwc3";
11948 fmt = "E,o(b)";
11949 /* Itbl support may require additional care here. */
11950 coproc = 1;
11951 goto ld_st;
11952 case M_LWL_AB:
11953 s = "lwl";
11954 fmt = MEM12_FMT;
11955 offbits = (mips_opts.micromips ? 12 : 16);
11956 goto ld_st;
11957 case M_LWR_AB:
11958 s = "lwr";
11959 fmt = MEM12_FMT;
11960 offbits = (mips_opts.micromips ? 12 : 16);
11961 goto ld_st;
11962 case M_LDC1_AB:
11963 s = "ldc1";
11964 fmt = "T,o(b)";
11965 /* Itbl support may require additional care here. */
11966 coproc = 1;
11967 goto ld_st;
11968 case M_LDC2_AB:
11969 s = "ldc2";
11970 fmt = COP12_FMT;
11971 offbits = (mips_opts.micromips ? 12
11972 : ISA_IS_R6 (mips_opts.isa) ? 11
11973 : 16);
11974 /* Itbl support may require additional care here. */
11975 coproc = 1;
11976 goto ld_st;
11977 case M_LQC2_AB:
11978 s = "lqc2";
11979 fmt = "+7,o(b)";
11980 /* Itbl support may require additional care here. */
11981 coproc = 1;
11982 goto ld_st;
11983 case M_LDC3_AB:
11984 s = "ldc3";
11985 fmt = "E,o(b)";
11986 /* Itbl support may require additional care here. */
11987 coproc = 1;
11988 goto ld_st;
11989 case M_LDL_AB:
11990 s = "ldl";
11991 fmt = MEM12_FMT;
11992 offbits = (mips_opts.micromips ? 12 : 16);
11993 goto ld_st;
11994 case M_LDR_AB:
11995 s = "ldr";
11996 fmt = MEM12_FMT;
11997 offbits = (mips_opts.micromips ? 12 : 16);
11998 goto ld_st;
11999 case M_LL_AB:
12000 s = "ll";
12001 fmt = LL_SC_FMT;
12002 offbits = (mips_opts.micromips ? 12
12003 : ISA_IS_R6 (mips_opts.isa) ? 9
12004 : 16);
12005 goto ld;
12006 case M_LLD_AB:
12007 s = "lld";
12008 fmt = LL_SC_FMT;
12009 offbits = (mips_opts.micromips ? 12
12010 : ISA_IS_R6 (mips_opts.isa) ? 9
12011 : 16);
12012 goto ld;
12013 case M_LWU_AB:
12014 s = "lwu";
12015 fmt = MEM12_FMT;
12016 offbits = (mips_opts.micromips ? 12 : 16);
12017 goto ld;
12018 case M_LWP_AB:
12019 gas_assert (mips_opts.micromips);
12020 s = "lwp";
12021 fmt = "t,~(b)";
12022 offbits = 12;
12023 lp = 1;
12024 goto ld;
12025 case M_LDP_AB:
12026 gas_assert (mips_opts.micromips);
12027 s = "ldp";
12028 fmt = "t,~(b)";
12029 offbits = 12;
12030 lp = 1;
12031 goto ld;
12032 case M_LLDP_AB:
12033 case M_LLWP_AB:
12034 case M_LLWPE_AB:
12035 s = ip->insn_mo->name;
12036 fmt = "t,d,s";
12037 ll_sc_paired = 1;
12038 offbits = 0;
12039 goto ld;
12040 case M_LWM_AB:
12041 gas_assert (mips_opts.micromips);
12042 s = "lwm";
12043 fmt = "n,~(b)";
12044 offbits = 12;
12045 goto ld_st;
12046 case M_LDM_AB:
12047 gas_assert (mips_opts.micromips);
12048 s = "ldm";
12049 fmt = "n,~(b)";
12050 offbits = 12;
12051 goto ld_st;
12052
12053 ld:
12054 /* Try to use one the the load registers to compute the base address.
12055 We don't want to use $0 as tempreg. */
12056 if (ll_sc_paired)
12057 {
12058 if ((op[0] == ZERO && op[3] == op[1])
12059 || (op[1] == ZERO && op[3] == op[0])
12060 || (op[0] == ZERO && op[1] == ZERO))
12061 goto ld_st;
12062 else if (op[0] != op[3] && op[0] != ZERO)
12063 tempreg = op[0];
12064 else
12065 tempreg = op[1];
12066 }
12067 else
12068 {
12069 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
12070 goto ld_st;
12071 else
12072 tempreg = op[0] + lp;
12073 }
12074 goto ld_noat;
12075
12076 case M_SB_AB:
12077 s = "sb";
12078 fmt = "t,o(b)";
12079 goto ld_st;
12080 case M_SH_AB:
12081 s = "sh";
12082 fmt = "t,o(b)";
12083 goto ld_st;
12084 case M_SW_AB:
12085 s = "sw";
12086 fmt = "t,o(b)";
12087 goto ld_st;
12088 case M_SWC0_AB:
12089 gas_assert (!mips_opts.micromips);
12090 s = "swc0";
12091 fmt = "E,o(b)";
12092 /* Itbl support may require additional care here. */
12093 coproc = 1;
12094 goto ld_st;
12095 case M_SWC1_AB:
12096 s = "swc1";
12097 fmt = "T,o(b)";
12098 /* Itbl support may require additional care here. */
12099 coproc = 1;
12100 goto ld_st;
12101 case M_SWC2_AB:
12102 s = "swc2";
12103 fmt = COP12_FMT;
12104 offbits = (mips_opts.micromips ? 12
12105 : ISA_IS_R6 (mips_opts.isa) ? 11
12106 : 16);
12107 /* Itbl support may require additional care here. */
12108 coproc = 1;
12109 goto ld_st;
12110 case M_SWC3_AB:
12111 gas_assert (!mips_opts.micromips);
12112 s = "swc3";
12113 fmt = "E,o(b)";
12114 /* Itbl support may require additional care here. */
12115 coproc = 1;
12116 goto ld_st;
12117 case M_SWL_AB:
12118 s = "swl";
12119 fmt = MEM12_FMT;
12120 offbits = (mips_opts.micromips ? 12 : 16);
12121 goto ld_st;
12122 case M_SWR_AB:
12123 s = "swr";
12124 fmt = MEM12_FMT;
12125 offbits = (mips_opts.micromips ? 12 : 16);
12126 goto ld_st;
12127 case M_SC_AB:
12128 s = "sc";
12129 fmt = LL_SC_FMT;
12130 offbits = (mips_opts.micromips ? 12
12131 : ISA_IS_R6 (mips_opts.isa) ? 9
12132 : 16);
12133 goto ld_st;
12134 case M_SCD_AB:
12135 s = "scd";
12136 fmt = LL_SC_FMT;
12137 offbits = (mips_opts.micromips ? 12
12138 : ISA_IS_R6 (mips_opts.isa) ? 9
12139 : 16);
12140 goto ld_st;
12141 case M_SCDP_AB:
12142 case M_SCWP_AB:
12143 case M_SCWPE_AB:
12144 s = ip->insn_mo->name;
12145 fmt = "t,d,s";
12146 ll_sc_paired = 1;
12147 offbits = 0;
12148 goto ld_st;
12149 case M_CACHE_AB:
12150 s = "cache";
12151 fmt = (mips_opts.micromips ? "k,~(b)"
12152 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12153 : "k,o(b)");
12154 offbits = (mips_opts.micromips ? 12
12155 : ISA_IS_R6 (mips_opts.isa) ? 9
12156 : 16);
12157 goto ld_st;
12158 case M_CACHEE_AB:
12159 s = "cachee";
12160 fmt = "k,+j(b)";
12161 offbits = 9;
12162 goto ld_st;
12163 case M_PREF_AB:
12164 s = "pref";
12165 fmt = (mips_opts.micromips ? "k,~(b)"
12166 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12167 : "k,o(b)");
12168 offbits = (mips_opts.micromips ? 12
12169 : ISA_IS_R6 (mips_opts.isa) ? 9
12170 : 16);
12171 goto ld_st;
12172 case M_PREFE_AB:
12173 s = "prefe";
12174 fmt = "k,+j(b)";
12175 offbits = 9;
12176 goto ld_st;
12177 case M_SDC1_AB:
12178 s = "sdc1";
12179 fmt = "T,o(b)";
12180 coproc = 1;
12181 /* Itbl support may require additional care here. */
12182 goto ld_st;
12183 case M_SDC2_AB:
12184 s = "sdc2";
12185 fmt = COP12_FMT;
12186 offbits = (mips_opts.micromips ? 12
12187 : ISA_IS_R6 (mips_opts.isa) ? 11
12188 : 16);
12189 /* Itbl support may require additional care here. */
12190 coproc = 1;
12191 goto ld_st;
12192 case M_SQC2_AB:
12193 s = "sqc2";
12194 fmt = "+7,o(b)";
12195 /* Itbl support may require additional care here. */
12196 coproc = 1;
12197 goto ld_st;
12198 case M_SDC3_AB:
12199 gas_assert (!mips_opts.micromips);
12200 s = "sdc3";
12201 fmt = "E,o(b)";
12202 /* Itbl support may require additional care here. */
12203 coproc = 1;
12204 goto ld_st;
12205 case M_SDL_AB:
12206 s = "sdl";
12207 fmt = MEM12_FMT;
12208 offbits = (mips_opts.micromips ? 12 : 16);
12209 goto ld_st;
12210 case M_SDR_AB:
12211 s = "sdr";
12212 fmt = MEM12_FMT;
12213 offbits = (mips_opts.micromips ? 12 : 16);
12214 goto ld_st;
12215 case M_SWP_AB:
12216 gas_assert (mips_opts.micromips);
12217 s = "swp";
12218 fmt = "t,~(b)";
12219 offbits = 12;
12220 goto ld_st;
12221 case M_SDP_AB:
12222 gas_assert (mips_opts.micromips);
12223 s = "sdp";
12224 fmt = "t,~(b)";
12225 offbits = 12;
12226 goto ld_st;
12227 case M_SWM_AB:
12228 gas_assert (mips_opts.micromips);
12229 s = "swm";
12230 fmt = "n,~(b)";
12231 offbits = 12;
12232 goto ld_st;
12233 case M_SDM_AB:
12234 gas_assert (mips_opts.micromips);
12235 s = "sdm";
12236 fmt = "n,~(b)";
12237 offbits = 12;
12238
12239 ld_st:
12240 tempreg = AT;
12241 ld_noat:
12242 breg = ll_sc_paired ? op[3] : op[2];
12243 if (small_offset_p (0, align, 16))
12244 {
12245 /* The first case exists for M_LD_AB and M_SD_AB, which are
12246 macros for o32 but which should act like normal instructions
12247 otherwise. */
12248 if (offbits == 16)
12249 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
12250 offset_reloc[1], offset_reloc[2], breg);
12251 else if (small_offset_p (0, align, offbits))
12252 {
12253 if (offbits == 0)
12254 {
12255 if (ll_sc_paired)
12256 macro_build (NULL, s, fmt, op[0], op[1], breg);
12257 else
12258 macro_build (NULL, s, fmt, op[0], breg);
12259 }
12260 else
12261 macro_build (NULL, s, fmt, op[0],
12262 (int) offset_expr.X_add_number, breg);
12263 }
12264 else
12265 {
12266 if (tempreg == AT)
12267 used_at = 1;
12268 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12269 tempreg, breg, -1, offset_reloc[0],
12270 offset_reloc[1], offset_reloc[2]);
12271 if (offbits == 0)
12272 {
12273 if (ll_sc_paired)
12274 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12275 else
12276 macro_build (NULL, s, fmt, op[0], tempreg);
12277 }
12278 else
12279 macro_build (NULL, s, fmt, op[0], 0, tempreg);
12280 }
12281 break;
12282 }
12283
12284 if (tempreg == AT)
12285 used_at = 1;
12286
12287 if (offset_expr.X_op != O_constant
12288 && offset_expr.X_op != O_symbol)
12289 {
12290 as_bad (_("expression too complex"));
12291 offset_expr.X_op = O_constant;
12292 }
12293
12294 if (HAVE_32BIT_ADDRESSES
12295 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12296 {
12297 char value [32];
12298
12299 sprintf_vma (value, offset_expr.X_add_number);
12300 as_bad (_("number (0x%s) larger than 32 bits"), value);
12301 }
12302
12303 /* A constant expression in PIC code can be handled just as it
12304 is in non PIC code. */
12305 if (offset_expr.X_op == O_constant)
12306 {
12307 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12308 offbits == 0 ? 16 : offbits);
12309 offset_expr.X_add_number -= expr1.X_add_number;
12310
12311 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12312 if (breg != 0)
12313 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12314 tempreg, tempreg, breg);
12315 if (offbits == 0)
12316 {
12317 if (offset_expr.X_add_number != 0)
12318 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
12319 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
12320 if (ll_sc_paired)
12321 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12322 else
12323 macro_build (NULL, s, fmt, op[0], tempreg);
12324 }
12325 else if (offbits == 16)
12326 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12327 else
12328 macro_build (NULL, s, fmt, op[0],
12329 (int) offset_expr.X_add_number, tempreg);
12330 }
12331 else if (offbits != 16)
12332 {
12333 /* The offset field is too narrow to be used for a low-part
12334 relocation, so load the whole address into the auxiliary
12335 register. */
12336 load_address (tempreg, &offset_expr, &used_at);
12337 if (breg != 0)
12338 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12339 tempreg, tempreg, breg);
12340 if (offbits == 0)
12341 {
12342 if (ll_sc_paired)
12343 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12344 else
12345 macro_build (NULL, s, fmt, op[0], tempreg);
12346 }
12347 else
12348 macro_build (NULL, s, fmt, op[0], 0, tempreg);
12349 }
12350 else if (mips_pic == NO_PIC)
12351 {
12352 /* If this is a reference to a GP relative symbol, and there
12353 is no base register, we want
12354 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12355 Otherwise, if there is no base register, we want
12356 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12357 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12358 If we have a constant, we need two instructions anyhow,
12359 so we always use the latter form.
12360
12361 If we have a base register, and this is a reference to a
12362 GP relative symbol, we want
12363 addu $tempreg,$breg,$gp
12364 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
12365 Otherwise we want
12366 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12367 addu $tempreg,$tempreg,$breg
12368 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12369 With a constant we always use the latter case.
12370
12371 With 64bit address space and no base register and $at usable,
12372 we want
12373 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12374 lui $at,<sym> (BFD_RELOC_HI16_S)
12375 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12376 dsll32 $tempreg,0
12377 daddu $tempreg,$at
12378 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12379 If we have a base register, we want
12380 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12381 lui $at,<sym> (BFD_RELOC_HI16_S)
12382 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12383 daddu $at,$breg
12384 dsll32 $tempreg,0
12385 daddu $tempreg,$at
12386 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12387
12388 Without $at we can't generate the optimal path for superscalar
12389 processors here since this would require two temporary registers.
12390 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12391 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12392 dsll $tempreg,16
12393 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12394 dsll $tempreg,16
12395 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12396 If we have a base register, we want
12397 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12398 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12399 dsll $tempreg,16
12400 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12401 dsll $tempreg,16
12402 daddu $tempreg,$tempreg,$breg
12403 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
12404
12405 For GP relative symbols in 64bit address space we can use
12406 the same sequence as in 32bit address space. */
12407 if (HAVE_64BIT_SYMBOLS)
12408 {
12409 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12410 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12411 {
12412 relax_start (offset_expr.X_add_symbol);
12413 if (breg == 0)
12414 {
12415 macro_build (&offset_expr, s, fmt, op[0],
12416 BFD_RELOC_GPREL16, mips_gp_register);
12417 }
12418 else
12419 {
12420 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12421 tempreg, breg, mips_gp_register);
12422 macro_build (&offset_expr, s, fmt, op[0],
12423 BFD_RELOC_GPREL16, tempreg);
12424 }
12425 relax_switch ();
12426 }
12427
12428 if (used_at == 0 && mips_opts.at)
12429 {
12430 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12431 BFD_RELOC_MIPS_HIGHEST);
12432 macro_build (&offset_expr, "lui", LUI_FMT, AT,
12433 BFD_RELOC_HI16_S);
12434 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12435 tempreg, BFD_RELOC_MIPS_HIGHER);
12436 if (breg != 0)
12437 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
12438 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
12439 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
12440 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
12441 tempreg);
12442 used_at = 1;
12443 }
12444 else
12445 {
12446 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12447 BFD_RELOC_MIPS_HIGHEST);
12448 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12449 tempreg, BFD_RELOC_MIPS_HIGHER);
12450 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12451 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12452 tempreg, BFD_RELOC_HI16_S);
12453 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12454 if (breg != 0)
12455 macro_build (NULL, "daddu", "d,v,t",
12456 tempreg, tempreg, breg);
12457 macro_build (&offset_expr, s, fmt, op[0],
12458 BFD_RELOC_LO16, tempreg);
12459 }
12460
12461 if (mips_relax.sequence)
12462 relax_end ();
12463 break;
12464 }
12465
12466 if (breg == 0)
12467 {
12468 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12469 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12470 {
12471 relax_start (offset_expr.X_add_symbol);
12472 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
12473 mips_gp_register);
12474 relax_switch ();
12475 }
12476 macro_build_lui (&offset_expr, tempreg);
12477 macro_build (&offset_expr, s, fmt, op[0],
12478 BFD_RELOC_LO16, tempreg);
12479 if (mips_relax.sequence)
12480 relax_end ();
12481 }
12482 else
12483 {
12484 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12485 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12486 {
12487 relax_start (offset_expr.X_add_symbol);
12488 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12489 tempreg, breg, mips_gp_register);
12490 macro_build (&offset_expr, s, fmt, op[0],
12491 BFD_RELOC_GPREL16, tempreg);
12492 relax_switch ();
12493 }
12494 macro_build_lui (&offset_expr, tempreg);
12495 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12496 tempreg, tempreg, breg);
12497 macro_build (&offset_expr, s, fmt, op[0],
12498 BFD_RELOC_LO16, tempreg);
12499 if (mips_relax.sequence)
12500 relax_end ();
12501 }
12502 }
12503 else if (!mips_big_got)
12504 {
12505 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
12506
12507 /* If this is a reference to an external symbol, we want
12508 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12509 nop
12510 <op> op[0],0($tempreg)
12511 Otherwise we want
12512 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12513 nop
12514 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
12515 <op> op[0],0($tempreg)
12516
12517 For NewABI, we want
12518 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
12519 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
12520
12521 If there is a base register, we add it to $tempreg before
12522 the <op>. If there is a constant, we stick it in the
12523 <op> instruction. We don't handle constants larger than
12524 16 bits, because we have no way to load the upper 16 bits
12525 (actually, we could handle them for the subset of cases
12526 in which we are not using $at). */
12527 gas_assert (offset_expr.X_op == O_symbol);
12528 if (HAVE_NEWABI)
12529 {
12530 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12531 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12532 if (breg != 0)
12533 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12534 tempreg, tempreg, breg);
12535 macro_build (&offset_expr, s, fmt, op[0],
12536 BFD_RELOC_MIPS_GOT_OFST, tempreg);
12537 break;
12538 }
12539 expr1.X_add_number = offset_expr.X_add_number;
12540 offset_expr.X_add_number = 0;
12541 if (expr1.X_add_number < -0x8000
12542 || expr1.X_add_number >= 0x8000)
12543 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12544 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12545 lw_reloc_type, mips_gp_register);
12546 load_delay_nop ();
12547 relax_start (offset_expr.X_add_symbol);
12548 relax_switch ();
12549 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12550 tempreg, BFD_RELOC_LO16);
12551 relax_end ();
12552 if (breg != 0)
12553 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12554 tempreg, tempreg, breg);
12555 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12556 }
12557 else if (mips_big_got && !HAVE_NEWABI)
12558 {
12559 int gpdelay;
12560
12561 /* If this is a reference to an external symbol, we want
12562 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12563 addu $tempreg,$tempreg,$gp
12564 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12565 <op> op[0],0($tempreg)
12566 Otherwise we want
12567 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12568 nop
12569 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
12570 <op> op[0],0($tempreg)
12571 If there is a base register, we add it to $tempreg before
12572 the <op>. If there is a constant, we stick it in the
12573 <op> instruction. We don't handle constants larger than
12574 16 bits, because we have no way to load the upper 16 bits
12575 (actually, we could handle them for the subset of cases
12576 in which we are not using $at). */
12577 gas_assert (offset_expr.X_op == O_symbol);
12578 expr1.X_add_number = offset_expr.X_add_number;
12579 offset_expr.X_add_number = 0;
12580 if (expr1.X_add_number < -0x8000
12581 || expr1.X_add_number >= 0x8000)
12582 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12583 gpdelay = reg_needs_delay (mips_gp_register);
12584 relax_start (offset_expr.X_add_symbol);
12585 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12586 BFD_RELOC_MIPS_GOT_HI16);
12587 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12588 mips_gp_register);
12589 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12590 BFD_RELOC_MIPS_GOT_LO16, tempreg);
12591 relax_switch ();
12592 if (gpdelay)
12593 macro_build (NULL, "nop", "");
12594 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12595 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12596 load_delay_nop ();
12597 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12598 tempreg, BFD_RELOC_LO16);
12599 relax_end ();
12600
12601 if (breg != 0)
12602 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12603 tempreg, tempreg, breg);
12604 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12605 }
12606 else if (mips_big_got && HAVE_NEWABI)
12607 {
12608 /* If this is a reference to an external symbol, we want
12609 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12610 add $tempreg,$tempreg,$gp
12611 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12612 <op> op[0],<ofst>($tempreg)
12613 Otherwise, for local symbols, we want:
12614 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
12615 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
12616 gas_assert (offset_expr.X_op == O_symbol);
12617 expr1.X_add_number = offset_expr.X_add_number;
12618 offset_expr.X_add_number = 0;
12619 if (expr1.X_add_number < -0x8000
12620 || expr1.X_add_number >= 0x8000)
12621 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12622 relax_start (offset_expr.X_add_symbol);
12623 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12624 BFD_RELOC_MIPS_GOT_HI16);
12625 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12626 mips_gp_register);
12627 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12628 BFD_RELOC_MIPS_GOT_LO16, tempreg);
12629 if (breg != 0)
12630 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12631 tempreg, tempreg, breg);
12632 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12633
12634 relax_switch ();
12635 offset_expr.X_add_number = expr1.X_add_number;
12636 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12637 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12638 if (breg != 0)
12639 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12640 tempreg, tempreg, breg);
12641 macro_build (&offset_expr, s, fmt, op[0],
12642 BFD_RELOC_MIPS_GOT_OFST, tempreg);
12643 relax_end ();
12644 }
12645 else
12646 abort ();
12647
12648 break;
12649
12650 case M_JRADDIUSP:
12651 gas_assert (mips_opts.micromips);
12652 gas_assert (mips_opts.insn32);
12653 start_noreorder ();
12654 macro_build (NULL, "jr", "s", RA);
12655 expr1.X_add_number = op[0] << 2;
12656 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12657 end_noreorder ();
12658 break;
12659
12660 case M_JRC:
12661 gas_assert (mips_opts.micromips);
12662 gas_assert (mips_opts.insn32);
12663 macro_build (NULL, "jr", "s", op[0]);
12664 if (mips_opts.noreorder)
12665 macro_build (NULL, "nop", "");
12666 break;
12667
12668 case M_LI:
12669 case M_LI_S:
12670 load_register (op[0], &imm_expr, 0);
12671 break;
12672
12673 case M_DLI:
12674 load_register (op[0], &imm_expr, 1);
12675 break;
12676
12677 case M_LI_SS:
12678 if (imm_expr.X_op == O_constant)
12679 {
12680 used_at = 1;
12681 load_register (AT, &imm_expr, 0);
12682 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12683 break;
12684 }
12685 else
12686 {
12687 gas_assert (imm_expr.X_op == O_absent
12688 && offset_expr.X_op == O_symbol
12689 && strcmp (segment_name (S_GET_SEGMENT
12690 (offset_expr.X_add_symbol)),
12691 ".lit4") == 0
12692 && offset_expr.X_add_number == 0);
12693 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
12694 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
12695 break;
12696 }
12697
12698 case M_LI_D:
12699 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12700 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12701 order 32 bits of the value and the low order 32 bits are either
12702 zero or in OFFSET_EXPR. */
12703 if (imm_expr.X_op == O_constant)
12704 {
12705 if (GPR_SIZE == 64)
12706 load_register (op[0], &imm_expr, 1);
12707 else
12708 {
12709 int hreg, lreg;
12710
12711 if (target_big_endian)
12712 {
12713 hreg = op[0];
12714 lreg = op[0] + 1;
12715 }
12716 else
12717 {
12718 hreg = op[0] + 1;
12719 lreg = op[0];
12720 }
12721
12722 if (hreg <= 31)
12723 load_register (hreg, &imm_expr, 0);
12724 if (lreg <= 31)
12725 {
12726 if (offset_expr.X_op == O_absent)
12727 move_register (lreg, 0);
12728 else
12729 {
12730 gas_assert (offset_expr.X_op == O_constant);
12731 load_register (lreg, &offset_expr, 0);
12732 }
12733 }
12734 }
12735 break;
12736 }
12737 gas_assert (imm_expr.X_op == O_absent);
12738
12739 /* We know that sym is in the .rdata section. First we get the
12740 upper 16 bits of the address. */
12741 if (mips_pic == NO_PIC)
12742 {
12743 macro_build_lui (&offset_expr, AT);
12744 used_at = 1;
12745 }
12746 else
12747 {
12748 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12749 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12750 used_at = 1;
12751 }
12752
12753 /* Now we load the register(s). */
12754 if (GPR_SIZE == 64)
12755 {
12756 used_at = 1;
12757 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12758 BFD_RELOC_LO16, AT);
12759 }
12760 else
12761 {
12762 used_at = 1;
12763 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12764 BFD_RELOC_LO16, AT);
12765 if (op[0] != RA)
12766 {
12767 /* FIXME: How in the world do we deal with the possible
12768 overflow here? */
12769 offset_expr.X_add_number += 4;
12770 macro_build (&offset_expr, "lw", "t,o(b)",
12771 op[0] + 1, BFD_RELOC_LO16, AT);
12772 }
12773 }
12774 break;
12775
12776 case M_LI_DD:
12777 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12778 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12779 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12780 the value and the low order 32 bits are either zero or in
12781 OFFSET_EXPR. */
12782 if (imm_expr.X_op == O_constant)
12783 {
12784 tempreg = ZERO;
12785 if (((FPR_SIZE == 64 && GPR_SIZE == 64)
12786 || !ISA_HAS_MXHC1 (mips_opts.isa))
12787 && imm_expr.X_add_number != 0)
12788 {
12789 used_at = 1;
12790 tempreg = AT;
12791 load_register (AT, &imm_expr, FPR_SIZE == 64);
12792 }
12793 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12794 macro_build (NULL, "dmtc1", "t,S", tempreg, op[0]);
12795 else
12796 {
12797 if (!ISA_HAS_MXHC1 (mips_opts.isa))
12798 {
12799 if (FPR_SIZE != 32)
12800 as_bad (_("Unable to generate `%s' compliant code "
12801 "without mthc1"),
12802 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12803 else
12804 macro_build (NULL, "mtc1", "t,G", tempreg, op[0] + 1);
12805 }
12806 if (offset_expr.X_op == O_absent)
12807 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
12808 else
12809 {
12810 gas_assert (offset_expr.X_op == O_constant);
12811 load_register (AT, &offset_expr, 0);
12812 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12813 }
12814 if (ISA_HAS_MXHC1 (mips_opts.isa))
12815 {
12816 if (imm_expr.X_add_number != 0)
12817 {
12818 used_at = 1;
12819 tempreg = AT;
12820 load_register (AT, &imm_expr, 0);
12821 }
12822 macro_build (NULL, "mthc1", "t,G", tempreg, op[0]);
12823 }
12824 }
12825 break;
12826 }
12827
12828 gas_assert (imm_expr.X_op == O_absent
12829 && offset_expr.X_op == O_symbol
12830 && offset_expr.X_add_number == 0);
12831 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12832 if (strcmp (s, ".lit8") == 0)
12833 {
12834 op[2] = mips_gp_register;
12835 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12836 offset_reloc[1] = BFD_RELOC_UNUSED;
12837 offset_reloc[2] = BFD_RELOC_UNUSED;
12838 }
12839 else
12840 {
12841 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
12842 used_at = 1;
12843 if (mips_pic != NO_PIC)
12844 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12845 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12846 else
12847 {
12848 /* FIXME: This won't work for a 64 bit address. */
12849 macro_build_lui (&offset_expr, AT);
12850 }
12851
12852 op[2] = AT;
12853 offset_reloc[0] = BFD_RELOC_LO16;
12854 offset_reloc[1] = BFD_RELOC_UNUSED;
12855 offset_reloc[2] = BFD_RELOC_UNUSED;
12856 }
12857 align = 8;
12858 /* Fall through. */
12859
12860 case M_L_DAB:
12861 /* The MIPS assembler seems to check for X_add_number not
12862 being double aligned and generating:
12863 lui at,%hi(foo+1)
12864 addu at,at,v1
12865 addiu at,at,%lo(foo+1)
12866 lwc1 f2,0(at)
12867 lwc1 f3,4(at)
12868 But, the resulting address is the same after relocation so why
12869 generate the extra instruction? */
12870 /* Itbl support may require additional care here. */
12871 coproc = 1;
12872 fmt = "T,o(b)";
12873 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12874 {
12875 s = "ldc1";
12876 goto ld_st;
12877 }
12878 s = "lwc1";
12879 goto ldd_std;
12880
12881 case M_S_DAB:
12882 gas_assert (!mips_opts.micromips);
12883 /* Itbl support may require additional care here. */
12884 coproc = 1;
12885 fmt = "T,o(b)";
12886 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12887 {
12888 s = "sdc1";
12889 goto ld_st;
12890 }
12891 s = "swc1";
12892 goto ldd_std;
12893
12894 case M_LQ_AB:
12895 fmt = "t,o(b)";
12896 s = "lq";
12897 goto ld;
12898
12899 case M_SQ_AB:
12900 fmt = "t,o(b)";
12901 s = "sq";
12902 goto ld_st;
12903
12904 case M_LD_AB:
12905 fmt = "t,o(b)";
12906 if (GPR_SIZE == 64)
12907 {
12908 s = "ld";
12909 goto ld;
12910 }
12911 s = "lw";
12912 goto ldd_std;
12913
12914 case M_SD_AB:
12915 fmt = "t,o(b)";
12916 if (GPR_SIZE == 64)
12917 {
12918 s = "sd";
12919 goto ld_st;
12920 }
12921 s = "sw";
12922
12923 ldd_std:
12924 /* Even on a big endian machine $fn comes before $fn+1. We have
12925 to adjust when loading from memory. We set coproc if we must
12926 load $fn+1 first. */
12927 /* Itbl support may require additional care here. */
12928 if (!target_big_endian)
12929 coproc = 0;
12930
12931 breg = op[2];
12932 if (small_offset_p (0, align, 16))
12933 {
12934 ep = &offset_expr;
12935 if (!small_offset_p (4, align, 16))
12936 {
12937 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12938 -1, offset_reloc[0], offset_reloc[1],
12939 offset_reloc[2]);
12940 expr1.X_add_number = 0;
12941 ep = &expr1;
12942 breg = AT;
12943 used_at = 1;
12944 offset_reloc[0] = BFD_RELOC_LO16;
12945 offset_reloc[1] = BFD_RELOC_UNUSED;
12946 offset_reloc[2] = BFD_RELOC_UNUSED;
12947 }
12948 if (strcmp (s, "lw") == 0 && op[0] == breg)
12949 {
12950 ep->X_add_number += 4;
12951 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
12952 offset_reloc[1], offset_reloc[2], breg);
12953 ep->X_add_number -= 4;
12954 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
12955 offset_reloc[1], offset_reloc[2], breg);
12956 }
12957 else
12958 {
12959 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
12960 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12961 breg);
12962 ep->X_add_number += 4;
12963 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
12964 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12965 breg);
12966 }
12967 break;
12968 }
12969
12970 if (offset_expr.X_op != O_symbol
12971 && offset_expr.X_op != O_constant)
12972 {
12973 as_bad (_("expression too complex"));
12974 offset_expr.X_op = O_constant;
12975 }
12976
12977 if (HAVE_32BIT_ADDRESSES
12978 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12979 {
12980 char value [32];
12981
12982 sprintf_vma (value, offset_expr.X_add_number);
12983 as_bad (_("number (0x%s) larger than 32 bits"), value);
12984 }
12985
12986 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
12987 {
12988 /* If this is a reference to a GP relative symbol, we want
12989 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12990 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
12991 If we have a base register, we use this
12992 addu $at,$breg,$gp
12993 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12994 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
12995 If this is not a GP relative symbol, we want
12996 lui $at,<sym> (BFD_RELOC_HI16_S)
12997 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12998 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12999 If there is a base register, we add it to $at after the
13000 lui instruction. If there is a constant, we always use
13001 the last case. */
13002 if (offset_expr.X_op == O_symbol
13003 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
13004 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
13005 {
13006 relax_start (offset_expr.X_add_symbol);
13007 if (breg == 0)
13008 {
13009 tempreg = mips_gp_register;
13010 }
13011 else
13012 {
13013 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13014 AT, breg, mips_gp_register);
13015 tempreg = AT;
13016 used_at = 1;
13017 }
13018
13019 /* Itbl support may require additional care here. */
13020 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13021 BFD_RELOC_GPREL16, tempreg);
13022 offset_expr.X_add_number += 4;
13023
13024 /* Set mips_optimize to 2 to avoid inserting an
13025 undesired nop. */
13026 hold_mips_optimize = mips_optimize;
13027 mips_optimize = 2;
13028 /* Itbl support may require additional care here. */
13029 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13030 BFD_RELOC_GPREL16, tempreg);
13031 mips_optimize = hold_mips_optimize;
13032
13033 relax_switch ();
13034
13035 offset_expr.X_add_number -= 4;
13036 }
13037 used_at = 1;
13038 if (offset_high_part (offset_expr.X_add_number, 16)
13039 != offset_high_part (offset_expr.X_add_number + 4, 16))
13040 {
13041 load_address (AT, &offset_expr, &used_at);
13042 offset_expr.X_op = O_constant;
13043 offset_expr.X_add_number = 0;
13044 }
13045 else
13046 macro_build_lui (&offset_expr, AT);
13047 if (breg != 0)
13048 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13049 /* Itbl support may require additional care here. */
13050 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13051 BFD_RELOC_LO16, AT);
13052 /* FIXME: How do we handle overflow here? */
13053 offset_expr.X_add_number += 4;
13054 /* Itbl support may require additional care here. */
13055 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13056 BFD_RELOC_LO16, AT);
13057 if (mips_relax.sequence)
13058 relax_end ();
13059 }
13060 else if (!mips_big_got)
13061 {
13062 /* If this is a reference to an external symbol, we want
13063 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13064 nop
13065 <op> op[0],0($at)
13066 <op> op[0]+1,4($at)
13067 Otherwise we want
13068 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13069 nop
13070 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13071 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
13072 If there is a base register we add it to $at before the
13073 lwc1 instructions. If there is a constant we include it
13074 in the lwc1 instructions. */
13075 used_at = 1;
13076 expr1.X_add_number = offset_expr.X_add_number;
13077 if (expr1.X_add_number < -0x8000
13078 || expr1.X_add_number >= 0x8000 - 4)
13079 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
13080 load_got_offset (AT, &offset_expr);
13081 load_delay_nop ();
13082 if (breg != 0)
13083 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13084
13085 /* Set mips_optimize to 2 to avoid inserting an undesired
13086 nop. */
13087 hold_mips_optimize = mips_optimize;
13088 mips_optimize = 2;
13089
13090 /* Itbl support may require additional care here. */
13091 relax_start (offset_expr.X_add_symbol);
13092 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
13093 BFD_RELOC_LO16, AT);
13094 expr1.X_add_number += 4;
13095 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
13096 BFD_RELOC_LO16, AT);
13097 relax_switch ();
13098 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13099 BFD_RELOC_LO16, AT);
13100 offset_expr.X_add_number += 4;
13101 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13102 BFD_RELOC_LO16, AT);
13103 relax_end ();
13104
13105 mips_optimize = hold_mips_optimize;
13106 }
13107 else if (mips_big_got)
13108 {
13109 int gpdelay;
13110
13111 /* If this is a reference to an external symbol, we want
13112 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
13113 addu $at,$at,$gp
13114 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
13115 nop
13116 <op> op[0],0($at)
13117 <op> op[0]+1,4($at)
13118 Otherwise we want
13119 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13120 nop
13121 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13122 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
13123 If there is a base register we add it to $at before the
13124 lwc1 instructions. If there is a constant we include it
13125 in the lwc1 instructions. */
13126 used_at = 1;
13127 expr1.X_add_number = offset_expr.X_add_number;
13128 offset_expr.X_add_number = 0;
13129 if (expr1.X_add_number < -0x8000
13130 || expr1.X_add_number >= 0x8000 - 4)
13131 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
13132 gpdelay = reg_needs_delay (mips_gp_register);
13133 relax_start (offset_expr.X_add_symbol);
13134 macro_build (&offset_expr, "lui", LUI_FMT,
13135 AT, BFD_RELOC_MIPS_GOT_HI16);
13136 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13137 AT, AT, mips_gp_register);
13138 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
13139 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
13140 load_delay_nop ();
13141 if (breg != 0)
13142 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13143 /* Itbl support may require additional care here. */
13144 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
13145 BFD_RELOC_LO16, AT);
13146 expr1.X_add_number += 4;
13147
13148 /* Set mips_optimize to 2 to avoid inserting an undesired
13149 nop. */
13150 hold_mips_optimize = mips_optimize;
13151 mips_optimize = 2;
13152 /* Itbl support may require additional care here. */
13153 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
13154 BFD_RELOC_LO16, AT);
13155 mips_optimize = hold_mips_optimize;
13156 expr1.X_add_number -= 4;
13157
13158 relax_switch ();
13159 offset_expr.X_add_number = expr1.X_add_number;
13160 if (gpdelay)
13161 macro_build (NULL, "nop", "");
13162 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
13163 BFD_RELOC_MIPS_GOT16, mips_gp_register);
13164 load_delay_nop ();
13165 if (breg != 0)
13166 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13167 /* Itbl support may require additional care here. */
13168 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13169 BFD_RELOC_LO16, AT);
13170 offset_expr.X_add_number += 4;
13171
13172 /* Set mips_optimize to 2 to avoid inserting an undesired
13173 nop. */
13174 hold_mips_optimize = mips_optimize;
13175 mips_optimize = 2;
13176 /* Itbl support may require additional care here. */
13177 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13178 BFD_RELOC_LO16, AT);
13179 mips_optimize = hold_mips_optimize;
13180 relax_end ();
13181 }
13182 else
13183 abort ();
13184
13185 break;
13186
13187 case M_SAA_AB:
13188 s = "saa";
13189 goto saa_saad;
13190 case M_SAAD_AB:
13191 s = "saad";
13192 saa_saad:
13193 gas_assert (!mips_opts.micromips);
13194 offbits = 0;
13195 fmt = "t,(b)";
13196 goto ld_st;
13197
13198 /* New code added to support COPZ instructions.
13199 This code builds table entries out of the macros in mip_opcodes.
13200 R4000 uses interlocks to handle coproc delays.
13201 Other chips (like the R3000) require nops to be inserted for delays.
13202
13203 FIXME: Currently, we require that the user handle delays.
13204 In order to fill delay slots for non-interlocked chips,
13205 we must have a way to specify delays based on the coprocessor.
13206 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
13207 What are the side-effects of the cop instruction?
13208 What cache support might we have and what are its effects?
13209 Both coprocessor & memory require delays. how long???
13210 What registers are read/set/modified?
13211
13212 If an itbl is provided to interpret cop instructions,
13213 this knowledge can be encoded in the itbl spec. */
13214
13215 case M_COP0:
13216 s = "c0";
13217 goto copz;
13218 case M_COP1:
13219 s = "c1";
13220 goto copz;
13221 case M_COP2:
13222 s = "c2";
13223 goto copz;
13224 case M_COP3:
13225 s = "c3";
13226 copz:
13227 gas_assert (!mips_opts.micromips);
13228 /* For now we just do C (same as Cz). The parameter will be
13229 stored in insn_opcode by mips_ip. */
13230 macro_build (NULL, s, "C", (int) ip->insn_opcode);
13231 break;
13232
13233 case M_MOVE:
13234 move_register (op[0], op[1]);
13235 break;
13236
13237 case M_MOVEP:
13238 gas_assert (mips_opts.micromips);
13239 gas_assert (mips_opts.insn32);
13240 move_register (micromips_to_32_reg_h_map1[op[0]],
13241 micromips_to_32_reg_m_map[op[1]]);
13242 move_register (micromips_to_32_reg_h_map2[op[0]],
13243 micromips_to_32_reg_n_map[op[2]]);
13244 break;
13245
13246 case M_DMUL:
13247 dbl = 1;
13248 /* Fall through. */
13249 case M_MUL:
13250 if (mips_opts.arch == CPU_R5900)
13251 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
13252 op[2]);
13253 else
13254 {
13255 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
13256 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13257 }
13258 break;
13259
13260 case M_DMUL_I:
13261 dbl = 1;
13262 /* Fall through. */
13263 case M_MUL_I:
13264 /* The MIPS assembler some times generates shifts and adds. I'm
13265 not trying to be that fancy. GCC should do this for us
13266 anyway. */
13267 used_at = 1;
13268 load_register (AT, &imm_expr, dbl);
13269 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
13270 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13271 break;
13272
13273 case M_DMULO_I:
13274 dbl = 1;
13275 /* Fall through. */
13276 case M_MULO_I:
13277 imm = 1;
13278 goto do_mulo;
13279
13280 case M_DMULO:
13281 dbl = 1;
13282 /* Fall through. */
13283 case M_MULO:
13284 do_mulo:
13285 start_noreorder ();
13286 used_at = 1;
13287 if (imm)
13288 load_register (AT, &imm_expr, dbl);
13289 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13290 op[1], imm ? AT : op[2]);
13291 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13292 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
13293 macro_build (NULL, "mfhi", MFHL_FMT, AT);
13294 if (mips_trap)
13295 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
13296 else
13297 {
13298 if (mips_opts.micromips)
13299 micromips_label_expr (&label_expr);
13300 else
13301 label_expr.X_add_number = 8;
13302 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
13303 macro_build (NULL, "nop", "");
13304 macro_build (NULL, "break", BRK_FMT, 6);
13305 if (mips_opts.micromips)
13306 micromips_add_label ();
13307 }
13308 end_noreorder ();
13309 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13310 break;
13311
13312 case M_DMULOU_I:
13313 dbl = 1;
13314 /* Fall through. */
13315 case M_MULOU_I:
13316 imm = 1;
13317 goto do_mulou;
13318
13319 case M_DMULOU:
13320 dbl = 1;
13321 /* Fall through. */
13322 case M_MULOU:
13323 do_mulou:
13324 start_noreorder ();
13325 used_at = 1;
13326 if (imm)
13327 load_register (AT, &imm_expr, dbl);
13328 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
13329 op[1], imm ? AT : op[2]);
13330 macro_build (NULL, "mfhi", MFHL_FMT, AT);
13331 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13332 if (mips_trap)
13333 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
13334 else
13335 {
13336 if (mips_opts.micromips)
13337 micromips_label_expr (&label_expr);
13338 else
13339 label_expr.X_add_number = 8;
13340 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
13341 macro_build (NULL, "nop", "");
13342 macro_build (NULL, "break", BRK_FMT, 6);
13343 if (mips_opts.micromips)
13344 micromips_add_label ();
13345 }
13346 end_noreorder ();
13347 break;
13348
13349 case M_DROL:
13350 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13351 {
13352 if (op[0] == op[1])
13353 {
13354 tempreg = AT;
13355 used_at = 1;
13356 }
13357 else
13358 tempreg = op[0];
13359 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13360 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
13361 break;
13362 }
13363 used_at = 1;
13364 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13365 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13366 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13367 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13368 break;
13369
13370 case M_ROL:
13371 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13372 {
13373 if (op[0] == op[1])
13374 {
13375 tempreg = AT;
13376 used_at = 1;
13377 }
13378 else
13379 tempreg = op[0];
13380 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13381 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
13382 break;
13383 }
13384 used_at = 1;
13385 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13386 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13387 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13388 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13389 break;
13390
13391 case M_DROL_I:
13392 {
13393 unsigned int rot;
13394 const char *l;
13395 const char *rr;
13396
13397 rot = imm_expr.X_add_number & 0x3f;
13398 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13399 {
13400 rot = (64 - rot) & 0x3f;
13401 if (rot >= 32)
13402 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13403 else
13404 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13405 break;
13406 }
13407 if (rot == 0)
13408 {
13409 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13410 break;
13411 }
13412 l = (rot < 0x20) ? "dsll" : "dsll32";
13413 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
13414 rot &= 0x1f;
13415 used_at = 1;
13416 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13417 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13418 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13419 }
13420 break;
13421
13422 case M_ROL_I:
13423 {
13424 unsigned int rot;
13425
13426 rot = imm_expr.X_add_number & 0x1f;
13427 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13428 {
13429 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13430 (32 - rot) & 0x1f);
13431 break;
13432 }
13433 if (rot == 0)
13434 {
13435 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13436 break;
13437 }
13438 used_at = 1;
13439 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13440 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13441 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13442 }
13443 break;
13444
13445 case M_DROR:
13446 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13447 {
13448 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
13449 break;
13450 }
13451 used_at = 1;
13452 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13453 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13454 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13455 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13456 break;
13457
13458 case M_ROR:
13459 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13460 {
13461 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
13462 break;
13463 }
13464 used_at = 1;
13465 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13466 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13467 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13468 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13469 break;
13470
13471 case M_DROR_I:
13472 {
13473 unsigned int rot;
13474 const char *l;
13475 const char *rr;
13476
13477 rot = imm_expr.X_add_number & 0x3f;
13478 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13479 {
13480 if (rot >= 32)
13481 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13482 else
13483 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13484 break;
13485 }
13486 if (rot == 0)
13487 {
13488 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13489 break;
13490 }
13491 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
13492 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13493 rot &= 0x1f;
13494 used_at = 1;
13495 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13496 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13497 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13498 }
13499 break;
13500
13501 case M_ROR_I:
13502 {
13503 unsigned int rot;
13504
13505 rot = imm_expr.X_add_number & 0x1f;
13506 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13507 {
13508 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
13509 break;
13510 }
13511 if (rot == 0)
13512 {
13513 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13514 break;
13515 }
13516 used_at = 1;
13517 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13518 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13519 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13520 }
13521 break;
13522
13523 case M_SEQ:
13524 if (op[1] == 0)
13525 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13526 else if (op[2] == 0)
13527 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13528 else
13529 {
13530 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13531 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13532 }
13533 break;
13534
13535 case M_SEQ_I:
13536 if (imm_expr.X_add_number == 0)
13537 {
13538 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13539 break;
13540 }
13541 if (op[1] == 0)
13542 {
13543 as_warn (_("instruction %s: result is always false"),
13544 ip->insn_mo->name);
13545 move_register (op[0], 0);
13546 break;
13547 }
13548 if (CPU_HAS_SEQ (mips_opts.arch)
13549 && -512 <= imm_expr.X_add_number
13550 && imm_expr.X_add_number < 512)
13551 {
13552 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
13553 (int) imm_expr.X_add_number);
13554 break;
13555 }
13556 if (imm_expr.X_add_number >= 0
13557 && imm_expr.X_add_number < 0x10000)
13558 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
13559 else if (imm_expr.X_add_number > -0x8000
13560 && imm_expr.X_add_number < 0)
13561 {
13562 imm_expr.X_add_number = -imm_expr.X_add_number;
13563 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13564 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13565 }
13566 else if (CPU_HAS_SEQ (mips_opts.arch))
13567 {
13568 used_at = 1;
13569 load_register (AT, &imm_expr, GPR_SIZE == 64);
13570 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
13571 break;
13572 }
13573 else
13574 {
13575 load_register (AT, &imm_expr, GPR_SIZE == 64);
13576 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13577 used_at = 1;
13578 }
13579 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13580 break;
13581
13582 case M_SGE: /* X >= Y <==> not (X < Y) */
13583 s = "slt";
13584 goto sge;
13585 case M_SGEU:
13586 s = "sltu";
13587 sge:
13588 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13589 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13590 break;
13591
13592 case M_SGE_I: /* X >= I <==> not (X < I). */
13593 case M_SGEU_I:
13594 if (imm_expr.X_add_number >= -0x8000
13595 && imm_expr.X_add_number < 0x8000)
13596 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13597 op[0], op[1], BFD_RELOC_LO16);
13598 else
13599 {
13600 load_register (AT, &imm_expr, GPR_SIZE == 64);
13601 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
13602 op[0], op[1], AT);
13603 used_at = 1;
13604 }
13605 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13606 break;
13607
13608 case M_SGT: /* X > Y <==> Y < X. */
13609 s = "slt";
13610 goto sgt;
13611 case M_SGTU:
13612 s = "sltu";
13613 sgt:
13614 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13615 break;
13616
13617 case M_SGT_I: /* X > I <==> I < X. */
13618 s = "slt";
13619 goto sgti;
13620 case M_SGTU_I:
13621 s = "sltu";
13622 sgti:
13623 used_at = 1;
13624 load_register (AT, &imm_expr, GPR_SIZE == 64);
13625 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13626 break;
13627
13628 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X). */
13629 s = "slt";
13630 goto sle;
13631 case M_SLEU:
13632 s = "sltu";
13633 sle:
13634 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13635 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13636 break;
13637
13638 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
13639 s = "slt";
13640 goto slei;
13641 case M_SLEU_I:
13642 s = "sltu";
13643 slei:
13644 used_at = 1;
13645 load_register (AT, &imm_expr, GPR_SIZE == 64);
13646 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13647 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13648 break;
13649
13650 case M_SLT_I:
13651 if (imm_expr.X_add_number >= -0x8000
13652 && imm_expr.X_add_number < 0x8000)
13653 {
13654 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13655 BFD_RELOC_LO16);
13656 break;
13657 }
13658 used_at = 1;
13659 load_register (AT, &imm_expr, GPR_SIZE == 64);
13660 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
13661 break;
13662
13663 case M_SLTU_I:
13664 if (imm_expr.X_add_number >= -0x8000
13665 && imm_expr.X_add_number < 0x8000)
13666 {
13667 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
13668 BFD_RELOC_LO16);
13669 break;
13670 }
13671 used_at = 1;
13672 load_register (AT, &imm_expr, GPR_SIZE == 64);
13673 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
13674 break;
13675
13676 case M_SNE:
13677 if (op[1] == 0)
13678 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13679 else if (op[2] == 0)
13680 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13681 else
13682 {
13683 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13684 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13685 }
13686 break;
13687
13688 case M_SNE_I:
13689 if (imm_expr.X_add_number == 0)
13690 {
13691 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13692 break;
13693 }
13694 if (op[1] == 0)
13695 {
13696 as_warn (_("instruction %s: result is always true"),
13697 ip->insn_mo->name);
13698 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
13699 op[0], 0, BFD_RELOC_LO16);
13700 break;
13701 }
13702 if (CPU_HAS_SEQ (mips_opts.arch)
13703 && -512 <= imm_expr.X_add_number
13704 && imm_expr.X_add_number < 512)
13705 {
13706 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
13707 (int) imm_expr.X_add_number);
13708 break;
13709 }
13710 if (imm_expr.X_add_number >= 0
13711 && imm_expr.X_add_number < 0x10000)
13712 {
13713 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13714 BFD_RELOC_LO16);
13715 }
13716 else if (imm_expr.X_add_number > -0x8000
13717 && imm_expr.X_add_number < 0)
13718 {
13719 imm_expr.X_add_number = -imm_expr.X_add_number;
13720 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13721 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13722 }
13723 else if (CPU_HAS_SEQ (mips_opts.arch))
13724 {
13725 used_at = 1;
13726 load_register (AT, &imm_expr, GPR_SIZE == 64);
13727 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
13728 break;
13729 }
13730 else
13731 {
13732 load_register (AT, &imm_expr, GPR_SIZE == 64);
13733 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13734 used_at = 1;
13735 }
13736 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13737 break;
13738
13739 case M_SUB_I:
13740 s = "addi";
13741 s2 = "sub";
13742 if (ISA_IS_R6 (mips_opts.isa))
13743 goto do_subi_i;
13744 else
13745 goto do_subi;
13746 case M_SUBU_I:
13747 s = "addiu";
13748 s2 = "subu";
13749 goto do_subi;
13750 case M_DSUB_I:
13751 dbl = 1;
13752 s = "daddi";
13753 s2 = "dsub";
13754 if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
13755 goto do_subi;
13756 if (imm_expr.X_add_number > -0x200
13757 && imm_expr.X_add_number <= 0x200
13758 && !ISA_IS_R6 (mips_opts.isa))
13759 {
13760 macro_build (NULL, s, "t,r,.", op[0], op[1],
13761 (int) -imm_expr.X_add_number);
13762 break;
13763 }
13764 goto do_subi_i;
13765 case M_DSUBU_I:
13766 dbl = 1;
13767 s = "daddiu";
13768 s2 = "dsubu";
13769 do_subi:
13770 if (imm_expr.X_add_number > -0x8000
13771 && imm_expr.X_add_number <= 0x8000)
13772 {
13773 imm_expr.X_add_number = -imm_expr.X_add_number;
13774 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13775 break;
13776 }
13777 do_subi_i:
13778 used_at = 1;
13779 load_register (AT, &imm_expr, dbl);
13780 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
13781 break;
13782
13783 case M_TEQ_I:
13784 s = "teq";
13785 goto trap;
13786 case M_TGE_I:
13787 s = "tge";
13788 goto trap;
13789 case M_TGEU_I:
13790 s = "tgeu";
13791 goto trap;
13792 case M_TLT_I:
13793 s = "tlt";
13794 goto trap;
13795 case M_TLTU_I:
13796 s = "tltu";
13797 goto trap;
13798 case M_TNE_I:
13799 s = "tne";
13800 trap:
13801 used_at = 1;
13802 load_register (AT, &imm_expr, GPR_SIZE == 64);
13803 macro_build (NULL, s, "s,t", op[0], AT);
13804 break;
13805
13806 case M_TRUNCWS:
13807 case M_TRUNCWD:
13808 gas_assert (!mips_opts.micromips);
13809 gas_assert (mips_opts.isa == ISA_MIPS1);
13810 used_at = 1;
13811
13812 /*
13813 * Is the double cfc1 instruction a bug in the mips assembler;
13814 * or is there a reason for it?
13815 */
13816 start_noreorder ();
13817 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13818 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13819 macro_build (NULL, "nop", "");
13820 expr1.X_add_number = 3;
13821 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
13822 expr1.X_add_number = 2;
13823 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13824 macro_build (NULL, "ctc1", "t,G", AT, RA);
13825 macro_build (NULL, "nop", "");
13826 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
13827 op[0], op[1]);
13828 macro_build (NULL, "ctc1", "t,G", op[2], RA);
13829 macro_build (NULL, "nop", "");
13830 end_noreorder ();
13831 break;
13832
13833 case M_ULH_AB:
13834 s = "lb";
13835 s2 = "lbu";
13836 off = 1;
13837 goto uld_st;
13838 case M_ULHU_AB:
13839 s = "lbu";
13840 s2 = "lbu";
13841 off = 1;
13842 goto uld_st;
13843 case M_ULW_AB:
13844 s = "lwl";
13845 s2 = "lwr";
13846 offbits = (mips_opts.micromips ? 12 : 16);
13847 off = 3;
13848 goto uld_st;
13849 case M_ULD_AB:
13850 s = "ldl";
13851 s2 = "ldr";
13852 offbits = (mips_opts.micromips ? 12 : 16);
13853 off = 7;
13854 goto uld_st;
13855 case M_USH_AB:
13856 s = "sb";
13857 s2 = "sb";
13858 off = 1;
13859 ust = 1;
13860 goto uld_st;
13861 case M_USW_AB:
13862 s = "swl";
13863 s2 = "swr";
13864 offbits = (mips_opts.micromips ? 12 : 16);
13865 off = 3;
13866 ust = 1;
13867 goto uld_st;
13868 case M_USD_AB:
13869 s = "sdl";
13870 s2 = "sdr";
13871 offbits = (mips_opts.micromips ? 12 : 16);
13872 off = 7;
13873 ust = 1;
13874
13875 uld_st:
13876 breg = op[2];
13877 large_offset = !small_offset_p (off, align, offbits);
13878 ep = &offset_expr;
13879 expr1.X_add_number = 0;
13880 if (large_offset)
13881 {
13882 used_at = 1;
13883 tempreg = AT;
13884 if (small_offset_p (0, align, 16))
13885 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13886 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13887 else
13888 {
13889 load_address (tempreg, ep, &used_at);
13890 if (breg != 0)
13891 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13892 tempreg, tempreg, breg);
13893 }
13894 offset_reloc[0] = BFD_RELOC_LO16;
13895 offset_reloc[1] = BFD_RELOC_UNUSED;
13896 offset_reloc[2] = BFD_RELOC_UNUSED;
13897 breg = tempreg;
13898 tempreg = op[0];
13899 ep = &expr1;
13900 }
13901 else if (!ust && op[0] == breg)
13902 {
13903 used_at = 1;
13904 tempreg = AT;
13905 }
13906 else
13907 tempreg = op[0];
13908
13909 if (off == 1)
13910 goto ulh_sh;
13911
13912 if (!target_big_endian)
13913 ep->X_add_number += off;
13914 if (offbits == 12)
13915 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
13916 else
13917 macro_build (ep, s, "t,o(b)", tempreg, -1,
13918 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13919
13920 if (!target_big_endian)
13921 ep->X_add_number -= off;
13922 else
13923 ep->X_add_number += off;
13924 if (offbits == 12)
13925 macro_build (NULL, s2, "t,~(b)",
13926 tempreg, (int) ep->X_add_number, breg);
13927 else
13928 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13929 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13930
13931 /* If necessary, move the result in tempreg to the final destination. */
13932 if (!ust && op[0] != tempreg)
13933 {
13934 /* Protect second load's delay slot. */
13935 load_delay_nop ();
13936 move_register (op[0], tempreg);
13937 }
13938 break;
13939
13940 ulh_sh:
13941 used_at = 1;
13942 if (target_big_endian == ust)
13943 ep->X_add_number += off;
13944 tempreg = ust || large_offset ? op[0] : AT;
13945 macro_build (ep, s, "t,o(b)", tempreg, -1,
13946 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13947
13948 /* For halfword transfers we need a temporary register to shuffle
13949 bytes. Unfortunately for M_USH_A we have none available before
13950 the next store as AT holds the base address. We deal with this
13951 case by clobbering TREG and then restoring it as with ULH. */
13952 tempreg = ust == large_offset ? op[0] : AT;
13953 if (ust)
13954 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
13955
13956 if (target_big_endian == ust)
13957 ep->X_add_number -= off;
13958 else
13959 ep->X_add_number += off;
13960 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13961 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13962
13963 /* For M_USH_A re-retrieve the LSB. */
13964 if (ust && large_offset)
13965 {
13966 if (target_big_endian)
13967 ep->X_add_number += off;
13968 else
13969 ep->X_add_number -= off;
13970 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13971 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
13972 }
13973 /* For ULH and M_USH_A OR the LSB in. */
13974 if (!ust || large_offset)
13975 {
13976 tempreg = !large_offset ? AT : op[0];
13977 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
13978 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13979 }
13980 break;
13981
13982 default:
13983 /* FIXME: Check if this is one of the itbl macros, since they
13984 are added dynamically. */
13985 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
13986 break;
13987 }
13988 if (!mips_opts.at && used_at)
13989 as_bad (_("macro used $at after \".set noat\""));
13990 }
13991
13992 /* Implement macros in mips16 mode. */
13993
13994 static void
13995 mips16_macro (struct mips_cl_insn *ip)
13996 {
13997 const struct mips_operand_array *operands;
13998 int mask;
13999 int tmp;
14000 expressionS expr1;
14001 int dbl;
14002 const char *s, *s2, *s3;
14003 unsigned int op[MAX_OPERANDS];
14004 unsigned int i;
14005
14006 mask = ip->insn_mo->mask;
14007
14008 operands = insn_operands (ip);
14009 for (i = 0; i < MAX_OPERANDS; i++)
14010 if (operands->operand[i])
14011 op[i] = insn_extract_operand (ip, operands->operand[i]);
14012 else
14013 op[i] = -1;
14014
14015 expr1.X_op = O_constant;
14016 expr1.X_op_symbol = NULL;
14017 expr1.X_add_symbol = NULL;
14018 expr1.X_add_number = 1;
14019
14020 dbl = 0;
14021
14022 switch (mask)
14023 {
14024 default:
14025 abort ();
14026
14027 case M_DDIV_3:
14028 dbl = 1;
14029 /* Fall through. */
14030 case M_DIV_3:
14031 s = "mflo";
14032 goto do_div3;
14033 case M_DREM_3:
14034 dbl = 1;
14035 /* Fall through. */
14036 case M_REM_3:
14037 s = "mfhi";
14038 do_div3:
14039 start_noreorder ();
14040 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
14041 expr1.X_add_number = 2;
14042 macro_build (&expr1, "bnez", "x,p", op[2]);
14043 macro_build (NULL, "break", "6", 7);
14044
14045 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
14046 since that causes an overflow. We should do that as well,
14047 but I don't see how to do the comparisons without a temporary
14048 register. */
14049 end_noreorder ();
14050 macro_build (NULL, s, "x", op[0]);
14051 break;
14052
14053 case M_DIVU_3:
14054 s = "divu";
14055 s2 = "mflo";
14056 goto do_divu3;
14057 case M_REMU_3:
14058 s = "divu";
14059 s2 = "mfhi";
14060 goto do_divu3;
14061 case M_DDIVU_3:
14062 s = "ddivu";
14063 s2 = "mflo";
14064 goto do_divu3;
14065 case M_DREMU_3:
14066 s = "ddivu";
14067 s2 = "mfhi";
14068 do_divu3:
14069 start_noreorder ();
14070 macro_build (NULL, s, ".,x,y", op[1], op[2]);
14071 expr1.X_add_number = 2;
14072 macro_build (&expr1, "bnez", "x,p", op[2]);
14073 macro_build (NULL, "break", "6", 7);
14074 end_noreorder ();
14075 macro_build (NULL, s2, "x", op[0]);
14076 break;
14077
14078 case M_DMUL:
14079 dbl = 1;
14080 /* Fall through. */
14081 case M_MUL:
14082 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
14083 macro_build (NULL, "mflo", "x", op[0]);
14084 break;
14085
14086 case M_DSUBU_I:
14087 dbl = 1;
14088 goto do_subu;
14089 case M_SUBU_I:
14090 do_subu:
14091 imm_expr.X_add_number = -imm_expr.X_add_number;
14092 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
14093 break;
14094
14095 case M_SUBU_I_2:
14096 imm_expr.X_add_number = -imm_expr.X_add_number;
14097 macro_build (&imm_expr, "addiu", "x,k", op[0]);
14098 break;
14099
14100 case M_DSUBU_I_2:
14101 imm_expr.X_add_number = -imm_expr.X_add_number;
14102 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
14103 break;
14104
14105 case M_BEQ:
14106 s = "cmp";
14107 s2 = "bteqz";
14108 goto do_branch;
14109 case M_BNE:
14110 s = "cmp";
14111 s2 = "btnez";
14112 goto do_branch;
14113 case M_BLT:
14114 s = "slt";
14115 s2 = "btnez";
14116 goto do_branch;
14117 case M_BLTU:
14118 s = "sltu";
14119 s2 = "btnez";
14120 goto do_branch;
14121 case M_BLE:
14122 s = "slt";
14123 s2 = "bteqz";
14124 goto do_reverse_branch;
14125 case M_BLEU:
14126 s = "sltu";
14127 s2 = "bteqz";
14128 goto do_reverse_branch;
14129 case M_BGE:
14130 s = "slt";
14131 s2 = "bteqz";
14132 goto do_branch;
14133 case M_BGEU:
14134 s = "sltu";
14135 s2 = "bteqz";
14136 goto do_branch;
14137 case M_BGT:
14138 s = "slt";
14139 s2 = "btnez";
14140 goto do_reverse_branch;
14141 case M_BGTU:
14142 s = "sltu";
14143 s2 = "btnez";
14144
14145 do_reverse_branch:
14146 tmp = op[1];
14147 op[1] = op[0];
14148 op[0] = tmp;
14149
14150 do_branch:
14151 macro_build (NULL, s, "x,y", op[0], op[1]);
14152 macro_build (&offset_expr, s2, "p");
14153 break;
14154
14155 case M_BEQ_I:
14156 s = "cmpi";
14157 s2 = "bteqz";
14158 s3 = "x,U";
14159 goto do_branch_i;
14160 case M_BNE_I:
14161 s = "cmpi";
14162 s2 = "btnez";
14163 s3 = "x,U";
14164 goto do_branch_i;
14165 case M_BLT_I:
14166 s = "slti";
14167 s2 = "btnez";
14168 s3 = "x,8";
14169 goto do_branch_i;
14170 case M_BLTU_I:
14171 s = "sltiu";
14172 s2 = "btnez";
14173 s3 = "x,8";
14174 goto do_branch_i;
14175 case M_BLE_I:
14176 s = "slti";
14177 s2 = "btnez";
14178 s3 = "x,8";
14179 goto do_addone_branch_i;
14180 case M_BLEU_I:
14181 s = "sltiu";
14182 s2 = "btnez";
14183 s3 = "x,8";
14184 goto do_addone_branch_i;
14185 case M_BGE_I:
14186 s = "slti";
14187 s2 = "bteqz";
14188 s3 = "x,8";
14189 goto do_branch_i;
14190 case M_BGEU_I:
14191 s = "sltiu";
14192 s2 = "bteqz";
14193 s3 = "x,8";
14194 goto do_branch_i;
14195 case M_BGT_I:
14196 s = "slti";
14197 s2 = "bteqz";
14198 s3 = "x,8";
14199 goto do_addone_branch_i;
14200 case M_BGTU_I:
14201 s = "sltiu";
14202 s2 = "bteqz";
14203 s3 = "x,8";
14204
14205 do_addone_branch_i:
14206 ++imm_expr.X_add_number;
14207
14208 do_branch_i:
14209 macro_build (&imm_expr, s, s3, op[0]);
14210 macro_build (&offset_expr, s2, "p");
14211 break;
14212
14213 case M_ABS:
14214 expr1.X_add_number = 0;
14215 macro_build (&expr1, "slti", "x,8", op[1]);
14216 if (op[0] != op[1])
14217 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
14218 expr1.X_add_number = 2;
14219 macro_build (&expr1, "bteqz", "p");
14220 macro_build (NULL, "neg", "x,w", op[0], op[0]);
14221 break;
14222 }
14223 }
14224
14225 /* Look up instruction [START, START + LENGTH) in HASH. Record any extra
14226 opcode bits in *OPCODE_EXTRA. */
14227
14228 static struct mips_opcode *
14229 mips_lookup_insn (struct hash_control *hash, const char *start,
14230 ssize_t length, unsigned int *opcode_extra)
14231 {
14232 char *name, *dot, *p;
14233 unsigned int mask, suffix;
14234 ssize_t opend;
14235 struct mips_opcode *insn;
14236
14237 /* Make a copy of the instruction so that we can fiddle with it. */
14238 name = xstrndup (start, length);
14239
14240 /* Look up the instruction as-is. */
14241 insn = (struct mips_opcode *) hash_find (hash, name);
14242 if (insn)
14243 goto end;
14244
14245 dot = strchr (name, '.');
14246 if (dot && dot[1])
14247 {
14248 /* Try to interpret the text after the dot as a VU0 channel suffix. */
14249 p = mips_parse_vu0_channels (dot + 1, &mask);
14250 if (*p == 0 && mask != 0)
14251 {
14252 *dot = 0;
14253 insn = (struct mips_opcode *) hash_find (hash, name);
14254 *dot = '.';
14255 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
14256 {
14257 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
14258 goto end;
14259 }
14260 }
14261 }
14262
14263 if (mips_opts.micromips)
14264 {
14265 /* See if there's an instruction size override suffix,
14266 either `16' or `32', at the end of the mnemonic proper,
14267 that defines the operation, i.e. before the first `.'
14268 character if any. Strip it and retry. */
14269 opend = dot != NULL ? dot - name : length;
14270 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
14271 suffix = 2;
14272 else if (opend >= 2 && name[opend - 2] == '3' && name[opend - 1] == '2')
14273 suffix = 4;
14274 else
14275 suffix = 0;
14276 if (suffix)
14277 {
14278 memmove (name + opend - 2, name + opend, length - opend + 1);
14279 insn = (struct mips_opcode *) hash_find (hash, name);
14280 if (insn)
14281 {
14282 forced_insn_length = suffix;
14283 goto end;
14284 }
14285 }
14286 }
14287
14288 insn = NULL;
14289 end:
14290 free (name);
14291 return insn;
14292 }
14293
14294 /* Assemble an instruction into its binary format. If the instruction
14295 is a macro, set imm_expr and offset_expr to the values associated
14296 with "I" and "A" operands respectively. Otherwise store the value
14297 of the relocatable field (if any) in offset_expr. In both cases
14298 set offset_reloc to the relocation operators applied to offset_expr. */
14299
14300 static void
14301 mips_ip (char *str, struct mips_cl_insn *insn)
14302 {
14303 const struct mips_opcode *first, *past;
14304 struct hash_control *hash;
14305 char format;
14306 size_t end;
14307 struct mips_operand_token *tokens;
14308 unsigned int opcode_extra;
14309
14310 if (mips_opts.micromips)
14311 {
14312 hash = micromips_op_hash;
14313 past = &micromips_opcodes[bfd_micromips_num_opcodes];
14314 }
14315 else
14316 {
14317 hash = op_hash;
14318 past = &mips_opcodes[NUMOPCODES];
14319 }
14320 forced_insn_length = 0;
14321 opcode_extra = 0;
14322
14323 /* We first try to match an instruction up to a space or to the end. */
14324 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14325 continue;
14326
14327 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14328 if (first == NULL)
14329 {
14330 set_insn_error (0, _("unrecognized opcode"));
14331 return;
14332 }
14333
14334 if (strcmp (first->name, "li.s") == 0)
14335 format = 'f';
14336 else if (strcmp (first->name, "li.d") == 0)
14337 format = 'd';
14338 else
14339 format = 0;
14340 tokens = mips_parse_arguments (str + end, format);
14341 if (!tokens)
14342 return;
14343
14344 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
14345 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
14346 set_insn_error (0, _("invalid operands"));
14347
14348 obstack_free (&mips_operand_tokens, tokens);
14349 }
14350
14351 /* As for mips_ip, but used when assembling MIPS16 code.
14352 Also set forced_insn_length to the resulting instruction size in
14353 bytes if the user explicitly requested a small or extended instruction. */
14354
14355 static void
14356 mips16_ip (char *str, struct mips_cl_insn *insn)
14357 {
14358 char *end, *s, c;
14359 struct mips_opcode *first;
14360 struct mips_operand_token *tokens;
14361 unsigned int l;
14362
14363 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
14364 ;
14365 end = s;
14366 c = *end;
14367
14368 l = 0;
14369 switch (c)
14370 {
14371 case '\0':
14372 break;
14373
14374 case ' ':
14375 s++;
14376 break;
14377
14378 case '.':
14379 s++;
14380 if (*s == 't')
14381 {
14382 l = 2;
14383 s++;
14384 }
14385 else if (*s == 'e')
14386 {
14387 l = 4;
14388 s++;
14389 }
14390 if (*s == '\0')
14391 break;
14392 else if (*s++ == ' ')
14393 break;
14394 set_insn_error (0, _("unrecognized opcode"));
14395 return;
14396 }
14397 forced_insn_length = l;
14398
14399 *end = 0;
14400 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
14401 *end = c;
14402
14403 if (!first)
14404 {
14405 set_insn_error (0, _("unrecognized opcode"));
14406 return;
14407 }
14408
14409 tokens = mips_parse_arguments (s, 0);
14410 if (!tokens)
14411 return;
14412
14413 if (!match_mips16_insns (insn, first, tokens))
14414 set_insn_error (0, _("invalid operands"));
14415
14416 obstack_free (&mips_operand_tokens, tokens);
14417 }
14418
14419 /* Marshal immediate value VAL for an extended MIPS16 instruction.
14420 NBITS is the number of significant bits in VAL. */
14421
14422 static unsigned long
14423 mips16_immed_extend (offsetT val, unsigned int nbits)
14424 {
14425 int extval;
14426
14427 extval = 0;
14428 val &= (1U << nbits) - 1;
14429 if (nbits == 16 || nbits == 9)
14430 {
14431 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14432 val &= 0x1f;
14433 }
14434 else if (nbits == 15)
14435 {
14436 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14437 val &= 0xf;
14438 }
14439 else if (nbits == 6)
14440 {
14441 extval = ((val & 0x1f) << 6) | (val & 0x20);
14442 val = 0;
14443 }
14444 return (extval << 16) | val;
14445 }
14446
14447 /* Like decode_mips16_operand, but require the operand to be defined and
14448 require it to be an integer. */
14449
14450 static const struct mips_int_operand *
14451 mips16_immed_operand (int type, bfd_boolean extended_p)
14452 {
14453 const struct mips_operand *operand;
14454
14455 operand = decode_mips16_operand (type, extended_p);
14456 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14457 abort ();
14458 return (const struct mips_int_operand *) operand;
14459 }
14460
14461 /* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14462
14463 static bfd_boolean
14464 mips16_immed_in_range_p (const struct mips_int_operand *operand,
14465 bfd_reloc_code_real_type reloc, offsetT sval)
14466 {
14467 int min_val, max_val;
14468
14469 min_val = mips_int_operand_min (operand);
14470 max_val = mips_int_operand_max (operand);
14471 if (reloc != BFD_RELOC_UNUSED)
14472 {
14473 if (min_val < 0)
14474 sval = SEXT_16BIT (sval);
14475 else
14476 sval &= 0xffff;
14477 }
14478
14479 return (sval >= min_val
14480 && sval <= max_val
14481 && (sval & ((1 << operand->shift) - 1)) == 0);
14482 }
14483
14484 /* Install immediate value VAL into MIPS16 instruction *INSN,
14485 extending it if necessary. The instruction in *INSN may
14486 already be extended.
14487
14488 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14489 if none. In the former case, VAL is a 16-bit number with no
14490 defined signedness.
14491
14492 TYPE is the type of the immediate field. USER_INSN_LENGTH
14493 is the length that the user requested, or 0 if none. */
14494
14495 static void
14496 mips16_immed (const char *file, unsigned int line, int type,
14497 bfd_reloc_code_real_type reloc, offsetT val,
14498 unsigned int user_insn_length, unsigned long *insn)
14499 {
14500 const struct mips_int_operand *operand;
14501 unsigned int uval, length;
14502
14503 operand = mips16_immed_operand (type, FALSE);
14504 if (!mips16_immed_in_range_p (operand, reloc, val))
14505 {
14506 /* We need an extended instruction. */
14507 if (user_insn_length == 2)
14508 as_bad_where (file, line, _("invalid unextended operand value"));
14509 else
14510 *insn |= MIPS16_EXTEND;
14511 }
14512 else if (user_insn_length == 4)
14513 {
14514 /* The operand doesn't force an unextended instruction to be extended.
14515 Warn if the user wanted an extended instruction anyway. */
14516 *insn |= MIPS16_EXTEND;
14517 as_warn_where (file, line,
14518 _("extended operand requested but not required"));
14519 }
14520
14521 length = mips16_opcode_length (*insn);
14522 if (length == 4)
14523 {
14524 operand = mips16_immed_operand (type, TRUE);
14525 if (!mips16_immed_in_range_p (operand, reloc, val))
14526 as_bad_where (file, line,
14527 _("operand value out of range for instruction"));
14528 }
14529 uval = ((unsigned int) val >> operand->shift) - operand->bias;
14530 if (length == 2 || operand->root.lsb != 0)
14531 *insn = mips_insert_operand (&operand->root, *insn, uval);
14532 else
14533 *insn |= mips16_immed_extend (uval, operand->root.size);
14534 }
14535 \f
14536 struct percent_op_match
14537 {
14538 const char *str;
14539 bfd_reloc_code_real_type reloc;
14540 };
14541
14542 static const struct percent_op_match mips_percent_op[] =
14543 {
14544 {"%lo", BFD_RELOC_LO16},
14545 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14546 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14547 {"%call16", BFD_RELOC_MIPS_CALL16},
14548 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14549 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14550 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14551 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14552 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14553 {"%got", BFD_RELOC_MIPS_GOT16},
14554 {"%gp_rel", BFD_RELOC_GPREL16},
14555 {"%gprel", BFD_RELOC_GPREL16},
14556 {"%half", BFD_RELOC_16},
14557 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14558 {"%higher", BFD_RELOC_MIPS_HIGHER},
14559 {"%neg", BFD_RELOC_MIPS_SUB},
14560 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14561 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14562 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14563 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14564 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14565 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14566 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14567 {"%hi", BFD_RELOC_HI16_S},
14568 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14569 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
14570 };
14571
14572 static const struct percent_op_match mips16_percent_op[] =
14573 {
14574 {"%lo", BFD_RELOC_MIPS16_LO16},
14575 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
14576 {"%gprel", BFD_RELOC_MIPS16_GPREL},
14577 {"%got", BFD_RELOC_MIPS16_GOT16},
14578 {"%call16", BFD_RELOC_MIPS16_CALL16},
14579 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14580 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14581 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14582 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14583 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14584 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14585 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14586 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14587 };
14588
14589
14590 /* Return true if *STR points to a relocation operator. When returning true,
14591 move *STR over the operator and store its relocation code in *RELOC.
14592 Leave both *STR and *RELOC alone when returning false. */
14593
14594 static bfd_boolean
14595 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14596 {
14597 const struct percent_op_match *percent_op;
14598 size_t limit, i;
14599
14600 if (mips_opts.mips16)
14601 {
14602 percent_op = mips16_percent_op;
14603 limit = ARRAY_SIZE (mips16_percent_op);
14604 }
14605 else
14606 {
14607 percent_op = mips_percent_op;
14608 limit = ARRAY_SIZE (mips_percent_op);
14609 }
14610
14611 for (i = 0; i < limit; i++)
14612 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14613 {
14614 int len = strlen (percent_op[i].str);
14615
14616 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14617 continue;
14618
14619 *str += strlen (percent_op[i].str);
14620 *reloc = percent_op[i].reloc;
14621
14622 /* Check whether the output BFD supports this relocation.
14623 If not, issue an error and fall back on something safe. */
14624 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14625 {
14626 as_bad (_("relocation %s isn't supported by the current ABI"),
14627 percent_op[i].str);
14628 *reloc = BFD_RELOC_UNUSED;
14629 }
14630 return TRUE;
14631 }
14632 return FALSE;
14633 }
14634
14635
14636 /* Parse string STR as a 16-bit relocatable operand. Store the
14637 expression in *EP and the relocations in the array starting
14638 at RELOC. Return the number of relocation operators used.
14639
14640 On exit, EXPR_END points to the first character after the expression. */
14641
14642 static size_t
14643 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14644 char *str)
14645 {
14646 bfd_reloc_code_real_type reversed_reloc[3];
14647 size_t reloc_index, i;
14648 int crux_depth, str_depth;
14649 char *crux;
14650
14651 /* Search for the start of the main expression, recoding relocations
14652 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14653 of the main expression and with CRUX_DEPTH containing the number
14654 of open brackets at that point. */
14655 reloc_index = -1;
14656 str_depth = 0;
14657 do
14658 {
14659 reloc_index++;
14660 crux = str;
14661 crux_depth = str_depth;
14662
14663 /* Skip over whitespace and brackets, keeping count of the number
14664 of brackets. */
14665 while (*str == ' ' || *str == '\t' || *str == '(')
14666 if (*str++ == '(')
14667 str_depth++;
14668 }
14669 while (*str == '%'
14670 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14671 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14672
14673 my_getExpression (ep, crux);
14674 str = expr_end;
14675
14676 /* Match every open bracket. */
14677 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14678 if (*str++ == ')')
14679 crux_depth--;
14680
14681 if (crux_depth > 0)
14682 as_bad (_("unclosed '('"));
14683
14684 expr_end = str;
14685
14686 if (reloc_index != 0)
14687 {
14688 prev_reloc_op_frag = frag_now;
14689 for (i = 0; i < reloc_index; i++)
14690 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14691 }
14692
14693 return reloc_index;
14694 }
14695
14696 static void
14697 my_getExpression (expressionS *ep, char *str)
14698 {
14699 char *save_in;
14700
14701 save_in = input_line_pointer;
14702 input_line_pointer = str;
14703 expression (ep);
14704 expr_end = input_line_pointer;
14705 input_line_pointer = save_in;
14706 }
14707
14708 const char *
14709 md_atof (int type, char *litP, int *sizeP)
14710 {
14711 return ieee_md_atof (type, litP, sizeP, target_big_endian);
14712 }
14713
14714 void
14715 md_number_to_chars (char *buf, valueT val, int n)
14716 {
14717 if (target_big_endian)
14718 number_to_chars_bigendian (buf, val, n);
14719 else
14720 number_to_chars_littleendian (buf, val, n);
14721 }
14722 \f
14723 static int support_64bit_objects(void)
14724 {
14725 const char **list, **l;
14726 int yes;
14727
14728 list = bfd_target_list ();
14729 for (l = list; *l != NULL; l++)
14730 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14731 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14732 break;
14733 yes = (*l != NULL);
14734 free (list);
14735 return yes;
14736 }
14737
14738 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14739 NEW_VALUE. Warn if another value was already specified. Note:
14740 we have to defer parsing the -march and -mtune arguments in order
14741 to handle 'from-abi' correctly, since the ABI might be specified
14742 in a later argument. */
14743
14744 static void
14745 mips_set_option_string (const char **string_ptr, const char *new_value)
14746 {
14747 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14748 as_warn (_("a different %s was already specified, is now %s"),
14749 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14750 new_value);
14751
14752 *string_ptr = new_value;
14753 }
14754
14755 int
14756 md_parse_option (int c, const char *arg)
14757 {
14758 unsigned int i;
14759
14760 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14761 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14762 {
14763 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
14764 c == mips_ases[i].option_on);
14765 return 1;
14766 }
14767
14768 switch (c)
14769 {
14770 case OPTION_CONSTRUCT_FLOATS:
14771 mips_disable_float_construction = 0;
14772 break;
14773
14774 case OPTION_NO_CONSTRUCT_FLOATS:
14775 mips_disable_float_construction = 1;
14776 break;
14777
14778 case OPTION_TRAP:
14779 mips_trap = 1;
14780 break;
14781
14782 case OPTION_BREAK:
14783 mips_trap = 0;
14784 break;
14785
14786 case OPTION_EB:
14787 target_big_endian = 1;
14788 break;
14789
14790 case OPTION_EL:
14791 target_big_endian = 0;
14792 break;
14793
14794 case 'O':
14795 if (arg == NULL)
14796 mips_optimize = 1;
14797 else if (arg[0] == '0')
14798 mips_optimize = 0;
14799 else if (arg[0] == '1')
14800 mips_optimize = 1;
14801 else
14802 mips_optimize = 2;
14803 break;
14804
14805 case 'g':
14806 if (arg == NULL)
14807 mips_debug = 2;
14808 else
14809 mips_debug = atoi (arg);
14810 break;
14811
14812 case OPTION_MIPS1:
14813 file_mips_opts.isa = ISA_MIPS1;
14814 break;
14815
14816 case OPTION_MIPS2:
14817 file_mips_opts.isa = ISA_MIPS2;
14818 break;
14819
14820 case OPTION_MIPS3:
14821 file_mips_opts.isa = ISA_MIPS3;
14822 break;
14823
14824 case OPTION_MIPS4:
14825 file_mips_opts.isa = ISA_MIPS4;
14826 break;
14827
14828 case OPTION_MIPS5:
14829 file_mips_opts.isa = ISA_MIPS5;
14830 break;
14831
14832 case OPTION_MIPS32:
14833 file_mips_opts.isa = ISA_MIPS32;
14834 break;
14835
14836 case OPTION_MIPS32R2:
14837 file_mips_opts.isa = ISA_MIPS32R2;
14838 break;
14839
14840 case OPTION_MIPS32R3:
14841 file_mips_opts.isa = ISA_MIPS32R3;
14842 break;
14843
14844 case OPTION_MIPS32R5:
14845 file_mips_opts.isa = ISA_MIPS32R5;
14846 break;
14847
14848 case OPTION_MIPS32R6:
14849 file_mips_opts.isa = ISA_MIPS32R6;
14850 break;
14851
14852 case OPTION_MIPS64R2:
14853 file_mips_opts.isa = ISA_MIPS64R2;
14854 break;
14855
14856 case OPTION_MIPS64R3:
14857 file_mips_opts.isa = ISA_MIPS64R3;
14858 break;
14859
14860 case OPTION_MIPS64R5:
14861 file_mips_opts.isa = ISA_MIPS64R5;
14862 break;
14863
14864 case OPTION_MIPS64R6:
14865 file_mips_opts.isa = ISA_MIPS64R6;
14866 break;
14867
14868 case OPTION_MIPS64:
14869 file_mips_opts.isa = ISA_MIPS64;
14870 break;
14871
14872 case OPTION_MTUNE:
14873 mips_set_option_string (&mips_tune_string, arg);
14874 break;
14875
14876 case OPTION_MARCH:
14877 mips_set_option_string (&mips_arch_string, arg);
14878 break;
14879
14880 case OPTION_M4650:
14881 mips_set_option_string (&mips_arch_string, "4650");
14882 mips_set_option_string (&mips_tune_string, "4650");
14883 break;
14884
14885 case OPTION_NO_M4650:
14886 break;
14887
14888 case OPTION_M4010:
14889 mips_set_option_string (&mips_arch_string, "4010");
14890 mips_set_option_string (&mips_tune_string, "4010");
14891 break;
14892
14893 case OPTION_NO_M4010:
14894 break;
14895
14896 case OPTION_M4100:
14897 mips_set_option_string (&mips_arch_string, "4100");
14898 mips_set_option_string (&mips_tune_string, "4100");
14899 break;
14900
14901 case OPTION_NO_M4100:
14902 break;
14903
14904 case OPTION_M3900:
14905 mips_set_option_string (&mips_arch_string, "3900");
14906 mips_set_option_string (&mips_tune_string, "3900");
14907 break;
14908
14909 case OPTION_NO_M3900:
14910 break;
14911
14912 case OPTION_MICROMIPS:
14913 if (file_mips_opts.mips16 == 1)
14914 {
14915 as_bad (_("-mmicromips cannot be used with -mips16"));
14916 return 0;
14917 }
14918 file_mips_opts.micromips = 1;
14919 mips_no_prev_insn ();
14920 break;
14921
14922 case OPTION_NO_MICROMIPS:
14923 file_mips_opts.micromips = 0;
14924 mips_no_prev_insn ();
14925 break;
14926
14927 case OPTION_MIPS16:
14928 if (file_mips_opts.micromips == 1)
14929 {
14930 as_bad (_("-mips16 cannot be used with -micromips"));
14931 return 0;
14932 }
14933 file_mips_opts.mips16 = 1;
14934 mips_no_prev_insn ();
14935 break;
14936
14937 case OPTION_NO_MIPS16:
14938 file_mips_opts.mips16 = 0;
14939 mips_no_prev_insn ();
14940 break;
14941
14942 case OPTION_FIX_24K:
14943 mips_fix_24k = 1;
14944 break;
14945
14946 case OPTION_NO_FIX_24K:
14947 mips_fix_24k = 0;
14948 break;
14949
14950 case OPTION_FIX_RM7000:
14951 mips_fix_rm7000 = 1;
14952 break;
14953
14954 case OPTION_NO_FIX_RM7000:
14955 mips_fix_rm7000 = 0;
14956 break;
14957
14958 case OPTION_FIX_LOONGSON3_LLSC:
14959 mips_fix_loongson3_llsc = TRUE;
14960 break;
14961
14962 case OPTION_NO_FIX_LOONGSON3_LLSC:
14963 mips_fix_loongson3_llsc = FALSE;
14964 break;
14965
14966 case OPTION_FIX_LOONGSON2F_JUMP:
14967 mips_fix_loongson2f_jump = TRUE;
14968 break;
14969
14970 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14971 mips_fix_loongson2f_jump = FALSE;
14972 break;
14973
14974 case OPTION_FIX_LOONGSON2F_NOP:
14975 mips_fix_loongson2f_nop = TRUE;
14976 break;
14977
14978 case OPTION_NO_FIX_LOONGSON2F_NOP:
14979 mips_fix_loongson2f_nop = FALSE;
14980 break;
14981
14982 case OPTION_FIX_VR4120:
14983 mips_fix_vr4120 = 1;
14984 break;
14985
14986 case OPTION_NO_FIX_VR4120:
14987 mips_fix_vr4120 = 0;
14988 break;
14989
14990 case OPTION_FIX_VR4130:
14991 mips_fix_vr4130 = 1;
14992 break;
14993
14994 case OPTION_NO_FIX_VR4130:
14995 mips_fix_vr4130 = 0;
14996 break;
14997
14998 case OPTION_FIX_CN63XXP1:
14999 mips_fix_cn63xxp1 = TRUE;
15000 break;
15001
15002 case OPTION_NO_FIX_CN63XXP1:
15003 mips_fix_cn63xxp1 = FALSE;
15004 break;
15005
15006 case OPTION_FIX_R5900:
15007 mips_fix_r5900 = TRUE;
15008 mips_fix_r5900_explicit = TRUE;
15009 break;
15010
15011 case OPTION_NO_FIX_R5900:
15012 mips_fix_r5900 = FALSE;
15013 mips_fix_r5900_explicit = TRUE;
15014 break;
15015
15016 case OPTION_RELAX_BRANCH:
15017 mips_relax_branch = 1;
15018 break;
15019
15020 case OPTION_NO_RELAX_BRANCH:
15021 mips_relax_branch = 0;
15022 break;
15023
15024 case OPTION_IGNORE_BRANCH_ISA:
15025 mips_ignore_branch_isa = TRUE;
15026 break;
15027
15028 case OPTION_NO_IGNORE_BRANCH_ISA:
15029 mips_ignore_branch_isa = FALSE;
15030 break;
15031
15032 case OPTION_INSN32:
15033 file_mips_opts.insn32 = TRUE;
15034 break;
15035
15036 case OPTION_NO_INSN32:
15037 file_mips_opts.insn32 = FALSE;
15038 break;
15039
15040 case OPTION_MSHARED:
15041 mips_in_shared = TRUE;
15042 break;
15043
15044 case OPTION_MNO_SHARED:
15045 mips_in_shared = FALSE;
15046 break;
15047
15048 case OPTION_MSYM32:
15049 file_mips_opts.sym32 = TRUE;
15050 break;
15051
15052 case OPTION_MNO_SYM32:
15053 file_mips_opts.sym32 = FALSE;
15054 break;
15055
15056 /* When generating ELF code, we permit -KPIC and -call_shared to
15057 select SVR4_PIC, and -non_shared to select no PIC. This is
15058 intended to be compatible with Irix 5. */
15059 case OPTION_CALL_SHARED:
15060 mips_pic = SVR4_PIC;
15061 mips_abicalls = TRUE;
15062 break;
15063
15064 case OPTION_CALL_NONPIC:
15065 mips_pic = NO_PIC;
15066 mips_abicalls = TRUE;
15067 break;
15068
15069 case OPTION_NON_SHARED:
15070 mips_pic = NO_PIC;
15071 mips_abicalls = FALSE;
15072 break;
15073
15074 /* The -xgot option tells the assembler to use 32 bit offsets
15075 when accessing the got in SVR4_PIC mode. It is for Irix
15076 compatibility. */
15077 case OPTION_XGOT:
15078 mips_big_got = 1;
15079 break;
15080
15081 case 'G':
15082 g_switch_value = atoi (arg);
15083 g_switch_seen = 1;
15084 break;
15085
15086 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15087 and -mabi=64. */
15088 case OPTION_32:
15089 mips_abi = O32_ABI;
15090 break;
15091
15092 case OPTION_N32:
15093 mips_abi = N32_ABI;
15094 break;
15095
15096 case OPTION_64:
15097 mips_abi = N64_ABI;
15098 if (!support_64bit_objects())
15099 as_fatal (_("no compiled in support for 64 bit object file format"));
15100 break;
15101
15102 case OPTION_GP32:
15103 file_mips_opts.gp = 32;
15104 break;
15105
15106 case OPTION_GP64:
15107 file_mips_opts.gp = 64;
15108 break;
15109
15110 case OPTION_FP32:
15111 file_mips_opts.fp = 32;
15112 break;
15113
15114 case OPTION_FPXX:
15115 file_mips_opts.fp = 0;
15116 break;
15117
15118 case OPTION_FP64:
15119 file_mips_opts.fp = 64;
15120 break;
15121
15122 case OPTION_ODD_SPREG:
15123 file_mips_opts.oddspreg = 1;
15124 break;
15125
15126 case OPTION_NO_ODD_SPREG:
15127 file_mips_opts.oddspreg = 0;
15128 break;
15129
15130 case OPTION_SINGLE_FLOAT:
15131 file_mips_opts.single_float = 1;
15132 break;
15133
15134 case OPTION_DOUBLE_FLOAT:
15135 file_mips_opts.single_float = 0;
15136 break;
15137
15138 case OPTION_SOFT_FLOAT:
15139 file_mips_opts.soft_float = 1;
15140 break;
15141
15142 case OPTION_HARD_FLOAT:
15143 file_mips_opts.soft_float = 0;
15144 break;
15145
15146 case OPTION_MABI:
15147 if (strcmp (arg, "32") == 0)
15148 mips_abi = O32_ABI;
15149 else if (strcmp (arg, "o64") == 0)
15150 mips_abi = O64_ABI;
15151 else if (strcmp (arg, "n32") == 0)
15152 mips_abi = N32_ABI;
15153 else if (strcmp (arg, "64") == 0)
15154 {
15155 mips_abi = N64_ABI;
15156 if (! support_64bit_objects())
15157 as_fatal (_("no compiled in support for 64 bit object file "
15158 "format"));
15159 }
15160 else if (strcmp (arg, "eabi") == 0)
15161 mips_abi = EABI_ABI;
15162 else
15163 {
15164 as_fatal (_("invalid abi -mabi=%s"), arg);
15165 return 0;
15166 }
15167 break;
15168
15169 case OPTION_M7000_HILO_FIX:
15170 mips_7000_hilo_fix = TRUE;
15171 break;
15172
15173 case OPTION_MNO_7000_HILO_FIX:
15174 mips_7000_hilo_fix = FALSE;
15175 break;
15176
15177 case OPTION_MDEBUG:
15178 mips_flag_mdebug = TRUE;
15179 break;
15180
15181 case OPTION_NO_MDEBUG:
15182 mips_flag_mdebug = FALSE;
15183 break;
15184
15185 case OPTION_PDR:
15186 mips_flag_pdr = TRUE;
15187 break;
15188
15189 case OPTION_NO_PDR:
15190 mips_flag_pdr = FALSE;
15191 break;
15192
15193 case OPTION_MVXWORKS_PIC:
15194 mips_pic = VXWORKS_PIC;
15195 break;
15196
15197 case OPTION_NAN:
15198 if (strcmp (arg, "2008") == 0)
15199 mips_nan2008 = 1;
15200 else if (strcmp (arg, "legacy") == 0)
15201 mips_nan2008 = 0;
15202 else
15203 {
15204 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
15205 return 0;
15206 }
15207 break;
15208
15209 default:
15210 return 0;
15211 }
15212
15213 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15214
15215 return 1;
15216 }
15217 \f
15218 /* Set up globals to tune for the ISA or processor described by INFO. */
15219
15220 static void
15221 mips_set_tune (const struct mips_cpu_info *info)
15222 {
15223 if (info != 0)
15224 mips_tune = info->cpu;
15225 }
15226
15227
15228 void
15229 mips_after_parse_args (void)
15230 {
15231 const struct mips_cpu_info *arch_info = 0;
15232 const struct mips_cpu_info *tune_info = 0;
15233
15234 /* GP relative stuff not working for PE. */
15235 if (strncmp (TARGET_OS, "pe", 2) == 0)
15236 {
15237 if (g_switch_seen && g_switch_value != 0)
15238 as_bad (_("-G not supported in this configuration"));
15239 g_switch_value = 0;
15240 }
15241
15242 if (mips_abi == NO_ABI)
15243 mips_abi = MIPS_DEFAULT_ABI;
15244
15245 /* The following code determines the architecture.
15246 Similar code was added to GCC 3.3 (see override_options() in
15247 config/mips/mips.c). The GAS and GCC code should be kept in sync
15248 as much as possible. */
15249
15250 if (mips_arch_string != 0)
15251 arch_info = mips_parse_cpu ("-march", mips_arch_string);
15252
15253 if (file_mips_opts.isa != ISA_UNKNOWN)
15254 {
15255 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
15256 ISA level specified by -mipsN, while arch_info->isa contains
15257 the -march selection (if any). */
15258 if (arch_info != 0)
15259 {
15260 /* -march takes precedence over -mipsN, since it is more descriptive.
15261 There's no harm in specifying both as long as the ISA levels
15262 are the same. */
15263 if (file_mips_opts.isa != arch_info->isa)
15264 as_bad (_("-%s conflicts with the other architecture options,"
15265 " which imply -%s"),
15266 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
15267 mips_cpu_info_from_isa (arch_info->isa)->name);
15268 }
15269 else
15270 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
15271 }
15272
15273 if (arch_info == 0)
15274 {
15275 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15276 gas_assert (arch_info);
15277 }
15278
15279 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
15280 as_bad (_("-march=%s is not compatible with the selected ABI"),
15281 arch_info->name);
15282
15283 file_mips_opts.arch = arch_info->cpu;
15284 file_mips_opts.isa = arch_info->isa;
15285 file_mips_opts.init_ase = arch_info->ase;
15286
15287 /* The EVA Extension has instructions which are only valid when the R6 ISA
15288 is enabled. This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
15289 present. */
15290 if (((file_mips_opts.ase & ASE_EVA) != 0) && ISA_IS_R6 (file_mips_opts.isa))
15291 file_mips_opts.ase |= ASE_EVA_R6;
15292
15293 /* Set up initial mips_opts state. */
15294 mips_opts = file_mips_opts;
15295
15296 /* For the R5900 default to `-mfix-r5900' unless the user told otherwise. */
15297 if (!mips_fix_r5900_explicit)
15298 mips_fix_r5900 = file_mips_opts.arch == CPU_R5900;
15299
15300 /* The register size inference code is now placed in
15301 file_mips_check_options. */
15302
15303 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
15304 processor. */
15305 if (mips_tune_string != 0)
15306 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
15307
15308 if (tune_info == 0)
15309 mips_set_tune (arch_info);
15310 else
15311 mips_set_tune (tune_info);
15312
15313 if (mips_flag_mdebug < 0)
15314 mips_flag_mdebug = 0;
15315 }
15316 \f
15317 void
15318 mips_init_after_args (void)
15319 {
15320 /* Initialize opcodes. */
15321 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15322 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15323 }
15324
15325 long
15326 md_pcrel_from (fixS *fixP)
15327 {
15328 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15329
15330 switch (fixP->fx_r_type)
15331 {
15332 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15333 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15334 /* Return the address of the delay slot. */
15335 return addr + 2;
15336
15337 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15338 case BFD_RELOC_MICROMIPS_JMP:
15339 case BFD_RELOC_MIPS16_16_PCREL_S1:
15340 case BFD_RELOC_16_PCREL_S2:
15341 case BFD_RELOC_MIPS_21_PCREL_S2:
15342 case BFD_RELOC_MIPS_26_PCREL_S2:
15343 case BFD_RELOC_MIPS_JMP:
15344 /* Return the address of the delay slot. */
15345 return addr + 4;
15346
15347 case BFD_RELOC_MIPS_18_PCREL_S3:
15348 /* Return the aligned address of the doubleword containing
15349 the instruction. */
15350 return addr & ~7;
15351
15352 default:
15353 return addr;
15354 }
15355 }
15356
15357 /* This is called before the symbol table is processed. In order to
15358 work with gcc when using mips-tfile, we must keep all local labels.
15359 However, in other cases, we want to discard them. If we were
15360 called with -g, but we didn't see any debugging information, it may
15361 mean that gcc is smuggling debugging information through to
15362 mips-tfile, in which case we must generate all local labels. */
15363
15364 void
15365 mips_frob_file_before_adjust (void)
15366 {
15367 #ifndef NO_ECOFF_DEBUGGING
15368 if (ECOFF_DEBUGGING
15369 && mips_debug != 0
15370 && ! ecoff_debugging_seen)
15371 flag_keep_locals = 1;
15372 #endif
15373 }
15374
15375 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15376 the corresponding LO16 reloc. This is called before md_apply_fix and
15377 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15378 relocation operators.
15379
15380 For our purposes, a %lo() expression matches a %got() or %hi()
15381 expression if:
15382
15383 (a) it refers to the same symbol; and
15384 (b) the offset applied in the %lo() expression is no lower than
15385 the offset applied in the %got() or %hi().
15386
15387 (b) allows us to cope with code like:
15388
15389 lui $4,%hi(foo)
15390 lh $4,%lo(foo+2)($4)
15391
15392 ...which is legal on RELA targets, and has a well-defined behaviour
15393 if the user knows that adding 2 to "foo" will not induce a carry to
15394 the high 16 bits.
15395
15396 When several %lo()s match a particular %got() or %hi(), we use the
15397 following rules to distinguish them:
15398
15399 (1) %lo()s with smaller offsets are a better match than %lo()s with
15400 higher offsets.
15401
15402 (2) %lo()s with no matching %got() or %hi() are better than those
15403 that already have a matching %got() or %hi().
15404
15405 (3) later %lo()s are better than earlier %lo()s.
15406
15407 These rules are applied in order.
15408
15409 (1) means, among other things, that %lo()s with identical offsets are
15410 chosen if they exist.
15411
15412 (2) means that we won't associate several high-part relocations with
15413 the same low-part relocation unless there's no alternative. Having
15414 several high parts for the same low part is a GNU extension; this rule
15415 allows careful users to avoid it.
15416
15417 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15418 with the last high-part relocation being at the front of the list.
15419 It therefore makes sense to choose the last matching low-part
15420 relocation, all other things being equal. It's also easier
15421 to code that way. */
15422
15423 void
15424 mips_frob_file (void)
15425 {
15426 struct mips_hi_fixup *l;
15427 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15428
15429 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15430 {
15431 segment_info_type *seginfo;
15432 bfd_boolean matched_lo_p;
15433 fixS **hi_pos, **lo_pos, **pos;
15434
15435 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15436
15437 /* If a GOT16 relocation turns out to be against a global symbol,
15438 there isn't supposed to be a matching LO. Ignore %gots against
15439 constants; we'll report an error for those later. */
15440 if (got16_reloc_p (l->fixp->fx_r_type)
15441 && !(l->fixp->fx_addsy
15442 && pic_need_relax (l->fixp->fx_addsy)))
15443 continue;
15444
15445 /* Check quickly whether the next fixup happens to be a matching %lo. */
15446 if (fixup_has_matching_lo_p (l->fixp))
15447 continue;
15448
15449 seginfo = seg_info (l->seg);
15450
15451 /* Set HI_POS to the position of this relocation in the chain.
15452 Set LO_POS to the position of the chosen low-part relocation.
15453 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15454 relocation that matches an immediately-preceding high-part
15455 relocation. */
15456 hi_pos = NULL;
15457 lo_pos = NULL;
15458 matched_lo_p = FALSE;
15459 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15460
15461 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15462 {
15463 if (*pos == l->fixp)
15464 hi_pos = pos;
15465
15466 if ((*pos)->fx_r_type == looking_for_rtype
15467 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15468 && (*pos)->fx_offset >= l->fixp->fx_offset
15469 && (lo_pos == NULL
15470 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15471 || (!matched_lo_p
15472 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15473 lo_pos = pos;
15474
15475 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15476 && fixup_has_matching_lo_p (*pos));
15477 }
15478
15479 /* If we found a match, remove the high-part relocation from its
15480 current position and insert it before the low-part relocation.
15481 Make the offsets match so that fixup_has_matching_lo_p()
15482 will return true.
15483
15484 We don't warn about unmatched high-part relocations since some
15485 versions of gcc have been known to emit dead "lui ...%hi(...)"
15486 instructions. */
15487 if (lo_pos != NULL)
15488 {
15489 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15490 if (l->fixp->fx_next != *lo_pos)
15491 {
15492 *hi_pos = l->fixp->fx_next;
15493 l->fixp->fx_next = *lo_pos;
15494 *lo_pos = l->fixp;
15495 }
15496 }
15497 }
15498 }
15499
15500 int
15501 mips_force_relocation (fixS *fixp)
15502 {
15503 if (generic_force_reloc (fixp))
15504 return 1;
15505
15506 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15507 so that the linker relaxation can update targets. */
15508 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15509 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15510 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15511 return 1;
15512
15513 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15514 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15515 microMIPS symbols so that we can do cross-mode branch diagnostics
15516 and BAL to JALX conversion by the linker. */
15517 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15518 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15519 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15520 && fixp->fx_addsy
15521 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15522 return 1;
15523
15524 /* We want all PC-relative relocations to be kept for R6 relaxation. */
15525 if (ISA_IS_R6 (file_mips_opts.isa)
15526 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15527 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15528 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15529 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15530 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15531 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15532 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15533 return 1;
15534
15535 return 0;
15536 }
15537
15538 /* Implement TC_FORCE_RELOCATION_ABS. */
15539
15540 bfd_boolean
15541 mips_force_relocation_abs (fixS *fixp)
15542 {
15543 if (generic_force_reloc (fixp))
15544 return TRUE;
15545
15546 /* These relocations do not have enough bits in the in-place addend
15547 to hold an arbitrary absolute section's offset. */
15548 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15549 return TRUE;
15550
15551 return FALSE;
15552 }
15553
15554 /* Read the instruction associated with RELOC from BUF. */
15555
15556 static unsigned int
15557 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15558 {
15559 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15560 return read_compressed_insn (buf, 4);
15561 else
15562 return read_insn (buf);
15563 }
15564
15565 /* Write instruction INSN to BUF, given that it has been relocated
15566 by RELOC. */
15567
15568 static void
15569 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15570 unsigned long insn)
15571 {
15572 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15573 write_compressed_insn (buf, insn, 4);
15574 else
15575 write_insn (buf, insn);
15576 }
15577
15578 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15579 to a symbol in another ISA mode, which cannot be converted to JALX. */
15580
15581 static bfd_boolean
15582 fix_bad_cross_mode_jump_p (fixS *fixP)
15583 {
15584 unsigned long opcode;
15585 int other;
15586 char *buf;
15587
15588 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15589 return FALSE;
15590
15591 other = S_GET_OTHER (fixP->fx_addsy);
15592 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15593 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15594 switch (fixP->fx_r_type)
15595 {
15596 case BFD_RELOC_MIPS_JMP:
15597 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15598 case BFD_RELOC_MICROMIPS_JMP:
15599 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15600 default:
15601 return FALSE;
15602 }
15603 }
15604
15605 /* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15606 jump to a symbol in the same ISA mode. */
15607
15608 static bfd_boolean
15609 fix_bad_same_mode_jalx_p (fixS *fixP)
15610 {
15611 unsigned long opcode;
15612 int other;
15613 char *buf;
15614
15615 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15616 return FALSE;
15617
15618 other = S_GET_OTHER (fixP->fx_addsy);
15619 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15620 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15621 switch (fixP->fx_r_type)
15622 {
15623 case BFD_RELOC_MIPS_JMP:
15624 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15625 case BFD_RELOC_MIPS16_JMP:
15626 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15627 case BFD_RELOC_MICROMIPS_JMP:
15628 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15629 default:
15630 return FALSE;
15631 }
15632 }
15633
15634 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15635 to a symbol whose value plus addend is not aligned according to the
15636 ultimate (after linker relaxation) jump instruction's immediate field
15637 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15638 regular MIPS code, to (1 << 2). */
15639
15640 static bfd_boolean
15641 fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15642 {
15643 bfd_boolean micro_to_mips_p;
15644 valueT val;
15645 int other;
15646
15647 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15648 return FALSE;
15649
15650 other = S_GET_OTHER (fixP->fx_addsy);
15651 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15652 val += fixP->fx_offset;
15653 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15654 && !ELF_ST_IS_MICROMIPS (other));
15655 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15656 != ELF_ST_IS_COMPRESSED (other));
15657 }
15658
15659 /* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15660 to a symbol whose annotation indicates another ISA mode. For absolute
15661 symbols check the ISA bit instead.
15662
15663 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15664 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15665 MIPS symbols and associated with BAL instructions as these instructions
15666 may be converted to JALX by the linker. */
15667
15668 static bfd_boolean
15669 fix_bad_cross_mode_branch_p (fixS *fixP)
15670 {
15671 bfd_boolean absolute_p;
15672 unsigned long opcode;
15673 asection *symsec;
15674 valueT val;
15675 int other;
15676 char *buf;
15677
15678 if (mips_ignore_branch_isa)
15679 return FALSE;
15680
15681 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15682 return FALSE;
15683
15684 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15685 absolute_p = bfd_is_abs_section (symsec);
15686
15687 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15688 other = S_GET_OTHER (fixP->fx_addsy);
15689
15690 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15691 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15692 switch (fixP->fx_r_type)
15693 {
15694 case BFD_RELOC_16_PCREL_S2:
15695 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15696 && opcode != 0x0411);
15697 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15698 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15699 && opcode != 0x4060);
15700 case BFD_RELOC_MIPS_21_PCREL_S2:
15701 case BFD_RELOC_MIPS_26_PCREL_S2:
15702 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15703 case BFD_RELOC_MIPS16_16_PCREL_S1:
15704 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15705 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15706 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15707 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15708 default:
15709 abort ();
15710 }
15711 }
15712
15713 /* Return TRUE if the symbol plus addend associated with a regular MIPS
15714 branch instruction pointed to by FIXP is not aligned according to the
15715 branch instruction's immediate field requirement. We need the addend
15716 to preserve the ISA bit and also the sum must not have bit 2 set. We
15717 must explicitly OR in the ISA bit from symbol annotation as the bit
15718 won't be set in the symbol's value then. */
15719
15720 static bfd_boolean
15721 fix_bad_misaligned_branch_p (fixS *fixP)
15722 {
15723 bfd_boolean absolute_p;
15724 asection *symsec;
15725 valueT isa_bit;
15726 valueT val;
15727 valueT off;
15728 int other;
15729
15730 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15731 return FALSE;
15732
15733 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15734 absolute_p = bfd_is_abs_section (symsec);
15735
15736 val = S_GET_VALUE (fixP->fx_addsy);
15737 other = S_GET_OTHER (fixP->fx_addsy);
15738 off = fixP->fx_offset;
15739
15740 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15741 val |= ELF_ST_IS_COMPRESSED (other);
15742 val += off;
15743 return (val & 0x3) != isa_bit;
15744 }
15745
15746 /* Calculate the relocation target by masking off ISA mode bit before
15747 combining symbol and addend. */
15748
15749 static valueT
15750 fix_bad_misaligned_address (fixS *fixP)
15751 {
15752 valueT val;
15753 valueT off;
15754 unsigned isa_mode;
15755 gas_assert (fixP != NULL && fixP->fx_addsy != NULL);
15756 val = S_GET_VALUE (fixP->fx_addsy);
15757 off = fixP->fx_offset;
15758 isa_mode = (ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixP->fx_addsy))
15759 ? 1 : 0);
15760
15761 return ((val & ~isa_mode) + off);
15762 }
15763
15764 /* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15765 and its calculated value VAL. */
15766
15767 static void
15768 fix_validate_branch (fixS *fixP, valueT val)
15769 {
15770 if (fixP->fx_done && (val & 0x3) != 0)
15771 as_bad_where (fixP->fx_file, fixP->fx_line,
15772 _("branch to misaligned address (0x%lx)"),
15773 (long) (val + md_pcrel_from (fixP)));
15774 else if (fix_bad_cross_mode_branch_p (fixP))
15775 as_bad_where (fixP->fx_file, fixP->fx_line,
15776 _("branch to a symbol in another ISA mode"));
15777 else if (fix_bad_misaligned_branch_p (fixP))
15778 as_bad_where (fixP->fx_file, fixP->fx_line,
15779 _("branch to misaligned address (0x%lx)"),
15780 (long) fix_bad_misaligned_address (fixP));
15781 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15782 as_bad_where (fixP->fx_file, fixP->fx_line,
15783 _("cannot encode misaligned addend "
15784 "in the relocatable field (0x%lx)"),
15785 (long) fixP->fx_offset);
15786 }
15787
15788 /* Apply a fixup to the object file. */
15789
15790 void
15791 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15792 {
15793 char *buf;
15794 unsigned long insn;
15795 reloc_howto_type *howto;
15796
15797 if (fixP->fx_pcrel)
15798 switch (fixP->fx_r_type)
15799 {
15800 case BFD_RELOC_16_PCREL_S2:
15801 case BFD_RELOC_MIPS16_16_PCREL_S1:
15802 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15803 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15804 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15805 case BFD_RELOC_32_PCREL:
15806 case BFD_RELOC_MIPS_21_PCREL_S2:
15807 case BFD_RELOC_MIPS_26_PCREL_S2:
15808 case BFD_RELOC_MIPS_18_PCREL_S3:
15809 case BFD_RELOC_MIPS_19_PCREL_S2:
15810 case BFD_RELOC_HI16_S_PCREL:
15811 case BFD_RELOC_LO16_PCREL:
15812 break;
15813
15814 case BFD_RELOC_32:
15815 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15816 break;
15817
15818 default:
15819 as_bad_where (fixP->fx_file, fixP->fx_line,
15820 _("PC-relative reference to a different section"));
15821 break;
15822 }
15823
15824 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15825 that have no MIPS ELF equivalent. */
15826 if (fixP->fx_r_type != BFD_RELOC_8)
15827 {
15828 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15829 if (!howto)
15830 return;
15831 }
15832
15833 gas_assert (fixP->fx_size == 2
15834 || fixP->fx_size == 4
15835 || fixP->fx_r_type == BFD_RELOC_8
15836 || fixP->fx_r_type == BFD_RELOC_16
15837 || fixP->fx_r_type == BFD_RELOC_64
15838 || fixP->fx_r_type == BFD_RELOC_CTOR
15839 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15840 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15841 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15842 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15843 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15844 || fixP->fx_r_type == BFD_RELOC_NONE);
15845
15846 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15847
15848 /* Don't treat parts of a composite relocation as done. There are two
15849 reasons for this:
15850
15851 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15852 should nevertheless be emitted if the first part is.
15853
15854 (2) In normal usage, composite relocations are never assembly-time
15855 constants. The easiest way of dealing with the pathological
15856 exceptions is to generate a relocation against STN_UNDEF and
15857 leave everything up to the linker. */
15858 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15859 fixP->fx_done = 1;
15860
15861 switch (fixP->fx_r_type)
15862 {
15863 case BFD_RELOC_MIPS_TLS_GD:
15864 case BFD_RELOC_MIPS_TLS_LDM:
15865 case BFD_RELOC_MIPS_TLS_DTPREL32:
15866 case BFD_RELOC_MIPS_TLS_DTPREL64:
15867 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15868 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15869 case BFD_RELOC_MIPS_TLS_GOTTPREL:
15870 case BFD_RELOC_MIPS_TLS_TPREL32:
15871 case BFD_RELOC_MIPS_TLS_TPREL64:
15872 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15873 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15874 case BFD_RELOC_MICROMIPS_TLS_GD:
15875 case BFD_RELOC_MICROMIPS_TLS_LDM:
15876 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15877 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15878 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15879 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15880 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15881 case BFD_RELOC_MIPS16_TLS_GD:
15882 case BFD_RELOC_MIPS16_TLS_LDM:
15883 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15884 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15885 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15886 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15887 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15888 if (fixP->fx_addsy)
15889 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15890 else
15891 as_bad_where (fixP->fx_file, fixP->fx_line,
15892 _("TLS relocation against a constant"));
15893 break;
15894
15895 case BFD_RELOC_MIPS_JMP:
15896 case BFD_RELOC_MIPS16_JMP:
15897 case BFD_RELOC_MICROMIPS_JMP:
15898 {
15899 int shift;
15900
15901 gas_assert (!fixP->fx_done);
15902
15903 /* Shift is 2, unusually, for microMIPS JALX. */
15904 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15905 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15906 shift = 1;
15907 else
15908 shift = 2;
15909
15910 if (fix_bad_cross_mode_jump_p (fixP))
15911 as_bad_where (fixP->fx_file, fixP->fx_line,
15912 _("jump to a symbol in another ISA mode"));
15913 else if (fix_bad_same_mode_jalx_p (fixP))
15914 as_bad_where (fixP->fx_file, fixP->fx_line,
15915 _("JALX to a symbol in the same ISA mode"));
15916 else if (fix_bad_misaligned_jump_p (fixP, shift))
15917 as_bad_where (fixP->fx_file, fixP->fx_line,
15918 _("jump to misaligned address (0x%lx)"),
15919 (long) fix_bad_misaligned_address (fixP));
15920 else if (HAVE_IN_PLACE_ADDENDS
15921 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15922 as_bad_where (fixP->fx_file, fixP->fx_line,
15923 _("cannot encode misaligned addend "
15924 "in the relocatable field (0x%lx)"),
15925 (long) fixP->fx_offset);
15926 }
15927 /* Fall through. */
15928
15929 case BFD_RELOC_MIPS_SHIFT5:
15930 case BFD_RELOC_MIPS_SHIFT6:
15931 case BFD_RELOC_MIPS_GOT_DISP:
15932 case BFD_RELOC_MIPS_GOT_PAGE:
15933 case BFD_RELOC_MIPS_GOT_OFST:
15934 case BFD_RELOC_MIPS_SUB:
15935 case BFD_RELOC_MIPS_INSERT_A:
15936 case BFD_RELOC_MIPS_INSERT_B:
15937 case BFD_RELOC_MIPS_DELETE:
15938 case BFD_RELOC_MIPS_HIGHEST:
15939 case BFD_RELOC_MIPS_HIGHER:
15940 case BFD_RELOC_MIPS_SCN_DISP:
15941 case BFD_RELOC_MIPS_REL16:
15942 case BFD_RELOC_MIPS_RELGOT:
15943 case BFD_RELOC_MIPS_JALR:
15944 case BFD_RELOC_HI16:
15945 case BFD_RELOC_HI16_S:
15946 case BFD_RELOC_LO16:
15947 case BFD_RELOC_GPREL16:
15948 case BFD_RELOC_MIPS_LITERAL:
15949 case BFD_RELOC_MIPS_CALL16:
15950 case BFD_RELOC_MIPS_GOT16:
15951 case BFD_RELOC_GPREL32:
15952 case BFD_RELOC_MIPS_GOT_HI16:
15953 case BFD_RELOC_MIPS_GOT_LO16:
15954 case BFD_RELOC_MIPS_CALL_HI16:
15955 case BFD_RELOC_MIPS_CALL_LO16:
15956 case BFD_RELOC_HI16_S_PCREL:
15957 case BFD_RELOC_LO16_PCREL:
15958 case BFD_RELOC_MIPS16_GPREL:
15959 case BFD_RELOC_MIPS16_GOT16:
15960 case BFD_RELOC_MIPS16_CALL16:
15961 case BFD_RELOC_MIPS16_HI16:
15962 case BFD_RELOC_MIPS16_HI16_S:
15963 case BFD_RELOC_MIPS16_LO16:
15964 case BFD_RELOC_MICROMIPS_GOT_DISP:
15965 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15966 case BFD_RELOC_MICROMIPS_GOT_OFST:
15967 case BFD_RELOC_MICROMIPS_SUB:
15968 case BFD_RELOC_MICROMIPS_HIGHEST:
15969 case BFD_RELOC_MICROMIPS_HIGHER:
15970 case BFD_RELOC_MICROMIPS_SCN_DISP:
15971 case BFD_RELOC_MICROMIPS_JALR:
15972 case BFD_RELOC_MICROMIPS_HI16:
15973 case BFD_RELOC_MICROMIPS_HI16_S:
15974 case BFD_RELOC_MICROMIPS_LO16:
15975 case BFD_RELOC_MICROMIPS_GPREL16:
15976 case BFD_RELOC_MICROMIPS_LITERAL:
15977 case BFD_RELOC_MICROMIPS_CALL16:
15978 case BFD_RELOC_MICROMIPS_GOT16:
15979 case BFD_RELOC_MICROMIPS_GOT_HI16:
15980 case BFD_RELOC_MICROMIPS_GOT_LO16:
15981 case BFD_RELOC_MICROMIPS_CALL_HI16:
15982 case BFD_RELOC_MICROMIPS_CALL_LO16:
15983 case BFD_RELOC_MIPS_EH:
15984 if (fixP->fx_done)
15985 {
15986 offsetT value;
15987
15988 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15989 {
15990 insn = read_reloc_insn (buf, fixP->fx_r_type);
15991 if (mips16_reloc_p (fixP->fx_r_type))
15992 insn |= mips16_immed_extend (value, 16);
15993 else
15994 insn |= (value & 0xffff);
15995 write_reloc_insn (buf, fixP->fx_r_type, insn);
15996 }
15997 else
15998 as_bad_where (fixP->fx_file, fixP->fx_line,
15999 _("unsupported constant in relocation"));
16000 }
16001 break;
16002
16003 case BFD_RELOC_64:
16004 /* This is handled like BFD_RELOC_32, but we output a sign
16005 extended value if we are only 32 bits. */
16006 if (fixP->fx_done)
16007 {
16008 if (8 <= sizeof (valueT))
16009 md_number_to_chars (buf, *valP, 8);
16010 else
16011 {
16012 valueT hiv;
16013
16014 if ((*valP & 0x80000000) != 0)
16015 hiv = 0xffffffff;
16016 else
16017 hiv = 0;
16018 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
16019 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
16020 }
16021 }
16022 break;
16023
16024 case BFD_RELOC_RVA:
16025 case BFD_RELOC_32:
16026 case BFD_RELOC_32_PCREL:
16027 case BFD_RELOC_16:
16028 case BFD_RELOC_8:
16029 /* If we are deleting this reloc entry, we must fill in the
16030 value now. This can happen if we have a .word which is not
16031 resolved when it appears but is later defined. */
16032 if (fixP->fx_done)
16033 md_number_to_chars (buf, *valP, fixP->fx_size);
16034 break;
16035
16036 case BFD_RELOC_MIPS_21_PCREL_S2:
16037 fix_validate_branch (fixP, *valP);
16038 if (!fixP->fx_done)
16039 break;
16040
16041 if (*valP + 0x400000 <= 0x7fffff)
16042 {
16043 insn = read_insn (buf);
16044 insn |= (*valP >> 2) & 0x1fffff;
16045 write_insn (buf, insn);
16046 }
16047 else
16048 as_bad_where (fixP->fx_file, fixP->fx_line,
16049 _("branch out of range"));
16050 break;
16051
16052 case BFD_RELOC_MIPS_26_PCREL_S2:
16053 fix_validate_branch (fixP, *valP);
16054 if (!fixP->fx_done)
16055 break;
16056
16057 if (*valP + 0x8000000 <= 0xfffffff)
16058 {
16059 insn = read_insn (buf);
16060 insn |= (*valP >> 2) & 0x3ffffff;
16061 write_insn (buf, insn);
16062 }
16063 else
16064 as_bad_where (fixP->fx_file, fixP->fx_line,
16065 _("branch out of range"));
16066 break;
16067
16068 case BFD_RELOC_MIPS_18_PCREL_S3:
16069 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
16070 as_bad_where (fixP->fx_file, fixP->fx_line,
16071 _("PC-relative access using misaligned symbol (%lx)"),
16072 (long) S_GET_VALUE (fixP->fx_addsy));
16073 if ((fixP->fx_offset & 0x7) != 0)
16074 as_bad_where (fixP->fx_file, fixP->fx_line,
16075 _("PC-relative access using misaligned offset (%lx)"),
16076 (long) fixP->fx_offset);
16077 if (!fixP->fx_done)
16078 break;
16079
16080 if (*valP + 0x100000 <= 0x1fffff)
16081 {
16082 insn = read_insn (buf);
16083 insn |= (*valP >> 3) & 0x3ffff;
16084 write_insn (buf, insn);
16085 }
16086 else
16087 as_bad_where (fixP->fx_file, fixP->fx_line,
16088 _("PC-relative access out of range"));
16089 break;
16090
16091 case BFD_RELOC_MIPS_19_PCREL_S2:
16092 if ((*valP & 0x3) != 0)
16093 as_bad_where (fixP->fx_file, fixP->fx_line,
16094 _("PC-relative access to misaligned address (%lx)"),
16095 (long) *valP);
16096 if (!fixP->fx_done)
16097 break;
16098
16099 if (*valP + 0x100000 <= 0x1fffff)
16100 {
16101 insn = read_insn (buf);
16102 insn |= (*valP >> 2) & 0x7ffff;
16103 write_insn (buf, insn);
16104 }
16105 else
16106 as_bad_where (fixP->fx_file, fixP->fx_line,
16107 _("PC-relative access out of range"));
16108 break;
16109
16110 case BFD_RELOC_16_PCREL_S2:
16111 fix_validate_branch (fixP, *valP);
16112
16113 /* We need to save the bits in the instruction since fixup_segment()
16114 might be deleting the relocation entry (i.e., a branch within
16115 the current segment). */
16116 if (! fixP->fx_done)
16117 break;
16118
16119 /* Update old instruction data. */
16120 insn = read_insn (buf);
16121
16122 if (*valP + 0x20000 <= 0x3ffff)
16123 {
16124 insn |= (*valP >> 2) & 0xffff;
16125 write_insn (buf, insn);
16126 }
16127 else if (fixP->fx_tcbit2
16128 && fixP->fx_done
16129 && fixP->fx_frag->fr_address >= text_section->vma
16130 && (fixP->fx_frag->fr_address
16131 < text_section->vma + bfd_get_section_size (text_section))
16132 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
16133 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
16134 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
16135 {
16136 /* The branch offset is too large. If this is an
16137 unconditional branch, and we are not generating PIC code,
16138 we can convert it to an absolute jump instruction. */
16139 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
16140 insn = 0x0c000000; /* jal */
16141 else
16142 insn = 0x08000000; /* j */
16143 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
16144 fixP->fx_done = 0;
16145 fixP->fx_addsy = section_symbol (text_section);
16146 *valP += md_pcrel_from (fixP);
16147 write_insn (buf, insn);
16148 }
16149 else
16150 {
16151 /* If we got here, we have branch-relaxation disabled,
16152 and there's nothing we can do to fix this instruction
16153 without turning it into a longer sequence. */
16154 as_bad_where (fixP->fx_file, fixP->fx_line,
16155 _("branch out of range"));
16156 }
16157 break;
16158
16159 case BFD_RELOC_MIPS16_16_PCREL_S1:
16160 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
16161 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
16162 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
16163 gas_assert (!fixP->fx_done);
16164 if (fix_bad_cross_mode_branch_p (fixP))
16165 as_bad_where (fixP->fx_file, fixP->fx_line,
16166 _("branch to a symbol in another ISA mode"));
16167 else if (fixP->fx_addsy
16168 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
16169 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
16170 && (fixP->fx_offset & 0x1) != 0)
16171 as_bad_where (fixP->fx_file, fixP->fx_line,
16172 _("branch to misaligned address (0x%lx)"),
16173 (long) fix_bad_misaligned_address (fixP));
16174 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
16175 as_bad_where (fixP->fx_file, fixP->fx_line,
16176 _("cannot encode misaligned addend "
16177 "in the relocatable field (0x%lx)"),
16178 (long) fixP->fx_offset);
16179 break;
16180
16181 case BFD_RELOC_VTABLE_INHERIT:
16182 fixP->fx_done = 0;
16183 if (fixP->fx_addsy
16184 && !S_IS_DEFINED (fixP->fx_addsy)
16185 && !S_IS_WEAK (fixP->fx_addsy))
16186 S_SET_WEAK (fixP->fx_addsy);
16187 break;
16188
16189 case BFD_RELOC_NONE:
16190 case BFD_RELOC_VTABLE_ENTRY:
16191 fixP->fx_done = 0;
16192 break;
16193
16194 default:
16195 abort ();
16196 }
16197
16198 /* Remember value for tc_gen_reloc. */
16199 fixP->fx_addnumber = *valP;
16200 }
16201
16202 static symbolS *
16203 get_symbol (void)
16204 {
16205 int c;
16206 char *name;
16207 symbolS *p;
16208
16209 c = get_symbol_name (&name);
16210 p = (symbolS *) symbol_find_or_make (name);
16211 (void) restore_line_pointer (c);
16212 return p;
16213 }
16214
16215 /* Align the current frag to a given power of two. If a particular
16216 fill byte should be used, FILL points to an integer that contains
16217 that byte, otherwise FILL is null.
16218
16219 This function used to have the comment:
16220
16221 The MIPS assembler also automatically adjusts any preceding label.
16222
16223 The implementation therefore applied the adjustment to a maximum of
16224 one label. However, other label adjustments are applied to batches
16225 of labels, and adjusting just one caused problems when new labels
16226 were added for the sake of debugging or unwind information.
16227 We therefore adjust all preceding labels (given as LABELS) instead. */
16228
16229 static void
16230 mips_align (int to, int *fill, struct insn_label_list *labels)
16231 {
16232 mips_emit_delays ();
16233 mips_record_compressed_mode ();
16234 if (fill == NULL && subseg_text_p (now_seg))
16235 frag_align_code (to, 0);
16236 else
16237 frag_align (to, fill ? *fill : 0, 0);
16238 record_alignment (now_seg, to);
16239 mips_move_labels (labels, subseg_text_p (now_seg));
16240 }
16241
16242 /* Align to a given power of two. .align 0 turns off the automatic
16243 alignment used by the data creating pseudo-ops. */
16244
16245 static void
16246 s_align (int x ATTRIBUTE_UNUSED)
16247 {
16248 int temp, fill_value, *fill_ptr;
16249 long max_alignment = 28;
16250
16251 /* o Note that the assembler pulls down any immediately preceding label
16252 to the aligned address.
16253 o It's not documented but auto alignment is reinstated by
16254 a .align pseudo instruction.
16255 o Note also that after auto alignment is turned off the mips assembler
16256 issues an error on attempt to assemble an improperly aligned data item.
16257 We don't. */
16258
16259 temp = get_absolute_expression ();
16260 if (temp > max_alignment)
16261 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
16262 else if (temp < 0)
16263 {
16264 as_warn (_("alignment negative, 0 assumed"));
16265 temp = 0;
16266 }
16267 if (*input_line_pointer == ',')
16268 {
16269 ++input_line_pointer;
16270 fill_value = get_absolute_expression ();
16271 fill_ptr = &fill_value;
16272 }
16273 else
16274 fill_ptr = 0;
16275 if (temp)
16276 {
16277 segment_info_type *si = seg_info (now_seg);
16278 struct insn_label_list *l = si->label_list;
16279 /* Auto alignment should be switched on by next section change. */
16280 auto_align = 1;
16281 mips_align (temp, fill_ptr, l);
16282 }
16283 else
16284 {
16285 auto_align = 0;
16286 }
16287
16288 demand_empty_rest_of_line ();
16289 }
16290
16291 static void
16292 s_change_sec (int sec)
16293 {
16294 segT seg;
16295
16296 /* The ELF backend needs to know that we are changing sections, so
16297 that .previous works correctly. We could do something like check
16298 for an obj_section_change_hook macro, but that might be confusing
16299 as it would not be appropriate to use it in the section changing
16300 functions in read.c, since obj-elf.c intercepts those. FIXME:
16301 This should be cleaner, somehow. */
16302 obj_elf_section_change_hook ();
16303
16304 mips_emit_delays ();
16305
16306 switch (sec)
16307 {
16308 case 't':
16309 s_text (0);
16310 break;
16311 case 'd':
16312 s_data (0);
16313 break;
16314 case 'b':
16315 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16316 demand_empty_rest_of_line ();
16317 break;
16318
16319 case 'r':
16320 seg = subseg_new (RDATA_SECTION_NAME,
16321 (subsegT) get_absolute_expression ());
16322 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16323 | SEC_READONLY | SEC_RELOC
16324 | SEC_DATA));
16325 if (strncmp (TARGET_OS, "elf", 3) != 0)
16326 record_alignment (seg, 4);
16327 demand_empty_rest_of_line ();
16328 break;
16329
16330 case 's':
16331 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
16332 bfd_set_section_flags (stdoutput, seg,
16333 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16334 if (strncmp (TARGET_OS, "elf", 3) != 0)
16335 record_alignment (seg, 4);
16336 demand_empty_rest_of_line ();
16337 break;
16338
16339 case 'B':
16340 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16341 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16342 if (strncmp (TARGET_OS, "elf", 3) != 0)
16343 record_alignment (seg, 4);
16344 demand_empty_rest_of_line ();
16345 break;
16346 }
16347
16348 auto_align = 1;
16349 }
16350
16351 void
16352 s_change_section (int ignore ATTRIBUTE_UNUSED)
16353 {
16354 char *saved_ilp;
16355 char *section_name;
16356 char c, endc;
16357 char next_c = 0;
16358 int section_type;
16359 int section_flag;
16360 int section_entry_size;
16361 int section_alignment;
16362
16363 saved_ilp = input_line_pointer;
16364 endc = get_symbol_name (&section_name);
16365 c = (endc == '"' ? input_line_pointer[1] : endc);
16366 if (c)
16367 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
16368
16369 /* Do we have .section Name<,"flags">? */
16370 if (c != ',' || (c == ',' && next_c == '"'))
16371 {
16372 /* Just after name is now '\0'. */
16373 (void) restore_line_pointer (endc);
16374 input_line_pointer = saved_ilp;
16375 obj_elf_section (ignore);
16376 return;
16377 }
16378
16379 section_name = xstrdup (section_name);
16380 c = restore_line_pointer (endc);
16381
16382 input_line_pointer++;
16383
16384 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16385 if (c == ',')
16386 section_type = get_absolute_expression ();
16387 else
16388 section_type = 0;
16389
16390 if (*input_line_pointer++ == ',')
16391 section_flag = get_absolute_expression ();
16392 else
16393 section_flag = 0;
16394
16395 if (*input_line_pointer++ == ',')
16396 section_entry_size = get_absolute_expression ();
16397 else
16398 section_entry_size = 0;
16399
16400 if (*input_line_pointer++ == ',')
16401 section_alignment = get_absolute_expression ();
16402 else
16403 section_alignment = 0;
16404
16405 /* FIXME: really ignore? */
16406 (void) section_alignment;
16407
16408 /* When using the generic form of .section (as implemented by obj-elf.c),
16409 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16410 traditionally had to fall back on the more common @progbits instead.
16411
16412 There's nothing really harmful in this, since bfd will correct
16413 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
16414 means that, for backwards compatibility, the special_section entries
16415 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16416
16417 Even so, we shouldn't force users of the MIPS .section syntax to
16418 incorrectly label the sections as SHT_PROGBITS. The best compromise
16419 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16420 generic type-checking code. */
16421 if (section_type == SHT_MIPS_DWARF)
16422 section_type = SHT_PROGBITS;
16423
16424 obj_elf_change_section (section_name, section_type, 0, section_flag,
16425 section_entry_size, 0, 0, 0);
16426
16427 if (now_seg->name != section_name)
16428 free (section_name);
16429 }
16430
16431 void
16432 mips_enable_auto_align (void)
16433 {
16434 auto_align = 1;
16435 }
16436
16437 static void
16438 s_cons (int log_size)
16439 {
16440 segment_info_type *si = seg_info (now_seg);
16441 struct insn_label_list *l = si->label_list;
16442
16443 mips_emit_delays ();
16444 if (log_size > 0 && auto_align)
16445 mips_align (log_size, 0, l);
16446 cons (1 << log_size);
16447 mips_clear_insn_labels ();
16448 }
16449
16450 static void
16451 s_float_cons (int type)
16452 {
16453 segment_info_type *si = seg_info (now_seg);
16454 struct insn_label_list *l = si->label_list;
16455
16456 mips_emit_delays ();
16457
16458 if (auto_align)
16459 {
16460 if (type == 'd')
16461 mips_align (3, 0, l);
16462 else
16463 mips_align (2, 0, l);
16464 }
16465
16466 float_cons (type);
16467 mips_clear_insn_labels ();
16468 }
16469
16470 /* Handle .globl. We need to override it because on Irix 5 you are
16471 permitted to say
16472 .globl foo .text
16473 where foo is an undefined symbol, to mean that foo should be
16474 considered to be the address of a function. */
16475
16476 static void
16477 s_mips_globl (int x ATTRIBUTE_UNUSED)
16478 {
16479 char *name;
16480 int c;
16481 symbolS *symbolP;
16482
16483 do
16484 {
16485 c = get_symbol_name (&name);
16486 symbolP = symbol_find_or_make (name);
16487 S_SET_EXTERNAL (symbolP);
16488
16489 *input_line_pointer = c;
16490 SKIP_WHITESPACE_AFTER_NAME ();
16491
16492 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16493 && (*input_line_pointer != ','))
16494 {
16495 char *secname;
16496 asection *sec;
16497
16498 c = get_symbol_name (&secname);
16499 sec = bfd_get_section_by_name (stdoutput, secname);
16500 if (sec == NULL)
16501 as_bad (_("%s: no such section"), secname);
16502 (void) restore_line_pointer (c);
16503
16504 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16505 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
16506 }
16507
16508 c = *input_line_pointer;
16509 if (c == ',')
16510 {
16511 input_line_pointer++;
16512 SKIP_WHITESPACE ();
16513 if (is_end_of_line[(unsigned char) *input_line_pointer])
16514 c = '\n';
16515 }
16516 }
16517 while (c == ',');
16518
16519 demand_empty_rest_of_line ();
16520 }
16521
16522 #ifdef TE_IRIX
16523 /* The Irix 5 and 6 assemblers set the type of any common symbol and
16524 any undefined non-function symbol to STT_OBJECT. We try to be
16525 compatible, since newer Irix 5 and 6 linkers care. */
16526
16527 void
16528 mips_frob_symbol (symbolS *symp ATTRIBUTE_UNUSED)
16529 {
16530 /* This late in assembly we can set BSF_OBJECT indiscriminately
16531 and let elf.c:swap_out_syms sort out the symbol type. */
16532 flagword *flags = &symbol_get_bfdsym (symp)->flags;
16533 if ((*flags & (BSF_GLOBAL | BSF_WEAK)) != 0
16534 || !S_IS_DEFINED (symp))
16535 *flags |= BSF_OBJECT;
16536 }
16537 #endif
16538
16539 static void
16540 s_option (int x ATTRIBUTE_UNUSED)
16541 {
16542 char *opt;
16543 char c;
16544
16545 c = get_symbol_name (&opt);
16546
16547 if (*opt == 'O')
16548 {
16549 /* FIXME: What does this mean? */
16550 }
16551 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
16552 {
16553 int i;
16554
16555 i = atoi (opt + 3);
16556 if (i != 0 && i != 2)
16557 as_bad (_(".option pic%d not supported"), i);
16558 else if (mips_pic == VXWORKS_PIC)
16559 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16560 else if (i == 0)
16561 mips_pic = NO_PIC;
16562 else if (i == 2)
16563 {
16564 mips_pic = SVR4_PIC;
16565 mips_abicalls = TRUE;
16566 }
16567
16568 if (mips_pic == SVR4_PIC)
16569 {
16570 if (g_switch_seen && g_switch_value != 0)
16571 as_warn (_("-G may not be used with SVR4 PIC code"));
16572 g_switch_value = 0;
16573 bfd_set_gp_size (stdoutput, 0);
16574 }
16575 }
16576 else
16577 as_warn (_("unrecognized option \"%s\""), opt);
16578
16579 (void) restore_line_pointer (c);
16580 demand_empty_rest_of_line ();
16581 }
16582
16583 /* This structure is used to hold a stack of .set values. */
16584
16585 struct mips_option_stack
16586 {
16587 struct mips_option_stack *next;
16588 struct mips_set_options options;
16589 };
16590
16591 static struct mips_option_stack *mips_opts_stack;
16592
16593 /* Return status for .set/.module option handling. */
16594
16595 enum code_option_type
16596 {
16597 /* Unrecognized option. */
16598 OPTION_TYPE_BAD = -1,
16599
16600 /* Ordinary option. */
16601 OPTION_TYPE_NORMAL,
16602
16603 /* ISA changing option. */
16604 OPTION_TYPE_ISA
16605 };
16606
16607 /* Handle common .set/.module options. Return status indicating option
16608 type. */
16609
16610 static enum code_option_type
16611 parse_code_option (char * name)
16612 {
16613 bfd_boolean isa_set = FALSE;
16614 const struct mips_ase *ase;
16615
16616 if (strncmp (name, "at=", 3) == 0)
16617 {
16618 char *s = name + 3;
16619
16620 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16621 as_bad (_("unrecognized register name `%s'"), s);
16622 }
16623 else if (strcmp (name, "at") == 0)
16624 mips_opts.at = ATREG;
16625 else if (strcmp (name, "noat") == 0)
16626 mips_opts.at = ZERO;
16627 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16628 mips_opts.nomove = 0;
16629 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16630 mips_opts.nomove = 1;
16631 else if (strcmp (name, "bopt") == 0)
16632 mips_opts.nobopt = 0;
16633 else if (strcmp (name, "nobopt") == 0)
16634 mips_opts.nobopt = 1;
16635 else if (strcmp (name, "gp=32") == 0)
16636 mips_opts.gp = 32;
16637 else if (strcmp (name, "gp=64") == 0)
16638 mips_opts.gp = 64;
16639 else if (strcmp (name, "fp=32") == 0)
16640 mips_opts.fp = 32;
16641 else if (strcmp (name, "fp=xx") == 0)
16642 mips_opts.fp = 0;
16643 else if (strcmp (name, "fp=64") == 0)
16644 mips_opts.fp = 64;
16645 else if (strcmp (name, "softfloat") == 0)
16646 mips_opts.soft_float = 1;
16647 else if (strcmp (name, "hardfloat") == 0)
16648 mips_opts.soft_float = 0;
16649 else if (strcmp (name, "singlefloat") == 0)
16650 mips_opts.single_float = 1;
16651 else if (strcmp (name, "doublefloat") == 0)
16652 mips_opts.single_float = 0;
16653 else if (strcmp (name, "nooddspreg") == 0)
16654 mips_opts.oddspreg = 0;
16655 else if (strcmp (name, "oddspreg") == 0)
16656 mips_opts.oddspreg = 1;
16657 else if (strcmp (name, "mips16") == 0
16658 || strcmp (name, "MIPS-16") == 0)
16659 mips_opts.mips16 = 1;
16660 else if (strcmp (name, "nomips16") == 0
16661 || strcmp (name, "noMIPS-16") == 0)
16662 mips_opts.mips16 = 0;
16663 else if (strcmp (name, "micromips") == 0)
16664 mips_opts.micromips = 1;
16665 else if (strcmp (name, "nomicromips") == 0)
16666 mips_opts.micromips = 0;
16667 else if (name[0] == 'n'
16668 && name[1] == 'o'
16669 && (ase = mips_lookup_ase (name + 2)))
16670 mips_set_ase (ase, &mips_opts, FALSE);
16671 else if ((ase = mips_lookup_ase (name)))
16672 mips_set_ase (ase, &mips_opts, TRUE);
16673 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16674 {
16675 /* Permit the user to change the ISA and architecture on the fly.
16676 Needless to say, misuse can cause serious problems. */
16677 if (strncmp (name, "arch=", 5) == 0)
16678 {
16679 const struct mips_cpu_info *p;
16680
16681 p = mips_parse_cpu ("internal use", name + 5);
16682 if (!p)
16683 as_bad (_("unknown architecture %s"), name + 5);
16684 else
16685 {
16686 mips_opts.arch = p->cpu;
16687 mips_opts.isa = p->isa;
16688 isa_set = TRUE;
16689 mips_opts.init_ase = p->ase;
16690 }
16691 }
16692 else if (strncmp (name, "mips", 4) == 0)
16693 {
16694 const struct mips_cpu_info *p;
16695
16696 p = mips_parse_cpu ("internal use", name);
16697 if (!p)
16698 as_bad (_("unknown ISA level %s"), name + 4);
16699 else
16700 {
16701 mips_opts.arch = p->cpu;
16702 mips_opts.isa = p->isa;
16703 isa_set = TRUE;
16704 mips_opts.init_ase = p->ase;
16705 }
16706 }
16707 else
16708 as_bad (_("unknown ISA or architecture %s"), name);
16709 }
16710 else if (strcmp (name, "autoextend") == 0)
16711 mips_opts.noautoextend = 0;
16712 else if (strcmp (name, "noautoextend") == 0)
16713 mips_opts.noautoextend = 1;
16714 else if (strcmp (name, "insn32") == 0)
16715 mips_opts.insn32 = TRUE;
16716 else if (strcmp (name, "noinsn32") == 0)
16717 mips_opts.insn32 = FALSE;
16718 else if (strcmp (name, "sym32") == 0)
16719 mips_opts.sym32 = TRUE;
16720 else if (strcmp (name, "nosym32") == 0)
16721 mips_opts.sym32 = FALSE;
16722 else
16723 return OPTION_TYPE_BAD;
16724
16725 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
16726 }
16727
16728 /* Handle the .set pseudo-op. */
16729
16730 static void
16731 s_mipsset (int x ATTRIBUTE_UNUSED)
16732 {
16733 enum code_option_type type = OPTION_TYPE_NORMAL;
16734 char *name = input_line_pointer, ch;
16735
16736 file_mips_check_options ();
16737
16738 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16739 ++input_line_pointer;
16740 ch = *input_line_pointer;
16741 *input_line_pointer = '\0';
16742
16743 if (strchr (name, ','))
16744 {
16745 /* Generic ".set" directive; use the generic handler. */
16746 *input_line_pointer = ch;
16747 input_line_pointer = name;
16748 s_set (0);
16749 return;
16750 }
16751
16752 if (strcmp (name, "reorder") == 0)
16753 {
16754 if (mips_opts.noreorder)
16755 end_noreorder ();
16756 }
16757 else if (strcmp (name, "noreorder") == 0)
16758 {
16759 if (!mips_opts.noreorder)
16760 start_noreorder ();
16761 }
16762 else if (strcmp (name, "macro") == 0)
16763 mips_opts.warn_about_macros = 0;
16764 else if (strcmp (name, "nomacro") == 0)
16765 {
16766 if (mips_opts.noreorder == 0)
16767 as_bad (_("`noreorder' must be set before `nomacro'"));
16768 mips_opts.warn_about_macros = 1;
16769 }
16770 else if (strcmp (name, "gp=default") == 0)
16771 mips_opts.gp = file_mips_opts.gp;
16772 else if (strcmp (name, "fp=default") == 0)
16773 mips_opts.fp = file_mips_opts.fp;
16774 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16775 {
16776 mips_opts.isa = file_mips_opts.isa;
16777 mips_opts.arch = file_mips_opts.arch;
16778 mips_opts.init_ase = file_mips_opts.init_ase;
16779 mips_opts.gp = file_mips_opts.gp;
16780 mips_opts.fp = file_mips_opts.fp;
16781 }
16782 else if (strcmp (name, "push") == 0)
16783 {
16784 struct mips_option_stack *s;
16785
16786 s = XNEW (struct mips_option_stack);
16787 s->next = mips_opts_stack;
16788 s->options = mips_opts;
16789 mips_opts_stack = s;
16790 }
16791 else if (strcmp (name, "pop") == 0)
16792 {
16793 struct mips_option_stack *s;
16794
16795 s = mips_opts_stack;
16796 if (s == NULL)
16797 as_bad (_(".set pop with no .set push"));
16798 else
16799 {
16800 /* If we're changing the reorder mode we need to handle
16801 delay slots correctly. */
16802 if (s->options.noreorder && ! mips_opts.noreorder)
16803 start_noreorder ();
16804 else if (! s->options.noreorder && mips_opts.noreorder)
16805 end_noreorder ();
16806
16807 mips_opts = s->options;
16808 mips_opts_stack = s->next;
16809 free (s);
16810 }
16811 }
16812 else
16813 {
16814 type = parse_code_option (name);
16815 if (type == OPTION_TYPE_BAD)
16816 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16817 }
16818
16819 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16820 registers based on what is supported by the arch/cpu. */
16821 if (type == OPTION_TYPE_ISA)
16822 {
16823 switch (mips_opts.isa)
16824 {
16825 case 0:
16826 break;
16827 case ISA_MIPS1:
16828 /* MIPS I cannot support FPXX. */
16829 mips_opts.fp = 32;
16830 /* fall-through. */
16831 case ISA_MIPS2:
16832 case ISA_MIPS32:
16833 case ISA_MIPS32R2:
16834 case ISA_MIPS32R3:
16835 case ISA_MIPS32R5:
16836 mips_opts.gp = 32;
16837 if (mips_opts.fp != 0)
16838 mips_opts.fp = 32;
16839 break;
16840 case ISA_MIPS32R6:
16841 mips_opts.gp = 32;
16842 mips_opts.fp = 64;
16843 break;
16844 case ISA_MIPS3:
16845 case ISA_MIPS4:
16846 case ISA_MIPS5:
16847 case ISA_MIPS64:
16848 case ISA_MIPS64R2:
16849 case ISA_MIPS64R3:
16850 case ISA_MIPS64R5:
16851 case ISA_MIPS64R6:
16852 mips_opts.gp = 64;
16853 if (mips_opts.fp != 0)
16854 {
16855 if (mips_opts.arch == CPU_R5900)
16856 mips_opts.fp = 32;
16857 else
16858 mips_opts.fp = 64;
16859 }
16860 break;
16861 default:
16862 as_bad (_("unknown ISA level %s"), name + 4);
16863 break;
16864 }
16865 }
16866
16867 mips_check_options (&mips_opts, FALSE);
16868
16869 mips_check_isa_supports_ases ();
16870 *input_line_pointer = ch;
16871 demand_empty_rest_of_line ();
16872 }
16873
16874 /* Handle the .module pseudo-op. */
16875
16876 static void
16877 s_module (int ignore ATTRIBUTE_UNUSED)
16878 {
16879 char *name = input_line_pointer, ch;
16880
16881 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16882 ++input_line_pointer;
16883 ch = *input_line_pointer;
16884 *input_line_pointer = '\0';
16885
16886 if (!file_mips_opts_checked)
16887 {
16888 if (parse_code_option (name) == OPTION_TYPE_BAD)
16889 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16890
16891 /* Update module level settings from mips_opts. */
16892 file_mips_opts = mips_opts;
16893 }
16894 else
16895 as_bad (_(".module is not permitted after generating code"));
16896
16897 *input_line_pointer = ch;
16898 demand_empty_rest_of_line ();
16899 }
16900
16901 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
16902 .option pic2. It means to generate SVR4 PIC calls. */
16903
16904 static void
16905 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16906 {
16907 mips_pic = SVR4_PIC;
16908 mips_abicalls = TRUE;
16909
16910 if (g_switch_seen && g_switch_value != 0)
16911 as_warn (_("-G may not be used with SVR4 PIC code"));
16912 g_switch_value = 0;
16913
16914 bfd_set_gp_size (stdoutput, 0);
16915 demand_empty_rest_of_line ();
16916 }
16917
16918 /* Handle the .cpload pseudo-op. This is used when generating SVR4
16919 PIC code. It sets the $gp register for the function based on the
16920 function address, which is in the register named in the argument.
16921 This uses a relocation against _gp_disp, which is handled specially
16922 by the linker. The result is:
16923 lui $gp,%hi(_gp_disp)
16924 addiu $gp,$gp,%lo(_gp_disp)
16925 addu $gp,$gp,.cpload argument
16926 The .cpload argument is normally $25 == $t9.
16927
16928 The -mno-shared option changes this to:
16929 lui $gp,%hi(__gnu_local_gp)
16930 addiu $gp,$gp,%lo(__gnu_local_gp)
16931 and the argument is ignored. This saves an instruction, but the
16932 resulting code is not position independent; it uses an absolute
16933 address for __gnu_local_gp. Thus code assembled with -mno-shared
16934 can go into an ordinary executable, but not into a shared library. */
16935
16936 static void
16937 s_cpload (int ignore ATTRIBUTE_UNUSED)
16938 {
16939 expressionS ex;
16940 int reg;
16941 int in_shared;
16942
16943 file_mips_check_options ();
16944
16945 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16946 .cpload is ignored. */
16947 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16948 {
16949 s_ignore (0);
16950 return;
16951 }
16952
16953 if (mips_opts.mips16)
16954 {
16955 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16956 ignore_rest_of_line ();
16957 return;
16958 }
16959
16960 /* .cpload should be in a .set noreorder section. */
16961 if (mips_opts.noreorder == 0)
16962 as_warn (_(".cpload not in noreorder section"));
16963
16964 reg = tc_get_register (0);
16965
16966 /* If we need to produce a 64-bit address, we are better off using
16967 the default instruction sequence. */
16968 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16969
16970 ex.X_op = O_symbol;
16971 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16972 "__gnu_local_gp");
16973 ex.X_op_symbol = NULL;
16974 ex.X_add_number = 0;
16975
16976 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16977 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16978
16979 mips_mark_labels ();
16980 mips_assembling_insn = TRUE;
16981
16982 macro_start ();
16983 macro_build_lui (&ex, mips_gp_register);
16984 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16985 mips_gp_register, BFD_RELOC_LO16);
16986 if (in_shared)
16987 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16988 mips_gp_register, reg);
16989 macro_end ();
16990
16991 mips_assembling_insn = FALSE;
16992 demand_empty_rest_of_line ();
16993 }
16994
16995 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16996 .cpsetup $reg1, offset|$reg2, label
16997
16998 If offset is given, this results in:
16999 sd $gp, offset($sp)
17000 lui $gp, %hi(%neg(%gp_rel(label)))
17001 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
17002 daddu $gp, $gp, $reg1
17003
17004 If $reg2 is given, this results in:
17005 or $reg2, $gp, $0
17006 lui $gp, %hi(%neg(%gp_rel(label)))
17007 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
17008 daddu $gp, $gp, $reg1
17009 $reg1 is normally $25 == $t9.
17010
17011 The -mno-shared option replaces the last three instructions with
17012 lui $gp,%hi(_gp)
17013 addiu $gp,$gp,%lo(_gp) */
17014
17015 static void
17016 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
17017 {
17018 expressionS ex_off;
17019 expressionS ex_sym;
17020 int reg1;
17021
17022 file_mips_check_options ();
17023
17024 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
17025 We also need NewABI support. */
17026 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17027 {
17028 s_ignore (0);
17029 return;
17030 }
17031
17032 if (mips_opts.mips16)
17033 {
17034 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
17035 ignore_rest_of_line ();
17036 return;
17037 }
17038
17039 reg1 = tc_get_register (0);
17040 SKIP_WHITESPACE ();
17041 if (*input_line_pointer != ',')
17042 {
17043 as_bad (_("missing argument separator ',' for .cpsetup"));
17044 return;
17045 }
17046 else
17047 ++input_line_pointer;
17048 SKIP_WHITESPACE ();
17049 if (*input_line_pointer == '$')
17050 {
17051 mips_cpreturn_register = tc_get_register (0);
17052 mips_cpreturn_offset = -1;
17053 }
17054 else
17055 {
17056 mips_cpreturn_offset = get_absolute_expression ();
17057 mips_cpreturn_register = -1;
17058 }
17059 SKIP_WHITESPACE ();
17060 if (*input_line_pointer != ',')
17061 {
17062 as_bad (_("missing argument separator ',' for .cpsetup"));
17063 return;
17064 }
17065 else
17066 ++input_line_pointer;
17067 SKIP_WHITESPACE ();
17068 expression (&ex_sym);
17069
17070 mips_mark_labels ();
17071 mips_assembling_insn = TRUE;
17072
17073 macro_start ();
17074 if (mips_cpreturn_register == -1)
17075 {
17076 ex_off.X_op = O_constant;
17077 ex_off.X_add_symbol = NULL;
17078 ex_off.X_op_symbol = NULL;
17079 ex_off.X_add_number = mips_cpreturn_offset;
17080
17081 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17082 BFD_RELOC_LO16, SP);
17083 }
17084 else
17085 move_register (mips_cpreturn_register, mips_gp_register);
17086
17087 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
17088 {
17089 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
17090 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
17091 BFD_RELOC_HI16_S);
17092
17093 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
17094 mips_gp_register, -1, BFD_RELOC_GPREL16,
17095 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
17096
17097 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
17098 mips_gp_register, reg1);
17099 }
17100 else
17101 {
17102 expressionS ex;
17103
17104 ex.X_op = O_symbol;
17105 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
17106 ex.X_op_symbol = NULL;
17107 ex.X_add_number = 0;
17108
17109 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
17110 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
17111
17112 macro_build_lui (&ex, mips_gp_register);
17113 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17114 mips_gp_register, BFD_RELOC_LO16);
17115 }
17116
17117 macro_end ();
17118
17119 mips_assembling_insn = FALSE;
17120 demand_empty_rest_of_line ();
17121 }
17122
17123 static void
17124 s_cplocal (int ignore ATTRIBUTE_UNUSED)
17125 {
17126 file_mips_check_options ();
17127
17128 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
17129 .cplocal is ignored. */
17130 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17131 {
17132 s_ignore (0);
17133 return;
17134 }
17135
17136 if (mips_opts.mips16)
17137 {
17138 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
17139 ignore_rest_of_line ();
17140 return;
17141 }
17142
17143 mips_gp_register = tc_get_register (0);
17144 demand_empty_rest_of_line ();
17145 }
17146
17147 /* Handle the .cprestore pseudo-op. This stores $gp into a given
17148 offset from $sp. The offset is remembered, and after making a PIC
17149 call $gp is restored from that location. */
17150
17151 static void
17152 s_cprestore (int ignore ATTRIBUTE_UNUSED)
17153 {
17154 expressionS ex;
17155
17156 file_mips_check_options ();
17157
17158 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
17159 .cprestore is ignored. */
17160 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
17161 {
17162 s_ignore (0);
17163 return;
17164 }
17165
17166 if (mips_opts.mips16)
17167 {
17168 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
17169 ignore_rest_of_line ();
17170 return;
17171 }
17172
17173 mips_cprestore_offset = get_absolute_expression ();
17174 mips_cprestore_valid = 1;
17175
17176 ex.X_op = O_constant;
17177 ex.X_add_symbol = NULL;
17178 ex.X_op_symbol = NULL;
17179 ex.X_add_number = mips_cprestore_offset;
17180
17181 mips_mark_labels ();
17182 mips_assembling_insn = TRUE;
17183
17184 macro_start ();
17185 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
17186 SP, HAVE_64BIT_ADDRESSES);
17187 macro_end ();
17188
17189 mips_assembling_insn = FALSE;
17190 demand_empty_rest_of_line ();
17191 }
17192
17193 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
17194 was given in the preceding .cpsetup, it results in:
17195 ld $gp, offset($sp)
17196
17197 If a register $reg2 was given there, it results in:
17198 or $gp, $reg2, $0 */
17199
17200 static void
17201 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
17202 {
17203 expressionS ex;
17204
17205 file_mips_check_options ();
17206
17207 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
17208 We also need NewABI support. */
17209 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17210 {
17211 s_ignore (0);
17212 return;
17213 }
17214
17215 if (mips_opts.mips16)
17216 {
17217 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
17218 ignore_rest_of_line ();
17219 return;
17220 }
17221
17222 mips_mark_labels ();
17223 mips_assembling_insn = TRUE;
17224
17225 macro_start ();
17226 if (mips_cpreturn_register == -1)
17227 {
17228 ex.X_op = O_constant;
17229 ex.X_add_symbol = NULL;
17230 ex.X_op_symbol = NULL;
17231 ex.X_add_number = mips_cpreturn_offset;
17232
17233 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
17234 }
17235 else
17236 move_register (mips_gp_register, mips_cpreturn_register);
17237
17238 macro_end ();
17239
17240 mips_assembling_insn = FALSE;
17241 demand_empty_rest_of_line ();
17242 }
17243
17244 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17245 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17246 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17247 debug information or MIPS16 TLS. */
17248
17249 static void
17250 s_tls_rel_directive (const size_t bytes, const char *dirstr,
17251 bfd_reloc_code_real_type rtype)
17252 {
17253 expressionS ex;
17254 char *p;
17255
17256 expression (&ex);
17257
17258 if (ex.X_op != O_symbol)
17259 {
17260 as_bad (_("unsupported use of %s"), dirstr);
17261 ignore_rest_of_line ();
17262 }
17263
17264 p = frag_more (bytes);
17265 md_number_to_chars (p, 0, bytes);
17266 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
17267 demand_empty_rest_of_line ();
17268 mips_clear_insn_labels ();
17269 }
17270
17271 /* Handle .dtprelword. */
17272
17273 static void
17274 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17275 {
17276 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
17277 }
17278
17279 /* Handle .dtpreldword. */
17280
17281 static void
17282 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17283 {
17284 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17285 }
17286
17287 /* Handle .tprelword. */
17288
17289 static void
17290 s_tprelword (int ignore ATTRIBUTE_UNUSED)
17291 {
17292 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17293 }
17294
17295 /* Handle .tpreldword. */
17296
17297 static void
17298 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17299 {
17300 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
17301 }
17302
17303 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17304 code. It sets the offset to use in gp_rel relocations. */
17305
17306 static void
17307 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
17308 {
17309 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17310 We also need NewABI support. */
17311 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17312 {
17313 s_ignore (0);
17314 return;
17315 }
17316
17317 mips_gprel_offset = get_absolute_expression ();
17318
17319 demand_empty_rest_of_line ();
17320 }
17321
17322 /* Handle the .gpword pseudo-op. This is used when generating PIC
17323 code. It generates a 32 bit GP relative reloc. */
17324
17325 static void
17326 s_gpword (int ignore ATTRIBUTE_UNUSED)
17327 {
17328 segment_info_type *si;
17329 struct insn_label_list *l;
17330 expressionS ex;
17331 char *p;
17332
17333 /* When not generating PIC code, this is treated as .word. */
17334 if (mips_pic != SVR4_PIC)
17335 {
17336 s_cons (2);
17337 return;
17338 }
17339
17340 si = seg_info (now_seg);
17341 l = si->label_list;
17342 mips_emit_delays ();
17343 if (auto_align)
17344 mips_align (2, 0, l);
17345
17346 expression (&ex);
17347 mips_clear_insn_labels ();
17348
17349 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17350 {
17351 as_bad (_("unsupported use of .gpword"));
17352 ignore_rest_of_line ();
17353 }
17354
17355 p = frag_more (4);
17356 md_number_to_chars (p, 0, 4);
17357 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17358 BFD_RELOC_GPREL32);
17359
17360 demand_empty_rest_of_line ();
17361 }
17362
17363 static void
17364 s_gpdword (int ignore ATTRIBUTE_UNUSED)
17365 {
17366 segment_info_type *si;
17367 struct insn_label_list *l;
17368 expressionS ex;
17369 char *p;
17370
17371 /* When not generating PIC code, this is treated as .dword. */
17372 if (mips_pic != SVR4_PIC)
17373 {
17374 s_cons (3);
17375 return;
17376 }
17377
17378 si = seg_info (now_seg);
17379 l = si->label_list;
17380 mips_emit_delays ();
17381 if (auto_align)
17382 mips_align (3, 0, l);
17383
17384 expression (&ex);
17385 mips_clear_insn_labels ();
17386
17387 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17388 {
17389 as_bad (_("unsupported use of .gpdword"));
17390 ignore_rest_of_line ();
17391 }
17392
17393 p = frag_more (8);
17394 md_number_to_chars (p, 0, 8);
17395 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17396 BFD_RELOC_GPREL32)->fx_tcbit = 1;
17397
17398 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
17399 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17400 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
17401
17402 demand_empty_rest_of_line ();
17403 }
17404
17405 /* Handle the .ehword pseudo-op. This is used when generating unwinding
17406 tables. It generates a R_MIPS_EH reloc. */
17407
17408 static void
17409 s_ehword (int ignore ATTRIBUTE_UNUSED)
17410 {
17411 expressionS ex;
17412 char *p;
17413
17414 mips_emit_delays ();
17415
17416 expression (&ex);
17417 mips_clear_insn_labels ();
17418
17419 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17420 {
17421 as_bad (_("unsupported use of .ehword"));
17422 ignore_rest_of_line ();
17423 }
17424
17425 p = frag_more (4);
17426 md_number_to_chars (p, 0, 4);
17427 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17428 BFD_RELOC_32_PCREL);
17429
17430 demand_empty_rest_of_line ();
17431 }
17432
17433 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
17434 tables in SVR4 PIC code. */
17435
17436 static void
17437 s_cpadd (int ignore ATTRIBUTE_UNUSED)
17438 {
17439 int reg;
17440
17441 file_mips_check_options ();
17442
17443 /* This is ignored when not generating SVR4 PIC code. */
17444 if (mips_pic != SVR4_PIC)
17445 {
17446 s_ignore (0);
17447 return;
17448 }
17449
17450 mips_mark_labels ();
17451 mips_assembling_insn = TRUE;
17452
17453 /* Add $gp to the register named as an argument. */
17454 macro_start ();
17455 reg = tc_get_register (0);
17456 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
17457 macro_end ();
17458
17459 mips_assembling_insn = FALSE;
17460 demand_empty_rest_of_line ();
17461 }
17462
17463 /* Handle the .insn pseudo-op. This marks instruction labels in
17464 mips16/micromips mode. This permits the linker to handle them specially,
17465 such as generating jalx instructions when needed. We also make
17466 them odd for the duration of the assembly, in order to generate the
17467 right sort of code. We will make them even in the adjust_symtab
17468 routine, while leaving them marked. This is convenient for the
17469 debugger and the disassembler. The linker knows to make them odd
17470 again. */
17471
17472 static void
17473 s_insn (int ignore ATTRIBUTE_UNUSED)
17474 {
17475 file_mips_check_options ();
17476 file_ase_mips16 |= mips_opts.mips16;
17477 file_ase_micromips |= mips_opts.micromips;
17478
17479 mips_mark_labels ();
17480
17481 demand_empty_rest_of_line ();
17482 }
17483
17484 /* Handle the .nan pseudo-op. */
17485
17486 static void
17487 s_nan (int ignore ATTRIBUTE_UNUSED)
17488 {
17489 static const char str_legacy[] = "legacy";
17490 static const char str_2008[] = "2008";
17491 size_t i;
17492
17493 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17494
17495 if (i == sizeof (str_2008) - 1
17496 && memcmp (input_line_pointer, str_2008, i) == 0)
17497 mips_nan2008 = 1;
17498 else if (i == sizeof (str_legacy) - 1
17499 && memcmp (input_line_pointer, str_legacy, i) == 0)
17500 {
17501 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17502 mips_nan2008 = 0;
17503 else
17504 as_bad (_("`%s' does not support legacy NaN"),
17505 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17506 }
17507 else
17508 as_bad (_("bad .nan directive"));
17509
17510 input_line_pointer += i;
17511 demand_empty_rest_of_line ();
17512 }
17513
17514 /* Handle a .stab[snd] directive. Ideally these directives would be
17515 implemented in a transparent way, so that removing them would not
17516 have any effect on the generated instructions. However, s_stab
17517 internally changes the section, so in practice we need to decide
17518 now whether the preceding label marks compressed code. We do not
17519 support changing the compression mode of a label after a .stab*
17520 directive, such as in:
17521
17522 foo:
17523 .stabs ...
17524 .set mips16
17525
17526 so the current mode wins. */
17527
17528 static void
17529 s_mips_stab (int type)
17530 {
17531 file_mips_check_options ();
17532 mips_mark_labels ();
17533 s_stab (type);
17534 }
17535
17536 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
17537
17538 static void
17539 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17540 {
17541 char *name;
17542 int c;
17543 symbolS *symbolP;
17544 expressionS exp;
17545
17546 c = get_symbol_name (&name);
17547 symbolP = symbol_find_or_make (name);
17548 S_SET_WEAK (symbolP);
17549 *input_line_pointer = c;
17550
17551 SKIP_WHITESPACE_AFTER_NAME ();
17552
17553 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17554 {
17555 if (S_IS_DEFINED (symbolP))
17556 {
17557 as_bad (_("ignoring attempt to redefine symbol %s"),
17558 S_GET_NAME (symbolP));
17559 ignore_rest_of_line ();
17560 return;
17561 }
17562
17563 if (*input_line_pointer == ',')
17564 {
17565 ++input_line_pointer;
17566 SKIP_WHITESPACE ();
17567 }
17568
17569 expression (&exp);
17570 if (exp.X_op != O_symbol)
17571 {
17572 as_bad (_("bad .weakext directive"));
17573 ignore_rest_of_line ();
17574 return;
17575 }
17576 symbol_set_value_expression (symbolP, &exp);
17577 }
17578
17579 demand_empty_rest_of_line ();
17580 }
17581
17582 /* Parse a register string into a number. Called from the ECOFF code
17583 to parse .frame. The argument is non-zero if this is the frame
17584 register, so that we can record it in mips_frame_reg. */
17585
17586 int
17587 tc_get_register (int frame)
17588 {
17589 unsigned int reg;
17590
17591 SKIP_WHITESPACE ();
17592 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17593 reg = 0;
17594 if (frame)
17595 {
17596 mips_frame_reg = reg != 0 ? reg : SP;
17597 mips_frame_reg_valid = 1;
17598 mips_cprestore_valid = 0;
17599 }
17600 return reg;
17601 }
17602
17603 valueT
17604 md_section_align (asection *seg, valueT addr)
17605 {
17606 int align = bfd_get_section_alignment (stdoutput, seg);
17607
17608 /* We don't need to align ELF sections to the full alignment.
17609 However, Irix 5 may prefer that we align them at least to a 16
17610 byte boundary. We don't bother to align the sections if we
17611 are targeted for an embedded system. */
17612 if (strncmp (TARGET_OS, "elf", 3) == 0)
17613 return addr;
17614 if (align > 4)
17615 align = 4;
17616
17617 return ((addr + (1 << align) - 1) & -(1 << align));
17618 }
17619
17620 /* Utility routine, called from above as well. If called while the
17621 input file is still being read, it's only an approximation. (For
17622 example, a symbol may later become defined which appeared to be
17623 undefined earlier.) */
17624
17625 static int
17626 nopic_need_relax (symbolS *sym, int before_relaxing)
17627 {
17628 if (sym == 0)
17629 return 0;
17630
17631 if (g_switch_value > 0)
17632 {
17633 const char *symname;
17634 int change;
17635
17636 /* Find out whether this symbol can be referenced off the $gp
17637 register. It can be if it is smaller than the -G size or if
17638 it is in the .sdata or .sbss section. Certain symbols can
17639 not be referenced off the $gp, although it appears as though
17640 they can. */
17641 symname = S_GET_NAME (sym);
17642 if (symname != (const char *) NULL
17643 && (strcmp (symname, "eprol") == 0
17644 || strcmp (symname, "etext") == 0
17645 || strcmp (symname, "_gp") == 0
17646 || strcmp (symname, "edata") == 0
17647 || strcmp (symname, "_fbss") == 0
17648 || strcmp (symname, "_fdata") == 0
17649 || strcmp (symname, "_ftext") == 0
17650 || strcmp (symname, "end") == 0
17651 || strcmp (symname, "_gp_disp") == 0))
17652 change = 1;
17653 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17654 && (0
17655 #ifndef NO_ECOFF_DEBUGGING
17656 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17657 && (symbol_get_obj (sym)->ecoff_extern_size
17658 <= g_switch_value))
17659 #endif
17660 /* We must defer this decision until after the whole
17661 file has been read, since there might be a .extern
17662 after the first use of this symbol. */
17663 || (before_relaxing
17664 #ifndef NO_ECOFF_DEBUGGING
17665 && symbol_get_obj (sym)->ecoff_extern_size == 0
17666 #endif
17667 && S_GET_VALUE (sym) == 0)
17668 || (S_GET_VALUE (sym) != 0
17669 && S_GET_VALUE (sym) <= g_switch_value)))
17670 change = 0;
17671 else
17672 {
17673 const char *segname;
17674
17675 segname = segment_name (S_GET_SEGMENT (sym));
17676 gas_assert (strcmp (segname, ".lit8") != 0
17677 && strcmp (segname, ".lit4") != 0);
17678 change = (strcmp (segname, ".sdata") != 0
17679 && strcmp (segname, ".sbss") != 0
17680 && strncmp (segname, ".sdata.", 7) != 0
17681 && strncmp (segname, ".sbss.", 6) != 0
17682 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17683 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17684 }
17685 return change;
17686 }
17687 else
17688 /* We are not optimizing for the $gp register. */
17689 return 1;
17690 }
17691
17692
17693 /* Return true if the given symbol should be considered local for SVR4 PIC. */
17694
17695 static bfd_boolean
17696 pic_need_relax (symbolS *sym)
17697 {
17698 asection *symsec;
17699
17700 /* Handle the case of a symbol equated to another symbol. */
17701 while (symbol_equated_reloc_p (sym))
17702 {
17703 symbolS *n;
17704
17705 /* It's possible to get a loop here in a badly written program. */
17706 n = symbol_get_value_expression (sym)->X_add_symbol;
17707 if (n == sym)
17708 break;
17709 sym = n;
17710 }
17711
17712 if (symbol_section_p (sym))
17713 return TRUE;
17714
17715 symsec = S_GET_SEGMENT (sym);
17716
17717 /* This must duplicate the test in adjust_reloc_syms. */
17718 return (!bfd_is_und_section (symsec)
17719 && !bfd_is_abs_section (symsec)
17720 && !bfd_is_com_section (symsec)
17721 /* A global or weak symbol is treated as external. */
17722 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
17723 }
17724 \f
17725 /* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17726 convert a section-relative value VAL to the equivalent PC-relative
17727 value. */
17728
17729 static offsetT
17730 mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17731 offsetT val, long stretch)
17732 {
17733 fragS *sym_frag;
17734 addressT addr;
17735
17736 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17737
17738 sym_frag = symbol_get_frag (fragp->fr_symbol);
17739
17740 /* If the relax_marker of the symbol fragment differs from the
17741 relax_marker of this fragment, we have not yet adjusted the
17742 symbol fragment fr_address. We want to add in STRETCH in
17743 order to get a better estimate of the address. This
17744 particularly matters because of the shift bits. */
17745 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17746 {
17747 fragS *f;
17748
17749 /* Adjust stretch for any alignment frag. Note that if have
17750 been expanding the earlier code, the symbol may be
17751 defined in what appears to be an earlier frag. FIXME:
17752 This doesn't handle the fr_subtype field, which specifies
17753 a maximum number of bytes to skip when doing an
17754 alignment. */
17755 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17756 {
17757 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17758 {
17759 if (stretch < 0)
17760 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17761 else
17762 stretch &= ~((1 << (int) f->fr_offset) - 1);
17763 if (stretch == 0)
17764 break;
17765 }
17766 }
17767 if (f != NULL)
17768 val += stretch;
17769 }
17770
17771 addr = fragp->fr_address + fragp->fr_fix;
17772
17773 /* The base address rules are complicated. The base address of
17774 a branch is the following instruction. The base address of a
17775 PC relative load or add is the instruction itself, but if it
17776 is in a delay slot (in which case it can not be extended) use
17777 the address of the instruction whose delay slot it is in. */
17778 if (pcrel_op->include_isa_bit)
17779 {
17780 addr += 2;
17781
17782 /* If we are currently assuming that this frag should be
17783 extended, then the current address is two bytes higher. */
17784 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17785 addr += 2;
17786
17787 /* Ignore the low bit in the target, since it will be set
17788 for a text label. */
17789 val &= -2;
17790 }
17791 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17792 addr -= 4;
17793 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17794 addr -= 2;
17795
17796 val -= addr & -(1 << pcrel_op->align_log2);
17797
17798 return val;
17799 }
17800
17801 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17802 extended opcode. SEC is the section the frag is in. */
17803
17804 static int
17805 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17806 {
17807 const struct mips_int_operand *operand;
17808 offsetT val;
17809 segT symsec;
17810 int type;
17811
17812 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17813 return 0;
17814 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17815 return 1;
17816
17817 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17818 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17819 operand = mips16_immed_operand (type, FALSE);
17820 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17821 || (operand->root.type == OP_PCREL
17822 ? sec != symsec
17823 : !bfd_is_abs_section (symsec)))
17824 return 1;
17825
17826 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17827
17828 if (operand->root.type == OP_PCREL)
17829 {
17830 const struct mips_pcrel_operand *pcrel_op;
17831 offsetT maxtiny;
17832
17833 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
17834 return 1;
17835
17836 pcrel_op = (const struct mips_pcrel_operand *) operand;
17837 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17838
17839 /* If any of the shifted bits are set, we must use an extended
17840 opcode. If the address depends on the size of this
17841 instruction, this can lead to a loop, so we arrange to always
17842 use an extended opcode. */
17843 if ((val & ((1 << operand->shift) - 1)) != 0)
17844 {
17845 fragp->fr_subtype =
17846 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17847 return 1;
17848 }
17849
17850 /* If we are about to mark a frag as extended because the value
17851 is precisely the next value above maxtiny, then there is a
17852 chance of an infinite loop as in the following code:
17853 la $4,foo
17854 .skip 1020
17855 .align 2
17856 foo:
17857 In this case when the la is extended, foo is 0x3fc bytes
17858 away, so the la can be shrunk, but then foo is 0x400 away, so
17859 the la must be extended. To avoid this loop, we mark the
17860 frag as extended if it was small, and is about to become
17861 extended with the next value above maxtiny. */
17862 maxtiny = mips_int_operand_max (operand);
17863 if (val == maxtiny + (1 << operand->shift)
17864 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17865 {
17866 fragp->fr_subtype =
17867 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17868 return 1;
17869 }
17870 }
17871
17872 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17873 }
17874
17875 /* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17876 macro expansion. SEC is the section the frag is in. We only
17877 support PC-relative instructions (LA, DLA, LW, LD) here, in
17878 non-PIC code using 32-bit addressing. */
17879
17880 static int
17881 mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17882 {
17883 const struct mips_pcrel_operand *pcrel_op;
17884 const struct mips_int_operand *operand;
17885 offsetT val;
17886 segT symsec;
17887 int type;
17888
17889 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17890
17891 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17892 return 0;
17893 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17894 return 0;
17895
17896 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17897 switch (type)
17898 {
17899 case 'A':
17900 case 'B':
17901 case 'E':
17902 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17903 if (bfd_is_abs_section (symsec))
17904 return 1;
17905 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17906 return 0;
17907 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17908 return 1;
17909
17910 operand = mips16_immed_operand (type, TRUE);
17911 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17912 pcrel_op = (const struct mips_pcrel_operand *) operand;
17913 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17914
17915 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17916
17917 default:
17918 return 0;
17919 }
17920 }
17921
17922 /* Compute the length of a branch sequence, and adjust the
17923 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17924 worst-case length is computed, with UPDATE being used to indicate
17925 whether an unconditional (-1), branch-likely (+1) or regular (0)
17926 branch is to be computed. */
17927 static int
17928 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17929 {
17930 bfd_boolean toofar;
17931 int length;
17932
17933 if (fragp
17934 && S_IS_DEFINED (fragp->fr_symbol)
17935 && !S_IS_WEAK (fragp->fr_symbol)
17936 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17937 {
17938 addressT addr;
17939 offsetT val;
17940
17941 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17942
17943 addr = fragp->fr_address + fragp->fr_fix + 4;
17944
17945 val -= addr;
17946
17947 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17948 }
17949 else
17950 /* If the symbol is not defined or it's in a different segment,
17951 we emit the long sequence. */
17952 toofar = TRUE;
17953
17954 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17955 fragp->fr_subtype
17956 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17957 RELAX_BRANCH_PIC (fragp->fr_subtype),
17958 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17959 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17960 RELAX_BRANCH_LINK (fragp->fr_subtype),
17961 toofar);
17962
17963 length = 4;
17964 if (toofar)
17965 {
17966 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17967 length += 8;
17968
17969 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
17970 {
17971 /* Additional space for PIC loading of target address. */
17972 length += 8;
17973 if (mips_opts.isa == ISA_MIPS1)
17974 /* Additional space for $at-stabilizing nop. */
17975 length += 4;
17976 }
17977
17978 /* If branch is conditional. */
17979 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17980 length += 8;
17981 }
17982
17983 return length;
17984 }
17985
17986 /* Get a FRAG's branch instruction delay slot size, either from the
17987 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17988 or SHORT_INSN_SIZE otherwise. */
17989
17990 static int
17991 frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
17992 {
17993 char *buf = fragp->fr_literal + fragp->fr_fix;
17994
17995 if (al)
17996 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
17997 else
17998 return short_insn_size;
17999 }
18000
18001 /* Compute the length of a branch sequence, and adjust the
18002 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
18003 worst-case length is computed, with UPDATE being used to indicate
18004 whether an unconditional (-1), or regular (0) branch is to be
18005 computed. */
18006
18007 static int
18008 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
18009 {
18010 bfd_boolean insn32 = TRUE;
18011 bfd_boolean nods = TRUE;
18012 bfd_boolean pic = TRUE;
18013 bfd_boolean al = TRUE;
18014 int short_insn_size;
18015 bfd_boolean toofar;
18016 int length;
18017
18018 if (fragp)
18019 {
18020 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18021 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18022 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18023 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18024 }
18025 short_insn_size = insn32 ? 4 : 2;
18026
18027 if (fragp
18028 && S_IS_DEFINED (fragp->fr_symbol)
18029 && !S_IS_WEAK (fragp->fr_symbol)
18030 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18031 {
18032 addressT addr;
18033 offsetT val;
18034
18035 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18036 /* Ignore the low bit in the target, since it will be set
18037 for a text label. */
18038 if ((val & 1) != 0)
18039 --val;
18040
18041 addr = fragp->fr_address + fragp->fr_fix + 4;
18042
18043 val -= addr;
18044
18045 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
18046 }
18047 else
18048 /* If the symbol is not defined or it's in a different segment,
18049 we emit the long sequence. */
18050 toofar = TRUE;
18051
18052 if (fragp && update
18053 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18054 fragp->fr_subtype = (toofar
18055 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
18056 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
18057
18058 length = 4;
18059 if (toofar)
18060 {
18061 bfd_boolean compact_known = fragp != NULL;
18062 bfd_boolean compact = FALSE;
18063 bfd_boolean uncond;
18064
18065 if (fragp)
18066 {
18067 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18068 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
18069 }
18070 else
18071 uncond = update < 0;
18072
18073 /* If label is out of range, we turn branch <br>:
18074
18075 <br> label # 4 bytes
18076 0:
18077
18078 into:
18079
18080 j label # 4 bytes
18081 nop # 2/4 bytes if
18082 # compact && (!PIC || insn32)
18083 0:
18084 */
18085 if ((!pic || insn32) && (!compact_known || compact))
18086 length += short_insn_size;
18087
18088 /* If assembling PIC code, we further turn:
18089
18090 j label # 4 bytes
18091
18092 into:
18093
18094 lw/ld at, %got(label)(gp) # 4 bytes
18095 d/addiu at, %lo(label) # 4 bytes
18096 jr/c at # 2/4 bytes
18097 */
18098 if (pic)
18099 length += 4 + short_insn_size;
18100
18101 /* Add an extra nop if the jump has no compact form and we need
18102 to fill the delay slot. */
18103 if ((!pic || al) && nods)
18104 length += (fragp
18105 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
18106 : short_insn_size);
18107
18108 /* If branch <br> is conditional, we prepend negated branch <brneg>:
18109
18110 <brneg> 0f # 4 bytes
18111 nop # 2/4 bytes if !compact
18112 */
18113 if (!uncond)
18114 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
18115 }
18116 else if (nods)
18117 {
18118 /* Add an extra nop to fill the delay slot. */
18119 gas_assert (fragp);
18120 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
18121 }
18122
18123 return length;
18124 }
18125
18126 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
18127 bit accordingly. */
18128
18129 static int
18130 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
18131 {
18132 bfd_boolean toofar;
18133
18134 if (fragp
18135 && S_IS_DEFINED (fragp->fr_symbol)
18136 && !S_IS_WEAK (fragp->fr_symbol)
18137 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18138 {
18139 addressT addr;
18140 offsetT val;
18141 int type;
18142
18143 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18144 /* Ignore the low bit in the target, since it will be set
18145 for a text label. */
18146 if ((val & 1) != 0)
18147 --val;
18148
18149 /* Assume this is a 2-byte branch. */
18150 addr = fragp->fr_address + fragp->fr_fix + 2;
18151
18152 /* We try to avoid the infinite loop by not adding 2 more bytes for
18153 long branches. */
18154
18155 val -= addr;
18156
18157 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18158 if (type == 'D')
18159 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
18160 else if (type == 'E')
18161 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
18162 else
18163 abort ();
18164 }
18165 else
18166 /* If the symbol is not defined or it's in a different segment,
18167 we emit a normal 32-bit branch. */
18168 toofar = TRUE;
18169
18170 if (fragp && update
18171 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18172 fragp->fr_subtype
18173 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
18174 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
18175
18176 if (toofar)
18177 return 4;
18178
18179 return 2;
18180 }
18181
18182 /* Estimate the size of a frag before relaxing. Unless this is the
18183 mips16, we are not really relaxing here, and the final size is
18184 encoded in the subtype information. For the mips16, we have to
18185 decide whether we are using an extended opcode or not. */
18186
18187 int
18188 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
18189 {
18190 int change;
18191
18192 if (RELAX_BRANCH_P (fragp->fr_subtype))
18193 {
18194
18195 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
18196
18197 return fragp->fr_var;
18198 }
18199
18200 if (RELAX_MIPS16_P (fragp->fr_subtype))
18201 {
18202 /* We don't want to modify the EXTENDED bit here; it might get us
18203 into infinite loops. We change it only in mips_relax_frag(). */
18204 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18205 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
18206 else
18207 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
18208 }
18209
18210 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18211 {
18212 int length = 4;
18213
18214 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18215 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
18216 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18217 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
18218 fragp->fr_var = length;
18219
18220 return length;
18221 }
18222
18223 if (mips_pic == VXWORKS_PIC)
18224 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
18225 change = 0;
18226 else if (RELAX_PIC (fragp->fr_subtype))
18227 change = pic_need_relax (fragp->fr_symbol);
18228 else
18229 change = nopic_need_relax (fragp->fr_symbol, 0);
18230
18231 if (change)
18232 {
18233 fragp->fr_subtype |= RELAX_USE_SECOND;
18234 return -RELAX_FIRST (fragp->fr_subtype);
18235 }
18236 else
18237 return -RELAX_SECOND (fragp->fr_subtype);
18238 }
18239
18240 /* This is called to see whether a reloc against a defined symbol
18241 should be converted into a reloc against a section. */
18242
18243 int
18244 mips_fix_adjustable (fixS *fixp)
18245 {
18246 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
18247 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18248 return 0;
18249
18250 if (fixp->fx_addsy == NULL)
18251 return 1;
18252
18253 /* Allow relocs used for EH tables. */
18254 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
18255 return 1;
18256
18257 /* If symbol SYM is in a mergeable section, relocations of the form
18258 SYM + 0 can usually be made section-relative. The mergeable data
18259 is then identified by the section offset rather than by the symbol.
18260
18261 However, if we're generating REL LO16 relocations, the offset is split
18262 between the LO16 and partnering high part relocation. The linker will
18263 need to recalculate the complete offset in order to correctly identify
18264 the merge data.
18265
18266 The linker has traditionally not looked for the partnering high part
18267 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
18268 placed anywhere. Rather than break backwards compatibility by changing
18269 this, it seems better not to force the issue, and instead keep the
18270 original symbol. This will work with either linker behavior. */
18271 if ((lo16_reloc_p (fixp->fx_r_type)
18272 || reloc_needs_lo_p (fixp->fx_r_type))
18273 && HAVE_IN_PLACE_ADDENDS
18274 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
18275 return 0;
18276
18277 /* There is no place to store an in-place offset for JALR relocations. */
18278 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
18279 return 0;
18280
18281 /* Likewise an in-range offset of limited PC-relative relocations may
18282 overflow the in-place relocatable field if recalculated against the
18283 start address of the symbol's containing section.
18284
18285 Also, PC relative relocations for MIPS R6 need to be symbol rather than
18286 section relative to allow linker relaxations to be performed later on. */
18287 if (limited_pcrel_reloc_p (fixp->fx_r_type)
18288 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
18289 return 0;
18290
18291 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
18292 to a floating-point stub. The same is true for non-R_MIPS16_26
18293 relocations against MIPS16 functions; in this case, the stub becomes
18294 the function's canonical address.
18295
18296 Floating-point stubs are stored in unique .mips16.call.* or
18297 .mips16.fn.* sections. If a stub T for function F is in section S,
18298 the first relocation in section S must be against F; this is how the
18299 linker determines the target function. All relocations that might
18300 resolve to T must also be against F. We therefore have the following
18301 restrictions, which are given in an intentionally-redundant way:
18302
18303 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
18304 symbols.
18305
18306 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18307 if that stub might be used.
18308
18309 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18310 symbols.
18311
18312 4. We cannot reduce a stub's relocations against MIPS16 symbols if
18313 that stub might be used.
18314
18315 There is a further restriction:
18316
18317 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
18318 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
18319 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
18320 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
18321 against MIPS16 or microMIPS symbols because we need to keep the
18322 MIPS16 or microMIPS symbol for the purpose of mode mismatch
18323 detection and JAL or BAL to JALX instruction conversion in the
18324 linker.
18325
18326 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
18327 against a MIPS16 symbol. We deal with (5) by additionally leaving
18328 alone any jump and branch relocations against a microMIPS symbol.
18329
18330 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18331 relocation against some symbol R, no relocation against R may be
18332 reduced. (Note that this deals with (2) as well as (1) because
18333 relocations against global symbols will never be reduced on ELF
18334 targets.) This approach is a little simpler than trying to detect
18335 stub sections, and gives the "all or nothing" per-symbol consistency
18336 that we have for MIPS16 symbols. */
18337 if (fixp->fx_subsy == NULL
18338 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
18339 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
18340 && (jmp_reloc_p (fixp->fx_r_type)
18341 || b_reloc_p (fixp->fx_r_type)))
18342 || *symbol_get_tc (fixp->fx_addsy)))
18343 return 0;
18344
18345 return 1;
18346 }
18347
18348 /* Translate internal representation of relocation info to BFD target
18349 format. */
18350
18351 arelent **
18352 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
18353 {
18354 static arelent *retval[4];
18355 arelent *reloc;
18356 bfd_reloc_code_real_type code;
18357
18358 memset (retval, 0, sizeof(retval));
18359 reloc = retval[0] = XCNEW (arelent);
18360 reloc->sym_ptr_ptr = XNEW (asymbol *);
18361 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
18362 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18363
18364 if (fixp->fx_pcrel)
18365 {
18366 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
18367 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
18368 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18369 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
18370 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
18371 || fixp->fx_r_type == BFD_RELOC_32_PCREL
18372 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18373 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18374 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18375 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18376 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18377 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
18378
18379 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18380 Relocations want only the symbol offset. */
18381 switch (fixp->fx_r_type)
18382 {
18383 case BFD_RELOC_MIPS_18_PCREL_S3:
18384 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18385 break;
18386 default:
18387 reloc->addend = fixp->fx_addnumber + reloc->address;
18388 break;
18389 }
18390 }
18391 else if (HAVE_IN_PLACE_ADDENDS
18392 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18393 && (read_compressed_insn (fixp->fx_frag->fr_literal
18394 + fixp->fx_where, 4) >> 26) == 0x3c)
18395 {
18396 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
18397 addend accordingly. */
18398 reloc->addend = fixp->fx_addnumber >> 1;
18399 }
18400 else
18401 reloc->addend = fixp->fx_addnumber;
18402
18403 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18404 entry to be used in the relocation's section offset. */
18405 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18406 {
18407 reloc->address = reloc->addend;
18408 reloc->addend = 0;
18409 }
18410
18411 code = fixp->fx_r_type;
18412
18413 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
18414 if (reloc->howto == NULL)
18415 {
18416 as_bad_where (fixp->fx_file, fixp->fx_line,
18417 _("cannot represent %s relocation in this object file"
18418 " format"),
18419 bfd_get_reloc_code_name (code));
18420 retval[0] = NULL;
18421 }
18422
18423 return retval;
18424 }
18425
18426 /* Relax a machine dependent frag. This returns the amount by which
18427 the current size of the frag should change. */
18428
18429 int
18430 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
18431 {
18432 if (RELAX_BRANCH_P (fragp->fr_subtype))
18433 {
18434 offsetT old_var = fragp->fr_var;
18435
18436 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
18437
18438 return fragp->fr_var - old_var;
18439 }
18440
18441 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18442 {
18443 offsetT old_var = fragp->fr_var;
18444 offsetT new_var = 4;
18445
18446 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18447 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18448 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18449 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18450 fragp->fr_var = new_var;
18451
18452 return new_var - old_var;
18453 }
18454
18455 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18456 return 0;
18457
18458 if (!mips16_extended_frag (fragp, sec, stretch))
18459 {
18460 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18461 {
18462 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18463 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
18464 }
18465 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18466 {
18467 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18468 return -2;
18469 }
18470 else
18471 return 0;
18472 }
18473 else if (!mips16_macro_frag (fragp, sec, stretch))
18474 {
18475 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18476 {
18477 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18478 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18479 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
18480 }
18481 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18482 {
18483 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18484 return 2;
18485 }
18486 else
18487 return 0;
18488 }
18489 else
18490 {
18491 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18492 return 0;
18493 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18494 {
18495 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18496 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18497 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
18498 }
18499 else
18500 {
18501 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18502 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
18503 }
18504 }
18505
18506 return 0;
18507 }
18508
18509 /* Convert a machine dependent frag. */
18510
18511 void
18512 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
18513 {
18514 if (RELAX_BRANCH_P (fragp->fr_subtype))
18515 {
18516 char *buf;
18517 unsigned long insn;
18518 fixS *fixp;
18519
18520 buf = fragp->fr_literal + fragp->fr_fix;
18521 insn = read_insn (buf);
18522
18523 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18524 {
18525 /* We generate a fixup instead of applying it right now
18526 because, if there are linker relaxations, we're going to
18527 need the relocations. */
18528 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18529 fragp->fr_symbol, fragp->fr_offset,
18530 TRUE, BFD_RELOC_16_PCREL_S2);
18531 fixp->fx_file = fragp->fr_file;
18532 fixp->fx_line = fragp->fr_line;
18533
18534 buf = write_insn (buf, insn);
18535 }
18536 else
18537 {
18538 int i;
18539
18540 as_warn_where (fragp->fr_file, fragp->fr_line,
18541 _("relaxed out-of-range branch into a jump"));
18542
18543 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18544 goto uncond;
18545
18546 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18547 {
18548 /* Reverse the branch. */
18549 switch ((insn >> 28) & 0xf)
18550 {
18551 case 4:
18552 if ((insn & 0xff000000) == 0x47000000
18553 || (insn & 0xff600000) == 0x45600000)
18554 {
18555 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18556 reversed by tweaking bit 23. */
18557 insn ^= 0x00800000;
18558 }
18559 else
18560 {
18561 /* bc[0-3][tf]l? instructions can have the condition
18562 reversed by tweaking a single TF bit, and their
18563 opcodes all have 0x4???????. */
18564 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18565 insn ^= 0x00010000;
18566 }
18567 break;
18568
18569 case 0:
18570 /* bltz 0x04000000 bgez 0x04010000
18571 bltzal 0x04100000 bgezal 0x04110000 */
18572 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
18573 insn ^= 0x00010000;
18574 break;
18575
18576 case 1:
18577 /* beq 0x10000000 bne 0x14000000
18578 blez 0x18000000 bgtz 0x1c000000 */
18579 insn ^= 0x04000000;
18580 break;
18581
18582 default:
18583 abort ();
18584 }
18585 }
18586
18587 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18588 {
18589 /* Clear the and-link bit. */
18590 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
18591
18592 /* bltzal 0x04100000 bgezal 0x04110000
18593 bltzall 0x04120000 bgezall 0x04130000 */
18594 insn &= ~0x00100000;
18595 }
18596
18597 /* Branch over the branch (if the branch was likely) or the
18598 full jump (not likely case). Compute the offset from the
18599 current instruction to branch to. */
18600 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18601 i = 16;
18602 else
18603 {
18604 /* How many bytes in instructions we've already emitted? */
18605 i = buf - fragp->fr_literal - fragp->fr_fix;
18606 /* How many bytes in instructions from here to the end? */
18607 i = fragp->fr_var - i;
18608 }
18609 /* Convert to instruction count. */
18610 i >>= 2;
18611 /* Branch counts from the next instruction. */
18612 i--;
18613 insn |= i;
18614 /* Branch over the jump. */
18615 buf = write_insn (buf, insn);
18616
18617 /* nop */
18618 buf = write_insn (buf, 0);
18619
18620 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18621 {
18622 /* beql $0, $0, 2f */
18623 insn = 0x50000000;
18624 /* Compute the PC offset from the current instruction to
18625 the end of the variable frag. */
18626 /* How many bytes in instructions we've already emitted? */
18627 i = buf - fragp->fr_literal - fragp->fr_fix;
18628 /* How many bytes in instructions from here to the end? */
18629 i = fragp->fr_var - i;
18630 /* Convert to instruction count. */
18631 i >>= 2;
18632 /* Don't decrement i, because we want to branch over the
18633 delay slot. */
18634 insn |= i;
18635
18636 buf = write_insn (buf, insn);
18637 buf = write_insn (buf, 0);
18638 }
18639
18640 uncond:
18641 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
18642 {
18643 /* j or jal. */
18644 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18645 ? 0x0c000000 : 0x08000000);
18646
18647 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18648 fragp->fr_symbol, fragp->fr_offset,
18649 FALSE, BFD_RELOC_MIPS_JMP);
18650 fixp->fx_file = fragp->fr_file;
18651 fixp->fx_line = fragp->fr_line;
18652
18653 buf = write_insn (buf, insn);
18654 }
18655 else
18656 {
18657 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18658
18659 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
18660 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18661 insn |= at << OP_SH_RT;
18662
18663 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18664 fragp->fr_symbol, fragp->fr_offset,
18665 FALSE, BFD_RELOC_MIPS_GOT16);
18666 fixp->fx_file = fragp->fr_file;
18667 fixp->fx_line = fragp->fr_line;
18668
18669 buf = write_insn (buf, insn);
18670
18671 if (mips_opts.isa == ISA_MIPS1)
18672 /* nop */
18673 buf = write_insn (buf, 0);
18674
18675 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
18676 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18677 insn |= at << OP_SH_RS | at << OP_SH_RT;
18678
18679 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18680 fragp->fr_symbol, fragp->fr_offset,
18681 FALSE, BFD_RELOC_LO16);
18682 fixp->fx_file = fragp->fr_file;
18683 fixp->fx_line = fragp->fr_line;
18684
18685 buf = write_insn (buf, insn);
18686
18687 /* j(al)r $at. */
18688 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18689 insn = 0x0000f809;
18690 else
18691 insn = 0x00000008;
18692 insn |= at << OP_SH_RS;
18693
18694 buf = write_insn (buf, insn);
18695 }
18696 }
18697
18698 fragp->fr_fix += fragp->fr_var;
18699 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18700 return;
18701 }
18702
18703 /* Relax microMIPS branches. */
18704 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18705 {
18706 char *buf = fragp->fr_literal + fragp->fr_fix;
18707 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18708 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18709 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18710 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18711 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18712 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18713 bfd_boolean short_ds;
18714 unsigned long insn;
18715 fixS *fixp;
18716
18717 fragp->fr_fix += fragp->fr_var;
18718
18719 /* Handle 16-bit branches that fit or are forced to fit. */
18720 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18721 {
18722 /* We generate a fixup instead of applying it right now,
18723 because if there is linker relaxation, we're going to
18724 need the relocations. */
18725 switch (type)
18726 {
18727 case 'D':
18728 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18729 fragp->fr_symbol, fragp->fr_offset,
18730 TRUE, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18731 break;
18732 case 'E':
18733 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18734 fragp->fr_symbol, fragp->fr_offset,
18735 TRUE, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18736 break;
18737 default:
18738 abort ();
18739 }
18740
18741 fixp->fx_file = fragp->fr_file;
18742 fixp->fx_line = fragp->fr_line;
18743
18744 /* These relocations can have an addend that won't fit in
18745 2 octets. */
18746 fixp->fx_no_overflow = 1;
18747
18748 return;
18749 }
18750
18751 /* Handle 32-bit branches that fit or are forced to fit. */
18752 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18753 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18754 {
18755 /* We generate a fixup instead of applying it right now,
18756 because if there is linker relaxation, we're going to
18757 need the relocations. */
18758 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18759 fragp->fr_symbol, fragp->fr_offset,
18760 TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
18761 fixp->fx_file = fragp->fr_file;
18762 fixp->fx_line = fragp->fr_line;
18763
18764 if (type == 0)
18765 {
18766 insn = read_compressed_insn (buf, 4);
18767 buf += 4;
18768
18769 if (nods)
18770 {
18771 /* Check the short-delay-slot bit. */
18772 if (!al || (insn & 0x02000000) != 0)
18773 buf = write_compressed_insn (buf, 0x0c00, 2);
18774 else
18775 buf = write_compressed_insn (buf, 0x00000000, 4);
18776 }
18777
18778 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18779 return;
18780 }
18781 }
18782
18783 /* Relax 16-bit branches to 32-bit branches. */
18784 if (type != 0)
18785 {
18786 insn = read_compressed_insn (buf, 2);
18787
18788 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18789 insn = 0x94000000; /* beq */
18790 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18791 {
18792 unsigned long regno;
18793
18794 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18795 regno = micromips_to_32_reg_d_map [regno];
18796 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18797 insn |= regno << MICROMIPSOP_SH_RS;
18798 }
18799 else
18800 abort ();
18801
18802 /* Nothing else to do, just write it out. */
18803 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18804 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18805 {
18806 buf = write_compressed_insn (buf, insn, 4);
18807 if (nods)
18808 buf = write_compressed_insn (buf, 0x0c00, 2);
18809 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18810 return;
18811 }
18812 }
18813 else
18814 insn = read_compressed_insn (buf, 4);
18815
18816 /* Relax 32-bit branches to a sequence of instructions. */
18817 as_warn_where (fragp->fr_file, fragp->fr_line,
18818 _("relaxed out-of-range branch into a jump"));
18819
18820 /* Set the short-delay-slot bit. */
18821 short_ds = !al || (insn & 0x02000000) != 0;
18822
18823 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18824 {
18825 symbolS *l;
18826
18827 /* Reverse the branch. */
18828 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18829 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18830 insn ^= 0x20000000;
18831 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18832 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18833 || (insn & 0xffe00000) == 0x40800000 /* blez */
18834 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18835 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18836 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18837 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18838 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18839 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18840 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18841 insn ^= 0x00400000;
18842 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18843 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18844 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18845 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18846 insn ^= 0x00200000;
18847 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18848 BNZ.df */
18849 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18850 BNZ.V */
18851 insn ^= 0x00800000;
18852 else
18853 abort ();
18854
18855 if (al)
18856 {
18857 /* Clear the and-link and short-delay-slot bits. */
18858 gas_assert ((insn & 0xfda00000) == 0x40200000);
18859
18860 /* bltzal 0x40200000 bgezal 0x40600000 */
18861 /* bltzals 0x42200000 bgezals 0x42600000 */
18862 insn &= ~0x02200000;
18863 }
18864
18865 /* Make a label at the end for use with the branch. */
18866 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18867 micromips_label_inc ();
18868 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18869
18870 /* Refer to it. */
18871 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18872 BFD_RELOC_MICROMIPS_16_PCREL_S1);
18873 fixp->fx_file = fragp->fr_file;
18874 fixp->fx_line = fragp->fr_line;
18875
18876 /* Branch over the jump. */
18877 buf = write_compressed_insn (buf, insn, 4);
18878
18879 if (!compact)
18880 {
18881 /* nop */
18882 if (insn32)
18883 buf = write_compressed_insn (buf, 0x00000000, 4);
18884 else
18885 buf = write_compressed_insn (buf, 0x0c00, 2);
18886 }
18887 }
18888
18889 if (!pic)
18890 {
18891 unsigned long jal = (short_ds || nods
18892 ? 0x74000000 : 0xf4000000); /* jal/s */
18893
18894 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18895 insn = al ? jal : 0xd4000000;
18896
18897 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18898 fragp->fr_symbol, fragp->fr_offset,
18899 FALSE, BFD_RELOC_MICROMIPS_JMP);
18900 fixp->fx_file = fragp->fr_file;
18901 fixp->fx_line = fragp->fr_line;
18902
18903 buf = write_compressed_insn (buf, insn, 4);
18904
18905 if (compact || nods)
18906 {
18907 /* nop */
18908 if (insn32)
18909 buf = write_compressed_insn (buf, 0x00000000, 4);
18910 else
18911 buf = write_compressed_insn (buf, 0x0c00, 2);
18912 }
18913 }
18914 else
18915 {
18916 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18917
18918 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18919 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18920 insn |= at << MICROMIPSOP_SH_RT;
18921
18922 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18923 fragp->fr_symbol, fragp->fr_offset,
18924 FALSE, BFD_RELOC_MICROMIPS_GOT16);
18925 fixp->fx_file = fragp->fr_file;
18926 fixp->fx_line = fragp->fr_line;
18927
18928 buf = write_compressed_insn (buf, insn, 4);
18929
18930 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18931 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18932 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18933
18934 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18935 fragp->fr_symbol, fragp->fr_offset,
18936 FALSE, BFD_RELOC_MICROMIPS_LO16);
18937 fixp->fx_file = fragp->fr_file;
18938 fixp->fx_line = fragp->fr_line;
18939
18940 buf = write_compressed_insn (buf, insn, 4);
18941
18942 if (insn32)
18943 {
18944 /* jr/jalr $at */
18945 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18946 insn |= at << MICROMIPSOP_SH_RS;
18947
18948 buf = write_compressed_insn (buf, insn, 4);
18949
18950 if (compact || nods)
18951 /* nop */
18952 buf = write_compressed_insn (buf, 0x00000000, 4);
18953 }
18954 else
18955 {
18956 /* jr/jrc/jalr/jalrs $at */
18957 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18958 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
18959
18960 insn = al ? jalr : jr;
18961 insn |= at << MICROMIPSOP_SH_MJ;
18962
18963 buf = write_compressed_insn (buf, insn, 2);
18964 if (al && nods)
18965 {
18966 /* nop */
18967 if (short_ds)
18968 buf = write_compressed_insn (buf, 0x0c00, 2);
18969 else
18970 buf = write_compressed_insn (buf, 0x00000000, 4);
18971 }
18972 }
18973 }
18974
18975 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18976 return;
18977 }
18978
18979 if (RELAX_MIPS16_P (fragp->fr_subtype))
18980 {
18981 int type;
18982 const struct mips_int_operand *operand;
18983 offsetT val;
18984 char *buf;
18985 unsigned int user_length;
18986 bfd_boolean need_reloc;
18987 unsigned long insn;
18988 bfd_boolean mac;
18989 bfd_boolean ext;
18990 segT symsec;
18991
18992 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18993 operand = mips16_immed_operand (type, FALSE);
18994
18995 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
18996 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
18997 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
18998
18999 symsec = S_GET_SEGMENT (fragp->fr_symbol);
19000 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
19001 || (operand->root.type == OP_PCREL && !mac
19002 ? asec != symsec
19003 : !bfd_is_abs_section (symsec)));
19004
19005 if (operand->root.type == OP_PCREL && !mac)
19006 {
19007 const struct mips_pcrel_operand *pcrel_op;
19008
19009 pcrel_op = (const struct mips_pcrel_operand *) operand;
19010
19011 if (pcrel_op->include_isa_bit && !need_reloc)
19012 {
19013 if (!mips_ignore_branch_isa
19014 && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
19015 as_bad_where (fragp->fr_file, fragp->fr_line,
19016 _("branch to a symbol in another ISA mode"));
19017 else if ((fragp->fr_offset & 0x1) != 0)
19018 as_bad_where (fragp->fr_file, fragp->fr_line,
19019 _("branch to misaligned address (0x%lx)"),
19020 (long) (resolve_symbol_value (fragp->fr_symbol)
19021 + (fragp->fr_offset & ~1)));
19022 }
19023
19024 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
19025
19026 /* Make sure the section winds up with the alignment we have
19027 assumed. */
19028 if (operand->shift > 0)
19029 record_alignment (asec, operand->shift);
19030 }
19031
19032 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
19033 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
19034 {
19035 if (mac)
19036 as_warn_where (fragp->fr_file, fragp->fr_line,
19037 _("macro instruction expanded into multiple "
19038 "instructions in a branch delay slot"));
19039 else if (ext)
19040 as_warn_where (fragp->fr_file, fragp->fr_line,
19041 _("extended instruction in a branch delay slot"));
19042 }
19043 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
19044 as_warn_where (fragp->fr_file, fragp->fr_line,
19045 _("macro instruction expanded into multiple "
19046 "instructions"));
19047
19048 buf = fragp->fr_literal + fragp->fr_fix;
19049
19050 insn = read_compressed_insn (buf, 2);
19051 if (ext)
19052 insn |= MIPS16_EXTEND;
19053
19054 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
19055 user_length = 4;
19056 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
19057 user_length = 2;
19058 else
19059 user_length = 0;
19060
19061 if (mac)
19062 {
19063 unsigned long reg;
19064 unsigned long new;
19065 unsigned long op;
19066 bfd_boolean e2;
19067
19068 gas_assert (type == 'A' || type == 'B' || type == 'E');
19069 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
19070
19071 e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
19072
19073 if (need_reloc)
19074 {
19075 fixS *fixp;
19076
19077 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
19078
19079 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19080 fragp->fr_symbol, fragp->fr_offset,
19081 FALSE, BFD_RELOC_MIPS16_HI16_S);
19082 fixp->fx_file = fragp->fr_file;
19083 fixp->fx_line = fragp->fr_line;
19084
19085 fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
19086 fragp->fr_symbol, fragp->fr_offset,
19087 FALSE, BFD_RELOC_MIPS16_LO16);
19088 fixp->fx_file = fragp->fr_file;
19089 fixp->fx_line = fragp->fr_line;
19090
19091 val = 0;
19092 }
19093
19094 switch (insn & 0xf800)
19095 {
19096 case 0x0800: /* ADDIU */
19097 reg = (insn >> 8) & 0x7;
19098 op = 0xf0004800 | (reg << 8);
19099 break;
19100 case 0xb000: /* LW */
19101 reg = (insn >> 8) & 0x7;
19102 op = 0xf0009800 | (reg << 8) | (reg << 5);
19103 break;
19104 case 0xf800: /* I64 */
19105 reg = (insn >> 5) & 0x7;
19106 switch (insn & 0x0700)
19107 {
19108 case 0x0400: /* LD */
19109 op = 0xf0003800 | (reg << 8) | (reg << 5);
19110 break;
19111 case 0x0600: /* DADDIU */
19112 op = 0xf000fd00 | (reg << 5);
19113 break;
19114 default:
19115 abort ();
19116 }
19117 break;
19118 default:
19119 abort ();
19120 }
19121
19122 new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8); /* LUI/LI */
19123 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
19124 buf = write_compressed_insn (buf, new, 4);
19125 if (!e2)
19126 {
19127 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
19128 buf = write_compressed_insn (buf, new, 4);
19129 }
19130 op |= mips16_immed_extend (val, 16);
19131 buf = write_compressed_insn (buf, op, 4);
19132
19133 fragp->fr_fix += e2 ? 8 : 12;
19134 }
19135 else
19136 {
19137 unsigned int length = ext ? 4 : 2;
19138
19139 if (need_reloc)
19140 {
19141 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
19142 fixS *fixp;
19143
19144 switch (type)
19145 {
19146 case 'p':
19147 case 'q':
19148 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
19149 break;
19150 default:
19151 break;
19152 }
19153 if (mac || reloc == BFD_RELOC_NONE)
19154 as_bad_where (fragp->fr_file, fragp->fr_line,
19155 _("unsupported relocation"));
19156 else if (ext)
19157 {
19158 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19159 fragp->fr_symbol, fragp->fr_offset,
19160 TRUE, reloc);
19161 fixp->fx_file = fragp->fr_file;
19162 fixp->fx_line = fragp->fr_line;
19163 }
19164 else
19165 as_bad_where (fragp->fr_file, fragp->fr_line,
19166 _("invalid unextended operand value"));
19167 }
19168 else
19169 mips16_immed (fragp->fr_file, fragp->fr_line, type,
19170 BFD_RELOC_UNUSED, val, user_length, &insn);
19171
19172 gas_assert (mips16_opcode_length (insn) == length);
19173 write_compressed_insn (buf, insn, length);
19174 fragp->fr_fix += length;
19175 }
19176 }
19177 else
19178 {
19179 relax_substateT subtype = fragp->fr_subtype;
19180 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
19181 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
19182 unsigned int first, second;
19183 fixS *fixp;
19184
19185 first = RELAX_FIRST (subtype);
19186 second = RELAX_SECOND (subtype);
19187 fixp = (fixS *) fragp->fr_opcode;
19188
19189 /* If the delay slot chosen does not match the size of the instruction,
19190 then emit a warning. */
19191 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
19192 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
19193 {
19194 relax_substateT s;
19195 const char *msg;
19196
19197 s = subtype & (RELAX_DELAY_SLOT_16BIT
19198 | RELAX_DELAY_SLOT_SIZE_FIRST
19199 | RELAX_DELAY_SLOT_SIZE_SECOND);
19200 msg = macro_warning (s);
19201 if (msg != NULL)
19202 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
19203 subtype &= ~s;
19204 }
19205
19206 /* Possibly emit a warning if we've chosen the longer option. */
19207 if (use_second == second_longer)
19208 {
19209 relax_substateT s;
19210 const char *msg;
19211
19212 s = (subtype
19213 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
19214 msg = macro_warning (s);
19215 if (msg != NULL)
19216 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
19217 subtype &= ~s;
19218 }
19219
19220 /* Go through all the fixups for the first sequence. Disable them
19221 (by marking them as done) if we're going to use the second
19222 sequence instead. */
19223 while (fixp
19224 && fixp->fx_frag == fragp
19225 && fixp->fx_where + second < fragp->fr_fix)
19226 {
19227 if (subtype & RELAX_USE_SECOND)
19228 fixp->fx_done = 1;
19229 fixp = fixp->fx_next;
19230 }
19231
19232 /* Go through the fixups for the second sequence. Disable them if
19233 we're going to use the first sequence, otherwise adjust their
19234 addresses to account for the relaxation. */
19235 while (fixp && fixp->fx_frag == fragp)
19236 {
19237 if (subtype & RELAX_USE_SECOND)
19238 fixp->fx_where -= first;
19239 else
19240 fixp->fx_done = 1;
19241 fixp = fixp->fx_next;
19242 }
19243
19244 /* Now modify the frag contents. */
19245 if (subtype & RELAX_USE_SECOND)
19246 {
19247 char *start;
19248
19249 start = fragp->fr_literal + fragp->fr_fix - first - second;
19250 memmove (start, start + first, second);
19251 fragp->fr_fix -= first;
19252 }
19253 else
19254 fragp->fr_fix -= second;
19255 }
19256 }
19257
19258 /* This function is called after the relocs have been generated.
19259 We've been storing mips16 text labels as odd. Here we convert them
19260 back to even for the convenience of the debugger. */
19261
19262 void
19263 mips_frob_file_after_relocs (void)
19264 {
19265 asymbol **syms;
19266 unsigned int count, i;
19267
19268 syms = bfd_get_outsymbols (stdoutput);
19269 count = bfd_get_symcount (stdoutput);
19270 for (i = 0; i < count; i++, syms++)
19271 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
19272 && ((*syms)->value & 1) != 0)
19273 {
19274 (*syms)->value &= ~1;
19275 /* If the symbol has an odd size, it was probably computed
19276 incorrectly, so adjust that as well. */
19277 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
19278 ++elf_symbol (*syms)->internal_elf_sym.st_size;
19279 }
19280 }
19281
19282 /* This function is called whenever a label is defined, including fake
19283 labels instantiated off the dot special symbol. It is used when
19284 handling branch delays; if a branch has a label, we assume we cannot
19285 move it. This also bumps the value of the symbol by 1 in compressed
19286 code. */
19287
19288 static void
19289 mips_record_label (symbolS *sym)
19290 {
19291 segment_info_type *si = seg_info (now_seg);
19292 struct insn_label_list *l;
19293
19294 if (free_insn_labels == NULL)
19295 l = XNEW (struct insn_label_list);
19296 else
19297 {
19298 l = free_insn_labels;
19299 free_insn_labels = l->next;
19300 }
19301
19302 l->label = sym;
19303 l->next = si->label_list;
19304 si->label_list = l;
19305 }
19306
19307 /* This function is called as tc_frob_label() whenever a label is defined
19308 and adds a DWARF-2 record we only want for true labels. */
19309
19310 void
19311 mips_define_label (symbolS *sym)
19312 {
19313 mips_record_label (sym);
19314 dwarf2_emit_label (sym);
19315 }
19316
19317 /* This function is called by tc_new_dot_label whenever a new dot symbol
19318 is defined. */
19319
19320 void
19321 mips_add_dot_label (symbolS *sym)
19322 {
19323 mips_record_label (sym);
19324 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
19325 mips_compressed_mark_label (sym);
19326 }
19327 \f
19328 /* Converting ASE flags from internal to .MIPS.abiflags values. */
19329 static unsigned int
19330 mips_convert_ase_flags (int ase)
19331 {
19332 unsigned int ext_ases = 0;
19333
19334 if (ase & ASE_DSP)
19335 ext_ases |= AFL_ASE_DSP;
19336 if (ase & ASE_DSPR2)
19337 ext_ases |= AFL_ASE_DSPR2;
19338 if (ase & ASE_DSPR3)
19339 ext_ases |= AFL_ASE_DSPR3;
19340 if (ase & ASE_EVA)
19341 ext_ases |= AFL_ASE_EVA;
19342 if (ase & ASE_MCU)
19343 ext_ases |= AFL_ASE_MCU;
19344 if (ase & ASE_MDMX)
19345 ext_ases |= AFL_ASE_MDMX;
19346 if (ase & ASE_MIPS3D)
19347 ext_ases |= AFL_ASE_MIPS3D;
19348 if (ase & ASE_MT)
19349 ext_ases |= AFL_ASE_MT;
19350 if (ase & ASE_SMARTMIPS)
19351 ext_ases |= AFL_ASE_SMARTMIPS;
19352 if (ase & ASE_VIRT)
19353 ext_ases |= AFL_ASE_VIRT;
19354 if (ase & ASE_MSA)
19355 ext_ases |= AFL_ASE_MSA;
19356 if (ase & ASE_XPA)
19357 ext_ases |= AFL_ASE_XPA;
19358 if (ase & ASE_MIPS16E2)
19359 ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
19360 if (ase & ASE_CRC)
19361 ext_ases |= AFL_ASE_CRC;
19362 if (ase & ASE_GINV)
19363 ext_ases |= AFL_ASE_GINV;
19364 if (ase & ASE_LOONGSON_MMI)
19365 ext_ases |= AFL_ASE_LOONGSON_MMI;
19366 if (ase & ASE_LOONGSON_CAM)
19367 ext_ases |= AFL_ASE_LOONGSON_CAM;
19368 if (ase & ASE_LOONGSON_EXT)
19369 ext_ases |= AFL_ASE_LOONGSON_EXT;
19370 if (ase & ASE_LOONGSON_EXT2)
19371 ext_ases |= AFL_ASE_LOONGSON_EXT2;
19372
19373 return ext_ases;
19374 }
19375 /* Some special processing for a MIPS ELF file. */
19376
19377 void
19378 mips_elf_final_processing (void)
19379 {
19380 int fpabi;
19381 Elf_Internal_ABIFlags_v0 flags;
19382
19383 flags.version = 0;
19384 flags.isa_rev = 0;
19385 switch (file_mips_opts.isa)
19386 {
19387 case INSN_ISA1:
19388 flags.isa_level = 1;
19389 break;
19390 case INSN_ISA2:
19391 flags.isa_level = 2;
19392 break;
19393 case INSN_ISA3:
19394 flags.isa_level = 3;
19395 break;
19396 case INSN_ISA4:
19397 flags.isa_level = 4;
19398 break;
19399 case INSN_ISA5:
19400 flags.isa_level = 5;
19401 break;
19402 case INSN_ISA32:
19403 flags.isa_level = 32;
19404 flags.isa_rev = 1;
19405 break;
19406 case INSN_ISA32R2:
19407 flags.isa_level = 32;
19408 flags.isa_rev = 2;
19409 break;
19410 case INSN_ISA32R3:
19411 flags.isa_level = 32;
19412 flags.isa_rev = 3;
19413 break;
19414 case INSN_ISA32R5:
19415 flags.isa_level = 32;
19416 flags.isa_rev = 5;
19417 break;
19418 case INSN_ISA32R6:
19419 flags.isa_level = 32;
19420 flags.isa_rev = 6;
19421 break;
19422 case INSN_ISA64:
19423 flags.isa_level = 64;
19424 flags.isa_rev = 1;
19425 break;
19426 case INSN_ISA64R2:
19427 flags.isa_level = 64;
19428 flags.isa_rev = 2;
19429 break;
19430 case INSN_ISA64R3:
19431 flags.isa_level = 64;
19432 flags.isa_rev = 3;
19433 break;
19434 case INSN_ISA64R5:
19435 flags.isa_level = 64;
19436 flags.isa_rev = 5;
19437 break;
19438 case INSN_ISA64R6:
19439 flags.isa_level = 64;
19440 flags.isa_rev = 6;
19441 break;
19442 }
19443
19444 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19445 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19446 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19447 : (file_mips_opts.fp == 64) ? AFL_REG_64
19448 : AFL_REG_32;
19449 flags.cpr2_size = AFL_REG_NONE;
19450 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19451 Tag_GNU_MIPS_ABI_FP);
19452 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19453 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19454 if (file_ase_mips16)
19455 flags.ases |= AFL_ASE_MIPS16;
19456 if (file_ase_micromips)
19457 flags.ases |= AFL_ASE_MICROMIPS;
19458 flags.flags1 = 0;
19459 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19460 || file_mips_opts.fp == 64)
19461 && file_mips_opts.oddspreg)
19462 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19463 flags.flags2 = 0;
19464
19465 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19466 ((Elf_External_ABIFlags_v0 *)
19467 mips_flags_frag));
19468
19469 /* Write out the register information. */
19470 if (mips_abi != N64_ABI)
19471 {
19472 Elf32_RegInfo s;
19473
19474 s.ri_gprmask = mips_gprmask;
19475 s.ri_cprmask[0] = mips_cprmask[0];
19476 s.ri_cprmask[1] = mips_cprmask[1];
19477 s.ri_cprmask[2] = mips_cprmask[2];
19478 s.ri_cprmask[3] = mips_cprmask[3];
19479 /* The gp_value field is set by the MIPS ELF backend. */
19480
19481 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19482 ((Elf32_External_RegInfo *)
19483 mips_regmask_frag));
19484 }
19485 else
19486 {
19487 Elf64_Internal_RegInfo s;
19488
19489 s.ri_gprmask = mips_gprmask;
19490 s.ri_pad = 0;
19491 s.ri_cprmask[0] = mips_cprmask[0];
19492 s.ri_cprmask[1] = mips_cprmask[1];
19493 s.ri_cprmask[2] = mips_cprmask[2];
19494 s.ri_cprmask[3] = mips_cprmask[3];
19495 /* The gp_value field is set by the MIPS ELF backend. */
19496
19497 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19498 ((Elf64_External_RegInfo *)
19499 mips_regmask_frag));
19500 }
19501
19502 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19503 sort of BFD interface for this. */
19504 if (mips_any_noreorder)
19505 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19506 if (mips_pic != NO_PIC)
19507 {
19508 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
19509 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19510 }
19511 if (mips_abicalls)
19512 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19513
19514 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19515 defined at present; this might need to change in future. */
19516 if (file_ase_mips16)
19517 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
19518 if (file_ase_micromips)
19519 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
19520 if (file_mips_opts.ase & ASE_MDMX)
19521 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
19522
19523 /* Set the MIPS ELF ABI flags. */
19524 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
19525 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
19526 else if (mips_abi == O64_ABI)
19527 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
19528 else if (mips_abi == EABI_ABI)
19529 {
19530 if (file_mips_opts.gp == 64)
19531 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19532 else
19533 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19534 }
19535
19536 /* Nothing to do for N32_ABI or N64_ABI. */
19537
19538 if (mips_32bitmode)
19539 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
19540
19541 if (mips_nan2008 == 1)
19542 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19543
19544 /* 32 bit code with 64 bit FP registers. */
19545 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19546 Tag_GNU_MIPS_ABI_FP);
19547 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
19548 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
19549 }
19550 \f
19551 typedef struct proc {
19552 symbolS *func_sym;
19553 symbolS *func_end_sym;
19554 unsigned long reg_mask;
19555 unsigned long reg_offset;
19556 unsigned long fpreg_mask;
19557 unsigned long fpreg_offset;
19558 unsigned long frame_offset;
19559 unsigned long frame_reg;
19560 unsigned long pc_reg;
19561 } procS;
19562
19563 static procS cur_proc;
19564 static procS *cur_proc_ptr;
19565 static int numprocs;
19566
19567 /* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19568 as "2", and a normal nop as "0". */
19569
19570 #define NOP_OPCODE_MIPS 0
19571 #define NOP_OPCODE_MIPS16 1
19572 #define NOP_OPCODE_MICROMIPS 2
19573
19574 char
19575 mips_nop_opcode (void)
19576 {
19577 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19578 return NOP_OPCODE_MICROMIPS;
19579 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19580 return NOP_OPCODE_MIPS16;
19581 else
19582 return NOP_OPCODE_MIPS;
19583 }
19584
19585 /* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19586 32-bit microMIPS NOPs here (if applicable). */
19587
19588 void
19589 mips_handle_align (fragS *fragp)
19590 {
19591 char nop_opcode;
19592 char *p;
19593 int bytes, size, excess;
19594 valueT opcode;
19595
19596 if (fragp->fr_type != rs_align_code)
19597 return;
19598
19599 p = fragp->fr_literal + fragp->fr_fix;
19600 nop_opcode = *p;
19601 switch (nop_opcode)
19602 {
19603 case NOP_OPCODE_MICROMIPS:
19604 opcode = micromips_nop32_insn.insn_opcode;
19605 size = 4;
19606 break;
19607 case NOP_OPCODE_MIPS16:
19608 opcode = mips16_nop_insn.insn_opcode;
19609 size = 2;
19610 break;
19611 case NOP_OPCODE_MIPS:
19612 default:
19613 opcode = nop_insn.insn_opcode;
19614 size = 4;
19615 break;
19616 }
19617
19618 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19619 excess = bytes % size;
19620
19621 /* Handle the leading part if we're not inserting a whole number of
19622 instructions, and make it the end of the fixed part of the frag.
19623 Try to fit in a short microMIPS NOP if applicable and possible,
19624 and use zeroes otherwise. */
19625 gas_assert (excess < 4);
19626 fragp->fr_fix += excess;
19627 switch (excess)
19628 {
19629 case 3:
19630 *p++ = '\0';
19631 /* Fall through. */
19632 case 2:
19633 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
19634 {
19635 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
19636 break;
19637 }
19638 *p++ = '\0';
19639 /* Fall through. */
19640 case 1:
19641 *p++ = '\0';
19642 /* Fall through. */
19643 case 0:
19644 break;
19645 }
19646
19647 md_number_to_chars (p, opcode, size);
19648 fragp->fr_var = size;
19649 }
19650
19651 static long
19652 get_number (void)
19653 {
19654 int negative = 0;
19655 long val = 0;
19656
19657 if (*input_line_pointer == '-')
19658 {
19659 ++input_line_pointer;
19660 negative = 1;
19661 }
19662 if (!ISDIGIT (*input_line_pointer))
19663 as_bad (_("expected simple number"));
19664 if (input_line_pointer[0] == '0')
19665 {
19666 if (input_line_pointer[1] == 'x')
19667 {
19668 input_line_pointer += 2;
19669 while (ISXDIGIT (*input_line_pointer))
19670 {
19671 val <<= 4;
19672 val |= hex_value (*input_line_pointer++);
19673 }
19674 return negative ? -val : val;
19675 }
19676 else
19677 {
19678 ++input_line_pointer;
19679 while (ISDIGIT (*input_line_pointer))
19680 {
19681 val <<= 3;
19682 val |= *input_line_pointer++ - '0';
19683 }
19684 return negative ? -val : val;
19685 }
19686 }
19687 if (!ISDIGIT (*input_line_pointer))
19688 {
19689 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19690 *input_line_pointer, *input_line_pointer);
19691 as_warn (_("invalid number"));
19692 return -1;
19693 }
19694 while (ISDIGIT (*input_line_pointer))
19695 {
19696 val *= 10;
19697 val += *input_line_pointer++ - '0';
19698 }
19699 return negative ? -val : val;
19700 }
19701
19702 /* The .file directive; just like the usual .file directive, but there
19703 is an initial number which is the ECOFF file index. In the non-ECOFF
19704 case .file implies DWARF-2. */
19705
19706 static void
19707 s_mips_file (int x ATTRIBUTE_UNUSED)
19708 {
19709 static int first_file_directive = 0;
19710
19711 if (ECOFF_DEBUGGING)
19712 {
19713 get_number ();
19714 s_app_file (0);
19715 }
19716 else
19717 {
19718 char *filename;
19719
19720 filename = dwarf2_directive_filename ();
19721
19722 /* Versions of GCC up to 3.1 start files with a ".file"
19723 directive even for stabs output. Make sure that this
19724 ".file" is handled. Note that you need a version of GCC
19725 after 3.1 in order to support DWARF-2 on MIPS. */
19726 if (filename != NULL && ! first_file_directive)
19727 {
19728 (void) new_logical_line (filename, -1);
19729 s_app_file_string (filename, 0);
19730 }
19731 first_file_directive = 1;
19732 }
19733 }
19734
19735 /* The .loc directive, implying DWARF-2. */
19736
19737 static void
19738 s_mips_loc (int x ATTRIBUTE_UNUSED)
19739 {
19740 if (!ECOFF_DEBUGGING)
19741 dwarf2_directive_loc (0);
19742 }
19743
19744 /* The .end directive. */
19745
19746 static void
19747 s_mips_end (int x ATTRIBUTE_UNUSED)
19748 {
19749 symbolS *p;
19750
19751 /* Following functions need their own .frame and .cprestore directives. */
19752 mips_frame_reg_valid = 0;
19753 mips_cprestore_valid = 0;
19754
19755 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19756 {
19757 p = get_symbol ();
19758 demand_empty_rest_of_line ();
19759 }
19760 else
19761 p = NULL;
19762
19763 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19764 as_warn (_(".end not in text section"));
19765
19766 if (!cur_proc_ptr)
19767 {
19768 as_warn (_(".end directive without a preceding .ent directive"));
19769 demand_empty_rest_of_line ();
19770 return;
19771 }
19772
19773 if (p != NULL)
19774 {
19775 gas_assert (S_GET_NAME (p));
19776 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
19777 as_warn (_(".end symbol does not match .ent symbol"));
19778
19779 if (debug_type == DEBUG_STABS)
19780 stabs_generate_asm_endfunc (S_GET_NAME (p),
19781 S_GET_NAME (p));
19782 }
19783 else
19784 as_warn (_(".end directive missing or unknown symbol"));
19785
19786 /* Create an expression to calculate the size of the function. */
19787 if (p && cur_proc_ptr)
19788 {
19789 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19790 expressionS *exp = XNEW (expressionS);
19791
19792 obj->size = exp;
19793 exp->X_op = O_subtract;
19794 exp->X_add_symbol = symbol_temp_new_now ();
19795 exp->X_op_symbol = p;
19796 exp->X_add_number = 0;
19797
19798 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19799 }
19800
19801 #ifdef md_flush_pending_output
19802 md_flush_pending_output ();
19803 #endif
19804
19805 /* Generate a .pdr section. */
19806 if (!ECOFF_DEBUGGING && mips_flag_pdr)
19807 {
19808 segT saved_seg = now_seg;
19809 subsegT saved_subseg = now_subseg;
19810 expressionS exp;
19811 char *fragp;
19812
19813 gas_assert (pdr_seg);
19814 subseg_set (pdr_seg, 0);
19815
19816 /* Write the symbol. */
19817 exp.X_op = O_symbol;
19818 exp.X_add_symbol = p;
19819 exp.X_add_number = 0;
19820 emit_expr (&exp, 4);
19821
19822 fragp = frag_more (7 * 4);
19823
19824 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19825 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19826 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19827 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19828 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19829 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19830 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19831
19832 subseg_set (saved_seg, saved_subseg);
19833 }
19834
19835 cur_proc_ptr = NULL;
19836 }
19837
19838 /* The .aent and .ent directives. */
19839
19840 static void
19841 s_mips_ent (int aent)
19842 {
19843 symbolS *symbolP;
19844
19845 symbolP = get_symbol ();
19846 if (*input_line_pointer == ',')
19847 ++input_line_pointer;
19848 SKIP_WHITESPACE ();
19849 if (ISDIGIT (*input_line_pointer)
19850 || *input_line_pointer == '-')
19851 get_number ();
19852
19853 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19854 as_warn (_(".ent or .aent not in text section"));
19855
19856 if (!aent && cur_proc_ptr)
19857 as_warn (_("missing .end"));
19858
19859 if (!aent)
19860 {
19861 /* This function needs its own .frame and .cprestore directives. */
19862 mips_frame_reg_valid = 0;
19863 mips_cprestore_valid = 0;
19864
19865 cur_proc_ptr = &cur_proc;
19866 memset (cur_proc_ptr, '\0', sizeof (procS));
19867
19868 cur_proc_ptr->func_sym = symbolP;
19869
19870 ++numprocs;
19871
19872 if (debug_type == DEBUG_STABS)
19873 stabs_generate_asm_func (S_GET_NAME (symbolP),
19874 S_GET_NAME (symbolP));
19875 }
19876
19877 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19878
19879 demand_empty_rest_of_line ();
19880 }
19881
19882 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19883 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19884 s_mips_frame is used so that we can set the PDR information correctly.
19885 We can't use the ecoff routines because they make reference to the ecoff
19886 symbol table (in the mdebug section). */
19887
19888 static void
19889 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19890 {
19891 if (ECOFF_DEBUGGING)
19892 s_ignore (ignore);
19893 else
19894 {
19895 long val;
19896
19897 if (cur_proc_ptr == (procS *) NULL)
19898 {
19899 as_warn (_(".frame outside of .ent"));
19900 demand_empty_rest_of_line ();
19901 return;
19902 }
19903
19904 cur_proc_ptr->frame_reg = tc_get_register (1);
19905
19906 SKIP_WHITESPACE ();
19907 if (*input_line_pointer++ != ','
19908 || get_absolute_expression_and_terminator (&val) != ',')
19909 {
19910 as_warn (_("bad .frame directive"));
19911 --input_line_pointer;
19912 demand_empty_rest_of_line ();
19913 return;
19914 }
19915
19916 cur_proc_ptr->frame_offset = val;
19917 cur_proc_ptr->pc_reg = tc_get_register (0);
19918
19919 demand_empty_rest_of_line ();
19920 }
19921 }
19922
19923 /* The .fmask and .mask directives. If the mdebug section is present
19924 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19925 embedded targets, s_mips_mask is used so that we can set the PDR
19926 information correctly. We can't use the ecoff routines because they
19927 make reference to the ecoff symbol table (in the mdebug section). */
19928
19929 static void
19930 s_mips_mask (int reg_type)
19931 {
19932 if (ECOFF_DEBUGGING)
19933 s_ignore (reg_type);
19934 else
19935 {
19936 long mask, off;
19937
19938 if (cur_proc_ptr == (procS *) NULL)
19939 {
19940 as_warn (_(".mask/.fmask outside of .ent"));
19941 demand_empty_rest_of_line ();
19942 return;
19943 }
19944
19945 if (get_absolute_expression_and_terminator (&mask) != ',')
19946 {
19947 as_warn (_("bad .mask/.fmask directive"));
19948 --input_line_pointer;
19949 demand_empty_rest_of_line ();
19950 return;
19951 }
19952
19953 off = get_absolute_expression ();
19954
19955 if (reg_type == 'F')
19956 {
19957 cur_proc_ptr->fpreg_mask = mask;
19958 cur_proc_ptr->fpreg_offset = off;
19959 }
19960 else
19961 {
19962 cur_proc_ptr->reg_mask = mask;
19963 cur_proc_ptr->reg_offset = off;
19964 }
19965
19966 demand_empty_rest_of_line ();
19967 }
19968 }
19969
19970 /* A table describing all the processors gas knows about. Names are
19971 matched in the order listed.
19972
19973 To ease comparison, please keep this table in the same order as
19974 gcc's mips_cpu_info_table[]. */
19975 static const struct mips_cpu_info mips_cpu_info_table[] =
19976 {
19977 /* Entries for generic ISAs. */
19978 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19979 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19980 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19981 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19982 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19983 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19984 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19985 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19986 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
19987 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
19988 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19989 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
19990 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19991 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
19992 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
19993
19994 /* MIPS I */
19995 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19996 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19997 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
19998
19999 /* MIPS II */
20000 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
20001
20002 /* MIPS III */
20003 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
20004 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
20005 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
20006 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
20007 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
20008 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
20009 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
20010 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
20011 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
20012 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
20013 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
20014 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
20015 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
20016 /* ST Microelectronics Loongson 2E and 2F cores. */
20017 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
20018 { "loongson2f", 0, ASE_LOONGSON_MMI, ISA_MIPS3, CPU_LOONGSON_2F },
20019
20020 /* MIPS IV */
20021 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
20022 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
20023 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
20024 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
20025 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
20026 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
20027 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
20028 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
20029 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
20030 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
20031 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
20032 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
20033 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
20034 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
20035 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
20036
20037 /* MIPS 32 */
20038 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20039 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20040 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20041 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
20042
20043 /* MIPS 32 Release 2 */
20044 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20045 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20046 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20047 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
20048 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20049 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20050 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
20051 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
20052 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
20053 ISA_MIPS32R2, CPU_MIPS32R2 },
20054 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
20055 ISA_MIPS32R2, CPU_MIPS32R2 },
20056 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20057 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20058 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20059 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20060 /* Deprecated forms of the above. */
20061 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20062 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20063 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
20064 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20065 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20066 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20067 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20068 /* Deprecated forms of the above. */
20069 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20070 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20071 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
20072 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20073 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20074 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20075 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20076 /* Deprecated forms of the above. */
20077 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20078 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20079 /* 34Kn is a 34kc without DSP. */
20080 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20081 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
20082 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20083 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20084 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20085 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20086 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20087 /* Deprecated forms of the above. */
20088 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20089 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20090 /* 1004K cores are multiprocessor versions of the 34K. */
20091 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20092 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20093 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20094 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20095 /* interaptiv is the new name for 1004kf. */
20096 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20097 { "interaptiv-mr2", 0,
20098 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
20099 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
20100 /* M5100 family. */
20101 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
20102 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
20103 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
20104 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
20105
20106 /* MIPS 64 */
20107 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
20108 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
20109 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
20110 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
20111
20112 /* Broadcom SB-1 CPU core. */
20113 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
20114 /* Broadcom SB-1A CPU core. */
20115 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
20116
20117 /* MIPS 64 Release 2. */
20118 /* Loongson CPU core. */
20119 /* -march=loongson3a is an alias of -march=gs464 for compatibility. */
20120 { "loongson3a", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
20121 ISA_MIPS64R2, CPU_GS464 },
20122 { "gs464", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
20123 ISA_MIPS64R2, CPU_GS464 },
20124 { "gs464e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20125 | ASE_LOONGSON_EXT2, ISA_MIPS64R2, CPU_GS464E },
20126 { "gs264e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20127 | ASE_LOONGSON_EXT2 | ASE_MSA | ASE_MSA64, ISA_MIPS64R2, CPU_GS264E },
20128
20129 /* Cavium Networks Octeon CPU core. */
20130 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
20131 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
20132 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
20133 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
20134
20135 /* RMI Xlr */
20136 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
20137
20138 /* Broadcom XLP.
20139 XLP is mostly like XLR, with the prominent exception that it is
20140 MIPS64R2 rather than MIPS64. */
20141 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
20142
20143 /* MIPS 64 Release 6. */
20144 { "i6400", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
20145 { "i6500", 0, ASE_VIRT | ASE_MSA | ASE_CRC | ASE_GINV,
20146 ISA_MIPS64R6, CPU_MIPS64R6},
20147 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
20148
20149 /* End marker. */
20150 { NULL, 0, 0, 0, 0 }
20151 };
20152
20153
20154 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
20155 with a final "000" replaced by "k". Ignore case.
20156
20157 Note: this function is shared between GCC and GAS. */
20158
20159 static bfd_boolean
20160 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
20161 {
20162 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
20163 given++, canonical++;
20164
20165 return ((*given == 0 && *canonical == 0)
20166 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
20167 }
20168
20169
20170 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
20171 CPU name. We've traditionally allowed a lot of variation here.
20172
20173 Note: this function is shared between GCC and GAS. */
20174
20175 static bfd_boolean
20176 mips_matching_cpu_name_p (const char *canonical, const char *given)
20177 {
20178 /* First see if the name matches exactly, or with a final "000"
20179 turned into "k". */
20180 if (mips_strict_matching_cpu_name_p (canonical, given))
20181 return TRUE;
20182
20183 /* If not, try comparing based on numerical designation alone.
20184 See if GIVEN is an unadorned number, or 'r' followed by a number. */
20185 if (TOLOWER (*given) == 'r')
20186 given++;
20187 if (!ISDIGIT (*given))
20188 return FALSE;
20189
20190 /* Skip over some well-known prefixes in the canonical name,
20191 hoping to find a number there too. */
20192 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
20193 canonical += 2;
20194 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
20195 canonical += 2;
20196 else if (TOLOWER (canonical[0]) == 'r')
20197 canonical += 1;
20198
20199 return mips_strict_matching_cpu_name_p (canonical, given);
20200 }
20201
20202
20203 /* Parse an option that takes the name of a processor as its argument.
20204 OPTION is the name of the option and CPU_STRING is the argument.
20205 Return the corresponding processor enumeration if the CPU_STRING is
20206 recognized, otherwise report an error and return null.
20207
20208 A similar function exists in GCC. */
20209
20210 static const struct mips_cpu_info *
20211 mips_parse_cpu (const char *option, const char *cpu_string)
20212 {
20213 const struct mips_cpu_info *p;
20214
20215 /* 'from-abi' selects the most compatible architecture for the given
20216 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
20217 EABIs, we have to decide whether we're using the 32-bit or 64-bit
20218 version. Look first at the -mgp options, if given, otherwise base
20219 the choice on MIPS_DEFAULT_64BIT.
20220
20221 Treat NO_ABI like the EABIs. One reason to do this is that the
20222 plain 'mips' and 'mips64' configs have 'from-abi' as their default
20223 architecture. This code picks MIPS I for 'mips' and MIPS III for
20224 'mips64', just as we did in the days before 'from-abi'. */
20225 if (strcasecmp (cpu_string, "from-abi") == 0)
20226 {
20227 if (ABI_NEEDS_32BIT_REGS (mips_abi))
20228 return mips_cpu_info_from_isa (ISA_MIPS1);
20229
20230 if (ABI_NEEDS_64BIT_REGS (mips_abi))
20231 return mips_cpu_info_from_isa (ISA_MIPS3);
20232
20233 if (file_mips_opts.gp >= 0)
20234 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
20235 ? ISA_MIPS1 : ISA_MIPS3);
20236
20237 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
20238 ? ISA_MIPS3
20239 : ISA_MIPS1);
20240 }
20241
20242 /* 'default' has traditionally been a no-op. Probably not very useful. */
20243 if (strcasecmp (cpu_string, "default") == 0)
20244 return 0;
20245
20246 for (p = mips_cpu_info_table; p->name != 0; p++)
20247 if (mips_matching_cpu_name_p (p->name, cpu_string))
20248 return p;
20249
20250 as_bad (_("bad value (%s) for %s"), cpu_string, option);
20251 return 0;
20252 }
20253
20254 /* Return the canonical processor information for ISA (a member of the
20255 ISA_MIPS* enumeration). */
20256
20257 static const struct mips_cpu_info *
20258 mips_cpu_info_from_isa (int isa)
20259 {
20260 int i;
20261
20262 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20263 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
20264 && isa == mips_cpu_info_table[i].isa)
20265 return (&mips_cpu_info_table[i]);
20266
20267 return NULL;
20268 }
20269
20270 static const struct mips_cpu_info *
20271 mips_cpu_info_from_arch (int arch)
20272 {
20273 int i;
20274
20275 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20276 if (arch == mips_cpu_info_table[i].cpu)
20277 return (&mips_cpu_info_table[i]);
20278
20279 return NULL;
20280 }
20281 \f
20282 static void
20283 show (FILE *stream, const char *string, int *col_p, int *first_p)
20284 {
20285 if (*first_p)
20286 {
20287 fprintf (stream, "%24s", "");
20288 *col_p = 24;
20289 }
20290 else
20291 {
20292 fprintf (stream, ", ");
20293 *col_p += 2;
20294 }
20295
20296 if (*col_p + strlen (string) > 72)
20297 {
20298 fprintf (stream, "\n%24s", "");
20299 *col_p = 24;
20300 }
20301
20302 fprintf (stream, "%s", string);
20303 *col_p += strlen (string);
20304
20305 *first_p = 0;
20306 }
20307
20308 void
20309 md_show_usage (FILE *stream)
20310 {
20311 int column, first;
20312 size_t i;
20313
20314 fprintf (stream, _("\
20315 MIPS options:\n\
20316 -EB generate big endian output\n\
20317 -EL generate little endian output\n\
20318 -g, -g2 do not remove unneeded NOPs or swap branches\n\
20319 -G NUM allow referencing objects up to NUM bytes\n\
20320 implicitly with the gp register [default 8]\n"));
20321 fprintf (stream, _("\
20322 -mips1 generate MIPS ISA I instructions\n\
20323 -mips2 generate MIPS ISA II instructions\n\
20324 -mips3 generate MIPS ISA III instructions\n\
20325 -mips4 generate MIPS ISA IV instructions\n\
20326 -mips5 generate MIPS ISA V instructions\n\
20327 -mips32 generate MIPS32 ISA instructions\n\
20328 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
20329 -mips32r3 generate MIPS32 release 3 ISA instructions\n\
20330 -mips32r5 generate MIPS32 release 5 ISA instructions\n\
20331 -mips32r6 generate MIPS32 release 6 ISA instructions\n\
20332 -mips64 generate MIPS64 ISA instructions\n\
20333 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
20334 -mips64r3 generate MIPS64 release 3 ISA instructions\n\
20335 -mips64r5 generate MIPS64 release 5 ISA instructions\n\
20336 -mips64r6 generate MIPS64 release 6 ISA instructions\n\
20337 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
20338
20339 first = 1;
20340
20341 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20342 show (stream, mips_cpu_info_table[i].name, &column, &first);
20343 show (stream, "from-abi", &column, &first);
20344 fputc ('\n', stream);
20345
20346 fprintf (stream, _("\
20347 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
20348 -no-mCPU don't generate code specific to CPU.\n\
20349 For -mCPU and -no-mCPU, CPU must be one of:\n"));
20350
20351 first = 1;
20352
20353 show (stream, "3900", &column, &first);
20354 show (stream, "4010", &column, &first);
20355 show (stream, "4100", &column, &first);
20356 show (stream, "4650", &column, &first);
20357 fputc ('\n', stream);
20358
20359 fprintf (stream, _("\
20360 -mips16 generate mips16 instructions\n\
20361 -no-mips16 do not generate mips16 instructions\n"));
20362 fprintf (stream, _("\
20363 -mmips16e2 generate MIPS16e2 instructions\n\
20364 -mno-mips16e2 do not generate MIPS16e2 instructions\n"));
20365 fprintf (stream, _("\
20366 -mmicromips generate microMIPS instructions\n\
20367 -mno-micromips do not generate microMIPS instructions\n"));
20368 fprintf (stream, _("\
20369 -msmartmips generate smartmips instructions\n\
20370 -mno-smartmips do not generate smartmips instructions\n"));
20371 fprintf (stream, _("\
20372 -mdsp generate DSP instructions\n\
20373 -mno-dsp do not generate DSP instructions\n"));
20374 fprintf (stream, _("\
20375 -mdspr2 generate DSP R2 instructions\n\
20376 -mno-dspr2 do not generate DSP R2 instructions\n"));
20377 fprintf (stream, _("\
20378 -mdspr3 generate DSP R3 instructions\n\
20379 -mno-dspr3 do not generate DSP R3 instructions\n"));
20380 fprintf (stream, _("\
20381 -mmt generate MT instructions\n\
20382 -mno-mt do not generate MT instructions\n"));
20383 fprintf (stream, _("\
20384 -mmcu generate MCU instructions\n\
20385 -mno-mcu do not generate MCU instructions\n"));
20386 fprintf (stream, _("\
20387 -mmsa generate MSA instructions\n\
20388 -mno-msa do not generate MSA instructions\n"));
20389 fprintf (stream, _("\
20390 -mxpa generate eXtended Physical Address (XPA) instructions\n\
20391 -mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
20392 fprintf (stream, _("\
20393 -mvirt generate Virtualization instructions\n\
20394 -mno-virt do not generate Virtualization instructions\n"));
20395 fprintf (stream, _("\
20396 -mcrc generate CRC instructions\n\
20397 -mno-crc do not generate CRC instructions\n"));
20398 fprintf (stream, _("\
20399 -mginv generate Global INValidate (GINV) instructions\n\
20400 -mno-ginv do not generate Global INValidate instructions\n"));
20401 fprintf (stream, _("\
20402 -mloongson-mmi generate Loongson MultiMedia extensions Instructions (MMI) instructions\n\
20403 -mno-loongson-mmi do not generate Loongson MultiMedia extensions Instructions\n"));
20404 fprintf (stream, _("\
20405 -mloongson-cam generate Loongson Content Address Memory (CAM) instructions\n\
20406 -mno-loongson-cam do not generate Loongson Content Address Memory Instructions\n"));
20407 fprintf (stream, _("\
20408 -mloongson-ext generate Loongson EXTensions (EXT) instructions\n\
20409 -mno-loongson-ext do not generate Loongson EXTensions Instructions\n"));
20410 fprintf (stream, _("\
20411 -mloongson-ext2 generate Loongson EXTensions R2 (EXT2) instructions\n\
20412 -mno-loongson-ext2 do not generate Loongson EXTensions R2 Instructions\n"));
20413 fprintf (stream, _("\
20414 -minsn32 only generate 32-bit microMIPS instructions\n\
20415 -mno-insn32 generate all microMIPS instructions\n"));
20416 #if DEFAULT_MIPS_FIX_LOONGSON3_LLSC
20417 fprintf (stream, _("\
20418 -mfix-loongson3-llsc work around Loongson3 LL/SC errata, default\n\
20419 -mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n"));
20420 #else
20421 fprintf (stream, _("\
20422 -mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20423 -mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata, default\n"));
20424 #endif
20425 fprintf (stream, _("\
20426 -mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
20427 -mfix-loongson2f-nop work around Loongson2F NOP errata\n\
20428 -mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20429 -mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n\
20430 -mfix-vr4120 work around certain VR4120 errata\n\
20431 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
20432 -mfix-24k insert a nop after ERET and DERET instructions\n\
20433 -mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
20434 -mfix-r5900 work around R5900 short loop errata\n\
20435 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20436 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
20437 -msym32 assume all symbols have 32-bit values\n\
20438 -O0 do not remove unneeded NOPs, do not swap branches\n\
20439 -O, -O1 remove unneeded NOPs, do not swap branches\n\
20440 -O2 remove unneeded NOPs and swap branches\n\
20441 --trap, --no-break trap exception on div by 0 and mult overflow\n\
20442 --break, --no-trap break exception on div by 0 and mult overflow\n"));
20443 fprintf (stream, _("\
20444 -mhard-float allow floating-point instructions\n\
20445 -msoft-float do not allow floating-point instructions\n\
20446 -msingle-float only allow 32-bit floating-point operations\n\
20447 -mdouble-float allow 32-bit and 64-bit floating-point operations\n\
20448 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
20449 --[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
20450 -mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20451 -mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
20452 -mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20453
20454 first = 1;
20455
20456 show (stream, "legacy", &column, &first);
20457 show (stream, "2008", &column, &first);
20458
20459 fputc ('\n', stream);
20460
20461 fprintf (stream, _("\
20462 -KPIC, -call_shared generate SVR4 position independent code\n\
20463 -call_nonpic generate non-PIC code that can operate with DSOs\n\
20464 -mvxworks-pic generate VxWorks position independent code\n\
20465 -non_shared do not generate code that can operate with DSOs\n\
20466 -xgot assume a 32 bit GOT\n\
20467 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
20468 -mshared, -mno-shared disable/enable .cpload optimization for\n\
20469 position dependent (non shared) code\n\
20470 -mabi=ABI create ABI conformant object file for:\n"));
20471
20472 first = 1;
20473
20474 show (stream, "32", &column, &first);
20475 show (stream, "o64", &column, &first);
20476 show (stream, "n32", &column, &first);
20477 show (stream, "64", &column, &first);
20478 show (stream, "eabi", &column, &first);
20479
20480 fputc ('\n', stream);
20481
20482 fprintf (stream, _("\
20483 -32 create o32 ABI object file%s\n"),
20484 MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20485 fprintf (stream, _("\
20486 -n32 create n32 ABI object file%s\n"),
20487 MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20488 fprintf (stream, _("\
20489 -64 create 64 ABI object file%s\n"),
20490 MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
20491 }
20492
20493 #ifdef TE_IRIX
20494 enum dwarf2_format
20495 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
20496 {
20497 if (HAVE_64BIT_SYMBOLS)
20498 return dwarf2_format_64bit_irix;
20499 else
20500 return dwarf2_format_32bit;
20501 }
20502 #endif
20503
20504 int
20505 mips_dwarf2_addr_size (void)
20506 {
20507 if (HAVE_64BIT_OBJECTS)
20508 return 8;
20509 else
20510 return 4;
20511 }
20512
20513 /* Standard calling conventions leave the CFA at SP on entry. */
20514 void
20515 mips_cfi_frame_initial_instructions (void)
20516 {
20517 cfi_add_CFA_def_cfa_register (SP);
20518 }
20519
20520 int
20521 tc_mips_regname_to_dw2regnum (char *regname)
20522 {
20523 unsigned int regnum = -1;
20524 unsigned int reg;
20525
20526 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20527 regnum = reg;
20528
20529 return regnum;
20530 }
20531
20532 /* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20533 Given a symbolic attribute NAME, return the proper integer value.
20534 Returns -1 if the attribute is not known. */
20535
20536 int
20537 mips_convert_symbolic_attribute (const char *name)
20538 {
20539 static const struct
20540 {
20541 const char * name;
20542 const int tag;
20543 }
20544 attribute_table[] =
20545 {
20546 #define T(tag) {#tag, tag}
20547 T (Tag_GNU_MIPS_ABI_FP),
20548 T (Tag_GNU_MIPS_ABI_MSA),
20549 #undef T
20550 };
20551 unsigned int i;
20552
20553 if (name == NULL)
20554 return -1;
20555
20556 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20557 if (streq (name, attribute_table[i].name))
20558 return attribute_table[i].tag;
20559
20560 return -1;
20561 }
20562
20563 void
20564 md_mips_end (void)
20565 {
20566 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20567
20568 mips_emit_delays ();
20569 if (cur_proc_ptr)
20570 as_warn (_("missing .end at end of assembly"));
20571
20572 /* Just in case no code was emitted, do the consistency check. */
20573 file_mips_check_options ();
20574
20575 /* Set a floating-point ABI if the user did not. */
20576 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20577 {
20578 /* Perform consistency checks on the floating-point ABI. */
20579 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20580 Tag_GNU_MIPS_ABI_FP);
20581 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20582 check_fpabi (fpabi);
20583 }
20584 else
20585 {
20586 /* Soft-float gets precedence over single-float, the two options should
20587 not be used together so this should not matter. */
20588 if (file_mips_opts.soft_float == 1)
20589 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20590 /* Single-float gets precedence over all double_float cases. */
20591 else if (file_mips_opts.single_float == 1)
20592 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20593 else
20594 {
20595 switch (file_mips_opts.fp)
20596 {
20597 case 32:
20598 if (file_mips_opts.gp == 32)
20599 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20600 break;
20601 case 0:
20602 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20603 break;
20604 case 64:
20605 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20606 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20607 else if (file_mips_opts.gp == 32)
20608 fpabi = Val_GNU_MIPS_ABI_FP_64;
20609 else
20610 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20611 break;
20612 }
20613 }
20614
20615 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20616 Tag_GNU_MIPS_ABI_FP, fpabi);
20617 }
20618 }
20619
20620 /* Returns the relocation type required for a particular CFI encoding. */
20621
20622 bfd_reloc_code_real_type
20623 mips_cfi_reloc_for_encoding (int encoding)
20624 {
20625 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20626 return BFD_RELOC_32_PCREL;
20627 else return BFD_RELOC_NONE;
20628 }
This page took 0.522683 seconds and 4 git commands to generate.