MIPS16/GAS: Improve [32768,65535] out-of-range operand error diagnostics
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright (C) 1993-2017 Free Software Foundation, Inc.
3 Contributed by the OSF and Ralph Campbell.
4 Written by Keith Knowles and Ralph Campbell, working independently.
5 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
6 Support.
7
8 This file is part of GAS.
9
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
14
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 02110-1301, USA. */
24
25 #include "as.h"
26 #include "config.h"
27 #include "subsegs.h"
28 #include "safe-ctype.h"
29
30 #include "opcode/mips.h"
31 #include "itbl-ops.h"
32 #include "dwarf2dbg.h"
33 #include "dw2gencfi.h"
34
35 /* Check assumptions made in this file. */
36 typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
37 typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
38
39 #ifdef DEBUG
40 #define DBG(x) printf x
41 #else
42 #define DBG(x)
43 #endif
44
45 #define streq(a, b) (strcmp (a, b) == 0)
46
47 #define SKIP_SPACE_TABS(S) \
48 do { while (*(S) == ' ' || *(S) == '\t') ++(S); } while (0)
49
50 /* Clean up namespace so we can include obj-elf.h too. */
51 static int mips_output_flavor (void);
52 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
53 #undef OBJ_PROCESS_STAB
54 #undef OUTPUT_FLAVOR
55 #undef S_GET_ALIGN
56 #undef S_GET_SIZE
57 #undef S_SET_ALIGN
58 #undef S_SET_SIZE
59 #undef obj_frob_file
60 #undef obj_frob_file_after_relocs
61 #undef obj_frob_symbol
62 #undef obj_pop_insert
63 #undef obj_sec_sym_ok_for_reloc
64 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
65
66 #include "obj-elf.h"
67 /* Fix any of them that we actually care about. */
68 #undef OUTPUT_FLAVOR
69 #define OUTPUT_FLAVOR mips_output_flavor()
70
71 #include "elf/mips.h"
72
73 #ifndef ECOFF_DEBUGGING
74 #define NO_ECOFF_DEBUGGING
75 #define ECOFF_DEBUGGING 0
76 #endif
77
78 int mips_flag_mdebug = -1;
79
80 /* Control generation of .pdr sections. Off by default on IRIX: the native
81 linker doesn't know about and discards them, but relocations against them
82 remain, leading to rld crashes. */
83 #ifdef TE_IRIX
84 int mips_flag_pdr = FALSE;
85 #else
86 int mips_flag_pdr = TRUE;
87 #endif
88
89 #include "ecoff.h"
90
91 static char *mips_regmask_frag;
92 static char *mips_flags_frag;
93
94 #define ZERO 0
95 #define ATREG 1
96 #define S0 16
97 #define S7 23
98 #define TREG 24
99 #define PIC_CALL_REG 25
100 #define KT0 26
101 #define KT1 27
102 #define GP 28
103 #define SP 29
104 #define FP 30
105 #define RA 31
106
107 #define ILLEGAL_REG (32)
108
109 #define AT mips_opts.at
110
111 extern int target_big_endian;
112
113 /* The name of the readonly data section. */
114 #define RDATA_SECTION_NAME ".rodata"
115
116 /* Ways in which an instruction can be "appended" to the output. */
117 enum append_method {
118 /* Just add it normally. */
119 APPEND_ADD,
120
121 /* Add it normally and then add a nop. */
122 APPEND_ADD_WITH_NOP,
123
124 /* Turn an instruction with a delay slot into a "compact" version. */
125 APPEND_ADD_COMPACT,
126
127 /* Insert the instruction before the last one. */
128 APPEND_SWAP
129 };
130
131 /* Information about an instruction, including its format, operands
132 and fixups. */
133 struct mips_cl_insn
134 {
135 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
136 const struct mips_opcode *insn_mo;
137
138 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
139 a copy of INSN_MO->match with the operands filled in. If we have
140 decided to use an extended MIPS16 instruction, this includes the
141 extension. */
142 unsigned long insn_opcode;
143
144 /* The frag that contains the instruction. */
145 struct frag *frag;
146
147 /* The offset into FRAG of the first instruction byte. */
148 long where;
149
150 /* The relocs associated with the instruction, if any. */
151 fixS *fixp[3];
152
153 /* True if this entry cannot be moved from its current position. */
154 unsigned int fixed_p : 1;
155
156 /* True if this instruction occurred in a .set noreorder block. */
157 unsigned int noreorder_p : 1;
158
159 /* True for mips16 instructions that jump to an absolute address. */
160 unsigned int mips16_absolute_jump_p : 1;
161
162 /* True if this instruction is complete. */
163 unsigned int complete_p : 1;
164
165 /* True if this instruction is cleared from history by unconditional
166 branch. */
167 unsigned int cleared_p : 1;
168 };
169
170 /* The ABI to use. */
171 enum mips_abi_level
172 {
173 NO_ABI = 0,
174 O32_ABI,
175 O64_ABI,
176 N32_ABI,
177 N64_ABI,
178 EABI_ABI
179 };
180
181 /* MIPS ABI we are using for this output file. */
182 static enum mips_abi_level mips_abi = NO_ABI;
183
184 /* Whether or not we have code that can call pic code. */
185 int mips_abicalls = FALSE;
186
187 /* Whether or not we have code which can be put into a shared
188 library. */
189 static bfd_boolean mips_in_shared = TRUE;
190
191 /* This is the set of options which may be modified by the .set
192 pseudo-op. We use a struct so that .set push and .set pop are more
193 reliable. */
194
195 struct mips_set_options
196 {
197 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
198 if it has not been initialized. Changed by `.set mipsN', and the
199 -mipsN command line option, and the default CPU. */
200 int isa;
201 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
202 <asename>', by command line options, and based on the default
203 architecture. */
204 int ase;
205 /* Whether we are assembling for the mips16 processor. 0 if we are
206 not, 1 if we are, and -1 if the value has not been initialized.
207 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
208 -nomips16 command line options, and the default CPU. */
209 int mips16;
210 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
211 1 if we are, and -1 if the value has not been initialized. Changed
212 by `.set micromips' and `.set nomicromips', and the -mmicromips
213 and -mno-micromips command line options, and the default CPU. */
214 int micromips;
215 /* Non-zero if we should not reorder instructions. Changed by `.set
216 reorder' and `.set noreorder'. */
217 int noreorder;
218 /* Non-zero if we should not permit the register designated "assembler
219 temporary" to be used in instructions. The value is the register
220 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
221 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
222 unsigned int at;
223 /* Non-zero if we should warn when a macro instruction expands into
224 more than one machine instruction. Changed by `.set nomacro' and
225 `.set macro'. */
226 int warn_about_macros;
227 /* Non-zero if we should not move instructions. Changed by `.set
228 move', `.set volatile', `.set nomove', and `.set novolatile'. */
229 int nomove;
230 /* Non-zero if we should not optimize branches by moving the target
231 of the branch into the delay slot. Actually, we don't perform
232 this optimization anyhow. Changed by `.set bopt' and `.set
233 nobopt'. */
234 int nobopt;
235 /* Non-zero if we should not autoextend mips16 instructions.
236 Changed by `.set autoextend' and `.set noautoextend'. */
237 int noautoextend;
238 /* True if we should only emit 32-bit microMIPS instructions.
239 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
240 and -mno-insn32 command line options. */
241 bfd_boolean insn32;
242 /* Restrict general purpose registers and floating point registers
243 to 32 bit. This is initially determined when -mgp32 or -mfp32
244 is passed but can changed if the assembler code uses .set mipsN. */
245 int gp;
246 int fp;
247 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
248 command line option, and the default CPU. */
249 int arch;
250 /* True if ".set sym32" is in effect. */
251 bfd_boolean sym32;
252 /* True if floating-point operations are not allowed. Changed by .set
253 softfloat or .set hardfloat, by command line options -msoft-float or
254 -mhard-float. The default is false. */
255 bfd_boolean soft_float;
256
257 /* True if only single-precision floating-point operations are allowed.
258 Changed by .set singlefloat or .set doublefloat, command-line options
259 -msingle-float or -mdouble-float. The default is false. */
260 bfd_boolean single_float;
261
262 /* 1 if single-precision operations on odd-numbered registers are
263 allowed. */
264 int oddspreg;
265 };
266
267 /* Specifies whether module level options have been checked yet. */
268 static bfd_boolean file_mips_opts_checked = FALSE;
269
270 /* Do we support nan2008? 0 if we don't, 1 if we do, and -1 if the
271 value has not been initialized. Changed by `.nan legacy' and
272 `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
273 options, and the default CPU. */
274 static int mips_nan2008 = -1;
275
276 /* This is the struct we use to hold the module level set of options.
277 Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
278 fp fields to -1 to indicate that they have not been initialized. */
279
280 static struct mips_set_options file_mips_opts =
281 {
282 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
283 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
284 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
285 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
286 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
287 };
288
289 /* This is similar to file_mips_opts, but for the current set of options. */
290
291 static struct mips_set_options mips_opts =
292 {
293 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
294 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
295 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
296 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
297 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
298 };
299
300 /* Which bits of file_ase were explicitly set or cleared by ASE options. */
301 static unsigned int file_ase_explicit;
302
303 /* These variables are filled in with the masks of registers used.
304 The object format code reads them and puts them in the appropriate
305 place. */
306 unsigned long mips_gprmask;
307 unsigned long mips_cprmask[4];
308
309 /* True if any MIPS16 code was produced. */
310 static int file_ase_mips16;
311
312 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
313 || mips_opts.isa == ISA_MIPS32R2 \
314 || mips_opts.isa == ISA_MIPS32R3 \
315 || mips_opts.isa == ISA_MIPS32R5 \
316 || mips_opts.isa == ISA_MIPS64 \
317 || mips_opts.isa == ISA_MIPS64R2 \
318 || mips_opts.isa == ISA_MIPS64R3 \
319 || mips_opts.isa == ISA_MIPS64R5)
320
321 /* True if any microMIPS code was produced. */
322 static int file_ase_micromips;
323
324 /* True if we want to create R_MIPS_JALR for jalr $25. */
325 #ifdef TE_IRIX
326 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
327 #else
328 /* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
329 because there's no place for any addend, the only acceptable
330 expression is a bare symbol. */
331 #define MIPS_JALR_HINT_P(EXPR) \
332 (!HAVE_IN_PLACE_ADDENDS \
333 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
334 #endif
335
336 /* The argument of the -march= flag. The architecture we are assembling. */
337 static const char *mips_arch_string;
338
339 /* The argument of the -mtune= flag. The architecture for which we
340 are optimizing. */
341 static int mips_tune = CPU_UNKNOWN;
342 static const char *mips_tune_string;
343
344 /* True when generating 32-bit code for a 64-bit processor. */
345 static int mips_32bitmode = 0;
346
347 /* True if the given ABI requires 32-bit registers. */
348 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
349
350 /* Likewise 64-bit registers. */
351 #define ABI_NEEDS_64BIT_REGS(ABI) \
352 ((ABI) == N32_ABI \
353 || (ABI) == N64_ABI \
354 || (ABI) == O64_ABI)
355
356 #define ISA_IS_R6(ISA) \
357 ((ISA) == ISA_MIPS32R6 \
358 || (ISA) == ISA_MIPS64R6)
359
360 /* Return true if ISA supports 64 bit wide gp registers. */
361 #define ISA_HAS_64BIT_REGS(ISA) \
362 ((ISA) == ISA_MIPS3 \
363 || (ISA) == ISA_MIPS4 \
364 || (ISA) == ISA_MIPS5 \
365 || (ISA) == ISA_MIPS64 \
366 || (ISA) == ISA_MIPS64R2 \
367 || (ISA) == ISA_MIPS64R3 \
368 || (ISA) == ISA_MIPS64R5 \
369 || (ISA) == ISA_MIPS64R6)
370
371 /* Return true if ISA supports 64 bit wide float registers. */
372 #define ISA_HAS_64BIT_FPRS(ISA) \
373 ((ISA) == ISA_MIPS3 \
374 || (ISA) == ISA_MIPS4 \
375 || (ISA) == ISA_MIPS5 \
376 || (ISA) == ISA_MIPS32R2 \
377 || (ISA) == ISA_MIPS32R3 \
378 || (ISA) == ISA_MIPS32R5 \
379 || (ISA) == ISA_MIPS32R6 \
380 || (ISA) == ISA_MIPS64 \
381 || (ISA) == ISA_MIPS64R2 \
382 || (ISA) == ISA_MIPS64R3 \
383 || (ISA) == ISA_MIPS64R5 \
384 || (ISA) == ISA_MIPS64R6)
385
386 /* Return true if ISA supports 64-bit right rotate (dror et al.)
387 instructions. */
388 #define ISA_HAS_DROR(ISA) \
389 ((ISA) == ISA_MIPS64R2 \
390 || (ISA) == ISA_MIPS64R3 \
391 || (ISA) == ISA_MIPS64R5 \
392 || (ISA) == ISA_MIPS64R6 \
393 || (mips_opts.micromips \
394 && ISA_HAS_64BIT_REGS (ISA)) \
395 )
396
397 /* Return true if ISA supports 32-bit right rotate (ror et al.)
398 instructions. */
399 #define ISA_HAS_ROR(ISA) \
400 ((ISA) == ISA_MIPS32R2 \
401 || (ISA) == ISA_MIPS32R3 \
402 || (ISA) == ISA_MIPS32R5 \
403 || (ISA) == ISA_MIPS32R6 \
404 || (ISA) == ISA_MIPS64R2 \
405 || (ISA) == ISA_MIPS64R3 \
406 || (ISA) == ISA_MIPS64R5 \
407 || (ISA) == ISA_MIPS64R6 \
408 || (mips_opts.ase & ASE_SMARTMIPS) \
409 || mips_opts.micromips \
410 )
411
412 /* Return true if ISA supports single-precision floats in odd registers. */
413 #define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
414 (((ISA) == ISA_MIPS32 \
415 || (ISA) == ISA_MIPS32R2 \
416 || (ISA) == ISA_MIPS32R3 \
417 || (ISA) == ISA_MIPS32R5 \
418 || (ISA) == ISA_MIPS32R6 \
419 || (ISA) == ISA_MIPS64 \
420 || (ISA) == ISA_MIPS64R2 \
421 || (ISA) == ISA_MIPS64R3 \
422 || (ISA) == ISA_MIPS64R5 \
423 || (ISA) == ISA_MIPS64R6 \
424 || (CPU) == CPU_R5900) \
425 && (CPU) != CPU_LOONGSON_3A)
426
427 /* Return true if ISA supports move to/from high part of a 64-bit
428 floating-point register. */
429 #define ISA_HAS_MXHC1(ISA) \
430 ((ISA) == ISA_MIPS32R2 \
431 || (ISA) == ISA_MIPS32R3 \
432 || (ISA) == ISA_MIPS32R5 \
433 || (ISA) == ISA_MIPS32R6 \
434 || (ISA) == ISA_MIPS64R2 \
435 || (ISA) == ISA_MIPS64R3 \
436 || (ISA) == ISA_MIPS64R5 \
437 || (ISA) == ISA_MIPS64R6)
438
439 /* Return true if ISA supports legacy NAN. */
440 #define ISA_HAS_LEGACY_NAN(ISA) \
441 ((ISA) == ISA_MIPS1 \
442 || (ISA) == ISA_MIPS2 \
443 || (ISA) == ISA_MIPS3 \
444 || (ISA) == ISA_MIPS4 \
445 || (ISA) == ISA_MIPS5 \
446 || (ISA) == ISA_MIPS32 \
447 || (ISA) == ISA_MIPS32R2 \
448 || (ISA) == ISA_MIPS32R3 \
449 || (ISA) == ISA_MIPS32R5 \
450 || (ISA) == ISA_MIPS64 \
451 || (ISA) == ISA_MIPS64R2 \
452 || (ISA) == ISA_MIPS64R3 \
453 || (ISA) == ISA_MIPS64R5)
454
455 #define GPR_SIZE \
456 (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
457 ? 32 \
458 : mips_opts.gp)
459
460 #define FPR_SIZE \
461 (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
462 ? 32 \
463 : mips_opts.fp)
464
465 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
466
467 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
468
469 /* True if relocations are stored in-place. */
470 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
471
472 /* The ABI-derived address size. */
473 #define HAVE_64BIT_ADDRESSES \
474 (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
475 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
476
477 /* The size of symbolic constants (i.e., expressions of the form
478 "SYMBOL" or "SYMBOL + OFFSET"). */
479 #define HAVE_32BIT_SYMBOLS \
480 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
481 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
482
483 /* Addresses are loaded in different ways, depending on the address size
484 in use. The n32 ABI Documentation also mandates the use of additions
485 with overflow checking, but existing implementations don't follow it. */
486 #define ADDRESS_ADD_INSN \
487 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
488
489 #define ADDRESS_ADDI_INSN \
490 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
491
492 #define ADDRESS_LOAD_INSN \
493 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
494
495 #define ADDRESS_STORE_INSN \
496 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
497
498 /* Return true if the given CPU supports the MIPS16 ASE. */
499 #define CPU_HAS_MIPS16(cpu) \
500 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
501 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
502
503 /* Return true if the given CPU supports the microMIPS ASE. */
504 #define CPU_HAS_MICROMIPS(cpu) 0
505
506 /* True if CPU has a dror instruction. */
507 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
508
509 /* True if CPU has a ror instruction. */
510 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
511
512 /* True if CPU is in the Octeon family */
513 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
514 || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
515
516 /* True if CPU has seq/sne and seqi/snei instructions. */
517 #define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
518
519 /* True, if CPU has support for ldc1 and sdc1. */
520 #define CPU_HAS_LDC1_SDC1(CPU) \
521 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
522
523 /* True if mflo and mfhi can be immediately followed by instructions
524 which write to the HI and LO registers.
525
526 According to MIPS specifications, MIPS ISAs I, II, and III need
527 (at least) two instructions between the reads of HI/LO and
528 instructions which write them, and later ISAs do not. Contradicting
529 the MIPS specifications, some MIPS IV processor user manuals (e.g.
530 the UM for the NEC Vr5000) document needing the instructions between
531 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
532 MIPS64 and later ISAs to have the interlocks, plus any specific
533 earlier-ISA CPUs for which CPU documentation declares that the
534 instructions are really interlocked. */
535 #define hilo_interlocks \
536 (mips_opts.isa == ISA_MIPS32 \
537 || mips_opts.isa == ISA_MIPS32R2 \
538 || mips_opts.isa == ISA_MIPS32R3 \
539 || mips_opts.isa == ISA_MIPS32R5 \
540 || mips_opts.isa == ISA_MIPS32R6 \
541 || mips_opts.isa == ISA_MIPS64 \
542 || mips_opts.isa == ISA_MIPS64R2 \
543 || mips_opts.isa == ISA_MIPS64R3 \
544 || mips_opts.isa == ISA_MIPS64R5 \
545 || mips_opts.isa == ISA_MIPS64R6 \
546 || mips_opts.arch == CPU_R4010 \
547 || mips_opts.arch == CPU_R5900 \
548 || mips_opts.arch == CPU_R10000 \
549 || mips_opts.arch == CPU_R12000 \
550 || mips_opts.arch == CPU_R14000 \
551 || mips_opts.arch == CPU_R16000 \
552 || mips_opts.arch == CPU_RM7000 \
553 || mips_opts.arch == CPU_VR5500 \
554 || mips_opts.micromips \
555 )
556
557 /* Whether the processor uses hardware interlocks to protect reads
558 from the GPRs after they are loaded from memory, and thus does not
559 require nops to be inserted. This applies to instructions marked
560 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
561 level I and microMIPS mode instructions are always interlocked. */
562 #define gpr_interlocks \
563 (mips_opts.isa != ISA_MIPS1 \
564 || mips_opts.arch == CPU_R3900 \
565 || mips_opts.arch == CPU_R5900 \
566 || mips_opts.micromips \
567 )
568
569 /* Whether the processor uses hardware interlocks to avoid delays
570 required by coprocessor instructions, and thus does not require
571 nops to be inserted. This applies to instructions marked
572 INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
573 instructions marked INSN_WRITE_COND_CODE and ones marked
574 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
575 levels I, II, and III and microMIPS mode instructions are always
576 interlocked. */
577 /* Itbl support may require additional care here. */
578 #define cop_interlocks \
579 ((mips_opts.isa != ISA_MIPS1 \
580 && mips_opts.isa != ISA_MIPS2 \
581 && mips_opts.isa != ISA_MIPS3) \
582 || mips_opts.arch == CPU_R4300 \
583 || mips_opts.micromips \
584 )
585
586 /* Whether the processor uses hardware interlocks to protect reads
587 from coprocessor registers after they are loaded from memory, and
588 thus does not require nops to be inserted. This applies to
589 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
590 requires at MIPS ISA level I and microMIPS mode instructions are
591 always interlocked. */
592 #define cop_mem_interlocks \
593 (mips_opts.isa != ISA_MIPS1 \
594 || mips_opts.micromips \
595 )
596
597 /* Is this a mfhi or mflo instruction? */
598 #define MF_HILO_INSN(PINFO) \
599 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
600
601 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
602 has been selected. This implies, in particular, that addresses of text
603 labels have their LSB set. */
604 #define HAVE_CODE_COMPRESSION \
605 ((mips_opts.mips16 | mips_opts.micromips) != 0)
606
607 /* The minimum and maximum signed values that can be stored in a GPR. */
608 #define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
609 #define GPR_SMIN (-GPR_SMAX - 1)
610
611 /* MIPS PIC level. */
612
613 enum mips_pic_level mips_pic;
614
615 /* 1 if we should generate 32 bit offsets from the $gp register in
616 SVR4_PIC mode. Currently has no meaning in other modes. */
617 static int mips_big_got = 0;
618
619 /* 1 if trap instructions should used for overflow rather than break
620 instructions. */
621 static int mips_trap = 0;
622
623 /* 1 if double width floating point constants should not be constructed
624 by assembling two single width halves into two single width floating
625 point registers which just happen to alias the double width destination
626 register. On some architectures this aliasing can be disabled by a bit
627 in the status register, and the setting of this bit cannot be determined
628 automatically at assemble time. */
629 static int mips_disable_float_construction;
630
631 /* Non-zero if any .set noreorder directives were used. */
632
633 static int mips_any_noreorder;
634
635 /* Non-zero if nops should be inserted when the register referenced in
636 an mfhi/mflo instruction is read in the next two instructions. */
637 static int mips_7000_hilo_fix;
638
639 /* The size of objects in the small data section. */
640 static unsigned int g_switch_value = 8;
641 /* Whether the -G option was used. */
642 static int g_switch_seen = 0;
643
644 #define N_RMASK 0xc4
645 #define N_VFP 0xd4
646
647 /* If we can determine in advance that GP optimization won't be
648 possible, we can skip the relaxation stuff that tries to produce
649 GP-relative references. This makes delay slot optimization work
650 better.
651
652 This function can only provide a guess, but it seems to work for
653 gcc output. It needs to guess right for gcc, otherwise gcc
654 will put what it thinks is a GP-relative instruction in a branch
655 delay slot.
656
657 I don't know if a fix is needed for the SVR4_PIC mode. I've only
658 fixed it for the non-PIC mode. KR 95/04/07 */
659 static int nopic_need_relax (symbolS *, int);
660
661 /* handle of the OPCODE hash table */
662 static struct hash_control *op_hash = NULL;
663
664 /* The opcode hash table we use for the mips16. */
665 static struct hash_control *mips16_op_hash = NULL;
666
667 /* The opcode hash table we use for the microMIPS ASE. */
668 static struct hash_control *micromips_op_hash = NULL;
669
670 /* This array holds the chars that always start a comment. If the
671 pre-processor is disabled, these aren't very useful */
672 const char comment_chars[] = "#";
673
674 /* This array holds the chars that only start a comment at the beginning of
675 a line. If the line seems to have the form '# 123 filename'
676 .line and .file directives will appear in the pre-processed output */
677 /* Note that input_file.c hand checks for '#' at the beginning of the
678 first line of the input file. This is because the compiler outputs
679 #NO_APP at the beginning of its output. */
680 /* Also note that C style comments are always supported. */
681 const char line_comment_chars[] = "#";
682
683 /* This array holds machine specific line separator characters. */
684 const char line_separator_chars[] = ";";
685
686 /* Chars that can be used to separate mant from exp in floating point nums */
687 const char EXP_CHARS[] = "eE";
688
689 /* Chars that mean this number is a floating point constant */
690 /* As in 0f12.456 */
691 /* or 0d1.2345e12 */
692 const char FLT_CHARS[] = "rRsSfFdDxXpP";
693
694 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
695 changed in read.c . Ideally it shouldn't have to know about it at all,
696 but nothing is ideal around here.
697 */
698
699 /* Types of printf format used for instruction-related error messages.
700 "I" means int ("%d") and "S" means string ("%s"). */
701 enum mips_insn_error_format {
702 ERR_FMT_PLAIN,
703 ERR_FMT_I,
704 ERR_FMT_SS,
705 };
706
707 /* Information about an error that was found while assembling the current
708 instruction. */
709 struct mips_insn_error {
710 /* We sometimes need to match an instruction against more than one
711 opcode table entry. Errors found during this matching are reported
712 against a particular syntactic argument rather than against the
713 instruction as a whole. We grade these messages so that errors
714 against argument N have a greater priority than an error against
715 any argument < N, since the former implies that arguments up to N
716 were acceptable and that the opcode entry was therefore a closer match.
717 If several matches report an error against the same argument,
718 we only use that error if it is the same in all cases.
719
720 min_argnum is the minimum argument number for which an error message
721 should be accepted. It is 0 if MSG is against the instruction as
722 a whole. */
723 int min_argnum;
724
725 /* The printf()-style message, including its format and arguments. */
726 enum mips_insn_error_format format;
727 const char *msg;
728 union {
729 int i;
730 const char *ss[2];
731 } u;
732 };
733
734 /* The error that should be reported for the current instruction. */
735 static struct mips_insn_error insn_error;
736
737 static int auto_align = 1;
738
739 /* When outputting SVR4 PIC code, the assembler needs to know the
740 offset in the stack frame from which to restore the $gp register.
741 This is set by the .cprestore pseudo-op, and saved in this
742 variable. */
743 static offsetT mips_cprestore_offset = -1;
744
745 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
746 more optimizations, it can use a register value instead of a memory-saved
747 offset and even an other register than $gp as global pointer. */
748 static offsetT mips_cpreturn_offset = -1;
749 static int mips_cpreturn_register = -1;
750 static int mips_gp_register = GP;
751 static int mips_gprel_offset = 0;
752
753 /* Whether mips_cprestore_offset has been set in the current function
754 (or whether it has already been warned about, if not). */
755 static int mips_cprestore_valid = 0;
756
757 /* This is the register which holds the stack frame, as set by the
758 .frame pseudo-op. This is needed to implement .cprestore. */
759 static int mips_frame_reg = SP;
760
761 /* Whether mips_frame_reg has been set in the current function
762 (or whether it has already been warned about, if not). */
763 static int mips_frame_reg_valid = 0;
764
765 /* To output NOP instructions correctly, we need to keep information
766 about the previous two instructions. */
767
768 /* Whether we are optimizing. The default value of 2 means to remove
769 unneeded NOPs and swap branch instructions when possible. A value
770 of 1 means to not swap branches. A value of 0 means to always
771 insert NOPs. */
772 static int mips_optimize = 2;
773
774 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
775 equivalent to seeing no -g option at all. */
776 static int mips_debug = 0;
777
778 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
779 #define MAX_VR4130_NOPS 4
780
781 /* The maximum number of NOPs needed to fill delay slots. */
782 #define MAX_DELAY_NOPS 2
783
784 /* The maximum number of NOPs needed for any purpose. */
785 #define MAX_NOPS 4
786
787 /* A list of previous instructions, with index 0 being the most recent.
788 We need to look back MAX_NOPS instructions when filling delay slots
789 or working around processor errata. We need to look back one
790 instruction further if we're thinking about using history[0] to
791 fill a branch delay slot. */
792 static struct mips_cl_insn history[1 + MAX_NOPS];
793
794 /* Arrays of operands for each instruction. */
795 #define MAX_OPERANDS 6
796 struct mips_operand_array {
797 const struct mips_operand *operand[MAX_OPERANDS];
798 };
799 static struct mips_operand_array *mips_operands;
800 static struct mips_operand_array *mips16_operands;
801 static struct mips_operand_array *micromips_operands;
802
803 /* Nop instructions used by emit_nop. */
804 static struct mips_cl_insn nop_insn;
805 static struct mips_cl_insn mips16_nop_insn;
806 static struct mips_cl_insn micromips_nop16_insn;
807 static struct mips_cl_insn micromips_nop32_insn;
808
809 /* The appropriate nop for the current mode. */
810 #define NOP_INSN (mips_opts.mips16 \
811 ? &mips16_nop_insn \
812 : (mips_opts.micromips \
813 ? (mips_opts.insn32 \
814 ? &micromips_nop32_insn \
815 : &micromips_nop16_insn) \
816 : &nop_insn))
817
818 /* The size of NOP_INSN in bytes. */
819 #define NOP_INSN_SIZE ((mips_opts.mips16 \
820 || (mips_opts.micromips && !mips_opts.insn32)) \
821 ? 2 : 4)
822
823 /* If this is set, it points to a frag holding nop instructions which
824 were inserted before the start of a noreorder section. If those
825 nops turn out to be unnecessary, the size of the frag can be
826 decreased. */
827 static fragS *prev_nop_frag;
828
829 /* The number of nop instructions we created in prev_nop_frag. */
830 static int prev_nop_frag_holds;
831
832 /* The number of nop instructions that we know we need in
833 prev_nop_frag. */
834 static int prev_nop_frag_required;
835
836 /* The number of instructions we've seen since prev_nop_frag. */
837 static int prev_nop_frag_since;
838
839 /* Relocations against symbols are sometimes done in two parts, with a HI
840 relocation and a LO relocation. Each relocation has only 16 bits of
841 space to store an addend. This means that in order for the linker to
842 handle carries correctly, it must be able to locate both the HI and
843 the LO relocation. This means that the relocations must appear in
844 order in the relocation table.
845
846 In order to implement this, we keep track of each unmatched HI
847 relocation. We then sort them so that they immediately precede the
848 corresponding LO relocation. */
849
850 struct mips_hi_fixup
851 {
852 /* Next HI fixup. */
853 struct mips_hi_fixup *next;
854 /* This fixup. */
855 fixS *fixp;
856 /* The section this fixup is in. */
857 segT seg;
858 };
859
860 /* The list of unmatched HI relocs. */
861
862 static struct mips_hi_fixup *mips_hi_fixup_list;
863
864 /* The frag containing the last explicit relocation operator.
865 Null if explicit relocations have not been used. */
866
867 static fragS *prev_reloc_op_frag;
868
869 /* Map mips16 register numbers to normal MIPS register numbers. */
870
871 static const unsigned int mips16_to_32_reg_map[] =
872 {
873 16, 17, 2, 3, 4, 5, 6, 7
874 };
875
876 /* Map microMIPS register numbers to normal MIPS register numbers. */
877
878 #define micromips_to_32_reg_d_map mips16_to_32_reg_map
879
880 /* The microMIPS registers with type h. */
881 static const unsigned int micromips_to_32_reg_h_map1[] =
882 {
883 5, 5, 6, 4, 4, 4, 4, 4
884 };
885 static const unsigned int micromips_to_32_reg_h_map2[] =
886 {
887 6, 7, 7, 21, 22, 5, 6, 7
888 };
889
890 /* The microMIPS registers with type m. */
891 static const unsigned int micromips_to_32_reg_m_map[] =
892 {
893 0, 17, 2, 3, 16, 18, 19, 20
894 };
895
896 #define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
897
898 /* Classifies the kind of instructions we're interested in when
899 implementing -mfix-vr4120. */
900 enum fix_vr4120_class
901 {
902 FIX_VR4120_MACC,
903 FIX_VR4120_DMACC,
904 FIX_VR4120_MULT,
905 FIX_VR4120_DMULT,
906 FIX_VR4120_DIV,
907 FIX_VR4120_MTHILO,
908 NUM_FIX_VR4120_CLASSES
909 };
910
911 /* ...likewise -mfix-loongson2f-jump. */
912 static bfd_boolean mips_fix_loongson2f_jump;
913
914 /* ...likewise -mfix-loongson2f-nop. */
915 static bfd_boolean mips_fix_loongson2f_nop;
916
917 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
918 static bfd_boolean mips_fix_loongson2f;
919
920 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
921 there must be at least one other instruction between an instruction
922 of type X and an instruction of type Y. */
923 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
924
925 /* True if -mfix-vr4120 is in force. */
926 static int mips_fix_vr4120;
927
928 /* ...likewise -mfix-vr4130. */
929 static int mips_fix_vr4130;
930
931 /* ...likewise -mfix-24k. */
932 static int mips_fix_24k;
933
934 /* ...likewise -mfix-rm7000 */
935 static int mips_fix_rm7000;
936
937 /* ...likewise -mfix-cn63xxp1 */
938 static bfd_boolean mips_fix_cn63xxp1;
939
940 /* We don't relax branches by default, since this causes us to expand
941 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
942 fail to compute the offset before expanding the macro to the most
943 efficient expansion. */
944
945 static int mips_relax_branch;
946
947 /* TRUE if checks are suppressed for invalid branches between ISA modes.
948 Needed for broken assembly produced by some GCC versions and some
949 sloppy code out there, where branches to data labels are present. */
950 static bfd_boolean mips_ignore_branch_isa;
951 \f
952 /* The expansion of many macros depends on the type of symbol that
953 they refer to. For example, when generating position-dependent code,
954 a macro that refers to a symbol may have two different expansions,
955 one which uses GP-relative addresses and one which uses absolute
956 addresses. When generating SVR4-style PIC, a macro may have
957 different expansions for local and global symbols.
958
959 We handle these situations by generating both sequences and putting
960 them in variant frags. In position-dependent code, the first sequence
961 will be the GP-relative one and the second sequence will be the
962 absolute one. In SVR4 PIC, the first sequence will be for global
963 symbols and the second will be for local symbols.
964
965 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
966 SECOND are the lengths of the two sequences in bytes. These fields
967 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
968 the subtype has the following flags:
969
970 RELAX_PIC
971 Set if generating PIC code.
972
973 RELAX_USE_SECOND
974 Set if it has been decided that we should use the second
975 sequence instead of the first.
976
977 RELAX_SECOND_LONGER
978 Set in the first variant frag if the macro's second implementation
979 is longer than its first. This refers to the macro as a whole,
980 not an individual relaxation.
981
982 RELAX_NOMACRO
983 Set in the first variant frag if the macro appeared in a .set nomacro
984 block and if one alternative requires a warning but the other does not.
985
986 RELAX_DELAY_SLOT
987 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
988 delay slot.
989
990 RELAX_DELAY_SLOT_16BIT
991 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
992 16-bit instruction.
993
994 RELAX_DELAY_SLOT_SIZE_FIRST
995 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
996 the macro is of the wrong size for the branch delay slot.
997
998 RELAX_DELAY_SLOT_SIZE_SECOND
999 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1000 the macro is of the wrong size for the branch delay slot.
1001
1002 The frag's "opcode" points to the first fixup for relaxable code.
1003
1004 Relaxable macros are generated using a sequence such as:
1005
1006 relax_start (SYMBOL);
1007 ... generate first expansion ...
1008 relax_switch ();
1009 ... generate second expansion ...
1010 relax_end ();
1011
1012 The code and fixups for the unwanted alternative are discarded
1013 by md_convert_frag. */
1014 #define RELAX_ENCODE(FIRST, SECOND, PIC) \
1015 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
1016
1017 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1018 #define RELAX_SECOND(X) ((X) & 0xff)
1019 #define RELAX_PIC(X) (((X) & 0x10000) != 0)
1020 #define RELAX_USE_SECOND 0x20000
1021 #define RELAX_SECOND_LONGER 0x40000
1022 #define RELAX_NOMACRO 0x80000
1023 #define RELAX_DELAY_SLOT 0x100000
1024 #define RELAX_DELAY_SLOT_16BIT 0x200000
1025 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1026 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
1027
1028 /* Branch without likely bit. If label is out of range, we turn:
1029
1030 beq reg1, reg2, label
1031 delay slot
1032
1033 into
1034
1035 bne reg1, reg2, 0f
1036 nop
1037 j label
1038 0: delay slot
1039
1040 with the following opcode replacements:
1041
1042 beq <-> bne
1043 blez <-> bgtz
1044 bltz <-> bgez
1045 bc1f <-> bc1t
1046
1047 bltzal <-> bgezal (with jal label instead of j label)
1048
1049 Even though keeping the delay slot instruction in the delay slot of
1050 the branch would be more efficient, it would be very tricky to do
1051 correctly, because we'd have to introduce a variable frag *after*
1052 the delay slot instruction, and expand that instead. Let's do it
1053 the easy way for now, even if the branch-not-taken case now costs
1054 one additional instruction. Out-of-range branches are not supposed
1055 to be common, anyway.
1056
1057 Branch likely. If label is out of range, we turn:
1058
1059 beql reg1, reg2, label
1060 delay slot (annulled if branch not taken)
1061
1062 into
1063
1064 beql reg1, reg2, 1f
1065 nop
1066 beql $0, $0, 2f
1067 nop
1068 1: j[al] label
1069 delay slot (executed only if branch taken)
1070 2:
1071
1072 It would be possible to generate a shorter sequence by losing the
1073 likely bit, generating something like:
1074
1075 bne reg1, reg2, 0f
1076 nop
1077 j[al] label
1078 delay slot (executed only if branch taken)
1079 0:
1080
1081 beql -> bne
1082 bnel -> beq
1083 blezl -> bgtz
1084 bgtzl -> blez
1085 bltzl -> bgez
1086 bgezl -> bltz
1087 bc1fl -> bc1t
1088 bc1tl -> bc1f
1089
1090 bltzall -> bgezal (with jal label instead of j label)
1091 bgezall -> bltzal (ditto)
1092
1093
1094 but it's not clear that it would actually improve performance. */
1095 #define RELAX_BRANCH_ENCODE(at, pic, \
1096 uncond, likely, link, toofar) \
1097 ((relax_substateT) \
1098 (0xc0000000 \
1099 | ((at) & 0x1f) \
1100 | ((pic) ? 0x20 : 0) \
1101 | ((toofar) ? 0x40 : 0) \
1102 | ((link) ? 0x80 : 0) \
1103 | ((likely) ? 0x100 : 0) \
1104 | ((uncond) ? 0x200 : 0)))
1105 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1106 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1107 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1108 #define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1109 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1110 #define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
1111 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1112
1113 /* For mips16 code, we use an entirely different form of relaxation.
1114 mips16 supports two versions of most instructions which take
1115 immediate values: a small one which takes some small value, and a
1116 larger one which takes a 16 bit value. Since branches also follow
1117 this pattern, relaxing these values is required.
1118
1119 We can assemble both mips16 and normal MIPS code in a single
1120 object. Therefore, we need to support this type of relaxation at
1121 the same time that we support the relaxation described above. We
1122 use the high bit of the subtype field to distinguish these cases.
1123
1124 The information we store for this type of relaxation is the
1125 argument code found in the opcode file for this relocation, whether
1126 the user explicitly requested a small or extended form, and whether
1127 the relocation is in a jump or jal delay slot. That tells us the
1128 size of the value, and how it should be stored. We also store
1129 whether the fragment is considered to be extended or not. We also
1130 store whether this is known to be a branch to a different section,
1131 whether we have tried to relax this frag yet, and whether we have
1132 ever extended a PC relative fragment because of a shift count. */
1133 #define RELAX_MIPS16_ENCODE(type, pic, sym32, nomacro, \
1134 small, ext, \
1135 dslot, jal_dslot) \
1136 (0x80000000 \
1137 | ((type) & 0xff) \
1138 | ((pic) ? 0x100 : 0) \
1139 | ((sym32) ? 0x200 : 0) \
1140 | ((nomacro) ? 0x400 : 0) \
1141 | ((small) ? 0x800 : 0) \
1142 | ((ext) ? 0x1000 : 0) \
1143 | ((dslot) ? 0x2000 : 0) \
1144 | ((jal_dslot) ? 0x4000 : 0))
1145
1146 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1147 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1148 #define RELAX_MIPS16_PIC(i) (((i) & 0x100) != 0)
1149 #define RELAX_MIPS16_SYM32(i) (((i) & 0x200) != 0)
1150 #define RELAX_MIPS16_NOMACRO(i) (((i) & 0x400) != 0)
1151 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x800) != 0)
1152 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x1000) != 0)
1153 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x2000) != 0)
1154 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x4000) != 0)
1155
1156 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x8000) != 0)
1157 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x8000)
1158 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x8000)
1159 #define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x10000) != 0)
1160 #define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x10000)
1161 #define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x10000)
1162 #define RELAX_MIPS16_MACRO(i) (((i) & 0x20000) != 0)
1163 #define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x20000)
1164 #define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x20000)
1165
1166 /* For microMIPS code, we use relaxation similar to one we use for
1167 MIPS16 code. Some instructions that take immediate values support
1168 two encodings: a small one which takes some small value, and a
1169 larger one which takes a 16 bit value. As some branches also follow
1170 this pattern, relaxing these values is required.
1171
1172 We can assemble both microMIPS and normal MIPS code in a single
1173 object. Therefore, we need to support this type of relaxation at
1174 the same time that we support the relaxation described above. We
1175 use one of the high bits of the subtype field to distinguish these
1176 cases.
1177
1178 The information we store for this type of relaxation is the argument
1179 code found in the opcode file for this relocation, the register
1180 selected as the assembler temporary, whether in the 32-bit
1181 instruction mode, whether the branch is unconditional, whether it is
1182 compact, whether there is no delay-slot instruction available to fill
1183 in, whether it stores the link address implicitly in $ra, whether
1184 relaxation of out-of-range 32-bit branches to a sequence of
1185 instructions is enabled, and whether the displacement of a branch is
1186 too large to fit as an immediate argument of a 16-bit and a 32-bit
1187 branch, respectively. */
1188 #define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
1189 uncond, compact, link, nods, \
1190 relax32, toofar16, toofar32) \
1191 (0x40000000 \
1192 | ((type) & 0xff) \
1193 | (((at) & 0x1f) << 8) \
1194 | ((insn32) ? 0x2000 : 0) \
1195 | ((pic) ? 0x4000 : 0) \
1196 | ((uncond) ? 0x8000 : 0) \
1197 | ((compact) ? 0x10000 : 0) \
1198 | ((link) ? 0x20000 : 0) \
1199 | ((nods) ? 0x40000 : 0) \
1200 | ((relax32) ? 0x80000 : 0) \
1201 | ((toofar16) ? 0x100000 : 0) \
1202 | ((toofar32) ? 0x200000 : 0))
1203 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1204 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1205 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1206 #define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
1207 #define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1208 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1209 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1210 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1211 #define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1212 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1213
1214 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1215 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1216 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1217 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1218 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1219 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
1220
1221 /* Sign-extend 16-bit value X. */
1222 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1223
1224 /* Is the given value a sign-extended 32-bit value? */
1225 #define IS_SEXT_32BIT_NUM(x) \
1226 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1227 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1228
1229 /* Is the given value a sign-extended 16-bit value? */
1230 #define IS_SEXT_16BIT_NUM(x) \
1231 (((x) &~ (offsetT) 0x7fff) == 0 \
1232 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1233
1234 /* Is the given value a sign-extended 12-bit value? */
1235 #define IS_SEXT_12BIT_NUM(x) \
1236 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1237
1238 /* Is the given value a sign-extended 9-bit value? */
1239 #define IS_SEXT_9BIT_NUM(x) \
1240 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1241
1242 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
1243 #define IS_ZEXT_32BIT_NUM(x) \
1244 (((x) &~ (offsetT) 0xffffffff) == 0 \
1245 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1246
1247 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1248 SHIFT places. */
1249 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1250 (((STRUCT) >> (SHIFT)) & (MASK))
1251
1252 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
1253 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1254 (!(MICROMIPS) \
1255 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1256 : EXTRACT_BITS ((INSN).insn_opcode, \
1257 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1258 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1259 EXTRACT_BITS ((INSN).insn_opcode, \
1260 MIPS16OP_MASK_##FIELD, \
1261 MIPS16OP_SH_##FIELD)
1262
1263 /* The MIPS16 EXTEND opcode, shifted left 16 places. */
1264 #define MIPS16_EXTEND (0xf000U << 16)
1265 \f
1266 /* Whether or not we are emitting a branch-likely macro. */
1267 static bfd_boolean emit_branch_likely_macro = FALSE;
1268
1269 /* Global variables used when generating relaxable macros. See the
1270 comment above RELAX_ENCODE for more details about how relaxation
1271 is used. */
1272 static struct {
1273 /* 0 if we're not emitting a relaxable macro.
1274 1 if we're emitting the first of the two relaxation alternatives.
1275 2 if we're emitting the second alternative. */
1276 int sequence;
1277
1278 /* The first relaxable fixup in the current frag. (In other words,
1279 the first fixup that refers to relaxable code.) */
1280 fixS *first_fixup;
1281
1282 /* sizes[0] says how many bytes of the first alternative are stored in
1283 the current frag. Likewise sizes[1] for the second alternative. */
1284 unsigned int sizes[2];
1285
1286 /* The symbol on which the choice of sequence depends. */
1287 symbolS *symbol;
1288 } mips_relax;
1289 \f
1290 /* Global variables used to decide whether a macro needs a warning. */
1291 static struct {
1292 /* True if the macro is in a branch delay slot. */
1293 bfd_boolean delay_slot_p;
1294
1295 /* Set to the length in bytes required if the macro is in a delay slot
1296 that requires a specific length of instruction, otherwise zero. */
1297 unsigned int delay_slot_length;
1298
1299 /* For relaxable macros, sizes[0] is the length of the first alternative
1300 in bytes and sizes[1] is the length of the second alternative.
1301 For non-relaxable macros, both elements give the length of the
1302 macro in bytes. */
1303 unsigned int sizes[2];
1304
1305 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1306 instruction of the first alternative in bytes and first_insn_sizes[1]
1307 is the length of the first instruction of the second alternative.
1308 For non-relaxable macros, both elements give the length of the first
1309 instruction in bytes.
1310
1311 Set to zero if we haven't yet seen the first instruction. */
1312 unsigned int first_insn_sizes[2];
1313
1314 /* For relaxable macros, insns[0] is the number of instructions for the
1315 first alternative and insns[1] is the number of instructions for the
1316 second alternative.
1317
1318 For non-relaxable macros, both elements give the number of
1319 instructions for the macro. */
1320 unsigned int insns[2];
1321
1322 /* The first variant frag for this macro. */
1323 fragS *first_frag;
1324 } mips_macro_warning;
1325 \f
1326 /* Prototypes for static functions. */
1327
1328 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1329
1330 static void append_insn
1331 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1332 bfd_boolean expansionp);
1333 static void mips_no_prev_insn (void);
1334 static void macro_build (expressionS *, const char *, const char *, ...);
1335 static void mips16_macro_build
1336 (expressionS *, const char *, const char *, va_list *);
1337 static void load_register (int, expressionS *, int);
1338 static void macro_start (void);
1339 static void macro_end (void);
1340 static void macro (struct mips_cl_insn *ip, char *str);
1341 static void mips16_macro (struct mips_cl_insn * ip);
1342 static void mips_ip (char *str, struct mips_cl_insn * ip);
1343 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1344 static void mips16_immed
1345 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1346 unsigned int, unsigned long *);
1347 static size_t my_getSmallExpression
1348 (expressionS *, bfd_reloc_code_real_type *, char *);
1349 static void my_getExpression (expressionS *, char *);
1350 static void s_align (int);
1351 static void s_change_sec (int);
1352 static void s_change_section (int);
1353 static void s_cons (int);
1354 static void s_float_cons (int);
1355 static void s_mips_globl (int);
1356 static void s_option (int);
1357 static void s_mipsset (int);
1358 static void s_abicalls (int);
1359 static void s_cpload (int);
1360 static void s_cpsetup (int);
1361 static void s_cplocal (int);
1362 static void s_cprestore (int);
1363 static void s_cpreturn (int);
1364 static void s_dtprelword (int);
1365 static void s_dtpreldword (int);
1366 static void s_tprelword (int);
1367 static void s_tpreldword (int);
1368 static void s_gpvalue (int);
1369 static void s_gpword (int);
1370 static void s_gpdword (int);
1371 static void s_ehword (int);
1372 static void s_cpadd (int);
1373 static void s_insn (int);
1374 static void s_nan (int);
1375 static void s_module (int);
1376 static void s_mips_ent (int);
1377 static void s_mips_end (int);
1378 static void s_mips_frame (int);
1379 static void s_mips_mask (int reg_type);
1380 static void s_mips_stab (int);
1381 static void s_mips_weakext (int);
1382 static void s_mips_file (int);
1383 static void s_mips_loc (int);
1384 static bfd_boolean pic_need_relax (symbolS *);
1385 static int relaxed_branch_length (fragS *, asection *, int);
1386 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1387 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1388 static void file_mips_check_options (void);
1389
1390 /* Table and functions used to map between CPU/ISA names, and
1391 ISA levels, and CPU numbers. */
1392
1393 struct mips_cpu_info
1394 {
1395 const char *name; /* CPU or ISA name. */
1396 int flags; /* MIPS_CPU_* flags. */
1397 int ase; /* Set of ASEs implemented by the CPU. */
1398 int isa; /* ISA level. */
1399 int cpu; /* CPU number (default CPU if ISA). */
1400 };
1401
1402 #define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1403
1404 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1405 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1406 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1407 \f
1408 /* Command-line options. */
1409 const char *md_shortopts = "O::g::G:";
1410
1411 enum options
1412 {
1413 OPTION_MARCH = OPTION_MD_BASE,
1414 OPTION_MTUNE,
1415 OPTION_MIPS1,
1416 OPTION_MIPS2,
1417 OPTION_MIPS3,
1418 OPTION_MIPS4,
1419 OPTION_MIPS5,
1420 OPTION_MIPS32,
1421 OPTION_MIPS64,
1422 OPTION_MIPS32R2,
1423 OPTION_MIPS32R3,
1424 OPTION_MIPS32R5,
1425 OPTION_MIPS32R6,
1426 OPTION_MIPS64R2,
1427 OPTION_MIPS64R3,
1428 OPTION_MIPS64R5,
1429 OPTION_MIPS64R6,
1430 OPTION_MIPS16,
1431 OPTION_NO_MIPS16,
1432 OPTION_MIPS3D,
1433 OPTION_NO_MIPS3D,
1434 OPTION_MDMX,
1435 OPTION_NO_MDMX,
1436 OPTION_DSP,
1437 OPTION_NO_DSP,
1438 OPTION_MT,
1439 OPTION_NO_MT,
1440 OPTION_VIRT,
1441 OPTION_NO_VIRT,
1442 OPTION_MSA,
1443 OPTION_NO_MSA,
1444 OPTION_SMARTMIPS,
1445 OPTION_NO_SMARTMIPS,
1446 OPTION_DSPR2,
1447 OPTION_NO_DSPR2,
1448 OPTION_DSPR3,
1449 OPTION_NO_DSPR3,
1450 OPTION_EVA,
1451 OPTION_NO_EVA,
1452 OPTION_XPA,
1453 OPTION_NO_XPA,
1454 OPTION_MICROMIPS,
1455 OPTION_NO_MICROMIPS,
1456 OPTION_MCU,
1457 OPTION_NO_MCU,
1458 OPTION_COMPAT_ARCH_BASE,
1459 OPTION_M4650,
1460 OPTION_NO_M4650,
1461 OPTION_M4010,
1462 OPTION_NO_M4010,
1463 OPTION_M4100,
1464 OPTION_NO_M4100,
1465 OPTION_M3900,
1466 OPTION_NO_M3900,
1467 OPTION_M7000_HILO_FIX,
1468 OPTION_MNO_7000_HILO_FIX,
1469 OPTION_FIX_24K,
1470 OPTION_NO_FIX_24K,
1471 OPTION_FIX_RM7000,
1472 OPTION_NO_FIX_RM7000,
1473 OPTION_FIX_LOONGSON2F_JUMP,
1474 OPTION_NO_FIX_LOONGSON2F_JUMP,
1475 OPTION_FIX_LOONGSON2F_NOP,
1476 OPTION_NO_FIX_LOONGSON2F_NOP,
1477 OPTION_FIX_VR4120,
1478 OPTION_NO_FIX_VR4120,
1479 OPTION_FIX_VR4130,
1480 OPTION_NO_FIX_VR4130,
1481 OPTION_FIX_CN63XXP1,
1482 OPTION_NO_FIX_CN63XXP1,
1483 OPTION_TRAP,
1484 OPTION_BREAK,
1485 OPTION_EB,
1486 OPTION_EL,
1487 OPTION_FP32,
1488 OPTION_GP32,
1489 OPTION_CONSTRUCT_FLOATS,
1490 OPTION_NO_CONSTRUCT_FLOATS,
1491 OPTION_FP64,
1492 OPTION_FPXX,
1493 OPTION_GP64,
1494 OPTION_RELAX_BRANCH,
1495 OPTION_NO_RELAX_BRANCH,
1496 OPTION_IGNORE_BRANCH_ISA,
1497 OPTION_NO_IGNORE_BRANCH_ISA,
1498 OPTION_INSN32,
1499 OPTION_NO_INSN32,
1500 OPTION_MSHARED,
1501 OPTION_MNO_SHARED,
1502 OPTION_MSYM32,
1503 OPTION_MNO_SYM32,
1504 OPTION_SOFT_FLOAT,
1505 OPTION_HARD_FLOAT,
1506 OPTION_SINGLE_FLOAT,
1507 OPTION_DOUBLE_FLOAT,
1508 OPTION_32,
1509 OPTION_CALL_SHARED,
1510 OPTION_CALL_NONPIC,
1511 OPTION_NON_SHARED,
1512 OPTION_XGOT,
1513 OPTION_MABI,
1514 OPTION_N32,
1515 OPTION_64,
1516 OPTION_MDEBUG,
1517 OPTION_NO_MDEBUG,
1518 OPTION_PDR,
1519 OPTION_NO_PDR,
1520 OPTION_MVXWORKS_PIC,
1521 OPTION_NAN,
1522 OPTION_ODD_SPREG,
1523 OPTION_NO_ODD_SPREG,
1524 OPTION_END_OF_ENUM
1525 };
1526
1527 struct option md_longopts[] =
1528 {
1529 /* Options which specify architecture. */
1530 {"march", required_argument, NULL, OPTION_MARCH},
1531 {"mtune", required_argument, NULL, OPTION_MTUNE},
1532 {"mips0", no_argument, NULL, OPTION_MIPS1},
1533 {"mips1", no_argument, NULL, OPTION_MIPS1},
1534 {"mips2", no_argument, NULL, OPTION_MIPS2},
1535 {"mips3", no_argument, NULL, OPTION_MIPS3},
1536 {"mips4", no_argument, NULL, OPTION_MIPS4},
1537 {"mips5", no_argument, NULL, OPTION_MIPS5},
1538 {"mips32", no_argument, NULL, OPTION_MIPS32},
1539 {"mips64", no_argument, NULL, OPTION_MIPS64},
1540 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1541 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1542 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
1543 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
1544 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1545 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1546 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
1547 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
1548
1549 /* Options which specify Application Specific Extensions (ASEs). */
1550 {"mips16", no_argument, NULL, OPTION_MIPS16},
1551 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1552 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1553 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1554 {"mdmx", no_argument, NULL, OPTION_MDMX},
1555 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1556 {"mdsp", no_argument, NULL, OPTION_DSP},
1557 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1558 {"mmt", no_argument, NULL, OPTION_MT},
1559 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1560 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1561 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1562 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1563 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1564 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1565 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
1566 {"meva", no_argument, NULL, OPTION_EVA},
1567 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1568 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1569 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1570 {"mmcu", no_argument, NULL, OPTION_MCU},
1571 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1572 {"mvirt", no_argument, NULL, OPTION_VIRT},
1573 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1574 {"mmsa", no_argument, NULL, OPTION_MSA},
1575 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
1576 {"mxpa", no_argument, NULL, OPTION_XPA},
1577 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
1578
1579 /* Old-style architecture options. Don't add more of these. */
1580 {"m4650", no_argument, NULL, OPTION_M4650},
1581 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1582 {"m4010", no_argument, NULL, OPTION_M4010},
1583 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1584 {"m4100", no_argument, NULL, OPTION_M4100},
1585 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1586 {"m3900", no_argument, NULL, OPTION_M3900},
1587 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1588
1589 /* Options which enable bug fixes. */
1590 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1591 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1592 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1593 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1594 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1595 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1596 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1597 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1598 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1599 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1600 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1601 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1602 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1603 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1604 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
1605 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1606 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1607
1608 /* Miscellaneous options. */
1609 {"trap", no_argument, NULL, OPTION_TRAP},
1610 {"no-break", no_argument, NULL, OPTION_TRAP},
1611 {"break", no_argument, NULL, OPTION_BREAK},
1612 {"no-trap", no_argument, NULL, OPTION_BREAK},
1613 {"EB", no_argument, NULL, OPTION_EB},
1614 {"EL", no_argument, NULL, OPTION_EL},
1615 {"mfp32", no_argument, NULL, OPTION_FP32},
1616 {"mgp32", no_argument, NULL, OPTION_GP32},
1617 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1618 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1619 {"mfp64", no_argument, NULL, OPTION_FP64},
1620 {"mfpxx", no_argument, NULL, OPTION_FPXX},
1621 {"mgp64", no_argument, NULL, OPTION_GP64},
1622 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1623 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
1624 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1625 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
1626 {"minsn32", no_argument, NULL, OPTION_INSN32},
1627 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
1628 {"mshared", no_argument, NULL, OPTION_MSHARED},
1629 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1630 {"msym32", no_argument, NULL, OPTION_MSYM32},
1631 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1632 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1633 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1634 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1635 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1636 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1637 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
1638
1639 /* Strictly speaking this next option is ELF specific,
1640 but we allow it for other ports as well in order to
1641 make testing easier. */
1642 {"32", no_argument, NULL, OPTION_32},
1643
1644 /* ELF-specific options. */
1645 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1646 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1647 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1648 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1649 {"xgot", no_argument, NULL, OPTION_XGOT},
1650 {"mabi", required_argument, NULL, OPTION_MABI},
1651 {"n32", no_argument, NULL, OPTION_N32},
1652 {"64", no_argument, NULL, OPTION_64},
1653 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1654 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1655 {"mpdr", no_argument, NULL, OPTION_PDR},
1656 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1657 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
1658 {"mnan", required_argument, NULL, OPTION_NAN},
1659
1660 {NULL, no_argument, NULL, 0}
1661 };
1662 size_t md_longopts_size = sizeof (md_longopts);
1663 \f
1664 /* Information about either an Application Specific Extension or an
1665 optional architecture feature that, for simplicity, we treat in the
1666 same way as an ASE. */
1667 struct mips_ase
1668 {
1669 /* The name of the ASE, used in both the command-line and .set options. */
1670 const char *name;
1671
1672 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1673 and 64-bit architectures, the flags here refer to the subset that
1674 is available on both. */
1675 unsigned int flags;
1676
1677 /* The ASE_* flag used for instructions that are available on 64-bit
1678 architectures but that are not included in FLAGS. */
1679 unsigned int flags64;
1680
1681 /* The command-line options that turn the ASE on and off. */
1682 int option_on;
1683 int option_off;
1684
1685 /* The minimum required architecture revisions for MIPS32, MIPS64,
1686 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1687 int mips32_rev;
1688 int mips64_rev;
1689 int micromips32_rev;
1690 int micromips64_rev;
1691
1692 /* The architecture where the ASE was removed or -1 if the extension has not
1693 been removed. */
1694 int rem_rev;
1695 };
1696
1697 /* A table of all supported ASEs. */
1698 static const struct mips_ase mips_ases[] = {
1699 { "dsp", ASE_DSP, ASE_DSP64,
1700 OPTION_DSP, OPTION_NO_DSP,
1701 2, 2, 2, 2,
1702 -1 },
1703
1704 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1705 OPTION_DSPR2, OPTION_NO_DSPR2,
1706 2, 2, 2, 2,
1707 -1 },
1708
1709 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1710 OPTION_DSPR3, OPTION_NO_DSPR3,
1711 6, 6, -1, -1,
1712 -1 },
1713
1714 { "eva", ASE_EVA, 0,
1715 OPTION_EVA, OPTION_NO_EVA,
1716 2, 2, 2, 2,
1717 -1 },
1718
1719 { "mcu", ASE_MCU, 0,
1720 OPTION_MCU, OPTION_NO_MCU,
1721 2, 2, 2, 2,
1722 -1 },
1723
1724 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1725 { "mdmx", ASE_MDMX, 0,
1726 OPTION_MDMX, OPTION_NO_MDMX,
1727 -1, 1, -1, -1,
1728 6 },
1729
1730 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1731 { "mips3d", ASE_MIPS3D, 0,
1732 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1733 2, 1, -1, -1,
1734 6 },
1735
1736 { "mt", ASE_MT, 0,
1737 OPTION_MT, OPTION_NO_MT,
1738 2, 2, -1, -1,
1739 -1 },
1740
1741 { "smartmips", ASE_SMARTMIPS, 0,
1742 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1743 1, -1, -1, -1,
1744 6 },
1745
1746 { "virt", ASE_VIRT, ASE_VIRT64,
1747 OPTION_VIRT, OPTION_NO_VIRT,
1748 2, 2, 2, 2,
1749 -1 },
1750
1751 { "msa", ASE_MSA, ASE_MSA64,
1752 OPTION_MSA, OPTION_NO_MSA,
1753 2, 2, 2, 2,
1754 -1 },
1755
1756 { "xpa", ASE_XPA, 0,
1757 OPTION_XPA, OPTION_NO_XPA,
1758 2, 2, -1, -1,
1759 -1 },
1760 };
1761
1762 /* The set of ASEs that require -mfp64. */
1763 #define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
1764
1765 /* Groups of ASE_* flags that represent different revisions of an ASE. */
1766 static const unsigned int mips_ase_groups[] = {
1767 ASE_DSP | ASE_DSPR2 | ASE_DSPR3
1768 };
1769 \f
1770 /* Pseudo-op table.
1771
1772 The following pseudo-ops from the Kane and Heinrich MIPS book
1773 should be defined here, but are currently unsupported: .alias,
1774 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1775
1776 The following pseudo-ops from the Kane and Heinrich MIPS book are
1777 specific to the type of debugging information being generated, and
1778 should be defined by the object format: .aent, .begin, .bend,
1779 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1780 .vreg.
1781
1782 The following pseudo-ops from the Kane and Heinrich MIPS book are
1783 not MIPS CPU specific, but are also not specific to the object file
1784 format. This file is probably the best place to define them, but
1785 they are not currently supported: .asm0, .endr, .lab, .struct. */
1786
1787 static const pseudo_typeS mips_pseudo_table[] =
1788 {
1789 /* MIPS specific pseudo-ops. */
1790 {"option", s_option, 0},
1791 {"set", s_mipsset, 0},
1792 {"rdata", s_change_sec, 'r'},
1793 {"sdata", s_change_sec, 's'},
1794 {"livereg", s_ignore, 0},
1795 {"abicalls", s_abicalls, 0},
1796 {"cpload", s_cpload, 0},
1797 {"cpsetup", s_cpsetup, 0},
1798 {"cplocal", s_cplocal, 0},
1799 {"cprestore", s_cprestore, 0},
1800 {"cpreturn", s_cpreturn, 0},
1801 {"dtprelword", s_dtprelword, 0},
1802 {"dtpreldword", s_dtpreldword, 0},
1803 {"tprelword", s_tprelword, 0},
1804 {"tpreldword", s_tpreldword, 0},
1805 {"gpvalue", s_gpvalue, 0},
1806 {"gpword", s_gpword, 0},
1807 {"gpdword", s_gpdword, 0},
1808 {"ehword", s_ehword, 0},
1809 {"cpadd", s_cpadd, 0},
1810 {"insn", s_insn, 0},
1811 {"nan", s_nan, 0},
1812 {"module", s_module, 0},
1813
1814 /* Relatively generic pseudo-ops that happen to be used on MIPS
1815 chips. */
1816 {"asciiz", stringer, 8 + 1},
1817 {"bss", s_change_sec, 'b'},
1818 {"err", s_err, 0},
1819 {"half", s_cons, 1},
1820 {"dword", s_cons, 3},
1821 {"weakext", s_mips_weakext, 0},
1822 {"origin", s_org, 0},
1823 {"repeat", s_rept, 0},
1824
1825 /* For MIPS this is non-standard, but we define it for consistency. */
1826 {"sbss", s_change_sec, 'B'},
1827
1828 /* These pseudo-ops are defined in read.c, but must be overridden
1829 here for one reason or another. */
1830 {"align", s_align, 0},
1831 {"byte", s_cons, 0},
1832 {"data", s_change_sec, 'd'},
1833 {"double", s_float_cons, 'd'},
1834 {"float", s_float_cons, 'f'},
1835 {"globl", s_mips_globl, 0},
1836 {"global", s_mips_globl, 0},
1837 {"hword", s_cons, 1},
1838 {"int", s_cons, 2},
1839 {"long", s_cons, 2},
1840 {"octa", s_cons, 4},
1841 {"quad", s_cons, 3},
1842 {"section", s_change_section, 0},
1843 {"short", s_cons, 1},
1844 {"single", s_float_cons, 'f'},
1845 {"stabd", s_mips_stab, 'd'},
1846 {"stabn", s_mips_stab, 'n'},
1847 {"stabs", s_mips_stab, 's'},
1848 {"text", s_change_sec, 't'},
1849 {"word", s_cons, 2},
1850
1851 { "extern", ecoff_directive_extern, 0},
1852
1853 { NULL, NULL, 0 },
1854 };
1855
1856 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1857 {
1858 /* These pseudo-ops should be defined by the object file format.
1859 However, a.out doesn't support them, so we have versions here. */
1860 {"aent", s_mips_ent, 1},
1861 {"bgnb", s_ignore, 0},
1862 {"end", s_mips_end, 0},
1863 {"endb", s_ignore, 0},
1864 {"ent", s_mips_ent, 0},
1865 {"file", s_mips_file, 0},
1866 {"fmask", s_mips_mask, 'F'},
1867 {"frame", s_mips_frame, 0},
1868 {"loc", s_mips_loc, 0},
1869 {"mask", s_mips_mask, 'R'},
1870 {"verstamp", s_ignore, 0},
1871 { NULL, NULL, 0 },
1872 };
1873
1874 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1875 purpose of the `.dc.a' internal pseudo-op. */
1876
1877 int
1878 mips_address_bytes (void)
1879 {
1880 file_mips_check_options ();
1881 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1882 }
1883
1884 extern void pop_insert (const pseudo_typeS *);
1885
1886 void
1887 mips_pop_insert (void)
1888 {
1889 pop_insert (mips_pseudo_table);
1890 if (! ECOFF_DEBUGGING)
1891 pop_insert (mips_nonecoff_pseudo_table);
1892 }
1893 \f
1894 /* Symbols labelling the current insn. */
1895
1896 struct insn_label_list
1897 {
1898 struct insn_label_list *next;
1899 symbolS *label;
1900 };
1901
1902 static struct insn_label_list *free_insn_labels;
1903 #define label_list tc_segment_info_data.labels
1904
1905 static void mips_clear_insn_labels (void);
1906 static void mips_mark_labels (void);
1907 static void mips_compressed_mark_labels (void);
1908
1909 static inline void
1910 mips_clear_insn_labels (void)
1911 {
1912 struct insn_label_list **pl;
1913 segment_info_type *si;
1914
1915 if (now_seg)
1916 {
1917 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1918 ;
1919
1920 si = seg_info (now_seg);
1921 *pl = si->label_list;
1922 si->label_list = NULL;
1923 }
1924 }
1925
1926 /* Mark instruction labels in MIPS16/microMIPS mode. */
1927
1928 static inline void
1929 mips_mark_labels (void)
1930 {
1931 if (HAVE_CODE_COMPRESSION)
1932 mips_compressed_mark_labels ();
1933 }
1934 \f
1935 static char *expr_end;
1936
1937 /* An expression in a macro instruction. This is set by mips_ip and
1938 mips16_ip and when populated is always an O_constant. */
1939
1940 static expressionS imm_expr;
1941
1942 /* The relocatable field in an instruction and the relocs associated
1943 with it. These variables are used for instructions like LUI and
1944 JAL as well as true offsets. They are also used for address
1945 operands in macros. */
1946
1947 static expressionS offset_expr;
1948 static bfd_reloc_code_real_type offset_reloc[3]
1949 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1950
1951 /* This is set to the resulting size of the instruction to be produced
1952 by mips16_ip if an explicit extension is used or by mips_ip if an
1953 explicit size is supplied. */
1954
1955 static unsigned int forced_insn_length;
1956
1957 /* True if we are assembling an instruction. All dot symbols defined during
1958 this time should be treated as code labels. */
1959
1960 static bfd_boolean mips_assembling_insn;
1961
1962 /* The pdr segment for per procedure frame/regmask info. Not used for
1963 ECOFF debugging. */
1964
1965 static segT pdr_seg;
1966
1967 /* The default target format to use. */
1968
1969 #if defined (TE_FreeBSD)
1970 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1971 #elif defined (TE_TMIPS)
1972 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1973 #else
1974 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1975 #endif
1976
1977 const char *
1978 mips_target_format (void)
1979 {
1980 switch (OUTPUT_FLAVOR)
1981 {
1982 case bfd_target_elf_flavour:
1983 #ifdef TE_VXWORKS
1984 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1985 return (target_big_endian
1986 ? "elf32-bigmips-vxworks"
1987 : "elf32-littlemips-vxworks");
1988 #endif
1989 return (target_big_endian
1990 ? (HAVE_64BIT_OBJECTS
1991 ? ELF_TARGET ("elf64-", "big")
1992 : (HAVE_NEWABI
1993 ? ELF_TARGET ("elf32-n", "big")
1994 : ELF_TARGET ("elf32-", "big")))
1995 : (HAVE_64BIT_OBJECTS
1996 ? ELF_TARGET ("elf64-", "little")
1997 : (HAVE_NEWABI
1998 ? ELF_TARGET ("elf32-n", "little")
1999 : ELF_TARGET ("elf32-", "little"))));
2000 default:
2001 abort ();
2002 return NULL;
2003 }
2004 }
2005
2006 /* Return the ISA revision that is currently in use, or 0 if we are
2007 generating code for MIPS V or below. */
2008
2009 static int
2010 mips_isa_rev (void)
2011 {
2012 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2013 return 2;
2014
2015 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2016 return 3;
2017
2018 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2019 return 5;
2020
2021 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2022 return 6;
2023
2024 /* microMIPS implies revision 2 or above. */
2025 if (mips_opts.micromips)
2026 return 2;
2027
2028 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2029 return 1;
2030
2031 return 0;
2032 }
2033
2034 /* Return the mask of all ASEs that are revisions of those in FLAGS. */
2035
2036 static unsigned int
2037 mips_ase_mask (unsigned int flags)
2038 {
2039 unsigned int i;
2040
2041 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2042 if (flags & mips_ase_groups[i])
2043 flags |= mips_ase_groups[i];
2044 return flags;
2045 }
2046
2047 /* Check whether the current ISA supports ASE. Issue a warning if
2048 appropriate. */
2049
2050 static void
2051 mips_check_isa_supports_ase (const struct mips_ase *ase)
2052 {
2053 const char *base;
2054 int min_rev, size;
2055 static unsigned int warned_isa;
2056 static unsigned int warned_fp32;
2057
2058 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2059 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2060 else
2061 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2062 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2063 && (warned_isa & ase->flags) != ase->flags)
2064 {
2065 warned_isa |= ase->flags;
2066 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2067 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2068 if (min_rev < 0)
2069 as_warn (_("the %d-bit %s architecture does not support the"
2070 " `%s' extension"), size, base, ase->name);
2071 else
2072 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
2073 ase->name, base, size, min_rev);
2074 }
2075 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2076 && (warned_isa & ase->flags) != ase->flags)
2077 {
2078 warned_isa |= ase->flags;
2079 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2080 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2081 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2082 ase->name, base, size, ase->rem_rev);
2083 }
2084
2085 if ((ase->flags & FP64_ASES)
2086 && mips_opts.fp != 64
2087 && (warned_fp32 & ase->flags) != ase->flags)
2088 {
2089 warned_fp32 |= ase->flags;
2090 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
2091 }
2092 }
2093
2094 /* Check all enabled ASEs to see whether they are supported by the
2095 chosen architecture. */
2096
2097 static void
2098 mips_check_isa_supports_ases (void)
2099 {
2100 unsigned int i, mask;
2101
2102 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2103 {
2104 mask = mips_ase_mask (mips_ases[i].flags);
2105 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2106 mips_check_isa_supports_ase (&mips_ases[i]);
2107 }
2108 }
2109
2110 /* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2111 that were affected. */
2112
2113 static unsigned int
2114 mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2115 bfd_boolean enabled_p)
2116 {
2117 unsigned int mask;
2118
2119 mask = mips_ase_mask (ase->flags);
2120 opts->ase &= ~mask;
2121 if (enabled_p)
2122 opts->ase |= ase->flags;
2123 return mask;
2124 }
2125
2126 /* Return the ASE called NAME, or null if none. */
2127
2128 static const struct mips_ase *
2129 mips_lookup_ase (const char *name)
2130 {
2131 unsigned int i;
2132
2133 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2134 if (strcmp (name, mips_ases[i].name) == 0)
2135 return &mips_ases[i];
2136 return NULL;
2137 }
2138
2139 /* Return the length of a microMIPS instruction in bytes. If bits of
2140 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2141 otherwise it is a 32-bit instruction. */
2142
2143 static inline unsigned int
2144 micromips_insn_length (const struct mips_opcode *mo)
2145 {
2146 return mips_opcode_32bit_p (mo) ? 4 : 2;
2147 }
2148
2149 /* Return the length of MIPS16 instruction OPCODE. */
2150
2151 static inline unsigned int
2152 mips16_opcode_length (unsigned long opcode)
2153 {
2154 return (opcode >> 16) == 0 ? 2 : 4;
2155 }
2156
2157 /* Return the length of instruction INSN. */
2158
2159 static inline unsigned int
2160 insn_length (const struct mips_cl_insn *insn)
2161 {
2162 if (mips_opts.micromips)
2163 return micromips_insn_length (insn->insn_mo);
2164 else if (mips_opts.mips16)
2165 return mips16_opcode_length (insn->insn_opcode);
2166 else
2167 return 4;
2168 }
2169
2170 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2171
2172 static void
2173 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2174 {
2175 size_t i;
2176
2177 insn->insn_mo = mo;
2178 insn->insn_opcode = mo->match;
2179 insn->frag = NULL;
2180 insn->where = 0;
2181 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2182 insn->fixp[i] = NULL;
2183 insn->fixed_p = (mips_opts.noreorder > 0);
2184 insn->noreorder_p = (mips_opts.noreorder > 0);
2185 insn->mips16_absolute_jump_p = 0;
2186 insn->complete_p = 0;
2187 insn->cleared_p = 0;
2188 }
2189
2190 /* Get a list of all the operands in INSN. */
2191
2192 static const struct mips_operand_array *
2193 insn_operands (const struct mips_cl_insn *insn)
2194 {
2195 if (insn->insn_mo >= &mips_opcodes[0]
2196 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2197 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2198
2199 if (insn->insn_mo >= &mips16_opcodes[0]
2200 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2201 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2202
2203 if (insn->insn_mo >= &micromips_opcodes[0]
2204 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2205 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2206
2207 abort ();
2208 }
2209
2210 /* Get a description of operand OPNO of INSN. */
2211
2212 static const struct mips_operand *
2213 insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2214 {
2215 const struct mips_operand_array *operands;
2216
2217 operands = insn_operands (insn);
2218 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2219 abort ();
2220 return operands->operand[opno];
2221 }
2222
2223 /* Install UVAL as the value of OPERAND in INSN. */
2224
2225 static inline void
2226 insn_insert_operand (struct mips_cl_insn *insn,
2227 const struct mips_operand *operand, unsigned int uval)
2228 {
2229 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2230 }
2231
2232 /* Extract the value of OPERAND from INSN. */
2233
2234 static inline unsigned
2235 insn_extract_operand (const struct mips_cl_insn *insn,
2236 const struct mips_operand *operand)
2237 {
2238 return mips_extract_operand (operand, insn->insn_opcode);
2239 }
2240
2241 /* Record the current MIPS16/microMIPS mode in now_seg. */
2242
2243 static void
2244 mips_record_compressed_mode (void)
2245 {
2246 segment_info_type *si;
2247
2248 si = seg_info (now_seg);
2249 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2250 si->tc_segment_info_data.mips16 = mips_opts.mips16;
2251 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2252 si->tc_segment_info_data.micromips = mips_opts.micromips;
2253 }
2254
2255 /* Read a standard MIPS instruction from BUF. */
2256
2257 static unsigned long
2258 read_insn (char *buf)
2259 {
2260 if (target_big_endian)
2261 return bfd_getb32 ((bfd_byte *) buf);
2262 else
2263 return bfd_getl32 ((bfd_byte *) buf);
2264 }
2265
2266 /* Write standard MIPS instruction INSN to BUF. Return a pointer to
2267 the next byte. */
2268
2269 static char *
2270 write_insn (char *buf, unsigned int insn)
2271 {
2272 md_number_to_chars (buf, insn, 4);
2273 return buf + 4;
2274 }
2275
2276 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
2277 has length LENGTH. */
2278
2279 static unsigned long
2280 read_compressed_insn (char *buf, unsigned int length)
2281 {
2282 unsigned long insn;
2283 unsigned int i;
2284
2285 insn = 0;
2286 for (i = 0; i < length; i += 2)
2287 {
2288 insn <<= 16;
2289 if (target_big_endian)
2290 insn |= bfd_getb16 ((char *) buf);
2291 else
2292 insn |= bfd_getl16 ((char *) buf);
2293 buf += 2;
2294 }
2295 return insn;
2296 }
2297
2298 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2299 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2300
2301 static char *
2302 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2303 {
2304 unsigned int i;
2305
2306 for (i = 0; i < length; i += 2)
2307 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2308 return buf + length;
2309 }
2310
2311 /* Install INSN at the location specified by its "frag" and "where" fields. */
2312
2313 static void
2314 install_insn (const struct mips_cl_insn *insn)
2315 {
2316 char *f = insn->frag->fr_literal + insn->where;
2317 if (HAVE_CODE_COMPRESSION)
2318 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
2319 else
2320 write_insn (f, insn->insn_opcode);
2321 mips_record_compressed_mode ();
2322 }
2323
2324 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2325 and install the opcode in the new location. */
2326
2327 static void
2328 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2329 {
2330 size_t i;
2331
2332 insn->frag = frag;
2333 insn->where = where;
2334 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2335 if (insn->fixp[i] != NULL)
2336 {
2337 insn->fixp[i]->fx_frag = frag;
2338 insn->fixp[i]->fx_where = where;
2339 }
2340 install_insn (insn);
2341 }
2342
2343 /* Add INSN to the end of the output. */
2344
2345 static void
2346 add_fixed_insn (struct mips_cl_insn *insn)
2347 {
2348 char *f = frag_more (insn_length (insn));
2349 move_insn (insn, frag_now, f - frag_now->fr_literal);
2350 }
2351
2352 /* Start a variant frag and move INSN to the start of the variant part,
2353 marking it as fixed. The other arguments are as for frag_var. */
2354
2355 static void
2356 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2357 relax_substateT subtype, symbolS *symbol, offsetT offset)
2358 {
2359 frag_grow (max_chars);
2360 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2361 insn->fixed_p = 1;
2362 frag_var (rs_machine_dependent, max_chars, var,
2363 subtype, symbol, offset, NULL);
2364 }
2365
2366 /* Insert N copies of INSN into the history buffer, starting at
2367 position FIRST. Neither FIRST nor N need to be clipped. */
2368
2369 static void
2370 insert_into_history (unsigned int first, unsigned int n,
2371 const struct mips_cl_insn *insn)
2372 {
2373 if (mips_relax.sequence != 2)
2374 {
2375 unsigned int i;
2376
2377 for (i = ARRAY_SIZE (history); i-- > first;)
2378 if (i >= first + n)
2379 history[i] = history[i - n];
2380 else
2381 history[i] = *insn;
2382 }
2383 }
2384
2385 /* Clear the error in insn_error. */
2386
2387 static void
2388 clear_insn_error (void)
2389 {
2390 memset (&insn_error, 0, sizeof (insn_error));
2391 }
2392
2393 /* Possibly record error message MSG for the current instruction.
2394 If the error is about a particular argument, ARGNUM is the 1-based
2395 number of that argument, otherwise it is 0. FORMAT is the format
2396 of MSG. Return true if MSG was used, false if the current message
2397 was kept. */
2398
2399 static bfd_boolean
2400 set_insn_error_format (int argnum, enum mips_insn_error_format format,
2401 const char *msg)
2402 {
2403 if (argnum == 0)
2404 {
2405 /* Give priority to errors against specific arguments, and to
2406 the first whole-instruction message. */
2407 if (insn_error.msg)
2408 return FALSE;
2409 }
2410 else
2411 {
2412 /* Keep insn_error if it is against a later argument. */
2413 if (argnum < insn_error.min_argnum)
2414 return FALSE;
2415
2416 /* If both errors are against the same argument but are different,
2417 give up on reporting a specific error for this argument.
2418 See the comment about mips_insn_error for details. */
2419 if (argnum == insn_error.min_argnum
2420 && insn_error.msg
2421 && strcmp (insn_error.msg, msg) != 0)
2422 {
2423 insn_error.msg = 0;
2424 insn_error.min_argnum += 1;
2425 return FALSE;
2426 }
2427 }
2428 insn_error.min_argnum = argnum;
2429 insn_error.format = format;
2430 insn_error.msg = msg;
2431 return TRUE;
2432 }
2433
2434 /* Record an instruction error with no % format fields. ARGNUM and MSG are
2435 as for set_insn_error_format. */
2436
2437 static void
2438 set_insn_error (int argnum, const char *msg)
2439 {
2440 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2441 }
2442
2443 /* Record an instruction error with one %d field I. ARGNUM and MSG are
2444 as for set_insn_error_format. */
2445
2446 static void
2447 set_insn_error_i (int argnum, const char *msg, int i)
2448 {
2449 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2450 insn_error.u.i = i;
2451 }
2452
2453 /* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2454 are as for set_insn_error_format. */
2455
2456 static void
2457 set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2458 {
2459 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2460 {
2461 insn_error.u.ss[0] = s1;
2462 insn_error.u.ss[1] = s2;
2463 }
2464 }
2465
2466 /* Report the error in insn_error, which is against assembly code STR. */
2467
2468 static void
2469 report_insn_error (const char *str)
2470 {
2471 const char *msg = concat (insn_error.msg, " `%s'", NULL);
2472
2473 switch (insn_error.format)
2474 {
2475 case ERR_FMT_PLAIN:
2476 as_bad (msg, str);
2477 break;
2478
2479 case ERR_FMT_I:
2480 as_bad (msg, insn_error.u.i, str);
2481 break;
2482
2483 case ERR_FMT_SS:
2484 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2485 break;
2486 }
2487
2488 free ((char *) msg);
2489 }
2490
2491 /* Initialize vr4120_conflicts. There is a bit of duplication here:
2492 the idea is to make it obvious at a glance that each errata is
2493 included. */
2494
2495 static void
2496 init_vr4120_conflicts (void)
2497 {
2498 #define CONFLICT(FIRST, SECOND) \
2499 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2500
2501 /* Errata 21 - [D]DIV[U] after [D]MACC */
2502 CONFLICT (MACC, DIV);
2503 CONFLICT (DMACC, DIV);
2504
2505 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2506 CONFLICT (DMULT, DMULT);
2507 CONFLICT (DMULT, DMACC);
2508 CONFLICT (DMACC, DMULT);
2509 CONFLICT (DMACC, DMACC);
2510
2511 /* Errata 24 - MT{LO,HI} after [D]MACC */
2512 CONFLICT (MACC, MTHILO);
2513 CONFLICT (DMACC, MTHILO);
2514
2515 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2516 instruction is executed immediately after a MACC or DMACC
2517 instruction, the result of [either instruction] is incorrect." */
2518 CONFLICT (MACC, MULT);
2519 CONFLICT (MACC, DMULT);
2520 CONFLICT (DMACC, MULT);
2521 CONFLICT (DMACC, DMULT);
2522
2523 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2524 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2525 DDIV or DDIVU instruction, the result of the MACC or
2526 DMACC instruction is incorrect.". */
2527 CONFLICT (DMULT, MACC);
2528 CONFLICT (DMULT, DMACC);
2529 CONFLICT (DIV, MACC);
2530 CONFLICT (DIV, DMACC);
2531
2532 #undef CONFLICT
2533 }
2534
2535 struct regname {
2536 const char *name;
2537 unsigned int num;
2538 };
2539
2540 #define RNUM_MASK 0x00000ff
2541 #define RTYPE_MASK 0x0ffff00
2542 #define RTYPE_NUM 0x0000100
2543 #define RTYPE_FPU 0x0000200
2544 #define RTYPE_FCC 0x0000400
2545 #define RTYPE_VEC 0x0000800
2546 #define RTYPE_GP 0x0001000
2547 #define RTYPE_CP0 0x0002000
2548 #define RTYPE_PC 0x0004000
2549 #define RTYPE_ACC 0x0008000
2550 #define RTYPE_CCC 0x0010000
2551 #define RTYPE_VI 0x0020000
2552 #define RTYPE_VF 0x0040000
2553 #define RTYPE_R5900_I 0x0080000
2554 #define RTYPE_R5900_Q 0x0100000
2555 #define RTYPE_R5900_R 0x0200000
2556 #define RTYPE_R5900_ACC 0x0400000
2557 #define RTYPE_MSA 0x0800000
2558 #define RWARN 0x8000000
2559
2560 #define GENERIC_REGISTER_NUMBERS \
2561 {"$0", RTYPE_NUM | 0}, \
2562 {"$1", RTYPE_NUM | 1}, \
2563 {"$2", RTYPE_NUM | 2}, \
2564 {"$3", RTYPE_NUM | 3}, \
2565 {"$4", RTYPE_NUM | 4}, \
2566 {"$5", RTYPE_NUM | 5}, \
2567 {"$6", RTYPE_NUM | 6}, \
2568 {"$7", RTYPE_NUM | 7}, \
2569 {"$8", RTYPE_NUM | 8}, \
2570 {"$9", RTYPE_NUM | 9}, \
2571 {"$10", RTYPE_NUM | 10}, \
2572 {"$11", RTYPE_NUM | 11}, \
2573 {"$12", RTYPE_NUM | 12}, \
2574 {"$13", RTYPE_NUM | 13}, \
2575 {"$14", RTYPE_NUM | 14}, \
2576 {"$15", RTYPE_NUM | 15}, \
2577 {"$16", RTYPE_NUM | 16}, \
2578 {"$17", RTYPE_NUM | 17}, \
2579 {"$18", RTYPE_NUM | 18}, \
2580 {"$19", RTYPE_NUM | 19}, \
2581 {"$20", RTYPE_NUM | 20}, \
2582 {"$21", RTYPE_NUM | 21}, \
2583 {"$22", RTYPE_NUM | 22}, \
2584 {"$23", RTYPE_NUM | 23}, \
2585 {"$24", RTYPE_NUM | 24}, \
2586 {"$25", RTYPE_NUM | 25}, \
2587 {"$26", RTYPE_NUM | 26}, \
2588 {"$27", RTYPE_NUM | 27}, \
2589 {"$28", RTYPE_NUM | 28}, \
2590 {"$29", RTYPE_NUM | 29}, \
2591 {"$30", RTYPE_NUM | 30}, \
2592 {"$31", RTYPE_NUM | 31}
2593
2594 #define FPU_REGISTER_NAMES \
2595 {"$f0", RTYPE_FPU | 0}, \
2596 {"$f1", RTYPE_FPU | 1}, \
2597 {"$f2", RTYPE_FPU | 2}, \
2598 {"$f3", RTYPE_FPU | 3}, \
2599 {"$f4", RTYPE_FPU | 4}, \
2600 {"$f5", RTYPE_FPU | 5}, \
2601 {"$f6", RTYPE_FPU | 6}, \
2602 {"$f7", RTYPE_FPU | 7}, \
2603 {"$f8", RTYPE_FPU | 8}, \
2604 {"$f9", RTYPE_FPU | 9}, \
2605 {"$f10", RTYPE_FPU | 10}, \
2606 {"$f11", RTYPE_FPU | 11}, \
2607 {"$f12", RTYPE_FPU | 12}, \
2608 {"$f13", RTYPE_FPU | 13}, \
2609 {"$f14", RTYPE_FPU | 14}, \
2610 {"$f15", RTYPE_FPU | 15}, \
2611 {"$f16", RTYPE_FPU | 16}, \
2612 {"$f17", RTYPE_FPU | 17}, \
2613 {"$f18", RTYPE_FPU | 18}, \
2614 {"$f19", RTYPE_FPU | 19}, \
2615 {"$f20", RTYPE_FPU | 20}, \
2616 {"$f21", RTYPE_FPU | 21}, \
2617 {"$f22", RTYPE_FPU | 22}, \
2618 {"$f23", RTYPE_FPU | 23}, \
2619 {"$f24", RTYPE_FPU | 24}, \
2620 {"$f25", RTYPE_FPU | 25}, \
2621 {"$f26", RTYPE_FPU | 26}, \
2622 {"$f27", RTYPE_FPU | 27}, \
2623 {"$f28", RTYPE_FPU | 28}, \
2624 {"$f29", RTYPE_FPU | 29}, \
2625 {"$f30", RTYPE_FPU | 30}, \
2626 {"$f31", RTYPE_FPU | 31}
2627
2628 #define FPU_CONDITION_CODE_NAMES \
2629 {"$fcc0", RTYPE_FCC | 0}, \
2630 {"$fcc1", RTYPE_FCC | 1}, \
2631 {"$fcc2", RTYPE_FCC | 2}, \
2632 {"$fcc3", RTYPE_FCC | 3}, \
2633 {"$fcc4", RTYPE_FCC | 4}, \
2634 {"$fcc5", RTYPE_FCC | 5}, \
2635 {"$fcc6", RTYPE_FCC | 6}, \
2636 {"$fcc7", RTYPE_FCC | 7}
2637
2638 #define COPROC_CONDITION_CODE_NAMES \
2639 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2640 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2641 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2642 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2643 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2644 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2645 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2646 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2647
2648 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2649 {"$a4", RTYPE_GP | 8}, \
2650 {"$a5", RTYPE_GP | 9}, \
2651 {"$a6", RTYPE_GP | 10}, \
2652 {"$a7", RTYPE_GP | 11}, \
2653 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2654 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2655 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2656 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2657 {"$t0", RTYPE_GP | 12}, \
2658 {"$t1", RTYPE_GP | 13}, \
2659 {"$t2", RTYPE_GP | 14}, \
2660 {"$t3", RTYPE_GP | 15}
2661
2662 #define O32_SYMBOLIC_REGISTER_NAMES \
2663 {"$t0", RTYPE_GP | 8}, \
2664 {"$t1", RTYPE_GP | 9}, \
2665 {"$t2", RTYPE_GP | 10}, \
2666 {"$t3", RTYPE_GP | 11}, \
2667 {"$t4", RTYPE_GP | 12}, \
2668 {"$t5", RTYPE_GP | 13}, \
2669 {"$t6", RTYPE_GP | 14}, \
2670 {"$t7", RTYPE_GP | 15}, \
2671 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2672 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2673 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2674 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2675
2676 /* Remaining symbolic register names */
2677 #define SYMBOLIC_REGISTER_NAMES \
2678 {"$zero", RTYPE_GP | 0}, \
2679 {"$at", RTYPE_GP | 1}, \
2680 {"$AT", RTYPE_GP | 1}, \
2681 {"$v0", RTYPE_GP | 2}, \
2682 {"$v1", RTYPE_GP | 3}, \
2683 {"$a0", RTYPE_GP | 4}, \
2684 {"$a1", RTYPE_GP | 5}, \
2685 {"$a2", RTYPE_GP | 6}, \
2686 {"$a3", RTYPE_GP | 7}, \
2687 {"$s0", RTYPE_GP | 16}, \
2688 {"$s1", RTYPE_GP | 17}, \
2689 {"$s2", RTYPE_GP | 18}, \
2690 {"$s3", RTYPE_GP | 19}, \
2691 {"$s4", RTYPE_GP | 20}, \
2692 {"$s5", RTYPE_GP | 21}, \
2693 {"$s6", RTYPE_GP | 22}, \
2694 {"$s7", RTYPE_GP | 23}, \
2695 {"$t8", RTYPE_GP | 24}, \
2696 {"$t9", RTYPE_GP | 25}, \
2697 {"$k0", RTYPE_GP | 26}, \
2698 {"$kt0", RTYPE_GP | 26}, \
2699 {"$k1", RTYPE_GP | 27}, \
2700 {"$kt1", RTYPE_GP | 27}, \
2701 {"$gp", RTYPE_GP | 28}, \
2702 {"$sp", RTYPE_GP | 29}, \
2703 {"$s8", RTYPE_GP | 30}, \
2704 {"$fp", RTYPE_GP | 30}, \
2705 {"$ra", RTYPE_GP | 31}
2706
2707 #define MIPS16_SPECIAL_REGISTER_NAMES \
2708 {"$pc", RTYPE_PC | 0}
2709
2710 #define MDMX_VECTOR_REGISTER_NAMES \
2711 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2712 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2713 {"$v2", RTYPE_VEC | 2}, \
2714 {"$v3", RTYPE_VEC | 3}, \
2715 {"$v4", RTYPE_VEC | 4}, \
2716 {"$v5", RTYPE_VEC | 5}, \
2717 {"$v6", RTYPE_VEC | 6}, \
2718 {"$v7", RTYPE_VEC | 7}, \
2719 {"$v8", RTYPE_VEC | 8}, \
2720 {"$v9", RTYPE_VEC | 9}, \
2721 {"$v10", RTYPE_VEC | 10}, \
2722 {"$v11", RTYPE_VEC | 11}, \
2723 {"$v12", RTYPE_VEC | 12}, \
2724 {"$v13", RTYPE_VEC | 13}, \
2725 {"$v14", RTYPE_VEC | 14}, \
2726 {"$v15", RTYPE_VEC | 15}, \
2727 {"$v16", RTYPE_VEC | 16}, \
2728 {"$v17", RTYPE_VEC | 17}, \
2729 {"$v18", RTYPE_VEC | 18}, \
2730 {"$v19", RTYPE_VEC | 19}, \
2731 {"$v20", RTYPE_VEC | 20}, \
2732 {"$v21", RTYPE_VEC | 21}, \
2733 {"$v22", RTYPE_VEC | 22}, \
2734 {"$v23", RTYPE_VEC | 23}, \
2735 {"$v24", RTYPE_VEC | 24}, \
2736 {"$v25", RTYPE_VEC | 25}, \
2737 {"$v26", RTYPE_VEC | 26}, \
2738 {"$v27", RTYPE_VEC | 27}, \
2739 {"$v28", RTYPE_VEC | 28}, \
2740 {"$v29", RTYPE_VEC | 29}, \
2741 {"$v30", RTYPE_VEC | 30}, \
2742 {"$v31", RTYPE_VEC | 31}
2743
2744 #define R5900_I_NAMES \
2745 {"$I", RTYPE_R5900_I | 0}
2746
2747 #define R5900_Q_NAMES \
2748 {"$Q", RTYPE_R5900_Q | 0}
2749
2750 #define R5900_R_NAMES \
2751 {"$R", RTYPE_R5900_R | 0}
2752
2753 #define R5900_ACC_NAMES \
2754 {"$ACC", RTYPE_R5900_ACC | 0 }
2755
2756 #define MIPS_DSP_ACCUMULATOR_NAMES \
2757 {"$ac0", RTYPE_ACC | 0}, \
2758 {"$ac1", RTYPE_ACC | 1}, \
2759 {"$ac2", RTYPE_ACC | 2}, \
2760 {"$ac3", RTYPE_ACC | 3}
2761
2762 static const struct regname reg_names[] = {
2763 GENERIC_REGISTER_NUMBERS,
2764 FPU_REGISTER_NAMES,
2765 FPU_CONDITION_CODE_NAMES,
2766 COPROC_CONDITION_CODE_NAMES,
2767
2768 /* The $txx registers depends on the abi,
2769 these will be added later into the symbol table from
2770 one of the tables below once mips_abi is set after
2771 parsing of arguments from the command line. */
2772 SYMBOLIC_REGISTER_NAMES,
2773
2774 MIPS16_SPECIAL_REGISTER_NAMES,
2775 MDMX_VECTOR_REGISTER_NAMES,
2776 R5900_I_NAMES,
2777 R5900_Q_NAMES,
2778 R5900_R_NAMES,
2779 R5900_ACC_NAMES,
2780 MIPS_DSP_ACCUMULATOR_NAMES,
2781 {0, 0}
2782 };
2783
2784 static const struct regname reg_names_o32[] = {
2785 O32_SYMBOLIC_REGISTER_NAMES,
2786 {0, 0}
2787 };
2788
2789 static const struct regname reg_names_n32n64[] = {
2790 N32N64_SYMBOLIC_REGISTER_NAMES,
2791 {0, 0}
2792 };
2793
2794 /* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2795 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2796 of these register symbols, return the associated vector register,
2797 otherwise return SYMVAL itself. */
2798
2799 static unsigned int
2800 mips_prefer_vec_regno (unsigned int symval)
2801 {
2802 if ((symval & -2) == (RTYPE_GP | 2))
2803 return RTYPE_VEC | (symval & 1);
2804 return symval;
2805 }
2806
2807 /* Return true if string [S, E) is a valid register name, storing its
2808 symbol value in *SYMVAL_PTR if so. */
2809
2810 static bfd_boolean
2811 mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
2812 {
2813 char save_c;
2814 symbolS *symbol;
2815
2816 /* Terminate name. */
2817 save_c = *e;
2818 *e = '\0';
2819
2820 /* Look up the name. */
2821 symbol = symbol_find (s);
2822 *e = save_c;
2823
2824 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2825 return FALSE;
2826
2827 *symval_ptr = S_GET_VALUE (symbol);
2828 return TRUE;
2829 }
2830
2831 /* Return true if the string at *SPTR is a valid register name. Allow it
2832 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2833 is nonnull.
2834
2835 When returning true, move *SPTR past the register, store the
2836 register's symbol value in *SYMVAL_PTR and the channel mask in
2837 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2838 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2839 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2840
2841 static bfd_boolean
2842 mips_parse_register (char **sptr, unsigned int *symval_ptr,
2843 unsigned int *channels_ptr)
2844 {
2845 char *s, *e, *m;
2846 const char *q;
2847 unsigned int channels, symval, bit;
2848
2849 /* Find end of name. */
2850 s = e = *sptr;
2851 if (is_name_beginner (*e))
2852 ++e;
2853 while (is_part_of_name (*e))
2854 ++e;
2855
2856 channels = 0;
2857 if (!mips_parse_register_1 (s, e, &symval))
2858 {
2859 if (!channels_ptr)
2860 return FALSE;
2861
2862 /* Eat characters from the end of the string that are valid
2863 channel suffixes. The preceding register must be $ACC or
2864 end with a digit, so there is no ambiguity. */
2865 bit = 1;
2866 m = e;
2867 for (q = "wzyx"; *q; q++, bit <<= 1)
2868 if (m > s && m[-1] == *q)
2869 {
2870 --m;
2871 channels |= bit;
2872 }
2873
2874 if (channels == 0
2875 || !mips_parse_register_1 (s, m, &symval)
2876 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2877 return FALSE;
2878 }
2879
2880 *sptr = e;
2881 *symval_ptr = symval;
2882 if (channels_ptr)
2883 *channels_ptr = channels;
2884 return TRUE;
2885 }
2886
2887 /* Check if SPTR points at a valid register specifier according to TYPES.
2888 If so, then return 1, advance S to consume the specifier and store
2889 the register's number in REGNOP, otherwise return 0. */
2890
2891 static int
2892 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2893 {
2894 unsigned int regno;
2895
2896 if (mips_parse_register (s, &regno, NULL))
2897 {
2898 if (types & RTYPE_VEC)
2899 regno = mips_prefer_vec_regno (regno);
2900 if (regno & types)
2901 regno &= RNUM_MASK;
2902 else
2903 regno = ~0;
2904 }
2905 else
2906 {
2907 if (types & RWARN)
2908 as_warn (_("unrecognized register name `%s'"), *s);
2909 regno = ~0;
2910 }
2911 if (regnop)
2912 *regnop = regno;
2913 return regno <= RNUM_MASK;
2914 }
2915
2916 /* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2917 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2918
2919 static char *
2920 mips_parse_vu0_channels (char *s, unsigned int *channels)
2921 {
2922 unsigned int i;
2923
2924 *channels = 0;
2925 for (i = 0; i < 4; i++)
2926 if (*s == "xyzw"[i])
2927 {
2928 *channels |= 1 << (3 - i);
2929 ++s;
2930 }
2931 return s;
2932 }
2933
2934 /* Token types for parsed operand lists. */
2935 enum mips_operand_token_type {
2936 /* A plain register, e.g. $f2. */
2937 OT_REG,
2938
2939 /* A 4-bit XYZW channel mask. */
2940 OT_CHANNELS,
2941
2942 /* A constant vector index, e.g. [1]. */
2943 OT_INTEGER_INDEX,
2944
2945 /* A register vector index, e.g. [$2]. */
2946 OT_REG_INDEX,
2947
2948 /* A continuous range of registers, e.g. $s0-$s4. */
2949 OT_REG_RANGE,
2950
2951 /* A (possibly relocated) expression. */
2952 OT_INTEGER,
2953
2954 /* A floating-point value. */
2955 OT_FLOAT,
2956
2957 /* A single character. This can be '(', ')' or ',', but '(' only appears
2958 before OT_REGs. */
2959 OT_CHAR,
2960
2961 /* A doubled character, either "--" or "++". */
2962 OT_DOUBLE_CHAR,
2963
2964 /* The end of the operand list. */
2965 OT_END
2966 };
2967
2968 /* A parsed operand token. */
2969 struct mips_operand_token
2970 {
2971 /* The type of token. */
2972 enum mips_operand_token_type type;
2973 union
2974 {
2975 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
2976 unsigned int regno;
2977
2978 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
2979 unsigned int channels;
2980
2981 /* The integer value of an OT_INTEGER_INDEX. */
2982 addressT index;
2983
2984 /* The two register symbol values involved in an OT_REG_RANGE. */
2985 struct {
2986 unsigned int regno1;
2987 unsigned int regno2;
2988 } reg_range;
2989
2990 /* The value of an OT_INTEGER. The value is represented as an
2991 expression and the relocation operators that were applied to
2992 that expression. The reloc entries are BFD_RELOC_UNUSED if no
2993 relocation operators were used. */
2994 struct {
2995 expressionS value;
2996 bfd_reloc_code_real_type relocs[3];
2997 } integer;
2998
2999 /* The binary data for an OT_FLOAT constant, and the number of bytes
3000 in the constant. */
3001 struct {
3002 unsigned char data[8];
3003 int length;
3004 } flt;
3005
3006 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
3007 char ch;
3008 } u;
3009 };
3010
3011 /* An obstack used to construct lists of mips_operand_tokens. */
3012 static struct obstack mips_operand_tokens;
3013
3014 /* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3015
3016 static void
3017 mips_add_token (struct mips_operand_token *token,
3018 enum mips_operand_token_type type)
3019 {
3020 token->type = type;
3021 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3022 }
3023
3024 /* Check whether S is '(' followed by a register name. Add OT_CHAR
3025 and OT_REG tokens for them if so, and return a pointer to the first
3026 unconsumed character. Return null otherwise. */
3027
3028 static char *
3029 mips_parse_base_start (char *s)
3030 {
3031 struct mips_operand_token token;
3032 unsigned int regno, channels;
3033 bfd_boolean decrement_p;
3034
3035 if (*s != '(')
3036 return 0;
3037
3038 ++s;
3039 SKIP_SPACE_TABS (s);
3040
3041 /* Only match "--" as part of a base expression. In other contexts "--X"
3042 is a double negative. */
3043 decrement_p = (s[0] == '-' && s[1] == '-');
3044 if (decrement_p)
3045 {
3046 s += 2;
3047 SKIP_SPACE_TABS (s);
3048 }
3049
3050 /* Allow a channel specifier because that leads to better error messages
3051 than treating something like "$vf0x++" as an expression. */
3052 if (!mips_parse_register (&s, &regno, &channels))
3053 return 0;
3054
3055 token.u.ch = '(';
3056 mips_add_token (&token, OT_CHAR);
3057
3058 if (decrement_p)
3059 {
3060 token.u.ch = '-';
3061 mips_add_token (&token, OT_DOUBLE_CHAR);
3062 }
3063
3064 token.u.regno = regno;
3065 mips_add_token (&token, OT_REG);
3066
3067 if (channels)
3068 {
3069 token.u.channels = channels;
3070 mips_add_token (&token, OT_CHANNELS);
3071 }
3072
3073 /* For consistency, only match "++" as part of base expressions too. */
3074 SKIP_SPACE_TABS (s);
3075 if (s[0] == '+' && s[1] == '+')
3076 {
3077 s += 2;
3078 token.u.ch = '+';
3079 mips_add_token (&token, OT_DOUBLE_CHAR);
3080 }
3081
3082 return s;
3083 }
3084
3085 /* Parse one or more tokens from S. Return a pointer to the first
3086 unconsumed character on success. Return null if an error was found
3087 and store the error text in insn_error. FLOAT_FORMAT is as for
3088 mips_parse_arguments. */
3089
3090 static char *
3091 mips_parse_argument_token (char *s, char float_format)
3092 {
3093 char *end, *save_in;
3094 const char *err;
3095 unsigned int regno1, regno2, channels;
3096 struct mips_operand_token token;
3097
3098 /* First look for "($reg", since we want to treat that as an
3099 OT_CHAR and OT_REG rather than an expression. */
3100 end = mips_parse_base_start (s);
3101 if (end)
3102 return end;
3103
3104 /* Handle other characters that end up as OT_CHARs. */
3105 if (*s == ')' || *s == ',')
3106 {
3107 token.u.ch = *s;
3108 mips_add_token (&token, OT_CHAR);
3109 ++s;
3110 return s;
3111 }
3112
3113 /* Handle tokens that start with a register. */
3114 if (mips_parse_register (&s, &regno1, &channels))
3115 {
3116 if (channels)
3117 {
3118 /* A register and a VU0 channel suffix. */
3119 token.u.regno = regno1;
3120 mips_add_token (&token, OT_REG);
3121
3122 token.u.channels = channels;
3123 mips_add_token (&token, OT_CHANNELS);
3124 return s;
3125 }
3126
3127 SKIP_SPACE_TABS (s);
3128 if (*s == '-')
3129 {
3130 /* A register range. */
3131 ++s;
3132 SKIP_SPACE_TABS (s);
3133 if (!mips_parse_register (&s, &regno2, NULL))
3134 {
3135 set_insn_error (0, _("invalid register range"));
3136 return 0;
3137 }
3138
3139 token.u.reg_range.regno1 = regno1;
3140 token.u.reg_range.regno2 = regno2;
3141 mips_add_token (&token, OT_REG_RANGE);
3142 return s;
3143 }
3144
3145 /* Add the register itself. */
3146 token.u.regno = regno1;
3147 mips_add_token (&token, OT_REG);
3148
3149 /* Check for a vector index. */
3150 if (*s == '[')
3151 {
3152 ++s;
3153 SKIP_SPACE_TABS (s);
3154 if (mips_parse_register (&s, &token.u.regno, NULL))
3155 mips_add_token (&token, OT_REG_INDEX);
3156 else
3157 {
3158 expressionS element;
3159
3160 my_getExpression (&element, s);
3161 if (element.X_op != O_constant)
3162 {
3163 set_insn_error (0, _("vector element must be constant"));
3164 return 0;
3165 }
3166 s = expr_end;
3167 token.u.index = element.X_add_number;
3168 mips_add_token (&token, OT_INTEGER_INDEX);
3169 }
3170 SKIP_SPACE_TABS (s);
3171 if (*s != ']')
3172 {
3173 set_insn_error (0, _("missing `]'"));
3174 return 0;
3175 }
3176 ++s;
3177 }
3178 return s;
3179 }
3180
3181 if (float_format)
3182 {
3183 /* First try to treat expressions as floats. */
3184 save_in = input_line_pointer;
3185 input_line_pointer = s;
3186 err = md_atof (float_format, (char *) token.u.flt.data,
3187 &token.u.flt.length);
3188 end = input_line_pointer;
3189 input_line_pointer = save_in;
3190 if (err && *err)
3191 {
3192 set_insn_error (0, err);
3193 return 0;
3194 }
3195 if (s != end)
3196 {
3197 mips_add_token (&token, OT_FLOAT);
3198 return end;
3199 }
3200 }
3201
3202 /* Treat everything else as an integer expression. */
3203 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3204 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3205 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3206 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3207 s = expr_end;
3208 mips_add_token (&token, OT_INTEGER);
3209 return s;
3210 }
3211
3212 /* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3213 if expressions should be treated as 32-bit floating-point constants,
3214 'd' if they should be treated as 64-bit floating-point constants,
3215 or 0 if they should be treated as integer expressions (the usual case).
3216
3217 Return a list of tokens on success, otherwise return 0. The caller
3218 must obstack_free the list after use. */
3219
3220 static struct mips_operand_token *
3221 mips_parse_arguments (char *s, char float_format)
3222 {
3223 struct mips_operand_token token;
3224
3225 SKIP_SPACE_TABS (s);
3226 while (*s)
3227 {
3228 s = mips_parse_argument_token (s, float_format);
3229 if (!s)
3230 {
3231 obstack_free (&mips_operand_tokens,
3232 obstack_finish (&mips_operand_tokens));
3233 return 0;
3234 }
3235 SKIP_SPACE_TABS (s);
3236 }
3237 mips_add_token (&token, OT_END);
3238 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
3239 }
3240
3241 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3242 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
3243
3244 static bfd_boolean
3245 is_opcode_valid (const struct mips_opcode *mo)
3246 {
3247 int isa = mips_opts.isa;
3248 int ase = mips_opts.ase;
3249 int fp_s, fp_d;
3250 unsigned int i;
3251
3252 if (ISA_HAS_64BIT_REGS (isa))
3253 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3254 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3255 ase |= mips_ases[i].flags64;
3256
3257 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
3258 return FALSE;
3259
3260 /* Check whether the instruction or macro requires single-precision or
3261 double-precision floating-point support. Note that this information is
3262 stored differently in the opcode table for insns and macros. */
3263 if (mo->pinfo == INSN_MACRO)
3264 {
3265 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3266 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3267 }
3268 else
3269 {
3270 fp_s = mo->pinfo & FP_S;
3271 fp_d = mo->pinfo & FP_D;
3272 }
3273
3274 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3275 return FALSE;
3276
3277 if (fp_s && mips_opts.soft_float)
3278 return FALSE;
3279
3280 return TRUE;
3281 }
3282
3283 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
3284 selected ISA and architecture. */
3285
3286 static bfd_boolean
3287 is_opcode_valid_16 (const struct mips_opcode *mo)
3288 {
3289 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
3290 }
3291
3292 /* Return TRUE if the size of the microMIPS opcode MO matches one
3293 explicitly requested. Always TRUE in the standard MIPS mode.
3294 Use is_size_valid_16 for MIPS16 opcodes. */
3295
3296 static bfd_boolean
3297 is_size_valid (const struct mips_opcode *mo)
3298 {
3299 if (!mips_opts.micromips)
3300 return TRUE;
3301
3302 if (mips_opts.insn32)
3303 {
3304 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3305 return FALSE;
3306 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3307 return FALSE;
3308 }
3309 if (!forced_insn_length)
3310 return TRUE;
3311 if (mo->pinfo == INSN_MACRO)
3312 return FALSE;
3313 return forced_insn_length == micromips_insn_length (mo);
3314 }
3315
3316 /* Return TRUE if the size of the MIPS16 opcode MO matches one
3317 explicitly requested. */
3318
3319 static bfd_boolean
3320 is_size_valid_16 (const struct mips_opcode *mo)
3321 {
3322 if (!forced_insn_length)
3323 return TRUE;
3324 if (mo->pinfo == INSN_MACRO)
3325 return FALSE;
3326 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3327 return FALSE;
3328 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3329 return FALSE;
3330 return TRUE;
3331 }
3332
3333 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
3334 of the preceding instruction. Always TRUE in the standard MIPS mode.
3335
3336 We don't accept macros in 16-bit delay slots to avoid a case where
3337 a macro expansion fails because it relies on a preceding 32-bit real
3338 instruction to have matched and does not handle the operands correctly.
3339 The only macros that may expand to 16-bit instructions are JAL that
3340 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3341 and BGT (that likewise cannot be placed in a delay slot) that decay to
3342 a NOP. In all these cases the macros precede any corresponding real
3343 instruction definitions in the opcode table, so they will match in the
3344 second pass where the size of the delay slot is ignored and therefore
3345 produce correct code. */
3346
3347 static bfd_boolean
3348 is_delay_slot_valid (const struct mips_opcode *mo)
3349 {
3350 if (!mips_opts.micromips)
3351 return TRUE;
3352
3353 if (mo->pinfo == INSN_MACRO)
3354 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
3355 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3356 && micromips_insn_length (mo) != 4)
3357 return FALSE;
3358 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3359 && micromips_insn_length (mo) != 2)
3360 return FALSE;
3361
3362 return TRUE;
3363 }
3364
3365 /* For consistency checking, verify that all bits of OPCODE are specified
3366 either by the match/mask part of the instruction definition, or by the
3367 operand list. Also build up a list of operands in OPERANDS.
3368
3369 INSN_BITS says which bits of the instruction are significant.
3370 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3371 provides the mips_operand description of each operand. DECODE_OPERAND
3372 is null for MIPS16 instructions. */
3373
3374 static int
3375 validate_mips_insn (const struct mips_opcode *opcode,
3376 unsigned long insn_bits,
3377 const struct mips_operand *(*decode_operand) (const char *),
3378 struct mips_operand_array *operands)
3379 {
3380 const char *s;
3381 unsigned long used_bits, doubled, undefined, opno, mask;
3382 const struct mips_operand *operand;
3383
3384 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3385 if ((mask & opcode->match) != opcode->match)
3386 {
3387 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3388 opcode->name, opcode->args);
3389 return 0;
3390 }
3391 used_bits = 0;
3392 opno = 0;
3393 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3394 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
3395 for (s = opcode->args; *s; ++s)
3396 switch (*s)
3397 {
3398 case ',':
3399 case '(':
3400 case ')':
3401 break;
3402
3403 case '#':
3404 s++;
3405 break;
3406
3407 default:
3408 if (!decode_operand)
3409 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
3410 else
3411 operand = decode_operand (s);
3412 if (!operand && opcode->pinfo != INSN_MACRO)
3413 {
3414 as_bad (_("internal: unknown operand type: %s %s"),
3415 opcode->name, opcode->args);
3416 return 0;
3417 }
3418 gas_assert (opno < MAX_OPERANDS);
3419 operands->operand[opno] = operand;
3420 if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
3421 {
3422 used_bits = mips_insert_operand (operand, used_bits, -1);
3423 if (operand->type == OP_MDMX_IMM_REG)
3424 /* Bit 5 is the format selector (OB vs QH). The opcode table
3425 has separate entries for each format. */
3426 used_bits &= ~(1 << (operand->lsb + 5));
3427 if (operand->type == OP_ENTRY_EXIT_LIST)
3428 used_bits &= ~(mask & 0x700);
3429 }
3430 /* Skip prefix characters. */
3431 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
3432 ++s;
3433 opno += 1;
3434 break;
3435 }
3436 doubled = used_bits & mask & insn_bits;
3437 if (doubled)
3438 {
3439 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3440 " %s %s"), doubled, opcode->name, opcode->args);
3441 return 0;
3442 }
3443 used_bits |= mask;
3444 undefined = ~used_bits & insn_bits;
3445 if (opcode->pinfo != INSN_MACRO && undefined)
3446 {
3447 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3448 undefined, opcode->name, opcode->args);
3449 return 0;
3450 }
3451 used_bits &= ~insn_bits;
3452 if (used_bits)
3453 {
3454 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3455 used_bits, opcode->name, opcode->args);
3456 return 0;
3457 }
3458 return 1;
3459 }
3460
3461 /* The MIPS16 version of validate_mips_insn. */
3462
3463 static int
3464 validate_mips16_insn (const struct mips_opcode *opcode,
3465 struct mips_operand_array *operands)
3466 {
3467 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
3468
3469 return validate_mips_insn (opcode, insn_bits, 0, operands);
3470 }
3471
3472 /* The microMIPS version of validate_mips_insn. */
3473
3474 static int
3475 validate_micromips_insn (const struct mips_opcode *opc,
3476 struct mips_operand_array *operands)
3477 {
3478 unsigned long insn_bits;
3479 unsigned long major;
3480 unsigned int length;
3481
3482 if (opc->pinfo == INSN_MACRO)
3483 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3484 operands);
3485
3486 length = micromips_insn_length (opc);
3487 if (length != 2 && length != 4)
3488 {
3489 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
3490 "%s %s"), length, opc->name, opc->args);
3491 return 0;
3492 }
3493 major = opc->match >> (10 + 8 * (length - 2));
3494 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3495 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3496 {
3497 as_bad (_("internal error: bad microMIPS opcode "
3498 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3499 return 0;
3500 }
3501
3502 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3503 insn_bits = 1 << 4 * length;
3504 insn_bits <<= 4 * length;
3505 insn_bits -= 1;
3506 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3507 operands);
3508 }
3509
3510 /* This function is called once, at assembler startup time. It should set up
3511 all the tables, etc. that the MD part of the assembler will need. */
3512
3513 void
3514 md_begin (void)
3515 {
3516 const char *retval = NULL;
3517 int i = 0;
3518 int broken = 0;
3519
3520 if (mips_pic != NO_PIC)
3521 {
3522 if (g_switch_seen && g_switch_value != 0)
3523 as_bad (_("-G may not be used in position-independent code"));
3524 g_switch_value = 0;
3525 }
3526 else if (mips_abicalls)
3527 {
3528 if (g_switch_seen && g_switch_value != 0)
3529 as_bad (_("-G may not be used with abicalls"));
3530 g_switch_value = 0;
3531 }
3532
3533 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
3534 as_warn (_("could not set architecture and machine"));
3535
3536 op_hash = hash_new ();
3537
3538 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
3539 for (i = 0; i < NUMOPCODES;)
3540 {
3541 const char *name = mips_opcodes[i].name;
3542
3543 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
3544 if (retval != NULL)
3545 {
3546 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3547 mips_opcodes[i].name, retval);
3548 /* Probably a memory allocation problem? Give up now. */
3549 as_fatal (_("broken assembler, no assembly attempted"));
3550 }
3551 do
3552 {
3553 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3554 decode_mips_operand, &mips_operands[i]))
3555 broken = 1;
3556 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3557 {
3558 create_insn (&nop_insn, mips_opcodes + i);
3559 if (mips_fix_loongson2f_nop)
3560 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3561 nop_insn.fixed_p = 1;
3562 }
3563 ++i;
3564 }
3565 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3566 }
3567
3568 mips16_op_hash = hash_new ();
3569 mips16_operands = XCNEWVEC (struct mips_operand_array,
3570 bfd_mips16_num_opcodes);
3571
3572 i = 0;
3573 while (i < bfd_mips16_num_opcodes)
3574 {
3575 const char *name = mips16_opcodes[i].name;
3576
3577 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
3578 if (retval != NULL)
3579 as_fatal (_("internal: can't hash `%s': %s"),
3580 mips16_opcodes[i].name, retval);
3581 do
3582 {
3583 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3584 broken = 1;
3585 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3586 {
3587 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3588 mips16_nop_insn.fixed_p = 1;
3589 }
3590 ++i;
3591 }
3592 while (i < bfd_mips16_num_opcodes
3593 && strcmp (mips16_opcodes[i].name, name) == 0);
3594 }
3595
3596 micromips_op_hash = hash_new ();
3597 micromips_operands = XCNEWVEC (struct mips_operand_array,
3598 bfd_micromips_num_opcodes);
3599
3600 i = 0;
3601 while (i < bfd_micromips_num_opcodes)
3602 {
3603 const char *name = micromips_opcodes[i].name;
3604
3605 retval = hash_insert (micromips_op_hash, name,
3606 (void *) &micromips_opcodes[i]);
3607 if (retval != NULL)
3608 as_fatal (_("internal: can't hash `%s': %s"),
3609 micromips_opcodes[i].name, retval);
3610 do
3611 {
3612 struct mips_cl_insn *micromips_nop_insn;
3613
3614 if (!validate_micromips_insn (&micromips_opcodes[i],
3615 &micromips_operands[i]))
3616 broken = 1;
3617
3618 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3619 {
3620 if (micromips_insn_length (micromips_opcodes + i) == 2)
3621 micromips_nop_insn = &micromips_nop16_insn;
3622 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3623 micromips_nop_insn = &micromips_nop32_insn;
3624 else
3625 continue;
3626
3627 if (micromips_nop_insn->insn_mo == NULL
3628 && strcmp (name, "nop") == 0)
3629 {
3630 create_insn (micromips_nop_insn, micromips_opcodes + i);
3631 micromips_nop_insn->fixed_p = 1;
3632 }
3633 }
3634 }
3635 while (++i < bfd_micromips_num_opcodes
3636 && strcmp (micromips_opcodes[i].name, name) == 0);
3637 }
3638
3639 if (broken)
3640 as_fatal (_("broken assembler, no assembly attempted"));
3641
3642 /* We add all the general register names to the symbol table. This
3643 helps us detect invalid uses of them. */
3644 for (i = 0; reg_names[i].name; i++)
3645 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
3646 reg_names[i].num, /* & RNUM_MASK, */
3647 &zero_address_frag));
3648 if (HAVE_NEWABI)
3649 for (i = 0; reg_names_n32n64[i].name; i++)
3650 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
3651 reg_names_n32n64[i].num, /* & RNUM_MASK, */
3652 &zero_address_frag));
3653 else
3654 for (i = 0; reg_names_o32[i].name; i++)
3655 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
3656 reg_names_o32[i].num, /* & RNUM_MASK, */
3657 &zero_address_frag));
3658
3659 for (i = 0; i < 32; i++)
3660 {
3661 char regname[6];
3662
3663 /* R5900 VU0 floating-point register. */
3664 sprintf (regname, "$vf%d", i);
3665 symbol_table_insert (symbol_new (regname, reg_section,
3666 RTYPE_VF | i, &zero_address_frag));
3667
3668 /* R5900 VU0 integer register. */
3669 sprintf (regname, "$vi%d", i);
3670 symbol_table_insert (symbol_new (regname, reg_section,
3671 RTYPE_VI | i, &zero_address_frag));
3672
3673 /* MSA register. */
3674 sprintf (regname, "$w%d", i);
3675 symbol_table_insert (symbol_new (regname, reg_section,
3676 RTYPE_MSA | i, &zero_address_frag));
3677 }
3678
3679 obstack_init (&mips_operand_tokens);
3680
3681 mips_no_prev_insn ();
3682
3683 mips_gprmask = 0;
3684 mips_cprmask[0] = 0;
3685 mips_cprmask[1] = 0;
3686 mips_cprmask[2] = 0;
3687 mips_cprmask[3] = 0;
3688
3689 /* set the default alignment for the text section (2**2) */
3690 record_alignment (text_section, 2);
3691
3692 bfd_set_gp_size (stdoutput, g_switch_value);
3693
3694 /* On a native system other than VxWorks, sections must be aligned
3695 to 16 byte boundaries. When configured for an embedded ELF
3696 target, we don't bother. */
3697 if (strncmp (TARGET_OS, "elf", 3) != 0
3698 && strncmp (TARGET_OS, "vxworks", 7) != 0)
3699 {
3700 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3701 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3702 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3703 }
3704
3705 /* Create a .reginfo section for register masks and a .mdebug
3706 section for debugging information. */
3707 {
3708 segT seg;
3709 subsegT subseg;
3710 flagword flags;
3711 segT sec;
3712
3713 seg = now_seg;
3714 subseg = now_subseg;
3715
3716 /* The ABI says this section should be loaded so that the
3717 running program can access it. However, we don't load it
3718 if we are configured for an embedded target */
3719 flags = SEC_READONLY | SEC_DATA;
3720 if (strncmp (TARGET_OS, "elf", 3) != 0)
3721 flags |= SEC_ALLOC | SEC_LOAD;
3722
3723 if (mips_abi != N64_ABI)
3724 {
3725 sec = subseg_new (".reginfo", (subsegT) 0);
3726
3727 bfd_set_section_flags (stdoutput, sec, flags);
3728 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
3729
3730 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3731 }
3732 else
3733 {
3734 /* The 64-bit ABI uses a .MIPS.options section rather than
3735 .reginfo section. */
3736 sec = subseg_new (".MIPS.options", (subsegT) 0);
3737 bfd_set_section_flags (stdoutput, sec, flags);
3738 bfd_set_section_alignment (stdoutput, sec, 3);
3739
3740 /* Set up the option header. */
3741 {
3742 Elf_Internal_Options opthdr;
3743 char *f;
3744
3745 opthdr.kind = ODK_REGINFO;
3746 opthdr.size = (sizeof (Elf_External_Options)
3747 + sizeof (Elf64_External_RegInfo));
3748 opthdr.section = 0;
3749 opthdr.info = 0;
3750 f = frag_more (sizeof (Elf_External_Options));
3751 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3752 (Elf_External_Options *) f);
3753
3754 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3755 }
3756 }
3757
3758 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3759 bfd_set_section_flags (stdoutput, sec,
3760 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3761 bfd_set_section_alignment (stdoutput, sec, 3);
3762 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3763
3764 if (ECOFF_DEBUGGING)
3765 {
3766 sec = subseg_new (".mdebug", (subsegT) 0);
3767 (void) bfd_set_section_flags (stdoutput, sec,
3768 SEC_HAS_CONTENTS | SEC_READONLY);
3769 (void) bfd_set_section_alignment (stdoutput, sec, 2);
3770 }
3771 else if (mips_flag_pdr)
3772 {
3773 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3774 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3775 SEC_READONLY | SEC_RELOC
3776 | SEC_DEBUGGING);
3777 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3778 }
3779
3780 subseg_set (seg, subseg);
3781 }
3782
3783 if (mips_fix_vr4120)
3784 init_vr4120_conflicts ();
3785 }
3786
3787 static inline void
3788 fpabi_incompatible_with (int fpabi, const char *what)
3789 {
3790 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3791 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3792 }
3793
3794 static inline void
3795 fpabi_requires (int fpabi, const char *what)
3796 {
3797 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3798 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3799 }
3800
3801 /* Check -mabi and register sizes against the specified FP ABI. */
3802 static void
3803 check_fpabi (int fpabi)
3804 {
3805 switch (fpabi)
3806 {
3807 case Val_GNU_MIPS_ABI_FP_DOUBLE:
3808 if (file_mips_opts.soft_float)
3809 fpabi_incompatible_with (fpabi, "softfloat");
3810 else if (file_mips_opts.single_float)
3811 fpabi_incompatible_with (fpabi, "singlefloat");
3812 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3813 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3814 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3815 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
3816 break;
3817
3818 case Val_GNU_MIPS_ABI_FP_XX:
3819 if (mips_abi != O32_ABI)
3820 fpabi_requires (fpabi, "-mabi=32");
3821 else if (file_mips_opts.soft_float)
3822 fpabi_incompatible_with (fpabi, "softfloat");
3823 else if (file_mips_opts.single_float)
3824 fpabi_incompatible_with (fpabi, "singlefloat");
3825 else if (file_mips_opts.fp != 0)
3826 fpabi_requires (fpabi, "fp=xx");
3827 break;
3828
3829 case Val_GNU_MIPS_ABI_FP_64A:
3830 case Val_GNU_MIPS_ABI_FP_64:
3831 if (mips_abi != O32_ABI)
3832 fpabi_requires (fpabi, "-mabi=32");
3833 else if (file_mips_opts.soft_float)
3834 fpabi_incompatible_with (fpabi, "softfloat");
3835 else if (file_mips_opts.single_float)
3836 fpabi_incompatible_with (fpabi, "singlefloat");
3837 else if (file_mips_opts.fp != 64)
3838 fpabi_requires (fpabi, "fp=64");
3839 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3840 fpabi_incompatible_with (fpabi, "nooddspreg");
3841 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3842 fpabi_requires (fpabi, "nooddspreg");
3843 break;
3844
3845 case Val_GNU_MIPS_ABI_FP_SINGLE:
3846 if (file_mips_opts.soft_float)
3847 fpabi_incompatible_with (fpabi, "softfloat");
3848 else if (!file_mips_opts.single_float)
3849 fpabi_requires (fpabi, "singlefloat");
3850 break;
3851
3852 case Val_GNU_MIPS_ABI_FP_SOFT:
3853 if (!file_mips_opts.soft_float)
3854 fpabi_requires (fpabi, "softfloat");
3855 break;
3856
3857 case Val_GNU_MIPS_ABI_FP_OLD_64:
3858 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3859 Tag_GNU_MIPS_ABI_FP, fpabi);
3860 break;
3861
3862 case Val_GNU_MIPS_ABI_FP_NAN2008:
3863 /* Silently ignore compatibility value. */
3864 break;
3865
3866 default:
3867 as_warn (_(".gnu_attribute %d,%d is not a recognized"
3868 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3869 break;
3870 }
3871 }
3872
3873 /* Perform consistency checks on the current options. */
3874
3875 static void
3876 mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3877 {
3878 /* Check the size of integer registers agrees with the ABI and ISA. */
3879 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3880 as_bad (_("`gp=64' used with a 32-bit processor"));
3881 else if (abi_checks
3882 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3883 as_bad (_("`gp=32' used with a 64-bit ABI"));
3884 else if (abi_checks
3885 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3886 as_bad (_("`gp=64' used with a 32-bit ABI"));
3887
3888 /* Check the size of the float registers agrees with the ABI and ISA. */
3889 switch (opts->fp)
3890 {
3891 case 0:
3892 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
3893 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
3894 else if (opts->single_float == 1)
3895 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
3896 break;
3897 case 64:
3898 if (!ISA_HAS_64BIT_FPRS (opts->isa))
3899 as_bad (_("`fp=64' used with a 32-bit fpu"));
3900 else if (abi_checks
3901 && ABI_NEEDS_32BIT_REGS (mips_abi)
3902 && !ISA_HAS_MXHC1 (opts->isa))
3903 as_warn (_("`fp=64' used with a 32-bit ABI"));
3904 break;
3905 case 32:
3906 if (abi_checks
3907 && ABI_NEEDS_64BIT_REGS (mips_abi))
3908 as_warn (_("`fp=32' used with a 64-bit ABI"));
3909 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
3910 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
3911 break;
3912 default:
3913 as_bad (_("Unknown size of floating point registers"));
3914 break;
3915 }
3916
3917 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
3918 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
3919
3920 if (opts->micromips == 1 && opts->mips16 == 1)
3921 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
3922 else if (ISA_IS_R6 (opts->isa)
3923 && (opts->micromips == 1
3924 || opts->mips16 == 1))
3925 as_fatal (_("`%s' cannot be used with `%s'"),
3926 opts->micromips ? "micromips" : "mips16",
3927 mips_cpu_info_from_isa (opts->isa)->name);
3928
3929 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
3930 as_fatal (_("branch relaxation is not supported in `%s'"),
3931 mips_cpu_info_from_isa (opts->isa)->name);
3932 }
3933
3934 /* Perform consistency checks on the module level options exactly once.
3935 This is a deferred check that happens:
3936 at the first .set directive
3937 or, at the first pseudo op that generates code (inc .dc.a)
3938 or, at the first instruction
3939 or, at the end. */
3940
3941 static void
3942 file_mips_check_options (void)
3943 {
3944 const struct mips_cpu_info *arch_info = 0;
3945
3946 if (file_mips_opts_checked)
3947 return;
3948
3949 /* The following code determines the register size.
3950 Similar code was added to GCC 3.3 (see override_options() in
3951 config/mips/mips.c). The GAS and GCC code should be kept in sync
3952 as much as possible. */
3953
3954 if (file_mips_opts.gp < 0)
3955 {
3956 /* Infer the integer register size from the ABI and processor.
3957 Restrict ourselves to 32-bit registers if that's all the
3958 processor has, or if the ABI cannot handle 64-bit registers. */
3959 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
3960 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
3961 ? 32 : 64;
3962 }
3963
3964 if (file_mips_opts.fp < 0)
3965 {
3966 /* No user specified float register size.
3967 ??? GAS treats single-float processors as though they had 64-bit
3968 float registers (although it complains when double-precision
3969 instructions are used). As things stand, saying they have 32-bit
3970 registers would lead to spurious "register must be even" messages.
3971 So here we assume float registers are never smaller than the
3972 integer ones. */
3973 if (file_mips_opts.gp == 64)
3974 /* 64-bit integer registers implies 64-bit float registers. */
3975 file_mips_opts.fp = 64;
3976 else if ((file_mips_opts.ase & FP64_ASES)
3977 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
3978 /* Handle ASEs that require 64-bit float registers, if possible. */
3979 file_mips_opts.fp = 64;
3980 else if (ISA_IS_R6 (mips_opts.isa))
3981 /* R6 implies 64-bit float registers. */
3982 file_mips_opts.fp = 64;
3983 else
3984 /* 32-bit float registers. */
3985 file_mips_opts.fp = 32;
3986 }
3987
3988 arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
3989
3990 /* Disable operations on odd-numbered floating-point registers by default
3991 when using the FPXX ABI. */
3992 if (file_mips_opts.oddspreg < 0)
3993 {
3994 if (file_mips_opts.fp == 0)
3995 file_mips_opts.oddspreg = 0;
3996 else
3997 file_mips_opts.oddspreg = 1;
3998 }
3999
4000 /* End of GCC-shared inference code. */
4001
4002 /* This flag is set when we have a 64-bit capable CPU but use only
4003 32-bit wide registers. Note that EABI does not use it. */
4004 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4005 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4006 || mips_abi == O32_ABI))
4007 mips_32bitmode = 1;
4008
4009 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4010 as_bad (_("trap exception not supported at ISA 1"));
4011
4012 /* If the selected architecture includes support for ASEs, enable
4013 generation of code for them. */
4014 if (file_mips_opts.mips16 == -1)
4015 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4016 if (file_mips_opts.micromips == -1)
4017 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4018 ? 1 : 0;
4019
4020 if (mips_nan2008 == -1)
4021 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4022 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4023 as_fatal (_("`%s' does not support legacy NaN"),
4024 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4025
4026 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4027 being selected implicitly. */
4028 if (file_mips_opts.fp != 64)
4029 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4030
4031 /* If the user didn't explicitly select or deselect a particular ASE,
4032 use the default setting for the CPU. */
4033 file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
4034
4035 /* Set up the current options. These may change throughout assembly. */
4036 mips_opts = file_mips_opts;
4037
4038 mips_check_isa_supports_ases ();
4039 mips_check_options (&file_mips_opts, TRUE);
4040 file_mips_opts_checked = TRUE;
4041
4042 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4043 as_warn (_("could not set architecture and machine"));
4044 }
4045
4046 void
4047 md_assemble (char *str)
4048 {
4049 struct mips_cl_insn insn;
4050 bfd_reloc_code_real_type unused_reloc[3]
4051 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
4052
4053 file_mips_check_options ();
4054
4055 imm_expr.X_op = O_absent;
4056 offset_expr.X_op = O_absent;
4057 offset_reloc[0] = BFD_RELOC_UNUSED;
4058 offset_reloc[1] = BFD_RELOC_UNUSED;
4059 offset_reloc[2] = BFD_RELOC_UNUSED;
4060
4061 mips_mark_labels ();
4062 mips_assembling_insn = TRUE;
4063 clear_insn_error ();
4064
4065 if (mips_opts.mips16)
4066 mips16_ip (str, &insn);
4067 else
4068 {
4069 mips_ip (str, &insn);
4070 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4071 str, insn.insn_opcode));
4072 }
4073
4074 if (insn_error.msg)
4075 report_insn_error (str);
4076 else if (insn.insn_mo->pinfo == INSN_MACRO)
4077 {
4078 macro_start ();
4079 if (mips_opts.mips16)
4080 mips16_macro (&insn);
4081 else
4082 macro (&insn, str);
4083 macro_end ();
4084 }
4085 else
4086 {
4087 if (offset_expr.X_op != O_absent)
4088 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
4089 else
4090 append_insn (&insn, NULL, unused_reloc, FALSE);
4091 }
4092
4093 mips_assembling_insn = FALSE;
4094 }
4095
4096 /* Convenience functions for abstracting away the differences between
4097 MIPS16 and non-MIPS16 relocations. */
4098
4099 static inline bfd_boolean
4100 mips16_reloc_p (bfd_reloc_code_real_type reloc)
4101 {
4102 switch (reloc)
4103 {
4104 case BFD_RELOC_MIPS16_JMP:
4105 case BFD_RELOC_MIPS16_GPREL:
4106 case BFD_RELOC_MIPS16_GOT16:
4107 case BFD_RELOC_MIPS16_CALL16:
4108 case BFD_RELOC_MIPS16_HI16_S:
4109 case BFD_RELOC_MIPS16_HI16:
4110 case BFD_RELOC_MIPS16_LO16:
4111 case BFD_RELOC_MIPS16_16_PCREL_S1:
4112 return TRUE;
4113
4114 default:
4115 return FALSE;
4116 }
4117 }
4118
4119 static inline bfd_boolean
4120 micromips_reloc_p (bfd_reloc_code_real_type reloc)
4121 {
4122 switch (reloc)
4123 {
4124 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4125 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4126 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4127 case BFD_RELOC_MICROMIPS_GPREL16:
4128 case BFD_RELOC_MICROMIPS_JMP:
4129 case BFD_RELOC_MICROMIPS_HI16:
4130 case BFD_RELOC_MICROMIPS_HI16_S:
4131 case BFD_RELOC_MICROMIPS_LO16:
4132 case BFD_RELOC_MICROMIPS_LITERAL:
4133 case BFD_RELOC_MICROMIPS_GOT16:
4134 case BFD_RELOC_MICROMIPS_CALL16:
4135 case BFD_RELOC_MICROMIPS_GOT_HI16:
4136 case BFD_RELOC_MICROMIPS_GOT_LO16:
4137 case BFD_RELOC_MICROMIPS_CALL_HI16:
4138 case BFD_RELOC_MICROMIPS_CALL_LO16:
4139 case BFD_RELOC_MICROMIPS_SUB:
4140 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4141 case BFD_RELOC_MICROMIPS_GOT_OFST:
4142 case BFD_RELOC_MICROMIPS_GOT_DISP:
4143 case BFD_RELOC_MICROMIPS_HIGHEST:
4144 case BFD_RELOC_MICROMIPS_HIGHER:
4145 case BFD_RELOC_MICROMIPS_SCN_DISP:
4146 case BFD_RELOC_MICROMIPS_JALR:
4147 return TRUE;
4148
4149 default:
4150 return FALSE;
4151 }
4152 }
4153
4154 static inline bfd_boolean
4155 jmp_reloc_p (bfd_reloc_code_real_type reloc)
4156 {
4157 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4158 }
4159
4160 static inline bfd_boolean
4161 b_reloc_p (bfd_reloc_code_real_type reloc)
4162 {
4163 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4164 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4165 || reloc == BFD_RELOC_16_PCREL_S2
4166 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
4167 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4168 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4169 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4170 }
4171
4172 static inline bfd_boolean
4173 got16_reloc_p (bfd_reloc_code_real_type reloc)
4174 {
4175 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
4176 || reloc == BFD_RELOC_MICROMIPS_GOT16);
4177 }
4178
4179 static inline bfd_boolean
4180 hi16_reloc_p (bfd_reloc_code_real_type reloc)
4181 {
4182 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
4183 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
4184 }
4185
4186 static inline bfd_boolean
4187 lo16_reloc_p (bfd_reloc_code_real_type reloc)
4188 {
4189 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
4190 || reloc == BFD_RELOC_MICROMIPS_LO16);
4191 }
4192
4193 static inline bfd_boolean
4194 jalr_reloc_p (bfd_reloc_code_real_type reloc)
4195 {
4196 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
4197 }
4198
4199 static inline bfd_boolean
4200 gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4201 {
4202 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4203 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4204 }
4205
4206 /* Return true if RELOC is a PC-relative relocation that does not have
4207 full address range. */
4208
4209 static inline bfd_boolean
4210 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4211 {
4212 switch (reloc)
4213 {
4214 case BFD_RELOC_16_PCREL_S2:
4215 case BFD_RELOC_MIPS16_16_PCREL_S1:
4216 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4217 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4218 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4219 case BFD_RELOC_MIPS_21_PCREL_S2:
4220 case BFD_RELOC_MIPS_26_PCREL_S2:
4221 case BFD_RELOC_MIPS_18_PCREL_S3:
4222 case BFD_RELOC_MIPS_19_PCREL_S2:
4223 return TRUE;
4224
4225 case BFD_RELOC_32_PCREL:
4226 case BFD_RELOC_HI16_S_PCREL:
4227 case BFD_RELOC_LO16_PCREL:
4228 return HAVE_64BIT_ADDRESSES;
4229
4230 default:
4231 return FALSE;
4232 }
4233 }
4234
4235 /* Return true if the given relocation might need a matching %lo().
4236 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4237 need a matching %lo() when applied to local symbols. */
4238
4239 static inline bfd_boolean
4240 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
4241 {
4242 return (HAVE_IN_PLACE_ADDENDS
4243 && (hi16_reloc_p (reloc)
4244 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4245 all GOT16 relocations evaluate to "G". */
4246 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4247 }
4248
4249 /* Return the type of %lo() reloc needed by RELOC, given that
4250 reloc_needs_lo_p. */
4251
4252 static inline bfd_reloc_code_real_type
4253 matching_lo_reloc (bfd_reloc_code_real_type reloc)
4254 {
4255 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4256 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4257 : BFD_RELOC_LO16));
4258 }
4259
4260 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
4261 relocation. */
4262
4263 static inline bfd_boolean
4264 fixup_has_matching_lo_p (fixS *fixp)
4265 {
4266 return (fixp->fx_next != NULL
4267 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
4268 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4269 && fixp->fx_offset == fixp->fx_next->fx_offset);
4270 }
4271
4272 /* Move all labels in LABELS to the current insertion point. TEXT_P
4273 says whether the labels refer to text or data. */
4274
4275 static void
4276 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
4277 {
4278 struct insn_label_list *l;
4279 valueT val;
4280
4281 for (l = labels; l != NULL; l = l->next)
4282 {
4283 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
4284 symbol_set_frag (l->label, frag_now);
4285 val = (valueT) frag_now_fix ();
4286 /* MIPS16/microMIPS text labels are stored as odd. */
4287 if (text_p && HAVE_CODE_COMPRESSION)
4288 ++val;
4289 S_SET_VALUE (l->label, val);
4290 }
4291 }
4292
4293 /* Move all labels in insn_labels to the current insertion point
4294 and treat them as text labels. */
4295
4296 static void
4297 mips_move_text_labels (void)
4298 {
4299 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4300 }
4301
4302 /* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4303
4304 static bfd_boolean
4305 s_is_linkonce (symbolS *sym, segT from_seg)
4306 {
4307 bfd_boolean linkonce = FALSE;
4308 segT symseg = S_GET_SEGMENT (sym);
4309
4310 if (symseg != from_seg && !S_IS_LOCAL (sym))
4311 {
4312 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4313 linkonce = TRUE;
4314 /* The GNU toolchain uses an extension for ELF: a section
4315 beginning with the magic string .gnu.linkonce is a
4316 linkonce section. */
4317 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4318 sizeof ".gnu.linkonce" - 1) == 0)
4319 linkonce = TRUE;
4320 }
4321 return linkonce;
4322 }
4323
4324 /* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
4325 linker to handle them specially, such as generating jalx instructions
4326 when needed. We also make them odd for the duration of the assembly,
4327 in order to generate the right sort of code. We will make them even
4328 in the adjust_symtab routine, while leaving them marked. This is
4329 convenient for the debugger and the disassembler. The linker knows
4330 to make them odd again. */
4331
4332 static void
4333 mips_compressed_mark_label (symbolS *label)
4334 {
4335 gas_assert (HAVE_CODE_COMPRESSION);
4336
4337 if (mips_opts.mips16)
4338 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4339 else
4340 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
4341 if ((S_GET_VALUE (label) & 1) == 0
4342 /* Don't adjust the address if the label is global or weak, or
4343 in a link-once section, since we'll be emitting symbol reloc
4344 references to it which will be patched up by the linker, and
4345 the final value of the symbol may or may not be MIPS16/microMIPS. */
4346 && !S_IS_WEAK (label)
4347 && !S_IS_EXTERNAL (label)
4348 && !s_is_linkonce (label, now_seg))
4349 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4350 }
4351
4352 /* Mark preceding MIPS16 or microMIPS instruction labels. */
4353
4354 static void
4355 mips_compressed_mark_labels (void)
4356 {
4357 struct insn_label_list *l;
4358
4359 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4360 mips_compressed_mark_label (l->label);
4361 }
4362
4363 /* End the current frag. Make it a variant frag and record the
4364 relaxation info. */
4365
4366 static void
4367 relax_close_frag (void)
4368 {
4369 mips_macro_warning.first_frag = frag_now;
4370 frag_var (rs_machine_dependent, 0, 0,
4371 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4372 mips_pic != NO_PIC),
4373 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4374
4375 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4376 mips_relax.first_fixup = 0;
4377 }
4378
4379 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
4380 See the comment above RELAX_ENCODE for more details. */
4381
4382 static void
4383 relax_start (symbolS *symbol)
4384 {
4385 gas_assert (mips_relax.sequence == 0);
4386 mips_relax.sequence = 1;
4387 mips_relax.symbol = symbol;
4388 }
4389
4390 /* Start generating the second version of a relaxable sequence.
4391 See the comment above RELAX_ENCODE for more details. */
4392
4393 static void
4394 relax_switch (void)
4395 {
4396 gas_assert (mips_relax.sequence == 1);
4397 mips_relax.sequence = 2;
4398 }
4399
4400 /* End the current relaxable sequence. */
4401
4402 static void
4403 relax_end (void)
4404 {
4405 gas_assert (mips_relax.sequence == 2);
4406 relax_close_frag ();
4407 mips_relax.sequence = 0;
4408 }
4409
4410 /* Return true if IP is a delayed branch or jump. */
4411
4412 static inline bfd_boolean
4413 delayed_branch_p (const struct mips_cl_insn *ip)
4414 {
4415 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4416 | INSN_COND_BRANCH_DELAY
4417 | INSN_COND_BRANCH_LIKELY)) != 0;
4418 }
4419
4420 /* Return true if IP is a compact branch or jump. */
4421
4422 static inline bfd_boolean
4423 compact_branch_p (const struct mips_cl_insn *ip)
4424 {
4425 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4426 | INSN2_COND_BRANCH)) != 0;
4427 }
4428
4429 /* Return true if IP is an unconditional branch or jump. */
4430
4431 static inline bfd_boolean
4432 uncond_branch_p (const struct mips_cl_insn *ip)
4433 {
4434 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
4435 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
4436 }
4437
4438 /* Return true if IP is a branch-likely instruction. */
4439
4440 static inline bfd_boolean
4441 branch_likely_p (const struct mips_cl_insn *ip)
4442 {
4443 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4444 }
4445
4446 /* Return the type of nop that should be used to fill the delay slot
4447 of delayed branch IP. */
4448
4449 static struct mips_cl_insn *
4450 get_delay_slot_nop (const struct mips_cl_insn *ip)
4451 {
4452 if (mips_opts.micromips
4453 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4454 return &micromips_nop32_insn;
4455 return NOP_INSN;
4456 }
4457
4458 /* Return a mask that has bit N set if OPCODE reads the register(s)
4459 in operand N. */
4460
4461 static unsigned int
4462 insn_read_mask (const struct mips_opcode *opcode)
4463 {
4464 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4465 }
4466
4467 /* Return a mask that has bit N set if OPCODE writes to the register(s)
4468 in operand N. */
4469
4470 static unsigned int
4471 insn_write_mask (const struct mips_opcode *opcode)
4472 {
4473 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4474 }
4475
4476 /* Return a mask of the registers specified by operand OPERAND of INSN.
4477 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4478 is set. */
4479
4480 static unsigned int
4481 operand_reg_mask (const struct mips_cl_insn *insn,
4482 const struct mips_operand *operand,
4483 unsigned int type_mask)
4484 {
4485 unsigned int uval, vsel;
4486
4487 switch (operand->type)
4488 {
4489 case OP_INT:
4490 case OP_MAPPED_INT:
4491 case OP_MSB:
4492 case OP_PCREL:
4493 case OP_PERF_REG:
4494 case OP_ADDIUSP_INT:
4495 case OP_ENTRY_EXIT_LIST:
4496 case OP_REPEAT_DEST_REG:
4497 case OP_REPEAT_PREV_REG:
4498 case OP_PC:
4499 case OP_VU0_SUFFIX:
4500 case OP_VU0_MATCH_SUFFIX:
4501 case OP_IMM_INDEX:
4502 abort ();
4503
4504 case OP_REG:
4505 case OP_OPTIONAL_REG:
4506 {
4507 const struct mips_reg_operand *reg_op;
4508
4509 reg_op = (const struct mips_reg_operand *) operand;
4510 if (!(type_mask & (1 << reg_op->reg_type)))
4511 return 0;
4512 uval = insn_extract_operand (insn, operand);
4513 return 1 << mips_decode_reg_operand (reg_op, uval);
4514 }
4515
4516 case OP_REG_PAIR:
4517 {
4518 const struct mips_reg_pair_operand *pair_op;
4519
4520 pair_op = (const struct mips_reg_pair_operand *) operand;
4521 if (!(type_mask & (1 << pair_op->reg_type)))
4522 return 0;
4523 uval = insn_extract_operand (insn, operand);
4524 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4525 }
4526
4527 case OP_CLO_CLZ_DEST:
4528 if (!(type_mask & (1 << OP_REG_GP)))
4529 return 0;
4530 uval = insn_extract_operand (insn, operand);
4531 return (1 << (uval & 31)) | (1 << (uval >> 5));
4532
4533 case OP_SAME_RS_RT:
4534 if (!(type_mask & (1 << OP_REG_GP)))
4535 return 0;
4536 uval = insn_extract_operand (insn, operand);
4537 gas_assert ((uval & 31) == (uval >> 5));
4538 return 1 << (uval & 31);
4539
4540 case OP_CHECK_PREV:
4541 case OP_NON_ZERO_REG:
4542 if (!(type_mask & (1 << OP_REG_GP)))
4543 return 0;
4544 uval = insn_extract_operand (insn, operand);
4545 return 1 << (uval & 31);
4546
4547 case OP_LWM_SWM_LIST:
4548 abort ();
4549
4550 case OP_SAVE_RESTORE_LIST:
4551 abort ();
4552
4553 case OP_MDMX_IMM_REG:
4554 if (!(type_mask & (1 << OP_REG_VEC)))
4555 return 0;
4556 uval = insn_extract_operand (insn, operand);
4557 vsel = uval >> 5;
4558 if ((vsel & 0x18) == 0x18)
4559 return 0;
4560 return 1 << (uval & 31);
4561
4562 case OP_REG_INDEX:
4563 if (!(type_mask & (1 << OP_REG_GP)))
4564 return 0;
4565 return 1 << insn_extract_operand (insn, operand);
4566 }
4567 abort ();
4568 }
4569
4570 /* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4571 where bit N of OPNO_MASK is set if operand N should be included.
4572 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4573 is set. */
4574
4575 static unsigned int
4576 insn_reg_mask (const struct mips_cl_insn *insn,
4577 unsigned int type_mask, unsigned int opno_mask)
4578 {
4579 unsigned int opno, reg_mask;
4580
4581 opno = 0;
4582 reg_mask = 0;
4583 while (opno_mask != 0)
4584 {
4585 if (opno_mask & 1)
4586 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4587 opno_mask >>= 1;
4588 opno += 1;
4589 }
4590 return reg_mask;
4591 }
4592
4593 /* Return the mask of core registers that IP reads. */
4594
4595 static unsigned int
4596 gpr_read_mask (const struct mips_cl_insn *ip)
4597 {
4598 unsigned long pinfo, pinfo2;
4599 unsigned int mask;
4600
4601 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4602 pinfo = ip->insn_mo->pinfo;
4603 pinfo2 = ip->insn_mo->pinfo2;
4604 if (pinfo & INSN_UDI)
4605 {
4606 /* UDI instructions have traditionally been assumed to read RS
4607 and RT. */
4608 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4609 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4610 }
4611 if (pinfo & INSN_READ_GPR_24)
4612 mask |= 1 << 24;
4613 if (pinfo2 & INSN2_READ_GPR_16)
4614 mask |= 1 << 16;
4615 if (pinfo2 & INSN2_READ_SP)
4616 mask |= 1 << SP;
4617 if (pinfo2 & INSN2_READ_GPR_31)
4618 mask |= 1 << 31;
4619 /* Don't include register 0. */
4620 return mask & ~1;
4621 }
4622
4623 /* Return the mask of core registers that IP writes. */
4624
4625 static unsigned int
4626 gpr_write_mask (const struct mips_cl_insn *ip)
4627 {
4628 unsigned long pinfo, pinfo2;
4629 unsigned int mask;
4630
4631 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4632 pinfo = ip->insn_mo->pinfo;
4633 pinfo2 = ip->insn_mo->pinfo2;
4634 if (pinfo & INSN_WRITE_GPR_24)
4635 mask |= 1 << 24;
4636 if (pinfo & INSN_WRITE_GPR_31)
4637 mask |= 1 << 31;
4638 if (pinfo & INSN_UDI)
4639 /* UDI instructions have traditionally been assumed to write to RD. */
4640 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4641 if (pinfo2 & INSN2_WRITE_SP)
4642 mask |= 1 << SP;
4643 /* Don't include register 0. */
4644 return mask & ~1;
4645 }
4646
4647 /* Return the mask of floating-point registers that IP reads. */
4648
4649 static unsigned int
4650 fpr_read_mask (const struct mips_cl_insn *ip)
4651 {
4652 unsigned long pinfo;
4653 unsigned int mask;
4654
4655 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4656 | (1 << OP_REG_MSA)),
4657 insn_read_mask (ip->insn_mo));
4658 pinfo = ip->insn_mo->pinfo;
4659 /* Conservatively treat all operands to an FP_D instruction are doubles.
4660 (This is overly pessimistic for things like cvt.d.s.) */
4661 if (FPR_SIZE != 64 && (pinfo & FP_D))
4662 mask |= mask << 1;
4663 return mask;
4664 }
4665
4666 /* Return the mask of floating-point registers that IP writes. */
4667
4668 static unsigned int
4669 fpr_write_mask (const struct mips_cl_insn *ip)
4670 {
4671 unsigned long pinfo;
4672 unsigned int mask;
4673
4674 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4675 | (1 << OP_REG_MSA)),
4676 insn_write_mask (ip->insn_mo));
4677 pinfo = ip->insn_mo->pinfo;
4678 /* Conservatively treat all operands to an FP_D instruction are doubles.
4679 (This is overly pessimistic for things like cvt.s.d.) */
4680 if (FPR_SIZE != 64 && (pinfo & FP_D))
4681 mask |= mask << 1;
4682 return mask;
4683 }
4684
4685 /* Operand OPNUM of INSN is an odd-numbered floating-point register.
4686 Check whether that is allowed. */
4687
4688 static bfd_boolean
4689 mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4690 {
4691 const char *s = insn->name;
4692 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4693 || FPR_SIZE == 64)
4694 && mips_opts.oddspreg;
4695
4696 if (insn->pinfo == INSN_MACRO)
4697 /* Let a macro pass, we'll catch it later when it is expanded. */
4698 return TRUE;
4699
4700 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4701 otherwise it depends on oddspreg. */
4702 if ((insn->pinfo & FP_S)
4703 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
4704 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
4705 return FPR_SIZE == 32 || oddspreg;
4706
4707 /* Allow odd registers for single-precision ops and double-precision if the
4708 floating-point registers are 64-bit wide. */
4709 switch (insn->pinfo & (FP_S | FP_D))
4710 {
4711 case FP_S:
4712 case 0:
4713 return oddspreg;
4714 case FP_D:
4715 return FPR_SIZE == 64;
4716 default:
4717 break;
4718 }
4719
4720 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4721 s = strchr (insn->name, '.');
4722 if (s != NULL && opnum == 2)
4723 s = strchr (s + 1, '.');
4724 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4725 return oddspreg;
4726
4727 return FPR_SIZE == 64;
4728 }
4729
4730 /* Information about an instruction argument that we're trying to match. */
4731 struct mips_arg_info
4732 {
4733 /* The instruction so far. */
4734 struct mips_cl_insn *insn;
4735
4736 /* The first unconsumed operand token. */
4737 struct mips_operand_token *token;
4738
4739 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4740 int opnum;
4741
4742 /* The 1-based argument number, for error reporting. This does not
4743 count elided optional registers, etc.. */
4744 int argnum;
4745
4746 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4747 unsigned int last_regno;
4748
4749 /* If the first operand was an OP_REG, this is the register that it
4750 specified, otherwise it is ILLEGAL_REG. */
4751 unsigned int dest_regno;
4752
4753 /* The value of the last OP_INT operand. Only used for OP_MSB,
4754 where it gives the lsb position. */
4755 unsigned int last_op_int;
4756
4757 /* If true, match routines should assume that no later instruction
4758 alternative matches and should therefore be as accommodating as
4759 possible. Match routines should not report errors if something
4760 is only invalid for !LAX_MATCH. */
4761 bfd_boolean lax_match;
4762
4763 /* True if a reference to the current AT register was seen. */
4764 bfd_boolean seen_at;
4765 };
4766
4767 /* Record that the argument is out of range. */
4768
4769 static void
4770 match_out_of_range (struct mips_arg_info *arg)
4771 {
4772 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4773 }
4774
4775 /* Record that the argument isn't constant but needs to be. */
4776
4777 static void
4778 match_not_constant (struct mips_arg_info *arg)
4779 {
4780 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4781 arg->argnum);
4782 }
4783
4784 /* Try to match an OT_CHAR token for character CH. Consume the token
4785 and return true on success, otherwise return false. */
4786
4787 static bfd_boolean
4788 match_char (struct mips_arg_info *arg, char ch)
4789 {
4790 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4791 {
4792 ++arg->token;
4793 if (ch == ',')
4794 arg->argnum += 1;
4795 return TRUE;
4796 }
4797 return FALSE;
4798 }
4799
4800 /* Try to get an expression from the next tokens in ARG. Consume the
4801 tokens and return true on success, storing the expression value in
4802 VALUE and relocation types in R. */
4803
4804 static bfd_boolean
4805 match_expression (struct mips_arg_info *arg, expressionS *value,
4806 bfd_reloc_code_real_type *r)
4807 {
4808 /* If the next token is a '(' that was parsed as being part of a base
4809 expression, assume we have an elided offset. The later match will fail
4810 if this turns out to be wrong. */
4811 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4812 {
4813 value->X_op = O_constant;
4814 value->X_add_number = 0;
4815 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
4816 return TRUE;
4817 }
4818
4819 /* Reject register-based expressions such as "0+$2" and "(($2))".
4820 For plain registers the default error seems more appropriate. */
4821 if (arg->token->type == OT_INTEGER
4822 && arg->token->u.integer.value.X_op == O_register)
4823 {
4824 set_insn_error (arg->argnum, _("register value used as expression"));
4825 return FALSE;
4826 }
4827
4828 if (arg->token->type == OT_INTEGER)
4829 {
4830 *value = arg->token->u.integer.value;
4831 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4832 ++arg->token;
4833 return TRUE;
4834 }
4835
4836 set_insn_error_i
4837 (arg->argnum, _("operand %d must be an immediate expression"),
4838 arg->argnum);
4839 return FALSE;
4840 }
4841
4842 /* Try to get a constant expression from the next tokens in ARG. Consume
4843 the tokens and return return true on success, storing the constant value
4844 in *VALUE. */
4845
4846 static bfd_boolean
4847 match_const_int (struct mips_arg_info *arg, offsetT *value)
4848 {
4849 expressionS ex;
4850 bfd_reloc_code_real_type r[3];
4851
4852 if (!match_expression (arg, &ex, r))
4853 return FALSE;
4854
4855 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
4856 *value = ex.X_add_number;
4857 else
4858 {
4859 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
4860 match_out_of_range (arg);
4861 else
4862 match_not_constant (arg);
4863 return FALSE;
4864 }
4865 return TRUE;
4866 }
4867
4868 /* Return the RTYPE_* flags for a register operand of type TYPE that
4869 appears in instruction OPCODE. */
4870
4871 static unsigned int
4872 convert_reg_type (const struct mips_opcode *opcode,
4873 enum mips_reg_operand_type type)
4874 {
4875 switch (type)
4876 {
4877 case OP_REG_GP:
4878 return RTYPE_NUM | RTYPE_GP;
4879
4880 case OP_REG_FP:
4881 /* Allow vector register names for MDMX if the instruction is a 64-bit
4882 FPR load, store or move (including moves to and from GPRs). */
4883 if ((mips_opts.ase & ASE_MDMX)
4884 && (opcode->pinfo & FP_D)
4885 && (opcode->pinfo & (INSN_COPROC_MOVE
4886 | INSN_COPROC_MEMORY_DELAY
4887 | INSN_LOAD_COPROC
4888 | INSN_LOAD_MEMORY
4889 | INSN_STORE_MEMORY)))
4890 return RTYPE_FPU | RTYPE_VEC;
4891 return RTYPE_FPU;
4892
4893 case OP_REG_CCC:
4894 if (opcode->pinfo & (FP_D | FP_S))
4895 return RTYPE_CCC | RTYPE_FCC;
4896 return RTYPE_CCC;
4897
4898 case OP_REG_VEC:
4899 if (opcode->membership & INSN_5400)
4900 return RTYPE_FPU;
4901 return RTYPE_FPU | RTYPE_VEC;
4902
4903 case OP_REG_ACC:
4904 return RTYPE_ACC;
4905
4906 case OP_REG_COPRO:
4907 if (opcode->name[strlen (opcode->name) - 1] == '0')
4908 return RTYPE_NUM | RTYPE_CP0;
4909 return RTYPE_NUM;
4910
4911 case OP_REG_HW:
4912 return RTYPE_NUM;
4913
4914 case OP_REG_VI:
4915 return RTYPE_NUM | RTYPE_VI;
4916
4917 case OP_REG_VF:
4918 return RTYPE_NUM | RTYPE_VF;
4919
4920 case OP_REG_R5900_I:
4921 return RTYPE_R5900_I;
4922
4923 case OP_REG_R5900_Q:
4924 return RTYPE_R5900_Q;
4925
4926 case OP_REG_R5900_R:
4927 return RTYPE_R5900_R;
4928
4929 case OP_REG_R5900_ACC:
4930 return RTYPE_R5900_ACC;
4931
4932 case OP_REG_MSA:
4933 return RTYPE_MSA;
4934
4935 case OP_REG_MSA_CTRL:
4936 return RTYPE_NUM;
4937 }
4938 abort ();
4939 }
4940
4941 /* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
4942
4943 static void
4944 check_regno (struct mips_arg_info *arg,
4945 enum mips_reg_operand_type type, unsigned int regno)
4946 {
4947 if (AT && type == OP_REG_GP && regno == AT)
4948 arg->seen_at = TRUE;
4949
4950 if (type == OP_REG_FP
4951 && (regno & 1) != 0
4952 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
4953 {
4954 /* This was a warning prior to introducing O32 FPXX and FP64 support
4955 so maintain a warning for FP32 but raise an error for the new
4956 cases. */
4957 if (FPR_SIZE == 32)
4958 as_warn (_("float register should be even, was %d"), regno);
4959 else
4960 as_bad (_("float register should be even, was %d"), regno);
4961 }
4962
4963 if (type == OP_REG_CCC)
4964 {
4965 const char *name;
4966 size_t length;
4967
4968 name = arg->insn->insn_mo->name;
4969 length = strlen (name);
4970 if ((regno & 1) != 0
4971 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
4972 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
4973 as_warn (_("condition code register should be even for %s, was %d"),
4974 name, regno);
4975
4976 if ((regno & 3) != 0
4977 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
4978 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
4979 name, regno);
4980 }
4981 }
4982
4983 /* ARG is a register with symbol value SYMVAL. Try to interpret it as
4984 a register of type TYPE. Return true on success, storing the register
4985 number in *REGNO and warning about any dubious uses. */
4986
4987 static bfd_boolean
4988 match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4989 unsigned int symval, unsigned int *regno)
4990 {
4991 if (type == OP_REG_VEC)
4992 symval = mips_prefer_vec_regno (symval);
4993 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
4994 return FALSE;
4995
4996 *regno = symval & RNUM_MASK;
4997 check_regno (arg, type, *regno);
4998 return TRUE;
4999 }
5000
5001 /* Try to interpret the next token in ARG as a register of type TYPE.
5002 Consume the token and return true on success, storing the register
5003 number in *REGNO. Return false on failure. */
5004
5005 static bfd_boolean
5006 match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5007 unsigned int *regno)
5008 {
5009 if (arg->token->type == OT_REG
5010 && match_regno (arg, type, arg->token->u.regno, regno))
5011 {
5012 ++arg->token;
5013 return TRUE;
5014 }
5015 return FALSE;
5016 }
5017
5018 /* Try to interpret the next token in ARG as a range of registers of type TYPE.
5019 Consume the token and return true on success, storing the register numbers
5020 in *REGNO1 and *REGNO2. Return false on failure. */
5021
5022 static bfd_boolean
5023 match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5024 unsigned int *regno1, unsigned int *regno2)
5025 {
5026 if (match_reg (arg, type, regno1))
5027 {
5028 *regno2 = *regno1;
5029 return TRUE;
5030 }
5031 if (arg->token->type == OT_REG_RANGE
5032 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5033 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5034 && *regno1 <= *regno2)
5035 {
5036 ++arg->token;
5037 return TRUE;
5038 }
5039 return FALSE;
5040 }
5041
5042 /* OP_INT matcher. */
5043
5044 static bfd_boolean
5045 match_int_operand (struct mips_arg_info *arg,
5046 const struct mips_operand *operand_base)
5047 {
5048 const struct mips_int_operand *operand;
5049 unsigned int uval;
5050 int min_val, max_val, factor;
5051 offsetT sval;
5052
5053 operand = (const struct mips_int_operand *) operand_base;
5054 factor = 1 << operand->shift;
5055 min_val = mips_int_operand_min (operand);
5056 max_val = mips_int_operand_max (operand);
5057
5058 if (operand_base->lsb == 0
5059 && operand_base->size == 16
5060 && operand->shift == 0
5061 && operand->bias == 0
5062 && (operand->max_val == 32767 || operand->max_val == 65535))
5063 {
5064 /* The operand can be relocated. */
5065 if (!match_expression (arg, &offset_expr, offset_reloc))
5066 return FALSE;
5067
5068 if (offset_expr.X_op == O_big)
5069 {
5070 match_out_of_range (arg);
5071 return FALSE;
5072 }
5073
5074 if (offset_reloc[0] != BFD_RELOC_UNUSED)
5075 /* Relocation operators were used. Accept the argument and
5076 leave the relocation value in offset_expr and offset_relocs
5077 for the caller to process. */
5078 return TRUE;
5079
5080 if (offset_expr.X_op != O_constant)
5081 {
5082 /* Accept non-constant operands if no later alternative matches,
5083 leaving it for the caller to process. */
5084 if (!arg->lax_match)
5085 {
5086 match_not_constant (arg);
5087 return FALSE;
5088 }
5089 offset_reloc[0] = BFD_RELOC_LO16;
5090 return TRUE;
5091 }
5092
5093 /* Clear the global state; we're going to install the operand
5094 ourselves. */
5095 sval = offset_expr.X_add_number;
5096 offset_expr.X_op = O_absent;
5097
5098 /* For compatibility with older assemblers, we accept
5099 0x8000-0xffff as signed 16-bit numbers when only
5100 signed numbers are allowed. */
5101 if (sval > max_val)
5102 {
5103 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5104 if (!arg->lax_match && sval <= max_val)
5105 {
5106 match_out_of_range (arg);
5107 return FALSE;
5108 }
5109 }
5110 }
5111 else
5112 {
5113 if (!match_const_int (arg, &sval))
5114 return FALSE;
5115 }
5116
5117 arg->last_op_int = sval;
5118
5119 if (sval < min_val || sval > max_val || sval % factor)
5120 {
5121 match_out_of_range (arg);
5122 return FALSE;
5123 }
5124
5125 uval = (unsigned int) sval >> operand->shift;
5126 uval -= operand->bias;
5127
5128 /* Handle -mfix-cn63xxp1. */
5129 if (arg->opnum == 1
5130 && mips_fix_cn63xxp1
5131 && !mips_opts.micromips
5132 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5133 switch (uval)
5134 {
5135 case 5:
5136 case 25:
5137 case 26:
5138 case 27:
5139 case 28:
5140 case 29:
5141 case 30:
5142 case 31:
5143 /* These are ok. */
5144 break;
5145
5146 default:
5147 /* The rest must be changed to 28. */
5148 uval = 28;
5149 break;
5150 }
5151
5152 insn_insert_operand (arg->insn, operand_base, uval);
5153 return TRUE;
5154 }
5155
5156 /* OP_MAPPED_INT matcher. */
5157
5158 static bfd_boolean
5159 match_mapped_int_operand (struct mips_arg_info *arg,
5160 const struct mips_operand *operand_base)
5161 {
5162 const struct mips_mapped_int_operand *operand;
5163 unsigned int uval, num_vals;
5164 offsetT sval;
5165
5166 operand = (const struct mips_mapped_int_operand *) operand_base;
5167 if (!match_const_int (arg, &sval))
5168 return FALSE;
5169
5170 num_vals = 1 << operand_base->size;
5171 for (uval = 0; uval < num_vals; uval++)
5172 if (operand->int_map[uval] == sval)
5173 break;
5174 if (uval == num_vals)
5175 {
5176 match_out_of_range (arg);
5177 return FALSE;
5178 }
5179
5180 insn_insert_operand (arg->insn, operand_base, uval);
5181 return TRUE;
5182 }
5183
5184 /* OP_MSB matcher. */
5185
5186 static bfd_boolean
5187 match_msb_operand (struct mips_arg_info *arg,
5188 const struct mips_operand *operand_base)
5189 {
5190 const struct mips_msb_operand *operand;
5191 int min_val, max_val, max_high;
5192 offsetT size, sval, high;
5193
5194 operand = (const struct mips_msb_operand *) operand_base;
5195 min_val = operand->bias;
5196 max_val = min_val + (1 << operand_base->size) - 1;
5197 max_high = operand->opsize;
5198
5199 if (!match_const_int (arg, &size))
5200 return FALSE;
5201
5202 high = size + arg->last_op_int;
5203 sval = operand->add_lsb ? high : size;
5204
5205 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5206 {
5207 match_out_of_range (arg);
5208 return FALSE;
5209 }
5210 insn_insert_operand (arg->insn, operand_base, sval - min_val);
5211 return TRUE;
5212 }
5213
5214 /* OP_REG matcher. */
5215
5216 static bfd_boolean
5217 match_reg_operand (struct mips_arg_info *arg,
5218 const struct mips_operand *operand_base)
5219 {
5220 const struct mips_reg_operand *operand;
5221 unsigned int regno, uval, num_vals;
5222
5223 operand = (const struct mips_reg_operand *) operand_base;
5224 if (!match_reg (arg, operand->reg_type, &regno))
5225 return FALSE;
5226
5227 if (operand->reg_map)
5228 {
5229 num_vals = 1 << operand->root.size;
5230 for (uval = 0; uval < num_vals; uval++)
5231 if (operand->reg_map[uval] == regno)
5232 break;
5233 if (num_vals == uval)
5234 return FALSE;
5235 }
5236 else
5237 uval = regno;
5238
5239 arg->last_regno = regno;
5240 if (arg->opnum == 1)
5241 arg->dest_regno = regno;
5242 insn_insert_operand (arg->insn, operand_base, uval);
5243 return TRUE;
5244 }
5245
5246 /* OP_REG_PAIR matcher. */
5247
5248 static bfd_boolean
5249 match_reg_pair_operand (struct mips_arg_info *arg,
5250 const struct mips_operand *operand_base)
5251 {
5252 const struct mips_reg_pair_operand *operand;
5253 unsigned int regno1, regno2, uval, num_vals;
5254
5255 operand = (const struct mips_reg_pair_operand *) operand_base;
5256 if (!match_reg (arg, operand->reg_type, &regno1)
5257 || !match_char (arg, ',')
5258 || !match_reg (arg, operand->reg_type, &regno2))
5259 return FALSE;
5260
5261 num_vals = 1 << operand_base->size;
5262 for (uval = 0; uval < num_vals; uval++)
5263 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5264 break;
5265 if (uval == num_vals)
5266 return FALSE;
5267
5268 insn_insert_operand (arg->insn, operand_base, uval);
5269 return TRUE;
5270 }
5271
5272 /* OP_PCREL matcher. The caller chooses the relocation type. */
5273
5274 static bfd_boolean
5275 match_pcrel_operand (struct mips_arg_info *arg)
5276 {
5277 bfd_reloc_code_real_type r[3];
5278
5279 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
5280 }
5281
5282 /* OP_PERF_REG matcher. */
5283
5284 static bfd_boolean
5285 match_perf_reg_operand (struct mips_arg_info *arg,
5286 const struct mips_operand *operand)
5287 {
5288 offsetT sval;
5289
5290 if (!match_const_int (arg, &sval))
5291 return FALSE;
5292
5293 if (sval != 0
5294 && (sval != 1
5295 || (mips_opts.arch == CPU_R5900
5296 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5297 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5298 {
5299 set_insn_error (arg->argnum, _("invalid performance register"));
5300 return FALSE;
5301 }
5302
5303 insn_insert_operand (arg->insn, operand, sval);
5304 return TRUE;
5305 }
5306
5307 /* OP_ADDIUSP matcher. */
5308
5309 static bfd_boolean
5310 match_addiusp_operand (struct mips_arg_info *arg,
5311 const struct mips_operand *operand)
5312 {
5313 offsetT sval;
5314 unsigned int uval;
5315
5316 if (!match_const_int (arg, &sval))
5317 return FALSE;
5318
5319 if (sval % 4)
5320 {
5321 match_out_of_range (arg);
5322 return FALSE;
5323 }
5324
5325 sval /= 4;
5326 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
5327 {
5328 match_out_of_range (arg);
5329 return FALSE;
5330 }
5331
5332 uval = (unsigned int) sval;
5333 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5334 insn_insert_operand (arg->insn, operand, uval);
5335 return TRUE;
5336 }
5337
5338 /* OP_CLO_CLZ_DEST matcher. */
5339
5340 static bfd_boolean
5341 match_clo_clz_dest_operand (struct mips_arg_info *arg,
5342 const struct mips_operand *operand)
5343 {
5344 unsigned int regno;
5345
5346 if (!match_reg (arg, OP_REG_GP, &regno))
5347 return FALSE;
5348
5349 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5350 return TRUE;
5351 }
5352
5353 /* OP_CHECK_PREV matcher. */
5354
5355 static bfd_boolean
5356 match_check_prev_operand (struct mips_arg_info *arg,
5357 const struct mips_operand *operand_base)
5358 {
5359 const struct mips_check_prev_operand *operand;
5360 unsigned int regno;
5361
5362 operand = (const struct mips_check_prev_operand *) operand_base;
5363
5364 if (!match_reg (arg, OP_REG_GP, &regno))
5365 return FALSE;
5366
5367 if (!operand->zero_ok && regno == 0)
5368 return FALSE;
5369
5370 if ((operand->less_than_ok && regno < arg->last_regno)
5371 || (operand->greater_than_ok && regno > arg->last_regno)
5372 || (operand->equal_ok && regno == arg->last_regno))
5373 {
5374 arg->last_regno = regno;
5375 insn_insert_operand (arg->insn, operand_base, regno);
5376 return TRUE;
5377 }
5378
5379 return FALSE;
5380 }
5381
5382 /* OP_SAME_RS_RT matcher. */
5383
5384 static bfd_boolean
5385 match_same_rs_rt_operand (struct mips_arg_info *arg,
5386 const struct mips_operand *operand)
5387 {
5388 unsigned int regno;
5389
5390 if (!match_reg (arg, OP_REG_GP, &regno))
5391 return FALSE;
5392
5393 if (regno == 0)
5394 {
5395 set_insn_error (arg->argnum, _("the source register must not be $0"));
5396 return FALSE;
5397 }
5398
5399 arg->last_regno = regno;
5400
5401 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5402 return TRUE;
5403 }
5404
5405 /* OP_LWM_SWM_LIST matcher. */
5406
5407 static bfd_boolean
5408 match_lwm_swm_list_operand (struct mips_arg_info *arg,
5409 const struct mips_operand *operand)
5410 {
5411 unsigned int reglist, sregs, ra, regno1, regno2;
5412 struct mips_arg_info reset;
5413
5414 reglist = 0;
5415 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5416 return FALSE;
5417 do
5418 {
5419 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5420 {
5421 reglist |= 1 << FP;
5422 regno2 = S7;
5423 }
5424 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5425 reset = *arg;
5426 }
5427 while (match_char (arg, ',')
5428 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5429 *arg = reset;
5430
5431 if (operand->size == 2)
5432 {
5433 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5434
5435 s0, ra
5436 s0, s1, ra, s2, s3
5437 s0-s2, ra
5438
5439 and any permutations of these. */
5440 if ((reglist & 0xfff1ffff) != 0x80010000)
5441 return FALSE;
5442
5443 sregs = (reglist >> 17) & 7;
5444 ra = 0;
5445 }
5446 else
5447 {
5448 /* The list must include at least one of ra and s0-sN,
5449 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5450 which are $23 and $30 respectively.) E.g.:
5451
5452 ra
5453 s0
5454 ra, s0, s1, s2
5455 s0-s8
5456 s0-s5, ra
5457
5458 and any permutations of these. */
5459 if ((reglist & 0x3f00ffff) != 0)
5460 return FALSE;
5461
5462 ra = (reglist >> 27) & 0x10;
5463 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5464 }
5465 sregs += 1;
5466 if ((sregs & -sregs) != sregs)
5467 return FALSE;
5468
5469 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
5470 return TRUE;
5471 }
5472
5473 /* OP_ENTRY_EXIT_LIST matcher. */
5474
5475 static unsigned int
5476 match_entry_exit_operand (struct mips_arg_info *arg,
5477 const struct mips_operand *operand)
5478 {
5479 unsigned int mask;
5480 bfd_boolean is_exit;
5481
5482 /* The format is the same for both ENTRY and EXIT, but the constraints
5483 are different. */
5484 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5485 mask = (is_exit ? 7 << 3 : 0);
5486 do
5487 {
5488 unsigned int regno1, regno2;
5489 bfd_boolean is_freg;
5490
5491 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5492 is_freg = FALSE;
5493 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
5494 is_freg = TRUE;
5495 else
5496 return FALSE;
5497
5498 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5499 {
5500 mask &= ~(7 << 3);
5501 mask |= (5 + regno2) << 3;
5502 }
5503 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5504 mask |= (regno2 - 3) << 3;
5505 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5506 mask |= (regno2 - 15) << 1;
5507 else if (regno1 == RA && regno2 == RA)
5508 mask |= 1;
5509 else
5510 return FALSE;
5511 }
5512 while (match_char (arg, ','));
5513
5514 insn_insert_operand (arg->insn, operand, mask);
5515 return TRUE;
5516 }
5517
5518 /* OP_SAVE_RESTORE_LIST matcher. */
5519
5520 static bfd_boolean
5521 match_save_restore_list_operand (struct mips_arg_info *arg)
5522 {
5523 unsigned int opcode, args, statics, sregs;
5524 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
5525 offsetT frame_size;
5526
5527 opcode = arg->insn->insn_opcode;
5528 frame_size = 0;
5529 num_frame_sizes = 0;
5530 args = 0;
5531 statics = 0;
5532 sregs = 0;
5533 do
5534 {
5535 unsigned int regno1, regno2;
5536
5537 if (arg->token->type == OT_INTEGER)
5538 {
5539 /* Handle the frame size. */
5540 if (!match_const_int (arg, &frame_size))
5541 return FALSE;
5542 num_frame_sizes += 1;
5543 }
5544 else
5545 {
5546 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5547 return FALSE;
5548
5549 while (regno1 <= regno2)
5550 {
5551 if (regno1 >= 4 && regno1 <= 7)
5552 {
5553 if (num_frame_sizes == 0)
5554 /* args $a0-$a3 */
5555 args |= 1 << (regno1 - 4);
5556 else
5557 /* statics $a0-$a3 */
5558 statics |= 1 << (regno1 - 4);
5559 }
5560 else if (regno1 >= 16 && regno1 <= 23)
5561 /* $s0-$s7 */
5562 sregs |= 1 << (regno1 - 16);
5563 else if (regno1 == 30)
5564 /* $s8 */
5565 sregs |= 1 << 8;
5566 else if (regno1 == 31)
5567 /* Add $ra to insn. */
5568 opcode |= 0x40;
5569 else
5570 return FALSE;
5571 regno1 += 1;
5572 if (regno1 == 24)
5573 regno1 = 30;
5574 }
5575 }
5576 }
5577 while (match_char (arg, ','));
5578
5579 /* Encode args/statics combination. */
5580 if (args & statics)
5581 return FALSE;
5582 else if (args == 0xf)
5583 /* All $a0-$a3 are args. */
5584 opcode |= MIPS16_ALL_ARGS << 16;
5585 else if (statics == 0xf)
5586 /* All $a0-$a3 are statics. */
5587 opcode |= MIPS16_ALL_STATICS << 16;
5588 else
5589 {
5590 /* Count arg registers. */
5591 num_args = 0;
5592 while (args & 0x1)
5593 {
5594 args >>= 1;
5595 num_args += 1;
5596 }
5597 if (args != 0)
5598 return FALSE;
5599
5600 /* Count static registers. */
5601 num_statics = 0;
5602 while (statics & 0x8)
5603 {
5604 statics = (statics << 1) & 0xf;
5605 num_statics += 1;
5606 }
5607 if (statics != 0)
5608 return FALSE;
5609
5610 /* Encode args/statics. */
5611 opcode |= ((num_args << 2) | num_statics) << 16;
5612 }
5613
5614 /* Encode $s0/$s1. */
5615 if (sregs & (1 << 0)) /* $s0 */
5616 opcode |= 0x20;
5617 if (sregs & (1 << 1)) /* $s1 */
5618 opcode |= 0x10;
5619 sregs >>= 2;
5620
5621 /* Encode $s2-$s8. */
5622 num_sregs = 0;
5623 while (sregs & 1)
5624 {
5625 sregs >>= 1;
5626 num_sregs += 1;
5627 }
5628 if (sregs != 0)
5629 return FALSE;
5630 opcode |= num_sregs << 24;
5631
5632 /* Encode frame size. */
5633 if (num_frame_sizes == 0)
5634 {
5635 set_insn_error (arg->argnum, _("missing frame size"));
5636 return FALSE;
5637 }
5638 if (num_frame_sizes > 1)
5639 {
5640 set_insn_error (arg->argnum, _("frame size specified twice"));
5641 return FALSE;
5642 }
5643 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5644 {
5645 set_insn_error (arg->argnum, _("invalid frame size"));
5646 return FALSE;
5647 }
5648 if (frame_size != 128 || (opcode >> 16) != 0)
5649 {
5650 frame_size /= 8;
5651 opcode |= (((frame_size & 0xf0) << 16)
5652 | (frame_size & 0x0f));
5653 }
5654
5655 /* Finally build the instruction. */
5656 if ((opcode >> 16) != 0 || frame_size == 0)
5657 opcode |= MIPS16_EXTEND;
5658 arg->insn->insn_opcode = opcode;
5659 return TRUE;
5660 }
5661
5662 /* OP_MDMX_IMM_REG matcher. */
5663
5664 static bfd_boolean
5665 match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
5666 const struct mips_operand *operand)
5667 {
5668 unsigned int regno, uval;
5669 bfd_boolean is_qh;
5670 const struct mips_opcode *opcode;
5671
5672 /* The mips_opcode records whether this is an octobyte or quadhalf
5673 instruction. Start out with that bit in place. */
5674 opcode = arg->insn->insn_mo;
5675 uval = mips_extract_operand (operand, opcode->match);
5676 is_qh = (uval != 0);
5677
5678 if (arg->token->type == OT_REG)
5679 {
5680 if ((opcode->membership & INSN_5400)
5681 && strcmp (opcode->name, "rzu.ob") == 0)
5682 {
5683 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5684 arg->argnum);
5685 return FALSE;
5686 }
5687
5688 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5689 return FALSE;
5690 ++arg->token;
5691
5692 /* Check whether this is a vector register or a broadcast of
5693 a single element. */
5694 if (arg->token->type == OT_INTEGER_INDEX)
5695 {
5696 if (arg->token->u.index > (is_qh ? 3 : 7))
5697 {
5698 set_insn_error (arg->argnum, _("invalid element selector"));
5699 return FALSE;
5700 }
5701 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5702 ++arg->token;
5703 }
5704 else
5705 {
5706 /* A full vector. */
5707 if ((opcode->membership & INSN_5400)
5708 && (strcmp (opcode->name, "sll.ob") == 0
5709 || strcmp (opcode->name, "srl.ob") == 0))
5710 {
5711 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5712 arg->argnum);
5713 return FALSE;
5714 }
5715
5716 if (is_qh)
5717 uval |= MDMX_FMTSEL_VEC_QH << 5;
5718 else
5719 uval |= MDMX_FMTSEL_VEC_OB << 5;
5720 }
5721 uval |= regno;
5722 }
5723 else
5724 {
5725 offsetT sval;
5726
5727 if (!match_const_int (arg, &sval))
5728 return FALSE;
5729 if (sval < 0 || sval > 31)
5730 {
5731 match_out_of_range (arg);
5732 return FALSE;
5733 }
5734 uval |= (sval & 31);
5735 if (is_qh)
5736 uval |= MDMX_FMTSEL_IMM_QH << 5;
5737 else
5738 uval |= MDMX_FMTSEL_IMM_OB << 5;
5739 }
5740 insn_insert_operand (arg->insn, operand, uval);
5741 return TRUE;
5742 }
5743
5744 /* OP_IMM_INDEX matcher. */
5745
5746 static bfd_boolean
5747 match_imm_index_operand (struct mips_arg_info *arg,
5748 const struct mips_operand *operand)
5749 {
5750 unsigned int max_val;
5751
5752 if (arg->token->type != OT_INTEGER_INDEX)
5753 return FALSE;
5754
5755 max_val = (1 << operand->size) - 1;
5756 if (arg->token->u.index > max_val)
5757 {
5758 match_out_of_range (arg);
5759 return FALSE;
5760 }
5761 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5762 ++arg->token;
5763 return TRUE;
5764 }
5765
5766 /* OP_REG_INDEX matcher. */
5767
5768 static bfd_boolean
5769 match_reg_index_operand (struct mips_arg_info *arg,
5770 const struct mips_operand *operand)
5771 {
5772 unsigned int regno;
5773
5774 if (arg->token->type != OT_REG_INDEX)
5775 return FALSE;
5776
5777 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5778 return FALSE;
5779
5780 insn_insert_operand (arg->insn, operand, regno);
5781 ++arg->token;
5782 return TRUE;
5783 }
5784
5785 /* OP_PC matcher. */
5786
5787 static bfd_boolean
5788 match_pc_operand (struct mips_arg_info *arg)
5789 {
5790 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5791 {
5792 ++arg->token;
5793 return TRUE;
5794 }
5795 return FALSE;
5796 }
5797
5798 /* OP_NON_ZERO_REG matcher. */
5799
5800 static bfd_boolean
5801 match_non_zero_reg_operand (struct mips_arg_info *arg,
5802 const struct mips_operand *operand)
5803 {
5804 unsigned int regno;
5805
5806 if (!match_reg (arg, OP_REG_GP, &regno))
5807 return FALSE;
5808
5809 if (regno == 0)
5810 return FALSE;
5811
5812 arg->last_regno = regno;
5813 insn_insert_operand (arg->insn, operand, regno);
5814 return TRUE;
5815 }
5816
5817 /* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5818 register that we need to match. */
5819
5820 static bfd_boolean
5821 match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
5822 {
5823 unsigned int regno;
5824
5825 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
5826 }
5827
5828 /* Read a floating-point constant from S for LI.S or LI.D. LENGTH is
5829 the length of the value in bytes (4 for float, 8 for double) and
5830 USING_GPRS says whether the destination is a GPR rather than an FPR.
5831
5832 Return the constant in IMM and OFFSET as follows:
5833
5834 - If the constant should be loaded via memory, set IMM to O_absent and
5835 OFFSET to the memory address.
5836
5837 - Otherwise, if the constant should be loaded into two 32-bit registers,
5838 set IMM to the O_constant to load into the high register and OFFSET
5839 to the corresponding value for the low register.
5840
5841 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5842
5843 These constants only appear as the last operand in an instruction,
5844 and every instruction that accepts them in any variant accepts them
5845 in all variants. This means we don't have to worry about backing out
5846 any changes if the instruction does not match. We just match
5847 unconditionally and report an error if the constant is invalid. */
5848
5849 static bfd_boolean
5850 match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5851 expressionS *offset, int length, bfd_boolean using_gprs)
5852 {
5853 char *p;
5854 segT seg, new_seg;
5855 subsegT subseg;
5856 const char *newname;
5857 unsigned char *data;
5858
5859 /* Where the constant is placed is based on how the MIPS assembler
5860 does things:
5861
5862 length == 4 && using_gprs -- immediate value only
5863 length == 8 && using_gprs -- .rdata or immediate value
5864 length == 4 && !using_gprs -- .lit4 or immediate value
5865 length == 8 && !using_gprs -- .lit8 or immediate value
5866
5867 The .lit4 and .lit8 sections are only used if permitted by the
5868 -G argument. */
5869 if (arg->token->type != OT_FLOAT)
5870 {
5871 set_insn_error (arg->argnum, _("floating-point expression required"));
5872 return FALSE;
5873 }
5874
5875 gas_assert (arg->token->u.flt.length == length);
5876 data = arg->token->u.flt.data;
5877 ++arg->token;
5878
5879 /* Handle 32-bit constants for which an immediate value is best. */
5880 if (length == 4
5881 && (using_gprs
5882 || g_switch_value < 4
5883 || (data[0] == 0 && data[1] == 0)
5884 || (data[2] == 0 && data[3] == 0)))
5885 {
5886 imm->X_op = O_constant;
5887 if (!target_big_endian)
5888 imm->X_add_number = bfd_getl32 (data);
5889 else
5890 imm->X_add_number = bfd_getb32 (data);
5891 offset->X_op = O_absent;
5892 return TRUE;
5893 }
5894
5895 /* Handle 64-bit constants for which an immediate value is best. */
5896 if (length == 8
5897 && !mips_disable_float_construction
5898 /* Constants can only be constructed in GPRs and copied to FPRs if the
5899 GPRs are at least as wide as the FPRs or MTHC1 is available.
5900 Unlike most tests for 32-bit floating-point registers this check
5901 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
5902 permit 64-bit moves without MXHC1.
5903 Force the constant into memory otherwise. */
5904 && (using_gprs
5905 || GPR_SIZE == 64
5906 || ISA_HAS_MXHC1 (mips_opts.isa)
5907 || FPR_SIZE == 32)
5908 && ((data[0] == 0 && data[1] == 0)
5909 || (data[2] == 0 && data[3] == 0))
5910 && ((data[4] == 0 && data[5] == 0)
5911 || (data[6] == 0 && data[7] == 0)))
5912 {
5913 /* The value is simple enough to load with a couple of instructions.
5914 If using 32-bit registers, set IMM to the high order 32 bits and
5915 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
5916 64 bit constant. */
5917 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
5918 {
5919 imm->X_op = O_constant;
5920 offset->X_op = O_constant;
5921 if (!target_big_endian)
5922 {
5923 imm->X_add_number = bfd_getl32 (data + 4);
5924 offset->X_add_number = bfd_getl32 (data);
5925 }
5926 else
5927 {
5928 imm->X_add_number = bfd_getb32 (data);
5929 offset->X_add_number = bfd_getb32 (data + 4);
5930 }
5931 if (offset->X_add_number == 0)
5932 offset->X_op = O_absent;
5933 }
5934 else
5935 {
5936 imm->X_op = O_constant;
5937 if (!target_big_endian)
5938 imm->X_add_number = bfd_getl64 (data);
5939 else
5940 imm->X_add_number = bfd_getb64 (data);
5941 offset->X_op = O_absent;
5942 }
5943 return TRUE;
5944 }
5945
5946 /* Switch to the right section. */
5947 seg = now_seg;
5948 subseg = now_subseg;
5949 if (length == 4)
5950 {
5951 gas_assert (!using_gprs && g_switch_value >= 4);
5952 newname = ".lit4";
5953 }
5954 else
5955 {
5956 if (using_gprs || g_switch_value < 8)
5957 newname = RDATA_SECTION_NAME;
5958 else
5959 newname = ".lit8";
5960 }
5961
5962 new_seg = subseg_new (newname, (subsegT) 0);
5963 bfd_set_section_flags (stdoutput, new_seg,
5964 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
5965 frag_align (length == 4 ? 2 : 3, 0, 0);
5966 if (strncmp (TARGET_OS, "elf", 3) != 0)
5967 record_alignment (new_seg, 4);
5968 else
5969 record_alignment (new_seg, length == 4 ? 2 : 3);
5970 if (seg == now_seg)
5971 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
5972
5973 /* Set the argument to the current address in the section. */
5974 imm->X_op = O_absent;
5975 offset->X_op = O_symbol;
5976 offset->X_add_symbol = symbol_temp_new_now ();
5977 offset->X_add_number = 0;
5978
5979 /* Put the floating point number into the section. */
5980 p = frag_more (length);
5981 memcpy (p, data, length);
5982
5983 /* Switch back to the original section. */
5984 subseg_set (seg, subseg);
5985 return TRUE;
5986 }
5987
5988 /* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
5989 them. */
5990
5991 static bfd_boolean
5992 match_vu0_suffix_operand (struct mips_arg_info *arg,
5993 const struct mips_operand *operand,
5994 bfd_boolean match_p)
5995 {
5996 unsigned int uval;
5997
5998 /* The operand can be an XYZW mask or a single 2-bit channel index
5999 (with X being 0). */
6000 gas_assert (operand->size == 2 || operand->size == 4);
6001
6002 /* The suffix can be omitted when it is already part of the opcode. */
6003 if (arg->token->type != OT_CHANNELS)
6004 return match_p;
6005
6006 uval = arg->token->u.channels;
6007 if (operand->size == 2)
6008 {
6009 /* Check that a single bit is set and convert it into a 2-bit index. */
6010 if ((uval & -uval) != uval)
6011 return FALSE;
6012 uval = 4 - ffs (uval);
6013 }
6014
6015 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6016 return FALSE;
6017
6018 ++arg->token;
6019 if (!match_p)
6020 insn_insert_operand (arg->insn, operand, uval);
6021 return TRUE;
6022 }
6023
6024 /* S is the text seen for ARG. Match it against OPERAND. Return the end
6025 of the argument text if the match is successful, otherwise return null. */
6026
6027 static bfd_boolean
6028 match_operand (struct mips_arg_info *arg,
6029 const struct mips_operand *operand)
6030 {
6031 switch (operand->type)
6032 {
6033 case OP_INT:
6034 return match_int_operand (arg, operand);
6035
6036 case OP_MAPPED_INT:
6037 return match_mapped_int_operand (arg, operand);
6038
6039 case OP_MSB:
6040 return match_msb_operand (arg, operand);
6041
6042 case OP_REG:
6043 case OP_OPTIONAL_REG:
6044 return match_reg_operand (arg, operand);
6045
6046 case OP_REG_PAIR:
6047 return match_reg_pair_operand (arg, operand);
6048
6049 case OP_PCREL:
6050 return match_pcrel_operand (arg);
6051
6052 case OP_PERF_REG:
6053 return match_perf_reg_operand (arg, operand);
6054
6055 case OP_ADDIUSP_INT:
6056 return match_addiusp_operand (arg, operand);
6057
6058 case OP_CLO_CLZ_DEST:
6059 return match_clo_clz_dest_operand (arg, operand);
6060
6061 case OP_LWM_SWM_LIST:
6062 return match_lwm_swm_list_operand (arg, operand);
6063
6064 case OP_ENTRY_EXIT_LIST:
6065 return match_entry_exit_operand (arg, operand);
6066
6067 case OP_SAVE_RESTORE_LIST:
6068 return match_save_restore_list_operand (arg);
6069
6070 case OP_MDMX_IMM_REG:
6071 return match_mdmx_imm_reg_operand (arg, operand);
6072
6073 case OP_REPEAT_DEST_REG:
6074 return match_tied_reg_operand (arg, arg->dest_regno);
6075
6076 case OP_REPEAT_PREV_REG:
6077 return match_tied_reg_operand (arg, arg->last_regno);
6078
6079 case OP_PC:
6080 return match_pc_operand (arg);
6081
6082 case OP_VU0_SUFFIX:
6083 return match_vu0_suffix_operand (arg, operand, FALSE);
6084
6085 case OP_VU0_MATCH_SUFFIX:
6086 return match_vu0_suffix_operand (arg, operand, TRUE);
6087
6088 case OP_IMM_INDEX:
6089 return match_imm_index_operand (arg, operand);
6090
6091 case OP_REG_INDEX:
6092 return match_reg_index_operand (arg, operand);
6093
6094 case OP_SAME_RS_RT:
6095 return match_same_rs_rt_operand (arg, operand);
6096
6097 case OP_CHECK_PREV:
6098 return match_check_prev_operand (arg, operand);
6099
6100 case OP_NON_ZERO_REG:
6101 return match_non_zero_reg_operand (arg, operand);
6102 }
6103 abort ();
6104 }
6105
6106 /* ARG is the state after successfully matching an instruction.
6107 Issue any queued-up warnings. */
6108
6109 static void
6110 check_completed_insn (struct mips_arg_info *arg)
6111 {
6112 if (arg->seen_at)
6113 {
6114 if (AT == ATREG)
6115 as_warn (_("used $at without \".set noat\""));
6116 else
6117 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
6118 }
6119 }
6120
6121 /* Return true if modifying general-purpose register REG needs a delay. */
6122
6123 static bfd_boolean
6124 reg_needs_delay (unsigned int reg)
6125 {
6126 unsigned long prev_pinfo;
6127
6128 prev_pinfo = history[0].insn_mo->pinfo;
6129 if (!mips_opts.noreorder
6130 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
6131 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
6132 && (gpr_write_mask (&history[0]) & (1 << reg)))
6133 return TRUE;
6134
6135 return FALSE;
6136 }
6137
6138 /* Classify an instruction according to the FIX_VR4120_* enumeration.
6139 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6140 by VR4120 errata. */
6141
6142 static unsigned int
6143 classify_vr4120_insn (const char *name)
6144 {
6145 if (strncmp (name, "macc", 4) == 0)
6146 return FIX_VR4120_MACC;
6147 if (strncmp (name, "dmacc", 5) == 0)
6148 return FIX_VR4120_DMACC;
6149 if (strncmp (name, "mult", 4) == 0)
6150 return FIX_VR4120_MULT;
6151 if (strncmp (name, "dmult", 5) == 0)
6152 return FIX_VR4120_DMULT;
6153 if (strstr (name, "div"))
6154 return FIX_VR4120_DIV;
6155 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6156 return FIX_VR4120_MTHILO;
6157 return NUM_FIX_VR4120_CLASSES;
6158 }
6159
6160 #define INSN_ERET 0x42000018
6161 #define INSN_DERET 0x4200001f
6162 #define INSN_DMULT 0x1c
6163 #define INSN_DMULTU 0x1d
6164
6165 /* Return the number of instructions that must separate INSN1 and INSN2,
6166 where INSN1 is the earlier instruction. Return the worst-case value
6167 for any INSN2 if INSN2 is null. */
6168
6169 static unsigned int
6170 insns_between (const struct mips_cl_insn *insn1,
6171 const struct mips_cl_insn *insn2)
6172 {
6173 unsigned long pinfo1, pinfo2;
6174 unsigned int mask;
6175
6176 /* If INFO2 is null, pessimistically assume that all flags are set for
6177 the second instruction. */
6178 pinfo1 = insn1->insn_mo->pinfo;
6179 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
6180
6181 /* For most targets, write-after-read dependencies on the HI and LO
6182 registers must be separated by at least two instructions. */
6183 if (!hilo_interlocks)
6184 {
6185 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6186 return 2;
6187 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6188 return 2;
6189 }
6190
6191 /* If we're working around r7000 errata, there must be two instructions
6192 between an mfhi or mflo and any instruction that uses the result. */
6193 if (mips_7000_hilo_fix
6194 && !mips_opts.micromips
6195 && MF_HILO_INSN (pinfo1)
6196 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
6197 return 2;
6198
6199 /* If we're working around 24K errata, one instruction is required
6200 if an ERET or DERET is followed by a branch instruction. */
6201 if (mips_fix_24k && !mips_opts.micromips)
6202 {
6203 if (insn1->insn_opcode == INSN_ERET
6204 || insn1->insn_opcode == INSN_DERET)
6205 {
6206 if (insn2 == NULL
6207 || insn2->insn_opcode == INSN_ERET
6208 || insn2->insn_opcode == INSN_DERET
6209 || delayed_branch_p (insn2))
6210 return 1;
6211 }
6212 }
6213
6214 /* If we're working around PMC RM7000 errata, there must be three
6215 nops between a dmult and a load instruction. */
6216 if (mips_fix_rm7000 && !mips_opts.micromips)
6217 {
6218 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6219 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6220 {
6221 if (pinfo2 & INSN_LOAD_MEMORY)
6222 return 3;
6223 }
6224 }
6225
6226 /* If working around VR4120 errata, check for combinations that need
6227 a single intervening instruction. */
6228 if (mips_fix_vr4120 && !mips_opts.micromips)
6229 {
6230 unsigned int class1, class2;
6231
6232 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6233 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
6234 {
6235 if (insn2 == NULL)
6236 return 1;
6237 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6238 if (vr4120_conflicts[class1] & (1 << class2))
6239 return 1;
6240 }
6241 }
6242
6243 if (!HAVE_CODE_COMPRESSION)
6244 {
6245 /* Check for GPR or coprocessor load delays. All such delays
6246 are on the RT register. */
6247 /* Itbl support may require additional care here. */
6248 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
6249 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
6250 {
6251 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
6252 return 1;
6253 }
6254
6255 /* Check for generic coprocessor hazards.
6256
6257 This case is not handled very well. There is no special
6258 knowledge of CP0 handling, and the coprocessors other than
6259 the floating point unit are not distinguished at all. */
6260 /* Itbl support may require additional care here. FIXME!
6261 Need to modify this to include knowledge about
6262 user specified delays! */
6263 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
6264 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6265 {
6266 /* Handle cases where INSN1 writes to a known general coprocessor
6267 register. There must be a one instruction delay before INSN2
6268 if INSN2 reads that register, otherwise no delay is needed. */
6269 mask = fpr_write_mask (insn1);
6270 if (mask != 0)
6271 {
6272 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
6273 return 1;
6274 }
6275 else
6276 {
6277 /* Read-after-write dependencies on the control registers
6278 require a two-instruction gap. */
6279 if ((pinfo1 & INSN_WRITE_COND_CODE)
6280 && (pinfo2 & INSN_READ_COND_CODE))
6281 return 2;
6282
6283 /* We don't know exactly what INSN1 does. If INSN2 is
6284 also a coprocessor instruction, assume there must be
6285 a one instruction gap. */
6286 if (pinfo2 & INSN_COP)
6287 return 1;
6288 }
6289 }
6290
6291 /* Check for read-after-write dependencies on the coprocessor
6292 control registers in cases where INSN1 does not need a general
6293 coprocessor delay. This means that INSN1 is a floating point
6294 comparison instruction. */
6295 /* Itbl support may require additional care here. */
6296 else if (!cop_interlocks
6297 && (pinfo1 & INSN_WRITE_COND_CODE)
6298 && (pinfo2 & INSN_READ_COND_CODE))
6299 return 1;
6300 }
6301
6302 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6303 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6304 and pause. */
6305 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6306 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6307 || (insn2 && delayed_branch_p (insn2))))
6308 return 1;
6309
6310 return 0;
6311 }
6312
6313 /* Return the number of nops that would be needed to work around the
6314 VR4130 mflo/mfhi errata if instruction INSN immediately followed
6315 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6316 that are contained within the first IGNORE instructions of HIST. */
6317
6318 static int
6319 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
6320 const struct mips_cl_insn *insn)
6321 {
6322 int i, j;
6323 unsigned int mask;
6324
6325 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6326 are not affected by the errata. */
6327 if (insn != 0
6328 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6329 || strcmp (insn->insn_mo->name, "mtlo") == 0
6330 || strcmp (insn->insn_mo->name, "mthi") == 0))
6331 return 0;
6332
6333 /* Search for the first MFLO or MFHI. */
6334 for (i = 0; i < MAX_VR4130_NOPS; i++)
6335 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
6336 {
6337 /* Extract the destination register. */
6338 mask = gpr_write_mask (&hist[i]);
6339
6340 /* No nops are needed if INSN reads that register. */
6341 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
6342 return 0;
6343
6344 /* ...or if any of the intervening instructions do. */
6345 for (j = 0; j < i; j++)
6346 if (gpr_read_mask (&hist[j]) & mask)
6347 return 0;
6348
6349 if (i >= ignore)
6350 return MAX_VR4130_NOPS - i;
6351 }
6352 return 0;
6353 }
6354
6355 #define BASE_REG_EQ(INSN1, INSN2) \
6356 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
6357 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6358
6359 /* Return the minimum alignment for this store instruction. */
6360
6361 static int
6362 fix_24k_align_to (const struct mips_opcode *mo)
6363 {
6364 if (strcmp (mo->name, "sh") == 0)
6365 return 2;
6366
6367 if (strcmp (mo->name, "swc1") == 0
6368 || strcmp (mo->name, "swc2") == 0
6369 || strcmp (mo->name, "sw") == 0
6370 || strcmp (mo->name, "sc") == 0
6371 || strcmp (mo->name, "s.s") == 0)
6372 return 4;
6373
6374 if (strcmp (mo->name, "sdc1") == 0
6375 || strcmp (mo->name, "sdc2") == 0
6376 || strcmp (mo->name, "s.d") == 0)
6377 return 8;
6378
6379 /* sb, swl, swr */
6380 return 1;
6381 }
6382
6383 struct fix_24k_store_info
6384 {
6385 /* Immediate offset, if any, for this store instruction. */
6386 short off;
6387 /* Alignment required by this store instruction. */
6388 int align_to;
6389 /* True for register offsets. */
6390 int register_offset;
6391 };
6392
6393 /* Comparison function used by qsort. */
6394
6395 static int
6396 fix_24k_sort (const void *a, const void *b)
6397 {
6398 const struct fix_24k_store_info *pos1 = a;
6399 const struct fix_24k_store_info *pos2 = b;
6400
6401 return (pos1->off - pos2->off);
6402 }
6403
6404 /* INSN is a store instruction. Try to record the store information
6405 in STINFO. Return false if the information isn't known. */
6406
6407 static bfd_boolean
6408 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
6409 const struct mips_cl_insn *insn)
6410 {
6411 /* The instruction must have a known offset. */
6412 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6413 return FALSE;
6414
6415 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6416 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6417 return TRUE;
6418 }
6419
6420 /* Return the number of nops that would be needed to work around the 24k
6421 "lost data on stores during refill" errata if instruction INSN
6422 immediately followed the 2 instructions described by HIST.
6423 Ignore hazards that are contained within the first IGNORE
6424 instructions of HIST.
6425
6426 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6427 for the data cache refills and store data. The following describes
6428 the scenario where the store data could be lost.
6429
6430 * A data cache miss, due to either a load or a store, causing fill
6431 data to be supplied by the memory subsystem
6432 * The first three doublewords of fill data are returned and written
6433 into the cache
6434 * A sequence of four stores occurs in consecutive cycles around the
6435 final doubleword of the fill:
6436 * Store A
6437 * Store B
6438 * Store C
6439 * Zero, One or more instructions
6440 * Store D
6441
6442 The four stores A-D must be to different doublewords of the line that
6443 is being filled. The fourth instruction in the sequence above permits
6444 the fill of the final doubleword to be transferred from the FSB into
6445 the cache. In the sequence above, the stores may be either integer
6446 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6447 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6448 different doublewords on the line. If the floating point unit is
6449 running in 1:2 mode, it is not possible to create the sequence above
6450 using only floating point store instructions.
6451
6452 In this case, the cache line being filled is incorrectly marked
6453 invalid, thereby losing the data from any store to the line that
6454 occurs between the original miss and the completion of the five
6455 cycle sequence shown above.
6456
6457 The workarounds are:
6458
6459 * Run the data cache in write-through mode.
6460 * Insert a non-store instruction between
6461 Store A and Store B or Store B and Store C. */
6462
6463 static int
6464 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
6465 const struct mips_cl_insn *insn)
6466 {
6467 struct fix_24k_store_info pos[3];
6468 int align, i, base_offset;
6469
6470 if (ignore >= 2)
6471 return 0;
6472
6473 /* If the previous instruction wasn't a store, there's nothing to
6474 worry about. */
6475 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6476 return 0;
6477
6478 /* If the instructions after the previous one are unknown, we have
6479 to assume the worst. */
6480 if (!insn)
6481 return 1;
6482
6483 /* Check whether we are dealing with three consecutive stores. */
6484 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6485 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6486 return 0;
6487
6488 /* If we don't know the relationship between the store addresses,
6489 assume the worst. */
6490 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
6491 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6492 return 1;
6493
6494 if (!fix_24k_record_store_info (&pos[0], insn)
6495 || !fix_24k_record_store_info (&pos[1], &hist[0])
6496 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6497 return 1;
6498
6499 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6500
6501 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6502 X bytes and such that the base register + X is known to be aligned
6503 to align bytes. */
6504
6505 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6506 align = 8;
6507 else
6508 {
6509 align = pos[0].align_to;
6510 base_offset = pos[0].off;
6511 for (i = 1; i < 3; i++)
6512 if (align < pos[i].align_to)
6513 {
6514 align = pos[i].align_to;
6515 base_offset = pos[i].off;
6516 }
6517 for (i = 0; i < 3; i++)
6518 pos[i].off -= base_offset;
6519 }
6520
6521 pos[0].off &= ~align + 1;
6522 pos[1].off &= ~align + 1;
6523 pos[2].off &= ~align + 1;
6524
6525 /* If any two stores write to the same chunk, they also write to the
6526 same doubleword. The offsets are still sorted at this point. */
6527 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6528 return 0;
6529
6530 /* A range of at least 9 bytes is needed for the stores to be in
6531 non-overlapping doublewords. */
6532 if (pos[2].off - pos[0].off <= 8)
6533 return 0;
6534
6535 if (pos[2].off - pos[1].off >= 24
6536 || pos[1].off - pos[0].off >= 24
6537 || pos[2].off - pos[0].off >= 32)
6538 return 0;
6539
6540 return 1;
6541 }
6542
6543 /* Return the number of nops that would be needed if instruction INSN
6544 immediately followed the MAX_NOPS instructions given by HIST,
6545 where HIST[0] is the most recent instruction. Ignore hazards
6546 between INSN and the first IGNORE instructions in HIST.
6547
6548 If INSN is null, return the worse-case number of nops for any
6549 instruction. */
6550
6551 static int
6552 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
6553 const struct mips_cl_insn *insn)
6554 {
6555 int i, nops, tmp_nops;
6556
6557 nops = 0;
6558 for (i = ignore; i < MAX_DELAY_NOPS; i++)
6559 {
6560 tmp_nops = insns_between (hist + i, insn) - i;
6561 if (tmp_nops > nops)
6562 nops = tmp_nops;
6563 }
6564
6565 if (mips_fix_vr4130 && !mips_opts.micromips)
6566 {
6567 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
6568 if (tmp_nops > nops)
6569 nops = tmp_nops;
6570 }
6571
6572 if (mips_fix_24k && !mips_opts.micromips)
6573 {
6574 tmp_nops = nops_for_24k (ignore, hist, insn);
6575 if (tmp_nops > nops)
6576 nops = tmp_nops;
6577 }
6578
6579 return nops;
6580 }
6581
6582 /* The variable arguments provide NUM_INSNS extra instructions that
6583 might be added to HIST. Return the largest number of nops that
6584 would be needed after the extended sequence, ignoring hazards
6585 in the first IGNORE instructions. */
6586
6587 static int
6588 nops_for_sequence (int num_insns, int ignore,
6589 const struct mips_cl_insn *hist, ...)
6590 {
6591 va_list args;
6592 struct mips_cl_insn buffer[MAX_NOPS];
6593 struct mips_cl_insn *cursor;
6594 int nops;
6595
6596 va_start (args, hist);
6597 cursor = buffer + num_insns;
6598 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
6599 while (cursor > buffer)
6600 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6601
6602 nops = nops_for_insn (ignore, buffer, NULL);
6603 va_end (args);
6604 return nops;
6605 }
6606
6607 /* Like nops_for_insn, but if INSN is a branch, take into account the
6608 worst-case delay for the branch target. */
6609
6610 static int
6611 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
6612 const struct mips_cl_insn *insn)
6613 {
6614 int nops, tmp_nops;
6615
6616 nops = nops_for_insn (ignore, hist, insn);
6617 if (delayed_branch_p (insn))
6618 {
6619 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
6620 hist, insn, get_delay_slot_nop (insn));
6621 if (tmp_nops > nops)
6622 nops = tmp_nops;
6623 }
6624 else if (compact_branch_p (insn))
6625 {
6626 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
6627 if (tmp_nops > nops)
6628 nops = tmp_nops;
6629 }
6630 return nops;
6631 }
6632
6633 /* Fix NOP issue: Replace nops by "or at,at,zero". */
6634
6635 static void
6636 fix_loongson2f_nop (struct mips_cl_insn * ip)
6637 {
6638 gas_assert (!HAVE_CODE_COMPRESSION);
6639 if (strcmp (ip->insn_mo->name, "nop") == 0)
6640 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6641 }
6642
6643 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6644 jr target pc &= 'hffff_ffff_cfff_ffff. */
6645
6646 static void
6647 fix_loongson2f_jump (struct mips_cl_insn * ip)
6648 {
6649 gas_assert (!HAVE_CODE_COMPRESSION);
6650 if (strcmp (ip->insn_mo->name, "j") == 0
6651 || strcmp (ip->insn_mo->name, "jr") == 0
6652 || strcmp (ip->insn_mo->name, "jalr") == 0)
6653 {
6654 int sreg;
6655 expressionS ep;
6656
6657 if (! mips_opts.at)
6658 return;
6659
6660 sreg = EXTRACT_OPERAND (0, RS, *ip);
6661 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6662 return;
6663
6664 ep.X_op = O_constant;
6665 ep.X_add_number = 0xcfff0000;
6666 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6667 ep.X_add_number = 0xffff;
6668 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6669 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6670 }
6671 }
6672
6673 static void
6674 fix_loongson2f (struct mips_cl_insn * ip)
6675 {
6676 if (mips_fix_loongson2f_nop)
6677 fix_loongson2f_nop (ip);
6678
6679 if (mips_fix_loongson2f_jump)
6680 fix_loongson2f_jump (ip);
6681 }
6682
6683 /* IP is a branch that has a delay slot, and we need to fill it
6684 automatically. Return true if we can do that by swapping IP
6685 with the previous instruction.
6686 ADDRESS_EXPR is an operand of the instruction to be used with
6687 RELOC_TYPE. */
6688
6689 static bfd_boolean
6690 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
6691 bfd_reloc_code_real_type *reloc_type)
6692 {
6693 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
6694 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
6695 unsigned int fpr_read, prev_fpr_write;
6696
6697 /* -O2 and above is required for this optimization. */
6698 if (mips_optimize < 2)
6699 return FALSE;
6700
6701 /* If we have seen .set volatile or .set nomove, don't optimize. */
6702 if (mips_opts.nomove)
6703 return FALSE;
6704
6705 /* We can't swap if the previous instruction's position is fixed. */
6706 if (history[0].fixed_p)
6707 return FALSE;
6708
6709 /* If the previous previous insn was in a .set noreorder, we can't
6710 swap. Actually, the MIPS assembler will swap in this situation.
6711 However, gcc configured -with-gnu-as will generate code like
6712
6713 .set noreorder
6714 lw $4,XXX
6715 .set reorder
6716 INSN
6717 bne $4,$0,foo
6718
6719 in which we can not swap the bne and INSN. If gcc is not configured
6720 -with-gnu-as, it does not output the .set pseudo-ops. */
6721 if (history[1].noreorder_p)
6722 return FALSE;
6723
6724 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6725 This means that the previous instruction was a 4-byte one anyhow. */
6726 if (mips_opts.mips16 && history[0].fixp[0])
6727 return FALSE;
6728
6729 /* If the branch is itself the target of a branch, we can not swap.
6730 We cheat on this; all we check for is whether there is a label on
6731 this instruction. If there are any branches to anything other than
6732 a label, users must use .set noreorder. */
6733 if (seg_info (now_seg)->label_list)
6734 return FALSE;
6735
6736 /* If the previous instruction is in a variant frag other than this
6737 branch's one, we cannot do the swap. This does not apply to
6738 MIPS16 code, which uses variant frags for different purposes. */
6739 if (!mips_opts.mips16
6740 && history[0].frag
6741 && history[0].frag->fr_type == rs_machine_dependent)
6742 return FALSE;
6743
6744 /* We do not swap with instructions that cannot architecturally
6745 be placed in a branch delay slot, such as SYNC or ERET. We
6746 also refrain from swapping with a trap instruction, since it
6747 complicates trap handlers to have the trap instruction be in
6748 a delay slot. */
6749 prev_pinfo = history[0].insn_mo->pinfo;
6750 if (prev_pinfo & INSN_NO_DELAY_SLOT)
6751 return FALSE;
6752
6753 /* Check for conflicts between the branch and the instructions
6754 before the candidate delay slot. */
6755 if (nops_for_insn (0, history + 1, ip) > 0)
6756 return FALSE;
6757
6758 /* Check for conflicts between the swapped sequence and the
6759 target of the branch. */
6760 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6761 return FALSE;
6762
6763 /* If the branch reads a register that the previous
6764 instruction sets, we can not swap. */
6765 gpr_read = gpr_read_mask (ip);
6766 prev_gpr_write = gpr_write_mask (&history[0]);
6767 if (gpr_read & prev_gpr_write)
6768 return FALSE;
6769
6770 fpr_read = fpr_read_mask (ip);
6771 prev_fpr_write = fpr_write_mask (&history[0]);
6772 if (fpr_read & prev_fpr_write)
6773 return FALSE;
6774
6775 /* If the branch writes a register that the previous
6776 instruction sets, we can not swap. */
6777 gpr_write = gpr_write_mask (ip);
6778 if (gpr_write & prev_gpr_write)
6779 return FALSE;
6780
6781 /* If the branch writes a register that the previous
6782 instruction reads, we can not swap. */
6783 prev_gpr_read = gpr_read_mask (&history[0]);
6784 if (gpr_write & prev_gpr_read)
6785 return FALSE;
6786
6787 /* If one instruction sets a condition code and the
6788 other one uses a condition code, we can not swap. */
6789 pinfo = ip->insn_mo->pinfo;
6790 if ((pinfo & INSN_READ_COND_CODE)
6791 && (prev_pinfo & INSN_WRITE_COND_CODE))
6792 return FALSE;
6793 if ((pinfo & INSN_WRITE_COND_CODE)
6794 && (prev_pinfo & INSN_READ_COND_CODE))
6795 return FALSE;
6796
6797 /* If the previous instruction uses the PC, we can not swap. */
6798 prev_pinfo2 = history[0].insn_mo->pinfo2;
6799 if (prev_pinfo2 & INSN2_READ_PC)
6800 return FALSE;
6801
6802 /* If the previous instruction has an incorrect size for a fixed
6803 branch delay slot in microMIPS mode, we cannot swap. */
6804 pinfo2 = ip->insn_mo->pinfo2;
6805 if (mips_opts.micromips
6806 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6807 && insn_length (history) != 2)
6808 return FALSE;
6809 if (mips_opts.micromips
6810 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6811 && insn_length (history) != 4)
6812 return FALSE;
6813
6814 /* On R5900 short loops need to be fixed by inserting a nop in
6815 the branch delay slots.
6816 A short loop can be terminated too early. */
6817 if (mips_opts.arch == CPU_R5900
6818 /* Check if instruction has a parameter, ignore "j $31". */
6819 && (address_expr != NULL)
6820 /* Parameter must be 16 bit. */
6821 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6822 /* Branch to same segment. */
6823 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
6824 /* Branch to same code fragment. */
6825 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
6826 /* Can only calculate branch offset if value is known. */
6827 && symbol_constant_p (address_expr->X_add_symbol)
6828 /* Check if branch is really conditional. */
6829 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6830 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6831 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6832 {
6833 int distance;
6834 /* Check if loop is shorter than 6 instructions including
6835 branch and delay slot. */
6836 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
6837 if (distance <= 20)
6838 {
6839 int i;
6840 int rv;
6841
6842 rv = FALSE;
6843 /* When the loop includes branches or jumps,
6844 it is not a short loop. */
6845 for (i = 0; i < (distance / 4); i++)
6846 {
6847 if ((history[i].cleared_p)
6848 || delayed_branch_p (&history[i]))
6849 {
6850 rv = TRUE;
6851 break;
6852 }
6853 }
6854 if (rv == FALSE)
6855 {
6856 /* Insert nop after branch to fix short loop. */
6857 return FALSE;
6858 }
6859 }
6860 }
6861
6862 return TRUE;
6863 }
6864
6865 /* Decide how we should add IP to the instruction stream.
6866 ADDRESS_EXPR is an operand of the instruction to be used with
6867 RELOC_TYPE. */
6868
6869 static enum append_method
6870 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
6871 bfd_reloc_code_real_type *reloc_type)
6872 {
6873 /* The relaxed version of a macro sequence must be inherently
6874 hazard-free. */
6875 if (mips_relax.sequence == 2)
6876 return APPEND_ADD;
6877
6878 /* We must not dabble with instructions in a ".set noreorder" block. */
6879 if (mips_opts.noreorder)
6880 return APPEND_ADD;
6881
6882 /* Otherwise, it's our responsibility to fill branch delay slots. */
6883 if (delayed_branch_p (ip))
6884 {
6885 if (!branch_likely_p (ip)
6886 && can_swap_branch_p (ip, address_expr, reloc_type))
6887 return APPEND_SWAP;
6888
6889 if (mips_opts.mips16
6890 && ISA_SUPPORTS_MIPS16E
6891 && gpr_read_mask (ip) != 0)
6892 return APPEND_ADD_COMPACT;
6893
6894 if (mips_opts.micromips
6895 && ((ip->insn_opcode & 0xffe0) == 0x4580
6896 || (!forced_insn_length
6897 && ((ip->insn_opcode & 0xfc00) == 0xcc00
6898 || (ip->insn_opcode & 0xdc00) == 0x8c00))
6899 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
6900 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
6901 return APPEND_ADD_COMPACT;
6902
6903 return APPEND_ADD_WITH_NOP;
6904 }
6905
6906 return APPEND_ADD;
6907 }
6908
6909 /* IP is an instruction whose opcode we have just changed, END points
6910 to the end of the opcode table processed. Point IP->insn_mo to the
6911 new opcode's definition. */
6912
6913 static void
6914 find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
6915 {
6916 const struct mips_opcode *mo;
6917
6918 for (mo = ip->insn_mo; mo < end; mo++)
6919 if (mo->pinfo != INSN_MACRO
6920 && (ip->insn_opcode & mo->mask) == mo->match)
6921 {
6922 ip->insn_mo = mo;
6923 return;
6924 }
6925 abort ();
6926 }
6927
6928 /* IP is a MIPS16 instruction whose opcode we have just changed.
6929 Point IP->insn_mo to the new opcode's definition. */
6930
6931 static void
6932 find_altered_mips16_opcode (struct mips_cl_insn *ip)
6933 {
6934 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
6935 }
6936
6937 /* IP is a microMIPS instruction whose opcode we have just changed.
6938 Point IP->insn_mo to the new opcode's definition. */
6939
6940 static void
6941 find_altered_micromips_opcode (struct mips_cl_insn *ip)
6942 {
6943 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
6944 }
6945
6946 /* For microMIPS macros, we need to generate a local number label
6947 as the target of branches. */
6948 #define MICROMIPS_LABEL_CHAR '\037'
6949 static unsigned long micromips_target_label;
6950 static char micromips_target_name[32];
6951
6952 static char *
6953 micromips_label_name (void)
6954 {
6955 char *p = micromips_target_name;
6956 char symbol_name_temporary[24];
6957 unsigned long l;
6958 int i;
6959
6960 if (*p)
6961 return p;
6962
6963 i = 0;
6964 l = micromips_target_label;
6965 #ifdef LOCAL_LABEL_PREFIX
6966 *p++ = LOCAL_LABEL_PREFIX;
6967 #endif
6968 *p++ = 'L';
6969 *p++ = MICROMIPS_LABEL_CHAR;
6970 do
6971 {
6972 symbol_name_temporary[i++] = l % 10 + '0';
6973 l /= 10;
6974 }
6975 while (l != 0);
6976 while (i > 0)
6977 *p++ = symbol_name_temporary[--i];
6978 *p = '\0';
6979
6980 return micromips_target_name;
6981 }
6982
6983 static void
6984 micromips_label_expr (expressionS *label_expr)
6985 {
6986 label_expr->X_op = O_symbol;
6987 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
6988 label_expr->X_add_number = 0;
6989 }
6990
6991 static void
6992 micromips_label_inc (void)
6993 {
6994 micromips_target_label++;
6995 *micromips_target_name = '\0';
6996 }
6997
6998 static void
6999 micromips_add_label (void)
7000 {
7001 symbolS *s;
7002
7003 s = colon (micromips_label_name ());
7004 micromips_label_inc ();
7005 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
7006 }
7007
7008 /* If assembling microMIPS code, then return the microMIPS reloc
7009 corresponding to the requested one if any. Otherwise return
7010 the reloc unchanged. */
7011
7012 static bfd_reloc_code_real_type
7013 micromips_map_reloc (bfd_reloc_code_real_type reloc)
7014 {
7015 static const bfd_reloc_code_real_type relocs[][2] =
7016 {
7017 /* Keep sorted incrementally by the left-hand key. */
7018 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7019 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7020 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7021 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7022 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7023 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7024 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7025 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7026 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7027 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7028 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7029 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7030 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7031 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7032 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7033 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7034 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7035 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7036 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7037 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7038 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7039 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7040 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7041 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7042 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7043 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7044 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7045 };
7046 bfd_reloc_code_real_type r;
7047 size_t i;
7048
7049 if (!mips_opts.micromips)
7050 return reloc;
7051 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7052 {
7053 r = relocs[i][0];
7054 if (r > reloc)
7055 return reloc;
7056 if (r == reloc)
7057 return relocs[i][1];
7058 }
7059 return reloc;
7060 }
7061
7062 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7063 Return true on success, storing the resolved value in RESULT. */
7064
7065 static bfd_boolean
7066 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7067 offsetT *result)
7068 {
7069 switch (reloc)
7070 {
7071 case BFD_RELOC_MIPS_HIGHEST:
7072 case BFD_RELOC_MICROMIPS_HIGHEST:
7073 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7074 return TRUE;
7075
7076 case BFD_RELOC_MIPS_HIGHER:
7077 case BFD_RELOC_MICROMIPS_HIGHER:
7078 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7079 return TRUE;
7080
7081 case BFD_RELOC_HI16_S:
7082 case BFD_RELOC_HI16_S_PCREL:
7083 case BFD_RELOC_MICROMIPS_HI16_S:
7084 case BFD_RELOC_MIPS16_HI16_S:
7085 *result = ((operand + 0x8000) >> 16) & 0xffff;
7086 return TRUE;
7087
7088 case BFD_RELOC_HI16:
7089 case BFD_RELOC_MICROMIPS_HI16:
7090 case BFD_RELOC_MIPS16_HI16:
7091 *result = (operand >> 16) & 0xffff;
7092 return TRUE;
7093
7094 case BFD_RELOC_LO16:
7095 case BFD_RELOC_LO16_PCREL:
7096 case BFD_RELOC_MICROMIPS_LO16:
7097 case BFD_RELOC_MIPS16_LO16:
7098 *result = operand & 0xffff;
7099 return TRUE;
7100
7101 case BFD_RELOC_UNUSED:
7102 *result = operand;
7103 return TRUE;
7104
7105 default:
7106 return FALSE;
7107 }
7108 }
7109
7110 /* Output an instruction. IP is the instruction information.
7111 ADDRESS_EXPR is an operand of the instruction to be used with
7112 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7113 a macro expansion. */
7114
7115 static void
7116 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
7117 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
7118 {
7119 unsigned long prev_pinfo2, pinfo;
7120 bfd_boolean relaxed_branch = FALSE;
7121 enum append_method method;
7122 bfd_boolean relax32;
7123 int branch_disp;
7124
7125 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
7126 fix_loongson2f (ip);
7127
7128 file_ase_mips16 |= mips_opts.mips16;
7129 file_ase_micromips |= mips_opts.micromips;
7130
7131 prev_pinfo2 = history[0].insn_mo->pinfo2;
7132 pinfo = ip->insn_mo->pinfo;
7133
7134 /* Don't raise alarm about `nods' frags as they'll fill in the right
7135 kind of nop in relaxation if required. */
7136 if (mips_opts.micromips
7137 && !expansionp
7138 && !(history[0].frag
7139 && history[0].frag->fr_type == rs_machine_dependent
7140 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7141 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
7142 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7143 && micromips_insn_length (ip->insn_mo) != 2)
7144 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7145 && micromips_insn_length (ip->insn_mo) != 4)))
7146 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
7147 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
7148
7149 if (address_expr == NULL)
7150 ip->complete_p = 1;
7151 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7152 && reloc_type[1] == BFD_RELOC_UNUSED
7153 && reloc_type[2] == BFD_RELOC_UNUSED
7154 && address_expr->X_op == O_constant)
7155 {
7156 switch (*reloc_type)
7157 {
7158 case BFD_RELOC_MIPS_JMP:
7159 {
7160 int shift;
7161
7162 /* Shift is 2, unusually, for microMIPS JALX. */
7163 shift = (mips_opts.micromips
7164 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
7165 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7166 as_bad (_("jump to misaligned address (0x%lx)"),
7167 (unsigned long) address_expr->X_add_number);
7168 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7169 & 0x3ffffff);
7170 ip->complete_p = 1;
7171 }
7172 break;
7173
7174 case BFD_RELOC_MIPS16_JMP:
7175 if ((address_expr->X_add_number & 3) != 0)
7176 as_bad (_("jump to misaligned address (0x%lx)"),
7177 (unsigned long) address_expr->X_add_number);
7178 ip->insn_opcode |=
7179 (((address_expr->X_add_number & 0x7c0000) << 3)
7180 | ((address_expr->X_add_number & 0xf800000) >> 7)
7181 | ((address_expr->X_add_number & 0x3fffc) >> 2));
7182 ip->complete_p = 1;
7183 break;
7184
7185 case BFD_RELOC_16_PCREL_S2:
7186 {
7187 int shift;
7188
7189 shift = mips_opts.micromips ? 1 : 2;
7190 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7191 as_bad (_("branch to misaligned address (0x%lx)"),
7192 (unsigned long) address_expr->X_add_number);
7193 if (!mips_relax_branch)
7194 {
7195 if ((address_expr->X_add_number + (1 << (shift + 15)))
7196 & ~((1 << (shift + 16)) - 1))
7197 as_bad (_("branch address range overflow (0x%lx)"),
7198 (unsigned long) address_expr->X_add_number);
7199 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7200 & 0xffff);
7201 }
7202 }
7203 break;
7204
7205 case BFD_RELOC_MIPS_21_PCREL_S2:
7206 {
7207 int shift;
7208
7209 shift = 2;
7210 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7211 as_bad (_("branch to misaligned address (0x%lx)"),
7212 (unsigned long) address_expr->X_add_number);
7213 if ((address_expr->X_add_number + (1 << (shift + 20)))
7214 & ~((1 << (shift + 21)) - 1))
7215 as_bad (_("branch address range overflow (0x%lx)"),
7216 (unsigned long) address_expr->X_add_number);
7217 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7218 & 0x1fffff);
7219 }
7220 break;
7221
7222 case BFD_RELOC_MIPS_26_PCREL_S2:
7223 {
7224 int shift;
7225
7226 shift = 2;
7227 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7228 as_bad (_("branch to misaligned address (0x%lx)"),
7229 (unsigned long) address_expr->X_add_number);
7230 if ((address_expr->X_add_number + (1 << (shift + 25)))
7231 & ~((1 << (shift + 26)) - 1))
7232 as_bad (_("branch address range overflow (0x%lx)"),
7233 (unsigned long) address_expr->X_add_number);
7234 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7235 & 0x3ffffff);
7236 }
7237 break;
7238
7239 default:
7240 {
7241 offsetT value;
7242
7243 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7244 &value))
7245 {
7246 ip->insn_opcode |= value & 0xffff;
7247 ip->complete_p = 1;
7248 }
7249 }
7250 break;
7251 }
7252 }
7253
7254 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7255 {
7256 /* There are a lot of optimizations we could do that we don't.
7257 In particular, we do not, in general, reorder instructions.
7258 If you use gcc with optimization, it will reorder
7259 instructions and generally do much more optimization then we
7260 do here; repeating all that work in the assembler would only
7261 benefit hand written assembly code, and does not seem worth
7262 it. */
7263 int nops = (mips_optimize == 0
7264 ? nops_for_insn (0, history, NULL)
7265 : nops_for_insn_or_target (0, history, ip));
7266 if (nops > 0)
7267 {
7268 fragS *old_frag;
7269 unsigned long old_frag_offset;
7270 int i;
7271
7272 old_frag = frag_now;
7273 old_frag_offset = frag_now_fix ();
7274
7275 for (i = 0; i < nops; i++)
7276 add_fixed_insn (NOP_INSN);
7277 insert_into_history (0, nops, NOP_INSN);
7278
7279 if (listing)
7280 {
7281 listing_prev_line ();
7282 /* We may be at the start of a variant frag. In case we
7283 are, make sure there is enough space for the frag
7284 after the frags created by listing_prev_line. The
7285 argument to frag_grow here must be at least as large
7286 as the argument to all other calls to frag_grow in
7287 this file. We don't have to worry about being in the
7288 middle of a variant frag, because the variants insert
7289 all needed nop instructions themselves. */
7290 frag_grow (40);
7291 }
7292
7293 mips_move_text_labels ();
7294
7295 #ifndef NO_ECOFF_DEBUGGING
7296 if (ECOFF_DEBUGGING)
7297 ecoff_fix_loc (old_frag, old_frag_offset);
7298 #endif
7299 }
7300 }
7301 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7302 {
7303 int nops;
7304
7305 /* Work out how many nops in prev_nop_frag are needed by IP,
7306 ignoring hazards generated by the first prev_nop_frag_since
7307 instructions. */
7308 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
7309 gas_assert (nops <= prev_nop_frag_holds);
7310
7311 /* Enforce NOPS as a minimum. */
7312 if (nops > prev_nop_frag_required)
7313 prev_nop_frag_required = nops;
7314
7315 if (prev_nop_frag_holds == prev_nop_frag_required)
7316 {
7317 /* Settle for the current number of nops. Update the history
7318 accordingly (for the benefit of any future .set reorder code). */
7319 prev_nop_frag = NULL;
7320 insert_into_history (prev_nop_frag_since,
7321 prev_nop_frag_holds, NOP_INSN);
7322 }
7323 else
7324 {
7325 /* Allow this instruction to replace one of the nops that was
7326 tentatively added to prev_nop_frag. */
7327 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
7328 prev_nop_frag_holds--;
7329 prev_nop_frag_since++;
7330 }
7331 }
7332
7333 method = get_append_method (ip, address_expr, reloc_type);
7334 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
7335
7336 dwarf2_emit_insn (0);
7337 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7338 so "move" the instruction address accordingly.
7339
7340 Also, it doesn't seem appropriate for the assembler to reorder .loc
7341 entries. If this instruction is a branch that we are going to swap
7342 with the previous instruction, the two instructions should be
7343 treated as a unit, and the debug information for both instructions
7344 should refer to the start of the branch sequence. Using the
7345 current position is certainly wrong when swapping a 32-bit branch
7346 and a 16-bit delay slot, since the current position would then be
7347 in the middle of a branch. */
7348 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
7349
7350 relax32 = (mips_relax_branch
7351 /* Don't try branch relaxation within .set nomacro, or within
7352 .set noat if we use $at for PIC computations. If it turns
7353 out that the branch was out-of-range, we'll get an error. */
7354 && !mips_opts.warn_about_macros
7355 && (mips_opts.at || mips_pic == NO_PIC)
7356 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7357 as they have no complementing branches. */
7358 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
7359
7360 if (!HAVE_CODE_COMPRESSION
7361 && address_expr
7362 && relax32
7363 && *reloc_type == BFD_RELOC_16_PCREL_S2
7364 && delayed_branch_p (ip))
7365 {
7366 relaxed_branch = TRUE;
7367 add_relaxed_insn (ip, (relaxed_branch_length
7368 (NULL, NULL,
7369 uncond_branch_p (ip) ? -1
7370 : branch_likely_p (ip) ? 1
7371 : 0)), 4,
7372 RELAX_BRANCH_ENCODE
7373 (AT, mips_pic != NO_PIC,
7374 uncond_branch_p (ip),
7375 branch_likely_p (ip),
7376 pinfo & INSN_WRITE_GPR_31,
7377 0),
7378 address_expr->X_add_symbol,
7379 address_expr->X_add_number);
7380 *reloc_type = BFD_RELOC_UNUSED;
7381 }
7382 else if (mips_opts.micromips
7383 && address_expr
7384 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7385 || *reloc_type > BFD_RELOC_UNUSED)
7386 && (delayed_branch_p (ip) || compact_branch_p (ip))
7387 /* Don't try branch relaxation when users specify
7388 16-bit/32-bit instructions. */
7389 && !forced_insn_length)
7390 {
7391 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7392 && *reloc_type > BFD_RELOC_UNUSED);
7393 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
7394 int uncond = uncond_branch_p (ip) ? -1 : 0;
7395 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7396 int nods = method == APPEND_ADD_WITH_NOP;
7397 int al = pinfo & INSN_WRITE_GPR_31;
7398 int length32 = nods ? 8 : 4;
7399
7400 gas_assert (address_expr != NULL);
7401 gas_assert (!mips_relax.sequence);
7402
7403 relaxed_branch = TRUE;
7404 if (nods)
7405 method = APPEND_ADD;
7406 if (relax32)
7407 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7408 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
7409 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
7410 mips_pic != NO_PIC,
7411 uncond, compact, al, nods,
7412 relax32, 0, 0),
7413 address_expr->X_add_symbol,
7414 address_expr->X_add_number);
7415 *reloc_type = BFD_RELOC_UNUSED;
7416 }
7417 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
7418 {
7419 bfd_boolean require_unextended;
7420 bfd_boolean require_extended;
7421 symbolS *symbol;
7422 offsetT offset;
7423
7424 if (forced_insn_length != 0)
7425 {
7426 require_unextended = forced_insn_length == 2;
7427 require_extended = forced_insn_length == 4;
7428 }
7429 else
7430 {
7431 require_unextended = (mips_opts.noautoextend
7432 && !mips_opcode_32bit_p (ip->insn_mo));
7433 require_extended = 0;
7434 }
7435
7436 /* We need to set up a variant frag. */
7437 gas_assert (address_expr != NULL);
7438 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7439 symbol created by `make_expr_symbol' may not get a necessary
7440 external relocation produced. */
7441 if (address_expr->X_op == O_symbol)
7442 {
7443 symbol = address_expr->X_add_symbol;
7444 offset = address_expr->X_add_number;
7445 }
7446 else
7447 {
7448 symbol = make_expr_symbol (address_expr);
7449 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
7450 offset = 0;
7451 }
7452 add_relaxed_insn (ip, 12, 0,
7453 RELAX_MIPS16_ENCODE
7454 (*reloc_type - BFD_RELOC_UNUSED,
7455 mips_pic != NO_PIC,
7456 HAVE_32BIT_SYMBOLS,
7457 mips_opts.warn_about_macros,
7458 require_unextended, require_extended,
7459 delayed_branch_p (&history[0]),
7460 history[0].mips16_absolute_jump_p),
7461 symbol, offset);
7462 }
7463 else if (mips_opts.mips16 && insn_length (ip) == 2)
7464 {
7465 if (!delayed_branch_p (ip))
7466 /* Make sure there is enough room to swap this instruction with
7467 a following jump instruction. */
7468 frag_grow (6);
7469 add_fixed_insn (ip);
7470 }
7471 else
7472 {
7473 if (mips_opts.mips16
7474 && mips_opts.noreorder
7475 && delayed_branch_p (&history[0]))
7476 as_warn (_("extended instruction in delay slot"));
7477
7478 if (mips_relax.sequence)
7479 {
7480 /* If we've reached the end of this frag, turn it into a variant
7481 frag and record the information for the instructions we've
7482 written so far. */
7483 if (frag_room () < 4)
7484 relax_close_frag ();
7485 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
7486 }
7487
7488 if (mips_relax.sequence != 2)
7489 {
7490 if (mips_macro_warning.first_insn_sizes[0] == 0)
7491 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7492 mips_macro_warning.sizes[0] += insn_length (ip);
7493 mips_macro_warning.insns[0]++;
7494 }
7495 if (mips_relax.sequence != 1)
7496 {
7497 if (mips_macro_warning.first_insn_sizes[1] == 0)
7498 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7499 mips_macro_warning.sizes[1] += insn_length (ip);
7500 mips_macro_warning.insns[1]++;
7501 }
7502
7503 if (mips_opts.mips16)
7504 {
7505 ip->fixed_p = 1;
7506 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7507 }
7508 add_fixed_insn (ip);
7509 }
7510
7511 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
7512 {
7513 bfd_reloc_code_real_type final_type[3];
7514 reloc_howto_type *howto0;
7515 reloc_howto_type *howto;
7516 int i;
7517
7518 /* Perform any necessary conversion to microMIPS relocations
7519 and find out how many relocations there actually are. */
7520 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7521 final_type[i] = micromips_map_reloc (reloc_type[i]);
7522
7523 /* In a compound relocation, it is the final (outermost)
7524 operator that determines the relocated field. */
7525 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
7526 if (!howto)
7527 abort ();
7528
7529 if (i > 1)
7530 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
7531 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7532 bfd_get_reloc_size (howto),
7533 address_expr,
7534 howto0 && howto0->pc_relative,
7535 final_type[0]);
7536 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7537 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
7538
7539 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
7540 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
7541 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7542
7543 /* These relocations can have an addend that won't fit in
7544 4 octets for 64bit assembly. */
7545 if (GPR_SIZE == 64
7546 && ! howto->partial_inplace
7547 && (reloc_type[0] == BFD_RELOC_16
7548 || reloc_type[0] == BFD_RELOC_32
7549 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7550 || reloc_type[0] == BFD_RELOC_GPREL16
7551 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7552 || reloc_type[0] == BFD_RELOC_GPREL32
7553 || reloc_type[0] == BFD_RELOC_64
7554 || reloc_type[0] == BFD_RELOC_CTOR
7555 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7556 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7557 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7558 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7559 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7560 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7561 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7562 || hi16_reloc_p (reloc_type[0])
7563 || lo16_reloc_p (reloc_type[0])))
7564 ip->fixp[0]->fx_no_overflow = 1;
7565
7566 /* These relocations can have an addend that won't fit in 2 octets. */
7567 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7568 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7569 ip->fixp[0]->fx_no_overflow = 1;
7570
7571 if (mips_relax.sequence)
7572 {
7573 if (mips_relax.first_fixup == 0)
7574 mips_relax.first_fixup = ip->fixp[0];
7575 }
7576 else if (reloc_needs_lo_p (*reloc_type))
7577 {
7578 struct mips_hi_fixup *hi_fixup;
7579
7580 /* Reuse the last entry if it already has a matching %lo. */
7581 hi_fixup = mips_hi_fixup_list;
7582 if (hi_fixup == 0
7583 || !fixup_has_matching_lo_p (hi_fixup->fixp))
7584 {
7585 hi_fixup = XNEW (struct mips_hi_fixup);
7586 hi_fixup->next = mips_hi_fixup_list;
7587 mips_hi_fixup_list = hi_fixup;
7588 }
7589 hi_fixup->fixp = ip->fixp[0];
7590 hi_fixup->seg = now_seg;
7591 }
7592
7593 /* Add fixups for the second and third relocations, if given.
7594 Note that the ABI allows the second relocation to be
7595 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7596 moment we only use RSS_UNDEF, but we could add support
7597 for the others if it ever becomes necessary. */
7598 for (i = 1; i < 3; i++)
7599 if (reloc_type[i] != BFD_RELOC_UNUSED)
7600 {
7601 ip->fixp[i] = fix_new (ip->frag, ip->where,
7602 ip->fixp[0]->fx_size, NULL, 0,
7603 FALSE, final_type[i]);
7604
7605 /* Use fx_tcbit to mark compound relocs. */
7606 ip->fixp[0]->fx_tcbit = 1;
7607 ip->fixp[i]->fx_tcbit = 1;
7608 }
7609 }
7610
7611 /* Update the register mask information. */
7612 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7613 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
7614
7615 switch (method)
7616 {
7617 case APPEND_ADD:
7618 insert_into_history (0, 1, ip);
7619 break;
7620
7621 case APPEND_ADD_WITH_NOP:
7622 {
7623 struct mips_cl_insn *nop;
7624
7625 insert_into_history (0, 1, ip);
7626 nop = get_delay_slot_nop (ip);
7627 add_fixed_insn (nop);
7628 insert_into_history (0, 1, nop);
7629 if (mips_relax.sequence)
7630 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7631 }
7632 break;
7633
7634 case APPEND_ADD_COMPACT:
7635 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7636 if (mips_opts.mips16)
7637 {
7638 ip->insn_opcode |= 0x0080;
7639 find_altered_mips16_opcode (ip);
7640 }
7641 /* Convert microMIPS instructions. */
7642 else if (mips_opts.micromips)
7643 {
7644 /* jr16->jrc */
7645 if ((ip->insn_opcode & 0xffe0) == 0x4580)
7646 ip->insn_opcode |= 0x0020;
7647 /* b16->bc */
7648 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7649 ip->insn_opcode = 0x40e00000;
7650 /* beqz16->beqzc, bnez16->bnezc */
7651 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7652 {
7653 unsigned long regno;
7654
7655 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7656 regno &= MICROMIPSOP_MASK_MD;
7657 regno = micromips_to_32_reg_d_map[regno];
7658 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
7659 | (regno << MICROMIPSOP_SH_RS)
7660 | 0x40a00000) ^ 0x00400000;
7661 }
7662 /* beqz->beqzc, bnez->bnezc */
7663 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
7664 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
7665 | ((ip->insn_opcode >> 7) & 0x00400000)
7666 | 0x40a00000) ^ 0x00400000;
7667 /* beq $0->beqzc, bne $0->bnezc */
7668 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
7669 ip->insn_opcode = (((ip->insn_opcode >>
7670 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
7671 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
7672 | ((ip->insn_opcode >> 7) & 0x00400000)
7673 | 0x40a00000) ^ 0x00400000;
7674 else
7675 abort ();
7676 find_altered_micromips_opcode (ip);
7677 }
7678 else
7679 abort ();
7680 install_insn (ip);
7681 insert_into_history (0, 1, ip);
7682 break;
7683
7684 case APPEND_SWAP:
7685 {
7686 struct mips_cl_insn delay = history[0];
7687
7688 if (relaxed_branch || delay.frag != ip->frag)
7689 {
7690 /* Add the delay slot instruction to the end of the
7691 current frag and shrink the fixed part of the
7692 original frag. If the branch occupies the tail of
7693 the latter, move it backwards to cover the gap. */
7694 delay.frag->fr_fix -= branch_disp;
7695 if (delay.frag == ip->frag)
7696 move_insn (ip, ip->frag, ip->where - branch_disp);
7697 add_fixed_insn (&delay);
7698 }
7699 else
7700 {
7701 /* If this is not a relaxed branch and we are in the
7702 same frag, then just swap the instructions. */
7703 move_insn (ip, delay.frag, delay.where);
7704 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
7705 }
7706 history[0] = *ip;
7707 delay.fixed_p = 1;
7708 insert_into_history (0, 1, &delay);
7709 }
7710 break;
7711 }
7712
7713 /* If we have just completed an unconditional branch, clear the history. */
7714 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7715 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
7716 {
7717 unsigned int i;
7718
7719 mips_no_prev_insn ();
7720
7721 for (i = 0; i < ARRAY_SIZE (history); i++)
7722 history[i].cleared_p = 1;
7723 }
7724
7725 /* We need to emit a label at the end of branch-likely macros. */
7726 if (emit_branch_likely_macro)
7727 {
7728 emit_branch_likely_macro = FALSE;
7729 micromips_add_label ();
7730 }
7731
7732 /* We just output an insn, so the next one doesn't have a label. */
7733 mips_clear_insn_labels ();
7734 }
7735
7736 /* Forget that there was any previous instruction or label.
7737 When BRANCH is true, the branch history is also flushed. */
7738
7739 static void
7740 mips_no_prev_insn (void)
7741 {
7742 prev_nop_frag = NULL;
7743 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
7744 mips_clear_insn_labels ();
7745 }
7746
7747 /* This function must be called before we emit something other than
7748 instructions. It is like mips_no_prev_insn except that it inserts
7749 any NOPS that might be needed by previous instructions. */
7750
7751 void
7752 mips_emit_delays (void)
7753 {
7754 if (! mips_opts.noreorder)
7755 {
7756 int nops = nops_for_insn (0, history, NULL);
7757 if (nops > 0)
7758 {
7759 while (nops-- > 0)
7760 add_fixed_insn (NOP_INSN);
7761 mips_move_text_labels ();
7762 }
7763 }
7764 mips_no_prev_insn ();
7765 }
7766
7767 /* Start a (possibly nested) noreorder block. */
7768
7769 static void
7770 start_noreorder (void)
7771 {
7772 if (mips_opts.noreorder == 0)
7773 {
7774 unsigned int i;
7775 int nops;
7776
7777 /* None of the instructions before the .set noreorder can be moved. */
7778 for (i = 0; i < ARRAY_SIZE (history); i++)
7779 history[i].fixed_p = 1;
7780
7781 /* Insert any nops that might be needed between the .set noreorder
7782 block and the previous instructions. We will later remove any
7783 nops that turn out not to be needed. */
7784 nops = nops_for_insn (0, history, NULL);
7785 if (nops > 0)
7786 {
7787 if (mips_optimize != 0)
7788 {
7789 /* Record the frag which holds the nop instructions, so
7790 that we can remove them if we don't need them. */
7791 frag_grow (nops * NOP_INSN_SIZE);
7792 prev_nop_frag = frag_now;
7793 prev_nop_frag_holds = nops;
7794 prev_nop_frag_required = 0;
7795 prev_nop_frag_since = 0;
7796 }
7797
7798 for (; nops > 0; --nops)
7799 add_fixed_insn (NOP_INSN);
7800
7801 /* Move on to a new frag, so that it is safe to simply
7802 decrease the size of prev_nop_frag. */
7803 frag_wane (frag_now);
7804 frag_new (0);
7805 mips_move_text_labels ();
7806 }
7807 mips_mark_labels ();
7808 mips_clear_insn_labels ();
7809 }
7810 mips_opts.noreorder++;
7811 mips_any_noreorder = 1;
7812 }
7813
7814 /* End a nested noreorder block. */
7815
7816 static void
7817 end_noreorder (void)
7818 {
7819 mips_opts.noreorder--;
7820 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7821 {
7822 /* Commit to inserting prev_nop_frag_required nops and go back to
7823 handling nop insertion the .set reorder way. */
7824 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
7825 * NOP_INSN_SIZE);
7826 insert_into_history (prev_nop_frag_since,
7827 prev_nop_frag_required, NOP_INSN);
7828 prev_nop_frag = NULL;
7829 }
7830 }
7831
7832 /* Sign-extend 32-bit mode constants that have bit 31 set and all
7833 higher bits unset. */
7834
7835 static void
7836 normalize_constant_expr (expressionS *ex)
7837 {
7838 if (ex->X_op == O_constant
7839 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7840 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7841 - 0x80000000);
7842 }
7843
7844 /* Sign-extend 32-bit mode address offsets that have bit 31 set and
7845 all higher bits unset. */
7846
7847 static void
7848 normalize_address_expr (expressionS *ex)
7849 {
7850 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7851 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7852 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7853 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7854 - 0x80000000);
7855 }
7856
7857 /* Try to match TOKENS against OPCODE, storing the result in INSN.
7858 Return true if the match was successful.
7859
7860 OPCODE_EXTRA is a value that should be ORed into the opcode
7861 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
7862 there are more alternatives after OPCODE and SOFT_MATCH is
7863 as for mips_arg_info. */
7864
7865 static bfd_boolean
7866 match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
7867 struct mips_operand_token *tokens, unsigned int opcode_extra,
7868 bfd_boolean lax_match, bfd_boolean complete_p)
7869 {
7870 const char *args;
7871 struct mips_arg_info arg;
7872 const struct mips_operand *operand;
7873 char c;
7874
7875 imm_expr.X_op = O_absent;
7876 offset_expr.X_op = O_absent;
7877 offset_reloc[0] = BFD_RELOC_UNUSED;
7878 offset_reloc[1] = BFD_RELOC_UNUSED;
7879 offset_reloc[2] = BFD_RELOC_UNUSED;
7880
7881 create_insn (insn, opcode);
7882 /* When no opcode suffix is specified, assume ".xyzw". */
7883 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
7884 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
7885 else
7886 insn->insn_opcode |= opcode_extra;
7887 memset (&arg, 0, sizeof (arg));
7888 arg.insn = insn;
7889 arg.token = tokens;
7890 arg.argnum = 1;
7891 arg.last_regno = ILLEGAL_REG;
7892 arg.dest_regno = ILLEGAL_REG;
7893 arg.lax_match = lax_match;
7894 for (args = opcode->args;; ++args)
7895 {
7896 if (arg.token->type == OT_END)
7897 {
7898 /* Handle unary instructions in which only one operand is given.
7899 The source is then the same as the destination. */
7900 if (arg.opnum == 1 && *args == ',')
7901 {
7902 operand = (mips_opts.micromips
7903 ? decode_micromips_operand (args + 1)
7904 : decode_mips_operand (args + 1));
7905 if (operand && mips_optional_operand_p (operand))
7906 {
7907 arg.token = tokens;
7908 arg.argnum = 1;
7909 continue;
7910 }
7911 }
7912
7913 /* Treat elided base registers as $0. */
7914 if (strcmp (args, "(b)") == 0)
7915 args += 3;
7916
7917 if (args[0] == '+')
7918 switch (args[1])
7919 {
7920 case 'K':
7921 case 'N':
7922 /* The register suffix is optional. */
7923 args += 2;
7924 break;
7925 }
7926
7927 /* Fail the match if there were too few operands. */
7928 if (*args)
7929 return FALSE;
7930
7931 /* Successful match. */
7932 if (!complete_p)
7933 return TRUE;
7934 clear_insn_error ();
7935 if (arg.dest_regno == arg.last_regno
7936 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
7937 {
7938 if (arg.opnum == 2)
7939 set_insn_error
7940 (0, _("source and destination must be different"));
7941 else if (arg.last_regno == 31)
7942 set_insn_error
7943 (0, _("a destination register must be supplied"));
7944 }
7945 else if (arg.last_regno == 31
7946 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
7947 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
7948 set_insn_error (0, _("the source register must not be $31"));
7949 check_completed_insn (&arg);
7950 return TRUE;
7951 }
7952
7953 /* Fail the match if the line has too many operands. */
7954 if (*args == 0)
7955 return FALSE;
7956
7957 /* Handle characters that need to match exactly. */
7958 if (*args == '(' || *args == ')' || *args == ',')
7959 {
7960 if (match_char (&arg, *args))
7961 continue;
7962 return FALSE;
7963 }
7964 if (*args == '#')
7965 {
7966 ++args;
7967 if (arg.token->type == OT_DOUBLE_CHAR
7968 && arg.token->u.ch == *args)
7969 {
7970 ++arg.token;
7971 continue;
7972 }
7973 return FALSE;
7974 }
7975
7976 /* Handle special macro operands. Work out the properties of
7977 other operands. */
7978 arg.opnum += 1;
7979 switch (*args)
7980 {
7981 case '-':
7982 switch (args[1])
7983 {
7984 case 'A':
7985 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
7986 break;
7987
7988 case 'B':
7989 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
7990 break;
7991 }
7992 break;
7993
7994 case '+':
7995 switch (args[1])
7996 {
7997 case 'i':
7998 *offset_reloc = BFD_RELOC_MIPS_JMP;
7999 break;
8000
8001 case '\'':
8002 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8003 break;
8004
8005 case '\"':
8006 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8007 break;
8008 }
8009 break;
8010
8011 case 'I':
8012 if (!match_const_int (&arg, &imm_expr.X_add_number))
8013 return FALSE;
8014 imm_expr.X_op = O_constant;
8015 if (GPR_SIZE == 32)
8016 normalize_constant_expr (&imm_expr);
8017 continue;
8018
8019 case 'A':
8020 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8021 {
8022 /* Assume that the offset has been elided and that what
8023 we saw was a base register. The match will fail later
8024 if that assumption turns out to be wrong. */
8025 offset_expr.X_op = O_constant;
8026 offset_expr.X_add_number = 0;
8027 }
8028 else
8029 {
8030 if (!match_expression (&arg, &offset_expr, offset_reloc))
8031 return FALSE;
8032 normalize_address_expr (&offset_expr);
8033 }
8034 continue;
8035
8036 case 'F':
8037 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8038 8, TRUE))
8039 return FALSE;
8040 continue;
8041
8042 case 'L':
8043 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8044 8, FALSE))
8045 return FALSE;
8046 continue;
8047
8048 case 'f':
8049 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8050 4, TRUE))
8051 return FALSE;
8052 continue;
8053
8054 case 'l':
8055 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8056 4, FALSE))
8057 return FALSE;
8058 continue;
8059
8060 case 'p':
8061 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8062 break;
8063
8064 case 'a':
8065 *offset_reloc = BFD_RELOC_MIPS_JMP;
8066 break;
8067
8068 case 'm':
8069 gas_assert (mips_opts.micromips);
8070 c = args[1];
8071 switch (c)
8072 {
8073 case 'D':
8074 case 'E':
8075 if (!forced_insn_length)
8076 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8077 else if (c == 'D')
8078 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8079 else
8080 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8081 break;
8082 }
8083 break;
8084 }
8085
8086 operand = (mips_opts.micromips
8087 ? decode_micromips_operand (args)
8088 : decode_mips_operand (args));
8089 if (!operand)
8090 abort ();
8091
8092 /* Skip prefixes. */
8093 if (*args == '+' || *args == 'm' || *args == '-')
8094 args++;
8095
8096 if (mips_optional_operand_p (operand)
8097 && args[1] == ','
8098 && (arg.token[0].type != OT_REG
8099 || arg.token[1].type == OT_END))
8100 {
8101 /* Assume that the register has been elided and is the
8102 same as the first operand. */
8103 arg.token = tokens;
8104 arg.argnum = 1;
8105 }
8106
8107 if (!match_operand (&arg, operand))
8108 return FALSE;
8109 }
8110 }
8111
8112 /* Like match_insn, but for MIPS16. */
8113
8114 static bfd_boolean
8115 match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8116 struct mips_operand_token *tokens)
8117 {
8118 const char *args;
8119 const struct mips_operand *operand;
8120 const struct mips_operand *ext_operand;
8121 bfd_boolean pcrel = FALSE;
8122 int required_insn_length;
8123 struct mips_arg_info arg;
8124 int relax_char;
8125
8126 if (forced_insn_length)
8127 required_insn_length = forced_insn_length;
8128 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8129 required_insn_length = 2;
8130 else
8131 required_insn_length = 0;
8132
8133 create_insn (insn, opcode);
8134 imm_expr.X_op = O_absent;
8135 offset_expr.X_op = O_absent;
8136 offset_reloc[0] = BFD_RELOC_UNUSED;
8137 offset_reloc[1] = BFD_RELOC_UNUSED;
8138 offset_reloc[2] = BFD_RELOC_UNUSED;
8139 relax_char = 0;
8140
8141 memset (&arg, 0, sizeof (arg));
8142 arg.insn = insn;
8143 arg.token = tokens;
8144 arg.argnum = 1;
8145 arg.last_regno = ILLEGAL_REG;
8146 arg.dest_regno = ILLEGAL_REG;
8147 relax_char = 0;
8148 for (args = opcode->args;; ++args)
8149 {
8150 int c;
8151
8152 if (arg.token->type == OT_END)
8153 {
8154 offsetT value;
8155
8156 /* Handle unary instructions in which only one operand is given.
8157 The source is then the same as the destination. */
8158 if (arg.opnum == 1 && *args == ',')
8159 {
8160 operand = decode_mips16_operand (args[1], FALSE);
8161 if (operand && mips_optional_operand_p (operand))
8162 {
8163 arg.token = tokens;
8164 arg.argnum = 1;
8165 continue;
8166 }
8167 }
8168
8169 /* Fail the match if there were too few operands. */
8170 if (*args)
8171 return FALSE;
8172
8173 /* Successful match. Stuff the immediate value in now, if
8174 we can. */
8175 clear_insn_error ();
8176 if (opcode->pinfo == INSN_MACRO)
8177 {
8178 gas_assert (relax_char == 0 || relax_char == 'p');
8179 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8180 }
8181 else if (relax_char
8182 && offset_expr.X_op == O_constant
8183 && !pcrel
8184 && calculate_reloc (*offset_reloc,
8185 offset_expr.X_add_number,
8186 &value))
8187 {
8188 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
8189 required_insn_length, &insn->insn_opcode);
8190 offset_expr.X_op = O_absent;
8191 *offset_reloc = BFD_RELOC_UNUSED;
8192 }
8193 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8194 {
8195 if (required_insn_length == 2)
8196 set_insn_error (0, _("invalid unextended operand value"));
8197 else
8198 {
8199 forced_insn_length = 4;
8200 insn->insn_opcode |= MIPS16_EXTEND;
8201 }
8202 }
8203 else if (relax_char)
8204 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8205
8206 check_completed_insn (&arg);
8207 return TRUE;
8208 }
8209
8210 /* Fail the match if the line has too many operands. */
8211 if (*args == 0)
8212 return FALSE;
8213
8214 /* Handle characters that need to match exactly. */
8215 if (*args == '(' || *args == ')' || *args == ',')
8216 {
8217 if (match_char (&arg, *args))
8218 continue;
8219 return FALSE;
8220 }
8221
8222 arg.opnum += 1;
8223 c = *args;
8224 switch (c)
8225 {
8226 case 'p':
8227 case 'q':
8228 case 'A':
8229 case 'B':
8230 case 'E':
8231 relax_char = c;
8232 break;
8233
8234 case 'I':
8235 if (!match_const_int (&arg, &imm_expr.X_add_number))
8236 return FALSE;
8237 imm_expr.X_op = O_constant;
8238 if (GPR_SIZE == 32)
8239 normalize_constant_expr (&imm_expr);
8240 continue;
8241
8242 case 'a':
8243 case 'i':
8244 *offset_reloc = BFD_RELOC_MIPS16_JMP;
8245 break;
8246 }
8247
8248 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
8249 if (!operand)
8250 abort ();
8251
8252 if (operand->type == OP_PCREL)
8253 pcrel = TRUE;
8254 else
8255 {
8256 ext_operand = decode_mips16_operand (c, TRUE);
8257 if (operand != ext_operand)
8258 {
8259 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8260 {
8261 offset_expr.X_op = O_constant;
8262 offset_expr.X_add_number = 0;
8263 relax_char = c;
8264 continue;
8265 }
8266
8267 if (!match_expression (&arg, &offset_expr, offset_reloc))
8268 return FALSE;
8269
8270 /* '8' is used for SLTI(U) and has traditionally not
8271 been allowed to take relocation operators. */
8272 if (offset_reloc[0] != BFD_RELOC_UNUSED
8273 && (ext_operand->size != 16 || c == '8'))
8274 {
8275 match_not_constant (&arg);
8276 return FALSE;
8277 }
8278
8279 if (offset_expr.X_op == O_big)
8280 {
8281 match_out_of_range (&arg);
8282 return FALSE;
8283 }
8284
8285 relax_char = c;
8286 continue;
8287 }
8288 }
8289
8290 if (mips_optional_operand_p (operand)
8291 && args[1] == ','
8292 && (arg.token[0].type != OT_REG
8293 || arg.token[1].type == OT_END))
8294 {
8295 /* Assume that the register has been elided and is the
8296 same as the first operand. */
8297 arg.token = tokens;
8298 arg.argnum = 1;
8299 }
8300
8301 if (!match_operand (&arg, operand))
8302 return FALSE;
8303 }
8304 }
8305
8306 /* Record that the current instruction is invalid for the current ISA. */
8307
8308 static void
8309 match_invalid_for_isa (void)
8310 {
8311 set_insn_error_ss
8312 (0, _("opcode not supported on this processor: %s (%s)"),
8313 mips_cpu_info_from_arch (mips_opts.arch)->name,
8314 mips_cpu_info_from_isa (mips_opts.isa)->name);
8315 }
8316
8317 /* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8318 Return true if a definite match or failure was found, storing any match
8319 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8320 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8321 tried and failed to match under normal conditions and now want to try a
8322 more relaxed match. */
8323
8324 static bfd_boolean
8325 match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8326 const struct mips_opcode *past, struct mips_operand_token *tokens,
8327 int opcode_extra, bfd_boolean lax_match)
8328 {
8329 const struct mips_opcode *opcode;
8330 const struct mips_opcode *invalid_delay_slot;
8331 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8332
8333 /* Search for a match, ignoring alternatives that don't satisfy the
8334 current ISA or forced_length. */
8335 invalid_delay_slot = 0;
8336 seen_valid_for_isa = FALSE;
8337 seen_valid_for_size = FALSE;
8338 opcode = first;
8339 do
8340 {
8341 gas_assert (strcmp (opcode->name, first->name) == 0);
8342 if (is_opcode_valid (opcode))
8343 {
8344 seen_valid_for_isa = TRUE;
8345 if (is_size_valid (opcode))
8346 {
8347 bfd_boolean delay_slot_ok;
8348
8349 seen_valid_for_size = TRUE;
8350 delay_slot_ok = is_delay_slot_valid (opcode);
8351 if (match_insn (insn, opcode, tokens, opcode_extra,
8352 lax_match, delay_slot_ok))
8353 {
8354 if (!delay_slot_ok)
8355 {
8356 if (!invalid_delay_slot)
8357 invalid_delay_slot = opcode;
8358 }
8359 else
8360 return TRUE;
8361 }
8362 }
8363 }
8364 ++opcode;
8365 }
8366 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8367
8368 /* If the only matches we found had the wrong length for the delay slot,
8369 pick the first such match. We'll issue an appropriate warning later. */
8370 if (invalid_delay_slot)
8371 {
8372 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8373 lax_match, TRUE))
8374 return TRUE;
8375 abort ();
8376 }
8377
8378 /* Handle the case where we didn't try to match an instruction because
8379 all the alternatives were incompatible with the current ISA. */
8380 if (!seen_valid_for_isa)
8381 {
8382 match_invalid_for_isa ();
8383 return TRUE;
8384 }
8385
8386 /* Handle the case where we didn't try to match an instruction because
8387 all the alternatives were of the wrong size. */
8388 if (!seen_valid_for_size)
8389 {
8390 if (mips_opts.insn32)
8391 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
8392 else
8393 set_insn_error_i
8394 (0, _("unrecognized %d-bit version of microMIPS opcode"),
8395 8 * forced_insn_length);
8396 return TRUE;
8397 }
8398
8399 return FALSE;
8400 }
8401
8402 /* Like match_insns, but for MIPS16. */
8403
8404 static bfd_boolean
8405 match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8406 struct mips_operand_token *tokens)
8407 {
8408 const struct mips_opcode *opcode;
8409 bfd_boolean seen_valid_for_isa;
8410 bfd_boolean seen_valid_for_size;
8411
8412 /* Search for a match, ignoring alternatives that don't satisfy the
8413 current ISA. There are no separate entries for extended forms so
8414 we deal with forced_length later. */
8415 seen_valid_for_isa = FALSE;
8416 seen_valid_for_size = FALSE;
8417 opcode = first;
8418 do
8419 {
8420 gas_assert (strcmp (opcode->name, first->name) == 0);
8421 if (is_opcode_valid_16 (opcode))
8422 {
8423 seen_valid_for_isa = TRUE;
8424 if (is_size_valid_16 (opcode))
8425 {
8426 seen_valid_for_size = TRUE;
8427 if (match_mips16_insn (insn, opcode, tokens))
8428 return TRUE;
8429 }
8430 }
8431 ++opcode;
8432 }
8433 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8434 && strcmp (opcode->name, first->name) == 0);
8435
8436 /* Handle the case where we didn't try to match an instruction because
8437 all the alternatives were incompatible with the current ISA. */
8438 if (!seen_valid_for_isa)
8439 {
8440 match_invalid_for_isa ();
8441 return TRUE;
8442 }
8443
8444 /* Handle the case where we didn't try to match an instruction because
8445 all the alternatives were of the wrong size. */
8446 if (!seen_valid_for_size)
8447 {
8448 if (forced_insn_length == 2)
8449 set_insn_error
8450 (0, _("unrecognized unextended version of MIPS16 opcode"));
8451 else
8452 set_insn_error
8453 (0, _("unrecognized extended version of MIPS16 opcode"));
8454 return TRUE;
8455 }
8456
8457 return FALSE;
8458 }
8459
8460 /* Set up global variables for the start of a new macro. */
8461
8462 static void
8463 macro_start (void)
8464 {
8465 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
8466 memset (&mips_macro_warning.first_insn_sizes, 0,
8467 sizeof (mips_macro_warning.first_insn_sizes));
8468 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
8469 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
8470 && delayed_branch_p (&history[0]));
8471 if (history[0].frag
8472 && history[0].frag->fr_type == rs_machine_dependent
8473 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8474 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8475 mips_macro_warning.delay_slot_length = 0;
8476 else
8477 switch (history[0].insn_mo->pinfo2
8478 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8479 {
8480 case INSN2_BRANCH_DELAY_32BIT:
8481 mips_macro_warning.delay_slot_length = 4;
8482 break;
8483 case INSN2_BRANCH_DELAY_16BIT:
8484 mips_macro_warning.delay_slot_length = 2;
8485 break;
8486 default:
8487 mips_macro_warning.delay_slot_length = 0;
8488 break;
8489 }
8490 mips_macro_warning.first_frag = NULL;
8491 }
8492
8493 /* Given that a macro is longer than one instruction or of the wrong size,
8494 return the appropriate warning for it. Return null if no warning is
8495 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8496 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8497 and RELAX_NOMACRO. */
8498
8499 static const char *
8500 macro_warning (relax_substateT subtype)
8501 {
8502 if (subtype & RELAX_DELAY_SLOT)
8503 return _("macro instruction expanded into multiple instructions"
8504 " in a branch delay slot");
8505 else if (subtype & RELAX_NOMACRO)
8506 return _("macro instruction expanded into multiple instructions");
8507 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8508 | RELAX_DELAY_SLOT_SIZE_SECOND))
8509 return ((subtype & RELAX_DELAY_SLOT_16BIT)
8510 ? _("macro instruction expanded into a wrong size instruction"
8511 " in a 16-bit branch delay slot")
8512 : _("macro instruction expanded into a wrong size instruction"
8513 " in a 32-bit branch delay slot"));
8514 else
8515 return 0;
8516 }
8517
8518 /* Finish up a macro. Emit warnings as appropriate. */
8519
8520 static void
8521 macro_end (void)
8522 {
8523 /* Relaxation warning flags. */
8524 relax_substateT subtype = 0;
8525
8526 /* Check delay slot size requirements. */
8527 if (mips_macro_warning.delay_slot_length == 2)
8528 subtype |= RELAX_DELAY_SLOT_16BIT;
8529 if (mips_macro_warning.delay_slot_length != 0)
8530 {
8531 if (mips_macro_warning.delay_slot_length
8532 != mips_macro_warning.first_insn_sizes[0])
8533 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8534 if (mips_macro_warning.delay_slot_length
8535 != mips_macro_warning.first_insn_sizes[1])
8536 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8537 }
8538
8539 /* Check instruction count requirements. */
8540 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8541 {
8542 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
8543 subtype |= RELAX_SECOND_LONGER;
8544 if (mips_opts.warn_about_macros)
8545 subtype |= RELAX_NOMACRO;
8546 if (mips_macro_warning.delay_slot_p)
8547 subtype |= RELAX_DELAY_SLOT;
8548 }
8549
8550 /* If both alternatives fail to fill a delay slot correctly,
8551 emit the warning now. */
8552 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8553 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8554 {
8555 relax_substateT s;
8556 const char *msg;
8557
8558 s = subtype & (RELAX_DELAY_SLOT_16BIT
8559 | RELAX_DELAY_SLOT_SIZE_FIRST
8560 | RELAX_DELAY_SLOT_SIZE_SECOND);
8561 msg = macro_warning (s);
8562 if (msg != NULL)
8563 as_warn ("%s", msg);
8564 subtype &= ~s;
8565 }
8566
8567 /* If both implementations are longer than 1 instruction, then emit the
8568 warning now. */
8569 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8570 {
8571 relax_substateT s;
8572 const char *msg;
8573
8574 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8575 msg = macro_warning (s);
8576 if (msg != NULL)
8577 as_warn ("%s", msg);
8578 subtype &= ~s;
8579 }
8580
8581 /* If any flags still set, then one implementation might need a warning
8582 and the other either will need one of a different kind or none at all.
8583 Pass any remaining flags over to relaxation. */
8584 if (mips_macro_warning.first_frag != NULL)
8585 mips_macro_warning.first_frag->fr_subtype |= subtype;
8586 }
8587
8588 /* Instruction operand formats used in macros that vary between
8589 standard MIPS and microMIPS code. */
8590
8591 static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
8592 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8593 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8594 static const char * const lui_fmt[2] = { "t,u", "s,u" };
8595 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
8596 static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
8597 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8598 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8599
8600 #define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
8601 #define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8602 : cop12_fmt[mips_opts.micromips])
8603 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
8604 #define LUI_FMT (lui_fmt[mips_opts.micromips])
8605 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
8606 #define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8607 : mem12_fmt[mips_opts.micromips])
8608 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
8609 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
8610 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
8611
8612 /* Read a macro's relocation codes from *ARGS and store them in *R.
8613 The first argument in *ARGS will be either the code for a single
8614 relocation or -1 followed by the three codes that make up a
8615 composite relocation. */
8616
8617 static void
8618 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8619 {
8620 int i, next;
8621
8622 next = va_arg (*args, int);
8623 if (next >= 0)
8624 r[0] = (bfd_reloc_code_real_type) next;
8625 else
8626 {
8627 for (i = 0; i < 3; i++)
8628 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8629 /* This function is only used for 16-bit relocation fields.
8630 To make the macro code simpler, treat an unrelocated value
8631 in the same way as BFD_RELOC_LO16. */
8632 if (r[0] == BFD_RELOC_UNUSED)
8633 r[0] = BFD_RELOC_LO16;
8634 }
8635 }
8636
8637 /* Build an instruction created by a macro expansion. This is passed
8638 a pointer to the count of instructions created so far, an
8639 expression, the name of the instruction to build, an operand format
8640 string, and corresponding arguments. */
8641
8642 static void
8643 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
8644 {
8645 const struct mips_opcode *mo = NULL;
8646 bfd_reloc_code_real_type r[3];
8647 const struct mips_opcode *amo;
8648 const struct mips_operand *operand;
8649 struct hash_control *hash;
8650 struct mips_cl_insn insn;
8651 va_list args;
8652 unsigned int uval;
8653
8654 va_start (args, fmt);
8655
8656 if (mips_opts.mips16)
8657 {
8658 mips16_macro_build (ep, name, fmt, &args);
8659 va_end (args);
8660 return;
8661 }
8662
8663 r[0] = BFD_RELOC_UNUSED;
8664 r[1] = BFD_RELOC_UNUSED;
8665 r[2] = BFD_RELOC_UNUSED;
8666 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8667 amo = (struct mips_opcode *) hash_find (hash, name);
8668 gas_assert (amo);
8669 gas_assert (strcmp (name, amo->name) == 0);
8670
8671 do
8672 {
8673 /* Search until we get a match for NAME. It is assumed here that
8674 macros will never generate MDMX, MIPS-3D, or MT instructions.
8675 We try to match an instruction that fulfills the branch delay
8676 slot instruction length requirement (if any) of the previous
8677 instruction. While doing this we record the first instruction
8678 seen that matches all the other conditions and use it anyway
8679 if the requirement cannot be met; we will issue an appropriate
8680 warning later on. */
8681 if (strcmp (fmt, amo->args) == 0
8682 && amo->pinfo != INSN_MACRO
8683 && is_opcode_valid (amo)
8684 && is_size_valid (amo))
8685 {
8686 if (is_delay_slot_valid (amo))
8687 {
8688 mo = amo;
8689 break;
8690 }
8691 else if (!mo)
8692 mo = amo;
8693 }
8694
8695 ++amo;
8696 gas_assert (amo->name);
8697 }
8698 while (strcmp (name, amo->name) == 0);
8699
8700 gas_assert (mo);
8701 create_insn (&insn, mo);
8702 for (; *fmt; ++fmt)
8703 {
8704 switch (*fmt)
8705 {
8706 case ',':
8707 case '(':
8708 case ')':
8709 case 'z':
8710 break;
8711
8712 case 'i':
8713 case 'j':
8714 macro_read_relocs (&args, r);
8715 gas_assert (*r == BFD_RELOC_GPREL16
8716 || *r == BFD_RELOC_MIPS_HIGHER
8717 || *r == BFD_RELOC_HI16_S
8718 || *r == BFD_RELOC_LO16
8719 || *r == BFD_RELOC_MIPS_GOT_OFST);
8720 break;
8721
8722 case 'o':
8723 macro_read_relocs (&args, r);
8724 break;
8725
8726 case 'u':
8727 macro_read_relocs (&args, r);
8728 gas_assert (ep != NULL
8729 && (ep->X_op == O_constant
8730 || (ep->X_op == O_symbol
8731 && (*r == BFD_RELOC_MIPS_HIGHEST
8732 || *r == BFD_RELOC_HI16_S
8733 || *r == BFD_RELOC_HI16
8734 || *r == BFD_RELOC_GPREL16
8735 || *r == BFD_RELOC_MIPS_GOT_HI16
8736 || *r == BFD_RELOC_MIPS_CALL_HI16))));
8737 break;
8738
8739 case 'p':
8740 gas_assert (ep != NULL);
8741
8742 /*
8743 * This allows macro() to pass an immediate expression for
8744 * creating short branches without creating a symbol.
8745 *
8746 * We don't allow branch relaxation for these branches, as
8747 * they should only appear in ".set nomacro" anyway.
8748 */
8749 if (ep->X_op == O_constant)
8750 {
8751 /* For microMIPS we always use relocations for branches.
8752 So we should not resolve immediate values. */
8753 gas_assert (!mips_opts.micromips);
8754
8755 if ((ep->X_add_number & 3) != 0)
8756 as_bad (_("branch to misaligned address (0x%lx)"),
8757 (unsigned long) ep->X_add_number);
8758 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8759 as_bad (_("branch address range overflow (0x%lx)"),
8760 (unsigned long) ep->X_add_number);
8761 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8762 ep = NULL;
8763 }
8764 else
8765 *r = BFD_RELOC_16_PCREL_S2;
8766 break;
8767
8768 case 'a':
8769 gas_assert (ep != NULL);
8770 *r = BFD_RELOC_MIPS_JMP;
8771 break;
8772
8773 default:
8774 operand = (mips_opts.micromips
8775 ? decode_micromips_operand (fmt)
8776 : decode_mips_operand (fmt));
8777 if (!operand)
8778 abort ();
8779
8780 uval = va_arg (args, int);
8781 if (operand->type == OP_CLO_CLZ_DEST)
8782 uval |= (uval << 5);
8783 insn_insert_operand (&insn, operand, uval);
8784
8785 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
8786 ++fmt;
8787 break;
8788 }
8789 }
8790 va_end (args);
8791 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
8792
8793 append_insn (&insn, ep, r, TRUE);
8794 }
8795
8796 static void
8797 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
8798 va_list *args)
8799 {
8800 struct mips_opcode *mo;
8801 struct mips_cl_insn insn;
8802 const struct mips_operand *operand;
8803 bfd_reloc_code_real_type r[3]
8804 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
8805
8806 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
8807 gas_assert (mo);
8808 gas_assert (strcmp (name, mo->name) == 0);
8809
8810 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
8811 {
8812 ++mo;
8813 gas_assert (mo->name);
8814 gas_assert (strcmp (name, mo->name) == 0);
8815 }
8816
8817 create_insn (&insn, mo);
8818 for (; *fmt; ++fmt)
8819 {
8820 int c;
8821
8822 c = *fmt;
8823 switch (c)
8824 {
8825 case ',':
8826 case '(':
8827 case ')':
8828 break;
8829
8830 case '.':
8831 case 'S':
8832 case 'P':
8833 case 'R':
8834 break;
8835
8836 case '<':
8837 case '5':
8838 case 'F':
8839 case 'H':
8840 case 'W':
8841 case 'D':
8842 case 'j':
8843 case '8':
8844 case 'V':
8845 case 'C':
8846 case 'U':
8847 case 'k':
8848 case 'K':
8849 case 'p':
8850 case 'q':
8851 {
8852 offsetT value;
8853
8854 gas_assert (ep != NULL);
8855
8856 if (ep->X_op != O_constant)
8857 *r = (int) BFD_RELOC_UNUSED + c;
8858 else if (calculate_reloc (*r, ep->X_add_number, &value))
8859 {
8860 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
8861 ep = NULL;
8862 *r = BFD_RELOC_UNUSED;
8863 }
8864 }
8865 break;
8866
8867 default:
8868 operand = decode_mips16_operand (c, FALSE);
8869 if (!operand)
8870 abort ();
8871
8872 insn_insert_operand (&insn, operand, va_arg (*args, int));
8873 break;
8874 }
8875 }
8876
8877 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
8878
8879 append_insn (&insn, ep, r, TRUE);
8880 }
8881
8882 /*
8883 * Generate a "jalr" instruction with a relocation hint to the called
8884 * function. This occurs in NewABI PIC code.
8885 */
8886 static void
8887 macro_build_jalr (expressionS *ep, int cprestore)
8888 {
8889 static const bfd_reloc_code_real_type jalr_relocs[2]
8890 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
8891 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
8892 const char *jalr;
8893 char *f = NULL;
8894
8895 if (MIPS_JALR_HINT_P (ep))
8896 {
8897 frag_grow (8);
8898 f = frag_more (0);
8899 }
8900 if (mips_opts.micromips)
8901 {
8902 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
8903 ? "jalr" : "jalrs");
8904 if (MIPS_JALR_HINT_P (ep)
8905 || mips_opts.insn32
8906 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
8907 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
8908 else
8909 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
8910 }
8911 else
8912 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
8913 if (MIPS_JALR_HINT_P (ep))
8914 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
8915 }
8916
8917 /*
8918 * Generate a "lui" instruction.
8919 */
8920 static void
8921 macro_build_lui (expressionS *ep, int regnum)
8922 {
8923 gas_assert (! mips_opts.mips16);
8924
8925 if (ep->X_op != O_constant)
8926 {
8927 gas_assert (ep->X_op == O_symbol);
8928 /* _gp_disp is a special case, used from s_cpload.
8929 __gnu_local_gp is used if mips_no_shared. */
8930 gas_assert (mips_pic == NO_PIC
8931 || (! HAVE_NEWABI
8932 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
8933 || (! mips_in_shared
8934 && strcmp (S_GET_NAME (ep->X_add_symbol),
8935 "__gnu_local_gp") == 0));
8936 }
8937
8938 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
8939 }
8940
8941 /* Generate a sequence of instructions to do a load or store from a constant
8942 offset off of a base register (breg) into/from a target register (treg),
8943 using AT if necessary. */
8944 static void
8945 macro_build_ldst_constoffset (expressionS *ep, const char *op,
8946 int treg, int breg, int dbl)
8947 {
8948 gas_assert (ep->X_op == O_constant);
8949
8950 /* Sign-extending 32-bit constants makes their handling easier. */
8951 if (!dbl)
8952 normalize_constant_expr (ep);
8953
8954 /* Right now, this routine can only handle signed 32-bit constants. */
8955 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
8956 as_warn (_("operand overflow"));
8957
8958 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
8959 {
8960 /* Signed 16-bit offset will fit in the op. Easy! */
8961 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
8962 }
8963 else
8964 {
8965 /* 32-bit offset, need multiple instructions and AT, like:
8966 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
8967 addu $tempreg,$tempreg,$breg
8968 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
8969 to handle the complete offset. */
8970 macro_build_lui (ep, AT);
8971 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
8972 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
8973
8974 if (!mips_opts.at)
8975 as_bad (_("macro used $at after \".set noat\""));
8976 }
8977 }
8978
8979 /* set_at()
8980 * Generates code to set the $at register to true (one)
8981 * if reg is less than the immediate expression.
8982 */
8983 static void
8984 set_at (int reg, int unsignedp)
8985 {
8986 if (imm_expr.X_add_number >= -0x8000
8987 && imm_expr.X_add_number < 0x8000)
8988 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
8989 AT, reg, BFD_RELOC_LO16);
8990 else
8991 {
8992 load_register (AT, &imm_expr, GPR_SIZE == 64);
8993 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
8994 }
8995 }
8996
8997 /* Count the leading zeroes by performing a binary chop. This is a
8998 bulky bit of source, but performance is a LOT better for the
8999 majority of values than a simple loop to count the bits:
9000 for (lcnt = 0; (lcnt < 32); lcnt++)
9001 if ((v) & (1 << (31 - lcnt)))
9002 break;
9003 However it is not code size friendly, and the gain will drop a bit
9004 on certain cached systems.
9005 */
9006 #define COUNT_TOP_ZEROES(v) \
9007 (((v) & ~0xffff) == 0 \
9008 ? ((v) & ~0xff) == 0 \
9009 ? ((v) & ~0xf) == 0 \
9010 ? ((v) & ~0x3) == 0 \
9011 ? ((v) & ~0x1) == 0 \
9012 ? !(v) \
9013 ? 32 \
9014 : 31 \
9015 : 30 \
9016 : ((v) & ~0x7) == 0 \
9017 ? 29 \
9018 : 28 \
9019 : ((v) & ~0x3f) == 0 \
9020 ? ((v) & ~0x1f) == 0 \
9021 ? 27 \
9022 : 26 \
9023 : ((v) & ~0x7f) == 0 \
9024 ? 25 \
9025 : 24 \
9026 : ((v) & ~0xfff) == 0 \
9027 ? ((v) & ~0x3ff) == 0 \
9028 ? ((v) & ~0x1ff) == 0 \
9029 ? 23 \
9030 : 22 \
9031 : ((v) & ~0x7ff) == 0 \
9032 ? 21 \
9033 : 20 \
9034 : ((v) & ~0x3fff) == 0 \
9035 ? ((v) & ~0x1fff) == 0 \
9036 ? 19 \
9037 : 18 \
9038 : ((v) & ~0x7fff) == 0 \
9039 ? 17 \
9040 : 16 \
9041 : ((v) & ~0xffffff) == 0 \
9042 ? ((v) & ~0xfffff) == 0 \
9043 ? ((v) & ~0x3ffff) == 0 \
9044 ? ((v) & ~0x1ffff) == 0 \
9045 ? 15 \
9046 : 14 \
9047 : ((v) & ~0x7ffff) == 0 \
9048 ? 13 \
9049 : 12 \
9050 : ((v) & ~0x3fffff) == 0 \
9051 ? ((v) & ~0x1fffff) == 0 \
9052 ? 11 \
9053 : 10 \
9054 : ((v) & ~0x7fffff) == 0 \
9055 ? 9 \
9056 : 8 \
9057 : ((v) & ~0xfffffff) == 0 \
9058 ? ((v) & ~0x3ffffff) == 0 \
9059 ? ((v) & ~0x1ffffff) == 0 \
9060 ? 7 \
9061 : 6 \
9062 : ((v) & ~0x7ffffff) == 0 \
9063 ? 5 \
9064 : 4 \
9065 : ((v) & ~0x3fffffff) == 0 \
9066 ? ((v) & ~0x1fffffff) == 0 \
9067 ? 3 \
9068 : 2 \
9069 : ((v) & ~0x7fffffff) == 0 \
9070 ? 1 \
9071 : 0)
9072
9073 /* load_register()
9074 * This routine generates the least number of instructions necessary to load
9075 * an absolute expression value into a register.
9076 */
9077 static void
9078 load_register (int reg, expressionS *ep, int dbl)
9079 {
9080 int freg;
9081 expressionS hi32, lo32;
9082
9083 if (ep->X_op != O_big)
9084 {
9085 gas_assert (ep->X_op == O_constant);
9086
9087 /* Sign-extending 32-bit constants makes their handling easier. */
9088 if (!dbl)
9089 normalize_constant_expr (ep);
9090
9091 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
9092 {
9093 /* We can handle 16 bit signed values with an addiu to
9094 $zero. No need to ever use daddiu here, since $zero and
9095 the result are always correct in 32 bit mode. */
9096 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9097 return;
9098 }
9099 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9100 {
9101 /* We can handle 16 bit unsigned values with an ori to
9102 $zero. */
9103 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9104 return;
9105 }
9106 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
9107 {
9108 /* 32 bit values require an lui. */
9109 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9110 if ((ep->X_add_number & 0xffff) != 0)
9111 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9112 return;
9113 }
9114 }
9115
9116 /* The value is larger than 32 bits. */
9117
9118 if (!dbl || GPR_SIZE == 32)
9119 {
9120 char value[32];
9121
9122 sprintf_vma (value, ep->X_add_number);
9123 as_bad (_("number (0x%s) larger than 32 bits"), value);
9124 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9125 return;
9126 }
9127
9128 if (ep->X_op != O_big)
9129 {
9130 hi32 = *ep;
9131 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9132 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9133 hi32.X_add_number &= 0xffffffff;
9134 lo32 = *ep;
9135 lo32.X_add_number &= 0xffffffff;
9136 }
9137 else
9138 {
9139 gas_assert (ep->X_add_number > 2);
9140 if (ep->X_add_number == 3)
9141 generic_bignum[3] = 0;
9142 else if (ep->X_add_number > 4)
9143 as_bad (_("number larger than 64 bits"));
9144 lo32.X_op = O_constant;
9145 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9146 hi32.X_op = O_constant;
9147 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9148 }
9149
9150 if (hi32.X_add_number == 0)
9151 freg = 0;
9152 else
9153 {
9154 int shift, bit;
9155 unsigned long hi, lo;
9156
9157 if (hi32.X_add_number == (offsetT) 0xffffffff)
9158 {
9159 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9160 {
9161 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9162 return;
9163 }
9164 if (lo32.X_add_number & 0x80000000)
9165 {
9166 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9167 if (lo32.X_add_number & 0xffff)
9168 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9169 return;
9170 }
9171 }
9172
9173 /* Check for 16bit shifted constant. We know that hi32 is
9174 non-zero, so start the mask on the first bit of the hi32
9175 value. */
9176 shift = 17;
9177 do
9178 {
9179 unsigned long himask, lomask;
9180
9181 if (shift < 32)
9182 {
9183 himask = 0xffff >> (32 - shift);
9184 lomask = (0xffff << shift) & 0xffffffff;
9185 }
9186 else
9187 {
9188 himask = 0xffff << (shift - 32);
9189 lomask = 0;
9190 }
9191 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9192 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9193 {
9194 expressionS tmp;
9195
9196 tmp.X_op = O_constant;
9197 if (shift < 32)
9198 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9199 | (lo32.X_add_number >> shift));
9200 else
9201 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
9202 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9203 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9204 reg, reg, (shift >= 32) ? shift - 32 : shift);
9205 return;
9206 }
9207 ++shift;
9208 }
9209 while (shift <= (64 - 16));
9210
9211 /* Find the bit number of the lowest one bit, and store the
9212 shifted value in hi/lo. */
9213 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9214 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9215 if (lo != 0)
9216 {
9217 bit = 0;
9218 while ((lo & 1) == 0)
9219 {
9220 lo >>= 1;
9221 ++bit;
9222 }
9223 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9224 hi >>= bit;
9225 }
9226 else
9227 {
9228 bit = 32;
9229 while ((hi & 1) == 0)
9230 {
9231 hi >>= 1;
9232 ++bit;
9233 }
9234 lo = hi;
9235 hi = 0;
9236 }
9237
9238 /* Optimize if the shifted value is a (power of 2) - 1. */
9239 if ((hi == 0 && ((lo + 1) & lo) == 0)
9240 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
9241 {
9242 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
9243 if (shift != 0)
9244 {
9245 expressionS tmp;
9246
9247 /* This instruction will set the register to be all
9248 ones. */
9249 tmp.X_op = O_constant;
9250 tmp.X_add_number = (offsetT) -1;
9251 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9252 if (bit != 0)
9253 {
9254 bit += shift;
9255 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9256 reg, reg, (bit >= 32) ? bit - 32 : bit);
9257 }
9258 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
9259 reg, reg, (shift >= 32) ? shift - 32 : shift);
9260 return;
9261 }
9262 }
9263
9264 /* Sign extend hi32 before calling load_register, because we can
9265 generally get better code when we load a sign extended value. */
9266 if ((hi32.X_add_number & 0x80000000) != 0)
9267 hi32.X_add_number |= ~(offsetT) 0xffffffff;
9268 load_register (reg, &hi32, 0);
9269 freg = reg;
9270 }
9271 if ((lo32.X_add_number & 0xffff0000) == 0)
9272 {
9273 if (freg != 0)
9274 {
9275 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
9276 freg = reg;
9277 }
9278 }
9279 else
9280 {
9281 expressionS mid16;
9282
9283 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
9284 {
9285 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9286 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
9287 return;
9288 }
9289
9290 if (freg != 0)
9291 {
9292 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
9293 freg = reg;
9294 }
9295 mid16 = lo32;
9296 mid16.X_add_number >>= 16;
9297 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9298 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9299 freg = reg;
9300 }
9301 if ((lo32.X_add_number & 0xffff) != 0)
9302 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9303 }
9304
9305 static inline void
9306 load_delay_nop (void)
9307 {
9308 if (!gpr_interlocks)
9309 macro_build (NULL, "nop", "");
9310 }
9311
9312 /* Load an address into a register. */
9313
9314 static void
9315 load_address (int reg, expressionS *ep, int *used_at)
9316 {
9317 if (ep->X_op != O_constant
9318 && ep->X_op != O_symbol)
9319 {
9320 as_bad (_("expression too complex"));
9321 ep->X_op = O_constant;
9322 }
9323
9324 if (ep->X_op == O_constant)
9325 {
9326 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
9327 return;
9328 }
9329
9330 if (mips_pic == NO_PIC)
9331 {
9332 /* If this is a reference to a GP relative symbol, we want
9333 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
9334 Otherwise we want
9335 lui $reg,<sym> (BFD_RELOC_HI16_S)
9336 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9337 If we have an addend, we always use the latter form.
9338
9339 With 64bit address space and a usable $at we want
9340 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9341 lui $at,<sym> (BFD_RELOC_HI16_S)
9342 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9343 daddiu $at,<sym> (BFD_RELOC_LO16)
9344 dsll32 $reg,0
9345 daddu $reg,$reg,$at
9346
9347 If $at is already in use, we use a path which is suboptimal
9348 on superscalar processors.
9349 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9350 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9351 dsll $reg,16
9352 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9353 dsll $reg,16
9354 daddiu $reg,<sym> (BFD_RELOC_LO16)
9355
9356 For GP relative symbols in 64bit address space we can use
9357 the same sequence as in 32bit address space. */
9358 if (HAVE_64BIT_SYMBOLS)
9359 {
9360 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9361 && !nopic_need_relax (ep->X_add_symbol, 1))
9362 {
9363 relax_start (ep->X_add_symbol);
9364 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9365 mips_gp_register, BFD_RELOC_GPREL16);
9366 relax_switch ();
9367 }
9368
9369 if (*used_at == 0 && mips_opts.at)
9370 {
9371 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9372 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
9373 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9374 BFD_RELOC_MIPS_HIGHER);
9375 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
9376 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
9377 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
9378 *used_at = 1;
9379 }
9380 else
9381 {
9382 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9383 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9384 BFD_RELOC_MIPS_HIGHER);
9385 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9386 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
9387 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9388 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
9389 }
9390
9391 if (mips_relax.sequence)
9392 relax_end ();
9393 }
9394 else
9395 {
9396 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9397 && !nopic_need_relax (ep->X_add_symbol, 1))
9398 {
9399 relax_start (ep->X_add_symbol);
9400 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9401 mips_gp_register, BFD_RELOC_GPREL16);
9402 relax_switch ();
9403 }
9404 macro_build_lui (ep, reg);
9405 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9406 reg, reg, BFD_RELOC_LO16);
9407 if (mips_relax.sequence)
9408 relax_end ();
9409 }
9410 }
9411 else if (!mips_big_got)
9412 {
9413 expressionS ex;
9414
9415 /* If this is a reference to an external symbol, we want
9416 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9417 Otherwise we want
9418 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9419 nop
9420 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9421 If there is a constant, it must be added in after.
9422
9423 If we have NewABI, we want
9424 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9425 unless we're referencing a global symbol with a non-zero
9426 offset, in which case cst must be added separately. */
9427 if (HAVE_NEWABI)
9428 {
9429 if (ep->X_add_number)
9430 {
9431 ex.X_add_number = ep->X_add_number;
9432 ep->X_add_number = 0;
9433 relax_start (ep->X_add_symbol);
9434 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9435 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9436 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9437 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9438 ex.X_op = O_constant;
9439 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9440 reg, reg, BFD_RELOC_LO16);
9441 ep->X_add_number = ex.X_add_number;
9442 relax_switch ();
9443 }
9444 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9445 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9446 if (mips_relax.sequence)
9447 relax_end ();
9448 }
9449 else
9450 {
9451 ex.X_add_number = ep->X_add_number;
9452 ep->X_add_number = 0;
9453 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9454 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9455 load_delay_nop ();
9456 relax_start (ep->X_add_symbol);
9457 relax_switch ();
9458 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9459 BFD_RELOC_LO16);
9460 relax_end ();
9461
9462 if (ex.X_add_number != 0)
9463 {
9464 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9465 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9466 ex.X_op = O_constant;
9467 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9468 reg, reg, BFD_RELOC_LO16);
9469 }
9470 }
9471 }
9472 else if (mips_big_got)
9473 {
9474 expressionS ex;
9475
9476 /* This is the large GOT case. If this is a reference to an
9477 external symbol, we want
9478 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9479 addu $reg,$reg,$gp
9480 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
9481
9482 Otherwise, for a reference to a local symbol in old ABI, we want
9483 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9484 nop
9485 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
9486 If there is a constant, it must be added in after.
9487
9488 In the NewABI, for local symbols, with or without offsets, we want:
9489 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9490 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
9491 */
9492 if (HAVE_NEWABI)
9493 {
9494 ex.X_add_number = ep->X_add_number;
9495 ep->X_add_number = 0;
9496 relax_start (ep->X_add_symbol);
9497 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9498 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9499 reg, reg, mips_gp_register);
9500 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9501 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9502 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9503 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9504 else if (ex.X_add_number)
9505 {
9506 ex.X_op = O_constant;
9507 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9508 BFD_RELOC_LO16);
9509 }
9510
9511 ep->X_add_number = ex.X_add_number;
9512 relax_switch ();
9513 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9514 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9515 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9516 BFD_RELOC_MIPS_GOT_OFST);
9517 relax_end ();
9518 }
9519 else
9520 {
9521 ex.X_add_number = ep->X_add_number;
9522 ep->X_add_number = 0;
9523 relax_start (ep->X_add_symbol);
9524 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9525 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9526 reg, reg, mips_gp_register);
9527 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9528 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9529 relax_switch ();
9530 if (reg_needs_delay (mips_gp_register))
9531 {
9532 /* We need a nop before loading from $gp. This special
9533 check is required because the lui which starts the main
9534 instruction stream does not refer to $gp, and so will not
9535 insert the nop which may be required. */
9536 macro_build (NULL, "nop", "");
9537 }
9538 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9539 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9540 load_delay_nop ();
9541 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9542 BFD_RELOC_LO16);
9543 relax_end ();
9544
9545 if (ex.X_add_number != 0)
9546 {
9547 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9548 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9549 ex.X_op = O_constant;
9550 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9551 BFD_RELOC_LO16);
9552 }
9553 }
9554 }
9555 else
9556 abort ();
9557
9558 if (!mips_opts.at && *used_at == 1)
9559 as_bad (_("macro used $at after \".set noat\""));
9560 }
9561
9562 /* Move the contents of register SOURCE into register DEST. */
9563
9564 static void
9565 move_register (int dest, int source)
9566 {
9567 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9568 instruction specifically requires a 32-bit one. */
9569 if (mips_opts.micromips
9570 && !mips_opts.insn32
9571 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9572 macro_build (NULL, "move", "mp,mj", dest, source);
9573 else
9574 macro_build (NULL, "or", "d,v,t", dest, source, 0);
9575 }
9576
9577 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
9578 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9579 The two alternatives are:
9580
9581 Global symbol Local symbol
9582 ------------- ------------
9583 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9584 ... ...
9585 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9586
9587 load_got_offset emits the first instruction and add_got_offset
9588 emits the second for a 16-bit offset or add_got_offset_hilo emits
9589 a sequence to add a 32-bit offset using a scratch register. */
9590
9591 static void
9592 load_got_offset (int dest, expressionS *local)
9593 {
9594 expressionS global;
9595
9596 global = *local;
9597 global.X_add_number = 0;
9598
9599 relax_start (local->X_add_symbol);
9600 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9601 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9602 relax_switch ();
9603 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9604 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9605 relax_end ();
9606 }
9607
9608 static void
9609 add_got_offset (int dest, expressionS *local)
9610 {
9611 expressionS global;
9612
9613 global.X_op = O_constant;
9614 global.X_op_symbol = NULL;
9615 global.X_add_symbol = NULL;
9616 global.X_add_number = local->X_add_number;
9617
9618 relax_start (local->X_add_symbol);
9619 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
9620 dest, dest, BFD_RELOC_LO16);
9621 relax_switch ();
9622 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
9623 relax_end ();
9624 }
9625
9626 static void
9627 add_got_offset_hilo (int dest, expressionS *local, int tmp)
9628 {
9629 expressionS global;
9630 int hold_mips_optimize;
9631
9632 global.X_op = O_constant;
9633 global.X_op_symbol = NULL;
9634 global.X_add_symbol = NULL;
9635 global.X_add_number = local->X_add_number;
9636
9637 relax_start (local->X_add_symbol);
9638 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9639 relax_switch ();
9640 /* Set mips_optimize around the lui instruction to avoid
9641 inserting an unnecessary nop after the lw. */
9642 hold_mips_optimize = mips_optimize;
9643 mips_optimize = 2;
9644 macro_build_lui (&global, tmp);
9645 mips_optimize = hold_mips_optimize;
9646 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9647 relax_end ();
9648
9649 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9650 }
9651
9652 /* Emit a sequence of instructions to emulate a branch likely operation.
9653 BR is an ordinary branch corresponding to one to be emulated. BRNEG
9654 is its complementing branch with the original condition negated.
9655 CALL is set if the original branch specified the link operation.
9656 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9657
9658 Code like this is produced in the noreorder mode:
9659
9660 BRNEG <args>, 1f
9661 nop
9662 b <sym>
9663 delay slot (executed only if branch taken)
9664 1:
9665
9666 or, if CALL is set:
9667
9668 BRNEG <args>, 1f
9669 nop
9670 bal <sym>
9671 delay slot (executed only if branch taken)
9672 1:
9673
9674 In the reorder mode the delay slot would be filled with a nop anyway,
9675 so code produced is simply:
9676
9677 BR <args>, <sym>
9678 nop
9679
9680 This function is used when producing code for the microMIPS ASE that
9681 does not implement branch likely instructions in hardware. */
9682
9683 static void
9684 macro_build_branch_likely (const char *br, const char *brneg,
9685 int call, expressionS *ep, const char *fmt,
9686 unsigned int sreg, unsigned int treg)
9687 {
9688 int noreorder = mips_opts.noreorder;
9689 expressionS expr1;
9690
9691 gas_assert (mips_opts.micromips);
9692 start_noreorder ();
9693 if (noreorder)
9694 {
9695 micromips_label_expr (&expr1);
9696 macro_build (&expr1, brneg, fmt, sreg, treg);
9697 macro_build (NULL, "nop", "");
9698 macro_build (ep, call ? "bal" : "b", "p");
9699
9700 /* Set to true so that append_insn adds a label. */
9701 emit_branch_likely_macro = TRUE;
9702 }
9703 else
9704 {
9705 macro_build (ep, br, fmt, sreg, treg);
9706 macro_build (NULL, "nop", "");
9707 }
9708 end_noreorder ();
9709 }
9710
9711 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9712 the condition code tested. EP specifies the branch target. */
9713
9714 static void
9715 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9716 {
9717 const int call = 0;
9718 const char *brneg;
9719 const char *br;
9720
9721 switch (type)
9722 {
9723 case M_BC1FL:
9724 br = "bc1f";
9725 brneg = "bc1t";
9726 break;
9727 case M_BC1TL:
9728 br = "bc1t";
9729 brneg = "bc1f";
9730 break;
9731 case M_BC2FL:
9732 br = "bc2f";
9733 brneg = "bc2t";
9734 break;
9735 case M_BC2TL:
9736 br = "bc2t";
9737 brneg = "bc2f";
9738 break;
9739 default:
9740 abort ();
9741 }
9742 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9743 }
9744
9745 /* Emit a two-argument branch macro specified by TYPE, using SREG as
9746 the register tested. EP specifies the branch target. */
9747
9748 static void
9749 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9750 {
9751 const char *brneg = NULL;
9752 const char *br;
9753 int call = 0;
9754
9755 switch (type)
9756 {
9757 case M_BGEZ:
9758 br = "bgez";
9759 break;
9760 case M_BGEZL:
9761 br = mips_opts.micromips ? "bgez" : "bgezl";
9762 brneg = "bltz";
9763 break;
9764 case M_BGEZALL:
9765 gas_assert (mips_opts.micromips);
9766 br = mips_opts.insn32 ? "bgezal" : "bgezals";
9767 brneg = "bltz";
9768 call = 1;
9769 break;
9770 case M_BGTZ:
9771 br = "bgtz";
9772 break;
9773 case M_BGTZL:
9774 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9775 brneg = "blez";
9776 break;
9777 case M_BLEZ:
9778 br = "blez";
9779 break;
9780 case M_BLEZL:
9781 br = mips_opts.micromips ? "blez" : "blezl";
9782 brneg = "bgtz";
9783 break;
9784 case M_BLTZ:
9785 br = "bltz";
9786 break;
9787 case M_BLTZL:
9788 br = mips_opts.micromips ? "bltz" : "bltzl";
9789 brneg = "bgez";
9790 break;
9791 case M_BLTZALL:
9792 gas_assert (mips_opts.micromips);
9793 br = mips_opts.insn32 ? "bltzal" : "bltzals";
9794 brneg = "bgez";
9795 call = 1;
9796 break;
9797 default:
9798 abort ();
9799 }
9800 if (mips_opts.micromips && brneg)
9801 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9802 else
9803 macro_build (ep, br, "s,p", sreg);
9804 }
9805
9806 /* Emit a three-argument branch macro specified by TYPE, using SREG and
9807 TREG as the registers tested. EP specifies the branch target. */
9808
9809 static void
9810 macro_build_branch_rsrt (int type, expressionS *ep,
9811 unsigned int sreg, unsigned int treg)
9812 {
9813 const char *brneg = NULL;
9814 const int call = 0;
9815 const char *br;
9816
9817 switch (type)
9818 {
9819 case M_BEQ:
9820 case M_BEQ_I:
9821 br = "beq";
9822 break;
9823 case M_BEQL:
9824 case M_BEQL_I:
9825 br = mips_opts.micromips ? "beq" : "beql";
9826 brneg = "bne";
9827 break;
9828 case M_BNE:
9829 case M_BNE_I:
9830 br = "bne";
9831 break;
9832 case M_BNEL:
9833 case M_BNEL_I:
9834 br = mips_opts.micromips ? "bne" : "bnel";
9835 brneg = "beq";
9836 break;
9837 default:
9838 abort ();
9839 }
9840 if (mips_opts.micromips && brneg)
9841 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
9842 else
9843 macro_build (ep, br, "s,t,p", sreg, treg);
9844 }
9845
9846 /* Return the high part that should be loaded in order to make the low
9847 part of VALUE accessible using an offset of OFFBITS bits. */
9848
9849 static offsetT
9850 offset_high_part (offsetT value, unsigned int offbits)
9851 {
9852 offsetT bias;
9853 addressT low_mask;
9854
9855 if (offbits == 0)
9856 return value;
9857 bias = 1 << (offbits - 1);
9858 low_mask = bias * 2 - 1;
9859 return (value + bias) & ~low_mask;
9860 }
9861
9862 /* Return true if the value stored in offset_expr and offset_reloc
9863 fits into a signed offset of OFFBITS bits. RANGE is the maximum
9864 amount that the caller wants to add without inducing overflow
9865 and ALIGN is the known alignment of the value in bytes. */
9866
9867 static bfd_boolean
9868 small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
9869 {
9870 if (offbits == 16)
9871 {
9872 /* Accept any relocation operator if overflow isn't a concern. */
9873 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
9874 return TRUE;
9875
9876 /* These relocations are guaranteed not to overflow in correct links. */
9877 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
9878 || gprel16_reloc_p (*offset_reloc))
9879 return TRUE;
9880 }
9881 if (offset_expr.X_op == O_constant
9882 && offset_high_part (offset_expr.X_add_number, offbits) == 0
9883 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
9884 return TRUE;
9885 return FALSE;
9886 }
9887
9888 /*
9889 * Build macros
9890 * This routine implements the seemingly endless macro or synthesized
9891 * instructions and addressing modes in the mips assembly language. Many
9892 * of these macros are simple and are similar to each other. These could
9893 * probably be handled by some kind of table or grammar approach instead of
9894 * this verbose method. Others are not simple macros but are more like
9895 * optimizing code generation.
9896 * One interesting optimization is when several store macros appear
9897 * consecutively that would load AT with the upper half of the same address.
9898 * The ensuing load upper instructions are omitted. This implies some kind
9899 * of global optimization. We currently only optimize within a single macro.
9900 * For many of the load and store macros if the address is specified as a
9901 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
9902 * first load register 'at' with zero and use it as the base register. The
9903 * mips assembler simply uses register $zero. Just one tiny optimization
9904 * we're missing.
9905 */
9906 static void
9907 macro (struct mips_cl_insn *ip, char *str)
9908 {
9909 const struct mips_operand_array *operands;
9910 unsigned int breg, i;
9911 unsigned int tempreg;
9912 int mask;
9913 int used_at = 0;
9914 expressionS label_expr;
9915 expressionS expr1;
9916 expressionS *ep;
9917 const char *s;
9918 const char *s2;
9919 const char *fmt;
9920 int likely = 0;
9921 int coproc = 0;
9922 int offbits = 16;
9923 int call = 0;
9924 int jals = 0;
9925 int dbl = 0;
9926 int imm = 0;
9927 int ust = 0;
9928 int lp = 0;
9929 bfd_boolean large_offset;
9930 int off;
9931 int hold_mips_optimize;
9932 unsigned int align;
9933 unsigned int op[MAX_OPERANDS];
9934
9935 gas_assert (! mips_opts.mips16);
9936
9937 operands = insn_operands (ip);
9938 for (i = 0; i < MAX_OPERANDS; i++)
9939 if (operands->operand[i])
9940 op[i] = insn_extract_operand (ip, operands->operand[i]);
9941 else
9942 op[i] = -1;
9943
9944 mask = ip->insn_mo->mask;
9945
9946 label_expr.X_op = O_constant;
9947 label_expr.X_op_symbol = NULL;
9948 label_expr.X_add_symbol = NULL;
9949 label_expr.X_add_number = 0;
9950
9951 expr1.X_op = O_constant;
9952 expr1.X_op_symbol = NULL;
9953 expr1.X_add_symbol = NULL;
9954 expr1.X_add_number = 1;
9955 align = 1;
9956
9957 switch (mask)
9958 {
9959 case M_DABS:
9960 dbl = 1;
9961 /* Fall through. */
9962 case M_ABS:
9963 /* bgez $a0,1f
9964 move v0,$a0
9965 sub v0,$zero,$a0
9966 1:
9967 */
9968
9969 start_noreorder ();
9970
9971 if (mips_opts.micromips)
9972 micromips_label_expr (&label_expr);
9973 else
9974 label_expr.X_add_number = 8;
9975 macro_build (&label_expr, "bgez", "s,p", op[1]);
9976 if (op[0] == op[1])
9977 macro_build (NULL, "nop", "");
9978 else
9979 move_register (op[0], op[1]);
9980 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
9981 if (mips_opts.micromips)
9982 micromips_add_label ();
9983
9984 end_noreorder ();
9985 break;
9986
9987 case M_ADD_I:
9988 s = "addi";
9989 s2 = "add";
9990 goto do_addi;
9991 case M_ADDU_I:
9992 s = "addiu";
9993 s2 = "addu";
9994 goto do_addi;
9995 case M_DADD_I:
9996 dbl = 1;
9997 s = "daddi";
9998 s2 = "dadd";
9999 if (!mips_opts.micromips)
10000 goto do_addi;
10001 if (imm_expr.X_add_number >= -0x200
10002 && imm_expr.X_add_number < 0x200)
10003 {
10004 macro_build (NULL, s, "t,r,.", op[0], op[1],
10005 (int) imm_expr.X_add_number);
10006 break;
10007 }
10008 goto do_addi_i;
10009 case M_DADDU_I:
10010 dbl = 1;
10011 s = "daddiu";
10012 s2 = "daddu";
10013 do_addi:
10014 if (imm_expr.X_add_number >= -0x8000
10015 && imm_expr.X_add_number < 0x8000)
10016 {
10017 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
10018 break;
10019 }
10020 do_addi_i:
10021 used_at = 1;
10022 load_register (AT, &imm_expr, dbl);
10023 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10024 break;
10025
10026 case M_AND_I:
10027 s = "andi";
10028 s2 = "and";
10029 goto do_bit;
10030 case M_OR_I:
10031 s = "ori";
10032 s2 = "or";
10033 goto do_bit;
10034 case M_NOR_I:
10035 s = "";
10036 s2 = "nor";
10037 goto do_bit;
10038 case M_XOR_I:
10039 s = "xori";
10040 s2 = "xor";
10041 do_bit:
10042 if (imm_expr.X_add_number >= 0
10043 && imm_expr.X_add_number < 0x10000)
10044 {
10045 if (mask != M_NOR_I)
10046 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
10047 else
10048 {
10049 macro_build (&imm_expr, "ori", "t,r,i",
10050 op[0], op[1], BFD_RELOC_LO16);
10051 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
10052 }
10053 break;
10054 }
10055
10056 used_at = 1;
10057 load_register (AT, &imm_expr, GPR_SIZE == 64);
10058 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10059 break;
10060
10061 case M_BALIGN:
10062 switch (imm_expr.X_add_number)
10063 {
10064 case 0:
10065 macro_build (NULL, "nop", "");
10066 break;
10067 case 2:
10068 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
10069 break;
10070 case 1:
10071 case 3:
10072 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
10073 (int) imm_expr.X_add_number);
10074 break;
10075 default:
10076 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10077 (unsigned long) imm_expr.X_add_number);
10078 break;
10079 }
10080 break;
10081
10082 case M_BC1FL:
10083 case M_BC1TL:
10084 case M_BC2FL:
10085 case M_BC2TL:
10086 gas_assert (mips_opts.micromips);
10087 macro_build_branch_ccl (mask, &offset_expr,
10088 EXTRACT_OPERAND (1, BCC, *ip));
10089 break;
10090
10091 case M_BEQ_I:
10092 case M_BEQL_I:
10093 case M_BNE_I:
10094 case M_BNEL_I:
10095 if (imm_expr.X_add_number == 0)
10096 op[1] = 0;
10097 else
10098 {
10099 op[1] = AT;
10100 used_at = 1;
10101 load_register (op[1], &imm_expr, GPR_SIZE == 64);
10102 }
10103 /* Fall through. */
10104 case M_BEQL:
10105 case M_BNEL:
10106 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
10107 break;
10108
10109 case M_BGEL:
10110 likely = 1;
10111 /* Fall through. */
10112 case M_BGE:
10113 if (op[1] == 0)
10114 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10115 else if (op[0] == 0)
10116 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
10117 else
10118 {
10119 used_at = 1;
10120 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10121 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10122 &offset_expr, AT, ZERO);
10123 }
10124 break;
10125
10126 case M_BGEZL:
10127 case M_BGEZALL:
10128 case M_BGTZL:
10129 case M_BLEZL:
10130 case M_BLTZL:
10131 case M_BLTZALL:
10132 macro_build_branch_rs (mask, &offset_expr, op[0]);
10133 break;
10134
10135 case M_BGTL_I:
10136 likely = 1;
10137 /* Fall through. */
10138 case M_BGT_I:
10139 /* Check for > max integer. */
10140 if (imm_expr.X_add_number >= GPR_SMAX)
10141 {
10142 do_false:
10143 /* Result is always false. */
10144 if (! likely)
10145 macro_build (NULL, "nop", "");
10146 else
10147 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
10148 break;
10149 }
10150 ++imm_expr.X_add_number;
10151 /* FALLTHROUGH */
10152 case M_BGE_I:
10153 case M_BGEL_I:
10154 if (mask == M_BGEL_I)
10155 likely = 1;
10156 if (imm_expr.X_add_number == 0)
10157 {
10158 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
10159 &offset_expr, op[0]);
10160 break;
10161 }
10162 if (imm_expr.X_add_number == 1)
10163 {
10164 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
10165 &offset_expr, op[0]);
10166 break;
10167 }
10168 if (imm_expr.X_add_number <= GPR_SMIN)
10169 {
10170 do_true:
10171 /* result is always true */
10172 as_warn (_("branch %s is always true"), ip->insn_mo->name);
10173 macro_build (&offset_expr, "b", "p");
10174 break;
10175 }
10176 used_at = 1;
10177 set_at (op[0], 0);
10178 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10179 &offset_expr, AT, ZERO);
10180 break;
10181
10182 case M_BGEUL:
10183 likely = 1;
10184 /* Fall through. */
10185 case M_BGEU:
10186 if (op[1] == 0)
10187 goto do_true;
10188 else if (op[0] == 0)
10189 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10190 &offset_expr, ZERO, op[1]);
10191 else
10192 {
10193 used_at = 1;
10194 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10195 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10196 &offset_expr, AT, ZERO);
10197 }
10198 break;
10199
10200 case M_BGTUL_I:
10201 likely = 1;
10202 /* Fall through. */
10203 case M_BGTU_I:
10204 if (op[0] == 0
10205 || (GPR_SIZE == 32
10206 && imm_expr.X_add_number == -1))
10207 goto do_false;
10208 ++imm_expr.X_add_number;
10209 /* FALLTHROUGH */
10210 case M_BGEU_I:
10211 case M_BGEUL_I:
10212 if (mask == M_BGEUL_I)
10213 likely = 1;
10214 if (imm_expr.X_add_number == 0)
10215 goto do_true;
10216 else if (imm_expr.X_add_number == 1)
10217 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10218 &offset_expr, op[0], ZERO);
10219 else
10220 {
10221 used_at = 1;
10222 set_at (op[0], 1);
10223 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10224 &offset_expr, AT, ZERO);
10225 }
10226 break;
10227
10228 case M_BGTL:
10229 likely = 1;
10230 /* Fall through. */
10231 case M_BGT:
10232 if (op[1] == 0)
10233 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10234 else if (op[0] == 0)
10235 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
10236 else
10237 {
10238 used_at = 1;
10239 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10240 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10241 &offset_expr, AT, ZERO);
10242 }
10243 break;
10244
10245 case M_BGTUL:
10246 likely = 1;
10247 /* Fall through. */
10248 case M_BGTU:
10249 if (op[1] == 0)
10250 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10251 &offset_expr, op[0], ZERO);
10252 else if (op[0] == 0)
10253 goto do_false;
10254 else
10255 {
10256 used_at = 1;
10257 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10258 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10259 &offset_expr, AT, ZERO);
10260 }
10261 break;
10262
10263 case M_BLEL:
10264 likely = 1;
10265 /* Fall through. */
10266 case M_BLE:
10267 if (op[1] == 0)
10268 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10269 else if (op[0] == 0)
10270 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
10271 else
10272 {
10273 used_at = 1;
10274 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10275 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10276 &offset_expr, AT, ZERO);
10277 }
10278 break;
10279
10280 case M_BLEL_I:
10281 likely = 1;
10282 /* Fall through. */
10283 case M_BLE_I:
10284 if (imm_expr.X_add_number >= GPR_SMAX)
10285 goto do_true;
10286 ++imm_expr.X_add_number;
10287 /* FALLTHROUGH */
10288 case M_BLT_I:
10289 case M_BLTL_I:
10290 if (mask == M_BLTL_I)
10291 likely = 1;
10292 if (imm_expr.X_add_number == 0)
10293 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10294 else if (imm_expr.X_add_number == 1)
10295 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10296 else
10297 {
10298 used_at = 1;
10299 set_at (op[0], 0);
10300 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10301 &offset_expr, AT, ZERO);
10302 }
10303 break;
10304
10305 case M_BLEUL:
10306 likely = 1;
10307 /* Fall through. */
10308 case M_BLEU:
10309 if (op[1] == 0)
10310 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10311 &offset_expr, op[0], ZERO);
10312 else if (op[0] == 0)
10313 goto do_true;
10314 else
10315 {
10316 used_at = 1;
10317 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10318 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10319 &offset_expr, AT, ZERO);
10320 }
10321 break;
10322
10323 case M_BLEUL_I:
10324 likely = 1;
10325 /* Fall through. */
10326 case M_BLEU_I:
10327 if (op[0] == 0
10328 || (GPR_SIZE == 32
10329 && imm_expr.X_add_number == -1))
10330 goto do_true;
10331 ++imm_expr.X_add_number;
10332 /* FALLTHROUGH */
10333 case M_BLTU_I:
10334 case M_BLTUL_I:
10335 if (mask == M_BLTUL_I)
10336 likely = 1;
10337 if (imm_expr.X_add_number == 0)
10338 goto do_false;
10339 else if (imm_expr.X_add_number == 1)
10340 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10341 &offset_expr, op[0], ZERO);
10342 else
10343 {
10344 used_at = 1;
10345 set_at (op[0], 1);
10346 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10347 &offset_expr, AT, ZERO);
10348 }
10349 break;
10350
10351 case M_BLTL:
10352 likely = 1;
10353 /* Fall through. */
10354 case M_BLT:
10355 if (op[1] == 0)
10356 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10357 else if (op[0] == 0)
10358 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
10359 else
10360 {
10361 used_at = 1;
10362 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10363 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10364 &offset_expr, AT, ZERO);
10365 }
10366 break;
10367
10368 case M_BLTUL:
10369 likely = 1;
10370 /* Fall through. */
10371 case M_BLTU:
10372 if (op[1] == 0)
10373 goto do_false;
10374 else if (op[0] == 0)
10375 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10376 &offset_expr, ZERO, op[1]);
10377 else
10378 {
10379 used_at = 1;
10380 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10381 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10382 &offset_expr, AT, ZERO);
10383 }
10384 break;
10385
10386 case M_DDIV_3:
10387 dbl = 1;
10388 /* Fall through. */
10389 case M_DIV_3:
10390 s = "mflo";
10391 goto do_div3;
10392 case M_DREM_3:
10393 dbl = 1;
10394 /* Fall through. */
10395 case M_REM_3:
10396 s = "mfhi";
10397 do_div3:
10398 if (op[2] == 0)
10399 {
10400 as_warn (_("divide by zero"));
10401 if (mips_trap)
10402 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10403 else
10404 macro_build (NULL, "break", BRK_FMT, 7);
10405 break;
10406 }
10407
10408 start_noreorder ();
10409 if (mips_trap)
10410 {
10411 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10412 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10413 }
10414 else
10415 {
10416 if (mips_opts.micromips)
10417 micromips_label_expr (&label_expr);
10418 else
10419 label_expr.X_add_number = 8;
10420 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10421 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10422 macro_build (NULL, "break", BRK_FMT, 7);
10423 if (mips_opts.micromips)
10424 micromips_add_label ();
10425 }
10426 expr1.X_add_number = -1;
10427 used_at = 1;
10428 load_register (AT, &expr1, dbl);
10429 if (mips_opts.micromips)
10430 micromips_label_expr (&label_expr);
10431 else
10432 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
10433 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
10434 if (dbl)
10435 {
10436 expr1.X_add_number = 1;
10437 load_register (AT, &expr1, dbl);
10438 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
10439 }
10440 else
10441 {
10442 expr1.X_add_number = 0x80000000;
10443 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
10444 }
10445 if (mips_trap)
10446 {
10447 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
10448 /* We want to close the noreorder block as soon as possible, so
10449 that later insns are available for delay slot filling. */
10450 end_noreorder ();
10451 }
10452 else
10453 {
10454 if (mips_opts.micromips)
10455 micromips_label_expr (&label_expr);
10456 else
10457 label_expr.X_add_number = 8;
10458 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
10459 macro_build (NULL, "nop", "");
10460
10461 /* We want to close the noreorder block as soon as possible, so
10462 that later insns are available for delay slot filling. */
10463 end_noreorder ();
10464
10465 macro_build (NULL, "break", BRK_FMT, 6);
10466 }
10467 if (mips_opts.micromips)
10468 micromips_add_label ();
10469 macro_build (NULL, s, MFHL_FMT, op[0]);
10470 break;
10471
10472 case M_DIV_3I:
10473 s = "div";
10474 s2 = "mflo";
10475 goto do_divi;
10476 case M_DIVU_3I:
10477 s = "divu";
10478 s2 = "mflo";
10479 goto do_divi;
10480 case M_REM_3I:
10481 s = "div";
10482 s2 = "mfhi";
10483 goto do_divi;
10484 case M_REMU_3I:
10485 s = "divu";
10486 s2 = "mfhi";
10487 goto do_divi;
10488 case M_DDIV_3I:
10489 dbl = 1;
10490 s = "ddiv";
10491 s2 = "mflo";
10492 goto do_divi;
10493 case M_DDIVU_3I:
10494 dbl = 1;
10495 s = "ddivu";
10496 s2 = "mflo";
10497 goto do_divi;
10498 case M_DREM_3I:
10499 dbl = 1;
10500 s = "ddiv";
10501 s2 = "mfhi";
10502 goto do_divi;
10503 case M_DREMU_3I:
10504 dbl = 1;
10505 s = "ddivu";
10506 s2 = "mfhi";
10507 do_divi:
10508 if (imm_expr.X_add_number == 0)
10509 {
10510 as_warn (_("divide by zero"));
10511 if (mips_trap)
10512 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10513 else
10514 macro_build (NULL, "break", BRK_FMT, 7);
10515 break;
10516 }
10517 if (imm_expr.X_add_number == 1)
10518 {
10519 if (strcmp (s2, "mflo") == 0)
10520 move_register (op[0], op[1]);
10521 else
10522 move_register (op[0], ZERO);
10523 break;
10524 }
10525 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
10526 {
10527 if (strcmp (s2, "mflo") == 0)
10528 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
10529 else
10530 move_register (op[0], ZERO);
10531 break;
10532 }
10533
10534 used_at = 1;
10535 load_register (AT, &imm_expr, dbl);
10536 macro_build (NULL, s, "z,s,t", op[1], AT);
10537 macro_build (NULL, s2, MFHL_FMT, op[0]);
10538 break;
10539
10540 case M_DIVU_3:
10541 s = "divu";
10542 s2 = "mflo";
10543 goto do_divu3;
10544 case M_REMU_3:
10545 s = "divu";
10546 s2 = "mfhi";
10547 goto do_divu3;
10548 case M_DDIVU_3:
10549 s = "ddivu";
10550 s2 = "mflo";
10551 goto do_divu3;
10552 case M_DREMU_3:
10553 s = "ddivu";
10554 s2 = "mfhi";
10555 do_divu3:
10556 start_noreorder ();
10557 if (mips_trap)
10558 {
10559 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10560 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10561 /* We want to close the noreorder block as soon as possible, so
10562 that later insns are available for delay slot filling. */
10563 end_noreorder ();
10564 }
10565 else
10566 {
10567 if (mips_opts.micromips)
10568 micromips_label_expr (&label_expr);
10569 else
10570 label_expr.X_add_number = 8;
10571 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10572 macro_build (NULL, s, "z,s,t", op[1], op[2]);
10573
10574 /* We want to close the noreorder block as soon as possible, so
10575 that later insns are available for delay slot filling. */
10576 end_noreorder ();
10577 macro_build (NULL, "break", BRK_FMT, 7);
10578 if (mips_opts.micromips)
10579 micromips_add_label ();
10580 }
10581 macro_build (NULL, s2, MFHL_FMT, op[0]);
10582 break;
10583
10584 case M_DLCA_AB:
10585 dbl = 1;
10586 /* Fall through. */
10587 case M_LCA_AB:
10588 call = 1;
10589 goto do_la;
10590 case M_DLA_AB:
10591 dbl = 1;
10592 /* Fall through. */
10593 case M_LA_AB:
10594 do_la:
10595 /* Load the address of a symbol into a register. If breg is not
10596 zero, we then add a base register to it. */
10597
10598 breg = op[2];
10599 if (dbl && GPR_SIZE == 32)
10600 as_warn (_("dla used to load 32-bit register; recommend using la "
10601 "instead"));
10602
10603 if (!dbl && HAVE_64BIT_OBJECTS)
10604 as_warn (_("la used to load 64-bit address; recommend using dla "
10605 "instead"));
10606
10607 if (small_offset_p (0, align, 16))
10608 {
10609 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
10610 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10611 break;
10612 }
10613
10614 if (mips_opts.at && (op[0] == breg))
10615 {
10616 tempreg = AT;
10617 used_at = 1;
10618 }
10619 else
10620 tempreg = op[0];
10621
10622 if (offset_expr.X_op != O_symbol
10623 && offset_expr.X_op != O_constant)
10624 {
10625 as_bad (_("expression too complex"));
10626 offset_expr.X_op = O_constant;
10627 }
10628
10629 if (offset_expr.X_op == O_constant)
10630 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
10631 else if (mips_pic == NO_PIC)
10632 {
10633 /* If this is a reference to a GP relative symbol, we want
10634 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
10635 Otherwise we want
10636 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10637 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10638 If we have a constant, we need two instructions anyhow,
10639 so we may as well always use the latter form.
10640
10641 With 64bit address space and a usable $at we want
10642 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10643 lui $at,<sym> (BFD_RELOC_HI16_S)
10644 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10645 daddiu $at,<sym> (BFD_RELOC_LO16)
10646 dsll32 $tempreg,0
10647 daddu $tempreg,$tempreg,$at
10648
10649 If $at is already in use, we use a path which is suboptimal
10650 on superscalar processors.
10651 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10652 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10653 dsll $tempreg,16
10654 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10655 dsll $tempreg,16
10656 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
10657
10658 For GP relative symbols in 64bit address space we can use
10659 the same sequence as in 32bit address space. */
10660 if (HAVE_64BIT_SYMBOLS)
10661 {
10662 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10663 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10664 {
10665 relax_start (offset_expr.X_add_symbol);
10666 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10667 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10668 relax_switch ();
10669 }
10670
10671 if (used_at == 0 && mips_opts.at)
10672 {
10673 macro_build (&offset_expr, "lui", LUI_FMT,
10674 tempreg, BFD_RELOC_MIPS_HIGHEST);
10675 macro_build (&offset_expr, "lui", LUI_FMT,
10676 AT, BFD_RELOC_HI16_S);
10677 macro_build (&offset_expr, "daddiu", "t,r,j",
10678 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10679 macro_build (&offset_expr, "daddiu", "t,r,j",
10680 AT, AT, BFD_RELOC_LO16);
10681 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
10682 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
10683 used_at = 1;
10684 }
10685 else
10686 {
10687 macro_build (&offset_expr, "lui", LUI_FMT,
10688 tempreg, BFD_RELOC_MIPS_HIGHEST);
10689 macro_build (&offset_expr, "daddiu", "t,r,j",
10690 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10691 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10692 macro_build (&offset_expr, "daddiu", "t,r,j",
10693 tempreg, tempreg, BFD_RELOC_HI16_S);
10694 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10695 macro_build (&offset_expr, "daddiu", "t,r,j",
10696 tempreg, tempreg, BFD_RELOC_LO16);
10697 }
10698
10699 if (mips_relax.sequence)
10700 relax_end ();
10701 }
10702 else
10703 {
10704 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10705 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10706 {
10707 relax_start (offset_expr.X_add_symbol);
10708 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10709 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10710 relax_switch ();
10711 }
10712 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
10713 as_bad (_("offset too large"));
10714 macro_build_lui (&offset_expr, tempreg);
10715 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10716 tempreg, tempreg, BFD_RELOC_LO16);
10717 if (mips_relax.sequence)
10718 relax_end ();
10719 }
10720 }
10721 else if (!mips_big_got && !HAVE_NEWABI)
10722 {
10723 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10724
10725 /* If this is a reference to an external symbol, and there
10726 is no constant, we want
10727 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10728 or for lca or if tempreg is PIC_CALL_REG
10729 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10730 For a local symbol, we want
10731 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10732 nop
10733 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10734
10735 If we have a small constant, and this is a reference to
10736 an external symbol, we want
10737 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10738 nop
10739 addiu $tempreg,$tempreg,<constant>
10740 For a local symbol, we want the same instruction
10741 sequence, but we output a BFD_RELOC_LO16 reloc on the
10742 addiu instruction.
10743
10744 If we have a large constant, and this is a reference to
10745 an external symbol, we want
10746 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10747 lui $at,<hiconstant>
10748 addiu $at,$at,<loconstant>
10749 addu $tempreg,$tempreg,$at
10750 For a local symbol, we want the same instruction
10751 sequence, but we output a BFD_RELOC_LO16 reloc on the
10752 addiu instruction.
10753 */
10754
10755 if (offset_expr.X_add_number == 0)
10756 {
10757 if (mips_pic == SVR4_PIC
10758 && breg == 0
10759 && (call || tempreg == PIC_CALL_REG))
10760 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10761
10762 relax_start (offset_expr.X_add_symbol);
10763 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10764 lw_reloc_type, mips_gp_register);
10765 if (breg != 0)
10766 {
10767 /* We're going to put in an addu instruction using
10768 tempreg, so we may as well insert the nop right
10769 now. */
10770 load_delay_nop ();
10771 }
10772 relax_switch ();
10773 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10774 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
10775 load_delay_nop ();
10776 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10777 tempreg, tempreg, BFD_RELOC_LO16);
10778 relax_end ();
10779 /* FIXME: If breg == 0, and the next instruction uses
10780 $tempreg, then if this variant case is used an extra
10781 nop will be generated. */
10782 }
10783 else if (offset_expr.X_add_number >= -0x8000
10784 && offset_expr.X_add_number < 0x8000)
10785 {
10786 load_got_offset (tempreg, &offset_expr);
10787 load_delay_nop ();
10788 add_got_offset (tempreg, &offset_expr);
10789 }
10790 else
10791 {
10792 expr1.X_add_number = offset_expr.X_add_number;
10793 offset_expr.X_add_number =
10794 SEXT_16BIT (offset_expr.X_add_number);
10795 load_got_offset (tempreg, &offset_expr);
10796 offset_expr.X_add_number = expr1.X_add_number;
10797 /* If we are going to add in a base register, and the
10798 target register and the base register are the same,
10799 then we are using AT as a temporary register. Since
10800 we want to load the constant into AT, we add our
10801 current AT (from the global offset table) and the
10802 register into the register now, and pretend we were
10803 not using a base register. */
10804 if (breg == op[0])
10805 {
10806 load_delay_nop ();
10807 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10808 op[0], AT, breg);
10809 breg = 0;
10810 tempreg = op[0];
10811 }
10812 add_got_offset_hilo (tempreg, &offset_expr, AT);
10813 used_at = 1;
10814 }
10815 }
10816 else if (!mips_big_got && HAVE_NEWABI)
10817 {
10818 int add_breg_early = 0;
10819
10820 /* If this is a reference to an external, and there is no
10821 constant, or local symbol (*), with or without a
10822 constant, we want
10823 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10824 or for lca or if tempreg is PIC_CALL_REG
10825 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10826
10827 If we have a small constant, and this is a reference to
10828 an external symbol, we want
10829 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10830 addiu $tempreg,$tempreg,<constant>
10831
10832 If we have a large constant, and this is a reference to
10833 an external symbol, we want
10834 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10835 lui $at,<hiconstant>
10836 addiu $at,$at,<loconstant>
10837 addu $tempreg,$tempreg,$at
10838
10839 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
10840 local symbols, even though it introduces an additional
10841 instruction. */
10842
10843 if (offset_expr.X_add_number)
10844 {
10845 expr1.X_add_number = offset_expr.X_add_number;
10846 offset_expr.X_add_number = 0;
10847
10848 relax_start (offset_expr.X_add_symbol);
10849 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10850 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10851
10852 if (expr1.X_add_number >= -0x8000
10853 && expr1.X_add_number < 0x8000)
10854 {
10855 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10856 tempreg, tempreg, BFD_RELOC_LO16);
10857 }
10858 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
10859 {
10860 unsigned int dreg;
10861
10862 /* If we are going to add in a base register, and the
10863 target register and the base register are the same,
10864 then we are using AT as a temporary register. Since
10865 we want to load the constant into AT, we add our
10866 current AT (from the global offset table) and the
10867 register into the register now, and pretend we were
10868 not using a base register. */
10869 if (breg != op[0])
10870 dreg = tempreg;
10871 else
10872 {
10873 gas_assert (tempreg == AT);
10874 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10875 op[0], AT, breg);
10876 dreg = op[0];
10877 add_breg_early = 1;
10878 }
10879
10880 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
10881 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10882 dreg, dreg, AT);
10883
10884 used_at = 1;
10885 }
10886 else
10887 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
10888
10889 relax_switch ();
10890 offset_expr.X_add_number = expr1.X_add_number;
10891
10892 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10893 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10894 if (add_breg_early)
10895 {
10896 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10897 op[0], tempreg, breg);
10898 breg = 0;
10899 tempreg = op[0];
10900 }
10901 relax_end ();
10902 }
10903 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
10904 {
10905 relax_start (offset_expr.X_add_symbol);
10906 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10907 BFD_RELOC_MIPS_CALL16, mips_gp_register);
10908 relax_switch ();
10909 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10910 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10911 relax_end ();
10912 }
10913 else
10914 {
10915 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10916 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10917 }
10918 }
10919 else if (mips_big_got && !HAVE_NEWABI)
10920 {
10921 int gpdelay;
10922 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10923 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
10924 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10925
10926 /* This is the large GOT case. If this is a reference to an
10927 external symbol, and there is no constant, we want
10928 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10929 addu $tempreg,$tempreg,$gp
10930 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10931 or for lca or if tempreg is PIC_CALL_REG
10932 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10933 addu $tempreg,$tempreg,$gp
10934 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
10935 For a local symbol, we want
10936 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10937 nop
10938 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10939
10940 If we have a small constant, and this is a reference to
10941 an external symbol, we want
10942 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10943 addu $tempreg,$tempreg,$gp
10944 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10945 nop
10946 addiu $tempreg,$tempreg,<constant>
10947 For a local symbol, we want
10948 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10949 nop
10950 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
10951
10952 If we have a large constant, and this is a reference to
10953 an external symbol, we want
10954 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10955 addu $tempreg,$tempreg,$gp
10956 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10957 lui $at,<hiconstant>
10958 addiu $at,$at,<loconstant>
10959 addu $tempreg,$tempreg,$at
10960 For a local symbol, we want
10961 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10962 lui $at,<hiconstant>
10963 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
10964 addu $tempreg,$tempreg,$at
10965 */
10966
10967 expr1.X_add_number = offset_expr.X_add_number;
10968 offset_expr.X_add_number = 0;
10969 relax_start (offset_expr.X_add_symbol);
10970 gpdelay = reg_needs_delay (mips_gp_register);
10971 if (expr1.X_add_number == 0 && breg == 0
10972 && (call || tempreg == PIC_CALL_REG))
10973 {
10974 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10975 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10976 }
10977 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
10978 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10979 tempreg, tempreg, mips_gp_register);
10980 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10981 tempreg, lw_reloc_type, tempreg);
10982 if (expr1.X_add_number == 0)
10983 {
10984 if (breg != 0)
10985 {
10986 /* We're going to put in an addu instruction using
10987 tempreg, so we may as well insert the nop right
10988 now. */
10989 load_delay_nop ();
10990 }
10991 }
10992 else if (expr1.X_add_number >= -0x8000
10993 && expr1.X_add_number < 0x8000)
10994 {
10995 load_delay_nop ();
10996 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10997 tempreg, tempreg, BFD_RELOC_LO16);
10998 }
10999 else
11000 {
11001 unsigned int dreg;
11002
11003 /* If we are going to add in a base register, and the
11004 target register and the base register are the same,
11005 then we are using AT as a temporary register. Since
11006 we want to load the constant into AT, we add our
11007 current AT (from the global offset table) and the
11008 register into the register now, and pretend we were
11009 not using a base register. */
11010 if (breg != op[0])
11011 dreg = tempreg;
11012 else
11013 {
11014 gas_assert (tempreg == AT);
11015 load_delay_nop ();
11016 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11017 op[0], AT, breg);
11018 dreg = op[0];
11019 }
11020
11021 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11022 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11023
11024 used_at = 1;
11025 }
11026 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
11027 relax_switch ();
11028
11029 if (gpdelay)
11030 {
11031 /* This is needed because this instruction uses $gp, but
11032 the first instruction on the main stream does not. */
11033 macro_build (NULL, "nop", "");
11034 }
11035
11036 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11037 local_reloc_type, mips_gp_register);
11038 if (expr1.X_add_number >= -0x8000
11039 && expr1.X_add_number < 0x8000)
11040 {
11041 load_delay_nop ();
11042 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11043 tempreg, tempreg, BFD_RELOC_LO16);
11044 /* FIXME: If add_number is 0, and there was no base
11045 register, the external symbol case ended with a load,
11046 so if the symbol turns out to not be external, and
11047 the next instruction uses tempreg, an unnecessary nop
11048 will be inserted. */
11049 }
11050 else
11051 {
11052 if (breg == op[0])
11053 {
11054 /* We must add in the base register now, as in the
11055 external symbol case. */
11056 gas_assert (tempreg == AT);
11057 load_delay_nop ();
11058 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11059 op[0], AT, breg);
11060 tempreg = op[0];
11061 /* We set breg to 0 because we have arranged to add
11062 it in in both cases. */
11063 breg = 0;
11064 }
11065
11066 macro_build_lui (&expr1, AT);
11067 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11068 AT, AT, BFD_RELOC_LO16);
11069 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11070 tempreg, tempreg, AT);
11071 used_at = 1;
11072 }
11073 relax_end ();
11074 }
11075 else if (mips_big_got && HAVE_NEWABI)
11076 {
11077 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11078 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11079 int add_breg_early = 0;
11080
11081 /* This is the large GOT case. If this is a reference to an
11082 external symbol, and there is no constant, we want
11083 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11084 add $tempreg,$tempreg,$gp
11085 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11086 or for lca or if tempreg is PIC_CALL_REG
11087 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11088 add $tempreg,$tempreg,$gp
11089 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11090
11091 If we have a small constant, and this is a reference to
11092 an external symbol, we want
11093 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11094 add $tempreg,$tempreg,$gp
11095 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11096 addi $tempreg,$tempreg,<constant>
11097
11098 If we have a large constant, and this is a reference to
11099 an external symbol, we want
11100 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11101 addu $tempreg,$tempreg,$gp
11102 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11103 lui $at,<hiconstant>
11104 addi $at,$at,<loconstant>
11105 add $tempreg,$tempreg,$at
11106
11107 If we have NewABI, and we know it's a local symbol, we want
11108 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11109 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11110 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11111
11112 relax_start (offset_expr.X_add_symbol);
11113
11114 expr1.X_add_number = offset_expr.X_add_number;
11115 offset_expr.X_add_number = 0;
11116
11117 if (expr1.X_add_number == 0 && breg == 0
11118 && (call || tempreg == PIC_CALL_REG))
11119 {
11120 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11121 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11122 }
11123 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11124 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11125 tempreg, tempreg, mips_gp_register);
11126 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11127 tempreg, lw_reloc_type, tempreg);
11128
11129 if (expr1.X_add_number == 0)
11130 ;
11131 else if (expr1.X_add_number >= -0x8000
11132 && expr1.X_add_number < 0x8000)
11133 {
11134 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11135 tempreg, tempreg, BFD_RELOC_LO16);
11136 }
11137 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11138 {
11139 unsigned int dreg;
11140
11141 /* If we are going to add in a base register, and the
11142 target register and the base register are the same,
11143 then we are using AT as a temporary register. Since
11144 we want to load the constant into AT, we add our
11145 current AT (from the global offset table) and the
11146 register into the register now, and pretend we were
11147 not using a base register. */
11148 if (breg != op[0])
11149 dreg = tempreg;
11150 else
11151 {
11152 gas_assert (tempreg == AT);
11153 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11154 op[0], AT, breg);
11155 dreg = op[0];
11156 add_breg_early = 1;
11157 }
11158
11159 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11160 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11161
11162 used_at = 1;
11163 }
11164 else
11165 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11166
11167 relax_switch ();
11168 offset_expr.X_add_number = expr1.X_add_number;
11169 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11170 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11171 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11172 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11173 if (add_breg_early)
11174 {
11175 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11176 op[0], tempreg, breg);
11177 breg = 0;
11178 tempreg = op[0];
11179 }
11180 relax_end ();
11181 }
11182 else
11183 abort ();
11184
11185 if (breg != 0)
11186 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
11187 break;
11188
11189 case M_MSGSND:
11190 gas_assert (!mips_opts.micromips);
11191 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
11192 break;
11193
11194 case M_MSGLD:
11195 gas_assert (!mips_opts.micromips);
11196 macro_build (NULL, "c2", "C", 0x02);
11197 break;
11198
11199 case M_MSGLD_T:
11200 gas_assert (!mips_opts.micromips);
11201 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
11202 break;
11203
11204 case M_MSGWAIT:
11205 gas_assert (!mips_opts.micromips);
11206 macro_build (NULL, "c2", "C", 3);
11207 break;
11208
11209 case M_MSGWAIT_T:
11210 gas_assert (!mips_opts.micromips);
11211 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
11212 break;
11213
11214 case M_J_A:
11215 /* The j instruction may not be used in PIC code, since it
11216 requires an absolute address. We convert it to a b
11217 instruction. */
11218 if (mips_pic == NO_PIC)
11219 macro_build (&offset_expr, "j", "a");
11220 else
11221 macro_build (&offset_expr, "b", "p");
11222 break;
11223
11224 /* The jal instructions must be handled as macros because when
11225 generating PIC code they expand to multi-instruction
11226 sequences. Normally they are simple instructions. */
11227 case M_JALS_1:
11228 op[1] = op[0];
11229 op[0] = RA;
11230 /* Fall through. */
11231 case M_JALS_2:
11232 gas_assert (mips_opts.micromips);
11233 if (mips_opts.insn32)
11234 {
11235 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11236 break;
11237 }
11238 jals = 1;
11239 goto jal;
11240 case M_JAL_1:
11241 op[1] = op[0];
11242 op[0] = RA;
11243 /* Fall through. */
11244 case M_JAL_2:
11245 jal:
11246 if (mips_pic == NO_PIC)
11247 {
11248 s = jals ? "jalrs" : "jalr";
11249 if (mips_opts.micromips
11250 && !mips_opts.insn32
11251 && op[0] == RA
11252 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11253 macro_build (NULL, s, "mj", op[1]);
11254 else
11255 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11256 }
11257 else
11258 {
11259 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11260 && mips_cprestore_offset >= 0);
11261
11262 if (op[1] != PIC_CALL_REG)
11263 as_warn (_("MIPS PIC call to register other than $25"));
11264
11265 s = ((mips_opts.micromips
11266 && !mips_opts.insn32
11267 && (!mips_opts.noreorder || cprestore))
11268 ? "jalrs" : "jalr");
11269 if (mips_opts.micromips
11270 && !mips_opts.insn32
11271 && op[0] == RA
11272 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11273 macro_build (NULL, s, "mj", op[1]);
11274 else
11275 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11276 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
11277 {
11278 if (mips_cprestore_offset < 0)
11279 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11280 else
11281 {
11282 if (!mips_frame_reg_valid)
11283 {
11284 as_warn (_("no .frame pseudo-op used in PIC code"));
11285 /* Quiet this warning. */
11286 mips_frame_reg_valid = 1;
11287 }
11288 if (!mips_cprestore_valid)
11289 {
11290 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11291 /* Quiet this warning. */
11292 mips_cprestore_valid = 1;
11293 }
11294 if (mips_opts.noreorder)
11295 macro_build (NULL, "nop", "");
11296 expr1.X_add_number = mips_cprestore_offset;
11297 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11298 mips_gp_register,
11299 mips_frame_reg,
11300 HAVE_64BIT_ADDRESSES);
11301 }
11302 }
11303 }
11304
11305 break;
11306
11307 case M_JALS_A:
11308 gas_assert (mips_opts.micromips);
11309 if (mips_opts.insn32)
11310 {
11311 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11312 break;
11313 }
11314 jals = 1;
11315 /* Fall through. */
11316 case M_JAL_A:
11317 if (mips_pic == NO_PIC)
11318 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
11319 else if (mips_pic == SVR4_PIC)
11320 {
11321 /* If this is a reference to an external symbol, and we are
11322 using a small GOT, we want
11323 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11324 nop
11325 jalr $ra,$25
11326 nop
11327 lw $gp,cprestore($sp)
11328 The cprestore value is set using the .cprestore
11329 pseudo-op. If we are using a big GOT, we want
11330 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11331 addu $25,$25,$gp
11332 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11333 nop
11334 jalr $ra,$25
11335 nop
11336 lw $gp,cprestore($sp)
11337 If the symbol is not external, we want
11338 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11339 nop
11340 addiu $25,$25,<sym> (BFD_RELOC_LO16)
11341 jalr $ra,$25
11342 nop
11343 lw $gp,cprestore($sp)
11344
11345 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11346 sequences above, minus nops, unless the symbol is local,
11347 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11348 GOT_DISP. */
11349 if (HAVE_NEWABI)
11350 {
11351 if (!mips_big_got)
11352 {
11353 relax_start (offset_expr.X_add_symbol);
11354 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11355 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11356 mips_gp_register);
11357 relax_switch ();
11358 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11359 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
11360 mips_gp_register);
11361 relax_end ();
11362 }
11363 else
11364 {
11365 relax_start (offset_expr.X_add_symbol);
11366 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11367 BFD_RELOC_MIPS_CALL_HI16);
11368 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11369 PIC_CALL_REG, mips_gp_register);
11370 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11371 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11372 PIC_CALL_REG);
11373 relax_switch ();
11374 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11375 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11376 mips_gp_register);
11377 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11378 PIC_CALL_REG, PIC_CALL_REG,
11379 BFD_RELOC_MIPS_GOT_OFST);
11380 relax_end ();
11381 }
11382
11383 macro_build_jalr (&offset_expr, 0);
11384 }
11385 else
11386 {
11387 relax_start (offset_expr.X_add_symbol);
11388 if (!mips_big_got)
11389 {
11390 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11391 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11392 mips_gp_register);
11393 load_delay_nop ();
11394 relax_switch ();
11395 }
11396 else
11397 {
11398 int gpdelay;
11399
11400 gpdelay = reg_needs_delay (mips_gp_register);
11401 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11402 BFD_RELOC_MIPS_CALL_HI16);
11403 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11404 PIC_CALL_REG, mips_gp_register);
11405 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11406 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11407 PIC_CALL_REG);
11408 load_delay_nop ();
11409 relax_switch ();
11410 if (gpdelay)
11411 macro_build (NULL, "nop", "");
11412 }
11413 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11414 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
11415 mips_gp_register);
11416 load_delay_nop ();
11417 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11418 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
11419 relax_end ();
11420 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
11421
11422 if (mips_cprestore_offset < 0)
11423 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11424 else
11425 {
11426 if (!mips_frame_reg_valid)
11427 {
11428 as_warn (_("no .frame pseudo-op used in PIC code"));
11429 /* Quiet this warning. */
11430 mips_frame_reg_valid = 1;
11431 }
11432 if (!mips_cprestore_valid)
11433 {
11434 as_warn (_("no .cprestore pseudo-op used in PIC code"));
11435 /* Quiet this warning. */
11436 mips_cprestore_valid = 1;
11437 }
11438 if (mips_opts.noreorder)
11439 macro_build (NULL, "nop", "");
11440 expr1.X_add_number = mips_cprestore_offset;
11441 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11442 mips_gp_register,
11443 mips_frame_reg,
11444 HAVE_64BIT_ADDRESSES);
11445 }
11446 }
11447 }
11448 else if (mips_pic == VXWORKS_PIC)
11449 as_bad (_("non-PIC jump used in PIC library"));
11450 else
11451 abort ();
11452
11453 break;
11454
11455 case M_LBUE_AB:
11456 s = "lbue";
11457 fmt = "t,+j(b)";
11458 offbits = 9;
11459 goto ld_st;
11460 case M_LHUE_AB:
11461 s = "lhue";
11462 fmt = "t,+j(b)";
11463 offbits = 9;
11464 goto ld_st;
11465 case M_LBE_AB:
11466 s = "lbe";
11467 fmt = "t,+j(b)";
11468 offbits = 9;
11469 goto ld_st;
11470 case M_LHE_AB:
11471 s = "lhe";
11472 fmt = "t,+j(b)";
11473 offbits = 9;
11474 goto ld_st;
11475 case M_LLE_AB:
11476 s = "lle";
11477 fmt = "t,+j(b)";
11478 offbits = 9;
11479 goto ld_st;
11480 case M_LWE_AB:
11481 s = "lwe";
11482 fmt = "t,+j(b)";
11483 offbits = 9;
11484 goto ld_st;
11485 case M_LWLE_AB:
11486 s = "lwle";
11487 fmt = "t,+j(b)";
11488 offbits = 9;
11489 goto ld_st;
11490 case M_LWRE_AB:
11491 s = "lwre";
11492 fmt = "t,+j(b)";
11493 offbits = 9;
11494 goto ld_st;
11495 case M_SBE_AB:
11496 s = "sbe";
11497 fmt = "t,+j(b)";
11498 offbits = 9;
11499 goto ld_st;
11500 case M_SCE_AB:
11501 s = "sce";
11502 fmt = "t,+j(b)";
11503 offbits = 9;
11504 goto ld_st;
11505 case M_SHE_AB:
11506 s = "she";
11507 fmt = "t,+j(b)";
11508 offbits = 9;
11509 goto ld_st;
11510 case M_SWE_AB:
11511 s = "swe";
11512 fmt = "t,+j(b)";
11513 offbits = 9;
11514 goto ld_st;
11515 case M_SWLE_AB:
11516 s = "swle";
11517 fmt = "t,+j(b)";
11518 offbits = 9;
11519 goto ld_st;
11520 case M_SWRE_AB:
11521 s = "swre";
11522 fmt = "t,+j(b)";
11523 offbits = 9;
11524 goto ld_st;
11525 case M_ACLR_AB:
11526 s = "aclr";
11527 fmt = "\\,~(b)";
11528 offbits = 12;
11529 goto ld_st;
11530 case M_ASET_AB:
11531 s = "aset";
11532 fmt = "\\,~(b)";
11533 offbits = 12;
11534 goto ld_st;
11535 case M_LB_AB:
11536 s = "lb";
11537 fmt = "t,o(b)";
11538 goto ld;
11539 case M_LBU_AB:
11540 s = "lbu";
11541 fmt = "t,o(b)";
11542 goto ld;
11543 case M_LH_AB:
11544 s = "lh";
11545 fmt = "t,o(b)";
11546 goto ld;
11547 case M_LHU_AB:
11548 s = "lhu";
11549 fmt = "t,o(b)";
11550 goto ld;
11551 case M_LW_AB:
11552 s = "lw";
11553 fmt = "t,o(b)";
11554 goto ld;
11555 case M_LWC0_AB:
11556 gas_assert (!mips_opts.micromips);
11557 s = "lwc0";
11558 fmt = "E,o(b)";
11559 /* Itbl support may require additional care here. */
11560 coproc = 1;
11561 goto ld_st;
11562 case M_LWC1_AB:
11563 s = "lwc1";
11564 fmt = "T,o(b)";
11565 /* Itbl support may require additional care here. */
11566 coproc = 1;
11567 goto ld_st;
11568 case M_LWC2_AB:
11569 s = "lwc2";
11570 fmt = COP12_FMT;
11571 offbits = (mips_opts.micromips ? 12
11572 : ISA_IS_R6 (mips_opts.isa) ? 11
11573 : 16);
11574 /* Itbl support may require additional care here. */
11575 coproc = 1;
11576 goto ld_st;
11577 case M_LWC3_AB:
11578 gas_assert (!mips_opts.micromips);
11579 s = "lwc3";
11580 fmt = "E,o(b)";
11581 /* Itbl support may require additional care here. */
11582 coproc = 1;
11583 goto ld_st;
11584 case M_LWL_AB:
11585 s = "lwl";
11586 fmt = MEM12_FMT;
11587 offbits = (mips_opts.micromips ? 12 : 16);
11588 goto ld_st;
11589 case M_LWR_AB:
11590 s = "lwr";
11591 fmt = MEM12_FMT;
11592 offbits = (mips_opts.micromips ? 12 : 16);
11593 goto ld_st;
11594 case M_LDC1_AB:
11595 s = "ldc1";
11596 fmt = "T,o(b)";
11597 /* Itbl support may require additional care here. */
11598 coproc = 1;
11599 goto ld_st;
11600 case M_LDC2_AB:
11601 s = "ldc2";
11602 fmt = COP12_FMT;
11603 offbits = (mips_opts.micromips ? 12
11604 : ISA_IS_R6 (mips_opts.isa) ? 11
11605 : 16);
11606 /* Itbl support may require additional care here. */
11607 coproc = 1;
11608 goto ld_st;
11609 case M_LQC2_AB:
11610 s = "lqc2";
11611 fmt = "+7,o(b)";
11612 /* Itbl support may require additional care here. */
11613 coproc = 1;
11614 goto ld_st;
11615 case M_LDC3_AB:
11616 s = "ldc3";
11617 fmt = "E,o(b)";
11618 /* Itbl support may require additional care here. */
11619 coproc = 1;
11620 goto ld_st;
11621 case M_LDL_AB:
11622 s = "ldl";
11623 fmt = MEM12_FMT;
11624 offbits = (mips_opts.micromips ? 12 : 16);
11625 goto ld_st;
11626 case M_LDR_AB:
11627 s = "ldr";
11628 fmt = MEM12_FMT;
11629 offbits = (mips_opts.micromips ? 12 : 16);
11630 goto ld_st;
11631 case M_LL_AB:
11632 s = "ll";
11633 fmt = LL_SC_FMT;
11634 offbits = (mips_opts.micromips ? 12
11635 : ISA_IS_R6 (mips_opts.isa) ? 9
11636 : 16);
11637 goto ld;
11638 case M_LLD_AB:
11639 s = "lld";
11640 fmt = LL_SC_FMT;
11641 offbits = (mips_opts.micromips ? 12
11642 : ISA_IS_R6 (mips_opts.isa) ? 9
11643 : 16);
11644 goto ld;
11645 case M_LWU_AB:
11646 s = "lwu";
11647 fmt = MEM12_FMT;
11648 offbits = (mips_opts.micromips ? 12 : 16);
11649 goto ld;
11650 case M_LWP_AB:
11651 gas_assert (mips_opts.micromips);
11652 s = "lwp";
11653 fmt = "t,~(b)";
11654 offbits = 12;
11655 lp = 1;
11656 goto ld;
11657 case M_LDP_AB:
11658 gas_assert (mips_opts.micromips);
11659 s = "ldp";
11660 fmt = "t,~(b)";
11661 offbits = 12;
11662 lp = 1;
11663 goto ld;
11664 case M_LWM_AB:
11665 gas_assert (mips_opts.micromips);
11666 s = "lwm";
11667 fmt = "n,~(b)";
11668 offbits = 12;
11669 goto ld_st;
11670 case M_LDM_AB:
11671 gas_assert (mips_opts.micromips);
11672 s = "ldm";
11673 fmt = "n,~(b)";
11674 offbits = 12;
11675 goto ld_st;
11676
11677 ld:
11678 /* We don't want to use $0 as tempreg. */
11679 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
11680 goto ld_st;
11681 else
11682 tempreg = op[0] + lp;
11683 goto ld_noat;
11684
11685 case M_SB_AB:
11686 s = "sb";
11687 fmt = "t,o(b)";
11688 goto ld_st;
11689 case M_SH_AB:
11690 s = "sh";
11691 fmt = "t,o(b)";
11692 goto ld_st;
11693 case M_SW_AB:
11694 s = "sw";
11695 fmt = "t,o(b)";
11696 goto ld_st;
11697 case M_SWC0_AB:
11698 gas_assert (!mips_opts.micromips);
11699 s = "swc0";
11700 fmt = "E,o(b)";
11701 /* Itbl support may require additional care here. */
11702 coproc = 1;
11703 goto ld_st;
11704 case M_SWC1_AB:
11705 s = "swc1";
11706 fmt = "T,o(b)";
11707 /* Itbl support may require additional care here. */
11708 coproc = 1;
11709 goto ld_st;
11710 case M_SWC2_AB:
11711 s = "swc2";
11712 fmt = COP12_FMT;
11713 offbits = (mips_opts.micromips ? 12
11714 : ISA_IS_R6 (mips_opts.isa) ? 11
11715 : 16);
11716 /* Itbl support may require additional care here. */
11717 coproc = 1;
11718 goto ld_st;
11719 case M_SWC3_AB:
11720 gas_assert (!mips_opts.micromips);
11721 s = "swc3";
11722 fmt = "E,o(b)";
11723 /* Itbl support may require additional care here. */
11724 coproc = 1;
11725 goto ld_st;
11726 case M_SWL_AB:
11727 s = "swl";
11728 fmt = MEM12_FMT;
11729 offbits = (mips_opts.micromips ? 12 : 16);
11730 goto ld_st;
11731 case M_SWR_AB:
11732 s = "swr";
11733 fmt = MEM12_FMT;
11734 offbits = (mips_opts.micromips ? 12 : 16);
11735 goto ld_st;
11736 case M_SC_AB:
11737 s = "sc";
11738 fmt = LL_SC_FMT;
11739 offbits = (mips_opts.micromips ? 12
11740 : ISA_IS_R6 (mips_opts.isa) ? 9
11741 : 16);
11742 goto ld_st;
11743 case M_SCD_AB:
11744 s = "scd";
11745 fmt = LL_SC_FMT;
11746 offbits = (mips_opts.micromips ? 12
11747 : ISA_IS_R6 (mips_opts.isa) ? 9
11748 : 16);
11749 goto ld_st;
11750 case M_CACHE_AB:
11751 s = "cache";
11752 fmt = (mips_opts.micromips ? "k,~(b)"
11753 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11754 : "k,o(b)");
11755 offbits = (mips_opts.micromips ? 12
11756 : ISA_IS_R6 (mips_opts.isa) ? 9
11757 : 16);
11758 goto ld_st;
11759 case M_CACHEE_AB:
11760 s = "cachee";
11761 fmt = "k,+j(b)";
11762 offbits = 9;
11763 goto ld_st;
11764 case M_PREF_AB:
11765 s = "pref";
11766 fmt = (mips_opts.micromips ? "k,~(b)"
11767 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11768 : "k,o(b)");
11769 offbits = (mips_opts.micromips ? 12
11770 : ISA_IS_R6 (mips_opts.isa) ? 9
11771 : 16);
11772 goto ld_st;
11773 case M_PREFE_AB:
11774 s = "prefe";
11775 fmt = "k,+j(b)";
11776 offbits = 9;
11777 goto ld_st;
11778 case M_SDC1_AB:
11779 s = "sdc1";
11780 fmt = "T,o(b)";
11781 coproc = 1;
11782 /* Itbl support may require additional care here. */
11783 goto ld_st;
11784 case M_SDC2_AB:
11785 s = "sdc2";
11786 fmt = COP12_FMT;
11787 offbits = (mips_opts.micromips ? 12
11788 : ISA_IS_R6 (mips_opts.isa) ? 11
11789 : 16);
11790 /* Itbl support may require additional care here. */
11791 coproc = 1;
11792 goto ld_st;
11793 case M_SQC2_AB:
11794 s = "sqc2";
11795 fmt = "+7,o(b)";
11796 /* Itbl support may require additional care here. */
11797 coproc = 1;
11798 goto ld_st;
11799 case M_SDC3_AB:
11800 gas_assert (!mips_opts.micromips);
11801 s = "sdc3";
11802 fmt = "E,o(b)";
11803 /* Itbl support may require additional care here. */
11804 coproc = 1;
11805 goto ld_st;
11806 case M_SDL_AB:
11807 s = "sdl";
11808 fmt = MEM12_FMT;
11809 offbits = (mips_opts.micromips ? 12 : 16);
11810 goto ld_st;
11811 case M_SDR_AB:
11812 s = "sdr";
11813 fmt = MEM12_FMT;
11814 offbits = (mips_opts.micromips ? 12 : 16);
11815 goto ld_st;
11816 case M_SWP_AB:
11817 gas_assert (mips_opts.micromips);
11818 s = "swp";
11819 fmt = "t,~(b)";
11820 offbits = 12;
11821 goto ld_st;
11822 case M_SDP_AB:
11823 gas_assert (mips_opts.micromips);
11824 s = "sdp";
11825 fmt = "t,~(b)";
11826 offbits = 12;
11827 goto ld_st;
11828 case M_SWM_AB:
11829 gas_assert (mips_opts.micromips);
11830 s = "swm";
11831 fmt = "n,~(b)";
11832 offbits = 12;
11833 goto ld_st;
11834 case M_SDM_AB:
11835 gas_assert (mips_opts.micromips);
11836 s = "sdm";
11837 fmt = "n,~(b)";
11838 offbits = 12;
11839
11840 ld_st:
11841 tempreg = AT;
11842 ld_noat:
11843 breg = op[2];
11844 if (small_offset_p (0, align, 16))
11845 {
11846 /* The first case exists for M_LD_AB and M_SD_AB, which are
11847 macros for o32 but which should act like normal instructions
11848 otherwise. */
11849 if (offbits == 16)
11850 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
11851 offset_reloc[1], offset_reloc[2], breg);
11852 else if (small_offset_p (0, align, offbits))
11853 {
11854 if (offbits == 0)
11855 macro_build (NULL, s, fmt, op[0], breg);
11856 else
11857 macro_build (NULL, s, fmt, op[0],
11858 (int) offset_expr.X_add_number, breg);
11859 }
11860 else
11861 {
11862 if (tempreg == AT)
11863 used_at = 1;
11864 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11865 tempreg, breg, -1, offset_reloc[0],
11866 offset_reloc[1], offset_reloc[2]);
11867 if (offbits == 0)
11868 macro_build (NULL, s, fmt, op[0], tempreg);
11869 else
11870 macro_build (NULL, s, fmt, op[0], 0, tempreg);
11871 }
11872 break;
11873 }
11874
11875 if (tempreg == AT)
11876 used_at = 1;
11877
11878 if (offset_expr.X_op != O_constant
11879 && offset_expr.X_op != O_symbol)
11880 {
11881 as_bad (_("expression too complex"));
11882 offset_expr.X_op = O_constant;
11883 }
11884
11885 if (HAVE_32BIT_ADDRESSES
11886 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
11887 {
11888 char value [32];
11889
11890 sprintf_vma (value, offset_expr.X_add_number);
11891 as_bad (_("number (0x%s) larger than 32 bits"), value);
11892 }
11893
11894 /* A constant expression in PIC code can be handled just as it
11895 is in non PIC code. */
11896 if (offset_expr.X_op == O_constant)
11897 {
11898 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
11899 offbits == 0 ? 16 : offbits);
11900 offset_expr.X_add_number -= expr1.X_add_number;
11901
11902 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
11903 if (breg != 0)
11904 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11905 tempreg, tempreg, breg);
11906 if (offbits == 0)
11907 {
11908 if (offset_expr.X_add_number != 0)
11909 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
11910 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
11911 macro_build (NULL, s, fmt, op[0], tempreg);
11912 }
11913 else if (offbits == 16)
11914 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
11915 else
11916 macro_build (NULL, s, fmt, op[0],
11917 (int) offset_expr.X_add_number, tempreg);
11918 }
11919 else if (offbits != 16)
11920 {
11921 /* The offset field is too narrow to be used for a low-part
11922 relocation, so load the whole address into the auxiliary
11923 register. */
11924 load_address (tempreg, &offset_expr, &used_at);
11925 if (breg != 0)
11926 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11927 tempreg, tempreg, breg);
11928 if (offbits == 0)
11929 macro_build (NULL, s, fmt, op[0], tempreg);
11930 else
11931 macro_build (NULL, s, fmt, op[0], 0, tempreg);
11932 }
11933 else if (mips_pic == NO_PIC)
11934 {
11935 /* If this is a reference to a GP relative symbol, and there
11936 is no base register, we want
11937 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
11938 Otherwise, if there is no base register, we want
11939 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11940 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11941 If we have a constant, we need two instructions anyhow,
11942 so we always use the latter form.
11943
11944 If we have a base register, and this is a reference to a
11945 GP relative symbol, we want
11946 addu $tempreg,$breg,$gp
11947 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
11948 Otherwise we want
11949 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11950 addu $tempreg,$tempreg,$breg
11951 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11952 With a constant we always use the latter case.
11953
11954 With 64bit address space and no base register and $at usable,
11955 we want
11956 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11957 lui $at,<sym> (BFD_RELOC_HI16_S)
11958 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11959 dsll32 $tempreg,0
11960 daddu $tempreg,$at
11961 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11962 If we have a base register, we want
11963 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11964 lui $at,<sym> (BFD_RELOC_HI16_S)
11965 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11966 daddu $at,$breg
11967 dsll32 $tempreg,0
11968 daddu $tempreg,$at
11969 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11970
11971 Without $at we can't generate the optimal path for superscalar
11972 processors here since this would require two temporary registers.
11973 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11974 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11975 dsll $tempreg,16
11976 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11977 dsll $tempreg,16
11978 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11979 If we have a base register, we want
11980 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11981 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11982 dsll $tempreg,16
11983 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11984 dsll $tempreg,16
11985 daddu $tempreg,$tempreg,$breg
11986 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
11987
11988 For GP relative symbols in 64bit address space we can use
11989 the same sequence as in 32bit address space. */
11990 if (HAVE_64BIT_SYMBOLS)
11991 {
11992 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11993 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11994 {
11995 relax_start (offset_expr.X_add_symbol);
11996 if (breg == 0)
11997 {
11998 macro_build (&offset_expr, s, fmt, op[0],
11999 BFD_RELOC_GPREL16, mips_gp_register);
12000 }
12001 else
12002 {
12003 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12004 tempreg, breg, mips_gp_register);
12005 macro_build (&offset_expr, s, fmt, op[0],
12006 BFD_RELOC_GPREL16, tempreg);
12007 }
12008 relax_switch ();
12009 }
12010
12011 if (used_at == 0 && mips_opts.at)
12012 {
12013 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12014 BFD_RELOC_MIPS_HIGHEST);
12015 macro_build (&offset_expr, "lui", LUI_FMT, AT,
12016 BFD_RELOC_HI16_S);
12017 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12018 tempreg, BFD_RELOC_MIPS_HIGHER);
12019 if (breg != 0)
12020 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
12021 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
12022 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
12023 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
12024 tempreg);
12025 used_at = 1;
12026 }
12027 else
12028 {
12029 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12030 BFD_RELOC_MIPS_HIGHEST);
12031 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12032 tempreg, BFD_RELOC_MIPS_HIGHER);
12033 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12034 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12035 tempreg, BFD_RELOC_HI16_S);
12036 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12037 if (breg != 0)
12038 macro_build (NULL, "daddu", "d,v,t",
12039 tempreg, tempreg, breg);
12040 macro_build (&offset_expr, s, fmt, op[0],
12041 BFD_RELOC_LO16, tempreg);
12042 }
12043
12044 if (mips_relax.sequence)
12045 relax_end ();
12046 break;
12047 }
12048
12049 if (breg == 0)
12050 {
12051 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12052 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12053 {
12054 relax_start (offset_expr.X_add_symbol);
12055 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
12056 mips_gp_register);
12057 relax_switch ();
12058 }
12059 macro_build_lui (&offset_expr, tempreg);
12060 macro_build (&offset_expr, s, fmt, op[0],
12061 BFD_RELOC_LO16, tempreg);
12062 if (mips_relax.sequence)
12063 relax_end ();
12064 }
12065 else
12066 {
12067 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12068 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12069 {
12070 relax_start (offset_expr.X_add_symbol);
12071 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12072 tempreg, breg, mips_gp_register);
12073 macro_build (&offset_expr, s, fmt, op[0],
12074 BFD_RELOC_GPREL16, tempreg);
12075 relax_switch ();
12076 }
12077 macro_build_lui (&offset_expr, tempreg);
12078 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12079 tempreg, tempreg, breg);
12080 macro_build (&offset_expr, s, fmt, op[0],
12081 BFD_RELOC_LO16, tempreg);
12082 if (mips_relax.sequence)
12083 relax_end ();
12084 }
12085 }
12086 else if (!mips_big_got)
12087 {
12088 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
12089
12090 /* If this is a reference to an external symbol, we want
12091 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12092 nop
12093 <op> op[0],0($tempreg)
12094 Otherwise we want
12095 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12096 nop
12097 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
12098 <op> op[0],0($tempreg)
12099
12100 For NewABI, we want
12101 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
12102 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
12103
12104 If there is a base register, we add it to $tempreg before
12105 the <op>. If there is a constant, we stick it in the
12106 <op> instruction. We don't handle constants larger than
12107 16 bits, because we have no way to load the upper 16 bits
12108 (actually, we could handle them for the subset of cases
12109 in which we are not using $at). */
12110 gas_assert (offset_expr.X_op == O_symbol);
12111 if (HAVE_NEWABI)
12112 {
12113 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12114 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12115 if (breg != 0)
12116 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12117 tempreg, tempreg, breg);
12118 macro_build (&offset_expr, s, fmt, op[0],
12119 BFD_RELOC_MIPS_GOT_OFST, tempreg);
12120 break;
12121 }
12122 expr1.X_add_number = offset_expr.X_add_number;
12123 offset_expr.X_add_number = 0;
12124 if (expr1.X_add_number < -0x8000
12125 || expr1.X_add_number >= 0x8000)
12126 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12127 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12128 lw_reloc_type, mips_gp_register);
12129 load_delay_nop ();
12130 relax_start (offset_expr.X_add_symbol);
12131 relax_switch ();
12132 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12133 tempreg, BFD_RELOC_LO16);
12134 relax_end ();
12135 if (breg != 0)
12136 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12137 tempreg, tempreg, breg);
12138 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12139 }
12140 else if (mips_big_got && !HAVE_NEWABI)
12141 {
12142 int gpdelay;
12143
12144 /* If this is a reference to an external symbol, we want
12145 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12146 addu $tempreg,$tempreg,$gp
12147 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12148 <op> op[0],0($tempreg)
12149 Otherwise we want
12150 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12151 nop
12152 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
12153 <op> op[0],0($tempreg)
12154 If there is a base register, we add it to $tempreg before
12155 the <op>. If there is a constant, we stick it in the
12156 <op> instruction. We don't handle constants larger than
12157 16 bits, because we have no way to load the upper 16 bits
12158 (actually, we could handle them for the subset of cases
12159 in which we are not using $at). */
12160 gas_assert (offset_expr.X_op == O_symbol);
12161 expr1.X_add_number = offset_expr.X_add_number;
12162 offset_expr.X_add_number = 0;
12163 if (expr1.X_add_number < -0x8000
12164 || expr1.X_add_number >= 0x8000)
12165 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12166 gpdelay = reg_needs_delay (mips_gp_register);
12167 relax_start (offset_expr.X_add_symbol);
12168 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12169 BFD_RELOC_MIPS_GOT_HI16);
12170 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12171 mips_gp_register);
12172 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12173 BFD_RELOC_MIPS_GOT_LO16, tempreg);
12174 relax_switch ();
12175 if (gpdelay)
12176 macro_build (NULL, "nop", "");
12177 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12178 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12179 load_delay_nop ();
12180 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12181 tempreg, BFD_RELOC_LO16);
12182 relax_end ();
12183
12184 if (breg != 0)
12185 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12186 tempreg, tempreg, breg);
12187 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12188 }
12189 else if (mips_big_got && HAVE_NEWABI)
12190 {
12191 /* If this is a reference to an external symbol, we want
12192 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12193 add $tempreg,$tempreg,$gp
12194 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12195 <op> op[0],<ofst>($tempreg)
12196 Otherwise, for local symbols, we want:
12197 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
12198 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
12199 gas_assert (offset_expr.X_op == O_symbol);
12200 expr1.X_add_number = offset_expr.X_add_number;
12201 offset_expr.X_add_number = 0;
12202 if (expr1.X_add_number < -0x8000
12203 || expr1.X_add_number >= 0x8000)
12204 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12205 relax_start (offset_expr.X_add_symbol);
12206 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12207 BFD_RELOC_MIPS_GOT_HI16);
12208 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12209 mips_gp_register);
12210 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12211 BFD_RELOC_MIPS_GOT_LO16, tempreg);
12212 if (breg != 0)
12213 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12214 tempreg, tempreg, breg);
12215 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12216
12217 relax_switch ();
12218 offset_expr.X_add_number = expr1.X_add_number;
12219 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12220 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12221 if (breg != 0)
12222 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12223 tempreg, tempreg, breg);
12224 macro_build (&offset_expr, s, fmt, op[0],
12225 BFD_RELOC_MIPS_GOT_OFST, tempreg);
12226 relax_end ();
12227 }
12228 else
12229 abort ();
12230
12231 break;
12232
12233 case M_JRADDIUSP:
12234 gas_assert (mips_opts.micromips);
12235 gas_assert (mips_opts.insn32);
12236 start_noreorder ();
12237 macro_build (NULL, "jr", "s", RA);
12238 expr1.X_add_number = op[0] << 2;
12239 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12240 end_noreorder ();
12241 break;
12242
12243 case M_JRC:
12244 gas_assert (mips_opts.micromips);
12245 gas_assert (mips_opts.insn32);
12246 macro_build (NULL, "jr", "s", op[0]);
12247 if (mips_opts.noreorder)
12248 macro_build (NULL, "nop", "");
12249 break;
12250
12251 case M_LI:
12252 case M_LI_S:
12253 load_register (op[0], &imm_expr, 0);
12254 break;
12255
12256 case M_DLI:
12257 load_register (op[0], &imm_expr, 1);
12258 break;
12259
12260 case M_LI_SS:
12261 if (imm_expr.X_op == O_constant)
12262 {
12263 used_at = 1;
12264 load_register (AT, &imm_expr, 0);
12265 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12266 break;
12267 }
12268 else
12269 {
12270 gas_assert (imm_expr.X_op == O_absent
12271 && offset_expr.X_op == O_symbol
12272 && strcmp (segment_name (S_GET_SEGMENT
12273 (offset_expr.X_add_symbol)),
12274 ".lit4") == 0
12275 && offset_expr.X_add_number == 0);
12276 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
12277 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
12278 break;
12279 }
12280
12281 case M_LI_D:
12282 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12283 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12284 order 32 bits of the value and the low order 32 bits are either
12285 zero or in OFFSET_EXPR. */
12286 if (imm_expr.X_op == O_constant)
12287 {
12288 if (GPR_SIZE == 64)
12289 load_register (op[0], &imm_expr, 1);
12290 else
12291 {
12292 int hreg, lreg;
12293
12294 if (target_big_endian)
12295 {
12296 hreg = op[0];
12297 lreg = op[0] + 1;
12298 }
12299 else
12300 {
12301 hreg = op[0] + 1;
12302 lreg = op[0];
12303 }
12304
12305 if (hreg <= 31)
12306 load_register (hreg, &imm_expr, 0);
12307 if (lreg <= 31)
12308 {
12309 if (offset_expr.X_op == O_absent)
12310 move_register (lreg, 0);
12311 else
12312 {
12313 gas_assert (offset_expr.X_op == O_constant);
12314 load_register (lreg, &offset_expr, 0);
12315 }
12316 }
12317 }
12318 break;
12319 }
12320 gas_assert (imm_expr.X_op == O_absent);
12321
12322 /* We know that sym is in the .rdata section. First we get the
12323 upper 16 bits of the address. */
12324 if (mips_pic == NO_PIC)
12325 {
12326 macro_build_lui (&offset_expr, AT);
12327 used_at = 1;
12328 }
12329 else
12330 {
12331 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12332 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12333 used_at = 1;
12334 }
12335
12336 /* Now we load the register(s). */
12337 if (GPR_SIZE == 64)
12338 {
12339 used_at = 1;
12340 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12341 BFD_RELOC_LO16, AT);
12342 }
12343 else
12344 {
12345 used_at = 1;
12346 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12347 BFD_RELOC_LO16, AT);
12348 if (op[0] != RA)
12349 {
12350 /* FIXME: How in the world do we deal with the possible
12351 overflow here? */
12352 offset_expr.X_add_number += 4;
12353 macro_build (&offset_expr, "lw", "t,o(b)",
12354 op[0] + 1, BFD_RELOC_LO16, AT);
12355 }
12356 }
12357 break;
12358
12359 case M_LI_DD:
12360 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12361 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12362 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12363 the value and the low order 32 bits are either zero or in
12364 OFFSET_EXPR. */
12365 if (imm_expr.X_op == O_constant)
12366 {
12367 used_at = 1;
12368 load_register (AT, &imm_expr, FPR_SIZE == 64);
12369 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12370 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
12371 else
12372 {
12373 if (ISA_HAS_MXHC1 (mips_opts.isa))
12374 macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12375 else if (FPR_SIZE != 32)
12376 as_bad (_("Unable to generate `%s' compliant code "
12377 "without mthc1"),
12378 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12379 else
12380 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
12381 if (offset_expr.X_op == O_absent)
12382 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
12383 else
12384 {
12385 gas_assert (offset_expr.X_op == O_constant);
12386 load_register (AT, &offset_expr, 0);
12387 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12388 }
12389 }
12390 break;
12391 }
12392
12393 gas_assert (imm_expr.X_op == O_absent
12394 && offset_expr.X_op == O_symbol
12395 && offset_expr.X_add_number == 0);
12396 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12397 if (strcmp (s, ".lit8") == 0)
12398 {
12399 op[2] = mips_gp_register;
12400 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12401 offset_reloc[1] = BFD_RELOC_UNUSED;
12402 offset_reloc[2] = BFD_RELOC_UNUSED;
12403 }
12404 else
12405 {
12406 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
12407 used_at = 1;
12408 if (mips_pic != NO_PIC)
12409 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12410 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12411 else
12412 {
12413 /* FIXME: This won't work for a 64 bit address. */
12414 macro_build_lui (&offset_expr, AT);
12415 }
12416
12417 op[2] = AT;
12418 offset_reloc[0] = BFD_RELOC_LO16;
12419 offset_reloc[1] = BFD_RELOC_UNUSED;
12420 offset_reloc[2] = BFD_RELOC_UNUSED;
12421 }
12422 align = 8;
12423 /* Fall through */
12424
12425 case M_L_DAB:
12426 /*
12427 * The MIPS assembler seems to check for X_add_number not
12428 * being double aligned and generating:
12429 * lui at,%hi(foo+1)
12430 * addu at,at,v1
12431 * addiu at,at,%lo(foo+1)
12432 * lwc1 f2,0(at)
12433 * lwc1 f3,4(at)
12434 * But, the resulting address is the same after relocation so why
12435 * generate the extra instruction?
12436 */
12437 /* Itbl support may require additional care here. */
12438 coproc = 1;
12439 fmt = "T,o(b)";
12440 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12441 {
12442 s = "ldc1";
12443 goto ld_st;
12444 }
12445 s = "lwc1";
12446 goto ldd_std;
12447
12448 case M_S_DAB:
12449 gas_assert (!mips_opts.micromips);
12450 /* Itbl support may require additional care here. */
12451 coproc = 1;
12452 fmt = "T,o(b)";
12453 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12454 {
12455 s = "sdc1";
12456 goto ld_st;
12457 }
12458 s = "swc1";
12459 goto ldd_std;
12460
12461 case M_LQ_AB:
12462 fmt = "t,o(b)";
12463 s = "lq";
12464 goto ld;
12465
12466 case M_SQ_AB:
12467 fmt = "t,o(b)";
12468 s = "sq";
12469 goto ld_st;
12470
12471 case M_LD_AB:
12472 fmt = "t,o(b)";
12473 if (GPR_SIZE == 64)
12474 {
12475 s = "ld";
12476 goto ld;
12477 }
12478 s = "lw";
12479 goto ldd_std;
12480
12481 case M_SD_AB:
12482 fmt = "t,o(b)";
12483 if (GPR_SIZE == 64)
12484 {
12485 s = "sd";
12486 goto ld_st;
12487 }
12488 s = "sw";
12489
12490 ldd_std:
12491 /* Even on a big endian machine $fn comes before $fn+1. We have
12492 to adjust when loading from memory. We set coproc if we must
12493 load $fn+1 first. */
12494 /* Itbl support may require additional care here. */
12495 if (!target_big_endian)
12496 coproc = 0;
12497
12498 breg = op[2];
12499 if (small_offset_p (0, align, 16))
12500 {
12501 ep = &offset_expr;
12502 if (!small_offset_p (4, align, 16))
12503 {
12504 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12505 -1, offset_reloc[0], offset_reloc[1],
12506 offset_reloc[2]);
12507 expr1.X_add_number = 0;
12508 ep = &expr1;
12509 breg = AT;
12510 used_at = 1;
12511 offset_reloc[0] = BFD_RELOC_LO16;
12512 offset_reloc[1] = BFD_RELOC_UNUSED;
12513 offset_reloc[2] = BFD_RELOC_UNUSED;
12514 }
12515 if (strcmp (s, "lw") == 0 && op[0] == breg)
12516 {
12517 ep->X_add_number += 4;
12518 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
12519 offset_reloc[1], offset_reloc[2], breg);
12520 ep->X_add_number -= 4;
12521 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
12522 offset_reloc[1], offset_reloc[2], breg);
12523 }
12524 else
12525 {
12526 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
12527 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12528 breg);
12529 ep->X_add_number += 4;
12530 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
12531 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12532 breg);
12533 }
12534 break;
12535 }
12536
12537 if (offset_expr.X_op != O_symbol
12538 && offset_expr.X_op != O_constant)
12539 {
12540 as_bad (_("expression too complex"));
12541 offset_expr.X_op = O_constant;
12542 }
12543
12544 if (HAVE_32BIT_ADDRESSES
12545 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12546 {
12547 char value [32];
12548
12549 sprintf_vma (value, offset_expr.X_add_number);
12550 as_bad (_("number (0x%s) larger than 32 bits"), value);
12551 }
12552
12553 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
12554 {
12555 /* If this is a reference to a GP relative symbol, we want
12556 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12557 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
12558 If we have a base register, we use this
12559 addu $at,$breg,$gp
12560 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12561 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
12562 If this is not a GP relative symbol, we want
12563 lui $at,<sym> (BFD_RELOC_HI16_S)
12564 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12565 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12566 If there is a base register, we add it to $at after the
12567 lui instruction. If there is a constant, we always use
12568 the last case. */
12569 if (offset_expr.X_op == O_symbol
12570 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12571 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12572 {
12573 relax_start (offset_expr.X_add_symbol);
12574 if (breg == 0)
12575 {
12576 tempreg = mips_gp_register;
12577 }
12578 else
12579 {
12580 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12581 AT, breg, mips_gp_register);
12582 tempreg = AT;
12583 used_at = 1;
12584 }
12585
12586 /* Itbl support may require additional care here. */
12587 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12588 BFD_RELOC_GPREL16, tempreg);
12589 offset_expr.X_add_number += 4;
12590
12591 /* Set mips_optimize to 2 to avoid inserting an
12592 undesired nop. */
12593 hold_mips_optimize = mips_optimize;
12594 mips_optimize = 2;
12595 /* Itbl support may require additional care here. */
12596 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12597 BFD_RELOC_GPREL16, tempreg);
12598 mips_optimize = hold_mips_optimize;
12599
12600 relax_switch ();
12601
12602 offset_expr.X_add_number -= 4;
12603 }
12604 used_at = 1;
12605 if (offset_high_part (offset_expr.X_add_number, 16)
12606 != offset_high_part (offset_expr.X_add_number + 4, 16))
12607 {
12608 load_address (AT, &offset_expr, &used_at);
12609 offset_expr.X_op = O_constant;
12610 offset_expr.X_add_number = 0;
12611 }
12612 else
12613 macro_build_lui (&offset_expr, AT);
12614 if (breg != 0)
12615 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12616 /* Itbl support may require additional care here. */
12617 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12618 BFD_RELOC_LO16, AT);
12619 /* FIXME: How do we handle overflow here? */
12620 offset_expr.X_add_number += 4;
12621 /* Itbl support may require additional care here. */
12622 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12623 BFD_RELOC_LO16, AT);
12624 if (mips_relax.sequence)
12625 relax_end ();
12626 }
12627 else if (!mips_big_got)
12628 {
12629 /* If this is a reference to an external symbol, we want
12630 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12631 nop
12632 <op> op[0],0($at)
12633 <op> op[0]+1,4($at)
12634 Otherwise we want
12635 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12636 nop
12637 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12638 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12639 If there is a base register we add it to $at before the
12640 lwc1 instructions. If there is a constant we include it
12641 in the lwc1 instructions. */
12642 used_at = 1;
12643 expr1.X_add_number = offset_expr.X_add_number;
12644 if (expr1.X_add_number < -0x8000
12645 || expr1.X_add_number >= 0x8000 - 4)
12646 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12647 load_got_offset (AT, &offset_expr);
12648 load_delay_nop ();
12649 if (breg != 0)
12650 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12651
12652 /* Set mips_optimize to 2 to avoid inserting an undesired
12653 nop. */
12654 hold_mips_optimize = mips_optimize;
12655 mips_optimize = 2;
12656
12657 /* Itbl support may require additional care here. */
12658 relax_start (offset_expr.X_add_symbol);
12659 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12660 BFD_RELOC_LO16, AT);
12661 expr1.X_add_number += 4;
12662 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12663 BFD_RELOC_LO16, AT);
12664 relax_switch ();
12665 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12666 BFD_RELOC_LO16, AT);
12667 offset_expr.X_add_number += 4;
12668 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12669 BFD_RELOC_LO16, AT);
12670 relax_end ();
12671
12672 mips_optimize = hold_mips_optimize;
12673 }
12674 else if (mips_big_got)
12675 {
12676 int gpdelay;
12677
12678 /* If this is a reference to an external symbol, we want
12679 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12680 addu $at,$at,$gp
12681 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
12682 nop
12683 <op> op[0],0($at)
12684 <op> op[0]+1,4($at)
12685 Otherwise we want
12686 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12687 nop
12688 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12689 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
12690 If there is a base register we add it to $at before the
12691 lwc1 instructions. If there is a constant we include it
12692 in the lwc1 instructions. */
12693 used_at = 1;
12694 expr1.X_add_number = offset_expr.X_add_number;
12695 offset_expr.X_add_number = 0;
12696 if (expr1.X_add_number < -0x8000
12697 || expr1.X_add_number >= 0x8000 - 4)
12698 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12699 gpdelay = reg_needs_delay (mips_gp_register);
12700 relax_start (offset_expr.X_add_symbol);
12701 macro_build (&offset_expr, "lui", LUI_FMT,
12702 AT, BFD_RELOC_MIPS_GOT_HI16);
12703 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12704 AT, AT, mips_gp_register);
12705 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
12706 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
12707 load_delay_nop ();
12708 if (breg != 0)
12709 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12710 /* Itbl support may require additional care here. */
12711 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12712 BFD_RELOC_LO16, AT);
12713 expr1.X_add_number += 4;
12714
12715 /* Set mips_optimize to 2 to avoid inserting an undesired
12716 nop. */
12717 hold_mips_optimize = mips_optimize;
12718 mips_optimize = 2;
12719 /* Itbl support may require additional care here. */
12720 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12721 BFD_RELOC_LO16, AT);
12722 mips_optimize = hold_mips_optimize;
12723 expr1.X_add_number -= 4;
12724
12725 relax_switch ();
12726 offset_expr.X_add_number = expr1.X_add_number;
12727 if (gpdelay)
12728 macro_build (NULL, "nop", "");
12729 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12730 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12731 load_delay_nop ();
12732 if (breg != 0)
12733 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12734 /* Itbl support may require additional care here. */
12735 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12736 BFD_RELOC_LO16, AT);
12737 offset_expr.X_add_number += 4;
12738
12739 /* Set mips_optimize to 2 to avoid inserting an undesired
12740 nop. */
12741 hold_mips_optimize = mips_optimize;
12742 mips_optimize = 2;
12743 /* Itbl support may require additional care here. */
12744 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12745 BFD_RELOC_LO16, AT);
12746 mips_optimize = hold_mips_optimize;
12747 relax_end ();
12748 }
12749 else
12750 abort ();
12751
12752 break;
12753
12754 case M_SAA_AB:
12755 s = "saa";
12756 goto saa_saad;
12757 case M_SAAD_AB:
12758 s = "saad";
12759 saa_saad:
12760 gas_assert (!mips_opts.micromips);
12761 offbits = 0;
12762 fmt = "t,(b)";
12763 goto ld_st;
12764
12765 /* New code added to support COPZ instructions.
12766 This code builds table entries out of the macros in mip_opcodes.
12767 R4000 uses interlocks to handle coproc delays.
12768 Other chips (like the R3000) require nops to be inserted for delays.
12769
12770 FIXME: Currently, we require that the user handle delays.
12771 In order to fill delay slots for non-interlocked chips,
12772 we must have a way to specify delays based on the coprocessor.
12773 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
12774 What are the side-effects of the cop instruction?
12775 What cache support might we have and what are its effects?
12776 Both coprocessor & memory require delays. how long???
12777 What registers are read/set/modified?
12778
12779 If an itbl is provided to interpret cop instructions,
12780 this knowledge can be encoded in the itbl spec. */
12781
12782 case M_COP0:
12783 s = "c0";
12784 goto copz;
12785 case M_COP1:
12786 s = "c1";
12787 goto copz;
12788 case M_COP2:
12789 s = "c2";
12790 goto copz;
12791 case M_COP3:
12792 s = "c3";
12793 copz:
12794 gas_assert (!mips_opts.micromips);
12795 /* For now we just do C (same as Cz). The parameter will be
12796 stored in insn_opcode by mips_ip. */
12797 macro_build (NULL, s, "C", (int) ip->insn_opcode);
12798 break;
12799
12800 case M_MOVE:
12801 move_register (op[0], op[1]);
12802 break;
12803
12804 case M_MOVEP:
12805 gas_assert (mips_opts.micromips);
12806 gas_assert (mips_opts.insn32);
12807 move_register (micromips_to_32_reg_h_map1[op[0]],
12808 micromips_to_32_reg_m_map[op[1]]);
12809 move_register (micromips_to_32_reg_h_map2[op[0]],
12810 micromips_to_32_reg_n_map[op[2]]);
12811 break;
12812
12813 case M_DMUL:
12814 dbl = 1;
12815 /* Fall through. */
12816 case M_MUL:
12817 if (mips_opts.arch == CPU_R5900)
12818 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
12819 op[2]);
12820 else
12821 {
12822 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
12823 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12824 }
12825 break;
12826
12827 case M_DMUL_I:
12828 dbl = 1;
12829 /* Fall through. */
12830 case M_MUL_I:
12831 /* The MIPS assembler some times generates shifts and adds. I'm
12832 not trying to be that fancy. GCC should do this for us
12833 anyway. */
12834 used_at = 1;
12835 load_register (AT, &imm_expr, dbl);
12836 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
12837 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12838 break;
12839
12840 case M_DMULO_I:
12841 dbl = 1;
12842 /* Fall through. */
12843 case M_MULO_I:
12844 imm = 1;
12845 goto do_mulo;
12846
12847 case M_DMULO:
12848 dbl = 1;
12849 /* Fall through. */
12850 case M_MULO:
12851 do_mulo:
12852 start_noreorder ();
12853 used_at = 1;
12854 if (imm)
12855 load_register (AT, &imm_expr, dbl);
12856 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
12857 op[1], imm ? AT : op[2]);
12858 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12859 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
12860 macro_build (NULL, "mfhi", MFHL_FMT, AT);
12861 if (mips_trap)
12862 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
12863 else
12864 {
12865 if (mips_opts.micromips)
12866 micromips_label_expr (&label_expr);
12867 else
12868 label_expr.X_add_number = 8;
12869 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
12870 macro_build (NULL, "nop", "");
12871 macro_build (NULL, "break", BRK_FMT, 6);
12872 if (mips_opts.micromips)
12873 micromips_add_label ();
12874 }
12875 end_noreorder ();
12876 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12877 break;
12878
12879 case M_DMULOU_I:
12880 dbl = 1;
12881 /* Fall through. */
12882 case M_MULOU_I:
12883 imm = 1;
12884 goto do_mulou;
12885
12886 case M_DMULOU:
12887 dbl = 1;
12888 /* Fall through. */
12889 case M_MULOU:
12890 do_mulou:
12891 start_noreorder ();
12892 used_at = 1;
12893 if (imm)
12894 load_register (AT, &imm_expr, dbl);
12895 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
12896 op[1], imm ? AT : op[2]);
12897 macro_build (NULL, "mfhi", MFHL_FMT, AT);
12898 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12899 if (mips_trap)
12900 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
12901 else
12902 {
12903 if (mips_opts.micromips)
12904 micromips_label_expr (&label_expr);
12905 else
12906 label_expr.X_add_number = 8;
12907 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
12908 macro_build (NULL, "nop", "");
12909 macro_build (NULL, "break", BRK_FMT, 6);
12910 if (mips_opts.micromips)
12911 micromips_add_label ();
12912 }
12913 end_noreorder ();
12914 break;
12915
12916 case M_DROL:
12917 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
12918 {
12919 if (op[0] == op[1])
12920 {
12921 tempreg = AT;
12922 used_at = 1;
12923 }
12924 else
12925 tempreg = op[0];
12926 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
12927 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
12928 break;
12929 }
12930 used_at = 1;
12931 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12932 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
12933 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
12934 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12935 break;
12936
12937 case M_ROL:
12938 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
12939 {
12940 if (op[0] == op[1])
12941 {
12942 tempreg = AT;
12943 used_at = 1;
12944 }
12945 else
12946 tempreg = op[0];
12947 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
12948 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
12949 break;
12950 }
12951 used_at = 1;
12952 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12953 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
12954 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
12955 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12956 break;
12957
12958 case M_DROL_I:
12959 {
12960 unsigned int rot;
12961 const char *l;
12962 const char *rr;
12963
12964 rot = imm_expr.X_add_number & 0x3f;
12965 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
12966 {
12967 rot = (64 - rot) & 0x3f;
12968 if (rot >= 32)
12969 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
12970 else
12971 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
12972 break;
12973 }
12974 if (rot == 0)
12975 {
12976 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
12977 break;
12978 }
12979 l = (rot < 0x20) ? "dsll" : "dsll32";
12980 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
12981 rot &= 0x1f;
12982 used_at = 1;
12983 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
12984 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12985 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
12986 }
12987 break;
12988
12989 case M_ROL_I:
12990 {
12991 unsigned int rot;
12992
12993 rot = imm_expr.X_add_number & 0x1f;
12994 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
12995 {
12996 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
12997 (32 - rot) & 0x1f);
12998 break;
12999 }
13000 if (rot == 0)
13001 {
13002 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13003 break;
13004 }
13005 used_at = 1;
13006 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13007 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13008 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13009 }
13010 break;
13011
13012 case M_DROR:
13013 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13014 {
13015 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
13016 break;
13017 }
13018 used_at = 1;
13019 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13020 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13021 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13022 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13023 break;
13024
13025 case M_ROR:
13026 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13027 {
13028 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
13029 break;
13030 }
13031 used_at = 1;
13032 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13033 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13034 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13035 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13036 break;
13037
13038 case M_DROR_I:
13039 {
13040 unsigned int rot;
13041 const char *l;
13042 const char *rr;
13043
13044 rot = imm_expr.X_add_number & 0x3f;
13045 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13046 {
13047 if (rot >= 32)
13048 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13049 else
13050 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13051 break;
13052 }
13053 if (rot == 0)
13054 {
13055 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13056 break;
13057 }
13058 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
13059 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13060 rot &= 0x1f;
13061 used_at = 1;
13062 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13063 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13064 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13065 }
13066 break;
13067
13068 case M_ROR_I:
13069 {
13070 unsigned int rot;
13071
13072 rot = imm_expr.X_add_number & 0x1f;
13073 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13074 {
13075 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
13076 break;
13077 }
13078 if (rot == 0)
13079 {
13080 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13081 break;
13082 }
13083 used_at = 1;
13084 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13085 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13086 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13087 }
13088 break;
13089
13090 case M_SEQ:
13091 if (op[1] == 0)
13092 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13093 else if (op[2] == 0)
13094 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13095 else
13096 {
13097 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13098 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13099 }
13100 break;
13101
13102 case M_SEQ_I:
13103 if (imm_expr.X_add_number == 0)
13104 {
13105 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13106 break;
13107 }
13108 if (op[1] == 0)
13109 {
13110 as_warn (_("instruction %s: result is always false"),
13111 ip->insn_mo->name);
13112 move_register (op[0], 0);
13113 break;
13114 }
13115 if (CPU_HAS_SEQ (mips_opts.arch)
13116 && -512 <= imm_expr.X_add_number
13117 && imm_expr.X_add_number < 512)
13118 {
13119 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
13120 (int) imm_expr.X_add_number);
13121 break;
13122 }
13123 if (imm_expr.X_add_number >= 0
13124 && imm_expr.X_add_number < 0x10000)
13125 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
13126 else if (imm_expr.X_add_number > -0x8000
13127 && imm_expr.X_add_number < 0)
13128 {
13129 imm_expr.X_add_number = -imm_expr.X_add_number;
13130 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13131 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13132 }
13133 else if (CPU_HAS_SEQ (mips_opts.arch))
13134 {
13135 used_at = 1;
13136 load_register (AT, &imm_expr, GPR_SIZE == 64);
13137 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
13138 break;
13139 }
13140 else
13141 {
13142 load_register (AT, &imm_expr, GPR_SIZE == 64);
13143 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13144 used_at = 1;
13145 }
13146 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13147 break;
13148
13149 case M_SGE: /* X >= Y <==> not (X < Y) */
13150 s = "slt";
13151 goto sge;
13152 case M_SGEU:
13153 s = "sltu";
13154 sge:
13155 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13156 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13157 break;
13158
13159 case M_SGE_I: /* X >= I <==> not (X < I) */
13160 case M_SGEU_I:
13161 if (imm_expr.X_add_number >= -0x8000
13162 && imm_expr.X_add_number < 0x8000)
13163 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13164 op[0], op[1], BFD_RELOC_LO16);
13165 else
13166 {
13167 load_register (AT, &imm_expr, GPR_SIZE == 64);
13168 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
13169 op[0], op[1], AT);
13170 used_at = 1;
13171 }
13172 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13173 break;
13174
13175 case M_SGT: /* X > Y <==> Y < X */
13176 s = "slt";
13177 goto sgt;
13178 case M_SGTU:
13179 s = "sltu";
13180 sgt:
13181 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13182 break;
13183
13184 case M_SGT_I: /* X > I <==> I < X */
13185 s = "slt";
13186 goto sgti;
13187 case M_SGTU_I:
13188 s = "sltu";
13189 sgti:
13190 used_at = 1;
13191 load_register (AT, &imm_expr, GPR_SIZE == 64);
13192 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13193 break;
13194
13195 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
13196 s = "slt";
13197 goto sle;
13198 case M_SLEU:
13199 s = "sltu";
13200 sle:
13201 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13202 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13203 break;
13204
13205 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
13206 s = "slt";
13207 goto slei;
13208 case M_SLEU_I:
13209 s = "sltu";
13210 slei:
13211 used_at = 1;
13212 load_register (AT, &imm_expr, GPR_SIZE == 64);
13213 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13214 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13215 break;
13216
13217 case M_SLT_I:
13218 if (imm_expr.X_add_number >= -0x8000
13219 && imm_expr.X_add_number < 0x8000)
13220 {
13221 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13222 BFD_RELOC_LO16);
13223 break;
13224 }
13225 used_at = 1;
13226 load_register (AT, &imm_expr, GPR_SIZE == 64);
13227 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
13228 break;
13229
13230 case M_SLTU_I:
13231 if (imm_expr.X_add_number >= -0x8000
13232 && imm_expr.X_add_number < 0x8000)
13233 {
13234 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
13235 BFD_RELOC_LO16);
13236 break;
13237 }
13238 used_at = 1;
13239 load_register (AT, &imm_expr, GPR_SIZE == 64);
13240 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
13241 break;
13242
13243 case M_SNE:
13244 if (op[1] == 0)
13245 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13246 else if (op[2] == 0)
13247 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13248 else
13249 {
13250 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13251 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13252 }
13253 break;
13254
13255 case M_SNE_I:
13256 if (imm_expr.X_add_number == 0)
13257 {
13258 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13259 break;
13260 }
13261 if (op[1] == 0)
13262 {
13263 as_warn (_("instruction %s: result is always true"),
13264 ip->insn_mo->name);
13265 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
13266 op[0], 0, BFD_RELOC_LO16);
13267 break;
13268 }
13269 if (CPU_HAS_SEQ (mips_opts.arch)
13270 && -512 <= imm_expr.X_add_number
13271 && imm_expr.X_add_number < 512)
13272 {
13273 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
13274 (int) imm_expr.X_add_number);
13275 break;
13276 }
13277 if (imm_expr.X_add_number >= 0
13278 && imm_expr.X_add_number < 0x10000)
13279 {
13280 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13281 BFD_RELOC_LO16);
13282 }
13283 else if (imm_expr.X_add_number > -0x8000
13284 && imm_expr.X_add_number < 0)
13285 {
13286 imm_expr.X_add_number = -imm_expr.X_add_number;
13287 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13288 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13289 }
13290 else if (CPU_HAS_SEQ (mips_opts.arch))
13291 {
13292 used_at = 1;
13293 load_register (AT, &imm_expr, GPR_SIZE == 64);
13294 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
13295 break;
13296 }
13297 else
13298 {
13299 load_register (AT, &imm_expr, GPR_SIZE == 64);
13300 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13301 used_at = 1;
13302 }
13303 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13304 break;
13305
13306 case M_SUB_I:
13307 s = "addi";
13308 s2 = "sub";
13309 goto do_subi;
13310 case M_SUBU_I:
13311 s = "addiu";
13312 s2 = "subu";
13313 goto do_subi;
13314 case M_DSUB_I:
13315 dbl = 1;
13316 s = "daddi";
13317 s2 = "dsub";
13318 if (!mips_opts.micromips)
13319 goto do_subi;
13320 if (imm_expr.X_add_number > -0x200
13321 && imm_expr.X_add_number <= 0x200)
13322 {
13323 macro_build (NULL, s, "t,r,.", op[0], op[1],
13324 (int) -imm_expr.X_add_number);
13325 break;
13326 }
13327 goto do_subi_i;
13328 case M_DSUBU_I:
13329 dbl = 1;
13330 s = "daddiu";
13331 s2 = "dsubu";
13332 do_subi:
13333 if (imm_expr.X_add_number > -0x8000
13334 && imm_expr.X_add_number <= 0x8000)
13335 {
13336 imm_expr.X_add_number = -imm_expr.X_add_number;
13337 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13338 break;
13339 }
13340 do_subi_i:
13341 used_at = 1;
13342 load_register (AT, &imm_expr, dbl);
13343 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
13344 break;
13345
13346 case M_TEQ_I:
13347 s = "teq";
13348 goto trap;
13349 case M_TGE_I:
13350 s = "tge";
13351 goto trap;
13352 case M_TGEU_I:
13353 s = "tgeu";
13354 goto trap;
13355 case M_TLT_I:
13356 s = "tlt";
13357 goto trap;
13358 case M_TLTU_I:
13359 s = "tltu";
13360 goto trap;
13361 case M_TNE_I:
13362 s = "tne";
13363 trap:
13364 used_at = 1;
13365 load_register (AT, &imm_expr, GPR_SIZE == 64);
13366 macro_build (NULL, s, "s,t", op[0], AT);
13367 break;
13368
13369 case M_TRUNCWS:
13370 case M_TRUNCWD:
13371 gas_assert (!mips_opts.micromips);
13372 gas_assert (mips_opts.isa == ISA_MIPS1);
13373 used_at = 1;
13374
13375 /*
13376 * Is the double cfc1 instruction a bug in the mips assembler;
13377 * or is there a reason for it?
13378 */
13379 start_noreorder ();
13380 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13381 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13382 macro_build (NULL, "nop", "");
13383 expr1.X_add_number = 3;
13384 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
13385 expr1.X_add_number = 2;
13386 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13387 macro_build (NULL, "ctc1", "t,G", AT, RA);
13388 macro_build (NULL, "nop", "");
13389 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
13390 op[0], op[1]);
13391 macro_build (NULL, "ctc1", "t,G", op[2], RA);
13392 macro_build (NULL, "nop", "");
13393 end_noreorder ();
13394 break;
13395
13396 case M_ULH_AB:
13397 s = "lb";
13398 s2 = "lbu";
13399 off = 1;
13400 goto uld_st;
13401 case M_ULHU_AB:
13402 s = "lbu";
13403 s2 = "lbu";
13404 off = 1;
13405 goto uld_st;
13406 case M_ULW_AB:
13407 s = "lwl";
13408 s2 = "lwr";
13409 offbits = (mips_opts.micromips ? 12 : 16);
13410 off = 3;
13411 goto uld_st;
13412 case M_ULD_AB:
13413 s = "ldl";
13414 s2 = "ldr";
13415 offbits = (mips_opts.micromips ? 12 : 16);
13416 off = 7;
13417 goto uld_st;
13418 case M_USH_AB:
13419 s = "sb";
13420 s2 = "sb";
13421 off = 1;
13422 ust = 1;
13423 goto uld_st;
13424 case M_USW_AB:
13425 s = "swl";
13426 s2 = "swr";
13427 offbits = (mips_opts.micromips ? 12 : 16);
13428 off = 3;
13429 ust = 1;
13430 goto uld_st;
13431 case M_USD_AB:
13432 s = "sdl";
13433 s2 = "sdr";
13434 offbits = (mips_opts.micromips ? 12 : 16);
13435 off = 7;
13436 ust = 1;
13437
13438 uld_st:
13439 breg = op[2];
13440 large_offset = !small_offset_p (off, align, offbits);
13441 ep = &offset_expr;
13442 expr1.X_add_number = 0;
13443 if (large_offset)
13444 {
13445 used_at = 1;
13446 tempreg = AT;
13447 if (small_offset_p (0, align, 16))
13448 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13449 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13450 else
13451 {
13452 load_address (tempreg, ep, &used_at);
13453 if (breg != 0)
13454 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13455 tempreg, tempreg, breg);
13456 }
13457 offset_reloc[0] = BFD_RELOC_LO16;
13458 offset_reloc[1] = BFD_RELOC_UNUSED;
13459 offset_reloc[2] = BFD_RELOC_UNUSED;
13460 breg = tempreg;
13461 tempreg = op[0];
13462 ep = &expr1;
13463 }
13464 else if (!ust && op[0] == breg)
13465 {
13466 used_at = 1;
13467 tempreg = AT;
13468 }
13469 else
13470 tempreg = op[0];
13471
13472 if (off == 1)
13473 goto ulh_sh;
13474
13475 if (!target_big_endian)
13476 ep->X_add_number += off;
13477 if (offbits == 12)
13478 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
13479 else
13480 macro_build (ep, s, "t,o(b)", tempreg, -1,
13481 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13482
13483 if (!target_big_endian)
13484 ep->X_add_number -= off;
13485 else
13486 ep->X_add_number += off;
13487 if (offbits == 12)
13488 macro_build (NULL, s2, "t,~(b)",
13489 tempreg, (int) ep->X_add_number, breg);
13490 else
13491 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13492 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13493
13494 /* If necessary, move the result in tempreg to the final destination. */
13495 if (!ust && op[0] != tempreg)
13496 {
13497 /* Protect second load's delay slot. */
13498 load_delay_nop ();
13499 move_register (op[0], tempreg);
13500 }
13501 break;
13502
13503 ulh_sh:
13504 used_at = 1;
13505 if (target_big_endian == ust)
13506 ep->X_add_number += off;
13507 tempreg = ust || large_offset ? op[0] : AT;
13508 macro_build (ep, s, "t,o(b)", tempreg, -1,
13509 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13510
13511 /* For halfword transfers we need a temporary register to shuffle
13512 bytes. Unfortunately for M_USH_A we have none available before
13513 the next store as AT holds the base address. We deal with this
13514 case by clobbering TREG and then restoring it as with ULH. */
13515 tempreg = ust == large_offset ? op[0] : AT;
13516 if (ust)
13517 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
13518
13519 if (target_big_endian == ust)
13520 ep->X_add_number -= off;
13521 else
13522 ep->X_add_number += off;
13523 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13524 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13525
13526 /* For M_USH_A re-retrieve the LSB. */
13527 if (ust && large_offset)
13528 {
13529 if (target_big_endian)
13530 ep->X_add_number += off;
13531 else
13532 ep->X_add_number -= off;
13533 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13534 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
13535 }
13536 /* For ULH and M_USH_A OR the LSB in. */
13537 if (!ust || large_offset)
13538 {
13539 tempreg = !large_offset ? AT : op[0];
13540 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
13541 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13542 }
13543 break;
13544
13545 default:
13546 /* FIXME: Check if this is one of the itbl macros, since they
13547 are added dynamically. */
13548 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
13549 break;
13550 }
13551 if (!mips_opts.at && used_at)
13552 as_bad (_("macro used $at after \".set noat\""));
13553 }
13554
13555 /* Implement macros in mips16 mode. */
13556
13557 static void
13558 mips16_macro (struct mips_cl_insn *ip)
13559 {
13560 const struct mips_operand_array *operands;
13561 int mask;
13562 int tmp;
13563 expressionS expr1;
13564 int dbl;
13565 const char *s, *s2, *s3;
13566 unsigned int op[MAX_OPERANDS];
13567 unsigned int i;
13568
13569 mask = ip->insn_mo->mask;
13570
13571 operands = insn_operands (ip);
13572 for (i = 0; i < MAX_OPERANDS; i++)
13573 if (operands->operand[i])
13574 op[i] = insn_extract_operand (ip, operands->operand[i]);
13575 else
13576 op[i] = -1;
13577
13578 expr1.X_op = O_constant;
13579 expr1.X_op_symbol = NULL;
13580 expr1.X_add_symbol = NULL;
13581 expr1.X_add_number = 1;
13582
13583 dbl = 0;
13584
13585 switch (mask)
13586 {
13587 default:
13588 abort ();
13589
13590 case M_DDIV_3:
13591 dbl = 1;
13592 /* Fall through. */
13593 case M_DIV_3:
13594 s = "mflo";
13595 goto do_div3;
13596 case M_DREM_3:
13597 dbl = 1;
13598 /* Fall through. */
13599 case M_REM_3:
13600 s = "mfhi";
13601 do_div3:
13602 start_noreorder ();
13603 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
13604 expr1.X_add_number = 2;
13605 macro_build (&expr1, "bnez", "x,p", op[2]);
13606 macro_build (NULL, "break", "6", 7);
13607
13608 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13609 since that causes an overflow. We should do that as well,
13610 but I don't see how to do the comparisons without a temporary
13611 register. */
13612 end_noreorder ();
13613 macro_build (NULL, s, "x", op[0]);
13614 break;
13615
13616 case M_DIVU_3:
13617 s = "divu";
13618 s2 = "mflo";
13619 goto do_divu3;
13620 case M_REMU_3:
13621 s = "divu";
13622 s2 = "mfhi";
13623 goto do_divu3;
13624 case M_DDIVU_3:
13625 s = "ddivu";
13626 s2 = "mflo";
13627 goto do_divu3;
13628 case M_DREMU_3:
13629 s = "ddivu";
13630 s2 = "mfhi";
13631 do_divu3:
13632 start_noreorder ();
13633 macro_build (NULL, s, ".,x,y", op[1], op[2]);
13634 expr1.X_add_number = 2;
13635 macro_build (&expr1, "bnez", "x,p", op[2]);
13636 macro_build (NULL, "break", "6", 7);
13637 end_noreorder ();
13638 macro_build (NULL, s2, "x", op[0]);
13639 break;
13640
13641 case M_DMUL:
13642 dbl = 1;
13643 /* Fall through. */
13644 case M_MUL:
13645 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13646 macro_build (NULL, "mflo", "x", op[0]);
13647 break;
13648
13649 case M_DSUBU_I:
13650 dbl = 1;
13651 goto do_subu;
13652 case M_SUBU_I:
13653 do_subu:
13654 imm_expr.X_add_number = -imm_expr.X_add_number;
13655 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
13656 break;
13657
13658 case M_SUBU_I_2:
13659 imm_expr.X_add_number = -imm_expr.X_add_number;
13660 macro_build (&imm_expr, "addiu", "x,k", op[0]);
13661 break;
13662
13663 case M_DSUBU_I_2:
13664 imm_expr.X_add_number = -imm_expr.X_add_number;
13665 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
13666 break;
13667
13668 case M_BEQ:
13669 s = "cmp";
13670 s2 = "bteqz";
13671 goto do_branch;
13672 case M_BNE:
13673 s = "cmp";
13674 s2 = "btnez";
13675 goto do_branch;
13676 case M_BLT:
13677 s = "slt";
13678 s2 = "btnez";
13679 goto do_branch;
13680 case M_BLTU:
13681 s = "sltu";
13682 s2 = "btnez";
13683 goto do_branch;
13684 case M_BLE:
13685 s = "slt";
13686 s2 = "bteqz";
13687 goto do_reverse_branch;
13688 case M_BLEU:
13689 s = "sltu";
13690 s2 = "bteqz";
13691 goto do_reverse_branch;
13692 case M_BGE:
13693 s = "slt";
13694 s2 = "bteqz";
13695 goto do_branch;
13696 case M_BGEU:
13697 s = "sltu";
13698 s2 = "bteqz";
13699 goto do_branch;
13700 case M_BGT:
13701 s = "slt";
13702 s2 = "btnez";
13703 goto do_reverse_branch;
13704 case M_BGTU:
13705 s = "sltu";
13706 s2 = "btnez";
13707
13708 do_reverse_branch:
13709 tmp = op[1];
13710 op[1] = op[0];
13711 op[0] = tmp;
13712
13713 do_branch:
13714 macro_build (NULL, s, "x,y", op[0], op[1]);
13715 macro_build (&offset_expr, s2, "p");
13716 break;
13717
13718 case M_BEQ_I:
13719 s = "cmpi";
13720 s2 = "bteqz";
13721 s3 = "x,U";
13722 goto do_branch_i;
13723 case M_BNE_I:
13724 s = "cmpi";
13725 s2 = "btnez";
13726 s3 = "x,U";
13727 goto do_branch_i;
13728 case M_BLT_I:
13729 s = "slti";
13730 s2 = "btnez";
13731 s3 = "x,8";
13732 goto do_branch_i;
13733 case M_BLTU_I:
13734 s = "sltiu";
13735 s2 = "btnez";
13736 s3 = "x,8";
13737 goto do_branch_i;
13738 case M_BLE_I:
13739 s = "slti";
13740 s2 = "btnez";
13741 s3 = "x,8";
13742 goto do_addone_branch_i;
13743 case M_BLEU_I:
13744 s = "sltiu";
13745 s2 = "btnez";
13746 s3 = "x,8";
13747 goto do_addone_branch_i;
13748 case M_BGE_I:
13749 s = "slti";
13750 s2 = "bteqz";
13751 s3 = "x,8";
13752 goto do_branch_i;
13753 case M_BGEU_I:
13754 s = "sltiu";
13755 s2 = "bteqz";
13756 s3 = "x,8";
13757 goto do_branch_i;
13758 case M_BGT_I:
13759 s = "slti";
13760 s2 = "bteqz";
13761 s3 = "x,8";
13762 goto do_addone_branch_i;
13763 case M_BGTU_I:
13764 s = "sltiu";
13765 s2 = "bteqz";
13766 s3 = "x,8";
13767
13768 do_addone_branch_i:
13769 ++imm_expr.X_add_number;
13770
13771 do_branch_i:
13772 macro_build (&imm_expr, s, s3, op[0]);
13773 macro_build (&offset_expr, s2, "p");
13774 break;
13775
13776 case M_ABS:
13777 expr1.X_add_number = 0;
13778 macro_build (&expr1, "slti", "x,8", op[1]);
13779 if (op[0] != op[1])
13780 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
13781 expr1.X_add_number = 2;
13782 macro_build (&expr1, "bteqz", "p");
13783 macro_build (NULL, "neg", "x,w", op[0], op[0]);
13784 break;
13785 }
13786 }
13787
13788 /* Look up instruction [START, START + LENGTH) in HASH. Record any extra
13789 opcode bits in *OPCODE_EXTRA. */
13790
13791 static struct mips_opcode *
13792 mips_lookup_insn (struct hash_control *hash, const char *start,
13793 ssize_t length, unsigned int *opcode_extra)
13794 {
13795 char *name, *dot, *p;
13796 unsigned int mask, suffix;
13797 ssize_t opend;
13798 struct mips_opcode *insn;
13799
13800 /* Make a copy of the instruction so that we can fiddle with it. */
13801 name = xstrndup (start, length);
13802
13803 /* Look up the instruction as-is. */
13804 insn = (struct mips_opcode *) hash_find (hash, name);
13805 if (insn)
13806 goto end;
13807
13808 dot = strchr (name, '.');
13809 if (dot && dot[1])
13810 {
13811 /* Try to interpret the text after the dot as a VU0 channel suffix. */
13812 p = mips_parse_vu0_channels (dot + 1, &mask);
13813 if (*p == 0 && mask != 0)
13814 {
13815 *dot = 0;
13816 insn = (struct mips_opcode *) hash_find (hash, name);
13817 *dot = '.';
13818 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
13819 {
13820 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
13821 goto end;
13822 }
13823 }
13824 }
13825
13826 if (mips_opts.micromips)
13827 {
13828 /* See if there's an instruction size override suffix,
13829 either `16' or `32', at the end of the mnemonic proper,
13830 that defines the operation, i.e. before the first `.'
13831 character if any. Strip it and retry. */
13832 opend = dot != NULL ? dot - name : length;
13833 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
13834 suffix = 2;
13835 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
13836 suffix = 4;
13837 else
13838 suffix = 0;
13839 if (suffix)
13840 {
13841 memcpy (name + opend - 2, name + opend, length - opend + 1);
13842 insn = (struct mips_opcode *) hash_find (hash, name);
13843 if (insn)
13844 {
13845 forced_insn_length = suffix;
13846 goto end;
13847 }
13848 }
13849 }
13850
13851 insn = NULL;
13852 end:
13853 free (name);
13854 return insn;
13855 }
13856
13857 /* Assemble an instruction into its binary format. If the instruction
13858 is a macro, set imm_expr and offset_expr to the values associated
13859 with "I" and "A" operands respectively. Otherwise store the value
13860 of the relocatable field (if any) in offset_expr. In both cases
13861 set offset_reloc to the relocation operators applied to offset_expr. */
13862
13863 static void
13864 mips_ip (char *str, struct mips_cl_insn *insn)
13865 {
13866 const struct mips_opcode *first, *past;
13867 struct hash_control *hash;
13868 char format;
13869 size_t end;
13870 struct mips_operand_token *tokens;
13871 unsigned int opcode_extra;
13872
13873 if (mips_opts.micromips)
13874 {
13875 hash = micromips_op_hash;
13876 past = &micromips_opcodes[bfd_micromips_num_opcodes];
13877 }
13878 else
13879 {
13880 hash = op_hash;
13881 past = &mips_opcodes[NUMOPCODES];
13882 }
13883 forced_insn_length = 0;
13884 opcode_extra = 0;
13885
13886 /* We first try to match an instruction up to a space or to the end. */
13887 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
13888 continue;
13889
13890 first = mips_lookup_insn (hash, str, end, &opcode_extra);
13891 if (first == NULL)
13892 {
13893 set_insn_error (0, _("unrecognized opcode"));
13894 return;
13895 }
13896
13897 if (strcmp (first->name, "li.s") == 0)
13898 format = 'f';
13899 else if (strcmp (first->name, "li.d") == 0)
13900 format = 'd';
13901 else
13902 format = 0;
13903 tokens = mips_parse_arguments (str + end, format);
13904 if (!tokens)
13905 return;
13906
13907 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
13908 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
13909 set_insn_error (0, _("invalid operands"));
13910
13911 obstack_free (&mips_operand_tokens, tokens);
13912 }
13913
13914 /* As for mips_ip, but used when assembling MIPS16 code.
13915 Also set forced_insn_length to the resulting instruction size in
13916 bytes if the user explicitly requested a small or extended instruction. */
13917
13918 static void
13919 mips16_ip (char *str, struct mips_cl_insn *insn)
13920 {
13921 char *end, *s, c;
13922 struct mips_opcode *first;
13923 struct mips_operand_token *tokens;
13924 unsigned int l;
13925
13926 for (s = str; ISLOWER (*s); ++s)
13927 ;
13928 end = s;
13929 c = *end;
13930
13931 l = 0;
13932 switch (c)
13933 {
13934 case '\0':
13935 break;
13936
13937 case ' ':
13938 s++;
13939 break;
13940
13941 case '.':
13942 s++;
13943 if (*s == 't')
13944 {
13945 l = 2;
13946 s++;
13947 }
13948 else if (*s == 'e')
13949 {
13950 l = 4;
13951 s++;
13952 }
13953 if (*s == '\0')
13954 break;
13955 else if (*s++ == ' ')
13956 break;
13957 /* Fall through. */
13958 default:
13959 set_insn_error (0, _("unrecognized opcode"));
13960 return;
13961 }
13962 forced_insn_length = l;
13963
13964 *end = 0;
13965 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
13966 *end = c;
13967
13968 if (!first)
13969 {
13970 set_insn_error (0, _("unrecognized opcode"));
13971 return;
13972 }
13973
13974 tokens = mips_parse_arguments (s, 0);
13975 if (!tokens)
13976 return;
13977
13978 if (!match_mips16_insns (insn, first, tokens))
13979 set_insn_error (0, _("invalid operands"));
13980
13981 obstack_free (&mips_operand_tokens, tokens);
13982 }
13983
13984 /* Marshal immediate value VAL for an extended MIPS16 instruction.
13985 NBITS is the number of significant bits in VAL. */
13986
13987 static unsigned long
13988 mips16_immed_extend (offsetT val, unsigned int nbits)
13989 {
13990 int extval;
13991 if (nbits == 16)
13992 {
13993 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
13994 val &= 0x1f;
13995 }
13996 else if (nbits == 15)
13997 {
13998 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
13999 val &= 0xf;
14000 }
14001 else
14002 {
14003 extval = ((val & 0x1f) << 6) | (val & 0x20);
14004 val = 0;
14005 }
14006 return (extval << 16) | val;
14007 }
14008
14009 /* Like decode_mips16_operand, but require the operand to be defined and
14010 require it to be an integer. */
14011
14012 static const struct mips_int_operand *
14013 mips16_immed_operand (int type, bfd_boolean extended_p)
14014 {
14015 const struct mips_operand *operand;
14016
14017 operand = decode_mips16_operand (type, extended_p);
14018 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14019 abort ();
14020 return (const struct mips_int_operand *) operand;
14021 }
14022
14023 /* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14024
14025 static bfd_boolean
14026 mips16_immed_in_range_p (const struct mips_int_operand *operand,
14027 bfd_reloc_code_real_type reloc, offsetT sval)
14028 {
14029 int min_val, max_val;
14030
14031 min_val = mips_int_operand_min (operand);
14032 max_val = mips_int_operand_max (operand);
14033 if (reloc != BFD_RELOC_UNUSED)
14034 {
14035 if (min_val < 0)
14036 sval = SEXT_16BIT (sval);
14037 else
14038 sval &= 0xffff;
14039 }
14040
14041 return (sval >= min_val
14042 && sval <= max_val
14043 && (sval & ((1 << operand->shift) - 1)) == 0);
14044 }
14045
14046 /* Install immediate value VAL into MIPS16 instruction *INSN,
14047 extending it if necessary. The instruction in *INSN may
14048 already be extended.
14049
14050 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14051 if none. In the former case, VAL is a 16-bit number with no
14052 defined signedness.
14053
14054 TYPE is the type of the immediate field. USER_INSN_LENGTH
14055 is the length that the user requested, or 0 if none. */
14056
14057 static void
14058 mips16_immed (const char *file, unsigned int line, int type,
14059 bfd_reloc_code_real_type reloc, offsetT val,
14060 unsigned int user_insn_length, unsigned long *insn)
14061 {
14062 const struct mips_int_operand *operand;
14063 unsigned int uval, length;
14064
14065 operand = mips16_immed_operand (type, FALSE);
14066 if (!mips16_immed_in_range_p (operand, reloc, val))
14067 {
14068 /* We need an extended instruction. */
14069 if (user_insn_length == 2)
14070 as_bad_where (file, line, _("invalid unextended operand value"));
14071 else
14072 *insn |= MIPS16_EXTEND;
14073 }
14074 else if (user_insn_length == 4)
14075 {
14076 /* The operand doesn't force an unextended instruction to be extended.
14077 Warn if the user wanted an extended instruction anyway. */
14078 *insn |= MIPS16_EXTEND;
14079 as_warn_where (file, line,
14080 _("extended operand requested but not required"));
14081 }
14082
14083 length = mips16_opcode_length (*insn);
14084 if (length == 4)
14085 {
14086 operand = mips16_immed_operand (type, TRUE);
14087 if (!mips16_immed_in_range_p (operand, reloc, val))
14088 as_bad_where (file, line,
14089 _("operand value out of range for instruction"));
14090 }
14091 uval = ((unsigned int) val >> operand->shift) - operand->bias;
14092 if (length == 2 || operand->root.lsb != 0)
14093 *insn = mips_insert_operand (&operand->root, *insn, uval);
14094 else
14095 *insn |= mips16_immed_extend (uval, operand->root.size);
14096 }
14097 \f
14098 struct percent_op_match
14099 {
14100 const char *str;
14101 bfd_reloc_code_real_type reloc;
14102 };
14103
14104 static const struct percent_op_match mips_percent_op[] =
14105 {
14106 {"%lo", BFD_RELOC_LO16},
14107 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14108 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14109 {"%call16", BFD_RELOC_MIPS_CALL16},
14110 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14111 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14112 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14113 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14114 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14115 {"%got", BFD_RELOC_MIPS_GOT16},
14116 {"%gp_rel", BFD_RELOC_GPREL16},
14117 {"%gprel", BFD_RELOC_GPREL16},
14118 {"%half", BFD_RELOC_16},
14119 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14120 {"%higher", BFD_RELOC_MIPS_HIGHER},
14121 {"%neg", BFD_RELOC_MIPS_SUB},
14122 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14123 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14124 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14125 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14126 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14127 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14128 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14129 {"%hi", BFD_RELOC_HI16_S},
14130 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14131 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
14132 };
14133
14134 static const struct percent_op_match mips16_percent_op[] =
14135 {
14136 {"%lo", BFD_RELOC_MIPS16_LO16},
14137 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
14138 {"%gprel", BFD_RELOC_MIPS16_GPREL},
14139 {"%got", BFD_RELOC_MIPS16_GOT16},
14140 {"%call16", BFD_RELOC_MIPS16_CALL16},
14141 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14142 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14143 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14144 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14145 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14146 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14147 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14148 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14149 };
14150
14151
14152 /* Return true if *STR points to a relocation operator. When returning true,
14153 move *STR over the operator and store its relocation code in *RELOC.
14154 Leave both *STR and *RELOC alone when returning false. */
14155
14156 static bfd_boolean
14157 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14158 {
14159 const struct percent_op_match *percent_op;
14160 size_t limit, i;
14161
14162 if (mips_opts.mips16)
14163 {
14164 percent_op = mips16_percent_op;
14165 limit = ARRAY_SIZE (mips16_percent_op);
14166 }
14167 else
14168 {
14169 percent_op = mips_percent_op;
14170 limit = ARRAY_SIZE (mips_percent_op);
14171 }
14172
14173 for (i = 0; i < limit; i++)
14174 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14175 {
14176 int len = strlen (percent_op[i].str);
14177
14178 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14179 continue;
14180
14181 *str += strlen (percent_op[i].str);
14182 *reloc = percent_op[i].reloc;
14183
14184 /* Check whether the output BFD supports this relocation.
14185 If not, issue an error and fall back on something safe. */
14186 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14187 {
14188 as_bad (_("relocation %s isn't supported by the current ABI"),
14189 percent_op[i].str);
14190 *reloc = BFD_RELOC_UNUSED;
14191 }
14192 return TRUE;
14193 }
14194 return FALSE;
14195 }
14196
14197
14198 /* Parse string STR as a 16-bit relocatable operand. Store the
14199 expression in *EP and the relocations in the array starting
14200 at RELOC. Return the number of relocation operators used.
14201
14202 On exit, EXPR_END points to the first character after the expression. */
14203
14204 static size_t
14205 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14206 char *str)
14207 {
14208 bfd_reloc_code_real_type reversed_reloc[3];
14209 size_t reloc_index, i;
14210 int crux_depth, str_depth;
14211 char *crux;
14212
14213 /* Search for the start of the main expression, recoding relocations
14214 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14215 of the main expression and with CRUX_DEPTH containing the number
14216 of open brackets at that point. */
14217 reloc_index = -1;
14218 str_depth = 0;
14219 do
14220 {
14221 reloc_index++;
14222 crux = str;
14223 crux_depth = str_depth;
14224
14225 /* Skip over whitespace and brackets, keeping count of the number
14226 of brackets. */
14227 while (*str == ' ' || *str == '\t' || *str == '(')
14228 if (*str++ == '(')
14229 str_depth++;
14230 }
14231 while (*str == '%'
14232 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14233 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14234
14235 my_getExpression (ep, crux);
14236 str = expr_end;
14237
14238 /* Match every open bracket. */
14239 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14240 if (*str++ == ')')
14241 crux_depth--;
14242
14243 if (crux_depth > 0)
14244 as_bad (_("unclosed '('"));
14245
14246 expr_end = str;
14247
14248 if (reloc_index != 0)
14249 {
14250 prev_reloc_op_frag = frag_now;
14251 for (i = 0; i < reloc_index; i++)
14252 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14253 }
14254
14255 return reloc_index;
14256 }
14257
14258 static void
14259 my_getExpression (expressionS *ep, char *str)
14260 {
14261 char *save_in;
14262
14263 save_in = input_line_pointer;
14264 input_line_pointer = str;
14265 expression (ep);
14266 expr_end = input_line_pointer;
14267 input_line_pointer = save_in;
14268 }
14269
14270 const char *
14271 md_atof (int type, char *litP, int *sizeP)
14272 {
14273 return ieee_md_atof (type, litP, sizeP, target_big_endian);
14274 }
14275
14276 void
14277 md_number_to_chars (char *buf, valueT val, int n)
14278 {
14279 if (target_big_endian)
14280 number_to_chars_bigendian (buf, val, n);
14281 else
14282 number_to_chars_littleendian (buf, val, n);
14283 }
14284 \f
14285 static int support_64bit_objects(void)
14286 {
14287 const char **list, **l;
14288 int yes;
14289
14290 list = bfd_target_list ();
14291 for (l = list; *l != NULL; l++)
14292 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14293 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14294 break;
14295 yes = (*l != NULL);
14296 free (list);
14297 return yes;
14298 }
14299
14300 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14301 NEW_VALUE. Warn if another value was already specified. Note:
14302 we have to defer parsing the -march and -mtune arguments in order
14303 to handle 'from-abi' correctly, since the ABI might be specified
14304 in a later argument. */
14305
14306 static void
14307 mips_set_option_string (const char **string_ptr, const char *new_value)
14308 {
14309 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14310 as_warn (_("a different %s was already specified, is now %s"),
14311 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14312 new_value);
14313
14314 *string_ptr = new_value;
14315 }
14316
14317 int
14318 md_parse_option (int c, const char *arg)
14319 {
14320 unsigned int i;
14321
14322 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14323 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14324 {
14325 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
14326 c == mips_ases[i].option_on);
14327 return 1;
14328 }
14329
14330 switch (c)
14331 {
14332 case OPTION_CONSTRUCT_FLOATS:
14333 mips_disable_float_construction = 0;
14334 break;
14335
14336 case OPTION_NO_CONSTRUCT_FLOATS:
14337 mips_disable_float_construction = 1;
14338 break;
14339
14340 case OPTION_TRAP:
14341 mips_trap = 1;
14342 break;
14343
14344 case OPTION_BREAK:
14345 mips_trap = 0;
14346 break;
14347
14348 case OPTION_EB:
14349 target_big_endian = 1;
14350 break;
14351
14352 case OPTION_EL:
14353 target_big_endian = 0;
14354 break;
14355
14356 case 'O':
14357 if (arg == NULL)
14358 mips_optimize = 1;
14359 else if (arg[0] == '0')
14360 mips_optimize = 0;
14361 else if (arg[0] == '1')
14362 mips_optimize = 1;
14363 else
14364 mips_optimize = 2;
14365 break;
14366
14367 case 'g':
14368 if (arg == NULL)
14369 mips_debug = 2;
14370 else
14371 mips_debug = atoi (arg);
14372 break;
14373
14374 case OPTION_MIPS1:
14375 file_mips_opts.isa = ISA_MIPS1;
14376 break;
14377
14378 case OPTION_MIPS2:
14379 file_mips_opts.isa = ISA_MIPS2;
14380 break;
14381
14382 case OPTION_MIPS3:
14383 file_mips_opts.isa = ISA_MIPS3;
14384 break;
14385
14386 case OPTION_MIPS4:
14387 file_mips_opts.isa = ISA_MIPS4;
14388 break;
14389
14390 case OPTION_MIPS5:
14391 file_mips_opts.isa = ISA_MIPS5;
14392 break;
14393
14394 case OPTION_MIPS32:
14395 file_mips_opts.isa = ISA_MIPS32;
14396 break;
14397
14398 case OPTION_MIPS32R2:
14399 file_mips_opts.isa = ISA_MIPS32R2;
14400 break;
14401
14402 case OPTION_MIPS32R3:
14403 file_mips_opts.isa = ISA_MIPS32R3;
14404 break;
14405
14406 case OPTION_MIPS32R5:
14407 file_mips_opts.isa = ISA_MIPS32R5;
14408 break;
14409
14410 case OPTION_MIPS32R6:
14411 file_mips_opts.isa = ISA_MIPS32R6;
14412 break;
14413
14414 case OPTION_MIPS64R2:
14415 file_mips_opts.isa = ISA_MIPS64R2;
14416 break;
14417
14418 case OPTION_MIPS64R3:
14419 file_mips_opts.isa = ISA_MIPS64R3;
14420 break;
14421
14422 case OPTION_MIPS64R5:
14423 file_mips_opts.isa = ISA_MIPS64R5;
14424 break;
14425
14426 case OPTION_MIPS64R6:
14427 file_mips_opts.isa = ISA_MIPS64R6;
14428 break;
14429
14430 case OPTION_MIPS64:
14431 file_mips_opts.isa = ISA_MIPS64;
14432 break;
14433
14434 case OPTION_MTUNE:
14435 mips_set_option_string (&mips_tune_string, arg);
14436 break;
14437
14438 case OPTION_MARCH:
14439 mips_set_option_string (&mips_arch_string, arg);
14440 break;
14441
14442 case OPTION_M4650:
14443 mips_set_option_string (&mips_arch_string, "4650");
14444 mips_set_option_string (&mips_tune_string, "4650");
14445 break;
14446
14447 case OPTION_NO_M4650:
14448 break;
14449
14450 case OPTION_M4010:
14451 mips_set_option_string (&mips_arch_string, "4010");
14452 mips_set_option_string (&mips_tune_string, "4010");
14453 break;
14454
14455 case OPTION_NO_M4010:
14456 break;
14457
14458 case OPTION_M4100:
14459 mips_set_option_string (&mips_arch_string, "4100");
14460 mips_set_option_string (&mips_tune_string, "4100");
14461 break;
14462
14463 case OPTION_NO_M4100:
14464 break;
14465
14466 case OPTION_M3900:
14467 mips_set_option_string (&mips_arch_string, "3900");
14468 mips_set_option_string (&mips_tune_string, "3900");
14469 break;
14470
14471 case OPTION_NO_M3900:
14472 break;
14473
14474 case OPTION_MICROMIPS:
14475 if (file_mips_opts.mips16 == 1)
14476 {
14477 as_bad (_("-mmicromips cannot be used with -mips16"));
14478 return 0;
14479 }
14480 file_mips_opts.micromips = 1;
14481 mips_no_prev_insn ();
14482 break;
14483
14484 case OPTION_NO_MICROMIPS:
14485 file_mips_opts.micromips = 0;
14486 mips_no_prev_insn ();
14487 break;
14488
14489 case OPTION_MIPS16:
14490 if (file_mips_opts.micromips == 1)
14491 {
14492 as_bad (_("-mips16 cannot be used with -micromips"));
14493 return 0;
14494 }
14495 file_mips_opts.mips16 = 1;
14496 mips_no_prev_insn ();
14497 break;
14498
14499 case OPTION_NO_MIPS16:
14500 file_mips_opts.mips16 = 0;
14501 mips_no_prev_insn ();
14502 break;
14503
14504 case OPTION_FIX_24K:
14505 mips_fix_24k = 1;
14506 break;
14507
14508 case OPTION_NO_FIX_24K:
14509 mips_fix_24k = 0;
14510 break;
14511
14512 case OPTION_FIX_RM7000:
14513 mips_fix_rm7000 = 1;
14514 break;
14515
14516 case OPTION_NO_FIX_RM7000:
14517 mips_fix_rm7000 = 0;
14518 break;
14519
14520 case OPTION_FIX_LOONGSON2F_JUMP:
14521 mips_fix_loongson2f_jump = TRUE;
14522 break;
14523
14524 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14525 mips_fix_loongson2f_jump = FALSE;
14526 break;
14527
14528 case OPTION_FIX_LOONGSON2F_NOP:
14529 mips_fix_loongson2f_nop = TRUE;
14530 break;
14531
14532 case OPTION_NO_FIX_LOONGSON2F_NOP:
14533 mips_fix_loongson2f_nop = FALSE;
14534 break;
14535
14536 case OPTION_FIX_VR4120:
14537 mips_fix_vr4120 = 1;
14538 break;
14539
14540 case OPTION_NO_FIX_VR4120:
14541 mips_fix_vr4120 = 0;
14542 break;
14543
14544 case OPTION_FIX_VR4130:
14545 mips_fix_vr4130 = 1;
14546 break;
14547
14548 case OPTION_NO_FIX_VR4130:
14549 mips_fix_vr4130 = 0;
14550 break;
14551
14552 case OPTION_FIX_CN63XXP1:
14553 mips_fix_cn63xxp1 = TRUE;
14554 break;
14555
14556 case OPTION_NO_FIX_CN63XXP1:
14557 mips_fix_cn63xxp1 = FALSE;
14558 break;
14559
14560 case OPTION_RELAX_BRANCH:
14561 mips_relax_branch = 1;
14562 break;
14563
14564 case OPTION_NO_RELAX_BRANCH:
14565 mips_relax_branch = 0;
14566 break;
14567
14568 case OPTION_IGNORE_BRANCH_ISA:
14569 mips_ignore_branch_isa = TRUE;
14570 break;
14571
14572 case OPTION_NO_IGNORE_BRANCH_ISA:
14573 mips_ignore_branch_isa = FALSE;
14574 break;
14575
14576 case OPTION_INSN32:
14577 file_mips_opts.insn32 = TRUE;
14578 break;
14579
14580 case OPTION_NO_INSN32:
14581 file_mips_opts.insn32 = FALSE;
14582 break;
14583
14584 case OPTION_MSHARED:
14585 mips_in_shared = TRUE;
14586 break;
14587
14588 case OPTION_MNO_SHARED:
14589 mips_in_shared = FALSE;
14590 break;
14591
14592 case OPTION_MSYM32:
14593 file_mips_opts.sym32 = TRUE;
14594 break;
14595
14596 case OPTION_MNO_SYM32:
14597 file_mips_opts.sym32 = FALSE;
14598 break;
14599
14600 /* When generating ELF code, we permit -KPIC and -call_shared to
14601 select SVR4_PIC, and -non_shared to select no PIC. This is
14602 intended to be compatible with Irix 5. */
14603 case OPTION_CALL_SHARED:
14604 mips_pic = SVR4_PIC;
14605 mips_abicalls = TRUE;
14606 break;
14607
14608 case OPTION_CALL_NONPIC:
14609 mips_pic = NO_PIC;
14610 mips_abicalls = TRUE;
14611 break;
14612
14613 case OPTION_NON_SHARED:
14614 mips_pic = NO_PIC;
14615 mips_abicalls = FALSE;
14616 break;
14617
14618 /* The -xgot option tells the assembler to use 32 bit offsets
14619 when accessing the got in SVR4_PIC mode. It is for Irix
14620 compatibility. */
14621 case OPTION_XGOT:
14622 mips_big_got = 1;
14623 break;
14624
14625 case 'G':
14626 g_switch_value = atoi (arg);
14627 g_switch_seen = 1;
14628 break;
14629
14630 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14631 and -mabi=64. */
14632 case OPTION_32:
14633 mips_abi = O32_ABI;
14634 break;
14635
14636 case OPTION_N32:
14637 mips_abi = N32_ABI;
14638 break;
14639
14640 case OPTION_64:
14641 mips_abi = N64_ABI;
14642 if (!support_64bit_objects())
14643 as_fatal (_("no compiled in support for 64 bit object file format"));
14644 break;
14645
14646 case OPTION_GP32:
14647 file_mips_opts.gp = 32;
14648 break;
14649
14650 case OPTION_GP64:
14651 file_mips_opts.gp = 64;
14652 break;
14653
14654 case OPTION_FP32:
14655 file_mips_opts.fp = 32;
14656 break;
14657
14658 case OPTION_FPXX:
14659 file_mips_opts.fp = 0;
14660 break;
14661
14662 case OPTION_FP64:
14663 file_mips_opts.fp = 64;
14664 break;
14665
14666 case OPTION_ODD_SPREG:
14667 file_mips_opts.oddspreg = 1;
14668 break;
14669
14670 case OPTION_NO_ODD_SPREG:
14671 file_mips_opts.oddspreg = 0;
14672 break;
14673
14674 case OPTION_SINGLE_FLOAT:
14675 file_mips_opts.single_float = 1;
14676 break;
14677
14678 case OPTION_DOUBLE_FLOAT:
14679 file_mips_opts.single_float = 0;
14680 break;
14681
14682 case OPTION_SOFT_FLOAT:
14683 file_mips_opts.soft_float = 1;
14684 break;
14685
14686 case OPTION_HARD_FLOAT:
14687 file_mips_opts.soft_float = 0;
14688 break;
14689
14690 case OPTION_MABI:
14691 if (strcmp (arg, "32") == 0)
14692 mips_abi = O32_ABI;
14693 else if (strcmp (arg, "o64") == 0)
14694 mips_abi = O64_ABI;
14695 else if (strcmp (arg, "n32") == 0)
14696 mips_abi = N32_ABI;
14697 else if (strcmp (arg, "64") == 0)
14698 {
14699 mips_abi = N64_ABI;
14700 if (! support_64bit_objects())
14701 as_fatal (_("no compiled in support for 64 bit object file "
14702 "format"));
14703 }
14704 else if (strcmp (arg, "eabi") == 0)
14705 mips_abi = EABI_ABI;
14706 else
14707 {
14708 as_fatal (_("invalid abi -mabi=%s"), arg);
14709 return 0;
14710 }
14711 break;
14712
14713 case OPTION_M7000_HILO_FIX:
14714 mips_7000_hilo_fix = TRUE;
14715 break;
14716
14717 case OPTION_MNO_7000_HILO_FIX:
14718 mips_7000_hilo_fix = FALSE;
14719 break;
14720
14721 case OPTION_MDEBUG:
14722 mips_flag_mdebug = TRUE;
14723 break;
14724
14725 case OPTION_NO_MDEBUG:
14726 mips_flag_mdebug = FALSE;
14727 break;
14728
14729 case OPTION_PDR:
14730 mips_flag_pdr = TRUE;
14731 break;
14732
14733 case OPTION_NO_PDR:
14734 mips_flag_pdr = FALSE;
14735 break;
14736
14737 case OPTION_MVXWORKS_PIC:
14738 mips_pic = VXWORKS_PIC;
14739 break;
14740
14741 case OPTION_NAN:
14742 if (strcmp (arg, "2008") == 0)
14743 mips_nan2008 = 1;
14744 else if (strcmp (arg, "legacy") == 0)
14745 mips_nan2008 = 0;
14746 else
14747 {
14748 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
14749 return 0;
14750 }
14751 break;
14752
14753 default:
14754 return 0;
14755 }
14756
14757 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14758
14759 return 1;
14760 }
14761 \f
14762 /* Set up globals to tune for the ISA or processor described by INFO. */
14763
14764 static void
14765 mips_set_tune (const struct mips_cpu_info *info)
14766 {
14767 if (info != 0)
14768 mips_tune = info->cpu;
14769 }
14770
14771
14772 void
14773 mips_after_parse_args (void)
14774 {
14775 const struct mips_cpu_info *arch_info = 0;
14776 const struct mips_cpu_info *tune_info = 0;
14777
14778 /* GP relative stuff not working for PE */
14779 if (strncmp (TARGET_OS, "pe", 2) == 0)
14780 {
14781 if (g_switch_seen && g_switch_value != 0)
14782 as_bad (_("-G not supported in this configuration"));
14783 g_switch_value = 0;
14784 }
14785
14786 if (mips_abi == NO_ABI)
14787 mips_abi = MIPS_DEFAULT_ABI;
14788
14789 /* The following code determines the architecture.
14790 Similar code was added to GCC 3.3 (see override_options() in
14791 config/mips/mips.c). The GAS and GCC code should be kept in sync
14792 as much as possible. */
14793
14794 if (mips_arch_string != 0)
14795 arch_info = mips_parse_cpu ("-march", mips_arch_string);
14796
14797 if (file_mips_opts.isa != ISA_UNKNOWN)
14798 {
14799 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
14800 ISA level specified by -mipsN, while arch_info->isa contains
14801 the -march selection (if any). */
14802 if (arch_info != 0)
14803 {
14804 /* -march takes precedence over -mipsN, since it is more descriptive.
14805 There's no harm in specifying both as long as the ISA levels
14806 are the same. */
14807 if (file_mips_opts.isa != arch_info->isa)
14808 as_bad (_("-%s conflicts with the other architecture options,"
14809 " which imply -%s"),
14810 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
14811 mips_cpu_info_from_isa (arch_info->isa)->name);
14812 }
14813 else
14814 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
14815 }
14816
14817 if (arch_info == 0)
14818 {
14819 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
14820 gas_assert (arch_info);
14821 }
14822
14823 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
14824 as_bad (_("-march=%s is not compatible with the selected ABI"),
14825 arch_info->name);
14826
14827 file_mips_opts.arch = arch_info->cpu;
14828 file_mips_opts.isa = arch_info->isa;
14829
14830 /* Set up initial mips_opts state. */
14831 mips_opts = file_mips_opts;
14832
14833 /* The register size inference code is now placed in
14834 file_mips_check_options. */
14835
14836 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
14837 processor. */
14838 if (mips_tune_string != 0)
14839 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
14840
14841 if (tune_info == 0)
14842 mips_set_tune (arch_info);
14843 else
14844 mips_set_tune (tune_info);
14845
14846 if (mips_flag_mdebug < 0)
14847 mips_flag_mdebug = 0;
14848 }
14849 \f
14850 void
14851 mips_init_after_args (void)
14852 {
14853 /* initialize opcodes */
14854 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
14855 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
14856 }
14857
14858 long
14859 md_pcrel_from (fixS *fixP)
14860 {
14861 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
14862 switch (fixP->fx_r_type)
14863 {
14864 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14865 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14866 /* Return the address of the delay slot. */
14867 return addr + 2;
14868
14869 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14870 case BFD_RELOC_MICROMIPS_JMP:
14871 case BFD_RELOC_MIPS16_16_PCREL_S1:
14872 case BFD_RELOC_16_PCREL_S2:
14873 case BFD_RELOC_MIPS_21_PCREL_S2:
14874 case BFD_RELOC_MIPS_26_PCREL_S2:
14875 case BFD_RELOC_MIPS_JMP:
14876 /* Return the address of the delay slot. */
14877 return addr + 4;
14878
14879 case BFD_RELOC_MIPS_18_PCREL_S3:
14880 /* Return the aligned address of the doubleword containing
14881 the instruction. */
14882 return addr & ~7;
14883
14884 default:
14885 return addr;
14886 }
14887 }
14888
14889 /* This is called before the symbol table is processed. In order to
14890 work with gcc when using mips-tfile, we must keep all local labels.
14891 However, in other cases, we want to discard them. If we were
14892 called with -g, but we didn't see any debugging information, it may
14893 mean that gcc is smuggling debugging information through to
14894 mips-tfile, in which case we must generate all local labels. */
14895
14896 void
14897 mips_frob_file_before_adjust (void)
14898 {
14899 #ifndef NO_ECOFF_DEBUGGING
14900 if (ECOFF_DEBUGGING
14901 && mips_debug != 0
14902 && ! ecoff_debugging_seen)
14903 flag_keep_locals = 1;
14904 #endif
14905 }
14906
14907 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
14908 the corresponding LO16 reloc. This is called before md_apply_fix and
14909 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
14910 relocation operators.
14911
14912 For our purposes, a %lo() expression matches a %got() or %hi()
14913 expression if:
14914
14915 (a) it refers to the same symbol; and
14916 (b) the offset applied in the %lo() expression is no lower than
14917 the offset applied in the %got() or %hi().
14918
14919 (b) allows us to cope with code like:
14920
14921 lui $4,%hi(foo)
14922 lh $4,%lo(foo+2)($4)
14923
14924 ...which is legal on RELA targets, and has a well-defined behaviour
14925 if the user knows that adding 2 to "foo" will not induce a carry to
14926 the high 16 bits.
14927
14928 When several %lo()s match a particular %got() or %hi(), we use the
14929 following rules to distinguish them:
14930
14931 (1) %lo()s with smaller offsets are a better match than %lo()s with
14932 higher offsets.
14933
14934 (2) %lo()s with no matching %got() or %hi() are better than those
14935 that already have a matching %got() or %hi().
14936
14937 (3) later %lo()s are better than earlier %lo()s.
14938
14939 These rules are applied in order.
14940
14941 (1) means, among other things, that %lo()s with identical offsets are
14942 chosen if they exist.
14943
14944 (2) means that we won't associate several high-part relocations with
14945 the same low-part relocation unless there's no alternative. Having
14946 several high parts for the same low part is a GNU extension; this rule
14947 allows careful users to avoid it.
14948
14949 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
14950 with the last high-part relocation being at the front of the list.
14951 It therefore makes sense to choose the last matching low-part
14952 relocation, all other things being equal. It's also easier
14953 to code that way. */
14954
14955 void
14956 mips_frob_file (void)
14957 {
14958 struct mips_hi_fixup *l;
14959 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
14960
14961 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
14962 {
14963 segment_info_type *seginfo;
14964 bfd_boolean matched_lo_p;
14965 fixS **hi_pos, **lo_pos, **pos;
14966
14967 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
14968
14969 /* If a GOT16 relocation turns out to be against a global symbol,
14970 there isn't supposed to be a matching LO. Ignore %gots against
14971 constants; we'll report an error for those later. */
14972 if (got16_reloc_p (l->fixp->fx_r_type)
14973 && !(l->fixp->fx_addsy
14974 && pic_need_relax (l->fixp->fx_addsy)))
14975 continue;
14976
14977 /* Check quickly whether the next fixup happens to be a matching %lo. */
14978 if (fixup_has_matching_lo_p (l->fixp))
14979 continue;
14980
14981 seginfo = seg_info (l->seg);
14982
14983 /* Set HI_POS to the position of this relocation in the chain.
14984 Set LO_POS to the position of the chosen low-part relocation.
14985 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
14986 relocation that matches an immediately-preceding high-part
14987 relocation. */
14988 hi_pos = NULL;
14989 lo_pos = NULL;
14990 matched_lo_p = FALSE;
14991 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
14992
14993 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
14994 {
14995 if (*pos == l->fixp)
14996 hi_pos = pos;
14997
14998 if ((*pos)->fx_r_type == looking_for_rtype
14999 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15000 && (*pos)->fx_offset >= l->fixp->fx_offset
15001 && (lo_pos == NULL
15002 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15003 || (!matched_lo_p
15004 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15005 lo_pos = pos;
15006
15007 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15008 && fixup_has_matching_lo_p (*pos));
15009 }
15010
15011 /* If we found a match, remove the high-part relocation from its
15012 current position and insert it before the low-part relocation.
15013 Make the offsets match so that fixup_has_matching_lo_p()
15014 will return true.
15015
15016 We don't warn about unmatched high-part relocations since some
15017 versions of gcc have been known to emit dead "lui ...%hi(...)"
15018 instructions. */
15019 if (lo_pos != NULL)
15020 {
15021 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15022 if (l->fixp->fx_next != *lo_pos)
15023 {
15024 *hi_pos = l->fixp->fx_next;
15025 l->fixp->fx_next = *lo_pos;
15026 *lo_pos = l->fixp;
15027 }
15028 }
15029 }
15030 }
15031
15032 int
15033 mips_force_relocation (fixS *fixp)
15034 {
15035 if (generic_force_reloc (fixp))
15036 return 1;
15037
15038 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15039 so that the linker relaxation can update targets. */
15040 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15041 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15042 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15043 return 1;
15044
15045 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15046 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15047 microMIPS symbols so that we can do cross-mode branch diagnostics
15048 and BAL to JALX conversion by the linker. */
15049 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15050 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15051 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15052 && fixp->fx_addsy
15053 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15054 return 1;
15055
15056 /* We want all PC-relative relocations to be kept for R6 relaxation. */
15057 if (ISA_IS_R6 (file_mips_opts.isa)
15058 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15059 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15060 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15061 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15062 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15063 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15064 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15065 return 1;
15066
15067 return 0;
15068 }
15069
15070 /* Implement TC_FORCE_RELOCATION_ABS. */
15071
15072 bfd_boolean
15073 mips_force_relocation_abs (fixS *fixp)
15074 {
15075 if (generic_force_reloc (fixp))
15076 return TRUE;
15077
15078 /* These relocations do not have enough bits in the in-place addend
15079 to hold an arbitrary absolute section's offset. */
15080 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15081 return TRUE;
15082
15083 return FALSE;
15084 }
15085
15086 /* Read the instruction associated with RELOC from BUF. */
15087
15088 static unsigned int
15089 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15090 {
15091 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15092 return read_compressed_insn (buf, 4);
15093 else
15094 return read_insn (buf);
15095 }
15096
15097 /* Write instruction INSN to BUF, given that it has been relocated
15098 by RELOC. */
15099
15100 static void
15101 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15102 unsigned long insn)
15103 {
15104 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15105 write_compressed_insn (buf, insn, 4);
15106 else
15107 write_insn (buf, insn);
15108 }
15109
15110 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15111 to a symbol in another ISA mode, which cannot be converted to JALX. */
15112
15113 static bfd_boolean
15114 fix_bad_cross_mode_jump_p (fixS *fixP)
15115 {
15116 unsigned long opcode;
15117 int other;
15118 char *buf;
15119
15120 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15121 return FALSE;
15122
15123 other = S_GET_OTHER (fixP->fx_addsy);
15124 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15125 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15126 switch (fixP->fx_r_type)
15127 {
15128 case BFD_RELOC_MIPS_JMP:
15129 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15130 case BFD_RELOC_MICROMIPS_JMP:
15131 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15132 default:
15133 return FALSE;
15134 }
15135 }
15136
15137 /* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15138 jump to a symbol in the same ISA mode. */
15139
15140 static bfd_boolean
15141 fix_bad_same_mode_jalx_p (fixS *fixP)
15142 {
15143 unsigned long opcode;
15144 int other;
15145 char *buf;
15146
15147 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15148 return FALSE;
15149
15150 other = S_GET_OTHER (fixP->fx_addsy);
15151 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15152 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15153 switch (fixP->fx_r_type)
15154 {
15155 case BFD_RELOC_MIPS_JMP:
15156 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15157 case BFD_RELOC_MIPS16_JMP:
15158 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15159 case BFD_RELOC_MICROMIPS_JMP:
15160 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15161 default:
15162 return FALSE;
15163 }
15164 }
15165
15166 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15167 to a symbol whose value plus addend is not aligned according to the
15168 ultimate (after linker relaxation) jump instruction's immediate field
15169 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15170 regular MIPS code, to (1 << 2). */
15171
15172 static bfd_boolean
15173 fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15174 {
15175 bfd_boolean micro_to_mips_p;
15176 valueT val;
15177 int other;
15178
15179 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15180 return FALSE;
15181
15182 other = S_GET_OTHER (fixP->fx_addsy);
15183 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15184 val += fixP->fx_offset;
15185 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15186 && !ELF_ST_IS_MICROMIPS (other));
15187 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15188 != ELF_ST_IS_COMPRESSED (other));
15189 }
15190
15191 /* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15192 to a symbol whose annotation indicates another ISA mode. For absolute
15193 symbols check the ISA bit instead.
15194
15195 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15196 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15197 MIPS symbols and associated with BAL instructions as these instructions
15198 may be be converted to JALX by the linker. */
15199
15200 static bfd_boolean
15201 fix_bad_cross_mode_branch_p (fixS *fixP)
15202 {
15203 bfd_boolean absolute_p;
15204 unsigned long opcode;
15205 asection *symsec;
15206 valueT val;
15207 int other;
15208 char *buf;
15209
15210 if (mips_ignore_branch_isa)
15211 return FALSE;
15212
15213 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15214 return FALSE;
15215
15216 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15217 absolute_p = bfd_is_abs_section (symsec);
15218
15219 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15220 other = S_GET_OTHER (fixP->fx_addsy);
15221
15222 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15223 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15224 switch (fixP->fx_r_type)
15225 {
15226 case BFD_RELOC_16_PCREL_S2:
15227 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15228 && opcode != 0x0411);
15229 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15230 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15231 && opcode != 0x4060);
15232 case BFD_RELOC_MIPS_21_PCREL_S2:
15233 case BFD_RELOC_MIPS_26_PCREL_S2:
15234 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15235 case BFD_RELOC_MIPS16_16_PCREL_S1:
15236 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15237 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15238 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15239 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15240 default:
15241 abort ();
15242 }
15243 }
15244
15245 /* Return TRUE if the symbol plus addend associated with a regular MIPS
15246 branch instruction pointed to by FIXP is not aligned according to the
15247 branch instruction's immediate field requirement. We need the addend
15248 to preserve the ISA bit and also the sum must not have bit 2 set. We
15249 must explicitly OR in the ISA bit from symbol annotation as the bit
15250 won't be set in the symbol's value then. */
15251
15252 static bfd_boolean
15253 fix_bad_misaligned_branch_p (fixS *fixP)
15254 {
15255 bfd_boolean absolute_p;
15256 asection *symsec;
15257 valueT isa_bit;
15258 valueT val;
15259 valueT off;
15260 int other;
15261
15262 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15263 return FALSE;
15264
15265 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15266 absolute_p = bfd_is_abs_section (symsec);
15267
15268 val = S_GET_VALUE (fixP->fx_addsy);
15269 other = S_GET_OTHER (fixP->fx_addsy);
15270 off = fixP->fx_offset;
15271
15272 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15273 val |= ELF_ST_IS_COMPRESSED (other);
15274 val += off;
15275 return (val & 0x3) != isa_bit;
15276 }
15277
15278 /* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15279 and its calculated value VAL. */
15280
15281 static void
15282 fix_validate_branch (fixS *fixP, valueT val)
15283 {
15284 if (fixP->fx_done && (val & 0x3) != 0)
15285 as_bad_where (fixP->fx_file, fixP->fx_line,
15286 _("branch to misaligned address (0x%lx)"),
15287 (long) (val + md_pcrel_from (fixP)));
15288 else if (fix_bad_cross_mode_branch_p (fixP))
15289 as_bad_where (fixP->fx_file, fixP->fx_line,
15290 _("branch to a symbol in another ISA mode"));
15291 else if (fix_bad_misaligned_branch_p (fixP))
15292 as_bad_where (fixP->fx_file, fixP->fx_line,
15293 _("branch to misaligned address (0x%lx)"),
15294 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15295 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15296 as_bad_where (fixP->fx_file, fixP->fx_line,
15297 _("cannot encode misaligned addend "
15298 "in the relocatable field (0x%lx)"),
15299 (long) fixP->fx_offset);
15300 }
15301
15302 /* Apply a fixup to the object file. */
15303
15304 void
15305 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15306 {
15307 char *buf;
15308 unsigned long insn;
15309 reloc_howto_type *howto;
15310
15311 if (fixP->fx_pcrel)
15312 switch (fixP->fx_r_type)
15313 {
15314 case BFD_RELOC_16_PCREL_S2:
15315 case BFD_RELOC_MIPS16_16_PCREL_S1:
15316 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15317 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15318 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15319 case BFD_RELOC_32_PCREL:
15320 case BFD_RELOC_MIPS_21_PCREL_S2:
15321 case BFD_RELOC_MIPS_26_PCREL_S2:
15322 case BFD_RELOC_MIPS_18_PCREL_S3:
15323 case BFD_RELOC_MIPS_19_PCREL_S2:
15324 case BFD_RELOC_HI16_S_PCREL:
15325 case BFD_RELOC_LO16_PCREL:
15326 break;
15327
15328 case BFD_RELOC_32:
15329 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15330 break;
15331
15332 default:
15333 as_bad_where (fixP->fx_file, fixP->fx_line,
15334 _("PC-relative reference to a different section"));
15335 break;
15336 }
15337
15338 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15339 that have no MIPS ELF equivalent. */
15340 if (fixP->fx_r_type != BFD_RELOC_8)
15341 {
15342 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15343 if (!howto)
15344 return;
15345 }
15346
15347 gas_assert (fixP->fx_size == 2
15348 || fixP->fx_size == 4
15349 || fixP->fx_r_type == BFD_RELOC_8
15350 || fixP->fx_r_type == BFD_RELOC_16
15351 || fixP->fx_r_type == BFD_RELOC_64
15352 || fixP->fx_r_type == BFD_RELOC_CTOR
15353 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15354 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15355 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15356 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15357 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15358 || fixP->fx_r_type == BFD_RELOC_NONE);
15359
15360 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15361
15362 /* Don't treat parts of a composite relocation as done. There are two
15363 reasons for this:
15364
15365 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15366 should nevertheless be emitted if the first part is.
15367
15368 (2) In normal usage, composite relocations are never assembly-time
15369 constants. The easiest way of dealing with the pathological
15370 exceptions is to generate a relocation against STN_UNDEF and
15371 leave everything up to the linker. */
15372 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15373 fixP->fx_done = 1;
15374
15375 switch (fixP->fx_r_type)
15376 {
15377 case BFD_RELOC_MIPS_TLS_GD:
15378 case BFD_RELOC_MIPS_TLS_LDM:
15379 case BFD_RELOC_MIPS_TLS_DTPREL32:
15380 case BFD_RELOC_MIPS_TLS_DTPREL64:
15381 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15382 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15383 case BFD_RELOC_MIPS_TLS_GOTTPREL:
15384 case BFD_RELOC_MIPS_TLS_TPREL32:
15385 case BFD_RELOC_MIPS_TLS_TPREL64:
15386 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15387 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15388 case BFD_RELOC_MICROMIPS_TLS_GD:
15389 case BFD_RELOC_MICROMIPS_TLS_LDM:
15390 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15391 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15392 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15393 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15394 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15395 case BFD_RELOC_MIPS16_TLS_GD:
15396 case BFD_RELOC_MIPS16_TLS_LDM:
15397 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15398 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15399 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15400 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15401 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15402 if (fixP->fx_addsy)
15403 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15404 else
15405 as_bad_where (fixP->fx_file, fixP->fx_line,
15406 _("TLS relocation against a constant"));
15407 break;
15408
15409 case BFD_RELOC_MIPS_JMP:
15410 case BFD_RELOC_MIPS16_JMP:
15411 case BFD_RELOC_MICROMIPS_JMP:
15412 {
15413 int shift;
15414
15415 gas_assert (!fixP->fx_done);
15416
15417 /* Shift is 2, unusually, for microMIPS JALX. */
15418 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15419 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15420 shift = 1;
15421 else
15422 shift = 2;
15423
15424 if (fix_bad_cross_mode_jump_p (fixP))
15425 as_bad_where (fixP->fx_file, fixP->fx_line,
15426 _("jump to a symbol in another ISA mode"));
15427 else if (fix_bad_same_mode_jalx_p (fixP))
15428 as_bad_where (fixP->fx_file, fixP->fx_line,
15429 _("JALX to a symbol in the same ISA mode"));
15430 else if (fix_bad_misaligned_jump_p (fixP, shift))
15431 as_bad_where (fixP->fx_file, fixP->fx_line,
15432 _("jump to misaligned address (0x%lx)"),
15433 (long) (S_GET_VALUE (fixP->fx_addsy)
15434 + fixP->fx_offset));
15435 else if (HAVE_IN_PLACE_ADDENDS
15436 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15437 as_bad_where (fixP->fx_file, fixP->fx_line,
15438 _("cannot encode misaligned addend "
15439 "in the relocatable field (0x%lx)"),
15440 (long) fixP->fx_offset);
15441 }
15442 /* Fall through. */
15443
15444 case BFD_RELOC_MIPS_SHIFT5:
15445 case BFD_RELOC_MIPS_SHIFT6:
15446 case BFD_RELOC_MIPS_GOT_DISP:
15447 case BFD_RELOC_MIPS_GOT_PAGE:
15448 case BFD_RELOC_MIPS_GOT_OFST:
15449 case BFD_RELOC_MIPS_SUB:
15450 case BFD_RELOC_MIPS_INSERT_A:
15451 case BFD_RELOC_MIPS_INSERT_B:
15452 case BFD_RELOC_MIPS_DELETE:
15453 case BFD_RELOC_MIPS_HIGHEST:
15454 case BFD_RELOC_MIPS_HIGHER:
15455 case BFD_RELOC_MIPS_SCN_DISP:
15456 case BFD_RELOC_MIPS_REL16:
15457 case BFD_RELOC_MIPS_RELGOT:
15458 case BFD_RELOC_MIPS_JALR:
15459 case BFD_RELOC_HI16:
15460 case BFD_RELOC_HI16_S:
15461 case BFD_RELOC_LO16:
15462 case BFD_RELOC_GPREL16:
15463 case BFD_RELOC_MIPS_LITERAL:
15464 case BFD_RELOC_MIPS_CALL16:
15465 case BFD_RELOC_MIPS_GOT16:
15466 case BFD_RELOC_GPREL32:
15467 case BFD_RELOC_MIPS_GOT_HI16:
15468 case BFD_RELOC_MIPS_GOT_LO16:
15469 case BFD_RELOC_MIPS_CALL_HI16:
15470 case BFD_RELOC_MIPS_CALL_LO16:
15471 case BFD_RELOC_HI16_S_PCREL:
15472 case BFD_RELOC_LO16_PCREL:
15473 case BFD_RELOC_MIPS16_GPREL:
15474 case BFD_RELOC_MIPS16_GOT16:
15475 case BFD_RELOC_MIPS16_CALL16:
15476 case BFD_RELOC_MIPS16_HI16:
15477 case BFD_RELOC_MIPS16_HI16_S:
15478 case BFD_RELOC_MIPS16_LO16:
15479 case BFD_RELOC_MICROMIPS_GOT_DISP:
15480 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15481 case BFD_RELOC_MICROMIPS_GOT_OFST:
15482 case BFD_RELOC_MICROMIPS_SUB:
15483 case BFD_RELOC_MICROMIPS_HIGHEST:
15484 case BFD_RELOC_MICROMIPS_HIGHER:
15485 case BFD_RELOC_MICROMIPS_SCN_DISP:
15486 case BFD_RELOC_MICROMIPS_JALR:
15487 case BFD_RELOC_MICROMIPS_HI16:
15488 case BFD_RELOC_MICROMIPS_HI16_S:
15489 case BFD_RELOC_MICROMIPS_LO16:
15490 case BFD_RELOC_MICROMIPS_GPREL16:
15491 case BFD_RELOC_MICROMIPS_LITERAL:
15492 case BFD_RELOC_MICROMIPS_CALL16:
15493 case BFD_RELOC_MICROMIPS_GOT16:
15494 case BFD_RELOC_MICROMIPS_GOT_HI16:
15495 case BFD_RELOC_MICROMIPS_GOT_LO16:
15496 case BFD_RELOC_MICROMIPS_CALL_HI16:
15497 case BFD_RELOC_MICROMIPS_CALL_LO16:
15498 case BFD_RELOC_MIPS_EH:
15499 if (fixP->fx_done)
15500 {
15501 offsetT value;
15502
15503 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15504 {
15505 insn = read_reloc_insn (buf, fixP->fx_r_type);
15506 if (mips16_reloc_p (fixP->fx_r_type))
15507 insn |= mips16_immed_extend (value, 16);
15508 else
15509 insn |= (value & 0xffff);
15510 write_reloc_insn (buf, fixP->fx_r_type, insn);
15511 }
15512 else
15513 as_bad_where (fixP->fx_file, fixP->fx_line,
15514 _("unsupported constant in relocation"));
15515 }
15516 break;
15517
15518 case BFD_RELOC_64:
15519 /* This is handled like BFD_RELOC_32, but we output a sign
15520 extended value if we are only 32 bits. */
15521 if (fixP->fx_done)
15522 {
15523 if (8 <= sizeof (valueT))
15524 md_number_to_chars (buf, *valP, 8);
15525 else
15526 {
15527 valueT hiv;
15528
15529 if ((*valP & 0x80000000) != 0)
15530 hiv = 0xffffffff;
15531 else
15532 hiv = 0;
15533 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15534 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
15535 }
15536 }
15537 break;
15538
15539 case BFD_RELOC_RVA:
15540 case BFD_RELOC_32:
15541 case BFD_RELOC_32_PCREL:
15542 case BFD_RELOC_16:
15543 case BFD_RELOC_8:
15544 /* If we are deleting this reloc entry, we must fill in the
15545 value now. This can happen if we have a .word which is not
15546 resolved when it appears but is later defined. */
15547 if (fixP->fx_done)
15548 md_number_to_chars (buf, *valP, fixP->fx_size);
15549 break;
15550
15551 case BFD_RELOC_MIPS_21_PCREL_S2:
15552 fix_validate_branch (fixP, *valP);
15553 if (!fixP->fx_done)
15554 break;
15555
15556 if (*valP + 0x400000 <= 0x7fffff)
15557 {
15558 insn = read_insn (buf);
15559 insn |= (*valP >> 2) & 0x1fffff;
15560 write_insn (buf, insn);
15561 }
15562 else
15563 as_bad_where (fixP->fx_file, fixP->fx_line,
15564 _("branch out of range"));
15565 break;
15566
15567 case BFD_RELOC_MIPS_26_PCREL_S2:
15568 fix_validate_branch (fixP, *valP);
15569 if (!fixP->fx_done)
15570 break;
15571
15572 if (*valP + 0x8000000 <= 0xfffffff)
15573 {
15574 insn = read_insn (buf);
15575 insn |= (*valP >> 2) & 0x3ffffff;
15576 write_insn (buf, insn);
15577 }
15578 else
15579 as_bad_where (fixP->fx_file, fixP->fx_line,
15580 _("branch out of range"));
15581 break;
15582
15583 case BFD_RELOC_MIPS_18_PCREL_S3:
15584 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
15585 as_bad_where (fixP->fx_file, fixP->fx_line,
15586 _("PC-relative access using misaligned symbol (%lx)"),
15587 (long) S_GET_VALUE (fixP->fx_addsy));
15588 if ((fixP->fx_offset & 0x7) != 0)
15589 as_bad_where (fixP->fx_file, fixP->fx_line,
15590 _("PC-relative access using misaligned offset (%lx)"),
15591 (long) fixP->fx_offset);
15592 if (!fixP->fx_done)
15593 break;
15594
15595 if (*valP + 0x100000 <= 0x1fffff)
15596 {
15597 insn = read_insn (buf);
15598 insn |= (*valP >> 3) & 0x3ffff;
15599 write_insn (buf, insn);
15600 }
15601 else
15602 as_bad_where (fixP->fx_file, fixP->fx_line,
15603 _("PC-relative access out of range"));
15604 break;
15605
15606 case BFD_RELOC_MIPS_19_PCREL_S2:
15607 if ((*valP & 0x3) != 0)
15608 as_bad_where (fixP->fx_file, fixP->fx_line,
15609 _("PC-relative access to misaligned address (%lx)"),
15610 (long) *valP);
15611 if (!fixP->fx_done)
15612 break;
15613
15614 if (*valP + 0x100000 <= 0x1fffff)
15615 {
15616 insn = read_insn (buf);
15617 insn |= (*valP >> 2) & 0x7ffff;
15618 write_insn (buf, insn);
15619 }
15620 else
15621 as_bad_where (fixP->fx_file, fixP->fx_line,
15622 _("PC-relative access out of range"));
15623 break;
15624
15625 case BFD_RELOC_16_PCREL_S2:
15626 fix_validate_branch (fixP, *valP);
15627
15628 /* We need to save the bits in the instruction since fixup_segment()
15629 might be deleting the relocation entry (i.e., a branch within
15630 the current segment). */
15631 if (! fixP->fx_done)
15632 break;
15633
15634 /* Update old instruction data. */
15635 insn = read_insn (buf);
15636
15637 if (*valP + 0x20000 <= 0x3ffff)
15638 {
15639 insn |= (*valP >> 2) & 0xffff;
15640 write_insn (buf, insn);
15641 }
15642 else if (fixP->fx_tcbit2
15643 && fixP->fx_done
15644 && fixP->fx_frag->fr_address >= text_section->vma
15645 && (fixP->fx_frag->fr_address
15646 < text_section->vma + bfd_get_section_size (text_section))
15647 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15648 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15649 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15650 {
15651 /* The branch offset is too large. If this is an
15652 unconditional branch, and we are not generating PIC code,
15653 we can convert it to an absolute jump instruction. */
15654 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15655 insn = 0x0c000000; /* jal */
15656 else
15657 insn = 0x08000000; /* j */
15658 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15659 fixP->fx_done = 0;
15660 fixP->fx_addsy = section_symbol (text_section);
15661 *valP += md_pcrel_from (fixP);
15662 write_insn (buf, insn);
15663 }
15664 else
15665 {
15666 /* If we got here, we have branch-relaxation disabled,
15667 and there's nothing we can do to fix this instruction
15668 without turning it into a longer sequence. */
15669 as_bad_where (fixP->fx_file, fixP->fx_line,
15670 _("branch out of range"));
15671 }
15672 break;
15673
15674 case BFD_RELOC_MIPS16_16_PCREL_S1:
15675 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15676 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15677 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15678 gas_assert (!fixP->fx_done);
15679 if (fix_bad_cross_mode_branch_p (fixP))
15680 as_bad_where (fixP->fx_file, fixP->fx_line,
15681 _("branch to a symbol in another ISA mode"));
15682 else if (fixP->fx_addsy
15683 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
15684 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
15685 && (fixP->fx_offset & 0x1) != 0)
15686 as_bad_where (fixP->fx_file, fixP->fx_line,
15687 _("branch to misaligned address (0x%lx)"),
15688 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15689 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
15690 as_bad_where (fixP->fx_file, fixP->fx_line,
15691 _("cannot encode misaligned addend "
15692 "in the relocatable field (0x%lx)"),
15693 (long) fixP->fx_offset);
15694 break;
15695
15696 case BFD_RELOC_VTABLE_INHERIT:
15697 fixP->fx_done = 0;
15698 if (fixP->fx_addsy
15699 && !S_IS_DEFINED (fixP->fx_addsy)
15700 && !S_IS_WEAK (fixP->fx_addsy))
15701 S_SET_WEAK (fixP->fx_addsy);
15702 break;
15703
15704 case BFD_RELOC_NONE:
15705 case BFD_RELOC_VTABLE_ENTRY:
15706 fixP->fx_done = 0;
15707 break;
15708
15709 default:
15710 abort ();
15711 }
15712
15713 /* Remember value for tc_gen_reloc. */
15714 fixP->fx_addnumber = *valP;
15715 }
15716
15717 static symbolS *
15718 get_symbol (void)
15719 {
15720 int c;
15721 char *name;
15722 symbolS *p;
15723
15724 c = get_symbol_name (&name);
15725 p = (symbolS *) symbol_find_or_make (name);
15726 (void) restore_line_pointer (c);
15727 return p;
15728 }
15729
15730 /* Align the current frag to a given power of two. If a particular
15731 fill byte should be used, FILL points to an integer that contains
15732 that byte, otherwise FILL is null.
15733
15734 This function used to have the comment:
15735
15736 The MIPS assembler also automatically adjusts any preceding label.
15737
15738 The implementation therefore applied the adjustment to a maximum of
15739 one label. However, other label adjustments are applied to batches
15740 of labels, and adjusting just one caused problems when new labels
15741 were added for the sake of debugging or unwind information.
15742 We therefore adjust all preceding labels (given as LABELS) instead. */
15743
15744 static void
15745 mips_align (int to, int *fill, struct insn_label_list *labels)
15746 {
15747 mips_emit_delays ();
15748 mips_record_compressed_mode ();
15749 if (fill == NULL && subseg_text_p (now_seg))
15750 frag_align_code (to, 0);
15751 else
15752 frag_align (to, fill ? *fill : 0, 0);
15753 record_alignment (now_seg, to);
15754 mips_move_labels (labels, FALSE);
15755 }
15756
15757 /* Align to a given power of two. .align 0 turns off the automatic
15758 alignment used by the data creating pseudo-ops. */
15759
15760 static void
15761 s_align (int x ATTRIBUTE_UNUSED)
15762 {
15763 int temp, fill_value, *fill_ptr;
15764 long max_alignment = 28;
15765
15766 /* o Note that the assembler pulls down any immediately preceding label
15767 to the aligned address.
15768 o It's not documented but auto alignment is reinstated by
15769 a .align pseudo instruction.
15770 o Note also that after auto alignment is turned off the mips assembler
15771 issues an error on attempt to assemble an improperly aligned data item.
15772 We don't. */
15773
15774 temp = get_absolute_expression ();
15775 if (temp > max_alignment)
15776 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
15777 else if (temp < 0)
15778 {
15779 as_warn (_("alignment negative, 0 assumed"));
15780 temp = 0;
15781 }
15782 if (*input_line_pointer == ',')
15783 {
15784 ++input_line_pointer;
15785 fill_value = get_absolute_expression ();
15786 fill_ptr = &fill_value;
15787 }
15788 else
15789 fill_ptr = 0;
15790 if (temp)
15791 {
15792 segment_info_type *si = seg_info (now_seg);
15793 struct insn_label_list *l = si->label_list;
15794 /* Auto alignment should be switched on by next section change. */
15795 auto_align = 1;
15796 mips_align (temp, fill_ptr, l);
15797 }
15798 else
15799 {
15800 auto_align = 0;
15801 }
15802
15803 demand_empty_rest_of_line ();
15804 }
15805
15806 static void
15807 s_change_sec (int sec)
15808 {
15809 segT seg;
15810
15811 /* The ELF backend needs to know that we are changing sections, so
15812 that .previous works correctly. We could do something like check
15813 for an obj_section_change_hook macro, but that might be confusing
15814 as it would not be appropriate to use it in the section changing
15815 functions in read.c, since obj-elf.c intercepts those. FIXME:
15816 This should be cleaner, somehow. */
15817 obj_elf_section_change_hook ();
15818
15819 mips_emit_delays ();
15820
15821 switch (sec)
15822 {
15823 case 't':
15824 s_text (0);
15825 break;
15826 case 'd':
15827 s_data (0);
15828 break;
15829 case 'b':
15830 subseg_set (bss_section, (subsegT) get_absolute_expression ());
15831 demand_empty_rest_of_line ();
15832 break;
15833
15834 case 'r':
15835 seg = subseg_new (RDATA_SECTION_NAME,
15836 (subsegT) get_absolute_expression ());
15837 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15838 | SEC_READONLY | SEC_RELOC
15839 | SEC_DATA));
15840 if (strncmp (TARGET_OS, "elf", 3) != 0)
15841 record_alignment (seg, 4);
15842 demand_empty_rest_of_line ();
15843 break;
15844
15845 case 's':
15846 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
15847 bfd_set_section_flags (stdoutput, seg,
15848 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
15849 if (strncmp (TARGET_OS, "elf", 3) != 0)
15850 record_alignment (seg, 4);
15851 demand_empty_rest_of_line ();
15852 break;
15853
15854 case 'B':
15855 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
15856 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
15857 if (strncmp (TARGET_OS, "elf", 3) != 0)
15858 record_alignment (seg, 4);
15859 demand_empty_rest_of_line ();
15860 break;
15861 }
15862
15863 auto_align = 1;
15864 }
15865
15866 void
15867 s_change_section (int ignore ATTRIBUTE_UNUSED)
15868 {
15869 char *saved_ilp;
15870 char *section_name;
15871 char c, endc;
15872 char next_c = 0;
15873 int section_type;
15874 int section_flag;
15875 int section_entry_size;
15876 int section_alignment;
15877
15878 saved_ilp = input_line_pointer;
15879 endc = get_symbol_name (&section_name);
15880 c = (endc == '"' ? input_line_pointer[1] : endc);
15881 if (c)
15882 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
15883
15884 /* Do we have .section Name<,"flags">? */
15885 if (c != ',' || (c == ',' && next_c == '"'))
15886 {
15887 /* Just after name is now '\0'. */
15888 (void) restore_line_pointer (endc);
15889 input_line_pointer = saved_ilp;
15890 obj_elf_section (ignore);
15891 return;
15892 }
15893
15894 section_name = xstrdup (section_name);
15895 c = restore_line_pointer (endc);
15896
15897 input_line_pointer++;
15898
15899 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
15900 if (c == ',')
15901 section_type = get_absolute_expression ();
15902 else
15903 section_type = 0;
15904
15905 if (*input_line_pointer++ == ',')
15906 section_flag = get_absolute_expression ();
15907 else
15908 section_flag = 0;
15909
15910 if (*input_line_pointer++ == ',')
15911 section_entry_size = get_absolute_expression ();
15912 else
15913 section_entry_size = 0;
15914
15915 if (*input_line_pointer++ == ',')
15916 section_alignment = get_absolute_expression ();
15917 else
15918 section_alignment = 0;
15919
15920 /* FIXME: really ignore? */
15921 (void) section_alignment;
15922
15923 /* When using the generic form of .section (as implemented by obj-elf.c),
15924 there's no way to set the section type to SHT_MIPS_DWARF. Users have
15925 traditionally had to fall back on the more common @progbits instead.
15926
15927 There's nothing really harmful in this, since bfd will correct
15928 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
15929 means that, for backwards compatibility, the special_section entries
15930 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
15931
15932 Even so, we shouldn't force users of the MIPS .section syntax to
15933 incorrectly label the sections as SHT_PROGBITS. The best compromise
15934 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
15935 generic type-checking code. */
15936 if (section_type == SHT_MIPS_DWARF)
15937 section_type = SHT_PROGBITS;
15938
15939 obj_elf_change_section (section_name, section_type, 0, section_flag,
15940 section_entry_size, 0, 0, 0);
15941
15942 if (now_seg->name != section_name)
15943 free (section_name);
15944 }
15945
15946 void
15947 mips_enable_auto_align (void)
15948 {
15949 auto_align = 1;
15950 }
15951
15952 static void
15953 s_cons (int log_size)
15954 {
15955 segment_info_type *si = seg_info (now_seg);
15956 struct insn_label_list *l = si->label_list;
15957
15958 mips_emit_delays ();
15959 if (log_size > 0 && auto_align)
15960 mips_align (log_size, 0, l);
15961 cons (1 << log_size);
15962 mips_clear_insn_labels ();
15963 }
15964
15965 static void
15966 s_float_cons (int type)
15967 {
15968 segment_info_type *si = seg_info (now_seg);
15969 struct insn_label_list *l = si->label_list;
15970
15971 mips_emit_delays ();
15972
15973 if (auto_align)
15974 {
15975 if (type == 'd')
15976 mips_align (3, 0, l);
15977 else
15978 mips_align (2, 0, l);
15979 }
15980
15981 float_cons (type);
15982 mips_clear_insn_labels ();
15983 }
15984
15985 /* Handle .globl. We need to override it because on Irix 5 you are
15986 permitted to say
15987 .globl foo .text
15988 where foo is an undefined symbol, to mean that foo should be
15989 considered to be the address of a function. */
15990
15991 static void
15992 s_mips_globl (int x ATTRIBUTE_UNUSED)
15993 {
15994 char *name;
15995 int c;
15996 symbolS *symbolP;
15997 flagword flag;
15998
15999 do
16000 {
16001 c = get_symbol_name (&name);
16002 symbolP = symbol_find_or_make (name);
16003 S_SET_EXTERNAL (symbolP);
16004
16005 *input_line_pointer = c;
16006 SKIP_WHITESPACE_AFTER_NAME ();
16007
16008 /* On Irix 5, every global symbol that is not explicitly labelled as
16009 being a function is apparently labelled as being an object. */
16010 flag = BSF_OBJECT;
16011
16012 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16013 && (*input_line_pointer != ','))
16014 {
16015 char *secname;
16016 asection *sec;
16017
16018 c = get_symbol_name (&secname);
16019 sec = bfd_get_section_by_name (stdoutput, secname);
16020 if (sec == NULL)
16021 as_bad (_("%s: no such section"), secname);
16022 (void) restore_line_pointer (c);
16023
16024 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16025 flag = BSF_FUNCTION;
16026 }
16027
16028 symbol_get_bfdsym (symbolP)->flags |= flag;
16029
16030 c = *input_line_pointer;
16031 if (c == ',')
16032 {
16033 input_line_pointer++;
16034 SKIP_WHITESPACE ();
16035 if (is_end_of_line[(unsigned char) *input_line_pointer])
16036 c = '\n';
16037 }
16038 }
16039 while (c == ',');
16040
16041 demand_empty_rest_of_line ();
16042 }
16043
16044 static void
16045 s_option (int x ATTRIBUTE_UNUSED)
16046 {
16047 char *opt;
16048 char c;
16049
16050 c = get_symbol_name (&opt);
16051
16052 if (*opt == 'O')
16053 {
16054 /* FIXME: What does this mean? */
16055 }
16056 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
16057 {
16058 int i;
16059
16060 i = atoi (opt + 3);
16061 if (i != 0 && i != 2)
16062 as_bad (_(".option pic%d not supported"), i);
16063 else if (mips_pic == VXWORKS_PIC)
16064 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16065 else if (i == 0)
16066 mips_pic = NO_PIC;
16067 else if (i == 2)
16068 {
16069 mips_pic = SVR4_PIC;
16070 mips_abicalls = TRUE;
16071 }
16072
16073 if (mips_pic == SVR4_PIC)
16074 {
16075 if (g_switch_seen && g_switch_value != 0)
16076 as_warn (_("-G may not be used with SVR4 PIC code"));
16077 g_switch_value = 0;
16078 bfd_set_gp_size (stdoutput, 0);
16079 }
16080 }
16081 else
16082 as_warn (_("unrecognized option \"%s\""), opt);
16083
16084 (void) restore_line_pointer (c);
16085 demand_empty_rest_of_line ();
16086 }
16087
16088 /* This structure is used to hold a stack of .set values. */
16089
16090 struct mips_option_stack
16091 {
16092 struct mips_option_stack *next;
16093 struct mips_set_options options;
16094 };
16095
16096 static struct mips_option_stack *mips_opts_stack;
16097
16098 /* Return status for .set/.module option handling. */
16099
16100 enum code_option_type
16101 {
16102 /* Unrecognized option. */
16103 OPTION_TYPE_BAD = -1,
16104
16105 /* Ordinary option. */
16106 OPTION_TYPE_NORMAL,
16107
16108 /* ISA changing option. */
16109 OPTION_TYPE_ISA
16110 };
16111
16112 /* Handle common .set/.module options. Return status indicating option
16113 type. */
16114
16115 static enum code_option_type
16116 parse_code_option (char * name)
16117 {
16118 bfd_boolean isa_set = FALSE;
16119 const struct mips_ase *ase;
16120
16121 if (strncmp (name, "at=", 3) == 0)
16122 {
16123 char *s = name + 3;
16124
16125 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16126 as_bad (_("unrecognized register name `%s'"), s);
16127 }
16128 else if (strcmp (name, "at") == 0)
16129 mips_opts.at = ATREG;
16130 else if (strcmp (name, "noat") == 0)
16131 mips_opts.at = ZERO;
16132 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16133 mips_opts.nomove = 0;
16134 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16135 mips_opts.nomove = 1;
16136 else if (strcmp (name, "bopt") == 0)
16137 mips_opts.nobopt = 0;
16138 else if (strcmp (name, "nobopt") == 0)
16139 mips_opts.nobopt = 1;
16140 else if (strcmp (name, "gp=32") == 0)
16141 mips_opts.gp = 32;
16142 else if (strcmp (name, "gp=64") == 0)
16143 mips_opts.gp = 64;
16144 else if (strcmp (name, "fp=32") == 0)
16145 mips_opts.fp = 32;
16146 else if (strcmp (name, "fp=xx") == 0)
16147 mips_opts.fp = 0;
16148 else if (strcmp (name, "fp=64") == 0)
16149 mips_opts.fp = 64;
16150 else if (strcmp (name, "softfloat") == 0)
16151 mips_opts.soft_float = 1;
16152 else if (strcmp (name, "hardfloat") == 0)
16153 mips_opts.soft_float = 0;
16154 else if (strcmp (name, "singlefloat") == 0)
16155 mips_opts.single_float = 1;
16156 else if (strcmp (name, "doublefloat") == 0)
16157 mips_opts.single_float = 0;
16158 else if (strcmp (name, "nooddspreg") == 0)
16159 mips_opts.oddspreg = 0;
16160 else if (strcmp (name, "oddspreg") == 0)
16161 mips_opts.oddspreg = 1;
16162 else if (strcmp (name, "mips16") == 0
16163 || strcmp (name, "MIPS-16") == 0)
16164 mips_opts.mips16 = 1;
16165 else if (strcmp (name, "nomips16") == 0
16166 || strcmp (name, "noMIPS-16") == 0)
16167 mips_opts.mips16 = 0;
16168 else if (strcmp (name, "micromips") == 0)
16169 mips_opts.micromips = 1;
16170 else if (strcmp (name, "nomicromips") == 0)
16171 mips_opts.micromips = 0;
16172 else if (name[0] == 'n'
16173 && name[1] == 'o'
16174 && (ase = mips_lookup_ase (name + 2)))
16175 mips_set_ase (ase, &mips_opts, FALSE);
16176 else if ((ase = mips_lookup_ase (name)))
16177 mips_set_ase (ase, &mips_opts, TRUE);
16178 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16179 {
16180 /* Permit the user to change the ISA and architecture on the fly.
16181 Needless to say, misuse can cause serious problems. */
16182 if (strncmp (name, "arch=", 5) == 0)
16183 {
16184 const struct mips_cpu_info *p;
16185
16186 p = mips_parse_cpu ("internal use", name + 5);
16187 if (!p)
16188 as_bad (_("unknown architecture %s"), name + 5);
16189 else
16190 {
16191 mips_opts.arch = p->cpu;
16192 mips_opts.isa = p->isa;
16193 isa_set = TRUE;
16194 }
16195 }
16196 else if (strncmp (name, "mips", 4) == 0)
16197 {
16198 const struct mips_cpu_info *p;
16199
16200 p = mips_parse_cpu ("internal use", name);
16201 if (!p)
16202 as_bad (_("unknown ISA level %s"), name + 4);
16203 else
16204 {
16205 mips_opts.arch = p->cpu;
16206 mips_opts.isa = p->isa;
16207 isa_set = TRUE;
16208 }
16209 }
16210 else
16211 as_bad (_("unknown ISA or architecture %s"), name);
16212 }
16213 else if (strcmp (name, "autoextend") == 0)
16214 mips_opts.noautoextend = 0;
16215 else if (strcmp (name, "noautoextend") == 0)
16216 mips_opts.noautoextend = 1;
16217 else if (strcmp (name, "insn32") == 0)
16218 mips_opts.insn32 = TRUE;
16219 else if (strcmp (name, "noinsn32") == 0)
16220 mips_opts.insn32 = FALSE;
16221 else if (strcmp (name, "sym32") == 0)
16222 mips_opts.sym32 = TRUE;
16223 else if (strcmp (name, "nosym32") == 0)
16224 mips_opts.sym32 = FALSE;
16225 else
16226 return OPTION_TYPE_BAD;
16227
16228 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
16229 }
16230
16231 /* Handle the .set pseudo-op. */
16232
16233 static void
16234 s_mipsset (int x ATTRIBUTE_UNUSED)
16235 {
16236 enum code_option_type type = OPTION_TYPE_NORMAL;
16237 char *name = input_line_pointer, ch;
16238
16239 file_mips_check_options ();
16240
16241 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16242 ++input_line_pointer;
16243 ch = *input_line_pointer;
16244 *input_line_pointer = '\0';
16245
16246 if (strchr (name, ','))
16247 {
16248 /* Generic ".set" directive; use the generic handler. */
16249 *input_line_pointer = ch;
16250 input_line_pointer = name;
16251 s_set (0);
16252 return;
16253 }
16254
16255 if (strcmp (name, "reorder") == 0)
16256 {
16257 if (mips_opts.noreorder)
16258 end_noreorder ();
16259 }
16260 else if (strcmp (name, "noreorder") == 0)
16261 {
16262 if (!mips_opts.noreorder)
16263 start_noreorder ();
16264 }
16265 else if (strcmp (name, "macro") == 0)
16266 mips_opts.warn_about_macros = 0;
16267 else if (strcmp (name, "nomacro") == 0)
16268 {
16269 if (mips_opts.noreorder == 0)
16270 as_bad (_("`noreorder' must be set before `nomacro'"));
16271 mips_opts.warn_about_macros = 1;
16272 }
16273 else if (strcmp (name, "gp=default") == 0)
16274 mips_opts.gp = file_mips_opts.gp;
16275 else if (strcmp (name, "fp=default") == 0)
16276 mips_opts.fp = file_mips_opts.fp;
16277 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16278 {
16279 mips_opts.isa = file_mips_opts.isa;
16280 mips_opts.arch = file_mips_opts.arch;
16281 mips_opts.gp = file_mips_opts.gp;
16282 mips_opts.fp = file_mips_opts.fp;
16283 }
16284 else if (strcmp (name, "push") == 0)
16285 {
16286 struct mips_option_stack *s;
16287
16288 s = XNEW (struct mips_option_stack);
16289 s->next = mips_opts_stack;
16290 s->options = mips_opts;
16291 mips_opts_stack = s;
16292 }
16293 else if (strcmp (name, "pop") == 0)
16294 {
16295 struct mips_option_stack *s;
16296
16297 s = mips_opts_stack;
16298 if (s == NULL)
16299 as_bad (_(".set pop with no .set push"));
16300 else
16301 {
16302 /* If we're changing the reorder mode we need to handle
16303 delay slots correctly. */
16304 if (s->options.noreorder && ! mips_opts.noreorder)
16305 start_noreorder ();
16306 else if (! s->options.noreorder && mips_opts.noreorder)
16307 end_noreorder ();
16308
16309 mips_opts = s->options;
16310 mips_opts_stack = s->next;
16311 free (s);
16312 }
16313 }
16314 else
16315 {
16316 type = parse_code_option (name);
16317 if (type == OPTION_TYPE_BAD)
16318 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16319 }
16320
16321 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16322 registers based on what is supported by the arch/cpu. */
16323 if (type == OPTION_TYPE_ISA)
16324 {
16325 switch (mips_opts.isa)
16326 {
16327 case 0:
16328 break;
16329 case ISA_MIPS1:
16330 /* MIPS I cannot support FPXX. */
16331 mips_opts.fp = 32;
16332 /* fall-through. */
16333 case ISA_MIPS2:
16334 case ISA_MIPS32:
16335 case ISA_MIPS32R2:
16336 case ISA_MIPS32R3:
16337 case ISA_MIPS32R5:
16338 mips_opts.gp = 32;
16339 if (mips_opts.fp != 0)
16340 mips_opts.fp = 32;
16341 break;
16342 case ISA_MIPS32R6:
16343 mips_opts.gp = 32;
16344 mips_opts.fp = 64;
16345 break;
16346 case ISA_MIPS3:
16347 case ISA_MIPS4:
16348 case ISA_MIPS5:
16349 case ISA_MIPS64:
16350 case ISA_MIPS64R2:
16351 case ISA_MIPS64R3:
16352 case ISA_MIPS64R5:
16353 case ISA_MIPS64R6:
16354 mips_opts.gp = 64;
16355 if (mips_opts.fp != 0)
16356 {
16357 if (mips_opts.arch == CPU_R5900)
16358 mips_opts.fp = 32;
16359 else
16360 mips_opts.fp = 64;
16361 }
16362 break;
16363 default:
16364 as_bad (_("unknown ISA level %s"), name + 4);
16365 break;
16366 }
16367 }
16368
16369 mips_check_options (&mips_opts, FALSE);
16370
16371 mips_check_isa_supports_ases ();
16372 *input_line_pointer = ch;
16373 demand_empty_rest_of_line ();
16374 }
16375
16376 /* Handle the .module pseudo-op. */
16377
16378 static void
16379 s_module (int ignore ATTRIBUTE_UNUSED)
16380 {
16381 char *name = input_line_pointer, ch;
16382
16383 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16384 ++input_line_pointer;
16385 ch = *input_line_pointer;
16386 *input_line_pointer = '\0';
16387
16388 if (!file_mips_opts_checked)
16389 {
16390 if (parse_code_option (name) == OPTION_TYPE_BAD)
16391 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16392
16393 /* Update module level settings from mips_opts. */
16394 file_mips_opts = mips_opts;
16395 }
16396 else
16397 as_bad (_(".module is not permitted after generating code"));
16398
16399 *input_line_pointer = ch;
16400 demand_empty_rest_of_line ();
16401 }
16402
16403 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
16404 .option pic2. It means to generate SVR4 PIC calls. */
16405
16406 static void
16407 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16408 {
16409 mips_pic = SVR4_PIC;
16410 mips_abicalls = TRUE;
16411
16412 if (g_switch_seen && g_switch_value != 0)
16413 as_warn (_("-G may not be used with SVR4 PIC code"));
16414 g_switch_value = 0;
16415
16416 bfd_set_gp_size (stdoutput, 0);
16417 demand_empty_rest_of_line ();
16418 }
16419
16420 /* Handle the .cpload pseudo-op. This is used when generating SVR4
16421 PIC code. It sets the $gp register for the function based on the
16422 function address, which is in the register named in the argument.
16423 This uses a relocation against _gp_disp, which is handled specially
16424 by the linker. The result is:
16425 lui $gp,%hi(_gp_disp)
16426 addiu $gp,$gp,%lo(_gp_disp)
16427 addu $gp,$gp,.cpload argument
16428 The .cpload argument is normally $25 == $t9.
16429
16430 The -mno-shared option changes this to:
16431 lui $gp,%hi(__gnu_local_gp)
16432 addiu $gp,$gp,%lo(__gnu_local_gp)
16433 and the argument is ignored. This saves an instruction, but the
16434 resulting code is not position independent; it uses an absolute
16435 address for __gnu_local_gp. Thus code assembled with -mno-shared
16436 can go into an ordinary executable, but not into a shared library. */
16437
16438 static void
16439 s_cpload (int ignore ATTRIBUTE_UNUSED)
16440 {
16441 expressionS ex;
16442 int reg;
16443 int in_shared;
16444
16445 file_mips_check_options ();
16446
16447 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16448 .cpload is ignored. */
16449 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16450 {
16451 s_ignore (0);
16452 return;
16453 }
16454
16455 if (mips_opts.mips16)
16456 {
16457 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16458 ignore_rest_of_line ();
16459 return;
16460 }
16461
16462 /* .cpload should be in a .set noreorder section. */
16463 if (mips_opts.noreorder == 0)
16464 as_warn (_(".cpload not in noreorder section"));
16465
16466 reg = tc_get_register (0);
16467
16468 /* If we need to produce a 64-bit address, we are better off using
16469 the default instruction sequence. */
16470 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16471
16472 ex.X_op = O_symbol;
16473 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16474 "__gnu_local_gp");
16475 ex.X_op_symbol = NULL;
16476 ex.X_add_number = 0;
16477
16478 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16479 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16480
16481 mips_mark_labels ();
16482 mips_assembling_insn = TRUE;
16483
16484 macro_start ();
16485 macro_build_lui (&ex, mips_gp_register);
16486 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16487 mips_gp_register, BFD_RELOC_LO16);
16488 if (in_shared)
16489 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16490 mips_gp_register, reg);
16491 macro_end ();
16492
16493 mips_assembling_insn = FALSE;
16494 demand_empty_rest_of_line ();
16495 }
16496
16497 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16498 .cpsetup $reg1, offset|$reg2, label
16499
16500 If offset is given, this results in:
16501 sd $gp, offset($sp)
16502 lui $gp, %hi(%neg(%gp_rel(label)))
16503 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16504 daddu $gp, $gp, $reg1
16505
16506 If $reg2 is given, this results in:
16507 or $reg2, $gp, $0
16508 lui $gp, %hi(%neg(%gp_rel(label)))
16509 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16510 daddu $gp, $gp, $reg1
16511 $reg1 is normally $25 == $t9.
16512
16513 The -mno-shared option replaces the last three instructions with
16514 lui $gp,%hi(_gp)
16515 addiu $gp,$gp,%lo(_gp) */
16516
16517 static void
16518 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16519 {
16520 expressionS ex_off;
16521 expressionS ex_sym;
16522 int reg1;
16523
16524 file_mips_check_options ();
16525
16526 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16527 We also need NewABI support. */
16528 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16529 {
16530 s_ignore (0);
16531 return;
16532 }
16533
16534 if (mips_opts.mips16)
16535 {
16536 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16537 ignore_rest_of_line ();
16538 return;
16539 }
16540
16541 reg1 = tc_get_register (0);
16542 SKIP_WHITESPACE ();
16543 if (*input_line_pointer != ',')
16544 {
16545 as_bad (_("missing argument separator ',' for .cpsetup"));
16546 return;
16547 }
16548 else
16549 ++input_line_pointer;
16550 SKIP_WHITESPACE ();
16551 if (*input_line_pointer == '$')
16552 {
16553 mips_cpreturn_register = tc_get_register (0);
16554 mips_cpreturn_offset = -1;
16555 }
16556 else
16557 {
16558 mips_cpreturn_offset = get_absolute_expression ();
16559 mips_cpreturn_register = -1;
16560 }
16561 SKIP_WHITESPACE ();
16562 if (*input_line_pointer != ',')
16563 {
16564 as_bad (_("missing argument separator ',' for .cpsetup"));
16565 return;
16566 }
16567 else
16568 ++input_line_pointer;
16569 SKIP_WHITESPACE ();
16570 expression (&ex_sym);
16571
16572 mips_mark_labels ();
16573 mips_assembling_insn = TRUE;
16574
16575 macro_start ();
16576 if (mips_cpreturn_register == -1)
16577 {
16578 ex_off.X_op = O_constant;
16579 ex_off.X_add_symbol = NULL;
16580 ex_off.X_op_symbol = NULL;
16581 ex_off.X_add_number = mips_cpreturn_offset;
16582
16583 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16584 BFD_RELOC_LO16, SP);
16585 }
16586 else
16587 move_register (mips_cpreturn_register, mips_gp_register);
16588
16589 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16590 {
16591 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16592 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16593 BFD_RELOC_HI16_S);
16594
16595 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16596 mips_gp_register, -1, BFD_RELOC_GPREL16,
16597 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16598
16599 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16600 mips_gp_register, reg1);
16601 }
16602 else
16603 {
16604 expressionS ex;
16605
16606 ex.X_op = O_symbol;
16607 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16608 ex.X_op_symbol = NULL;
16609 ex.X_add_number = 0;
16610
16611 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16612 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16613
16614 macro_build_lui (&ex, mips_gp_register);
16615 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16616 mips_gp_register, BFD_RELOC_LO16);
16617 }
16618
16619 macro_end ();
16620
16621 mips_assembling_insn = FALSE;
16622 demand_empty_rest_of_line ();
16623 }
16624
16625 static void
16626 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16627 {
16628 file_mips_check_options ();
16629
16630 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16631 .cplocal is ignored. */
16632 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16633 {
16634 s_ignore (0);
16635 return;
16636 }
16637
16638 if (mips_opts.mips16)
16639 {
16640 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16641 ignore_rest_of_line ();
16642 return;
16643 }
16644
16645 mips_gp_register = tc_get_register (0);
16646 demand_empty_rest_of_line ();
16647 }
16648
16649 /* Handle the .cprestore pseudo-op. This stores $gp into a given
16650 offset from $sp. The offset is remembered, and after making a PIC
16651 call $gp is restored from that location. */
16652
16653 static void
16654 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16655 {
16656 expressionS ex;
16657
16658 file_mips_check_options ();
16659
16660 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16661 .cprestore is ignored. */
16662 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16663 {
16664 s_ignore (0);
16665 return;
16666 }
16667
16668 if (mips_opts.mips16)
16669 {
16670 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16671 ignore_rest_of_line ();
16672 return;
16673 }
16674
16675 mips_cprestore_offset = get_absolute_expression ();
16676 mips_cprestore_valid = 1;
16677
16678 ex.X_op = O_constant;
16679 ex.X_add_symbol = NULL;
16680 ex.X_op_symbol = NULL;
16681 ex.X_add_number = mips_cprestore_offset;
16682
16683 mips_mark_labels ();
16684 mips_assembling_insn = TRUE;
16685
16686 macro_start ();
16687 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16688 SP, HAVE_64BIT_ADDRESSES);
16689 macro_end ();
16690
16691 mips_assembling_insn = FALSE;
16692 demand_empty_rest_of_line ();
16693 }
16694
16695 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16696 was given in the preceding .cpsetup, it results in:
16697 ld $gp, offset($sp)
16698
16699 If a register $reg2 was given there, it results in:
16700 or $gp, $reg2, $0 */
16701
16702 static void
16703 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16704 {
16705 expressionS ex;
16706
16707 file_mips_check_options ();
16708
16709 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16710 We also need NewABI support. */
16711 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16712 {
16713 s_ignore (0);
16714 return;
16715 }
16716
16717 if (mips_opts.mips16)
16718 {
16719 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16720 ignore_rest_of_line ();
16721 return;
16722 }
16723
16724 mips_mark_labels ();
16725 mips_assembling_insn = TRUE;
16726
16727 macro_start ();
16728 if (mips_cpreturn_register == -1)
16729 {
16730 ex.X_op = O_constant;
16731 ex.X_add_symbol = NULL;
16732 ex.X_op_symbol = NULL;
16733 ex.X_add_number = mips_cpreturn_offset;
16734
16735 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16736 }
16737 else
16738 move_register (mips_gp_register, mips_cpreturn_register);
16739
16740 macro_end ();
16741
16742 mips_assembling_insn = FALSE;
16743 demand_empty_rest_of_line ();
16744 }
16745
16746 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16747 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16748 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16749 debug information or MIPS16 TLS. */
16750
16751 static void
16752 s_tls_rel_directive (const size_t bytes, const char *dirstr,
16753 bfd_reloc_code_real_type rtype)
16754 {
16755 expressionS ex;
16756 char *p;
16757
16758 expression (&ex);
16759
16760 if (ex.X_op != O_symbol)
16761 {
16762 as_bad (_("unsupported use of %s"), dirstr);
16763 ignore_rest_of_line ();
16764 }
16765
16766 p = frag_more (bytes);
16767 md_number_to_chars (p, 0, bytes);
16768 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
16769 demand_empty_rest_of_line ();
16770 mips_clear_insn_labels ();
16771 }
16772
16773 /* Handle .dtprelword. */
16774
16775 static void
16776 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16777 {
16778 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
16779 }
16780
16781 /* Handle .dtpreldword. */
16782
16783 static void
16784 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16785 {
16786 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16787 }
16788
16789 /* Handle .tprelword. */
16790
16791 static void
16792 s_tprelword (int ignore ATTRIBUTE_UNUSED)
16793 {
16794 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16795 }
16796
16797 /* Handle .tpreldword. */
16798
16799 static void
16800 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16801 {
16802 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
16803 }
16804
16805 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16806 code. It sets the offset to use in gp_rel relocations. */
16807
16808 static void
16809 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
16810 {
16811 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16812 We also need NewABI support. */
16813 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16814 {
16815 s_ignore (0);
16816 return;
16817 }
16818
16819 mips_gprel_offset = get_absolute_expression ();
16820
16821 demand_empty_rest_of_line ();
16822 }
16823
16824 /* Handle the .gpword pseudo-op. This is used when generating PIC
16825 code. It generates a 32 bit GP relative reloc. */
16826
16827 static void
16828 s_gpword (int ignore ATTRIBUTE_UNUSED)
16829 {
16830 segment_info_type *si;
16831 struct insn_label_list *l;
16832 expressionS ex;
16833 char *p;
16834
16835 /* When not generating PIC code, this is treated as .word. */
16836 if (mips_pic != SVR4_PIC)
16837 {
16838 s_cons (2);
16839 return;
16840 }
16841
16842 si = seg_info (now_seg);
16843 l = si->label_list;
16844 mips_emit_delays ();
16845 if (auto_align)
16846 mips_align (2, 0, l);
16847
16848 expression (&ex);
16849 mips_clear_insn_labels ();
16850
16851 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16852 {
16853 as_bad (_("unsupported use of .gpword"));
16854 ignore_rest_of_line ();
16855 }
16856
16857 p = frag_more (4);
16858 md_number_to_chars (p, 0, 4);
16859 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16860 BFD_RELOC_GPREL32);
16861
16862 demand_empty_rest_of_line ();
16863 }
16864
16865 static void
16866 s_gpdword (int ignore ATTRIBUTE_UNUSED)
16867 {
16868 segment_info_type *si;
16869 struct insn_label_list *l;
16870 expressionS ex;
16871 char *p;
16872
16873 /* When not generating PIC code, this is treated as .dword. */
16874 if (mips_pic != SVR4_PIC)
16875 {
16876 s_cons (3);
16877 return;
16878 }
16879
16880 si = seg_info (now_seg);
16881 l = si->label_list;
16882 mips_emit_delays ();
16883 if (auto_align)
16884 mips_align (3, 0, l);
16885
16886 expression (&ex);
16887 mips_clear_insn_labels ();
16888
16889 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16890 {
16891 as_bad (_("unsupported use of .gpdword"));
16892 ignore_rest_of_line ();
16893 }
16894
16895 p = frag_more (8);
16896 md_number_to_chars (p, 0, 8);
16897 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16898 BFD_RELOC_GPREL32)->fx_tcbit = 1;
16899
16900 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
16901 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
16902 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
16903
16904 demand_empty_rest_of_line ();
16905 }
16906
16907 /* Handle the .ehword pseudo-op. This is used when generating unwinding
16908 tables. It generates a R_MIPS_EH reloc. */
16909
16910 static void
16911 s_ehword (int ignore ATTRIBUTE_UNUSED)
16912 {
16913 expressionS ex;
16914 char *p;
16915
16916 mips_emit_delays ();
16917
16918 expression (&ex);
16919 mips_clear_insn_labels ();
16920
16921 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16922 {
16923 as_bad (_("unsupported use of .ehword"));
16924 ignore_rest_of_line ();
16925 }
16926
16927 p = frag_more (4);
16928 md_number_to_chars (p, 0, 4);
16929 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16930 BFD_RELOC_32_PCREL);
16931
16932 demand_empty_rest_of_line ();
16933 }
16934
16935 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
16936 tables in SVR4 PIC code. */
16937
16938 static void
16939 s_cpadd (int ignore ATTRIBUTE_UNUSED)
16940 {
16941 int reg;
16942
16943 file_mips_check_options ();
16944
16945 /* This is ignored when not generating SVR4 PIC code. */
16946 if (mips_pic != SVR4_PIC)
16947 {
16948 s_ignore (0);
16949 return;
16950 }
16951
16952 mips_mark_labels ();
16953 mips_assembling_insn = TRUE;
16954
16955 /* Add $gp to the register named as an argument. */
16956 macro_start ();
16957 reg = tc_get_register (0);
16958 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
16959 macro_end ();
16960
16961 mips_assembling_insn = FALSE;
16962 demand_empty_rest_of_line ();
16963 }
16964
16965 /* Handle the .insn pseudo-op. This marks instruction labels in
16966 mips16/micromips mode. This permits the linker to handle them specially,
16967 such as generating jalx instructions when needed. We also make
16968 them odd for the duration of the assembly, in order to generate the
16969 right sort of code. We will make them even in the adjust_symtab
16970 routine, while leaving them marked. This is convenient for the
16971 debugger and the disassembler. The linker knows to make them odd
16972 again. */
16973
16974 static void
16975 s_insn (int ignore ATTRIBUTE_UNUSED)
16976 {
16977 file_mips_check_options ();
16978 file_ase_mips16 |= mips_opts.mips16;
16979 file_ase_micromips |= mips_opts.micromips;
16980
16981 mips_mark_labels ();
16982
16983 demand_empty_rest_of_line ();
16984 }
16985
16986 /* Handle the .nan pseudo-op. */
16987
16988 static void
16989 s_nan (int ignore ATTRIBUTE_UNUSED)
16990 {
16991 static const char str_legacy[] = "legacy";
16992 static const char str_2008[] = "2008";
16993 size_t i;
16994
16995 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
16996
16997 if (i == sizeof (str_2008) - 1
16998 && memcmp (input_line_pointer, str_2008, i) == 0)
16999 mips_nan2008 = 1;
17000 else if (i == sizeof (str_legacy) - 1
17001 && memcmp (input_line_pointer, str_legacy, i) == 0)
17002 {
17003 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17004 mips_nan2008 = 0;
17005 else
17006 as_bad (_("`%s' does not support legacy NaN"),
17007 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17008 }
17009 else
17010 as_bad (_("bad .nan directive"));
17011
17012 input_line_pointer += i;
17013 demand_empty_rest_of_line ();
17014 }
17015
17016 /* Handle a .stab[snd] directive. Ideally these directives would be
17017 implemented in a transparent way, so that removing them would not
17018 have any effect on the generated instructions. However, s_stab
17019 internally changes the section, so in practice we need to decide
17020 now whether the preceding label marks compressed code. We do not
17021 support changing the compression mode of a label after a .stab*
17022 directive, such as in:
17023
17024 foo:
17025 .stabs ...
17026 .set mips16
17027
17028 so the current mode wins. */
17029
17030 static void
17031 s_mips_stab (int type)
17032 {
17033 mips_mark_labels ();
17034 s_stab (type);
17035 }
17036
17037 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
17038
17039 static void
17040 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17041 {
17042 char *name;
17043 int c;
17044 symbolS *symbolP;
17045 expressionS exp;
17046
17047 c = get_symbol_name (&name);
17048 symbolP = symbol_find_or_make (name);
17049 S_SET_WEAK (symbolP);
17050 *input_line_pointer = c;
17051
17052 SKIP_WHITESPACE_AFTER_NAME ();
17053
17054 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17055 {
17056 if (S_IS_DEFINED (symbolP))
17057 {
17058 as_bad (_("ignoring attempt to redefine symbol %s"),
17059 S_GET_NAME (symbolP));
17060 ignore_rest_of_line ();
17061 return;
17062 }
17063
17064 if (*input_line_pointer == ',')
17065 {
17066 ++input_line_pointer;
17067 SKIP_WHITESPACE ();
17068 }
17069
17070 expression (&exp);
17071 if (exp.X_op != O_symbol)
17072 {
17073 as_bad (_("bad .weakext directive"));
17074 ignore_rest_of_line ();
17075 return;
17076 }
17077 symbol_set_value_expression (symbolP, &exp);
17078 }
17079
17080 demand_empty_rest_of_line ();
17081 }
17082
17083 /* Parse a register string into a number. Called from the ECOFF code
17084 to parse .frame. The argument is non-zero if this is the frame
17085 register, so that we can record it in mips_frame_reg. */
17086
17087 int
17088 tc_get_register (int frame)
17089 {
17090 unsigned int reg;
17091
17092 SKIP_WHITESPACE ();
17093 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17094 reg = 0;
17095 if (frame)
17096 {
17097 mips_frame_reg = reg != 0 ? reg : SP;
17098 mips_frame_reg_valid = 1;
17099 mips_cprestore_valid = 0;
17100 }
17101 return reg;
17102 }
17103
17104 valueT
17105 md_section_align (asection *seg, valueT addr)
17106 {
17107 int align = bfd_get_section_alignment (stdoutput, seg);
17108
17109 /* We don't need to align ELF sections to the full alignment.
17110 However, Irix 5 may prefer that we align them at least to a 16
17111 byte boundary. We don't bother to align the sections if we
17112 are targeted for an embedded system. */
17113 if (strncmp (TARGET_OS, "elf", 3) == 0)
17114 return addr;
17115 if (align > 4)
17116 align = 4;
17117
17118 return ((addr + (1 << align) - 1) & -(1 << align));
17119 }
17120
17121 /* Utility routine, called from above as well. If called while the
17122 input file is still being read, it's only an approximation. (For
17123 example, a symbol may later become defined which appeared to be
17124 undefined earlier.) */
17125
17126 static int
17127 nopic_need_relax (symbolS *sym, int before_relaxing)
17128 {
17129 if (sym == 0)
17130 return 0;
17131
17132 if (g_switch_value > 0)
17133 {
17134 const char *symname;
17135 int change;
17136
17137 /* Find out whether this symbol can be referenced off the $gp
17138 register. It can be if it is smaller than the -G size or if
17139 it is in the .sdata or .sbss section. Certain symbols can
17140 not be referenced off the $gp, although it appears as though
17141 they can. */
17142 symname = S_GET_NAME (sym);
17143 if (symname != (const char *) NULL
17144 && (strcmp (symname, "eprol") == 0
17145 || strcmp (symname, "etext") == 0
17146 || strcmp (symname, "_gp") == 0
17147 || strcmp (symname, "edata") == 0
17148 || strcmp (symname, "_fbss") == 0
17149 || strcmp (symname, "_fdata") == 0
17150 || strcmp (symname, "_ftext") == 0
17151 || strcmp (symname, "end") == 0
17152 || strcmp (symname, "_gp_disp") == 0))
17153 change = 1;
17154 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17155 && (0
17156 #ifndef NO_ECOFF_DEBUGGING
17157 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17158 && (symbol_get_obj (sym)->ecoff_extern_size
17159 <= g_switch_value))
17160 #endif
17161 /* We must defer this decision until after the whole
17162 file has been read, since there might be a .extern
17163 after the first use of this symbol. */
17164 || (before_relaxing
17165 #ifndef NO_ECOFF_DEBUGGING
17166 && symbol_get_obj (sym)->ecoff_extern_size == 0
17167 #endif
17168 && S_GET_VALUE (sym) == 0)
17169 || (S_GET_VALUE (sym) != 0
17170 && S_GET_VALUE (sym) <= g_switch_value)))
17171 change = 0;
17172 else
17173 {
17174 const char *segname;
17175
17176 segname = segment_name (S_GET_SEGMENT (sym));
17177 gas_assert (strcmp (segname, ".lit8") != 0
17178 && strcmp (segname, ".lit4") != 0);
17179 change = (strcmp (segname, ".sdata") != 0
17180 && strcmp (segname, ".sbss") != 0
17181 && strncmp (segname, ".sdata.", 7) != 0
17182 && strncmp (segname, ".sbss.", 6) != 0
17183 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17184 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17185 }
17186 return change;
17187 }
17188 else
17189 /* We are not optimizing for the $gp register. */
17190 return 1;
17191 }
17192
17193
17194 /* Return true if the given symbol should be considered local for SVR4 PIC. */
17195
17196 static bfd_boolean
17197 pic_need_relax (symbolS *sym)
17198 {
17199 asection *symsec;
17200
17201 /* Handle the case of a symbol equated to another symbol. */
17202 while (symbol_equated_reloc_p (sym))
17203 {
17204 symbolS *n;
17205
17206 /* It's possible to get a loop here in a badly written program. */
17207 n = symbol_get_value_expression (sym)->X_add_symbol;
17208 if (n == sym)
17209 break;
17210 sym = n;
17211 }
17212
17213 if (symbol_section_p (sym))
17214 return TRUE;
17215
17216 symsec = S_GET_SEGMENT (sym);
17217
17218 /* This must duplicate the test in adjust_reloc_syms. */
17219 return (!bfd_is_und_section (symsec)
17220 && !bfd_is_abs_section (symsec)
17221 && !bfd_is_com_section (symsec)
17222 /* A global or weak symbol is treated as external. */
17223 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
17224 }
17225 \f
17226 /* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17227 convert a section-relative value VAL to the equivalent PC-relative
17228 value. */
17229
17230 static offsetT
17231 mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17232 offsetT val, long stretch)
17233 {
17234 fragS *sym_frag;
17235 addressT addr;
17236
17237 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17238
17239 sym_frag = symbol_get_frag (fragp->fr_symbol);
17240
17241 /* If the relax_marker of the symbol fragment differs from the
17242 relax_marker of this fragment, we have not yet adjusted the
17243 symbol fragment fr_address. We want to add in STRETCH in
17244 order to get a better estimate of the address. This
17245 particularly matters because of the shift bits. */
17246 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17247 {
17248 fragS *f;
17249
17250 /* Adjust stretch for any alignment frag. Note that if have
17251 been expanding the earlier code, the symbol may be
17252 defined in what appears to be an earlier frag. FIXME:
17253 This doesn't handle the fr_subtype field, which specifies
17254 a maximum number of bytes to skip when doing an
17255 alignment. */
17256 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17257 {
17258 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17259 {
17260 if (stretch < 0)
17261 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17262 else
17263 stretch &= ~((1 << (int) f->fr_offset) - 1);
17264 if (stretch == 0)
17265 break;
17266 }
17267 }
17268 if (f != NULL)
17269 val += stretch;
17270 }
17271
17272 addr = fragp->fr_address + fragp->fr_fix;
17273
17274 /* The base address rules are complicated. The base address of
17275 a branch is the following instruction. The base address of a
17276 PC relative load or add is the instruction itself, but if it
17277 is in a delay slot (in which case it can not be extended) use
17278 the address of the instruction whose delay slot it is in. */
17279 if (pcrel_op->include_isa_bit)
17280 {
17281 addr += 2;
17282
17283 /* If we are currently assuming that this frag should be
17284 extended, then the current address is two bytes higher. */
17285 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17286 addr += 2;
17287
17288 /* Ignore the low bit in the target, since it will be set
17289 for a text label. */
17290 val &= -2;
17291 }
17292 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17293 addr -= 4;
17294 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17295 addr -= 2;
17296
17297 val -= addr & -(1 << pcrel_op->align_log2);
17298
17299 return val;
17300 }
17301
17302 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17303 extended opcode. SEC is the section the frag is in. */
17304
17305 static int
17306 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17307 {
17308 const struct mips_int_operand *operand;
17309 offsetT val;
17310 segT symsec;
17311 int type;
17312
17313 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17314 return 0;
17315 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17316 return 1;
17317
17318 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17319 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17320 operand = mips16_immed_operand (type, FALSE);
17321 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17322 || (operand->root.type == OP_PCREL
17323 ? sec != symsec
17324 : !bfd_is_abs_section (symsec)))
17325 return 1;
17326
17327 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17328
17329 if (operand->root.type == OP_PCREL)
17330 {
17331 const struct mips_pcrel_operand *pcrel_op;
17332 offsetT maxtiny;
17333
17334 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
17335 return 1;
17336
17337 pcrel_op = (const struct mips_pcrel_operand *) operand;
17338 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17339
17340 /* If any of the shifted bits are set, we must use an extended
17341 opcode. If the address depends on the size of this
17342 instruction, this can lead to a loop, so we arrange to always
17343 use an extended opcode. */
17344 if ((val & ((1 << operand->shift) - 1)) != 0)
17345 {
17346 fragp->fr_subtype =
17347 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17348 return 1;
17349 }
17350
17351 /* If we are about to mark a frag as extended because the value
17352 is precisely the next value above maxtiny, then there is a
17353 chance of an infinite loop as in the following code:
17354 la $4,foo
17355 .skip 1020
17356 .align 2
17357 foo:
17358 In this case when the la is extended, foo is 0x3fc bytes
17359 away, so the la can be shrunk, but then foo is 0x400 away, so
17360 the la must be extended. To avoid this loop, we mark the
17361 frag as extended if it was small, and is about to become
17362 extended with the next value above maxtiny. */
17363 maxtiny = mips_int_operand_max (operand);
17364 if (val == maxtiny + (1 << operand->shift)
17365 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17366 {
17367 fragp->fr_subtype =
17368 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17369 return 1;
17370 }
17371 }
17372
17373 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17374 }
17375
17376 /* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17377 macro expansion. SEC is the section the frag is in. We only
17378 support PC-relative instructions (LA, DLA, LW, LD) here, in
17379 non-PIC code using 32-bit addressing. */
17380
17381 static int
17382 mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17383 {
17384 const struct mips_pcrel_operand *pcrel_op;
17385 const struct mips_int_operand *operand;
17386 offsetT val;
17387 segT symsec;
17388 int type;
17389
17390 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17391
17392 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17393 return 0;
17394 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17395 return 0;
17396
17397 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17398 switch (type)
17399 {
17400 case 'A':
17401 case 'B':
17402 case 'E':
17403 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17404 if (bfd_is_abs_section (symsec))
17405 return 1;
17406 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17407 return 0;
17408 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17409 return 1;
17410
17411 operand = mips16_immed_operand (type, TRUE);
17412 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17413 pcrel_op = (const struct mips_pcrel_operand *) operand;
17414 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17415
17416 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17417
17418 default:
17419 return 0;
17420 }
17421 }
17422
17423 /* Compute the length of a branch sequence, and adjust the
17424 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17425 worst-case length is computed, with UPDATE being used to indicate
17426 whether an unconditional (-1), branch-likely (+1) or regular (0)
17427 branch is to be computed. */
17428 static int
17429 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17430 {
17431 bfd_boolean toofar;
17432 int length;
17433
17434 if (fragp
17435 && S_IS_DEFINED (fragp->fr_symbol)
17436 && !S_IS_WEAK (fragp->fr_symbol)
17437 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17438 {
17439 addressT addr;
17440 offsetT val;
17441
17442 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17443
17444 addr = fragp->fr_address + fragp->fr_fix + 4;
17445
17446 val -= addr;
17447
17448 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17449 }
17450 else
17451 /* If the symbol is not defined or it's in a different segment,
17452 we emit the long sequence. */
17453 toofar = TRUE;
17454
17455 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17456 fragp->fr_subtype
17457 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17458 RELAX_BRANCH_PIC (fragp->fr_subtype),
17459 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17460 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17461 RELAX_BRANCH_LINK (fragp->fr_subtype),
17462 toofar);
17463
17464 length = 4;
17465 if (toofar)
17466 {
17467 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17468 length += 8;
17469
17470 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
17471 {
17472 /* Additional space for PIC loading of target address. */
17473 length += 8;
17474 if (mips_opts.isa == ISA_MIPS1)
17475 /* Additional space for $at-stabilizing nop. */
17476 length += 4;
17477 }
17478
17479 /* If branch is conditional. */
17480 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17481 length += 8;
17482 }
17483
17484 return length;
17485 }
17486
17487 /* Get a FRAG's branch instruction delay slot size, either from the
17488 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17489 or SHORT_INSN_SIZE otherwise. */
17490
17491 static int
17492 frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
17493 {
17494 char *buf = fragp->fr_literal + fragp->fr_fix;
17495
17496 if (al)
17497 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
17498 else
17499 return short_insn_size;
17500 }
17501
17502 /* Compute the length of a branch sequence, and adjust the
17503 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17504 worst-case length is computed, with UPDATE being used to indicate
17505 whether an unconditional (-1), or regular (0) branch is to be
17506 computed. */
17507
17508 static int
17509 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17510 {
17511 bfd_boolean insn32 = TRUE;
17512 bfd_boolean nods = TRUE;
17513 bfd_boolean pic = TRUE;
17514 bfd_boolean al = TRUE;
17515 int short_insn_size;
17516 bfd_boolean toofar;
17517 int length;
17518
17519 if (fragp)
17520 {
17521 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
17522 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
17523 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
17524 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17525 }
17526 short_insn_size = insn32 ? 4 : 2;
17527
17528 if (fragp
17529 && S_IS_DEFINED (fragp->fr_symbol)
17530 && !S_IS_WEAK (fragp->fr_symbol)
17531 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17532 {
17533 addressT addr;
17534 offsetT val;
17535
17536 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17537 /* Ignore the low bit in the target, since it will be set
17538 for a text label. */
17539 if ((val & 1) != 0)
17540 --val;
17541
17542 addr = fragp->fr_address + fragp->fr_fix + 4;
17543
17544 val -= addr;
17545
17546 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17547 }
17548 else
17549 /* If the symbol is not defined or it's in a different segment,
17550 we emit the long sequence. */
17551 toofar = TRUE;
17552
17553 if (fragp && update
17554 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17555 fragp->fr_subtype = (toofar
17556 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17557 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17558
17559 length = 4;
17560 if (toofar)
17561 {
17562 bfd_boolean compact_known = fragp != NULL;
17563 bfd_boolean compact = FALSE;
17564 bfd_boolean uncond;
17565
17566 if (fragp)
17567 {
17568 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17569 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17570 }
17571 else
17572 uncond = update < 0;
17573
17574 /* If label is out of range, we turn branch <br>:
17575
17576 <br> label # 4 bytes
17577 0:
17578
17579 into:
17580
17581 j label # 4 bytes
17582 nop # 2/4 bytes if
17583 # compact && (!PIC || insn32)
17584 0:
17585 */
17586 if ((!pic || insn32) && (!compact_known || compact))
17587 length += short_insn_size;
17588
17589 /* If assembling PIC code, we further turn:
17590
17591 j label # 4 bytes
17592
17593 into:
17594
17595 lw/ld at, %got(label)(gp) # 4 bytes
17596 d/addiu at, %lo(label) # 4 bytes
17597 jr/c at # 2/4 bytes
17598 */
17599 if (pic)
17600 length += 4 + short_insn_size;
17601
17602 /* Add an extra nop if the jump has no compact form and we need
17603 to fill the delay slot. */
17604 if ((!pic || al) && nods)
17605 length += (fragp
17606 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
17607 : short_insn_size);
17608
17609 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17610
17611 <brneg> 0f # 4 bytes
17612 nop # 2/4 bytes if !compact
17613 */
17614 if (!uncond)
17615 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
17616 }
17617 else if (nods)
17618 {
17619 /* Add an extra nop to fill the delay slot. */
17620 gas_assert (fragp);
17621 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
17622 }
17623
17624 return length;
17625 }
17626
17627 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17628 bit accordingly. */
17629
17630 static int
17631 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17632 {
17633 bfd_boolean toofar;
17634
17635 if (fragp
17636 && S_IS_DEFINED (fragp->fr_symbol)
17637 && !S_IS_WEAK (fragp->fr_symbol)
17638 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17639 {
17640 addressT addr;
17641 offsetT val;
17642 int type;
17643
17644 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17645 /* Ignore the low bit in the target, since it will be set
17646 for a text label. */
17647 if ((val & 1) != 0)
17648 --val;
17649
17650 /* Assume this is a 2-byte branch. */
17651 addr = fragp->fr_address + fragp->fr_fix + 2;
17652
17653 /* We try to avoid the infinite loop by not adding 2 more bytes for
17654 long branches. */
17655
17656 val -= addr;
17657
17658 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17659 if (type == 'D')
17660 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17661 else if (type == 'E')
17662 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17663 else
17664 abort ();
17665 }
17666 else
17667 /* If the symbol is not defined or it's in a different segment,
17668 we emit a normal 32-bit branch. */
17669 toofar = TRUE;
17670
17671 if (fragp && update
17672 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17673 fragp->fr_subtype
17674 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17675 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17676
17677 if (toofar)
17678 return 4;
17679
17680 return 2;
17681 }
17682
17683 /* Estimate the size of a frag before relaxing. Unless this is the
17684 mips16, we are not really relaxing here, and the final size is
17685 encoded in the subtype information. For the mips16, we have to
17686 decide whether we are using an extended opcode or not. */
17687
17688 int
17689 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17690 {
17691 int change;
17692
17693 if (RELAX_BRANCH_P (fragp->fr_subtype))
17694 {
17695
17696 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17697
17698 return fragp->fr_var;
17699 }
17700
17701 if (RELAX_MIPS16_P (fragp->fr_subtype))
17702 {
17703 /* We don't want to modify the EXTENDED bit here; it might get us
17704 into infinite loops. We change it only in mips_relax_frag(). */
17705 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
17706 return 12;
17707 else
17708 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
17709 }
17710
17711 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17712 {
17713 int length = 4;
17714
17715 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17716 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17717 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17718 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17719 fragp->fr_var = length;
17720
17721 return length;
17722 }
17723
17724 if (mips_pic == VXWORKS_PIC)
17725 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17726 change = 0;
17727 else if (RELAX_PIC (fragp->fr_subtype))
17728 change = pic_need_relax (fragp->fr_symbol);
17729 else
17730 change = nopic_need_relax (fragp->fr_symbol, 0);
17731
17732 if (change)
17733 {
17734 fragp->fr_subtype |= RELAX_USE_SECOND;
17735 return -RELAX_FIRST (fragp->fr_subtype);
17736 }
17737 else
17738 return -RELAX_SECOND (fragp->fr_subtype);
17739 }
17740
17741 /* This is called to see whether a reloc against a defined symbol
17742 should be converted into a reloc against a section. */
17743
17744 int
17745 mips_fix_adjustable (fixS *fixp)
17746 {
17747 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17748 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17749 return 0;
17750
17751 if (fixp->fx_addsy == NULL)
17752 return 1;
17753
17754 /* Allow relocs used for EH tables. */
17755 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
17756 return 1;
17757
17758 /* If symbol SYM is in a mergeable section, relocations of the form
17759 SYM + 0 can usually be made section-relative. The mergeable data
17760 is then identified by the section offset rather than by the symbol.
17761
17762 However, if we're generating REL LO16 relocations, the offset is split
17763 between the LO16 and partnering high part relocation. The linker will
17764 need to recalculate the complete offset in order to correctly identify
17765 the merge data.
17766
17767 The linker has traditionally not looked for the partnering high part
17768 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17769 placed anywhere. Rather than break backwards compatibility by changing
17770 this, it seems better not to force the issue, and instead keep the
17771 original symbol. This will work with either linker behavior. */
17772 if ((lo16_reloc_p (fixp->fx_r_type)
17773 || reloc_needs_lo_p (fixp->fx_r_type))
17774 && HAVE_IN_PLACE_ADDENDS
17775 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17776 return 0;
17777
17778 /* There is no place to store an in-place offset for JALR relocations. */
17779 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
17780 return 0;
17781
17782 /* Likewise an in-range offset of limited PC-relative relocations may
17783 overflow the in-place relocatable field if recalculated against the
17784 start address of the symbol's containing section.
17785
17786 Also, PC relative relocations for MIPS R6 need to be symbol rather than
17787 section relative to allow linker relaxations to be performed later on. */
17788 if (limited_pcrel_reloc_p (fixp->fx_r_type)
17789 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
17790 return 0;
17791
17792 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17793 to a floating-point stub. The same is true for non-R_MIPS16_26
17794 relocations against MIPS16 functions; in this case, the stub becomes
17795 the function's canonical address.
17796
17797 Floating-point stubs are stored in unique .mips16.call.* or
17798 .mips16.fn.* sections. If a stub T for function F is in section S,
17799 the first relocation in section S must be against F; this is how the
17800 linker determines the target function. All relocations that might
17801 resolve to T must also be against F. We therefore have the following
17802 restrictions, which are given in an intentionally-redundant way:
17803
17804 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17805 symbols.
17806
17807 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17808 if that stub might be used.
17809
17810 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17811 symbols.
17812
17813 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17814 that stub might be used.
17815
17816 There is a further restriction:
17817
17818 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17819 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
17820 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
17821 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
17822 against MIPS16 or microMIPS symbols because we need to keep the
17823 MIPS16 or microMIPS symbol for the purpose of mode mismatch
17824 detection and JAL or BAL to JALX instruction conversion in the
17825 linker.
17826
17827 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17828 against a MIPS16 symbol. We deal with (5) by additionally leaving
17829 alone any jump and branch relocations against a microMIPS symbol.
17830
17831 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17832 relocation against some symbol R, no relocation against R may be
17833 reduced. (Note that this deals with (2) as well as (1) because
17834 relocations against global symbols will never be reduced on ELF
17835 targets.) This approach is a little simpler than trying to detect
17836 stub sections, and gives the "all or nothing" per-symbol consistency
17837 that we have for MIPS16 symbols. */
17838 if (fixp->fx_subsy == NULL
17839 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
17840 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17841 && (jmp_reloc_p (fixp->fx_r_type)
17842 || b_reloc_p (fixp->fx_r_type)))
17843 || *symbol_get_tc (fixp->fx_addsy)))
17844 return 0;
17845
17846 return 1;
17847 }
17848
17849 /* Translate internal representation of relocation info to BFD target
17850 format. */
17851
17852 arelent **
17853 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
17854 {
17855 static arelent *retval[4];
17856 arelent *reloc;
17857 bfd_reloc_code_real_type code;
17858
17859 memset (retval, 0, sizeof(retval));
17860 reloc = retval[0] = XCNEW (arelent);
17861 reloc->sym_ptr_ptr = XNEW (asymbol *);
17862 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
17863 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17864
17865 if (fixp->fx_pcrel)
17866 {
17867 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
17868 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
17869 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17870 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
17871 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
17872 || fixp->fx_r_type == BFD_RELOC_32_PCREL
17873 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
17874 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
17875 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
17876 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
17877 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
17878 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
17879
17880 /* At this point, fx_addnumber is "symbol offset - pcrel address".
17881 Relocations want only the symbol offset. */
17882 switch (fixp->fx_r_type)
17883 {
17884 case BFD_RELOC_MIPS_18_PCREL_S3:
17885 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
17886 break;
17887 default:
17888 reloc->addend = fixp->fx_addnumber + reloc->address;
17889 break;
17890 }
17891 }
17892 else if (HAVE_IN_PLACE_ADDENDS
17893 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
17894 && (read_compressed_insn (fixp->fx_frag->fr_literal
17895 + fixp->fx_where, 4) >> 26) == 0x3c)
17896 {
17897 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
17898 addend accordingly. */
17899 reloc->addend = fixp->fx_addnumber >> 1;
17900 }
17901 else
17902 reloc->addend = fixp->fx_addnumber;
17903
17904 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17905 entry to be used in the relocation's section offset. */
17906 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17907 {
17908 reloc->address = reloc->addend;
17909 reloc->addend = 0;
17910 }
17911
17912 code = fixp->fx_r_type;
17913
17914 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
17915 if (reloc->howto == NULL)
17916 {
17917 as_bad_where (fixp->fx_file, fixp->fx_line,
17918 _("cannot represent %s relocation in this object file"
17919 " format"),
17920 bfd_get_reloc_code_name (code));
17921 retval[0] = NULL;
17922 }
17923
17924 return retval;
17925 }
17926
17927 /* Relax a machine dependent frag. This returns the amount by which
17928 the current size of the frag should change. */
17929
17930 int
17931 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
17932 {
17933 if (RELAX_BRANCH_P (fragp->fr_subtype))
17934 {
17935 offsetT old_var = fragp->fr_var;
17936
17937 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
17938
17939 return fragp->fr_var - old_var;
17940 }
17941
17942 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17943 {
17944 offsetT old_var = fragp->fr_var;
17945 offsetT new_var = 4;
17946
17947 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17948 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17949 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17950 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17951 fragp->fr_var = new_var;
17952
17953 return new_var - old_var;
17954 }
17955
17956 if (! RELAX_MIPS16_P (fragp->fr_subtype))
17957 return 0;
17958
17959 if (!mips16_extended_frag (fragp, sec, stretch))
17960 {
17961 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
17962 {
17963 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
17964 return -10;
17965 }
17966 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17967 {
17968 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17969 return -2;
17970 }
17971 else
17972 return 0;
17973 }
17974 else if (!mips16_macro_frag (fragp, sec, stretch))
17975 {
17976 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
17977 {
17978 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
17979 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17980 return -8;
17981 }
17982 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17983 {
17984 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17985 return 2;
17986 }
17987 else
17988 return 0;
17989 }
17990 else
17991 {
17992 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
17993 return 0;
17994 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17995 {
17996 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17997 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
17998 return 8;
17999 }
18000 else
18001 {
18002 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18003 return 10;
18004 }
18005 }
18006
18007 return 0;
18008 }
18009
18010 /* Convert a machine dependent frag. */
18011
18012 void
18013 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
18014 {
18015 if (RELAX_BRANCH_P (fragp->fr_subtype))
18016 {
18017 char *buf;
18018 unsigned long insn;
18019 expressionS exp;
18020 fixS *fixp;
18021
18022 buf = fragp->fr_literal + fragp->fr_fix;
18023 insn = read_insn (buf);
18024
18025 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18026 {
18027 /* We generate a fixup instead of applying it right now
18028 because, if there are linker relaxations, we're going to
18029 need the relocations. */
18030 exp.X_op = O_symbol;
18031 exp.X_add_symbol = fragp->fr_symbol;
18032 exp.X_add_number = fragp->fr_offset;
18033
18034 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18035 BFD_RELOC_16_PCREL_S2);
18036 fixp->fx_file = fragp->fr_file;
18037 fixp->fx_line = fragp->fr_line;
18038
18039 buf = write_insn (buf, insn);
18040 }
18041 else
18042 {
18043 int i;
18044
18045 as_warn_where (fragp->fr_file, fragp->fr_line,
18046 _("relaxed out-of-range branch into a jump"));
18047
18048 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18049 goto uncond;
18050
18051 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18052 {
18053 /* Reverse the branch. */
18054 switch ((insn >> 28) & 0xf)
18055 {
18056 case 4:
18057 if ((insn & 0xff000000) == 0x47000000
18058 || (insn & 0xff600000) == 0x45600000)
18059 {
18060 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18061 reversed by tweaking bit 23. */
18062 insn ^= 0x00800000;
18063 }
18064 else
18065 {
18066 /* bc[0-3][tf]l? instructions can have the condition
18067 reversed by tweaking a single TF bit, and their
18068 opcodes all have 0x4???????. */
18069 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18070 insn ^= 0x00010000;
18071 }
18072 break;
18073
18074 case 0:
18075 /* bltz 0x04000000 bgez 0x04010000
18076 bltzal 0x04100000 bgezal 0x04110000 */
18077 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
18078 insn ^= 0x00010000;
18079 break;
18080
18081 case 1:
18082 /* beq 0x10000000 bne 0x14000000
18083 blez 0x18000000 bgtz 0x1c000000 */
18084 insn ^= 0x04000000;
18085 break;
18086
18087 default:
18088 abort ();
18089 }
18090 }
18091
18092 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18093 {
18094 /* Clear the and-link bit. */
18095 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
18096
18097 /* bltzal 0x04100000 bgezal 0x04110000
18098 bltzall 0x04120000 bgezall 0x04130000 */
18099 insn &= ~0x00100000;
18100 }
18101
18102 /* Branch over the branch (if the branch was likely) or the
18103 full jump (not likely case). Compute the offset from the
18104 current instruction to branch to. */
18105 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18106 i = 16;
18107 else
18108 {
18109 /* How many bytes in instructions we've already emitted? */
18110 i = buf - fragp->fr_literal - fragp->fr_fix;
18111 /* How many bytes in instructions from here to the end? */
18112 i = fragp->fr_var - i;
18113 }
18114 /* Convert to instruction count. */
18115 i >>= 2;
18116 /* Branch counts from the next instruction. */
18117 i--;
18118 insn |= i;
18119 /* Branch over the jump. */
18120 buf = write_insn (buf, insn);
18121
18122 /* nop */
18123 buf = write_insn (buf, 0);
18124
18125 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18126 {
18127 /* beql $0, $0, 2f */
18128 insn = 0x50000000;
18129 /* Compute the PC offset from the current instruction to
18130 the end of the variable frag. */
18131 /* How many bytes in instructions we've already emitted? */
18132 i = buf - fragp->fr_literal - fragp->fr_fix;
18133 /* How many bytes in instructions from here to the end? */
18134 i = fragp->fr_var - i;
18135 /* Convert to instruction count. */
18136 i >>= 2;
18137 /* Don't decrement i, because we want to branch over the
18138 delay slot. */
18139 insn |= i;
18140
18141 buf = write_insn (buf, insn);
18142 buf = write_insn (buf, 0);
18143 }
18144
18145 uncond:
18146 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
18147 {
18148 /* j or jal. */
18149 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18150 ? 0x0c000000 : 0x08000000);
18151 exp.X_op = O_symbol;
18152 exp.X_add_symbol = fragp->fr_symbol;
18153 exp.X_add_number = fragp->fr_offset;
18154
18155 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18156 FALSE, BFD_RELOC_MIPS_JMP);
18157 fixp->fx_file = fragp->fr_file;
18158 fixp->fx_line = fragp->fr_line;
18159
18160 buf = write_insn (buf, insn);
18161 }
18162 else
18163 {
18164 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18165
18166 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
18167 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18168 insn |= at << OP_SH_RT;
18169 exp.X_op = O_symbol;
18170 exp.X_add_symbol = fragp->fr_symbol;
18171 exp.X_add_number = fragp->fr_offset;
18172
18173 if (fragp->fr_offset)
18174 {
18175 exp.X_add_symbol = make_expr_symbol (&exp);
18176 exp.X_add_number = 0;
18177 }
18178
18179 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18180 FALSE, BFD_RELOC_MIPS_GOT16);
18181 fixp->fx_file = fragp->fr_file;
18182 fixp->fx_line = fragp->fr_line;
18183
18184 buf = write_insn (buf, insn);
18185
18186 if (mips_opts.isa == ISA_MIPS1)
18187 /* nop */
18188 buf = write_insn (buf, 0);
18189
18190 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
18191 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18192 insn |= at << OP_SH_RS | at << OP_SH_RT;
18193
18194 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18195 FALSE, BFD_RELOC_LO16);
18196 fixp->fx_file = fragp->fr_file;
18197 fixp->fx_line = fragp->fr_line;
18198
18199 buf = write_insn (buf, insn);
18200
18201 /* j(al)r $at. */
18202 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18203 insn = 0x0000f809;
18204 else
18205 insn = 0x00000008;
18206 insn |= at << OP_SH_RS;
18207
18208 buf = write_insn (buf, insn);
18209 }
18210 }
18211
18212 fragp->fr_fix += fragp->fr_var;
18213 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18214 return;
18215 }
18216
18217 /* Relax microMIPS branches. */
18218 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18219 {
18220 char *buf = fragp->fr_literal + fragp->fr_fix;
18221 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18222 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18223 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18224 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18225 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18226 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18227 bfd_boolean short_ds;
18228 unsigned long insn;
18229 expressionS exp;
18230 fixS *fixp;
18231
18232 exp.X_op = O_symbol;
18233 exp.X_add_symbol = fragp->fr_symbol;
18234 exp.X_add_number = fragp->fr_offset;
18235
18236 fragp->fr_fix += fragp->fr_var;
18237
18238 /* Handle 16-bit branches that fit or are forced to fit. */
18239 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18240 {
18241 /* We generate a fixup instead of applying it right now,
18242 because if there is linker relaxation, we're going to
18243 need the relocations. */
18244 if (type == 'D')
18245 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18246 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18247 else if (type == 'E')
18248 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18249 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18250 else
18251 abort ();
18252
18253 fixp->fx_file = fragp->fr_file;
18254 fixp->fx_line = fragp->fr_line;
18255
18256 /* These relocations can have an addend that won't fit in
18257 2 octets. */
18258 fixp->fx_no_overflow = 1;
18259
18260 return;
18261 }
18262
18263 /* Handle 32-bit branches that fit or are forced to fit. */
18264 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18265 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18266 {
18267 /* We generate a fixup instead of applying it right now,
18268 because if there is linker relaxation, we're going to
18269 need the relocations. */
18270 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18271 BFD_RELOC_MICROMIPS_16_PCREL_S1);
18272 fixp->fx_file = fragp->fr_file;
18273 fixp->fx_line = fragp->fr_line;
18274
18275 if (type == 0)
18276 {
18277 insn = read_compressed_insn (buf, 4);
18278 buf += 4;
18279
18280 if (nods)
18281 {
18282 /* Check the short-delay-slot bit. */
18283 if (!al || (insn & 0x02000000) != 0)
18284 buf = write_compressed_insn (buf, 0x0c00, 2);
18285 else
18286 buf = write_compressed_insn (buf, 0x00000000, 4);
18287 }
18288
18289 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18290 return;
18291 }
18292 }
18293
18294 /* Relax 16-bit branches to 32-bit branches. */
18295 if (type != 0)
18296 {
18297 insn = read_compressed_insn (buf, 2);
18298
18299 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18300 insn = 0x94000000; /* beq */
18301 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18302 {
18303 unsigned long regno;
18304
18305 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18306 regno = micromips_to_32_reg_d_map [regno];
18307 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18308 insn |= regno << MICROMIPSOP_SH_RS;
18309 }
18310 else
18311 abort ();
18312
18313 /* Nothing else to do, just write it out. */
18314 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18315 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18316 {
18317 buf = write_compressed_insn (buf, insn, 4);
18318 if (nods)
18319 buf = write_compressed_insn (buf, 0x0c00, 2);
18320 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18321 return;
18322 }
18323 }
18324 else
18325 insn = read_compressed_insn (buf, 4);
18326
18327 /* Relax 32-bit branches to a sequence of instructions. */
18328 as_warn_where (fragp->fr_file, fragp->fr_line,
18329 _("relaxed out-of-range branch into a jump"));
18330
18331 /* Set the short-delay-slot bit. */
18332 short_ds = !al || (insn & 0x02000000) != 0;
18333
18334 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18335 {
18336 symbolS *l;
18337
18338 /* Reverse the branch. */
18339 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18340 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18341 insn ^= 0x20000000;
18342 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18343 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18344 || (insn & 0xffe00000) == 0x40800000 /* blez */
18345 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18346 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18347 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18348 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18349 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18350 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18351 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18352 insn ^= 0x00400000;
18353 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18354 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18355 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18356 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18357 insn ^= 0x00200000;
18358 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18359 BNZ.df */
18360 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18361 BNZ.V */
18362 insn ^= 0x00800000;
18363 else
18364 abort ();
18365
18366 if (al)
18367 {
18368 /* Clear the and-link and short-delay-slot bits. */
18369 gas_assert ((insn & 0xfda00000) == 0x40200000);
18370
18371 /* bltzal 0x40200000 bgezal 0x40600000 */
18372 /* bltzals 0x42200000 bgezals 0x42600000 */
18373 insn &= ~0x02200000;
18374 }
18375
18376 /* Make a label at the end for use with the branch. */
18377 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18378 micromips_label_inc ();
18379 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18380
18381 /* Refer to it. */
18382 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18383 BFD_RELOC_MICROMIPS_16_PCREL_S1);
18384 fixp->fx_file = fragp->fr_file;
18385 fixp->fx_line = fragp->fr_line;
18386
18387 /* Branch over the jump. */
18388 buf = write_compressed_insn (buf, insn, 4);
18389
18390 if (!compact)
18391 {
18392 /* nop */
18393 if (insn32)
18394 buf = write_compressed_insn (buf, 0x00000000, 4);
18395 else
18396 buf = write_compressed_insn (buf, 0x0c00, 2);
18397 }
18398 }
18399
18400 if (!pic)
18401 {
18402 unsigned long jal = (short_ds || nods
18403 ? 0x74000000 : 0xf4000000); /* jal/s */
18404
18405 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18406 insn = al ? jal : 0xd4000000;
18407
18408 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18409 BFD_RELOC_MICROMIPS_JMP);
18410 fixp->fx_file = fragp->fr_file;
18411 fixp->fx_line = fragp->fr_line;
18412
18413 buf = write_compressed_insn (buf, insn, 4);
18414
18415 if (compact || nods)
18416 {
18417 /* nop */
18418 if (insn32)
18419 buf = write_compressed_insn (buf, 0x00000000, 4);
18420 else
18421 buf = write_compressed_insn (buf, 0x0c00, 2);
18422 }
18423 }
18424 else
18425 {
18426 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18427
18428 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18429 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18430 insn |= at << MICROMIPSOP_SH_RT;
18431
18432 if (exp.X_add_number)
18433 {
18434 exp.X_add_symbol = make_expr_symbol (&exp);
18435 exp.X_add_number = 0;
18436 }
18437
18438 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18439 BFD_RELOC_MICROMIPS_GOT16);
18440 fixp->fx_file = fragp->fr_file;
18441 fixp->fx_line = fragp->fr_line;
18442
18443 buf = write_compressed_insn (buf, insn, 4);
18444
18445 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18446 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18447 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18448
18449 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18450 BFD_RELOC_MICROMIPS_LO16);
18451 fixp->fx_file = fragp->fr_file;
18452 fixp->fx_line = fragp->fr_line;
18453
18454 buf = write_compressed_insn (buf, insn, 4);
18455
18456 if (insn32)
18457 {
18458 /* jr/jalr $at */
18459 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18460 insn |= at << MICROMIPSOP_SH_RS;
18461
18462 buf = write_compressed_insn (buf, insn, 4);
18463
18464 if (compact || nods)
18465 /* nop */
18466 buf = write_compressed_insn (buf, 0x00000000, 4);
18467 }
18468 else
18469 {
18470 /* jr/jrc/jalr/jalrs $at */
18471 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18472 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
18473
18474 insn = al ? jalr : jr;
18475 insn |= at << MICROMIPSOP_SH_MJ;
18476
18477 buf = write_compressed_insn (buf, insn, 2);
18478 if (al && nods)
18479 {
18480 /* nop */
18481 if (short_ds)
18482 buf = write_compressed_insn (buf, 0x0c00, 2);
18483 else
18484 buf = write_compressed_insn (buf, 0x00000000, 4);
18485 }
18486 }
18487 }
18488
18489 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18490 return;
18491 }
18492
18493 if (RELAX_MIPS16_P (fragp->fr_subtype))
18494 {
18495 int type;
18496 const struct mips_int_operand *operand;
18497 offsetT val;
18498 char *buf;
18499 unsigned int user_length;
18500 bfd_boolean need_reloc;
18501 unsigned long insn;
18502 bfd_boolean mac;
18503 bfd_boolean ext;
18504 segT symsec;
18505
18506 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18507 operand = mips16_immed_operand (type, FALSE);
18508
18509 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
18510 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
18511 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
18512
18513 symsec = S_GET_SEGMENT (fragp->fr_symbol);
18514 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
18515 || (operand->root.type == OP_PCREL && !mac
18516 ? asec != symsec
18517 : !bfd_is_abs_section (symsec)));
18518
18519 if (operand->root.type == OP_PCREL && !mac)
18520 {
18521 const struct mips_pcrel_operand *pcrel_op;
18522
18523 pcrel_op = (const struct mips_pcrel_operand *) operand;
18524
18525 if (pcrel_op->include_isa_bit && !need_reloc)
18526 {
18527 if (!ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
18528 as_bad_where (fragp->fr_file, fragp->fr_line,
18529 _("branch to a symbol in another ISA mode"));
18530 else if ((fragp->fr_offset & 0x1) != 0)
18531 as_bad_where (fragp->fr_file, fragp->fr_line,
18532 _("branch to misaligned address (0x%lx)"),
18533 (long) val);
18534 }
18535
18536 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
18537
18538 /* Make sure the section winds up with the alignment we have
18539 assumed. */
18540 if (operand->shift > 0)
18541 record_alignment (asec, operand->shift);
18542 }
18543
18544 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18545 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18546 {
18547 if (mac)
18548 as_warn_where (fragp->fr_file, fragp->fr_line,
18549 _("macro instruction expanded into multiple "
18550 "instructions in a branch delay slot"));
18551 else if (ext)
18552 as_warn_where (fragp->fr_file, fragp->fr_line,
18553 _("extended instruction in a branch delay slot"));
18554 }
18555 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
18556 as_warn_where (fragp->fr_file, fragp->fr_line,
18557 _("macro instruction expanded into multiple "
18558 "instructions"));
18559
18560 buf = fragp->fr_literal + fragp->fr_fix;
18561
18562 insn = read_compressed_insn (buf, 2);
18563 if (ext)
18564 insn |= MIPS16_EXTEND;
18565
18566 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18567 user_length = 4;
18568 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18569 user_length = 2;
18570 else
18571 user_length = 0;
18572
18573 if (mac)
18574 {
18575 unsigned long reg;
18576 unsigned long new;
18577 unsigned long op;
18578
18579 gas_assert (type == 'A' || type == 'B' || type == 'E');
18580 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
18581
18582 if (need_reloc)
18583 {
18584 fixS *fixp;
18585
18586 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
18587
18588 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18589 fragp->fr_symbol, fragp->fr_offset,
18590 FALSE, BFD_RELOC_MIPS16_HI16_S);
18591 fixp->fx_file = fragp->fr_file;
18592 fixp->fx_line = fragp->fr_line;
18593
18594 fixp = fix_new (fragp, buf - fragp->fr_literal + 8, 4,
18595 fragp->fr_symbol, fragp->fr_offset,
18596 FALSE, BFD_RELOC_MIPS16_LO16);
18597 fixp->fx_file = fragp->fr_file;
18598 fixp->fx_line = fragp->fr_line;
18599
18600 val = 0;
18601 }
18602
18603 switch (insn & 0xf800)
18604 {
18605 case 0x0800: /* ADDIU */
18606 reg = (insn >> 8) & 0x7;
18607 op = 0xf0004800 | (reg << 8);
18608 break;
18609 case 0xb000: /* LW */
18610 reg = (insn >> 8) & 0x7;
18611 op = 0xf0009800 | (reg << 8) | (reg << 5);
18612 break;
18613 case 0xf800: /* I64 */
18614 reg = (insn >> 5) & 0x7;
18615 switch (insn & 0x0700)
18616 {
18617 case 0x0400: /* LD */
18618 op = 0xf0003800 | (reg << 8) | (reg << 5);
18619 break;
18620 case 0x0600: /* DADDIU */
18621 op = 0xf000fd00 | (reg << 5);
18622 break;
18623 default:
18624 abort ();
18625 }
18626 break;
18627 default:
18628 abort ();
18629 }
18630
18631 new = 0xf0006800 | (reg << 8); /* LI */
18632 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
18633 buf = write_compressed_insn (buf, new, 4);
18634 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
18635 buf = write_compressed_insn (buf, new, 4);
18636 op |= mips16_immed_extend (val, 16);
18637 buf = write_compressed_insn (buf, op, 4);
18638
18639 fragp->fr_fix += 12;
18640 }
18641 else
18642 {
18643 unsigned int length = ext ? 4 : 2;
18644
18645 if (need_reloc)
18646 {
18647 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
18648 expressionS exp;
18649 fixS *fixp;
18650
18651 switch (type)
18652 {
18653 case 'p':
18654 case 'q':
18655 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
18656 break;
18657 default:
18658 break;
18659 }
18660 if (mac || reloc == BFD_RELOC_NONE)
18661 as_bad_where (fragp->fr_file, fragp->fr_line,
18662 _("unsupported relocation"));
18663 else if (ext)
18664 {
18665 exp.X_op = O_symbol;
18666 exp.X_add_symbol = fragp->fr_symbol;
18667 exp.X_add_number = fragp->fr_offset;
18668
18669 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18670 TRUE, reloc);
18671
18672 fixp->fx_file = fragp->fr_file;
18673 fixp->fx_line = fragp->fr_line;
18674 }
18675 else
18676 as_bad_where (fragp->fr_file, fragp->fr_line,
18677 _("invalid unextended operand value"));
18678 }
18679 else
18680 mips16_immed (fragp->fr_file, fragp->fr_line, type,
18681 BFD_RELOC_UNUSED, val, user_length, &insn);
18682
18683 gas_assert (mips16_opcode_length (insn) == length);
18684 write_compressed_insn (buf, insn, length);
18685 fragp->fr_fix += length;
18686 }
18687 }
18688 else
18689 {
18690 relax_substateT subtype = fragp->fr_subtype;
18691 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18692 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18693 int first, second;
18694 fixS *fixp;
18695
18696 first = RELAX_FIRST (subtype);
18697 second = RELAX_SECOND (subtype);
18698 fixp = (fixS *) fragp->fr_opcode;
18699
18700 /* If the delay slot chosen does not match the size of the instruction,
18701 then emit a warning. */
18702 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18703 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18704 {
18705 relax_substateT s;
18706 const char *msg;
18707
18708 s = subtype & (RELAX_DELAY_SLOT_16BIT
18709 | RELAX_DELAY_SLOT_SIZE_FIRST
18710 | RELAX_DELAY_SLOT_SIZE_SECOND);
18711 msg = macro_warning (s);
18712 if (msg != NULL)
18713 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18714 subtype &= ~s;
18715 }
18716
18717 /* Possibly emit a warning if we've chosen the longer option. */
18718 if (use_second == second_longer)
18719 {
18720 relax_substateT s;
18721 const char *msg;
18722
18723 s = (subtype
18724 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18725 msg = macro_warning (s);
18726 if (msg != NULL)
18727 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18728 subtype &= ~s;
18729 }
18730
18731 /* Go through all the fixups for the first sequence. Disable them
18732 (by marking them as done) if we're going to use the second
18733 sequence instead. */
18734 while (fixp
18735 && fixp->fx_frag == fragp
18736 && fixp->fx_where < fragp->fr_fix - second)
18737 {
18738 if (subtype & RELAX_USE_SECOND)
18739 fixp->fx_done = 1;
18740 fixp = fixp->fx_next;
18741 }
18742
18743 /* Go through the fixups for the second sequence. Disable them if
18744 we're going to use the first sequence, otherwise adjust their
18745 addresses to account for the relaxation. */
18746 while (fixp && fixp->fx_frag == fragp)
18747 {
18748 if (subtype & RELAX_USE_SECOND)
18749 fixp->fx_where -= first;
18750 else
18751 fixp->fx_done = 1;
18752 fixp = fixp->fx_next;
18753 }
18754
18755 /* Now modify the frag contents. */
18756 if (subtype & RELAX_USE_SECOND)
18757 {
18758 char *start;
18759
18760 start = fragp->fr_literal + fragp->fr_fix - first - second;
18761 memmove (start, start + first, second);
18762 fragp->fr_fix -= first;
18763 }
18764 else
18765 fragp->fr_fix -= second;
18766 }
18767 }
18768
18769 /* This function is called after the relocs have been generated.
18770 We've been storing mips16 text labels as odd. Here we convert them
18771 back to even for the convenience of the debugger. */
18772
18773 void
18774 mips_frob_file_after_relocs (void)
18775 {
18776 asymbol **syms;
18777 unsigned int count, i;
18778
18779 syms = bfd_get_outsymbols (stdoutput);
18780 count = bfd_get_symcount (stdoutput);
18781 for (i = 0; i < count; i++, syms++)
18782 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18783 && ((*syms)->value & 1) != 0)
18784 {
18785 (*syms)->value &= ~1;
18786 /* If the symbol has an odd size, it was probably computed
18787 incorrectly, so adjust that as well. */
18788 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18789 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18790 }
18791 }
18792
18793 /* This function is called whenever a label is defined, including fake
18794 labels instantiated off the dot special symbol. It is used when
18795 handling branch delays; if a branch has a label, we assume we cannot
18796 move it. This also bumps the value of the symbol by 1 in compressed
18797 code. */
18798
18799 static void
18800 mips_record_label (symbolS *sym)
18801 {
18802 segment_info_type *si = seg_info (now_seg);
18803 struct insn_label_list *l;
18804
18805 if (free_insn_labels == NULL)
18806 l = XNEW (struct insn_label_list);
18807 else
18808 {
18809 l = free_insn_labels;
18810 free_insn_labels = l->next;
18811 }
18812
18813 l->label = sym;
18814 l->next = si->label_list;
18815 si->label_list = l;
18816 }
18817
18818 /* This function is called as tc_frob_label() whenever a label is defined
18819 and adds a DWARF-2 record we only want for true labels. */
18820
18821 void
18822 mips_define_label (symbolS *sym)
18823 {
18824 mips_record_label (sym);
18825 dwarf2_emit_label (sym);
18826 }
18827
18828 /* This function is called by tc_new_dot_label whenever a new dot symbol
18829 is defined. */
18830
18831 void
18832 mips_add_dot_label (symbolS *sym)
18833 {
18834 mips_record_label (sym);
18835 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18836 mips_compressed_mark_label (sym);
18837 }
18838 \f
18839 /* Converting ASE flags from internal to .MIPS.abiflags values. */
18840 static unsigned int
18841 mips_convert_ase_flags (int ase)
18842 {
18843 unsigned int ext_ases = 0;
18844
18845 if (ase & ASE_DSP)
18846 ext_ases |= AFL_ASE_DSP;
18847 if (ase & ASE_DSPR2)
18848 ext_ases |= AFL_ASE_DSPR2;
18849 if (ase & ASE_DSPR3)
18850 ext_ases |= AFL_ASE_DSPR3;
18851 if (ase & ASE_EVA)
18852 ext_ases |= AFL_ASE_EVA;
18853 if (ase & ASE_MCU)
18854 ext_ases |= AFL_ASE_MCU;
18855 if (ase & ASE_MDMX)
18856 ext_ases |= AFL_ASE_MDMX;
18857 if (ase & ASE_MIPS3D)
18858 ext_ases |= AFL_ASE_MIPS3D;
18859 if (ase & ASE_MT)
18860 ext_ases |= AFL_ASE_MT;
18861 if (ase & ASE_SMARTMIPS)
18862 ext_ases |= AFL_ASE_SMARTMIPS;
18863 if (ase & ASE_VIRT)
18864 ext_ases |= AFL_ASE_VIRT;
18865 if (ase & ASE_MSA)
18866 ext_ases |= AFL_ASE_MSA;
18867 if (ase & ASE_XPA)
18868 ext_ases |= AFL_ASE_XPA;
18869
18870 return ext_ases;
18871 }
18872 /* Some special processing for a MIPS ELF file. */
18873
18874 void
18875 mips_elf_final_processing (void)
18876 {
18877 int fpabi;
18878 Elf_Internal_ABIFlags_v0 flags;
18879
18880 flags.version = 0;
18881 flags.isa_rev = 0;
18882 switch (file_mips_opts.isa)
18883 {
18884 case INSN_ISA1:
18885 flags.isa_level = 1;
18886 break;
18887 case INSN_ISA2:
18888 flags.isa_level = 2;
18889 break;
18890 case INSN_ISA3:
18891 flags.isa_level = 3;
18892 break;
18893 case INSN_ISA4:
18894 flags.isa_level = 4;
18895 break;
18896 case INSN_ISA5:
18897 flags.isa_level = 5;
18898 break;
18899 case INSN_ISA32:
18900 flags.isa_level = 32;
18901 flags.isa_rev = 1;
18902 break;
18903 case INSN_ISA32R2:
18904 flags.isa_level = 32;
18905 flags.isa_rev = 2;
18906 break;
18907 case INSN_ISA32R3:
18908 flags.isa_level = 32;
18909 flags.isa_rev = 3;
18910 break;
18911 case INSN_ISA32R5:
18912 flags.isa_level = 32;
18913 flags.isa_rev = 5;
18914 break;
18915 case INSN_ISA32R6:
18916 flags.isa_level = 32;
18917 flags.isa_rev = 6;
18918 break;
18919 case INSN_ISA64:
18920 flags.isa_level = 64;
18921 flags.isa_rev = 1;
18922 break;
18923 case INSN_ISA64R2:
18924 flags.isa_level = 64;
18925 flags.isa_rev = 2;
18926 break;
18927 case INSN_ISA64R3:
18928 flags.isa_level = 64;
18929 flags.isa_rev = 3;
18930 break;
18931 case INSN_ISA64R5:
18932 flags.isa_level = 64;
18933 flags.isa_rev = 5;
18934 break;
18935 case INSN_ISA64R6:
18936 flags.isa_level = 64;
18937 flags.isa_rev = 6;
18938 break;
18939 }
18940
18941 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
18942 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
18943 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
18944 : (file_mips_opts.fp == 64) ? AFL_REG_64
18945 : AFL_REG_32;
18946 flags.cpr2_size = AFL_REG_NONE;
18947 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
18948 Tag_GNU_MIPS_ABI_FP);
18949 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
18950 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
18951 if (file_ase_mips16)
18952 flags.ases |= AFL_ASE_MIPS16;
18953 if (file_ase_micromips)
18954 flags.ases |= AFL_ASE_MICROMIPS;
18955 flags.flags1 = 0;
18956 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
18957 || file_mips_opts.fp == 64)
18958 && file_mips_opts.oddspreg)
18959 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
18960 flags.flags2 = 0;
18961
18962 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
18963 ((Elf_External_ABIFlags_v0 *)
18964 mips_flags_frag));
18965
18966 /* Write out the register information. */
18967 if (mips_abi != N64_ABI)
18968 {
18969 Elf32_RegInfo s;
18970
18971 s.ri_gprmask = mips_gprmask;
18972 s.ri_cprmask[0] = mips_cprmask[0];
18973 s.ri_cprmask[1] = mips_cprmask[1];
18974 s.ri_cprmask[2] = mips_cprmask[2];
18975 s.ri_cprmask[3] = mips_cprmask[3];
18976 /* The gp_value field is set by the MIPS ELF backend. */
18977
18978 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18979 ((Elf32_External_RegInfo *)
18980 mips_regmask_frag));
18981 }
18982 else
18983 {
18984 Elf64_Internal_RegInfo s;
18985
18986 s.ri_gprmask = mips_gprmask;
18987 s.ri_pad = 0;
18988 s.ri_cprmask[0] = mips_cprmask[0];
18989 s.ri_cprmask[1] = mips_cprmask[1];
18990 s.ri_cprmask[2] = mips_cprmask[2];
18991 s.ri_cprmask[3] = mips_cprmask[3];
18992 /* The gp_value field is set by the MIPS ELF backend. */
18993
18994 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18995 ((Elf64_External_RegInfo *)
18996 mips_regmask_frag));
18997 }
18998
18999 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19000 sort of BFD interface for this. */
19001 if (mips_any_noreorder)
19002 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19003 if (mips_pic != NO_PIC)
19004 {
19005 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
19006 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19007 }
19008 if (mips_abicalls)
19009 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19010
19011 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19012 defined at present; this might need to change in future. */
19013 if (file_ase_mips16)
19014 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
19015 if (file_ase_micromips)
19016 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
19017 if (file_mips_opts.ase & ASE_MDMX)
19018 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
19019
19020 /* Set the MIPS ELF ABI flags. */
19021 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
19022 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
19023 else if (mips_abi == O64_ABI)
19024 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
19025 else if (mips_abi == EABI_ABI)
19026 {
19027 if (file_mips_opts.gp == 64)
19028 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19029 else
19030 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19031 }
19032 else if (mips_abi == N32_ABI)
19033 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
19034
19035 /* Nothing to do for N64_ABI. */
19036
19037 if (mips_32bitmode)
19038 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
19039
19040 if (mips_nan2008 == 1)
19041 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19042
19043 /* 32 bit code with 64 bit FP registers. */
19044 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19045 Tag_GNU_MIPS_ABI_FP);
19046 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
19047 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
19048 }
19049 \f
19050 typedef struct proc {
19051 symbolS *func_sym;
19052 symbolS *func_end_sym;
19053 unsigned long reg_mask;
19054 unsigned long reg_offset;
19055 unsigned long fpreg_mask;
19056 unsigned long fpreg_offset;
19057 unsigned long frame_offset;
19058 unsigned long frame_reg;
19059 unsigned long pc_reg;
19060 } procS;
19061
19062 static procS cur_proc;
19063 static procS *cur_proc_ptr;
19064 static int numprocs;
19065
19066 /* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19067 as "2", and a normal nop as "0". */
19068
19069 #define NOP_OPCODE_MIPS 0
19070 #define NOP_OPCODE_MIPS16 1
19071 #define NOP_OPCODE_MICROMIPS 2
19072
19073 char
19074 mips_nop_opcode (void)
19075 {
19076 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19077 return NOP_OPCODE_MICROMIPS;
19078 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19079 return NOP_OPCODE_MIPS16;
19080 else
19081 return NOP_OPCODE_MIPS;
19082 }
19083
19084 /* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19085 32-bit microMIPS NOPs here (if applicable). */
19086
19087 void
19088 mips_handle_align (fragS *fragp)
19089 {
19090 char nop_opcode;
19091 char *p;
19092 int bytes, size, excess;
19093 valueT opcode;
19094
19095 if (fragp->fr_type != rs_align_code)
19096 return;
19097
19098 p = fragp->fr_literal + fragp->fr_fix;
19099 nop_opcode = *p;
19100 switch (nop_opcode)
19101 {
19102 case NOP_OPCODE_MICROMIPS:
19103 opcode = micromips_nop32_insn.insn_opcode;
19104 size = 4;
19105 break;
19106 case NOP_OPCODE_MIPS16:
19107 opcode = mips16_nop_insn.insn_opcode;
19108 size = 2;
19109 break;
19110 case NOP_OPCODE_MIPS:
19111 default:
19112 opcode = nop_insn.insn_opcode;
19113 size = 4;
19114 break;
19115 }
19116
19117 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19118 excess = bytes % size;
19119
19120 /* Handle the leading part if we're not inserting a whole number of
19121 instructions, and make it the end of the fixed part of the frag.
19122 Try to fit in a short microMIPS NOP if applicable and possible,
19123 and use zeroes otherwise. */
19124 gas_assert (excess < 4);
19125 fragp->fr_fix += excess;
19126 switch (excess)
19127 {
19128 case 3:
19129 *p++ = '\0';
19130 /* Fall through. */
19131 case 2:
19132 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
19133 {
19134 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
19135 break;
19136 }
19137 *p++ = '\0';
19138 /* Fall through. */
19139 case 1:
19140 *p++ = '\0';
19141 /* Fall through. */
19142 case 0:
19143 break;
19144 }
19145
19146 md_number_to_chars (p, opcode, size);
19147 fragp->fr_var = size;
19148 }
19149
19150 static long
19151 get_number (void)
19152 {
19153 int negative = 0;
19154 long val = 0;
19155
19156 if (*input_line_pointer == '-')
19157 {
19158 ++input_line_pointer;
19159 negative = 1;
19160 }
19161 if (!ISDIGIT (*input_line_pointer))
19162 as_bad (_("expected simple number"));
19163 if (input_line_pointer[0] == '0')
19164 {
19165 if (input_line_pointer[1] == 'x')
19166 {
19167 input_line_pointer += 2;
19168 while (ISXDIGIT (*input_line_pointer))
19169 {
19170 val <<= 4;
19171 val |= hex_value (*input_line_pointer++);
19172 }
19173 return negative ? -val : val;
19174 }
19175 else
19176 {
19177 ++input_line_pointer;
19178 while (ISDIGIT (*input_line_pointer))
19179 {
19180 val <<= 3;
19181 val |= *input_line_pointer++ - '0';
19182 }
19183 return negative ? -val : val;
19184 }
19185 }
19186 if (!ISDIGIT (*input_line_pointer))
19187 {
19188 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19189 *input_line_pointer, *input_line_pointer);
19190 as_warn (_("invalid number"));
19191 return -1;
19192 }
19193 while (ISDIGIT (*input_line_pointer))
19194 {
19195 val *= 10;
19196 val += *input_line_pointer++ - '0';
19197 }
19198 return negative ? -val : val;
19199 }
19200
19201 /* The .file directive; just like the usual .file directive, but there
19202 is an initial number which is the ECOFF file index. In the non-ECOFF
19203 case .file implies DWARF-2. */
19204
19205 static void
19206 s_mips_file (int x ATTRIBUTE_UNUSED)
19207 {
19208 static int first_file_directive = 0;
19209
19210 if (ECOFF_DEBUGGING)
19211 {
19212 get_number ();
19213 s_app_file (0);
19214 }
19215 else
19216 {
19217 char *filename;
19218
19219 filename = dwarf2_directive_file (0);
19220
19221 /* Versions of GCC up to 3.1 start files with a ".file"
19222 directive even for stabs output. Make sure that this
19223 ".file" is handled. Note that you need a version of GCC
19224 after 3.1 in order to support DWARF-2 on MIPS. */
19225 if (filename != NULL && ! first_file_directive)
19226 {
19227 (void) new_logical_line (filename, -1);
19228 s_app_file_string (filename, 0);
19229 }
19230 first_file_directive = 1;
19231 }
19232 }
19233
19234 /* The .loc directive, implying DWARF-2. */
19235
19236 static void
19237 s_mips_loc (int x ATTRIBUTE_UNUSED)
19238 {
19239 if (!ECOFF_DEBUGGING)
19240 dwarf2_directive_loc (0);
19241 }
19242
19243 /* The .end directive. */
19244
19245 static void
19246 s_mips_end (int x ATTRIBUTE_UNUSED)
19247 {
19248 symbolS *p;
19249
19250 /* Following functions need their own .frame and .cprestore directives. */
19251 mips_frame_reg_valid = 0;
19252 mips_cprestore_valid = 0;
19253
19254 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19255 {
19256 p = get_symbol ();
19257 demand_empty_rest_of_line ();
19258 }
19259 else
19260 p = NULL;
19261
19262 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19263 as_warn (_(".end not in text section"));
19264
19265 if (!cur_proc_ptr)
19266 {
19267 as_warn (_(".end directive without a preceding .ent directive"));
19268 demand_empty_rest_of_line ();
19269 return;
19270 }
19271
19272 if (p != NULL)
19273 {
19274 gas_assert (S_GET_NAME (p));
19275 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
19276 as_warn (_(".end symbol does not match .ent symbol"));
19277
19278 if (debug_type == DEBUG_STABS)
19279 stabs_generate_asm_endfunc (S_GET_NAME (p),
19280 S_GET_NAME (p));
19281 }
19282 else
19283 as_warn (_(".end directive missing or unknown symbol"));
19284
19285 /* Create an expression to calculate the size of the function. */
19286 if (p && cur_proc_ptr)
19287 {
19288 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19289 expressionS *exp = XNEW (expressionS);
19290
19291 obj->size = exp;
19292 exp->X_op = O_subtract;
19293 exp->X_add_symbol = symbol_temp_new_now ();
19294 exp->X_op_symbol = p;
19295 exp->X_add_number = 0;
19296
19297 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19298 }
19299
19300 #ifdef md_flush_pending_output
19301 md_flush_pending_output ();
19302 #endif
19303
19304 /* Generate a .pdr section. */
19305 if (!ECOFF_DEBUGGING && mips_flag_pdr)
19306 {
19307 segT saved_seg = now_seg;
19308 subsegT saved_subseg = now_subseg;
19309 expressionS exp;
19310 char *fragp;
19311
19312 gas_assert (pdr_seg);
19313 subseg_set (pdr_seg, 0);
19314
19315 /* Write the symbol. */
19316 exp.X_op = O_symbol;
19317 exp.X_add_symbol = p;
19318 exp.X_add_number = 0;
19319 emit_expr (&exp, 4);
19320
19321 fragp = frag_more (7 * 4);
19322
19323 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19324 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19325 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19326 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19327 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19328 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19329 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19330
19331 subseg_set (saved_seg, saved_subseg);
19332 }
19333
19334 cur_proc_ptr = NULL;
19335 }
19336
19337 /* The .aent and .ent directives. */
19338
19339 static void
19340 s_mips_ent (int aent)
19341 {
19342 symbolS *symbolP;
19343
19344 symbolP = get_symbol ();
19345 if (*input_line_pointer == ',')
19346 ++input_line_pointer;
19347 SKIP_WHITESPACE ();
19348 if (ISDIGIT (*input_line_pointer)
19349 || *input_line_pointer == '-')
19350 get_number ();
19351
19352 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19353 as_warn (_(".ent or .aent not in text section"));
19354
19355 if (!aent && cur_proc_ptr)
19356 as_warn (_("missing .end"));
19357
19358 if (!aent)
19359 {
19360 /* This function needs its own .frame and .cprestore directives. */
19361 mips_frame_reg_valid = 0;
19362 mips_cprestore_valid = 0;
19363
19364 cur_proc_ptr = &cur_proc;
19365 memset (cur_proc_ptr, '\0', sizeof (procS));
19366
19367 cur_proc_ptr->func_sym = symbolP;
19368
19369 ++numprocs;
19370
19371 if (debug_type == DEBUG_STABS)
19372 stabs_generate_asm_func (S_GET_NAME (symbolP),
19373 S_GET_NAME (symbolP));
19374 }
19375
19376 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19377
19378 demand_empty_rest_of_line ();
19379 }
19380
19381 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19382 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19383 s_mips_frame is used so that we can set the PDR information correctly.
19384 We can't use the ecoff routines because they make reference to the ecoff
19385 symbol table (in the mdebug section). */
19386
19387 static void
19388 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19389 {
19390 if (ECOFF_DEBUGGING)
19391 s_ignore (ignore);
19392 else
19393 {
19394 long val;
19395
19396 if (cur_proc_ptr == (procS *) NULL)
19397 {
19398 as_warn (_(".frame outside of .ent"));
19399 demand_empty_rest_of_line ();
19400 return;
19401 }
19402
19403 cur_proc_ptr->frame_reg = tc_get_register (1);
19404
19405 SKIP_WHITESPACE ();
19406 if (*input_line_pointer++ != ','
19407 || get_absolute_expression_and_terminator (&val) != ',')
19408 {
19409 as_warn (_("bad .frame directive"));
19410 --input_line_pointer;
19411 demand_empty_rest_of_line ();
19412 return;
19413 }
19414
19415 cur_proc_ptr->frame_offset = val;
19416 cur_proc_ptr->pc_reg = tc_get_register (0);
19417
19418 demand_empty_rest_of_line ();
19419 }
19420 }
19421
19422 /* The .fmask and .mask directives. If the mdebug section is present
19423 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19424 embedded targets, s_mips_mask is used so that we can set the PDR
19425 information correctly. We can't use the ecoff routines because they
19426 make reference to the ecoff symbol table (in the mdebug section). */
19427
19428 static void
19429 s_mips_mask (int reg_type)
19430 {
19431 if (ECOFF_DEBUGGING)
19432 s_ignore (reg_type);
19433 else
19434 {
19435 long mask, off;
19436
19437 if (cur_proc_ptr == (procS *) NULL)
19438 {
19439 as_warn (_(".mask/.fmask outside of .ent"));
19440 demand_empty_rest_of_line ();
19441 return;
19442 }
19443
19444 if (get_absolute_expression_and_terminator (&mask) != ',')
19445 {
19446 as_warn (_("bad .mask/.fmask directive"));
19447 --input_line_pointer;
19448 demand_empty_rest_of_line ();
19449 return;
19450 }
19451
19452 off = get_absolute_expression ();
19453
19454 if (reg_type == 'F')
19455 {
19456 cur_proc_ptr->fpreg_mask = mask;
19457 cur_proc_ptr->fpreg_offset = off;
19458 }
19459 else
19460 {
19461 cur_proc_ptr->reg_mask = mask;
19462 cur_proc_ptr->reg_offset = off;
19463 }
19464
19465 demand_empty_rest_of_line ();
19466 }
19467 }
19468
19469 /* A table describing all the processors gas knows about. Names are
19470 matched in the order listed.
19471
19472 To ease comparison, please keep this table in the same order as
19473 gcc's mips_cpu_info_table[]. */
19474 static const struct mips_cpu_info mips_cpu_info_table[] =
19475 {
19476 /* Entries for generic ISAs */
19477 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19478 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19479 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19480 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19481 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19482 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19483 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19484 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19485 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
19486 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
19487 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19488 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
19489 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19490 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
19491 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
19492
19493 /* MIPS I */
19494 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19495 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19496 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
19497
19498 /* MIPS II */
19499 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
19500
19501 /* MIPS III */
19502 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19503 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19504 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19505 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19506 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19507 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19508 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19509 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19510 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19511 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19512 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19513 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19514 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
19515 /* ST Microelectronics Loongson 2E and 2F cores */
19516 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19517 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
19518
19519 /* MIPS IV */
19520 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19521 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19522 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19523 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19524 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19525 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19526 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19527 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19528 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19529 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19530 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19531 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19532 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19533 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19534 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
19535
19536 /* MIPS 32 */
19537 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19538 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19539 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19540 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
19541
19542 /* MIPS 32 Release 2 */
19543 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19544 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19545 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19546 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19547 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19548 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19549 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19550 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19551 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19552 ISA_MIPS32R2, CPU_MIPS32R2 },
19553 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19554 ISA_MIPS32R2, CPU_MIPS32R2 },
19555 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19556 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19557 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19558 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19559 /* Deprecated forms of the above. */
19560 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19561 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19562 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
19563 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19564 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19565 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19566 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19567 /* Deprecated forms of the above. */
19568 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19569 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19570 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
19571 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19572 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19573 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19574 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19575 /* Deprecated forms of the above. */
19576 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19577 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19578 /* 34Kn is a 34kc without DSP. */
19579 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19580 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
19581 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19582 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19583 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19584 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19585 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19586 /* Deprecated forms of the above. */
19587 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19588 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19589 /* 1004K cores are multiprocessor versions of the 34K. */
19590 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19591 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19592 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19593 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19594 /* interaptiv is the new name for 1004kf */
19595 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19596 /* M5100 family */
19597 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19598 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19599 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
19600 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
19601
19602 /* MIPS 64 */
19603 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19604 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19605 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19606 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19607
19608 /* Broadcom SB-1 CPU core */
19609 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19610 /* Broadcom SB-1A CPU core */
19611 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19612
19613 { "loongson3a", 0, 0, ISA_MIPS64R2, CPU_LOONGSON_3A },
19614
19615 /* MIPS 64 Release 2 */
19616
19617 /* Cavium Networks Octeon CPU core */
19618 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19619 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19620 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
19621 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
19622
19623 /* RMI Xlr */
19624 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
19625
19626 /* Broadcom XLP.
19627 XLP is mostly like XLR, with the prominent exception that it is
19628 MIPS64R2 rather than MIPS64. */
19629 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
19630
19631 /* MIPS 64 Release 6 */
19632 { "i6400", 0, ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
19633 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
19634
19635 /* End marker */
19636 { NULL, 0, 0, 0, 0 }
19637 };
19638
19639
19640 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19641 with a final "000" replaced by "k". Ignore case.
19642
19643 Note: this function is shared between GCC and GAS. */
19644
19645 static bfd_boolean
19646 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19647 {
19648 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19649 given++, canonical++;
19650
19651 return ((*given == 0 && *canonical == 0)
19652 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19653 }
19654
19655
19656 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19657 CPU name. We've traditionally allowed a lot of variation here.
19658
19659 Note: this function is shared between GCC and GAS. */
19660
19661 static bfd_boolean
19662 mips_matching_cpu_name_p (const char *canonical, const char *given)
19663 {
19664 /* First see if the name matches exactly, or with a final "000"
19665 turned into "k". */
19666 if (mips_strict_matching_cpu_name_p (canonical, given))
19667 return TRUE;
19668
19669 /* If not, try comparing based on numerical designation alone.
19670 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19671 if (TOLOWER (*given) == 'r')
19672 given++;
19673 if (!ISDIGIT (*given))
19674 return FALSE;
19675
19676 /* Skip over some well-known prefixes in the canonical name,
19677 hoping to find a number there too. */
19678 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19679 canonical += 2;
19680 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19681 canonical += 2;
19682 else if (TOLOWER (canonical[0]) == 'r')
19683 canonical += 1;
19684
19685 return mips_strict_matching_cpu_name_p (canonical, given);
19686 }
19687
19688
19689 /* Parse an option that takes the name of a processor as its argument.
19690 OPTION is the name of the option and CPU_STRING is the argument.
19691 Return the corresponding processor enumeration if the CPU_STRING is
19692 recognized, otherwise report an error and return null.
19693
19694 A similar function exists in GCC. */
19695
19696 static const struct mips_cpu_info *
19697 mips_parse_cpu (const char *option, const char *cpu_string)
19698 {
19699 const struct mips_cpu_info *p;
19700
19701 /* 'from-abi' selects the most compatible architecture for the given
19702 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19703 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19704 version. Look first at the -mgp options, if given, otherwise base
19705 the choice on MIPS_DEFAULT_64BIT.
19706
19707 Treat NO_ABI like the EABIs. One reason to do this is that the
19708 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19709 architecture. This code picks MIPS I for 'mips' and MIPS III for
19710 'mips64', just as we did in the days before 'from-abi'. */
19711 if (strcasecmp (cpu_string, "from-abi") == 0)
19712 {
19713 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19714 return mips_cpu_info_from_isa (ISA_MIPS1);
19715
19716 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19717 return mips_cpu_info_from_isa (ISA_MIPS3);
19718
19719 if (file_mips_opts.gp >= 0)
19720 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
19721 ? ISA_MIPS1 : ISA_MIPS3);
19722
19723 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19724 ? ISA_MIPS3
19725 : ISA_MIPS1);
19726 }
19727
19728 /* 'default' has traditionally been a no-op. Probably not very useful. */
19729 if (strcasecmp (cpu_string, "default") == 0)
19730 return 0;
19731
19732 for (p = mips_cpu_info_table; p->name != 0; p++)
19733 if (mips_matching_cpu_name_p (p->name, cpu_string))
19734 return p;
19735
19736 as_bad (_("bad value (%s) for %s"), cpu_string, option);
19737 return 0;
19738 }
19739
19740 /* Return the canonical processor information for ISA (a member of the
19741 ISA_MIPS* enumeration). */
19742
19743 static const struct mips_cpu_info *
19744 mips_cpu_info_from_isa (int isa)
19745 {
19746 int i;
19747
19748 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19749 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
19750 && isa == mips_cpu_info_table[i].isa)
19751 return (&mips_cpu_info_table[i]);
19752
19753 return NULL;
19754 }
19755
19756 static const struct mips_cpu_info *
19757 mips_cpu_info_from_arch (int arch)
19758 {
19759 int i;
19760
19761 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19762 if (arch == mips_cpu_info_table[i].cpu)
19763 return (&mips_cpu_info_table[i]);
19764
19765 return NULL;
19766 }
19767 \f
19768 static void
19769 show (FILE *stream, const char *string, int *col_p, int *first_p)
19770 {
19771 if (*first_p)
19772 {
19773 fprintf (stream, "%24s", "");
19774 *col_p = 24;
19775 }
19776 else
19777 {
19778 fprintf (stream, ", ");
19779 *col_p += 2;
19780 }
19781
19782 if (*col_p + strlen (string) > 72)
19783 {
19784 fprintf (stream, "\n%24s", "");
19785 *col_p = 24;
19786 }
19787
19788 fprintf (stream, "%s", string);
19789 *col_p += strlen (string);
19790
19791 *first_p = 0;
19792 }
19793
19794 void
19795 md_show_usage (FILE *stream)
19796 {
19797 int column, first;
19798 size_t i;
19799
19800 fprintf (stream, _("\
19801 MIPS options:\n\
19802 -EB generate big endian output\n\
19803 -EL generate little endian output\n\
19804 -g, -g2 do not remove unneeded NOPs or swap branches\n\
19805 -G NUM allow referencing objects up to NUM bytes\n\
19806 implicitly with the gp register [default 8]\n"));
19807 fprintf (stream, _("\
19808 -mips1 generate MIPS ISA I instructions\n\
19809 -mips2 generate MIPS ISA II instructions\n\
19810 -mips3 generate MIPS ISA III instructions\n\
19811 -mips4 generate MIPS ISA IV instructions\n\
19812 -mips5 generate MIPS ISA V instructions\n\
19813 -mips32 generate MIPS32 ISA instructions\n\
19814 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
19815 -mips32r3 generate MIPS32 release 3 ISA instructions\n\
19816 -mips32r5 generate MIPS32 release 5 ISA instructions\n\
19817 -mips32r6 generate MIPS32 release 6 ISA instructions\n\
19818 -mips64 generate MIPS64 ISA instructions\n\
19819 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
19820 -mips64r3 generate MIPS64 release 3 ISA instructions\n\
19821 -mips64r5 generate MIPS64 release 5 ISA instructions\n\
19822 -mips64r6 generate MIPS64 release 6 ISA instructions\n\
19823 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19824
19825 first = 1;
19826
19827 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19828 show (stream, mips_cpu_info_table[i].name, &column, &first);
19829 show (stream, "from-abi", &column, &first);
19830 fputc ('\n', stream);
19831
19832 fprintf (stream, _("\
19833 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19834 -no-mCPU don't generate code specific to CPU.\n\
19835 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19836
19837 first = 1;
19838
19839 show (stream, "3900", &column, &first);
19840 show (stream, "4010", &column, &first);
19841 show (stream, "4100", &column, &first);
19842 show (stream, "4650", &column, &first);
19843 fputc ('\n', stream);
19844
19845 fprintf (stream, _("\
19846 -mips16 generate mips16 instructions\n\
19847 -no-mips16 do not generate mips16 instructions\n"));
19848 fprintf (stream, _("\
19849 -mmicromips generate microMIPS instructions\n\
19850 -mno-micromips do not generate microMIPS instructions\n"));
19851 fprintf (stream, _("\
19852 -msmartmips generate smartmips instructions\n\
19853 -mno-smartmips do not generate smartmips instructions\n"));
19854 fprintf (stream, _("\
19855 -mdsp generate DSP instructions\n\
19856 -mno-dsp do not generate DSP instructions\n"));
19857 fprintf (stream, _("\
19858 -mdspr2 generate DSP R2 instructions\n\
19859 -mno-dspr2 do not generate DSP R2 instructions\n"));
19860 fprintf (stream, _("\
19861 -mdspr3 generate DSP R3 instructions\n\
19862 -mno-dspr3 do not generate DSP R3 instructions\n"));
19863 fprintf (stream, _("\
19864 -mmt generate MT instructions\n\
19865 -mno-mt do not generate MT instructions\n"));
19866 fprintf (stream, _("\
19867 -mmcu generate MCU instructions\n\
19868 -mno-mcu do not generate MCU instructions\n"));
19869 fprintf (stream, _("\
19870 -mmsa generate MSA instructions\n\
19871 -mno-msa do not generate MSA instructions\n"));
19872 fprintf (stream, _("\
19873 -mxpa generate eXtended Physical Address (XPA) instructions\n\
19874 -mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
19875 fprintf (stream, _("\
19876 -mvirt generate Virtualization instructions\n\
19877 -mno-virt do not generate Virtualization instructions\n"));
19878 fprintf (stream, _("\
19879 -minsn32 only generate 32-bit microMIPS instructions\n\
19880 -mno-insn32 generate all microMIPS instructions\n"));
19881 fprintf (stream, _("\
19882 -mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19883 -mfix-loongson2f-nop work around Loongson2F NOP errata\n\
19884 -mfix-vr4120 work around certain VR4120 errata\n\
19885 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
19886 -mfix-24k insert a nop after ERET and DERET instructions\n\
19887 -mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
19888 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19889 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
19890 -msym32 assume all symbols have 32-bit values\n\
19891 -O0 remove unneeded NOPs, do not swap branches\n\
19892 -O remove unneeded NOPs and swap branches\n\
19893 --trap, --no-break trap exception on div by 0 and mult overflow\n\
19894 --break, --no-trap break exception on div by 0 and mult overflow\n"));
19895 fprintf (stream, _("\
19896 -mhard-float allow floating-point instructions\n\
19897 -msoft-float do not allow floating-point instructions\n\
19898 -msingle-float only allow 32-bit floating-point operations\n\
19899 -mdouble-float allow 32-bit and 64-bit floating-point operations\n\
19900 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
19901 --[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
19902 -mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
19903 -mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
19904 -mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
19905
19906 first = 1;
19907
19908 show (stream, "legacy", &column, &first);
19909 show (stream, "2008", &column, &first);
19910
19911 fputc ('\n', stream);
19912
19913 fprintf (stream, _("\
19914 -KPIC, -call_shared generate SVR4 position independent code\n\
19915 -call_nonpic generate non-PIC code that can operate with DSOs\n\
19916 -mvxworks-pic generate VxWorks position independent code\n\
19917 -non_shared do not generate code that can operate with DSOs\n\
19918 -xgot assume a 32 bit GOT\n\
19919 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
19920 -mshared, -mno-shared disable/enable .cpload optimization for\n\
19921 position dependent (non shared) code\n\
19922 -mabi=ABI create ABI conformant object file for:\n"));
19923
19924 first = 1;
19925
19926 show (stream, "32", &column, &first);
19927 show (stream, "o64", &column, &first);
19928 show (stream, "n32", &column, &first);
19929 show (stream, "64", &column, &first);
19930 show (stream, "eabi", &column, &first);
19931
19932 fputc ('\n', stream);
19933
19934 fprintf (stream, _("\
19935 -32 create o32 ABI object file (default)\n\
19936 -n32 create n32 ABI object file\n\
19937 -64 create 64 ABI object file\n"));
19938 }
19939
19940 #ifdef TE_IRIX
19941 enum dwarf2_format
19942 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
19943 {
19944 if (HAVE_64BIT_SYMBOLS)
19945 return dwarf2_format_64bit_irix;
19946 else
19947 return dwarf2_format_32bit;
19948 }
19949 #endif
19950
19951 int
19952 mips_dwarf2_addr_size (void)
19953 {
19954 if (HAVE_64BIT_OBJECTS)
19955 return 8;
19956 else
19957 return 4;
19958 }
19959
19960 /* Standard calling conventions leave the CFA at SP on entry. */
19961 void
19962 mips_cfi_frame_initial_instructions (void)
19963 {
19964 cfi_add_CFA_def_cfa_register (SP);
19965 }
19966
19967 int
19968 tc_mips_regname_to_dw2regnum (char *regname)
19969 {
19970 unsigned int regnum = -1;
19971 unsigned int reg;
19972
19973 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19974 regnum = reg;
19975
19976 return regnum;
19977 }
19978
19979 /* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
19980 Given a symbolic attribute NAME, return the proper integer value.
19981 Returns -1 if the attribute is not known. */
19982
19983 int
19984 mips_convert_symbolic_attribute (const char *name)
19985 {
19986 static const struct
19987 {
19988 const char * name;
19989 const int tag;
19990 }
19991 attribute_table[] =
19992 {
19993 #define T(tag) {#tag, tag}
19994 T (Tag_GNU_MIPS_ABI_FP),
19995 T (Tag_GNU_MIPS_ABI_MSA),
19996 #undef T
19997 };
19998 unsigned int i;
19999
20000 if (name == NULL)
20001 return -1;
20002
20003 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20004 if (streq (name, attribute_table[i].name))
20005 return attribute_table[i].tag;
20006
20007 return -1;
20008 }
20009
20010 void
20011 md_mips_end (void)
20012 {
20013 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20014
20015 mips_emit_delays ();
20016 if (cur_proc_ptr)
20017 as_warn (_("missing .end at end of assembly"));
20018
20019 /* Just in case no code was emitted, do the consistency check. */
20020 file_mips_check_options ();
20021
20022 /* Set a floating-point ABI if the user did not. */
20023 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20024 {
20025 /* Perform consistency checks on the floating-point ABI. */
20026 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20027 Tag_GNU_MIPS_ABI_FP);
20028 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20029 check_fpabi (fpabi);
20030 }
20031 else
20032 {
20033 /* Soft-float gets precedence over single-float, the two options should
20034 not be used together so this should not matter. */
20035 if (file_mips_opts.soft_float == 1)
20036 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20037 /* Single-float gets precedence over all double_float cases. */
20038 else if (file_mips_opts.single_float == 1)
20039 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20040 else
20041 {
20042 switch (file_mips_opts.fp)
20043 {
20044 case 32:
20045 if (file_mips_opts.gp == 32)
20046 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20047 break;
20048 case 0:
20049 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20050 break;
20051 case 64:
20052 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20053 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20054 else if (file_mips_opts.gp == 32)
20055 fpabi = Val_GNU_MIPS_ABI_FP_64;
20056 else
20057 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20058 break;
20059 }
20060 }
20061
20062 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20063 Tag_GNU_MIPS_ABI_FP, fpabi);
20064 }
20065 }
20066
20067 /* Returns the relocation type required for a particular CFI encoding. */
20068
20069 bfd_reloc_code_real_type
20070 mips_cfi_reloc_for_encoding (int encoding)
20071 {
20072 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20073 return BFD_RELOC_32_PCREL;
20074 else return BFD_RELOC_NONE;
20075 }
This page took 0.641476 seconds and 5 git commands to generate.