]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/tc-mips.c
include/opcode/
[thirdparty/binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
4 Free Software Foundation, Inc.
5 Contributed by the OSF and Ralph Campbell.
6 Written by Keith Knowles and Ralph Campbell, working independently.
7 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
8 Support.
9
10 This file is part of GAS.
11
12 GAS is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
16
17 GAS is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GAS; see the file COPYING. If not, write to the Free
24 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
25 02110-1301, USA. */
26
27 #include "as.h"
28 #include "config.h"
29 #include "subsegs.h"
30 #include "safe-ctype.h"
31
32 #include "opcode/mips.h"
33 #include "itbl-ops.h"
34 #include "dwarf2dbg.h"
35 #include "dw2gencfi.h"
36
37 /* Check assumptions made in this file. */
38 typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
39 typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
40
41 #ifdef DEBUG
42 #define DBG(x) printf x
43 #else
44 #define DBG(x)
45 #endif
46
47 /* Clean up namespace so we can include obj-elf.h too. */
48 static int mips_output_flavor (void);
49 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
50 #undef OBJ_PROCESS_STAB
51 #undef OUTPUT_FLAVOR
52 #undef S_GET_ALIGN
53 #undef S_GET_SIZE
54 #undef S_SET_ALIGN
55 #undef S_SET_SIZE
56 #undef obj_frob_file
57 #undef obj_frob_file_after_relocs
58 #undef obj_frob_symbol
59 #undef obj_pop_insert
60 #undef obj_sec_sym_ok_for_reloc
61 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
62
63 #include "obj-elf.h"
64 /* Fix any of them that we actually care about. */
65 #undef OUTPUT_FLAVOR
66 #define OUTPUT_FLAVOR mips_output_flavor()
67
68 #include "elf/mips.h"
69
70 #ifndef ECOFF_DEBUGGING
71 #define NO_ECOFF_DEBUGGING
72 #define ECOFF_DEBUGGING 0
73 #endif
74
75 int mips_flag_mdebug = -1;
76
77 /* Control generation of .pdr sections. Off by default on IRIX: the native
78 linker doesn't know about and discards them, but relocations against them
79 remain, leading to rld crashes. */
80 #ifdef TE_IRIX
81 int mips_flag_pdr = FALSE;
82 #else
83 int mips_flag_pdr = TRUE;
84 #endif
85
86 #include "ecoff.h"
87
88 static char *mips_regmask_frag;
89
90 #define ZERO 0
91 #define ATREG 1
92 #define S0 16
93 #define S7 23
94 #define TREG 24
95 #define PIC_CALL_REG 25
96 #define KT0 26
97 #define KT1 27
98 #define GP 28
99 #define SP 29
100 #define FP 30
101 #define RA 31
102
103 #define ILLEGAL_REG (32)
104
105 #define AT mips_opts.at
106
107 extern int target_big_endian;
108
109 /* The name of the readonly data section. */
110 #define RDATA_SECTION_NAME ".rodata"
111
112 /* Ways in which an instruction can be "appended" to the output. */
113 enum append_method {
114 /* Just add it normally. */
115 APPEND_ADD,
116
117 /* Add it normally and then add a nop. */
118 APPEND_ADD_WITH_NOP,
119
120 /* Turn an instruction with a delay slot into a "compact" version. */
121 APPEND_ADD_COMPACT,
122
123 /* Insert the instruction before the last one. */
124 APPEND_SWAP
125 };
126
127 /* Information about an instruction, including its format, operands
128 and fixups. */
129 struct mips_cl_insn
130 {
131 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
132 const struct mips_opcode *insn_mo;
133
134 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
135 a copy of INSN_MO->match with the operands filled in. If we have
136 decided to use an extended MIPS16 instruction, this includes the
137 extension. */
138 unsigned long insn_opcode;
139
140 /* The frag that contains the instruction. */
141 struct frag *frag;
142
143 /* The offset into FRAG of the first instruction byte. */
144 long where;
145
146 /* The relocs associated with the instruction, if any. */
147 fixS *fixp[3];
148
149 /* True if this entry cannot be moved from its current position. */
150 unsigned int fixed_p : 1;
151
152 /* True if this instruction occurred in a .set noreorder block. */
153 unsigned int noreorder_p : 1;
154
155 /* True for mips16 instructions that jump to an absolute address. */
156 unsigned int mips16_absolute_jump_p : 1;
157
158 /* True if this instruction is complete. */
159 unsigned int complete_p : 1;
160
161 /* True if this instruction is cleared from history by unconditional
162 branch. */
163 unsigned int cleared_p : 1;
164 };
165
166 /* The ABI to use. */
167 enum mips_abi_level
168 {
169 NO_ABI = 0,
170 O32_ABI,
171 O64_ABI,
172 N32_ABI,
173 N64_ABI,
174 EABI_ABI
175 };
176
177 /* MIPS ABI we are using for this output file. */
178 static enum mips_abi_level mips_abi = NO_ABI;
179
180 /* Whether or not we have code that can call pic code. */
181 int mips_abicalls = FALSE;
182
183 /* Whether or not we have code which can be put into a shared
184 library. */
185 static bfd_boolean mips_in_shared = TRUE;
186
187 /* This is the set of options which may be modified by the .set
188 pseudo-op. We use a struct so that .set push and .set pop are more
189 reliable. */
190
191 struct mips_set_options
192 {
193 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
194 if it has not been initialized. Changed by `.set mipsN', and the
195 -mipsN command line option, and the default CPU. */
196 int isa;
197 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
198 <asename>', by command line options, and based on the default
199 architecture. */
200 int ase;
201 /* Whether we are assembling for the mips16 processor. 0 if we are
202 not, 1 if we are, and -1 if the value has not been initialized.
203 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
204 -nomips16 command line options, and the default CPU. */
205 int mips16;
206 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
207 1 if we are, and -1 if the value has not been initialized. Changed
208 by `.set micromips' and `.set nomicromips', and the -mmicromips
209 and -mno-micromips command line options, and the default CPU. */
210 int micromips;
211 /* Non-zero if we should not reorder instructions. Changed by `.set
212 reorder' and `.set noreorder'. */
213 int noreorder;
214 /* Non-zero if we should not permit the register designated "assembler
215 temporary" to be used in instructions. The value is the register
216 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
217 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
218 unsigned int at;
219 /* Non-zero if we should warn when a macro instruction expands into
220 more than one machine instruction. Changed by `.set nomacro' and
221 `.set macro'. */
222 int warn_about_macros;
223 /* Non-zero if we should not move instructions. Changed by `.set
224 move', `.set volatile', `.set nomove', and `.set novolatile'. */
225 int nomove;
226 /* Non-zero if we should not optimize branches by moving the target
227 of the branch into the delay slot. Actually, we don't perform
228 this optimization anyhow. Changed by `.set bopt' and `.set
229 nobopt'. */
230 int nobopt;
231 /* Non-zero if we should not autoextend mips16 instructions.
232 Changed by `.set autoextend' and `.set noautoextend'. */
233 int noautoextend;
234 /* True if we should only emit 32-bit microMIPS instructions.
235 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
236 and -mno-insn32 command line options. */
237 bfd_boolean insn32;
238 /* Restrict general purpose registers and floating point registers
239 to 32 bit. This is initially determined when -mgp32 or -mfp32
240 is passed but can changed if the assembler code uses .set mipsN. */
241 int gp32;
242 int fp32;
243 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
244 command line option, and the default CPU. */
245 int arch;
246 /* True if ".set sym32" is in effect. */
247 bfd_boolean sym32;
248 /* True if floating-point operations are not allowed. Changed by .set
249 softfloat or .set hardfloat, by command line options -msoft-float or
250 -mhard-float. The default is false. */
251 bfd_boolean soft_float;
252
253 /* True if only single-precision floating-point operations are allowed.
254 Changed by .set singlefloat or .set doublefloat, command-line options
255 -msingle-float or -mdouble-float. The default is false. */
256 bfd_boolean single_float;
257 };
258
259 /* This is the struct we use to hold the current set of options. Note
260 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
261 -1 to indicate that they have not been initialized. */
262
263 /* True if -mgp32 was passed. */
264 static int file_mips_gp32 = -1;
265
266 /* True if -mfp32 was passed. */
267 static int file_mips_fp32 = -1;
268
269 /* 1 if -msoft-float, 0 if -mhard-float. The default is 0. */
270 static int file_mips_soft_float = 0;
271
272 /* 1 if -msingle-float, 0 if -mdouble-float. The default is 0. */
273 static int file_mips_single_float = 0;
274
275 static struct mips_set_options mips_opts =
276 {
277 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
278 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
279 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
280 /* gp32 */ 0, /* fp32 */ 0, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
281 /* soft_float */ FALSE, /* single_float */ FALSE
282 };
283
284 /* The set of ASEs that were selected on the command line, either
285 explicitly via ASE options or implicitly through things like -march. */
286 static unsigned int file_ase;
287
288 /* Which bits of file_ase were explicitly set or cleared by ASE options. */
289 static unsigned int file_ase_explicit;
290
291 /* These variables are filled in with the masks of registers used.
292 The object format code reads them and puts them in the appropriate
293 place. */
294 unsigned long mips_gprmask;
295 unsigned long mips_cprmask[4];
296
297 /* MIPS ISA we are using for this output file. */
298 static int file_mips_isa = ISA_UNKNOWN;
299
300 /* True if any MIPS16 code was produced. */
301 static int file_ase_mips16;
302
303 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
304 || mips_opts.isa == ISA_MIPS32R2 \
305 || mips_opts.isa == ISA_MIPS64 \
306 || mips_opts.isa == ISA_MIPS64R2)
307
308 /* True if any microMIPS code was produced. */
309 static int file_ase_micromips;
310
311 /* True if we want to create R_MIPS_JALR for jalr $25. */
312 #ifdef TE_IRIX
313 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
314 #else
315 /* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
316 because there's no place for any addend, the only acceptable
317 expression is a bare symbol. */
318 #define MIPS_JALR_HINT_P(EXPR) \
319 (!HAVE_IN_PLACE_ADDENDS \
320 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
321 #endif
322
323 /* The argument of the -march= flag. The architecture we are assembling. */
324 static int file_mips_arch = CPU_UNKNOWN;
325 static const char *mips_arch_string;
326
327 /* The argument of the -mtune= flag. The architecture for which we
328 are optimizing. */
329 static int mips_tune = CPU_UNKNOWN;
330 static const char *mips_tune_string;
331
332 /* True when generating 32-bit code for a 64-bit processor. */
333 static int mips_32bitmode = 0;
334
335 /* True if the given ABI requires 32-bit registers. */
336 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
337
338 /* Likewise 64-bit registers. */
339 #define ABI_NEEDS_64BIT_REGS(ABI) \
340 ((ABI) == N32_ABI \
341 || (ABI) == N64_ABI \
342 || (ABI) == O64_ABI)
343
344 /* Return true if ISA supports 64 bit wide gp registers. */
345 #define ISA_HAS_64BIT_REGS(ISA) \
346 ((ISA) == ISA_MIPS3 \
347 || (ISA) == ISA_MIPS4 \
348 || (ISA) == ISA_MIPS5 \
349 || (ISA) == ISA_MIPS64 \
350 || (ISA) == ISA_MIPS64R2)
351
352 /* Return true if ISA supports 64 bit wide float registers. */
353 #define ISA_HAS_64BIT_FPRS(ISA) \
354 ((ISA) == ISA_MIPS3 \
355 || (ISA) == ISA_MIPS4 \
356 || (ISA) == ISA_MIPS5 \
357 || (ISA) == ISA_MIPS32R2 \
358 || (ISA) == ISA_MIPS64 \
359 || (ISA) == ISA_MIPS64R2)
360
361 /* Return true if ISA supports 64-bit right rotate (dror et al.)
362 instructions. */
363 #define ISA_HAS_DROR(ISA) \
364 ((ISA) == ISA_MIPS64R2 \
365 || (mips_opts.micromips \
366 && ISA_HAS_64BIT_REGS (ISA)) \
367 )
368
369 /* Return true if ISA supports 32-bit right rotate (ror et al.)
370 instructions. */
371 #define ISA_HAS_ROR(ISA) \
372 ((ISA) == ISA_MIPS32R2 \
373 || (ISA) == ISA_MIPS64R2 \
374 || (mips_opts.ase & ASE_SMARTMIPS) \
375 || mips_opts.micromips \
376 )
377
378 /* Return true if ISA supports single-precision floats in odd registers. */
379 #define ISA_HAS_ODD_SINGLE_FPR(ISA) \
380 ((ISA) == ISA_MIPS32 \
381 || (ISA) == ISA_MIPS32R2 \
382 || (ISA) == ISA_MIPS64 \
383 || (ISA) == ISA_MIPS64R2)
384
385 /* Return true if ISA supports move to/from high part of a 64-bit
386 floating-point register. */
387 #define ISA_HAS_MXHC1(ISA) \
388 ((ISA) == ISA_MIPS32R2 \
389 || (ISA) == ISA_MIPS64R2)
390
391 #define HAVE_32BIT_GPRS \
392 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
393
394 #define HAVE_32BIT_FPRS \
395 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
396
397 #define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
398 #define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
399
400 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
401
402 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
403
404 /* True if relocations are stored in-place. */
405 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
406
407 /* The ABI-derived address size. */
408 #define HAVE_64BIT_ADDRESSES \
409 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
410 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
411
412 /* The size of symbolic constants (i.e., expressions of the form
413 "SYMBOL" or "SYMBOL + OFFSET"). */
414 #define HAVE_32BIT_SYMBOLS \
415 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
416 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
417
418 /* Addresses are loaded in different ways, depending on the address size
419 in use. The n32 ABI Documentation also mandates the use of additions
420 with overflow checking, but existing implementations don't follow it. */
421 #define ADDRESS_ADD_INSN \
422 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
423
424 #define ADDRESS_ADDI_INSN \
425 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
426
427 #define ADDRESS_LOAD_INSN \
428 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
429
430 #define ADDRESS_STORE_INSN \
431 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
432
433 /* Return true if the given CPU supports the MIPS16 ASE. */
434 #define CPU_HAS_MIPS16(cpu) \
435 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
436 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
437
438 /* Return true if the given CPU supports the microMIPS ASE. */
439 #define CPU_HAS_MICROMIPS(cpu) 0
440
441 /* True if CPU has a dror instruction. */
442 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
443
444 /* True if CPU has a ror instruction. */
445 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
446
447 /* True if CPU is in the Octeon family */
448 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
449
450 /* True if CPU has seq/sne and seqi/snei instructions. */
451 #define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
452
453 /* True, if CPU has support for ldc1 and sdc1. */
454 #define CPU_HAS_LDC1_SDC1(CPU) \
455 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
456
457 /* True if mflo and mfhi can be immediately followed by instructions
458 which write to the HI and LO registers.
459
460 According to MIPS specifications, MIPS ISAs I, II, and III need
461 (at least) two instructions between the reads of HI/LO and
462 instructions which write them, and later ISAs do not. Contradicting
463 the MIPS specifications, some MIPS IV processor user manuals (e.g.
464 the UM for the NEC Vr5000) document needing the instructions between
465 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
466 MIPS64 and later ISAs to have the interlocks, plus any specific
467 earlier-ISA CPUs for which CPU documentation declares that the
468 instructions are really interlocked. */
469 #define hilo_interlocks \
470 (mips_opts.isa == ISA_MIPS32 \
471 || mips_opts.isa == ISA_MIPS32R2 \
472 || mips_opts.isa == ISA_MIPS64 \
473 || mips_opts.isa == ISA_MIPS64R2 \
474 || mips_opts.arch == CPU_R4010 \
475 || mips_opts.arch == CPU_R5900 \
476 || mips_opts.arch == CPU_R10000 \
477 || mips_opts.arch == CPU_R12000 \
478 || mips_opts.arch == CPU_R14000 \
479 || mips_opts.arch == CPU_R16000 \
480 || mips_opts.arch == CPU_RM7000 \
481 || mips_opts.arch == CPU_VR5500 \
482 || mips_opts.micromips \
483 )
484
485 /* Whether the processor uses hardware interlocks to protect reads
486 from the GPRs after they are loaded from memory, and thus does not
487 require nops to be inserted. This applies to instructions marked
488 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
489 level I and microMIPS mode instructions are always interlocked. */
490 #define gpr_interlocks \
491 (mips_opts.isa != ISA_MIPS1 \
492 || mips_opts.arch == CPU_R3900 \
493 || mips_opts.arch == CPU_R5900 \
494 || mips_opts.micromips \
495 )
496
497 /* Whether the processor uses hardware interlocks to avoid delays
498 required by coprocessor instructions, and thus does not require
499 nops to be inserted. This applies to instructions marked
500 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
501 between instructions marked INSN_WRITE_COND_CODE and ones marked
502 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
503 levels I, II, and III and microMIPS mode instructions are always
504 interlocked. */
505 /* Itbl support may require additional care here. */
506 #define cop_interlocks \
507 ((mips_opts.isa != ISA_MIPS1 \
508 && mips_opts.isa != ISA_MIPS2 \
509 && mips_opts.isa != ISA_MIPS3) \
510 || mips_opts.arch == CPU_R4300 \
511 || mips_opts.micromips \
512 )
513
514 /* Whether the processor uses hardware interlocks to protect reads
515 from coprocessor registers after they are loaded from memory, and
516 thus does not require nops to be inserted. This applies to
517 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
518 requires at MIPS ISA level I and microMIPS mode instructions are
519 always interlocked. */
520 #define cop_mem_interlocks \
521 (mips_opts.isa != ISA_MIPS1 \
522 || mips_opts.micromips \
523 )
524
525 /* Is this a mfhi or mflo instruction? */
526 #define MF_HILO_INSN(PINFO) \
527 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
528
529 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
530 has been selected. This implies, in particular, that addresses of text
531 labels have their LSB set. */
532 #define HAVE_CODE_COMPRESSION \
533 ((mips_opts.mips16 | mips_opts.micromips) != 0)
534
535 /* The minimum and maximum signed values that can be stored in a GPR. */
536 #define GPR_SMAX ((offsetT) (((valueT) 1 << (HAVE_64BIT_GPRS ? 63 : 31)) - 1))
537 #define GPR_SMIN (-GPR_SMAX - 1)
538
539 /* MIPS PIC level. */
540
541 enum mips_pic_level mips_pic;
542
543 /* 1 if we should generate 32 bit offsets from the $gp register in
544 SVR4_PIC mode. Currently has no meaning in other modes. */
545 static int mips_big_got = 0;
546
547 /* 1 if trap instructions should used for overflow rather than break
548 instructions. */
549 static int mips_trap = 0;
550
551 /* 1 if double width floating point constants should not be constructed
552 by assembling two single width halves into two single width floating
553 point registers which just happen to alias the double width destination
554 register. On some architectures this aliasing can be disabled by a bit
555 in the status register, and the setting of this bit cannot be determined
556 automatically at assemble time. */
557 static int mips_disable_float_construction;
558
559 /* Non-zero if any .set noreorder directives were used. */
560
561 static int mips_any_noreorder;
562
563 /* Non-zero if nops should be inserted when the register referenced in
564 an mfhi/mflo instruction is read in the next two instructions. */
565 static int mips_7000_hilo_fix;
566
567 /* The size of objects in the small data section. */
568 static unsigned int g_switch_value = 8;
569 /* Whether the -G option was used. */
570 static int g_switch_seen = 0;
571
572 #define N_RMASK 0xc4
573 #define N_VFP 0xd4
574
575 /* If we can determine in advance that GP optimization won't be
576 possible, we can skip the relaxation stuff that tries to produce
577 GP-relative references. This makes delay slot optimization work
578 better.
579
580 This function can only provide a guess, but it seems to work for
581 gcc output. It needs to guess right for gcc, otherwise gcc
582 will put what it thinks is a GP-relative instruction in a branch
583 delay slot.
584
585 I don't know if a fix is needed for the SVR4_PIC mode. I've only
586 fixed it for the non-PIC mode. KR 95/04/07 */
587 static int nopic_need_relax (symbolS *, int);
588
589 /* handle of the OPCODE hash table */
590 static struct hash_control *op_hash = NULL;
591
592 /* The opcode hash table we use for the mips16. */
593 static struct hash_control *mips16_op_hash = NULL;
594
595 /* The opcode hash table we use for the microMIPS ASE. */
596 static struct hash_control *micromips_op_hash = NULL;
597
598 /* This array holds the chars that always start a comment. If the
599 pre-processor is disabled, these aren't very useful */
600 const char comment_chars[] = "#";
601
602 /* This array holds the chars that only start a comment at the beginning of
603 a line. If the line seems to have the form '# 123 filename'
604 .line and .file directives will appear in the pre-processed output */
605 /* Note that input_file.c hand checks for '#' at the beginning of the
606 first line of the input file. This is because the compiler outputs
607 #NO_APP at the beginning of its output. */
608 /* Also note that C style comments are always supported. */
609 const char line_comment_chars[] = "#";
610
611 /* This array holds machine specific line separator characters. */
612 const char line_separator_chars[] = ";";
613
614 /* Chars that can be used to separate mant from exp in floating point nums */
615 const char EXP_CHARS[] = "eE";
616
617 /* Chars that mean this number is a floating point constant */
618 /* As in 0f12.456 */
619 /* or 0d1.2345e12 */
620 const char FLT_CHARS[] = "rRsSfFdDxXpP";
621
622 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
623 changed in read.c . Ideally it shouldn't have to know about it at all,
624 but nothing is ideal around here.
625 */
626
627 static char *insn_error;
628
629 static int auto_align = 1;
630
631 /* When outputting SVR4 PIC code, the assembler needs to know the
632 offset in the stack frame from which to restore the $gp register.
633 This is set by the .cprestore pseudo-op, and saved in this
634 variable. */
635 static offsetT mips_cprestore_offset = -1;
636
637 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
638 more optimizations, it can use a register value instead of a memory-saved
639 offset and even an other register than $gp as global pointer. */
640 static offsetT mips_cpreturn_offset = -1;
641 static int mips_cpreturn_register = -1;
642 static int mips_gp_register = GP;
643 static int mips_gprel_offset = 0;
644
645 /* Whether mips_cprestore_offset has been set in the current function
646 (or whether it has already been warned about, if not). */
647 static int mips_cprestore_valid = 0;
648
649 /* This is the register which holds the stack frame, as set by the
650 .frame pseudo-op. This is needed to implement .cprestore. */
651 static int mips_frame_reg = SP;
652
653 /* Whether mips_frame_reg has been set in the current function
654 (or whether it has already been warned about, if not). */
655 static int mips_frame_reg_valid = 0;
656
657 /* To output NOP instructions correctly, we need to keep information
658 about the previous two instructions. */
659
660 /* Whether we are optimizing. The default value of 2 means to remove
661 unneeded NOPs and swap branch instructions when possible. A value
662 of 1 means to not swap branches. A value of 0 means to always
663 insert NOPs. */
664 static int mips_optimize = 2;
665
666 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
667 equivalent to seeing no -g option at all. */
668 static int mips_debug = 0;
669
670 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
671 #define MAX_VR4130_NOPS 4
672
673 /* The maximum number of NOPs needed to fill delay slots. */
674 #define MAX_DELAY_NOPS 2
675
676 /* The maximum number of NOPs needed for any purpose. */
677 #define MAX_NOPS 4
678
679 /* A list of previous instructions, with index 0 being the most recent.
680 We need to look back MAX_NOPS instructions when filling delay slots
681 or working around processor errata. We need to look back one
682 instruction further if we're thinking about using history[0] to
683 fill a branch delay slot. */
684 static struct mips_cl_insn history[1 + MAX_NOPS];
685
686 /* Nop instructions used by emit_nop. */
687 static struct mips_cl_insn nop_insn;
688 static struct mips_cl_insn mips16_nop_insn;
689 static struct mips_cl_insn micromips_nop16_insn;
690 static struct mips_cl_insn micromips_nop32_insn;
691
692 /* The appropriate nop for the current mode. */
693 #define NOP_INSN (mips_opts.mips16 \
694 ? &mips16_nop_insn \
695 : (mips_opts.micromips \
696 ? (mips_opts.insn32 \
697 ? &micromips_nop32_insn \
698 : &micromips_nop16_insn) \
699 : &nop_insn))
700
701 /* The size of NOP_INSN in bytes. */
702 #define NOP_INSN_SIZE ((mips_opts.mips16 \
703 || (mips_opts.micromips && !mips_opts.insn32)) \
704 ? 2 : 4)
705
706 /* If this is set, it points to a frag holding nop instructions which
707 were inserted before the start of a noreorder section. If those
708 nops turn out to be unnecessary, the size of the frag can be
709 decreased. */
710 static fragS *prev_nop_frag;
711
712 /* The number of nop instructions we created in prev_nop_frag. */
713 static int prev_nop_frag_holds;
714
715 /* The number of nop instructions that we know we need in
716 prev_nop_frag. */
717 static int prev_nop_frag_required;
718
719 /* The number of instructions we've seen since prev_nop_frag. */
720 static int prev_nop_frag_since;
721
722 /* Relocations against symbols are sometimes done in two parts, with a HI
723 relocation and a LO relocation. Each relocation has only 16 bits of
724 space to store an addend. This means that in order for the linker to
725 handle carries correctly, it must be able to locate both the HI and
726 the LO relocation. This means that the relocations must appear in
727 order in the relocation table.
728
729 In order to implement this, we keep track of each unmatched HI
730 relocation. We then sort them so that they immediately precede the
731 corresponding LO relocation. */
732
733 struct mips_hi_fixup
734 {
735 /* Next HI fixup. */
736 struct mips_hi_fixup *next;
737 /* This fixup. */
738 fixS *fixp;
739 /* The section this fixup is in. */
740 segT seg;
741 };
742
743 /* The list of unmatched HI relocs. */
744
745 static struct mips_hi_fixup *mips_hi_fixup_list;
746
747 /* The frag containing the last explicit relocation operator.
748 Null if explicit relocations have not been used. */
749
750 static fragS *prev_reloc_op_frag;
751
752 /* Map normal MIPS register numbers to mips16 register numbers. */
753
754 #define X ILLEGAL_REG
755 static const int mips32_to_16_reg_map[] =
756 {
757 X, X, 2, 3, 4, 5, 6, 7,
758 X, X, X, X, X, X, X, X,
759 0, 1, X, X, X, X, X, X,
760 X, X, X, X, X, X, X, X
761 };
762 #undef X
763
764 /* Map mips16 register numbers to normal MIPS register numbers. */
765
766 static const unsigned int mips16_to_32_reg_map[] =
767 {
768 16, 17, 2, 3, 4, 5, 6, 7
769 };
770
771 /* Map normal MIPS register numbers to microMIPS register numbers. */
772
773 #define mips32_to_micromips_reg_b_map mips32_to_16_reg_map
774 #define mips32_to_micromips_reg_c_map mips32_to_16_reg_map
775 #define mips32_to_micromips_reg_d_map mips32_to_16_reg_map
776 #define mips32_to_micromips_reg_e_map mips32_to_16_reg_map
777 #define mips32_to_micromips_reg_f_map mips32_to_16_reg_map
778 #define mips32_to_micromips_reg_g_map mips32_to_16_reg_map
779 #define mips32_to_micromips_reg_l_map mips32_to_16_reg_map
780
781 #define X ILLEGAL_REG
782 /* reg type h: 4, 5, 6. */
783 static const int mips32_to_micromips_reg_h_map[] =
784 {
785 X, X, X, X, 4, 5, 6, X,
786 X, X, X, X, X, X, X, X,
787 X, X, X, X, X, X, X, X,
788 X, X, X, X, X, X, X, X
789 };
790
791 /* reg type m: 0, 17, 2, 3, 16, 18, 19, 20. */
792 static const int mips32_to_micromips_reg_m_map[] =
793 {
794 0, X, 2, 3, X, X, X, X,
795 X, X, X, X, X, X, X, X,
796 4, 1, 5, 6, 7, X, X, X,
797 X, X, X, X, X, X, X, X
798 };
799
800 /* reg type q: 0, 2-7. 17. */
801 static const int mips32_to_micromips_reg_q_map[] =
802 {
803 0, X, 2, 3, 4, 5, 6, 7,
804 X, X, X, X, X, X, X, X,
805 X, 1, X, X, X, X, X, X,
806 X, X, X, X, X, X, X, X
807 };
808
809 #define mips32_to_micromips_reg_n_map mips32_to_micromips_reg_m_map
810 #undef X
811
812 /* Map microMIPS register numbers to normal MIPS register numbers. */
813
814 #define micromips_to_32_reg_b_map mips16_to_32_reg_map
815 #define micromips_to_32_reg_c_map mips16_to_32_reg_map
816 #define micromips_to_32_reg_d_map mips16_to_32_reg_map
817 #define micromips_to_32_reg_e_map mips16_to_32_reg_map
818 #define micromips_to_32_reg_f_map mips16_to_32_reg_map
819 #define micromips_to_32_reg_g_map mips16_to_32_reg_map
820
821 /* The microMIPS registers with type h. */
822 static const unsigned int micromips_to_32_reg_h_map[] =
823 {
824 5, 5, 6, 4, 4, 4, 4, 4
825 };
826
827 /* The microMIPS registers with type i. */
828 static const unsigned int micromips_to_32_reg_i_map[] =
829 {
830 6, 7, 7, 21, 22, 5, 6, 7
831 };
832
833 #define micromips_to_32_reg_l_map mips16_to_32_reg_map
834
835 /* The microMIPS registers with type m. */
836 static const unsigned int micromips_to_32_reg_m_map[] =
837 {
838 0, 17, 2, 3, 16, 18, 19, 20
839 };
840
841 #define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
842
843 /* The microMIPS registers with type q. */
844 static const unsigned int micromips_to_32_reg_q_map[] =
845 {
846 0, 17, 2, 3, 4, 5, 6, 7
847 };
848
849 /* microMIPS imm type B. */
850 static const int micromips_imm_b_map[] =
851 {
852 1, 4, 8, 12, 16, 20, 24, -1
853 };
854
855 /* microMIPS imm type C. */
856 static const int micromips_imm_c_map[] =
857 {
858 128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 255, 32768, 65535
859 };
860
861 /* Classifies the kind of instructions we're interested in when
862 implementing -mfix-vr4120. */
863 enum fix_vr4120_class
864 {
865 FIX_VR4120_MACC,
866 FIX_VR4120_DMACC,
867 FIX_VR4120_MULT,
868 FIX_VR4120_DMULT,
869 FIX_VR4120_DIV,
870 FIX_VR4120_MTHILO,
871 NUM_FIX_VR4120_CLASSES
872 };
873
874 /* ...likewise -mfix-loongson2f-jump. */
875 static bfd_boolean mips_fix_loongson2f_jump;
876
877 /* ...likewise -mfix-loongson2f-nop. */
878 static bfd_boolean mips_fix_loongson2f_nop;
879
880 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
881 static bfd_boolean mips_fix_loongson2f;
882
883 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
884 there must be at least one other instruction between an instruction
885 of type X and an instruction of type Y. */
886 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
887
888 /* True if -mfix-vr4120 is in force. */
889 static int mips_fix_vr4120;
890
891 /* ...likewise -mfix-vr4130. */
892 static int mips_fix_vr4130;
893
894 /* ...likewise -mfix-24k. */
895 static int mips_fix_24k;
896
897 /* ...likewise -mfix-cn63xxp1 */
898 static bfd_boolean mips_fix_cn63xxp1;
899
900 /* We don't relax branches by default, since this causes us to expand
901 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
902 fail to compute the offset before expanding the macro to the most
903 efficient expansion. */
904
905 static int mips_relax_branch;
906 \f
907 /* The expansion of many macros depends on the type of symbol that
908 they refer to. For example, when generating position-dependent code,
909 a macro that refers to a symbol may have two different expansions,
910 one which uses GP-relative addresses and one which uses absolute
911 addresses. When generating SVR4-style PIC, a macro may have
912 different expansions for local and global symbols.
913
914 We handle these situations by generating both sequences and putting
915 them in variant frags. In position-dependent code, the first sequence
916 will be the GP-relative one and the second sequence will be the
917 absolute one. In SVR4 PIC, the first sequence will be for global
918 symbols and the second will be for local symbols.
919
920 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
921 SECOND are the lengths of the two sequences in bytes. These fields
922 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
923 the subtype has the following flags:
924
925 RELAX_USE_SECOND
926 Set if it has been decided that we should use the second
927 sequence instead of the first.
928
929 RELAX_SECOND_LONGER
930 Set in the first variant frag if the macro's second implementation
931 is longer than its first. This refers to the macro as a whole,
932 not an individual relaxation.
933
934 RELAX_NOMACRO
935 Set in the first variant frag if the macro appeared in a .set nomacro
936 block and if one alternative requires a warning but the other does not.
937
938 RELAX_DELAY_SLOT
939 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
940 delay slot.
941
942 RELAX_DELAY_SLOT_16BIT
943 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
944 16-bit instruction.
945
946 RELAX_DELAY_SLOT_SIZE_FIRST
947 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
948 the macro is of the wrong size for the branch delay slot.
949
950 RELAX_DELAY_SLOT_SIZE_SECOND
951 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
952 the macro is of the wrong size for the branch delay slot.
953
954 The frag's "opcode" points to the first fixup for relaxable code.
955
956 Relaxable macros are generated using a sequence such as:
957
958 relax_start (SYMBOL);
959 ... generate first expansion ...
960 relax_switch ();
961 ... generate second expansion ...
962 relax_end ();
963
964 The code and fixups for the unwanted alternative are discarded
965 by md_convert_frag. */
966 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
967
968 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
969 #define RELAX_SECOND(X) ((X) & 0xff)
970 #define RELAX_USE_SECOND 0x10000
971 #define RELAX_SECOND_LONGER 0x20000
972 #define RELAX_NOMACRO 0x40000
973 #define RELAX_DELAY_SLOT 0x80000
974 #define RELAX_DELAY_SLOT_16BIT 0x100000
975 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
976 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
977
978 /* Branch without likely bit. If label is out of range, we turn:
979
980 beq reg1, reg2, label
981 delay slot
982
983 into
984
985 bne reg1, reg2, 0f
986 nop
987 j label
988 0: delay slot
989
990 with the following opcode replacements:
991
992 beq <-> bne
993 blez <-> bgtz
994 bltz <-> bgez
995 bc1f <-> bc1t
996
997 bltzal <-> bgezal (with jal label instead of j label)
998
999 Even though keeping the delay slot instruction in the delay slot of
1000 the branch would be more efficient, it would be very tricky to do
1001 correctly, because we'd have to introduce a variable frag *after*
1002 the delay slot instruction, and expand that instead. Let's do it
1003 the easy way for now, even if the branch-not-taken case now costs
1004 one additional instruction. Out-of-range branches are not supposed
1005 to be common, anyway.
1006
1007 Branch likely. If label is out of range, we turn:
1008
1009 beql reg1, reg2, label
1010 delay slot (annulled if branch not taken)
1011
1012 into
1013
1014 beql reg1, reg2, 1f
1015 nop
1016 beql $0, $0, 2f
1017 nop
1018 1: j[al] label
1019 delay slot (executed only if branch taken)
1020 2:
1021
1022 It would be possible to generate a shorter sequence by losing the
1023 likely bit, generating something like:
1024
1025 bne reg1, reg2, 0f
1026 nop
1027 j[al] label
1028 delay slot (executed only if branch taken)
1029 0:
1030
1031 beql -> bne
1032 bnel -> beq
1033 blezl -> bgtz
1034 bgtzl -> blez
1035 bltzl -> bgez
1036 bgezl -> bltz
1037 bc1fl -> bc1t
1038 bc1tl -> bc1f
1039
1040 bltzall -> bgezal (with jal label instead of j label)
1041 bgezall -> bltzal (ditto)
1042
1043
1044 but it's not clear that it would actually improve performance. */
1045 #define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
1046 ((relax_substateT) \
1047 (0xc0000000 \
1048 | ((at) & 0x1f) \
1049 | ((toofar) ? 0x20 : 0) \
1050 | ((link) ? 0x40 : 0) \
1051 | ((likely) ? 0x80 : 0) \
1052 | ((uncond) ? 0x100 : 0)))
1053 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1054 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1055 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1056 #define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1057 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1058 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1059
1060 /* For mips16 code, we use an entirely different form of relaxation.
1061 mips16 supports two versions of most instructions which take
1062 immediate values: a small one which takes some small value, and a
1063 larger one which takes a 16 bit value. Since branches also follow
1064 this pattern, relaxing these values is required.
1065
1066 We can assemble both mips16 and normal MIPS code in a single
1067 object. Therefore, we need to support this type of relaxation at
1068 the same time that we support the relaxation described above. We
1069 use the high bit of the subtype field to distinguish these cases.
1070
1071 The information we store for this type of relaxation is the
1072 argument code found in the opcode file for this relocation, whether
1073 the user explicitly requested a small or extended form, and whether
1074 the relocation is in a jump or jal delay slot. That tells us the
1075 size of the value, and how it should be stored. We also store
1076 whether the fragment is considered to be extended or not. We also
1077 store whether this is known to be a branch to a different section,
1078 whether we have tried to relax this frag yet, and whether we have
1079 ever extended a PC relative fragment because of a shift count. */
1080 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1081 (0x80000000 \
1082 | ((type) & 0xff) \
1083 | ((small) ? 0x100 : 0) \
1084 | ((ext) ? 0x200 : 0) \
1085 | ((dslot) ? 0x400 : 0) \
1086 | ((jal_dslot) ? 0x800 : 0))
1087 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1088 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1089 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1090 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1091 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1092 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1093 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1094 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1095 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1096 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1097 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1098 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
1099
1100 /* For microMIPS code, we use relaxation similar to one we use for
1101 MIPS16 code. Some instructions that take immediate values support
1102 two encodings: a small one which takes some small value, and a
1103 larger one which takes a 16 bit value. As some branches also follow
1104 this pattern, relaxing these values is required.
1105
1106 We can assemble both microMIPS and normal MIPS code in a single
1107 object. Therefore, we need to support this type of relaxation at
1108 the same time that we support the relaxation described above. We
1109 use one of the high bits of the subtype field to distinguish these
1110 cases.
1111
1112 The information we store for this type of relaxation is the argument
1113 code found in the opcode file for this relocation, the register
1114 selected as the assembler temporary, whether the branch is
1115 unconditional, whether it is compact, whether it stores the link
1116 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1117 branches to a sequence of instructions is enabled, and whether the
1118 displacement of a branch is too large to fit as an immediate argument
1119 of a 16-bit and a 32-bit branch, respectively. */
1120 #define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1121 relax32, toofar16, toofar32) \
1122 (0x40000000 \
1123 | ((type) & 0xff) \
1124 | (((at) & 0x1f) << 8) \
1125 | ((uncond) ? 0x2000 : 0) \
1126 | ((compact) ? 0x4000 : 0) \
1127 | ((link) ? 0x8000 : 0) \
1128 | ((relax32) ? 0x10000 : 0) \
1129 | ((toofar16) ? 0x20000 : 0) \
1130 | ((toofar32) ? 0x40000 : 0))
1131 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1132 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1133 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1134 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1135 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1136 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1137 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1138
1139 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1140 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1141 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1142 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1143 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1144 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
1145
1146 /* Sign-extend 16-bit value X. */
1147 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1148
1149 /* Is the given value a sign-extended 32-bit value? */
1150 #define IS_SEXT_32BIT_NUM(x) \
1151 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1152 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1153
1154 /* Is the given value a sign-extended 16-bit value? */
1155 #define IS_SEXT_16BIT_NUM(x) \
1156 (((x) &~ (offsetT) 0x7fff) == 0 \
1157 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1158
1159 /* Is the given value a sign-extended 12-bit value? */
1160 #define IS_SEXT_12BIT_NUM(x) \
1161 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1162
1163 /* Is the given value a sign-extended 9-bit value? */
1164 #define IS_SEXT_9BIT_NUM(x) \
1165 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1166
1167 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
1168 #define IS_ZEXT_32BIT_NUM(x) \
1169 (((x) &~ (offsetT) 0xffffffff) == 0 \
1170 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1171
1172 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
1173 VALUE << SHIFT. VALUE is evaluated exactly once. */
1174 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
1175 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
1176 | (((VALUE) & (MASK)) << (SHIFT)))
1177
1178 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1179 SHIFT places. */
1180 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1181 (((STRUCT) >> (SHIFT)) & (MASK))
1182
1183 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
1184 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
1185
1186 include/opcode/mips.h specifies operand fields using the macros
1187 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
1188 with "MIPS16OP" instead of "OP". */
1189 #define INSERT_OPERAND(MICROMIPS, FIELD, INSN, VALUE) \
1190 do \
1191 if (!(MICROMIPS)) \
1192 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1193 OP_MASK_##FIELD, OP_SH_##FIELD); \
1194 else \
1195 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1196 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD); \
1197 while (0)
1198 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
1199 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1200 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
1201
1202 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
1203 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1204 (!(MICROMIPS) \
1205 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1206 : EXTRACT_BITS ((INSN).insn_opcode, \
1207 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1208 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1209 EXTRACT_BITS ((INSN).insn_opcode, \
1210 MIPS16OP_MASK_##FIELD, \
1211 MIPS16OP_SH_##FIELD)
1212
1213 /* The MIPS16 EXTEND opcode, shifted left 16 places. */
1214 #define MIPS16_EXTEND (0xf000U << 16)
1215 \f
1216 /* Whether or not we are emitting a branch-likely macro. */
1217 static bfd_boolean emit_branch_likely_macro = FALSE;
1218
1219 /* Global variables used when generating relaxable macros. See the
1220 comment above RELAX_ENCODE for more details about how relaxation
1221 is used. */
1222 static struct {
1223 /* 0 if we're not emitting a relaxable macro.
1224 1 if we're emitting the first of the two relaxation alternatives.
1225 2 if we're emitting the second alternative. */
1226 int sequence;
1227
1228 /* The first relaxable fixup in the current frag. (In other words,
1229 the first fixup that refers to relaxable code.) */
1230 fixS *first_fixup;
1231
1232 /* sizes[0] says how many bytes of the first alternative are stored in
1233 the current frag. Likewise sizes[1] for the second alternative. */
1234 unsigned int sizes[2];
1235
1236 /* The symbol on which the choice of sequence depends. */
1237 symbolS *symbol;
1238 } mips_relax;
1239 \f
1240 /* Global variables used to decide whether a macro needs a warning. */
1241 static struct {
1242 /* True if the macro is in a branch delay slot. */
1243 bfd_boolean delay_slot_p;
1244
1245 /* Set to the length in bytes required if the macro is in a delay slot
1246 that requires a specific length of instruction, otherwise zero. */
1247 unsigned int delay_slot_length;
1248
1249 /* For relaxable macros, sizes[0] is the length of the first alternative
1250 in bytes and sizes[1] is the length of the second alternative.
1251 For non-relaxable macros, both elements give the length of the
1252 macro in bytes. */
1253 unsigned int sizes[2];
1254
1255 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1256 instruction of the first alternative in bytes and first_insn_sizes[1]
1257 is the length of the first instruction of the second alternative.
1258 For non-relaxable macros, both elements give the length of the first
1259 instruction in bytes.
1260
1261 Set to zero if we haven't yet seen the first instruction. */
1262 unsigned int first_insn_sizes[2];
1263
1264 /* For relaxable macros, insns[0] is the number of instructions for the
1265 first alternative and insns[1] is the number of instructions for the
1266 second alternative.
1267
1268 For non-relaxable macros, both elements give the number of
1269 instructions for the macro. */
1270 unsigned int insns[2];
1271
1272 /* The first variant frag for this macro. */
1273 fragS *first_frag;
1274 } mips_macro_warning;
1275 \f
1276 /* Prototypes for static functions. */
1277
1278 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1279
1280 static void append_insn
1281 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1282 bfd_boolean expansionp);
1283 static void mips_no_prev_insn (void);
1284 static void macro_build (expressionS *, const char *, const char *, ...);
1285 static void mips16_macro_build
1286 (expressionS *, const char *, const char *, va_list *);
1287 static void load_register (int, expressionS *, int);
1288 static void macro_start (void);
1289 static void macro_end (void);
1290 static void macro (struct mips_cl_insn *ip, char *str);
1291 static void mips16_macro (struct mips_cl_insn * ip);
1292 static void mips_ip (char *str, struct mips_cl_insn * ip);
1293 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1294 static void mips16_immed
1295 (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1296 unsigned int, unsigned long *);
1297 static size_t my_getSmallExpression
1298 (expressionS *, bfd_reloc_code_real_type *, char *);
1299 static void my_getExpression (expressionS *, char *);
1300 static void s_align (int);
1301 static void s_change_sec (int);
1302 static void s_change_section (int);
1303 static void s_cons (int);
1304 static void s_float_cons (int);
1305 static void s_mips_globl (int);
1306 static void s_option (int);
1307 static void s_mipsset (int);
1308 static void s_abicalls (int);
1309 static void s_cpload (int);
1310 static void s_cpsetup (int);
1311 static void s_cplocal (int);
1312 static void s_cprestore (int);
1313 static void s_cpreturn (int);
1314 static void s_dtprelword (int);
1315 static void s_dtpreldword (int);
1316 static void s_tprelword (int);
1317 static void s_tpreldword (int);
1318 static void s_gpvalue (int);
1319 static void s_gpword (int);
1320 static void s_gpdword (int);
1321 static void s_ehword (int);
1322 static void s_cpadd (int);
1323 static void s_insn (int);
1324 static void md_obj_begin (void);
1325 static void md_obj_end (void);
1326 static void s_mips_ent (int);
1327 static void s_mips_end (int);
1328 static void s_mips_frame (int);
1329 static void s_mips_mask (int reg_type);
1330 static void s_mips_stab (int);
1331 static void s_mips_weakext (int);
1332 static void s_mips_file (int);
1333 static void s_mips_loc (int);
1334 static bfd_boolean pic_need_relax (symbolS *, asection *);
1335 static int relaxed_branch_length (fragS *, asection *, int);
1336 static int validate_mips_insn (const struct mips_opcode *);
1337 static int validate_micromips_insn (const struct mips_opcode *);
1338 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1339 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1340
1341 /* Table and functions used to map between CPU/ISA names, and
1342 ISA levels, and CPU numbers. */
1343
1344 struct mips_cpu_info
1345 {
1346 const char *name; /* CPU or ISA name. */
1347 int flags; /* MIPS_CPU_* flags. */
1348 int ase; /* Set of ASEs implemented by the CPU. */
1349 int isa; /* ISA level. */
1350 int cpu; /* CPU number (default CPU if ISA). */
1351 };
1352
1353 #define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1354
1355 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1356 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1357 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1358 \f
1359 /* Command-line options. */
1360 const char *md_shortopts = "O::g::G:";
1361
1362 enum options
1363 {
1364 OPTION_MARCH = OPTION_MD_BASE,
1365 OPTION_MTUNE,
1366 OPTION_MIPS1,
1367 OPTION_MIPS2,
1368 OPTION_MIPS3,
1369 OPTION_MIPS4,
1370 OPTION_MIPS5,
1371 OPTION_MIPS32,
1372 OPTION_MIPS64,
1373 OPTION_MIPS32R2,
1374 OPTION_MIPS64R2,
1375 OPTION_MIPS16,
1376 OPTION_NO_MIPS16,
1377 OPTION_MIPS3D,
1378 OPTION_NO_MIPS3D,
1379 OPTION_MDMX,
1380 OPTION_NO_MDMX,
1381 OPTION_DSP,
1382 OPTION_NO_DSP,
1383 OPTION_MT,
1384 OPTION_NO_MT,
1385 OPTION_VIRT,
1386 OPTION_NO_VIRT,
1387 OPTION_SMARTMIPS,
1388 OPTION_NO_SMARTMIPS,
1389 OPTION_DSPR2,
1390 OPTION_NO_DSPR2,
1391 OPTION_EVA,
1392 OPTION_NO_EVA,
1393 OPTION_MICROMIPS,
1394 OPTION_NO_MICROMIPS,
1395 OPTION_MCU,
1396 OPTION_NO_MCU,
1397 OPTION_COMPAT_ARCH_BASE,
1398 OPTION_M4650,
1399 OPTION_NO_M4650,
1400 OPTION_M4010,
1401 OPTION_NO_M4010,
1402 OPTION_M4100,
1403 OPTION_NO_M4100,
1404 OPTION_M3900,
1405 OPTION_NO_M3900,
1406 OPTION_M7000_HILO_FIX,
1407 OPTION_MNO_7000_HILO_FIX,
1408 OPTION_FIX_24K,
1409 OPTION_NO_FIX_24K,
1410 OPTION_FIX_LOONGSON2F_JUMP,
1411 OPTION_NO_FIX_LOONGSON2F_JUMP,
1412 OPTION_FIX_LOONGSON2F_NOP,
1413 OPTION_NO_FIX_LOONGSON2F_NOP,
1414 OPTION_FIX_VR4120,
1415 OPTION_NO_FIX_VR4120,
1416 OPTION_FIX_VR4130,
1417 OPTION_NO_FIX_VR4130,
1418 OPTION_FIX_CN63XXP1,
1419 OPTION_NO_FIX_CN63XXP1,
1420 OPTION_TRAP,
1421 OPTION_BREAK,
1422 OPTION_EB,
1423 OPTION_EL,
1424 OPTION_FP32,
1425 OPTION_GP32,
1426 OPTION_CONSTRUCT_FLOATS,
1427 OPTION_NO_CONSTRUCT_FLOATS,
1428 OPTION_FP64,
1429 OPTION_GP64,
1430 OPTION_RELAX_BRANCH,
1431 OPTION_NO_RELAX_BRANCH,
1432 OPTION_INSN32,
1433 OPTION_NO_INSN32,
1434 OPTION_MSHARED,
1435 OPTION_MNO_SHARED,
1436 OPTION_MSYM32,
1437 OPTION_MNO_SYM32,
1438 OPTION_SOFT_FLOAT,
1439 OPTION_HARD_FLOAT,
1440 OPTION_SINGLE_FLOAT,
1441 OPTION_DOUBLE_FLOAT,
1442 OPTION_32,
1443 OPTION_CALL_SHARED,
1444 OPTION_CALL_NONPIC,
1445 OPTION_NON_SHARED,
1446 OPTION_XGOT,
1447 OPTION_MABI,
1448 OPTION_N32,
1449 OPTION_64,
1450 OPTION_MDEBUG,
1451 OPTION_NO_MDEBUG,
1452 OPTION_PDR,
1453 OPTION_NO_PDR,
1454 OPTION_MVXWORKS_PIC,
1455 OPTION_END_OF_ENUM
1456 };
1457
1458 struct option md_longopts[] =
1459 {
1460 /* Options which specify architecture. */
1461 {"march", required_argument, NULL, OPTION_MARCH},
1462 {"mtune", required_argument, NULL, OPTION_MTUNE},
1463 {"mips0", no_argument, NULL, OPTION_MIPS1},
1464 {"mips1", no_argument, NULL, OPTION_MIPS1},
1465 {"mips2", no_argument, NULL, OPTION_MIPS2},
1466 {"mips3", no_argument, NULL, OPTION_MIPS3},
1467 {"mips4", no_argument, NULL, OPTION_MIPS4},
1468 {"mips5", no_argument, NULL, OPTION_MIPS5},
1469 {"mips32", no_argument, NULL, OPTION_MIPS32},
1470 {"mips64", no_argument, NULL, OPTION_MIPS64},
1471 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1472 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1473
1474 /* Options which specify Application Specific Extensions (ASEs). */
1475 {"mips16", no_argument, NULL, OPTION_MIPS16},
1476 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1477 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1478 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1479 {"mdmx", no_argument, NULL, OPTION_MDMX},
1480 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1481 {"mdsp", no_argument, NULL, OPTION_DSP},
1482 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1483 {"mmt", no_argument, NULL, OPTION_MT},
1484 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1485 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1486 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1487 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1488 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1489 {"meva", no_argument, NULL, OPTION_EVA},
1490 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1491 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1492 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1493 {"mmcu", no_argument, NULL, OPTION_MCU},
1494 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1495 {"mvirt", no_argument, NULL, OPTION_VIRT},
1496 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1497
1498 /* Old-style architecture options. Don't add more of these. */
1499 {"m4650", no_argument, NULL, OPTION_M4650},
1500 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1501 {"m4010", no_argument, NULL, OPTION_M4010},
1502 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1503 {"m4100", no_argument, NULL, OPTION_M4100},
1504 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1505 {"m3900", no_argument, NULL, OPTION_M3900},
1506 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1507
1508 /* Options which enable bug fixes. */
1509 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1510 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1511 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1512 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1513 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1514 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1515 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1516 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1517 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1518 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1519 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1520 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1521 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1522 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1523 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1524
1525 /* Miscellaneous options. */
1526 {"trap", no_argument, NULL, OPTION_TRAP},
1527 {"no-break", no_argument, NULL, OPTION_TRAP},
1528 {"break", no_argument, NULL, OPTION_BREAK},
1529 {"no-trap", no_argument, NULL, OPTION_BREAK},
1530 {"EB", no_argument, NULL, OPTION_EB},
1531 {"EL", no_argument, NULL, OPTION_EL},
1532 {"mfp32", no_argument, NULL, OPTION_FP32},
1533 {"mgp32", no_argument, NULL, OPTION_GP32},
1534 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1535 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1536 {"mfp64", no_argument, NULL, OPTION_FP64},
1537 {"mgp64", no_argument, NULL, OPTION_GP64},
1538 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1539 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
1540 {"minsn32", no_argument, NULL, OPTION_INSN32},
1541 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
1542 {"mshared", no_argument, NULL, OPTION_MSHARED},
1543 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1544 {"msym32", no_argument, NULL, OPTION_MSYM32},
1545 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1546 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1547 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1548 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1549 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1550
1551 /* Strictly speaking this next option is ELF specific,
1552 but we allow it for other ports as well in order to
1553 make testing easier. */
1554 {"32", no_argument, NULL, OPTION_32},
1555
1556 /* ELF-specific options. */
1557 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1558 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1559 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1560 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1561 {"xgot", no_argument, NULL, OPTION_XGOT},
1562 {"mabi", required_argument, NULL, OPTION_MABI},
1563 {"n32", no_argument, NULL, OPTION_N32},
1564 {"64", no_argument, NULL, OPTION_64},
1565 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1566 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1567 {"mpdr", no_argument, NULL, OPTION_PDR},
1568 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1569 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
1570
1571 {NULL, no_argument, NULL, 0}
1572 };
1573 size_t md_longopts_size = sizeof (md_longopts);
1574 \f
1575 /* Information about either an Application Specific Extension or an
1576 optional architecture feature that, for simplicity, we treat in the
1577 same way as an ASE. */
1578 struct mips_ase
1579 {
1580 /* The name of the ASE, used in both the command-line and .set options. */
1581 const char *name;
1582
1583 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1584 and 64-bit architectures, the flags here refer to the subset that
1585 is available on both. */
1586 unsigned int flags;
1587
1588 /* The ASE_* flag used for instructions that are available on 64-bit
1589 architectures but that are not included in FLAGS. */
1590 unsigned int flags64;
1591
1592 /* The command-line options that turn the ASE on and off. */
1593 int option_on;
1594 int option_off;
1595
1596 /* The minimum required architecture revisions for MIPS32, MIPS64,
1597 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1598 int mips32_rev;
1599 int mips64_rev;
1600 int micromips32_rev;
1601 int micromips64_rev;
1602 };
1603
1604 /* A table of all supported ASEs. */
1605 static const struct mips_ase mips_ases[] = {
1606 { "dsp", ASE_DSP, ASE_DSP64,
1607 OPTION_DSP, OPTION_NO_DSP,
1608 2, 2, 2, 2 },
1609
1610 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1611 OPTION_DSPR2, OPTION_NO_DSPR2,
1612 2, 2, 2, 2 },
1613
1614 { "eva", ASE_EVA, 0,
1615 OPTION_EVA, OPTION_NO_EVA,
1616 2, 2, 2, 2 },
1617
1618 { "mcu", ASE_MCU, 0,
1619 OPTION_MCU, OPTION_NO_MCU,
1620 2, 2, 2, 2 },
1621
1622 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1623 { "mdmx", ASE_MDMX, 0,
1624 OPTION_MDMX, OPTION_NO_MDMX,
1625 -1, 1, -1, -1 },
1626
1627 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1628 { "mips3d", ASE_MIPS3D, 0,
1629 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1630 2, 1, -1, -1 },
1631
1632 { "mt", ASE_MT, 0,
1633 OPTION_MT, OPTION_NO_MT,
1634 2, 2, -1, -1 },
1635
1636 { "smartmips", ASE_SMARTMIPS, 0,
1637 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1638 1, -1, -1, -1 },
1639
1640 { "virt", ASE_VIRT, ASE_VIRT64,
1641 OPTION_VIRT, OPTION_NO_VIRT,
1642 2, 2, 2, 2 }
1643 };
1644
1645 /* The set of ASEs that require -mfp64. */
1646 #define FP64_ASES (ASE_MIPS3D | ASE_MDMX)
1647
1648 /* Groups of ASE_* flags that represent different revisions of an ASE. */
1649 static const unsigned int mips_ase_groups[] = {
1650 ASE_DSP | ASE_DSPR2
1651 };
1652 \f
1653 /* Pseudo-op table.
1654
1655 The following pseudo-ops from the Kane and Heinrich MIPS book
1656 should be defined here, but are currently unsupported: .alias,
1657 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1658
1659 The following pseudo-ops from the Kane and Heinrich MIPS book are
1660 specific to the type of debugging information being generated, and
1661 should be defined by the object format: .aent, .begin, .bend,
1662 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1663 .vreg.
1664
1665 The following pseudo-ops from the Kane and Heinrich MIPS book are
1666 not MIPS CPU specific, but are also not specific to the object file
1667 format. This file is probably the best place to define them, but
1668 they are not currently supported: .asm0, .endr, .lab, .struct. */
1669
1670 static const pseudo_typeS mips_pseudo_table[] =
1671 {
1672 /* MIPS specific pseudo-ops. */
1673 {"option", s_option, 0},
1674 {"set", s_mipsset, 0},
1675 {"rdata", s_change_sec, 'r'},
1676 {"sdata", s_change_sec, 's'},
1677 {"livereg", s_ignore, 0},
1678 {"abicalls", s_abicalls, 0},
1679 {"cpload", s_cpload, 0},
1680 {"cpsetup", s_cpsetup, 0},
1681 {"cplocal", s_cplocal, 0},
1682 {"cprestore", s_cprestore, 0},
1683 {"cpreturn", s_cpreturn, 0},
1684 {"dtprelword", s_dtprelword, 0},
1685 {"dtpreldword", s_dtpreldword, 0},
1686 {"tprelword", s_tprelword, 0},
1687 {"tpreldword", s_tpreldword, 0},
1688 {"gpvalue", s_gpvalue, 0},
1689 {"gpword", s_gpword, 0},
1690 {"gpdword", s_gpdword, 0},
1691 {"ehword", s_ehword, 0},
1692 {"cpadd", s_cpadd, 0},
1693 {"insn", s_insn, 0},
1694
1695 /* Relatively generic pseudo-ops that happen to be used on MIPS
1696 chips. */
1697 {"asciiz", stringer, 8 + 1},
1698 {"bss", s_change_sec, 'b'},
1699 {"err", s_err, 0},
1700 {"half", s_cons, 1},
1701 {"dword", s_cons, 3},
1702 {"weakext", s_mips_weakext, 0},
1703 {"origin", s_org, 0},
1704 {"repeat", s_rept, 0},
1705
1706 /* For MIPS this is non-standard, but we define it for consistency. */
1707 {"sbss", s_change_sec, 'B'},
1708
1709 /* These pseudo-ops are defined in read.c, but must be overridden
1710 here for one reason or another. */
1711 {"align", s_align, 0},
1712 {"byte", s_cons, 0},
1713 {"data", s_change_sec, 'd'},
1714 {"double", s_float_cons, 'd'},
1715 {"float", s_float_cons, 'f'},
1716 {"globl", s_mips_globl, 0},
1717 {"global", s_mips_globl, 0},
1718 {"hword", s_cons, 1},
1719 {"int", s_cons, 2},
1720 {"long", s_cons, 2},
1721 {"octa", s_cons, 4},
1722 {"quad", s_cons, 3},
1723 {"section", s_change_section, 0},
1724 {"short", s_cons, 1},
1725 {"single", s_float_cons, 'f'},
1726 {"stabd", s_mips_stab, 'd'},
1727 {"stabn", s_mips_stab, 'n'},
1728 {"stabs", s_mips_stab, 's'},
1729 {"text", s_change_sec, 't'},
1730 {"word", s_cons, 2},
1731
1732 { "extern", ecoff_directive_extern, 0},
1733
1734 { NULL, NULL, 0 },
1735 };
1736
1737 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1738 {
1739 /* These pseudo-ops should be defined by the object file format.
1740 However, a.out doesn't support them, so we have versions here. */
1741 {"aent", s_mips_ent, 1},
1742 {"bgnb", s_ignore, 0},
1743 {"end", s_mips_end, 0},
1744 {"endb", s_ignore, 0},
1745 {"ent", s_mips_ent, 0},
1746 {"file", s_mips_file, 0},
1747 {"fmask", s_mips_mask, 'F'},
1748 {"frame", s_mips_frame, 0},
1749 {"loc", s_mips_loc, 0},
1750 {"mask", s_mips_mask, 'R'},
1751 {"verstamp", s_ignore, 0},
1752 { NULL, NULL, 0 },
1753 };
1754
1755 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1756 purpose of the `.dc.a' internal pseudo-op. */
1757
1758 int
1759 mips_address_bytes (void)
1760 {
1761 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1762 }
1763
1764 extern void pop_insert (const pseudo_typeS *);
1765
1766 void
1767 mips_pop_insert (void)
1768 {
1769 pop_insert (mips_pseudo_table);
1770 if (! ECOFF_DEBUGGING)
1771 pop_insert (mips_nonecoff_pseudo_table);
1772 }
1773 \f
1774 /* Symbols labelling the current insn. */
1775
1776 struct insn_label_list
1777 {
1778 struct insn_label_list *next;
1779 symbolS *label;
1780 };
1781
1782 static struct insn_label_list *free_insn_labels;
1783 #define label_list tc_segment_info_data.labels
1784
1785 static void mips_clear_insn_labels (void);
1786 static void mips_mark_labels (void);
1787 static void mips_compressed_mark_labels (void);
1788
1789 static inline void
1790 mips_clear_insn_labels (void)
1791 {
1792 register struct insn_label_list **pl;
1793 segment_info_type *si;
1794
1795 if (now_seg)
1796 {
1797 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1798 ;
1799
1800 si = seg_info (now_seg);
1801 *pl = si->label_list;
1802 si->label_list = NULL;
1803 }
1804 }
1805
1806 /* Mark instruction labels in MIPS16/microMIPS mode. */
1807
1808 static inline void
1809 mips_mark_labels (void)
1810 {
1811 if (HAVE_CODE_COMPRESSION)
1812 mips_compressed_mark_labels ();
1813 }
1814 \f
1815 static char *expr_end;
1816
1817 /* Expressions which appear in instructions. These are set by
1818 mips_ip. */
1819
1820 static expressionS imm_expr;
1821 static expressionS imm2_expr;
1822 static expressionS offset_expr;
1823
1824 /* Relocs associated with imm_expr and offset_expr. */
1825
1826 static bfd_reloc_code_real_type imm_reloc[3]
1827 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1828 static bfd_reloc_code_real_type offset_reloc[3]
1829 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1830
1831 /* This is set to the resulting size of the instruction to be produced
1832 by mips16_ip if an explicit extension is used or by mips_ip if an
1833 explicit size is supplied. */
1834
1835 static unsigned int forced_insn_length;
1836
1837 /* True if we are assembling an instruction. All dot symbols defined during
1838 this time should be treated as code labels. */
1839
1840 static bfd_boolean mips_assembling_insn;
1841
1842 /* The pdr segment for per procedure frame/regmask info. Not used for
1843 ECOFF debugging. */
1844
1845 static segT pdr_seg;
1846
1847 /* The default target format to use. */
1848
1849 #if defined (TE_FreeBSD)
1850 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1851 #elif defined (TE_TMIPS)
1852 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1853 #else
1854 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1855 #endif
1856
1857 const char *
1858 mips_target_format (void)
1859 {
1860 switch (OUTPUT_FLAVOR)
1861 {
1862 case bfd_target_elf_flavour:
1863 #ifdef TE_VXWORKS
1864 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1865 return (target_big_endian
1866 ? "elf32-bigmips-vxworks"
1867 : "elf32-littlemips-vxworks");
1868 #endif
1869 return (target_big_endian
1870 ? (HAVE_64BIT_OBJECTS
1871 ? ELF_TARGET ("elf64-", "big")
1872 : (HAVE_NEWABI
1873 ? ELF_TARGET ("elf32-n", "big")
1874 : ELF_TARGET ("elf32-", "big")))
1875 : (HAVE_64BIT_OBJECTS
1876 ? ELF_TARGET ("elf64-", "little")
1877 : (HAVE_NEWABI
1878 ? ELF_TARGET ("elf32-n", "little")
1879 : ELF_TARGET ("elf32-", "little"))));
1880 default:
1881 abort ();
1882 return NULL;
1883 }
1884 }
1885
1886 /* Return the ISA revision that is currently in use, or 0 if we are
1887 generating code for MIPS V or below. */
1888
1889 static int
1890 mips_isa_rev (void)
1891 {
1892 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
1893 return 2;
1894
1895 /* microMIPS implies revision 2 or above. */
1896 if (mips_opts.micromips)
1897 return 2;
1898
1899 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
1900 return 1;
1901
1902 return 0;
1903 }
1904
1905 /* Return the mask of all ASEs that are revisions of those in FLAGS. */
1906
1907 static unsigned int
1908 mips_ase_mask (unsigned int flags)
1909 {
1910 unsigned int i;
1911
1912 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
1913 if (flags & mips_ase_groups[i])
1914 flags |= mips_ase_groups[i];
1915 return flags;
1916 }
1917
1918 /* Check whether the current ISA supports ASE. Issue a warning if
1919 appropriate. */
1920
1921 static void
1922 mips_check_isa_supports_ase (const struct mips_ase *ase)
1923 {
1924 const char *base;
1925 int min_rev, size;
1926 static unsigned int warned_isa;
1927 static unsigned int warned_fp32;
1928
1929 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1930 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
1931 else
1932 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
1933 if ((min_rev < 0 || mips_isa_rev () < min_rev)
1934 && (warned_isa & ase->flags) != ase->flags)
1935 {
1936 warned_isa |= ase->flags;
1937 base = mips_opts.micromips ? "microMIPS" : "MIPS";
1938 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
1939 if (min_rev < 0)
1940 as_warn (_("The %d-bit %s architecture does not support the"
1941 " `%s' extension"), size, base, ase->name);
1942 else
1943 as_warn (_("The `%s' extension requires %s%d revision %d or greater"),
1944 ase->name, base, size, min_rev);
1945 }
1946 if ((ase->flags & FP64_ASES)
1947 && mips_opts.fp32
1948 && (warned_fp32 & ase->flags) != ase->flags)
1949 {
1950 warned_fp32 |= ase->flags;
1951 as_warn (_("The `%s' extension requires 64-bit FPRs"), ase->name);
1952 }
1953 }
1954
1955 /* Check all enabled ASEs to see whether they are supported by the
1956 chosen architecture. */
1957
1958 static void
1959 mips_check_isa_supports_ases (void)
1960 {
1961 unsigned int i, mask;
1962
1963 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1964 {
1965 mask = mips_ase_mask (mips_ases[i].flags);
1966 if ((mips_opts.ase & mask) == mips_ases[i].flags)
1967 mips_check_isa_supports_ase (&mips_ases[i]);
1968 }
1969 }
1970
1971 /* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
1972 that were affected. */
1973
1974 static unsigned int
1975 mips_set_ase (const struct mips_ase *ase, bfd_boolean enabled_p)
1976 {
1977 unsigned int mask;
1978
1979 mask = mips_ase_mask (ase->flags);
1980 mips_opts.ase &= ~mask;
1981 if (enabled_p)
1982 mips_opts.ase |= ase->flags;
1983 return mask;
1984 }
1985
1986 /* Return the ASE called NAME, or null if none. */
1987
1988 static const struct mips_ase *
1989 mips_lookup_ase (const char *name)
1990 {
1991 unsigned int i;
1992
1993 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1994 if (strcmp (name, mips_ases[i].name) == 0)
1995 return &mips_ases[i];
1996 return NULL;
1997 }
1998
1999 /* Return the length of a microMIPS instruction in bytes. If bits of
2000 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
2001 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
2002 major opcode) will require further modifications to the opcode
2003 table. */
2004
2005 static inline unsigned int
2006 micromips_insn_length (const struct mips_opcode *mo)
2007 {
2008 return (mo->mask >> 16) == 0 ? 2 : 4;
2009 }
2010
2011 /* Return the length of MIPS16 instruction OPCODE. */
2012
2013 static inline unsigned int
2014 mips16_opcode_length (unsigned long opcode)
2015 {
2016 return (opcode >> 16) == 0 ? 2 : 4;
2017 }
2018
2019 /* Return the length of instruction INSN. */
2020
2021 static inline unsigned int
2022 insn_length (const struct mips_cl_insn *insn)
2023 {
2024 if (mips_opts.micromips)
2025 return micromips_insn_length (insn->insn_mo);
2026 else if (mips_opts.mips16)
2027 return mips16_opcode_length (insn->insn_opcode);
2028 else
2029 return 4;
2030 }
2031
2032 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2033
2034 static void
2035 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2036 {
2037 size_t i;
2038
2039 insn->insn_mo = mo;
2040 insn->insn_opcode = mo->match;
2041 insn->frag = NULL;
2042 insn->where = 0;
2043 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2044 insn->fixp[i] = NULL;
2045 insn->fixed_p = (mips_opts.noreorder > 0);
2046 insn->noreorder_p = (mips_opts.noreorder > 0);
2047 insn->mips16_absolute_jump_p = 0;
2048 insn->complete_p = 0;
2049 insn->cleared_p = 0;
2050 }
2051
2052 /* Record the current MIPS16/microMIPS mode in now_seg. */
2053
2054 static void
2055 mips_record_compressed_mode (void)
2056 {
2057 segment_info_type *si;
2058
2059 si = seg_info (now_seg);
2060 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2061 si->tc_segment_info_data.mips16 = mips_opts.mips16;
2062 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2063 si->tc_segment_info_data.micromips = mips_opts.micromips;
2064 }
2065
2066 /* Read a standard MIPS instruction from BUF. */
2067
2068 static unsigned long
2069 read_insn (char *buf)
2070 {
2071 if (target_big_endian)
2072 return bfd_getb32 ((bfd_byte *) buf);
2073 else
2074 return bfd_getl32 ((bfd_byte *) buf);
2075 }
2076
2077 /* Write standard MIPS instruction INSN to BUF. Return a pointer to
2078 the next byte. */
2079
2080 static char *
2081 write_insn (char *buf, unsigned int insn)
2082 {
2083 md_number_to_chars (buf, insn, 4);
2084 return buf + 4;
2085 }
2086
2087 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
2088 has length LENGTH. */
2089
2090 static unsigned long
2091 read_compressed_insn (char *buf, unsigned int length)
2092 {
2093 unsigned long insn;
2094 unsigned int i;
2095
2096 insn = 0;
2097 for (i = 0; i < length; i += 2)
2098 {
2099 insn <<= 16;
2100 if (target_big_endian)
2101 insn |= bfd_getb16 ((char *) buf);
2102 else
2103 insn |= bfd_getl16 ((char *) buf);
2104 buf += 2;
2105 }
2106 return insn;
2107 }
2108
2109 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2110 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2111
2112 static char *
2113 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2114 {
2115 unsigned int i;
2116
2117 for (i = 0; i < length; i += 2)
2118 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2119 return buf + length;
2120 }
2121
2122 /* Install INSN at the location specified by its "frag" and "where" fields. */
2123
2124 static void
2125 install_insn (const struct mips_cl_insn *insn)
2126 {
2127 char *f = insn->frag->fr_literal + insn->where;
2128 if (HAVE_CODE_COMPRESSION)
2129 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
2130 else
2131 write_insn (f, insn->insn_opcode);
2132 mips_record_compressed_mode ();
2133 }
2134
2135 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2136 and install the opcode in the new location. */
2137
2138 static void
2139 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2140 {
2141 size_t i;
2142
2143 insn->frag = frag;
2144 insn->where = where;
2145 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2146 if (insn->fixp[i] != NULL)
2147 {
2148 insn->fixp[i]->fx_frag = frag;
2149 insn->fixp[i]->fx_where = where;
2150 }
2151 install_insn (insn);
2152 }
2153
2154 /* Add INSN to the end of the output. */
2155
2156 static void
2157 add_fixed_insn (struct mips_cl_insn *insn)
2158 {
2159 char *f = frag_more (insn_length (insn));
2160 move_insn (insn, frag_now, f - frag_now->fr_literal);
2161 }
2162
2163 /* Start a variant frag and move INSN to the start of the variant part,
2164 marking it as fixed. The other arguments are as for frag_var. */
2165
2166 static void
2167 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2168 relax_substateT subtype, symbolS *symbol, offsetT offset)
2169 {
2170 frag_grow (max_chars);
2171 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2172 insn->fixed_p = 1;
2173 frag_var (rs_machine_dependent, max_chars, var,
2174 subtype, symbol, offset, NULL);
2175 }
2176
2177 /* Insert N copies of INSN into the history buffer, starting at
2178 position FIRST. Neither FIRST nor N need to be clipped. */
2179
2180 static void
2181 insert_into_history (unsigned int first, unsigned int n,
2182 const struct mips_cl_insn *insn)
2183 {
2184 if (mips_relax.sequence != 2)
2185 {
2186 unsigned int i;
2187
2188 for (i = ARRAY_SIZE (history); i-- > first;)
2189 if (i >= first + n)
2190 history[i] = history[i - n];
2191 else
2192 history[i] = *insn;
2193 }
2194 }
2195
2196 /* Initialize vr4120_conflicts. There is a bit of duplication here:
2197 the idea is to make it obvious at a glance that each errata is
2198 included. */
2199
2200 static void
2201 init_vr4120_conflicts (void)
2202 {
2203 #define CONFLICT(FIRST, SECOND) \
2204 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2205
2206 /* Errata 21 - [D]DIV[U] after [D]MACC */
2207 CONFLICT (MACC, DIV);
2208 CONFLICT (DMACC, DIV);
2209
2210 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2211 CONFLICT (DMULT, DMULT);
2212 CONFLICT (DMULT, DMACC);
2213 CONFLICT (DMACC, DMULT);
2214 CONFLICT (DMACC, DMACC);
2215
2216 /* Errata 24 - MT{LO,HI} after [D]MACC */
2217 CONFLICT (MACC, MTHILO);
2218 CONFLICT (DMACC, MTHILO);
2219
2220 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2221 instruction is executed immediately after a MACC or DMACC
2222 instruction, the result of [either instruction] is incorrect." */
2223 CONFLICT (MACC, MULT);
2224 CONFLICT (MACC, DMULT);
2225 CONFLICT (DMACC, MULT);
2226 CONFLICT (DMACC, DMULT);
2227
2228 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2229 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2230 DDIV or DDIVU instruction, the result of the MACC or
2231 DMACC instruction is incorrect.". */
2232 CONFLICT (DMULT, MACC);
2233 CONFLICT (DMULT, DMACC);
2234 CONFLICT (DIV, MACC);
2235 CONFLICT (DIV, DMACC);
2236
2237 #undef CONFLICT
2238 }
2239
2240 struct regname {
2241 const char *name;
2242 unsigned int num;
2243 };
2244
2245 #define RTYPE_MASK 0x1ff00
2246 #define RTYPE_NUM 0x00100
2247 #define RTYPE_FPU 0x00200
2248 #define RTYPE_FCC 0x00400
2249 #define RTYPE_VEC 0x00800
2250 #define RTYPE_GP 0x01000
2251 #define RTYPE_CP0 0x02000
2252 #define RTYPE_PC 0x04000
2253 #define RTYPE_ACC 0x08000
2254 #define RTYPE_CCC 0x10000
2255 #define RNUM_MASK 0x000ff
2256 #define RWARN 0x80000
2257
2258 #define GENERIC_REGISTER_NUMBERS \
2259 {"$0", RTYPE_NUM | 0}, \
2260 {"$1", RTYPE_NUM | 1}, \
2261 {"$2", RTYPE_NUM | 2}, \
2262 {"$3", RTYPE_NUM | 3}, \
2263 {"$4", RTYPE_NUM | 4}, \
2264 {"$5", RTYPE_NUM | 5}, \
2265 {"$6", RTYPE_NUM | 6}, \
2266 {"$7", RTYPE_NUM | 7}, \
2267 {"$8", RTYPE_NUM | 8}, \
2268 {"$9", RTYPE_NUM | 9}, \
2269 {"$10", RTYPE_NUM | 10}, \
2270 {"$11", RTYPE_NUM | 11}, \
2271 {"$12", RTYPE_NUM | 12}, \
2272 {"$13", RTYPE_NUM | 13}, \
2273 {"$14", RTYPE_NUM | 14}, \
2274 {"$15", RTYPE_NUM | 15}, \
2275 {"$16", RTYPE_NUM | 16}, \
2276 {"$17", RTYPE_NUM | 17}, \
2277 {"$18", RTYPE_NUM | 18}, \
2278 {"$19", RTYPE_NUM | 19}, \
2279 {"$20", RTYPE_NUM | 20}, \
2280 {"$21", RTYPE_NUM | 21}, \
2281 {"$22", RTYPE_NUM | 22}, \
2282 {"$23", RTYPE_NUM | 23}, \
2283 {"$24", RTYPE_NUM | 24}, \
2284 {"$25", RTYPE_NUM | 25}, \
2285 {"$26", RTYPE_NUM | 26}, \
2286 {"$27", RTYPE_NUM | 27}, \
2287 {"$28", RTYPE_NUM | 28}, \
2288 {"$29", RTYPE_NUM | 29}, \
2289 {"$30", RTYPE_NUM | 30}, \
2290 {"$31", RTYPE_NUM | 31}
2291
2292 #define FPU_REGISTER_NAMES \
2293 {"$f0", RTYPE_FPU | 0}, \
2294 {"$f1", RTYPE_FPU | 1}, \
2295 {"$f2", RTYPE_FPU | 2}, \
2296 {"$f3", RTYPE_FPU | 3}, \
2297 {"$f4", RTYPE_FPU | 4}, \
2298 {"$f5", RTYPE_FPU | 5}, \
2299 {"$f6", RTYPE_FPU | 6}, \
2300 {"$f7", RTYPE_FPU | 7}, \
2301 {"$f8", RTYPE_FPU | 8}, \
2302 {"$f9", RTYPE_FPU | 9}, \
2303 {"$f10", RTYPE_FPU | 10}, \
2304 {"$f11", RTYPE_FPU | 11}, \
2305 {"$f12", RTYPE_FPU | 12}, \
2306 {"$f13", RTYPE_FPU | 13}, \
2307 {"$f14", RTYPE_FPU | 14}, \
2308 {"$f15", RTYPE_FPU | 15}, \
2309 {"$f16", RTYPE_FPU | 16}, \
2310 {"$f17", RTYPE_FPU | 17}, \
2311 {"$f18", RTYPE_FPU | 18}, \
2312 {"$f19", RTYPE_FPU | 19}, \
2313 {"$f20", RTYPE_FPU | 20}, \
2314 {"$f21", RTYPE_FPU | 21}, \
2315 {"$f22", RTYPE_FPU | 22}, \
2316 {"$f23", RTYPE_FPU | 23}, \
2317 {"$f24", RTYPE_FPU | 24}, \
2318 {"$f25", RTYPE_FPU | 25}, \
2319 {"$f26", RTYPE_FPU | 26}, \
2320 {"$f27", RTYPE_FPU | 27}, \
2321 {"$f28", RTYPE_FPU | 28}, \
2322 {"$f29", RTYPE_FPU | 29}, \
2323 {"$f30", RTYPE_FPU | 30}, \
2324 {"$f31", RTYPE_FPU | 31}
2325
2326 #define FPU_CONDITION_CODE_NAMES \
2327 {"$fcc0", RTYPE_FCC | 0}, \
2328 {"$fcc1", RTYPE_FCC | 1}, \
2329 {"$fcc2", RTYPE_FCC | 2}, \
2330 {"$fcc3", RTYPE_FCC | 3}, \
2331 {"$fcc4", RTYPE_FCC | 4}, \
2332 {"$fcc5", RTYPE_FCC | 5}, \
2333 {"$fcc6", RTYPE_FCC | 6}, \
2334 {"$fcc7", RTYPE_FCC | 7}
2335
2336 #define COPROC_CONDITION_CODE_NAMES \
2337 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2338 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2339 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2340 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2341 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2342 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2343 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2344 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2345
2346 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2347 {"$a4", RTYPE_GP | 8}, \
2348 {"$a5", RTYPE_GP | 9}, \
2349 {"$a6", RTYPE_GP | 10}, \
2350 {"$a7", RTYPE_GP | 11}, \
2351 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2352 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2353 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2354 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2355 {"$t0", RTYPE_GP | 12}, \
2356 {"$t1", RTYPE_GP | 13}, \
2357 {"$t2", RTYPE_GP | 14}, \
2358 {"$t3", RTYPE_GP | 15}
2359
2360 #define O32_SYMBOLIC_REGISTER_NAMES \
2361 {"$t0", RTYPE_GP | 8}, \
2362 {"$t1", RTYPE_GP | 9}, \
2363 {"$t2", RTYPE_GP | 10}, \
2364 {"$t3", RTYPE_GP | 11}, \
2365 {"$t4", RTYPE_GP | 12}, \
2366 {"$t5", RTYPE_GP | 13}, \
2367 {"$t6", RTYPE_GP | 14}, \
2368 {"$t7", RTYPE_GP | 15}, \
2369 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2370 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2371 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2372 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2373
2374 /* Remaining symbolic register names */
2375 #define SYMBOLIC_REGISTER_NAMES \
2376 {"$zero", RTYPE_GP | 0}, \
2377 {"$at", RTYPE_GP | 1}, \
2378 {"$AT", RTYPE_GP | 1}, \
2379 {"$v0", RTYPE_GP | 2}, \
2380 {"$v1", RTYPE_GP | 3}, \
2381 {"$a0", RTYPE_GP | 4}, \
2382 {"$a1", RTYPE_GP | 5}, \
2383 {"$a2", RTYPE_GP | 6}, \
2384 {"$a3", RTYPE_GP | 7}, \
2385 {"$s0", RTYPE_GP | 16}, \
2386 {"$s1", RTYPE_GP | 17}, \
2387 {"$s2", RTYPE_GP | 18}, \
2388 {"$s3", RTYPE_GP | 19}, \
2389 {"$s4", RTYPE_GP | 20}, \
2390 {"$s5", RTYPE_GP | 21}, \
2391 {"$s6", RTYPE_GP | 22}, \
2392 {"$s7", RTYPE_GP | 23}, \
2393 {"$t8", RTYPE_GP | 24}, \
2394 {"$t9", RTYPE_GP | 25}, \
2395 {"$k0", RTYPE_GP | 26}, \
2396 {"$kt0", RTYPE_GP | 26}, \
2397 {"$k1", RTYPE_GP | 27}, \
2398 {"$kt1", RTYPE_GP | 27}, \
2399 {"$gp", RTYPE_GP | 28}, \
2400 {"$sp", RTYPE_GP | 29}, \
2401 {"$s8", RTYPE_GP | 30}, \
2402 {"$fp", RTYPE_GP | 30}, \
2403 {"$ra", RTYPE_GP | 31}
2404
2405 #define MIPS16_SPECIAL_REGISTER_NAMES \
2406 {"$pc", RTYPE_PC | 0}
2407
2408 #define MDMX_VECTOR_REGISTER_NAMES \
2409 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2410 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2411 {"$v2", RTYPE_VEC | 2}, \
2412 {"$v3", RTYPE_VEC | 3}, \
2413 {"$v4", RTYPE_VEC | 4}, \
2414 {"$v5", RTYPE_VEC | 5}, \
2415 {"$v6", RTYPE_VEC | 6}, \
2416 {"$v7", RTYPE_VEC | 7}, \
2417 {"$v8", RTYPE_VEC | 8}, \
2418 {"$v9", RTYPE_VEC | 9}, \
2419 {"$v10", RTYPE_VEC | 10}, \
2420 {"$v11", RTYPE_VEC | 11}, \
2421 {"$v12", RTYPE_VEC | 12}, \
2422 {"$v13", RTYPE_VEC | 13}, \
2423 {"$v14", RTYPE_VEC | 14}, \
2424 {"$v15", RTYPE_VEC | 15}, \
2425 {"$v16", RTYPE_VEC | 16}, \
2426 {"$v17", RTYPE_VEC | 17}, \
2427 {"$v18", RTYPE_VEC | 18}, \
2428 {"$v19", RTYPE_VEC | 19}, \
2429 {"$v20", RTYPE_VEC | 20}, \
2430 {"$v21", RTYPE_VEC | 21}, \
2431 {"$v22", RTYPE_VEC | 22}, \
2432 {"$v23", RTYPE_VEC | 23}, \
2433 {"$v24", RTYPE_VEC | 24}, \
2434 {"$v25", RTYPE_VEC | 25}, \
2435 {"$v26", RTYPE_VEC | 26}, \
2436 {"$v27", RTYPE_VEC | 27}, \
2437 {"$v28", RTYPE_VEC | 28}, \
2438 {"$v29", RTYPE_VEC | 29}, \
2439 {"$v30", RTYPE_VEC | 30}, \
2440 {"$v31", RTYPE_VEC | 31}
2441
2442 #define MIPS_DSP_ACCUMULATOR_NAMES \
2443 {"$ac0", RTYPE_ACC | 0}, \
2444 {"$ac1", RTYPE_ACC | 1}, \
2445 {"$ac2", RTYPE_ACC | 2}, \
2446 {"$ac3", RTYPE_ACC | 3}
2447
2448 static const struct regname reg_names[] = {
2449 GENERIC_REGISTER_NUMBERS,
2450 FPU_REGISTER_NAMES,
2451 FPU_CONDITION_CODE_NAMES,
2452 COPROC_CONDITION_CODE_NAMES,
2453
2454 /* The $txx registers depends on the abi,
2455 these will be added later into the symbol table from
2456 one of the tables below once mips_abi is set after
2457 parsing of arguments from the command line. */
2458 SYMBOLIC_REGISTER_NAMES,
2459
2460 MIPS16_SPECIAL_REGISTER_NAMES,
2461 MDMX_VECTOR_REGISTER_NAMES,
2462 MIPS_DSP_ACCUMULATOR_NAMES,
2463 {0, 0}
2464 };
2465
2466 static const struct regname reg_names_o32[] = {
2467 O32_SYMBOLIC_REGISTER_NAMES,
2468 {0, 0}
2469 };
2470
2471 static const struct regname reg_names_n32n64[] = {
2472 N32N64_SYMBOLIC_REGISTER_NAMES,
2473 {0, 0}
2474 };
2475
2476 /* Check if S points at a valid register specifier according to TYPES.
2477 If so, then return 1, advance S to consume the specifier and store
2478 the register's number in REGNOP, otherwise return 0. */
2479
2480 static int
2481 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2482 {
2483 symbolS *symbolP;
2484 char *e;
2485 char save_c;
2486 int reg = -1;
2487
2488 /* Find end of name. */
2489 e = *s;
2490 if (is_name_beginner (*e))
2491 ++e;
2492 while (is_part_of_name (*e))
2493 ++e;
2494
2495 /* Terminate name. */
2496 save_c = *e;
2497 *e = '\0';
2498
2499 /* Look for a register symbol. */
2500 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2501 {
2502 int r = S_GET_VALUE (symbolP);
2503 if (r & types)
2504 reg = r & RNUM_MASK;
2505 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2506 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
2507 reg = (r & RNUM_MASK) - 2;
2508 }
2509 /* Else see if this is a register defined in an itbl entry. */
2510 else if ((types & RTYPE_GP) && itbl_have_entries)
2511 {
2512 char *n = *s;
2513 unsigned long r;
2514
2515 if (*n == '$')
2516 ++n;
2517 if (itbl_get_reg_val (n, &r))
2518 reg = r & RNUM_MASK;
2519 }
2520
2521 /* Advance to next token if a register was recognised. */
2522 if (reg >= 0)
2523 *s = e;
2524 else if (types & RWARN)
2525 as_warn (_("Unrecognized register name `%s'"), *s);
2526
2527 *e = save_c;
2528 if (regnop)
2529 *regnop = reg;
2530 return reg >= 0;
2531 }
2532
2533 /* Check if S points at a valid register list according to TYPES.
2534 If so, then return 1, advance S to consume the list and store
2535 the registers present on the list as a bitmask of ones in REGLISTP,
2536 otherwise return 0. A valid list comprises a comma-separated
2537 enumeration of valid single registers and/or dash-separated
2538 contiguous register ranges as determined by their numbers.
2539
2540 As a special exception if one of s0-s7 registers is specified as
2541 the range's lower delimiter and s8 (fp) is its upper one, then no
2542 registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2543 are selected; they have to be listed separately if needed. */
2544
2545 static int
2546 reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2547 {
2548 unsigned int reglist = 0;
2549 unsigned int lastregno;
2550 bfd_boolean ok = TRUE;
2551 unsigned int regmask;
2552 char *s_endlist = *s;
2553 char *s_reset = *s;
2554 unsigned int regno;
2555
2556 while (reg_lookup (s, types, &regno))
2557 {
2558 lastregno = regno;
2559 if (**s == '-')
2560 {
2561 (*s)++;
2562 ok = reg_lookup (s, types, &lastregno);
2563 if (ok && lastregno < regno)
2564 ok = FALSE;
2565 if (!ok)
2566 break;
2567 }
2568
2569 if (lastregno == FP && regno >= S0 && regno <= S7)
2570 {
2571 lastregno = S7;
2572 reglist |= 1 << FP;
2573 }
2574 regmask = 1 << lastregno;
2575 regmask = (regmask << 1) - 1;
2576 regmask ^= (1 << regno) - 1;
2577 reglist |= regmask;
2578
2579 s_endlist = *s;
2580 if (**s != ',')
2581 break;
2582 (*s)++;
2583 }
2584
2585 if (ok)
2586 *s = s_endlist;
2587 else
2588 *s = s_reset;
2589 if (reglistp)
2590 *reglistp = reglist;
2591 return ok && reglist != 0;
2592 }
2593
2594 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
2595 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
2596
2597 static bfd_boolean
2598 is_opcode_valid (const struct mips_opcode *mo)
2599 {
2600 int isa = mips_opts.isa;
2601 int ase = mips_opts.ase;
2602 int fp_s, fp_d;
2603 unsigned int i;
2604
2605 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2606 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2607 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
2608 ase |= mips_ases[i].flags64;
2609
2610 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
2611 return FALSE;
2612
2613 /* Check whether the instruction or macro requires single-precision or
2614 double-precision floating-point support. Note that this information is
2615 stored differently in the opcode table for insns and macros. */
2616 if (mo->pinfo == INSN_MACRO)
2617 {
2618 fp_s = mo->pinfo2 & INSN2_M_FP_S;
2619 fp_d = mo->pinfo2 & INSN2_M_FP_D;
2620 }
2621 else
2622 {
2623 fp_s = mo->pinfo & FP_S;
2624 fp_d = mo->pinfo & FP_D;
2625 }
2626
2627 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2628 return FALSE;
2629
2630 if (fp_s && mips_opts.soft_float)
2631 return FALSE;
2632
2633 return TRUE;
2634 }
2635
2636 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
2637 selected ISA and architecture. */
2638
2639 static bfd_boolean
2640 is_opcode_valid_16 (const struct mips_opcode *mo)
2641 {
2642 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
2643 }
2644
2645 /* Return TRUE if the size of the microMIPS opcode MO matches one
2646 explicitly requested. Always TRUE in the standard MIPS mode. */
2647
2648 static bfd_boolean
2649 is_size_valid (const struct mips_opcode *mo)
2650 {
2651 if (!mips_opts.micromips)
2652 return TRUE;
2653
2654 if (mips_opts.insn32)
2655 {
2656 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
2657 return FALSE;
2658 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
2659 return FALSE;
2660 }
2661 if (!forced_insn_length)
2662 return TRUE;
2663 if (mo->pinfo == INSN_MACRO)
2664 return FALSE;
2665 return forced_insn_length == micromips_insn_length (mo);
2666 }
2667
2668 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
2669 of the preceding instruction. Always TRUE in the standard MIPS mode.
2670
2671 We don't accept macros in 16-bit delay slots to avoid a case where
2672 a macro expansion fails because it relies on a preceding 32-bit real
2673 instruction to have matched and does not handle the operands correctly.
2674 The only macros that may expand to 16-bit instructions are JAL that
2675 cannot be placed in a delay slot anyway, and corner cases of BALIGN
2676 and BGT (that likewise cannot be placed in a delay slot) that decay to
2677 a NOP. In all these cases the macros precede any corresponding real
2678 instruction definitions in the opcode table, so they will match in the
2679 second pass where the size of the delay slot is ignored and therefore
2680 produce correct code. */
2681
2682 static bfd_boolean
2683 is_delay_slot_valid (const struct mips_opcode *mo)
2684 {
2685 if (!mips_opts.micromips)
2686 return TRUE;
2687
2688 if (mo->pinfo == INSN_MACRO)
2689 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
2690 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2691 && micromips_insn_length (mo) != 4)
2692 return FALSE;
2693 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2694 && micromips_insn_length (mo) != 2)
2695 return FALSE;
2696
2697 return TRUE;
2698 }
2699
2700 /* This function is called once, at assembler startup time. It should set up
2701 all the tables, etc. that the MD part of the assembler will need. */
2702
2703 void
2704 md_begin (void)
2705 {
2706 const char *retval = NULL;
2707 int i = 0;
2708 int broken = 0;
2709
2710 if (mips_pic != NO_PIC)
2711 {
2712 if (g_switch_seen && g_switch_value != 0)
2713 as_bad (_("-G may not be used in position-independent code"));
2714 g_switch_value = 0;
2715 }
2716
2717 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
2718 as_warn (_("Could not set architecture and machine"));
2719
2720 op_hash = hash_new ();
2721
2722 for (i = 0; i < NUMOPCODES;)
2723 {
2724 const char *name = mips_opcodes[i].name;
2725
2726 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
2727 if (retval != NULL)
2728 {
2729 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2730 mips_opcodes[i].name, retval);
2731 /* Probably a memory allocation problem? Give up now. */
2732 as_fatal (_("Broken assembler. No assembly attempted."));
2733 }
2734 do
2735 {
2736 if (mips_opcodes[i].pinfo != INSN_MACRO)
2737 {
2738 if (!validate_mips_insn (&mips_opcodes[i]))
2739 broken = 1;
2740 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2741 {
2742 create_insn (&nop_insn, mips_opcodes + i);
2743 if (mips_fix_loongson2f_nop)
2744 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
2745 nop_insn.fixed_p = 1;
2746 }
2747 }
2748 ++i;
2749 }
2750 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2751 }
2752
2753 mips16_op_hash = hash_new ();
2754
2755 i = 0;
2756 while (i < bfd_mips16_num_opcodes)
2757 {
2758 const char *name = mips16_opcodes[i].name;
2759
2760 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
2761 if (retval != NULL)
2762 as_fatal (_("internal: can't hash `%s': %s"),
2763 mips16_opcodes[i].name, retval);
2764 do
2765 {
2766 if (mips16_opcodes[i].pinfo != INSN_MACRO
2767 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2768 != mips16_opcodes[i].match))
2769 {
2770 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2771 mips16_opcodes[i].name, mips16_opcodes[i].args);
2772 broken = 1;
2773 }
2774 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2775 {
2776 create_insn (&mips16_nop_insn, mips16_opcodes + i);
2777 mips16_nop_insn.fixed_p = 1;
2778 }
2779 ++i;
2780 }
2781 while (i < bfd_mips16_num_opcodes
2782 && strcmp (mips16_opcodes[i].name, name) == 0);
2783 }
2784
2785 micromips_op_hash = hash_new ();
2786
2787 i = 0;
2788 while (i < bfd_micromips_num_opcodes)
2789 {
2790 const char *name = micromips_opcodes[i].name;
2791
2792 retval = hash_insert (micromips_op_hash, name,
2793 (void *) &micromips_opcodes[i]);
2794 if (retval != NULL)
2795 as_fatal (_("internal: can't hash `%s': %s"),
2796 micromips_opcodes[i].name, retval);
2797 do
2798 if (micromips_opcodes[i].pinfo != INSN_MACRO)
2799 {
2800 struct mips_cl_insn *micromips_nop_insn;
2801
2802 if (!validate_micromips_insn (&micromips_opcodes[i]))
2803 broken = 1;
2804
2805 if (micromips_insn_length (micromips_opcodes + i) == 2)
2806 micromips_nop_insn = &micromips_nop16_insn;
2807 else if (micromips_insn_length (micromips_opcodes + i) == 4)
2808 micromips_nop_insn = &micromips_nop32_insn;
2809 else
2810 continue;
2811
2812 if (micromips_nop_insn->insn_mo == NULL
2813 && strcmp (name, "nop") == 0)
2814 {
2815 create_insn (micromips_nop_insn, micromips_opcodes + i);
2816 micromips_nop_insn->fixed_p = 1;
2817 }
2818 }
2819 while (++i < bfd_micromips_num_opcodes
2820 && strcmp (micromips_opcodes[i].name, name) == 0);
2821 }
2822
2823 if (broken)
2824 as_fatal (_("Broken assembler. No assembly attempted."));
2825
2826 /* We add all the general register names to the symbol table. This
2827 helps us detect invalid uses of them. */
2828 for (i = 0; reg_names[i].name; i++)
2829 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
2830 reg_names[i].num, /* & RNUM_MASK, */
2831 &zero_address_frag));
2832 if (HAVE_NEWABI)
2833 for (i = 0; reg_names_n32n64[i].name; i++)
2834 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
2835 reg_names_n32n64[i].num, /* & RNUM_MASK, */
2836 &zero_address_frag));
2837 else
2838 for (i = 0; reg_names_o32[i].name; i++)
2839 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
2840 reg_names_o32[i].num, /* & RNUM_MASK, */
2841 &zero_address_frag));
2842
2843 mips_no_prev_insn ();
2844
2845 mips_gprmask = 0;
2846 mips_cprmask[0] = 0;
2847 mips_cprmask[1] = 0;
2848 mips_cprmask[2] = 0;
2849 mips_cprmask[3] = 0;
2850
2851 /* set the default alignment for the text section (2**2) */
2852 record_alignment (text_section, 2);
2853
2854 bfd_set_gp_size (stdoutput, g_switch_value);
2855
2856 /* On a native system other than VxWorks, sections must be aligned
2857 to 16 byte boundaries. When configured for an embedded ELF
2858 target, we don't bother. */
2859 if (strncmp (TARGET_OS, "elf", 3) != 0
2860 && strncmp (TARGET_OS, "vxworks", 7) != 0)
2861 {
2862 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2863 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2864 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2865 }
2866
2867 /* Create a .reginfo section for register masks and a .mdebug
2868 section for debugging information. */
2869 {
2870 segT seg;
2871 subsegT subseg;
2872 flagword flags;
2873 segT sec;
2874
2875 seg = now_seg;
2876 subseg = now_subseg;
2877
2878 /* The ABI says this section should be loaded so that the
2879 running program can access it. However, we don't load it
2880 if we are configured for an embedded target */
2881 flags = SEC_READONLY | SEC_DATA;
2882 if (strncmp (TARGET_OS, "elf", 3) != 0)
2883 flags |= SEC_ALLOC | SEC_LOAD;
2884
2885 if (mips_abi != N64_ABI)
2886 {
2887 sec = subseg_new (".reginfo", (subsegT) 0);
2888
2889 bfd_set_section_flags (stdoutput, sec, flags);
2890 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
2891
2892 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
2893 }
2894 else
2895 {
2896 /* The 64-bit ABI uses a .MIPS.options section rather than
2897 .reginfo section. */
2898 sec = subseg_new (".MIPS.options", (subsegT) 0);
2899 bfd_set_section_flags (stdoutput, sec, flags);
2900 bfd_set_section_alignment (stdoutput, sec, 3);
2901
2902 /* Set up the option header. */
2903 {
2904 Elf_Internal_Options opthdr;
2905 char *f;
2906
2907 opthdr.kind = ODK_REGINFO;
2908 opthdr.size = (sizeof (Elf_External_Options)
2909 + sizeof (Elf64_External_RegInfo));
2910 opthdr.section = 0;
2911 opthdr.info = 0;
2912 f = frag_more (sizeof (Elf_External_Options));
2913 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2914 (Elf_External_Options *) f);
2915
2916 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2917 }
2918 }
2919
2920 if (ECOFF_DEBUGGING)
2921 {
2922 sec = subseg_new (".mdebug", (subsegT) 0);
2923 (void) bfd_set_section_flags (stdoutput, sec,
2924 SEC_HAS_CONTENTS | SEC_READONLY);
2925 (void) bfd_set_section_alignment (stdoutput, sec, 2);
2926 }
2927 else if (mips_flag_pdr)
2928 {
2929 pdr_seg = subseg_new (".pdr", (subsegT) 0);
2930 (void) bfd_set_section_flags (stdoutput, pdr_seg,
2931 SEC_READONLY | SEC_RELOC
2932 | SEC_DEBUGGING);
2933 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2934 }
2935
2936 subseg_set (seg, subseg);
2937 }
2938
2939 if (! ECOFF_DEBUGGING)
2940 md_obj_begin ();
2941
2942 if (mips_fix_vr4120)
2943 init_vr4120_conflicts ();
2944 }
2945
2946 void
2947 md_mips_end (void)
2948 {
2949 mips_emit_delays ();
2950 if (! ECOFF_DEBUGGING)
2951 md_obj_end ();
2952 }
2953
2954 void
2955 md_assemble (char *str)
2956 {
2957 struct mips_cl_insn insn;
2958 bfd_reloc_code_real_type unused_reloc[3]
2959 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2960
2961 imm_expr.X_op = O_absent;
2962 imm2_expr.X_op = O_absent;
2963 offset_expr.X_op = O_absent;
2964 imm_reloc[0] = BFD_RELOC_UNUSED;
2965 imm_reloc[1] = BFD_RELOC_UNUSED;
2966 imm_reloc[2] = BFD_RELOC_UNUSED;
2967 offset_reloc[0] = BFD_RELOC_UNUSED;
2968 offset_reloc[1] = BFD_RELOC_UNUSED;
2969 offset_reloc[2] = BFD_RELOC_UNUSED;
2970
2971 mips_mark_labels ();
2972 mips_assembling_insn = TRUE;
2973
2974 if (mips_opts.mips16)
2975 mips16_ip (str, &insn);
2976 else
2977 {
2978 mips_ip (str, &insn);
2979 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2980 str, insn.insn_opcode));
2981 }
2982
2983 if (insn_error)
2984 as_bad ("%s `%s'", insn_error, str);
2985 else if (insn.insn_mo->pinfo == INSN_MACRO)
2986 {
2987 macro_start ();
2988 if (mips_opts.mips16)
2989 mips16_macro (&insn);
2990 else
2991 macro (&insn, str);
2992 macro_end ();
2993 }
2994 else
2995 {
2996 if (imm_expr.X_op != O_absent)
2997 append_insn (&insn, &imm_expr, imm_reloc, FALSE);
2998 else if (offset_expr.X_op != O_absent)
2999 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
3000 else
3001 append_insn (&insn, NULL, unused_reloc, FALSE);
3002 }
3003
3004 mips_assembling_insn = FALSE;
3005 }
3006
3007 /* Convenience functions for abstracting away the differences between
3008 MIPS16 and non-MIPS16 relocations. */
3009
3010 static inline bfd_boolean
3011 mips16_reloc_p (bfd_reloc_code_real_type reloc)
3012 {
3013 switch (reloc)
3014 {
3015 case BFD_RELOC_MIPS16_JMP:
3016 case BFD_RELOC_MIPS16_GPREL:
3017 case BFD_RELOC_MIPS16_GOT16:
3018 case BFD_RELOC_MIPS16_CALL16:
3019 case BFD_RELOC_MIPS16_HI16_S:
3020 case BFD_RELOC_MIPS16_HI16:
3021 case BFD_RELOC_MIPS16_LO16:
3022 return TRUE;
3023
3024 default:
3025 return FALSE;
3026 }
3027 }
3028
3029 static inline bfd_boolean
3030 micromips_reloc_p (bfd_reloc_code_real_type reloc)
3031 {
3032 switch (reloc)
3033 {
3034 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3035 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3036 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3037 case BFD_RELOC_MICROMIPS_GPREL16:
3038 case BFD_RELOC_MICROMIPS_JMP:
3039 case BFD_RELOC_MICROMIPS_HI16:
3040 case BFD_RELOC_MICROMIPS_HI16_S:
3041 case BFD_RELOC_MICROMIPS_LO16:
3042 case BFD_RELOC_MICROMIPS_LITERAL:
3043 case BFD_RELOC_MICROMIPS_GOT16:
3044 case BFD_RELOC_MICROMIPS_CALL16:
3045 case BFD_RELOC_MICROMIPS_GOT_HI16:
3046 case BFD_RELOC_MICROMIPS_GOT_LO16:
3047 case BFD_RELOC_MICROMIPS_CALL_HI16:
3048 case BFD_RELOC_MICROMIPS_CALL_LO16:
3049 case BFD_RELOC_MICROMIPS_SUB:
3050 case BFD_RELOC_MICROMIPS_GOT_PAGE:
3051 case BFD_RELOC_MICROMIPS_GOT_OFST:
3052 case BFD_RELOC_MICROMIPS_GOT_DISP:
3053 case BFD_RELOC_MICROMIPS_HIGHEST:
3054 case BFD_RELOC_MICROMIPS_HIGHER:
3055 case BFD_RELOC_MICROMIPS_SCN_DISP:
3056 case BFD_RELOC_MICROMIPS_JALR:
3057 return TRUE;
3058
3059 default:
3060 return FALSE;
3061 }
3062 }
3063
3064 static inline bfd_boolean
3065 jmp_reloc_p (bfd_reloc_code_real_type reloc)
3066 {
3067 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
3068 }
3069
3070 static inline bfd_boolean
3071 got16_reloc_p (bfd_reloc_code_real_type reloc)
3072 {
3073 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
3074 || reloc == BFD_RELOC_MICROMIPS_GOT16);
3075 }
3076
3077 static inline bfd_boolean
3078 hi16_reloc_p (bfd_reloc_code_real_type reloc)
3079 {
3080 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
3081 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
3082 }
3083
3084 static inline bfd_boolean
3085 lo16_reloc_p (bfd_reloc_code_real_type reloc)
3086 {
3087 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
3088 || reloc == BFD_RELOC_MICROMIPS_LO16);
3089 }
3090
3091 static inline bfd_boolean
3092 jalr_reloc_p (bfd_reloc_code_real_type reloc)
3093 {
3094 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
3095 }
3096
3097 /* Return true if RELOC is a PC-relative relocation that does not have
3098 full address range. */
3099
3100 static inline bfd_boolean
3101 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
3102 {
3103 switch (reloc)
3104 {
3105 case BFD_RELOC_16_PCREL_S2:
3106 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3107 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3108 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3109 return TRUE;
3110
3111 case BFD_RELOC_32_PCREL:
3112 return HAVE_64BIT_ADDRESSES;
3113
3114 default:
3115 return FALSE;
3116 }
3117 }
3118
3119 /* Return true if the given relocation might need a matching %lo().
3120 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
3121 need a matching %lo() when applied to local symbols. */
3122
3123 static inline bfd_boolean
3124 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
3125 {
3126 return (HAVE_IN_PLACE_ADDENDS
3127 && (hi16_reloc_p (reloc)
3128 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
3129 all GOT16 relocations evaluate to "G". */
3130 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
3131 }
3132
3133 /* Return the type of %lo() reloc needed by RELOC, given that
3134 reloc_needs_lo_p. */
3135
3136 static inline bfd_reloc_code_real_type
3137 matching_lo_reloc (bfd_reloc_code_real_type reloc)
3138 {
3139 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
3140 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
3141 : BFD_RELOC_LO16));
3142 }
3143
3144 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
3145 relocation. */
3146
3147 static inline bfd_boolean
3148 fixup_has_matching_lo_p (fixS *fixp)
3149 {
3150 return (fixp->fx_next != NULL
3151 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
3152 && fixp->fx_addsy == fixp->fx_next->fx_addsy
3153 && fixp->fx_offset == fixp->fx_next->fx_offset);
3154 }
3155
3156 /* This function returns true if modifying a register requires a
3157 delay. */
3158
3159 static int
3160 reg_needs_delay (unsigned int reg)
3161 {
3162 unsigned long prev_pinfo;
3163
3164 prev_pinfo = history[0].insn_mo->pinfo;
3165 if (! mips_opts.noreorder
3166 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
3167 && ! gpr_interlocks)
3168 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
3169 && ! cop_interlocks)))
3170 {
3171 /* A load from a coprocessor or from memory. All load delays
3172 delay the use of general register rt for one instruction. */
3173 /* Itbl support may require additional care here. */
3174 know (prev_pinfo & INSN_WRITE_GPR_T);
3175 if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
3176 return 1;
3177 }
3178
3179 return 0;
3180 }
3181
3182 /* Move all labels in LABELS to the current insertion point. TEXT_P
3183 says whether the labels refer to text or data. */
3184
3185 static void
3186 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
3187 {
3188 struct insn_label_list *l;
3189 valueT val;
3190
3191 for (l = labels; l != NULL; l = l->next)
3192 {
3193 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
3194 symbol_set_frag (l->label, frag_now);
3195 val = (valueT) frag_now_fix ();
3196 /* MIPS16/microMIPS text labels are stored as odd. */
3197 if (text_p && HAVE_CODE_COMPRESSION)
3198 ++val;
3199 S_SET_VALUE (l->label, val);
3200 }
3201 }
3202
3203 /* Move all labels in insn_labels to the current insertion point
3204 and treat them as text labels. */
3205
3206 static void
3207 mips_move_text_labels (void)
3208 {
3209 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
3210 }
3211
3212 static bfd_boolean
3213 s_is_linkonce (symbolS *sym, segT from_seg)
3214 {
3215 bfd_boolean linkonce = FALSE;
3216 segT symseg = S_GET_SEGMENT (sym);
3217
3218 if (symseg != from_seg && !S_IS_LOCAL (sym))
3219 {
3220 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
3221 linkonce = TRUE;
3222 /* The GNU toolchain uses an extension for ELF: a section
3223 beginning with the magic string .gnu.linkonce is a
3224 linkonce section. */
3225 if (strncmp (segment_name (symseg), ".gnu.linkonce",
3226 sizeof ".gnu.linkonce" - 1) == 0)
3227 linkonce = TRUE;
3228 }
3229 return linkonce;
3230 }
3231
3232 /* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
3233 linker to handle them specially, such as generating jalx instructions
3234 when needed. We also make them odd for the duration of the assembly,
3235 in order to generate the right sort of code. We will make them even
3236 in the adjust_symtab routine, while leaving them marked. This is
3237 convenient for the debugger and the disassembler. The linker knows
3238 to make them odd again. */
3239
3240 static void
3241 mips_compressed_mark_label (symbolS *label)
3242 {
3243 gas_assert (HAVE_CODE_COMPRESSION);
3244
3245 if (mips_opts.mips16)
3246 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
3247 else
3248 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
3249 if ((S_GET_VALUE (label) & 1) == 0
3250 /* Don't adjust the address if the label is global or weak, or
3251 in a link-once section, since we'll be emitting symbol reloc
3252 references to it which will be patched up by the linker, and
3253 the final value of the symbol may or may not be MIPS16/microMIPS. */
3254 && !S_IS_WEAK (label)
3255 && !S_IS_EXTERNAL (label)
3256 && !s_is_linkonce (label, now_seg))
3257 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
3258 }
3259
3260 /* Mark preceding MIPS16 or microMIPS instruction labels. */
3261
3262 static void
3263 mips_compressed_mark_labels (void)
3264 {
3265 struct insn_label_list *l;
3266
3267 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
3268 mips_compressed_mark_label (l->label);
3269 }
3270
3271 /* End the current frag. Make it a variant frag and record the
3272 relaxation info. */
3273
3274 static void
3275 relax_close_frag (void)
3276 {
3277 mips_macro_warning.first_frag = frag_now;
3278 frag_var (rs_machine_dependent, 0, 0,
3279 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
3280 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
3281
3282 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
3283 mips_relax.first_fixup = 0;
3284 }
3285
3286 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
3287 See the comment above RELAX_ENCODE for more details. */
3288
3289 static void
3290 relax_start (symbolS *symbol)
3291 {
3292 gas_assert (mips_relax.sequence == 0);
3293 mips_relax.sequence = 1;
3294 mips_relax.symbol = symbol;
3295 }
3296
3297 /* Start generating the second version of a relaxable sequence.
3298 See the comment above RELAX_ENCODE for more details. */
3299
3300 static void
3301 relax_switch (void)
3302 {
3303 gas_assert (mips_relax.sequence == 1);
3304 mips_relax.sequence = 2;
3305 }
3306
3307 /* End the current relaxable sequence. */
3308
3309 static void
3310 relax_end (void)
3311 {
3312 gas_assert (mips_relax.sequence == 2);
3313 relax_close_frag ();
3314 mips_relax.sequence = 0;
3315 }
3316
3317 /* Return true if IP is a delayed branch or jump. */
3318
3319 static inline bfd_boolean
3320 delayed_branch_p (const struct mips_cl_insn *ip)
3321 {
3322 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
3323 | INSN_COND_BRANCH_DELAY
3324 | INSN_COND_BRANCH_LIKELY)) != 0;
3325 }
3326
3327 /* Return true if IP is a compact branch or jump. */
3328
3329 static inline bfd_boolean
3330 compact_branch_p (const struct mips_cl_insn *ip)
3331 {
3332 if (mips_opts.mips16)
3333 return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
3334 | MIPS16_INSN_COND_BRANCH)) != 0;
3335 else
3336 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
3337 | INSN2_COND_BRANCH)) != 0;
3338 }
3339
3340 /* Return true if IP is an unconditional branch or jump. */
3341
3342 static inline bfd_boolean
3343 uncond_branch_p (const struct mips_cl_insn *ip)
3344 {
3345 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
3346 || (mips_opts.mips16
3347 ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
3348 : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
3349 }
3350
3351 /* Return true if IP is a branch-likely instruction. */
3352
3353 static inline bfd_boolean
3354 branch_likely_p (const struct mips_cl_insn *ip)
3355 {
3356 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
3357 }
3358
3359 /* Return the type of nop that should be used to fill the delay slot
3360 of delayed branch IP. */
3361
3362 static struct mips_cl_insn *
3363 get_delay_slot_nop (const struct mips_cl_insn *ip)
3364 {
3365 if (mips_opts.micromips
3366 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3367 return &micromips_nop32_insn;
3368 return NOP_INSN;
3369 }
3370
3371 /* Return the mask of core registers that IP reads or writes. */
3372
3373 static unsigned int
3374 gpr_mod_mask (const struct mips_cl_insn *ip)
3375 {
3376 unsigned long pinfo2;
3377 unsigned int mask;
3378
3379 mask = 0;
3380 pinfo2 = ip->insn_mo->pinfo2;
3381 if (mips_opts.micromips)
3382 {
3383 if (pinfo2 & INSN2_MOD_GPR_MD)
3384 mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
3385 if (pinfo2 & INSN2_MOD_GPR_MF)
3386 mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
3387 if (pinfo2 & INSN2_MOD_SP)
3388 mask |= 1 << SP;
3389 }
3390 return mask;
3391 }
3392
3393 /* Return the mask of core registers that IP reads. */
3394
3395 static unsigned int
3396 gpr_read_mask (const struct mips_cl_insn *ip)
3397 {
3398 unsigned long pinfo, pinfo2;
3399 unsigned int mask;
3400
3401 mask = gpr_mod_mask (ip);
3402 pinfo = ip->insn_mo->pinfo;
3403 pinfo2 = ip->insn_mo->pinfo2;
3404 if (mips_opts.mips16)
3405 {
3406 if (pinfo & MIPS16_INSN_READ_X)
3407 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3408 if (pinfo & MIPS16_INSN_READ_Y)
3409 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3410 if (pinfo & MIPS16_INSN_READ_T)
3411 mask |= 1 << TREG;
3412 if (pinfo & MIPS16_INSN_READ_SP)
3413 mask |= 1 << SP;
3414 if (pinfo & MIPS16_INSN_READ_31)
3415 mask |= 1 << RA;
3416 if (pinfo & MIPS16_INSN_READ_Z)
3417 mask |= 1 << (mips16_to_32_reg_map
3418 [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
3419 if (pinfo & MIPS16_INSN_READ_GPR_X)
3420 mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
3421 }
3422 else
3423 {
3424 if (pinfo2 & INSN2_READ_GPR_D)
3425 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3426 if (pinfo & INSN_READ_GPR_T)
3427 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3428 if (pinfo & INSN_READ_GPR_S)
3429 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3430 if (pinfo2 & INSN2_READ_GP)
3431 mask |= 1 << GP;
3432 if (pinfo2 & INSN2_READ_GPR_31)
3433 mask |= 1 << RA;
3434 if (pinfo2 & INSN2_READ_GPR_Z)
3435 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3436 }
3437 if (mips_opts.micromips)
3438 {
3439 if (pinfo2 & INSN2_READ_GPR_MC)
3440 mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3441 if (pinfo2 & INSN2_READ_GPR_ME)
3442 mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3443 if (pinfo2 & INSN2_READ_GPR_MG)
3444 mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3445 if (pinfo2 & INSN2_READ_GPR_MJ)
3446 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3447 if (pinfo2 & INSN2_READ_GPR_MMN)
3448 {
3449 mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3450 mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3451 }
3452 if (pinfo2 & INSN2_READ_GPR_MP)
3453 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3454 if (pinfo2 & INSN2_READ_GPR_MQ)
3455 mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3456 }
3457 /* Don't include register 0. */
3458 return mask & ~1;
3459 }
3460
3461 /* Return the mask of core registers that IP writes. */
3462
3463 static unsigned int
3464 gpr_write_mask (const struct mips_cl_insn *ip)
3465 {
3466 unsigned long pinfo, pinfo2;
3467 unsigned int mask;
3468
3469 mask = gpr_mod_mask (ip);
3470 pinfo = ip->insn_mo->pinfo;
3471 pinfo2 = ip->insn_mo->pinfo2;
3472 if (mips_opts.mips16)
3473 {
3474 if (pinfo & MIPS16_INSN_WRITE_X)
3475 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3476 if (pinfo & MIPS16_INSN_WRITE_Y)
3477 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3478 if (pinfo & MIPS16_INSN_WRITE_Z)
3479 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3480 if (pinfo & MIPS16_INSN_WRITE_T)
3481 mask |= 1 << TREG;
3482 if (pinfo & MIPS16_INSN_WRITE_SP)
3483 mask |= 1 << SP;
3484 if (pinfo & MIPS16_INSN_WRITE_31)
3485 mask |= 1 << RA;
3486 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3487 mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3488 }
3489 else
3490 {
3491 if (pinfo & INSN_WRITE_GPR_D)
3492 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3493 if (pinfo & INSN_WRITE_GPR_T)
3494 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3495 if (pinfo & INSN_WRITE_GPR_S)
3496 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3497 if (pinfo & INSN_WRITE_GPR_31)
3498 mask |= 1 << RA;
3499 if (pinfo2 & INSN2_WRITE_GPR_Z)
3500 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3501 }
3502 if (mips_opts.micromips)
3503 {
3504 if (pinfo2 & INSN2_WRITE_GPR_MB)
3505 mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3506 if (pinfo2 & INSN2_WRITE_GPR_MHI)
3507 {
3508 mask |= 1 << micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
3509 mask |= 1 << micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
3510 }
3511 if (pinfo2 & INSN2_WRITE_GPR_MJ)
3512 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3513 if (pinfo2 & INSN2_WRITE_GPR_MP)
3514 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3515 }
3516 /* Don't include register 0. */
3517 return mask & ~1;
3518 }
3519
3520 /* Return the mask of floating-point registers that IP reads. */
3521
3522 static unsigned int
3523 fpr_read_mask (const struct mips_cl_insn *ip)
3524 {
3525 unsigned long pinfo, pinfo2;
3526 unsigned int mask;
3527
3528 mask = 0;
3529 pinfo = ip->insn_mo->pinfo;
3530 pinfo2 = ip->insn_mo->pinfo2;
3531 if (!mips_opts.mips16)
3532 {
3533 if (pinfo2 & INSN2_READ_FPR_D)
3534 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3535 if (pinfo & INSN_READ_FPR_S)
3536 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3537 if (pinfo & INSN_READ_FPR_T)
3538 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3539 if (pinfo & INSN_READ_FPR_R)
3540 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
3541 if (pinfo2 & INSN2_READ_FPR_Z)
3542 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3543 }
3544 /* Conservatively treat all operands to an FP_D instruction are doubles.
3545 (This is overly pessimistic for things like cvt.d.s.) */
3546 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3547 mask |= mask << 1;
3548 return mask;
3549 }
3550
3551 /* Return the mask of floating-point registers that IP writes. */
3552
3553 static unsigned int
3554 fpr_write_mask (const struct mips_cl_insn *ip)
3555 {
3556 unsigned long pinfo, pinfo2;
3557 unsigned int mask;
3558
3559 mask = 0;
3560 pinfo = ip->insn_mo->pinfo;
3561 pinfo2 = ip->insn_mo->pinfo2;
3562 if (!mips_opts.mips16)
3563 {
3564 if (pinfo & INSN_WRITE_FPR_D)
3565 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3566 if (pinfo & INSN_WRITE_FPR_S)
3567 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3568 if (pinfo & INSN_WRITE_FPR_T)
3569 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3570 if (pinfo2 & INSN2_WRITE_FPR_Z)
3571 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3572 }
3573 /* Conservatively treat all operands to an FP_D instruction are doubles.
3574 (This is overly pessimistic for things like cvt.s.d.) */
3575 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3576 mask |= mask << 1;
3577 return mask;
3578 }
3579
3580 /* Classify an instruction according to the FIX_VR4120_* enumeration.
3581 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3582 by VR4120 errata. */
3583
3584 static unsigned int
3585 classify_vr4120_insn (const char *name)
3586 {
3587 if (strncmp (name, "macc", 4) == 0)
3588 return FIX_VR4120_MACC;
3589 if (strncmp (name, "dmacc", 5) == 0)
3590 return FIX_VR4120_DMACC;
3591 if (strncmp (name, "mult", 4) == 0)
3592 return FIX_VR4120_MULT;
3593 if (strncmp (name, "dmult", 5) == 0)
3594 return FIX_VR4120_DMULT;
3595 if (strstr (name, "div"))
3596 return FIX_VR4120_DIV;
3597 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3598 return FIX_VR4120_MTHILO;
3599 return NUM_FIX_VR4120_CLASSES;
3600 }
3601
3602 #define INSN_ERET 0x42000018
3603 #define INSN_DERET 0x4200001f
3604
3605 /* Return the number of instructions that must separate INSN1 and INSN2,
3606 where INSN1 is the earlier instruction. Return the worst-case value
3607 for any INSN2 if INSN2 is null. */
3608
3609 static unsigned int
3610 insns_between (const struct mips_cl_insn *insn1,
3611 const struct mips_cl_insn *insn2)
3612 {
3613 unsigned long pinfo1, pinfo2;
3614 unsigned int mask;
3615
3616 /* This function needs to know which pinfo flags are set for INSN2
3617 and which registers INSN2 uses. The former is stored in PINFO2 and
3618 the latter is tested via INSN2_USES_GPR. If INSN2 is null, PINFO2
3619 will have every flag set and INSN2_USES_GPR will always return true. */
3620 pinfo1 = insn1->insn_mo->pinfo;
3621 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
3622
3623 #define INSN2_USES_GPR(REG) \
3624 (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
3625
3626 /* For most targets, write-after-read dependencies on the HI and LO
3627 registers must be separated by at least two instructions. */
3628 if (!hilo_interlocks)
3629 {
3630 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3631 return 2;
3632 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3633 return 2;
3634 }
3635
3636 /* If we're working around r7000 errata, there must be two instructions
3637 between an mfhi or mflo and any instruction that uses the result. */
3638 if (mips_7000_hilo_fix
3639 && !mips_opts.micromips
3640 && MF_HILO_INSN (pinfo1)
3641 && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
3642 return 2;
3643
3644 /* If we're working around 24K errata, one instruction is required
3645 if an ERET or DERET is followed by a branch instruction. */
3646 if (mips_fix_24k && !mips_opts.micromips)
3647 {
3648 if (insn1->insn_opcode == INSN_ERET
3649 || insn1->insn_opcode == INSN_DERET)
3650 {
3651 if (insn2 == NULL
3652 || insn2->insn_opcode == INSN_ERET
3653 || insn2->insn_opcode == INSN_DERET
3654 || delayed_branch_p (insn2))
3655 return 1;
3656 }
3657 }
3658
3659 /* If working around VR4120 errata, check for combinations that need
3660 a single intervening instruction. */
3661 if (mips_fix_vr4120 && !mips_opts.micromips)
3662 {
3663 unsigned int class1, class2;
3664
3665 class1 = classify_vr4120_insn (insn1->insn_mo->name);
3666 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
3667 {
3668 if (insn2 == NULL)
3669 return 1;
3670 class2 = classify_vr4120_insn (insn2->insn_mo->name);
3671 if (vr4120_conflicts[class1] & (1 << class2))
3672 return 1;
3673 }
3674 }
3675
3676 if (!HAVE_CODE_COMPRESSION)
3677 {
3678 /* Check for GPR or coprocessor load delays. All such delays
3679 are on the RT register. */
3680 /* Itbl support may require additional care here. */
3681 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3682 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
3683 {
3684 know (pinfo1 & INSN_WRITE_GPR_T);
3685 if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
3686 return 1;
3687 }
3688
3689 /* Check for generic coprocessor hazards.
3690
3691 This case is not handled very well. There is no special
3692 knowledge of CP0 handling, and the coprocessors other than
3693 the floating point unit are not distinguished at all. */
3694 /* Itbl support may require additional care here. FIXME!
3695 Need to modify this to include knowledge about
3696 user specified delays! */
3697 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3698 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3699 {
3700 /* Handle cases where INSN1 writes to a known general coprocessor
3701 register. There must be a one instruction delay before INSN2
3702 if INSN2 reads that register, otherwise no delay is needed. */
3703 mask = fpr_write_mask (insn1);
3704 if (mask != 0)
3705 {
3706 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
3707 return 1;
3708 }
3709 else
3710 {
3711 /* Read-after-write dependencies on the control registers
3712 require a two-instruction gap. */
3713 if ((pinfo1 & INSN_WRITE_COND_CODE)
3714 && (pinfo2 & INSN_READ_COND_CODE))
3715 return 2;
3716
3717 /* We don't know exactly what INSN1 does. If INSN2 is
3718 also a coprocessor instruction, assume there must be
3719 a one instruction gap. */
3720 if (pinfo2 & INSN_COP)
3721 return 1;
3722 }
3723 }
3724
3725 /* Check for read-after-write dependencies on the coprocessor
3726 control registers in cases where INSN1 does not need a general
3727 coprocessor delay. This means that INSN1 is a floating point
3728 comparison instruction. */
3729 /* Itbl support may require additional care here. */
3730 else if (!cop_interlocks
3731 && (pinfo1 & INSN_WRITE_COND_CODE)
3732 && (pinfo2 & INSN_READ_COND_CODE))
3733 return 1;
3734 }
3735
3736 #undef INSN2_USES_GPR
3737
3738 return 0;
3739 }
3740
3741 /* Return the number of nops that would be needed to work around the
3742 VR4130 mflo/mfhi errata if instruction INSN immediately followed
3743 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
3744 that are contained within the first IGNORE instructions of HIST. */
3745
3746 static int
3747 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
3748 const struct mips_cl_insn *insn)
3749 {
3750 int i, j;
3751 unsigned int mask;
3752
3753 /* Check if the instruction writes to HI or LO. MTHI and MTLO
3754 are not affected by the errata. */
3755 if (insn != 0
3756 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3757 || strcmp (insn->insn_mo->name, "mtlo") == 0
3758 || strcmp (insn->insn_mo->name, "mthi") == 0))
3759 return 0;
3760
3761 /* Search for the first MFLO or MFHI. */
3762 for (i = 0; i < MAX_VR4130_NOPS; i++)
3763 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
3764 {
3765 /* Extract the destination register. */
3766 mask = gpr_write_mask (&hist[i]);
3767
3768 /* No nops are needed if INSN reads that register. */
3769 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
3770 return 0;
3771
3772 /* ...or if any of the intervening instructions do. */
3773 for (j = 0; j < i; j++)
3774 if (gpr_read_mask (&hist[j]) & mask)
3775 return 0;
3776
3777 if (i >= ignore)
3778 return MAX_VR4130_NOPS - i;
3779 }
3780 return 0;
3781 }
3782
3783 #define BASE_REG_EQ(INSN1, INSN2) \
3784 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3785 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3786
3787 /* Return the minimum alignment for this store instruction. */
3788
3789 static int
3790 fix_24k_align_to (const struct mips_opcode *mo)
3791 {
3792 if (strcmp (mo->name, "sh") == 0)
3793 return 2;
3794
3795 if (strcmp (mo->name, "swc1") == 0
3796 || strcmp (mo->name, "swc2") == 0
3797 || strcmp (mo->name, "sw") == 0
3798 || strcmp (mo->name, "sc") == 0
3799 || strcmp (mo->name, "s.s") == 0)
3800 return 4;
3801
3802 if (strcmp (mo->name, "sdc1") == 0
3803 || strcmp (mo->name, "sdc2") == 0
3804 || strcmp (mo->name, "s.d") == 0)
3805 return 8;
3806
3807 /* sb, swl, swr */
3808 return 1;
3809 }
3810
3811 struct fix_24k_store_info
3812 {
3813 /* Immediate offset, if any, for this store instruction. */
3814 short off;
3815 /* Alignment required by this store instruction. */
3816 int align_to;
3817 /* True for register offsets. */
3818 int register_offset;
3819 };
3820
3821 /* Comparison function used by qsort. */
3822
3823 static int
3824 fix_24k_sort (const void *a, const void *b)
3825 {
3826 const struct fix_24k_store_info *pos1 = a;
3827 const struct fix_24k_store_info *pos2 = b;
3828
3829 return (pos1->off - pos2->off);
3830 }
3831
3832 /* INSN is a store instruction. Try to record the store information
3833 in STINFO. Return false if the information isn't known. */
3834
3835 static bfd_boolean
3836 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
3837 const struct mips_cl_insn *insn)
3838 {
3839 /* The instruction must have a known offset. */
3840 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3841 return FALSE;
3842
3843 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3844 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3845 return TRUE;
3846 }
3847
3848 /* Return the number of nops that would be needed to work around the 24k
3849 "lost data on stores during refill" errata if instruction INSN
3850 immediately followed the 2 instructions described by HIST.
3851 Ignore hazards that are contained within the first IGNORE
3852 instructions of HIST.
3853
3854 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3855 for the data cache refills and store data. The following describes
3856 the scenario where the store data could be lost.
3857
3858 * A data cache miss, due to either a load or a store, causing fill
3859 data to be supplied by the memory subsystem
3860 * The first three doublewords of fill data are returned and written
3861 into the cache
3862 * A sequence of four stores occurs in consecutive cycles around the
3863 final doubleword of the fill:
3864 * Store A
3865 * Store B
3866 * Store C
3867 * Zero, One or more instructions
3868 * Store D
3869
3870 The four stores A-D must be to different doublewords of the line that
3871 is being filled. The fourth instruction in the sequence above permits
3872 the fill of the final doubleword to be transferred from the FSB into
3873 the cache. In the sequence above, the stores may be either integer
3874 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3875 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3876 different doublewords on the line. If the floating point unit is
3877 running in 1:2 mode, it is not possible to create the sequence above
3878 using only floating point store instructions.
3879
3880 In this case, the cache line being filled is incorrectly marked
3881 invalid, thereby losing the data from any store to the line that
3882 occurs between the original miss and the completion of the five
3883 cycle sequence shown above.
3884
3885 The workarounds are:
3886
3887 * Run the data cache in write-through mode.
3888 * Insert a non-store instruction between
3889 Store A and Store B or Store B and Store C. */
3890
3891 static int
3892 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
3893 const struct mips_cl_insn *insn)
3894 {
3895 struct fix_24k_store_info pos[3];
3896 int align, i, base_offset;
3897
3898 if (ignore >= 2)
3899 return 0;
3900
3901 /* If the previous instruction wasn't a store, there's nothing to
3902 worry about. */
3903 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3904 return 0;
3905
3906 /* If the instructions after the previous one are unknown, we have
3907 to assume the worst. */
3908 if (!insn)
3909 return 1;
3910
3911 /* Check whether we are dealing with three consecutive stores. */
3912 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3913 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3914 return 0;
3915
3916 /* If we don't know the relationship between the store addresses,
3917 assume the worst. */
3918 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
3919 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3920 return 1;
3921
3922 if (!fix_24k_record_store_info (&pos[0], insn)
3923 || !fix_24k_record_store_info (&pos[1], &hist[0])
3924 || !fix_24k_record_store_info (&pos[2], &hist[1]))
3925 return 1;
3926
3927 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3928
3929 /* Pick a value of ALIGN and X such that all offsets are adjusted by
3930 X bytes and such that the base register + X is known to be aligned
3931 to align bytes. */
3932
3933 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3934 align = 8;
3935 else
3936 {
3937 align = pos[0].align_to;
3938 base_offset = pos[0].off;
3939 for (i = 1; i < 3; i++)
3940 if (align < pos[i].align_to)
3941 {
3942 align = pos[i].align_to;
3943 base_offset = pos[i].off;
3944 }
3945 for (i = 0; i < 3; i++)
3946 pos[i].off -= base_offset;
3947 }
3948
3949 pos[0].off &= ~align + 1;
3950 pos[1].off &= ~align + 1;
3951 pos[2].off &= ~align + 1;
3952
3953 /* If any two stores write to the same chunk, they also write to the
3954 same doubleword. The offsets are still sorted at this point. */
3955 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3956 return 0;
3957
3958 /* A range of at least 9 bytes is needed for the stores to be in
3959 non-overlapping doublewords. */
3960 if (pos[2].off - pos[0].off <= 8)
3961 return 0;
3962
3963 if (pos[2].off - pos[1].off >= 24
3964 || pos[1].off - pos[0].off >= 24
3965 || pos[2].off - pos[0].off >= 32)
3966 return 0;
3967
3968 return 1;
3969 }
3970
3971 /* Return the number of nops that would be needed if instruction INSN
3972 immediately followed the MAX_NOPS instructions given by HIST,
3973 where HIST[0] is the most recent instruction. Ignore hazards
3974 between INSN and the first IGNORE instructions in HIST.
3975
3976 If INSN is null, return the worse-case number of nops for any
3977 instruction. */
3978
3979 static int
3980 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
3981 const struct mips_cl_insn *insn)
3982 {
3983 int i, nops, tmp_nops;
3984
3985 nops = 0;
3986 for (i = ignore; i < MAX_DELAY_NOPS; i++)
3987 {
3988 tmp_nops = insns_between (hist + i, insn) - i;
3989 if (tmp_nops > nops)
3990 nops = tmp_nops;
3991 }
3992
3993 if (mips_fix_vr4130 && !mips_opts.micromips)
3994 {
3995 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
3996 if (tmp_nops > nops)
3997 nops = tmp_nops;
3998 }
3999
4000 if (mips_fix_24k && !mips_opts.micromips)
4001 {
4002 tmp_nops = nops_for_24k (ignore, hist, insn);
4003 if (tmp_nops > nops)
4004 nops = tmp_nops;
4005 }
4006
4007 return nops;
4008 }
4009
4010 /* The variable arguments provide NUM_INSNS extra instructions that
4011 might be added to HIST. Return the largest number of nops that
4012 would be needed after the extended sequence, ignoring hazards
4013 in the first IGNORE instructions. */
4014
4015 static int
4016 nops_for_sequence (int num_insns, int ignore,
4017 const struct mips_cl_insn *hist, ...)
4018 {
4019 va_list args;
4020 struct mips_cl_insn buffer[MAX_NOPS];
4021 struct mips_cl_insn *cursor;
4022 int nops;
4023
4024 va_start (args, hist);
4025 cursor = buffer + num_insns;
4026 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
4027 while (cursor > buffer)
4028 *--cursor = *va_arg (args, const struct mips_cl_insn *);
4029
4030 nops = nops_for_insn (ignore, buffer, NULL);
4031 va_end (args);
4032 return nops;
4033 }
4034
4035 /* Like nops_for_insn, but if INSN is a branch, take into account the
4036 worst-case delay for the branch target. */
4037
4038 static int
4039 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
4040 const struct mips_cl_insn *insn)
4041 {
4042 int nops, tmp_nops;
4043
4044 nops = nops_for_insn (ignore, hist, insn);
4045 if (delayed_branch_p (insn))
4046 {
4047 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
4048 hist, insn, get_delay_slot_nop (insn));
4049 if (tmp_nops > nops)
4050 nops = tmp_nops;
4051 }
4052 else if (compact_branch_p (insn))
4053 {
4054 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
4055 if (tmp_nops > nops)
4056 nops = tmp_nops;
4057 }
4058 return nops;
4059 }
4060
4061 /* Fix NOP issue: Replace nops by "or at,at,zero". */
4062
4063 static void
4064 fix_loongson2f_nop (struct mips_cl_insn * ip)
4065 {
4066 gas_assert (!HAVE_CODE_COMPRESSION);
4067 if (strcmp (ip->insn_mo->name, "nop") == 0)
4068 ip->insn_opcode = LOONGSON2F_NOP_INSN;
4069 }
4070
4071 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
4072 jr target pc &= 'hffff_ffff_cfff_ffff. */
4073
4074 static void
4075 fix_loongson2f_jump (struct mips_cl_insn * ip)
4076 {
4077 gas_assert (!HAVE_CODE_COMPRESSION);
4078 if (strcmp (ip->insn_mo->name, "j") == 0
4079 || strcmp (ip->insn_mo->name, "jr") == 0
4080 || strcmp (ip->insn_mo->name, "jalr") == 0)
4081 {
4082 int sreg;
4083 expressionS ep;
4084
4085 if (! mips_opts.at)
4086 return;
4087
4088 sreg = EXTRACT_OPERAND (0, RS, *ip);
4089 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
4090 return;
4091
4092 ep.X_op = O_constant;
4093 ep.X_add_number = 0xcfff0000;
4094 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
4095 ep.X_add_number = 0xffff;
4096 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
4097 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
4098 }
4099 }
4100
4101 static void
4102 fix_loongson2f (struct mips_cl_insn * ip)
4103 {
4104 if (mips_fix_loongson2f_nop)
4105 fix_loongson2f_nop (ip);
4106
4107 if (mips_fix_loongson2f_jump)
4108 fix_loongson2f_jump (ip);
4109 }
4110
4111 /* IP is a branch that has a delay slot, and we need to fill it
4112 automatically. Return true if we can do that by swapping IP
4113 with the previous instruction.
4114 ADDRESS_EXPR is an operand of the instruction to be used with
4115 RELOC_TYPE. */
4116
4117 static bfd_boolean
4118 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
4119 bfd_reloc_code_real_type *reloc_type)
4120 {
4121 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
4122 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
4123
4124 /* -O2 and above is required for this optimization. */
4125 if (mips_optimize < 2)
4126 return FALSE;
4127
4128 /* If we have seen .set volatile or .set nomove, don't optimize. */
4129 if (mips_opts.nomove)
4130 return FALSE;
4131
4132 /* We can't swap if the previous instruction's position is fixed. */
4133 if (history[0].fixed_p)
4134 return FALSE;
4135
4136 /* If the previous previous insn was in a .set noreorder, we can't
4137 swap. Actually, the MIPS assembler will swap in this situation.
4138 However, gcc configured -with-gnu-as will generate code like
4139
4140 .set noreorder
4141 lw $4,XXX
4142 .set reorder
4143 INSN
4144 bne $4,$0,foo
4145
4146 in which we can not swap the bne and INSN. If gcc is not configured
4147 -with-gnu-as, it does not output the .set pseudo-ops. */
4148 if (history[1].noreorder_p)
4149 return FALSE;
4150
4151 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
4152 This means that the previous instruction was a 4-byte one anyhow. */
4153 if (mips_opts.mips16 && history[0].fixp[0])
4154 return FALSE;
4155
4156 /* If the branch is itself the target of a branch, we can not swap.
4157 We cheat on this; all we check for is whether there is a label on
4158 this instruction. If there are any branches to anything other than
4159 a label, users must use .set noreorder. */
4160 if (seg_info (now_seg)->label_list)
4161 return FALSE;
4162
4163 /* If the previous instruction is in a variant frag other than this
4164 branch's one, we cannot do the swap. This does not apply to
4165 MIPS16 code, which uses variant frags for different purposes. */
4166 if (!mips_opts.mips16
4167 && history[0].frag
4168 && history[0].frag->fr_type == rs_machine_dependent)
4169 return FALSE;
4170
4171 /* We do not swap with instructions that cannot architecturally
4172 be placed in a branch delay slot, such as SYNC or ERET. We
4173 also refrain from swapping with a trap instruction, since it
4174 complicates trap handlers to have the trap instruction be in
4175 a delay slot. */
4176 prev_pinfo = history[0].insn_mo->pinfo;
4177 if (prev_pinfo & INSN_NO_DELAY_SLOT)
4178 return FALSE;
4179
4180 /* Check for conflicts between the branch and the instructions
4181 before the candidate delay slot. */
4182 if (nops_for_insn (0, history + 1, ip) > 0)
4183 return FALSE;
4184
4185 /* Check for conflicts between the swapped sequence and the
4186 target of the branch. */
4187 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
4188 return FALSE;
4189
4190 /* If the branch reads a register that the previous
4191 instruction sets, we can not swap. */
4192 gpr_read = gpr_read_mask (ip);
4193 prev_gpr_write = gpr_write_mask (&history[0]);
4194 if (gpr_read & prev_gpr_write)
4195 return FALSE;
4196
4197 /* If the branch writes a register that the previous
4198 instruction sets, we can not swap. */
4199 gpr_write = gpr_write_mask (ip);
4200 if (gpr_write & prev_gpr_write)
4201 return FALSE;
4202
4203 /* If the branch writes a register that the previous
4204 instruction reads, we can not swap. */
4205 prev_gpr_read = gpr_read_mask (&history[0]);
4206 if (gpr_write & prev_gpr_read)
4207 return FALSE;
4208
4209 /* If one instruction sets a condition code and the
4210 other one uses a condition code, we can not swap. */
4211 pinfo = ip->insn_mo->pinfo;
4212 if ((pinfo & INSN_READ_COND_CODE)
4213 && (prev_pinfo & INSN_WRITE_COND_CODE))
4214 return FALSE;
4215 if ((pinfo & INSN_WRITE_COND_CODE)
4216 && (prev_pinfo & INSN_READ_COND_CODE))
4217 return FALSE;
4218
4219 /* If the previous instruction uses the PC, we can not swap. */
4220 prev_pinfo2 = history[0].insn_mo->pinfo2;
4221 if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
4222 return FALSE;
4223 if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
4224 return FALSE;
4225
4226 /* If the previous instruction has an incorrect size for a fixed
4227 branch delay slot in microMIPS mode, we cannot swap. */
4228 pinfo2 = ip->insn_mo->pinfo2;
4229 if (mips_opts.micromips
4230 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
4231 && insn_length (history) != 2)
4232 return FALSE;
4233 if (mips_opts.micromips
4234 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
4235 && insn_length (history) != 4)
4236 return FALSE;
4237
4238 /* On R5900 short loops need to be fixed by inserting a nop in
4239 the branch delay slots.
4240 A short loop can be terminated too early. */
4241 if (mips_opts.arch == CPU_R5900
4242 /* Check if instruction has a parameter, ignore "j $31". */
4243 && (address_expr != NULL)
4244 /* Parameter must be 16 bit. */
4245 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
4246 /* Branch to same segment. */
4247 && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
4248 /* Branch to same code fragment. */
4249 && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
4250 /* Can only calculate branch offset if value is known. */
4251 && symbol_constant_p(address_expr->X_add_symbol)
4252 /* Check if branch is really conditional. */
4253 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
4254 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
4255 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
4256 {
4257 int distance;
4258 /* Check if loop is shorter than 6 instructions including
4259 branch and delay slot. */
4260 distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
4261 if (distance <= 20)
4262 {
4263 int i;
4264 int rv;
4265
4266 rv = FALSE;
4267 /* When the loop includes branches or jumps,
4268 it is not a short loop. */
4269 for (i = 0; i < (distance / 4); i++)
4270 {
4271 if ((history[i].cleared_p)
4272 || delayed_branch_p(&history[i]))
4273 {
4274 rv = TRUE;
4275 break;
4276 }
4277 }
4278 if (rv == FALSE)
4279 {
4280 /* Insert nop after branch to fix short loop. */
4281 return FALSE;
4282 }
4283 }
4284 }
4285
4286 return TRUE;
4287 }
4288
4289 /* Decide how we should add IP to the instruction stream.
4290 ADDRESS_EXPR is an operand of the instruction to be used with
4291 RELOC_TYPE. */
4292
4293 static enum append_method
4294 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
4295 bfd_reloc_code_real_type *reloc_type)
4296 {
4297 unsigned long pinfo;
4298
4299 /* The relaxed version of a macro sequence must be inherently
4300 hazard-free. */
4301 if (mips_relax.sequence == 2)
4302 return APPEND_ADD;
4303
4304 /* We must not dabble with instructions in a ".set norerorder" block. */
4305 if (mips_opts.noreorder)
4306 return APPEND_ADD;
4307
4308 /* Otherwise, it's our responsibility to fill branch delay slots. */
4309 if (delayed_branch_p (ip))
4310 {
4311 if (!branch_likely_p (ip)
4312 && can_swap_branch_p (ip, address_expr, reloc_type))
4313 return APPEND_SWAP;
4314
4315 pinfo = ip->insn_mo->pinfo;
4316 if (mips_opts.mips16
4317 && ISA_SUPPORTS_MIPS16E
4318 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
4319 return APPEND_ADD_COMPACT;
4320
4321 return APPEND_ADD_WITH_NOP;
4322 }
4323
4324 return APPEND_ADD;
4325 }
4326
4327 /* IP is a MIPS16 instruction whose opcode we have just changed.
4328 Point IP->insn_mo to the new opcode's definition. */
4329
4330 static void
4331 find_altered_mips16_opcode (struct mips_cl_insn *ip)
4332 {
4333 const struct mips_opcode *mo, *end;
4334
4335 end = &mips16_opcodes[bfd_mips16_num_opcodes];
4336 for (mo = ip->insn_mo; mo < end; mo++)
4337 if ((ip->insn_opcode & mo->mask) == mo->match)
4338 {
4339 ip->insn_mo = mo;
4340 return;
4341 }
4342 abort ();
4343 }
4344
4345 /* For microMIPS macros, we need to generate a local number label
4346 as the target of branches. */
4347 #define MICROMIPS_LABEL_CHAR '\037'
4348 static unsigned long micromips_target_label;
4349 static char micromips_target_name[32];
4350
4351 static char *
4352 micromips_label_name (void)
4353 {
4354 char *p = micromips_target_name;
4355 char symbol_name_temporary[24];
4356 unsigned long l;
4357 int i;
4358
4359 if (*p)
4360 return p;
4361
4362 i = 0;
4363 l = micromips_target_label;
4364 #ifdef LOCAL_LABEL_PREFIX
4365 *p++ = LOCAL_LABEL_PREFIX;
4366 #endif
4367 *p++ = 'L';
4368 *p++ = MICROMIPS_LABEL_CHAR;
4369 do
4370 {
4371 symbol_name_temporary[i++] = l % 10 + '0';
4372 l /= 10;
4373 }
4374 while (l != 0);
4375 while (i > 0)
4376 *p++ = symbol_name_temporary[--i];
4377 *p = '\0';
4378
4379 return micromips_target_name;
4380 }
4381
4382 static void
4383 micromips_label_expr (expressionS *label_expr)
4384 {
4385 label_expr->X_op = O_symbol;
4386 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
4387 label_expr->X_add_number = 0;
4388 }
4389
4390 static void
4391 micromips_label_inc (void)
4392 {
4393 micromips_target_label++;
4394 *micromips_target_name = '\0';
4395 }
4396
4397 static void
4398 micromips_add_label (void)
4399 {
4400 symbolS *s;
4401
4402 s = colon (micromips_label_name ());
4403 micromips_label_inc ();
4404 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
4405 }
4406
4407 /* If assembling microMIPS code, then return the microMIPS reloc
4408 corresponding to the requested one if any. Otherwise return
4409 the reloc unchanged. */
4410
4411 static bfd_reloc_code_real_type
4412 micromips_map_reloc (bfd_reloc_code_real_type reloc)
4413 {
4414 static const bfd_reloc_code_real_type relocs[][2] =
4415 {
4416 /* Keep sorted incrementally by the left-hand key. */
4417 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
4418 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
4419 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
4420 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
4421 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
4422 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
4423 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
4424 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
4425 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
4426 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
4427 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
4428 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
4429 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
4430 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
4431 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
4432 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
4433 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
4434 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
4435 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
4436 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
4437 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
4438 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
4439 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
4440 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
4441 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
4442 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
4443 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
4444 };
4445 bfd_reloc_code_real_type r;
4446 size_t i;
4447
4448 if (!mips_opts.micromips)
4449 return reloc;
4450 for (i = 0; i < ARRAY_SIZE (relocs); i++)
4451 {
4452 r = relocs[i][0];
4453 if (r > reloc)
4454 return reloc;
4455 if (r == reloc)
4456 return relocs[i][1];
4457 }
4458 return reloc;
4459 }
4460
4461 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
4462 Return true on success, storing the resolved value in RESULT. */
4463
4464 static bfd_boolean
4465 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
4466 offsetT *result)
4467 {
4468 switch (reloc)
4469 {
4470 case BFD_RELOC_MIPS_HIGHEST:
4471 case BFD_RELOC_MICROMIPS_HIGHEST:
4472 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
4473 return TRUE;
4474
4475 case BFD_RELOC_MIPS_HIGHER:
4476 case BFD_RELOC_MICROMIPS_HIGHER:
4477 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
4478 return TRUE;
4479
4480 case BFD_RELOC_HI16_S:
4481 case BFD_RELOC_MICROMIPS_HI16_S:
4482 case BFD_RELOC_MIPS16_HI16_S:
4483 *result = ((operand + 0x8000) >> 16) & 0xffff;
4484 return TRUE;
4485
4486 case BFD_RELOC_HI16:
4487 case BFD_RELOC_MICROMIPS_HI16:
4488 case BFD_RELOC_MIPS16_HI16:
4489 *result = (operand >> 16) & 0xffff;
4490 return TRUE;
4491
4492 case BFD_RELOC_LO16:
4493 case BFD_RELOC_MICROMIPS_LO16:
4494 case BFD_RELOC_MIPS16_LO16:
4495 *result = operand & 0xffff;
4496 return TRUE;
4497
4498 case BFD_RELOC_UNUSED:
4499 *result = operand;
4500 return TRUE;
4501
4502 default:
4503 return FALSE;
4504 }
4505 }
4506
4507 /* Output an instruction. IP is the instruction information.
4508 ADDRESS_EXPR is an operand of the instruction to be used with
4509 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
4510 a macro expansion. */
4511
4512 static void
4513 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
4514 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
4515 {
4516 unsigned long prev_pinfo2, pinfo;
4517 bfd_boolean relaxed_branch = FALSE;
4518 enum append_method method;
4519 bfd_boolean relax32;
4520 int branch_disp;
4521
4522 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
4523 fix_loongson2f (ip);
4524
4525 file_ase_mips16 |= mips_opts.mips16;
4526 file_ase_micromips |= mips_opts.micromips;
4527
4528 prev_pinfo2 = history[0].insn_mo->pinfo2;
4529 pinfo = ip->insn_mo->pinfo;
4530
4531 if (mips_opts.micromips
4532 && !expansionp
4533 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4534 && micromips_insn_length (ip->insn_mo) != 2)
4535 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4536 && micromips_insn_length (ip->insn_mo) != 4)))
4537 as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4538 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
4539
4540 if (address_expr == NULL)
4541 ip->complete_p = 1;
4542 else if (reloc_type[0] <= BFD_RELOC_UNUSED
4543 && reloc_type[1] == BFD_RELOC_UNUSED
4544 && reloc_type[2] == BFD_RELOC_UNUSED
4545 && address_expr->X_op == O_constant)
4546 {
4547 switch (*reloc_type)
4548 {
4549 case BFD_RELOC_MIPS_JMP:
4550 {
4551 int shift;
4552
4553 shift = mips_opts.micromips ? 1 : 2;
4554 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4555 as_bad (_("jump to misaligned address (0x%lx)"),
4556 (unsigned long) address_expr->X_add_number);
4557 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4558 & 0x3ffffff);
4559 ip->complete_p = 1;
4560 }
4561 break;
4562
4563 case BFD_RELOC_MIPS16_JMP:
4564 if ((address_expr->X_add_number & 3) != 0)
4565 as_bad (_("jump to misaligned address (0x%lx)"),
4566 (unsigned long) address_expr->X_add_number);
4567 ip->insn_opcode |=
4568 (((address_expr->X_add_number & 0x7c0000) << 3)
4569 | ((address_expr->X_add_number & 0xf800000) >> 7)
4570 | ((address_expr->X_add_number & 0x3fffc) >> 2));
4571 ip->complete_p = 1;
4572 break;
4573
4574 case BFD_RELOC_16_PCREL_S2:
4575 {
4576 int shift;
4577
4578 shift = mips_opts.micromips ? 1 : 2;
4579 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4580 as_bad (_("branch to misaligned address (0x%lx)"),
4581 (unsigned long) address_expr->X_add_number);
4582 if (!mips_relax_branch)
4583 {
4584 if ((address_expr->X_add_number + (1 << (shift + 15)))
4585 & ~((1 << (shift + 16)) - 1))
4586 as_bad (_("branch address range overflow (0x%lx)"),
4587 (unsigned long) address_expr->X_add_number);
4588 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4589 & 0xffff);
4590 }
4591 }
4592 break;
4593
4594 default:
4595 {
4596 offsetT value;
4597
4598 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
4599 &value))
4600 {
4601 ip->insn_opcode |= value & 0xffff;
4602 ip->complete_p = 1;
4603 }
4604 }
4605 break;
4606 }
4607 }
4608
4609 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4610 {
4611 /* There are a lot of optimizations we could do that we don't.
4612 In particular, we do not, in general, reorder instructions.
4613 If you use gcc with optimization, it will reorder
4614 instructions and generally do much more optimization then we
4615 do here; repeating all that work in the assembler would only
4616 benefit hand written assembly code, and does not seem worth
4617 it. */
4618 int nops = (mips_optimize == 0
4619 ? nops_for_insn (0, history, NULL)
4620 : nops_for_insn_or_target (0, history, ip));
4621 if (nops > 0)
4622 {
4623 fragS *old_frag;
4624 unsigned long old_frag_offset;
4625 int i;
4626
4627 old_frag = frag_now;
4628 old_frag_offset = frag_now_fix ();
4629
4630 for (i = 0; i < nops; i++)
4631 add_fixed_insn (NOP_INSN);
4632 insert_into_history (0, nops, NOP_INSN);
4633
4634 if (listing)
4635 {
4636 listing_prev_line ();
4637 /* We may be at the start of a variant frag. In case we
4638 are, make sure there is enough space for the frag
4639 after the frags created by listing_prev_line. The
4640 argument to frag_grow here must be at least as large
4641 as the argument to all other calls to frag_grow in
4642 this file. We don't have to worry about being in the
4643 middle of a variant frag, because the variants insert
4644 all needed nop instructions themselves. */
4645 frag_grow (40);
4646 }
4647
4648 mips_move_text_labels ();
4649
4650 #ifndef NO_ECOFF_DEBUGGING
4651 if (ECOFF_DEBUGGING)
4652 ecoff_fix_loc (old_frag, old_frag_offset);
4653 #endif
4654 }
4655 }
4656 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4657 {
4658 int nops;
4659
4660 /* Work out how many nops in prev_nop_frag are needed by IP,
4661 ignoring hazards generated by the first prev_nop_frag_since
4662 instructions. */
4663 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
4664 gas_assert (nops <= prev_nop_frag_holds);
4665
4666 /* Enforce NOPS as a minimum. */
4667 if (nops > prev_nop_frag_required)
4668 prev_nop_frag_required = nops;
4669
4670 if (prev_nop_frag_holds == prev_nop_frag_required)
4671 {
4672 /* Settle for the current number of nops. Update the history
4673 accordingly (for the benefit of any future .set reorder code). */
4674 prev_nop_frag = NULL;
4675 insert_into_history (prev_nop_frag_since,
4676 prev_nop_frag_holds, NOP_INSN);
4677 }
4678 else
4679 {
4680 /* Allow this instruction to replace one of the nops that was
4681 tentatively added to prev_nop_frag. */
4682 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
4683 prev_nop_frag_holds--;
4684 prev_nop_frag_since++;
4685 }
4686 }
4687
4688 method = get_append_method (ip, address_expr, reloc_type);
4689 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
4690
4691 dwarf2_emit_insn (0);
4692 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
4693 so "move" the instruction address accordingly.
4694
4695 Also, it doesn't seem appropriate for the assembler to reorder .loc
4696 entries. If this instruction is a branch that we are going to swap
4697 with the previous instruction, the two instructions should be
4698 treated as a unit, and the debug information for both instructions
4699 should refer to the start of the branch sequence. Using the
4700 current position is certainly wrong when swapping a 32-bit branch
4701 and a 16-bit delay slot, since the current position would then be
4702 in the middle of a branch. */
4703 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
4704
4705 relax32 = (mips_relax_branch
4706 /* Don't try branch relaxation within .set nomacro, or within
4707 .set noat if we use $at for PIC computations. If it turns
4708 out that the branch was out-of-range, we'll get an error. */
4709 && !mips_opts.warn_about_macros
4710 && (mips_opts.at || mips_pic == NO_PIC)
4711 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
4712 as they have no complementing branches. */
4713 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
4714
4715 if (!HAVE_CODE_COMPRESSION
4716 && address_expr
4717 && relax32
4718 && *reloc_type == BFD_RELOC_16_PCREL_S2
4719 && delayed_branch_p (ip))
4720 {
4721 relaxed_branch = TRUE;
4722 add_relaxed_insn (ip, (relaxed_branch_length
4723 (NULL, NULL,
4724 uncond_branch_p (ip) ? -1
4725 : branch_likely_p (ip) ? 1
4726 : 0)), 4,
4727 RELAX_BRANCH_ENCODE
4728 (AT,
4729 uncond_branch_p (ip),
4730 branch_likely_p (ip),
4731 pinfo & INSN_WRITE_GPR_31,
4732 0),
4733 address_expr->X_add_symbol,
4734 address_expr->X_add_number);
4735 *reloc_type = BFD_RELOC_UNUSED;
4736 }
4737 else if (mips_opts.micromips
4738 && address_expr
4739 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4740 || *reloc_type > BFD_RELOC_UNUSED)
4741 && (delayed_branch_p (ip) || compact_branch_p (ip))
4742 /* Don't try branch relaxation when users specify
4743 16-bit/32-bit instructions. */
4744 && !forced_insn_length)
4745 {
4746 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4747 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
4748 int uncond = uncond_branch_p (ip) ? -1 : 0;
4749 int compact = compact_branch_p (ip);
4750 int al = pinfo & INSN_WRITE_GPR_31;
4751 int length32;
4752
4753 gas_assert (address_expr != NULL);
4754 gas_assert (!mips_relax.sequence);
4755
4756 relaxed_branch = TRUE;
4757 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4758 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
4759 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4760 relax32, 0, 0),
4761 address_expr->X_add_symbol,
4762 address_expr->X_add_number);
4763 *reloc_type = BFD_RELOC_UNUSED;
4764 }
4765 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
4766 {
4767 /* We need to set up a variant frag. */
4768 gas_assert (address_expr != NULL);
4769 add_relaxed_insn (ip, 4, 0,
4770 RELAX_MIPS16_ENCODE
4771 (*reloc_type - BFD_RELOC_UNUSED,
4772 forced_insn_length == 2, forced_insn_length == 4,
4773 delayed_branch_p (&history[0]),
4774 history[0].mips16_absolute_jump_p),
4775 make_expr_symbol (address_expr), 0);
4776 }
4777 else if (mips_opts.mips16 && insn_length (ip) == 2)
4778 {
4779 if (!delayed_branch_p (ip))
4780 /* Make sure there is enough room to swap this instruction with
4781 a following jump instruction. */
4782 frag_grow (6);
4783 add_fixed_insn (ip);
4784 }
4785 else
4786 {
4787 if (mips_opts.mips16
4788 && mips_opts.noreorder
4789 && delayed_branch_p (&history[0]))
4790 as_warn (_("extended instruction in delay slot"));
4791
4792 if (mips_relax.sequence)
4793 {
4794 /* If we've reached the end of this frag, turn it into a variant
4795 frag and record the information for the instructions we've
4796 written so far. */
4797 if (frag_room () < 4)
4798 relax_close_frag ();
4799 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4800 }
4801
4802 if (mips_relax.sequence != 2)
4803 {
4804 if (mips_macro_warning.first_insn_sizes[0] == 0)
4805 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4806 mips_macro_warning.sizes[0] += insn_length (ip);
4807 mips_macro_warning.insns[0]++;
4808 }
4809 if (mips_relax.sequence != 1)
4810 {
4811 if (mips_macro_warning.first_insn_sizes[1] == 0)
4812 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4813 mips_macro_warning.sizes[1] += insn_length (ip);
4814 mips_macro_warning.insns[1]++;
4815 }
4816
4817 if (mips_opts.mips16)
4818 {
4819 ip->fixed_p = 1;
4820 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4821 }
4822 add_fixed_insn (ip);
4823 }
4824
4825 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
4826 {
4827 bfd_reloc_code_real_type final_type[3];
4828 reloc_howto_type *howto0;
4829 reloc_howto_type *howto;
4830 int i;
4831
4832 /* Perform any necessary conversion to microMIPS relocations
4833 and find out how many relocations there actually are. */
4834 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4835 final_type[i] = micromips_map_reloc (reloc_type[i]);
4836
4837 /* In a compound relocation, it is the final (outermost)
4838 operator that determines the relocated field. */
4839 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
4840 if (!howto)
4841 abort ();
4842
4843 if (i > 1)
4844 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
4845 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4846 bfd_get_reloc_size (howto),
4847 address_expr,
4848 howto0 && howto0->pc_relative,
4849 final_type[0]);
4850
4851 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
4852 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
4853 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4854
4855 /* These relocations can have an addend that won't fit in
4856 4 octets for 64bit assembly. */
4857 if (HAVE_64BIT_GPRS
4858 && ! howto->partial_inplace
4859 && (reloc_type[0] == BFD_RELOC_16
4860 || reloc_type[0] == BFD_RELOC_32
4861 || reloc_type[0] == BFD_RELOC_MIPS_JMP
4862 || reloc_type[0] == BFD_RELOC_GPREL16
4863 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4864 || reloc_type[0] == BFD_RELOC_GPREL32
4865 || reloc_type[0] == BFD_RELOC_64
4866 || reloc_type[0] == BFD_RELOC_CTOR
4867 || reloc_type[0] == BFD_RELOC_MIPS_SUB
4868 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4869 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4870 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4871 || reloc_type[0] == BFD_RELOC_MIPS_REL16
4872 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4873 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4874 || hi16_reloc_p (reloc_type[0])
4875 || lo16_reloc_p (reloc_type[0])))
4876 ip->fixp[0]->fx_no_overflow = 1;
4877
4878 /* These relocations can have an addend that won't fit in 2 octets. */
4879 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
4880 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
4881 ip->fixp[0]->fx_no_overflow = 1;
4882
4883 if (mips_relax.sequence)
4884 {
4885 if (mips_relax.first_fixup == 0)
4886 mips_relax.first_fixup = ip->fixp[0];
4887 }
4888 else if (reloc_needs_lo_p (*reloc_type))
4889 {
4890 struct mips_hi_fixup *hi_fixup;
4891
4892 /* Reuse the last entry if it already has a matching %lo. */
4893 hi_fixup = mips_hi_fixup_list;
4894 if (hi_fixup == 0
4895 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4896 {
4897 hi_fixup = ((struct mips_hi_fixup *)
4898 xmalloc (sizeof (struct mips_hi_fixup)));
4899 hi_fixup->next = mips_hi_fixup_list;
4900 mips_hi_fixup_list = hi_fixup;
4901 }
4902 hi_fixup->fixp = ip->fixp[0];
4903 hi_fixup->seg = now_seg;
4904 }
4905
4906 /* Add fixups for the second and third relocations, if given.
4907 Note that the ABI allows the second relocation to be
4908 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
4909 moment we only use RSS_UNDEF, but we could add support
4910 for the others if it ever becomes necessary. */
4911 for (i = 1; i < 3; i++)
4912 if (reloc_type[i] != BFD_RELOC_UNUSED)
4913 {
4914 ip->fixp[i] = fix_new (ip->frag, ip->where,
4915 ip->fixp[0]->fx_size, NULL, 0,
4916 FALSE, final_type[i]);
4917
4918 /* Use fx_tcbit to mark compound relocs. */
4919 ip->fixp[0]->fx_tcbit = 1;
4920 ip->fixp[i]->fx_tcbit = 1;
4921 }
4922 }
4923 install_insn (ip);
4924
4925 /* Update the register mask information. */
4926 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4927 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
4928
4929 switch (method)
4930 {
4931 case APPEND_ADD:
4932 insert_into_history (0, 1, ip);
4933 break;
4934
4935 case APPEND_ADD_WITH_NOP:
4936 {
4937 struct mips_cl_insn *nop;
4938
4939 insert_into_history (0, 1, ip);
4940 nop = get_delay_slot_nop (ip);
4941 add_fixed_insn (nop);
4942 insert_into_history (0, 1, nop);
4943 if (mips_relax.sequence)
4944 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4945 }
4946 break;
4947
4948 case APPEND_ADD_COMPACT:
4949 /* Convert MIPS16 jr/jalr into a "compact" jump. */
4950 gas_assert (mips_opts.mips16);
4951 ip->insn_opcode |= 0x0080;
4952 find_altered_mips16_opcode (ip);
4953 install_insn (ip);
4954 insert_into_history (0, 1, ip);
4955 break;
4956
4957 case APPEND_SWAP:
4958 {
4959 struct mips_cl_insn delay = history[0];
4960 if (mips_opts.mips16)
4961 {
4962 know (delay.frag == ip->frag);
4963 move_insn (ip, delay.frag, delay.where);
4964 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4965 }
4966 else if (relaxed_branch || delay.frag != ip->frag)
4967 {
4968 /* Add the delay slot instruction to the end of the
4969 current frag and shrink the fixed part of the
4970 original frag. If the branch occupies the tail of
4971 the latter, move it backwards to cover the gap. */
4972 delay.frag->fr_fix -= branch_disp;
4973 if (delay.frag == ip->frag)
4974 move_insn (ip, ip->frag, ip->where - branch_disp);
4975 add_fixed_insn (&delay);
4976 }
4977 else
4978 {
4979 move_insn (&delay, ip->frag,
4980 ip->where - branch_disp + insn_length (ip));
4981 move_insn (ip, history[0].frag, history[0].where);
4982 }
4983 history[0] = *ip;
4984 delay.fixed_p = 1;
4985 insert_into_history (0, 1, &delay);
4986 }
4987 break;
4988 }
4989
4990 /* If we have just completed an unconditional branch, clear the history. */
4991 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
4992 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
4993 {
4994 unsigned int i;
4995
4996 mips_no_prev_insn ();
4997
4998 for (i = 0; i < ARRAY_SIZE (history); i++)
4999 history[i].cleared_p = 1;
5000 }
5001
5002 /* We need to emit a label at the end of branch-likely macros. */
5003 if (emit_branch_likely_macro)
5004 {
5005 emit_branch_likely_macro = FALSE;
5006 micromips_add_label ();
5007 }
5008
5009 /* We just output an insn, so the next one doesn't have a label. */
5010 mips_clear_insn_labels ();
5011 }
5012
5013 /* Forget that there was any previous instruction or label.
5014 When BRANCH is true, the branch history is also flushed. */
5015
5016 static void
5017 mips_no_prev_insn (void)
5018 {
5019 prev_nop_frag = NULL;
5020 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
5021 mips_clear_insn_labels ();
5022 }
5023
5024 /* This function must be called before we emit something other than
5025 instructions. It is like mips_no_prev_insn except that it inserts
5026 any NOPS that might be needed by previous instructions. */
5027
5028 void
5029 mips_emit_delays (void)
5030 {
5031 if (! mips_opts.noreorder)
5032 {
5033 int nops = nops_for_insn (0, history, NULL);
5034 if (nops > 0)
5035 {
5036 while (nops-- > 0)
5037 add_fixed_insn (NOP_INSN);
5038 mips_move_text_labels ();
5039 }
5040 }
5041 mips_no_prev_insn ();
5042 }
5043
5044 /* Start a (possibly nested) noreorder block. */
5045
5046 static void
5047 start_noreorder (void)
5048 {
5049 if (mips_opts.noreorder == 0)
5050 {
5051 unsigned int i;
5052 int nops;
5053
5054 /* None of the instructions before the .set noreorder can be moved. */
5055 for (i = 0; i < ARRAY_SIZE (history); i++)
5056 history[i].fixed_p = 1;
5057
5058 /* Insert any nops that might be needed between the .set noreorder
5059 block and the previous instructions. We will later remove any
5060 nops that turn out not to be needed. */
5061 nops = nops_for_insn (0, history, NULL);
5062 if (nops > 0)
5063 {
5064 if (mips_optimize != 0)
5065 {
5066 /* Record the frag which holds the nop instructions, so
5067 that we can remove them if we don't need them. */
5068 frag_grow (nops * NOP_INSN_SIZE);
5069 prev_nop_frag = frag_now;
5070 prev_nop_frag_holds = nops;
5071 prev_nop_frag_required = 0;
5072 prev_nop_frag_since = 0;
5073 }
5074
5075 for (; nops > 0; --nops)
5076 add_fixed_insn (NOP_INSN);
5077
5078 /* Move on to a new frag, so that it is safe to simply
5079 decrease the size of prev_nop_frag. */
5080 frag_wane (frag_now);
5081 frag_new (0);
5082 mips_move_text_labels ();
5083 }
5084 mips_mark_labels ();
5085 mips_clear_insn_labels ();
5086 }
5087 mips_opts.noreorder++;
5088 mips_any_noreorder = 1;
5089 }
5090
5091 /* End a nested noreorder block. */
5092
5093 static void
5094 end_noreorder (void)
5095 {
5096 mips_opts.noreorder--;
5097 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
5098 {
5099 /* Commit to inserting prev_nop_frag_required nops and go back to
5100 handling nop insertion the .set reorder way. */
5101 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
5102 * NOP_INSN_SIZE);
5103 insert_into_history (prev_nop_frag_since,
5104 prev_nop_frag_required, NOP_INSN);
5105 prev_nop_frag = NULL;
5106 }
5107 }
5108
5109 /* Set up global variables for the start of a new macro. */
5110
5111 static void
5112 macro_start (void)
5113 {
5114 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
5115 memset (&mips_macro_warning.first_insn_sizes, 0,
5116 sizeof (mips_macro_warning.first_insn_sizes));
5117 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
5118 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
5119 && delayed_branch_p (&history[0]));
5120 switch (history[0].insn_mo->pinfo2
5121 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
5122 {
5123 case INSN2_BRANCH_DELAY_32BIT:
5124 mips_macro_warning.delay_slot_length = 4;
5125 break;
5126 case INSN2_BRANCH_DELAY_16BIT:
5127 mips_macro_warning.delay_slot_length = 2;
5128 break;
5129 default:
5130 mips_macro_warning.delay_slot_length = 0;
5131 break;
5132 }
5133 mips_macro_warning.first_frag = NULL;
5134 }
5135
5136 /* Given that a macro is longer than one instruction or of the wrong size,
5137 return the appropriate warning for it. Return null if no warning is
5138 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
5139 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
5140 and RELAX_NOMACRO. */
5141
5142 static const char *
5143 macro_warning (relax_substateT subtype)
5144 {
5145 if (subtype & RELAX_DELAY_SLOT)
5146 return _("Macro instruction expanded into multiple instructions"
5147 " in a branch delay slot");
5148 else if (subtype & RELAX_NOMACRO)
5149 return _("Macro instruction expanded into multiple instructions");
5150 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
5151 | RELAX_DELAY_SLOT_SIZE_SECOND))
5152 return ((subtype & RELAX_DELAY_SLOT_16BIT)
5153 ? _("Macro instruction expanded into a wrong size instruction"
5154 " in a 16-bit branch delay slot")
5155 : _("Macro instruction expanded into a wrong size instruction"
5156 " in a 32-bit branch delay slot"));
5157 else
5158 return 0;
5159 }
5160
5161 /* Finish up a macro. Emit warnings as appropriate. */
5162
5163 static void
5164 macro_end (void)
5165 {
5166 /* Relaxation warning flags. */
5167 relax_substateT subtype = 0;
5168
5169 /* Check delay slot size requirements. */
5170 if (mips_macro_warning.delay_slot_length == 2)
5171 subtype |= RELAX_DELAY_SLOT_16BIT;
5172 if (mips_macro_warning.delay_slot_length != 0)
5173 {
5174 if (mips_macro_warning.delay_slot_length
5175 != mips_macro_warning.first_insn_sizes[0])
5176 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
5177 if (mips_macro_warning.delay_slot_length
5178 != mips_macro_warning.first_insn_sizes[1])
5179 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
5180 }
5181
5182 /* Check instruction count requirements. */
5183 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
5184 {
5185 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
5186 subtype |= RELAX_SECOND_LONGER;
5187 if (mips_opts.warn_about_macros)
5188 subtype |= RELAX_NOMACRO;
5189 if (mips_macro_warning.delay_slot_p)
5190 subtype |= RELAX_DELAY_SLOT;
5191 }
5192
5193 /* If both alternatives fail to fill a delay slot correctly,
5194 emit the warning now. */
5195 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
5196 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
5197 {
5198 relax_substateT s;
5199 const char *msg;
5200
5201 s = subtype & (RELAX_DELAY_SLOT_16BIT
5202 | RELAX_DELAY_SLOT_SIZE_FIRST
5203 | RELAX_DELAY_SLOT_SIZE_SECOND);
5204 msg = macro_warning (s);
5205 if (msg != NULL)
5206 as_warn ("%s", msg);
5207 subtype &= ~s;
5208 }
5209
5210 /* If both implementations are longer than 1 instruction, then emit the
5211 warning now. */
5212 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
5213 {
5214 relax_substateT s;
5215 const char *msg;
5216
5217 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
5218 msg = macro_warning (s);
5219 if (msg != NULL)
5220 as_warn ("%s", msg);
5221 subtype &= ~s;
5222 }
5223
5224 /* If any flags still set, then one implementation might need a warning
5225 and the other either will need one of a different kind or none at all.
5226 Pass any remaining flags over to relaxation. */
5227 if (mips_macro_warning.first_frag != NULL)
5228 mips_macro_warning.first_frag->fr_subtype |= subtype;
5229 }
5230
5231 /* Instruction operand formats used in macros that vary between
5232 standard MIPS and microMIPS code. */
5233
5234 static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
5235 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
5236 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
5237 static const char * const lui_fmt[2] = { "t,u", "s,u" };
5238 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
5239 static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
5240 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
5241 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
5242
5243 #define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
5244 #define COP12_FMT (cop12_fmt[mips_opts.micromips])
5245 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
5246 #define LUI_FMT (lui_fmt[mips_opts.micromips])
5247 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
5248 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
5249 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
5250 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
5251
5252 /* Read a macro's relocation codes from *ARGS and store them in *R.
5253 The first argument in *ARGS will be either the code for a single
5254 relocation or -1 followed by the three codes that make up a
5255 composite relocation. */
5256
5257 static void
5258 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
5259 {
5260 int i, next;
5261
5262 next = va_arg (*args, int);
5263 if (next >= 0)
5264 r[0] = (bfd_reloc_code_real_type) next;
5265 else
5266 for (i = 0; i < 3; i++)
5267 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
5268 }
5269
5270 /* Build an instruction created by a macro expansion. This is passed
5271 a pointer to the count of instructions created so far, an
5272 expression, the name of the instruction to build, an operand format
5273 string, and corresponding arguments. */
5274
5275 static void
5276 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
5277 {
5278 const struct mips_opcode *mo = NULL;
5279 bfd_reloc_code_real_type r[3];
5280 const struct mips_opcode *amo;
5281 struct hash_control *hash;
5282 struct mips_cl_insn insn;
5283 va_list args;
5284
5285 va_start (args, fmt);
5286
5287 if (mips_opts.mips16)
5288 {
5289 mips16_macro_build (ep, name, fmt, &args);
5290 va_end (args);
5291 return;
5292 }
5293
5294 r[0] = BFD_RELOC_UNUSED;
5295 r[1] = BFD_RELOC_UNUSED;
5296 r[2] = BFD_RELOC_UNUSED;
5297 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
5298 amo = (struct mips_opcode *) hash_find (hash, name);
5299 gas_assert (amo);
5300 gas_assert (strcmp (name, amo->name) == 0);
5301
5302 do
5303 {
5304 /* Search until we get a match for NAME. It is assumed here that
5305 macros will never generate MDMX, MIPS-3D, or MT instructions.
5306 We try to match an instruction that fulfils the branch delay
5307 slot instruction length requirement (if any) of the previous
5308 instruction. While doing this we record the first instruction
5309 seen that matches all the other conditions and use it anyway
5310 if the requirement cannot be met; we will issue an appropriate
5311 warning later on. */
5312 if (strcmp (fmt, amo->args) == 0
5313 && amo->pinfo != INSN_MACRO
5314 && is_opcode_valid (amo)
5315 && is_size_valid (amo))
5316 {
5317 if (is_delay_slot_valid (amo))
5318 {
5319 mo = amo;
5320 break;
5321 }
5322 else if (!mo)
5323 mo = amo;
5324 }
5325
5326 ++amo;
5327 gas_assert (amo->name);
5328 }
5329 while (strcmp (name, amo->name) == 0);
5330
5331 gas_assert (mo);
5332 create_insn (&insn, mo);
5333 for (;;)
5334 {
5335 switch (*fmt++)
5336 {
5337 case '\0':
5338 break;
5339
5340 case ',':
5341 case '(':
5342 case ')':
5343 continue;
5344
5345 case '+':
5346 switch (*fmt++)
5347 {
5348 case 'A':
5349 case 'E':
5350 INSERT_OPERAND (mips_opts.micromips,
5351 EXTLSB, insn, va_arg (args, int));
5352 continue;
5353
5354 case 'B':
5355 case 'F':
5356 /* Note that in the macro case, these arguments are already
5357 in MSB form. (When handling the instruction in the
5358 non-macro case, these arguments are sizes from which
5359 MSB values must be calculated.) */
5360 INSERT_OPERAND (mips_opts.micromips,
5361 INSMSB, insn, va_arg (args, int));
5362 continue;
5363
5364 case 'J':
5365 gas_assert (!mips_opts.micromips);
5366 INSERT_OPERAND (0, CODE10, insn, va_arg (args, int));
5367 continue;
5368
5369 case 'C':
5370 case 'G':
5371 case 'H':
5372 /* Note that in the macro case, these arguments are already
5373 in MSBD form. (When handling the instruction in the
5374 non-macro case, these arguments are sizes from which
5375 MSBD values must be calculated.) */
5376 INSERT_OPERAND (mips_opts.micromips,
5377 EXTMSBD, insn, va_arg (args, int));
5378 continue;
5379
5380 case 'Q':
5381 gas_assert (!mips_opts.micromips);
5382 INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
5383 continue;
5384
5385 case 'j':
5386 INSERT_OPERAND (mips_opts.micromips, EVAOFFSET, insn, va_arg (args, int));
5387 continue;
5388
5389 default:
5390 abort ();
5391 }
5392 continue;
5393
5394 case '2':
5395 INSERT_OPERAND (mips_opts.micromips, BP, insn, va_arg (args, int));
5396 continue;
5397
5398 case 'n':
5399 gas_assert (mips_opts.micromips);
5400 case 't':
5401 case 'w':
5402 case 'E':
5403 INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
5404 continue;
5405
5406 case 'c':
5407 INSERT_OPERAND (mips_opts.micromips, CODE, insn, va_arg (args, int));
5408 continue;
5409
5410 case 'W':
5411 gas_assert (!mips_opts.micromips);
5412 case 'T':
5413 INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
5414 continue;
5415
5416 case 'G':
5417 if (mips_opts.micromips)
5418 INSERT_OPERAND (1, RS, insn, va_arg (args, int));
5419 else
5420 INSERT_OPERAND (0, RD, insn, va_arg (args, int));
5421 continue;
5422
5423 case 'K':
5424 gas_assert (!mips_opts.micromips);
5425 case 'd':
5426 INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
5427 continue;
5428
5429 case 'U':
5430 gas_assert (!mips_opts.micromips);
5431 {
5432 int tmp = va_arg (args, int);
5433
5434 INSERT_OPERAND (0, RT, insn, tmp);
5435 INSERT_OPERAND (0, RD, insn, tmp);
5436 }
5437 continue;
5438
5439 case 'V':
5440 case 'S':
5441 gas_assert (!mips_opts.micromips);
5442 INSERT_OPERAND (0, FS, insn, va_arg (args, int));
5443 continue;
5444
5445 case 'z':
5446 continue;
5447
5448 case '<':
5449 INSERT_OPERAND (mips_opts.micromips,
5450 SHAMT, insn, va_arg (args, int));
5451 continue;
5452
5453 case 'D':
5454 gas_assert (!mips_opts.micromips);
5455 INSERT_OPERAND (0, FD, insn, va_arg (args, int));
5456 continue;
5457
5458 case 'B':
5459 gas_assert (!mips_opts.micromips);
5460 INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
5461 continue;
5462
5463 case 'J':
5464 gas_assert (!mips_opts.micromips);
5465 INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
5466 continue;
5467
5468 case 'q':
5469 gas_assert (!mips_opts.micromips);
5470 INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
5471 continue;
5472
5473 case 'b':
5474 case 's':
5475 case 'r':
5476 case 'v':
5477 INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
5478 continue;
5479
5480 case 'i':
5481 case 'j':
5482 macro_read_relocs (&args, r);
5483 gas_assert (*r == BFD_RELOC_GPREL16
5484 || *r == BFD_RELOC_MIPS_HIGHER
5485 || *r == BFD_RELOC_HI16_S
5486 || *r == BFD_RELOC_LO16
5487 || *r == BFD_RELOC_MIPS_GOT_OFST);
5488 continue;
5489
5490 case 'o':
5491 macro_read_relocs (&args, r);
5492 continue;
5493
5494 case 'u':
5495 macro_read_relocs (&args, r);
5496 gas_assert (ep != NULL
5497 && (ep->X_op == O_constant
5498 || (ep->X_op == O_symbol
5499 && (*r == BFD_RELOC_MIPS_HIGHEST
5500 || *r == BFD_RELOC_HI16_S
5501 || *r == BFD_RELOC_HI16
5502 || *r == BFD_RELOC_GPREL16
5503 || *r == BFD_RELOC_MIPS_GOT_HI16
5504 || *r == BFD_RELOC_MIPS_CALL_HI16))));
5505 continue;
5506
5507 case 'p':
5508 gas_assert (ep != NULL);
5509
5510 /*
5511 * This allows macro() to pass an immediate expression for
5512 * creating short branches without creating a symbol.
5513 *
5514 * We don't allow branch relaxation for these branches, as
5515 * they should only appear in ".set nomacro" anyway.
5516 */
5517 if (ep->X_op == O_constant)
5518 {
5519 /* For microMIPS we always use relocations for branches.
5520 So we should not resolve immediate values. */
5521 gas_assert (!mips_opts.micromips);
5522
5523 if ((ep->X_add_number & 3) != 0)
5524 as_bad (_("branch to misaligned address (0x%lx)"),
5525 (unsigned long) ep->X_add_number);
5526 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5527 as_bad (_("branch address range overflow (0x%lx)"),
5528 (unsigned long) ep->X_add_number);
5529 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5530 ep = NULL;
5531 }
5532 else
5533 *r = BFD_RELOC_16_PCREL_S2;
5534 continue;
5535
5536 case 'a':
5537 gas_assert (ep != NULL);
5538 *r = BFD_RELOC_MIPS_JMP;
5539 continue;
5540
5541 case 'C':
5542 gas_assert (!mips_opts.micromips);
5543 INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
5544 continue;
5545
5546 case 'k':
5547 INSERT_OPERAND (mips_opts.micromips,
5548 CACHE, insn, va_arg (args, unsigned long));
5549 continue;
5550
5551 case '|':
5552 gas_assert (mips_opts.micromips);
5553 INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5554 continue;
5555
5556 case '.':
5557 gas_assert (mips_opts.micromips);
5558 INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5559 continue;
5560
5561 case '\\':
5562 INSERT_OPERAND (mips_opts.micromips,
5563 3BITPOS, insn, va_arg (args, unsigned int));
5564 continue;
5565
5566 case '~':
5567 INSERT_OPERAND (mips_opts.micromips,
5568 OFFSET12, insn, va_arg (args, unsigned long));
5569 continue;
5570
5571 case 'N':
5572 gas_assert (mips_opts.micromips);
5573 INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5574 continue;
5575
5576 case 'm': /* Opcode extension character. */
5577 gas_assert (mips_opts.micromips);
5578 switch (*fmt++)
5579 {
5580 case 'j':
5581 INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5582 break;
5583
5584 case 'p':
5585 INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5586 break;
5587
5588 case 'F':
5589 INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5590 break;
5591
5592 default:
5593 abort ();
5594 }
5595 continue;
5596
5597 default:
5598 abort ();
5599 }
5600 break;
5601 }
5602 va_end (args);
5603 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5604
5605 append_insn (&insn, ep, r, TRUE);
5606 }
5607
5608 static void
5609 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
5610 va_list *args)
5611 {
5612 struct mips_opcode *mo;
5613 struct mips_cl_insn insn;
5614 bfd_reloc_code_real_type r[3]
5615 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5616
5617 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
5618 gas_assert (mo);
5619 gas_assert (strcmp (name, mo->name) == 0);
5620
5621 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
5622 {
5623 ++mo;
5624 gas_assert (mo->name);
5625 gas_assert (strcmp (name, mo->name) == 0);
5626 }
5627
5628 create_insn (&insn, mo);
5629 for (;;)
5630 {
5631 int c;
5632
5633 c = *fmt++;
5634 switch (c)
5635 {
5636 case '\0':
5637 break;
5638
5639 case ',':
5640 case '(':
5641 case ')':
5642 continue;
5643
5644 case 'y':
5645 case 'w':
5646 MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
5647 continue;
5648
5649 case 'x':
5650 case 'v':
5651 MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
5652 continue;
5653
5654 case 'z':
5655 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
5656 continue;
5657
5658 case 'Z':
5659 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
5660 continue;
5661
5662 case '0':
5663 case 'S':
5664 case 'P':
5665 case 'R':
5666 continue;
5667
5668 case 'X':
5669 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
5670 continue;
5671
5672 case 'Y':
5673 {
5674 int regno;
5675
5676 regno = va_arg (*args, int);
5677 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
5678 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
5679 }
5680 continue;
5681
5682 case '<':
5683 case '>':
5684 case '4':
5685 case '5':
5686 case 'H':
5687 case 'W':
5688 case 'D':
5689 case 'j':
5690 case '8':
5691 case 'V':
5692 case 'C':
5693 case 'U':
5694 case 'k':
5695 case 'K':
5696 case 'p':
5697 case 'q':
5698 {
5699 offsetT value;
5700
5701 gas_assert (ep != NULL);
5702
5703 if (ep->X_op != O_constant)
5704 *r = (int) BFD_RELOC_UNUSED + c;
5705 else if (calculate_reloc (*r, ep->X_add_number, &value))
5706 {
5707 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
5708 ep = NULL;
5709 *r = BFD_RELOC_UNUSED;
5710 }
5711 }
5712 continue;
5713
5714 case '6':
5715 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
5716 continue;
5717 }
5718
5719 break;
5720 }
5721
5722 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5723
5724 append_insn (&insn, ep, r, TRUE);
5725 }
5726
5727 /*
5728 * Sign-extend 32-bit mode constants that have bit 31 set and all
5729 * higher bits unset.
5730 */
5731 static void
5732 normalize_constant_expr (expressionS *ex)
5733 {
5734 if (ex->X_op == O_constant
5735 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5736 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5737 - 0x80000000);
5738 }
5739
5740 /*
5741 * Sign-extend 32-bit mode address offsets that have bit 31 set and
5742 * all higher bits unset.
5743 */
5744 static void
5745 normalize_address_expr (expressionS *ex)
5746 {
5747 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5748 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5749 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5750 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5751 - 0x80000000);
5752 }
5753
5754 /*
5755 * Generate a "jalr" instruction with a relocation hint to the called
5756 * function. This occurs in NewABI PIC code.
5757 */
5758 static void
5759 macro_build_jalr (expressionS *ep, int cprestore)
5760 {
5761 static const bfd_reloc_code_real_type jalr_relocs[2]
5762 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5763 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5764 const char *jalr;
5765 char *f = NULL;
5766
5767 if (MIPS_JALR_HINT_P (ep))
5768 {
5769 frag_grow (8);
5770 f = frag_more (0);
5771 }
5772 if (mips_opts.micromips)
5773 {
5774 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
5775 ? "jalr" : "jalrs");
5776 if (MIPS_JALR_HINT_P (ep)
5777 || mips_opts.insn32
5778 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
5779 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5780 else
5781 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5782 }
5783 else
5784 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
5785 if (MIPS_JALR_HINT_P (ep))
5786 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
5787 }
5788
5789 /*
5790 * Generate a "lui" instruction.
5791 */
5792 static void
5793 macro_build_lui (expressionS *ep, int regnum)
5794 {
5795 gas_assert (! mips_opts.mips16);
5796
5797 if (ep->X_op != O_constant)
5798 {
5799 gas_assert (ep->X_op == O_symbol);
5800 /* _gp_disp is a special case, used from s_cpload.
5801 __gnu_local_gp is used if mips_no_shared. */
5802 gas_assert (mips_pic == NO_PIC
5803 || (! HAVE_NEWABI
5804 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5805 || (! mips_in_shared
5806 && strcmp (S_GET_NAME (ep->X_add_symbol),
5807 "__gnu_local_gp") == 0));
5808 }
5809
5810 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
5811 }
5812
5813 /* Generate a sequence of instructions to do a load or store from a constant
5814 offset off of a base register (breg) into/from a target register (treg),
5815 using AT if necessary. */
5816 static void
5817 macro_build_ldst_constoffset (expressionS *ep, const char *op,
5818 int treg, int breg, int dbl)
5819 {
5820 gas_assert (ep->X_op == O_constant);
5821
5822 /* Sign-extending 32-bit constants makes their handling easier. */
5823 if (!dbl)
5824 normalize_constant_expr (ep);
5825
5826 /* Right now, this routine can only handle signed 32-bit constants. */
5827 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
5828 as_warn (_("operand overflow"));
5829
5830 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5831 {
5832 /* Signed 16-bit offset will fit in the op. Easy! */
5833 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
5834 }
5835 else
5836 {
5837 /* 32-bit offset, need multiple instructions and AT, like:
5838 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
5839 addu $tempreg,$tempreg,$breg
5840 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
5841 to handle the complete offset. */
5842 macro_build_lui (ep, AT);
5843 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5844 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
5845
5846 if (!mips_opts.at)
5847 as_bad (_("Macro used $at after \".set noat\""));
5848 }
5849 }
5850
5851 /* set_at()
5852 * Generates code to set the $at register to true (one)
5853 * if reg is less than the immediate expression.
5854 */
5855 static void
5856 set_at (int reg, int unsignedp)
5857 {
5858 if (imm_expr.X_op == O_constant
5859 && imm_expr.X_add_number >= -0x8000
5860 && imm_expr.X_add_number < 0x8000)
5861 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5862 AT, reg, BFD_RELOC_LO16);
5863 else
5864 {
5865 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5866 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
5867 }
5868 }
5869
5870 /* Warn if an expression is not a constant. */
5871
5872 static void
5873 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
5874 {
5875 if (ex->X_op == O_big)
5876 as_bad (_("unsupported large constant"));
5877 else if (ex->X_op != O_constant)
5878 as_bad (_("Instruction %s requires absolute expression"),
5879 ip->insn_mo->name);
5880
5881 if (HAVE_32BIT_GPRS)
5882 normalize_constant_expr (ex);
5883 }
5884
5885 /* Count the leading zeroes by performing a binary chop. This is a
5886 bulky bit of source, but performance is a LOT better for the
5887 majority of values than a simple loop to count the bits:
5888 for (lcnt = 0; (lcnt < 32); lcnt++)
5889 if ((v) & (1 << (31 - lcnt)))
5890 break;
5891 However it is not code size friendly, and the gain will drop a bit
5892 on certain cached systems.
5893 */
5894 #define COUNT_TOP_ZEROES(v) \
5895 (((v) & ~0xffff) == 0 \
5896 ? ((v) & ~0xff) == 0 \
5897 ? ((v) & ~0xf) == 0 \
5898 ? ((v) & ~0x3) == 0 \
5899 ? ((v) & ~0x1) == 0 \
5900 ? !(v) \
5901 ? 32 \
5902 : 31 \
5903 : 30 \
5904 : ((v) & ~0x7) == 0 \
5905 ? 29 \
5906 : 28 \
5907 : ((v) & ~0x3f) == 0 \
5908 ? ((v) & ~0x1f) == 0 \
5909 ? 27 \
5910 : 26 \
5911 : ((v) & ~0x7f) == 0 \
5912 ? 25 \
5913 : 24 \
5914 : ((v) & ~0xfff) == 0 \
5915 ? ((v) & ~0x3ff) == 0 \
5916 ? ((v) & ~0x1ff) == 0 \
5917 ? 23 \
5918 : 22 \
5919 : ((v) & ~0x7ff) == 0 \
5920 ? 21 \
5921 : 20 \
5922 : ((v) & ~0x3fff) == 0 \
5923 ? ((v) & ~0x1fff) == 0 \
5924 ? 19 \
5925 : 18 \
5926 : ((v) & ~0x7fff) == 0 \
5927 ? 17 \
5928 : 16 \
5929 : ((v) & ~0xffffff) == 0 \
5930 ? ((v) & ~0xfffff) == 0 \
5931 ? ((v) & ~0x3ffff) == 0 \
5932 ? ((v) & ~0x1ffff) == 0 \
5933 ? 15 \
5934 : 14 \
5935 : ((v) & ~0x7ffff) == 0 \
5936 ? 13 \
5937 : 12 \
5938 : ((v) & ~0x3fffff) == 0 \
5939 ? ((v) & ~0x1fffff) == 0 \
5940 ? 11 \
5941 : 10 \
5942 : ((v) & ~0x7fffff) == 0 \
5943 ? 9 \
5944 : 8 \
5945 : ((v) & ~0xfffffff) == 0 \
5946 ? ((v) & ~0x3ffffff) == 0 \
5947 ? ((v) & ~0x1ffffff) == 0 \
5948 ? 7 \
5949 : 6 \
5950 : ((v) & ~0x7ffffff) == 0 \
5951 ? 5 \
5952 : 4 \
5953 : ((v) & ~0x3fffffff) == 0 \
5954 ? ((v) & ~0x1fffffff) == 0 \
5955 ? 3 \
5956 : 2 \
5957 : ((v) & ~0x7fffffff) == 0 \
5958 ? 1 \
5959 : 0)
5960
5961 /* load_register()
5962 * This routine generates the least number of instructions necessary to load
5963 * an absolute expression value into a register.
5964 */
5965 static void
5966 load_register (int reg, expressionS *ep, int dbl)
5967 {
5968 int freg;
5969 expressionS hi32, lo32;
5970
5971 if (ep->X_op != O_big)
5972 {
5973 gas_assert (ep->X_op == O_constant);
5974
5975 /* Sign-extending 32-bit constants makes their handling easier. */
5976 if (!dbl)
5977 normalize_constant_expr (ep);
5978
5979 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
5980 {
5981 /* We can handle 16 bit signed values with an addiu to
5982 $zero. No need to ever use daddiu here, since $zero and
5983 the result are always correct in 32 bit mode. */
5984 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5985 return;
5986 }
5987 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
5988 {
5989 /* We can handle 16 bit unsigned values with an ori to
5990 $zero. */
5991 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
5992 return;
5993 }
5994 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
5995 {
5996 /* 32 bit values require an lui. */
5997 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5998 if ((ep->X_add_number & 0xffff) != 0)
5999 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
6000 return;
6001 }
6002 }
6003
6004 /* The value is larger than 32 bits. */
6005
6006 if (!dbl || HAVE_32BIT_GPRS)
6007 {
6008 char value[32];
6009
6010 sprintf_vma (value, ep->X_add_number);
6011 as_bad (_("Number (0x%s) larger than 32 bits"), value);
6012 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
6013 return;
6014 }
6015
6016 if (ep->X_op != O_big)
6017 {
6018 hi32 = *ep;
6019 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
6020 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
6021 hi32.X_add_number &= 0xffffffff;
6022 lo32 = *ep;
6023 lo32.X_add_number &= 0xffffffff;
6024 }
6025 else
6026 {
6027 gas_assert (ep->X_add_number > 2);
6028 if (ep->X_add_number == 3)
6029 generic_bignum[3] = 0;
6030 else if (ep->X_add_number > 4)
6031 as_bad (_("Number larger than 64 bits"));
6032 lo32.X_op = O_constant;
6033 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
6034 hi32.X_op = O_constant;
6035 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
6036 }
6037
6038 if (hi32.X_add_number == 0)
6039 freg = 0;
6040 else
6041 {
6042 int shift, bit;
6043 unsigned long hi, lo;
6044
6045 if (hi32.X_add_number == (offsetT) 0xffffffff)
6046 {
6047 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
6048 {
6049 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
6050 return;
6051 }
6052 if (lo32.X_add_number & 0x80000000)
6053 {
6054 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
6055 if (lo32.X_add_number & 0xffff)
6056 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
6057 return;
6058 }
6059 }
6060
6061 /* Check for 16bit shifted constant. We know that hi32 is
6062 non-zero, so start the mask on the first bit of the hi32
6063 value. */
6064 shift = 17;
6065 do
6066 {
6067 unsigned long himask, lomask;
6068
6069 if (shift < 32)
6070 {
6071 himask = 0xffff >> (32 - shift);
6072 lomask = (0xffff << shift) & 0xffffffff;
6073 }
6074 else
6075 {
6076 himask = 0xffff << (shift - 32);
6077 lomask = 0;
6078 }
6079 if ((hi32.X_add_number & ~(offsetT) himask) == 0
6080 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
6081 {
6082 expressionS tmp;
6083
6084 tmp.X_op = O_constant;
6085 if (shift < 32)
6086 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
6087 | (lo32.X_add_number >> shift));
6088 else
6089 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
6090 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
6091 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
6092 reg, reg, (shift >= 32) ? shift - 32 : shift);
6093 return;
6094 }
6095 ++shift;
6096 }
6097 while (shift <= (64 - 16));
6098
6099 /* Find the bit number of the lowest one bit, and store the
6100 shifted value in hi/lo. */
6101 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
6102 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
6103 if (lo != 0)
6104 {
6105 bit = 0;
6106 while ((lo & 1) == 0)
6107 {
6108 lo >>= 1;
6109 ++bit;
6110 }
6111 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
6112 hi >>= bit;
6113 }
6114 else
6115 {
6116 bit = 32;
6117 while ((hi & 1) == 0)
6118 {
6119 hi >>= 1;
6120 ++bit;
6121 }
6122 lo = hi;
6123 hi = 0;
6124 }
6125
6126 /* Optimize if the shifted value is a (power of 2) - 1. */
6127 if ((hi == 0 && ((lo + 1) & lo) == 0)
6128 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
6129 {
6130 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
6131 if (shift != 0)
6132 {
6133 expressionS tmp;
6134
6135 /* This instruction will set the register to be all
6136 ones. */
6137 tmp.X_op = O_constant;
6138 tmp.X_add_number = (offsetT) -1;
6139 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
6140 if (bit != 0)
6141 {
6142 bit += shift;
6143 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
6144 reg, reg, (bit >= 32) ? bit - 32 : bit);
6145 }
6146 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
6147 reg, reg, (shift >= 32) ? shift - 32 : shift);
6148 return;
6149 }
6150 }
6151
6152 /* Sign extend hi32 before calling load_register, because we can
6153 generally get better code when we load a sign extended value. */
6154 if ((hi32.X_add_number & 0x80000000) != 0)
6155 hi32.X_add_number |= ~(offsetT) 0xffffffff;
6156 load_register (reg, &hi32, 0);
6157 freg = reg;
6158 }
6159 if ((lo32.X_add_number & 0xffff0000) == 0)
6160 {
6161 if (freg != 0)
6162 {
6163 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
6164 freg = reg;
6165 }
6166 }
6167 else
6168 {
6169 expressionS mid16;
6170
6171 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
6172 {
6173 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
6174 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
6175 return;
6176 }
6177
6178 if (freg != 0)
6179 {
6180 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
6181 freg = reg;
6182 }
6183 mid16 = lo32;
6184 mid16.X_add_number >>= 16;
6185 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
6186 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
6187 freg = reg;
6188 }
6189 if ((lo32.X_add_number & 0xffff) != 0)
6190 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
6191 }
6192
6193 static inline void
6194 load_delay_nop (void)
6195 {
6196 if (!gpr_interlocks)
6197 macro_build (NULL, "nop", "");
6198 }
6199
6200 /* Load an address into a register. */
6201
6202 static void
6203 load_address (int reg, expressionS *ep, int *used_at)
6204 {
6205 if (ep->X_op != O_constant
6206 && ep->X_op != O_symbol)
6207 {
6208 as_bad (_("expression too complex"));
6209 ep->X_op = O_constant;
6210 }
6211
6212 if (ep->X_op == O_constant)
6213 {
6214 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
6215 return;
6216 }
6217
6218 if (mips_pic == NO_PIC)
6219 {
6220 /* If this is a reference to a GP relative symbol, we want
6221 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
6222 Otherwise we want
6223 lui $reg,<sym> (BFD_RELOC_HI16_S)
6224 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
6225 If we have an addend, we always use the latter form.
6226
6227 With 64bit address space and a usable $at we want
6228 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6229 lui $at,<sym> (BFD_RELOC_HI16_S)
6230 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
6231 daddiu $at,<sym> (BFD_RELOC_LO16)
6232 dsll32 $reg,0
6233 daddu $reg,$reg,$at
6234
6235 If $at is already in use, we use a path which is suboptimal
6236 on superscalar processors.
6237 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6238 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
6239 dsll $reg,16
6240 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
6241 dsll $reg,16
6242 daddiu $reg,<sym> (BFD_RELOC_LO16)
6243
6244 For GP relative symbols in 64bit address space we can use
6245 the same sequence as in 32bit address space. */
6246 if (HAVE_64BIT_SYMBOLS)
6247 {
6248 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6249 && !nopic_need_relax (ep->X_add_symbol, 1))
6250 {
6251 relax_start (ep->X_add_symbol);
6252 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
6253 mips_gp_register, BFD_RELOC_GPREL16);
6254 relax_switch ();
6255 }
6256
6257 if (*used_at == 0 && mips_opts.at)
6258 {
6259 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
6260 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
6261 macro_build (ep, "daddiu", "t,r,j", reg, reg,
6262 BFD_RELOC_MIPS_HIGHER);
6263 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
6264 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
6265 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
6266 *used_at = 1;
6267 }
6268 else
6269 {
6270 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
6271 macro_build (ep, "daddiu", "t,r,j", reg, reg,
6272 BFD_RELOC_MIPS_HIGHER);
6273 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
6274 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
6275 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
6276 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
6277 }
6278
6279 if (mips_relax.sequence)
6280 relax_end ();
6281 }
6282 else
6283 {
6284 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6285 && !nopic_need_relax (ep->X_add_symbol, 1))
6286 {
6287 relax_start (ep->X_add_symbol);
6288 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
6289 mips_gp_register, BFD_RELOC_GPREL16);
6290 relax_switch ();
6291 }
6292 macro_build_lui (ep, reg);
6293 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
6294 reg, reg, BFD_RELOC_LO16);
6295 if (mips_relax.sequence)
6296 relax_end ();
6297 }
6298 }
6299 else if (!mips_big_got)
6300 {
6301 expressionS ex;
6302
6303 /* If this is a reference to an external symbol, we want
6304 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6305 Otherwise we want
6306 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6307 nop
6308 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
6309 If there is a constant, it must be added in after.
6310
6311 If we have NewABI, we want
6312 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
6313 unless we're referencing a global symbol with a non-zero
6314 offset, in which case cst must be added separately. */
6315 if (HAVE_NEWABI)
6316 {
6317 if (ep->X_add_number)
6318 {
6319 ex.X_add_number = ep->X_add_number;
6320 ep->X_add_number = 0;
6321 relax_start (ep->X_add_symbol);
6322 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6323 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
6324 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6325 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6326 ex.X_op = O_constant;
6327 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
6328 reg, reg, BFD_RELOC_LO16);
6329 ep->X_add_number = ex.X_add_number;
6330 relax_switch ();
6331 }
6332 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6333 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
6334 if (mips_relax.sequence)
6335 relax_end ();
6336 }
6337 else
6338 {
6339 ex.X_add_number = ep->X_add_number;
6340 ep->X_add_number = 0;
6341 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6342 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6343 load_delay_nop ();
6344 relax_start (ep->X_add_symbol);
6345 relax_switch ();
6346 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6347 BFD_RELOC_LO16);
6348 relax_end ();
6349
6350 if (ex.X_add_number != 0)
6351 {
6352 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6353 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6354 ex.X_op = O_constant;
6355 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
6356 reg, reg, BFD_RELOC_LO16);
6357 }
6358 }
6359 }
6360 else if (mips_big_got)
6361 {
6362 expressionS ex;
6363
6364 /* This is the large GOT case. If this is a reference to an
6365 external symbol, we want
6366 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6367 addu $reg,$reg,$gp
6368 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
6369
6370 Otherwise, for a reference to a local symbol in old ABI, we want
6371 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6372 nop
6373 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
6374 If there is a constant, it must be added in after.
6375
6376 In the NewABI, for local symbols, with or without offsets, we want:
6377 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6378 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
6379 */
6380 if (HAVE_NEWABI)
6381 {
6382 ex.X_add_number = ep->X_add_number;
6383 ep->X_add_number = 0;
6384 relax_start (ep->X_add_symbol);
6385 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
6386 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6387 reg, reg, mips_gp_register);
6388 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6389 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
6390 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6391 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6392 else if (ex.X_add_number)
6393 {
6394 ex.X_op = O_constant;
6395 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6396 BFD_RELOC_LO16);
6397 }
6398
6399 ep->X_add_number = ex.X_add_number;
6400 relax_switch ();
6401 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6402 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6403 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6404 BFD_RELOC_MIPS_GOT_OFST);
6405 relax_end ();
6406 }
6407 else
6408 {
6409 ex.X_add_number = ep->X_add_number;
6410 ep->X_add_number = 0;
6411 relax_start (ep->X_add_symbol);
6412 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
6413 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6414 reg, reg, mips_gp_register);
6415 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6416 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
6417 relax_switch ();
6418 if (reg_needs_delay (mips_gp_register))
6419 {
6420 /* We need a nop before loading from $gp. This special
6421 check is required because the lui which starts the main
6422 instruction stream does not refer to $gp, and so will not
6423 insert the nop which may be required. */
6424 macro_build (NULL, "nop", "");
6425 }
6426 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6427 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6428 load_delay_nop ();
6429 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6430 BFD_RELOC_LO16);
6431 relax_end ();
6432
6433 if (ex.X_add_number != 0)
6434 {
6435 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6436 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6437 ex.X_op = O_constant;
6438 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6439 BFD_RELOC_LO16);
6440 }
6441 }
6442 }
6443 else
6444 abort ();
6445
6446 if (!mips_opts.at && *used_at == 1)
6447 as_bad (_("Macro used $at after \".set noat\""));
6448 }
6449
6450 /* Move the contents of register SOURCE into register DEST. */
6451
6452 static void
6453 move_register (int dest, int source)
6454 {
6455 /* Prefer to use a 16-bit microMIPS instruction unless the previous
6456 instruction specifically requires a 32-bit one. */
6457 if (mips_opts.micromips
6458 && !mips_opts.insn32
6459 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
6460 macro_build (NULL, "move", "mp,mj", dest, source);
6461 else
6462 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
6463 dest, source, 0);
6464 }
6465
6466 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
6467 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
6468 The two alternatives are:
6469
6470 Global symbol Local sybmol
6471 ------------- ------------
6472 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
6473 ... ...
6474 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
6475
6476 load_got_offset emits the first instruction and add_got_offset
6477 emits the second for a 16-bit offset or add_got_offset_hilo emits
6478 a sequence to add a 32-bit offset using a scratch register. */
6479
6480 static void
6481 load_got_offset (int dest, expressionS *local)
6482 {
6483 expressionS global;
6484
6485 global = *local;
6486 global.X_add_number = 0;
6487
6488 relax_start (local->X_add_symbol);
6489 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6490 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6491 relax_switch ();
6492 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6493 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6494 relax_end ();
6495 }
6496
6497 static void
6498 add_got_offset (int dest, expressionS *local)
6499 {
6500 expressionS global;
6501
6502 global.X_op = O_constant;
6503 global.X_op_symbol = NULL;
6504 global.X_add_symbol = NULL;
6505 global.X_add_number = local->X_add_number;
6506
6507 relax_start (local->X_add_symbol);
6508 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
6509 dest, dest, BFD_RELOC_LO16);
6510 relax_switch ();
6511 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
6512 relax_end ();
6513 }
6514
6515 static void
6516 add_got_offset_hilo (int dest, expressionS *local, int tmp)
6517 {
6518 expressionS global;
6519 int hold_mips_optimize;
6520
6521 global.X_op = O_constant;
6522 global.X_op_symbol = NULL;
6523 global.X_add_symbol = NULL;
6524 global.X_add_number = local->X_add_number;
6525
6526 relax_start (local->X_add_symbol);
6527 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6528 relax_switch ();
6529 /* Set mips_optimize around the lui instruction to avoid
6530 inserting an unnecessary nop after the lw. */
6531 hold_mips_optimize = mips_optimize;
6532 mips_optimize = 2;
6533 macro_build_lui (&global, tmp);
6534 mips_optimize = hold_mips_optimize;
6535 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6536 relax_end ();
6537
6538 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6539 }
6540
6541 /* Emit a sequence of instructions to emulate a branch likely operation.
6542 BR is an ordinary branch corresponding to one to be emulated. BRNEG
6543 is its complementing branch with the original condition negated.
6544 CALL is set if the original branch specified the link operation.
6545 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6546
6547 Code like this is produced in the noreorder mode:
6548
6549 BRNEG <args>, 1f
6550 nop
6551 b <sym>
6552 delay slot (executed only if branch taken)
6553 1:
6554
6555 or, if CALL is set:
6556
6557 BRNEG <args>, 1f
6558 nop
6559 bal <sym>
6560 delay slot (executed only if branch taken)
6561 1:
6562
6563 In the reorder mode the delay slot would be filled with a nop anyway,
6564 so code produced is simply:
6565
6566 BR <args>, <sym>
6567 nop
6568
6569 This function is used when producing code for the microMIPS ASE that
6570 does not implement branch likely instructions in hardware. */
6571
6572 static void
6573 macro_build_branch_likely (const char *br, const char *brneg,
6574 int call, expressionS *ep, const char *fmt,
6575 unsigned int sreg, unsigned int treg)
6576 {
6577 int noreorder = mips_opts.noreorder;
6578 expressionS expr1;
6579
6580 gas_assert (mips_opts.micromips);
6581 start_noreorder ();
6582 if (noreorder)
6583 {
6584 micromips_label_expr (&expr1);
6585 macro_build (&expr1, brneg, fmt, sreg, treg);
6586 macro_build (NULL, "nop", "");
6587 macro_build (ep, call ? "bal" : "b", "p");
6588
6589 /* Set to true so that append_insn adds a label. */
6590 emit_branch_likely_macro = TRUE;
6591 }
6592 else
6593 {
6594 macro_build (ep, br, fmt, sreg, treg);
6595 macro_build (NULL, "nop", "");
6596 }
6597 end_noreorder ();
6598 }
6599
6600 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6601 the condition code tested. EP specifies the branch target. */
6602
6603 static void
6604 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6605 {
6606 const int call = 0;
6607 const char *brneg;
6608 const char *br;
6609
6610 switch (type)
6611 {
6612 case M_BC1FL:
6613 br = "bc1f";
6614 brneg = "bc1t";
6615 break;
6616 case M_BC1TL:
6617 br = "bc1t";
6618 brneg = "bc1f";
6619 break;
6620 case M_BC2FL:
6621 br = "bc2f";
6622 brneg = "bc2t";
6623 break;
6624 case M_BC2TL:
6625 br = "bc2t";
6626 brneg = "bc2f";
6627 break;
6628 default:
6629 abort ();
6630 }
6631 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6632 }
6633
6634 /* Emit a two-argument branch macro specified by TYPE, using SREG as
6635 the register tested. EP specifies the branch target. */
6636
6637 static void
6638 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6639 {
6640 const char *brneg = NULL;
6641 const char *br;
6642 int call = 0;
6643
6644 switch (type)
6645 {
6646 case M_BGEZ:
6647 br = "bgez";
6648 break;
6649 case M_BGEZL:
6650 br = mips_opts.micromips ? "bgez" : "bgezl";
6651 brneg = "bltz";
6652 break;
6653 case M_BGEZALL:
6654 gas_assert (mips_opts.micromips);
6655 br = mips_opts.insn32 ? "bgezal" : "bgezals";
6656 brneg = "bltz";
6657 call = 1;
6658 break;
6659 case M_BGTZ:
6660 br = "bgtz";
6661 break;
6662 case M_BGTZL:
6663 br = mips_opts.micromips ? "bgtz" : "bgtzl";
6664 brneg = "blez";
6665 break;
6666 case M_BLEZ:
6667 br = "blez";
6668 break;
6669 case M_BLEZL:
6670 br = mips_opts.micromips ? "blez" : "blezl";
6671 brneg = "bgtz";
6672 break;
6673 case M_BLTZ:
6674 br = "bltz";
6675 break;
6676 case M_BLTZL:
6677 br = mips_opts.micromips ? "bltz" : "bltzl";
6678 brneg = "bgez";
6679 break;
6680 case M_BLTZALL:
6681 gas_assert (mips_opts.micromips);
6682 br = mips_opts.insn32 ? "bltzal" : "bltzals";
6683 brneg = "bgez";
6684 call = 1;
6685 break;
6686 default:
6687 abort ();
6688 }
6689 if (mips_opts.micromips && brneg)
6690 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6691 else
6692 macro_build (ep, br, "s,p", sreg);
6693 }
6694
6695 /* Emit a three-argument branch macro specified by TYPE, using SREG and
6696 TREG as the registers tested. EP specifies the branch target. */
6697
6698 static void
6699 macro_build_branch_rsrt (int type, expressionS *ep,
6700 unsigned int sreg, unsigned int treg)
6701 {
6702 const char *brneg = NULL;
6703 const int call = 0;
6704 const char *br;
6705
6706 switch (type)
6707 {
6708 case M_BEQ:
6709 case M_BEQ_I:
6710 br = "beq";
6711 break;
6712 case M_BEQL:
6713 case M_BEQL_I:
6714 br = mips_opts.micromips ? "beq" : "beql";
6715 brneg = "bne";
6716 break;
6717 case M_BNE:
6718 case M_BNE_I:
6719 br = "bne";
6720 break;
6721 case M_BNEL:
6722 case M_BNEL_I:
6723 br = mips_opts.micromips ? "bne" : "bnel";
6724 brneg = "beq";
6725 break;
6726 default:
6727 abort ();
6728 }
6729 if (mips_opts.micromips && brneg)
6730 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6731 else
6732 macro_build (ep, br, "s,t,p", sreg, treg);
6733 }
6734
6735 /*
6736 * Build macros
6737 * This routine implements the seemingly endless macro or synthesized
6738 * instructions and addressing modes in the mips assembly language. Many
6739 * of these macros are simple and are similar to each other. These could
6740 * probably be handled by some kind of table or grammar approach instead of
6741 * this verbose method. Others are not simple macros but are more like
6742 * optimizing code generation.
6743 * One interesting optimization is when several store macros appear
6744 * consecutively that would load AT with the upper half of the same address.
6745 * The ensuing load upper instructions are ommited. This implies some kind
6746 * of global optimization. We currently only optimize within a single macro.
6747 * For many of the load and store macros if the address is specified as a
6748 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6749 * first load register 'at' with zero and use it as the base register. The
6750 * mips assembler simply uses register $zero. Just one tiny optimization
6751 * we're missing.
6752 */
6753 static void
6754 macro (struct mips_cl_insn *ip, char *str)
6755 {
6756 unsigned int treg, sreg, dreg, breg;
6757 unsigned int tempreg;
6758 int mask;
6759 int used_at = 0;
6760 expressionS label_expr;
6761 expressionS expr1;
6762 expressionS *ep;
6763 const char *s;
6764 const char *s2;
6765 const char *fmt;
6766 int likely = 0;
6767 int coproc = 0;
6768 int offbits = 16;
6769 int call = 0;
6770 int jals = 0;
6771 int dbl = 0;
6772 int imm = 0;
6773 int ust = 0;
6774 int lp = 0;
6775 int ab = 0;
6776 int off;
6777 bfd_reloc_code_real_type r;
6778 int hold_mips_optimize;
6779
6780 gas_assert (! mips_opts.mips16);
6781
6782 treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6783 dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6784 sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
6785 mask = ip->insn_mo->mask;
6786
6787 label_expr.X_op = O_constant;
6788 label_expr.X_op_symbol = NULL;
6789 label_expr.X_add_symbol = NULL;
6790 label_expr.X_add_number = 0;
6791
6792 expr1.X_op = O_constant;
6793 expr1.X_op_symbol = NULL;
6794 expr1.X_add_symbol = NULL;
6795 expr1.X_add_number = 1;
6796
6797 switch (mask)
6798 {
6799 case M_DABS:
6800 dbl = 1;
6801 case M_ABS:
6802 /* bgez $a0,1f
6803 move v0,$a0
6804 sub v0,$zero,$a0
6805 1:
6806 */
6807
6808 start_noreorder ();
6809
6810 if (mips_opts.micromips)
6811 micromips_label_expr (&label_expr);
6812 else
6813 label_expr.X_add_number = 8;
6814 macro_build (&label_expr, "bgez", "s,p", sreg);
6815 if (dreg == sreg)
6816 macro_build (NULL, "nop", "");
6817 else
6818 move_register (dreg, sreg);
6819 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
6820 if (mips_opts.micromips)
6821 micromips_add_label ();
6822
6823 end_noreorder ();
6824 break;
6825
6826 case M_ADD_I:
6827 s = "addi";
6828 s2 = "add";
6829 goto do_addi;
6830 case M_ADDU_I:
6831 s = "addiu";
6832 s2 = "addu";
6833 goto do_addi;
6834 case M_DADD_I:
6835 dbl = 1;
6836 s = "daddi";
6837 s2 = "dadd";
6838 if (!mips_opts.micromips)
6839 goto do_addi;
6840 if (imm_expr.X_op == O_constant
6841 && imm_expr.X_add_number >= -0x200
6842 && imm_expr.X_add_number < 0x200)
6843 {
6844 macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6845 break;
6846 }
6847 goto do_addi_i;
6848 case M_DADDU_I:
6849 dbl = 1;
6850 s = "daddiu";
6851 s2 = "daddu";
6852 do_addi:
6853 if (imm_expr.X_op == O_constant
6854 && imm_expr.X_add_number >= -0x8000
6855 && imm_expr.X_add_number < 0x8000)
6856 {
6857 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
6858 break;
6859 }
6860 do_addi_i:
6861 used_at = 1;
6862 load_register (AT, &imm_expr, dbl);
6863 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6864 break;
6865
6866 case M_AND_I:
6867 s = "andi";
6868 s2 = "and";
6869 goto do_bit;
6870 case M_OR_I:
6871 s = "ori";
6872 s2 = "or";
6873 goto do_bit;
6874 case M_NOR_I:
6875 s = "";
6876 s2 = "nor";
6877 goto do_bit;
6878 case M_XOR_I:
6879 s = "xori";
6880 s2 = "xor";
6881 do_bit:
6882 if (imm_expr.X_op == O_constant
6883 && imm_expr.X_add_number >= 0
6884 && imm_expr.X_add_number < 0x10000)
6885 {
6886 if (mask != M_NOR_I)
6887 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
6888 else
6889 {
6890 macro_build (&imm_expr, "ori", "t,r,i",
6891 treg, sreg, BFD_RELOC_LO16);
6892 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
6893 }
6894 break;
6895 }
6896
6897 used_at = 1;
6898 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6899 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6900 break;
6901
6902 case M_BALIGN:
6903 switch (imm_expr.X_add_number)
6904 {
6905 case 0:
6906 macro_build (NULL, "nop", "");
6907 break;
6908 case 2:
6909 macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6910 break;
6911 case 1:
6912 case 3:
6913 macro_build (NULL, "balign", "t,s,2", treg, sreg,
6914 (int) imm_expr.X_add_number);
6915 break;
6916 default:
6917 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
6918 (unsigned long) imm_expr.X_add_number);
6919 break;
6920 }
6921 break;
6922
6923 case M_BC1FL:
6924 case M_BC1TL:
6925 case M_BC2FL:
6926 case M_BC2TL:
6927 gas_assert (mips_opts.micromips);
6928 macro_build_branch_ccl (mask, &offset_expr,
6929 EXTRACT_OPERAND (1, BCC, *ip));
6930 break;
6931
6932 case M_BEQ_I:
6933 case M_BEQL_I:
6934 case M_BNE_I:
6935 case M_BNEL_I:
6936 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6937 treg = 0;
6938 else
6939 {
6940 treg = AT;
6941 used_at = 1;
6942 load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
6943 }
6944 /* Fall through. */
6945 case M_BEQL:
6946 case M_BNEL:
6947 macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
6948 break;
6949
6950 case M_BGEL:
6951 likely = 1;
6952 case M_BGE:
6953 if (treg == 0)
6954 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
6955 else if (sreg == 0)
6956 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
6957 else
6958 {
6959 used_at = 1;
6960 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6961 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6962 &offset_expr, AT, ZERO);
6963 }
6964 break;
6965
6966 case M_BGEZL:
6967 case M_BGEZALL:
6968 case M_BGTZL:
6969 case M_BLEZL:
6970 case M_BLTZL:
6971 case M_BLTZALL:
6972 macro_build_branch_rs (mask, &offset_expr, sreg);
6973 break;
6974
6975 case M_BGTL_I:
6976 likely = 1;
6977 case M_BGT_I:
6978 /* Check for > max integer. */
6979 if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
6980 {
6981 do_false:
6982 /* Result is always false. */
6983 if (! likely)
6984 macro_build (NULL, "nop", "");
6985 else
6986 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
6987 break;
6988 }
6989 if (imm_expr.X_op != O_constant)
6990 as_bad (_("Unsupported large constant"));
6991 ++imm_expr.X_add_number;
6992 /* FALLTHROUGH */
6993 case M_BGE_I:
6994 case M_BGEL_I:
6995 if (mask == M_BGEL_I)
6996 likely = 1;
6997 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6998 {
6999 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
7000 &offset_expr, sreg);
7001 break;
7002 }
7003 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7004 {
7005 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
7006 &offset_expr, sreg);
7007 break;
7008 }
7009 if (imm_expr.X_op == O_constant && imm_expr.X_add_number <= GPR_SMIN)
7010 {
7011 do_true:
7012 /* result is always true */
7013 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
7014 macro_build (&offset_expr, "b", "p");
7015 break;
7016 }
7017 used_at = 1;
7018 set_at (sreg, 0);
7019 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7020 &offset_expr, AT, ZERO);
7021 break;
7022
7023 case M_BGEUL:
7024 likely = 1;
7025 case M_BGEU:
7026 if (treg == 0)
7027 goto do_true;
7028 else if (sreg == 0)
7029 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7030 &offset_expr, ZERO, treg);
7031 else
7032 {
7033 used_at = 1;
7034 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
7035 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7036 &offset_expr, AT, ZERO);
7037 }
7038 break;
7039
7040 case M_BGTUL_I:
7041 likely = 1;
7042 case M_BGTU_I:
7043 if (sreg == 0
7044 || (HAVE_32BIT_GPRS
7045 && imm_expr.X_op == O_constant
7046 && imm_expr.X_add_number == -1))
7047 goto do_false;
7048 if (imm_expr.X_op != O_constant)
7049 as_bad (_("Unsupported large constant"));
7050 ++imm_expr.X_add_number;
7051 /* FALLTHROUGH */
7052 case M_BGEU_I:
7053 case M_BGEUL_I:
7054 if (mask == M_BGEUL_I)
7055 likely = 1;
7056 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7057 goto do_true;
7058 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7059 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7060 &offset_expr, sreg, ZERO);
7061 else
7062 {
7063 used_at = 1;
7064 set_at (sreg, 1);
7065 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7066 &offset_expr, AT, ZERO);
7067 }
7068 break;
7069
7070 case M_BGTL:
7071 likely = 1;
7072 case M_BGT:
7073 if (treg == 0)
7074 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
7075 else if (sreg == 0)
7076 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
7077 else
7078 {
7079 used_at = 1;
7080 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
7081 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7082 &offset_expr, AT, ZERO);
7083 }
7084 break;
7085
7086 case M_BGTUL:
7087 likely = 1;
7088 case M_BGTU:
7089 if (treg == 0)
7090 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7091 &offset_expr, sreg, ZERO);
7092 else if (sreg == 0)
7093 goto do_false;
7094 else
7095 {
7096 used_at = 1;
7097 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
7098 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7099 &offset_expr, AT, ZERO);
7100 }
7101 break;
7102
7103 case M_BLEL:
7104 likely = 1;
7105 case M_BLE:
7106 if (treg == 0)
7107 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
7108 else if (sreg == 0)
7109 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
7110 else
7111 {
7112 used_at = 1;
7113 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
7114 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7115 &offset_expr, AT, ZERO);
7116 }
7117 break;
7118
7119 case M_BLEL_I:
7120 likely = 1;
7121 case M_BLE_I:
7122 if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
7123 goto do_true;
7124 if (imm_expr.X_op != O_constant)
7125 as_bad (_("Unsupported large constant"));
7126 ++imm_expr.X_add_number;
7127 /* FALLTHROUGH */
7128 case M_BLT_I:
7129 case M_BLTL_I:
7130 if (mask == M_BLTL_I)
7131 likely = 1;
7132 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7133 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
7134 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7135 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
7136 else
7137 {
7138 used_at = 1;
7139 set_at (sreg, 0);
7140 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7141 &offset_expr, AT, ZERO);
7142 }
7143 break;
7144
7145 case M_BLEUL:
7146 likely = 1;
7147 case M_BLEU:
7148 if (treg == 0)
7149 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7150 &offset_expr, sreg, ZERO);
7151 else if (sreg == 0)
7152 goto do_true;
7153 else
7154 {
7155 used_at = 1;
7156 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
7157 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7158 &offset_expr, AT, ZERO);
7159 }
7160 break;
7161
7162 case M_BLEUL_I:
7163 likely = 1;
7164 case M_BLEU_I:
7165 if (sreg == 0
7166 || (HAVE_32BIT_GPRS
7167 && imm_expr.X_op == O_constant
7168 && imm_expr.X_add_number == -1))
7169 goto do_true;
7170 if (imm_expr.X_op != O_constant)
7171 as_bad (_("Unsupported large constant"));
7172 ++imm_expr.X_add_number;
7173 /* FALLTHROUGH */
7174 case M_BLTU_I:
7175 case M_BLTUL_I:
7176 if (mask == M_BLTUL_I)
7177 likely = 1;
7178 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7179 goto do_false;
7180 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7181 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7182 &offset_expr, sreg, ZERO);
7183 else
7184 {
7185 used_at = 1;
7186 set_at (sreg, 1);
7187 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7188 &offset_expr, AT, ZERO);
7189 }
7190 break;
7191
7192 case M_BLTL:
7193 likely = 1;
7194 case M_BLT:
7195 if (treg == 0)
7196 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
7197 else if (sreg == 0)
7198 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
7199 else
7200 {
7201 used_at = 1;
7202 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
7203 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7204 &offset_expr, AT, ZERO);
7205 }
7206 break;
7207
7208 case M_BLTUL:
7209 likely = 1;
7210 case M_BLTU:
7211 if (treg == 0)
7212 goto do_false;
7213 else if (sreg == 0)
7214 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7215 &offset_expr, ZERO, treg);
7216 else
7217 {
7218 used_at = 1;
7219 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
7220 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7221 &offset_expr, AT, ZERO);
7222 }
7223 break;
7224
7225 case M_DEXT:
7226 {
7227 /* Use unsigned arithmetic. */
7228 addressT pos;
7229 addressT size;
7230
7231 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
7232 {
7233 as_bad (_("Unsupported large constant"));
7234 pos = size = 1;
7235 }
7236 else
7237 {
7238 pos = imm_expr.X_add_number;
7239 size = imm2_expr.X_add_number;
7240 }
7241
7242 if (pos > 63)
7243 {
7244 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
7245 pos = 1;
7246 }
7247 if (size == 0 || size > 64 || (pos + size - 1) > 63)
7248 {
7249 as_bad (_("Improper extract size (%lu, position %lu)"),
7250 (unsigned long) size, (unsigned long) pos);
7251 size = 1;
7252 }
7253
7254 if (size <= 32 && pos < 32)
7255 {
7256 s = "dext";
7257 fmt = "t,r,+A,+C";
7258 }
7259 else if (size <= 32)
7260 {
7261 s = "dextu";
7262 fmt = "t,r,+E,+H";
7263 }
7264 else
7265 {
7266 s = "dextm";
7267 fmt = "t,r,+A,+G";
7268 }
7269 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
7270 (int) (size - 1));
7271 }
7272 break;
7273
7274 case M_DINS:
7275 {
7276 /* Use unsigned arithmetic. */
7277 addressT pos;
7278 addressT size;
7279
7280 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
7281 {
7282 as_bad (_("Unsupported large constant"));
7283 pos = size = 1;
7284 }
7285 else
7286 {
7287 pos = imm_expr.X_add_number;
7288 size = imm2_expr.X_add_number;
7289 }
7290
7291 if (pos > 63)
7292 {
7293 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
7294 pos = 1;
7295 }
7296 if (size == 0 || size > 64 || (pos + size - 1) > 63)
7297 {
7298 as_bad (_("Improper insert size (%lu, position %lu)"),
7299 (unsigned long) size, (unsigned long) pos);
7300 size = 1;
7301 }
7302
7303 if (pos < 32 && (pos + size - 1) < 32)
7304 {
7305 s = "dins";
7306 fmt = "t,r,+A,+B";
7307 }
7308 else if (pos >= 32)
7309 {
7310 s = "dinsu";
7311 fmt = "t,r,+E,+F";
7312 }
7313 else
7314 {
7315 s = "dinsm";
7316 fmt = "t,r,+A,+F";
7317 }
7318 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
7319 (int) (pos + size - 1));
7320 }
7321 break;
7322
7323 case M_DDIV_3:
7324 dbl = 1;
7325 case M_DIV_3:
7326 s = "mflo";
7327 goto do_div3;
7328 case M_DREM_3:
7329 dbl = 1;
7330 case M_REM_3:
7331 s = "mfhi";
7332 do_div3:
7333 if (treg == 0)
7334 {
7335 as_warn (_("Divide by zero."));
7336 if (mips_trap)
7337 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
7338 else
7339 macro_build (NULL, "break", BRK_FMT, 7);
7340 break;
7341 }
7342
7343 start_noreorder ();
7344 if (mips_trap)
7345 {
7346 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
7347 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
7348 }
7349 else
7350 {
7351 if (mips_opts.micromips)
7352 micromips_label_expr (&label_expr);
7353 else
7354 label_expr.X_add_number = 8;
7355 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
7356 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
7357 macro_build (NULL, "break", BRK_FMT, 7);
7358 if (mips_opts.micromips)
7359 micromips_add_label ();
7360 }
7361 expr1.X_add_number = -1;
7362 used_at = 1;
7363 load_register (AT, &expr1, dbl);
7364 if (mips_opts.micromips)
7365 micromips_label_expr (&label_expr);
7366 else
7367 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
7368 macro_build (&label_expr, "bne", "s,t,p", treg, AT);
7369 if (dbl)
7370 {
7371 expr1.X_add_number = 1;
7372 load_register (AT, &expr1, dbl);
7373 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
7374 }
7375 else
7376 {
7377 expr1.X_add_number = 0x80000000;
7378 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
7379 }
7380 if (mips_trap)
7381 {
7382 macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
7383 /* We want to close the noreorder block as soon as possible, so
7384 that later insns are available for delay slot filling. */
7385 end_noreorder ();
7386 }
7387 else
7388 {
7389 if (mips_opts.micromips)
7390 micromips_label_expr (&label_expr);
7391 else
7392 label_expr.X_add_number = 8;
7393 macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
7394 macro_build (NULL, "nop", "");
7395
7396 /* We want to close the noreorder block as soon as possible, so
7397 that later insns are available for delay slot filling. */
7398 end_noreorder ();
7399
7400 macro_build (NULL, "break", BRK_FMT, 6);
7401 }
7402 if (mips_opts.micromips)
7403 micromips_add_label ();
7404 macro_build (NULL, s, MFHL_FMT, dreg);
7405 break;
7406
7407 case M_DIV_3I:
7408 s = "div";
7409 s2 = "mflo";
7410 goto do_divi;
7411 case M_DIVU_3I:
7412 s = "divu";
7413 s2 = "mflo";
7414 goto do_divi;
7415 case M_REM_3I:
7416 s = "div";
7417 s2 = "mfhi";
7418 goto do_divi;
7419 case M_REMU_3I:
7420 s = "divu";
7421 s2 = "mfhi";
7422 goto do_divi;
7423 case M_DDIV_3I:
7424 dbl = 1;
7425 s = "ddiv";
7426 s2 = "mflo";
7427 goto do_divi;
7428 case M_DDIVU_3I:
7429 dbl = 1;
7430 s = "ddivu";
7431 s2 = "mflo";
7432 goto do_divi;
7433 case M_DREM_3I:
7434 dbl = 1;
7435 s = "ddiv";
7436 s2 = "mfhi";
7437 goto do_divi;
7438 case M_DREMU_3I:
7439 dbl = 1;
7440 s = "ddivu";
7441 s2 = "mfhi";
7442 do_divi:
7443 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7444 {
7445 as_warn (_("Divide by zero."));
7446 if (mips_trap)
7447 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
7448 else
7449 macro_build (NULL, "break", BRK_FMT, 7);
7450 break;
7451 }
7452 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7453 {
7454 if (strcmp (s2, "mflo") == 0)
7455 move_register (dreg, sreg);
7456 else
7457 move_register (dreg, ZERO);
7458 break;
7459 }
7460 if (imm_expr.X_op == O_constant
7461 && imm_expr.X_add_number == -1
7462 && s[strlen (s) - 1] != 'u')
7463 {
7464 if (strcmp (s2, "mflo") == 0)
7465 {
7466 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
7467 }
7468 else
7469 move_register (dreg, ZERO);
7470 break;
7471 }
7472
7473 used_at = 1;
7474 load_register (AT, &imm_expr, dbl);
7475 macro_build (NULL, s, "z,s,t", sreg, AT);
7476 macro_build (NULL, s2, MFHL_FMT, dreg);
7477 break;
7478
7479 case M_DIVU_3:
7480 s = "divu";
7481 s2 = "mflo";
7482 goto do_divu3;
7483 case M_REMU_3:
7484 s = "divu";
7485 s2 = "mfhi";
7486 goto do_divu3;
7487 case M_DDIVU_3:
7488 s = "ddivu";
7489 s2 = "mflo";
7490 goto do_divu3;
7491 case M_DREMU_3:
7492 s = "ddivu";
7493 s2 = "mfhi";
7494 do_divu3:
7495 start_noreorder ();
7496 if (mips_trap)
7497 {
7498 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
7499 macro_build (NULL, s, "z,s,t", sreg, treg);
7500 /* We want to close the noreorder block as soon as possible, so
7501 that later insns are available for delay slot filling. */
7502 end_noreorder ();
7503 }
7504 else
7505 {
7506 if (mips_opts.micromips)
7507 micromips_label_expr (&label_expr);
7508 else
7509 label_expr.X_add_number = 8;
7510 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
7511 macro_build (NULL, s, "z,s,t", sreg, treg);
7512
7513 /* We want to close the noreorder block as soon as possible, so
7514 that later insns are available for delay slot filling. */
7515 end_noreorder ();
7516 macro_build (NULL, "break", BRK_FMT, 7);
7517 if (mips_opts.micromips)
7518 micromips_add_label ();
7519 }
7520 macro_build (NULL, s2, MFHL_FMT, dreg);
7521 break;
7522
7523 case M_DLCA_AB:
7524 dbl = 1;
7525 case M_LCA_AB:
7526 call = 1;
7527 goto do_la;
7528 case M_DLA_AB:
7529 dbl = 1;
7530 case M_LA_AB:
7531 do_la:
7532 /* Load the address of a symbol into a register. If breg is not
7533 zero, we then add a base register to it. */
7534
7535 if (dbl && HAVE_32BIT_GPRS)
7536 as_warn (_("dla used to load 32-bit register"));
7537
7538 if (!dbl && HAVE_64BIT_OBJECTS)
7539 as_warn (_("la used to load 64-bit address"));
7540
7541 if (offset_expr.X_op == O_constant
7542 && offset_expr.X_add_number >= -0x8000
7543 && offset_expr.X_add_number < 0x8000)
7544 {
7545 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
7546 "t,r,j", treg, sreg, BFD_RELOC_LO16);
7547 break;
7548 }
7549
7550 if (mips_opts.at && (treg == breg))
7551 {
7552 tempreg = AT;
7553 used_at = 1;
7554 }
7555 else
7556 {
7557 tempreg = treg;
7558 }
7559
7560 if (offset_expr.X_op != O_symbol
7561 && offset_expr.X_op != O_constant)
7562 {
7563 as_bad (_("Expression too complex"));
7564 offset_expr.X_op = O_constant;
7565 }
7566
7567 if (offset_expr.X_op == O_constant)
7568 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
7569 else if (mips_pic == NO_PIC)
7570 {
7571 /* If this is a reference to a GP relative symbol, we want
7572 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
7573 Otherwise we want
7574 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
7575 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7576 If we have a constant, we need two instructions anyhow,
7577 so we may as well always use the latter form.
7578
7579 With 64bit address space and a usable $at we want
7580 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7581 lui $at,<sym> (BFD_RELOC_HI16_S)
7582 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7583 daddiu $at,<sym> (BFD_RELOC_LO16)
7584 dsll32 $tempreg,0
7585 daddu $tempreg,$tempreg,$at
7586
7587 If $at is already in use, we use a path which is suboptimal
7588 on superscalar processors.
7589 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7590 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7591 dsll $tempreg,16
7592 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
7593 dsll $tempreg,16
7594 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
7595
7596 For GP relative symbols in 64bit address space we can use
7597 the same sequence as in 32bit address space. */
7598 if (HAVE_64BIT_SYMBOLS)
7599 {
7600 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7601 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7602 {
7603 relax_start (offset_expr.X_add_symbol);
7604 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7605 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7606 relax_switch ();
7607 }
7608
7609 if (used_at == 0 && mips_opts.at)
7610 {
7611 macro_build (&offset_expr, "lui", LUI_FMT,
7612 tempreg, BFD_RELOC_MIPS_HIGHEST);
7613 macro_build (&offset_expr, "lui", LUI_FMT,
7614 AT, BFD_RELOC_HI16_S);
7615 macro_build (&offset_expr, "daddiu", "t,r,j",
7616 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7617 macro_build (&offset_expr, "daddiu", "t,r,j",
7618 AT, AT, BFD_RELOC_LO16);
7619 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
7620 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
7621 used_at = 1;
7622 }
7623 else
7624 {
7625 macro_build (&offset_expr, "lui", LUI_FMT,
7626 tempreg, BFD_RELOC_MIPS_HIGHEST);
7627 macro_build (&offset_expr, "daddiu", "t,r,j",
7628 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7629 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7630 macro_build (&offset_expr, "daddiu", "t,r,j",
7631 tempreg, tempreg, BFD_RELOC_HI16_S);
7632 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7633 macro_build (&offset_expr, "daddiu", "t,r,j",
7634 tempreg, tempreg, BFD_RELOC_LO16);
7635 }
7636
7637 if (mips_relax.sequence)
7638 relax_end ();
7639 }
7640 else
7641 {
7642 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7643 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7644 {
7645 relax_start (offset_expr.X_add_symbol);
7646 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7647 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7648 relax_switch ();
7649 }
7650 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
7651 as_bad (_("Offset too large"));
7652 macro_build_lui (&offset_expr, tempreg);
7653 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7654 tempreg, tempreg, BFD_RELOC_LO16);
7655 if (mips_relax.sequence)
7656 relax_end ();
7657 }
7658 }
7659 else if (!mips_big_got && !HAVE_NEWABI)
7660 {
7661 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7662
7663 /* If this is a reference to an external symbol, and there
7664 is no constant, we want
7665 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7666 or for lca or if tempreg is PIC_CALL_REG
7667 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7668 For a local symbol, we want
7669 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7670 nop
7671 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7672
7673 If we have a small constant, and this is a reference to
7674 an external symbol, we want
7675 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7676 nop
7677 addiu $tempreg,$tempreg,<constant>
7678 For a local symbol, we want the same instruction
7679 sequence, but we output a BFD_RELOC_LO16 reloc on the
7680 addiu instruction.
7681
7682 If we have a large constant, and this is a reference to
7683 an external symbol, we want
7684 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7685 lui $at,<hiconstant>
7686 addiu $at,$at,<loconstant>
7687 addu $tempreg,$tempreg,$at
7688 For a local symbol, we want the same instruction
7689 sequence, but we output a BFD_RELOC_LO16 reloc on the
7690 addiu instruction.
7691 */
7692
7693 if (offset_expr.X_add_number == 0)
7694 {
7695 if (mips_pic == SVR4_PIC
7696 && breg == 0
7697 && (call || tempreg == PIC_CALL_REG))
7698 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7699
7700 relax_start (offset_expr.X_add_symbol);
7701 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7702 lw_reloc_type, mips_gp_register);
7703 if (breg != 0)
7704 {
7705 /* We're going to put in an addu instruction using
7706 tempreg, so we may as well insert the nop right
7707 now. */
7708 load_delay_nop ();
7709 }
7710 relax_switch ();
7711 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7712 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
7713 load_delay_nop ();
7714 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7715 tempreg, tempreg, BFD_RELOC_LO16);
7716 relax_end ();
7717 /* FIXME: If breg == 0, and the next instruction uses
7718 $tempreg, then if this variant case is used an extra
7719 nop will be generated. */
7720 }
7721 else if (offset_expr.X_add_number >= -0x8000
7722 && offset_expr.X_add_number < 0x8000)
7723 {
7724 load_got_offset (tempreg, &offset_expr);
7725 load_delay_nop ();
7726 add_got_offset (tempreg, &offset_expr);
7727 }
7728 else
7729 {
7730 expr1.X_add_number = offset_expr.X_add_number;
7731 offset_expr.X_add_number =
7732 SEXT_16BIT (offset_expr.X_add_number);
7733 load_got_offset (tempreg, &offset_expr);
7734 offset_expr.X_add_number = expr1.X_add_number;
7735 /* If we are going to add in a base register, and the
7736 target register and the base register are the same,
7737 then we are using AT as a temporary register. Since
7738 we want to load the constant into AT, we add our
7739 current AT (from the global offset table) and the
7740 register into the register now, and pretend we were
7741 not using a base register. */
7742 if (breg == treg)
7743 {
7744 load_delay_nop ();
7745 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7746 treg, AT, breg);
7747 breg = 0;
7748 tempreg = treg;
7749 }
7750 add_got_offset_hilo (tempreg, &offset_expr, AT);
7751 used_at = 1;
7752 }
7753 }
7754 else if (!mips_big_got && HAVE_NEWABI)
7755 {
7756 int add_breg_early = 0;
7757
7758 /* If this is a reference to an external, and there is no
7759 constant, or local symbol (*), with or without a
7760 constant, we want
7761 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7762 or for lca or if tempreg is PIC_CALL_REG
7763 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7764
7765 If we have a small constant, and this is a reference to
7766 an external symbol, we want
7767 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7768 addiu $tempreg,$tempreg,<constant>
7769
7770 If we have a large constant, and this is a reference to
7771 an external symbol, we want
7772 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7773 lui $at,<hiconstant>
7774 addiu $at,$at,<loconstant>
7775 addu $tempreg,$tempreg,$at
7776
7777 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7778 local symbols, even though it introduces an additional
7779 instruction. */
7780
7781 if (offset_expr.X_add_number)
7782 {
7783 expr1.X_add_number = offset_expr.X_add_number;
7784 offset_expr.X_add_number = 0;
7785
7786 relax_start (offset_expr.X_add_symbol);
7787 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7788 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7789
7790 if (expr1.X_add_number >= -0x8000
7791 && expr1.X_add_number < 0x8000)
7792 {
7793 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7794 tempreg, tempreg, BFD_RELOC_LO16);
7795 }
7796 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
7797 {
7798 /* If we are going to add in a base register, and the
7799 target register and the base register are the same,
7800 then we are using AT as a temporary register. Since
7801 we want to load the constant into AT, we add our
7802 current AT (from the global offset table) and the
7803 register into the register now, and pretend we were
7804 not using a base register. */
7805 if (breg != treg)
7806 dreg = tempreg;
7807 else
7808 {
7809 gas_assert (tempreg == AT);
7810 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7811 treg, AT, breg);
7812 dreg = treg;
7813 add_breg_early = 1;
7814 }
7815
7816 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7817 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7818 dreg, dreg, AT);
7819
7820 used_at = 1;
7821 }
7822 else
7823 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7824
7825 relax_switch ();
7826 offset_expr.X_add_number = expr1.X_add_number;
7827
7828 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7829 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7830 if (add_breg_early)
7831 {
7832 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7833 treg, tempreg, breg);
7834 breg = 0;
7835 tempreg = treg;
7836 }
7837 relax_end ();
7838 }
7839 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
7840 {
7841 relax_start (offset_expr.X_add_symbol);
7842 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7843 BFD_RELOC_MIPS_CALL16, mips_gp_register);
7844 relax_switch ();
7845 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7846 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7847 relax_end ();
7848 }
7849 else
7850 {
7851 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7852 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7853 }
7854 }
7855 else if (mips_big_got && !HAVE_NEWABI)
7856 {
7857 int gpdelay;
7858 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7859 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
7860 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7861
7862 /* This is the large GOT case. If this is a reference to an
7863 external symbol, and there is no constant, we want
7864 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7865 addu $tempreg,$tempreg,$gp
7866 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7867 or for lca or if tempreg is PIC_CALL_REG
7868 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7869 addu $tempreg,$tempreg,$gp
7870 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7871 For a local symbol, we want
7872 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7873 nop
7874 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7875
7876 If we have a small constant, and this is a reference to
7877 an external symbol, we want
7878 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7879 addu $tempreg,$tempreg,$gp
7880 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7881 nop
7882 addiu $tempreg,$tempreg,<constant>
7883 For a local symbol, we want
7884 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7885 nop
7886 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7887
7888 If we have a large constant, and this is a reference to
7889 an external symbol, we want
7890 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7891 addu $tempreg,$tempreg,$gp
7892 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7893 lui $at,<hiconstant>
7894 addiu $at,$at,<loconstant>
7895 addu $tempreg,$tempreg,$at
7896 For a local symbol, we want
7897 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7898 lui $at,<hiconstant>
7899 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
7900 addu $tempreg,$tempreg,$at
7901 */
7902
7903 expr1.X_add_number = offset_expr.X_add_number;
7904 offset_expr.X_add_number = 0;
7905 relax_start (offset_expr.X_add_symbol);
7906 gpdelay = reg_needs_delay (mips_gp_register);
7907 if (expr1.X_add_number == 0 && breg == 0
7908 && (call || tempreg == PIC_CALL_REG))
7909 {
7910 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7911 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7912 }
7913 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
7914 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7915 tempreg, tempreg, mips_gp_register);
7916 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7917 tempreg, lw_reloc_type, tempreg);
7918 if (expr1.X_add_number == 0)
7919 {
7920 if (breg != 0)
7921 {
7922 /* We're going to put in an addu instruction using
7923 tempreg, so we may as well insert the nop right
7924 now. */
7925 load_delay_nop ();
7926 }
7927 }
7928 else if (expr1.X_add_number >= -0x8000
7929 && expr1.X_add_number < 0x8000)
7930 {
7931 load_delay_nop ();
7932 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7933 tempreg, tempreg, BFD_RELOC_LO16);
7934 }
7935 else
7936 {
7937 /* If we are going to add in a base register, and the
7938 target register and the base register are the same,
7939 then we are using AT as a temporary register. Since
7940 we want to load the constant into AT, we add our
7941 current AT (from the global offset table) and the
7942 register into the register now, and pretend we were
7943 not using a base register. */
7944 if (breg != treg)
7945 dreg = tempreg;
7946 else
7947 {
7948 gas_assert (tempreg == AT);
7949 load_delay_nop ();
7950 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7951 treg, AT, breg);
7952 dreg = treg;
7953 }
7954
7955 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7956 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
7957
7958 used_at = 1;
7959 }
7960 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
7961 relax_switch ();
7962
7963 if (gpdelay)
7964 {
7965 /* This is needed because this instruction uses $gp, but
7966 the first instruction on the main stream does not. */
7967 macro_build (NULL, "nop", "");
7968 }
7969
7970 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7971 local_reloc_type, mips_gp_register);
7972 if (expr1.X_add_number >= -0x8000
7973 && expr1.X_add_number < 0x8000)
7974 {
7975 load_delay_nop ();
7976 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7977 tempreg, tempreg, BFD_RELOC_LO16);
7978 /* FIXME: If add_number is 0, and there was no base
7979 register, the external symbol case ended with a load,
7980 so if the symbol turns out to not be external, and
7981 the next instruction uses tempreg, an unnecessary nop
7982 will be inserted. */
7983 }
7984 else
7985 {
7986 if (breg == treg)
7987 {
7988 /* We must add in the base register now, as in the
7989 external symbol case. */
7990 gas_assert (tempreg == AT);
7991 load_delay_nop ();
7992 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7993 treg, AT, breg);
7994 tempreg = treg;
7995 /* We set breg to 0 because we have arranged to add
7996 it in in both cases. */
7997 breg = 0;
7998 }
7999
8000 macro_build_lui (&expr1, AT);
8001 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8002 AT, AT, BFD_RELOC_LO16);
8003 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8004 tempreg, tempreg, AT);
8005 used_at = 1;
8006 }
8007 relax_end ();
8008 }
8009 else if (mips_big_got && HAVE_NEWABI)
8010 {
8011 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
8012 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
8013 int add_breg_early = 0;
8014
8015 /* This is the large GOT case. If this is a reference to an
8016 external symbol, and there is no constant, we want
8017 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8018 add $tempreg,$tempreg,$gp
8019 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8020 or for lca or if tempreg is PIC_CALL_REG
8021 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
8022 add $tempreg,$tempreg,$gp
8023 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
8024
8025 If we have a small constant, and this is a reference to
8026 an external symbol, we want
8027 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8028 add $tempreg,$tempreg,$gp
8029 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8030 addi $tempreg,$tempreg,<constant>
8031
8032 If we have a large constant, and this is a reference to
8033 an external symbol, we want
8034 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8035 addu $tempreg,$tempreg,$gp
8036 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8037 lui $at,<hiconstant>
8038 addi $at,$at,<loconstant>
8039 add $tempreg,$tempreg,$at
8040
8041 If we have NewABI, and we know it's a local symbol, we want
8042 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8043 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
8044 otherwise we have to resort to GOT_HI16/GOT_LO16. */
8045
8046 relax_start (offset_expr.X_add_symbol);
8047
8048 expr1.X_add_number = offset_expr.X_add_number;
8049 offset_expr.X_add_number = 0;
8050
8051 if (expr1.X_add_number == 0 && breg == 0
8052 && (call || tempreg == PIC_CALL_REG))
8053 {
8054 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
8055 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
8056 }
8057 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
8058 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8059 tempreg, tempreg, mips_gp_register);
8060 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8061 tempreg, lw_reloc_type, tempreg);
8062
8063 if (expr1.X_add_number == 0)
8064 ;
8065 else if (expr1.X_add_number >= -0x8000
8066 && expr1.X_add_number < 0x8000)
8067 {
8068 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
8069 tempreg, tempreg, BFD_RELOC_LO16);
8070 }
8071 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
8072 {
8073 /* If we are going to add in a base register, and the
8074 target register and the base register are the same,
8075 then we are using AT as a temporary register. Since
8076 we want to load the constant into AT, we add our
8077 current AT (from the global offset table) and the
8078 register into the register now, and pretend we were
8079 not using a base register. */
8080 if (breg != treg)
8081 dreg = tempreg;
8082 else
8083 {
8084 gas_assert (tempreg == AT);
8085 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8086 treg, AT, breg);
8087 dreg = treg;
8088 add_breg_early = 1;
8089 }
8090
8091 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
8092 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
8093
8094 used_at = 1;
8095 }
8096 else
8097 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
8098
8099 relax_switch ();
8100 offset_expr.X_add_number = expr1.X_add_number;
8101 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8102 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8103 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8104 tempreg, BFD_RELOC_MIPS_GOT_OFST);
8105 if (add_breg_early)
8106 {
8107 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8108 treg, tempreg, breg);
8109 breg = 0;
8110 tempreg = treg;
8111 }
8112 relax_end ();
8113 }
8114 else
8115 abort ();
8116
8117 if (breg != 0)
8118 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
8119 break;
8120
8121 case M_MSGSND:
8122 gas_assert (!mips_opts.micromips);
8123 {
8124 unsigned long temp = (treg << 16) | (0x01);
8125 macro_build (NULL, "c2", "C", temp);
8126 }
8127 break;
8128
8129 case M_MSGLD:
8130 gas_assert (!mips_opts.micromips);
8131 {
8132 unsigned long temp = (0x02);
8133 macro_build (NULL, "c2", "C", temp);
8134 }
8135 break;
8136
8137 case M_MSGLD_T:
8138 gas_assert (!mips_opts.micromips);
8139 {
8140 unsigned long temp = (treg << 16) | (0x02);
8141 macro_build (NULL, "c2", "C", temp);
8142 }
8143 break;
8144
8145 case M_MSGWAIT:
8146 gas_assert (!mips_opts.micromips);
8147 macro_build (NULL, "c2", "C", 3);
8148 break;
8149
8150 case M_MSGWAIT_T:
8151 gas_assert (!mips_opts.micromips);
8152 {
8153 unsigned long temp = (treg << 16) | 0x03;
8154 macro_build (NULL, "c2", "C", temp);
8155 }
8156 break;
8157
8158 case M_J_A:
8159 /* The j instruction may not be used in PIC code, since it
8160 requires an absolute address. We convert it to a b
8161 instruction. */
8162 if (mips_pic == NO_PIC)
8163 macro_build (&offset_expr, "j", "a");
8164 else
8165 macro_build (&offset_expr, "b", "p");
8166 break;
8167
8168 /* The jal instructions must be handled as macros because when
8169 generating PIC code they expand to multi-instruction
8170 sequences. Normally they are simple instructions. */
8171 case M_JALS_1:
8172 dreg = RA;
8173 /* Fall through. */
8174 case M_JALS_2:
8175 gas_assert (mips_opts.micromips);
8176 if (mips_opts.insn32)
8177 {
8178 as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
8179 break;
8180 }
8181 jals = 1;
8182 goto jal;
8183 case M_JAL_1:
8184 dreg = RA;
8185 /* Fall through. */
8186 case M_JAL_2:
8187 jal:
8188 if (mips_pic == NO_PIC)
8189 {
8190 s = jals ? "jalrs" : "jalr";
8191 if (mips_opts.micromips
8192 && !mips_opts.insn32
8193 && dreg == RA
8194 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
8195 macro_build (NULL, s, "mj", sreg);
8196 else
8197 macro_build (NULL, s, JALR_FMT, dreg, sreg);
8198 }
8199 else
8200 {
8201 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
8202 && mips_cprestore_offset >= 0);
8203
8204 if (sreg != PIC_CALL_REG)
8205 as_warn (_("MIPS PIC call to register other than $25"));
8206
8207 s = ((mips_opts.micromips
8208 && !mips_opts.insn32
8209 && (!mips_opts.noreorder || cprestore))
8210 ? "jalrs" : "jalr");
8211 if (mips_opts.micromips
8212 && !mips_opts.insn32
8213 && dreg == RA
8214 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
8215 macro_build (NULL, s, "mj", sreg);
8216 else
8217 macro_build (NULL, s, JALR_FMT, dreg, sreg);
8218 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
8219 {
8220 if (mips_cprestore_offset < 0)
8221 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8222 else
8223 {
8224 if (!mips_frame_reg_valid)
8225 {
8226 as_warn (_("No .frame pseudo-op used in PIC code"));
8227 /* Quiet this warning. */
8228 mips_frame_reg_valid = 1;
8229 }
8230 if (!mips_cprestore_valid)
8231 {
8232 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8233 /* Quiet this warning. */
8234 mips_cprestore_valid = 1;
8235 }
8236 if (mips_opts.noreorder)
8237 macro_build (NULL, "nop", "");
8238 expr1.X_add_number = mips_cprestore_offset;
8239 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
8240 mips_gp_register,
8241 mips_frame_reg,
8242 HAVE_64BIT_ADDRESSES);
8243 }
8244 }
8245 }
8246
8247 break;
8248
8249 case M_JALS_A:
8250 gas_assert (mips_opts.micromips);
8251 if (mips_opts.insn32)
8252 {
8253 as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
8254 break;
8255 }
8256 jals = 1;
8257 /* Fall through. */
8258 case M_JAL_A:
8259 if (mips_pic == NO_PIC)
8260 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
8261 else if (mips_pic == SVR4_PIC)
8262 {
8263 /* If this is a reference to an external symbol, and we are
8264 using a small GOT, we want
8265 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
8266 nop
8267 jalr $ra,$25
8268 nop
8269 lw $gp,cprestore($sp)
8270 The cprestore value is set using the .cprestore
8271 pseudo-op. If we are using a big GOT, we want
8272 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
8273 addu $25,$25,$gp
8274 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
8275 nop
8276 jalr $ra,$25
8277 nop
8278 lw $gp,cprestore($sp)
8279 If the symbol is not external, we want
8280 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8281 nop
8282 addiu $25,$25,<sym> (BFD_RELOC_LO16)
8283 jalr $ra,$25
8284 nop
8285 lw $gp,cprestore($sp)
8286
8287 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
8288 sequences above, minus nops, unless the symbol is local,
8289 which enables us to use GOT_PAGE/GOT_OFST (big got) or
8290 GOT_DISP. */
8291 if (HAVE_NEWABI)
8292 {
8293 if (!mips_big_got)
8294 {
8295 relax_start (offset_expr.X_add_symbol);
8296 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8297 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
8298 mips_gp_register);
8299 relax_switch ();
8300 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8301 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
8302 mips_gp_register);
8303 relax_end ();
8304 }
8305 else
8306 {
8307 relax_start (offset_expr.X_add_symbol);
8308 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
8309 BFD_RELOC_MIPS_CALL_HI16);
8310 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8311 PIC_CALL_REG, mips_gp_register);
8312 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8313 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8314 PIC_CALL_REG);
8315 relax_switch ();
8316 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8317 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
8318 mips_gp_register);
8319 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8320 PIC_CALL_REG, PIC_CALL_REG,
8321 BFD_RELOC_MIPS_GOT_OFST);
8322 relax_end ();
8323 }
8324
8325 macro_build_jalr (&offset_expr, 0);
8326 }
8327 else
8328 {
8329 relax_start (offset_expr.X_add_symbol);
8330 if (!mips_big_got)
8331 {
8332 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8333 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
8334 mips_gp_register);
8335 load_delay_nop ();
8336 relax_switch ();
8337 }
8338 else
8339 {
8340 int gpdelay;
8341
8342 gpdelay = reg_needs_delay (mips_gp_register);
8343 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
8344 BFD_RELOC_MIPS_CALL_HI16);
8345 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8346 PIC_CALL_REG, mips_gp_register);
8347 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8348 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8349 PIC_CALL_REG);
8350 load_delay_nop ();
8351 relax_switch ();
8352 if (gpdelay)
8353 macro_build (NULL, "nop", "");
8354 }
8355 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8356 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
8357 mips_gp_register);
8358 load_delay_nop ();
8359 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8360 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
8361 relax_end ();
8362 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
8363
8364 if (mips_cprestore_offset < 0)
8365 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8366 else
8367 {
8368 if (!mips_frame_reg_valid)
8369 {
8370 as_warn (_("No .frame pseudo-op used in PIC code"));
8371 /* Quiet this warning. */
8372 mips_frame_reg_valid = 1;
8373 }
8374 if (!mips_cprestore_valid)
8375 {
8376 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8377 /* Quiet this warning. */
8378 mips_cprestore_valid = 1;
8379 }
8380 if (mips_opts.noreorder)
8381 macro_build (NULL, "nop", "");
8382 expr1.X_add_number = mips_cprestore_offset;
8383 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
8384 mips_gp_register,
8385 mips_frame_reg,
8386 HAVE_64BIT_ADDRESSES);
8387 }
8388 }
8389 }
8390 else if (mips_pic == VXWORKS_PIC)
8391 as_bad (_("Non-PIC jump used in PIC library"));
8392 else
8393 abort ();
8394
8395 break;
8396
8397 case M_LBUE_AB:
8398 ab = 1;
8399 case M_LBUE_OB:
8400 s = "lbue";
8401 fmt = "t,+j(b)";
8402 offbits = 9;
8403 goto ld_st;
8404 case M_LHUE_AB:
8405 ab = 1;
8406 case M_LHUE_OB:
8407 s = "lhue";
8408 fmt = "t,+j(b)";
8409 offbits = 9;
8410 goto ld_st;
8411 case M_LBE_AB:
8412 ab = 1;
8413 case M_LBE_OB:
8414 s = "lbe";
8415 fmt = "t,+j(b)";
8416 offbits = 9;
8417 goto ld_st;
8418 case M_LHE_AB:
8419 ab = 1;
8420 case M_LHE_OB:
8421 s = "lhe";
8422 fmt = "t,+j(b)";
8423 offbits = 9;
8424 goto ld_st;
8425 case M_LLE_AB:
8426 ab = 1;
8427 case M_LLE_OB:
8428 s = "lle";
8429 fmt = "t,+j(b)";
8430 offbits = 9;
8431 goto ld_st;
8432 case M_LWE_AB:
8433 ab = 1;
8434 case M_LWE_OB:
8435 s = "lwe";
8436 fmt = "t,+j(b)";
8437 offbits = 9;
8438 goto ld_st;
8439 case M_LWLE_AB:
8440 ab = 1;
8441 case M_LWLE_OB:
8442 s = "lwle";
8443 fmt = "t,+j(b)";
8444 offbits = 9;
8445 goto ld_st;
8446 case M_LWRE_AB:
8447 ab = 1;
8448 case M_LWRE_OB:
8449 s = "lwre";
8450 fmt = "t,+j(b)";
8451 offbits = 9;
8452 goto ld_st;
8453 case M_SBE_AB:
8454 ab = 1;
8455 case M_SBE_OB:
8456 s = "sbe";
8457 fmt = "t,+j(b)";
8458 offbits = 9;
8459 goto ld_st;
8460 case M_SCE_AB:
8461 ab = 1;
8462 case M_SCE_OB:
8463 s = "sce";
8464 fmt = "t,+j(b)";
8465 offbits = 9;
8466 goto ld_st;
8467 case M_SHE_AB:
8468 ab = 1;
8469 case M_SHE_OB:
8470 s = "she";
8471 fmt = "t,+j(b)";
8472 offbits = 9;
8473 goto ld_st;
8474 case M_SWE_AB:
8475 ab = 1;
8476 case M_SWE_OB:
8477 s = "swe";
8478 fmt = "t,+j(b)";
8479 offbits = 9;
8480 goto ld_st;
8481 case M_SWLE_AB:
8482 ab = 1;
8483 case M_SWLE_OB:
8484 s = "swle";
8485 fmt = "t,+j(b)";
8486 offbits = 9;
8487 goto ld_st;
8488 case M_SWRE_AB:
8489 ab = 1;
8490 case M_SWRE_OB:
8491 s = "swre";
8492 fmt = "t,+j(b)";
8493 offbits = 9;
8494 goto ld_st;
8495 case M_ACLR_AB:
8496 ab = 1;
8497 case M_ACLR_OB:
8498 s = "aclr";
8499 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8500 fmt = "\\,~(b)";
8501 offbits = 12;
8502 goto ld_st;
8503 case M_ASET_AB:
8504 ab = 1;
8505 case M_ASET_OB:
8506 s = "aset";
8507 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8508 fmt = "\\,~(b)";
8509 offbits = 12;
8510 goto ld_st;
8511 case M_LB_AB:
8512 ab = 1;
8513 s = "lb";
8514 fmt = "t,o(b)";
8515 goto ld;
8516 case M_LBU_AB:
8517 ab = 1;
8518 s = "lbu";
8519 fmt = "t,o(b)";
8520 goto ld;
8521 case M_LH_AB:
8522 ab = 1;
8523 s = "lh";
8524 fmt = "t,o(b)";
8525 goto ld;
8526 case M_LHU_AB:
8527 ab = 1;
8528 s = "lhu";
8529 fmt = "t,o(b)";
8530 goto ld;
8531 case M_LW_AB:
8532 ab = 1;
8533 s = "lw";
8534 fmt = "t,o(b)";
8535 goto ld;
8536 case M_LWC0_AB:
8537 ab = 1;
8538 gas_assert (!mips_opts.micromips);
8539 s = "lwc0";
8540 fmt = "E,o(b)";
8541 /* Itbl support may require additional care here. */
8542 coproc = 1;
8543 goto ld_st;
8544 case M_LWC1_AB:
8545 ab = 1;
8546 s = "lwc1";
8547 fmt = "T,o(b)";
8548 /* Itbl support may require additional care here. */
8549 coproc = 1;
8550 goto ld_st;
8551 case M_LWC2_AB:
8552 ab = 1;
8553 case M_LWC2_OB:
8554 s = "lwc2";
8555 fmt = COP12_FMT;
8556 offbits = (mips_opts.micromips ? 12 : 16);
8557 /* Itbl support may require additional care here. */
8558 coproc = 1;
8559 goto ld_st;
8560 case M_LWC3_AB:
8561 ab = 1;
8562 gas_assert (!mips_opts.micromips);
8563 s = "lwc3";
8564 fmt = "E,o(b)";
8565 /* Itbl support may require additional care here. */
8566 coproc = 1;
8567 goto ld_st;
8568 case M_LWL_AB:
8569 ab = 1;
8570 case M_LWL_OB:
8571 s = "lwl";
8572 fmt = MEM12_FMT;
8573 offbits = (mips_opts.micromips ? 12 : 16);
8574 goto ld_st;
8575 case M_LWR_AB:
8576 ab = 1;
8577 case M_LWR_OB:
8578 s = "lwr";
8579 fmt = MEM12_FMT;
8580 offbits = (mips_opts.micromips ? 12 : 16);
8581 goto ld_st;
8582 case M_LDC1_AB:
8583 ab = 1;
8584 s = "ldc1";
8585 fmt = "T,o(b)";
8586 /* Itbl support may require additional care here. */
8587 coproc = 1;
8588 goto ld_st;
8589 case M_LDC2_AB:
8590 ab = 1;
8591 case M_LDC2_OB:
8592 s = "ldc2";
8593 fmt = COP12_FMT;
8594 offbits = (mips_opts.micromips ? 12 : 16);
8595 /* Itbl support may require additional care here. */
8596 coproc = 1;
8597 goto ld_st;
8598 case M_LQC2_AB:
8599 ab = 1;
8600 s = "lqc2";
8601 fmt = "E,o(b)";
8602 /* Itbl support may require additional care here. */
8603 coproc = 1;
8604 goto ld_st;
8605 case M_LDC3_AB:
8606 ab = 1;
8607 s = "ldc3";
8608 fmt = "E,o(b)";
8609 /* Itbl support may require additional care here. */
8610 coproc = 1;
8611 goto ld_st;
8612 case M_LDL_AB:
8613 ab = 1;
8614 case M_LDL_OB:
8615 s = "ldl";
8616 fmt = MEM12_FMT;
8617 offbits = (mips_opts.micromips ? 12 : 16);
8618 goto ld_st;
8619 case M_LDR_AB:
8620 ab = 1;
8621 case M_LDR_OB:
8622 s = "ldr";
8623 fmt = MEM12_FMT;
8624 offbits = (mips_opts.micromips ? 12 : 16);
8625 goto ld_st;
8626 case M_LL_AB:
8627 ab = 1;
8628 case M_LL_OB:
8629 s = "ll";
8630 fmt = MEM12_FMT;
8631 offbits = (mips_opts.micromips ? 12 : 16);
8632 goto ld;
8633 case M_LLD_AB:
8634 ab = 1;
8635 case M_LLD_OB:
8636 s = "lld";
8637 fmt = MEM12_FMT;
8638 offbits = (mips_opts.micromips ? 12 : 16);
8639 goto ld;
8640 case M_LWU_AB:
8641 ab = 1;
8642 case M_LWU_OB:
8643 s = "lwu";
8644 fmt = MEM12_FMT;
8645 offbits = (mips_opts.micromips ? 12 : 16);
8646 goto ld;
8647 case M_LWP_AB:
8648 ab = 1;
8649 case M_LWP_OB:
8650 gas_assert (mips_opts.micromips);
8651 s = "lwp";
8652 fmt = "t,~(b)";
8653 offbits = 12;
8654 lp = 1;
8655 goto ld;
8656 case M_LDP_AB:
8657 ab = 1;
8658 case M_LDP_OB:
8659 gas_assert (mips_opts.micromips);
8660 s = "ldp";
8661 fmt = "t,~(b)";
8662 offbits = 12;
8663 lp = 1;
8664 goto ld;
8665 case M_LWM_AB:
8666 ab = 1;
8667 case M_LWM_OB:
8668 gas_assert (mips_opts.micromips);
8669 s = "lwm";
8670 fmt = "n,~(b)";
8671 offbits = 12;
8672 goto ld_st;
8673 case M_LDM_AB:
8674 ab = 1;
8675 case M_LDM_OB:
8676 gas_assert (mips_opts.micromips);
8677 s = "ldm";
8678 fmt = "n,~(b)";
8679 offbits = 12;
8680 goto ld_st;
8681
8682 ld:
8683 /* We don't want to use $0 as tempreg. */
8684 if (breg == treg + lp || treg + lp == ZERO)
8685 goto ld_st;
8686 else
8687 tempreg = treg + lp;
8688 goto ld_noat;
8689
8690 case M_SB_AB:
8691 ab = 1;
8692 s = "sb";
8693 fmt = "t,o(b)";
8694 goto ld_st;
8695 case M_SH_AB:
8696 ab = 1;
8697 s = "sh";
8698 fmt = "t,o(b)";
8699 goto ld_st;
8700 case M_SW_AB:
8701 ab = 1;
8702 s = "sw";
8703 fmt = "t,o(b)";
8704 goto ld_st;
8705 case M_SWC0_AB:
8706 ab = 1;
8707 gas_assert (!mips_opts.micromips);
8708 s = "swc0";
8709 fmt = "E,o(b)";
8710 /* Itbl support may require additional care here. */
8711 coproc = 1;
8712 goto ld_st;
8713 case M_SWC1_AB:
8714 ab = 1;
8715 s = "swc1";
8716 fmt = "T,o(b)";
8717 /* Itbl support may require additional care here. */
8718 coproc = 1;
8719 goto ld_st;
8720 case M_SWC2_AB:
8721 ab = 1;
8722 case M_SWC2_OB:
8723 s = "swc2";
8724 fmt = COP12_FMT;
8725 offbits = (mips_opts.micromips ? 12 : 16);
8726 /* Itbl support may require additional care here. */
8727 coproc = 1;
8728 goto ld_st;
8729 case M_SWC3_AB:
8730 ab = 1;
8731 gas_assert (!mips_opts.micromips);
8732 s = "swc3";
8733 fmt = "E,o(b)";
8734 /* Itbl support may require additional care here. */
8735 coproc = 1;
8736 goto ld_st;
8737 case M_SWL_AB:
8738 ab = 1;
8739 case M_SWL_OB:
8740 s = "swl";
8741 fmt = MEM12_FMT;
8742 offbits = (mips_opts.micromips ? 12 : 16);
8743 goto ld_st;
8744 case M_SWR_AB:
8745 ab = 1;
8746 case M_SWR_OB:
8747 s = "swr";
8748 fmt = MEM12_FMT;
8749 offbits = (mips_opts.micromips ? 12 : 16);
8750 goto ld_st;
8751 case M_SC_AB:
8752 ab = 1;
8753 case M_SC_OB:
8754 s = "sc";
8755 fmt = MEM12_FMT;
8756 offbits = (mips_opts.micromips ? 12 : 16);
8757 goto ld_st;
8758 case M_SCD_AB:
8759 ab = 1;
8760 case M_SCD_OB:
8761 s = "scd";
8762 fmt = MEM12_FMT;
8763 offbits = (mips_opts.micromips ? 12 : 16);
8764 goto ld_st;
8765 case M_CACHE_AB:
8766 ab = 1;
8767 case M_CACHE_OB:
8768 s = "cache";
8769 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
8770 offbits = (mips_opts.micromips ? 12 : 16);
8771 goto ld_st;
8772 case M_CACHEE_AB:
8773 ab = 1;
8774 case M_CACHEE_OB:
8775 s = "cachee";
8776 fmt = "k,+j(b)";
8777 offbits = 9;
8778 goto ld_st;
8779 case M_PREF_AB:
8780 ab = 1;
8781 case M_PREF_OB:
8782 s = "pref";
8783 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
8784 offbits = (mips_opts.micromips ? 12 : 16);
8785 goto ld_st;
8786 case M_PREFE_AB:
8787 ab = 1;
8788 case M_PREFE_OB:
8789 s = "prefe";
8790 fmt = "k,+j(b)";
8791 offbits = 9;
8792 goto ld_st;
8793 case M_SDC1_AB:
8794 ab = 1;
8795 s = "sdc1";
8796 fmt = "T,o(b)";
8797 coproc = 1;
8798 /* Itbl support may require additional care here. */
8799 goto ld_st;
8800 case M_SDC2_AB:
8801 ab = 1;
8802 case M_SDC2_OB:
8803 s = "sdc2";
8804 fmt = COP12_FMT;
8805 offbits = (mips_opts.micromips ? 12 : 16);
8806 /* Itbl support may require additional care here. */
8807 coproc = 1;
8808 goto ld_st;
8809 case M_SQC2_AB:
8810 ab = 1;
8811 s = "sqc2";
8812 fmt = "E,o(b)";
8813 /* Itbl support may require additional care here. */
8814 coproc = 1;
8815 goto ld_st;
8816 case M_SDC3_AB:
8817 ab = 1;
8818 gas_assert (!mips_opts.micromips);
8819 s = "sdc3";
8820 fmt = "E,o(b)";
8821 /* Itbl support may require additional care here. */
8822 coproc = 1;
8823 goto ld_st;
8824 case M_SDL_AB:
8825 ab = 1;
8826 case M_SDL_OB:
8827 s = "sdl";
8828 fmt = MEM12_FMT;
8829 offbits = (mips_opts.micromips ? 12 : 16);
8830 goto ld_st;
8831 case M_SDR_AB:
8832 ab = 1;
8833 case M_SDR_OB:
8834 s = "sdr";
8835 fmt = MEM12_FMT;
8836 offbits = (mips_opts.micromips ? 12 : 16);
8837 goto ld_st;
8838 case M_SWP_AB:
8839 ab = 1;
8840 case M_SWP_OB:
8841 gas_assert (mips_opts.micromips);
8842 s = "swp";
8843 fmt = "t,~(b)";
8844 offbits = 12;
8845 goto ld_st;
8846 case M_SDP_AB:
8847 ab = 1;
8848 case M_SDP_OB:
8849 gas_assert (mips_opts.micromips);
8850 s = "sdp";
8851 fmt = "t,~(b)";
8852 offbits = 12;
8853 goto ld_st;
8854 case M_SWM_AB:
8855 ab = 1;
8856 case M_SWM_OB:
8857 gas_assert (mips_opts.micromips);
8858 s = "swm";
8859 fmt = "n,~(b)";
8860 offbits = 12;
8861 goto ld_st;
8862 case M_SDM_AB:
8863 ab = 1;
8864 case M_SDM_OB:
8865 gas_assert (mips_opts.micromips);
8866 s = "sdm";
8867 fmt = "n,~(b)";
8868 offbits = 12;
8869
8870 ld_st:
8871 tempreg = AT;
8872 used_at = 1;
8873 ld_noat:
8874 if (offset_expr.X_op != O_constant
8875 && offset_expr.X_op != O_symbol)
8876 {
8877 as_bad (_("Expression too complex"));
8878 offset_expr.X_op = O_constant;
8879 }
8880
8881 if (HAVE_32BIT_ADDRESSES
8882 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
8883 {
8884 char value [32];
8885
8886 sprintf_vma (value, offset_expr.X_add_number);
8887 as_bad (_("Number (0x%s) larger than 32 bits"), value);
8888 }
8889
8890 /* A constant expression in PIC code can be handled just as it
8891 is in non PIC code. */
8892 if (offset_expr.X_op == O_constant)
8893 {
8894 int hipart = 0;
8895
8896 expr1.X_add_number = offset_expr.X_add_number;
8897 normalize_address_expr (&expr1);
8898 if ((offbits == 0 || offbits == 16)
8899 && !IS_SEXT_16BIT_NUM (expr1.X_add_number))
8900 {
8901 expr1.X_add_number = ((expr1.X_add_number + 0x8000)
8902 & ~(bfd_vma) 0xffff);
8903 hipart = 1;
8904 }
8905 else if (offbits == 12 && !IS_SEXT_12BIT_NUM (expr1.X_add_number))
8906 {
8907 expr1.X_add_number = ((expr1.X_add_number + 0x800)
8908 & ~(bfd_vma) 0xfff);
8909 hipart = 1;
8910 }
8911 else if (offbits == 9 && !IS_SEXT_9BIT_NUM (expr1.X_add_number))
8912 {
8913 expr1.X_add_number = ((expr1.X_add_number + 0x100)
8914 & ~(bfd_vma) 0x1ff);
8915 hipart = 1;
8916 }
8917 if (hipart)
8918 {
8919 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8920 if (breg != 0)
8921 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8922 tempreg, tempreg, breg);
8923 breg = tempreg;
8924 }
8925 if (offbits == 0)
8926 {
8927 if (offset_expr.X_add_number == 0)
8928 tempreg = breg;
8929 else
8930 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8931 "t,r,j", tempreg, breg, BFD_RELOC_LO16);
8932 macro_build (NULL, s, fmt, treg, tempreg);
8933 }
8934 else if (offbits == 16)
8935 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, breg);
8936 else
8937 macro_build (NULL, s, fmt,
8938 treg, (unsigned long) offset_expr.X_add_number, breg);
8939 }
8940 else if (offbits != 16)
8941 {
8942 /* The offset field is too narrow to be used for a low-part
8943 relocation, so load the whole address into the auxillary
8944 register. In the case of "A(b)" addresses, we first load
8945 absolute address "A" into the register and then add base
8946 register "b". In the case of "o(b)" addresses, we simply
8947 need to add 16-bit offset "o" to base register "b", and
8948 offset_reloc already contains the relocations associated
8949 with "o". */
8950 if (ab)
8951 {
8952 load_address (tempreg, &offset_expr, &used_at);
8953 if (breg != 0)
8954 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8955 tempreg, tempreg, breg);
8956 }
8957 else
8958 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8959 tempreg, breg, -1,
8960 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8961 expr1.X_add_number = 0;
8962 if (offbits == 0)
8963 macro_build (NULL, s, fmt, treg, tempreg);
8964 else
8965 macro_build (NULL, s, fmt,
8966 treg, (unsigned long) expr1.X_add_number, tempreg);
8967 }
8968 else if (mips_pic == NO_PIC)
8969 {
8970 /* If this is a reference to a GP relative symbol, and there
8971 is no base register, we want
8972 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
8973 Otherwise, if there is no base register, we want
8974 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8975 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8976 If we have a constant, we need two instructions anyhow,
8977 so we always use the latter form.
8978
8979 If we have a base register, and this is a reference to a
8980 GP relative symbol, we want
8981 addu $tempreg,$breg,$gp
8982 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
8983 Otherwise we want
8984 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8985 addu $tempreg,$tempreg,$breg
8986 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8987 With a constant we always use the latter case.
8988
8989 With 64bit address space and no base register and $at usable,
8990 we want
8991 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8992 lui $at,<sym> (BFD_RELOC_HI16_S)
8993 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8994 dsll32 $tempreg,0
8995 daddu $tempreg,$at
8996 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8997 If we have a base register, we want
8998 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8999 lui $at,<sym> (BFD_RELOC_HI16_S)
9000 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9001 daddu $at,$breg
9002 dsll32 $tempreg,0
9003 daddu $tempreg,$at
9004 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
9005
9006 Without $at we can't generate the optimal path for superscalar
9007 processors here since this would require two temporary registers.
9008 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9009 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9010 dsll $tempreg,16
9011 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
9012 dsll $tempreg,16
9013 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
9014 If we have a base register, we want
9015 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9016 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9017 dsll $tempreg,16
9018 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
9019 dsll $tempreg,16
9020 daddu $tempreg,$tempreg,$breg
9021 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
9022
9023 For GP relative symbols in 64bit address space we can use
9024 the same sequence as in 32bit address space. */
9025 if (HAVE_64BIT_SYMBOLS)
9026 {
9027 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9028 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9029 {
9030 relax_start (offset_expr.X_add_symbol);
9031 if (breg == 0)
9032 {
9033 macro_build (&offset_expr, s, fmt, treg,
9034 BFD_RELOC_GPREL16, mips_gp_register);
9035 }
9036 else
9037 {
9038 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9039 tempreg, breg, mips_gp_register);
9040 macro_build (&offset_expr, s, fmt, treg,
9041 BFD_RELOC_GPREL16, tempreg);
9042 }
9043 relax_switch ();
9044 }
9045
9046 if (used_at == 0 && mips_opts.at)
9047 {
9048 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
9049 BFD_RELOC_MIPS_HIGHEST);
9050 macro_build (&offset_expr, "lui", LUI_FMT, AT,
9051 BFD_RELOC_HI16_S);
9052 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
9053 tempreg, BFD_RELOC_MIPS_HIGHER);
9054 if (breg != 0)
9055 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
9056 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
9057 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
9058 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
9059 tempreg);
9060 used_at = 1;
9061 }
9062 else
9063 {
9064 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
9065 BFD_RELOC_MIPS_HIGHEST);
9066 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
9067 tempreg, BFD_RELOC_MIPS_HIGHER);
9068 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
9069 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
9070 tempreg, BFD_RELOC_HI16_S);
9071 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
9072 if (breg != 0)
9073 macro_build (NULL, "daddu", "d,v,t",
9074 tempreg, tempreg, breg);
9075 macro_build (&offset_expr, s, fmt, treg,
9076 BFD_RELOC_LO16, tempreg);
9077 }
9078
9079 if (mips_relax.sequence)
9080 relax_end ();
9081 break;
9082 }
9083
9084 if (breg == 0)
9085 {
9086 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9087 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9088 {
9089 relax_start (offset_expr.X_add_symbol);
9090 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
9091 mips_gp_register);
9092 relax_switch ();
9093 }
9094 macro_build_lui (&offset_expr, tempreg);
9095 macro_build (&offset_expr, s, fmt, treg,
9096 BFD_RELOC_LO16, tempreg);
9097 if (mips_relax.sequence)
9098 relax_end ();
9099 }
9100 else
9101 {
9102 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9103 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9104 {
9105 relax_start (offset_expr.X_add_symbol);
9106 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9107 tempreg, breg, mips_gp_register);
9108 macro_build (&offset_expr, s, fmt, treg,
9109 BFD_RELOC_GPREL16, tempreg);
9110 relax_switch ();
9111 }
9112 macro_build_lui (&offset_expr, tempreg);
9113 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9114 tempreg, tempreg, breg);
9115 macro_build (&offset_expr, s, fmt, treg,
9116 BFD_RELOC_LO16, tempreg);
9117 if (mips_relax.sequence)
9118 relax_end ();
9119 }
9120 }
9121 else if (!mips_big_got)
9122 {
9123 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
9124
9125 /* If this is a reference to an external symbol, we want
9126 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9127 nop
9128 <op> $treg,0($tempreg)
9129 Otherwise we want
9130 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9131 nop
9132 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9133 <op> $treg,0($tempreg)
9134
9135 For NewABI, we want
9136 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9137 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
9138
9139 If there is a base register, we add it to $tempreg before
9140 the <op>. If there is a constant, we stick it in the
9141 <op> instruction. We don't handle constants larger than
9142 16 bits, because we have no way to load the upper 16 bits
9143 (actually, we could handle them for the subset of cases
9144 in which we are not using $at). */
9145 gas_assert (offset_expr.X_op == O_symbol);
9146 if (HAVE_NEWABI)
9147 {
9148 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9149 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9150 if (breg != 0)
9151 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9152 tempreg, tempreg, breg);
9153 macro_build (&offset_expr, s, fmt, treg,
9154 BFD_RELOC_MIPS_GOT_OFST, tempreg);
9155 break;
9156 }
9157 expr1.X_add_number = offset_expr.X_add_number;
9158 offset_expr.X_add_number = 0;
9159 if (expr1.X_add_number < -0x8000
9160 || expr1.X_add_number >= 0x8000)
9161 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9162 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9163 lw_reloc_type, mips_gp_register);
9164 load_delay_nop ();
9165 relax_start (offset_expr.X_add_symbol);
9166 relax_switch ();
9167 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
9168 tempreg, BFD_RELOC_LO16);
9169 relax_end ();
9170 if (breg != 0)
9171 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9172 tempreg, tempreg, breg);
9173 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
9174 }
9175 else if (mips_big_got && !HAVE_NEWABI)
9176 {
9177 int gpdelay;
9178
9179 /* If this is a reference to an external symbol, we want
9180 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9181 addu $tempreg,$tempreg,$gp
9182 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9183 <op> $treg,0($tempreg)
9184 Otherwise we want
9185 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9186 nop
9187 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9188 <op> $treg,0($tempreg)
9189 If there is a base register, we add it to $tempreg before
9190 the <op>. If there is a constant, we stick it in the
9191 <op> instruction. We don't handle constants larger than
9192 16 bits, because we have no way to load the upper 16 bits
9193 (actually, we could handle them for the subset of cases
9194 in which we are not using $at). */
9195 gas_assert (offset_expr.X_op == O_symbol);
9196 expr1.X_add_number = offset_expr.X_add_number;
9197 offset_expr.X_add_number = 0;
9198 if (expr1.X_add_number < -0x8000
9199 || expr1.X_add_number >= 0x8000)
9200 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9201 gpdelay = reg_needs_delay (mips_gp_register);
9202 relax_start (offset_expr.X_add_symbol);
9203 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
9204 BFD_RELOC_MIPS_GOT_HI16);
9205 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
9206 mips_gp_register);
9207 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9208 BFD_RELOC_MIPS_GOT_LO16, tempreg);
9209 relax_switch ();
9210 if (gpdelay)
9211 macro_build (NULL, "nop", "");
9212 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9213 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9214 load_delay_nop ();
9215 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
9216 tempreg, BFD_RELOC_LO16);
9217 relax_end ();
9218
9219 if (breg != 0)
9220 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9221 tempreg, tempreg, breg);
9222 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
9223 }
9224 else if (mips_big_got && HAVE_NEWABI)
9225 {
9226 /* If this is a reference to an external symbol, we want
9227 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9228 add $tempreg,$tempreg,$gp
9229 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9230 <op> $treg,<ofst>($tempreg)
9231 Otherwise, for local symbols, we want:
9232 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9233 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9234 gas_assert (offset_expr.X_op == O_symbol);
9235 expr1.X_add_number = offset_expr.X_add_number;
9236 offset_expr.X_add_number = 0;
9237 if (expr1.X_add_number < -0x8000
9238 || expr1.X_add_number >= 0x8000)
9239 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9240 relax_start (offset_expr.X_add_symbol);
9241 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
9242 BFD_RELOC_MIPS_GOT_HI16);
9243 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
9244 mips_gp_register);
9245 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9246 BFD_RELOC_MIPS_GOT_LO16, tempreg);
9247 if (breg != 0)
9248 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9249 tempreg, tempreg, breg);
9250 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
9251
9252 relax_switch ();
9253 offset_expr.X_add_number = expr1.X_add_number;
9254 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9255 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9256 if (breg != 0)
9257 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9258 tempreg, tempreg, breg);
9259 macro_build (&offset_expr, s, fmt, treg,
9260 BFD_RELOC_MIPS_GOT_OFST, tempreg);
9261 relax_end ();
9262 }
9263 else
9264 abort ();
9265
9266 break;
9267
9268 case M_JRADDIUSP:
9269 gas_assert (mips_opts.micromips);
9270 gas_assert (mips_opts.insn32);
9271 start_noreorder ();
9272 macro_build (NULL, "jr", "s", RA);
9273 expr1.X_add_number = EXTRACT_OPERAND (1, IMMP, *ip) << 2;
9274 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
9275 end_noreorder ();
9276 break;
9277
9278 case M_JRC:
9279 gas_assert (mips_opts.micromips);
9280 gas_assert (mips_opts.insn32);
9281 macro_build (NULL, "jr", "s", sreg);
9282 if (mips_opts.noreorder)
9283 macro_build (NULL, "nop", "");
9284 break;
9285
9286 case M_LI:
9287 case M_LI_S:
9288 load_register (treg, &imm_expr, 0);
9289 break;
9290
9291 case M_DLI:
9292 load_register (treg, &imm_expr, 1);
9293 break;
9294
9295 case M_LI_SS:
9296 if (imm_expr.X_op == O_constant)
9297 {
9298 used_at = 1;
9299 load_register (AT, &imm_expr, 0);
9300 macro_build (NULL, "mtc1", "t,G", AT, treg);
9301 break;
9302 }
9303 else
9304 {
9305 gas_assert (offset_expr.X_op == O_symbol
9306 && strcmp (segment_name (S_GET_SEGMENT
9307 (offset_expr.X_add_symbol)),
9308 ".lit4") == 0
9309 && offset_expr.X_add_number == 0);
9310 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
9311 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
9312 break;
9313 }
9314
9315 case M_LI_D:
9316 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
9317 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
9318 order 32 bits of the value and the low order 32 bits are either
9319 zero or in OFFSET_EXPR. */
9320 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
9321 {
9322 if (HAVE_64BIT_GPRS)
9323 load_register (treg, &imm_expr, 1);
9324 else
9325 {
9326 int hreg, lreg;
9327
9328 if (target_big_endian)
9329 {
9330 hreg = treg;
9331 lreg = treg + 1;
9332 }
9333 else
9334 {
9335 hreg = treg + 1;
9336 lreg = treg;
9337 }
9338
9339 if (hreg <= 31)
9340 load_register (hreg, &imm_expr, 0);
9341 if (lreg <= 31)
9342 {
9343 if (offset_expr.X_op == O_absent)
9344 move_register (lreg, 0);
9345 else
9346 {
9347 gas_assert (offset_expr.X_op == O_constant);
9348 load_register (lreg, &offset_expr, 0);
9349 }
9350 }
9351 }
9352 break;
9353 }
9354
9355 /* We know that sym is in the .rdata section. First we get the
9356 upper 16 bits of the address. */
9357 if (mips_pic == NO_PIC)
9358 {
9359 macro_build_lui (&offset_expr, AT);
9360 used_at = 1;
9361 }
9362 else
9363 {
9364 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9365 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9366 used_at = 1;
9367 }
9368
9369 /* Now we load the register(s). */
9370 if (HAVE_64BIT_GPRS)
9371 {
9372 used_at = 1;
9373 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
9374 }
9375 else
9376 {
9377 used_at = 1;
9378 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
9379 if (treg != RA)
9380 {
9381 /* FIXME: How in the world do we deal with the possible
9382 overflow here? */
9383 offset_expr.X_add_number += 4;
9384 macro_build (&offset_expr, "lw", "t,o(b)",
9385 treg + 1, BFD_RELOC_LO16, AT);
9386 }
9387 }
9388 break;
9389
9390 case M_LI_DD:
9391 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
9392 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
9393 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
9394 the value and the low order 32 bits are either zero or in
9395 OFFSET_EXPR. */
9396 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
9397 {
9398 used_at = 1;
9399 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
9400 if (HAVE_64BIT_FPRS)
9401 {
9402 gas_assert (HAVE_64BIT_GPRS);
9403 macro_build (NULL, "dmtc1", "t,S", AT, treg);
9404 }
9405 else
9406 {
9407 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
9408 if (offset_expr.X_op == O_absent)
9409 macro_build (NULL, "mtc1", "t,G", 0, treg);
9410 else
9411 {
9412 gas_assert (offset_expr.X_op == O_constant);
9413 load_register (AT, &offset_expr, 0);
9414 macro_build (NULL, "mtc1", "t,G", AT, treg);
9415 }
9416 }
9417 break;
9418 }
9419
9420 gas_assert (offset_expr.X_op == O_symbol
9421 && offset_expr.X_add_number == 0);
9422 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
9423 if (strcmp (s, ".lit8") == 0)
9424 {
9425 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
9426 {
9427 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
9428 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
9429 break;
9430 }
9431 breg = mips_gp_register;
9432 r = BFD_RELOC_MIPS_LITERAL;
9433 goto dob;
9434 }
9435 else
9436 {
9437 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
9438 used_at = 1;
9439 if (mips_pic != NO_PIC)
9440 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9441 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9442 else
9443 {
9444 /* FIXME: This won't work for a 64 bit address. */
9445 macro_build_lui (&offset_expr, AT);
9446 }
9447
9448 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
9449 {
9450 macro_build (&offset_expr, "ldc1", "T,o(b)",
9451 treg, BFD_RELOC_LO16, AT);
9452 break;
9453 }
9454 breg = AT;
9455 r = BFD_RELOC_LO16;
9456 goto dob;
9457 }
9458
9459 case M_L_DOB:
9460 /* Even on a big endian machine $fn comes before $fn+1. We have
9461 to adjust when loading from memory. */
9462 r = BFD_RELOC_LO16;
9463 dob:
9464 gas_assert (!mips_opts.micromips);
9465 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
9466 macro_build (&offset_expr, "lwc1", "T,o(b)",
9467 target_big_endian ? treg + 1 : treg, r, breg);
9468 /* FIXME: A possible overflow which I don't know how to deal
9469 with. */
9470 offset_expr.X_add_number += 4;
9471 macro_build (&offset_expr, "lwc1", "T,o(b)",
9472 target_big_endian ? treg : treg + 1, r, breg);
9473 break;
9474
9475 case M_S_DOB:
9476 gas_assert (!mips_opts.micromips);
9477 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
9478 /* Even on a big endian machine $fn comes before $fn+1. We have
9479 to adjust when storing to memory. */
9480 macro_build (&offset_expr, "swc1", "T,o(b)",
9481 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
9482 offset_expr.X_add_number += 4;
9483 macro_build (&offset_expr, "swc1", "T,o(b)",
9484 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
9485 break;
9486
9487 case M_L_DAB:
9488 gas_assert (!mips_opts.micromips);
9489 /*
9490 * The MIPS assembler seems to check for X_add_number not
9491 * being double aligned and generating:
9492 * lui at,%hi(foo+1)
9493 * addu at,at,v1
9494 * addiu at,at,%lo(foo+1)
9495 * lwc1 f2,0(at)
9496 * lwc1 f3,4(at)
9497 * But, the resulting address is the same after relocation so why
9498 * generate the extra instruction?
9499 */
9500 /* Itbl support may require additional care here. */
9501 coproc = 1;
9502 fmt = "T,o(b)";
9503 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
9504 {
9505 s = "ldc1";
9506 goto ld_st;
9507 }
9508 s = "lwc1";
9509 goto ldd_std;
9510
9511 case M_S_DAB:
9512 gas_assert (!mips_opts.micromips);
9513 /* Itbl support may require additional care here. */
9514 coproc = 1;
9515 fmt = "T,o(b)";
9516 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
9517 {
9518 s = "sdc1";
9519 goto ld_st;
9520 }
9521 s = "swc1";
9522 goto ldd_std;
9523
9524 case M_LQ_AB:
9525 fmt = "t,o(b)";
9526 s = "lq";
9527 goto ld;
9528
9529 case M_SQ_AB:
9530 fmt = "t,o(b)";
9531 s = "sq";
9532 goto ld_st;
9533
9534 case M_LD_AB:
9535 fmt = "t,o(b)";
9536 if (HAVE_64BIT_GPRS)
9537 {
9538 s = "ld";
9539 goto ld;
9540 }
9541 s = "lw";
9542 goto ldd_std;
9543
9544 case M_SD_AB:
9545 fmt = "t,o(b)";
9546 if (HAVE_64BIT_GPRS)
9547 {
9548 s = "sd";
9549 goto ld_st;
9550 }
9551 s = "sw";
9552
9553 ldd_std:
9554 if (offset_expr.X_op != O_symbol
9555 && offset_expr.X_op != O_constant)
9556 {
9557 as_bad (_("Expression too complex"));
9558 offset_expr.X_op = O_constant;
9559 }
9560
9561 if (HAVE_32BIT_ADDRESSES
9562 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
9563 {
9564 char value [32];
9565
9566 sprintf_vma (value, offset_expr.X_add_number);
9567 as_bad (_("Number (0x%s) larger than 32 bits"), value);
9568 }
9569
9570 /* Even on a big endian machine $fn comes before $fn+1. We have
9571 to adjust when loading from memory. We set coproc if we must
9572 load $fn+1 first. */
9573 /* Itbl support may require additional care here. */
9574 if (!target_big_endian)
9575 coproc = 0;
9576
9577 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
9578 {
9579 /* If this is a reference to a GP relative symbol, we want
9580 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
9581 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
9582 If we have a base register, we use this
9583 addu $at,$breg,$gp
9584 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
9585 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
9586 If this is not a GP relative symbol, we want
9587 lui $at,<sym> (BFD_RELOC_HI16_S)
9588 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9589 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9590 If there is a base register, we add it to $at after the
9591 lui instruction. If there is a constant, we always use
9592 the last case. */
9593 if (offset_expr.X_op == O_symbol
9594 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9595 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9596 {
9597 relax_start (offset_expr.X_add_symbol);
9598 if (breg == 0)
9599 {
9600 tempreg = mips_gp_register;
9601 }
9602 else
9603 {
9604 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9605 AT, breg, mips_gp_register);
9606 tempreg = AT;
9607 used_at = 1;
9608 }
9609
9610 /* Itbl support may require additional care here. */
9611 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9612 BFD_RELOC_GPREL16, tempreg);
9613 offset_expr.X_add_number += 4;
9614
9615 /* Set mips_optimize to 2 to avoid inserting an
9616 undesired nop. */
9617 hold_mips_optimize = mips_optimize;
9618 mips_optimize = 2;
9619 /* Itbl support may require additional care here. */
9620 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9621 BFD_RELOC_GPREL16, tempreg);
9622 mips_optimize = hold_mips_optimize;
9623
9624 relax_switch ();
9625
9626 offset_expr.X_add_number -= 4;
9627 }
9628 used_at = 1;
9629 macro_build_lui (&offset_expr, AT);
9630 if (breg != 0)
9631 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9632 /* Itbl support may require additional care here. */
9633 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9634 BFD_RELOC_LO16, AT);
9635 /* FIXME: How do we handle overflow here? */
9636 offset_expr.X_add_number += 4;
9637 /* Itbl support may require additional care here. */
9638 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9639 BFD_RELOC_LO16, AT);
9640 if (mips_relax.sequence)
9641 relax_end ();
9642 }
9643 else if (!mips_big_got)
9644 {
9645 /* If this is a reference to an external symbol, we want
9646 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9647 nop
9648 <op> $treg,0($at)
9649 <op> $treg+1,4($at)
9650 Otherwise we want
9651 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9652 nop
9653 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9654 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9655 If there is a base register we add it to $at before the
9656 lwc1 instructions. If there is a constant we include it
9657 in the lwc1 instructions. */
9658 used_at = 1;
9659 expr1.X_add_number = offset_expr.X_add_number;
9660 if (expr1.X_add_number < -0x8000
9661 || expr1.X_add_number >= 0x8000 - 4)
9662 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9663 load_got_offset (AT, &offset_expr);
9664 load_delay_nop ();
9665 if (breg != 0)
9666 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9667
9668 /* Set mips_optimize to 2 to avoid inserting an undesired
9669 nop. */
9670 hold_mips_optimize = mips_optimize;
9671 mips_optimize = 2;
9672
9673 /* Itbl support may require additional care here. */
9674 relax_start (offset_expr.X_add_symbol);
9675 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9676 BFD_RELOC_LO16, AT);
9677 expr1.X_add_number += 4;
9678 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9679 BFD_RELOC_LO16, AT);
9680 relax_switch ();
9681 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9682 BFD_RELOC_LO16, AT);
9683 offset_expr.X_add_number += 4;
9684 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9685 BFD_RELOC_LO16, AT);
9686 relax_end ();
9687
9688 mips_optimize = hold_mips_optimize;
9689 }
9690 else if (mips_big_got)
9691 {
9692 int gpdelay;
9693
9694 /* If this is a reference to an external symbol, we want
9695 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9696 addu $at,$at,$gp
9697 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
9698 nop
9699 <op> $treg,0($at)
9700 <op> $treg+1,4($at)
9701 Otherwise we want
9702 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9703 nop
9704 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9705 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9706 If there is a base register we add it to $at before the
9707 lwc1 instructions. If there is a constant we include it
9708 in the lwc1 instructions. */
9709 used_at = 1;
9710 expr1.X_add_number = offset_expr.X_add_number;
9711 offset_expr.X_add_number = 0;
9712 if (expr1.X_add_number < -0x8000
9713 || expr1.X_add_number >= 0x8000 - 4)
9714 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9715 gpdelay = reg_needs_delay (mips_gp_register);
9716 relax_start (offset_expr.X_add_symbol);
9717 macro_build (&offset_expr, "lui", LUI_FMT,
9718 AT, BFD_RELOC_MIPS_GOT_HI16);
9719 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9720 AT, AT, mips_gp_register);
9721 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9722 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
9723 load_delay_nop ();
9724 if (breg != 0)
9725 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9726 /* Itbl support may require additional care here. */
9727 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9728 BFD_RELOC_LO16, AT);
9729 expr1.X_add_number += 4;
9730
9731 /* Set mips_optimize to 2 to avoid inserting an undesired
9732 nop. */
9733 hold_mips_optimize = mips_optimize;
9734 mips_optimize = 2;
9735 /* Itbl support may require additional care here. */
9736 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9737 BFD_RELOC_LO16, AT);
9738 mips_optimize = hold_mips_optimize;
9739 expr1.X_add_number -= 4;
9740
9741 relax_switch ();
9742 offset_expr.X_add_number = expr1.X_add_number;
9743 if (gpdelay)
9744 macro_build (NULL, "nop", "");
9745 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9746 BFD_RELOC_MIPS_GOT16, mips_gp_register);
9747 load_delay_nop ();
9748 if (breg != 0)
9749 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9750 /* Itbl support may require additional care here. */
9751 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9752 BFD_RELOC_LO16, AT);
9753 offset_expr.X_add_number += 4;
9754
9755 /* Set mips_optimize to 2 to avoid inserting an undesired
9756 nop. */
9757 hold_mips_optimize = mips_optimize;
9758 mips_optimize = 2;
9759 /* Itbl support may require additional care here. */
9760 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9761 BFD_RELOC_LO16, AT);
9762 mips_optimize = hold_mips_optimize;
9763 relax_end ();
9764 }
9765 else
9766 abort ();
9767
9768 break;
9769
9770 case M_LD_OB:
9771 s = HAVE_64BIT_GPRS ? "ld" : "lw";
9772 goto sd_ob;
9773 case M_SD_OB:
9774 s = HAVE_64BIT_GPRS ? "sd" : "sw";
9775 sd_ob:
9776 macro_build (&offset_expr, s, "t,o(b)", treg,
9777 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9778 breg);
9779 if (!HAVE_64BIT_GPRS)
9780 {
9781 offset_expr.X_add_number += 4;
9782 macro_build (&offset_expr, s, "t,o(b)", treg + 1,
9783 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9784 breg);
9785 }
9786 break;
9787
9788
9789 case M_SAA_AB:
9790 ab = 1;
9791 case M_SAA_OB:
9792 s = "saa";
9793 offbits = 0;
9794 fmt = "t,(b)";
9795 goto ld_st;
9796 case M_SAAD_AB:
9797 ab = 1;
9798 case M_SAAD_OB:
9799 s = "saad";
9800 offbits = 0;
9801 fmt = "t,(b)";
9802 goto ld_st;
9803
9804 /* New code added to support COPZ instructions.
9805 This code builds table entries out of the macros in mip_opcodes.
9806 R4000 uses interlocks to handle coproc delays.
9807 Other chips (like the R3000) require nops to be inserted for delays.
9808
9809 FIXME: Currently, we require that the user handle delays.
9810 In order to fill delay slots for non-interlocked chips,
9811 we must have a way to specify delays based on the coprocessor.
9812 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9813 What are the side-effects of the cop instruction?
9814 What cache support might we have and what are its effects?
9815 Both coprocessor & memory require delays. how long???
9816 What registers are read/set/modified?
9817
9818 If an itbl is provided to interpret cop instructions,
9819 this knowledge can be encoded in the itbl spec. */
9820
9821 case M_COP0:
9822 s = "c0";
9823 goto copz;
9824 case M_COP1:
9825 s = "c1";
9826 goto copz;
9827 case M_COP2:
9828 s = "c2";
9829 goto copz;
9830 case M_COP3:
9831 s = "c3";
9832 copz:
9833 gas_assert (!mips_opts.micromips);
9834 /* For now we just do C (same as Cz). The parameter will be
9835 stored in insn_opcode by mips_ip. */
9836 macro_build (NULL, s, "C", ip->insn_opcode);
9837 break;
9838
9839 case M_MOVE:
9840 move_register (dreg, sreg);
9841 break;
9842
9843 case M_MOVEP:
9844 gas_assert (mips_opts.micromips);
9845 gas_assert (mips_opts.insn32);
9846 dreg = micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
9847 breg = micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
9848 sreg = micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
9849 treg = micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
9850 move_register (dreg, sreg);
9851 move_register (breg, treg);
9852 break;
9853
9854 case M_DMUL:
9855 dbl = 1;
9856 case M_MUL:
9857 if (mips_opts.arch == CPU_R5900)
9858 {
9859 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", dreg, sreg, treg);
9860 }
9861 else
9862 {
9863 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
9864 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9865 }
9866 break;
9867
9868 case M_DMUL_I:
9869 dbl = 1;
9870 case M_MUL_I:
9871 /* The MIPS assembler some times generates shifts and adds. I'm
9872 not trying to be that fancy. GCC should do this for us
9873 anyway. */
9874 used_at = 1;
9875 load_register (AT, &imm_expr, dbl);
9876 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
9877 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9878 break;
9879
9880 case M_DMULO_I:
9881 dbl = 1;
9882 case M_MULO_I:
9883 imm = 1;
9884 goto do_mulo;
9885
9886 case M_DMULO:
9887 dbl = 1;
9888 case M_MULO:
9889 do_mulo:
9890 start_noreorder ();
9891 used_at = 1;
9892 if (imm)
9893 load_register (AT, &imm_expr, dbl);
9894 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
9895 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9896 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9897 macro_build (NULL, "mfhi", MFHL_FMT, AT);
9898 if (mips_trap)
9899 macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
9900 else
9901 {
9902 if (mips_opts.micromips)
9903 micromips_label_expr (&label_expr);
9904 else
9905 label_expr.X_add_number = 8;
9906 macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
9907 macro_build (NULL, "nop", "");
9908 macro_build (NULL, "break", BRK_FMT, 6);
9909 if (mips_opts.micromips)
9910 micromips_add_label ();
9911 }
9912 end_noreorder ();
9913 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9914 break;
9915
9916 case M_DMULOU_I:
9917 dbl = 1;
9918 case M_MULOU_I:
9919 imm = 1;
9920 goto do_mulou;
9921
9922 case M_DMULOU:
9923 dbl = 1;
9924 case M_MULOU:
9925 do_mulou:
9926 start_noreorder ();
9927 used_at = 1;
9928 if (imm)
9929 load_register (AT, &imm_expr, dbl);
9930 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
9931 sreg, imm ? AT : treg);
9932 macro_build (NULL, "mfhi", MFHL_FMT, AT);
9933 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9934 if (mips_trap)
9935 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
9936 else
9937 {
9938 if (mips_opts.micromips)
9939 micromips_label_expr (&label_expr);
9940 else
9941 label_expr.X_add_number = 8;
9942 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
9943 macro_build (NULL, "nop", "");
9944 macro_build (NULL, "break", BRK_FMT, 6);
9945 if (mips_opts.micromips)
9946 micromips_add_label ();
9947 }
9948 end_noreorder ();
9949 break;
9950
9951 case M_DROL:
9952 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9953 {
9954 if (dreg == sreg)
9955 {
9956 tempreg = AT;
9957 used_at = 1;
9958 }
9959 else
9960 {
9961 tempreg = dreg;
9962 }
9963 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9964 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
9965 break;
9966 }
9967 used_at = 1;
9968 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9969 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9970 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9971 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9972 break;
9973
9974 case M_ROL:
9975 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9976 {
9977 if (dreg == sreg)
9978 {
9979 tempreg = AT;
9980 used_at = 1;
9981 }
9982 else
9983 {
9984 tempreg = dreg;
9985 }
9986 macro_build (NULL, "negu", "d,w", tempreg, treg);
9987 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
9988 break;
9989 }
9990 used_at = 1;
9991 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9992 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9993 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9994 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9995 break;
9996
9997 case M_DROL_I:
9998 {
9999 unsigned int rot;
10000 char *l;
10001 char *rr;
10002
10003 if (imm_expr.X_op != O_constant)
10004 as_bad (_("Improper rotate count"));
10005 rot = imm_expr.X_add_number & 0x3f;
10006 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
10007 {
10008 rot = (64 - rot) & 0x3f;
10009 if (rot >= 32)
10010 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
10011 else
10012 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
10013 break;
10014 }
10015 if (rot == 0)
10016 {
10017 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
10018 break;
10019 }
10020 l = (rot < 0x20) ? "dsll" : "dsll32";
10021 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
10022 rot &= 0x1f;
10023 used_at = 1;
10024 macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
10025 macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
10026 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
10027 }
10028 break;
10029
10030 case M_ROL_I:
10031 {
10032 unsigned int rot;
10033
10034 if (imm_expr.X_op != O_constant)
10035 as_bad (_("Improper rotate count"));
10036 rot = imm_expr.X_add_number & 0x1f;
10037 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
10038 {
10039 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
10040 break;
10041 }
10042 if (rot == 0)
10043 {
10044 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
10045 break;
10046 }
10047 used_at = 1;
10048 macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
10049 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
10050 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
10051 }
10052 break;
10053
10054 case M_DROR:
10055 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
10056 {
10057 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
10058 break;
10059 }
10060 used_at = 1;
10061 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
10062 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
10063 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
10064 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
10065 break;
10066
10067 case M_ROR:
10068 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
10069 {
10070 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
10071 break;
10072 }
10073 used_at = 1;
10074 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
10075 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
10076 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
10077 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
10078 break;
10079
10080 case M_DROR_I:
10081 {
10082 unsigned int rot;
10083 char *l;
10084 char *rr;
10085
10086 if (imm_expr.X_op != O_constant)
10087 as_bad (_("Improper rotate count"));
10088 rot = imm_expr.X_add_number & 0x3f;
10089 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
10090 {
10091 if (rot >= 32)
10092 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
10093 else
10094 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
10095 break;
10096 }
10097 if (rot == 0)
10098 {
10099 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
10100 break;
10101 }
10102 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
10103 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
10104 rot &= 0x1f;
10105 used_at = 1;
10106 macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
10107 macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
10108 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
10109 }
10110 break;
10111
10112 case M_ROR_I:
10113 {
10114 unsigned int rot;
10115
10116 if (imm_expr.X_op != O_constant)
10117 as_bad (_("Improper rotate count"));
10118 rot = imm_expr.X_add_number & 0x1f;
10119 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
10120 {
10121 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
10122 break;
10123 }
10124 if (rot == 0)
10125 {
10126 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
10127 break;
10128 }
10129 used_at = 1;
10130 macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
10131 macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
10132 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
10133 }
10134 break;
10135
10136 case M_SEQ:
10137 if (sreg == 0)
10138 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
10139 else if (treg == 0)
10140 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
10141 else
10142 {
10143 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
10144 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
10145 }
10146 break;
10147
10148 case M_SEQ_I:
10149 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
10150 {
10151 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
10152 break;
10153 }
10154 if (sreg == 0)
10155 {
10156 as_warn (_("Instruction %s: result is always false"),
10157 ip->insn_mo->name);
10158 move_register (dreg, 0);
10159 break;
10160 }
10161 if (CPU_HAS_SEQ (mips_opts.arch)
10162 && -512 <= imm_expr.X_add_number
10163 && imm_expr.X_add_number < 512)
10164 {
10165 macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
10166 (int) imm_expr.X_add_number);
10167 break;
10168 }
10169 if (imm_expr.X_op == O_constant
10170 && imm_expr.X_add_number >= 0
10171 && imm_expr.X_add_number < 0x10000)
10172 {
10173 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
10174 }
10175 else if (imm_expr.X_op == O_constant
10176 && imm_expr.X_add_number > -0x8000
10177 && imm_expr.X_add_number < 0)
10178 {
10179 imm_expr.X_add_number = -imm_expr.X_add_number;
10180 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
10181 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
10182 }
10183 else if (CPU_HAS_SEQ (mips_opts.arch))
10184 {
10185 used_at = 1;
10186 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10187 macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
10188 break;
10189 }
10190 else
10191 {
10192 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10193 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
10194 used_at = 1;
10195 }
10196 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
10197 break;
10198
10199 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
10200 s = "slt";
10201 goto sge;
10202 case M_SGEU:
10203 s = "sltu";
10204 sge:
10205 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
10206 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
10207 break;
10208
10209 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
10210 case M_SGEU_I:
10211 if (imm_expr.X_op == O_constant
10212 && imm_expr.X_add_number >= -0x8000
10213 && imm_expr.X_add_number < 0x8000)
10214 {
10215 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
10216 dreg, sreg, BFD_RELOC_LO16);
10217 }
10218 else
10219 {
10220 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10221 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
10222 dreg, sreg, AT);
10223 used_at = 1;
10224 }
10225 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
10226 break;
10227
10228 case M_SGT: /* sreg > treg <==> treg < sreg */
10229 s = "slt";
10230 goto sgt;
10231 case M_SGTU:
10232 s = "sltu";
10233 sgt:
10234 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
10235 break;
10236
10237 case M_SGT_I: /* sreg > I <==> I < sreg */
10238 s = "slt";
10239 goto sgti;
10240 case M_SGTU_I:
10241 s = "sltu";
10242 sgti:
10243 used_at = 1;
10244 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10245 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
10246 break;
10247
10248 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
10249 s = "slt";
10250 goto sle;
10251 case M_SLEU:
10252 s = "sltu";
10253 sle:
10254 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
10255 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
10256 break;
10257
10258 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
10259 s = "slt";
10260 goto slei;
10261 case M_SLEU_I:
10262 s = "sltu";
10263 slei:
10264 used_at = 1;
10265 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10266 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
10267 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
10268 break;
10269
10270 case M_SLT_I:
10271 if (imm_expr.X_op == O_constant
10272 && imm_expr.X_add_number >= -0x8000
10273 && imm_expr.X_add_number < 0x8000)
10274 {
10275 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
10276 break;
10277 }
10278 used_at = 1;
10279 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10280 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
10281 break;
10282
10283 case M_SLTU_I:
10284 if (imm_expr.X_op == O_constant
10285 && imm_expr.X_add_number >= -0x8000
10286 && imm_expr.X_add_number < 0x8000)
10287 {
10288 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
10289 BFD_RELOC_LO16);
10290 break;
10291 }
10292 used_at = 1;
10293 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10294 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
10295 break;
10296
10297 case M_SNE:
10298 if (sreg == 0)
10299 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
10300 else if (treg == 0)
10301 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
10302 else
10303 {
10304 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
10305 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
10306 }
10307 break;
10308
10309 case M_SNE_I:
10310 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
10311 {
10312 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
10313 break;
10314 }
10315 if (sreg == 0)
10316 {
10317 as_warn (_("Instruction %s: result is always true"),
10318 ip->insn_mo->name);
10319 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
10320 dreg, 0, BFD_RELOC_LO16);
10321 break;
10322 }
10323 if (CPU_HAS_SEQ (mips_opts.arch)
10324 && -512 <= imm_expr.X_add_number
10325 && imm_expr.X_add_number < 512)
10326 {
10327 macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
10328 (int) imm_expr.X_add_number);
10329 break;
10330 }
10331 if (imm_expr.X_op == O_constant
10332 && imm_expr.X_add_number >= 0
10333 && imm_expr.X_add_number < 0x10000)
10334 {
10335 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
10336 }
10337 else if (imm_expr.X_op == O_constant
10338 && imm_expr.X_add_number > -0x8000
10339 && imm_expr.X_add_number < 0)
10340 {
10341 imm_expr.X_add_number = -imm_expr.X_add_number;
10342 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
10343 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
10344 }
10345 else if (CPU_HAS_SEQ (mips_opts.arch))
10346 {
10347 used_at = 1;
10348 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10349 macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
10350 break;
10351 }
10352 else
10353 {
10354 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10355 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
10356 used_at = 1;
10357 }
10358 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
10359 break;
10360
10361 case M_SUB_I:
10362 s = "addi";
10363 s2 = "sub";
10364 goto do_subi;
10365 case M_SUBU_I:
10366 s = "addiu";
10367 s2 = "subu";
10368 goto do_subi;
10369 case M_DSUB_I:
10370 dbl = 1;
10371 s = "daddi";
10372 s2 = "dsub";
10373 if (!mips_opts.micromips)
10374 goto do_subi;
10375 if (imm_expr.X_op == O_constant
10376 && imm_expr.X_add_number > -0x200
10377 && imm_expr.X_add_number <= 0x200)
10378 {
10379 macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
10380 break;
10381 }
10382 goto do_subi_i;
10383 case M_DSUBU_I:
10384 dbl = 1;
10385 s = "daddiu";
10386 s2 = "dsubu";
10387 do_subi:
10388 if (imm_expr.X_op == O_constant
10389 && imm_expr.X_add_number > -0x8000
10390 && imm_expr.X_add_number <= 0x8000)
10391 {
10392 imm_expr.X_add_number = -imm_expr.X_add_number;
10393 macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
10394 break;
10395 }
10396 do_subi_i:
10397 used_at = 1;
10398 load_register (AT, &imm_expr, dbl);
10399 macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
10400 break;
10401
10402 case M_TEQ_I:
10403 s = "teq";
10404 goto trap;
10405 case M_TGE_I:
10406 s = "tge";
10407 goto trap;
10408 case M_TGEU_I:
10409 s = "tgeu";
10410 goto trap;
10411 case M_TLT_I:
10412 s = "tlt";
10413 goto trap;
10414 case M_TLTU_I:
10415 s = "tltu";
10416 goto trap;
10417 case M_TNE_I:
10418 s = "tne";
10419 trap:
10420 used_at = 1;
10421 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10422 macro_build (NULL, s, "s,t", sreg, AT);
10423 break;
10424
10425 case M_TRUNCWS:
10426 case M_TRUNCWD:
10427 gas_assert (!mips_opts.micromips);
10428 gas_assert (mips_opts.isa == ISA_MIPS1);
10429 used_at = 1;
10430 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
10431 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
10432
10433 /*
10434 * Is the double cfc1 instruction a bug in the mips assembler;
10435 * or is there a reason for it?
10436 */
10437 start_noreorder ();
10438 macro_build (NULL, "cfc1", "t,G", treg, RA);
10439 macro_build (NULL, "cfc1", "t,G", treg, RA);
10440 macro_build (NULL, "nop", "");
10441 expr1.X_add_number = 3;
10442 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
10443 expr1.X_add_number = 2;
10444 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
10445 macro_build (NULL, "ctc1", "t,G", AT, RA);
10446 macro_build (NULL, "nop", "");
10447 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
10448 dreg, sreg);
10449 macro_build (NULL, "ctc1", "t,G", treg, RA);
10450 macro_build (NULL, "nop", "");
10451 end_noreorder ();
10452 break;
10453
10454 case M_ULH_A:
10455 ab = 1;
10456 case M_ULH:
10457 s = "lb";
10458 s2 = "lbu";
10459 off = 1;
10460 goto uld_st;
10461 case M_ULHU_A:
10462 ab = 1;
10463 case M_ULHU:
10464 s = "lbu";
10465 s2 = "lbu";
10466 off = 1;
10467 goto uld_st;
10468 case M_ULW_A:
10469 ab = 1;
10470 case M_ULW:
10471 s = "lwl";
10472 s2 = "lwr";
10473 offbits = (mips_opts.micromips ? 12 : 16);
10474 off = 3;
10475 goto uld_st;
10476 case M_ULD_A:
10477 ab = 1;
10478 case M_ULD:
10479 s = "ldl";
10480 s2 = "ldr";
10481 offbits = (mips_opts.micromips ? 12 : 16);
10482 off = 7;
10483 goto uld_st;
10484 case M_USH_A:
10485 ab = 1;
10486 case M_USH:
10487 s = "sb";
10488 s2 = "sb";
10489 off = 1;
10490 ust = 1;
10491 goto uld_st;
10492 case M_USW_A:
10493 ab = 1;
10494 case M_USW:
10495 s = "swl";
10496 s2 = "swr";
10497 offbits = (mips_opts.micromips ? 12 : 16);
10498 off = 3;
10499 ust = 1;
10500 goto uld_st;
10501 case M_USD_A:
10502 ab = 1;
10503 case M_USD:
10504 s = "sdl";
10505 s2 = "sdr";
10506 offbits = (mips_opts.micromips ? 12 : 16);
10507 off = 7;
10508 ust = 1;
10509
10510 uld_st:
10511 if (!ab && offset_expr.X_add_number >= 0x8000 - off)
10512 as_bad (_("Operand overflow"));
10513
10514 ep = &offset_expr;
10515 expr1.X_add_number = 0;
10516 if (ab)
10517 {
10518 used_at = 1;
10519 tempreg = AT;
10520 load_address (tempreg, ep, &used_at);
10521 if (breg != 0)
10522 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10523 tempreg, tempreg, breg);
10524 breg = tempreg;
10525 tempreg = treg;
10526 ep = &expr1;
10527 }
10528 else if (offbits == 12
10529 && (offset_expr.X_op != O_constant
10530 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
10531 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
10532 {
10533 used_at = 1;
10534 tempreg = AT;
10535 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
10536 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10537 breg = tempreg;
10538 tempreg = treg;
10539 ep = &expr1;
10540 }
10541 else if (!ust && treg == breg)
10542 {
10543 used_at = 1;
10544 tempreg = AT;
10545 }
10546 else
10547 tempreg = treg;
10548
10549 if (off == 1)
10550 goto ulh_sh;
10551
10552 if (!target_big_endian)
10553 ep->X_add_number += off;
10554 if (offbits != 12)
10555 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10556 else
10557 macro_build (NULL, s, "t,~(b)",
10558 tempreg, (unsigned long) ep->X_add_number, breg);
10559
10560 if (!target_big_endian)
10561 ep->X_add_number -= off;
10562 else
10563 ep->X_add_number += off;
10564 if (offbits != 12)
10565 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10566 else
10567 macro_build (NULL, s2, "t,~(b)",
10568 tempreg, (unsigned long) ep->X_add_number, breg);
10569
10570 /* If necessary, move the result in tempreg to the final destination. */
10571 if (!ust && treg != tempreg)
10572 {
10573 /* Protect second load's delay slot. */
10574 load_delay_nop ();
10575 move_register (treg, tempreg);
10576 }
10577 break;
10578
10579 ulh_sh:
10580 used_at = 1;
10581 if (target_big_endian == ust)
10582 ep->X_add_number += off;
10583 tempreg = ust || ab ? treg : AT;
10584 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10585
10586 /* For halfword transfers we need a temporary register to shuffle
10587 bytes. Unfortunately for M_USH_A we have none available before
10588 the next store as AT holds the base address. We deal with this
10589 case by clobbering TREG and then restoring it as with ULH. */
10590 tempreg = ust == ab ? treg : AT;
10591 if (ust)
10592 macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
10593
10594 if (target_big_endian == ust)
10595 ep->X_add_number -= off;
10596 else
10597 ep->X_add_number += off;
10598 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10599
10600 /* For M_USH_A re-retrieve the LSB. */
10601 if (ust && ab)
10602 {
10603 if (target_big_endian)
10604 ep->X_add_number += off;
10605 else
10606 ep->X_add_number -= off;
10607 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
10608 }
10609 /* For ULH and M_USH_A OR the LSB in. */
10610 if (!ust || ab)
10611 {
10612 tempreg = !ab ? AT : treg;
10613 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
10614 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
10615 }
10616 break;
10617
10618 default:
10619 /* FIXME: Check if this is one of the itbl macros, since they
10620 are added dynamically. */
10621 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
10622 break;
10623 }
10624 if (!mips_opts.at && used_at)
10625 as_bad (_("Macro used $at after \".set noat\""));
10626 }
10627
10628 /* Implement macros in mips16 mode. */
10629
10630 static void
10631 mips16_macro (struct mips_cl_insn *ip)
10632 {
10633 int mask;
10634 int xreg, yreg, zreg, tmp;
10635 expressionS expr1;
10636 int dbl;
10637 const char *s, *s2, *s3;
10638
10639 mask = ip->insn_mo->mask;
10640
10641 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10642 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10643 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
10644
10645 expr1.X_op = O_constant;
10646 expr1.X_op_symbol = NULL;
10647 expr1.X_add_symbol = NULL;
10648 expr1.X_add_number = 1;
10649
10650 dbl = 0;
10651
10652 switch (mask)
10653 {
10654 default:
10655 abort ();
10656
10657 case M_DDIV_3:
10658 dbl = 1;
10659 case M_DIV_3:
10660 s = "mflo";
10661 goto do_div3;
10662 case M_DREM_3:
10663 dbl = 1;
10664 case M_REM_3:
10665 s = "mfhi";
10666 do_div3:
10667 start_noreorder ();
10668 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
10669 expr1.X_add_number = 2;
10670 macro_build (&expr1, "bnez", "x,p", yreg);
10671 macro_build (NULL, "break", "6", 7);
10672
10673 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10674 since that causes an overflow. We should do that as well,
10675 but I don't see how to do the comparisons without a temporary
10676 register. */
10677 end_noreorder ();
10678 macro_build (NULL, s, "x", zreg);
10679 break;
10680
10681 case M_DIVU_3:
10682 s = "divu";
10683 s2 = "mflo";
10684 goto do_divu3;
10685 case M_REMU_3:
10686 s = "divu";
10687 s2 = "mfhi";
10688 goto do_divu3;
10689 case M_DDIVU_3:
10690 s = "ddivu";
10691 s2 = "mflo";
10692 goto do_divu3;
10693 case M_DREMU_3:
10694 s = "ddivu";
10695 s2 = "mfhi";
10696 do_divu3:
10697 start_noreorder ();
10698 macro_build (NULL, s, "0,x,y", xreg, yreg);
10699 expr1.X_add_number = 2;
10700 macro_build (&expr1, "bnez", "x,p", yreg);
10701 macro_build (NULL, "break", "6", 7);
10702 end_noreorder ();
10703 macro_build (NULL, s2, "x", zreg);
10704 break;
10705
10706 case M_DMUL:
10707 dbl = 1;
10708 case M_MUL:
10709 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10710 macro_build (NULL, "mflo", "x", zreg);
10711 break;
10712
10713 case M_DSUBU_I:
10714 dbl = 1;
10715 goto do_subu;
10716 case M_SUBU_I:
10717 do_subu:
10718 if (imm_expr.X_op != O_constant)
10719 as_bad (_("Unsupported large constant"));
10720 imm_expr.X_add_number = -imm_expr.X_add_number;
10721 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
10722 break;
10723
10724 case M_SUBU_I_2:
10725 if (imm_expr.X_op != O_constant)
10726 as_bad (_("Unsupported large constant"));
10727 imm_expr.X_add_number = -imm_expr.X_add_number;
10728 macro_build (&imm_expr, "addiu", "x,k", xreg);
10729 break;
10730
10731 case M_DSUBU_I_2:
10732 if (imm_expr.X_op != O_constant)
10733 as_bad (_("Unsupported large constant"));
10734 imm_expr.X_add_number = -imm_expr.X_add_number;
10735 macro_build (&imm_expr, "daddiu", "y,j", yreg);
10736 break;
10737
10738 case M_BEQ:
10739 s = "cmp";
10740 s2 = "bteqz";
10741 goto do_branch;
10742 case M_BNE:
10743 s = "cmp";
10744 s2 = "btnez";
10745 goto do_branch;
10746 case M_BLT:
10747 s = "slt";
10748 s2 = "btnez";
10749 goto do_branch;
10750 case M_BLTU:
10751 s = "sltu";
10752 s2 = "btnez";
10753 goto do_branch;
10754 case M_BLE:
10755 s = "slt";
10756 s2 = "bteqz";
10757 goto do_reverse_branch;
10758 case M_BLEU:
10759 s = "sltu";
10760 s2 = "bteqz";
10761 goto do_reverse_branch;
10762 case M_BGE:
10763 s = "slt";
10764 s2 = "bteqz";
10765 goto do_branch;
10766 case M_BGEU:
10767 s = "sltu";
10768 s2 = "bteqz";
10769 goto do_branch;
10770 case M_BGT:
10771 s = "slt";
10772 s2 = "btnez";
10773 goto do_reverse_branch;
10774 case M_BGTU:
10775 s = "sltu";
10776 s2 = "btnez";
10777
10778 do_reverse_branch:
10779 tmp = xreg;
10780 xreg = yreg;
10781 yreg = tmp;
10782
10783 do_branch:
10784 macro_build (NULL, s, "x,y", xreg, yreg);
10785 macro_build (&offset_expr, s2, "p");
10786 break;
10787
10788 case M_BEQ_I:
10789 s = "cmpi";
10790 s2 = "bteqz";
10791 s3 = "x,U";
10792 goto do_branch_i;
10793 case M_BNE_I:
10794 s = "cmpi";
10795 s2 = "btnez";
10796 s3 = "x,U";
10797 goto do_branch_i;
10798 case M_BLT_I:
10799 s = "slti";
10800 s2 = "btnez";
10801 s3 = "x,8";
10802 goto do_branch_i;
10803 case M_BLTU_I:
10804 s = "sltiu";
10805 s2 = "btnez";
10806 s3 = "x,8";
10807 goto do_branch_i;
10808 case M_BLE_I:
10809 s = "slti";
10810 s2 = "btnez";
10811 s3 = "x,8";
10812 goto do_addone_branch_i;
10813 case M_BLEU_I:
10814 s = "sltiu";
10815 s2 = "btnez";
10816 s3 = "x,8";
10817 goto do_addone_branch_i;
10818 case M_BGE_I:
10819 s = "slti";
10820 s2 = "bteqz";
10821 s3 = "x,8";
10822 goto do_branch_i;
10823 case M_BGEU_I:
10824 s = "sltiu";
10825 s2 = "bteqz";
10826 s3 = "x,8";
10827 goto do_branch_i;
10828 case M_BGT_I:
10829 s = "slti";
10830 s2 = "bteqz";
10831 s3 = "x,8";
10832 goto do_addone_branch_i;
10833 case M_BGTU_I:
10834 s = "sltiu";
10835 s2 = "bteqz";
10836 s3 = "x,8";
10837
10838 do_addone_branch_i:
10839 if (imm_expr.X_op != O_constant)
10840 as_bad (_("Unsupported large constant"));
10841 ++imm_expr.X_add_number;
10842
10843 do_branch_i:
10844 macro_build (&imm_expr, s, s3, xreg);
10845 macro_build (&offset_expr, s2, "p");
10846 break;
10847
10848 case M_ABS:
10849 expr1.X_add_number = 0;
10850 macro_build (&expr1, "slti", "x,8", yreg);
10851 if (xreg != yreg)
10852 move_register (xreg, yreg);
10853 expr1.X_add_number = 2;
10854 macro_build (&expr1, "bteqz", "p");
10855 macro_build (NULL, "neg", "x,w", xreg, xreg);
10856 }
10857 }
10858
10859 /* For consistency checking, verify that all bits are specified either
10860 by the match/mask part of the instruction definition, or by the
10861 operand list. */
10862 static int
10863 validate_mips_insn (const struct mips_opcode *opc)
10864 {
10865 const char *p = opc->args;
10866 char c;
10867 unsigned long used_bits = opc->mask;
10868
10869 if ((used_bits & opc->match) != opc->match)
10870 {
10871 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10872 opc->name, opc->args);
10873 return 0;
10874 }
10875 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
10876 while (*p)
10877 switch (c = *p++)
10878 {
10879 case ',': break;
10880 case '(': break;
10881 case ')': break;
10882 case '+':
10883 switch (c = *p++)
10884 {
10885 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
10886 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
10887 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
10888 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
10889 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10890 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10891 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10892 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10893 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10894 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10895 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10896 case 'I': break;
10897 case 'J': USE_BITS (OP_MASK_CODE10, OP_SH_CODE10); break;
10898 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10899 case 'x': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10900 case 'X': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10901 case 'p': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
10902 case 'P': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
10903 case 'Q': USE_BITS (OP_MASK_SEQI, OP_SH_SEQI); break;
10904 case 's': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
10905 case 'S': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
10906 case 'z': USE_BITS (OP_MASK_RZ, OP_SH_RZ); break;
10907 case 'Z': USE_BITS (OP_MASK_FZ, OP_SH_FZ); break;
10908 case 'a': USE_BITS (OP_MASK_OFFSET_A, OP_SH_OFFSET_A); break;
10909 case 'b': USE_BITS (OP_MASK_OFFSET_B, OP_SH_OFFSET_B); break;
10910 case 'c': USE_BITS (OP_MASK_OFFSET_C, OP_SH_OFFSET_C); break;
10911 case 'j': USE_BITS (OP_MASK_EVAOFFSET, OP_SH_EVAOFFSET); break;
10912
10913 default:
10914 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10915 c, opc->name, opc->args);
10916 return 0;
10917 }
10918 break;
10919 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10920 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10921 case 'A': break;
10922 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
10923 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
10924 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10925 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10926 case 'F': break;
10927 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10928 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
10929 case 'I': break;
10930 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
10931 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10932 case 'L': break;
10933 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
10934 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
10935 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
10936 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
10937 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10938 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
10939 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10940 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10941 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10942 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10943 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10944 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10945 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10946 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
10947 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10948 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
10949 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10950 case 'f': break;
10951 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
10952 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10953 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10954 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
10955 case 'l': break;
10956 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10957 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10958 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
10959 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10960 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10961 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10962 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10963 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10964 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10965 case 'x': break;
10966 case 'z': break;
10967 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
10968 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
10969 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10970 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
10971 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
10972 case '[': break;
10973 case ']': break;
10974 case '1': USE_BITS (OP_MASK_STYPE, OP_SH_STYPE); break;
10975 case '2': USE_BITS (OP_MASK_BP, OP_SH_BP); break;
10976 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
10977 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
10978 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
10979 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10980 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
10981 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
10982 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
10983 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
10984 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
10985 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
10986 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
10987 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
10988 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
10989 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
10990 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
10991 case '\\': USE_BITS (OP_MASK_3BITPOS, OP_SH_3BITPOS); break;
10992 case '~': USE_BITS (OP_MASK_OFFSET12, OP_SH_OFFSET12); break;
10993 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10994 default:
10995 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10996 c, opc->name, opc->args);
10997 return 0;
10998 }
10999 #undef USE_BITS
11000 if (used_bits != 0xffffffff)
11001 {
11002 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
11003 ~used_bits & 0xffffffff, opc->name, opc->args);
11004 return 0;
11005 }
11006 return 1;
11007 }
11008
11009 /* For consistency checking, verify that the length implied matches the
11010 major opcode and that all bits are specified either by the match/mask
11011 part of the instruction definition, or by the operand list. */
11012
11013 static int
11014 validate_micromips_insn (const struct mips_opcode *opc)
11015 {
11016 unsigned long match = opc->match;
11017 unsigned long mask = opc->mask;
11018 const char *p = opc->args;
11019 unsigned long insn_bits;
11020 unsigned long used_bits;
11021 unsigned long major;
11022 unsigned int length;
11023 char e;
11024 char c;
11025
11026 if ((mask & match) != match)
11027 {
11028 as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
11029 opc->name, opc->args);
11030 return 0;
11031 }
11032 length = micromips_insn_length (opc);
11033 if (length != 2 && length != 4)
11034 {
11035 as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
11036 "%s %s"), length, opc->name, opc->args);
11037 return 0;
11038 }
11039 major = match >> (10 + 8 * (length - 2));
11040 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
11041 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
11042 {
11043 as_bad (_("Internal error: bad microMIPS opcode "
11044 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
11045 return 0;
11046 }
11047
11048 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
11049 insn_bits = 1 << 4 * length;
11050 insn_bits <<= 4 * length;
11051 insn_bits -= 1;
11052 used_bits = mask;
11053 #define USE_BITS(field) \
11054 (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
11055 while (*p)
11056 switch (c = *p++)
11057 {
11058 case ',': break;
11059 case '(': break;
11060 case ')': break;
11061 case '+':
11062 e = c;
11063 switch (c = *p++)
11064 {
11065 case 'A': USE_BITS (EXTLSB); break;
11066 case 'B': USE_BITS (INSMSB); break;
11067 case 'C': USE_BITS (EXTMSBD); break;
11068 case 'E': USE_BITS (EXTLSB); break;
11069 case 'F': USE_BITS (INSMSB); break;
11070 case 'G': USE_BITS (EXTMSBD); break;
11071 case 'H': USE_BITS (EXTMSBD); break;
11072 case 'j': USE_BITS (EVAOFFSET); break;
11073 default:
11074 as_bad (_("Internal error: bad mips opcode "
11075 "(unknown extension operand type `%c%c'): %s %s"),
11076 e, c, opc->name, opc->args);
11077 return 0;
11078 }
11079 break;
11080 case 'm':
11081 e = c;
11082 switch (c = *p++)
11083 {
11084 case 'A': USE_BITS (IMMA); break;
11085 case 'B': USE_BITS (IMMB); break;
11086 case 'C': USE_BITS (IMMC); break;
11087 case 'D': USE_BITS (IMMD); break;
11088 case 'E': USE_BITS (IMME); break;
11089 case 'F': USE_BITS (IMMF); break;
11090 case 'G': USE_BITS (IMMG); break;
11091 case 'H': USE_BITS (IMMH); break;
11092 case 'I': USE_BITS (IMMI); break;
11093 case 'J': USE_BITS (IMMJ); break;
11094 case 'L': USE_BITS (IMML); break;
11095 case 'M': USE_BITS (IMMM); break;
11096 case 'N': USE_BITS (IMMN); break;
11097 case 'O': USE_BITS (IMMO); break;
11098 case 'P': USE_BITS (IMMP); break;
11099 case 'Q': USE_BITS (IMMQ); break;
11100 case 'U': USE_BITS (IMMU); break;
11101 case 'W': USE_BITS (IMMW); break;
11102 case 'X': USE_BITS (IMMX); break;
11103 case 'Y': USE_BITS (IMMY); break;
11104 case 'Z': break;
11105 case 'a': break;
11106 case 'b': USE_BITS (MB); break;
11107 case 'c': USE_BITS (MC); break;
11108 case 'd': USE_BITS (MD); break;
11109 case 'e': USE_BITS (ME); break;
11110 case 'f': USE_BITS (MF); break;
11111 case 'g': USE_BITS (MG); break;
11112 case 'h': USE_BITS (MH); break;
11113 case 'i': USE_BITS (MI); break;
11114 case 'j': USE_BITS (MJ); break;
11115 case 'l': USE_BITS (ML); break;
11116 case 'm': USE_BITS (MM); break;
11117 case 'n': USE_BITS (MN); break;
11118 case 'p': USE_BITS (MP); break;
11119 case 'q': USE_BITS (MQ); break;
11120 case 'r': break;
11121 case 's': break;
11122 case 't': break;
11123 case 'x': break;
11124 case 'y': break;
11125 case 'z': break;
11126 default:
11127 as_bad (_("Internal error: bad mips opcode "
11128 "(unknown extension operand type `%c%c'): %s %s"),
11129 e, c, opc->name, opc->args);
11130 return 0;
11131 }
11132 break;
11133 case '.': USE_BITS (OFFSET10); break;
11134 case '1': USE_BITS (STYPE); break;
11135 case '2': USE_BITS (BP); break;
11136 case '3': USE_BITS (SA3); break;
11137 case '4': USE_BITS (SA4); break;
11138 case '5': USE_BITS (IMM8); break;
11139 case '6': USE_BITS (RS); break;
11140 case '7': USE_BITS (DSPACC); break;
11141 case '8': USE_BITS (WRDSP); break;
11142 case '0': USE_BITS (DSPSFT); break;
11143 case '<': USE_BITS (SHAMT); break;
11144 case '>': USE_BITS (SHAMT); break;
11145 case '@': USE_BITS (IMM10); break;
11146 case 'B': USE_BITS (CODE10); break;
11147 case 'C': USE_BITS (COPZ); break;
11148 case 'D': USE_BITS (FD); break;
11149 case 'E': USE_BITS (RT); break;
11150 case 'G': USE_BITS (RS); break;
11151 case 'H': USE_BITS (SEL); break;
11152 case 'K': USE_BITS (RS); break;
11153 case 'M': USE_BITS (CCC); break;
11154 case 'N': USE_BITS (BCC); break;
11155 case 'R': USE_BITS (FR); break;
11156 case 'S': USE_BITS (FS); break;
11157 case 'T': USE_BITS (FT); break;
11158 case 'V': USE_BITS (FS); break;
11159 case '\\': USE_BITS (3BITPOS); break;
11160 case '^': USE_BITS (RD); break;
11161 case 'a': USE_BITS (TARGET); break;
11162 case 'b': USE_BITS (RS); break;
11163 case 'c': USE_BITS (CODE); break;
11164 case 'd': USE_BITS (RD); break;
11165 case 'h': USE_BITS (PREFX); break;
11166 case 'i': USE_BITS (IMMEDIATE); break;
11167 case 'j': USE_BITS (DELTA); break;
11168 case 'k': USE_BITS (CACHE); break;
11169 case 'n': USE_BITS (RT); break;
11170 case 'o': USE_BITS (DELTA); break;
11171 case 'p': USE_BITS (DELTA); break;
11172 case 'q': USE_BITS (CODE2); break;
11173 case 'r': USE_BITS (RS); break;
11174 case 's': USE_BITS (RS); break;
11175 case 't': USE_BITS (RT); break;
11176 case 'u': USE_BITS (IMMEDIATE); break;
11177 case 'v': USE_BITS (RS); break;
11178 case 'w': USE_BITS (RT); break;
11179 case 'y': USE_BITS (RS3); break;
11180 case 'z': break;
11181 case '|': USE_BITS (TRAP); break;
11182 case '~': USE_BITS (OFFSET12); break;
11183 default:
11184 as_bad (_("Internal error: bad microMIPS opcode "
11185 "(unknown operand type `%c'): %s %s"),
11186 c, opc->name, opc->args);
11187 return 0;
11188 }
11189 #undef USE_BITS
11190 if (used_bits != insn_bits)
11191 {
11192 if (~used_bits & insn_bits)
11193 as_bad (_("Internal error: bad microMIPS opcode "
11194 "(bits 0x%lx undefined): %s %s"),
11195 ~used_bits & insn_bits, opc->name, opc->args);
11196 if (used_bits & ~insn_bits)
11197 as_bad (_("Internal error: bad microMIPS opcode "
11198 "(bits 0x%lx defined): %s %s"),
11199 used_bits & ~insn_bits, opc->name, opc->args);
11200 return 0;
11201 }
11202 return 1;
11203 }
11204
11205 /* UDI immediates. */
11206 struct mips_immed {
11207 char type;
11208 unsigned int shift;
11209 unsigned long mask;
11210 const char * desc;
11211 };
11212
11213 static const struct mips_immed mips_immed[] = {
11214 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
11215 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
11216 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
11217 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
11218 { 0,0,0,0 }
11219 };
11220
11221 /* Check whether an odd floating-point register is allowed. */
11222 static int
11223 mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
11224 {
11225 const char *s = insn->name;
11226
11227 if (insn->pinfo == INSN_MACRO)
11228 /* Let a macro pass, we'll catch it later when it is expanded. */
11229 return 1;
11230
11231 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || (mips_opts.arch == CPU_R5900))
11232 {
11233 /* Allow odd registers for single-precision ops. */
11234 switch (insn->pinfo & (FP_S | FP_D))
11235 {
11236 case FP_S:
11237 case 0:
11238 return 1; /* both single precision - ok */
11239 case FP_D:
11240 return 0; /* both double precision - fail */
11241 default:
11242 break;
11243 }
11244
11245 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
11246 s = strchr (insn->name, '.');
11247 if (argnum == 2)
11248 s = s != NULL ? strchr (s + 1, '.') : NULL;
11249 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
11250 }
11251
11252 /* Single-precision coprocessor loads and moves are OK too. */
11253 if ((insn->pinfo & FP_S)
11254 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
11255 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
11256 return 1;
11257
11258 return 0;
11259 }
11260
11261 /* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
11262 taking bits from BIT up. */
11263 static int
11264 expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
11265 {
11266 return (ep->X_op == O_constant
11267 && (ep->X_add_number & ((1 << bit) - 1)) == 0
11268 && ep->X_add_number >= min << bit
11269 && ep->X_add_number < max << bit);
11270 }
11271
11272 /* This routine assembles an instruction into its binary format. As a
11273 side effect, it sets one of the global variables imm_reloc or
11274 offset_reloc to the type of relocation to do if one of the operands
11275 is an address expression. */
11276
11277 static void
11278 mips_ip (char *str, struct mips_cl_insn *ip)
11279 {
11280 bfd_boolean wrong_delay_slot_insns = FALSE;
11281 bfd_boolean need_delay_slot_ok = TRUE;
11282 struct mips_opcode *firstinsn = NULL;
11283 const struct mips_opcode *past;
11284 struct hash_control *hash;
11285 char *s;
11286 const char *args;
11287 char c = 0;
11288 struct mips_opcode *insn;
11289 char *argsStart;
11290 unsigned int regno;
11291 unsigned int lastregno;
11292 unsigned int destregno = 0;
11293 unsigned int lastpos = 0;
11294 unsigned int limlo, limhi;
11295 int sizelo;
11296 char *s_reset;
11297 offsetT min_range, max_range;
11298 long opend;
11299 char *name;
11300 int argnum;
11301 unsigned int rtype;
11302 char *dot;
11303 long end;
11304
11305 insn_error = NULL;
11306
11307 if (mips_opts.micromips)
11308 {
11309 hash = micromips_op_hash;
11310 past = &micromips_opcodes[bfd_micromips_num_opcodes];
11311 }
11312 else
11313 {
11314 hash = op_hash;
11315 past = &mips_opcodes[NUMOPCODES];
11316 }
11317 forced_insn_length = 0;
11318 insn = NULL;
11319
11320 /* We first try to match an instruction up to a space or to the end. */
11321 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
11322 continue;
11323
11324 /* Make a copy of the instruction so that we can fiddle with it. */
11325 name = alloca (end + 1);
11326 memcpy (name, str, end);
11327 name[end] = '\0';
11328
11329 for (;;)
11330 {
11331 insn = (struct mips_opcode *) hash_find (hash, name);
11332
11333 if (insn != NULL || !mips_opts.micromips)
11334 break;
11335 if (forced_insn_length)
11336 break;
11337
11338 /* See if there's an instruction size override suffix,
11339 either `16' or `32', at the end of the mnemonic proper,
11340 that defines the operation, i.e. before the first `.'
11341 character if any. Strip it and retry. */
11342 dot = strchr (name, '.');
11343 opend = dot != NULL ? dot - name : end;
11344 if (opend < 3)
11345 break;
11346 if (name[opend - 2] == '1' && name[opend - 1] == '6')
11347 forced_insn_length = 2;
11348 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
11349 forced_insn_length = 4;
11350 else
11351 break;
11352 memcpy (name + opend - 2, name + opend, end - opend + 1);
11353 }
11354 if (insn == NULL)
11355 {
11356 insn_error = _("Unrecognized opcode");
11357 return;
11358 }
11359
11360 /* For microMIPS instructions placed in a fixed-length branch delay slot
11361 we make up to two passes over the relevant fragment of the opcode
11362 table. First we try instructions that meet the delay slot's length
11363 requirement. If none matched, then we retry with the remaining ones
11364 and if one matches, then we use it and then issue an appropriate
11365 warning later on. */
11366 argsStart = s = str + end;
11367 for (;;)
11368 {
11369 bfd_boolean delay_slot_ok;
11370 bfd_boolean size_ok;
11371 bfd_boolean ok;
11372
11373 gas_assert (strcmp (insn->name, name) == 0);
11374
11375 ok = is_opcode_valid (insn);
11376 size_ok = is_size_valid (insn);
11377 delay_slot_ok = is_delay_slot_valid (insn);
11378 if (!delay_slot_ok && !wrong_delay_slot_insns)
11379 {
11380 firstinsn = insn;
11381 wrong_delay_slot_insns = TRUE;
11382 }
11383 if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
11384 {
11385 static char buf[256];
11386
11387 if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
11388 {
11389 ++insn;
11390 continue;
11391 }
11392 if (wrong_delay_slot_insns && need_delay_slot_ok)
11393 {
11394 gas_assert (firstinsn);
11395 need_delay_slot_ok = FALSE;
11396 past = insn + 1;
11397 insn = firstinsn;
11398 continue;
11399 }
11400
11401 if (insn_error)
11402 return;
11403
11404 if (!ok)
11405 sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
11406 mips_cpu_info_from_arch (mips_opts.arch)->name,
11407 mips_cpu_info_from_isa (mips_opts.isa)->name);
11408 else if (mips_opts.insn32)
11409 sprintf (buf, _("Opcode not supported in the `insn32' mode"));
11410 else
11411 sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
11412 8 * forced_insn_length);
11413 insn_error = buf;
11414
11415 return;
11416 }
11417
11418 create_insn (ip, insn);
11419 insn_error = NULL;
11420 argnum = 1;
11421 lastregno = 0xffffffff;
11422 for (args = insn->args;; ++args)
11423 {
11424 int is_mdmx;
11425
11426 s += strspn (s, " \t");
11427 is_mdmx = 0;
11428 switch (*args)
11429 {
11430 case '\0': /* end of args */
11431 if (*s == '\0')
11432 return;
11433 break;
11434
11435 case '2':
11436 /* DSP 2-bit unsigned immediate in bit 11 (for standard MIPS
11437 code) or 14 (for microMIPS code). */
11438 my_getExpression (&imm_expr, s);
11439 check_absolute_expr (ip, &imm_expr);
11440 if ((unsigned long) imm_expr.X_add_number != 1
11441 && (unsigned long) imm_expr.X_add_number != 3)
11442 {
11443 as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
11444 (unsigned long) imm_expr.X_add_number);
11445 }
11446 INSERT_OPERAND (mips_opts.micromips,
11447 BP, *ip, imm_expr.X_add_number);
11448 imm_expr.X_op = O_absent;
11449 s = expr_end;
11450 continue;
11451
11452 case '3':
11453 /* DSP 3-bit unsigned immediate in bit 21 (for standard MIPS
11454 code) or 13 (for microMIPS code). */
11455 {
11456 unsigned long mask = (mips_opts.micromips
11457 ? MICROMIPSOP_MASK_SA3 : OP_MASK_SA3);
11458
11459 my_getExpression (&imm_expr, s);
11460 check_absolute_expr (ip, &imm_expr);
11461 if ((unsigned long) imm_expr.X_add_number > mask)
11462 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11463 mask, (unsigned long) imm_expr.X_add_number);
11464 INSERT_OPERAND (mips_opts.micromips,
11465 SA3, *ip, imm_expr.X_add_number);
11466 imm_expr.X_op = O_absent;
11467 s = expr_end;
11468 }
11469 continue;
11470
11471 case '4':
11472 /* DSP 4-bit unsigned immediate in bit 21 (for standard MIPS
11473 code) or 12 (for microMIPS code). */
11474 {
11475 unsigned long mask = (mips_opts.micromips
11476 ? MICROMIPSOP_MASK_SA4 : OP_MASK_SA4);
11477
11478 my_getExpression (&imm_expr, s);
11479 check_absolute_expr (ip, &imm_expr);
11480 if ((unsigned long) imm_expr.X_add_number > mask)
11481 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11482 mask, (unsigned long) imm_expr.X_add_number);
11483 INSERT_OPERAND (mips_opts.micromips,
11484 SA4, *ip, imm_expr.X_add_number);
11485 imm_expr.X_op = O_absent;
11486 s = expr_end;
11487 }
11488 continue;
11489
11490 case '5':
11491 /* DSP 8-bit unsigned immediate in bit 16 (for standard MIPS
11492 code) or 13 (for microMIPS code). */
11493 {
11494 unsigned long mask = (mips_opts.micromips
11495 ? MICROMIPSOP_MASK_IMM8 : OP_MASK_IMM8);
11496
11497 my_getExpression (&imm_expr, s);
11498 check_absolute_expr (ip, &imm_expr);
11499 if ((unsigned long) imm_expr.X_add_number > mask)
11500 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11501 mask, (unsigned long) imm_expr.X_add_number);
11502 INSERT_OPERAND (mips_opts.micromips,
11503 IMM8, *ip, imm_expr.X_add_number);
11504 imm_expr.X_op = O_absent;
11505 s = expr_end;
11506 }
11507 continue;
11508
11509 case '6':
11510 /* DSP 5-bit unsigned immediate in bit 21 (for standard MIPS
11511 code) or 16 (for microMIPS code). */
11512 {
11513 unsigned long mask = (mips_opts.micromips
11514 ? MICROMIPSOP_MASK_RS : OP_MASK_RS);
11515
11516 my_getExpression (&imm_expr, s);
11517 check_absolute_expr (ip, &imm_expr);
11518 if ((unsigned long) imm_expr.X_add_number > mask)
11519 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11520 mask, (unsigned long) imm_expr.X_add_number);
11521 INSERT_OPERAND (mips_opts.micromips,
11522 RS, *ip, imm_expr.X_add_number);
11523 imm_expr.X_op = O_absent;
11524 s = expr_end;
11525 }
11526 continue;
11527
11528 case '7':
11529 /* Four DSP accumulators in bit 11 (for standard MIPS code)
11530 or 14 (for microMIPS code). */
11531 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11532 && s[3] >= '0' && s[3] <= '3')
11533 {
11534 regno = s[3] - '0';
11535 s += 4;
11536 INSERT_OPERAND (mips_opts.micromips, DSPACC, *ip, regno);
11537 continue;
11538 }
11539 else
11540 as_bad (_("Invalid dsp acc register"));
11541 break;
11542
11543 case '8':
11544 /* DSP 6-bit unsigned immediate in bit 11 (for standard MIPS
11545 code) or 14 (for microMIPS code). */
11546 {
11547 unsigned long mask = (mips_opts.micromips
11548 ? MICROMIPSOP_MASK_WRDSP
11549 : OP_MASK_WRDSP);
11550
11551 my_getExpression (&imm_expr, s);
11552 check_absolute_expr (ip, &imm_expr);
11553 if ((unsigned long) imm_expr.X_add_number > mask)
11554 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11555 mask, (unsigned long) imm_expr.X_add_number);
11556 INSERT_OPERAND (mips_opts.micromips,
11557 WRDSP, *ip, imm_expr.X_add_number);
11558 imm_expr.X_op = O_absent;
11559 s = expr_end;
11560 }
11561 continue;
11562
11563 case '9': /* Four DSP accumulators in bits 21,22. */
11564 gas_assert (!mips_opts.micromips);
11565 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11566 && s[3] >= '0' && s[3] <= '3')
11567 {
11568 regno = s[3] - '0';
11569 s += 4;
11570 INSERT_OPERAND (0, DSPACC_S, *ip, regno);
11571 continue;
11572 }
11573 else
11574 as_bad (_("Invalid dsp acc register"));
11575 break;
11576
11577 case '0':
11578 /* DSP 6-bit signed immediate in bit 20 (for standard MIPS
11579 code) or 16 (for microMIPS code). */
11580 {
11581 long mask = (mips_opts.micromips
11582 ? MICROMIPSOP_MASK_DSPSFT : OP_MASK_DSPSFT);
11583
11584 my_getExpression (&imm_expr, s);
11585 check_absolute_expr (ip, &imm_expr);
11586 min_range = -((mask + 1) >> 1);
11587 max_range = ((mask + 1) >> 1) - 1;
11588 if (imm_expr.X_add_number < min_range
11589 || imm_expr.X_add_number > max_range)
11590 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11591 (long) min_range, (long) max_range,
11592 (long) imm_expr.X_add_number);
11593 INSERT_OPERAND (mips_opts.micromips,
11594 DSPSFT, *ip, imm_expr.X_add_number);
11595 imm_expr.X_op = O_absent;
11596 s = expr_end;
11597 }
11598 continue;
11599
11600 case '\'': /* DSP 6-bit unsigned immediate in bit 16. */
11601 gas_assert (!mips_opts.micromips);
11602 my_getExpression (&imm_expr, s);
11603 check_absolute_expr (ip, &imm_expr);
11604 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
11605 {
11606 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11607 OP_MASK_RDDSP,
11608 (unsigned long) imm_expr.X_add_number);
11609 }
11610 INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
11611 imm_expr.X_op = O_absent;
11612 s = expr_end;
11613 continue;
11614
11615 case ':': /* DSP 7-bit signed immediate in bit 19. */
11616 gas_assert (!mips_opts.micromips);
11617 my_getExpression (&imm_expr, s);
11618 check_absolute_expr (ip, &imm_expr);
11619 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
11620 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
11621 if (imm_expr.X_add_number < min_range ||
11622 imm_expr.X_add_number > max_range)
11623 {
11624 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11625 (long) min_range, (long) max_range,
11626 (long) imm_expr.X_add_number);
11627 }
11628 INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
11629 imm_expr.X_op = O_absent;
11630 s = expr_end;
11631 continue;
11632
11633 case '@': /* DSP 10-bit signed immediate in bit 16. */
11634 {
11635 long mask = (mips_opts.micromips
11636 ? MICROMIPSOP_MASK_IMM10 : OP_MASK_IMM10);
11637
11638 my_getExpression (&imm_expr, s);
11639 check_absolute_expr (ip, &imm_expr);
11640 min_range = -((mask + 1) >> 1);
11641 max_range = ((mask + 1) >> 1) - 1;
11642 if (imm_expr.X_add_number < min_range
11643 || imm_expr.X_add_number > max_range)
11644 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11645 (long) min_range, (long) max_range,
11646 (long) imm_expr.X_add_number);
11647 INSERT_OPERAND (mips_opts.micromips,
11648 IMM10, *ip, imm_expr.X_add_number);
11649 imm_expr.X_op = O_absent;
11650 s = expr_end;
11651 }
11652 continue;
11653
11654 case '^': /* DSP 5-bit unsigned immediate in bit 11. */
11655 gas_assert (mips_opts.micromips);
11656 my_getExpression (&imm_expr, s);
11657 check_absolute_expr (ip, &imm_expr);
11658 if (imm_expr.X_add_number & ~MICROMIPSOP_MASK_RD)
11659 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11660 MICROMIPSOP_MASK_RD,
11661 (unsigned long) imm_expr.X_add_number);
11662 INSERT_OPERAND (1, RD, *ip, imm_expr.X_add_number);
11663 imm_expr.X_op = O_absent;
11664 s = expr_end;
11665 continue;
11666
11667 case '!': /* MT usermode flag bit. */
11668 gas_assert (!mips_opts.micromips);
11669 my_getExpression (&imm_expr, s);
11670 check_absolute_expr (ip, &imm_expr);
11671 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
11672 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
11673 (unsigned long) imm_expr.X_add_number);
11674 INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
11675 imm_expr.X_op = O_absent;
11676 s = expr_end;
11677 continue;
11678
11679 case '$': /* MT load high flag bit. */
11680 gas_assert (!mips_opts.micromips);
11681 my_getExpression (&imm_expr, s);
11682 check_absolute_expr (ip, &imm_expr);
11683 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
11684 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
11685 (unsigned long) imm_expr.X_add_number);
11686 INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
11687 imm_expr.X_op = O_absent;
11688 s = expr_end;
11689 continue;
11690
11691 case '*': /* Four DSP accumulators in bits 18,19. */
11692 gas_assert (!mips_opts.micromips);
11693 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11694 s[3] >= '0' && s[3] <= '3')
11695 {
11696 regno = s[3] - '0';
11697 s += 4;
11698 INSERT_OPERAND (0, MTACC_T, *ip, regno);
11699 continue;
11700 }
11701 else
11702 as_bad (_("Invalid dsp/smartmips acc register"));
11703 break;
11704
11705 case '&': /* Four DSP accumulators in bits 13,14. */
11706 gas_assert (!mips_opts.micromips);
11707 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11708 s[3] >= '0' && s[3] <= '3')
11709 {
11710 regno = s[3] - '0';
11711 s += 4;
11712 INSERT_OPERAND (0, MTACC_D, *ip, regno);
11713 continue;
11714 }
11715 else
11716 as_bad (_("Invalid dsp/smartmips acc register"));
11717 break;
11718
11719 case '\\': /* 3-bit bit position. */
11720 {
11721 unsigned long mask = (mips_opts.micromips
11722 ? MICROMIPSOP_MASK_3BITPOS
11723 : OP_MASK_3BITPOS);
11724
11725 my_getExpression (&imm_expr, s);
11726 check_absolute_expr (ip, &imm_expr);
11727 if ((unsigned long) imm_expr.X_add_number > mask)
11728 as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11729 ip->insn_mo->name,
11730 mask, (unsigned long) imm_expr.X_add_number);
11731 INSERT_OPERAND (mips_opts.micromips,
11732 3BITPOS, *ip, imm_expr.X_add_number);
11733 imm_expr.X_op = O_absent;
11734 s = expr_end;
11735 }
11736 continue;
11737
11738 case ',':
11739 ++argnum;
11740 if (*s++ == *args)
11741 continue;
11742 s--;
11743 switch (*++args)
11744 {
11745 case 'r':
11746 case 'v':
11747 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
11748 continue;
11749
11750 case 'w':
11751 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
11752 continue;
11753
11754 case 'W':
11755 gas_assert (!mips_opts.micromips);
11756 INSERT_OPERAND (0, FT, *ip, lastregno);
11757 continue;
11758
11759 case 'V':
11760 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
11761 continue;
11762 }
11763 break;
11764
11765 case '(':
11766 /* Handle optional base register.
11767 Either the base register is omitted or
11768 we must have a left paren. */
11769 /* This is dependent on the next operand specifier
11770 is a base register specification. */
11771 gas_assert (args[1] == 'b'
11772 || (mips_opts.micromips
11773 && args[1] == 'm'
11774 && (args[2] == 'l' || args[2] == 'n'
11775 || args[2] == 's' || args[2] == 'a')));
11776 if (*s == '\0' && args[1] == 'b')
11777 return;
11778 /* Fall through. */
11779
11780 case ')': /* These must match exactly. */
11781 if (*s++ == *args)
11782 continue;
11783 break;
11784
11785 case '[': /* These must match exactly. */
11786 case ']':
11787 gas_assert (!mips_opts.micromips);
11788 if (*s++ == *args)
11789 continue;
11790 break;
11791
11792 case '+': /* Opcode extension character. */
11793 switch (*++args)
11794 {
11795 case '1': /* UDI immediates. */
11796 case '2':
11797 case '3':
11798 case '4':
11799 gas_assert (!mips_opts.micromips);
11800 {
11801 const struct mips_immed *imm = mips_immed;
11802
11803 while (imm->type && imm->type != *args)
11804 ++imm;
11805 if (! imm->type)
11806 abort ();
11807 my_getExpression (&imm_expr, s);
11808 check_absolute_expr (ip, &imm_expr);
11809 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11810 {
11811 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11812 imm->desc ? imm->desc : ip->insn_mo->name,
11813 (unsigned long) imm_expr.X_add_number,
11814 (unsigned long) imm_expr.X_add_number);
11815 imm_expr.X_add_number &= imm->mask;
11816 }
11817 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11818 << imm->shift);
11819 imm_expr.X_op = O_absent;
11820 s = expr_end;
11821 }
11822 continue;
11823
11824 case 'J': /* 10-bit hypcall code. */
11825 gas_assert (!mips_opts.micromips);
11826 {
11827 unsigned long mask = OP_MASK_CODE10;
11828
11829 my_getExpression (&imm_expr, s);
11830 check_absolute_expr (ip, &imm_expr);
11831 if ((unsigned long) imm_expr.X_add_number > mask)
11832 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11833 ip->insn_mo->name,
11834 mask, (unsigned long) imm_expr.X_add_number);
11835 INSERT_OPERAND (0, CODE10, *ip, imm_expr.X_add_number);
11836 imm_expr.X_op = O_absent;
11837 s = expr_end;
11838 }
11839 continue;
11840
11841 case 'A': /* ins/ext position, becomes LSB. */
11842 limlo = 0;
11843 limhi = 31;
11844 goto do_lsb;
11845 case 'E':
11846 limlo = 32;
11847 limhi = 63;
11848 goto do_lsb;
11849 do_lsb:
11850 my_getExpression (&imm_expr, s);
11851 check_absolute_expr (ip, &imm_expr);
11852 if ((unsigned long) imm_expr.X_add_number < limlo
11853 || (unsigned long) imm_expr.X_add_number > limhi)
11854 {
11855 as_bad (_("Improper position (%lu)"),
11856 (unsigned long) imm_expr.X_add_number);
11857 imm_expr.X_add_number = limlo;
11858 }
11859 lastpos = imm_expr.X_add_number;
11860 INSERT_OPERAND (mips_opts.micromips,
11861 EXTLSB, *ip, imm_expr.X_add_number);
11862 imm_expr.X_op = O_absent;
11863 s = expr_end;
11864 continue;
11865
11866 case 'B': /* ins size, becomes MSB. */
11867 limlo = 1;
11868 limhi = 32;
11869 goto do_msb;
11870 case 'F':
11871 limlo = 33;
11872 limhi = 64;
11873 goto do_msb;
11874 do_msb:
11875 my_getExpression (&imm_expr, s);
11876 check_absolute_expr (ip, &imm_expr);
11877 /* Check for negative input so that small negative numbers
11878 will not succeed incorrectly. The checks against
11879 (pos+size) transitively check "size" itself,
11880 assuming that "pos" is reasonable. */
11881 if ((long) imm_expr.X_add_number < 0
11882 || ((unsigned long) imm_expr.X_add_number
11883 + lastpos) < limlo
11884 || ((unsigned long) imm_expr.X_add_number
11885 + lastpos) > limhi)
11886 {
11887 as_bad (_("Improper insert size (%lu, position %lu)"),
11888 (unsigned long) imm_expr.X_add_number,
11889 (unsigned long) lastpos);
11890 imm_expr.X_add_number = limlo - lastpos;
11891 }
11892 INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11893 lastpos + imm_expr.X_add_number - 1);
11894 imm_expr.X_op = O_absent;
11895 s = expr_end;
11896 continue;
11897
11898 case 'C': /* ext size, becomes MSBD. */
11899 limlo = 1;
11900 limhi = 32;
11901 sizelo = 1;
11902 goto do_msbd;
11903 case 'G':
11904 limlo = 33;
11905 limhi = 64;
11906 sizelo = 33;
11907 goto do_msbd;
11908 case 'H':
11909 limlo = 33;
11910 limhi = 64;
11911 sizelo = 1;
11912 goto do_msbd;
11913 do_msbd:
11914 my_getExpression (&imm_expr, s);
11915 check_absolute_expr (ip, &imm_expr);
11916 /* The checks against (pos+size) don't transitively check
11917 "size" itself, assuming that "pos" is reasonable.
11918 We also need to check the lower bound of "size". */
11919 if ((long) imm_expr.X_add_number < sizelo
11920 || ((unsigned long) imm_expr.X_add_number
11921 + lastpos) < limlo
11922 || ((unsigned long) imm_expr.X_add_number
11923 + lastpos) > limhi)
11924 {
11925 as_bad (_("Improper extract size (%lu, position %lu)"),
11926 (unsigned long) imm_expr.X_add_number,
11927 (unsigned long) lastpos);
11928 imm_expr.X_add_number = limlo - lastpos;
11929 }
11930 INSERT_OPERAND (mips_opts.micromips,
11931 EXTMSBD, *ip, imm_expr.X_add_number - 1);
11932 imm_expr.X_op = O_absent;
11933 s = expr_end;
11934 continue;
11935
11936 case 'I':
11937 /* "+I" is like "I", except that imm2_expr is used. */
11938 my_getExpression (&imm2_expr, s);
11939 if (imm2_expr.X_op != O_big
11940 && imm2_expr.X_op != O_constant)
11941 insn_error = _("absolute expression required");
11942 if (HAVE_32BIT_GPRS)
11943 normalize_constant_expr (&imm2_expr);
11944 s = expr_end;
11945 continue;
11946
11947 case 't': /* Coprocessor register number. */
11948 gas_assert (!mips_opts.micromips);
11949 if (s[0] == '$' && ISDIGIT (s[1]))
11950 {
11951 ++s;
11952 regno = 0;
11953 do
11954 {
11955 regno *= 10;
11956 regno += *s - '0';
11957 ++s;
11958 }
11959 while (ISDIGIT (*s));
11960 if (regno > 31)
11961 as_bad (_("Invalid register number (%d)"), regno);
11962 else
11963 {
11964 INSERT_OPERAND (0, RT, *ip, regno);
11965 continue;
11966 }
11967 }
11968 else
11969 as_bad (_("Invalid coprocessor 0 register number"));
11970 break;
11971
11972 case 'x':
11973 /* bbit[01] and bbit[01]32 bit index. Give error if index
11974 is not in the valid range. */
11975 gas_assert (!mips_opts.micromips);
11976 my_getExpression (&imm_expr, s);
11977 check_absolute_expr (ip, &imm_expr);
11978 if ((unsigned) imm_expr.X_add_number > 31)
11979 {
11980 as_bad (_("Improper bit index (%lu)"),
11981 (unsigned long) imm_expr.X_add_number);
11982 imm_expr.X_add_number = 0;
11983 }
11984 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
11985 imm_expr.X_op = O_absent;
11986 s = expr_end;
11987 continue;
11988
11989 case 'X':
11990 /* bbit[01] bit index when bbit is used but we generate
11991 bbit[01]32 because the index is over 32. Move to the
11992 next candidate if index is not in the valid range. */
11993 gas_assert (!mips_opts.micromips);
11994 my_getExpression (&imm_expr, s);
11995 check_absolute_expr (ip, &imm_expr);
11996 if ((unsigned) imm_expr.X_add_number < 32
11997 || (unsigned) imm_expr.X_add_number > 63)
11998 break;
11999 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
12000 imm_expr.X_op = O_absent;
12001 s = expr_end;
12002 continue;
12003
12004 case 'p':
12005 /* cins, cins32, exts and exts32 position field. Give error
12006 if it's not in the valid range. */
12007 gas_assert (!mips_opts.micromips);
12008 my_getExpression (&imm_expr, s);
12009 check_absolute_expr (ip, &imm_expr);
12010 if ((unsigned) imm_expr.X_add_number > 31)
12011 {
12012 as_bad (_("Improper position (%lu)"),
12013 (unsigned long) imm_expr.X_add_number);
12014 imm_expr.X_add_number = 0;
12015 }
12016 /* Make the pos explicit to simplify +S. */
12017 lastpos = imm_expr.X_add_number + 32;
12018 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
12019 imm_expr.X_op = O_absent;
12020 s = expr_end;
12021 continue;
12022
12023 case 'P':
12024 /* cins, cins32, exts and exts32 position field. Move to
12025 the next candidate if it's not in the valid range. */
12026 gas_assert (!mips_opts.micromips);
12027 my_getExpression (&imm_expr, s);
12028 check_absolute_expr (ip, &imm_expr);
12029 if ((unsigned) imm_expr.X_add_number < 32
12030 || (unsigned) imm_expr.X_add_number > 63)
12031 break;
12032 lastpos = imm_expr.X_add_number;
12033 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
12034 imm_expr.X_op = O_absent;
12035 s = expr_end;
12036 continue;
12037
12038 case 's':
12039 /* cins and exts length-minus-one field. */
12040 gas_assert (!mips_opts.micromips);
12041 my_getExpression (&imm_expr, s);
12042 check_absolute_expr (ip, &imm_expr);
12043 if ((unsigned long) imm_expr.X_add_number > 31)
12044 {
12045 as_bad (_("Improper size (%lu)"),
12046 (unsigned long) imm_expr.X_add_number);
12047 imm_expr.X_add_number = 0;
12048 }
12049 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
12050 imm_expr.X_op = O_absent;
12051 s = expr_end;
12052 continue;
12053
12054 case 'S':
12055 /* cins32/exts32 and cins/exts aliasing cint32/exts32
12056 length-minus-one field. */
12057 gas_assert (!mips_opts.micromips);
12058 my_getExpression (&imm_expr, s);
12059 check_absolute_expr (ip, &imm_expr);
12060 if ((long) imm_expr.X_add_number < 0
12061 || (unsigned long) imm_expr.X_add_number + lastpos > 63)
12062 {
12063 as_bad (_("Improper size (%lu)"),
12064 (unsigned long) imm_expr.X_add_number);
12065 imm_expr.X_add_number = 0;
12066 }
12067 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
12068 imm_expr.X_op = O_absent;
12069 s = expr_end;
12070 continue;
12071
12072 case 'Q':
12073 /* seqi/snei immediate field. */
12074 gas_assert (!mips_opts.micromips);
12075 my_getExpression (&imm_expr, s);
12076 check_absolute_expr (ip, &imm_expr);
12077 if ((long) imm_expr.X_add_number < -512
12078 || (long) imm_expr.X_add_number >= 512)
12079 {
12080 as_bad (_("Improper immediate (%ld)"),
12081 (long) imm_expr.X_add_number);
12082 imm_expr.X_add_number = 0;
12083 }
12084 INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
12085 imm_expr.X_op = O_absent;
12086 s = expr_end;
12087 continue;
12088
12089 case 'a': /* 8-bit signed offset in bit 6 */
12090 gas_assert (!mips_opts.micromips);
12091 my_getExpression (&imm_expr, s);
12092 check_absolute_expr (ip, &imm_expr);
12093 min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
12094 max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
12095 if (imm_expr.X_add_number < min_range
12096 || imm_expr.X_add_number > max_range)
12097 {
12098 as_bad (_("Offset not in range %ld..%ld (%ld)"),
12099 (long) min_range, (long) max_range,
12100 (long) imm_expr.X_add_number);
12101 }
12102 INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
12103 imm_expr.X_op = O_absent;
12104 s = expr_end;
12105 continue;
12106
12107 case 'b': /* 8-bit signed offset in bit 3 */
12108 gas_assert (!mips_opts.micromips);
12109 my_getExpression (&imm_expr, s);
12110 check_absolute_expr (ip, &imm_expr);
12111 min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
12112 max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
12113 if (imm_expr.X_add_number < min_range
12114 || imm_expr.X_add_number > max_range)
12115 {
12116 as_bad (_("Offset not in range %ld..%ld (%ld)"),
12117 (long) min_range, (long) max_range,
12118 (long) imm_expr.X_add_number);
12119 }
12120 INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
12121 imm_expr.X_op = O_absent;
12122 s = expr_end;
12123 continue;
12124
12125 case 'c': /* 9-bit signed offset in bit 6 */
12126 gas_assert (!mips_opts.micromips);
12127 my_getExpression (&imm_expr, s);
12128 check_absolute_expr (ip, &imm_expr);
12129 min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
12130 max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
12131 /* We check the offset range before adjusted. */
12132 min_range <<= 4;
12133 max_range <<= 4;
12134 if (imm_expr.X_add_number < min_range
12135 || imm_expr.X_add_number > max_range)
12136 {
12137 as_bad (_("Offset not in range %ld..%ld (%ld)"),
12138 (long) min_range, (long) max_range,
12139 (long) imm_expr.X_add_number);
12140 }
12141 if (imm_expr.X_add_number & 0xf)
12142 {
12143 as_bad (_("Offset not 16 bytes alignment (%ld)"),
12144 (long) imm_expr.X_add_number);
12145 }
12146 /* Right shift 4 bits to adjust the offset operand. */
12147 INSERT_OPERAND (0, OFFSET_C, *ip,
12148 imm_expr.X_add_number >> 4);
12149 imm_expr.X_op = O_absent;
12150 s = expr_end;
12151 continue;
12152
12153 case 'z':
12154 gas_assert (!mips_opts.micromips);
12155 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
12156 break;
12157 if (regno == AT && mips_opts.at)
12158 {
12159 if (mips_opts.at == ATREG)
12160 as_warn (_("used $at without \".set noat\""));
12161 else
12162 as_warn (_("used $%u with \".set at=$%u\""),
12163 regno, mips_opts.at);
12164 }
12165 INSERT_OPERAND (0, RZ, *ip, regno);
12166 continue;
12167
12168 case 'Z':
12169 gas_assert (!mips_opts.micromips);
12170 if (!reg_lookup (&s, RTYPE_FPU, &regno))
12171 break;
12172 INSERT_OPERAND (0, FZ, *ip, regno);
12173 continue;
12174
12175 case 'j':
12176 {
12177 int shift = 8;
12178 size_t i;
12179 /* Check whether there is only a single bracketed expression
12180 left. If so, it must be the base register and the
12181 constant must be zero. */
12182 if (*s == '(' && strchr (s + 1, '(') == 0)
12183 continue;
12184
12185 /* If this value won't fit into the offset, then go find
12186 a macro that will generate a 16- or 32-bit offset code
12187 pattern. */
12188 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
12189 if ((i == 0 && (imm_expr.X_op != O_constant
12190 || imm_expr.X_add_number >= 1 << shift
12191 || imm_expr.X_add_number < -1 << shift))
12192 || i > 0)
12193 {
12194 imm_expr.X_op = O_absent;
12195 break;
12196 }
12197 INSERT_OPERAND (mips_opts.micromips, EVAOFFSET, *ip,
12198 imm_expr.X_add_number);
12199 imm_expr.X_op = O_absent;
12200 s = expr_end;
12201 }
12202 continue;
12203
12204 default:
12205 as_bad (_("Internal error: bad %s opcode "
12206 "(unknown extension operand type `+%c'): %s %s"),
12207 mips_opts.micromips ? "microMIPS" : "MIPS",
12208 *args, insn->name, insn->args);
12209 /* Further processing is fruitless. */
12210 return;
12211 }
12212 break;
12213
12214 case '.': /* 10-bit offset. */
12215 gas_assert (mips_opts.micromips);
12216 case '~': /* 12-bit offset. */
12217 {
12218 int shift = *args == '.' ? 9 : 11;
12219 size_t i;
12220
12221 /* Check whether there is only a single bracketed expression
12222 left. If so, it must be the base register and the
12223 constant must be zero. */
12224 if (*s == '(' && strchr (s + 1, '(') == 0)
12225 continue;
12226
12227 /* If this value won't fit into the offset, then go find
12228 a macro that will generate a 16- or 32-bit offset code
12229 pattern. */
12230 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
12231 if ((i == 0 && (imm_expr.X_op != O_constant
12232 || imm_expr.X_add_number >= 1 << shift
12233 || imm_expr.X_add_number < -1 << shift))
12234 || i > 0)
12235 {
12236 imm_expr.X_op = O_absent;
12237 break;
12238 }
12239 if (shift == 9)
12240 INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
12241 else
12242 INSERT_OPERAND (mips_opts.micromips,
12243 OFFSET12, *ip, imm_expr.X_add_number);
12244 imm_expr.X_op = O_absent;
12245 s = expr_end;
12246 }
12247 continue;
12248
12249 case '<': /* must be at least one digit */
12250 /*
12251 * According to the manual, if the shift amount is greater
12252 * than 31 or less than 0, then the shift amount should be
12253 * mod 32. In reality the mips assembler issues an error.
12254 * We issue a warning and mask out all but the low 5 bits.
12255 */
12256 my_getExpression (&imm_expr, s);
12257 check_absolute_expr (ip, &imm_expr);
12258 if ((unsigned long) imm_expr.X_add_number > 31)
12259 as_warn (_("Improper shift amount (%lu)"),
12260 (unsigned long) imm_expr.X_add_number);
12261 INSERT_OPERAND (mips_opts.micromips,
12262 SHAMT, *ip, imm_expr.X_add_number);
12263 imm_expr.X_op = O_absent;
12264 s = expr_end;
12265 continue;
12266
12267 case '>': /* shift amount minus 32 */
12268 my_getExpression (&imm_expr, s);
12269 check_absolute_expr (ip, &imm_expr);
12270 if ((unsigned long) imm_expr.X_add_number < 32
12271 || (unsigned long) imm_expr.X_add_number > 63)
12272 break;
12273 INSERT_OPERAND (mips_opts.micromips,
12274 SHAMT, *ip, imm_expr.X_add_number - 32);
12275 imm_expr.X_op = O_absent;
12276 s = expr_end;
12277 continue;
12278
12279 case 'k': /* CACHE code. */
12280 case 'h': /* PREFX code. */
12281 case '1': /* SYNC type. */
12282 my_getExpression (&imm_expr, s);
12283 check_absolute_expr (ip, &imm_expr);
12284 if ((unsigned long) imm_expr.X_add_number > 31)
12285 as_warn (_("Invalid value for `%s' (%lu)"),
12286 ip->insn_mo->name,
12287 (unsigned long) imm_expr.X_add_number);
12288 switch (*args)
12289 {
12290 case 'k':
12291 if (mips_fix_cn63xxp1
12292 && !mips_opts.micromips
12293 && strcmp ("pref", insn->name) == 0)
12294 switch (imm_expr.X_add_number)
12295 {
12296 case 5:
12297 case 25:
12298 case 26:
12299 case 27:
12300 case 28:
12301 case 29:
12302 case 30:
12303 case 31: /* These are ok. */
12304 break;
12305
12306 default: /* The rest must be changed to 28. */
12307 imm_expr.X_add_number = 28;
12308 break;
12309 }
12310 INSERT_OPERAND (mips_opts.micromips,
12311 CACHE, *ip, imm_expr.X_add_number);
12312 break;
12313 case 'h':
12314 INSERT_OPERAND (mips_opts.micromips,
12315 PREFX, *ip, imm_expr.X_add_number);
12316 break;
12317 case '1':
12318 INSERT_OPERAND (mips_opts.micromips,
12319 STYPE, *ip, imm_expr.X_add_number);
12320 break;
12321 }
12322 imm_expr.X_op = O_absent;
12323 s = expr_end;
12324 continue;
12325
12326 case 'c': /* BREAK code. */
12327 {
12328 unsigned long mask = (mips_opts.micromips
12329 ? MICROMIPSOP_MASK_CODE
12330 : OP_MASK_CODE);
12331
12332 my_getExpression (&imm_expr, s);
12333 check_absolute_expr (ip, &imm_expr);
12334 if ((unsigned long) imm_expr.X_add_number > mask)
12335 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
12336 ip->insn_mo->name,
12337 mask, (unsigned long) imm_expr.X_add_number);
12338 INSERT_OPERAND (mips_opts.micromips,
12339 CODE, *ip, imm_expr.X_add_number);
12340 imm_expr.X_op = O_absent;
12341 s = expr_end;
12342 }
12343 continue;
12344
12345 case 'q': /* Lower BREAK code. */
12346 {
12347 unsigned long mask = (mips_opts.micromips
12348 ? MICROMIPSOP_MASK_CODE2
12349 : OP_MASK_CODE2);
12350
12351 my_getExpression (&imm_expr, s);
12352 check_absolute_expr (ip, &imm_expr);
12353 if ((unsigned long) imm_expr.X_add_number > mask)
12354 as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
12355 ip->insn_mo->name,
12356 mask, (unsigned long) imm_expr.X_add_number);
12357 INSERT_OPERAND (mips_opts.micromips,
12358 CODE2, *ip, imm_expr.X_add_number);
12359 imm_expr.X_op = O_absent;
12360 s = expr_end;
12361 }
12362 continue;
12363
12364 case 'B': /* 20- or 10-bit syscall/break/wait code. */
12365 {
12366 unsigned long mask = (mips_opts.micromips
12367 ? MICROMIPSOP_MASK_CODE10
12368 : OP_MASK_CODE20);
12369
12370 my_getExpression (&imm_expr, s);
12371 check_absolute_expr (ip, &imm_expr);
12372 if ((unsigned long) imm_expr.X_add_number > mask)
12373 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
12374 ip->insn_mo->name,
12375 mask, (unsigned long) imm_expr.X_add_number);
12376 if (mips_opts.micromips)
12377 INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
12378 else
12379 INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
12380 imm_expr.X_op = O_absent;
12381 s = expr_end;
12382 }
12383 continue;
12384
12385 case 'C': /* 25- or 23-bit coprocessor code. */
12386 {
12387 unsigned long mask = (mips_opts.micromips
12388 ? MICROMIPSOP_MASK_COPZ
12389 : OP_MASK_COPZ);
12390
12391 my_getExpression (&imm_expr, s);
12392 check_absolute_expr (ip, &imm_expr);
12393 if ((unsigned long) imm_expr.X_add_number > mask)
12394 as_warn (_("Coproccesor code > %u bits (%lu)"),
12395 mips_opts.micromips ? 23U : 25U,
12396 (unsigned long) imm_expr.X_add_number);
12397 INSERT_OPERAND (mips_opts.micromips,
12398 COPZ, *ip, imm_expr.X_add_number);
12399 imm_expr.X_op = O_absent;
12400 s = expr_end;
12401 }
12402 continue;
12403
12404 case 'J': /* 19-bit WAIT code. */
12405 gas_assert (!mips_opts.micromips);
12406 my_getExpression (&imm_expr, s);
12407 check_absolute_expr (ip, &imm_expr);
12408 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
12409 {
12410 as_warn (_("Illegal 19-bit code (%lu)"),
12411 (unsigned long) imm_expr.X_add_number);
12412 imm_expr.X_add_number &= OP_MASK_CODE19;
12413 }
12414 INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
12415 imm_expr.X_op = O_absent;
12416 s = expr_end;
12417 continue;
12418
12419 case 'P': /* Performance register. */
12420 gas_assert (!mips_opts.micromips);
12421 my_getExpression (&imm_expr, s);
12422 check_absolute_expr (ip, &imm_expr);
12423 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
12424 as_warn (_("Invalid performance register (%lu)"),
12425 (unsigned long) imm_expr.X_add_number);
12426 if (imm_expr.X_add_number != 0 && mips_opts.arch == CPU_R5900
12427 && (!strcmp(insn->name,"mfps") || !strcmp(insn->name,"mtps")))
12428 as_warn (_("Invalid performance register (%lu)"),
12429 (unsigned long) imm_expr.X_add_number);
12430 INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
12431 imm_expr.X_op = O_absent;
12432 s = expr_end;
12433 continue;
12434
12435 case 'G': /* Coprocessor destination register. */
12436 {
12437 unsigned long opcode = ip->insn_opcode;
12438 unsigned long mask;
12439 unsigned int types;
12440 int cop0;
12441
12442 if (mips_opts.micromips)
12443 {
12444 mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
12445 | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
12446 | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
12447 opcode &= mask;
12448 switch (opcode)
12449 {
12450 case 0x000000fc: /* mfc0 */
12451 case 0x000002fc: /* mtc0 */
12452 case 0x580000fc: /* dmfc0 */
12453 case 0x580002fc: /* dmtc0 */
12454 cop0 = 1;
12455 break;
12456 default:
12457 cop0 = 0;
12458 break;
12459 }
12460 }
12461 else
12462 {
12463 opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
12464 cop0 = opcode == OP_OP_COP0;
12465 }
12466 types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
12467 ok = reg_lookup (&s, types, &regno);
12468 if (mips_opts.micromips)
12469 INSERT_OPERAND (1, RS, *ip, regno);
12470 else
12471 INSERT_OPERAND (0, RD, *ip, regno);
12472 if (ok)
12473 {
12474 lastregno = regno;
12475 continue;
12476 }
12477 }
12478 break;
12479
12480 case 'y': /* ALNV.PS source register. */
12481 gas_assert (mips_opts.micromips);
12482 goto do_reg;
12483 case 'x': /* Ignore register name. */
12484 case 'U': /* Destination register (CLO/CLZ). */
12485 case 'g': /* Coprocessor destination register. */
12486 gas_assert (!mips_opts.micromips);
12487 case 'b': /* Base register. */
12488 case 'd': /* Destination register. */
12489 case 's': /* Source register. */
12490 case 't': /* Target register. */
12491 case 'r': /* Both target and source. */
12492 case 'v': /* Both dest and source. */
12493 case 'w': /* Both dest and target. */
12494 case 'E': /* Coprocessor target register. */
12495 case 'K': /* RDHWR destination register. */
12496 case 'z': /* Must be zero register. */
12497 do_reg:
12498 s_reset = s;
12499 if (*args == 'E' || *args == 'K')
12500 ok = reg_lookup (&s, RTYPE_NUM, &regno);
12501 else
12502 {
12503 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12504 if (regno == AT && mips_opts.at)
12505 {
12506 if (mips_opts.at == ATREG)
12507 as_warn (_("Used $at without \".set noat\""));
12508 else
12509 as_warn (_("Used $%u with \".set at=$%u\""),
12510 regno, mips_opts.at);
12511 }
12512 }
12513 if (ok)
12514 {
12515 c = *args;
12516 if (*s == ' ')
12517 ++s;
12518 if (args[1] != *s)
12519 {
12520 if (c == 'r' || c == 'v' || c == 'w')
12521 {
12522 regno = lastregno;
12523 s = s_reset;
12524 ++args;
12525 }
12526 }
12527 /* 'z' only matches $0. */
12528 if (c == 'z' && regno != 0)
12529 break;
12530
12531 if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
12532 {
12533 if (regno == lastregno)
12534 {
12535 insn_error
12536 = _("Source and destination must be different");
12537 continue;
12538 }
12539 if (regno == 31 && lastregno == 0xffffffff)
12540 {
12541 insn_error
12542 = _("A destination register must be supplied");
12543 continue;
12544 }
12545 }
12546 /* Now that we have assembled one operand, we use the args
12547 string to figure out where it goes in the instruction. */
12548 switch (c)
12549 {
12550 case 'r':
12551 case 's':
12552 case 'v':
12553 case 'b':
12554 INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
12555 break;
12556
12557 case 'K':
12558 if (mips_opts.micromips)
12559 INSERT_OPERAND (1, RS, *ip, regno);
12560 else
12561 INSERT_OPERAND (0, RD, *ip, regno);
12562 break;
12563
12564 case 'd':
12565 case 'g':
12566 INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
12567 break;
12568
12569 case 'U':
12570 gas_assert (!mips_opts.micromips);
12571 INSERT_OPERAND (0, RD, *ip, regno);
12572 INSERT_OPERAND (0, RT, *ip, regno);
12573 break;
12574
12575 case 'w':
12576 case 't':
12577 case 'E':
12578 INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
12579 break;
12580
12581 case 'y':
12582 gas_assert (mips_opts.micromips);
12583 INSERT_OPERAND (1, RS3, *ip, regno);
12584 break;
12585
12586 case 'x':
12587 /* This case exists because on the r3000 trunc
12588 expands into a macro which requires a gp
12589 register. On the r6000 or r4000 it is
12590 assembled into a single instruction which
12591 ignores the register. Thus the insn version
12592 is MIPS_ISA2 and uses 'x', and the macro
12593 version is MIPS_ISA1 and uses 't'. */
12594 break;
12595
12596 case 'z':
12597 /* This case is for the div instruction, which
12598 acts differently if the destination argument
12599 is $0. This only matches $0, and is checked
12600 outside the switch. */
12601 break;
12602 }
12603 lastregno = regno;
12604 continue;
12605 }
12606 switch (*args++)
12607 {
12608 case 'r':
12609 case 'v':
12610 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
12611 continue;
12612
12613 case 'w':
12614 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
12615 continue;
12616 }
12617 break;
12618
12619 case 'O': /* MDMX alignment immediate constant. */
12620 gas_assert (!mips_opts.micromips);
12621 my_getExpression (&imm_expr, s);
12622 check_absolute_expr (ip, &imm_expr);
12623 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
12624 as_warn (_("Improper align amount (%ld), using low bits"),
12625 (long) imm_expr.X_add_number);
12626 INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
12627 imm_expr.X_op = O_absent;
12628 s = expr_end;
12629 continue;
12630
12631 case 'Q': /* MDMX vector, element sel, or const. */
12632 if (s[0] != '$')
12633 {
12634 /* MDMX Immediate. */
12635 gas_assert (!mips_opts.micromips);
12636 my_getExpression (&imm_expr, s);
12637 check_absolute_expr (ip, &imm_expr);
12638 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
12639 as_warn (_("Invalid MDMX Immediate (%ld)"),
12640 (long) imm_expr.X_add_number);
12641 INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
12642 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12643 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
12644 else
12645 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
12646 imm_expr.X_op = O_absent;
12647 s = expr_end;
12648 continue;
12649 }
12650 /* Not MDMX Immediate. Fall through. */
12651 case 'X': /* MDMX destination register. */
12652 case 'Y': /* MDMX source register. */
12653 case 'Z': /* MDMX target register. */
12654 is_mdmx = 1;
12655 case 'W':
12656 gas_assert (!mips_opts.micromips);
12657 case 'D': /* Floating point destination register. */
12658 case 'S': /* Floating point source register. */
12659 case 'T': /* Floating point target register. */
12660 case 'R': /* Floating point source register. */
12661 case 'V':
12662 rtype = RTYPE_FPU;
12663 if (is_mdmx
12664 || ((mips_opts.ase & ASE_MDMX)
12665 && (ip->insn_mo->pinfo & FP_D)
12666 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
12667 | INSN_COPROC_MEMORY_DELAY
12668 | INSN_LOAD_COPROC_DELAY
12669 | INSN_LOAD_MEMORY_DELAY
12670 | INSN_STORE_MEMORY))))
12671 rtype |= RTYPE_VEC;
12672 s_reset = s;
12673 if (reg_lookup (&s, rtype, &regno))
12674 {
12675 if ((regno & 1) != 0
12676 && HAVE_32BIT_FPRS
12677 && !mips_oddfpreg_ok (ip->insn_mo, argnum))
12678 as_warn (_("Float register should be even, was %d"),
12679 regno);
12680
12681 c = *args;
12682 if (*s == ' ')
12683 ++s;
12684 if (args[1] != *s)
12685 {
12686 if (c == 'V' || c == 'W')
12687 {
12688 regno = lastregno;
12689 s = s_reset;
12690 ++args;
12691 }
12692 }
12693 switch (c)
12694 {
12695 case 'D':
12696 case 'X':
12697 INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
12698 break;
12699
12700 case 'V':
12701 case 'S':
12702 case 'Y':
12703 INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
12704 break;
12705
12706 case 'Q':
12707 /* This is like 'Z', but also needs to fix the MDMX
12708 vector/scalar select bits. Note that the
12709 scalar immediate case is handled above. */
12710 if (*s == '[')
12711 {
12712 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12713 int max_el = (is_qh ? 3 : 7);
12714 s++;
12715 my_getExpression(&imm_expr, s);
12716 check_absolute_expr (ip, &imm_expr);
12717 s = expr_end;
12718 if (imm_expr.X_add_number > max_el)
12719 as_bad (_("Bad element selector %ld"),
12720 (long) imm_expr.X_add_number);
12721 imm_expr.X_add_number &= max_el;
12722 ip->insn_opcode |= (imm_expr.X_add_number
12723 << (OP_SH_VSEL +
12724 (is_qh ? 2 : 1)));
12725 imm_expr.X_op = O_absent;
12726 if (*s != ']')
12727 as_warn (_("Expecting ']' found '%s'"), s);
12728 else
12729 s++;
12730 }
12731 else
12732 {
12733 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12734 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12735 << OP_SH_VSEL);
12736 else
12737 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12738 OP_SH_VSEL);
12739 }
12740 /* Fall through. */
12741 case 'W':
12742 case 'T':
12743 case 'Z':
12744 INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
12745 break;
12746
12747 case 'R':
12748 INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
12749 break;
12750 }
12751 lastregno = regno;
12752 continue;
12753 }
12754
12755 switch (*args++)
12756 {
12757 case 'V':
12758 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
12759 continue;
12760
12761 case 'W':
12762 INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
12763 continue;
12764 }
12765 break;
12766
12767 case 'I':
12768 my_getExpression (&imm_expr, s);
12769 if (imm_expr.X_op != O_big
12770 && imm_expr.X_op != O_constant)
12771 insn_error = _("absolute expression required");
12772 if (HAVE_32BIT_GPRS)
12773 normalize_constant_expr (&imm_expr);
12774 s = expr_end;
12775 continue;
12776
12777 case 'A':
12778 my_getExpression (&offset_expr, s);
12779 normalize_address_expr (&offset_expr);
12780 *imm_reloc = BFD_RELOC_32;
12781 s = expr_end;
12782 continue;
12783
12784 case 'F':
12785 case 'L':
12786 case 'f':
12787 case 'l':
12788 {
12789 int f64;
12790 int using_gprs;
12791 char *save_in;
12792 char *err;
12793 unsigned char temp[8];
12794 int len;
12795 unsigned int length;
12796 segT seg;
12797 subsegT subseg;
12798 char *p;
12799
12800 /* These only appear as the last operand in an
12801 instruction, and every instruction that accepts
12802 them in any variant accepts them in all variants.
12803 This means we don't have to worry about backing out
12804 any changes if the instruction does not match.
12805
12806 The difference between them is the size of the
12807 floating point constant and where it goes. For 'F'
12808 and 'L' the constant is 64 bits; for 'f' and 'l' it
12809 is 32 bits. Where the constant is placed is based
12810 on how the MIPS assembler does things:
12811 F -- .rdata
12812 L -- .lit8
12813 f -- immediate value
12814 l -- .lit4
12815
12816 The .lit4 and .lit8 sections are only used if
12817 permitted by the -G argument.
12818
12819 The code below needs to know whether the target register
12820 is 32 or 64 bits wide. It relies on the fact 'f' and
12821 'F' are used with GPR-based instructions and 'l' and
12822 'L' are used with FPR-based instructions. */
12823
12824 f64 = *args == 'F' || *args == 'L';
12825 using_gprs = *args == 'F' || *args == 'f';
12826
12827 save_in = input_line_pointer;
12828 input_line_pointer = s;
12829 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12830 length = len;
12831 s = input_line_pointer;
12832 input_line_pointer = save_in;
12833 if (err != NULL && *err != '\0')
12834 {
12835 as_bad (_("Bad floating point constant: %s"), err);
12836 memset (temp, '\0', sizeof temp);
12837 length = f64 ? 8 : 4;
12838 }
12839
12840 gas_assert (length == (unsigned) (f64 ? 8 : 4));
12841
12842 if (*args == 'f'
12843 || (*args == 'l'
12844 && (g_switch_value < 4
12845 || (temp[0] == 0 && temp[1] == 0)
12846 || (temp[2] == 0 && temp[3] == 0))))
12847 {
12848 imm_expr.X_op = O_constant;
12849 if (!target_big_endian)
12850 imm_expr.X_add_number = bfd_getl32 (temp);
12851 else
12852 imm_expr.X_add_number = bfd_getb32 (temp);
12853 }
12854 else if (length > 4
12855 && !mips_disable_float_construction
12856 /* Constants can only be constructed in GPRs and
12857 copied to FPRs if the GPRs are at least as wide
12858 as the FPRs. Force the constant into memory if
12859 we are using 64-bit FPRs but the GPRs are only
12860 32 bits wide. */
12861 && (using_gprs
12862 || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
12863 && ((temp[0] == 0 && temp[1] == 0)
12864 || (temp[2] == 0 && temp[3] == 0))
12865 && ((temp[4] == 0 && temp[5] == 0)
12866 || (temp[6] == 0 && temp[7] == 0)))
12867 {
12868 /* The value is simple enough to load with a couple of
12869 instructions. If using 32-bit registers, set
12870 imm_expr to the high order 32 bits and offset_expr to
12871 the low order 32 bits. Otherwise, set imm_expr to
12872 the entire 64 bit constant. */
12873 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
12874 {
12875 imm_expr.X_op = O_constant;
12876 offset_expr.X_op = O_constant;
12877 if (!target_big_endian)
12878 {
12879 imm_expr.X_add_number = bfd_getl32 (temp + 4);
12880 offset_expr.X_add_number = bfd_getl32 (temp);
12881 }
12882 else
12883 {
12884 imm_expr.X_add_number = bfd_getb32 (temp);
12885 offset_expr.X_add_number = bfd_getb32 (temp + 4);
12886 }
12887 if (offset_expr.X_add_number == 0)
12888 offset_expr.X_op = O_absent;
12889 }
12890 else
12891 {
12892 imm_expr.X_op = O_constant;
12893 if (!target_big_endian)
12894 imm_expr.X_add_number = bfd_getl64 (temp);
12895 else
12896 imm_expr.X_add_number = bfd_getb64 (temp);
12897 }
12898 }
12899 else
12900 {
12901 const char *newname;
12902 segT new_seg;
12903
12904 /* Switch to the right section. */
12905 seg = now_seg;
12906 subseg = now_subseg;
12907 switch (*args)
12908 {
12909 default: /* unused default case avoids warnings. */
12910 case 'L':
12911 newname = RDATA_SECTION_NAME;
12912 if (g_switch_value >= 8)
12913 newname = ".lit8";
12914 break;
12915 case 'F':
12916 newname = RDATA_SECTION_NAME;
12917 break;
12918 case 'l':
12919 gas_assert (g_switch_value >= 4);
12920 newname = ".lit4";
12921 break;
12922 }
12923 new_seg = subseg_new (newname, (subsegT) 0);
12924 bfd_set_section_flags (stdoutput, new_seg,
12925 (SEC_ALLOC
12926 | SEC_LOAD
12927 | SEC_READONLY
12928 | SEC_DATA));
12929 frag_align (*args == 'l' ? 2 : 3, 0, 0);
12930 if (strncmp (TARGET_OS, "elf", 3) != 0)
12931 record_alignment (new_seg, 4);
12932 else
12933 record_alignment (new_seg, *args == 'l' ? 2 : 3);
12934 if (seg == now_seg)
12935 as_bad (_("Can't use floating point insn in this section"));
12936
12937 /* Set the argument to the current address in the
12938 section. */
12939 offset_expr.X_op = O_symbol;
12940 offset_expr.X_add_symbol = symbol_temp_new_now ();
12941 offset_expr.X_add_number = 0;
12942
12943 /* Put the floating point number into the section. */
12944 p = frag_more ((int) length);
12945 memcpy (p, temp, length);
12946
12947 /* Switch back to the original section. */
12948 subseg_set (seg, subseg);
12949 }
12950 }
12951 continue;
12952
12953 case 'i': /* 16-bit unsigned immediate. */
12954 case 'j': /* 16-bit signed immediate. */
12955 *imm_reloc = BFD_RELOC_LO16;
12956 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12957 {
12958 int more;
12959 offsetT minval, maxval;
12960
12961 more = (insn + 1 < past
12962 && strcmp (insn->name, insn[1].name) == 0);
12963
12964 /* For compatibility with older assemblers, we accept
12965 0x8000-0xffff as signed 16-bit numbers when only
12966 signed numbers are allowed. */
12967 if (*args == 'i')
12968 minval = 0, maxval = 0xffff;
12969 else if (more)
12970 minval = -0x8000, maxval = 0x7fff;
12971 else
12972 minval = -0x8000, maxval = 0xffff;
12973
12974 if (imm_expr.X_op != O_constant
12975 || imm_expr.X_add_number < minval
12976 || imm_expr.X_add_number > maxval)
12977 {
12978 if (more)
12979 break;
12980 if (imm_expr.X_op == O_constant
12981 || imm_expr.X_op == O_big)
12982 as_bad (_("Expression out of range"));
12983 }
12984 }
12985 s = expr_end;
12986 continue;
12987
12988 case 'o': /* 16-bit offset. */
12989 offset_reloc[0] = BFD_RELOC_LO16;
12990 offset_reloc[1] = BFD_RELOC_UNUSED;
12991 offset_reloc[2] = BFD_RELOC_UNUSED;
12992
12993 /* Check whether there is only a single bracketed expression
12994 left. If so, it must be the base register and the
12995 constant must be zero. */
12996 if (*s == '(' && strchr (s + 1, '(') == 0)
12997 {
12998 offset_expr.X_op = O_constant;
12999 offset_expr.X_add_number = 0;
13000 continue;
13001 }
13002
13003 /* If this value won't fit into a 16 bit offset, then go
13004 find a macro that will generate the 32 bit offset
13005 code pattern. */
13006 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
13007 && (offset_expr.X_op != O_constant
13008 || offset_expr.X_add_number >= 0x8000
13009 || offset_expr.X_add_number < -0x8000))
13010 break;
13011
13012 s = expr_end;
13013 continue;
13014
13015 case 'p': /* PC-relative offset. */
13016 *offset_reloc = BFD_RELOC_16_PCREL_S2;
13017 my_getExpression (&offset_expr, s);
13018 s = expr_end;
13019 continue;
13020
13021 case 'u': /* Upper 16 bits. */
13022 *imm_reloc = BFD_RELOC_LO16;
13023 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
13024 && imm_expr.X_op == O_constant
13025 && (imm_expr.X_add_number < 0
13026 || imm_expr.X_add_number >= 0x10000))
13027 as_bad (_("lui expression (%lu) not in range 0..65535"),
13028 (unsigned long) imm_expr.X_add_number);
13029 s = expr_end;
13030 continue;
13031
13032 case 'a': /* 26-bit address. */
13033 *offset_reloc = BFD_RELOC_MIPS_JMP;
13034 my_getExpression (&offset_expr, s);
13035 s = expr_end;
13036 continue;
13037
13038 case 'N': /* 3-bit branch condition code. */
13039 case 'M': /* 3-bit compare condition code. */
13040 rtype = RTYPE_CCC;
13041 if (ip->insn_mo->pinfo & (FP_D | FP_S))
13042 rtype |= RTYPE_FCC;
13043 if (!reg_lookup (&s, rtype, &regno))
13044 break;
13045 if ((strcmp (str + strlen (str) - 3, ".ps") == 0
13046 || strcmp (str + strlen (str) - 5, "any2f") == 0
13047 || strcmp (str + strlen (str) - 5, "any2t") == 0)
13048 && (regno & 1) != 0)
13049 as_warn (_("Condition code register should be even for %s, "
13050 "was %d"),
13051 str, regno);
13052 if ((strcmp (str + strlen (str) - 5, "any4f") == 0
13053 || strcmp (str + strlen (str) - 5, "any4t") == 0)
13054 && (regno & 3) != 0)
13055 as_warn (_("Condition code register should be 0 or 4 for %s, "
13056 "was %d"),
13057 str, regno);
13058 if (*args == 'N')
13059 INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
13060 else
13061 INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
13062 continue;
13063
13064 case 'H':
13065 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
13066 s += 2;
13067 if (ISDIGIT (*s))
13068 {
13069 c = 0;
13070 do
13071 {
13072 c *= 10;
13073 c += *s - '0';
13074 ++s;
13075 }
13076 while (ISDIGIT (*s));
13077 }
13078 else
13079 c = 8; /* Invalid sel value. */
13080
13081 if (c > 7)
13082 as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
13083 INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
13084 continue;
13085
13086 case 'e':
13087 gas_assert (!mips_opts.micromips);
13088 /* Must be at least one digit. */
13089 my_getExpression (&imm_expr, s);
13090 check_absolute_expr (ip, &imm_expr);
13091
13092 if ((unsigned long) imm_expr.X_add_number
13093 > (unsigned long) OP_MASK_VECBYTE)
13094 {
13095 as_bad (_("bad byte vector index (%ld)"),
13096 (long) imm_expr.X_add_number);
13097 imm_expr.X_add_number = 0;
13098 }
13099
13100 INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
13101 imm_expr.X_op = O_absent;
13102 s = expr_end;
13103 continue;
13104
13105 case '%':
13106 gas_assert (!mips_opts.micromips);
13107 my_getExpression (&imm_expr, s);
13108 check_absolute_expr (ip, &imm_expr);
13109
13110 if ((unsigned long) imm_expr.X_add_number
13111 > (unsigned long) OP_MASK_VECALIGN)
13112 {
13113 as_bad (_("bad byte vector index (%ld)"),
13114 (long) imm_expr.X_add_number);
13115 imm_expr.X_add_number = 0;
13116 }
13117
13118 INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
13119 imm_expr.X_op = O_absent;
13120 s = expr_end;
13121 continue;
13122
13123 case 'm': /* Opcode extension character. */
13124 gas_assert (mips_opts.micromips);
13125 c = *++args;
13126 switch (c)
13127 {
13128 case 'r':
13129 if (strncmp (s, "$pc", 3) == 0)
13130 {
13131 s += 3;
13132 continue;
13133 }
13134 break;
13135
13136 case 'a':
13137 case 'b':
13138 case 'c':
13139 case 'd':
13140 case 'e':
13141 case 'f':
13142 case 'g':
13143 case 'h':
13144 case 'i':
13145 case 'j':
13146 case 'l':
13147 case 'm':
13148 case 'n':
13149 case 'p':
13150 case 'q':
13151 case 's':
13152 case 't':
13153 case 'x':
13154 case 'y':
13155 case 'z':
13156 s_reset = s;
13157 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
13158 if (regno == AT && mips_opts.at)
13159 {
13160 if (mips_opts.at == ATREG)
13161 as_warn (_("Used $at without \".set noat\""));
13162 else
13163 as_warn (_("Used $%u with \".set at=$%u\""),
13164 regno, mips_opts.at);
13165 }
13166 if (!ok)
13167 {
13168 if (c == 'c')
13169 {
13170 gas_assert (args[1] == ',');
13171 regno = lastregno;
13172 ++args;
13173 }
13174 else if (c == 't')
13175 {
13176 gas_assert (args[1] == ',');
13177 ++args;
13178 continue; /* Nothing to do. */
13179 }
13180 else
13181 break;
13182 }
13183
13184 if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
13185 {
13186 if (regno == lastregno)
13187 {
13188 insn_error
13189 = _("Source and destination must be different");
13190 continue;
13191 }
13192 if (regno == 31 && lastregno == 0xffffffff)
13193 {
13194 insn_error
13195 = _("A destination register must be supplied");
13196 continue;
13197 }
13198 }
13199
13200 if (*s == ' ')
13201 ++s;
13202 if (args[1] != *s)
13203 {
13204 if (c == 'e')
13205 {
13206 gas_assert (args[1] == ',');
13207 regno = lastregno;
13208 s = s_reset;
13209 ++args;
13210 }
13211 else if (c == 't')
13212 {
13213 gas_assert (args[1] == ',');
13214 s = s_reset;
13215 ++args;
13216 continue; /* Nothing to do. */
13217 }
13218 }
13219
13220 /* Make sure regno is the same as lastregno. */
13221 if (c == 't' && regno != lastregno)
13222 break;
13223
13224 /* Make sure regno is the same as destregno. */
13225 if (c == 'x' && regno != destregno)
13226 break;
13227
13228 /* We need to save regno, before regno maps to the
13229 microMIPS register encoding. */
13230 lastregno = regno;
13231
13232 if (c == 'f')
13233 destregno = regno;
13234
13235 switch (c)
13236 {
13237 case 'a':
13238 if (regno != GP)
13239 regno = ILLEGAL_REG;
13240 break;
13241
13242 case 'b':
13243 regno = mips32_to_micromips_reg_b_map[regno];
13244 break;
13245
13246 case 'c':
13247 regno = mips32_to_micromips_reg_c_map[regno];
13248 break;
13249
13250 case 'd':
13251 regno = mips32_to_micromips_reg_d_map[regno];
13252 break;
13253
13254 case 'e':
13255 regno = mips32_to_micromips_reg_e_map[regno];
13256 break;
13257
13258 case 'f':
13259 regno = mips32_to_micromips_reg_f_map[regno];
13260 break;
13261
13262 case 'g':
13263 regno = mips32_to_micromips_reg_g_map[regno];
13264 break;
13265
13266 case 'h':
13267 regno = mips32_to_micromips_reg_h_map[regno];
13268 break;
13269
13270 case 'i':
13271 switch (EXTRACT_OPERAND (1, MI, *ip))
13272 {
13273 case 4:
13274 if (regno == 21)
13275 regno = 3;
13276 else if (regno == 22)
13277 regno = 4;
13278 else if (regno == 5)
13279 regno = 5;
13280 else if (regno == 6)
13281 regno = 6;
13282 else if (regno == 7)
13283 regno = 7;
13284 else
13285 regno = ILLEGAL_REG;
13286 break;
13287
13288 case 5:
13289 if (regno == 6)
13290 regno = 0;
13291 else if (regno == 7)
13292 regno = 1;
13293 else
13294 regno = ILLEGAL_REG;
13295 break;
13296
13297 case 6:
13298 if (regno == 7)
13299 regno = 2;
13300 else
13301 regno = ILLEGAL_REG;
13302 break;
13303
13304 default:
13305 regno = ILLEGAL_REG;
13306 break;
13307 }
13308 break;
13309
13310 case 'l':
13311 regno = mips32_to_micromips_reg_l_map[regno];
13312 break;
13313
13314 case 'm':
13315 regno = mips32_to_micromips_reg_m_map[regno];
13316 break;
13317
13318 case 'n':
13319 regno = mips32_to_micromips_reg_n_map[regno];
13320 break;
13321
13322 case 'q':
13323 regno = mips32_to_micromips_reg_q_map[regno];
13324 break;
13325
13326 case 's':
13327 if (regno != SP)
13328 regno = ILLEGAL_REG;
13329 break;
13330
13331 case 'y':
13332 if (regno != 31)
13333 regno = ILLEGAL_REG;
13334 break;
13335
13336 case 'z':
13337 if (regno != ZERO)
13338 regno = ILLEGAL_REG;
13339 break;
13340
13341 case 'j': /* Do nothing. */
13342 case 'p':
13343 case 't':
13344 case 'x':
13345 break;
13346
13347 default:
13348 abort ();
13349 }
13350
13351 if (regno == ILLEGAL_REG)
13352 break;
13353
13354 switch (c)
13355 {
13356 case 'b':
13357 INSERT_OPERAND (1, MB, *ip, regno);
13358 break;
13359
13360 case 'c':
13361 INSERT_OPERAND (1, MC, *ip, regno);
13362 break;
13363
13364 case 'd':
13365 INSERT_OPERAND (1, MD, *ip, regno);
13366 break;
13367
13368 case 'e':
13369 INSERT_OPERAND (1, ME, *ip, regno);
13370 break;
13371
13372 case 'f':
13373 INSERT_OPERAND (1, MF, *ip, regno);
13374 break;
13375
13376 case 'g':
13377 INSERT_OPERAND (1, MG, *ip, regno);
13378 break;
13379
13380 case 'h':
13381 INSERT_OPERAND (1, MH, *ip, regno);
13382 break;
13383
13384 case 'i':
13385 INSERT_OPERAND (1, MI, *ip, regno);
13386 break;
13387
13388 case 'j':
13389 INSERT_OPERAND (1, MJ, *ip, regno);
13390 break;
13391
13392 case 'l':
13393 INSERT_OPERAND (1, ML, *ip, regno);
13394 break;
13395
13396 case 'm':
13397 INSERT_OPERAND (1, MM, *ip, regno);
13398 break;
13399
13400 case 'n':
13401 INSERT_OPERAND (1, MN, *ip, regno);
13402 break;
13403
13404 case 'p':
13405 INSERT_OPERAND (1, MP, *ip, regno);
13406 break;
13407
13408 case 'q':
13409 INSERT_OPERAND (1, MQ, *ip, regno);
13410 break;
13411
13412 case 'a': /* Do nothing. */
13413 case 's': /* Do nothing. */
13414 case 't': /* Do nothing. */
13415 case 'x': /* Do nothing. */
13416 case 'y': /* Do nothing. */
13417 case 'z': /* Do nothing. */
13418 break;
13419
13420 default:
13421 abort ();
13422 }
13423 continue;
13424
13425 case 'A':
13426 {
13427 bfd_reloc_code_real_type r[3];
13428 expressionS ep;
13429 int imm;
13430
13431 /* Check whether there is only a single bracketed
13432 expression left. If so, it must be the base register
13433 and the constant must be zero. */
13434 if (*s == '(' && strchr (s + 1, '(') == 0)
13435 {
13436 INSERT_OPERAND (1, IMMA, *ip, 0);
13437 continue;
13438 }
13439
13440 if (my_getSmallExpression (&ep, r, s) > 0
13441 || !expr_const_in_range (&ep, -64, 64, 2))
13442 break;
13443
13444 imm = ep.X_add_number >> 2;
13445 INSERT_OPERAND (1, IMMA, *ip, imm);
13446 }
13447 s = expr_end;
13448 continue;
13449
13450 case 'B':
13451 {
13452 bfd_reloc_code_real_type r[3];
13453 expressionS ep;
13454 int imm;
13455
13456 if (my_getSmallExpression (&ep, r, s) > 0
13457 || ep.X_op != O_constant)
13458 break;
13459
13460 for (imm = 0; imm < 8; imm++)
13461 if (micromips_imm_b_map[imm] == ep.X_add_number)
13462 break;
13463 if (imm >= 8)
13464 break;
13465
13466 INSERT_OPERAND (1, IMMB, *ip, imm);
13467 }
13468 s = expr_end;
13469 continue;
13470
13471 case 'C':
13472 {
13473 bfd_reloc_code_real_type r[3];
13474 expressionS ep;
13475 int imm;
13476
13477 if (my_getSmallExpression (&ep, r, s) > 0
13478 || ep.X_op != O_constant)
13479 break;
13480
13481 for (imm = 0; imm < 16; imm++)
13482 if (micromips_imm_c_map[imm] == ep.X_add_number)
13483 break;
13484 if (imm >= 16)
13485 break;
13486
13487 INSERT_OPERAND (1, IMMC, *ip, imm);
13488 }
13489 s = expr_end;
13490 continue;
13491
13492 case 'D': /* pc relative offset */
13493 case 'E': /* pc relative offset */
13494 my_getExpression (&offset_expr, s);
13495 if (offset_expr.X_op == O_register)
13496 break;
13497
13498 if (!forced_insn_length)
13499 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
13500 else if (c == 'D')
13501 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
13502 else
13503 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
13504 s = expr_end;
13505 continue;
13506
13507 case 'F':
13508 {
13509 bfd_reloc_code_real_type r[3];
13510 expressionS ep;
13511 int imm;
13512
13513 if (my_getSmallExpression (&ep, r, s) > 0
13514 || !expr_const_in_range (&ep, 0, 16, 0))
13515 break;
13516
13517 imm = ep.X_add_number;
13518 INSERT_OPERAND (1, IMMF, *ip, imm);
13519 }
13520 s = expr_end;
13521 continue;
13522
13523 case 'G':
13524 {
13525 bfd_reloc_code_real_type r[3];
13526 expressionS ep;
13527 int imm;
13528
13529 /* Check whether there is only a single bracketed
13530 expression left. If so, it must be the base register
13531 and the constant must be zero. */
13532 if (*s == '(' && strchr (s + 1, '(') == 0)
13533 {
13534 INSERT_OPERAND (1, IMMG, *ip, 0);
13535 continue;
13536 }
13537
13538 if (my_getSmallExpression (&ep, r, s) > 0
13539 || !expr_const_in_range (&ep, -1, 15, 0))
13540 break;
13541
13542 imm = ep.X_add_number & 15;
13543 INSERT_OPERAND (1, IMMG, *ip, imm);
13544 }
13545 s = expr_end;
13546 continue;
13547
13548 case 'H':
13549 {
13550 bfd_reloc_code_real_type r[3];
13551 expressionS ep;
13552 int imm;
13553
13554 /* Check whether there is only a single bracketed
13555 expression left. If so, it must be the base register
13556 and the constant must be zero. */
13557 if (*s == '(' && strchr (s + 1, '(') == 0)
13558 {
13559 INSERT_OPERAND (1, IMMH, *ip, 0);
13560 continue;
13561 }
13562
13563 if (my_getSmallExpression (&ep, r, s) > 0
13564 || !expr_const_in_range (&ep, 0, 16, 1))
13565 break;
13566
13567 imm = ep.X_add_number >> 1;
13568 INSERT_OPERAND (1, IMMH, *ip, imm);
13569 }
13570 s = expr_end;
13571 continue;
13572
13573 case 'I':
13574 {
13575 bfd_reloc_code_real_type r[3];
13576 expressionS ep;
13577 int imm;
13578
13579 if (my_getSmallExpression (&ep, r, s) > 0
13580 || !expr_const_in_range (&ep, -1, 127, 0))
13581 break;
13582
13583 imm = ep.X_add_number & 127;
13584 INSERT_OPERAND (1, IMMI, *ip, imm);
13585 }
13586 s = expr_end;
13587 continue;
13588
13589 case 'J':
13590 {
13591 bfd_reloc_code_real_type r[3];
13592 expressionS ep;
13593 int imm;
13594
13595 /* Check whether there is only a single bracketed
13596 expression left. If so, it must be the base register
13597 and the constant must be zero. */
13598 if (*s == '(' && strchr (s + 1, '(') == 0)
13599 {
13600 INSERT_OPERAND (1, IMMJ, *ip, 0);
13601 continue;
13602 }
13603
13604 if (my_getSmallExpression (&ep, r, s) > 0
13605 || !expr_const_in_range (&ep, 0, 16, 2))
13606 break;
13607
13608 imm = ep.X_add_number >> 2;
13609 INSERT_OPERAND (1, IMMJ, *ip, imm);
13610 }
13611 s = expr_end;
13612 continue;
13613
13614 case 'L':
13615 {
13616 bfd_reloc_code_real_type r[3];
13617 expressionS ep;
13618 int imm;
13619
13620 /* Check whether there is only a single bracketed
13621 expression left. If so, it must be the base register
13622 and the constant must be zero. */
13623 if (*s == '(' && strchr (s + 1, '(') == 0)
13624 {
13625 INSERT_OPERAND (1, IMML, *ip, 0);
13626 continue;
13627 }
13628
13629 if (my_getSmallExpression (&ep, r, s) > 0
13630 || !expr_const_in_range (&ep, 0, 16, 0))
13631 break;
13632
13633 imm = ep.X_add_number;
13634 INSERT_OPERAND (1, IMML, *ip, imm);
13635 }
13636 s = expr_end;
13637 continue;
13638
13639 case 'M':
13640 {
13641 bfd_reloc_code_real_type r[3];
13642 expressionS ep;
13643 int imm;
13644
13645 if (my_getSmallExpression (&ep, r, s) > 0
13646 || !expr_const_in_range (&ep, 1, 9, 0))
13647 break;
13648
13649 imm = ep.X_add_number & 7;
13650 INSERT_OPERAND (1, IMMM, *ip, imm);
13651 }
13652 s = expr_end;
13653 continue;
13654
13655 case 'N': /* Register list for lwm and swm. */
13656 {
13657 /* A comma-separated list of registers and/or
13658 dash-separated contiguous ranges including
13659 both ra and a set of one or more registers
13660 starting at s0 up to s3 which have to be
13661 consecutive, e.g.:
13662
13663 s0, ra
13664 s0, s1, ra, s2, s3
13665 s0-s2, ra
13666
13667 and any permutations of these. */
13668 unsigned int reglist;
13669 int imm;
13670
13671 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13672 break;
13673
13674 if ((reglist & 0xfff1ffff) != 0x80010000)
13675 break;
13676
13677 reglist = (reglist >> 17) & 7;
13678 reglist += 1;
13679 if ((reglist & -reglist) != reglist)
13680 break;
13681
13682 imm = ffs (reglist) - 1;
13683 INSERT_OPERAND (1, IMMN, *ip, imm);
13684 }
13685 continue;
13686
13687 case 'O': /* sdbbp 4-bit code. */
13688 {
13689 bfd_reloc_code_real_type r[3];
13690 expressionS ep;
13691 int imm;
13692
13693 if (my_getSmallExpression (&ep, r, s) > 0
13694 || !expr_const_in_range (&ep, 0, 16, 0))
13695 break;
13696
13697 imm = ep.X_add_number;
13698 INSERT_OPERAND (1, IMMO, *ip, imm);
13699 }
13700 s = expr_end;
13701 continue;
13702
13703 case 'P':
13704 {
13705 bfd_reloc_code_real_type r[3];
13706 expressionS ep;
13707 int imm;
13708
13709 if (my_getSmallExpression (&ep, r, s) > 0
13710 || !expr_const_in_range (&ep, 0, 32, 2))
13711 break;
13712
13713 imm = ep.X_add_number >> 2;
13714 INSERT_OPERAND (1, IMMP, *ip, imm);
13715 }
13716 s = expr_end;
13717 continue;
13718
13719 case 'Q':
13720 {
13721 bfd_reloc_code_real_type r[3];
13722 expressionS ep;
13723 int imm;
13724
13725 if (my_getSmallExpression (&ep, r, s) > 0
13726 || !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13727 break;
13728
13729 imm = ep.X_add_number >> 2;
13730 INSERT_OPERAND (1, IMMQ, *ip, imm);
13731 }
13732 s = expr_end;
13733 continue;
13734
13735 case 'U':
13736 {
13737 bfd_reloc_code_real_type r[3];
13738 expressionS ep;
13739 int imm;
13740
13741 /* Check whether there is only a single bracketed
13742 expression left. If so, it must be the base register
13743 and the constant must be zero. */
13744 if (*s == '(' && strchr (s + 1, '(') == 0)
13745 {
13746 INSERT_OPERAND (1, IMMU, *ip, 0);
13747 continue;
13748 }
13749
13750 if (my_getSmallExpression (&ep, r, s) > 0
13751 || !expr_const_in_range (&ep, 0, 32, 2))
13752 break;
13753
13754 imm = ep.X_add_number >> 2;
13755 INSERT_OPERAND (1, IMMU, *ip, imm);
13756 }
13757 s = expr_end;
13758 continue;
13759
13760 case 'W':
13761 {
13762 bfd_reloc_code_real_type r[3];
13763 expressionS ep;
13764 int imm;
13765
13766 if (my_getSmallExpression (&ep, r, s) > 0
13767 || !expr_const_in_range (&ep, 0, 64, 2))
13768 break;
13769
13770 imm = ep.X_add_number >> 2;
13771 INSERT_OPERAND (1, IMMW, *ip, imm);
13772 }
13773 s = expr_end;
13774 continue;
13775
13776 case 'X':
13777 {
13778 bfd_reloc_code_real_type r[3];
13779 expressionS ep;
13780 int imm;
13781
13782 if (my_getSmallExpression (&ep, r, s) > 0
13783 || !expr_const_in_range (&ep, -8, 8, 0))
13784 break;
13785
13786 imm = ep.X_add_number;
13787 INSERT_OPERAND (1, IMMX, *ip, imm);
13788 }
13789 s = expr_end;
13790 continue;
13791
13792 case 'Y':
13793 {
13794 bfd_reloc_code_real_type r[3];
13795 expressionS ep;
13796 int imm;
13797
13798 if (my_getSmallExpression (&ep, r, s) > 0
13799 || expr_const_in_range (&ep, -2, 2, 2)
13800 || !expr_const_in_range (&ep, -258, 258, 2))
13801 break;
13802
13803 imm = ep.X_add_number >> 2;
13804 imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13805 INSERT_OPERAND (1, IMMY, *ip, imm);
13806 }
13807 s = expr_end;
13808 continue;
13809
13810 case 'Z':
13811 {
13812 bfd_reloc_code_real_type r[3];
13813 expressionS ep;
13814
13815 if (my_getSmallExpression (&ep, r, s) > 0
13816 || !expr_const_in_range (&ep, 0, 1, 0))
13817 break;
13818 }
13819 s = expr_end;
13820 continue;
13821
13822 default:
13823 as_bad (_("Internal error: bad microMIPS opcode "
13824 "(unknown extension operand type `m%c'): %s %s"),
13825 *args, insn->name, insn->args);
13826 /* Further processing is fruitless. */
13827 return;
13828 }
13829 break;
13830
13831 case 'n': /* Register list for 32-bit lwm and swm. */
13832 gas_assert (mips_opts.micromips);
13833 {
13834 /* A comma-separated list of registers and/or
13835 dash-separated contiguous ranges including
13836 at least one of ra and a set of one or more
13837 registers starting at s0 up to s7 and then
13838 s8 which have to be consecutive, e.g.:
13839
13840 ra
13841 s0
13842 ra, s0, s1, s2
13843 s0-s8
13844 s0-s5, ra
13845
13846 and any permutations of these. */
13847 unsigned int reglist;
13848 int imm;
13849 int ra;
13850
13851 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13852 break;
13853
13854 if ((reglist & 0x3f00ffff) != 0)
13855 break;
13856
13857 ra = (reglist >> 27) & 0x10;
13858 reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13859 reglist += 1;
13860 if ((reglist & -reglist) != reglist)
13861 break;
13862
13863 imm = (ffs (reglist) - 1) | ra;
13864 INSERT_OPERAND (1, RT, *ip, imm);
13865 imm_expr.X_op = O_absent;
13866 }
13867 continue;
13868
13869 case '|': /* 4-bit trap code. */
13870 gas_assert (mips_opts.micromips);
13871 my_getExpression (&imm_expr, s);
13872 check_absolute_expr (ip, &imm_expr);
13873 if ((unsigned long) imm_expr.X_add_number
13874 > MICROMIPSOP_MASK_TRAP)
13875 as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13876 (unsigned long) imm_expr.X_add_number,
13877 ip->insn_mo->name);
13878 INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
13879 imm_expr.X_op = O_absent;
13880 s = expr_end;
13881 continue;
13882
13883 default:
13884 as_bad (_("Bad char = '%c'\n"), *args);
13885 abort ();
13886 }
13887 break;
13888 }
13889 /* Args don't match. */
13890 s = argsStart;
13891 insn_error = _("Illegal operands");
13892 if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
13893 {
13894 ++insn;
13895 continue;
13896 }
13897 else if (wrong_delay_slot_insns && need_delay_slot_ok)
13898 {
13899 gas_assert (firstinsn);
13900 need_delay_slot_ok = FALSE;
13901 past = insn + 1;
13902 insn = firstinsn;
13903 continue;
13904 }
13905 return;
13906 }
13907 }
13908
13909 #define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13910
13911 /* This routine assembles an instruction into its binary format when
13912 assembling for the mips16. As a side effect, it sets one of the
13913 global variables imm_reloc or offset_reloc to the type of relocation
13914 to do if one of the operands is an address expression. It also sets
13915 forced_insn_length to the resulting instruction size in bytes if the
13916 user explicitly requested a small or extended instruction. */
13917
13918 static void
13919 mips16_ip (char *str, struct mips_cl_insn *ip)
13920 {
13921 char *s;
13922 const char *args;
13923 struct mips_opcode *insn;
13924 char *argsstart;
13925 unsigned int regno;
13926 unsigned int lastregno = 0;
13927 char *s_reset;
13928 size_t i;
13929
13930 insn_error = NULL;
13931
13932 forced_insn_length = 0;
13933
13934 for (s = str; ISLOWER (*s); ++s)
13935 ;
13936 switch (*s)
13937 {
13938 case '\0':
13939 break;
13940
13941 case ' ':
13942 *s++ = '\0';
13943 break;
13944
13945 case '.':
13946 if (s[1] == 't' && s[2] == ' ')
13947 {
13948 *s = '\0';
13949 forced_insn_length = 2;
13950 s += 3;
13951 break;
13952 }
13953 else if (s[1] == 'e' && s[2] == ' ')
13954 {
13955 *s = '\0';
13956 forced_insn_length = 4;
13957 s += 3;
13958 break;
13959 }
13960 /* Fall through. */
13961 default:
13962 insn_error = _("unknown opcode");
13963 return;
13964 }
13965
13966 if (mips_opts.noautoextend && !forced_insn_length)
13967 forced_insn_length = 2;
13968
13969 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13970 {
13971 insn_error = _("unrecognized opcode");
13972 return;
13973 }
13974
13975 argsstart = s;
13976 for (;;)
13977 {
13978 bfd_boolean ok;
13979
13980 gas_assert (strcmp (insn->name, str) == 0);
13981
13982 ok = is_opcode_valid_16 (insn);
13983 if (! ok)
13984 {
13985 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13986 && strcmp (insn->name, insn[1].name) == 0)
13987 {
13988 ++insn;
13989 continue;
13990 }
13991 else
13992 {
13993 if (!insn_error)
13994 {
13995 static char buf[100];
13996 sprintf (buf,
13997 _("Opcode not supported on this processor: %s (%s)"),
13998 mips_cpu_info_from_arch (mips_opts.arch)->name,
13999 mips_cpu_info_from_isa (mips_opts.isa)->name);
14000 insn_error = buf;
14001 }
14002 return;
14003 }
14004 }
14005
14006 create_insn (ip, insn);
14007 imm_expr.X_op = O_absent;
14008 imm_reloc[0] = BFD_RELOC_UNUSED;
14009 imm_reloc[1] = BFD_RELOC_UNUSED;
14010 imm_reloc[2] = BFD_RELOC_UNUSED;
14011 imm2_expr.X_op = O_absent;
14012 offset_expr.X_op = O_absent;
14013 offset_reloc[0] = BFD_RELOC_UNUSED;
14014 offset_reloc[1] = BFD_RELOC_UNUSED;
14015 offset_reloc[2] = BFD_RELOC_UNUSED;
14016 for (args = insn->args; 1; ++args)
14017 {
14018 int c;
14019
14020 if (*s == ' ')
14021 ++s;
14022
14023 /* In this switch statement we call break if we did not find
14024 a match, continue if we did find a match, or return if we
14025 are done. */
14026
14027 c = *args;
14028 switch (c)
14029 {
14030 case '\0':
14031 if (*s == '\0')
14032 {
14033 offsetT value;
14034
14035 /* Stuff the immediate value in now, if we can. */
14036 if (imm_expr.X_op == O_constant
14037 && *imm_reloc > BFD_RELOC_UNUSED
14038 && insn->pinfo != INSN_MACRO
14039 && calculate_reloc (*offset_reloc,
14040 imm_expr.X_add_number, &value))
14041 {
14042 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
14043 *offset_reloc, value, forced_insn_length,
14044 &ip->insn_opcode);
14045 imm_expr.X_op = O_absent;
14046 *imm_reloc = BFD_RELOC_UNUSED;
14047 *offset_reloc = BFD_RELOC_UNUSED;
14048 }
14049
14050 return;
14051 }
14052 break;
14053
14054 case ',':
14055 if (*s++ == c)
14056 continue;
14057 s--;
14058 switch (*++args)
14059 {
14060 case 'v':
14061 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
14062 continue;
14063 case 'w':
14064 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
14065 continue;
14066 }
14067 break;
14068
14069 case '(':
14070 case ')':
14071 if (*s++ == c)
14072 continue;
14073 break;
14074
14075 case 'v':
14076 case 'w':
14077 if (s[0] != '$')
14078 {
14079 if (c == 'v')
14080 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
14081 else
14082 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
14083 ++args;
14084 continue;
14085 }
14086 /* Fall through. */
14087 case 'x':
14088 case 'y':
14089 case 'z':
14090 case 'Z':
14091 case '0':
14092 case 'S':
14093 case 'R':
14094 case 'X':
14095 case 'Y':
14096 s_reset = s;
14097 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
14098 {
14099 if (c == 'v' || c == 'w')
14100 {
14101 if (c == 'v')
14102 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
14103 else
14104 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
14105 ++args;
14106 continue;
14107 }
14108 break;
14109 }
14110
14111 if (*s == ' ')
14112 ++s;
14113 if (args[1] != *s)
14114 {
14115 if (c == 'v' || c == 'w')
14116 {
14117 regno = mips16_to_32_reg_map[lastregno];
14118 s = s_reset;
14119 ++args;
14120 }
14121 }
14122
14123 switch (c)
14124 {
14125 case 'x':
14126 case 'y':
14127 case 'z':
14128 case 'v':
14129 case 'w':
14130 case 'Z':
14131 regno = mips32_to_16_reg_map[regno];
14132 break;
14133
14134 case '0':
14135 if (regno != 0)
14136 regno = ILLEGAL_REG;
14137 break;
14138
14139 case 'S':
14140 if (regno != SP)
14141 regno = ILLEGAL_REG;
14142 break;
14143
14144 case 'R':
14145 if (regno != RA)
14146 regno = ILLEGAL_REG;
14147 break;
14148
14149 case 'X':
14150 case 'Y':
14151 if (regno == AT && mips_opts.at)
14152 {
14153 if (mips_opts.at == ATREG)
14154 as_warn (_("used $at without \".set noat\""));
14155 else
14156 as_warn (_("used $%u with \".set at=$%u\""),
14157 regno, mips_opts.at);
14158 }
14159 break;
14160
14161 default:
14162 abort ();
14163 }
14164
14165 if (regno == ILLEGAL_REG)
14166 break;
14167
14168 switch (c)
14169 {
14170 case 'x':
14171 case 'v':
14172 MIPS16_INSERT_OPERAND (RX, *ip, regno);
14173 break;
14174 case 'y':
14175 case 'w':
14176 MIPS16_INSERT_OPERAND (RY, *ip, regno);
14177 break;
14178 case 'z':
14179 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
14180 break;
14181 case 'Z':
14182 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
14183 case '0':
14184 case 'S':
14185 case 'R':
14186 break;
14187 case 'X':
14188 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
14189 break;
14190 case 'Y':
14191 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
14192 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
14193 break;
14194 default:
14195 abort ();
14196 }
14197
14198 lastregno = regno;
14199 continue;
14200
14201 case 'P':
14202 if (strncmp (s, "$pc", 3) == 0)
14203 {
14204 s += 3;
14205 continue;
14206 }
14207 break;
14208
14209 case '5':
14210 case 'H':
14211 case 'W':
14212 case 'D':
14213 case 'j':
14214 case 'V':
14215 case 'C':
14216 case 'U':
14217 case 'k':
14218 case 'K':
14219 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
14220 if (i > 0)
14221 {
14222 if (imm_expr.X_op != O_constant)
14223 {
14224 forced_insn_length = 4;
14225 ip->insn_opcode |= MIPS16_EXTEND;
14226 }
14227 else
14228 {
14229 /* We need to relax this instruction. */
14230 *offset_reloc = *imm_reloc;
14231 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
14232 }
14233 s = expr_end;
14234 continue;
14235 }
14236 *imm_reloc = BFD_RELOC_UNUSED;
14237 /* Fall through. */
14238 case '<':
14239 case '>':
14240 case '[':
14241 case ']':
14242 case '4':
14243 case '8':
14244 my_getExpression (&imm_expr, s);
14245 if (imm_expr.X_op == O_register)
14246 {
14247 /* What we thought was an expression turned out to
14248 be a register. */
14249
14250 if (s[0] == '(' && args[1] == '(')
14251 {
14252 /* It looks like the expression was omitted
14253 before a register indirection, which means
14254 that the expression is implicitly zero. We
14255 still set up imm_expr, so that we handle
14256 explicit extensions correctly. */
14257 imm_expr.X_op = O_constant;
14258 imm_expr.X_add_number = 0;
14259 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
14260 continue;
14261 }
14262
14263 break;
14264 }
14265
14266 /* We need to relax this instruction. */
14267 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
14268 s = expr_end;
14269 continue;
14270
14271 case 'p':
14272 case 'q':
14273 case 'A':
14274 case 'B':
14275 case 'E':
14276 /* We use offset_reloc rather than imm_reloc for the PC
14277 relative operands. This lets macros with both
14278 immediate and address operands work correctly. */
14279 my_getExpression (&offset_expr, s);
14280
14281 if (offset_expr.X_op == O_register)
14282 break;
14283
14284 /* We need to relax this instruction. */
14285 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
14286 s = expr_end;
14287 continue;
14288
14289 case '6': /* break code */
14290 my_getExpression (&imm_expr, s);
14291 check_absolute_expr (ip, &imm_expr);
14292 if ((unsigned long) imm_expr.X_add_number > 63)
14293 as_warn (_("Invalid value for `%s' (%lu)"),
14294 ip->insn_mo->name,
14295 (unsigned long) imm_expr.X_add_number);
14296 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
14297 imm_expr.X_op = O_absent;
14298 s = expr_end;
14299 continue;
14300
14301 case 'a': /* 26 bit address */
14302 my_getExpression (&offset_expr, s);
14303 s = expr_end;
14304 *offset_reloc = BFD_RELOC_MIPS16_JMP;
14305 ip->insn_opcode <<= 16;
14306 continue;
14307
14308 case 'l': /* register list for entry macro */
14309 case 'L': /* register list for exit macro */
14310 {
14311 int mask;
14312
14313 if (c == 'l')
14314 mask = 0;
14315 else
14316 mask = 7 << 3;
14317 while (*s != '\0')
14318 {
14319 unsigned int freg, reg1, reg2;
14320
14321 while (*s == ' ' || *s == ',')
14322 ++s;
14323 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
14324 freg = 0;
14325 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
14326 freg = 1;
14327 else
14328 {
14329 as_bad (_("can't parse register list"));
14330 break;
14331 }
14332 if (*s == ' ')
14333 ++s;
14334 if (*s != '-')
14335 reg2 = reg1;
14336 else
14337 {
14338 ++s;
14339 if (!reg_lookup (&s, freg ? RTYPE_FPU
14340 : (RTYPE_GP | RTYPE_NUM), &reg2))
14341 {
14342 as_bad (_("invalid register list"));
14343 break;
14344 }
14345 }
14346 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
14347 {
14348 mask &= ~ (7 << 3);
14349 mask |= 5 << 3;
14350 }
14351 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
14352 {
14353 mask &= ~ (7 << 3);
14354 mask |= 6 << 3;
14355 }
14356 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
14357 mask |= (reg2 - 3) << 3;
14358 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
14359 mask |= (reg2 - 15) << 1;
14360 else if (reg1 == RA && reg2 == RA)
14361 mask |= 1;
14362 else
14363 {
14364 as_bad (_("invalid register list"));
14365 break;
14366 }
14367 }
14368 /* The mask is filled in in the opcode table for the
14369 benefit of the disassembler. We remove it before
14370 applying the actual mask. */
14371 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
14372 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
14373 }
14374 continue;
14375
14376 case 'm': /* Register list for save insn. */
14377 case 'M': /* Register list for restore insn. */
14378 {
14379 int opcode = ip->insn_opcode;
14380 int framesz = 0, seen_framesz = 0;
14381 int nargs = 0, statics = 0, sregs = 0;
14382
14383 while (*s != '\0')
14384 {
14385 unsigned int reg1, reg2;
14386
14387 SKIP_SPACE_TABS (s);
14388 while (*s == ',')
14389 ++s;
14390 SKIP_SPACE_TABS (s);
14391
14392 my_getExpression (&imm_expr, s);
14393 if (imm_expr.X_op == O_constant)
14394 {
14395 /* Handle the frame size. */
14396 if (seen_framesz)
14397 {
14398 as_bad (_("more than one frame size in list"));
14399 break;
14400 }
14401 seen_framesz = 1;
14402 framesz = imm_expr.X_add_number;
14403 imm_expr.X_op = O_absent;
14404 s = expr_end;
14405 continue;
14406 }
14407
14408 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
14409 {
14410 as_bad (_("can't parse register list"));
14411 break;
14412 }
14413
14414 while (*s == ' ')
14415 ++s;
14416
14417 if (*s != '-')
14418 reg2 = reg1;
14419 else
14420 {
14421 ++s;
14422 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
14423 || reg2 < reg1)
14424 {
14425 as_bad (_("can't parse register list"));
14426 break;
14427 }
14428 }
14429
14430 while (reg1 <= reg2)
14431 {
14432 if (reg1 >= 4 && reg1 <= 7)
14433 {
14434 if (!seen_framesz)
14435 /* args $a0-$a3 */
14436 nargs |= 1 << (reg1 - 4);
14437 else
14438 /* statics $a0-$a3 */
14439 statics |= 1 << (reg1 - 4);
14440 }
14441 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
14442 {
14443 /* $s0-$s8 */
14444 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
14445 }
14446 else if (reg1 == 31)
14447 {
14448 /* Add $ra to insn. */
14449 opcode |= 0x40;
14450 }
14451 else
14452 {
14453 as_bad (_("unexpected register in list"));
14454 break;
14455 }
14456 if (++reg1 == 24)
14457 reg1 = 30;
14458 }
14459 }
14460
14461 /* Encode args/statics combination. */
14462 if (nargs & statics)
14463 as_bad (_("arg/static registers overlap"));
14464 else if (nargs == 0xf)
14465 /* All $a0-$a3 are args. */
14466 opcode |= MIPS16_ALL_ARGS << 16;
14467 else if (statics == 0xf)
14468 /* All $a0-$a3 are statics. */
14469 opcode |= MIPS16_ALL_STATICS << 16;
14470 else
14471 {
14472 int narg = 0, nstat = 0;
14473
14474 /* Count arg registers. */
14475 while (nargs & 0x1)
14476 {
14477 nargs >>= 1;
14478 narg++;
14479 }
14480 if (nargs != 0)
14481 as_bad (_("invalid arg register list"));
14482
14483 /* Count static registers. */
14484 while (statics & 0x8)
14485 {
14486 statics = (statics << 1) & 0xf;
14487 nstat++;
14488 }
14489 if (statics != 0)
14490 as_bad (_("invalid static register list"));
14491
14492 /* Encode args/statics. */
14493 opcode |= ((narg << 2) | nstat) << 16;
14494 }
14495
14496 /* Encode $s0/$s1. */
14497 if (sregs & (1 << 0)) /* $s0 */
14498 opcode |= 0x20;
14499 if (sregs & (1 << 1)) /* $s1 */
14500 opcode |= 0x10;
14501 sregs >>= 2;
14502
14503 if (sregs != 0)
14504 {
14505 /* Count regs $s2-$s8. */
14506 int nsreg = 0;
14507 while (sregs & 1)
14508 {
14509 sregs >>= 1;
14510 nsreg++;
14511 }
14512 if (sregs != 0)
14513 as_bad (_("invalid static register list"));
14514 /* Encode $s2-$s8. */
14515 opcode |= nsreg << 24;
14516 }
14517
14518 /* Encode frame size. */
14519 if (!seen_framesz)
14520 as_bad (_("missing frame size"));
14521 else if ((framesz & 7) != 0 || framesz < 0
14522 || framesz > 0xff * 8)
14523 as_bad (_("invalid frame size"));
14524 else if (framesz != 128 || (opcode >> 16) != 0)
14525 {
14526 framesz /= 8;
14527 opcode |= (((framesz & 0xf0) << 16)
14528 | (framesz & 0x0f));
14529 }
14530
14531 /* Finally build the instruction. */
14532 if ((opcode >> 16) != 0 || framesz == 0)
14533 opcode |= MIPS16_EXTEND;
14534 ip->insn_opcode = opcode;
14535 }
14536 continue;
14537
14538 case 'e': /* extend code */
14539 my_getExpression (&imm_expr, s);
14540 check_absolute_expr (ip, &imm_expr);
14541 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
14542 {
14543 as_warn (_("Invalid value for `%s' (%lu)"),
14544 ip->insn_mo->name,
14545 (unsigned long) imm_expr.X_add_number);
14546 imm_expr.X_add_number &= 0x7ff;
14547 }
14548 ip->insn_opcode |= imm_expr.X_add_number;
14549 imm_expr.X_op = O_absent;
14550 s = expr_end;
14551 continue;
14552
14553 default:
14554 abort ();
14555 }
14556 break;
14557 }
14558
14559 /* Args don't match. */
14560 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
14561 strcmp (insn->name, insn[1].name) == 0)
14562 {
14563 ++insn;
14564 s = argsstart;
14565 continue;
14566 }
14567
14568 insn_error = _("illegal operands");
14569
14570 return;
14571 }
14572 }
14573
14574 /* This structure holds information we know about a mips16 immediate
14575 argument type. */
14576
14577 struct mips16_immed_operand
14578 {
14579 /* The type code used in the argument string in the opcode table. */
14580 int type;
14581 /* The number of bits in the short form of the opcode. */
14582 int nbits;
14583 /* The number of bits in the extended form of the opcode. */
14584 int extbits;
14585 /* The amount by which the short form is shifted when it is used;
14586 for example, the sw instruction has a shift count of 2. */
14587 int shift;
14588 /* The amount by which the short form is shifted when it is stored
14589 into the instruction code. */
14590 int op_shift;
14591 /* Non-zero if the short form is unsigned. */
14592 int unsp;
14593 /* Non-zero if the extended form is unsigned. */
14594 int extu;
14595 /* Non-zero if the value is PC relative. */
14596 int pcrel;
14597 };
14598
14599 /* The mips16 immediate operand types. */
14600
14601 static const struct mips16_immed_operand mips16_immed_operands[] =
14602 {
14603 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14604 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14605 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14606 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14607 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
14608 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
14609 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
14610 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
14611 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
14612 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
14613 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
14614 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
14615 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
14616 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
14617 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
14618 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
14619 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14620 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14621 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
14622 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
14623 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
14624 };
14625
14626 #define MIPS16_NUM_IMMED \
14627 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
14628
14629 /* Marshal immediate value VAL for an extended MIPS16 instruction.
14630 NBITS is the number of significant bits in VAL. */
14631
14632 static unsigned long
14633 mips16_immed_extend (offsetT val, unsigned int nbits)
14634 {
14635 int extval;
14636 if (nbits == 16)
14637 {
14638 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14639 val &= 0x1f;
14640 }
14641 else if (nbits == 15)
14642 {
14643 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14644 val &= 0xf;
14645 }
14646 else
14647 {
14648 extval = ((val & 0x1f) << 6) | (val & 0x20);
14649 val = 0;
14650 }
14651 return (extval << 16) | val;
14652 }
14653
14654 /* Install immediate value VAL into MIPS16 instruction *INSN,
14655 extending it if necessary. The instruction in *INSN may
14656 already be extended.
14657
14658 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14659 if none. In the former case, VAL is a 16-bit number with no
14660 defined signedness.
14661
14662 TYPE is the type of the immediate field. USER_INSN_LENGTH
14663 is the length that the user requested, or 0 if none. */
14664
14665 static void
14666 mips16_immed (char *file, unsigned int line, int type,
14667 bfd_reloc_code_real_type reloc, offsetT val,
14668 unsigned int user_insn_length, unsigned long *insn)
14669 {
14670 const struct mips16_immed_operand *op;
14671 int mintiny, maxtiny;
14672
14673 op = mips16_immed_operands;
14674 while (op->type != type)
14675 {
14676 ++op;
14677 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
14678 }
14679
14680 if (op->unsp)
14681 {
14682 if (type == '<' || type == '>' || type == '[' || type == ']')
14683 {
14684 mintiny = 1;
14685 maxtiny = 1 << op->nbits;
14686 }
14687 else
14688 {
14689 mintiny = 0;
14690 maxtiny = (1 << op->nbits) - 1;
14691 }
14692 if (reloc != BFD_RELOC_UNUSED)
14693 val &= 0xffff;
14694 }
14695 else
14696 {
14697 mintiny = - (1 << (op->nbits - 1));
14698 maxtiny = (1 << (op->nbits - 1)) - 1;
14699 if (reloc != BFD_RELOC_UNUSED)
14700 val = SEXT_16BIT (val);
14701 }
14702
14703 /* Branch offsets have an implicit 0 in the lowest bit. */
14704 if (type == 'p' || type == 'q')
14705 val /= 2;
14706
14707 if ((val & ((1 << op->shift) - 1)) != 0
14708 || val < (mintiny << op->shift)
14709 || val > (maxtiny << op->shift))
14710 {
14711 /* We need an extended instruction. */
14712 if (user_insn_length == 2)
14713 as_bad_where (file, line, _("invalid unextended operand value"));
14714 else
14715 *insn |= MIPS16_EXTEND;
14716 }
14717 else if (user_insn_length == 4)
14718 {
14719 /* The operand doesn't force an unextended instruction to be extended.
14720 Warn if the user wanted an extended instruction anyway. */
14721 *insn |= MIPS16_EXTEND;
14722 as_warn_where (file, line,
14723 _("extended operand requested but not required"));
14724 }
14725
14726 if (mips16_opcode_length (*insn) == 2)
14727 {
14728 int insnval;
14729
14730 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14731 insnval <<= op->op_shift;
14732 *insn |= insnval;
14733 }
14734 else
14735 {
14736 long minext, maxext;
14737
14738 if (reloc == BFD_RELOC_UNUSED)
14739 {
14740 if (op->extu)
14741 {
14742 minext = 0;
14743 maxext = (1 << op->extbits) - 1;
14744 }
14745 else
14746 {
14747 minext = - (1 << (op->extbits - 1));
14748 maxext = (1 << (op->extbits - 1)) - 1;
14749 }
14750 if (val < minext || val > maxext)
14751 as_bad_where (file, line,
14752 _("operand value out of range for instruction"));
14753 }
14754
14755 *insn |= mips16_immed_extend (val, op->extbits);
14756 }
14757 }
14758 \f
14759 struct percent_op_match
14760 {
14761 const char *str;
14762 bfd_reloc_code_real_type reloc;
14763 };
14764
14765 static const struct percent_op_match mips_percent_op[] =
14766 {
14767 {"%lo", BFD_RELOC_LO16},
14768 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14769 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14770 {"%call16", BFD_RELOC_MIPS_CALL16},
14771 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14772 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14773 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14774 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14775 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14776 {"%got", BFD_RELOC_MIPS_GOT16},
14777 {"%gp_rel", BFD_RELOC_GPREL16},
14778 {"%half", BFD_RELOC_16},
14779 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14780 {"%higher", BFD_RELOC_MIPS_HIGHER},
14781 {"%neg", BFD_RELOC_MIPS_SUB},
14782 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14783 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14784 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14785 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14786 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14787 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14788 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14789 {"%hi", BFD_RELOC_HI16_S}
14790 };
14791
14792 static const struct percent_op_match mips16_percent_op[] =
14793 {
14794 {"%lo", BFD_RELOC_MIPS16_LO16},
14795 {"%gprel", BFD_RELOC_MIPS16_GPREL},
14796 {"%got", BFD_RELOC_MIPS16_GOT16},
14797 {"%call16", BFD_RELOC_MIPS16_CALL16},
14798 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14799 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14800 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14801 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14802 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14803 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14804 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14805 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14806 };
14807
14808
14809 /* Return true if *STR points to a relocation operator. When returning true,
14810 move *STR over the operator and store its relocation code in *RELOC.
14811 Leave both *STR and *RELOC alone when returning false. */
14812
14813 static bfd_boolean
14814 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14815 {
14816 const struct percent_op_match *percent_op;
14817 size_t limit, i;
14818
14819 if (mips_opts.mips16)
14820 {
14821 percent_op = mips16_percent_op;
14822 limit = ARRAY_SIZE (mips16_percent_op);
14823 }
14824 else
14825 {
14826 percent_op = mips_percent_op;
14827 limit = ARRAY_SIZE (mips_percent_op);
14828 }
14829
14830 for (i = 0; i < limit; i++)
14831 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14832 {
14833 int len = strlen (percent_op[i].str);
14834
14835 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14836 continue;
14837
14838 *str += strlen (percent_op[i].str);
14839 *reloc = percent_op[i].reloc;
14840
14841 /* Check whether the output BFD supports this relocation.
14842 If not, issue an error and fall back on something safe. */
14843 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14844 {
14845 as_bad (_("relocation %s isn't supported by the current ABI"),
14846 percent_op[i].str);
14847 *reloc = BFD_RELOC_UNUSED;
14848 }
14849 return TRUE;
14850 }
14851 return FALSE;
14852 }
14853
14854
14855 /* Parse string STR as a 16-bit relocatable operand. Store the
14856 expression in *EP and the relocations in the array starting
14857 at RELOC. Return the number of relocation operators used.
14858
14859 On exit, EXPR_END points to the first character after the expression. */
14860
14861 static size_t
14862 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14863 char *str)
14864 {
14865 bfd_reloc_code_real_type reversed_reloc[3];
14866 size_t reloc_index, i;
14867 int crux_depth, str_depth;
14868 char *crux;
14869
14870 /* Search for the start of the main expression, recoding relocations
14871 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14872 of the main expression and with CRUX_DEPTH containing the number
14873 of open brackets at that point. */
14874 reloc_index = -1;
14875 str_depth = 0;
14876 do
14877 {
14878 reloc_index++;
14879 crux = str;
14880 crux_depth = str_depth;
14881
14882 /* Skip over whitespace and brackets, keeping count of the number
14883 of brackets. */
14884 while (*str == ' ' || *str == '\t' || *str == '(')
14885 if (*str++ == '(')
14886 str_depth++;
14887 }
14888 while (*str == '%'
14889 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14890 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14891
14892 my_getExpression (ep, crux);
14893 str = expr_end;
14894
14895 /* Match every open bracket. */
14896 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14897 if (*str++ == ')')
14898 crux_depth--;
14899
14900 if (crux_depth > 0)
14901 as_bad (_("unclosed '('"));
14902
14903 expr_end = str;
14904
14905 if (reloc_index != 0)
14906 {
14907 prev_reloc_op_frag = frag_now;
14908 for (i = 0; i < reloc_index; i++)
14909 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14910 }
14911
14912 return reloc_index;
14913 }
14914
14915 static void
14916 my_getExpression (expressionS *ep, char *str)
14917 {
14918 char *save_in;
14919
14920 save_in = input_line_pointer;
14921 input_line_pointer = str;
14922 expression (ep);
14923 expr_end = input_line_pointer;
14924 input_line_pointer = save_in;
14925 }
14926
14927 char *
14928 md_atof (int type, char *litP, int *sizeP)
14929 {
14930 return ieee_md_atof (type, litP, sizeP, target_big_endian);
14931 }
14932
14933 void
14934 md_number_to_chars (char *buf, valueT val, int n)
14935 {
14936 if (target_big_endian)
14937 number_to_chars_bigendian (buf, val, n);
14938 else
14939 number_to_chars_littleendian (buf, val, n);
14940 }
14941 \f
14942 static int support_64bit_objects(void)
14943 {
14944 const char **list, **l;
14945 int yes;
14946
14947 list = bfd_target_list ();
14948 for (l = list; *l != NULL; l++)
14949 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14950 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14951 break;
14952 yes = (*l != NULL);
14953 free (list);
14954 return yes;
14955 }
14956
14957 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14958 NEW_VALUE. Warn if another value was already specified. Note:
14959 we have to defer parsing the -march and -mtune arguments in order
14960 to handle 'from-abi' correctly, since the ABI might be specified
14961 in a later argument. */
14962
14963 static void
14964 mips_set_option_string (const char **string_ptr, const char *new_value)
14965 {
14966 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14967 as_warn (_("A different %s was already specified, is now %s"),
14968 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14969 new_value);
14970
14971 *string_ptr = new_value;
14972 }
14973
14974 int
14975 md_parse_option (int c, char *arg)
14976 {
14977 unsigned int i;
14978
14979 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14980 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14981 {
14982 file_ase_explicit |= mips_set_ase (&mips_ases[i],
14983 c == mips_ases[i].option_on);
14984 return 1;
14985 }
14986
14987 switch (c)
14988 {
14989 case OPTION_CONSTRUCT_FLOATS:
14990 mips_disable_float_construction = 0;
14991 break;
14992
14993 case OPTION_NO_CONSTRUCT_FLOATS:
14994 mips_disable_float_construction = 1;
14995 break;
14996
14997 case OPTION_TRAP:
14998 mips_trap = 1;
14999 break;
15000
15001 case OPTION_BREAK:
15002 mips_trap = 0;
15003 break;
15004
15005 case OPTION_EB:
15006 target_big_endian = 1;
15007 break;
15008
15009 case OPTION_EL:
15010 target_big_endian = 0;
15011 break;
15012
15013 case 'O':
15014 if (arg == NULL)
15015 mips_optimize = 1;
15016 else if (arg[0] == '0')
15017 mips_optimize = 0;
15018 else if (arg[0] == '1')
15019 mips_optimize = 1;
15020 else
15021 mips_optimize = 2;
15022 break;
15023
15024 case 'g':
15025 if (arg == NULL)
15026 mips_debug = 2;
15027 else
15028 mips_debug = atoi (arg);
15029 break;
15030
15031 case OPTION_MIPS1:
15032 file_mips_isa = ISA_MIPS1;
15033 break;
15034
15035 case OPTION_MIPS2:
15036 file_mips_isa = ISA_MIPS2;
15037 break;
15038
15039 case OPTION_MIPS3:
15040 file_mips_isa = ISA_MIPS3;
15041 break;
15042
15043 case OPTION_MIPS4:
15044 file_mips_isa = ISA_MIPS4;
15045 break;
15046
15047 case OPTION_MIPS5:
15048 file_mips_isa = ISA_MIPS5;
15049 break;
15050
15051 case OPTION_MIPS32:
15052 file_mips_isa = ISA_MIPS32;
15053 break;
15054
15055 case OPTION_MIPS32R2:
15056 file_mips_isa = ISA_MIPS32R2;
15057 break;
15058
15059 case OPTION_MIPS64R2:
15060 file_mips_isa = ISA_MIPS64R2;
15061 break;
15062
15063 case OPTION_MIPS64:
15064 file_mips_isa = ISA_MIPS64;
15065 break;
15066
15067 case OPTION_MTUNE:
15068 mips_set_option_string (&mips_tune_string, arg);
15069 break;
15070
15071 case OPTION_MARCH:
15072 mips_set_option_string (&mips_arch_string, arg);
15073 break;
15074
15075 case OPTION_M4650:
15076 mips_set_option_string (&mips_arch_string, "4650");
15077 mips_set_option_string (&mips_tune_string, "4650");
15078 break;
15079
15080 case OPTION_NO_M4650:
15081 break;
15082
15083 case OPTION_M4010:
15084 mips_set_option_string (&mips_arch_string, "4010");
15085 mips_set_option_string (&mips_tune_string, "4010");
15086 break;
15087
15088 case OPTION_NO_M4010:
15089 break;
15090
15091 case OPTION_M4100:
15092 mips_set_option_string (&mips_arch_string, "4100");
15093 mips_set_option_string (&mips_tune_string, "4100");
15094 break;
15095
15096 case OPTION_NO_M4100:
15097 break;
15098
15099 case OPTION_M3900:
15100 mips_set_option_string (&mips_arch_string, "3900");
15101 mips_set_option_string (&mips_tune_string, "3900");
15102 break;
15103
15104 case OPTION_NO_M3900:
15105 break;
15106
15107 case OPTION_MICROMIPS:
15108 if (mips_opts.mips16 == 1)
15109 {
15110 as_bad (_("-mmicromips cannot be used with -mips16"));
15111 return 0;
15112 }
15113 mips_opts.micromips = 1;
15114 mips_no_prev_insn ();
15115 break;
15116
15117 case OPTION_NO_MICROMIPS:
15118 mips_opts.micromips = 0;
15119 mips_no_prev_insn ();
15120 break;
15121
15122 case OPTION_MIPS16:
15123 if (mips_opts.micromips == 1)
15124 {
15125 as_bad (_("-mips16 cannot be used with -micromips"));
15126 return 0;
15127 }
15128 mips_opts.mips16 = 1;
15129 mips_no_prev_insn ();
15130 break;
15131
15132 case OPTION_NO_MIPS16:
15133 mips_opts.mips16 = 0;
15134 mips_no_prev_insn ();
15135 break;
15136
15137 case OPTION_FIX_24K:
15138 mips_fix_24k = 1;
15139 break;
15140
15141 case OPTION_NO_FIX_24K:
15142 mips_fix_24k = 0;
15143 break;
15144
15145 case OPTION_FIX_LOONGSON2F_JUMP:
15146 mips_fix_loongson2f_jump = TRUE;
15147 break;
15148
15149 case OPTION_NO_FIX_LOONGSON2F_JUMP:
15150 mips_fix_loongson2f_jump = FALSE;
15151 break;
15152
15153 case OPTION_FIX_LOONGSON2F_NOP:
15154 mips_fix_loongson2f_nop = TRUE;
15155 break;
15156
15157 case OPTION_NO_FIX_LOONGSON2F_NOP:
15158 mips_fix_loongson2f_nop = FALSE;
15159 break;
15160
15161 case OPTION_FIX_VR4120:
15162 mips_fix_vr4120 = 1;
15163 break;
15164
15165 case OPTION_NO_FIX_VR4120:
15166 mips_fix_vr4120 = 0;
15167 break;
15168
15169 case OPTION_FIX_VR4130:
15170 mips_fix_vr4130 = 1;
15171 break;
15172
15173 case OPTION_NO_FIX_VR4130:
15174 mips_fix_vr4130 = 0;
15175 break;
15176
15177 case OPTION_FIX_CN63XXP1:
15178 mips_fix_cn63xxp1 = TRUE;
15179 break;
15180
15181 case OPTION_NO_FIX_CN63XXP1:
15182 mips_fix_cn63xxp1 = FALSE;
15183 break;
15184
15185 case OPTION_RELAX_BRANCH:
15186 mips_relax_branch = 1;
15187 break;
15188
15189 case OPTION_NO_RELAX_BRANCH:
15190 mips_relax_branch = 0;
15191 break;
15192
15193 case OPTION_INSN32:
15194 mips_opts.insn32 = TRUE;
15195 break;
15196
15197 case OPTION_NO_INSN32:
15198 mips_opts.insn32 = FALSE;
15199 break;
15200
15201 case OPTION_MSHARED:
15202 mips_in_shared = TRUE;
15203 break;
15204
15205 case OPTION_MNO_SHARED:
15206 mips_in_shared = FALSE;
15207 break;
15208
15209 case OPTION_MSYM32:
15210 mips_opts.sym32 = TRUE;
15211 break;
15212
15213 case OPTION_MNO_SYM32:
15214 mips_opts.sym32 = FALSE;
15215 break;
15216
15217 /* When generating ELF code, we permit -KPIC and -call_shared to
15218 select SVR4_PIC, and -non_shared to select no PIC. This is
15219 intended to be compatible with Irix 5. */
15220 case OPTION_CALL_SHARED:
15221 mips_pic = SVR4_PIC;
15222 mips_abicalls = TRUE;
15223 break;
15224
15225 case OPTION_CALL_NONPIC:
15226 mips_pic = NO_PIC;
15227 mips_abicalls = TRUE;
15228 break;
15229
15230 case OPTION_NON_SHARED:
15231 mips_pic = NO_PIC;
15232 mips_abicalls = FALSE;
15233 break;
15234
15235 /* The -xgot option tells the assembler to use 32 bit offsets
15236 when accessing the got in SVR4_PIC mode. It is for Irix
15237 compatibility. */
15238 case OPTION_XGOT:
15239 mips_big_got = 1;
15240 break;
15241
15242 case 'G':
15243 g_switch_value = atoi (arg);
15244 g_switch_seen = 1;
15245 break;
15246
15247 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15248 and -mabi=64. */
15249 case OPTION_32:
15250 mips_abi = O32_ABI;
15251 break;
15252
15253 case OPTION_N32:
15254 mips_abi = N32_ABI;
15255 break;
15256
15257 case OPTION_64:
15258 mips_abi = N64_ABI;
15259 if (!support_64bit_objects())
15260 as_fatal (_("No compiled in support for 64 bit object file format"));
15261 break;
15262
15263 case OPTION_GP32:
15264 file_mips_gp32 = 1;
15265 break;
15266
15267 case OPTION_GP64:
15268 file_mips_gp32 = 0;
15269 break;
15270
15271 case OPTION_FP32:
15272 file_mips_fp32 = 1;
15273 break;
15274
15275 case OPTION_FP64:
15276 file_mips_fp32 = 0;
15277 break;
15278
15279 case OPTION_SINGLE_FLOAT:
15280 file_mips_single_float = 1;
15281 break;
15282
15283 case OPTION_DOUBLE_FLOAT:
15284 file_mips_single_float = 0;
15285 break;
15286
15287 case OPTION_SOFT_FLOAT:
15288 file_mips_soft_float = 1;
15289 break;
15290
15291 case OPTION_HARD_FLOAT:
15292 file_mips_soft_float = 0;
15293 break;
15294
15295 case OPTION_MABI:
15296 if (strcmp (arg, "32") == 0)
15297 mips_abi = O32_ABI;
15298 else if (strcmp (arg, "o64") == 0)
15299 mips_abi = O64_ABI;
15300 else if (strcmp (arg, "n32") == 0)
15301 mips_abi = N32_ABI;
15302 else if (strcmp (arg, "64") == 0)
15303 {
15304 mips_abi = N64_ABI;
15305 if (! support_64bit_objects())
15306 as_fatal (_("No compiled in support for 64 bit object file "
15307 "format"));
15308 }
15309 else if (strcmp (arg, "eabi") == 0)
15310 mips_abi = EABI_ABI;
15311 else
15312 {
15313 as_fatal (_("invalid abi -mabi=%s"), arg);
15314 return 0;
15315 }
15316 break;
15317
15318 case OPTION_M7000_HILO_FIX:
15319 mips_7000_hilo_fix = TRUE;
15320 break;
15321
15322 case OPTION_MNO_7000_HILO_FIX:
15323 mips_7000_hilo_fix = FALSE;
15324 break;
15325
15326 case OPTION_MDEBUG:
15327 mips_flag_mdebug = TRUE;
15328 break;
15329
15330 case OPTION_NO_MDEBUG:
15331 mips_flag_mdebug = FALSE;
15332 break;
15333
15334 case OPTION_PDR:
15335 mips_flag_pdr = TRUE;
15336 break;
15337
15338 case OPTION_NO_PDR:
15339 mips_flag_pdr = FALSE;
15340 break;
15341
15342 case OPTION_MVXWORKS_PIC:
15343 mips_pic = VXWORKS_PIC;
15344 break;
15345
15346 default:
15347 return 0;
15348 }
15349
15350 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15351
15352 return 1;
15353 }
15354 \f
15355 /* Set up globals to generate code for the ISA or processor
15356 described by INFO. */
15357
15358 static void
15359 mips_set_architecture (const struct mips_cpu_info *info)
15360 {
15361 if (info != 0)
15362 {
15363 file_mips_arch = info->cpu;
15364 mips_opts.arch = info->cpu;
15365 mips_opts.isa = info->isa;
15366 }
15367 }
15368
15369
15370 /* Likewise for tuning. */
15371
15372 static void
15373 mips_set_tune (const struct mips_cpu_info *info)
15374 {
15375 if (info != 0)
15376 mips_tune = info->cpu;
15377 }
15378
15379
15380 void
15381 mips_after_parse_args (void)
15382 {
15383 const struct mips_cpu_info *arch_info = 0;
15384 const struct mips_cpu_info *tune_info = 0;
15385
15386 /* GP relative stuff not working for PE */
15387 if (strncmp (TARGET_OS, "pe", 2) == 0)
15388 {
15389 if (g_switch_seen && g_switch_value != 0)
15390 as_bad (_("-G not supported in this configuration."));
15391 g_switch_value = 0;
15392 }
15393
15394 if (mips_abi == NO_ABI)
15395 mips_abi = MIPS_DEFAULT_ABI;
15396
15397 /* The following code determines the architecture and register size.
15398 Similar code was added to GCC 3.3 (see override_options() in
15399 config/mips/mips.c). The GAS and GCC code should be kept in sync
15400 as much as possible. */
15401
15402 if (mips_arch_string != 0)
15403 arch_info = mips_parse_cpu ("-march", mips_arch_string);
15404
15405 if (file_mips_isa != ISA_UNKNOWN)
15406 {
15407 /* Handle -mipsN. At this point, file_mips_isa contains the
15408 ISA level specified by -mipsN, while arch_info->isa contains
15409 the -march selection (if any). */
15410 if (arch_info != 0)
15411 {
15412 /* -march takes precedence over -mipsN, since it is more descriptive.
15413 There's no harm in specifying both as long as the ISA levels
15414 are the same. */
15415 if (file_mips_isa != arch_info->isa)
15416 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15417 mips_cpu_info_from_isa (file_mips_isa)->name,
15418 mips_cpu_info_from_isa (arch_info->isa)->name);
15419 }
15420 else
15421 arch_info = mips_cpu_info_from_isa (file_mips_isa);
15422 }
15423
15424 if (arch_info == 0)
15425 {
15426 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15427 gas_assert (arch_info);
15428 }
15429
15430 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
15431 as_bad (_("-march=%s is not compatible with the selected ABI"),
15432 arch_info->name);
15433
15434 mips_set_architecture (arch_info);
15435
15436 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
15437 if (mips_tune_string != 0)
15438 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
15439
15440 if (tune_info == 0)
15441 mips_set_tune (arch_info);
15442 else
15443 mips_set_tune (tune_info);
15444
15445 if (file_mips_gp32 >= 0)
15446 {
15447 /* The user specified the size of the integer registers. Make sure
15448 it agrees with the ABI and ISA. */
15449 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15450 as_bad (_("-mgp64 used with a 32-bit processor"));
15451 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15452 as_bad (_("-mgp32 used with a 64-bit ABI"));
15453 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15454 as_bad (_("-mgp64 used with a 32-bit ABI"));
15455 }
15456 else
15457 {
15458 /* Infer the integer register size from the ABI and processor.
15459 Restrict ourselves to 32-bit registers if that's all the
15460 processor has, or if the ABI cannot handle 64-bit registers. */
15461 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15462 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
15463 }
15464
15465 switch (file_mips_fp32)
15466 {
15467 default:
15468 case -1:
15469 /* No user specified float register size.
15470 ??? GAS treats single-float processors as though they had 64-bit
15471 float registers (although it complains when double-precision
15472 instructions are used). As things stand, saying they have 32-bit
15473 registers would lead to spurious "register must be even" messages.
15474 So here we assume float registers are never smaller than the
15475 integer ones. */
15476 if (file_mips_gp32 == 0)
15477 /* 64-bit integer registers implies 64-bit float registers. */
15478 file_mips_fp32 = 0;
15479 else if ((mips_opts.ase & FP64_ASES)
15480 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15481 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
15482 file_mips_fp32 = 0;
15483 else
15484 /* 32-bit float registers. */
15485 file_mips_fp32 = 1;
15486 break;
15487
15488 /* The user specified the size of the float registers. Check if it
15489 agrees with the ABI and ISA. */
15490 case 0:
15491 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15492 as_bad (_("-mfp64 used with a 32-bit fpu"));
15493 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15494 && !ISA_HAS_MXHC1 (mips_opts.isa))
15495 as_warn (_("-mfp64 used with a 32-bit ABI"));
15496 break;
15497 case 1:
15498 if (ABI_NEEDS_64BIT_REGS (mips_abi))
15499 as_warn (_("-mfp32 used with a 64-bit ABI"));
15500 break;
15501 }
15502
15503 /* End of GCC-shared inference code. */
15504
15505 /* This flag is set when we have a 64-bit capable CPU but use only
15506 32-bit wide registers. Note that EABI does not use it. */
15507 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15508 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15509 || mips_abi == O32_ABI))
15510 mips_32bitmode = 1;
15511
15512 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15513 as_bad (_("trap exception not supported at ISA 1"));
15514
15515 /* If the selected architecture includes support for ASEs, enable
15516 generation of code for them. */
15517 if (mips_opts.mips16 == -1)
15518 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
15519 if (mips_opts.micromips == -1)
15520 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
15521
15522 /* MIPS3D and MDMX require 64-bit FPRs, so -mfp32 should stop those
15523 ASEs from being selected implicitly. */
15524 if (file_mips_fp32 == 1)
15525 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX;
15526
15527 /* If the user didn't explicitly select or deselect a particular ASE,
15528 use the default setting for the CPU. */
15529 mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
15530
15531 file_mips_isa = mips_opts.isa;
15532 file_ase = mips_opts.ase;
15533 mips_opts.gp32 = file_mips_gp32;
15534 mips_opts.fp32 = file_mips_fp32;
15535 mips_opts.soft_float = file_mips_soft_float;
15536 mips_opts.single_float = file_mips_single_float;
15537
15538 mips_check_isa_supports_ases ();
15539
15540 if (mips_flag_mdebug < 0)
15541 mips_flag_mdebug = 0;
15542 }
15543 \f
15544 void
15545 mips_init_after_args (void)
15546 {
15547 /* initialize opcodes */
15548 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15549 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15550 }
15551
15552 long
15553 md_pcrel_from (fixS *fixP)
15554 {
15555 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15556 switch (fixP->fx_r_type)
15557 {
15558 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15559 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15560 /* Return the address of the delay slot. */
15561 return addr + 2;
15562
15563 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15564 case BFD_RELOC_MICROMIPS_JMP:
15565 case BFD_RELOC_16_PCREL_S2:
15566 case BFD_RELOC_MIPS_JMP:
15567 /* Return the address of the delay slot. */
15568 return addr + 4;
15569
15570 case BFD_RELOC_32_PCREL:
15571 return addr;
15572
15573 default:
15574 /* We have no relocation type for PC relative MIPS16 instructions. */
15575 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15576 as_bad_where (fixP->fx_file, fixP->fx_line,
15577 _("PC relative MIPS16 instruction references a different section"));
15578 return addr;
15579 }
15580 }
15581
15582 /* This is called before the symbol table is processed. In order to
15583 work with gcc when using mips-tfile, we must keep all local labels.
15584 However, in other cases, we want to discard them. If we were
15585 called with -g, but we didn't see any debugging information, it may
15586 mean that gcc is smuggling debugging information through to
15587 mips-tfile, in which case we must generate all local labels. */
15588
15589 void
15590 mips_frob_file_before_adjust (void)
15591 {
15592 #ifndef NO_ECOFF_DEBUGGING
15593 if (ECOFF_DEBUGGING
15594 && mips_debug != 0
15595 && ! ecoff_debugging_seen)
15596 flag_keep_locals = 1;
15597 #endif
15598 }
15599
15600 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15601 the corresponding LO16 reloc. This is called before md_apply_fix and
15602 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15603 relocation operators.
15604
15605 For our purposes, a %lo() expression matches a %got() or %hi()
15606 expression if:
15607
15608 (a) it refers to the same symbol; and
15609 (b) the offset applied in the %lo() expression is no lower than
15610 the offset applied in the %got() or %hi().
15611
15612 (b) allows us to cope with code like:
15613
15614 lui $4,%hi(foo)
15615 lh $4,%lo(foo+2)($4)
15616
15617 ...which is legal on RELA targets, and has a well-defined behaviour
15618 if the user knows that adding 2 to "foo" will not induce a carry to
15619 the high 16 bits.
15620
15621 When several %lo()s match a particular %got() or %hi(), we use the
15622 following rules to distinguish them:
15623
15624 (1) %lo()s with smaller offsets are a better match than %lo()s with
15625 higher offsets.
15626
15627 (2) %lo()s with no matching %got() or %hi() are better than those
15628 that already have a matching %got() or %hi().
15629
15630 (3) later %lo()s are better than earlier %lo()s.
15631
15632 These rules are applied in order.
15633
15634 (1) means, among other things, that %lo()s with identical offsets are
15635 chosen if they exist.
15636
15637 (2) means that we won't associate several high-part relocations with
15638 the same low-part relocation unless there's no alternative. Having
15639 several high parts for the same low part is a GNU extension; this rule
15640 allows careful users to avoid it.
15641
15642 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15643 with the last high-part relocation being at the front of the list.
15644 It therefore makes sense to choose the last matching low-part
15645 relocation, all other things being equal. It's also easier
15646 to code that way. */
15647
15648 void
15649 mips_frob_file (void)
15650 {
15651 struct mips_hi_fixup *l;
15652 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15653
15654 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15655 {
15656 segment_info_type *seginfo;
15657 bfd_boolean matched_lo_p;
15658 fixS **hi_pos, **lo_pos, **pos;
15659
15660 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15661
15662 /* If a GOT16 relocation turns out to be against a global symbol,
15663 there isn't supposed to be a matching LO. Ignore %gots against
15664 constants; we'll report an error for those later. */
15665 if (got16_reloc_p (l->fixp->fx_r_type)
15666 && !(l->fixp->fx_addsy
15667 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
15668 continue;
15669
15670 /* Check quickly whether the next fixup happens to be a matching %lo. */
15671 if (fixup_has_matching_lo_p (l->fixp))
15672 continue;
15673
15674 seginfo = seg_info (l->seg);
15675
15676 /* Set HI_POS to the position of this relocation in the chain.
15677 Set LO_POS to the position of the chosen low-part relocation.
15678 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15679 relocation that matches an immediately-preceding high-part
15680 relocation. */
15681 hi_pos = NULL;
15682 lo_pos = NULL;
15683 matched_lo_p = FALSE;
15684 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15685
15686 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15687 {
15688 if (*pos == l->fixp)
15689 hi_pos = pos;
15690
15691 if ((*pos)->fx_r_type == looking_for_rtype
15692 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15693 && (*pos)->fx_offset >= l->fixp->fx_offset
15694 && (lo_pos == NULL
15695 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15696 || (!matched_lo_p
15697 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15698 lo_pos = pos;
15699
15700 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15701 && fixup_has_matching_lo_p (*pos));
15702 }
15703
15704 /* If we found a match, remove the high-part relocation from its
15705 current position and insert it before the low-part relocation.
15706 Make the offsets match so that fixup_has_matching_lo_p()
15707 will return true.
15708
15709 We don't warn about unmatched high-part relocations since some
15710 versions of gcc have been known to emit dead "lui ...%hi(...)"
15711 instructions. */
15712 if (lo_pos != NULL)
15713 {
15714 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15715 if (l->fixp->fx_next != *lo_pos)
15716 {
15717 *hi_pos = l->fixp->fx_next;
15718 l->fixp->fx_next = *lo_pos;
15719 *lo_pos = l->fixp;
15720 }
15721 }
15722 }
15723 }
15724
15725 int
15726 mips_force_relocation (fixS *fixp)
15727 {
15728 if (generic_force_reloc (fixp))
15729 return 1;
15730
15731 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15732 so that the linker relaxation can update targets. */
15733 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15734 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15735 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15736 return 1;
15737
15738 return 0;
15739 }
15740
15741 /* Read the instruction associated with RELOC from BUF. */
15742
15743 static unsigned int
15744 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15745 {
15746 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15747 return read_compressed_insn (buf, 4);
15748 else
15749 return read_insn (buf);
15750 }
15751
15752 /* Write instruction INSN to BUF, given that it has been relocated
15753 by RELOC. */
15754
15755 static void
15756 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15757 unsigned long insn)
15758 {
15759 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15760 write_compressed_insn (buf, insn, 4);
15761 else
15762 write_insn (buf, insn);
15763 }
15764
15765 /* Apply a fixup to the object file. */
15766
15767 void
15768 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15769 {
15770 char *buf;
15771 unsigned long insn;
15772 reloc_howto_type *howto;
15773
15774 /* We ignore generic BFD relocations we don't know about. */
15775 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15776 if (! howto)
15777 return;
15778
15779 gas_assert (fixP->fx_size == 2
15780 || fixP->fx_size == 4
15781 || fixP->fx_r_type == BFD_RELOC_16
15782 || fixP->fx_r_type == BFD_RELOC_64
15783 || fixP->fx_r_type == BFD_RELOC_CTOR
15784 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15785 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15786 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15787 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15788 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
15789
15790 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15791
15792 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15793 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15794 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15795 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
15796 || fixP->fx_r_type == BFD_RELOC_32_PCREL);
15797
15798 /* Don't treat parts of a composite relocation as done. There are two
15799 reasons for this:
15800
15801 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15802 should nevertheless be emitted if the first part is.
15803
15804 (2) In normal usage, composite relocations are never assembly-time
15805 constants. The easiest way of dealing with the pathological
15806 exceptions is to generate a relocation against STN_UNDEF and
15807 leave everything up to the linker. */
15808 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15809 fixP->fx_done = 1;
15810
15811 switch (fixP->fx_r_type)
15812 {
15813 case BFD_RELOC_MIPS_TLS_GD:
15814 case BFD_RELOC_MIPS_TLS_LDM:
15815 case BFD_RELOC_MIPS_TLS_DTPREL32:
15816 case BFD_RELOC_MIPS_TLS_DTPREL64:
15817 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15818 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15819 case BFD_RELOC_MIPS_TLS_GOTTPREL:
15820 case BFD_RELOC_MIPS_TLS_TPREL32:
15821 case BFD_RELOC_MIPS_TLS_TPREL64:
15822 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15823 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15824 case BFD_RELOC_MICROMIPS_TLS_GD:
15825 case BFD_RELOC_MICROMIPS_TLS_LDM:
15826 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15827 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15828 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15829 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15830 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15831 case BFD_RELOC_MIPS16_TLS_GD:
15832 case BFD_RELOC_MIPS16_TLS_LDM:
15833 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15834 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15835 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15836 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15837 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15838 if (!fixP->fx_addsy)
15839 {
15840 as_bad_where (fixP->fx_file, fixP->fx_line,
15841 _("TLS relocation against a constant"));
15842 break;
15843 }
15844 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15845 /* fall through */
15846
15847 case BFD_RELOC_MIPS_JMP:
15848 case BFD_RELOC_MIPS_SHIFT5:
15849 case BFD_RELOC_MIPS_SHIFT6:
15850 case BFD_RELOC_MIPS_GOT_DISP:
15851 case BFD_RELOC_MIPS_GOT_PAGE:
15852 case BFD_RELOC_MIPS_GOT_OFST:
15853 case BFD_RELOC_MIPS_SUB:
15854 case BFD_RELOC_MIPS_INSERT_A:
15855 case BFD_RELOC_MIPS_INSERT_B:
15856 case BFD_RELOC_MIPS_DELETE:
15857 case BFD_RELOC_MIPS_HIGHEST:
15858 case BFD_RELOC_MIPS_HIGHER:
15859 case BFD_RELOC_MIPS_SCN_DISP:
15860 case BFD_RELOC_MIPS_REL16:
15861 case BFD_RELOC_MIPS_RELGOT:
15862 case BFD_RELOC_MIPS_JALR:
15863 case BFD_RELOC_HI16:
15864 case BFD_RELOC_HI16_S:
15865 case BFD_RELOC_LO16:
15866 case BFD_RELOC_GPREL16:
15867 case BFD_RELOC_MIPS_LITERAL:
15868 case BFD_RELOC_MIPS_CALL16:
15869 case BFD_RELOC_MIPS_GOT16:
15870 case BFD_RELOC_GPREL32:
15871 case BFD_RELOC_MIPS_GOT_HI16:
15872 case BFD_RELOC_MIPS_GOT_LO16:
15873 case BFD_RELOC_MIPS_CALL_HI16:
15874 case BFD_RELOC_MIPS_CALL_LO16:
15875 case BFD_RELOC_MIPS16_GPREL:
15876 case BFD_RELOC_MIPS16_GOT16:
15877 case BFD_RELOC_MIPS16_CALL16:
15878 case BFD_RELOC_MIPS16_HI16:
15879 case BFD_RELOC_MIPS16_HI16_S:
15880 case BFD_RELOC_MIPS16_LO16:
15881 case BFD_RELOC_MIPS16_JMP:
15882 case BFD_RELOC_MICROMIPS_JMP:
15883 case BFD_RELOC_MICROMIPS_GOT_DISP:
15884 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15885 case BFD_RELOC_MICROMIPS_GOT_OFST:
15886 case BFD_RELOC_MICROMIPS_SUB:
15887 case BFD_RELOC_MICROMIPS_HIGHEST:
15888 case BFD_RELOC_MICROMIPS_HIGHER:
15889 case BFD_RELOC_MICROMIPS_SCN_DISP:
15890 case BFD_RELOC_MICROMIPS_JALR:
15891 case BFD_RELOC_MICROMIPS_HI16:
15892 case BFD_RELOC_MICROMIPS_HI16_S:
15893 case BFD_RELOC_MICROMIPS_LO16:
15894 case BFD_RELOC_MICROMIPS_GPREL16:
15895 case BFD_RELOC_MICROMIPS_LITERAL:
15896 case BFD_RELOC_MICROMIPS_CALL16:
15897 case BFD_RELOC_MICROMIPS_GOT16:
15898 case BFD_RELOC_MICROMIPS_GOT_HI16:
15899 case BFD_RELOC_MICROMIPS_GOT_LO16:
15900 case BFD_RELOC_MICROMIPS_CALL_HI16:
15901 case BFD_RELOC_MICROMIPS_CALL_LO16:
15902 case BFD_RELOC_MIPS_EH:
15903 if (fixP->fx_done)
15904 {
15905 offsetT value;
15906
15907 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15908 {
15909 insn = read_reloc_insn (buf, fixP->fx_r_type);
15910 if (mips16_reloc_p (fixP->fx_r_type))
15911 insn |= mips16_immed_extend (value, 16);
15912 else
15913 insn |= (value & 0xffff);
15914 write_reloc_insn (buf, fixP->fx_r_type, insn);
15915 }
15916 else
15917 as_bad_where (fixP->fx_file, fixP->fx_line,
15918 _("Unsupported constant in relocation"));
15919 }
15920 break;
15921
15922 case BFD_RELOC_64:
15923 /* This is handled like BFD_RELOC_32, but we output a sign
15924 extended value if we are only 32 bits. */
15925 if (fixP->fx_done)
15926 {
15927 if (8 <= sizeof (valueT))
15928 md_number_to_chars (buf, *valP, 8);
15929 else
15930 {
15931 valueT hiv;
15932
15933 if ((*valP & 0x80000000) != 0)
15934 hiv = 0xffffffff;
15935 else
15936 hiv = 0;
15937 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15938 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
15939 }
15940 }
15941 break;
15942
15943 case BFD_RELOC_RVA:
15944 case BFD_RELOC_32:
15945 case BFD_RELOC_32_PCREL:
15946 case BFD_RELOC_16:
15947 /* If we are deleting this reloc entry, we must fill in the
15948 value now. This can happen if we have a .word which is not
15949 resolved when it appears but is later defined. */
15950 if (fixP->fx_done)
15951 md_number_to_chars (buf, *valP, fixP->fx_size);
15952 break;
15953
15954 case BFD_RELOC_16_PCREL_S2:
15955 if ((*valP & 0x3) != 0)
15956 as_bad_where (fixP->fx_file, fixP->fx_line,
15957 _("Branch to misaligned address (%lx)"), (long) *valP);
15958
15959 /* We need to save the bits in the instruction since fixup_segment()
15960 might be deleting the relocation entry (i.e., a branch within
15961 the current segment). */
15962 if (! fixP->fx_done)
15963 break;
15964
15965 /* Update old instruction data. */
15966 insn = read_insn (buf);
15967
15968 if (*valP + 0x20000 <= 0x3ffff)
15969 {
15970 insn |= (*valP >> 2) & 0xffff;
15971 write_insn (buf, insn);
15972 }
15973 else if (mips_pic == NO_PIC
15974 && fixP->fx_done
15975 && fixP->fx_frag->fr_address >= text_section->vma
15976 && (fixP->fx_frag->fr_address
15977 < text_section->vma + bfd_get_section_size (text_section))
15978 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15979 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15980 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15981 {
15982 /* The branch offset is too large. If this is an
15983 unconditional branch, and we are not generating PIC code,
15984 we can convert it to an absolute jump instruction. */
15985 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15986 insn = 0x0c000000; /* jal */
15987 else
15988 insn = 0x08000000; /* j */
15989 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15990 fixP->fx_done = 0;
15991 fixP->fx_addsy = section_symbol (text_section);
15992 *valP += md_pcrel_from (fixP);
15993 write_insn (buf, insn);
15994 }
15995 else
15996 {
15997 /* If we got here, we have branch-relaxation disabled,
15998 and there's nothing we can do to fix this instruction
15999 without turning it into a longer sequence. */
16000 as_bad_where (fixP->fx_file, fixP->fx_line,
16001 _("Branch out of range"));
16002 }
16003 break;
16004
16005 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
16006 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
16007 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
16008 /* We adjust the offset back to even. */
16009 if ((*valP & 0x1) != 0)
16010 --(*valP);
16011
16012 if (! fixP->fx_done)
16013 break;
16014
16015 /* Should never visit here, because we keep the relocation. */
16016 abort ();
16017 break;
16018
16019 case BFD_RELOC_VTABLE_INHERIT:
16020 fixP->fx_done = 0;
16021 if (fixP->fx_addsy
16022 && !S_IS_DEFINED (fixP->fx_addsy)
16023 && !S_IS_WEAK (fixP->fx_addsy))
16024 S_SET_WEAK (fixP->fx_addsy);
16025 break;
16026
16027 case BFD_RELOC_VTABLE_ENTRY:
16028 fixP->fx_done = 0;
16029 break;
16030
16031 default:
16032 abort ();
16033 }
16034
16035 /* Remember value for tc_gen_reloc. */
16036 fixP->fx_addnumber = *valP;
16037 }
16038
16039 static symbolS *
16040 get_symbol (void)
16041 {
16042 int c;
16043 char *name;
16044 symbolS *p;
16045
16046 name = input_line_pointer;
16047 c = get_symbol_end ();
16048 p = (symbolS *) symbol_find_or_make (name);
16049 *input_line_pointer = c;
16050 return p;
16051 }
16052
16053 /* Align the current frag to a given power of two. If a particular
16054 fill byte should be used, FILL points to an integer that contains
16055 that byte, otherwise FILL is null.
16056
16057 This function used to have the comment:
16058
16059 The MIPS assembler also automatically adjusts any preceding label.
16060
16061 The implementation therefore applied the adjustment to a maximum of
16062 one label. However, other label adjustments are applied to batches
16063 of labels, and adjusting just one caused problems when new labels
16064 were added for the sake of debugging or unwind information.
16065 We therefore adjust all preceding labels (given as LABELS) instead. */
16066
16067 static void
16068 mips_align (int to, int *fill, struct insn_label_list *labels)
16069 {
16070 mips_emit_delays ();
16071 mips_record_compressed_mode ();
16072 if (fill == NULL && subseg_text_p (now_seg))
16073 frag_align_code (to, 0);
16074 else
16075 frag_align (to, fill ? *fill : 0, 0);
16076 record_alignment (now_seg, to);
16077 mips_move_labels (labels, FALSE);
16078 }
16079
16080 /* Align to a given power of two. .align 0 turns off the automatic
16081 alignment used by the data creating pseudo-ops. */
16082
16083 static void
16084 s_align (int x ATTRIBUTE_UNUSED)
16085 {
16086 int temp, fill_value, *fill_ptr;
16087 long max_alignment = 28;
16088
16089 /* o Note that the assembler pulls down any immediately preceding label
16090 to the aligned address.
16091 o It's not documented but auto alignment is reinstated by
16092 a .align pseudo instruction.
16093 o Note also that after auto alignment is turned off the mips assembler
16094 issues an error on attempt to assemble an improperly aligned data item.
16095 We don't. */
16096
16097 temp = get_absolute_expression ();
16098 if (temp > max_alignment)
16099 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
16100 else if (temp < 0)
16101 {
16102 as_warn (_("Alignment negative: 0 assumed."));
16103 temp = 0;
16104 }
16105 if (*input_line_pointer == ',')
16106 {
16107 ++input_line_pointer;
16108 fill_value = get_absolute_expression ();
16109 fill_ptr = &fill_value;
16110 }
16111 else
16112 fill_ptr = 0;
16113 if (temp)
16114 {
16115 segment_info_type *si = seg_info (now_seg);
16116 struct insn_label_list *l = si->label_list;
16117 /* Auto alignment should be switched on by next section change. */
16118 auto_align = 1;
16119 mips_align (temp, fill_ptr, l);
16120 }
16121 else
16122 {
16123 auto_align = 0;
16124 }
16125
16126 demand_empty_rest_of_line ();
16127 }
16128
16129 static void
16130 s_change_sec (int sec)
16131 {
16132 segT seg;
16133
16134 /* The ELF backend needs to know that we are changing sections, so
16135 that .previous works correctly. We could do something like check
16136 for an obj_section_change_hook macro, but that might be confusing
16137 as it would not be appropriate to use it in the section changing
16138 functions in read.c, since obj-elf.c intercepts those. FIXME:
16139 This should be cleaner, somehow. */
16140 obj_elf_section_change_hook ();
16141
16142 mips_emit_delays ();
16143
16144 switch (sec)
16145 {
16146 case 't':
16147 s_text (0);
16148 break;
16149 case 'd':
16150 s_data (0);
16151 break;
16152 case 'b':
16153 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16154 demand_empty_rest_of_line ();
16155 break;
16156
16157 case 'r':
16158 seg = subseg_new (RDATA_SECTION_NAME,
16159 (subsegT) get_absolute_expression ());
16160 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16161 | SEC_READONLY | SEC_RELOC
16162 | SEC_DATA));
16163 if (strncmp (TARGET_OS, "elf", 3) != 0)
16164 record_alignment (seg, 4);
16165 demand_empty_rest_of_line ();
16166 break;
16167
16168 case 's':
16169 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
16170 bfd_set_section_flags (stdoutput, seg,
16171 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16172 if (strncmp (TARGET_OS, "elf", 3) != 0)
16173 record_alignment (seg, 4);
16174 demand_empty_rest_of_line ();
16175 break;
16176
16177 case 'B':
16178 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16179 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16180 if (strncmp (TARGET_OS, "elf", 3) != 0)
16181 record_alignment (seg, 4);
16182 demand_empty_rest_of_line ();
16183 break;
16184 }
16185
16186 auto_align = 1;
16187 }
16188
16189 void
16190 s_change_section (int ignore ATTRIBUTE_UNUSED)
16191 {
16192 char *section_name;
16193 char c;
16194 char next_c = 0;
16195 int section_type;
16196 int section_flag;
16197 int section_entry_size;
16198 int section_alignment;
16199
16200 section_name = input_line_pointer;
16201 c = get_symbol_end ();
16202 if (c)
16203 next_c = *(input_line_pointer + 1);
16204
16205 /* Do we have .section Name<,"flags">? */
16206 if (c != ',' || (c == ',' && next_c == '"'))
16207 {
16208 /* just after name is now '\0'. */
16209 *input_line_pointer = c;
16210 input_line_pointer = section_name;
16211 obj_elf_section (ignore);
16212 return;
16213 }
16214 input_line_pointer++;
16215
16216 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16217 if (c == ',')
16218 section_type = get_absolute_expression ();
16219 else
16220 section_type = 0;
16221 if (*input_line_pointer++ == ',')
16222 section_flag = get_absolute_expression ();
16223 else
16224 section_flag = 0;
16225 if (*input_line_pointer++ == ',')
16226 section_entry_size = get_absolute_expression ();
16227 else
16228 section_entry_size = 0;
16229 if (*input_line_pointer++ == ',')
16230 section_alignment = get_absolute_expression ();
16231 else
16232 section_alignment = 0;
16233 /* FIXME: really ignore? */
16234 (void) section_alignment;
16235
16236 section_name = xstrdup (section_name);
16237
16238 /* When using the generic form of .section (as implemented by obj-elf.c),
16239 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16240 traditionally had to fall back on the more common @progbits instead.
16241
16242 There's nothing really harmful in this, since bfd will correct
16243 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
16244 means that, for backwards compatibility, the special_section entries
16245 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16246
16247 Even so, we shouldn't force users of the MIPS .section syntax to
16248 incorrectly label the sections as SHT_PROGBITS. The best compromise
16249 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16250 generic type-checking code. */
16251 if (section_type == SHT_MIPS_DWARF)
16252 section_type = SHT_PROGBITS;
16253
16254 obj_elf_change_section (section_name, section_type, section_flag,
16255 section_entry_size, 0, 0, 0);
16256
16257 if (now_seg->name != section_name)
16258 free (section_name);
16259 }
16260
16261 void
16262 mips_enable_auto_align (void)
16263 {
16264 auto_align = 1;
16265 }
16266
16267 static void
16268 s_cons (int log_size)
16269 {
16270 segment_info_type *si = seg_info (now_seg);
16271 struct insn_label_list *l = si->label_list;
16272
16273 mips_emit_delays ();
16274 if (log_size > 0 && auto_align)
16275 mips_align (log_size, 0, l);
16276 cons (1 << log_size);
16277 mips_clear_insn_labels ();
16278 }
16279
16280 static void
16281 s_float_cons (int type)
16282 {
16283 segment_info_type *si = seg_info (now_seg);
16284 struct insn_label_list *l = si->label_list;
16285
16286 mips_emit_delays ();
16287
16288 if (auto_align)
16289 {
16290 if (type == 'd')
16291 mips_align (3, 0, l);
16292 else
16293 mips_align (2, 0, l);
16294 }
16295
16296 float_cons (type);
16297 mips_clear_insn_labels ();
16298 }
16299
16300 /* Handle .globl. We need to override it because on Irix 5 you are
16301 permitted to say
16302 .globl foo .text
16303 where foo is an undefined symbol, to mean that foo should be
16304 considered to be the address of a function. */
16305
16306 static void
16307 s_mips_globl (int x ATTRIBUTE_UNUSED)
16308 {
16309 char *name;
16310 int c;
16311 symbolS *symbolP;
16312 flagword flag;
16313
16314 do
16315 {
16316 name = input_line_pointer;
16317 c = get_symbol_end ();
16318 symbolP = symbol_find_or_make (name);
16319 S_SET_EXTERNAL (symbolP);
16320
16321 *input_line_pointer = c;
16322 SKIP_WHITESPACE ();
16323
16324 /* On Irix 5, every global symbol that is not explicitly labelled as
16325 being a function is apparently labelled as being an object. */
16326 flag = BSF_OBJECT;
16327
16328 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16329 && (*input_line_pointer != ','))
16330 {
16331 char *secname;
16332 asection *sec;
16333
16334 secname = input_line_pointer;
16335 c = get_symbol_end ();
16336 sec = bfd_get_section_by_name (stdoutput, secname);
16337 if (sec == NULL)
16338 as_bad (_("%s: no such section"), secname);
16339 *input_line_pointer = c;
16340
16341 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16342 flag = BSF_FUNCTION;
16343 }
16344
16345 symbol_get_bfdsym (symbolP)->flags |= flag;
16346
16347 c = *input_line_pointer;
16348 if (c == ',')
16349 {
16350 input_line_pointer++;
16351 SKIP_WHITESPACE ();
16352 if (is_end_of_line[(unsigned char) *input_line_pointer])
16353 c = '\n';
16354 }
16355 }
16356 while (c == ',');
16357
16358 demand_empty_rest_of_line ();
16359 }
16360
16361 static void
16362 s_option (int x ATTRIBUTE_UNUSED)
16363 {
16364 char *opt;
16365 char c;
16366
16367 opt = input_line_pointer;
16368 c = get_symbol_end ();
16369
16370 if (*opt == 'O')
16371 {
16372 /* FIXME: What does this mean? */
16373 }
16374 else if (strncmp (opt, "pic", 3) == 0)
16375 {
16376 int i;
16377
16378 i = atoi (opt + 3);
16379 if (i == 0)
16380 mips_pic = NO_PIC;
16381 else if (i == 2)
16382 {
16383 mips_pic = SVR4_PIC;
16384 mips_abicalls = TRUE;
16385 }
16386 else
16387 as_bad (_(".option pic%d not supported"), i);
16388
16389 if (mips_pic == SVR4_PIC)
16390 {
16391 if (g_switch_seen && g_switch_value != 0)
16392 as_warn (_("-G may not be used with SVR4 PIC code"));
16393 g_switch_value = 0;
16394 bfd_set_gp_size (stdoutput, 0);
16395 }
16396 }
16397 else
16398 as_warn (_("Unrecognized option \"%s\""), opt);
16399
16400 *input_line_pointer = c;
16401 demand_empty_rest_of_line ();
16402 }
16403
16404 /* This structure is used to hold a stack of .set values. */
16405
16406 struct mips_option_stack
16407 {
16408 struct mips_option_stack *next;
16409 struct mips_set_options options;
16410 };
16411
16412 static struct mips_option_stack *mips_opts_stack;
16413
16414 /* Handle the .set pseudo-op. */
16415
16416 static void
16417 s_mipsset (int x ATTRIBUTE_UNUSED)
16418 {
16419 char *name = input_line_pointer, ch;
16420 const struct mips_ase *ase;
16421
16422 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16423 ++input_line_pointer;
16424 ch = *input_line_pointer;
16425 *input_line_pointer = '\0';
16426
16427 if (strcmp (name, "reorder") == 0)
16428 {
16429 if (mips_opts.noreorder)
16430 end_noreorder ();
16431 }
16432 else if (strcmp (name, "noreorder") == 0)
16433 {
16434 if (!mips_opts.noreorder)
16435 start_noreorder ();
16436 }
16437 else if (strncmp (name, "at=", 3) == 0)
16438 {
16439 char *s = name + 3;
16440
16441 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16442 as_bad (_("Unrecognized register name `%s'"), s);
16443 }
16444 else if (strcmp (name, "at") == 0)
16445 {
16446 mips_opts.at = ATREG;
16447 }
16448 else if (strcmp (name, "noat") == 0)
16449 {
16450 mips_opts.at = ZERO;
16451 }
16452 else if (strcmp (name, "macro") == 0)
16453 {
16454 mips_opts.warn_about_macros = 0;
16455 }
16456 else if (strcmp (name, "nomacro") == 0)
16457 {
16458 if (mips_opts.noreorder == 0)
16459 as_bad (_("`noreorder' must be set before `nomacro'"));
16460 mips_opts.warn_about_macros = 1;
16461 }
16462 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16463 {
16464 mips_opts.nomove = 0;
16465 }
16466 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16467 {
16468 mips_opts.nomove = 1;
16469 }
16470 else if (strcmp (name, "bopt") == 0)
16471 {
16472 mips_opts.nobopt = 0;
16473 }
16474 else if (strcmp (name, "nobopt") == 0)
16475 {
16476 mips_opts.nobopt = 1;
16477 }
16478 else if (strcmp (name, "gp=default") == 0)
16479 mips_opts.gp32 = file_mips_gp32;
16480 else if (strcmp (name, "gp=32") == 0)
16481 mips_opts.gp32 = 1;
16482 else if (strcmp (name, "gp=64") == 0)
16483 {
16484 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
16485 as_warn (_("%s isa does not support 64-bit registers"),
16486 mips_cpu_info_from_isa (mips_opts.isa)->name);
16487 mips_opts.gp32 = 0;
16488 }
16489 else if (strcmp (name, "fp=default") == 0)
16490 mips_opts.fp32 = file_mips_fp32;
16491 else if (strcmp (name, "fp=32") == 0)
16492 mips_opts.fp32 = 1;
16493 else if (strcmp (name, "fp=64") == 0)
16494 {
16495 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
16496 as_warn (_("%s isa does not support 64-bit floating point registers"),
16497 mips_cpu_info_from_isa (mips_opts.isa)->name);
16498 mips_opts.fp32 = 0;
16499 }
16500 else if (strcmp (name, "softfloat") == 0)
16501 mips_opts.soft_float = 1;
16502 else if (strcmp (name, "hardfloat") == 0)
16503 mips_opts.soft_float = 0;
16504 else if (strcmp (name, "singlefloat") == 0)
16505 mips_opts.single_float = 1;
16506 else if (strcmp (name, "doublefloat") == 0)
16507 mips_opts.single_float = 0;
16508 else if (strcmp (name, "mips16") == 0
16509 || strcmp (name, "MIPS-16") == 0)
16510 {
16511 if (mips_opts.micromips == 1)
16512 as_fatal (_("`mips16' cannot be used with `micromips'"));
16513 mips_opts.mips16 = 1;
16514 }
16515 else if (strcmp (name, "nomips16") == 0
16516 || strcmp (name, "noMIPS-16") == 0)
16517 mips_opts.mips16 = 0;
16518 else if (strcmp (name, "micromips") == 0)
16519 {
16520 if (mips_opts.mips16 == 1)
16521 as_fatal (_("`micromips' cannot be used with `mips16'"));
16522 mips_opts.micromips = 1;
16523 }
16524 else if (strcmp (name, "nomicromips") == 0)
16525 mips_opts.micromips = 0;
16526 else if (name[0] == 'n'
16527 && name[1] == 'o'
16528 && (ase = mips_lookup_ase (name + 2)))
16529 mips_set_ase (ase, FALSE);
16530 else if ((ase = mips_lookup_ase (name)))
16531 mips_set_ase (ase, TRUE);
16532 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16533 {
16534 int reset = 0;
16535
16536 /* Permit the user to change the ISA and architecture on the fly.
16537 Needless to say, misuse can cause serious problems. */
16538 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16539 {
16540 reset = 1;
16541 mips_opts.isa = file_mips_isa;
16542 mips_opts.arch = file_mips_arch;
16543 }
16544 else if (strncmp (name, "arch=", 5) == 0)
16545 {
16546 const struct mips_cpu_info *p;
16547
16548 p = mips_parse_cpu("internal use", name + 5);
16549 if (!p)
16550 as_bad (_("unknown architecture %s"), name + 5);
16551 else
16552 {
16553 mips_opts.arch = p->cpu;
16554 mips_opts.isa = p->isa;
16555 }
16556 }
16557 else if (strncmp (name, "mips", 4) == 0)
16558 {
16559 const struct mips_cpu_info *p;
16560
16561 p = mips_parse_cpu("internal use", name);
16562 if (!p)
16563 as_bad (_("unknown ISA level %s"), name + 4);
16564 else
16565 {
16566 mips_opts.arch = p->cpu;
16567 mips_opts.isa = p->isa;
16568 }
16569 }
16570 else
16571 as_bad (_("unknown ISA or architecture %s"), name);
16572
16573 switch (mips_opts.isa)
16574 {
16575 case 0:
16576 break;
16577 case ISA_MIPS1:
16578 case ISA_MIPS2:
16579 case ISA_MIPS32:
16580 case ISA_MIPS32R2:
16581 mips_opts.gp32 = 1;
16582 mips_opts.fp32 = 1;
16583 break;
16584 case ISA_MIPS3:
16585 case ISA_MIPS4:
16586 case ISA_MIPS5:
16587 case ISA_MIPS64:
16588 case ISA_MIPS64R2:
16589 mips_opts.gp32 = 0;
16590 if (mips_opts.arch == CPU_R5900)
16591 {
16592 mips_opts.fp32 = 1;
16593 }
16594 else
16595 {
16596 mips_opts.fp32 = 0;
16597 }
16598 break;
16599 default:
16600 as_bad (_("unknown ISA level %s"), name + 4);
16601 break;
16602 }
16603 if (reset)
16604 {
16605 mips_opts.gp32 = file_mips_gp32;
16606 mips_opts.fp32 = file_mips_fp32;
16607 }
16608 }
16609 else if (strcmp (name, "autoextend") == 0)
16610 mips_opts.noautoextend = 0;
16611 else if (strcmp (name, "noautoextend") == 0)
16612 mips_opts.noautoextend = 1;
16613 else if (strcmp (name, "insn32") == 0)
16614 mips_opts.insn32 = TRUE;
16615 else if (strcmp (name, "noinsn32") == 0)
16616 mips_opts.insn32 = FALSE;
16617 else if (strcmp (name, "push") == 0)
16618 {
16619 struct mips_option_stack *s;
16620
16621 s = (struct mips_option_stack *) xmalloc (sizeof *s);
16622 s->next = mips_opts_stack;
16623 s->options = mips_opts;
16624 mips_opts_stack = s;
16625 }
16626 else if (strcmp (name, "pop") == 0)
16627 {
16628 struct mips_option_stack *s;
16629
16630 s = mips_opts_stack;
16631 if (s == NULL)
16632 as_bad (_(".set pop with no .set push"));
16633 else
16634 {
16635 /* If we're changing the reorder mode we need to handle
16636 delay slots correctly. */
16637 if (s->options.noreorder && ! mips_opts.noreorder)
16638 start_noreorder ();
16639 else if (! s->options.noreorder && mips_opts.noreorder)
16640 end_noreorder ();
16641
16642 mips_opts = s->options;
16643 mips_opts_stack = s->next;
16644 free (s);
16645 }
16646 }
16647 else if (strcmp (name, "sym32") == 0)
16648 mips_opts.sym32 = TRUE;
16649 else if (strcmp (name, "nosym32") == 0)
16650 mips_opts.sym32 = FALSE;
16651 else if (strchr (name, ','))
16652 {
16653 /* Generic ".set" directive; use the generic handler. */
16654 *input_line_pointer = ch;
16655 input_line_pointer = name;
16656 s_set (0);
16657 return;
16658 }
16659 else
16660 {
16661 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16662 }
16663 mips_check_isa_supports_ases ();
16664 *input_line_pointer = ch;
16665 demand_empty_rest_of_line ();
16666 }
16667
16668 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
16669 .option pic2. It means to generate SVR4 PIC calls. */
16670
16671 static void
16672 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16673 {
16674 mips_pic = SVR4_PIC;
16675 mips_abicalls = TRUE;
16676
16677 if (g_switch_seen && g_switch_value != 0)
16678 as_warn (_("-G may not be used with SVR4 PIC code"));
16679 g_switch_value = 0;
16680
16681 bfd_set_gp_size (stdoutput, 0);
16682 demand_empty_rest_of_line ();
16683 }
16684
16685 /* Handle the .cpload pseudo-op. This is used when generating SVR4
16686 PIC code. It sets the $gp register for the function based on the
16687 function address, which is in the register named in the argument.
16688 This uses a relocation against _gp_disp, which is handled specially
16689 by the linker. The result is:
16690 lui $gp,%hi(_gp_disp)
16691 addiu $gp,$gp,%lo(_gp_disp)
16692 addu $gp,$gp,.cpload argument
16693 The .cpload argument is normally $25 == $t9.
16694
16695 The -mno-shared option changes this to:
16696 lui $gp,%hi(__gnu_local_gp)
16697 addiu $gp,$gp,%lo(__gnu_local_gp)
16698 and the argument is ignored. This saves an instruction, but the
16699 resulting code is not position independent; it uses an absolute
16700 address for __gnu_local_gp. Thus code assembled with -mno-shared
16701 can go into an ordinary executable, but not into a shared library. */
16702
16703 static void
16704 s_cpload (int ignore ATTRIBUTE_UNUSED)
16705 {
16706 expressionS ex;
16707 int reg;
16708 int in_shared;
16709
16710 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16711 .cpload is ignored. */
16712 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16713 {
16714 s_ignore (0);
16715 return;
16716 }
16717
16718 if (mips_opts.mips16)
16719 {
16720 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16721 ignore_rest_of_line ();
16722 return;
16723 }
16724
16725 /* .cpload should be in a .set noreorder section. */
16726 if (mips_opts.noreorder == 0)
16727 as_warn (_(".cpload not in noreorder section"));
16728
16729 reg = tc_get_register (0);
16730
16731 /* If we need to produce a 64-bit address, we are better off using
16732 the default instruction sequence. */
16733 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16734
16735 ex.X_op = O_symbol;
16736 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16737 "__gnu_local_gp");
16738 ex.X_op_symbol = NULL;
16739 ex.X_add_number = 0;
16740
16741 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16742 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16743
16744 mips_mark_labels ();
16745 mips_assembling_insn = TRUE;
16746
16747 macro_start ();
16748 macro_build_lui (&ex, mips_gp_register);
16749 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16750 mips_gp_register, BFD_RELOC_LO16);
16751 if (in_shared)
16752 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16753 mips_gp_register, reg);
16754 macro_end ();
16755
16756 mips_assembling_insn = FALSE;
16757 demand_empty_rest_of_line ();
16758 }
16759
16760 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16761 .cpsetup $reg1, offset|$reg2, label
16762
16763 If offset is given, this results in:
16764 sd $gp, offset($sp)
16765 lui $gp, %hi(%neg(%gp_rel(label)))
16766 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16767 daddu $gp, $gp, $reg1
16768
16769 If $reg2 is given, this results in:
16770 daddu $reg2, $gp, $0
16771 lui $gp, %hi(%neg(%gp_rel(label)))
16772 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16773 daddu $gp, $gp, $reg1
16774 $reg1 is normally $25 == $t9.
16775
16776 The -mno-shared option replaces the last three instructions with
16777 lui $gp,%hi(_gp)
16778 addiu $gp,$gp,%lo(_gp) */
16779
16780 static void
16781 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16782 {
16783 expressionS ex_off;
16784 expressionS ex_sym;
16785 int reg1;
16786
16787 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16788 We also need NewABI support. */
16789 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16790 {
16791 s_ignore (0);
16792 return;
16793 }
16794
16795 if (mips_opts.mips16)
16796 {
16797 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16798 ignore_rest_of_line ();
16799 return;
16800 }
16801
16802 reg1 = tc_get_register (0);
16803 SKIP_WHITESPACE ();
16804 if (*input_line_pointer != ',')
16805 {
16806 as_bad (_("missing argument separator ',' for .cpsetup"));
16807 return;
16808 }
16809 else
16810 ++input_line_pointer;
16811 SKIP_WHITESPACE ();
16812 if (*input_line_pointer == '$')
16813 {
16814 mips_cpreturn_register = tc_get_register (0);
16815 mips_cpreturn_offset = -1;
16816 }
16817 else
16818 {
16819 mips_cpreturn_offset = get_absolute_expression ();
16820 mips_cpreturn_register = -1;
16821 }
16822 SKIP_WHITESPACE ();
16823 if (*input_line_pointer != ',')
16824 {
16825 as_bad (_("missing argument separator ',' for .cpsetup"));
16826 return;
16827 }
16828 else
16829 ++input_line_pointer;
16830 SKIP_WHITESPACE ();
16831 expression (&ex_sym);
16832
16833 mips_mark_labels ();
16834 mips_assembling_insn = TRUE;
16835
16836 macro_start ();
16837 if (mips_cpreturn_register == -1)
16838 {
16839 ex_off.X_op = O_constant;
16840 ex_off.X_add_symbol = NULL;
16841 ex_off.X_op_symbol = NULL;
16842 ex_off.X_add_number = mips_cpreturn_offset;
16843
16844 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16845 BFD_RELOC_LO16, SP);
16846 }
16847 else
16848 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
16849 mips_gp_register, 0);
16850
16851 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16852 {
16853 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16854 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16855 BFD_RELOC_HI16_S);
16856
16857 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16858 mips_gp_register, -1, BFD_RELOC_GPREL16,
16859 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16860
16861 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16862 mips_gp_register, reg1);
16863 }
16864 else
16865 {
16866 expressionS ex;
16867
16868 ex.X_op = O_symbol;
16869 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16870 ex.X_op_symbol = NULL;
16871 ex.X_add_number = 0;
16872
16873 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16874 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16875
16876 macro_build_lui (&ex, mips_gp_register);
16877 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16878 mips_gp_register, BFD_RELOC_LO16);
16879 }
16880
16881 macro_end ();
16882
16883 mips_assembling_insn = FALSE;
16884 demand_empty_rest_of_line ();
16885 }
16886
16887 static void
16888 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16889 {
16890 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16891 .cplocal is ignored. */
16892 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16893 {
16894 s_ignore (0);
16895 return;
16896 }
16897
16898 if (mips_opts.mips16)
16899 {
16900 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16901 ignore_rest_of_line ();
16902 return;
16903 }
16904
16905 mips_gp_register = tc_get_register (0);
16906 demand_empty_rest_of_line ();
16907 }
16908
16909 /* Handle the .cprestore pseudo-op. This stores $gp into a given
16910 offset from $sp. The offset is remembered, and after making a PIC
16911 call $gp is restored from that location. */
16912
16913 static void
16914 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16915 {
16916 expressionS ex;
16917
16918 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16919 .cprestore is ignored. */
16920 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16921 {
16922 s_ignore (0);
16923 return;
16924 }
16925
16926 if (mips_opts.mips16)
16927 {
16928 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16929 ignore_rest_of_line ();
16930 return;
16931 }
16932
16933 mips_cprestore_offset = get_absolute_expression ();
16934 mips_cprestore_valid = 1;
16935
16936 ex.X_op = O_constant;
16937 ex.X_add_symbol = NULL;
16938 ex.X_op_symbol = NULL;
16939 ex.X_add_number = mips_cprestore_offset;
16940
16941 mips_mark_labels ();
16942 mips_assembling_insn = TRUE;
16943
16944 macro_start ();
16945 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16946 SP, HAVE_64BIT_ADDRESSES);
16947 macro_end ();
16948
16949 mips_assembling_insn = FALSE;
16950 demand_empty_rest_of_line ();
16951 }
16952
16953 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16954 was given in the preceding .cpsetup, it results in:
16955 ld $gp, offset($sp)
16956
16957 If a register $reg2 was given there, it results in:
16958 daddu $gp, $reg2, $0 */
16959
16960 static void
16961 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16962 {
16963 expressionS ex;
16964
16965 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16966 We also need NewABI support. */
16967 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16968 {
16969 s_ignore (0);
16970 return;
16971 }
16972
16973 if (mips_opts.mips16)
16974 {
16975 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16976 ignore_rest_of_line ();
16977 return;
16978 }
16979
16980 mips_mark_labels ();
16981 mips_assembling_insn = TRUE;
16982
16983 macro_start ();
16984 if (mips_cpreturn_register == -1)
16985 {
16986 ex.X_op = O_constant;
16987 ex.X_add_symbol = NULL;
16988 ex.X_op_symbol = NULL;
16989 ex.X_add_number = mips_cpreturn_offset;
16990
16991 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16992 }
16993 else
16994 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
16995 mips_cpreturn_register, 0);
16996 macro_end ();
16997
16998 mips_assembling_insn = FALSE;
16999 demand_empty_rest_of_line ();
17000 }
17001
17002 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17003 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17004 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17005 debug information or MIPS16 TLS. */
17006
17007 static void
17008 s_tls_rel_directive (const size_t bytes, const char *dirstr,
17009 bfd_reloc_code_real_type rtype)
17010 {
17011 expressionS ex;
17012 char *p;
17013
17014 expression (&ex);
17015
17016 if (ex.X_op != O_symbol)
17017 {
17018 as_bad (_("Unsupported use of %s"), dirstr);
17019 ignore_rest_of_line ();
17020 }
17021
17022 p = frag_more (bytes);
17023 md_number_to_chars (p, 0, bytes);
17024 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
17025 demand_empty_rest_of_line ();
17026 mips_clear_insn_labels ();
17027 }
17028
17029 /* Handle .dtprelword. */
17030
17031 static void
17032 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17033 {
17034 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
17035 }
17036
17037 /* Handle .dtpreldword. */
17038
17039 static void
17040 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17041 {
17042 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17043 }
17044
17045 /* Handle .tprelword. */
17046
17047 static void
17048 s_tprelword (int ignore ATTRIBUTE_UNUSED)
17049 {
17050 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17051 }
17052
17053 /* Handle .tpreldword. */
17054
17055 static void
17056 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17057 {
17058 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
17059 }
17060
17061 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17062 code. It sets the offset to use in gp_rel relocations. */
17063
17064 static void
17065 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
17066 {
17067 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17068 We also need NewABI support. */
17069 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17070 {
17071 s_ignore (0);
17072 return;
17073 }
17074
17075 mips_gprel_offset = get_absolute_expression ();
17076
17077 demand_empty_rest_of_line ();
17078 }
17079
17080 /* Handle the .gpword pseudo-op. This is used when generating PIC
17081 code. It generates a 32 bit GP relative reloc. */
17082
17083 static void
17084 s_gpword (int ignore ATTRIBUTE_UNUSED)
17085 {
17086 segment_info_type *si;
17087 struct insn_label_list *l;
17088 expressionS ex;
17089 char *p;
17090
17091 /* When not generating PIC code, this is treated as .word. */
17092 if (mips_pic != SVR4_PIC)
17093 {
17094 s_cons (2);
17095 return;
17096 }
17097
17098 si = seg_info (now_seg);
17099 l = si->label_list;
17100 mips_emit_delays ();
17101 if (auto_align)
17102 mips_align (2, 0, l);
17103
17104 expression (&ex);
17105 mips_clear_insn_labels ();
17106
17107 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17108 {
17109 as_bad (_("Unsupported use of .gpword"));
17110 ignore_rest_of_line ();
17111 }
17112
17113 p = frag_more (4);
17114 md_number_to_chars (p, 0, 4);
17115 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17116 BFD_RELOC_GPREL32);
17117
17118 demand_empty_rest_of_line ();
17119 }
17120
17121 static void
17122 s_gpdword (int ignore ATTRIBUTE_UNUSED)
17123 {
17124 segment_info_type *si;
17125 struct insn_label_list *l;
17126 expressionS ex;
17127 char *p;
17128
17129 /* When not generating PIC code, this is treated as .dword. */
17130 if (mips_pic != SVR4_PIC)
17131 {
17132 s_cons (3);
17133 return;
17134 }
17135
17136 si = seg_info (now_seg);
17137 l = si->label_list;
17138 mips_emit_delays ();
17139 if (auto_align)
17140 mips_align (3, 0, l);
17141
17142 expression (&ex);
17143 mips_clear_insn_labels ();
17144
17145 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17146 {
17147 as_bad (_("Unsupported use of .gpdword"));
17148 ignore_rest_of_line ();
17149 }
17150
17151 p = frag_more (8);
17152 md_number_to_chars (p, 0, 8);
17153 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17154 BFD_RELOC_GPREL32)->fx_tcbit = 1;
17155
17156 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
17157 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17158 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
17159
17160 demand_empty_rest_of_line ();
17161 }
17162
17163 /* Handle the .ehword pseudo-op. This is used when generating unwinding
17164 tables. It generates a R_MIPS_EH reloc. */
17165
17166 static void
17167 s_ehword (int ignore ATTRIBUTE_UNUSED)
17168 {
17169 expressionS ex;
17170 char *p;
17171
17172 mips_emit_delays ();
17173
17174 expression (&ex);
17175 mips_clear_insn_labels ();
17176
17177 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17178 {
17179 as_bad (_("Unsupported use of .ehword"));
17180 ignore_rest_of_line ();
17181 }
17182
17183 p = frag_more (4);
17184 md_number_to_chars (p, 0, 4);
17185 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17186 BFD_RELOC_MIPS_EH);
17187
17188 demand_empty_rest_of_line ();
17189 }
17190
17191 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
17192 tables in SVR4 PIC code. */
17193
17194 static void
17195 s_cpadd (int ignore ATTRIBUTE_UNUSED)
17196 {
17197 int reg;
17198
17199 /* This is ignored when not generating SVR4 PIC code. */
17200 if (mips_pic != SVR4_PIC)
17201 {
17202 s_ignore (0);
17203 return;
17204 }
17205
17206 mips_mark_labels ();
17207 mips_assembling_insn = TRUE;
17208
17209 /* Add $gp to the register named as an argument. */
17210 macro_start ();
17211 reg = tc_get_register (0);
17212 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
17213 macro_end ();
17214
17215 mips_assembling_insn = FALSE;
17216 demand_empty_rest_of_line ();
17217 }
17218
17219 /* Handle the .insn pseudo-op. This marks instruction labels in
17220 mips16/micromips mode. This permits the linker to handle them specially,
17221 such as generating jalx instructions when needed. We also make
17222 them odd for the duration of the assembly, in order to generate the
17223 right sort of code. We will make them even in the adjust_symtab
17224 routine, while leaving them marked. This is convenient for the
17225 debugger and the disassembler. The linker knows to make them odd
17226 again. */
17227
17228 static void
17229 s_insn (int ignore ATTRIBUTE_UNUSED)
17230 {
17231 mips_mark_labels ();
17232
17233 demand_empty_rest_of_line ();
17234 }
17235
17236 /* Handle a .stab[snd] directive. Ideally these directives would be
17237 implemented in a transparent way, so that removing them would not
17238 have any effect on the generated instructions. However, s_stab
17239 internally changes the section, so in practice we need to decide
17240 now whether the preceding label marks compressed code. We do not
17241 support changing the compression mode of a label after a .stab*
17242 directive, such as in:
17243
17244 foo:
17245 .stabs ...
17246 .set mips16
17247
17248 so the current mode wins. */
17249
17250 static void
17251 s_mips_stab (int type)
17252 {
17253 mips_mark_labels ();
17254 s_stab (type);
17255 }
17256
17257 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
17258
17259 static void
17260 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17261 {
17262 char *name;
17263 int c;
17264 symbolS *symbolP;
17265 expressionS exp;
17266
17267 name = input_line_pointer;
17268 c = get_symbol_end ();
17269 symbolP = symbol_find_or_make (name);
17270 S_SET_WEAK (symbolP);
17271 *input_line_pointer = c;
17272
17273 SKIP_WHITESPACE ();
17274
17275 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17276 {
17277 if (S_IS_DEFINED (symbolP))
17278 {
17279 as_bad (_("ignoring attempt to redefine symbol %s"),
17280 S_GET_NAME (symbolP));
17281 ignore_rest_of_line ();
17282 return;
17283 }
17284
17285 if (*input_line_pointer == ',')
17286 {
17287 ++input_line_pointer;
17288 SKIP_WHITESPACE ();
17289 }
17290
17291 expression (&exp);
17292 if (exp.X_op != O_symbol)
17293 {
17294 as_bad (_("bad .weakext directive"));
17295 ignore_rest_of_line ();
17296 return;
17297 }
17298 symbol_set_value_expression (symbolP, &exp);
17299 }
17300
17301 demand_empty_rest_of_line ();
17302 }
17303
17304 /* Parse a register string into a number. Called from the ECOFF code
17305 to parse .frame. The argument is non-zero if this is the frame
17306 register, so that we can record it in mips_frame_reg. */
17307
17308 int
17309 tc_get_register (int frame)
17310 {
17311 unsigned int reg;
17312
17313 SKIP_WHITESPACE ();
17314 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17315 reg = 0;
17316 if (frame)
17317 {
17318 mips_frame_reg = reg != 0 ? reg : SP;
17319 mips_frame_reg_valid = 1;
17320 mips_cprestore_valid = 0;
17321 }
17322 return reg;
17323 }
17324
17325 valueT
17326 md_section_align (asection *seg, valueT addr)
17327 {
17328 int align = bfd_get_section_alignment (stdoutput, seg);
17329
17330 /* We don't need to align ELF sections to the full alignment.
17331 However, Irix 5 may prefer that we align them at least to a 16
17332 byte boundary. We don't bother to align the sections if we
17333 are targeted for an embedded system. */
17334 if (strncmp (TARGET_OS, "elf", 3) == 0)
17335 return addr;
17336 if (align > 4)
17337 align = 4;
17338
17339 return ((addr + (1 << align) - 1) & (-1 << align));
17340 }
17341
17342 /* Utility routine, called from above as well. If called while the
17343 input file is still being read, it's only an approximation. (For
17344 example, a symbol may later become defined which appeared to be
17345 undefined earlier.) */
17346
17347 static int
17348 nopic_need_relax (symbolS *sym, int before_relaxing)
17349 {
17350 if (sym == 0)
17351 return 0;
17352
17353 if (g_switch_value > 0)
17354 {
17355 const char *symname;
17356 int change;
17357
17358 /* Find out whether this symbol can be referenced off the $gp
17359 register. It can be if it is smaller than the -G size or if
17360 it is in the .sdata or .sbss section. Certain symbols can
17361 not be referenced off the $gp, although it appears as though
17362 they can. */
17363 symname = S_GET_NAME (sym);
17364 if (symname != (const char *) NULL
17365 && (strcmp (symname, "eprol") == 0
17366 || strcmp (symname, "etext") == 0
17367 || strcmp (symname, "_gp") == 0
17368 || strcmp (symname, "edata") == 0
17369 || strcmp (symname, "_fbss") == 0
17370 || strcmp (symname, "_fdata") == 0
17371 || strcmp (symname, "_ftext") == 0
17372 || strcmp (symname, "end") == 0
17373 || strcmp (symname, "_gp_disp") == 0))
17374 change = 1;
17375 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17376 && (0
17377 #ifndef NO_ECOFF_DEBUGGING
17378 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17379 && (symbol_get_obj (sym)->ecoff_extern_size
17380 <= g_switch_value))
17381 #endif
17382 /* We must defer this decision until after the whole
17383 file has been read, since there might be a .extern
17384 after the first use of this symbol. */
17385 || (before_relaxing
17386 #ifndef NO_ECOFF_DEBUGGING
17387 && symbol_get_obj (sym)->ecoff_extern_size == 0
17388 #endif
17389 && S_GET_VALUE (sym) == 0)
17390 || (S_GET_VALUE (sym) != 0
17391 && S_GET_VALUE (sym) <= g_switch_value)))
17392 change = 0;
17393 else
17394 {
17395 const char *segname;
17396
17397 segname = segment_name (S_GET_SEGMENT (sym));
17398 gas_assert (strcmp (segname, ".lit8") != 0
17399 && strcmp (segname, ".lit4") != 0);
17400 change = (strcmp (segname, ".sdata") != 0
17401 && strcmp (segname, ".sbss") != 0
17402 && strncmp (segname, ".sdata.", 7) != 0
17403 && strncmp (segname, ".sbss.", 6) != 0
17404 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17405 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17406 }
17407 return change;
17408 }
17409 else
17410 /* We are not optimizing for the $gp register. */
17411 return 1;
17412 }
17413
17414
17415 /* Return true if the given symbol should be considered local for SVR4 PIC. */
17416
17417 static bfd_boolean
17418 pic_need_relax (symbolS *sym, asection *segtype)
17419 {
17420 asection *symsec;
17421
17422 /* Handle the case of a symbol equated to another symbol. */
17423 while (symbol_equated_reloc_p (sym))
17424 {
17425 symbolS *n;
17426
17427 /* It's possible to get a loop here in a badly written program. */
17428 n = symbol_get_value_expression (sym)->X_add_symbol;
17429 if (n == sym)
17430 break;
17431 sym = n;
17432 }
17433
17434 if (symbol_section_p (sym))
17435 return TRUE;
17436
17437 symsec = S_GET_SEGMENT (sym);
17438
17439 /* This must duplicate the test in adjust_reloc_syms. */
17440 return (!bfd_is_und_section (symsec)
17441 && !bfd_is_abs_section (symsec)
17442 && !bfd_is_com_section (symsec)
17443 && !s_is_linkonce (sym, segtype)
17444 /* A global or weak symbol is treated as external. */
17445 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
17446 }
17447
17448
17449 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17450 extended opcode. SEC is the section the frag is in. */
17451
17452 static int
17453 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17454 {
17455 int type;
17456 const struct mips16_immed_operand *op;
17457 offsetT val;
17458 int mintiny, maxtiny;
17459 segT symsec;
17460 fragS *sym_frag;
17461
17462 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17463 return 0;
17464 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17465 return 1;
17466
17467 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17468 op = mips16_immed_operands;
17469 while (op->type != type)
17470 {
17471 ++op;
17472 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
17473 }
17474
17475 if (op->unsp)
17476 {
17477 if (type == '<' || type == '>' || type == '[' || type == ']')
17478 {
17479 mintiny = 1;
17480 maxtiny = 1 << op->nbits;
17481 }
17482 else
17483 {
17484 mintiny = 0;
17485 maxtiny = (1 << op->nbits) - 1;
17486 }
17487 }
17488 else
17489 {
17490 mintiny = - (1 << (op->nbits - 1));
17491 maxtiny = (1 << (op->nbits - 1)) - 1;
17492 }
17493
17494 sym_frag = symbol_get_frag (fragp->fr_symbol);
17495 val = S_GET_VALUE (fragp->fr_symbol);
17496 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17497
17498 if (op->pcrel)
17499 {
17500 addressT addr;
17501
17502 /* We won't have the section when we are called from
17503 mips_relax_frag. However, we will always have been called
17504 from md_estimate_size_before_relax first. If this is a
17505 branch to a different section, we mark it as such. If SEC is
17506 NULL, and the frag is not marked, then it must be a branch to
17507 the same section. */
17508 if (sec == NULL)
17509 {
17510 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17511 return 1;
17512 }
17513 else
17514 {
17515 /* Must have been called from md_estimate_size_before_relax. */
17516 if (symsec != sec)
17517 {
17518 fragp->fr_subtype =
17519 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17520
17521 /* FIXME: We should support this, and let the linker
17522 catch branches and loads that are out of range. */
17523 as_bad_where (fragp->fr_file, fragp->fr_line,
17524 _("unsupported PC relative reference to different section"));
17525
17526 return 1;
17527 }
17528 if (fragp != sym_frag && sym_frag->fr_address == 0)
17529 /* Assume non-extended on the first relaxation pass.
17530 The address we have calculated will be bogus if this is
17531 a forward branch to another frag, as the forward frag
17532 will have fr_address == 0. */
17533 return 0;
17534 }
17535
17536 /* In this case, we know for sure that the symbol fragment is in
17537 the same section. If the relax_marker of the symbol fragment
17538 differs from the relax_marker of this fragment, we have not
17539 yet adjusted the symbol fragment fr_address. We want to add
17540 in STRETCH in order to get a better estimate of the address.
17541 This particularly matters because of the shift bits. */
17542 if (stretch != 0
17543 && sym_frag->relax_marker != fragp->relax_marker)
17544 {
17545 fragS *f;
17546
17547 /* Adjust stretch for any alignment frag. Note that if have
17548 been expanding the earlier code, the symbol may be
17549 defined in what appears to be an earlier frag. FIXME:
17550 This doesn't handle the fr_subtype field, which specifies
17551 a maximum number of bytes to skip when doing an
17552 alignment. */
17553 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17554 {
17555 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17556 {
17557 if (stretch < 0)
17558 stretch = - ((- stretch)
17559 & ~ ((1 << (int) f->fr_offset) - 1));
17560 else
17561 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17562 if (stretch == 0)
17563 break;
17564 }
17565 }
17566 if (f != NULL)
17567 val += stretch;
17568 }
17569
17570 addr = fragp->fr_address + fragp->fr_fix;
17571
17572 /* The base address rules are complicated. The base address of
17573 a branch is the following instruction. The base address of a
17574 PC relative load or add is the instruction itself, but if it
17575 is in a delay slot (in which case it can not be extended) use
17576 the address of the instruction whose delay slot it is in. */
17577 if (type == 'p' || type == 'q')
17578 {
17579 addr += 2;
17580
17581 /* If we are currently assuming that this frag should be
17582 extended, then, the current address is two bytes
17583 higher. */
17584 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17585 addr += 2;
17586
17587 /* Ignore the low bit in the target, since it will be set
17588 for a text label. */
17589 if ((val & 1) != 0)
17590 --val;
17591 }
17592 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17593 addr -= 4;
17594 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17595 addr -= 2;
17596
17597 val -= addr & ~ ((1 << op->shift) - 1);
17598
17599 /* Branch offsets have an implicit 0 in the lowest bit. */
17600 if (type == 'p' || type == 'q')
17601 val /= 2;
17602
17603 /* If any of the shifted bits are set, we must use an extended
17604 opcode. If the address depends on the size of this
17605 instruction, this can lead to a loop, so we arrange to always
17606 use an extended opcode. We only check this when we are in
17607 the main relaxation loop, when SEC is NULL. */
17608 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17609 {
17610 fragp->fr_subtype =
17611 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17612 return 1;
17613 }
17614
17615 /* If we are about to mark a frag as extended because the value
17616 is precisely maxtiny + 1, then there is a chance of an
17617 infinite loop as in the following code:
17618 la $4,foo
17619 .skip 1020
17620 .align 2
17621 foo:
17622 In this case when the la is extended, foo is 0x3fc bytes
17623 away, so the la can be shrunk, but then foo is 0x400 away, so
17624 the la must be extended. To avoid this loop, we mark the
17625 frag as extended if it was small, and is about to become
17626 extended with a value of maxtiny + 1. */
17627 if (val == ((maxtiny + 1) << op->shift)
17628 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17629 && sec == NULL)
17630 {
17631 fragp->fr_subtype =
17632 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17633 return 1;
17634 }
17635 }
17636 else if (symsec != absolute_section && sec != NULL)
17637 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17638
17639 if ((val & ((1 << op->shift) - 1)) != 0
17640 || val < (mintiny << op->shift)
17641 || val > (maxtiny << op->shift))
17642 return 1;
17643 else
17644 return 0;
17645 }
17646
17647 /* Compute the length of a branch sequence, and adjust the
17648 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17649 worst-case length is computed, with UPDATE being used to indicate
17650 whether an unconditional (-1), branch-likely (+1) or regular (0)
17651 branch is to be computed. */
17652 static int
17653 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17654 {
17655 bfd_boolean toofar;
17656 int length;
17657
17658 if (fragp
17659 && S_IS_DEFINED (fragp->fr_symbol)
17660 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17661 {
17662 addressT addr;
17663 offsetT val;
17664
17665 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17666
17667 addr = fragp->fr_address + fragp->fr_fix + 4;
17668
17669 val -= addr;
17670
17671 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17672 }
17673 else if (fragp)
17674 /* If the symbol is not defined or it's in a different segment,
17675 assume the user knows what's going on and emit a short
17676 branch. */
17677 toofar = FALSE;
17678 else
17679 toofar = TRUE;
17680
17681 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17682 fragp->fr_subtype
17683 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17684 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17685 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17686 RELAX_BRANCH_LINK (fragp->fr_subtype),
17687 toofar);
17688
17689 length = 4;
17690 if (toofar)
17691 {
17692 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17693 length += 8;
17694
17695 if (mips_pic != NO_PIC)
17696 {
17697 /* Additional space for PIC loading of target address. */
17698 length += 8;
17699 if (mips_opts.isa == ISA_MIPS1)
17700 /* Additional space for $at-stabilizing nop. */
17701 length += 4;
17702 }
17703
17704 /* If branch is conditional. */
17705 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17706 length += 8;
17707 }
17708
17709 return length;
17710 }
17711
17712 /* Compute the length of a branch sequence, and adjust the
17713 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17714 worst-case length is computed, with UPDATE being used to indicate
17715 whether an unconditional (-1), or regular (0) branch is to be
17716 computed. */
17717
17718 static int
17719 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17720 {
17721 bfd_boolean toofar;
17722 int length;
17723
17724 if (fragp
17725 && S_IS_DEFINED (fragp->fr_symbol)
17726 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17727 {
17728 addressT addr;
17729 offsetT val;
17730
17731 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17732 /* Ignore the low bit in the target, since it will be set
17733 for a text label. */
17734 if ((val & 1) != 0)
17735 --val;
17736
17737 addr = fragp->fr_address + fragp->fr_fix + 4;
17738
17739 val -= addr;
17740
17741 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17742 }
17743 else if (fragp)
17744 /* If the symbol is not defined or it's in a different segment,
17745 assume the user knows what's going on and emit a short
17746 branch. */
17747 toofar = FALSE;
17748 else
17749 toofar = TRUE;
17750
17751 if (fragp && update
17752 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17753 fragp->fr_subtype = (toofar
17754 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17755 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17756
17757 length = 4;
17758 if (toofar)
17759 {
17760 bfd_boolean compact_known = fragp != NULL;
17761 bfd_boolean compact = FALSE;
17762 bfd_boolean uncond;
17763
17764 if (compact_known)
17765 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17766 if (fragp)
17767 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17768 else
17769 uncond = update < 0;
17770
17771 /* If label is out of range, we turn branch <br>:
17772
17773 <br> label # 4 bytes
17774 0:
17775
17776 into:
17777
17778 j label # 4 bytes
17779 nop # 2 bytes if compact && !PIC
17780 0:
17781 */
17782 if (mips_pic == NO_PIC && (!compact_known || compact))
17783 length += 2;
17784
17785 /* If assembling PIC code, we further turn:
17786
17787 j label # 4 bytes
17788
17789 into:
17790
17791 lw/ld at, %got(label)(gp) # 4 bytes
17792 d/addiu at, %lo(label) # 4 bytes
17793 jr/c at # 2 bytes
17794 */
17795 if (mips_pic != NO_PIC)
17796 length += 6;
17797
17798 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17799
17800 <brneg> 0f # 4 bytes
17801 nop # 2 bytes if !compact
17802 */
17803 if (!uncond)
17804 length += (compact_known && compact) ? 4 : 6;
17805 }
17806
17807 return length;
17808 }
17809
17810 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17811 bit accordingly. */
17812
17813 static int
17814 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17815 {
17816 bfd_boolean toofar;
17817
17818 if (fragp
17819 && S_IS_DEFINED (fragp->fr_symbol)
17820 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17821 {
17822 addressT addr;
17823 offsetT val;
17824 int type;
17825
17826 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17827 /* Ignore the low bit in the target, since it will be set
17828 for a text label. */
17829 if ((val & 1) != 0)
17830 --val;
17831
17832 /* Assume this is a 2-byte branch. */
17833 addr = fragp->fr_address + fragp->fr_fix + 2;
17834
17835 /* We try to avoid the infinite loop by not adding 2 more bytes for
17836 long branches. */
17837
17838 val -= addr;
17839
17840 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17841 if (type == 'D')
17842 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17843 else if (type == 'E')
17844 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17845 else
17846 abort ();
17847 }
17848 else
17849 /* If the symbol is not defined or it's in a different segment,
17850 we emit a normal 32-bit branch. */
17851 toofar = TRUE;
17852
17853 if (fragp && update
17854 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17855 fragp->fr_subtype
17856 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17857 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17858
17859 if (toofar)
17860 return 4;
17861
17862 return 2;
17863 }
17864
17865 /* Estimate the size of a frag before relaxing. Unless this is the
17866 mips16, we are not really relaxing here, and the final size is
17867 encoded in the subtype information. For the mips16, we have to
17868 decide whether we are using an extended opcode or not. */
17869
17870 int
17871 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17872 {
17873 int change;
17874
17875 if (RELAX_BRANCH_P (fragp->fr_subtype))
17876 {
17877
17878 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17879
17880 return fragp->fr_var;
17881 }
17882
17883 if (RELAX_MIPS16_P (fragp->fr_subtype))
17884 /* We don't want to modify the EXTENDED bit here; it might get us
17885 into infinite loops. We change it only in mips_relax_frag(). */
17886 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
17887
17888 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17889 {
17890 int length = 4;
17891
17892 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17893 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17894 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17895 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17896 fragp->fr_var = length;
17897
17898 return length;
17899 }
17900
17901 if (mips_pic == NO_PIC)
17902 change = nopic_need_relax (fragp->fr_symbol, 0);
17903 else if (mips_pic == SVR4_PIC)
17904 change = pic_need_relax (fragp->fr_symbol, segtype);
17905 else if (mips_pic == VXWORKS_PIC)
17906 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17907 change = 0;
17908 else
17909 abort ();
17910
17911 if (change)
17912 {
17913 fragp->fr_subtype |= RELAX_USE_SECOND;
17914 return -RELAX_FIRST (fragp->fr_subtype);
17915 }
17916 else
17917 return -RELAX_SECOND (fragp->fr_subtype);
17918 }
17919
17920 /* This is called to see whether a reloc against a defined symbol
17921 should be converted into a reloc against a section. */
17922
17923 int
17924 mips_fix_adjustable (fixS *fixp)
17925 {
17926 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17927 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17928 return 0;
17929
17930 if (fixp->fx_addsy == NULL)
17931 return 1;
17932
17933 /* If symbol SYM is in a mergeable section, relocations of the form
17934 SYM + 0 can usually be made section-relative. The mergeable data
17935 is then identified by the section offset rather than by the symbol.
17936
17937 However, if we're generating REL LO16 relocations, the offset is split
17938 between the LO16 and parterning high part relocation. The linker will
17939 need to recalculate the complete offset in order to correctly identify
17940 the merge data.
17941
17942 The linker has traditionally not looked for the parterning high part
17943 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17944 placed anywhere. Rather than break backwards compatibility by changing
17945 this, it seems better not to force the issue, and instead keep the
17946 original symbol. This will work with either linker behavior. */
17947 if ((lo16_reloc_p (fixp->fx_r_type)
17948 || reloc_needs_lo_p (fixp->fx_r_type))
17949 && HAVE_IN_PLACE_ADDENDS
17950 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17951 return 0;
17952
17953 /* There is no place to store an in-place offset for JALR relocations.
17954 Likewise an in-range offset of limited PC-relative relocations may
17955 overflow the in-place relocatable field if recalculated against the
17956 start address of the symbol's containing section. */
17957 if (HAVE_IN_PLACE_ADDENDS
17958 && (limited_pcrel_reloc_p (fixp->fx_r_type)
17959 || jalr_reloc_p (fixp->fx_r_type)))
17960 return 0;
17961
17962 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17963 to a floating-point stub. The same is true for non-R_MIPS16_26
17964 relocations against MIPS16 functions; in this case, the stub becomes
17965 the function's canonical address.
17966
17967 Floating-point stubs are stored in unique .mips16.call.* or
17968 .mips16.fn.* sections. If a stub T for function F is in section S,
17969 the first relocation in section S must be against F; this is how the
17970 linker determines the target function. All relocations that might
17971 resolve to T must also be against F. We therefore have the following
17972 restrictions, which are given in an intentionally-redundant way:
17973
17974 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17975 symbols.
17976
17977 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17978 if that stub might be used.
17979
17980 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17981 symbols.
17982
17983 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17984 that stub might be used.
17985
17986 There is a further restriction:
17987
17988 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17989 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
17990 targets with in-place addends; the relocation field cannot
17991 encode the low bit.
17992
17993 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17994 against a MIPS16 symbol. We deal with (5) by by not reducing any
17995 such relocations on REL targets.
17996
17997 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17998 relocation against some symbol R, no relocation against R may be
17999 reduced. (Note that this deals with (2) as well as (1) because
18000 relocations against global symbols will never be reduced on ELF
18001 targets.) This approach is a little simpler than trying to detect
18002 stub sections, and gives the "all or nothing" per-symbol consistency
18003 that we have for MIPS16 symbols. */
18004 if (fixp->fx_subsy == NULL
18005 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
18006 || *symbol_get_tc (fixp->fx_addsy)
18007 || (HAVE_IN_PLACE_ADDENDS
18008 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
18009 && jmp_reloc_p (fixp->fx_r_type))))
18010 return 0;
18011
18012 return 1;
18013 }
18014
18015 /* Translate internal representation of relocation info to BFD target
18016 format. */
18017
18018 arelent **
18019 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
18020 {
18021 static arelent *retval[4];
18022 arelent *reloc;
18023 bfd_reloc_code_real_type code;
18024
18025 memset (retval, 0, sizeof(retval));
18026 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
18027 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
18028 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
18029 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18030
18031 if (fixp->fx_pcrel)
18032 {
18033 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
18034 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18035 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
18036 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
18037 || fixp->fx_r_type == BFD_RELOC_32_PCREL);
18038
18039 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18040 Relocations want only the symbol offset. */
18041 reloc->addend = fixp->fx_addnumber + reloc->address;
18042 }
18043 else
18044 reloc->addend = fixp->fx_addnumber;
18045
18046 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18047 entry to be used in the relocation's section offset. */
18048 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18049 {
18050 reloc->address = reloc->addend;
18051 reloc->addend = 0;
18052 }
18053
18054 code = fixp->fx_r_type;
18055
18056 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
18057 if (reloc->howto == NULL)
18058 {
18059 as_bad_where (fixp->fx_file, fixp->fx_line,
18060 _("Can not represent %s relocation in this object file format"),
18061 bfd_get_reloc_code_name (code));
18062 retval[0] = NULL;
18063 }
18064
18065 return retval;
18066 }
18067
18068 /* Relax a machine dependent frag. This returns the amount by which
18069 the current size of the frag should change. */
18070
18071 int
18072 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
18073 {
18074 if (RELAX_BRANCH_P (fragp->fr_subtype))
18075 {
18076 offsetT old_var = fragp->fr_var;
18077
18078 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
18079
18080 return fragp->fr_var - old_var;
18081 }
18082
18083 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18084 {
18085 offsetT old_var = fragp->fr_var;
18086 offsetT new_var = 4;
18087
18088 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18089 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18090 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18091 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18092 fragp->fr_var = new_var;
18093
18094 return new_var - old_var;
18095 }
18096
18097 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18098 return 0;
18099
18100 if (mips16_extended_frag (fragp, NULL, stretch))
18101 {
18102 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18103 return 0;
18104 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18105 return 2;
18106 }
18107 else
18108 {
18109 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18110 return 0;
18111 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18112 return -2;
18113 }
18114
18115 return 0;
18116 }
18117
18118 /* Convert a machine dependent frag. */
18119
18120 void
18121 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
18122 {
18123 if (RELAX_BRANCH_P (fragp->fr_subtype))
18124 {
18125 char *buf;
18126 unsigned long insn;
18127 expressionS exp;
18128 fixS *fixp;
18129
18130 buf = fragp->fr_literal + fragp->fr_fix;
18131 insn = read_insn (buf);
18132
18133 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18134 {
18135 /* We generate a fixup instead of applying it right now
18136 because, if there are linker relaxations, we're going to
18137 need the relocations. */
18138 exp.X_op = O_symbol;
18139 exp.X_add_symbol = fragp->fr_symbol;
18140 exp.X_add_number = fragp->fr_offset;
18141
18142 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18143 BFD_RELOC_16_PCREL_S2);
18144 fixp->fx_file = fragp->fr_file;
18145 fixp->fx_line = fragp->fr_line;
18146
18147 buf = write_insn (buf, insn);
18148 }
18149 else
18150 {
18151 int i;
18152
18153 as_warn_where (fragp->fr_file, fragp->fr_line,
18154 _("Relaxed out-of-range branch into a jump"));
18155
18156 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18157 goto uncond;
18158
18159 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18160 {
18161 /* Reverse the branch. */
18162 switch ((insn >> 28) & 0xf)
18163 {
18164 case 4:
18165 /* bc[0-3][tf]l? instructions can have the condition
18166 reversed by tweaking a single TF bit, and their
18167 opcodes all have 0x4???????. */
18168 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18169 insn ^= 0x00010000;
18170 break;
18171
18172 case 0:
18173 /* bltz 0x04000000 bgez 0x04010000
18174 bltzal 0x04100000 bgezal 0x04110000 */
18175 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
18176 insn ^= 0x00010000;
18177 break;
18178
18179 case 1:
18180 /* beq 0x10000000 bne 0x14000000
18181 blez 0x18000000 bgtz 0x1c000000 */
18182 insn ^= 0x04000000;
18183 break;
18184
18185 default:
18186 abort ();
18187 }
18188 }
18189
18190 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18191 {
18192 /* Clear the and-link bit. */
18193 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
18194
18195 /* bltzal 0x04100000 bgezal 0x04110000
18196 bltzall 0x04120000 bgezall 0x04130000 */
18197 insn &= ~0x00100000;
18198 }
18199
18200 /* Branch over the branch (if the branch was likely) or the
18201 full jump (not likely case). Compute the offset from the
18202 current instruction to branch to. */
18203 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18204 i = 16;
18205 else
18206 {
18207 /* How many bytes in instructions we've already emitted? */
18208 i = buf - fragp->fr_literal - fragp->fr_fix;
18209 /* How many bytes in instructions from here to the end? */
18210 i = fragp->fr_var - i;
18211 }
18212 /* Convert to instruction count. */
18213 i >>= 2;
18214 /* Branch counts from the next instruction. */
18215 i--;
18216 insn |= i;
18217 /* Branch over the jump. */
18218 buf = write_insn (buf, insn);
18219
18220 /* nop */
18221 buf = write_insn (buf, 0);
18222
18223 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18224 {
18225 /* beql $0, $0, 2f */
18226 insn = 0x50000000;
18227 /* Compute the PC offset from the current instruction to
18228 the end of the variable frag. */
18229 /* How many bytes in instructions we've already emitted? */
18230 i = buf - fragp->fr_literal - fragp->fr_fix;
18231 /* How many bytes in instructions from here to the end? */
18232 i = fragp->fr_var - i;
18233 /* Convert to instruction count. */
18234 i >>= 2;
18235 /* Don't decrement i, because we want to branch over the
18236 delay slot. */
18237 insn |= i;
18238
18239 buf = write_insn (buf, insn);
18240 buf = write_insn (buf, 0);
18241 }
18242
18243 uncond:
18244 if (mips_pic == NO_PIC)
18245 {
18246 /* j or jal. */
18247 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18248 ? 0x0c000000 : 0x08000000);
18249 exp.X_op = O_symbol;
18250 exp.X_add_symbol = fragp->fr_symbol;
18251 exp.X_add_number = fragp->fr_offset;
18252
18253 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18254 FALSE, BFD_RELOC_MIPS_JMP);
18255 fixp->fx_file = fragp->fr_file;
18256 fixp->fx_line = fragp->fr_line;
18257
18258 buf = write_insn (buf, insn);
18259 }
18260 else
18261 {
18262 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18263
18264 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
18265 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18266 insn |= at << OP_SH_RT;
18267 exp.X_op = O_symbol;
18268 exp.X_add_symbol = fragp->fr_symbol;
18269 exp.X_add_number = fragp->fr_offset;
18270
18271 if (fragp->fr_offset)
18272 {
18273 exp.X_add_symbol = make_expr_symbol (&exp);
18274 exp.X_add_number = 0;
18275 }
18276
18277 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18278 FALSE, BFD_RELOC_MIPS_GOT16);
18279 fixp->fx_file = fragp->fr_file;
18280 fixp->fx_line = fragp->fr_line;
18281
18282 buf = write_insn (buf, insn);
18283
18284 if (mips_opts.isa == ISA_MIPS1)
18285 /* nop */
18286 buf = write_insn (buf, 0);
18287
18288 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
18289 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18290 insn |= at << OP_SH_RS | at << OP_SH_RT;
18291
18292 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18293 FALSE, BFD_RELOC_LO16);
18294 fixp->fx_file = fragp->fr_file;
18295 fixp->fx_line = fragp->fr_line;
18296
18297 buf = write_insn (buf, insn);
18298
18299 /* j(al)r $at. */
18300 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18301 insn = 0x0000f809;
18302 else
18303 insn = 0x00000008;
18304 insn |= at << OP_SH_RS;
18305
18306 buf = write_insn (buf, insn);
18307 }
18308 }
18309
18310 fragp->fr_fix += fragp->fr_var;
18311 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18312 return;
18313 }
18314
18315 /* Relax microMIPS branches. */
18316 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18317 {
18318 char *buf = fragp->fr_literal + fragp->fr_fix;
18319 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18320 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18321 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18322 bfd_boolean short_ds;
18323 unsigned long insn;
18324 expressionS exp;
18325 fixS *fixp;
18326
18327 exp.X_op = O_symbol;
18328 exp.X_add_symbol = fragp->fr_symbol;
18329 exp.X_add_number = fragp->fr_offset;
18330
18331 fragp->fr_fix += fragp->fr_var;
18332
18333 /* Handle 16-bit branches that fit or are forced to fit. */
18334 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18335 {
18336 /* We generate a fixup instead of applying it right now,
18337 because if there is linker relaxation, we're going to
18338 need the relocations. */
18339 if (type == 'D')
18340 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18341 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18342 else if (type == 'E')
18343 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18344 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18345 else
18346 abort ();
18347
18348 fixp->fx_file = fragp->fr_file;
18349 fixp->fx_line = fragp->fr_line;
18350
18351 /* These relocations can have an addend that won't fit in
18352 2 octets. */
18353 fixp->fx_no_overflow = 1;
18354
18355 return;
18356 }
18357
18358 /* Handle 32-bit branches that fit or are forced to fit. */
18359 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18360 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18361 {
18362 /* We generate a fixup instead of applying it right now,
18363 because if there is linker relaxation, we're going to
18364 need the relocations. */
18365 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18366 BFD_RELOC_MICROMIPS_16_PCREL_S1);
18367 fixp->fx_file = fragp->fr_file;
18368 fixp->fx_line = fragp->fr_line;
18369
18370 if (type == 0)
18371 return;
18372 }
18373
18374 /* Relax 16-bit branches to 32-bit branches. */
18375 if (type != 0)
18376 {
18377 insn = read_compressed_insn (buf, 2);
18378
18379 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18380 insn = 0x94000000; /* beq */
18381 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18382 {
18383 unsigned long regno;
18384
18385 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18386 regno = micromips_to_32_reg_d_map [regno];
18387 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18388 insn |= regno << MICROMIPSOP_SH_RS;
18389 }
18390 else
18391 abort ();
18392
18393 /* Nothing else to do, just write it out. */
18394 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18395 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18396 {
18397 buf = write_compressed_insn (buf, insn, 4);
18398 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18399 return;
18400 }
18401 }
18402 else
18403 insn = read_compressed_insn (buf, 4);
18404
18405 /* Relax 32-bit branches to a sequence of instructions. */
18406 as_warn_where (fragp->fr_file, fragp->fr_line,
18407 _("Relaxed out-of-range branch into a jump"));
18408
18409 /* Set the short-delay-slot bit. */
18410 short_ds = al && (insn & 0x02000000) != 0;
18411
18412 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18413 {
18414 symbolS *l;
18415
18416 /* Reverse the branch. */
18417 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18418 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18419 insn ^= 0x20000000;
18420 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18421 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18422 || (insn & 0xffe00000) == 0x40800000 /* blez */
18423 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18424 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18425 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18426 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18427 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18428 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18429 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18430 insn ^= 0x00400000;
18431 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18432 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18433 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18434 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18435 insn ^= 0x00200000;
18436 else
18437 abort ();
18438
18439 if (al)
18440 {
18441 /* Clear the and-link and short-delay-slot bits. */
18442 gas_assert ((insn & 0xfda00000) == 0x40200000);
18443
18444 /* bltzal 0x40200000 bgezal 0x40600000 */
18445 /* bltzals 0x42200000 bgezals 0x42600000 */
18446 insn &= ~0x02200000;
18447 }
18448
18449 /* Make a label at the end for use with the branch. */
18450 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18451 micromips_label_inc ();
18452 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18453
18454 /* Refer to it. */
18455 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18456 BFD_RELOC_MICROMIPS_16_PCREL_S1);
18457 fixp->fx_file = fragp->fr_file;
18458 fixp->fx_line = fragp->fr_line;
18459
18460 /* Branch over the jump. */
18461 buf = write_compressed_insn (buf, insn, 4);
18462 if (!compact)
18463 /* nop */
18464 buf = write_compressed_insn (buf, 0x0c00, 2);
18465 }
18466
18467 if (mips_pic == NO_PIC)
18468 {
18469 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
18470
18471 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18472 insn = al ? jal : 0xd4000000;
18473
18474 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18475 BFD_RELOC_MICROMIPS_JMP);
18476 fixp->fx_file = fragp->fr_file;
18477 fixp->fx_line = fragp->fr_line;
18478
18479 buf = write_compressed_insn (buf, insn, 4);
18480 if (compact)
18481 /* nop */
18482 buf = write_compressed_insn (buf, 0x0c00, 2);
18483 }
18484 else
18485 {
18486 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18487 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18488 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
18489
18490 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18491 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18492 insn |= at << MICROMIPSOP_SH_RT;
18493
18494 if (exp.X_add_number)
18495 {
18496 exp.X_add_symbol = make_expr_symbol (&exp);
18497 exp.X_add_number = 0;
18498 }
18499
18500 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18501 BFD_RELOC_MICROMIPS_GOT16);
18502 fixp->fx_file = fragp->fr_file;
18503 fixp->fx_line = fragp->fr_line;
18504
18505 buf = write_compressed_insn (buf, insn, 4);
18506
18507 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18508 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18509 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18510
18511 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18512 BFD_RELOC_MICROMIPS_LO16);
18513 fixp->fx_file = fragp->fr_file;
18514 fixp->fx_line = fragp->fr_line;
18515
18516 buf = write_compressed_insn (buf, insn, 4);
18517
18518 /* jr/jrc/jalr/jalrs $at */
18519 insn = al ? jalr : jr;
18520 insn |= at << MICROMIPSOP_SH_MJ;
18521
18522 buf = write_compressed_insn (buf, insn, 2);
18523 }
18524
18525 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18526 return;
18527 }
18528
18529 if (RELAX_MIPS16_P (fragp->fr_subtype))
18530 {
18531 int type;
18532 const struct mips16_immed_operand *op;
18533 offsetT val;
18534 char *buf;
18535 unsigned int user_length, length;
18536 unsigned long insn;
18537 bfd_boolean ext;
18538
18539 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18540 op = mips16_immed_operands;
18541 while (op->type != type)
18542 ++op;
18543
18544 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
18545 val = resolve_symbol_value (fragp->fr_symbol);
18546 if (op->pcrel)
18547 {
18548 addressT addr;
18549
18550 addr = fragp->fr_address + fragp->fr_fix;
18551
18552 /* The rules for the base address of a PC relative reloc are
18553 complicated; see mips16_extended_frag. */
18554 if (type == 'p' || type == 'q')
18555 {
18556 addr += 2;
18557 if (ext)
18558 addr += 2;
18559 /* Ignore the low bit in the target, since it will be
18560 set for a text label. */
18561 if ((val & 1) != 0)
18562 --val;
18563 }
18564 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18565 addr -= 4;
18566 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18567 addr -= 2;
18568
18569 addr &= ~ (addressT) ((1 << op->shift) - 1);
18570 val -= addr;
18571
18572 /* Make sure the section winds up with the alignment we have
18573 assumed. */
18574 if (op->shift > 0)
18575 record_alignment (asec, op->shift);
18576 }
18577
18578 if (ext
18579 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18580 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18581 as_warn_where (fragp->fr_file, fragp->fr_line,
18582 _("extended instruction in delay slot"));
18583
18584 buf = fragp->fr_literal + fragp->fr_fix;
18585
18586 insn = read_compressed_insn (buf, 2);
18587 if (ext)
18588 insn |= MIPS16_EXTEND;
18589
18590 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18591 user_length = 4;
18592 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18593 user_length = 2;
18594 else
18595 user_length = 0;
18596
18597 mips16_immed (fragp->fr_file, fragp->fr_line, type,
18598 BFD_RELOC_UNUSED, val, user_length, &insn);
18599
18600 length = (ext ? 4 : 2);
18601 gas_assert (mips16_opcode_length (insn) == length);
18602 write_compressed_insn (buf, insn, length);
18603 fragp->fr_fix += length;
18604 }
18605 else
18606 {
18607 relax_substateT subtype = fragp->fr_subtype;
18608 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18609 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18610 int first, second;
18611 fixS *fixp;
18612
18613 first = RELAX_FIRST (subtype);
18614 second = RELAX_SECOND (subtype);
18615 fixp = (fixS *) fragp->fr_opcode;
18616
18617 /* If the delay slot chosen does not match the size of the instruction,
18618 then emit a warning. */
18619 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18620 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18621 {
18622 relax_substateT s;
18623 const char *msg;
18624
18625 s = subtype & (RELAX_DELAY_SLOT_16BIT
18626 | RELAX_DELAY_SLOT_SIZE_FIRST
18627 | RELAX_DELAY_SLOT_SIZE_SECOND);
18628 msg = macro_warning (s);
18629 if (msg != NULL)
18630 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18631 subtype &= ~s;
18632 }
18633
18634 /* Possibly emit a warning if we've chosen the longer option. */
18635 if (use_second == second_longer)
18636 {
18637 relax_substateT s;
18638 const char *msg;
18639
18640 s = (subtype
18641 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18642 msg = macro_warning (s);
18643 if (msg != NULL)
18644 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18645 subtype &= ~s;
18646 }
18647
18648 /* Go through all the fixups for the first sequence. Disable them
18649 (by marking them as done) if we're going to use the second
18650 sequence instead. */
18651 while (fixp
18652 && fixp->fx_frag == fragp
18653 && fixp->fx_where < fragp->fr_fix - second)
18654 {
18655 if (subtype & RELAX_USE_SECOND)
18656 fixp->fx_done = 1;
18657 fixp = fixp->fx_next;
18658 }
18659
18660 /* Go through the fixups for the second sequence. Disable them if
18661 we're going to use the first sequence, otherwise adjust their
18662 addresses to account for the relaxation. */
18663 while (fixp && fixp->fx_frag == fragp)
18664 {
18665 if (subtype & RELAX_USE_SECOND)
18666 fixp->fx_where -= first;
18667 else
18668 fixp->fx_done = 1;
18669 fixp = fixp->fx_next;
18670 }
18671
18672 /* Now modify the frag contents. */
18673 if (subtype & RELAX_USE_SECOND)
18674 {
18675 char *start;
18676
18677 start = fragp->fr_literal + fragp->fr_fix - first - second;
18678 memmove (start, start + first, second);
18679 fragp->fr_fix -= first;
18680 }
18681 else
18682 fragp->fr_fix -= second;
18683 }
18684 }
18685
18686 /* This function is called after the relocs have been generated.
18687 We've been storing mips16 text labels as odd. Here we convert them
18688 back to even for the convenience of the debugger. */
18689
18690 void
18691 mips_frob_file_after_relocs (void)
18692 {
18693 asymbol **syms;
18694 unsigned int count, i;
18695
18696 syms = bfd_get_outsymbols (stdoutput);
18697 count = bfd_get_symcount (stdoutput);
18698 for (i = 0; i < count; i++, syms++)
18699 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18700 && ((*syms)->value & 1) != 0)
18701 {
18702 (*syms)->value &= ~1;
18703 /* If the symbol has an odd size, it was probably computed
18704 incorrectly, so adjust that as well. */
18705 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18706 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18707 }
18708 }
18709
18710 /* This function is called whenever a label is defined, including fake
18711 labels instantiated off the dot special symbol. It is used when
18712 handling branch delays; if a branch has a label, we assume we cannot
18713 move it. This also bumps the value of the symbol by 1 in compressed
18714 code. */
18715
18716 static void
18717 mips_record_label (symbolS *sym)
18718 {
18719 segment_info_type *si = seg_info (now_seg);
18720 struct insn_label_list *l;
18721
18722 if (free_insn_labels == NULL)
18723 l = (struct insn_label_list *) xmalloc (sizeof *l);
18724 else
18725 {
18726 l = free_insn_labels;
18727 free_insn_labels = l->next;
18728 }
18729
18730 l->label = sym;
18731 l->next = si->label_list;
18732 si->label_list = l;
18733 }
18734
18735 /* This function is called as tc_frob_label() whenever a label is defined
18736 and adds a DWARF-2 record we only want for true labels. */
18737
18738 void
18739 mips_define_label (symbolS *sym)
18740 {
18741 mips_record_label (sym);
18742 dwarf2_emit_label (sym);
18743 }
18744
18745 /* This function is called by tc_new_dot_label whenever a new dot symbol
18746 is defined. */
18747
18748 void
18749 mips_add_dot_label (symbolS *sym)
18750 {
18751 mips_record_label (sym);
18752 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18753 mips_compressed_mark_label (sym);
18754 }
18755 \f
18756 /* Some special processing for a MIPS ELF file. */
18757
18758 void
18759 mips_elf_final_processing (void)
18760 {
18761 /* Write out the register information. */
18762 if (mips_abi != N64_ABI)
18763 {
18764 Elf32_RegInfo s;
18765
18766 s.ri_gprmask = mips_gprmask;
18767 s.ri_cprmask[0] = mips_cprmask[0];
18768 s.ri_cprmask[1] = mips_cprmask[1];
18769 s.ri_cprmask[2] = mips_cprmask[2];
18770 s.ri_cprmask[3] = mips_cprmask[3];
18771 /* The gp_value field is set by the MIPS ELF backend. */
18772
18773 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18774 ((Elf32_External_RegInfo *)
18775 mips_regmask_frag));
18776 }
18777 else
18778 {
18779 Elf64_Internal_RegInfo s;
18780
18781 s.ri_gprmask = mips_gprmask;
18782 s.ri_pad = 0;
18783 s.ri_cprmask[0] = mips_cprmask[0];
18784 s.ri_cprmask[1] = mips_cprmask[1];
18785 s.ri_cprmask[2] = mips_cprmask[2];
18786 s.ri_cprmask[3] = mips_cprmask[3];
18787 /* The gp_value field is set by the MIPS ELF backend. */
18788
18789 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18790 ((Elf64_External_RegInfo *)
18791 mips_regmask_frag));
18792 }
18793
18794 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
18795 sort of BFD interface for this. */
18796 if (mips_any_noreorder)
18797 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18798 if (mips_pic != NO_PIC)
18799 {
18800 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
18801 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18802 }
18803 if (mips_abicalls)
18804 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18805
18806 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
18807 defined at present; this might need to change in future. */
18808 if (file_ase_mips16)
18809 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
18810 if (file_ase_micromips)
18811 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
18812 if (file_ase & ASE_MDMX)
18813 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
18814
18815 /* Set the MIPS ELF ABI flags. */
18816 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
18817 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
18818 else if (mips_abi == O64_ABI)
18819 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
18820 else if (mips_abi == EABI_ABI)
18821 {
18822 if (!file_mips_gp32)
18823 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18824 else
18825 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18826 }
18827 else if (mips_abi == N32_ABI)
18828 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18829
18830 /* Nothing to do for N64_ABI. */
18831
18832 if (mips_32bitmode)
18833 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
18834
18835 #if 0 /* XXX FIXME */
18836 /* 32 bit code with 64 bit FP registers. */
18837 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18838 elf_elfheader (stdoutput)->e_flags |= ???;
18839 #endif
18840 }
18841 \f
18842 typedef struct proc {
18843 symbolS *func_sym;
18844 symbolS *func_end_sym;
18845 unsigned long reg_mask;
18846 unsigned long reg_offset;
18847 unsigned long fpreg_mask;
18848 unsigned long fpreg_offset;
18849 unsigned long frame_offset;
18850 unsigned long frame_reg;
18851 unsigned long pc_reg;
18852 } procS;
18853
18854 static procS cur_proc;
18855 static procS *cur_proc_ptr;
18856 static int numprocs;
18857
18858 /* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
18859 as "2", and a normal nop as "0". */
18860
18861 #define NOP_OPCODE_MIPS 0
18862 #define NOP_OPCODE_MIPS16 1
18863 #define NOP_OPCODE_MICROMIPS 2
18864
18865 char
18866 mips_nop_opcode (void)
18867 {
18868 if (seg_info (now_seg)->tc_segment_info_data.micromips)
18869 return NOP_OPCODE_MICROMIPS;
18870 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18871 return NOP_OPCODE_MIPS16;
18872 else
18873 return NOP_OPCODE_MIPS;
18874 }
18875
18876 /* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
18877 32-bit microMIPS NOPs here (if applicable). */
18878
18879 void
18880 mips_handle_align (fragS *fragp)
18881 {
18882 char nop_opcode;
18883 char *p;
18884 int bytes, size, excess;
18885 valueT opcode;
18886
18887 if (fragp->fr_type != rs_align_code)
18888 return;
18889
18890 p = fragp->fr_literal + fragp->fr_fix;
18891 nop_opcode = *p;
18892 switch (nop_opcode)
18893 {
18894 case NOP_OPCODE_MICROMIPS:
18895 opcode = micromips_nop32_insn.insn_opcode;
18896 size = 4;
18897 break;
18898 case NOP_OPCODE_MIPS16:
18899 opcode = mips16_nop_insn.insn_opcode;
18900 size = 2;
18901 break;
18902 case NOP_OPCODE_MIPS:
18903 default:
18904 opcode = nop_insn.insn_opcode;
18905 size = 4;
18906 break;
18907 }
18908
18909 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18910 excess = bytes % size;
18911
18912 /* Handle the leading part if we're not inserting a whole number of
18913 instructions, and make it the end of the fixed part of the frag.
18914 Try to fit in a short microMIPS NOP if applicable and possible,
18915 and use zeroes otherwise. */
18916 gas_assert (excess < 4);
18917 fragp->fr_fix += excess;
18918 switch (excess)
18919 {
18920 case 3:
18921 *p++ = '\0';
18922 /* Fall through. */
18923 case 2:
18924 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
18925 {
18926 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
18927 break;
18928 }
18929 *p++ = '\0';
18930 /* Fall through. */
18931 case 1:
18932 *p++ = '\0';
18933 /* Fall through. */
18934 case 0:
18935 break;
18936 }
18937
18938 md_number_to_chars (p, opcode, size);
18939 fragp->fr_var = size;
18940 }
18941
18942 static void
18943 md_obj_begin (void)
18944 {
18945 }
18946
18947 static void
18948 md_obj_end (void)
18949 {
18950 /* Check for premature end, nesting errors, etc. */
18951 if (cur_proc_ptr)
18952 as_warn (_("missing .end at end of assembly"));
18953 }
18954
18955 static long
18956 get_number (void)
18957 {
18958 int negative = 0;
18959 long val = 0;
18960
18961 if (*input_line_pointer == '-')
18962 {
18963 ++input_line_pointer;
18964 negative = 1;
18965 }
18966 if (!ISDIGIT (*input_line_pointer))
18967 as_bad (_("expected simple number"));
18968 if (input_line_pointer[0] == '0')
18969 {
18970 if (input_line_pointer[1] == 'x')
18971 {
18972 input_line_pointer += 2;
18973 while (ISXDIGIT (*input_line_pointer))
18974 {
18975 val <<= 4;
18976 val |= hex_value (*input_line_pointer++);
18977 }
18978 return negative ? -val : val;
18979 }
18980 else
18981 {
18982 ++input_line_pointer;
18983 while (ISDIGIT (*input_line_pointer))
18984 {
18985 val <<= 3;
18986 val |= *input_line_pointer++ - '0';
18987 }
18988 return negative ? -val : val;
18989 }
18990 }
18991 if (!ISDIGIT (*input_line_pointer))
18992 {
18993 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18994 *input_line_pointer, *input_line_pointer);
18995 as_warn (_("invalid number"));
18996 return -1;
18997 }
18998 while (ISDIGIT (*input_line_pointer))
18999 {
19000 val *= 10;
19001 val += *input_line_pointer++ - '0';
19002 }
19003 return negative ? -val : val;
19004 }
19005
19006 /* The .file directive; just like the usual .file directive, but there
19007 is an initial number which is the ECOFF file index. In the non-ECOFF
19008 case .file implies DWARF-2. */
19009
19010 static void
19011 s_mips_file (int x ATTRIBUTE_UNUSED)
19012 {
19013 static int first_file_directive = 0;
19014
19015 if (ECOFF_DEBUGGING)
19016 {
19017 get_number ();
19018 s_app_file (0);
19019 }
19020 else
19021 {
19022 char *filename;
19023
19024 filename = dwarf2_directive_file (0);
19025
19026 /* Versions of GCC up to 3.1 start files with a ".file"
19027 directive even for stabs output. Make sure that this
19028 ".file" is handled. Note that you need a version of GCC
19029 after 3.1 in order to support DWARF-2 on MIPS. */
19030 if (filename != NULL && ! first_file_directive)
19031 {
19032 (void) new_logical_line (filename, -1);
19033 s_app_file_string (filename, 0);
19034 }
19035 first_file_directive = 1;
19036 }
19037 }
19038
19039 /* The .loc directive, implying DWARF-2. */
19040
19041 static void
19042 s_mips_loc (int x ATTRIBUTE_UNUSED)
19043 {
19044 if (!ECOFF_DEBUGGING)
19045 dwarf2_directive_loc (0);
19046 }
19047
19048 /* The .end directive. */
19049
19050 static void
19051 s_mips_end (int x ATTRIBUTE_UNUSED)
19052 {
19053 symbolS *p;
19054
19055 /* Following functions need their own .frame and .cprestore directives. */
19056 mips_frame_reg_valid = 0;
19057 mips_cprestore_valid = 0;
19058
19059 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19060 {
19061 p = get_symbol ();
19062 demand_empty_rest_of_line ();
19063 }
19064 else
19065 p = NULL;
19066
19067 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19068 as_warn (_(".end not in text section"));
19069
19070 if (!cur_proc_ptr)
19071 {
19072 as_warn (_(".end directive without a preceding .ent directive."));
19073 demand_empty_rest_of_line ();
19074 return;
19075 }
19076
19077 if (p != NULL)
19078 {
19079 gas_assert (S_GET_NAME (p));
19080 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
19081 as_warn (_(".end symbol does not match .ent symbol."));
19082
19083 if (debug_type == DEBUG_STABS)
19084 stabs_generate_asm_endfunc (S_GET_NAME (p),
19085 S_GET_NAME (p));
19086 }
19087 else
19088 as_warn (_(".end directive missing or unknown symbol"));
19089
19090 /* Create an expression to calculate the size of the function. */
19091 if (p && cur_proc_ptr)
19092 {
19093 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19094 expressionS *exp = xmalloc (sizeof (expressionS));
19095
19096 obj->size = exp;
19097 exp->X_op = O_subtract;
19098 exp->X_add_symbol = symbol_temp_new_now ();
19099 exp->X_op_symbol = p;
19100 exp->X_add_number = 0;
19101
19102 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19103 }
19104
19105 /* Generate a .pdr section. */
19106 if (!ECOFF_DEBUGGING && mips_flag_pdr)
19107 {
19108 segT saved_seg = now_seg;
19109 subsegT saved_subseg = now_subseg;
19110 expressionS exp;
19111 char *fragp;
19112
19113 #ifdef md_flush_pending_output
19114 md_flush_pending_output ();
19115 #endif
19116
19117 gas_assert (pdr_seg);
19118 subseg_set (pdr_seg, 0);
19119
19120 /* Write the symbol. */
19121 exp.X_op = O_symbol;
19122 exp.X_add_symbol = p;
19123 exp.X_add_number = 0;
19124 emit_expr (&exp, 4);
19125
19126 fragp = frag_more (7 * 4);
19127
19128 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19129 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19130 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19131 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19132 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19133 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19134 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19135
19136 subseg_set (saved_seg, saved_subseg);
19137 }
19138
19139 cur_proc_ptr = NULL;
19140 }
19141
19142 /* The .aent and .ent directives. */
19143
19144 static void
19145 s_mips_ent (int aent)
19146 {
19147 symbolS *symbolP;
19148
19149 symbolP = get_symbol ();
19150 if (*input_line_pointer == ',')
19151 ++input_line_pointer;
19152 SKIP_WHITESPACE ();
19153 if (ISDIGIT (*input_line_pointer)
19154 || *input_line_pointer == '-')
19155 get_number ();
19156
19157 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19158 as_warn (_(".ent or .aent not in text section."));
19159
19160 if (!aent && cur_proc_ptr)
19161 as_warn (_("missing .end"));
19162
19163 if (!aent)
19164 {
19165 /* This function needs its own .frame and .cprestore directives. */
19166 mips_frame_reg_valid = 0;
19167 mips_cprestore_valid = 0;
19168
19169 cur_proc_ptr = &cur_proc;
19170 memset (cur_proc_ptr, '\0', sizeof (procS));
19171
19172 cur_proc_ptr->func_sym = symbolP;
19173
19174 ++numprocs;
19175
19176 if (debug_type == DEBUG_STABS)
19177 stabs_generate_asm_func (S_GET_NAME (symbolP),
19178 S_GET_NAME (symbolP));
19179 }
19180
19181 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19182
19183 demand_empty_rest_of_line ();
19184 }
19185
19186 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19187 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19188 s_mips_frame is used so that we can set the PDR information correctly.
19189 We can't use the ecoff routines because they make reference to the ecoff
19190 symbol table (in the mdebug section). */
19191
19192 static void
19193 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19194 {
19195 if (ECOFF_DEBUGGING)
19196 s_ignore (ignore);
19197 else
19198 {
19199 long val;
19200
19201 if (cur_proc_ptr == (procS *) NULL)
19202 {
19203 as_warn (_(".frame outside of .ent"));
19204 demand_empty_rest_of_line ();
19205 return;
19206 }
19207
19208 cur_proc_ptr->frame_reg = tc_get_register (1);
19209
19210 SKIP_WHITESPACE ();
19211 if (*input_line_pointer++ != ','
19212 || get_absolute_expression_and_terminator (&val) != ',')
19213 {
19214 as_warn (_("Bad .frame directive"));
19215 --input_line_pointer;
19216 demand_empty_rest_of_line ();
19217 return;
19218 }
19219
19220 cur_proc_ptr->frame_offset = val;
19221 cur_proc_ptr->pc_reg = tc_get_register (0);
19222
19223 demand_empty_rest_of_line ();
19224 }
19225 }
19226
19227 /* The .fmask and .mask directives. If the mdebug section is present
19228 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19229 embedded targets, s_mips_mask is used so that we can set the PDR
19230 information correctly. We can't use the ecoff routines because they
19231 make reference to the ecoff symbol table (in the mdebug section). */
19232
19233 static void
19234 s_mips_mask (int reg_type)
19235 {
19236 if (ECOFF_DEBUGGING)
19237 s_ignore (reg_type);
19238 else
19239 {
19240 long mask, off;
19241
19242 if (cur_proc_ptr == (procS *) NULL)
19243 {
19244 as_warn (_(".mask/.fmask outside of .ent"));
19245 demand_empty_rest_of_line ();
19246 return;
19247 }
19248
19249 if (get_absolute_expression_and_terminator (&mask) != ',')
19250 {
19251 as_warn (_("Bad .mask/.fmask directive"));
19252 --input_line_pointer;
19253 demand_empty_rest_of_line ();
19254 return;
19255 }
19256
19257 off = get_absolute_expression ();
19258
19259 if (reg_type == 'F')
19260 {
19261 cur_proc_ptr->fpreg_mask = mask;
19262 cur_proc_ptr->fpreg_offset = off;
19263 }
19264 else
19265 {
19266 cur_proc_ptr->reg_mask = mask;
19267 cur_proc_ptr->reg_offset = off;
19268 }
19269
19270 demand_empty_rest_of_line ();
19271 }
19272 }
19273
19274 /* A table describing all the processors gas knows about. Names are
19275 matched in the order listed.
19276
19277 To ease comparison, please keep this table in the same order as
19278 gcc's mips_cpu_info_table[]. */
19279 static const struct mips_cpu_info mips_cpu_info_table[] =
19280 {
19281 /* Entries for generic ISAs */
19282 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19283 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19284 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19285 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19286 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19287 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19288 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19289 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19290 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
19291
19292 /* MIPS I */
19293 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19294 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19295 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
19296
19297 /* MIPS II */
19298 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
19299
19300 /* MIPS III */
19301 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19302 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19303 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19304 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19305 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19306 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19307 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19308 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19309 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19310 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19311 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19312 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19313 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
19314 /* ST Microelectronics Loongson 2E and 2F cores */
19315 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19316 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
19317
19318 /* MIPS IV */
19319 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19320 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19321 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19322 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19323 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19324 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19325 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19326 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19327 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19328 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19329 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19330 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19331 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19332 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19333 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
19334
19335 /* MIPS 32 */
19336 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19337 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19338 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19339 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
19340
19341 /* MIPS 32 Release 2 */
19342 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19343 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19344 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19345 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19346 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19347 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19348 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19349 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19350 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19351 ISA_MIPS32R2, CPU_MIPS32R2 },
19352 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19353 ISA_MIPS32R2, CPU_MIPS32R2 },
19354 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19355 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19356 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19357 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19358 /* Deprecated forms of the above. */
19359 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19360 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19361 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
19362 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19363 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19364 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19365 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19366 /* Deprecated forms of the above. */
19367 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19368 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19369 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
19370 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19371 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19372 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19373 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19374 /* Deprecated forms of the above. */
19375 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19376 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19377 /* 34Kn is a 34kc without DSP. */
19378 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19379 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
19380 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19381 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19382 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19383 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19384 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19385 /* Deprecated forms of the above. */
19386 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19387 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19388 /* 1004K cores are multiprocessor versions of the 34K. */
19389 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19390 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19391 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19392 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19393
19394 /* MIPS 64 */
19395 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19396 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19397 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19398 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19399
19400 /* Broadcom SB-1 CPU core */
19401 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19402 /* Broadcom SB-1A CPU core */
19403 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
19404
19405 { "loongson3a", 0, 0, ISA_MIPS64, CPU_LOONGSON_3A },
19406
19407 /* MIPS 64 Release 2 */
19408
19409 /* Cavium Networks Octeon CPU core */
19410 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19411 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19412 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
19413
19414 /* RMI Xlr */
19415 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
19416
19417 /* Broadcom XLP.
19418 XLP is mostly like XLR, with the prominent exception that it is
19419 MIPS64R2 rather than MIPS64. */
19420 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
19421
19422 /* End marker */
19423 { NULL, 0, 0, 0, 0 }
19424 };
19425
19426
19427 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19428 with a final "000" replaced by "k". Ignore case.
19429
19430 Note: this function is shared between GCC and GAS. */
19431
19432 static bfd_boolean
19433 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19434 {
19435 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19436 given++, canonical++;
19437
19438 return ((*given == 0 && *canonical == 0)
19439 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19440 }
19441
19442
19443 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19444 CPU name. We've traditionally allowed a lot of variation here.
19445
19446 Note: this function is shared between GCC and GAS. */
19447
19448 static bfd_boolean
19449 mips_matching_cpu_name_p (const char *canonical, const char *given)
19450 {
19451 /* First see if the name matches exactly, or with a final "000"
19452 turned into "k". */
19453 if (mips_strict_matching_cpu_name_p (canonical, given))
19454 return TRUE;
19455
19456 /* If not, try comparing based on numerical designation alone.
19457 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19458 if (TOLOWER (*given) == 'r')
19459 given++;
19460 if (!ISDIGIT (*given))
19461 return FALSE;
19462
19463 /* Skip over some well-known prefixes in the canonical name,
19464 hoping to find a number there too. */
19465 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19466 canonical += 2;
19467 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19468 canonical += 2;
19469 else if (TOLOWER (canonical[0]) == 'r')
19470 canonical += 1;
19471
19472 return mips_strict_matching_cpu_name_p (canonical, given);
19473 }
19474
19475
19476 /* Parse an option that takes the name of a processor as its argument.
19477 OPTION is the name of the option and CPU_STRING is the argument.
19478 Return the corresponding processor enumeration if the CPU_STRING is
19479 recognized, otherwise report an error and return null.
19480
19481 A similar function exists in GCC. */
19482
19483 static const struct mips_cpu_info *
19484 mips_parse_cpu (const char *option, const char *cpu_string)
19485 {
19486 const struct mips_cpu_info *p;
19487
19488 /* 'from-abi' selects the most compatible architecture for the given
19489 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19490 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19491 version. Look first at the -mgp options, if given, otherwise base
19492 the choice on MIPS_DEFAULT_64BIT.
19493
19494 Treat NO_ABI like the EABIs. One reason to do this is that the
19495 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19496 architecture. This code picks MIPS I for 'mips' and MIPS III for
19497 'mips64', just as we did in the days before 'from-abi'. */
19498 if (strcasecmp (cpu_string, "from-abi") == 0)
19499 {
19500 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19501 return mips_cpu_info_from_isa (ISA_MIPS1);
19502
19503 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19504 return mips_cpu_info_from_isa (ISA_MIPS3);
19505
19506 if (file_mips_gp32 >= 0)
19507 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19508
19509 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19510 ? ISA_MIPS3
19511 : ISA_MIPS1);
19512 }
19513
19514 /* 'default' has traditionally been a no-op. Probably not very useful. */
19515 if (strcasecmp (cpu_string, "default") == 0)
19516 return 0;
19517
19518 for (p = mips_cpu_info_table; p->name != 0; p++)
19519 if (mips_matching_cpu_name_p (p->name, cpu_string))
19520 return p;
19521
19522 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
19523 return 0;
19524 }
19525
19526 /* Return the canonical processor information for ISA (a member of the
19527 ISA_MIPS* enumeration). */
19528
19529 static const struct mips_cpu_info *
19530 mips_cpu_info_from_isa (int isa)
19531 {
19532 int i;
19533
19534 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19535 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
19536 && isa == mips_cpu_info_table[i].isa)
19537 return (&mips_cpu_info_table[i]);
19538
19539 return NULL;
19540 }
19541
19542 static const struct mips_cpu_info *
19543 mips_cpu_info_from_arch (int arch)
19544 {
19545 int i;
19546
19547 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19548 if (arch == mips_cpu_info_table[i].cpu)
19549 return (&mips_cpu_info_table[i]);
19550
19551 return NULL;
19552 }
19553 \f
19554 static void
19555 show (FILE *stream, const char *string, int *col_p, int *first_p)
19556 {
19557 if (*first_p)
19558 {
19559 fprintf (stream, "%24s", "");
19560 *col_p = 24;
19561 }
19562 else
19563 {
19564 fprintf (stream, ", ");
19565 *col_p += 2;
19566 }
19567
19568 if (*col_p + strlen (string) > 72)
19569 {
19570 fprintf (stream, "\n%24s", "");
19571 *col_p = 24;
19572 }
19573
19574 fprintf (stream, "%s", string);
19575 *col_p += strlen (string);
19576
19577 *first_p = 0;
19578 }
19579
19580 void
19581 md_show_usage (FILE *stream)
19582 {
19583 int column, first;
19584 size_t i;
19585
19586 fprintf (stream, _("\
19587 MIPS options:\n\
19588 -EB generate big endian output\n\
19589 -EL generate little endian output\n\
19590 -g, -g2 do not remove unneeded NOPs or swap branches\n\
19591 -G NUM allow referencing objects up to NUM bytes\n\
19592 implicitly with the gp register [default 8]\n"));
19593 fprintf (stream, _("\
19594 -mips1 generate MIPS ISA I instructions\n\
19595 -mips2 generate MIPS ISA II instructions\n\
19596 -mips3 generate MIPS ISA III instructions\n\
19597 -mips4 generate MIPS ISA IV instructions\n\
19598 -mips5 generate MIPS ISA V instructions\n\
19599 -mips32 generate MIPS32 ISA instructions\n\
19600 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
19601 -mips64 generate MIPS64 ISA instructions\n\
19602 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
19603 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19604
19605 first = 1;
19606
19607 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19608 show (stream, mips_cpu_info_table[i].name, &column, &first);
19609 show (stream, "from-abi", &column, &first);
19610 fputc ('\n', stream);
19611
19612 fprintf (stream, _("\
19613 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19614 -no-mCPU don't generate code specific to CPU.\n\
19615 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19616
19617 first = 1;
19618
19619 show (stream, "3900", &column, &first);
19620 show (stream, "4010", &column, &first);
19621 show (stream, "4100", &column, &first);
19622 show (stream, "4650", &column, &first);
19623 fputc ('\n', stream);
19624
19625 fprintf (stream, _("\
19626 -mips16 generate mips16 instructions\n\
19627 -no-mips16 do not generate mips16 instructions\n"));
19628 fprintf (stream, _("\
19629 -mmicromips generate microMIPS instructions\n\
19630 -mno-micromips do not generate microMIPS instructions\n"));
19631 fprintf (stream, _("\
19632 -msmartmips generate smartmips instructions\n\
19633 -mno-smartmips do not generate smartmips instructions\n"));
19634 fprintf (stream, _("\
19635 -mdsp generate DSP instructions\n\
19636 -mno-dsp do not generate DSP instructions\n"));
19637 fprintf (stream, _("\
19638 -mdspr2 generate DSP R2 instructions\n\
19639 -mno-dspr2 do not generate DSP R2 instructions\n"));
19640 fprintf (stream, _("\
19641 -mmt generate MT instructions\n\
19642 -mno-mt do not generate MT instructions\n"));
19643 fprintf (stream, _("\
19644 -mmcu generate MCU instructions\n\
19645 -mno-mcu do not generate MCU instructions\n"));
19646 fprintf (stream, _("\
19647 -mvirt generate Virtualization instructions\n\
19648 -mno-virt do not generate Virtualization instructions\n"));
19649 fprintf (stream, _("\
19650 -minsn32 only generate 32-bit microMIPS instructions\n\
19651 -mno-insn32 generate all microMIPS instructions\n"));
19652 fprintf (stream, _("\
19653 -mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19654 -mfix-loongson2f-nop work around Loongson2F NOP errata\n\
19655 -mfix-vr4120 work around certain VR4120 errata\n\
19656 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
19657 -mfix-24k insert a nop after ERET and DERET instructions\n\
19658 -mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
19659 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19660 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
19661 -msym32 assume all symbols have 32-bit values\n\
19662 -O0 remove unneeded NOPs, do not swap branches\n\
19663 -O remove unneeded NOPs and swap branches\n\
19664 --trap, --no-break trap exception on div by 0 and mult overflow\n\
19665 --break, --no-trap break exception on div by 0 and mult overflow\n"));
19666 fprintf (stream, _("\
19667 -mhard-float allow floating-point instructions\n\
19668 -msoft-float do not allow floating-point instructions\n\
19669 -msingle-float only allow 32-bit floating-point operations\n\
19670 -mdouble-float allow 32-bit and 64-bit floating-point operations\n\
19671 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
19672 --[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n"
19673 ));
19674 fprintf (stream, _("\
19675 -KPIC, -call_shared generate SVR4 position independent code\n\
19676 -call_nonpic generate non-PIC code that can operate with DSOs\n\
19677 -mvxworks-pic generate VxWorks position independent code\n\
19678 -non_shared do not generate code that can operate with DSOs\n\
19679 -xgot assume a 32 bit GOT\n\
19680 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
19681 -mshared, -mno-shared disable/enable .cpload optimization for\n\
19682 position dependent (non shared) code\n\
19683 -mabi=ABI create ABI conformant object file for:\n"));
19684
19685 first = 1;
19686
19687 show (stream, "32", &column, &first);
19688 show (stream, "o64", &column, &first);
19689 show (stream, "n32", &column, &first);
19690 show (stream, "64", &column, &first);
19691 show (stream, "eabi", &column, &first);
19692
19693 fputc ('\n', stream);
19694
19695 fprintf (stream, _("\
19696 -32 create o32 ABI object file (default)\n\
19697 -n32 create n32 ABI object file\n\
19698 -64 create 64 ABI object file\n"));
19699 }
19700
19701 #ifdef TE_IRIX
19702 enum dwarf2_format
19703 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
19704 {
19705 if (HAVE_64BIT_SYMBOLS)
19706 return dwarf2_format_64bit_irix;
19707 else
19708 return dwarf2_format_32bit;
19709 }
19710 #endif
19711
19712 int
19713 mips_dwarf2_addr_size (void)
19714 {
19715 if (HAVE_64BIT_OBJECTS)
19716 return 8;
19717 else
19718 return 4;
19719 }
19720
19721 /* Standard calling conventions leave the CFA at SP on entry. */
19722 void
19723 mips_cfi_frame_initial_instructions (void)
19724 {
19725 cfi_add_CFA_def_cfa_register (SP);
19726 }
19727
19728 int
19729 tc_mips_regname_to_dw2regnum (char *regname)
19730 {
19731 unsigned int regnum = -1;
19732 unsigned int reg;
19733
19734 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19735 regnum = reg;
19736
19737 return regnum;
19738 }