[ gas/ChangeLog ]
[deliverable/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 Free Software Foundation, Inc.
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
24 02110-1301, USA. */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #include "opcode/mips.h"
32 #include "itbl-ops.h"
33 #include "dwarf2dbg.h"
34 #include "dw2gencfi.h"
35
36 #ifdef DEBUG
37 #define DBG(x) printf x
38 #else
39 #define DBG(x)
40 #endif
41
42 #ifdef OBJ_MAYBE_ELF
43 /* Clean up namespace so we can include obj-elf.h too. */
44 static int mips_output_flavor (void);
45 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
46 #undef OBJ_PROCESS_STAB
47 #undef OUTPUT_FLAVOR
48 #undef S_GET_ALIGN
49 #undef S_GET_SIZE
50 #undef S_SET_ALIGN
51 #undef S_SET_SIZE
52 #undef obj_frob_file
53 #undef obj_frob_file_after_relocs
54 #undef obj_frob_symbol
55 #undef obj_pop_insert
56 #undef obj_sec_sym_ok_for_reloc
57 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
58
59 #include "obj-elf.h"
60 /* Fix any of them that we actually care about. */
61 #undef OUTPUT_FLAVOR
62 #define OUTPUT_FLAVOR mips_output_flavor()
63 #endif
64
65 #if defined (OBJ_ELF)
66 #include "elf/mips.h"
67 #endif
68
69 #ifndef ECOFF_DEBUGGING
70 #define NO_ECOFF_DEBUGGING
71 #define ECOFF_DEBUGGING 0
72 #endif
73
74 int mips_flag_mdebug = -1;
75
76 /* Control generation of .pdr sections. Off by default on IRIX: the native
77 linker doesn't know about and discards them, but relocations against them
78 remain, leading to rld crashes. */
79 #ifdef TE_IRIX
80 int mips_flag_pdr = FALSE;
81 #else
82 int mips_flag_pdr = TRUE;
83 #endif
84
85 #include "ecoff.h"
86
87 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
88 static char *mips_regmask_frag;
89 #endif
90
91 #define ZERO 0
92 #define AT 1
93 #define TREG 24
94 #define PIC_CALL_REG 25
95 #define KT0 26
96 #define KT1 27
97 #define GP 28
98 #define SP 29
99 #define FP 30
100 #define RA 31
101
102 #define ILLEGAL_REG (32)
103
104 /* Allow override of standard little-endian ECOFF format. */
105
106 #ifndef ECOFF_LITTLE_FORMAT
107 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
108 #endif
109
110 extern int target_big_endian;
111
112 /* The name of the readonly data section. */
113 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
114 ? ".rdata" \
115 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
116 ? ".rdata" \
117 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
118 ? ".rodata" \
119 : (abort (), ""))
120
121 /* Information about an instruction, including its format, operands
122 and fixups. */
123 struct mips_cl_insn
124 {
125 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
126 const struct mips_opcode *insn_mo;
127
128 /* True if this is a mips16 instruction and if we want the extended
129 form of INSN_MO. */
130 bfd_boolean use_extend;
131
132 /* The 16-bit extension instruction to use when USE_EXTEND is true. */
133 unsigned short extend;
134
135 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
136 a copy of INSN_MO->match with the operands filled in. */
137 unsigned long insn_opcode;
138
139 /* The frag that contains the instruction. */
140 struct frag *frag;
141
142 /* The offset into FRAG of the first instruction byte. */
143 long where;
144
145 /* The relocs associated with the instruction, if any. */
146 fixS *fixp[3];
147
148 /* True if this entry cannot be moved from its current position. */
149 unsigned int fixed_p : 1;
150
151 /* True if this instruction occurred in a .set noreorder block. */
152 unsigned int noreorder_p : 1;
153
154 /* True for mips16 instructions that jump to an absolute address. */
155 unsigned int mips16_absolute_jump_p : 1;
156 };
157
158 /* The ABI to use. */
159 enum mips_abi_level
160 {
161 NO_ABI = 0,
162 O32_ABI,
163 O64_ABI,
164 N32_ABI,
165 N64_ABI,
166 EABI_ABI
167 };
168
169 /* MIPS ABI we are using for this output file. */
170 static enum mips_abi_level mips_abi = NO_ABI;
171
172 /* Whether or not we have code that can call pic code. */
173 int mips_abicalls = FALSE;
174
175 /* Whether or not we have code which can be put into a shared
176 library. */
177 static bfd_boolean mips_in_shared = TRUE;
178
179 /* This is the set of options which may be modified by the .set
180 pseudo-op. We use a struct so that .set push and .set pop are more
181 reliable. */
182
183 struct mips_set_options
184 {
185 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
186 if it has not been initialized. Changed by `.set mipsN', and the
187 -mipsN command line option, and the default CPU. */
188 int isa;
189 /* Enabled Application Specific Extensions (ASEs). These are set to -1
190 if they have not been initialized. Changed by `.set <asename>', by
191 command line options, and based on the default architecture. */
192 int ase_mips3d;
193 int ase_mdmx;
194 int ase_smartmips;
195 int ase_dsp;
196 int ase_mt;
197 /* Whether we are assembling for the mips16 processor. 0 if we are
198 not, 1 if we are, and -1 if the value has not been initialized.
199 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
200 -nomips16 command line options, and the default CPU. */
201 int mips16;
202 /* Non-zero if we should not reorder instructions. Changed by `.set
203 reorder' and `.set noreorder'. */
204 int noreorder;
205 /* Non-zero if we should not permit the $at ($1) register to be used
206 in instructions. Changed by `.set at' and `.set noat'. */
207 int noat;
208 /* Non-zero if we should warn when a macro instruction expands into
209 more than one machine instruction. Changed by `.set nomacro' and
210 `.set macro'. */
211 int warn_about_macros;
212 /* Non-zero if we should not move instructions. Changed by `.set
213 move', `.set volatile', `.set nomove', and `.set novolatile'. */
214 int nomove;
215 /* Non-zero if we should not optimize branches by moving the target
216 of the branch into the delay slot. Actually, we don't perform
217 this optimization anyhow. Changed by `.set bopt' and `.set
218 nobopt'. */
219 int nobopt;
220 /* Non-zero if we should not autoextend mips16 instructions.
221 Changed by `.set autoextend' and `.set noautoextend'. */
222 int noautoextend;
223 /* Restrict general purpose registers and floating point registers
224 to 32 bit. This is initially determined when -mgp32 or -mfp32
225 is passed but can changed if the assembler code uses .set mipsN. */
226 int gp32;
227 int fp32;
228 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
229 command line option, and the default CPU. */
230 int arch;
231 /* True if ".set sym32" is in effect. */
232 bfd_boolean sym32;
233 };
234
235 /* True if -mgp32 was passed. */
236 static int file_mips_gp32 = -1;
237
238 /* True if -mfp32 was passed. */
239 static int file_mips_fp32 = -1;
240
241 /* This is the struct we use to hold the current set of options. Note
242 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
243 -1 to indicate that they have not been initialized. */
244
245 static struct mips_set_options mips_opts =
246 {
247 ISA_UNKNOWN, -1, -1, 0, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN, FALSE
248 };
249
250 /* These variables are filled in with the masks of registers used.
251 The object format code reads them and puts them in the appropriate
252 place. */
253 unsigned long mips_gprmask;
254 unsigned long mips_cprmask[4];
255
256 /* MIPS ISA we are using for this output file. */
257 static int file_mips_isa = ISA_UNKNOWN;
258
259 /* True if -mips16 was passed or implied by arguments passed on the
260 command line (e.g., by -march). */
261 static int file_ase_mips16;
262
263 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
264 || mips_opts.isa == ISA_MIPS32R2 \
265 || mips_opts.isa == ISA_MIPS64 \
266 || mips_opts.isa == ISA_MIPS64R2)
267
268 /* True if -mips3d was passed or implied by arguments passed on the
269 command line (e.g., by -march). */
270 static int file_ase_mips3d;
271
272 /* True if -mdmx was passed or implied by arguments passed on the
273 command line (e.g., by -march). */
274 static int file_ase_mdmx;
275
276 /* True if -msmartmips was passed or implied by arguments passed on the
277 command line (e.g., by -march). */
278 static int file_ase_smartmips;
279
280 #define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32 \
281 || mips_opts.isa == ISA_MIPS32R2)
282
283 /* True if -mdsp was passed or implied by arguments passed on the
284 command line (e.g., by -march). */
285 static int file_ase_dsp;
286
287 #define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2 \
288 || mips_opts.isa == ISA_MIPS64R2)
289
290 #define ISA_SUPPORTS_DSP64_ASE (mips_opts.isa == ISA_MIPS64R2)
291
292 /* True if -mmt was passed or implied by arguments passed on the
293 command line (e.g., by -march). */
294 static int file_ase_mt;
295
296 #define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2 \
297 || mips_opts.isa == ISA_MIPS64R2)
298
299 /* The argument of the -march= flag. The architecture we are assembling. */
300 static int file_mips_arch = CPU_UNKNOWN;
301 static const char *mips_arch_string;
302
303 /* The argument of the -mtune= flag. The architecture for which we
304 are optimizing. */
305 static int mips_tune = CPU_UNKNOWN;
306 static const char *mips_tune_string;
307
308 /* True when generating 32-bit code for a 64-bit processor. */
309 static int mips_32bitmode = 0;
310
311 /* True if the given ABI requires 32-bit registers. */
312 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
313
314 /* Likewise 64-bit registers. */
315 #define ABI_NEEDS_64BIT_REGS(ABI) \
316 ((ABI) == N32_ABI \
317 || (ABI) == N64_ABI \
318 || (ABI) == O64_ABI)
319
320 /* Return true if ISA supports 64 bit wide gp registers. */
321 #define ISA_HAS_64BIT_REGS(ISA) \
322 ((ISA) == ISA_MIPS3 \
323 || (ISA) == ISA_MIPS4 \
324 || (ISA) == ISA_MIPS5 \
325 || (ISA) == ISA_MIPS64 \
326 || (ISA) == ISA_MIPS64R2)
327
328 /* Return true if ISA supports 64 bit wide float registers. */
329 #define ISA_HAS_64BIT_FPRS(ISA) \
330 ((ISA) == ISA_MIPS3 \
331 || (ISA) == ISA_MIPS4 \
332 || (ISA) == ISA_MIPS5 \
333 || (ISA) == ISA_MIPS32R2 \
334 || (ISA) == ISA_MIPS64 \
335 || (ISA) == ISA_MIPS64R2)
336
337 /* Return true if ISA supports 64-bit right rotate (dror et al.)
338 instructions. */
339 #define ISA_HAS_DROR(ISA) \
340 ((ISA) == ISA_MIPS64R2)
341
342 /* Return true if ISA supports 32-bit right rotate (ror et al.)
343 instructions. */
344 #define ISA_HAS_ROR(ISA) \
345 ((ISA) == ISA_MIPS32R2 \
346 || (ISA) == ISA_MIPS64R2 \
347 || mips_opts.ase_smartmips)
348
349 /* Return true if ISA supports single-precision floats in odd registers. */
350 #define ISA_HAS_ODD_SINGLE_FPR(ISA) \
351 ((ISA) == ISA_MIPS32 \
352 || (ISA) == ISA_MIPS32R2 \
353 || (ISA) == ISA_MIPS64 \
354 || (ISA) == ISA_MIPS64R2)
355
356 /* Return true if ISA supports move to/from high part of a 64-bit
357 floating-point register. */
358 #define ISA_HAS_MXHC1(ISA) \
359 ((ISA) == ISA_MIPS32R2 \
360 || (ISA) == ISA_MIPS64R2)
361
362 #define HAVE_32BIT_GPRS \
363 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
364
365 #define HAVE_32BIT_FPRS \
366 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
367
368 #define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
369 #define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
370
371 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
372
373 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
374
375 /* True if relocations are stored in-place. */
376 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
377
378 /* The ABI-derived address size. */
379 #define HAVE_64BIT_ADDRESSES \
380 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
381 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
382
383 /* The size of symbolic constants (i.e., expressions of the form
384 "SYMBOL" or "SYMBOL + OFFSET"). */
385 #define HAVE_32BIT_SYMBOLS \
386 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
387 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
388
389 /* Addresses are loaded in different ways, depending on the address size
390 in use. The n32 ABI Documentation also mandates the use of additions
391 with overflow checking, but existing implementations don't follow it. */
392 #define ADDRESS_ADD_INSN \
393 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
394
395 #define ADDRESS_ADDI_INSN \
396 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
397
398 #define ADDRESS_LOAD_INSN \
399 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
400
401 #define ADDRESS_STORE_INSN \
402 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
403
404 /* Return true if the given CPU supports the MIPS16 ASE. */
405 #define CPU_HAS_MIPS16(cpu) \
406 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
407 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
408
409 /* True if CPU has a dror instruction. */
410 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
411
412 /* True if CPU has a ror instruction. */
413 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
414
415 /* True if mflo and mfhi can be immediately followed by instructions
416 which write to the HI and LO registers.
417
418 According to MIPS specifications, MIPS ISAs I, II, and III need
419 (at least) two instructions between the reads of HI/LO and
420 instructions which write them, and later ISAs do not. Contradicting
421 the MIPS specifications, some MIPS IV processor user manuals (e.g.
422 the UM for the NEC Vr5000) document needing the instructions between
423 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
424 MIPS64 and later ISAs to have the interlocks, plus any specific
425 earlier-ISA CPUs for which CPU documentation declares that the
426 instructions are really interlocked. */
427 #define hilo_interlocks \
428 (mips_opts.isa == ISA_MIPS32 \
429 || mips_opts.isa == ISA_MIPS32R2 \
430 || mips_opts.isa == ISA_MIPS64 \
431 || mips_opts.isa == ISA_MIPS64R2 \
432 || mips_opts.arch == CPU_R4010 \
433 || mips_opts.arch == CPU_R10000 \
434 || mips_opts.arch == CPU_R12000 \
435 || mips_opts.arch == CPU_RM7000 \
436 || mips_opts.arch == CPU_VR5500 \
437 )
438
439 /* Whether the processor uses hardware interlocks to protect reads
440 from the GPRs after they are loaded from memory, and thus does not
441 require nops to be inserted. This applies to instructions marked
442 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
443 level I. */
444 #define gpr_interlocks \
445 (mips_opts.isa != ISA_MIPS1 \
446 || mips_opts.arch == CPU_R3900)
447
448 /* Whether the processor uses hardware interlocks to avoid delays
449 required by coprocessor instructions, and thus does not require
450 nops to be inserted. This applies to instructions marked
451 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
452 between instructions marked INSN_WRITE_COND_CODE and ones marked
453 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
454 levels I, II, and III. */
455 /* Itbl support may require additional care here. */
456 #define cop_interlocks \
457 ((mips_opts.isa != ISA_MIPS1 \
458 && mips_opts.isa != ISA_MIPS2 \
459 && mips_opts.isa != ISA_MIPS3) \
460 || mips_opts.arch == CPU_R4300 \
461 )
462
463 /* Whether the processor uses hardware interlocks to protect reads
464 from coprocessor registers after they are loaded from memory, and
465 thus does not require nops to be inserted. This applies to
466 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
467 requires at MIPS ISA level I. */
468 #define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
469
470 /* Is this a mfhi or mflo instruction? */
471 #define MF_HILO_INSN(PINFO) \
472 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
473
474 /* MIPS PIC level. */
475
476 enum mips_pic_level mips_pic;
477
478 /* 1 if we should generate 32 bit offsets from the $gp register in
479 SVR4_PIC mode. Currently has no meaning in other modes. */
480 static int mips_big_got = 0;
481
482 /* 1 if trap instructions should used for overflow rather than break
483 instructions. */
484 static int mips_trap = 0;
485
486 /* 1 if double width floating point constants should not be constructed
487 by assembling two single width halves into two single width floating
488 point registers which just happen to alias the double width destination
489 register. On some architectures this aliasing can be disabled by a bit
490 in the status register, and the setting of this bit cannot be determined
491 automatically at assemble time. */
492 static int mips_disable_float_construction;
493
494 /* Non-zero if any .set noreorder directives were used. */
495
496 static int mips_any_noreorder;
497
498 /* Non-zero if nops should be inserted when the register referenced in
499 an mfhi/mflo instruction is read in the next two instructions. */
500 static int mips_7000_hilo_fix;
501
502 /* The size of the small data section. */
503 static unsigned int g_switch_value = 8;
504 /* Whether the -G option was used. */
505 static int g_switch_seen = 0;
506
507 #define N_RMASK 0xc4
508 #define N_VFP 0xd4
509
510 /* If we can determine in advance that GP optimization won't be
511 possible, we can skip the relaxation stuff that tries to produce
512 GP-relative references. This makes delay slot optimization work
513 better.
514
515 This function can only provide a guess, but it seems to work for
516 gcc output. It needs to guess right for gcc, otherwise gcc
517 will put what it thinks is a GP-relative instruction in a branch
518 delay slot.
519
520 I don't know if a fix is needed for the SVR4_PIC mode. I've only
521 fixed it for the non-PIC mode. KR 95/04/07 */
522 static int nopic_need_relax (symbolS *, int);
523
524 /* handle of the OPCODE hash table */
525 static struct hash_control *op_hash = NULL;
526
527 /* The opcode hash table we use for the mips16. */
528 static struct hash_control *mips16_op_hash = NULL;
529
530 /* This array holds the chars that always start a comment. If the
531 pre-processor is disabled, these aren't very useful */
532 const char comment_chars[] = "#";
533
534 /* This array holds the chars that only start a comment at the beginning of
535 a line. If the line seems to have the form '# 123 filename'
536 .line and .file directives will appear in the pre-processed output */
537 /* Note that input_file.c hand checks for '#' at the beginning of the
538 first line of the input file. This is because the compiler outputs
539 #NO_APP at the beginning of its output. */
540 /* Also note that C style comments are always supported. */
541 const char line_comment_chars[] = "#";
542
543 /* This array holds machine specific line separator characters. */
544 const char line_separator_chars[] = ";";
545
546 /* Chars that can be used to separate mant from exp in floating point nums */
547 const char EXP_CHARS[] = "eE";
548
549 /* Chars that mean this number is a floating point constant */
550 /* As in 0f12.456 */
551 /* or 0d1.2345e12 */
552 const char FLT_CHARS[] = "rRsSfFdDxXpP";
553
554 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
555 changed in read.c . Ideally it shouldn't have to know about it at all,
556 but nothing is ideal around here.
557 */
558
559 static char *insn_error;
560
561 static int auto_align = 1;
562
563 /* When outputting SVR4 PIC code, the assembler needs to know the
564 offset in the stack frame from which to restore the $gp register.
565 This is set by the .cprestore pseudo-op, and saved in this
566 variable. */
567 static offsetT mips_cprestore_offset = -1;
568
569 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
570 more optimizations, it can use a register value instead of a memory-saved
571 offset and even an other register than $gp as global pointer. */
572 static offsetT mips_cpreturn_offset = -1;
573 static int mips_cpreturn_register = -1;
574 static int mips_gp_register = GP;
575 static int mips_gprel_offset = 0;
576
577 /* Whether mips_cprestore_offset has been set in the current function
578 (or whether it has already been warned about, if not). */
579 static int mips_cprestore_valid = 0;
580
581 /* This is the register which holds the stack frame, as set by the
582 .frame pseudo-op. This is needed to implement .cprestore. */
583 static int mips_frame_reg = SP;
584
585 /* Whether mips_frame_reg has been set in the current function
586 (or whether it has already been warned about, if not). */
587 static int mips_frame_reg_valid = 0;
588
589 /* To output NOP instructions correctly, we need to keep information
590 about the previous two instructions. */
591
592 /* Whether we are optimizing. The default value of 2 means to remove
593 unneeded NOPs and swap branch instructions when possible. A value
594 of 1 means to not swap branches. A value of 0 means to always
595 insert NOPs. */
596 static int mips_optimize = 2;
597
598 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
599 equivalent to seeing no -g option at all. */
600 static int mips_debug = 0;
601
602 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
603 #define MAX_VR4130_NOPS 4
604
605 /* The maximum number of NOPs needed to fill delay slots. */
606 #define MAX_DELAY_NOPS 2
607
608 /* The maximum number of NOPs needed for any purpose. */
609 #define MAX_NOPS 4
610
611 /* A list of previous instructions, with index 0 being the most recent.
612 We need to look back MAX_NOPS instructions when filling delay slots
613 or working around processor errata. We need to look back one
614 instruction further if we're thinking about using history[0] to
615 fill a branch delay slot. */
616 static struct mips_cl_insn history[1 + MAX_NOPS];
617
618 /* Nop instructions used by emit_nop. */
619 static struct mips_cl_insn nop_insn, mips16_nop_insn;
620
621 /* The appropriate nop for the current mode. */
622 #define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn : &nop_insn)
623
624 /* If this is set, it points to a frag holding nop instructions which
625 were inserted before the start of a noreorder section. If those
626 nops turn out to be unnecessary, the size of the frag can be
627 decreased. */
628 static fragS *prev_nop_frag;
629
630 /* The number of nop instructions we created in prev_nop_frag. */
631 static int prev_nop_frag_holds;
632
633 /* The number of nop instructions that we know we need in
634 prev_nop_frag. */
635 static int prev_nop_frag_required;
636
637 /* The number of instructions we've seen since prev_nop_frag. */
638 static int prev_nop_frag_since;
639
640 /* For ECOFF and ELF, relocations against symbols are done in two
641 parts, with a HI relocation and a LO relocation. Each relocation
642 has only 16 bits of space to store an addend. This means that in
643 order for the linker to handle carries correctly, it must be able
644 to locate both the HI and the LO relocation. This means that the
645 relocations must appear in order in the relocation table.
646
647 In order to implement this, we keep track of each unmatched HI
648 relocation. We then sort them so that they immediately precede the
649 corresponding LO relocation. */
650
651 struct mips_hi_fixup
652 {
653 /* Next HI fixup. */
654 struct mips_hi_fixup *next;
655 /* This fixup. */
656 fixS *fixp;
657 /* The section this fixup is in. */
658 segT seg;
659 };
660
661 /* The list of unmatched HI relocs. */
662
663 static struct mips_hi_fixup *mips_hi_fixup_list;
664
665 /* The frag containing the last explicit relocation operator.
666 Null if explicit relocations have not been used. */
667
668 static fragS *prev_reloc_op_frag;
669
670 /* Map normal MIPS register numbers to mips16 register numbers. */
671
672 #define X ILLEGAL_REG
673 static const int mips32_to_16_reg_map[] =
674 {
675 X, X, 2, 3, 4, 5, 6, 7,
676 X, X, X, X, X, X, X, X,
677 0, 1, X, X, X, X, X, X,
678 X, X, X, X, X, X, X, X
679 };
680 #undef X
681
682 /* Map mips16 register numbers to normal MIPS register numbers. */
683
684 static const unsigned int mips16_to_32_reg_map[] =
685 {
686 16, 17, 2, 3, 4, 5, 6, 7
687 };
688
689 /* Classifies the kind of instructions we're interested in when
690 implementing -mfix-vr4120. */
691 enum fix_vr4120_class {
692 FIX_VR4120_MACC,
693 FIX_VR4120_DMACC,
694 FIX_VR4120_MULT,
695 FIX_VR4120_DMULT,
696 FIX_VR4120_DIV,
697 FIX_VR4120_MTHILO,
698 NUM_FIX_VR4120_CLASSES
699 };
700
701 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
702 there must be at least one other instruction between an instruction
703 of type X and an instruction of type Y. */
704 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
705
706 /* True if -mfix-vr4120 is in force. */
707 static int mips_fix_vr4120;
708
709 /* ...likewise -mfix-vr4130. */
710 static int mips_fix_vr4130;
711
712 /* We don't relax branches by default, since this causes us to expand
713 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
714 fail to compute the offset before expanding the macro to the most
715 efficient expansion. */
716
717 static int mips_relax_branch;
718 \f
719 /* The expansion of many macros depends on the type of symbol that
720 they refer to. For example, when generating position-dependent code,
721 a macro that refers to a symbol may have two different expansions,
722 one which uses GP-relative addresses and one which uses absolute
723 addresses. When generating SVR4-style PIC, a macro may have
724 different expansions for local and global symbols.
725
726 We handle these situations by generating both sequences and putting
727 them in variant frags. In position-dependent code, the first sequence
728 will be the GP-relative one and the second sequence will be the
729 absolute one. In SVR4 PIC, the first sequence will be for global
730 symbols and the second will be for local symbols.
731
732 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
733 SECOND are the lengths of the two sequences in bytes. These fields
734 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
735 the subtype has the following flags:
736
737 RELAX_USE_SECOND
738 Set if it has been decided that we should use the second
739 sequence instead of the first.
740
741 RELAX_SECOND_LONGER
742 Set in the first variant frag if the macro's second implementation
743 is longer than its first. This refers to the macro as a whole,
744 not an individual relaxation.
745
746 RELAX_NOMACRO
747 Set in the first variant frag if the macro appeared in a .set nomacro
748 block and if one alternative requires a warning but the other does not.
749
750 RELAX_DELAY_SLOT
751 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
752 delay slot.
753
754 The frag's "opcode" points to the first fixup for relaxable code.
755
756 Relaxable macros are generated using a sequence such as:
757
758 relax_start (SYMBOL);
759 ... generate first expansion ...
760 relax_switch ();
761 ... generate second expansion ...
762 relax_end ();
763
764 The code and fixups for the unwanted alternative are discarded
765 by md_convert_frag. */
766 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
767
768 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
769 #define RELAX_SECOND(X) ((X) & 0xff)
770 #define RELAX_USE_SECOND 0x10000
771 #define RELAX_SECOND_LONGER 0x20000
772 #define RELAX_NOMACRO 0x40000
773 #define RELAX_DELAY_SLOT 0x80000
774
775 /* Branch without likely bit. If label is out of range, we turn:
776
777 beq reg1, reg2, label
778 delay slot
779
780 into
781
782 bne reg1, reg2, 0f
783 nop
784 j label
785 0: delay slot
786
787 with the following opcode replacements:
788
789 beq <-> bne
790 blez <-> bgtz
791 bltz <-> bgez
792 bc1f <-> bc1t
793
794 bltzal <-> bgezal (with jal label instead of j label)
795
796 Even though keeping the delay slot instruction in the delay slot of
797 the branch would be more efficient, it would be very tricky to do
798 correctly, because we'd have to introduce a variable frag *after*
799 the delay slot instruction, and expand that instead. Let's do it
800 the easy way for now, even if the branch-not-taken case now costs
801 one additional instruction. Out-of-range branches are not supposed
802 to be common, anyway.
803
804 Branch likely. If label is out of range, we turn:
805
806 beql reg1, reg2, label
807 delay slot (annulled if branch not taken)
808
809 into
810
811 beql reg1, reg2, 1f
812 nop
813 beql $0, $0, 2f
814 nop
815 1: j[al] label
816 delay slot (executed only if branch taken)
817 2:
818
819 It would be possible to generate a shorter sequence by losing the
820 likely bit, generating something like:
821
822 bne reg1, reg2, 0f
823 nop
824 j[al] label
825 delay slot (executed only if branch taken)
826 0:
827
828 beql -> bne
829 bnel -> beq
830 blezl -> bgtz
831 bgtzl -> blez
832 bltzl -> bgez
833 bgezl -> bltz
834 bc1fl -> bc1t
835 bc1tl -> bc1f
836
837 bltzall -> bgezal (with jal label instead of j label)
838 bgezall -> bltzal (ditto)
839
840
841 but it's not clear that it would actually improve performance. */
842 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
843 ((relax_substateT) \
844 (0xc0000000 \
845 | ((toofar) ? 1 : 0) \
846 | ((link) ? 2 : 0) \
847 | ((likely) ? 4 : 0) \
848 | ((uncond) ? 8 : 0)))
849 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
850 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
851 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
852 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
853 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
854
855 /* For mips16 code, we use an entirely different form of relaxation.
856 mips16 supports two versions of most instructions which take
857 immediate values: a small one which takes some small value, and a
858 larger one which takes a 16 bit value. Since branches also follow
859 this pattern, relaxing these values is required.
860
861 We can assemble both mips16 and normal MIPS code in a single
862 object. Therefore, we need to support this type of relaxation at
863 the same time that we support the relaxation described above. We
864 use the high bit of the subtype field to distinguish these cases.
865
866 The information we store for this type of relaxation is the
867 argument code found in the opcode file for this relocation, whether
868 the user explicitly requested a small or extended form, and whether
869 the relocation is in a jump or jal delay slot. That tells us the
870 size of the value, and how it should be stored. We also store
871 whether the fragment is considered to be extended or not. We also
872 store whether this is known to be a branch to a different section,
873 whether we have tried to relax this frag yet, and whether we have
874 ever extended a PC relative fragment because of a shift count. */
875 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
876 (0x80000000 \
877 | ((type) & 0xff) \
878 | ((small) ? 0x100 : 0) \
879 | ((ext) ? 0x200 : 0) \
880 | ((dslot) ? 0x400 : 0) \
881 | ((jal_dslot) ? 0x800 : 0))
882 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
883 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
884 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
885 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
886 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
887 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
888 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
889 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
890 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
891 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
892 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
893 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
894
895 /* Is the given value a sign-extended 32-bit value? */
896 #define IS_SEXT_32BIT_NUM(x) \
897 (((x) &~ (offsetT) 0x7fffffff) == 0 \
898 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
899
900 /* Is the given value a sign-extended 16-bit value? */
901 #define IS_SEXT_16BIT_NUM(x) \
902 (((x) &~ (offsetT) 0x7fff) == 0 \
903 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
904
905 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
906 #define IS_ZEXT_32BIT_NUM(x) \
907 (((x) &~ (offsetT) 0xffffffff) == 0 \
908 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
909
910 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
911 VALUE << SHIFT. VALUE is evaluated exactly once. */
912 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
913 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
914 | (((VALUE) & (MASK)) << (SHIFT)))
915
916 /* Extract bits MASK << SHIFT from STRUCT and shift them right
917 SHIFT places. */
918 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
919 (((STRUCT) >> (SHIFT)) & (MASK))
920
921 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
922 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
923
924 include/opcode/mips.h specifies operand fields using the macros
925 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
926 with "MIPS16OP" instead of "OP". */
927 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
928 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
929 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
930 INSERT_BITS ((INSN).insn_opcode, VALUE, \
931 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
932
933 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
934 #define EXTRACT_OPERAND(FIELD, INSN) \
935 EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD)
936 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
937 EXTRACT_BITS ((INSN).insn_opcode, \
938 MIPS16OP_MASK_##FIELD, \
939 MIPS16OP_SH_##FIELD)
940 \f
941 /* Global variables used when generating relaxable macros. See the
942 comment above RELAX_ENCODE for more details about how relaxation
943 is used. */
944 static struct {
945 /* 0 if we're not emitting a relaxable macro.
946 1 if we're emitting the first of the two relaxation alternatives.
947 2 if we're emitting the second alternative. */
948 int sequence;
949
950 /* The first relaxable fixup in the current frag. (In other words,
951 the first fixup that refers to relaxable code.) */
952 fixS *first_fixup;
953
954 /* sizes[0] says how many bytes of the first alternative are stored in
955 the current frag. Likewise sizes[1] for the second alternative. */
956 unsigned int sizes[2];
957
958 /* The symbol on which the choice of sequence depends. */
959 symbolS *symbol;
960 } mips_relax;
961 \f
962 /* Global variables used to decide whether a macro needs a warning. */
963 static struct {
964 /* True if the macro is in a branch delay slot. */
965 bfd_boolean delay_slot_p;
966
967 /* For relaxable macros, sizes[0] is the length of the first alternative
968 in bytes and sizes[1] is the length of the second alternative.
969 For non-relaxable macros, both elements give the length of the
970 macro in bytes. */
971 unsigned int sizes[2];
972
973 /* The first variant frag for this macro. */
974 fragS *first_frag;
975 } mips_macro_warning;
976 \f
977 /* Prototypes for static functions. */
978
979 #define internalError() \
980 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
981
982 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
983
984 static void append_insn
985 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
986 static void mips_no_prev_insn (void);
987 static void mips16_macro_build
988 (expressionS *, const char *, const char *, va_list);
989 static void load_register (int, expressionS *, int);
990 static void macro_start (void);
991 static void macro_end (void);
992 static void macro (struct mips_cl_insn * ip);
993 static void mips16_macro (struct mips_cl_insn * ip);
994 #ifdef LOSING_COMPILER
995 static void macro2 (struct mips_cl_insn * ip);
996 #endif
997 static void mips_ip (char *str, struct mips_cl_insn * ip);
998 static void mips16_ip (char *str, struct mips_cl_insn * ip);
999 static void mips16_immed
1000 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
1001 unsigned long *, bfd_boolean *, unsigned short *);
1002 static size_t my_getSmallExpression
1003 (expressionS *, bfd_reloc_code_real_type *, char *);
1004 static void my_getExpression (expressionS *, char *);
1005 static void s_align (int);
1006 static void s_change_sec (int);
1007 static void s_change_section (int);
1008 static void s_cons (int);
1009 static void s_float_cons (int);
1010 static void s_mips_globl (int);
1011 static void s_option (int);
1012 static void s_mipsset (int);
1013 static void s_abicalls (int);
1014 static void s_cpload (int);
1015 static void s_cpsetup (int);
1016 static void s_cplocal (int);
1017 static void s_cprestore (int);
1018 static void s_cpreturn (int);
1019 static void s_gpvalue (int);
1020 static void s_gpword (int);
1021 static void s_gpdword (int);
1022 static void s_cpadd (int);
1023 static void s_insn (int);
1024 static void md_obj_begin (void);
1025 static void md_obj_end (void);
1026 static void s_mips_ent (int);
1027 static void s_mips_end (int);
1028 static void s_mips_frame (int);
1029 static void s_mips_mask (int reg_type);
1030 static void s_mips_stab (int);
1031 static void s_mips_weakext (int);
1032 static void s_mips_file (int);
1033 static void s_mips_loc (int);
1034 static bfd_boolean pic_need_relax (symbolS *, asection *);
1035 static int relaxed_branch_length (fragS *, asection *, int);
1036 static int validate_mips_insn (const struct mips_opcode *);
1037
1038 /* Table and functions used to map between CPU/ISA names, and
1039 ISA levels, and CPU numbers. */
1040
1041 struct mips_cpu_info
1042 {
1043 const char *name; /* CPU or ISA name. */
1044 int flags; /* ASEs available, or ISA flag. */
1045 int isa; /* ISA level. */
1046 int cpu; /* CPU number (default CPU if ISA). */
1047 };
1048
1049 #define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1050 #define MIPS_CPU_ASE_SMARTMIPS 0x0002 /* CPU implements SmartMIPS ASE */
1051 #define MIPS_CPU_ASE_DSP 0x0004 /* CPU implements DSP ASE */
1052 #define MIPS_CPU_ASE_MT 0x0008 /* CPU implements MT ASE */
1053 #define MIPS_CPU_ASE_MIPS3D 0x0010 /* CPU implements MIPS-3D ASE */
1054 #define MIPS_CPU_ASE_MDMX 0x0020 /* CPU implements MDMX ASE */
1055
1056 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1057 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1058 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1059 \f
1060 /* Pseudo-op table.
1061
1062 The following pseudo-ops from the Kane and Heinrich MIPS book
1063 should be defined here, but are currently unsupported: .alias,
1064 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1065
1066 The following pseudo-ops from the Kane and Heinrich MIPS book are
1067 specific to the type of debugging information being generated, and
1068 should be defined by the object format: .aent, .begin, .bend,
1069 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1070 .vreg.
1071
1072 The following pseudo-ops from the Kane and Heinrich MIPS book are
1073 not MIPS CPU specific, but are also not specific to the object file
1074 format. This file is probably the best place to define them, but
1075 they are not currently supported: .asm0, .endr, .lab, .repeat,
1076 .struct. */
1077
1078 static const pseudo_typeS mips_pseudo_table[] =
1079 {
1080 /* MIPS specific pseudo-ops. */
1081 {"option", s_option, 0},
1082 {"set", s_mipsset, 0},
1083 {"rdata", s_change_sec, 'r'},
1084 {"sdata", s_change_sec, 's'},
1085 {"livereg", s_ignore, 0},
1086 {"abicalls", s_abicalls, 0},
1087 {"cpload", s_cpload, 0},
1088 {"cpsetup", s_cpsetup, 0},
1089 {"cplocal", s_cplocal, 0},
1090 {"cprestore", s_cprestore, 0},
1091 {"cpreturn", s_cpreturn, 0},
1092 {"gpvalue", s_gpvalue, 0},
1093 {"gpword", s_gpword, 0},
1094 {"gpdword", s_gpdword, 0},
1095 {"cpadd", s_cpadd, 0},
1096 {"insn", s_insn, 0},
1097
1098 /* Relatively generic pseudo-ops that happen to be used on MIPS
1099 chips. */
1100 {"asciiz", stringer, 1},
1101 {"bss", s_change_sec, 'b'},
1102 {"err", s_err, 0},
1103 {"half", s_cons, 1},
1104 {"dword", s_cons, 3},
1105 {"weakext", s_mips_weakext, 0},
1106 {"origin", s_org, 0},
1107 {"repeat", s_rept, 0},
1108
1109 /* These pseudo-ops are defined in read.c, but must be overridden
1110 here for one reason or another. */
1111 {"align", s_align, 0},
1112 {"byte", s_cons, 0},
1113 {"data", s_change_sec, 'd'},
1114 {"double", s_float_cons, 'd'},
1115 {"float", s_float_cons, 'f'},
1116 {"globl", s_mips_globl, 0},
1117 {"global", s_mips_globl, 0},
1118 {"hword", s_cons, 1},
1119 {"int", s_cons, 2},
1120 {"long", s_cons, 2},
1121 {"octa", s_cons, 4},
1122 {"quad", s_cons, 3},
1123 {"section", s_change_section, 0},
1124 {"short", s_cons, 1},
1125 {"single", s_float_cons, 'f'},
1126 {"stabn", s_mips_stab, 'n'},
1127 {"text", s_change_sec, 't'},
1128 {"word", s_cons, 2},
1129
1130 { "extern", ecoff_directive_extern, 0},
1131
1132 { NULL, NULL, 0 },
1133 };
1134
1135 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1136 {
1137 /* These pseudo-ops should be defined by the object file format.
1138 However, a.out doesn't support them, so we have versions here. */
1139 {"aent", s_mips_ent, 1},
1140 {"bgnb", s_ignore, 0},
1141 {"end", s_mips_end, 0},
1142 {"endb", s_ignore, 0},
1143 {"ent", s_mips_ent, 0},
1144 {"file", s_mips_file, 0},
1145 {"fmask", s_mips_mask, 'F'},
1146 {"frame", s_mips_frame, 0},
1147 {"loc", s_mips_loc, 0},
1148 {"mask", s_mips_mask, 'R'},
1149 {"verstamp", s_ignore, 0},
1150 { NULL, NULL, 0 },
1151 };
1152
1153 extern void pop_insert (const pseudo_typeS *);
1154
1155 void
1156 mips_pop_insert (void)
1157 {
1158 pop_insert (mips_pseudo_table);
1159 if (! ECOFF_DEBUGGING)
1160 pop_insert (mips_nonecoff_pseudo_table);
1161 }
1162 \f
1163 /* Symbols labelling the current insn. */
1164
1165 struct insn_label_list
1166 {
1167 struct insn_label_list *next;
1168 symbolS *label;
1169 };
1170
1171 static struct insn_label_list *free_insn_labels;
1172 #define label_list tc_segment_info_data
1173
1174 static void mips_clear_insn_labels (void);
1175
1176 static inline void
1177 mips_clear_insn_labels (void)
1178 {
1179 register struct insn_label_list **pl;
1180 segment_info_type *si;
1181
1182 if (now_seg)
1183 {
1184 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1185 ;
1186
1187 si = seg_info (now_seg);
1188 *pl = si->label_list;
1189 si->label_list = NULL;
1190 }
1191 }
1192
1193 \f
1194 static char *expr_end;
1195
1196 /* Expressions which appear in instructions. These are set by
1197 mips_ip. */
1198
1199 static expressionS imm_expr;
1200 static expressionS imm2_expr;
1201 static expressionS offset_expr;
1202
1203 /* Relocs associated with imm_expr and offset_expr. */
1204
1205 static bfd_reloc_code_real_type imm_reloc[3]
1206 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1207 static bfd_reloc_code_real_type offset_reloc[3]
1208 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1209
1210 /* These are set by mips16_ip if an explicit extension is used. */
1211
1212 static bfd_boolean mips16_small, mips16_ext;
1213
1214 #ifdef OBJ_ELF
1215 /* The pdr segment for per procedure frame/regmask info. Not used for
1216 ECOFF debugging. */
1217
1218 static segT pdr_seg;
1219 #endif
1220
1221 /* The default target format to use. */
1222
1223 const char *
1224 mips_target_format (void)
1225 {
1226 switch (OUTPUT_FLAVOR)
1227 {
1228 case bfd_target_ecoff_flavour:
1229 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1230 case bfd_target_coff_flavour:
1231 return "pe-mips";
1232 case bfd_target_elf_flavour:
1233 #ifdef TE_VXWORKS
1234 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1235 return (target_big_endian
1236 ? "elf32-bigmips-vxworks"
1237 : "elf32-littlemips-vxworks");
1238 #endif
1239 #ifdef TE_TMIPS
1240 /* This is traditional mips. */
1241 return (target_big_endian
1242 ? (HAVE_64BIT_OBJECTS
1243 ? "elf64-tradbigmips"
1244 : (HAVE_NEWABI
1245 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1246 : (HAVE_64BIT_OBJECTS
1247 ? "elf64-tradlittlemips"
1248 : (HAVE_NEWABI
1249 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1250 #else
1251 return (target_big_endian
1252 ? (HAVE_64BIT_OBJECTS
1253 ? "elf64-bigmips"
1254 : (HAVE_NEWABI
1255 ? "elf32-nbigmips" : "elf32-bigmips"))
1256 : (HAVE_64BIT_OBJECTS
1257 ? "elf64-littlemips"
1258 : (HAVE_NEWABI
1259 ? "elf32-nlittlemips" : "elf32-littlemips")));
1260 #endif
1261 default:
1262 abort ();
1263 return NULL;
1264 }
1265 }
1266
1267 /* Return the length of instruction INSN. */
1268
1269 static inline unsigned int
1270 insn_length (const struct mips_cl_insn *insn)
1271 {
1272 if (!mips_opts.mips16)
1273 return 4;
1274 return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1275 }
1276
1277 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1278
1279 static void
1280 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1281 {
1282 size_t i;
1283
1284 insn->insn_mo = mo;
1285 insn->use_extend = FALSE;
1286 insn->extend = 0;
1287 insn->insn_opcode = mo->match;
1288 insn->frag = NULL;
1289 insn->where = 0;
1290 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1291 insn->fixp[i] = NULL;
1292 insn->fixed_p = (mips_opts.noreorder > 0);
1293 insn->noreorder_p = (mips_opts.noreorder > 0);
1294 insn->mips16_absolute_jump_p = 0;
1295 }
1296
1297 /* Install INSN at the location specified by its "frag" and "where" fields. */
1298
1299 static void
1300 install_insn (const struct mips_cl_insn *insn)
1301 {
1302 char *f = insn->frag->fr_literal + insn->where;
1303 if (!mips_opts.mips16)
1304 md_number_to_chars (f, insn->insn_opcode, 4);
1305 else if (insn->mips16_absolute_jump_p)
1306 {
1307 md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1308 md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1309 }
1310 else
1311 {
1312 if (insn->use_extend)
1313 {
1314 md_number_to_chars (f, 0xf000 | insn->extend, 2);
1315 f += 2;
1316 }
1317 md_number_to_chars (f, insn->insn_opcode, 2);
1318 }
1319 }
1320
1321 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1322 and install the opcode in the new location. */
1323
1324 static void
1325 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1326 {
1327 size_t i;
1328
1329 insn->frag = frag;
1330 insn->where = where;
1331 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1332 if (insn->fixp[i] != NULL)
1333 {
1334 insn->fixp[i]->fx_frag = frag;
1335 insn->fixp[i]->fx_where = where;
1336 }
1337 install_insn (insn);
1338 }
1339
1340 /* Add INSN to the end of the output. */
1341
1342 static void
1343 add_fixed_insn (struct mips_cl_insn *insn)
1344 {
1345 char *f = frag_more (insn_length (insn));
1346 move_insn (insn, frag_now, f - frag_now->fr_literal);
1347 }
1348
1349 /* Start a variant frag and move INSN to the start of the variant part,
1350 marking it as fixed. The other arguments are as for frag_var. */
1351
1352 static void
1353 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1354 relax_substateT subtype, symbolS *symbol, offsetT offset)
1355 {
1356 frag_grow (max_chars);
1357 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1358 insn->fixed_p = 1;
1359 frag_var (rs_machine_dependent, max_chars, var,
1360 subtype, symbol, offset, NULL);
1361 }
1362
1363 /* Insert N copies of INSN into the history buffer, starting at
1364 position FIRST. Neither FIRST nor N need to be clipped. */
1365
1366 static void
1367 insert_into_history (unsigned int first, unsigned int n,
1368 const struct mips_cl_insn *insn)
1369 {
1370 if (mips_relax.sequence != 2)
1371 {
1372 unsigned int i;
1373
1374 for (i = ARRAY_SIZE (history); i-- > first;)
1375 if (i >= first + n)
1376 history[i] = history[i - n];
1377 else
1378 history[i] = *insn;
1379 }
1380 }
1381
1382 /* Emit a nop instruction, recording it in the history buffer. */
1383
1384 static void
1385 emit_nop (void)
1386 {
1387 add_fixed_insn (NOP_INSN);
1388 insert_into_history (0, 1, NOP_INSN);
1389 }
1390
1391 /* Initialize vr4120_conflicts. There is a bit of duplication here:
1392 the idea is to make it obvious at a glance that each errata is
1393 included. */
1394
1395 static void
1396 init_vr4120_conflicts (void)
1397 {
1398 #define CONFLICT(FIRST, SECOND) \
1399 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1400
1401 /* Errata 21 - [D]DIV[U] after [D]MACC */
1402 CONFLICT (MACC, DIV);
1403 CONFLICT (DMACC, DIV);
1404
1405 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1406 CONFLICT (DMULT, DMULT);
1407 CONFLICT (DMULT, DMACC);
1408 CONFLICT (DMACC, DMULT);
1409 CONFLICT (DMACC, DMACC);
1410
1411 /* Errata 24 - MT{LO,HI} after [D]MACC */
1412 CONFLICT (MACC, MTHILO);
1413 CONFLICT (DMACC, MTHILO);
1414
1415 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1416 instruction is executed immediately after a MACC or DMACC
1417 instruction, the result of [either instruction] is incorrect." */
1418 CONFLICT (MACC, MULT);
1419 CONFLICT (MACC, DMULT);
1420 CONFLICT (DMACC, MULT);
1421 CONFLICT (DMACC, DMULT);
1422
1423 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1424 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1425 DDIV or DDIVU instruction, the result of the MACC or
1426 DMACC instruction is incorrect.". */
1427 CONFLICT (DMULT, MACC);
1428 CONFLICT (DMULT, DMACC);
1429 CONFLICT (DIV, MACC);
1430 CONFLICT (DIV, DMACC);
1431
1432 #undef CONFLICT
1433 }
1434
1435 struct regname {
1436 const char *name;
1437 unsigned int num;
1438 };
1439
1440 #define RTYPE_MASK 0x1ff00
1441 #define RTYPE_NUM 0x00100
1442 #define RTYPE_FPU 0x00200
1443 #define RTYPE_FCC 0x00400
1444 #define RTYPE_VEC 0x00800
1445 #define RTYPE_GP 0x01000
1446 #define RTYPE_CP0 0x02000
1447 #define RTYPE_PC 0x04000
1448 #define RTYPE_ACC 0x08000
1449 #define RTYPE_CCC 0x10000
1450 #define RNUM_MASK 0x000ff
1451 #define RWARN 0x80000
1452
1453 #define GENERIC_REGISTER_NUMBERS \
1454 {"$0", RTYPE_NUM | 0}, \
1455 {"$1", RTYPE_NUM | 1}, \
1456 {"$2", RTYPE_NUM | 2}, \
1457 {"$3", RTYPE_NUM | 3}, \
1458 {"$4", RTYPE_NUM | 4}, \
1459 {"$5", RTYPE_NUM | 5}, \
1460 {"$6", RTYPE_NUM | 6}, \
1461 {"$7", RTYPE_NUM | 7}, \
1462 {"$8", RTYPE_NUM | 8}, \
1463 {"$9", RTYPE_NUM | 9}, \
1464 {"$10", RTYPE_NUM | 10}, \
1465 {"$11", RTYPE_NUM | 11}, \
1466 {"$12", RTYPE_NUM | 12}, \
1467 {"$13", RTYPE_NUM | 13}, \
1468 {"$14", RTYPE_NUM | 14}, \
1469 {"$15", RTYPE_NUM | 15}, \
1470 {"$16", RTYPE_NUM | 16}, \
1471 {"$17", RTYPE_NUM | 17}, \
1472 {"$18", RTYPE_NUM | 18}, \
1473 {"$19", RTYPE_NUM | 19}, \
1474 {"$20", RTYPE_NUM | 20}, \
1475 {"$21", RTYPE_NUM | 21}, \
1476 {"$22", RTYPE_NUM | 22}, \
1477 {"$23", RTYPE_NUM | 23}, \
1478 {"$24", RTYPE_NUM | 24}, \
1479 {"$25", RTYPE_NUM | 25}, \
1480 {"$26", RTYPE_NUM | 26}, \
1481 {"$27", RTYPE_NUM | 27}, \
1482 {"$28", RTYPE_NUM | 28}, \
1483 {"$29", RTYPE_NUM | 29}, \
1484 {"$30", RTYPE_NUM | 30}, \
1485 {"$31", RTYPE_NUM | 31}
1486
1487 #define FPU_REGISTER_NAMES \
1488 {"$f0", RTYPE_FPU | 0}, \
1489 {"$f1", RTYPE_FPU | 1}, \
1490 {"$f2", RTYPE_FPU | 2}, \
1491 {"$f3", RTYPE_FPU | 3}, \
1492 {"$f4", RTYPE_FPU | 4}, \
1493 {"$f5", RTYPE_FPU | 5}, \
1494 {"$f6", RTYPE_FPU | 6}, \
1495 {"$f7", RTYPE_FPU | 7}, \
1496 {"$f8", RTYPE_FPU | 8}, \
1497 {"$f9", RTYPE_FPU | 9}, \
1498 {"$f10", RTYPE_FPU | 10}, \
1499 {"$f11", RTYPE_FPU | 11}, \
1500 {"$f12", RTYPE_FPU | 12}, \
1501 {"$f13", RTYPE_FPU | 13}, \
1502 {"$f14", RTYPE_FPU | 14}, \
1503 {"$f15", RTYPE_FPU | 15}, \
1504 {"$f16", RTYPE_FPU | 16}, \
1505 {"$f17", RTYPE_FPU | 17}, \
1506 {"$f18", RTYPE_FPU | 18}, \
1507 {"$f19", RTYPE_FPU | 19}, \
1508 {"$f20", RTYPE_FPU | 20}, \
1509 {"$f21", RTYPE_FPU | 21}, \
1510 {"$f22", RTYPE_FPU | 22}, \
1511 {"$f23", RTYPE_FPU | 23}, \
1512 {"$f24", RTYPE_FPU | 24}, \
1513 {"$f25", RTYPE_FPU | 25}, \
1514 {"$f26", RTYPE_FPU | 26}, \
1515 {"$f27", RTYPE_FPU | 27}, \
1516 {"$f28", RTYPE_FPU | 28}, \
1517 {"$f29", RTYPE_FPU | 29}, \
1518 {"$f30", RTYPE_FPU | 30}, \
1519 {"$f31", RTYPE_FPU | 31}
1520
1521 #define FPU_CONDITION_CODE_NAMES \
1522 {"$fcc0", RTYPE_FCC | 0}, \
1523 {"$fcc1", RTYPE_FCC | 1}, \
1524 {"$fcc2", RTYPE_FCC | 2}, \
1525 {"$fcc3", RTYPE_FCC | 3}, \
1526 {"$fcc4", RTYPE_FCC | 4}, \
1527 {"$fcc5", RTYPE_FCC | 5}, \
1528 {"$fcc6", RTYPE_FCC | 6}, \
1529 {"$fcc7", RTYPE_FCC | 7}
1530
1531 #define COPROC_CONDITION_CODE_NAMES \
1532 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
1533 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
1534 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
1535 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
1536 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
1537 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
1538 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
1539 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
1540
1541 #define N32N64_SYMBOLIC_REGISTER_NAMES \
1542 {"$a4", RTYPE_GP | 8}, \
1543 {"$a5", RTYPE_GP | 9}, \
1544 {"$a6", RTYPE_GP | 10}, \
1545 {"$a7", RTYPE_GP | 11}, \
1546 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
1547 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
1548 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
1549 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
1550 {"$t0", RTYPE_GP | 12}, \
1551 {"$t1", RTYPE_GP | 13}, \
1552 {"$t2", RTYPE_GP | 14}, \
1553 {"$t3", RTYPE_GP | 15}
1554
1555 #define O32_SYMBOLIC_REGISTER_NAMES \
1556 {"$t0", RTYPE_GP | 8}, \
1557 {"$t1", RTYPE_GP | 9}, \
1558 {"$t2", RTYPE_GP | 10}, \
1559 {"$t3", RTYPE_GP | 11}, \
1560 {"$t4", RTYPE_GP | 12}, \
1561 {"$t5", RTYPE_GP | 13}, \
1562 {"$t6", RTYPE_GP | 14}, \
1563 {"$t7", RTYPE_GP | 15}, \
1564 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
1565 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
1566 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
1567 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
1568
1569 /* Remaining symbolic register names */
1570 #define SYMBOLIC_REGISTER_NAMES \
1571 {"$zero", RTYPE_GP | 0}, \
1572 {"$at", RTYPE_GP | 1}, \
1573 {"$AT", RTYPE_GP | 1}, \
1574 {"$v0", RTYPE_GP | 2}, \
1575 {"$v1", RTYPE_GP | 3}, \
1576 {"$a0", RTYPE_GP | 4}, \
1577 {"$a1", RTYPE_GP | 5}, \
1578 {"$a2", RTYPE_GP | 6}, \
1579 {"$a3", RTYPE_GP | 7}, \
1580 {"$s0", RTYPE_GP | 16}, \
1581 {"$s1", RTYPE_GP | 17}, \
1582 {"$s2", RTYPE_GP | 18}, \
1583 {"$s3", RTYPE_GP | 19}, \
1584 {"$s4", RTYPE_GP | 20}, \
1585 {"$s5", RTYPE_GP | 21}, \
1586 {"$s6", RTYPE_GP | 22}, \
1587 {"$s7", RTYPE_GP | 23}, \
1588 {"$t8", RTYPE_GP | 24}, \
1589 {"$t9", RTYPE_GP | 25}, \
1590 {"$k0", RTYPE_GP | 26}, \
1591 {"$kt0", RTYPE_GP | 26}, \
1592 {"$k1", RTYPE_GP | 27}, \
1593 {"$kt1", RTYPE_GP | 27}, \
1594 {"$gp", RTYPE_GP | 28}, \
1595 {"$sp", RTYPE_GP | 29}, \
1596 {"$s8", RTYPE_GP | 30}, \
1597 {"$fp", RTYPE_GP | 30}, \
1598 {"$ra", RTYPE_GP | 31}
1599
1600 #define MIPS16_SPECIAL_REGISTER_NAMES \
1601 {"$pc", RTYPE_PC | 0}
1602
1603 #define MDMX_VECTOR_REGISTER_NAMES \
1604 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
1605 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
1606 {"$v2", RTYPE_VEC | 2}, \
1607 {"$v3", RTYPE_VEC | 3}, \
1608 {"$v4", RTYPE_VEC | 4}, \
1609 {"$v5", RTYPE_VEC | 5}, \
1610 {"$v6", RTYPE_VEC | 6}, \
1611 {"$v7", RTYPE_VEC | 7}, \
1612 {"$v8", RTYPE_VEC | 8}, \
1613 {"$v9", RTYPE_VEC | 9}, \
1614 {"$v10", RTYPE_VEC | 10}, \
1615 {"$v11", RTYPE_VEC | 11}, \
1616 {"$v12", RTYPE_VEC | 12}, \
1617 {"$v13", RTYPE_VEC | 13}, \
1618 {"$v14", RTYPE_VEC | 14}, \
1619 {"$v15", RTYPE_VEC | 15}, \
1620 {"$v16", RTYPE_VEC | 16}, \
1621 {"$v17", RTYPE_VEC | 17}, \
1622 {"$v18", RTYPE_VEC | 18}, \
1623 {"$v19", RTYPE_VEC | 19}, \
1624 {"$v20", RTYPE_VEC | 20}, \
1625 {"$v21", RTYPE_VEC | 21}, \
1626 {"$v22", RTYPE_VEC | 22}, \
1627 {"$v23", RTYPE_VEC | 23}, \
1628 {"$v24", RTYPE_VEC | 24}, \
1629 {"$v25", RTYPE_VEC | 25}, \
1630 {"$v26", RTYPE_VEC | 26}, \
1631 {"$v27", RTYPE_VEC | 27}, \
1632 {"$v28", RTYPE_VEC | 28}, \
1633 {"$v29", RTYPE_VEC | 29}, \
1634 {"$v30", RTYPE_VEC | 30}, \
1635 {"$v31", RTYPE_VEC | 31}
1636
1637 #define MIPS_DSP_ACCUMULATOR_NAMES \
1638 {"$ac0", RTYPE_ACC | 0}, \
1639 {"$ac1", RTYPE_ACC | 1}, \
1640 {"$ac2", RTYPE_ACC | 2}, \
1641 {"$ac3", RTYPE_ACC | 3}
1642
1643 static const struct regname reg_names[] = {
1644 GENERIC_REGISTER_NUMBERS,
1645 FPU_REGISTER_NAMES,
1646 FPU_CONDITION_CODE_NAMES,
1647 COPROC_CONDITION_CODE_NAMES,
1648
1649 /* The $txx registers depends on the abi,
1650 these will be added later into the symbol table from
1651 one of the tables below once mips_abi is set after
1652 parsing of arguments from the command line. */
1653 SYMBOLIC_REGISTER_NAMES,
1654
1655 MIPS16_SPECIAL_REGISTER_NAMES,
1656 MDMX_VECTOR_REGISTER_NAMES,
1657 MIPS_DSP_ACCUMULATOR_NAMES,
1658 {0, 0}
1659 };
1660
1661 static const struct regname reg_names_o32[] = {
1662 O32_SYMBOLIC_REGISTER_NAMES,
1663 {0, 0}
1664 };
1665
1666 static const struct regname reg_names_n32n64[] = {
1667 N32N64_SYMBOLIC_REGISTER_NAMES,
1668 {0, 0}
1669 };
1670
1671 static int
1672 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
1673 {
1674 symbolS *symbolP;
1675 char *e;
1676 char save_c;
1677 int reg = -1;
1678
1679 /* Find end of name. */
1680 e = *s;
1681 if (is_name_beginner (*e))
1682 ++e;
1683 while (is_part_of_name (*e))
1684 ++e;
1685
1686 /* Terminate name. */
1687 save_c = *e;
1688 *e = '\0';
1689
1690 /* Look for a register symbol. */
1691 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
1692 {
1693 int r = S_GET_VALUE (symbolP);
1694 if (r & types)
1695 reg = r & RNUM_MASK;
1696 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
1697 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
1698 reg = (r & RNUM_MASK) - 2;
1699 }
1700 /* Else see if this is a register defined in an itbl entry. */
1701 else if ((types & RTYPE_GP) && itbl_have_entries)
1702 {
1703 char *n = *s;
1704 unsigned long r;
1705
1706 if (*n == '$')
1707 ++n;
1708 if (itbl_get_reg_val (n, &r))
1709 reg = r & RNUM_MASK;
1710 }
1711
1712 /* Advance to next token if a register was recognised. */
1713 if (reg >= 0)
1714 *s = e;
1715 else if (types & RWARN)
1716 as_warn ("Unrecognized register name `%s'", *s);
1717
1718 *e = save_c;
1719 if (regnop)
1720 *regnop = reg;
1721 return reg >= 0;
1722 }
1723
1724 /* This function is called once, at assembler startup time. It should set up
1725 all the tables, etc. that the MD part of the assembler will need. */
1726
1727 void
1728 md_begin (void)
1729 {
1730 const char *retval = NULL;
1731 int i = 0;
1732 int broken = 0;
1733
1734 if (mips_pic != NO_PIC)
1735 {
1736 if (g_switch_seen && g_switch_value != 0)
1737 as_bad (_("-G may not be used in position-independent code"));
1738 g_switch_value = 0;
1739 }
1740
1741 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1742 as_warn (_("Could not set architecture and machine"));
1743
1744 op_hash = hash_new ();
1745
1746 for (i = 0; i < NUMOPCODES;)
1747 {
1748 const char *name = mips_opcodes[i].name;
1749
1750 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1751 if (retval != NULL)
1752 {
1753 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1754 mips_opcodes[i].name, retval);
1755 /* Probably a memory allocation problem? Give up now. */
1756 as_fatal (_("Broken assembler. No assembly attempted."));
1757 }
1758 do
1759 {
1760 if (mips_opcodes[i].pinfo != INSN_MACRO)
1761 {
1762 if (!validate_mips_insn (&mips_opcodes[i]))
1763 broken = 1;
1764 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1765 {
1766 create_insn (&nop_insn, mips_opcodes + i);
1767 nop_insn.fixed_p = 1;
1768 }
1769 }
1770 ++i;
1771 }
1772 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1773 }
1774
1775 mips16_op_hash = hash_new ();
1776
1777 i = 0;
1778 while (i < bfd_mips16_num_opcodes)
1779 {
1780 const char *name = mips16_opcodes[i].name;
1781
1782 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1783 if (retval != NULL)
1784 as_fatal (_("internal: can't hash `%s': %s"),
1785 mips16_opcodes[i].name, retval);
1786 do
1787 {
1788 if (mips16_opcodes[i].pinfo != INSN_MACRO
1789 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1790 != mips16_opcodes[i].match))
1791 {
1792 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1793 mips16_opcodes[i].name, mips16_opcodes[i].args);
1794 broken = 1;
1795 }
1796 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1797 {
1798 create_insn (&mips16_nop_insn, mips16_opcodes + i);
1799 mips16_nop_insn.fixed_p = 1;
1800 }
1801 ++i;
1802 }
1803 while (i < bfd_mips16_num_opcodes
1804 && strcmp (mips16_opcodes[i].name, name) == 0);
1805 }
1806
1807 if (broken)
1808 as_fatal (_("Broken assembler. No assembly attempted."));
1809
1810 /* We add all the general register names to the symbol table. This
1811 helps us detect invalid uses of them. */
1812 for (i = 0; reg_names[i].name; i++)
1813 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
1814 reg_names[i].num, // & RNUM_MASK,
1815 &zero_address_frag));
1816 if (HAVE_NEWABI)
1817 for (i = 0; reg_names_n32n64[i].name; i++)
1818 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
1819 reg_names_n32n64[i].num, // & RNUM_MASK,
1820 &zero_address_frag));
1821 else
1822 for (i = 0; reg_names_o32[i].name; i++)
1823 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
1824 reg_names_o32[i].num, // & RNUM_MASK,
1825 &zero_address_frag));
1826
1827 mips_no_prev_insn ();
1828
1829 mips_gprmask = 0;
1830 mips_cprmask[0] = 0;
1831 mips_cprmask[1] = 0;
1832 mips_cprmask[2] = 0;
1833 mips_cprmask[3] = 0;
1834
1835 /* set the default alignment for the text section (2**2) */
1836 record_alignment (text_section, 2);
1837
1838 bfd_set_gp_size (stdoutput, g_switch_value);
1839
1840 #ifdef OBJ_ELF
1841 if (IS_ELF)
1842 {
1843 /* On a native system other than VxWorks, sections must be aligned
1844 to 16 byte boundaries. When configured for an embedded ELF
1845 target, we don't bother. */
1846 if (strcmp (TARGET_OS, "elf") != 0
1847 && strcmp (TARGET_OS, "vxworks") != 0)
1848 {
1849 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1850 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1851 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1852 }
1853
1854 /* Create a .reginfo section for register masks and a .mdebug
1855 section for debugging information. */
1856 {
1857 segT seg;
1858 subsegT subseg;
1859 flagword flags;
1860 segT sec;
1861
1862 seg = now_seg;
1863 subseg = now_subseg;
1864
1865 /* The ABI says this section should be loaded so that the
1866 running program can access it. However, we don't load it
1867 if we are configured for an embedded target */
1868 flags = SEC_READONLY | SEC_DATA;
1869 if (strcmp (TARGET_OS, "elf") != 0)
1870 flags |= SEC_ALLOC | SEC_LOAD;
1871
1872 if (mips_abi != N64_ABI)
1873 {
1874 sec = subseg_new (".reginfo", (subsegT) 0);
1875
1876 bfd_set_section_flags (stdoutput, sec, flags);
1877 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1878
1879 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1880 }
1881 else
1882 {
1883 /* The 64-bit ABI uses a .MIPS.options section rather than
1884 .reginfo section. */
1885 sec = subseg_new (".MIPS.options", (subsegT) 0);
1886 bfd_set_section_flags (stdoutput, sec, flags);
1887 bfd_set_section_alignment (stdoutput, sec, 3);
1888
1889 /* Set up the option header. */
1890 {
1891 Elf_Internal_Options opthdr;
1892 char *f;
1893
1894 opthdr.kind = ODK_REGINFO;
1895 opthdr.size = (sizeof (Elf_External_Options)
1896 + sizeof (Elf64_External_RegInfo));
1897 opthdr.section = 0;
1898 opthdr.info = 0;
1899 f = frag_more (sizeof (Elf_External_Options));
1900 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1901 (Elf_External_Options *) f);
1902
1903 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1904 }
1905 }
1906
1907 if (ECOFF_DEBUGGING)
1908 {
1909 sec = subseg_new (".mdebug", (subsegT) 0);
1910 (void) bfd_set_section_flags (stdoutput, sec,
1911 SEC_HAS_CONTENTS | SEC_READONLY);
1912 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1913 }
1914 else if (mips_flag_pdr)
1915 {
1916 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1917 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1918 SEC_READONLY | SEC_RELOC
1919 | SEC_DEBUGGING);
1920 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1921 }
1922
1923 subseg_set (seg, subseg);
1924 }
1925 }
1926 #endif /* OBJ_ELF */
1927
1928 if (! ECOFF_DEBUGGING)
1929 md_obj_begin ();
1930
1931 if (mips_fix_vr4120)
1932 init_vr4120_conflicts ();
1933 }
1934
1935 void
1936 md_mips_end (void)
1937 {
1938 if (! ECOFF_DEBUGGING)
1939 md_obj_end ();
1940 }
1941
1942 void
1943 md_assemble (char *str)
1944 {
1945 struct mips_cl_insn insn;
1946 bfd_reloc_code_real_type unused_reloc[3]
1947 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1948
1949 imm_expr.X_op = O_absent;
1950 imm2_expr.X_op = O_absent;
1951 offset_expr.X_op = O_absent;
1952 imm_reloc[0] = BFD_RELOC_UNUSED;
1953 imm_reloc[1] = BFD_RELOC_UNUSED;
1954 imm_reloc[2] = BFD_RELOC_UNUSED;
1955 offset_reloc[0] = BFD_RELOC_UNUSED;
1956 offset_reloc[1] = BFD_RELOC_UNUSED;
1957 offset_reloc[2] = BFD_RELOC_UNUSED;
1958
1959 if (mips_opts.mips16)
1960 mips16_ip (str, &insn);
1961 else
1962 {
1963 mips_ip (str, &insn);
1964 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1965 str, insn.insn_opcode));
1966 }
1967
1968 if (insn_error)
1969 {
1970 as_bad ("%s `%s'", insn_error, str);
1971 return;
1972 }
1973
1974 if (insn.insn_mo->pinfo == INSN_MACRO)
1975 {
1976 macro_start ();
1977 if (mips_opts.mips16)
1978 mips16_macro (&insn);
1979 else
1980 macro (&insn);
1981 macro_end ();
1982 }
1983 else
1984 {
1985 if (imm_expr.X_op != O_absent)
1986 append_insn (&insn, &imm_expr, imm_reloc);
1987 else if (offset_expr.X_op != O_absent)
1988 append_insn (&insn, &offset_expr, offset_reloc);
1989 else
1990 append_insn (&insn, NULL, unused_reloc);
1991 }
1992 }
1993
1994 /* Return true if the given relocation might need a matching %lo().
1995 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
1996 need a matching %lo() when applied to local symbols. */
1997
1998 static inline bfd_boolean
1999 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
2000 {
2001 return (HAVE_IN_PLACE_ADDENDS
2002 && (reloc == BFD_RELOC_HI16_S
2003 || reloc == BFD_RELOC_MIPS16_HI16_S
2004 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2005 all GOT16 relocations evaluate to "G". */
2006 || (reloc == BFD_RELOC_MIPS_GOT16 && mips_pic != VXWORKS_PIC)));
2007 }
2008
2009 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
2010 relocation. */
2011
2012 static inline bfd_boolean
2013 fixup_has_matching_lo_p (fixS *fixp)
2014 {
2015 return (fixp->fx_next != NULL
2016 && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
2017 || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
2018 && fixp->fx_addsy == fixp->fx_next->fx_addsy
2019 && fixp->fx_offset == fixp->fx_next->fx_offset);
2020 }
2021
2022 /* See whether instruction IP reads register REG. CLASS is the type
2023 of register. */
2024
2025 static int
2026 insn_uses_reg (const struct mips_cl_insn *ip, unsigned int reg,
2027 enum mips_regclass class)
2028 {
2029 if (class == MIPS16_REG)
2030 {
2031 assert (mips_opts.mips16);
2032 reg = mips16_to_32_reg_map[reg];
2033 class = MIPS_GR_REG;
2034 }
2035
2036 /* Don't report on general register ZERO, since it never changes. */
2037 if (class == MIPS_GR_REG && reg == ZERO)
2038 return 0;
2039
2040 if (class == MIPS_FP_REG)
2041 {
2042 assert (! mips_opts.mips16);
2043 /* If we are called with either $f0 or $f1, we must check $f0.
2044 This is not optimal, because it will introduce an unnecessary
2045 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
2046 need to distinguish reading both $f0 and $f1 or just one of
2047 them. Note that we don't have to check the other way,
2048 because there is no instruction that sets both $f0 and $f1
2049 and requires a delay. */
2050 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
2051 && ((EXTRACT_OPERAND (FS, *ip) & ~(unsigned) 1)
2052 == (reg &~ (unsigned) 1)))
2053 return 1;
2054 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
2055 && ((EXTRACT_OPERAND (FT, *ip) & ~(unsigned) 1)
2056 == (reg &~ (unsigned) 1)))
2057 return 1;
2058 }
2059 else if (! mips_opts.mips16)
2060 {
2061 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
2062 && EXTRACT_OPERAND (RS, *ip) == reg)
2063 return 1;
2064 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
2065 && EXTRACT_OPERAND (RT, *ip) == reg)
2066 return 1;
2067 }
2068 else
2069 {
2070 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
2071 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)] == reg)
2072 return 1;
2073 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
2074 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)] == reg)
2075 return 1;
2076 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
2077 && (mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]
2078 == reg))
2079 return 1;
2080 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
2081 return 1;
2082 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
2083 return 1;
2084 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
2085 return 1;
2086 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
2087 && MIPS16_EXTRACT_OPERAND (REGR32, *ip) == reg)
2088 return 1;
2089 }
2090
2091 return 0;
2092 }
2093
2094 /* This function returns true if modifying a register requires a
2095 delay. */
2096
2097 static int
2098 reg_needs_delay (unsigned int reg)
2099 {
2100 unsigned long prev_pinfo;
2101
2102 prev_pinfo = history[0].insn_mo->pinfo;
2103 if (! mips_opts.noreorder
2104 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2105 && ! gpr_interlocks)
2106 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2107 && ! cop_interlocks)))
2108 {
2109 /* A load from a coprocessor or from memory. All load delays
2110 delay the use of general register rt for one instruction. */
2111 /* Itbl support may require additional care here. */
2112 know (prev_pinfo & INSN_WRITE_GPR_T);
2113 if (reg == EXTRACT_OPERAND (RT, history[0]))
2114 return 1;
2115 }
2116
2117 return 0;
2118 }
2119
2120 /* Move all labels in insn_labels to the current insertion point. */
2121
2122 static void
2123 mips_move_labels (void)
2124 {
2125 segment_info_type *si = seg_info (now_seg);
2126 struct insn_label_list *l;
2127 valueT val;
2128
2129 for (l = si->label_list; l != NULL; l = l->next)
2130 {
2131 assert (S_GET_SEGMENT (l->label) == now_seg);
2132 symbol_set_frag (l->label, frag_now);
2133 val = (valueT) frag_now_fix ();
2134 /* mips16 text labels are stored as odd. */
2135 if (mips_opts.mips16)
2136 ++val;
2137 S_SET_VALUE (l->label, val);
2138 }
2139 }
2140
2141 static bfd_boolean
2142 s_is_linkonce (symbolS *sym, segT from_seg)
2143 {
2144 bfd_boolean linkonce = FALSE;
2145 segT symseg = S_GET_SEGMENT (sym);
2146
2147 if (symseg != from_seg && !S_IS_LOCAL (sym))
2148 {
2149 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2150 linkonce = TRUE;
2151 #ifdef OBJ_ELF
2152 /* The GNU toolchain uses an extension for ELF: a section
2153 beginning with the magic string .gnu.linkonce is a
2154 linkonce section. */
2155 if (strncmp (segment_name (symseg), ".gnu.linkonce",
2156 sizeof ".gnu.linkonce" - 1) == 0)
2157 linkonce = TRUE;
2158 #endif
2159 }
2160 return linkonce;
2161 }
2162
2163 /* Mark instruction labels in mips16 mode. This permits the linker to
2164 handle them specially, such as generating jalx instructions when
2165 needed. We also make them odd for the duration of the assembly, in
2166 order to generate the right sort of code. We will make them even
2167 in the adjust_symtab routine, while leaving them marked. This is
2168 convenient for the debugger and the disassembler. The linker knows
2169 to make them odd again. */
2170
2171 static void
2172 mips16_mark_labels (void)
2173 {
2174 segment_info_type *si = seg_info (now_seg);
2175 struct insn_label_list *l;
2176
2177 if (!mips_opts.mips16)
2178 return;
2179
2180 for (l = si->label_list; l != NULL; l = l->next)
2181 {
2182 symbolS *label = l->label;
2183
2184 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
2185 if (IS_ELF)
2186 S_SET_OTHER (label, STO_MIPS16);
2187 #endif
2188 if ((S_GET_VALUE (label) & 1) == 0
2189 /* Don't adjust the address if the label is global or weak, or
2190 in a link-once section, since we'll be emitting symbol reloc
2191 references to it which will be patched up by the linker, and
2192 the final value of the symbol may or may not be MIPS16. */
2193 && ! S_IS_WEAK (label)
2194 && ! S_IS_EXTERNAL (label)
2195 && ! s_is_linkonce (label, now_seg))
2196 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
2197 }
2198 }
2199
2200 /* End the current frag. Make it a variant frag and record the
2201 relaxation info. */
2202
2203 static void
2204 relax_close_frag (void)
2205 {
2206 mips_macro_warning.first_frag = frag_now;
2207 frag_var (rs_machine_dependent, 0, 0,
2208 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
2209 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2210
2211 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2212 mips_relax.first_fixup = 0;
2213 }
2214
2215 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
2216 See the comment above RELAX_ENCODE for more details. */
2217
2218 static void
2219 relax_start (symbolS *symbol)
2220 {
2221 assert (mips_relax.sequence == 0);
2222 mips_relax.sequence = 1;
2223 mips_relax.symbol = symbol;
2224 }
2225
2226 /* Start generating the second version of a relaxable sequence.
2227 See the comment above RELAX_ENCODE for more details. */
2228
2229 static void
2230 relax_switch (void)
2231 {
2232 assert (mips_relax.sequence == 1);
2233 mips_relax.sequence = 2;
2234 }
2235
2236 /* End the current relaxable sequence. */
2237
2238 static void
2239 relax_end (void)
2240 {
2241 assert (mips_relax.sequence == 2);
2242 relax_close_frag ();
2243 mips_relax.sequence = 0;
2244 }
2245
2246 /* Classify an instruction according to the FIX_VR4120_* enumeration.
2247 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
2248 by VR4120 errata. */
2249
2250 static unsigned int
2251 classify_vr4120_insn (const char *name)
2252 {
2253 if (strncmp (name, "macc", 4) == 0)
2254 return FIX_VR4120_MACC;
2255 if (strncmp (name, "dmacc", 5) == 0)
2256 return FIX_VR4120_DMACC;
2257 if (strncmp (name, "mult", 4) == 0)
2258 return FIX_VR4120_MULT;
2259 if (strncmp (name, "dmult", 5) == 0)
2260 return FIX_VR4120_DMULT;
2261 if (strstr (name, "div"))
2262 return FIX_VR4120_DIV;
2263 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
2264 return FIX_VR4120_MTHILO;
2265 return NUM_FIX_VR4120_CLASSES;
2266 }
2267
2268 /* Return the number of instructions that must separate INSN1 and INSN2,
2269 where INSN1 is the earlier instruction. Return the worst-case value
2270 for any INSN2 if INSN2 is null. */
2271
2272 static unsigned int
2273 insns_between (const struct mips_cl_insn *insn1,
2274 const struct mips_cl_insn *insn2)
2275 {
2276 unsigned long pinfo1, pinfo2;
2277
2278 /* This function needs to know which pinfo flags are set for INSN2
2279 and which registers INSN2 uses. The former is stored in PINFO2 and
2280 the latter is tested via INSN2_USES_REG. If INSN2 is null, PINFO2
2281 will have every flag set and INSN2_USES_REG will always return true. */
2282 pinfo1 = insn1->insn_mo->pinfo;
2283 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
2284
2285 #define INSN2_USES_REG(REG, CLASS) \
2286 (insn2 == NULL || insn_uses_reg (insn2, REG, CLASS))
2287
2288 /* For most targets, write-after-read dependencies on the HI and LO
2289 registers must be separated by at least two instructions. */
2290 if (!hilo_interlocks)
2291 {
2292 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
2293 return 2;
2294 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
2295 return 2;
2296 }
2297
2298 /* If we're working around r7000 errata, there must be two instructions
2299 between an mfhi or mflo and any instruction that uses the result. */
2300 if (mips_7000_hilo_fix
2301 && MF_HILO_INSN (pinfo1)
2302 && INSN2_USES_REG (EXTRACT_OPERAND (RD, *insn1), MIPS_GR_REG))
2303 return 2;
2304
2305 /* If working around VR4120 errata, check for combinations that need
2306 a single intervening instruction. */
2307 if (mips_fix_vr4120)
2308 {
2309 unsigned int class1, class2;
2310
2311 class1 = classify_vr4120_insn (insn1->insn_mo->name);
2312 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
2313 {
2314 if (insn2 == NULL)
2315 return 1;
2316 class2 = classify_vr4120_insn (insn2->insn_mo->name);
2317 if (vr4120_conflicts[class1] & (1 << class2))
2318 return 1;
2319 }
2320 }
2321
2322 if (!mips_opts.mips16)
2323 {
2324 /* Check for GPR or coprocessor load delays. All such delays
2325 are on the RT register. */
2326 /* Itbl support may require additional care here. */
2327 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
2328 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
2329 {
2330 know (pinfo1 & INSN_WRITE_GPR_T);
2331 if (INSN2_USES_REG (EXTRACT_OPERAND (RT, *insn1), MIPS_GR_REG))
2332 return 1;
2333 }
2334
2335 /* Check for generic coprocessor hazards.
2336
2337 This case is not handled very well. There is no special
2338 knowledge of CP0 handling, and the coprocessors other than
2339 the floating point unit are not distinguished at all. */
2340 /* Itbl support may require additional care here. FIXME!
2341 Need to modify this to include knowledge about
2342 user specified delays! */
2343 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
2344 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
2345 {
2346 /* Handle cases where INSN1 writes to a known general coprocessor
2347 register. There must be a one instruction delay before INSN2
2348 if INSN2 reads that register, otherwise no delay is needed. */
2349 if (pinfo1 & INSN_WRITE_FPR_T)
2350 {
2351 if (INSN2_USES_REG (EXTRACT_OPERAND (FT, *insn1), MIPS_FP_REG))
2352 return 1;
2353 }
2354 else if (pinfo1 & INSN_WRITE_FPR_S)
2355 {
2356 if (INSN2_USES_REG (EXTRACT_OPERAND (FS, *insn1), MIPS_FP_REG))
2357 return 1;
2358 }
2359 else
2360 {
2361 /* Read-after-write dependencies on the control registers
2362 require a two-instruction gap. */
2363 if ((pinfo1 & INSN_WRITE_COND_CODE)
2364 && (pinfo2 & INSN_READ_COND_CODE))
2365 return 2;
2366
2367 /* We don't know exactly what INSN1 does. If INSN2 is
2368 also a coprocessor instruction, assume there must be
2369 a one instruction gap. */
2370 if (pinfo2 & INSN_COP)
2371 return 1;
2372 }
2373 }
2374
2375 /* Check for read-after-write dependencies on the coprocessor
2376 control registers in cases where INSN1 does not need a general
2377 coprocessor delay. This means that INSN1 is a floating point
2378 comparison instruction. */
2379 /* Itbl support may require additional care here. */
2380 else if (!cop_interlocks
2381 && (pinfo1 & INSN_WRITE_COND_CODE)
2382 && (pinfo2 & INSN_READ_COND_CODE))
2383 return 1;
2384 }
2385
2386 #undef INSN2_USES_REG
2387
2388 return 0;
2389 }
2390
2391 /* Return the number of nops that would be needed to work around the
2392 VR4130 mflo/mfhi errata if instruction INSN immediately followed
2393 the MAX_VR4130_NOPS instructions described by HISTORY. */
2394
2395 static int
2396 nops_for_vr4130 (const struct mips_cl_insn *history,
2397 const struct mips_cl_insn *insn)
2398 {
2399 int i, j, reg;
2400
2401 /* Check if the instruction writes to HI or LO. MTHI and MTLO
2402 are not affected by the errata. */
2403 if (insn != 0
2404 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
2405 || strcmp (insn->insn_mo->name, "mtlo") == 0
2406 || strcmp (insn->insn_mo->name, "mthi") == 0))
2407 return 0;
2408
2409 /* Search for the first MFLO or MFHI. */
2410 for (i = 0; i < MAX_VR4130_NOPS; i++)
2411 if (!history[i].noreorder_p && MF_HILO_INSN (history[i].insn_mo->pinfo))
2412 {
2413 /* Extract the destination register. */
2414 if (mips_opts.mips16)
2415 reg = mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, history[i])];
2416 else
2417 reg = EXTRACT_OPERAND (RD, history[i]);
2418
2419 /* No nops are needed if INSN reads that register. */
2420 if (insn != NULL && insn_uses_reg (insn, reg, MIPS_GR_REG))
2421 return 0;
2422
2423 /* ...or if any of the intervening instructions do. */
2424 for (j = 0; j < i; j++)
2425 if (insn_uses_reg (&history[j], reg, MIPS_GR_REG))
2426 return 0;
2427
2428 return MAX_VR4130_NOPS - i;
2429 }
2430 return 0;
2431 }
2432
2433 /* Return the number of nops that would be needed if instruction INSN
2434 immediately followed the MAX_NOPS instructions given by HISTORY,
2435 where HISTORY[0] is the most recent instruction. If INSN is null,
2436 return the worse-case number of nops for any instruction. */
2437
2438 static int
2439 nops_for_insn (const struct mips_cl_insn *history,
2440 const struct mips_cl_insn *insn)
2441 {
2442 int i, nops, tmp_nops;
2443
2444 nops = 0;
2445 for (i = 0; i < MAX_DELAY_NOPS; i++)
2446 if (!history[i].noreorder_p)
2447 {
2448 tmp_nops = insns_between (history + i, insn) - i;
2449 if (tmp_nops > nops)
2450 nops = tmp_nops;
2451 }
2452
2453 if (mips_fix_vr4130)
2454 {
2455 tmp_nops = nops_for_vr4130 (history, insn);
2456 if (tmp_nops > nops)
2457 nops = tmp_nops;
2458 }
2459
2460 return nops;
2461 }
2462
2463 /* The variable arguments provide NUM_INSNS extra instructions that
2464 might be added to HISTORY. Return the largest number of nops that
2465 would be needed after the extended sequence. */
2466
2467 static int
2468 nops_for_sequence (int num_insns, const struct mips_cl_insn *history, ...)
2469 {
2470 va_list args;
2471 struct mips_cl_insn buffer[MAX_NOPS];
2472 struct mips_cl_insn *cursor;
2473 int nops;
2474
2475 va_start (args, history);
2476 cursor = buffer + num_insns;
2477 memcpy (cursor, history, (MAX_NOPS - num_insns) * sizeof (*cursor));
2478 while (cursor > buffer)
2479 *--cursor = *va_arg (args, const struct mips_cl_insn *);
2480
2481 nops = nops_for_insn (buffer, NULL);
2482 va_end (args);
2483 return nops;
2484 }
2485
2486 /* Like nops_for_insn, but if INSN is a branch, take into account the
2487 worst-case delay for the branch target. */
2488
2489 static int
2490 nops_for_insn_or_target (const struct mips_cl_insn *history,
2491 const struct mips_cl_insn *insn)
2492 {
2493 int nops, tmp_nops;
2494
2495 nops = nops_for_insn (history, insn);
2496 if (insn->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2497 | INSN_COND_BRANCH_DELAY
2498 | INSN_COND_BRANCH_LIKELY))
2499 {
2500 tmp_nops = nops_for_sequence (2, history, insn, NOP_INSN);
2501 if (tmp_nops > nops)
2502 nops = tmp_nops;
2503 }
2504 else if (mips_opts.mips16 && (insn->insn_mo->pinfo & MIPS16_INSN_BRANCH))
2505 {
2506 tmp_nops = nops_for_sequence (1, history, insn);
2507 if (tmp_nops > nops)
2508 nops = tmp_nops;
2509 }
2510 return nops;
2511 }
2512
2513 /* Output an instruction. IP is the instruction information.
2514 ADDRESS_EXPR is an operand of the instruction to be used with
2515 RELOC_TYPE. */
2516
2517 static void
2518 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
2519 bfd_reloc_code_real_type *reloc_type)
2520 {
2521 unsigned long prev_pinfo, pinfo;
2522 relax_stateT prev_insn_frag_type = 0;
2523 bfd_boolean relaxed_branch = FALSE;
2524 segment_info_type *si = seg_info (now_seg);
2525
2526 /* Mark instruction labels in mips16 mode. */
2527 mips16_mark_labels ();
2528
2529 prev_pinfo = history[0].insn_mo->pinfo;
2530 pinfo = ip->insn_mo->pinfo;
2531
2532 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2533 {
2534 /* There are a lot of optimizations we could do that we don't.
2535 In particular, we do not, in general, reorder instructions.
2536 If you use gcc with optimization, it will reorder
2537 instructions and generally do much more optimization then we
2538 do here; repeating all that work in the assembler would only
2539 benefit hand written assembly code, and does not seem worth
2540 it. */
2541 int nops = (mips_optimize == 0
2542 ? nops_for_insn (history, NULL)
2543 : nops_for_insn_or_target (history, ip));
2544 if (nops > 0)
2545 {
2546 fragS *old_frag;
2547 unsigned long old_frag_offset;
2548 int i;
2549
2550 old_frag = frag_now;
2551 old_frag_offset = frag_now_fix ();
2552
2553 for (i = 0; i < nops; i++)
2554 emit_nop ();
2555
2556 if (listing)
2557 {
2558 listing_prev_line ();
2559 /* We may be at the start of a variant frag. In case we
2560 are, make sure there is enough space for the frag
2561 after the frags created by listing_prev_line. The
2562 argument to frag_grow here must be at least as large
2563 as the argument to all other calls to frag_grow in
2564 this file. We don't have to worry about being in the
2565 middle of a variant frag, because the variants insert
2566 all needed nop instructions themselves. */
2567 frag_grow (40);
2568 }
2569
2570 mips_move_labels ();
2571
2572 #ifndef NO_ECOFF_DEBUGGING
2573 if (ECOFF_DEBUGGING)
2574 ecoff_fix_loc (old_frag, old_frag_offset);
2575 #endif
2576 }
2577 }
2578 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
2579 {
2580 /* Work out how many nops in prev_nop_frag are needed by IP. */
2581 int nops = nops_for_insn_or_target (history, ip);
2582 assert (nops <= prev_nop_frag_holds);
2583
2584 /* Enforce NOPS as a minimum. */
2585 if (nops > prev_nop_frag_required)
2586 prev_nop_frag_required = nops;
2587
2588 if (prev_nop_frag_holds == prev_nop_frag_required)
2589 {
2590 /* Settle for the current number of nops. Update the history
2591 accordingly (for the benefit of any future .set reorder code). */
2592 prev_nop_frag = NULL;
2593 insert_into_history (prev_nop_frag_since,
2594 prev_nop_frag_holds, NOP_INSN);
2595 }
2596 else
2597 {
2598 /* Allow this instruction to replace one of the nops that was
2599 tentatively added to prev_nop_frag. */
2600 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2601 prev_nop_frag_holds--;
2602 prev_nop_frag_since++;
2603 }
2604 }
2605
2606 #ifdef OBJ_ELF
2607 /* The value passed to dwarf2_emit_insn is the distance between
2608 the beginning of the current instruction and the address that
2609 should be recorded in the debug tables. For MIPS16 debug info
2610 we want to use ISA-encoded addresses, so we pass -1 for an
2611 address higher by one than the current. */
2612 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2613 #endif
2614
2615 /* Record the frag type before frag_var. */
2616 if (history[0].frag)
2617 prev_insn_frag_type = history[0].frag->fr_type;
2618
2619 if (address_expr
2620 && *reloc_type == BFD_RELOC_16_PCREL_S2
2621 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2622 || pinfo & INSN_COND_BRANCH_LIKELY)
2623 && mips_relax_branch
2624 /* Don't try branch relaxation within .set nomacro, or within
2625 .set noat if we use $at for PIC computations. If it turns
2626 out that the branch was out-of-range, we'll get an error. */
2627 && !mips_opts.warn_about_macros
2628 && !(mips_opts.noat && mips_pic != NO_PIC)
2629 && !mips_opts.mips16)
2630 {
2631 relaxed_branch = TRUE;
2632 add_relaxed_insn (ip, (relaxed_branch_length
2633 (NULL, NULL,
2634 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2635 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1
2636 : 0)), 4,
2637 RELAX_BRANCH_ENCODE
2638 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2639 pinfo & INSN_COND_BRANCH_LIKELY,
2640 pinfo & INSN_WRITE_GPR_31,
2641 0),
2642 address_expr->X_add_symbol,
2643 address_expr->X_add_number);
2644 *reloc_type = BFD_RELOC_UNUSED;
2645 }
2646 else if (*reloc_type > BFD_RELOC_UNUSED)
2647 {
2648 /* We need to set up a variant frag. */
2649 assert (mips_opts.mips16 && address_expr != NULL);
2650 add_relaxed_insn (ip, 4, 0,
2651 RELAX_MIPS16_ENCODE
2652 (*reloc_type - BFD_RELOC_UNUSED,
2653 mips16_small, mips16_ext,
2654 prev_pinfo & INSN_UNCOND_BRANCH_DELAY,
2655 history[0].mips16_absolute_jump_p),
2656 make_expr_symbol (address_expr), 0);
2657 }
2658 else if (mips_opts.mips16
2659 && ! ip->use_extend
2660 && *reloc_type != BFD_RELOC_MIPS16_JMP)
2661 {
2662 if ((pinfo & INSN_UNCOND_BRANCH_DELAY) == 0)
2663 /* Make sure there is enough room to swap this instruction with
2664 a following jump instruction. */
2665 frag_grow (6);
2666 add_fixed_insn (ip);
2667 }
2668 else
2669 {
2670 if (mips_opts.mips16
2671 && mips_opts.noreorder
2672 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2673 as_warn (_("extended instruction in delay slot"));
2674
2675 if (mips_relax.sequence)
2676 {
2677 /* If we've reached the end of this frag, turn it into a variant
2678 frag and record the information for the instructions we've
2679 written so far. */
2680 if (frag_room () < 4)
2681 relax_close_frag ();
2682 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2683 }
2684
2685 if (mips_relax.sequence != 2)
2686 mips_macro_warning.sizes[0] += 4;
2687 if (mips_relax.sequence != 1)
2688 mips_macro_warning.sizes[1] += 4;
2689
2690 if (mips_opts.mips16)
2691 {
2692 ip->fixed_p = 1;
2693 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
2694 }
2695 add_fixed_insn (ip);
2696 }
2697
2698 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
2699 {
2700 if (address_expr->X_op == O_constant)
2701 {
2702 unsigned int tmp;
2703
2704 switch (*reloc_type)
2705 {
2706 case BFD_RELOC_32:
2707 ip->insn_opcode |= address_expr->X_add_number;
2708 break;
2709
2710 case BFD_RELOC_MIPS_HIGHEST:
2711 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2712 ip->insn_opcode |= tmp & 0xffff;
2713 break;
2714
2715 case BFD_RELOC_MIPS_HIGHER:
2716 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2717 ip->insn_opcode |= tmp & 0xffff;
2718 break;
2719
2720 case BFD_RELOC_HI16_S:
2721 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2722 ip->insn_opcode |= tmp & 0xffff;
2723 break;
2724
2725 case BFD_RELOC_HI16:
2726 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2727 break;
2728
2729 case BFD_RELOC_UNUSED:
2730 case BFD_RELOC_LO16:
2731 case BFD_RELOC_MIPS_GOT_DISP:
2732 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2733 break;
2734
2735 case BFD_RELOC_MIPS_JMP:
2736 if ((address_expr->X_add_number & 3) != 0)
2737 as_bad (_("jump to misaligned address (0x%lx)"),
2738 (unsigned long) address_expr->X_add_number);
2739 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2740 break;
2741
2742 case BFD_RELOC_MIPS16_JMP:
2743 if ((address_expr->X_add_number & 3) != 0)
2744 as_bad (_("jump to misaligned address (0x%lx)"),
2745 (unsigned long) address_expr->X_add_number);
2746 ip->insn_opcode |=
2747 (((address_expr->X_add_number & 0x7c0000) << 3)
2748 | ((address_expr->X_add_number & 0xf800000) >> 7)
2749 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2750 break;
2751
2752 case BFD_RELOC_16_PCREL_S2:
2753 if ((address_expr->X_add_number & 3) != 0)
2754 as_bad (_("branch to misaligned address (0x%lx)"),
2755 (unsigned long) address_expr->X_add_number);
2756 if (mips_relax_branch)
2757 goto need_reloc;
2758 if ((address_expr->X_add_number + 0x20000) & ~0x3ffff)
2759 as_bad (_("branch address range overflow (0x%lx)"),
2760 (unsigned long) address_expr->X_add_number);
2761 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0xffff;
2762 break;
2763
2764 default:
2765 internalError ();
2766 }
2767 }
2768 else if (*reloc_type < BFD_RELOC_UNUSED)
2769 need_reloc:
2770 {
2771 reloc_howto_type *howto;
2772 int i;
2773
2774 /* In a compound relocation, it is the final (outermost)
2775 operator that determines the relocated field. */
2776 for (i = 1; i < 3; i++)
2777 if (reloc_type[i] == BFD_RELOC_UNUSED)
2778 break;
2779
2780 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2781 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
2782 bfd_get_reloc_size (howto),
2783 address_expr,
2784 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2785 reloc_type[0]);
2786
2787 /* These relocations can have an addend that won't fit in
2788 4 octets for 64bit assembly. */
2789 if (HAVE_64BIT_GPRS
2790 && ! howto->partial_inplace
2791 && (reloc_type[0] == BFD_RELOC_16
2792 || reloc_type[0] == BFD_RELOC_32
2793 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2794 || reloc_type[0] == BFD_RELOC_HI16_S
2795 || reloc_type[0] == BFD_RELOC_LO16
2796 || reloc_type[0] == BFD_RELOC_GPREL16
2797 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2798 || reloc_type[0] == BFD_RELOC_GPREL32
2799 || reloc_type[0] == BFD_RELOC_64
2800 || reloc_type[0] == BFD_RELOC_CTOR
2801 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2802 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2803 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2804 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2805 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2806 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2807 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2808 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2809 || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
2810 ip->fixp[0]->fx_no_overflow = 1;
2811
2812 if (mips_relax.sequence)
2813 {
2814 if (mips_relax.first_fixup == 0)
2815 mips_relax.first_fixup = ip->fixp[0];
2816 }
2817 else if (reloc_needs_lo_p (*reloc_type))
2818 {
2819 struct mips_hi_fixup *hi_fixup;
2820
2821 /* Reuse the last entry if it already has a matching %lo. */
2822 hi_fixup = mips_hi_fixup_list;
2823 if (hi_fixup == 0
2824 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2825 {
2826 hi_fixup = ((struct mips_hi_fixup *)
2827 xmalloc (sizeof (struct mips_hi_fixup)));
2828 hi_fixup->next = mips_hi_fixup_list;
2829 mips_hi_fixup_list = hi_fixup;
2830 }
2831 hi_fixup->fixp = ip->fixp[0];
2832 hi_fixup->seg = now_seg;
2833 }
2834
2835 /* Add fixups for the second and third relocations, if given.
2836 Note that the ABI allows the second relocation to be
2837 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2838 moment we only use RSS_UNDEF, but we could add support
2839 for the others if it ever becomes necessary. */
2840 for (i = 1; i < 3; i++)
2841 if (reloc_type[i] != BFD_RELOC_UNUSED)
2842 {
2843 ip->fixp[i] = fix_new (ip->frag, ip->where,
2844 ip->fixp[0]->fx_size, NULL, 0,
2845 FALSE, reloc_type[i]);
2846
2847 /* Use fx_tcbit to mark compound relocs. */
2848 ip->fixp[0]->fx_tcbit = 1;
2849 ip->fixp[i]->fx_tcbit = 1;
2850 }
2851 }
2852 }
2853 install_insn (ip);
2854
2855 /* Update the register mask information. */
2856 if (! mips_opts.mips16)
2857 {
2858 if (pinfo & INSN_WRITE_GPR_D)
2859 mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip);
2860 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2861 mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip);
2862 if (pinfo & INSN_READ_GPR_S)
2863 mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip);
2864 if (pinfo & INSN_WRITE_GPR_31)
2865 mips_gprmask |= 1 << RA;
2866 if (pinfo & INSN_WRITE_FPR_D)
2867 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip);
2868 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2869 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip);
2870 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2871 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip);
2872 if ((pinfo & INSN_READ_FPR_R) != 0)
2873 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip);
2874 if (pinfo & INSN_COP)
2875 {
2876 /* We don't keep enough information to sort these cases out.
2877 The itbl support does keep this information however, although
2878 we currently don't support itbl fprmats as part of the cop
2879 instruction. May want to add this support in the future. */
2880 }
2881 /* Never set the bit for $0, which is always zero. */
2882 mips_gprmask &= ~1 << 0;
2883 }
2884 else
2885 {
2886 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2887 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip);
2888 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2889 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip);
2890 if (pinfo & MIPS16_INSN_WRITE_Z)
2891 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip);
2892 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2893 mips_gprmask |= 1 << TREG;
2894 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2895 mips_gprmask |= 1 << SP;
2896 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2897 mips_gprmask |= 1 << RA;
2898 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2899 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2900 if (pinfo & MIPS16_INSN_READ_Z)
2901 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip);
2902 if (pinfo & MIPS16_INSN_READ_GPR_X)
2903 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
2904 }
2905
2906 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2907 {
2908 /* Filling the branch delay slot is more complex. We try to
2909 switch the branch with the previous instruction, which we can
2910 do if the previous instruction does not set up a condition
2911 that the branch tests and if the branch is not itself the
2912 target of any branch. */
2913 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2914 || (pinfo & INSN_COND_BRANCH_DELAY))
2915 {
2916 if (mips_optimize < 2
2917 /* If we have seen .set volatile or .set nomove, don't
2918 optimize. */
2919 || mips_opts.nomove != 0
2920 /* We can't swap if the previous instruction's position
2921 is fixed. */
2922 || history[0].fixed_p
2923 /* If the previous previous insn was in a .set
2924 noreorder, we can't swap. Actually, the MIPS
2925 assembler will swap in this situation. However, gcc
2926 configured -with-gnu-as will generate code like
2927 .set noreorder
2928 lw $4,XXX
2929 .set reorder
2930 INSN
2931 bne $4,$0,foo
2932 in which we can not swap the bne and INSN. If gcc is
2933 not configured -with-gnu-as, it does not output the
2934 .set pseudo-ops. */
2935 || history[1].noreorder_p
2936 /* If the branch is itself the target of a branch, we
2937 can not swap. We cheat on this; all we check for is
2938 whether there is a label on this instruction. If
2939 there are any branches to anything other than a
2940 label, users must use .set noreorder. */
2941 || si->label_list != NULL
2942 /* If the previous instruction is in a variant frag
2943 other than this branch's one, we cannot do the swap.
2944 This does not apply to the mips16, which uses variant
2945 frags for different purposes. */
2946 || (! mips_opts.mips16
2947 && prev_insn_frag_type == rs_machine_dependent)
2948 /* Check for conflicts between the branch and the instructions
2949 before the candidate delay slot. */
2950 || nops_for_insn (history + 1, ip) > 0
2951 /* Check for conflicts between the swapped sequence and the
2952 target of the branch. */
2953 || nops_for_sequence (2, history + 1, ip, history) > 0
2954 /* We do not swap with a trap instruction, since it
2955 complicates trap handlers to have the trap
2956 instruction be in a delay slot. */
2957 || (prev_pinfo & INSN_TRAP)
2958 /* If the branch reads a register that the previous
2959 instruction sets, we can not swap. */
2960 || (! mips_opts.mips16
2961 && (prev_pinfo & INSN_WRITE_GPR_T)
2962 && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
2963 MIPS_GR_REG))
2964 || (! mips_opts.mips16
2965 && (prev_pinfo & INSN_WRITE_GPR_D)
2966 && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
2967 MIPS_GR_REG))
2968 || (mips_opts.mips16
2969 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2970 && (insn_uses_reg
2971 (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
2972 MIPS16_REG)))
2973 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2974 && (insn_uses_reg
2975 (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
2976 MIPS16_REG)))
2977 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2978 && (insn_uses_reg
2979 (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
2980 MIPS16_REG)))
2981 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2982 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2983 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2984 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2985 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2986 && insn_uses_reg (ip,
2987 MIPS16OP_EXTRACT_REG32R
2988 (history[0].insn_opcode),
2989 MIPS_GR_REG))))
2990 /* If the branch writes a register that the previous
2991 instruction sets, we can not swap (we know that
2992 branches write only to RD or to $31). */
2993 || (! mips_opts.mips16
2994 && (prev_pinfo & INSN_WRITE_GPR_T)
2995 && (((pinfo & INSN_WRITE_GPR_D)
2996 && (EXTRACT_OPERAND (RT, history[0])
2997 == EXTRACT_OPERAND (RD, *ip)))
2998 || ((pinfo & INSN_WRITE_GPR_31)
2999 && EXTRACT_OPERAND (RT, history[0]) == RA)))
3000 || (! mips_opts.mips16
3001 && (prev_pinfo & INSN_WRITE_GPR_D)
3002 && (((pinfo & INSN_WRITE_GPR_D)
3003 && (EXTRACT_OPERAND (RD, history[0])
3004 == EXTRACT_OPERAND (RD, *ip)))
3005 || ((pinfo & INSN_WRITE_GPR_31)
3006 && EXTRACT_OPERAND (RD, history[0]) == RA)))
3007 || (mips_opts.mips16
3008 && (pinfo & MIPS16_INSN_WRITE_31)
3009 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
3010 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
3011 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
3012 == RA))))
3013 /* If the branch writes a register that the previous
3014 instruction reads, we can not swap (we know that
3015 branches only write to RD or to $31). */
3016 || (! mips_opts.mips16
3017 && (pinfo & INSN_WRITE_GPR_D)
3018 && insn_uses_reg (&history[0],
3019 EXTRACT_OPERAND (RD, *ip),
3020 MIPS_GR_REG))
3021 || (! mips_opts.mips16
3022 && (pinfo & INSN_WRITE_GPR_31)
3023 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
3024 || (mips_opts.mips16
3025 && (pinfo & MIPS16_INSN_WRITE_31)
3026 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
3027 /* If one instruction sets a condition code and the
3028 other one uses a condition code, we can not swap. */
3029 || ((pinfo & INSN_READ_COND_CODE)
3030 && (prev_pinfo & INSN_WRITE_COND_CODE))
3031 || ((pinfo & INSN_WRITE_COND_CODE)
3032 && (prev_pinfo & INSN_READ_COND_CODE))
3033 /* If the previous instruction uses the PC, we can not
3034 swap. */
3035 || (mips_opts.mips16
3036 && (prev_pinfo & MIPS16_INSN_READ_PC))
3037 /* If the previous instruction had a fixup in mips16
3038 mode, we can not swap. This normally means that the
3039 previous instruction was a 4 byte branch anyhow. */
3040 || (mips_opts.mips16 && history[0].fixp[0])
3041 /* If the previous instruction is a sync, sync.l, or
3042 sync.p, we can not swap. */
3043 || (prev_pinfo & INSN_SYNC))
3044 {
3045 if (mips_opts.mips16
3046 && (pinfo & INSN_UNCOND_BRANCH_DELAY)
3047 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31))
3048 && ISA_SUPPORTS_MIPS16E)
3049 {
3050 /* Convert MIPS16 jr/jalr into a "compact" jump. */
3051 ip->insn_opcode |= 0x0080;
3052 install_insn (ip);
3053 insert_into_history (0, 1, ip);
3054 }
3055 else
3056 {
3057 /* We could do even better for unconditional branches to
3058 portions of this object file; we could pick up the
3059 instruction at the destination, put it in the delay
3060 slot, and bump the destination address. */
3061 insert_into_history (0, 1, ip);
3062 emit_nop ();
3063 }
3064
3065 if (mips_relax.sequence)
3066 mips_relax.sizes[mips_relax.sequence - 1] += 4;
3067 }
3068 else
3069 {
3070 /* It looks like we can actually do the swap. */
3071 struct mips_cl_insn delay = history[0];
3072 if (mips_opts.mips16)
3073 {
3074 know (delay.frag == ip->frag);
3075 move_insn (ip, delay.frag, delay.where);
3076 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
3077 }
3078 else if (relaxed_branch)
3079 {
3080 /* Add the delay slot instruction to the end of the
3081 current frag and shrink the fixed part of the
3082 original frag. If the branch occupies the tail of
3083 the latter, move it backwards to cover the gap. */
3084 delay.frag->fr_fix -= 4;
3085 if (delay.frag == ip->frag)
3086 move_insn (ip, ip->frag, ip->where - 4);
3087 add_fixed_insn (&delay);
3088 }
3089 else
3090 {
3091 move_insn (&delay, ip->frag, ip->where);
3092 move_insn (ip, history[0].frag, history[0].where);
3093 }
3094 history[0] = *ip;
3095 delay.fixed_p = 1;
3096 insert_into_history (0, 1, &delay);
3097 }
3098
3099 /* If that was an unconditional branch, forget the previous
3100 insn information. */
3101 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
3102 mips_no_prev_insn ();
3103 }
3104 else if (pinfo & INSN_COND_BRANCH_LIKELY)
3105 {
3106 /* We don't yet optimize a branch likely. What we should do
3107 is look at the target, copy the instruction found there
3108 into the delay slot, and increment the branch to jump to
3109 the next instruction. */
3110 insert_into_history (0, 1, ip);
3111 emit_nop ();
3112 }
3113 else
3114 insert_into_history (0, 1, ip);
3115 }
3116 else
3117 insert_into_history (0, 1, ip);
3118
3119 /* We just output an insn, so the next one doesn't have a label. */
3120 mips_clear_insn_labels ();
3121 }
3122
3123 /* Forget that there was any previous instruction or label. */
3124
3125 static void
3126 mips_no_prev_insn (void)
3127 {
3128 prev_nop_frag = NULL;
3129 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
3130 mips_clear_insn_labels ();
3131 }
3132
3133 /* This function must be called before we emit something other than
3134 instructions. It is like mips_no_prev_insn except that it inserts
3135 any NOPS that might be needed by previous instructions. */
3136
3137 void
3138 mips_emit_delays (void)
3139 {
3140 if (! mips_opts.noreorder)
3141 {
3142 int nops = nops_for_insn (history, NULL);
3143 if (nops > 0)
3144 {
3145 while (nops-- > 0)
3146 add_fixed_insn (NOP_INSN);
3147 mips_move_labels ();
3148 }
3149 }
3150 mips_no_prev_insn ();
3151 }
3152
3153 /* Start a (possibly nested) noreorder block. */
3154
3155 static void
3156 start_noreorder (void)
3157 {
3158 if (mips_opts.noreorder == 0)
3159 {
3160 unsigned int i;
3161 int nops;
3162
3163 /* None of the instructions before the .set noreorder can be moved. */
3164 for (i = 0; i < ARRAY_SIZE (history); i++)
3165 history[i].fixed_p = 1;
3166
3167 /* Insert any nops that might be needed between the .set noreorder
3168 block and the previous instructions. We will later remove any
3169 nops that turn out not to be needed. */
3170 nops = nops_for_insn (history, NULL);
3171 if (nops > 0)
3172 {
3173 if (mips_optimize != 0)
3174 {
3175 /* Record the frag which holds the nop instructions, so
3176 that we can remove them if we don't need them. */
3177 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
3178 prev_nop_frag = frag_now;
3179 prev_nop_frag_holds = nops;
3180 prev_nop_frag_required = 0;
3181 prev_nop_frag_since = 0;
3182 }
3183
3184 for (; nops > 0; --nops)
3185 add_fixed_insn (NOP_INSN);
3186
3187 /* Move on to a new frag, so that it is safe to simply
3188 decrease the size of prev_nop_frag. */
3189 frag_wane (frag_now);
3190 frag_new (0);
3191 mips_move_labels ();
3192 }
3193 mips16_mark_labels ();
3194 mips_clear_insn_labels ();
3195 }
3196 mips_opts.noreorder++;
3197 mips_any_noreorder = 1;
3198 }
3199
3200 /* End a nested noreorder block. */
3201
3202 static void
3203 end_noreorder (void)
3204 {
3205 mips_opts.noreorder--;
3206 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
3207 {
3208 /* Commit to inserting prev_nop_frag_required nops and go back to
3209 handling nop insertion the .set reorder way. */
3210 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
3211 * (mips_opts.mips16 ? 2 : 4));
3212 insert_into_history (prev_nop_frag_since,
3213 prev_nop_frag_required, NOP_INSN);
3214 prev_nop_frag = NULL;
3215 }
3216 }
3217
3218 /* Set up global variables for the start of a new macro. */
3219
3220 static void
3221 macro_start (void)
3222 {
3223 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
3224 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
3225 && (history[0].insn_mo->pinfo
3226 & (INSN_UNCOND_BRANCH_DELAY
3227 | INSN_COND_BRANCH_DELAY
3228 | INSN_COND_BRANCH_LIKELY)) != 0);
3229 }
3230
3231 /* Given that a macro is longer than 4 bytes, return the appropriate warning
3232 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
3233 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
3234
3235 static const char *
3236 macro_warning (relax_substateT subtype)
3237 {
3238 if (subtype & RELAX_DELAY_SLOT)
3239 return _("Macro instruction expanded into multiple instructions"
3240 " in a branch delay slot");
3241 else if (subtype & RELAX_NOMACRO)
3242 return _("Macro instruction expanded into multiple instructions");
3243 else
3244 return 0;
3245 }
3246
3247 /* Finish up a macro. Emit warnings as appropriate. */
3248
3249 static void
3250 macro_end (void)
3251 {
3252 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
3253 {
3254 relax_substateT subtype;
3255
3256 /* Set up the relaxation warning flags. */
3257 subtype = 0;
3258 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
3259 subtype |= RELAX_SECOND_LONGER;
3260 if (mips_opts.warn_about_macros)
3261 subtype |= RELAX_NOMACRO;
3262 if (mips_macro_warning.delay_slot_p)
3263 subtype |= RELAX_DELAY_SLOT;
3264
3265 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
3266 {
3267 /* Either the macro has a single implementation or both
3268 implementations are longer than 4 bytes. Emit the
3269 warning now. */
3270 const char *msg = macro_warning (subtype);
3271 if (msg != 0)
3272 as_warn (msg);
3273 }
3274 else
3275 {
3276 /* One implementation might need a warning but the other
3277 definitely doesn't. */
3278 mips_macro_warning.first_frag->fr_subtype |= subtype;
3279 }
3280 }
3281 }
3282
3283 /* Read a macro's relocation codes from *ARGS and store them in *R.
3284 The first argument in *ARGS will be either the code for a single
3285 relocation or -1 followed by the three codes that make up a
3286 composite relocation. */
3287
3288 static void
3289 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
3290 {
3291 int i, next;
3292
3293 next = va_arg (*args, int);
3294 if (next >= 0)
3295 r[0] = (bfd_reloc_code_real_type) next;
3296 else
3297 for (i = 0; i < 3; i++)
3298 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
3299 }
3300
3301 /* Build an instruction created by a macro expansion. This is passed
3302 a pointer to the count of instructions created so far, an
3303 expression, the name of the instruction to build, an operand format
3304 string, and corresponding arguments. */
3305
3306 static void
3307 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
3308 {
3309 const struct mips_opcode *mo;
3310 struct mips_cl_insn insn;
3311 bfd_reloc_code_real_type r[3];
3312 va_list args;
3313
3314 va_start (args, fmt);
3315
3316 if (mips_opts.mips16)
3317 {
3318 mips16_macro_build (ep, name, fmt, args);
3319 va_end (args);
3320 return;
3321 }
3322
3323 r[0] = BFD_RELOC_UNUSED;
3324 r[1] = BFD_RELOC_UNUSED;
3325 r[2] = BFD_RELOC_UNUSED;
3326 mo = (struct mips_opcode *) hash_find (op_hash, name);
3327 assert (mo);
3328 assert (strcmp (name, mo->name) == 0);
3329
3330 /* Search until we get a match for NAME. It is assumed here that
3331 macros will never generate MDMX, MIPS-3D, DSP or MT instructions. */
3332 while (strcmp (fmt, mo->args) != 0
3333 || mo->pinfo == INSN_MACRO
3334 || !OPCODE_IS_MEMBER (mo,
3335 (mips_opts.isa
3336 | (mips_opts.mips16 ? INSN_MIPS16 : 0)
3337 | (mips_opts.ase_smartmips ? INSN_SMARTMIPS : 0)),
3338 mips_opts.arch)
3339 || (mips_opts.arch == CPU_R4650 && (mo->pinfo & FP_D) != 0))
3340 {
3341 ++mo;
3342 assert (mo->name);
3343 assert (strcmp (name, mo->name) == 0);
3344 }
3345
3346 create_insn (&insn, mo);
3347 for (;;)
3348 {
3349 switch (*fmt++)
3350 {
3351 case '\0':
3352 break;
3353
3354 case ',':
3355 case '(':
3356 case ')':
3357 continue;
3358
3359 case '+':
3360 switch (*fmt++)
3361 {
3362 case 'A':
3363 case 'E':
3364 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
3365 continue;
3366
3367 case 'B':
3368 case 'F':
3369 /* Note that in the macro case, these arguments are already
3370 in MSB form. (When handling the instruction in the
3371 non-macro case, these arguments are sizes from which
3372 MSB values must be calculated.) */
3373 INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
3374 continue;
3375
3376 case 'C':
3377 case 'G':
3378 case 'H':
3379 /* Note that in the macro case, these arguments are already
3380 in MSBD form. (When handling the instruction in the
3381 non-macro case, these arguments are sizes from which
3382 MSBD values must be calculated.) */
3383 INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
3384 continue;
3385
3386 default:
3387 internalError ();
3388 }
3389 continue;
3390
3391 case 't':
3392 case 'w':
3393 case 'E':
3394 INSERT_OPERAND (RT, insn, va_arg (args, int));
3395 continue;
3396
3397 case 'c':
3398 INSERT_OPERAND (CODE, insn, va_arg (args, int));
3399 continue;
3400
3401 case 'T':
3402 case 'W':
3403 INSERT_OPERAND (FT, insn, va_arg (args, int));
3404 continue;
3405
3406 case 'd':
3407 case 'G':
3408 case 'K':
3409 INSERT_OPERAND (RD, insn, va_arg (args, int));
3410 continue;
3411
3412 case 'U':
3413 {
3414 int tmp = va_arg (args, int);
3415
3416 INSERT_OPERAND (RT, insn, tmp);
3417 INSERT_OPERAND (RD, insn, tmp);
3418 continue;
3419 }
3420
3421 case 'V':
3422 case 'S':
3423 INSERT_OPERAND (FS, insn, va_arg (args, int));
3424 continue;
3425
3426 case 'z':
3427 continue;
3428
3429 case '<':
3430 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
3431 continue;
3432
3433 case 'D':
3434 INSERT_OPERAND (FD, insn, va_arg (args, int));
3435 continue;
3436
3437 case 'B':
3438 INSERT_OPERAND (CODE20, insn, va_arg (args, int));
3439 continue;
3440
3441 case 'J':
3442 INSERT_OPERAND (CODE19, insn, va_arg (args, int));
3443 continue;
3444
3445 case 'q':
3446 INSERT_OPERAND (CODE2, insn, va_arg (args, int));
3447 continue;
3448
3449 case 'b':
3450 case 's':
3451 case 'r':
3452 case 'v':
3453 INSERT_OPERAND (RS, insn, va_arg (args, int));
3454 continue;
3455
3456 case 'i':
3457 case 'j':
3458 case 'o':
3459 macro_read_relocs (&args, r);
3460 assert (*r == BFD_RELOC_GPREL16
3461 || *r == BFD_RELOC_MIPS_LITERAL
3462 || *r == BFD_RELOC_MIPS_HIGHER
3463 || *r == BFD_RELOC_HI16_S
3464 || *r == BFD_RELOC_LO16
3465 || *r == BFD_RELOC_MIPS_GOT16
3466 || *r == BFD_RELOC_MIPS_CALL16
3467 || *r == BFD_RELOC_MIPS_GOT_DISP
3468 || *r == BFD_RELOC_MIPS_GOT_PAGE
3469 || *r == BFD_RELOC_MIPS_GOT_OFST
3470 || *r == BFD_RELOC_MIPS_GOT_LO16
3471 || *r == BFD_RELOC_MIPS_CALL_LO16);
3472 continue;
3473
3474 case 'u':
3475 macro_read_relocs (&args, r);
3476 assert (ep != NULL
3477 && (ep->X_op == O_constant
3478 || (ep->X_op == O_symbol
3479 && (*r == BFD_RELOC_MIPS_HIGHEST
3480 || *r == BFD_RELOC_HI16_S
3481 || *r == BFD_RELOC_HI16
3482 || *r == BFD_RELOC_GPREL16
3483 || *r == BFD_RELOC_MIPS_GOT_HI16
3484 || *r == BFD_RELOC_MIPS_CALL_HI16))));
3485 continue;
3486
3487 case 'p':
3488 assert (ep != NULL);
3489
3490 /*
3491 * This allows macro() to pass an immediate expression for
3492 * creating short branches without creating a symbol.
3493 *
3494 * We don't allow branch relaxation for these branches, as
3495 * they should only appear in ".set nomacro" anyway.
3496 */
3497 if (ep->X_op == O_constant)
3498 {
3499 if ((ep->X_add_number & 3) != 0)
3500 as_bad (_("branch to misaligned address (0x%lx)"),
3501 (unsigned long) ep->X_add_number);
3502 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
3503 as_bad (_("branch address range overflow (0x%lx)"),
3504 (unsigned long) ep->X_add_number);
3505 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3506 ep = NULL;
3507 }
3508 else
3509 *r = BFD_RELOC_16_PCREL_S2;
3510 continue;
3511
3512 case 'a':
3513 assert (ep != NULL);
3514 *r = BFD_RELOC_MIPS_JMP;
3515 continue;
3516
3517 case 'C':
3518 INSERT_OPERAND (COPZ, insn, va_arg (args, unsigned long));
3519 continue;
3520
3521 case 'k':
3522 INSERT_OPERAND (CACHE, insn, va_arg (args, unsigned long));
3523 continue;
3524
3525 default:
3526 internalError ();
3527 }
3528 break;
3529 }
3530 va_end (args);
3531 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3532
3533 append_insn (&insn, ep, r);
3534 }
3535
3536 static void
3537 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
3538 va_list args)
3539 {
3540 struct mips_opcode *mo;
3541 struct mips_cl_insn insn;
3542 bfd_reloc_code_real_type r[3]
3543 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3544
3545 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3546 assert (mo);
3547 assert (strcmp (name, mo->name) == 0);
3548
3549 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
3550 {
3551 ++mo;
3552 assert (mo->name);
3553 assert (strcmp (name, mo->name) == 0);
3554 }
3555
3556 create_insn (&insn, mo);
3557 for (;;)
3558 {
3559 int c;
3560
3561 c = *fmt++;
3562 switch (c)
3563 {
3564 case '\0':
3565 break;
3566
3567 case ',':
3568 case '(':
3569 case ')':
3570 continue;
3571
3572 case 'y':
3573 case 'w':
3574 MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
3575 continue;
3576
3577 case 'x':
3578 case 'v':
3579 MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
3580 continue;
3581
3582 case 'z':
3583 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
3584 continue;
3585
3586 case 'Z':
3587 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
3588 continue;
3589
3590 case '0':
3591 case 'S':
3592 case 'P':
3593 case 'R':
3594 continue;
3595
3596 case 'X':
3597 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
3598 continue;
3599
3600 case 'Y':
3601 {
3602 int regno;
3603
3604 regno = va_arg (args, int);
3605 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3606 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
3607 }
3608 continue;
3609
3610 case '<':
3611 case '>':
3612 case '4':
3613 case '5':
3614 case 'H':
3615 case 'W':
3616 case 'D':
3617 case 'j':
3618 case '8':
3619 case 'V':
3620 case 'C':
3621 case 'U':
3622 case 'k':
3623 case 'K':
3624 case 'p':
3625 case 'q':
3626 {
3627 assert (ep != NULL);
3628
3629 if (ep->X_op != O_constant)
3630 *r = (int) BFD_RELOC_UNUSED + c;
3631 else
3632 {
3633 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3634 FALSE, &insn.insn_opcode, &insn.use_extend,
3635 &insn.extend);
3636 ep = NULL;
3637 *r = BFD_RELOC_UNUSED;
3638 }
3639 }
3640 continue;
3641
3642 case '6':
3643 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
3644 continue;
3645 }
3646
3647 break;
3648 }
3649
3650 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3651
3652 append_insn (&insn, ep, r);
3653 }
3654
3655 /*
3656 * Sign-extend 32-bit mode constants that have bit 31 set and all
3657 * higher bits unset.
3658 */
3659 static void
3660 normalize_constant_expr (expressionS *ex)
3661 {
3662 if (ex->X_op == O_constant
3663 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3664 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3665 - 0x80000000);
3666 }
3667
3668 /*
3669 * Sign-extend 32-bit mode address offsets that have bit 31 set and
3670 * all higher bits unset.
3671 */
3672 static void
3673 normalize_address_expr (expressionS *ex)
3674 {
3675 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
3676 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
3677 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3678 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3679 - 0x80000000);
3680 }
3681
3682 /*
3683 * Generate a "jalr" instruction with a relocation hint to the called
3684 * function. This occurs in NewABI PIC code.
3685 */
3686 static void
3687 macro_build_jalr (expressionS *ep)
3688 {
3689 char *f = NULL;
3690
3691 if (HAVE_NEWABI)
3692 {
3693 frag_grow (8);
3694 f = frag_more (0);
3695 }
3696 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3697 if (HAVE_NEWABI)
3698 fix_new_exp (frag_now, f - frag_now->fr_literal,
3699 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3700 }
3701
3702 /*
3703 * Generate a "lui" instruction.
3704 */
3705 static void
3706 macro_build_lui (expressionS *ep, int regnum)
3707 {
3708 expressionS high_expr;
3709 const struct mips_opcode *mo;
3710 struct mips_cl_insn insn;
3711 bfd_reloc_code_real_type r[3]
3712 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3713 const char *name = "lui";
3714 const char *fmt = "t,u";
3715
3716 assert (! mips_opts.mips16);
3717
3718 high_expr = *ep;
3719
3720 if (high_expr.X_op == O_constant)
3721 {
3722 /* We can compute the instruction now without a relocation entry. */
3723 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3724 >> 16) & 0xffff;
3725 *r = BFD_RELOC_UNUSED;
3726 }
3727 else
3728 {
3729 assert (ep->X_op == O_symbol);
3730 /* _gp_disp is a special case, used from s_cpload.
3731 __gnu_local_gp is used if mips_no_shared. */
3732 assert (mips_pic == NO_PIC
3733 || (! HAVE_NEWABI
3734 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3735 || (! mips_in_shared
3736 && strcmp (S_GET_NAME (ep->X_add_symbol),
3737 "__gnu_local_gp") == 0));
3738 *r = BFD_RELOC_HI16_S;
3739 }
3740
3741 mo = hash_find (op_hash, name);
3742 assert (strcmp (name, mo->name) == 0);
3743 assert (strcmp (fmt, mo->args) == 0);
3744 create_insn (&insn, mo);
3745
3746 insn.insn_opcode = insn.insn_mo->match;
3747 INSERT_OPERAND (RT, insn, regnum);
3748 if (*r == BFD_RELOC_UNUSED)
3749 {
3750 insn.insn_opcode |= high_expr.X_add_number;
3751 append_insn (&insn, NULL, r);
3752 }
3753 else
3754 append_insn (&insn, &high_expr, r);
3755 }
3756
3757 /* Generate a sequence of instructions to do a load or store from a constant
3758 offset off of a base register (breg) into/from a target register (treg),
3759 using AT if necessary. */
3760 static void
3761 macro_build_ldst_constoffset (expressionS *ep, const char *op,
3762 int treg, int breg, int dbl)
3763 {
3764 assert (ep->X_op == O_constant);
3765
3766 /* Sign-extending 32-bit constants makes their handling easier. */
3767 if (!dbl)
3768 normalize_constant_expr (ep);
3769
3770 /* Right now, this routine can only handle signed 32-bit constants. */
3771 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3772 as_warn (_("operand overflow"));
3773
3774 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3775 {
3776 /* Signed 16-bit offset will fit in the op. Easy! */
3777 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
3778 }
3779 else
3780 {
3781 /* 32-bit offset, need multiple instructions and AT, like:
3782 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3783 addu $tempreg,$tempreg,$breg
3784 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3785 to handle the complete offset. */
3786 macro_build_lui (ep, AT);
3787 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3788 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
3789
3790 if (mips_opts.noat)
3791 as_bad (_("Macro used $at after \".set noat\""));
3792 }
3793 }
3794
3795 /* set_at()
3796 * Generates code to set the $at register to true (one)
3797 * if reg is less than the immediate expression.
3798 */
3799 static void
3800 set_at (int reg, int unsignedp)
3801 {
3802 if (imm_expr.X_op == O_constant
3803 && imm_expr.X_add_number >= -0x8000
3804 && imm_expr.X_add_number < 0x8000)
3805 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3806 AT, reg, BFD_RELOC_LO16);
3807 else
3808 {
3809 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3810 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
3811 }
3812 }
3813
3814 /* Warn if an expression is not a constant. */
3815
3816 static void
3817 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3818 {
3819 if (ex->X_op == O_big)
3820 as_bad (_("unsupported large constant"));
3821 else if (ex->X_op != O_constant)
3822 as_bad (_("Instruction %s requires absolute expression"),
3823 ip->insn_mo->name);
3824
3825 if (HAVE_32BIT_GPRS)
3826 normalize_constant_expr (ex);
3827 }
3828
3829 /* Count the leading zeroes by performing a binary chop. This is a
3830 bulky bit of source, but performance is a LOT better for the
3831 majority of values than a simple loop to count the bits:
3832 for (lcnt = 0; (lcnt < 32); lcnt++)
3833 if ((v) & (1 << (31 - lcnt)))
3834 break;
3835 However it is not code size friendly, and the gain will drop a bit
3836 on certain cached systems.
3837 */
3838 #define COUNT_TOP_ZEROES(v) \
3839 (((v) & ~0xffff) == 0 \
3840 ? ((v) & ~0xff) == 0 \
3841 ? ((v) & ~0xf) == 0 \
3842 ? ((v) & ~0x3) == 0 \
3843 ? ((v) & ~0x1) == 0 \
3844 ? !(v) \
3845 ? 32 \
3846 : 31 \
3847 : 30 \
3848 : ((v) & ~0x7) == 0 \
3849 ? 29 \
3850 : 28 \
3851 : ((v) & ~0x3f) == 0 \
3852 ? ((v) & ~0x1f) == 0 \
3853 ? 27 \
3854 : 26 \
3855 : ((v) & ~0x7f) == 0 \
3856 ? 25 \
3857 : 24 \
3858 : ((v) & ~0xfff) == 0 \
3859 ? ((v) & ~0x3ff) == 0 \
3860 ? ((v) & ~0x1ff) == 0 \
3861 ? 23 \
3862 : 22 \
3863 : ((v) & ~0x7ff) == 0 \
3864 ? 21 \
3865 : 20 \
3866 : ((v) & ~0x3fff) == 0 \
3867 ? ((v) & ~0x1fff) == 0 \
3868 ? 19 \
3869 : 18 \
3870 : ((v) & ~0x7fff) == 0 \
3871 ? 17 \
3872 : 16 \
3873 : ((v) & ~0xffffff) == 0 \
3874 ? ((v) & ~0xfffff) == 0 \
3875 ? ((v) & ~0x3ffff) == 0 \
3876 ? ((v) & ~0x1ffff) == 0 \
3877 ? 15 \
3878 : 14 \
3879 : ((v) & ~0x7ffff) == 0 \
3880 ? 13 \
3881 : 12 \
3882 : ((v) & ~0x3fffff) == 0 \
3883 ? ((v) & ~0x1fffff) == 0 \
3884 ? 11 \
3885 : 10 \
3886 : ((v) & ~0x7fffff) == 0 \
3887 ? 9 \
3888 : 8 \
3889 : ((v) & ~0xfffffff) == 0 \
3890 ? ((v) & ~0x3ffffff) == 0 \
3891 ? ((v) & ~0x1ffffff) == 0 \
3892 ? 7 \
3893 : 6 \
3894 : ((v) & ~0x7ffffff) == 0 \
3895 ? 5 \
3896 : 4 \
3897 : ((v) & ~0x3fffffff) == 0 \
3898 ? ((v) & ~0x1fffffff) == 0 \
3899 ? 3 \
3900 : 2 \
3901 : ((v) & ~0x7fffffff) == 0 \
3902 ? 1 \
3903 : 0)
3904
3905 /* load_register()
3906 * This routine generates the least number of instructions necessary to load
3907 * an absolute expression value into a register.
3908 */
3909 static void
3910 load_register (int reg, expressionS *ep, int dbl)
3911 {
3912 int freg;
3913 expressionS hi32, lo32;
3914
3915 if (ep->X_op != O_big)
3916 {
3917 assert (ep->X_op == O_constant);
3918
3919 /* Sign-extending 32-bit constants makes their handling easier. */
3920 if (!dbl)
3921 normalize_constant_expr (ep);
3922
3923 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3924 {
3925 /* We can handle 16 bit signed values with an addiu to
3926 $zero. No need to ever use daddiu here, since $zero and
3927 the result are always correct in 32 bit mode. */
3928 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3929 return;
3930 }
3931 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3932 {
3933 /* We can handle 16 bit unsigned values with an ori to
3934 $zero. */
3935 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3936 return;
3937 }
3938 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3939 {
3940 /* 32 bit values require an lui. */
3941 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3942 if ((ep->X_add_number & 0xffff) != 0)
3943 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3944 return;
3945 }
3946 }
3947
3948 /* The value is larger than 32 bits. */
3949
3950 if (!dbl || HAVE_32BIT_GPRS)
3951 {
3952 char value[32];
3953
3954 sprintf_vma (value, ep->X_add_number);
3955 as_bad (_("Number (0x%s) larger than 32 bits"), value);
3956 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3957 return;
3958 }
3959
3960 if (ep->X_op != O_big)
3961 {
3962 hi32 = *ep;
3963 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3964 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3965 hi32.X_add_number &= 0xffffffff;
3966 lo32 = *ep;
3967 lo32.X_add_number &= 0xffffffff;
3968 }
3969 else
3970 {
3971 assert (ep->X_add_number > 2);
3972 if (ep->X_add_number == 3)
3973 generic_bignum[3] = 0;
3974 else if (ep->X_add_number > 4)
3975 as_bad (_("Number larger than 64 bits"));
3976 lo32.X_op = O_constant;
3977 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3978 hi32.X_op = O_constant;
3979 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3980 }
3981
3982 if (hi32.X_add_number == 0)
3983 freg = 0;
3984 else
3985 {
3986 int shift, bit;
3987 unsigned long hi, lo;
3988
3989 if (hi32.X_add_number == (offsetT) 0xffffffff)
3990 {
3991 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3992 {
3993 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3994 return;
3995 }
3996 if (lo32.X_add_number & 0x80000000)
3997 {
3998 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3999 if (lo32.X_add_number & 0xffff)
4000 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
4001 return;
4002 }
4003 }
4004
4005 /* Check for 16bit shifted constant. We know that hi32 is
4006 non-zero, so start the mask on the first bit of the hi32
4007 value. */
4008 shift = 17;
4009 do
4010 {
4011 unsigned long himask, lomask;
4012
4013 if (shift < 32)
4014 {
4015 himask = 0xffff >> (32 - shift);
4016 lomask = (0xffff << shift) & 0xffffffff;
4017 }
4018 else
4019 {
4020 himask = 0xffff << (shift - 32);
4021 lomask = 0;
4022 }
4023 if ((hi32.X_add_number & ~(offsetT) himask) == 0
4024 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
4025 {
4026 expressionS tmp;
4027
4028 tmp.X_op = O_constant;
4029 if (shift < 32)
4030 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
4031 | (lo32.X_add_number >> shift));
4032 else
4033 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
4034 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
4035 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
4036 reg, reg, (shift >= 32) ? shift - 32 : shift);
4037 return;
4038 }
4039 ++shift;
4040 }
4041 while (shift <= (64 - 16));
4042
4043 /* Find the bit number of the lowest one bit, and store the
4044 shifted value in hi/lo. */
4045 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
4046 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
4047 if (lo != 0)
4048 {
4049 bit = 0;
4050 while ((lo & 1) == 0)
4051 {
4052 lo >>= 1;
4053 ++bit;
4054 }
4055 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
4056 hi >>= bit;
4057 }
4058 else
4059 {
4060 bit = 32;
4061 while ((hi & 1) == 0)
4062 {
4063 hi >>= 1;
4064 ++bit;
4065 }
4066 lo = hi;
4067 hi = 0;
4068 }
4069
4070 /* Optimize if the shifted value is a (power of 2) - 1. */
4071 if ((hi == 0 && ((lo + 1) & lo) == 0)
4072 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
4073 {
4074 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
4075 if (shift != 0)
4076 {
4077 expressionS tmp;
4078
4079 /* This instruction will set the register to be all
4080 ones. */
4081 tmp.X_op = O_constant;
4082 tmp.X_add_number = (offsetT) -1;
4083 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
4084 if (bit != 0)
4085 {
4086 bit += shift;
4087 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
4088 reg, reg, (bit >= 32) ? bit - 32 : bit);
4089 }
4090 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
4091 reg, reg, (shift >= 32) ? shift - 32 : shift);
4092 return;
4093 }
4094 }
4095
4096 /* Sign extend hi32 before calling load_register, because we can
4097 generally get better code when we load a sign extended value. */
4098 if ((hi32.X_add_number & 0x80000000) != 0)
4099 hi32.X_add_number |= ~(offsetT) 0xffffffff;
4100 load_register (reg, &hi32, 0);
4101 freg = reg;
4102 }
4103 if ((lo32.X_add_number & 0xffff0000) == 0)
4104 {
4105 if (freg != 0)
4106 {
4107 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
4108 freg = reg;
4109 }
4110 }
4111 else
4112 {
4113 expressionS mid16;
4114
4115 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
4116 {
4117 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
4118 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
4119 return;
4120 }
4121
4122 if (freg != 0)
4123 {
4124 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
4125 freg = reg;
4126 }
4127 mid16 = lo32;
4128 mid16.X_add_number >>= 16;
4129 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
4130 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4131 freg = reg;
4132 }
4133 if ((lo32.X_add_number & 0xffff) != 0)
4134 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
4135 }
4136
4137 static inline void
4138 load_delay_nop (void)
4139 {
4140 if (!gpr_interlocks)
4141 macro_build (NULL, "nop", "");
4142 }
4143
4144 /* Load an address into a register. */
4145
4146 static void
4147 load_address (int reg, expressionS *ep, int *used_at)
4148 {
4149 if (ep->X_op != O_constant
4150 && ep->X_op != O_symbol)
4151 {
4152 as_bad (_("expression too complex"));
4153 ep->X_op = O_constant;
4154 }
4155
4156 if (ep->X_op == O_constant)
4157 {
4158 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
4159 return;
4160 }
4161
4162 if (mips_pic == NO_PIC)
4163 {
4164 /* If this is a reference to a GP relative symbol, we want
4165 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
4166 Otherwise we want
4167 lui $reg,<sym> (BFD_RELOC_HI16_S)
4168 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
4169 If we have an addend, we always use the latter form.
4170
4171 With 64bit address space and a usable $at we want
4172 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4173 lui $at,<sym> (BFD_RELOC_HI16_S)
4174 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
4175 daddiu $at,<sym> (BFD_RELOC_LO16)
4176 dsll32 $reg,0
4177 daddu $reg,$reg,$at
4178
4179 If $at is already in use, we use a path which is suboptimal
4180 on superscalar processors.
4181 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4182 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
4183 dsll $reg,16
4184 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
4185 dsll $reg,16
4186 daddiu $reg,<sym> (BFD_RELOC_LO16)
4187
4188 For GP relative symbols in 64bit address space we can use
4189 the same sequence as in 32bit address space. */
4190 if (HAVE_64BIT_SYMBOLS)
4191 {
4192 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
4193 && !nopic_need_relax (ep->X_add_symbol, 1))
4194 {
4195 relax_start (ep->X_add_symbol);
4196 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
4197 mips_gp_register, BFD_RELOC_GPREL16);
4198 relax_switch ();
4199 }
4200
4201 if (*used_at == 0 && !mips_opts.noat)
4202 {
4203 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
4204 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
4205 macro_build (ep, "daddiu", "t,r,j", reg, reg,
4206 BFD_RELOC_MIPS_HIGHER);
4207 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
4208 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
4209 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
4210 *used_at = 1;
4211 }
4212 else
4213 {
4214 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
4215 macro_build (ep, "daddiu", "t,r,j", reg, reg,
4216 BFD_RELOC_MIPS_HIGHER);
4217 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4218 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
4219 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4220 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
4221 }
4222
4223 if (mips_relax.sequence)
4224 relax_end ();
4225 }
4226 else
4227 {
4228 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
4229 && !nopic_need_relax (ep->X_add_symbol, 1))
4230 {
4231 relax_start (ep->X_add_symbol);
4232 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
4233 mips_gp_register, BFD_RELOC_GPREL16);
4234 relax_switch ();
4235 }
4236 macro_build_lui (ep, reg);
4237 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
4238 reg, reg, BFD_RELOC_LO16);
4239 if (mips_relax.sequence)
4240 relax_end ();
4241 }
4242 }
4243 else if (!mips_big_got)
4244 {
4245 expressionS ex;
4246
4247 /* If this is a reference to an external symbol, we want
4248 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4249 Otherwise we want
4250 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4251 nop
4252 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
4253 If there is a constant, it must be added in after.
4254
4255 If we have NewABI, we want
4256 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4257 unless we're referencing a global symbol with a non-zero
4258 offset, in which case cst must be added separately. */
4259 if (HAVE_NEWABI)
4260 {
4261 if (ep->X_add_number)
4262 {
4263 ex.X_add_number = ep->X_add_number;
4264 ep->X_add_number = 0;
4265 relax_start (ep->X_add_symbol);
4266 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4267 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4268 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4269 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4270 ex.X_op = O_constant;
4271 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
4272 reg, reg, BFD_RELOC_LO16);
4273 ep->X_add_number = ex.X_add_number;
4274 relax_switch ();
4275 }
4276 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4277 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4278 if (mips_relax.sequence)
4279 relax_end ();
4280 }
4281 else
4282 {
4283 ex.X_add_number = ep->X_add_number;
4284 ep->X_add_number = 0;
4285 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4286 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4287 load_delay_nop ();
4288 relax_start (ep->X_add_symbol);
4289 relax_switch ();
4290 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4291 BFD_RELOC_LO16);
4292 relax_end ();
4293
4294 if (ex.X_add_number != 0)
4295 {
4296 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4297 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4298 ex.X_op = O_constant;
4299 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
4300 reg, reg, BFD_RELOC_LO16);
4301 }
4302 }
4303 }
4304 else if (mips_big_got)
4305 {
4306 expressionS ex;
4307
4308 /* This is the large GOT case. If this is a reference to an
4309 external symbol, we want
4310 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4311 addu $reg,$reg,$gp
4312 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
4313
4314 Otherwise, for a reference to a local symbol in old ABI, we want
4315 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4316 nop
4317 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
4318 If there is a constant, it must be added in after.
4319
4320 In the NewABI, for local symbols, with or without offsets, we want:
4321 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4322 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4323 */
4324 if (HAVE_NEWABI)
4325 {
4326 ex.X_add_number = ep->X_add_number;
4327 ep->X_add_number = 0;
4328 relax_start (ep->X_add_symbol);
4329 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4330 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4331 reg, reg, mips_gp_register);
4332 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4333 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4334 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4335 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4336 else if (ex.X_add_number)
4337 {
4338 ex.X_op = O_constant;
4339 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4340 BFD_RELOC_LO16);
4341 }
4342
4343 ep->X_add_number = ex.X_add_number;
4344 relax_switch ();
4345 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4346 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
4347 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4348 BFD_RELOC_MIPS_GOT_OFST);
4349 relax_end ();
4350 }
4351 else
4352 {
4353 ex.X_add_number = ep->X_add_number;
4354 ep->X_add_number = 0;
4355 relax_start (ep->X_add_symbol);
4356 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4357 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4358 reg, reg, mips_gp_register);
4359 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4360 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4361 relax_switch ();
4362 if (reg_needs_delay (mips_gp_register))
4363 {
4364 /* We need a nop before loading from $gp. This special
4365 check is required because the lui which starts the main
4366 instruction stream does not refer to $gp, and so will not
4367 insert the nop which may be required. */
4368 macro_build (NULL, "nop", "");
4369 }
4370 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4371 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4372 load_delay_nop ();
4373 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4374 BFD_RELOC_LO16);
4375 relax_end ();
4376
4377 if (ex.X_add_number != 0)
4378 {
4379 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4380 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4381 ex.X_op = O_constant;
4382 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4383 BFD_RELOC_LO16);
4384 }
4385 }
4386 }
4387 else
4388 abort ();
4389
4390 if (mips_opts.noat && *used_at == 1)
4391 as_bad (_("Macro used $at after \".set noat\""));
4392 }
4393
4394 /* Move the contents of register SOURCE into register DEST. */
4395
4396 static void
4397 move_register (int dest, int source)
4398 {
4399 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4400 dest, source, 0);
4401 }
4402
4403 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
4404 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4405 The two alternatives are:
4406
4407 Global symbol Local sybmol
4408 ------------- ------------
4409 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4410 ... ...
4411 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4412
4413 load_got_offset emits the first instruction and add_got_offset
4414 emits the second for a 16-bit offset or add_got_offset_hilo emits
4415 a sequence to add a 32-bit offset using a scratch register. */
4416
4417 static void
4418 load_got_offset (int dest, expressionS *local)
4419 {
4420 expressionS global;
4421
4422 global = *local;
4423 global.X_add_number = 0;
4424
4425 relax_start (local->X_add_symbol);
4426 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4427 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4428 relax_switch ();
4429 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4430 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4431 relax_end ();
4432 }
4433
4434 static void
4435 add_got_offset (int dest, expressionS *local)
4436 {
4437 expressionS global;
4438
4439 global.X_op = O_constant;
4440 global.X_op_symbol = NULL;
4441 global.X_add_symbol = NULL;
4442 global.X_add_number = local->X_add_number;
4443
4444 relax_start (local->X_add_symbol);
4445 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4446 dest, dest, BFD_RELOC_LO16);
4447 relax_switch ();
4448 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4449 relax_end ();
4450 }
4451
4452 static void
4453 add_got_offset_hilo (int dest, expressionS *local, int tmp)
4454 {
4455 expressionS global;
4456 int hold_mips_optimize;
4457
4458 global.X_op = O_constant;
4459 global.X_op_symbol = NULL;
4460 global.X_add_symbol = NULL;
4461 global.X_add_number = local->X_add_number;
4462
4463 relax_start (local->X_add_symbol);
4464 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4465 relax_switch ();
4466 /* Set mips_optimize around the lui instruction to avoid
4467 inserting an unnecessary nop after the lw. */
4468 hold_mips_optimize = mips_optimize;
4469 mips_optimize = 2;
4470 macro_build_lui (&global, tmp);
4471 mips_optimize = hold_mips_optimize;
4472 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4473 relax_end ();
4474
4475 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4476 }
4477
4478 /*
4479 * Build macros
4480 * This routine implements the seemingly endless macro or synthesized
4481 * instructions and addressing modes in the mips assembly language. Many
4482 * of these macros are simple and are similar to each other. These could
4483 * probably be handled by some kind of table or grammar approach instead of
4484 * this verbose method. Others are not simple macros but are more like
4485 * optimizing code generation.
4486 * One interesting optimization is when several store macros appear
4487 * consecutively that would load AT with the upper half of the same address.
4488 * The ensuing load upper instructions are ommited. This implies some kind
4489 * of global optimization. We currently only optimize within a single macro.
4490 * For many of the load and store macros if the address is specified as a
4491 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4492 * first load register 'at' with zero and use it as the base register. The
4493 * mips assembler simply uses register $zero. Just one tiny optimization
4494 * we're missing.
4495 */
4496 static void
4497 macro (struct mips_cl_insn *ip)
4498 {
4499 int treg, sreg, dreg, breg;
4500 int tempreg;
4501 int mask;
4502 int used_at = 0;
4503 expressionS expr1;
4504 const char *s;
4505 const char *s2;
4506 const char *fmt;
4507 int likely = 0;
4508 int dbl = 0;
4509 int coproc = 0;
4510 int lr = 0;
4511 int imm = 0;
4512 int call = 0;
4513 int off;
4514 offsetT maxnum;
4515 bfd_reloc_code_real_type r;
4516 int hold_mips_optimize;
4517
4518 assert (! mips_opts.mips16);
4519
4520 treg = (ip->insn_opcode >> 16) & 0x1f;
4521 dreg = (ip->insn_opcode >> 11) & 0x1f;
4522 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4523 mask = ip->insn_mo->mask;
4524
4525 expr1.X_op = O_constant;
4526 expr1.X_op_symbol = NULL;
4527 expr1.X_add_symbol = NULL;
4528 expr1.X_add_number = 1;
4529
4530 switch (mask)
4531 {
4532 case M_DABS:
4533 dbl = 1;
4534 case M_ABS:
4535 /* bgez $a0,.+12
4536 move v0,$a0
4537 sub v0,$zero,$a0
4538 */
4539
4540 start_noreorder ();
4541
4542 expr1.X_add_number = 8;
4543 macro_build (&expr1, "bgez", "s,p", sreg);
4544 if (dreg == sreg)
4545 macro_build (NULL, "nop", "", 0);
4546 else
4547 move_register (dreg, sreg);
4548 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4549
4550 end_noreorder ();
4551 break;
4552
4553 case M_ADD_I:
4554 s = "addi";
4555 s2 = "add";
4556 goto do_addi;
4557 case M_ADDU_I:
4558 s = "addiu";
4559 s2 = "addu";
4560 goto do_addi;
4561 case M_DADD_I:
4562 dbl = 1;
4563 s = "daddi";
4564 s2 = "dadd";
4565 goto do_addi;
4566 case M_DADDU_I:
4567 dbl = 1;
4568 s = "daddiu";
4569 s2 = "daddu";
4570 do_addi:
4571 if (imm_expr.X_op == O_constant
4572 && imm_expr.X_add_number >= -0x8000
4573 && imm_expr.X_add_number < 0x8000)
4574 {
4575 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
4576 break;
4577 }
4578 used_at = 1;
4579 load_register (AT, &imm_expr, dbl);
4580 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4581 break;
4582
4583 case M_AND_I:
4584 s = "andi";
4585 s2 = "and";
4586 goto do_bit;
4587 case M_OR_I:
4588 s = "ori";
4589 s2 = "or";
4590 goto do_bit;
4591 case M_NOR_I:
4592 s = "";
4593 s2 = "nor";
4594 goto do_bit;
4595 case M_XOR_I:
4596 s = "xori";
4597 s2 = "xor";
4598 do_bit:
4599 if (imm_expr.X_op == O_constant
4600 && imm_expr.X_add_number >= 0
4601 && imm_expr.X_add_number < 0x10000)
4602 {
4603 if (mask != M_NOR_I)
4604 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
4605 else
4606 {
4607 macro_build (&imm_expr, "ori", "t,r,i",
4608 treg, sreg, BFD_RELOC_LO16);
4609 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
4610 }
4611 break;
4612 }
4613
4614 used_at = 1;
4615 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4616 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4617 break;
4618
4619 case M_BEQ_I:
4620 s = "beq";
4621 goto beq_i;
4622 case M_BEQL_I:
4623 s = "beql";
4624 likely = 1;
4625 goto beq_i;
4626 case M_BNE_I:
4627 s = "bne";
4628 goto beq_i;
4629 case M_BNEL_I:
4630 s = "bnel";
4631 likely = 1;
4632 beq_i:
4633 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4634 {
4635 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
4636 break;
4637 }
4638 used_at = 1;
4639 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4640 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
4641 break;
4642
4643 case M_BGEL:
4644 likely = 1;
4645 case M_BGE:
4646 if (treg == 0)
4647 {
4648 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4649 break;
4650 }
4651 if (sreg == 0)
4652 {
4653 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
4654 break;
4655 }
4656 used_at = 1;
4657 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4658 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4659 break;
4660
4661 case M_BGTL_I:
4662 likely = 1;
4663 case M_BGT_I:
4664 /* check for > max integer */
4665 maxnum = 0x7fffffff;
4666 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4667 {
4668 maxnum <<= 16;
4669 maxnum |= 0xffff;
4670 maxnum <<= 16;
4671 maxnum |= 0xffff;
4672 }
4673 if (imm_expr.X_op == O_constant
4674 && imm_expr.X_add_number >= maxnum
4675 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4676 {
4677 do_false:
4678 /* result is always false */
4679 if (! likely)
4680 macro_build (NULL, "nop", "", 0);
4681 else
4682 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
4683 break;
4684 }
4685 if (imm_expr.X_op != O_constant)
4686 as_bad (_("Unsupported large constant"));
4687 ++imm_expr.X_add_number;
4688 /* FALLTHROUGH */
4689 case M_BGE_I:
4690 case M_BGEL_I:
4691 if (mask == M_BGEL_I)
4692 likely = 1;
4693 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4694 {
4695 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4696 break;
4697 }
4698 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4699 {
4700 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4701 break;
4702 }
4703 maxnum = 0x7fffffff;
4704 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4705 {
4706 maxnum <<= 16;
4707 maxnum |= 0xffff;
4708 maxnum <<= 16;
4709 maxnum |= 0xffff;
4710 }
4711 maxnum = - maxnum - 1;
4712 if (imm_expr.X_op == O_constant
4713 && imm_expr.X_add_number <= maxnum
4714 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4715 {
4716 do_true:
4717 /* result is always true */
4718 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4719 macro_build (&offset_expr, "b", "p");
4720 break;
4721 }
4722 used_at = 1;
4723 set_at (sreg, 0);
4724 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4725 break;
4726
4727 case M_BGEUL:
4728 likely = 1;
4729 case M_BGEU:
4730 if (treg == 0)
4731 goto do_true;
4732 if (sreg == 0)
4733 {
4734 macro_build (&offset_expr, likely ? "beql" : "beq",
4735 "s,t,p", 0, treg);
4736 break;
4737 }
4738 used_at = 1;
4739 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4740 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4741 break;
4742
4743 case M_BGTUL_I:
4744 likely = 1;
4745 case M_BGTU_I:
4746 if (sreg == 0
4747 || (HAVE_32BIT_GPRS
4748 && imm_expr.X_op == O_constant
4749 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4750 goto do_false;
4751 if (imm_expr.X_op != O_constant)
4752 as_bad (_("Unsupported large constant"));
4753 ++imm_expr.X_add_number;
4754 /* FALLTHROUGH */
4755 case M_BGEU_I:
4756 case M_BGEUL_I:
4757 if (mask == M_BGEUL_I)
4758 likely = 1;
4759 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4760 goto do_true;
4761 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4762 {
4763 macro_build (&offset_expr, likely ? "bnel" : "bne",
4764 "s,t,p", sreg, 0);
4765 break;
4766 }
4767 used_at = 1;
4768 set_at (sreg, 1);
4769 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4770 break;
4771
4772 case M_BGTL:
4773 likely = 1;
4774 case M_BGT:
4775 if (treg == 0)
4776 {
4777 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4778 break;
4779 }
4780 if (sreg == 0)
4781 {
4782 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
4783 break;
4784 }
4785 used_at = 1;
4786 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4787 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4788 break;
4789
4790 case M_BGTUL:
4791 likely = 1;
4792 case M_BGTU:
4793 if (treg == 0)
4794 {
4795 macro_build (&offset_expr, likely ? "bnel" : "bne",
4796 "s,t,p", sreg, 0);
4797 break;
4798 }
4799 if (sreg == 0)
4800 goto do_false;
4801 used_at = 1;
4802 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4803 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4804 break;
4805
4806 case M_BLEL:
4807 likely = 1;
4808 case M_BLE:
4809 if (treg == 0)
4810 {
4811 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4812 break;
4813 }
4814 if (sreg == 0)
4815 {
4816 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
4817 break;
4818 }
4819 used_at = 1;
4820 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4821 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4822 break;
4823
4824 case M_BLEL_I:
4825 likely = 1;
4826 case M_BLE_I:
4827 maxnum = 0x7fffffff;
4828 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4829 {
4830 maxnum <<= 16;
4831 maxnum |= 0xffff;
4832 maxnum <<= 16;
4833 maxnum |= 0xffff;
4834 }
4835 if (imm_expr.X_op == O_constant
4836 && imm_expr.X_add_number >= maxnum
4837 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4838 goto do_true;
4839 if (imm_expr.X_op != O_constant)
4840 as_bad (_("Unsupported large constant"));
4841 ++imm_expr.X_add_number;
4842 /* FALLTHROUGH */
4843 case M_BLT_I:
4844 case M_BLTL_I:
4845 if (mask == M_BLTL_I)
4846 likely = 1;
4847 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4848 {
4849 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4850 break;
4851 }
4852 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4853 {
4854 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4855 break;
4856 }
4857 used_at = 1;
4858 set_at (sreg, 0);
4859 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4860 break;
4861
4862 case M_BLEUL:
4863 likely = 1;
4864 case M_BLEU:
4865 if (treg == 0)
4866 {
4867 macro_build (&offset_expr, likely ? "beql" : "beq",
4868 "s,t,p", sreg, 0);
4869 break;
4870 }
4871 if (sreg == 0)
4872 goto do_true;
4873 used_at = 1;
4874 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4875 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4876 break;
4877
4878 case M_BLEUL_I:
4879 likely = 1;
4880 case M_BLEU_I:
4881 if (sreg == 0
4882 || (HAVE_32BIT_GPRS
4883 && imm_expr.X_op == O_constant
4884 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4885 goto do_true;
4886 if (imm_expr.X_op != O_constant)
4887 as_bad (_("Unsupported large constant"));
4888 ++imm_expr.X_add_number;
4889 /* FALLTHROUGH */
4890 case M_BLTU_I:
4891 case M_BLTUL_I:
4892 if (mask == M_BLTUL_I)
4893 likely = 1;
4894 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4895 goto do_false;
4896 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4897 {
4898 macro_build (&offset_expr, likely ? "beql" : "beq",
4899 "s,t,p", sreg, 0);
4900 break;
4901 }
4902 used_at = 1;
4903 set_at (sreg, 1);
4904 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4905 break;
4906
4907 case M_BLTL:
4908 likely = 1;
4909 case M_BLT:
4910 if (treg == 0)
4911 {
4912 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4913 break;
4914 }
4915 if (sreg == 0)
4916 {
4917 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
4918 break;
4919 }
4920 used_at = 1;
4921 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4922 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4923 break;
4924
4925 case M_BLTUL:
4926 likely = 1;
4927 case M_BLTU:
4928 if (treg == 0)
4929 goto do_false;
4930 if (sreg == 0)
4931 {
4932 macro_build (&offset_expr, likely ? "bnel" : "bne",
4933 "s,t,p", 0, treg);
4934 break;
4935 }
4936 used_at = 1;
4937 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4938 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4939 break;
4940
4941 case M_DEXT:
4942 {
4943 unsigned long pos;
4944 unsigned long size;
4945
4946 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4947 {
4948 as_bad (_("Unsupported large constant"));
4949 pos = size = 1;
4950 }
4951 else
4952 {
4953 pos = (unsigned long) imm_expr.X_add_number;
4954 size = (unsigned long) imm2_expr.X_add_number;
4955 }
4956
4957 if (pos > 63)
4958 {
4959 as_bad (_("Improper position (%lu)"), pos);
4960 pos = 1;
4961 }
4962 if (size == 0 || size > 64
4963 || (pos + size - 1) > 63)
4964 {
4965 as_bad (_("Improper extract size (%lu, position %lu)"),
4966 size, pos);
4967 size = 1;
4968 }
4969
4970 if (size <= 32 && pos < 32)
4971 {
4972 s = "dext";
4973 fmt = "t,r,+A,+C";
4974 }
4975 else if (size <= 32)
4976 {
4977 s = "dextu";
4978 fmt = "t,r,+E,+H";
4979 }
4980 else
4981 {
4982 s = "dextm";
4983 fmt = "t,r,+A,+G";
4984 }
4985 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
4986 }
4987 break;
4988
4989 case M_DINS:
4990 {
4991 unsigned long pos;
4992 unsigned long size;
4993
4994 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4995 {
4996 as_bad (_("Unsupported large constant"));
4997 pos = size = 1;
4998 }
4999 else
5000 {
5001 pos = (unsigned long) imm_expr.X_add_number;
5002 size = (unsigned long) imm2_expr.X_add_number;
5003 }
5004
5005 if (pos > 63)
5006 {
5007 as_bad (_("Improper position (%lu)"), pos);
5008 pos = 1;
5009 }
5010 if (size == 0 || size > 64
5011 || (pos + size - 1) > 63)
5012 {
5013 as_bad (_("Improper insert size (%lu, position %lu)"),
5014 size, pos);
5015 size = 1;
5016 }
5017
5018 if (pos < 32 && (pos + size - 1) < 32)
5019 {
5020 s = "dins";
5021 fmt = "t,r,+A,+B";
5022 }
5023 else if (pos >= 32)
5024 {
5025 s = "dinsu";
5026 fmt = "t,r,+E,+F";
5027 }
5028 else
5029 {
5030 s = "dinsm";
5031 fmt = "t,r,+A,+F";
5032 }
5033 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
5034 pos + size - 1);
5035 }
5036 break;
5037
5038 case M_DDIV_3:
5039 dbl = 1;
5040 case M_DIV_3:
5041 s = "mflo";
5042 goto do_div3;
5043 case M_DREM_3:
5044 dbl = 1;
5045 case M_REM_3:
5046 s = "mfhi";
5047 do_div3:
5048 if (treg == 0)
5049 {
5050 as_warn (_("Divide by zero."));
5051 if (mips_trap)
5052 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
5053 else
5054 macro_build (NULL, "break", "c", 7);
5055 break;
5056 }
5057
5058 start_noreorder ();
5059 if (mips_trap)
5060 {
5061 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
5062 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
5063 }
5064 else
5065 {
5066 expr1.X_add_number = 8;
5067 macro_build (&expr1, "bne", "s,t,p", treg, 0);
5068 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
5069 macro_build (NULL, "break", "c", 7);
5070 }
5071 expr1.X_add_number = -1;
5072 used_at = 1;
5073 load_register (AT, &expr1, dbl);
5074 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
5075 macro_build (&expr1, "bne", "s,t,p", treg, AT);
5076 if (dbl)
5077 {
5078 expr1.X_add_number = 1;
5079 load_register (AT, &expr1, dbl);
5080 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
5081 }
5082 else
5083 {
5084 expr1.X_add_number = 0x80000000;
5085 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
5086 }
5087 if (mips_trap)
5088 {
5089 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
5090 /* We want to close the noreorder block as soon as possible, so
5091 that later insns are available for delay slot filling. */
5092 end_noreorder ();
5093 }
5094 else
5095 {
5096 expr1.X_add_number = 8;
5097 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
5098 macro_build (NULL, "nop", "", 0);
5099
5100 /* We want to close the noreorder block as soon as possible, so
5101 that later insns are available for delay slot filling. */
5102 end_noreorder ();
5103
5104 macro_build (NULL, "break", "c", 6);
5105 }
5106 macro_build (NULL, s, "d", dreg);
5107 break;
5108
5109 case M_DIV_3I:
5110 s = "div";
5111 s2 = "mflo";
5112 goto do_divi;
5113 case M_DIVU_3I:
5114 s = "divu";
5115 s2 = "mflo";
5116 goto do_divi;
5117 case M_REM_3I:
5118 s = "div";
5119 s2 = "mfhi";
5120 goto do_divi;
5121 case M_REMU_3I:
5122 s = "divu";
5123 s2 = "mfhi";
5124 goto do_divi;
5125 case M_DDIV_3I:
5126 dbl = 1;
5127 s = "ddiv";
5128 s2 = "mflo";
5129 goto do_divi;
5130 case M_DDIVU_3I:
5131 dbl = 1;
5132 s = "ddivu";
5133 s2 = "mflo";
5134 goto do_divi;
5135 case M_DREM_3I:
5136 dbl = 1;
5137 s = "ddiv";
5138 s2 = "mfhi";
5139 goto do_divi;
5140 case M_DREMU_3I:
5141 dbl = 1;
5142 s = "ddivu";
5143 s2 = "mfhi";
5144 do_divi:
5145 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5146 {
5147 as_warn (_("Divide by zero."));
5148 if (mips_trap)
5149 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
5150 else
5151 macro_build (NULL, "break", "c", 7);
5152 break;
5153 }
5154 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
5155 {
5156 if (strcmp (s2, "mflo") == 0)
5157 move_register (dreg, sreg);
5158 else
5159 move_register (dreg, 0);
5160 break;
5161 }
5162 if (imm_expr.X_op == O_constant
5163 && imm_expr.X_add_number == -1
5164 && s[strlen (s) - 1] != 'u')
5165 {
5166 if (strcmp (s2, "mflo") == 0)
5167 {
5168 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
5169 }
5170 else
5171 move_register (dreg, 0);
5172 break;
5173 }
5174
5175 used_at = 1;
5176 load_register (AT, &imm_expr, dbl);
5177 macro_build (NULL, s, "z,s,t", sreg, AT);
5178 macro_build (NULL, s2, "d", dreg);
5179 break;
5180
5181 case M_DIVU_3:
5182 s = "divu";
5183 s2 = "mflo";
5184 goto do_divu3;
5185 case M_REMU_3:
5186 s = "divu";
5187 s2 = "mfhi";
5188 goto do_divu3;
5189 case M_DDIVU_3:
5190 s = "ddivu";
5191 s2 = "mflo";
5192 goto do_divu3;
5193 case M_DREMU_3:
5194 s = "ddivu";
5195 s2 = "mfhi";
5196 do_divu3:
5197 start_noreorder ();
5198 if (mips_trap)
5199 {
5200 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
5201 macro_build (NULL, s, "z,s,t", sreg, treg);
5202 /* We want to close the noreorder block as soon as possible, so
5203 that later insns are available for delay slot filling. */
5204 end_noreorder ();
5205 }
5206 else
5207 {
5208 expr1.X_add_number = 8;
5209 macro_build (&expr1, "bne", "s,t,p", treg, 0);
5210 macro_build (NULL, s, "z,s,t", sreg, treg);
5211
5212 /* We want to close the noreorder block as soon as possible, so
5213 that later insns are available for delay slot filling. */
5214 end_noreorder ();
5215 macro_build (NULL, "break", "c", 7);
5216 }
5217 macro_build (NULL, s2, "d", dreg);
5218 break;
5219
5220 case M_DLCA_AB:
5221 dbl = 1;
5222 case M_LCA_AB:
5223 call = 1;
5224 goto do_la;
5225 case M_DLA_AB:
5226 dbl = 1;
5227 case M_LA_AB:
5228 do_la:
5229 /* Load the address of a symbol into a register. If breg is not
5230 zero, we then add a base register to it. */
5231
5232 if (dbl && HAVE_32BIT_GPRS)
5233 as_warn (_("dla used to load 32-bit register"));
5234
5235 if (! dbl && HAVE_64BIT_OBJECTS)
5236 as_warn (_("la used to load 64-bit address"));
5237
5238 if (offset_expr.X_op == O_constant
5239 && offset_expr.X_add_number >= -0x8000
5240 && offset_expr.X_add_number < 0x8000)
5241 {
5242 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
5243 "t,r,j", treg, sreg, BFD_RELOC_LO16);
5244 break;
5245 }
5246
5247 if (!mips_opts.noat && (treg == breg))
5248 {
5249 tempreg = AT;
5250 used_at = 1;
5251 }
5252 else
5253 {
5254 tempreg = treg;
5255 }
5256
5257 if (offset_expr.X_op != O_symbol
5258 && offset_expr.X_op != O_constant)
5259 {
5260 as_bad (_("expression too complex"));
5261 offset_expr.X_op = O_constant;
5262 }
5263
5264 if (offset_expr.X_op == O_constant)
5265 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
5266 else if (mips_pic == NO_PIC)
5267 {
5268 /* If this is a reference to a GP relative symbol, we want
5269 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
5270 Otherwise we want
5271 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5272 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5273 If we have a constant, we need two instructions anyhow,
5274 so we may as well always use the latter form.
5275
5276 With 64bit address space and a usable $at we want
5277 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5278 lui $at,<sym> (BFD_RELOC_HI16_S)
5279 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5280 daddiu $at,<sym> (BFD_RELOC_LO16)
5281 dsll32 $tempreg,0
5282 daddu $tempreg,$tempreg,$at
5283
5284 If $at is already in use, we use a path which is suboptimal
5285 on superscalar processors.
5286 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5287 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5288 dsll $tempreg,16
5289 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5290 dsll $tempreg,16
5291 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
5292
5293 For GP relative symbols in 64bit address space we can use
5294 the same sequence as in 32bit address space. */
5295 if (HAVE_64BIT_SYMBOLS)
5296 {
5297 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5298 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5299 {
5300 relax_start (offset_expr.X_add_symbol);
5301 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5302 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5303 relax_switch ();
5304 }
5305
5306 if (used_at == 0 && !mips_opts.noat)
5307 {
5308 macro_build (&offset_expr, "lui", "t,u",
5309 tempreg, BFD_RELOC_MIPS_HIGHEST);
5310 macro_build (&offset_expr, "lui", "t,u",
5311 AT, BFD_RELOC_HI16_S);
5312 macro_build (&offset_expr, "daddiu", "t,r,j",
5313 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5314 macro_build (&offset_expr, "daddiu", "t,r,j",
5315 AT, AT, BFD_RELOC_LO16);
5316 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5317 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5318 used_at = 1;
5319 }
5320 else
5321 {
5322 macro_build (&offset_expr, "lui", "t,u",
5323 tempreg, BFD_RELOC_MIPS_HIGHEST);
5324 macro_build (&offset_expr, "daddiu", "t,r,j",
5325 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5326 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5327 macro_build (&offset_expr, "daddiu", "t,r,j",
5328 tempreg, tempreg, BFD_RELOC_HI16_S);
5329 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5330 macro_build (&offset_expr, "daddiu", "t,r,j",
5331 tempreg, tempreg, BFD_RELOC_LO16);
5332 }
5333
5334 if (mips_relax.sequence)
5335 relax_end ();
5336 }
5337 else
5338 {
5339 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5340 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5341 {
5342 relax_start (offset_expr.X_add_symbol);
5343 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5344 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5345 relax_switch ();
5346 }
5347 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5348 as_bad (_("offset too large"));
5349 macro_build_lui (&offset_expr, tempreg);
5350 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5351 tempreg, tempreg, BFD_RELOC_LO16);
5352 if (mips_relax.sequence)
5353 relax_end ();
5354 }
5355 }
5356 else if (!mips_big_got && !HAVE_NEWABI)
5357 {
5358 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5359
5360 /* If this is a reference to an external symbol, and there
5361 is no constant, we want
5362 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5363 or for lca or if tempreg is PIC_CALL_REG
5364 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5365 For a local symbol, we want
5366 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5367 nop
5368 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5369
5370 If we have a small constant, and this is a reference to
5371 an external symbol, we want
5372 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5373 nop
5374 addiu $tempreg,$tempreg,<constant>
5375 For a local symbol, we want the same instruction
5376 sequence, but we output a BFD_RELOC_LO16 reloc on the
5377 addiu instruction.
5378
5379 If we have a large constant, and this is a reference to
5380 an external symbol, we want
5381 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5382 lui $at,<hiconstant>
5383 addiu $at,$at,<loconstant>
5384 addu $tempreg,$tempreg,$at
5385 For a local symbol, we want the same instruction
5386 sequence, but we output a BFD_RELOC_LO16 reloc on the
5387 addiu instruction.
5388 */
5389
5390 if (offset_expr.X_add_number == 0)
5391 {
5392 if (mips_pic == SVR4_PIC
5393 && breg == 0
5394 && (call || tempreg == PIC_CALL_REG))
5395 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5396
5397 relax_start (offset_expr.X_add_symbol);
5398 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5399 lw_reloc_type, mips_gp_register);
5400 if (breg != 0)
5401 {
5402 /* We're going to put in an addu instruction using
5403 tempreg, so we may as well insert the nop right
5404 now. */
5405 load_delay_nop ();
5406 }
5407 relax_switch ();
5408 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5409 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
5410 load_delay_nop ();
5411 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5412 tempreg, tempreg, BFD_RELOC_LO16);
5413 relax_end ();
5414 /* FIXME: If breg == 0, and the next instruction uses
5415 $tempreg, then if this variant case is used an extra
5416 nop will be generated. */
5417 }
5418 else if (offset_expr.X_add_number >= -0x8000
5419 && offset_expr.X_add_number < 0x8000)
5420 {
5421 load_got_offset (tempreg, &offset_expr);
5422 load_delay_nop ();
5423 add_got_offset (tempreg, &offset_expr);
5424 }
5425 else
5426 {
5427 expr1.X_add_number = offset_expr.X_add_number;
5428 offset_expr.X_add_number =
5429 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
5430 load_got_offset (tempreg, &offset_expr);
5431 offset_expr.X_add_number = expr1.X_add_number;
5432 /* If we are going to add in a base register, and the
5433 target register and the base register are the same,
5434 then we are using AT as a temporary register. Since
5435 we want to load the constant into AT, we add our
5436 current AT (from the global offset table) and the
5437 register into the register now, and pretend we were
5438 not using a base register. */
5439 if (breg == treg)
5440 {
5441 load_delay_nop ();
5442 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5443 treg, AT, breg);
5444 breg = 0;
5445 tempreg = treg;
5446 }
5447 add_got_offset_hilo (tempreg, &offset_expr, AT);
5448 used_at = 1;
5449 }
5450 }
5451 else if (!mips_big_got && HAVE_NEWABI)
5452 {
5453 int add_breg_early = 0;
5454
5455 /* If this is a reference to an external, and there is no
5456 constant, or local symbol (*), with or without a
5457 constant, we want
5458 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5459 or for lca or if tempreg is PIC_CALL_REG
5460 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5461
5462 If we have a small constant, and this is a reference to
5463 an external symbol, we want
5464 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5465 addiu $tempreg,$tempreg,<constant>
5466
5467 If we have a large constant, and this is a reference to
5468 an external symbol, we want
5469 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5470 lui $at,<hiconstant>
5471 addiu $at,$at,<loconstant>
5472 addu $tempreg,$tempreg,$at
5473
5474 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5475 local symbols, even though it introduces an additional
5476 instruction. */
5477
5478 if (offset_expr.X_add_number)
5479 {
5480 expr1.X_add_number = offset_expr.X_add_number;
5481 offset_expr.X_add_number = 0;
5482
5483 relax_start (offset_expr.X_add_symbol);
5484 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5485 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5486
5487 if (expr1.X_add_number >= -0x8000
5488 && expr1.X_add_number < 0x8000)
5489 {
5490 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5491 tempreg, tempreg, BFD_RELOC_LO16);
5492 }
5493 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5494 {
5495 int dreg;
5496
5497 /* If we are going to add in a base register, and the
5498 target register and the base register are the same,
5499 then we are using AT as a temporary register. Since
5500 we want to load the constant into AT, we add our
5501 current AT (from the global offset table) and the
5502 register into the register now, and pretend we were
5503 not using a base register. */
5504 if (breg != treg)
5505 dreg = tempreg;
5506 else
5507 {
5508 assert (tempreg == AT);
5509 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5510 treg, AT, breg);
5511 dreg = treg;
5512 add_breg_early = 1;
5513 }
5514
5515 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5516 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5517 dreg, dreg, AT);
5518
5519 used_at = 1;
5520 }
5521 else
5522 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5523
5524 relax_switch ();
5525 offset_expr.X_add_number = expr1.X_add_number;
5526
5527 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5528 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5529 if (add_breg_early)
5530 {
5531 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5532 treg, tempreg, breg);
5533 breg = 0;
5534 tempreg = treg;
5535 }
5536 relax_end ();
5537 }
5538 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5539 {
5540 relax_start (offset_expr.X_add_symbol);
5541 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5542 BFD_RELOC_MIPS_CALL16, mips_gp_register);
5543 relax_switch ();
5544 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5545 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5546 relax_end ();
5547 }
5548 else
5549 {
5550 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5551 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5552 }
5553 }
5554 else if (mips_big_got && !HAVE_NEWABI)
5555 {
5556 int gpdelay;
5557 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5558 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5559 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5560
5561 /* This is the large GOT case. If this is a reference to an
5562 external symbol, and there is no constant, we want
5563 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5564 addu $tempreg,$tempreg,$gp
5565 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5566 or for lca or if tempreg is PIC_CALL_REG
5567 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5568 addu $tempreg,$tempreg,$gp
5569 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5570 For a local symbol, we want
5571 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5572 nop
5573 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5574
5575 If we have a small constant, and this is a reference to
5576 an external symbol, we want
5577 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5578 addu $tempreg,$tempreg,$gp
5579 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5580 nop
5581 addiu $tempreg,$tempreg,<constant>
5582 For a local symbol, we want
5583 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5584 nop
5585 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5586
5587 If we have a large constant, and this is a reference to
5588 an external symbol, we want
5589 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5590 addu $tempreg,$tempreg,$gp
5591 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5592 lui $at,<hiconstant>
5593 addiu $at,$at,<loconstant>
5594 addu $tempreg,$tempreg,$at
5595 For a local symbol, we want
5596 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5597 lui $at,<hiconstant>
5598 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5599 addu $tempreg,$tempreg,$at
5600 */
5601
5602 expr1.X_add_number = offset_expr.X_add_number;
5603 offset_expr.X_add_number = 0;
5604 relax_start (offset_expr.X_add_symbol);
5605 gpdelay = reg_needs_delay (mips_gp_register);
5606 if (expr1.X_add_number == 0 && breg == 0
5607 && (call || tempreg == PIC_CALL_REG))
5608 {
5609 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5610 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5611 }
5612 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5613 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5614 tempreg, tempreg, mips_gp_register);
5615 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5616 tempreg, lw_reloc_type, tempreg);
5617 if (expr1.X_add_number == 0)
5618 {
5619 if (breg != 0)
5620 {
5621 /* We're going to put in an addu instruction using
5622 tempreg, so we may as well insert the nop right
5623 now. */
5624 load_delay_nop ();
5625 }
5626 }
5627 else if (expr1.X_add_number >= -0x8000
5628 && expr1.X_add_number < 0x8000)
5629 {
5630 load_delay_nop ();
5631 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5632 tempreg, tempreg, BFD_RELOC_LO16);
5633 }
5634 else
5635 {
5636 int dreg;
5637
5638 /* If we are going to add in a base register, and the
5639 target register and the base register are the same,
5640 then we are using AT as a temporary register. Since
5641 we want to load the constant into AT, we add our
5642 current AT (from the global offset table) and the
5643 register into the register now, and pretend we were
5644 not using a base register. */
5645 if (breg != treg)
5646 dreg = tempreg;
5647 else
5648 {
5649 assert (tempreg == AT);
5650 load_delay_nop ();
5651 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5652 treg, AT, breg);
5653 dreg = treg;
5654 }
5655
5656 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5657 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5658
5659 used_at = 1;
5660 }
5661 offset_expr.X_add_number =
5662 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5663 relax_switch ();
5664
5665 if (gpdelay)
5666 {
5667 /* This is needed because this instruction uses $gp, but
5668 the first instruction on the main stream does not. */
5669 macro_build (NULL, "nop", "");
5670 }
5671
5672 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5673 local_reloc_type, mips_gp_register);
5674 if (expr1.X_add_number >= -0x8000
5675 && expr1.X_add_number < 0x8000)
5676 {
5677 load_delay_nop ();
5678 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5679 tempreg, tempreg, BFD_RELOC_LO16);
5680 /* FIXME: If add_number is 0, and there was no base
5681 register, the external symbol case ended with a load,
5682 so if the symbol turns out to not be external, and
5683 the next instruction uses tempreg, an unnecessary nop
5684 will be inserted. */
5685 }
5686 else
5687 {
5688 if (breg == treg)
5689 {
5690 /* We must add in the base register now, as in the
5691 external symbol case. */
5692 assert (tempreg == AT);
5693 load_delay_nop ();
5694 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5695 treg, AT, breg);
5696 tempreg = treg;
5697 /* We set breg to 0 because we have arranged to add
5698 it in in both cases. */
5699 breg = 0;
5700 }
5701
5702 macro_build_lui (&expr1, AT);
5703 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5704 AT, AT, BFD_RELOC_LO16);
5705 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5706 tempreg, tempreg, AT);
5707 used_at = 1;
5708 }
5709 relax_end ();
5710 }
5711 else if (mips_big_got && HAVE_NEWABI)
5712 {
5713 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5714 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5715 int add_breg_early = 0;
5716
5717 /* This is the large GOT case. If this is a reference to an
5718 external symbol, and there is no constant, we want
5719 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5720 add $tempreg,$tempreg,$gp
5721 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5722 or for lca or if tempreg is PIC_CALL_REG
5723 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5724 add $tempreg,$tempreg,$gp
5725 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5726
5727 If we have a small constant, and this is a reference to
5728 an external symbol, we want
5729 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5730 add $tempreg,$tempreg,$gp
5731 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5732 addi $tempreg,$tempreg,<constant>
5733
5734 If we have a large constant, and this is a reference to
5735 an external symbol, we want
5736 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5737 addu $tempreg,$tempreg,$gp
5738 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5739 lui $at,<hiconstant>
5740 addi $at,$at,<loconstant>
5741 add $tempreg,$tempreg,$at
5742
5743 If we have NewABI, and we know it's a local symbol, we want
5744 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5745 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5746 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5747
5748 relax_start (offset_expr.X_add_symbol);
5749
5750 expr1.X_add_number = offset_expr.X_add_number;
5751 offset_expr.X_add_number = 0;
5752
5753 if (expr1.X_add_number == 0 && breg == 0
5754 && (call || tempreg == PIC_CALL_REG))
5755 {
5756 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5757 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5758 }
5759 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5760 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5761 tempreg, tempreg, mips_gp_register);
5762 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5763 tempreg, lw_reloc_type, tempreg);
5764
5765 if (expr1.X_add_number == 0)
5766 ;
5767 else if (expr1.X_add_number >= -0x8000
5768 && expr1.X_add_number < 0x8000)
5769 {
5770 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5771 tempreg, tempreg, BFD_RELOC_LO16);
5772 }
5773 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5774 {
5775 int dreg;
5776
5777 /* If we are going to add in a base register, and the
5778 target register and the base register are the same,
5779 then we are using AT as a temporary register. Since
5780 we want to load the constant into AT, we add our
5781 current AT (from the global offset table) and the
5782 register into the register now, and pretend we were
5783 not using a base register. */
5784 if (breg != treg)
5785 dreg = tempreg;
5786 else
5787 {
5788 assert (tempreg == AT);
5789 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5790 treg, AT, breg);
5791 dreg = treg;
5792 add_breg_early = 1;
5793 }
5794
5795 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5796 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5797
5798 used_at = 1;
5799 }
5800 else
5801 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5802
5803 relax_switch ();
5804 offset_expr.X_add_number = expr1.X_add_number;
5805 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5806 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5807 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5808 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5809 if (add_breg_early)
5810 {
5811 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5812 treg, tempreg, breg);
5813 breg = 0;
5814 tempreg = treg;
5815 }
5816 relax_end ();
5817 }
5818 else
5819 abort ();
5820
5821 if (breg != 0)
5822 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
5823 break;
5824
5825 case M_J_A:
5826 /* The j instruction may not be used in PIC code, since it
5827 requires an absolute address. We convert it to a b
5828 instruction. */
5829 if (mips_pic == NO_PIC)
5830 macro_build (&offset_expr, "j", "a");
5831 else
5832 macro_build (&offset_expr, "b", "p");
5833 break;
5834
5835 /* The jal instructions must be handled as macros because when
5836 generating PIC code they expand to multi-instruction
5837 sequences. Normally they are simple instructions. */
5838 case M_JAL_1:
5839 dreg = RA;
5840 /* Fall through. */
5841 case M_JAL_2:
5842 if (mips_pic == NO_PIC)
5843 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5844 else
5845 {
5846 if (sreg != PIC_CALL_REG)
5847 as_warn (_("MIPS PIC call to register other than $25"));
5848
5849 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5850 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
5851 {
5852 if (mips_cprestore_offset < 0)
5853 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5854 else
5855 {
5856 if (! mips_frame_reg_valid)
5857 {
5858 as_warn (_("No .frame pseudo-op used in PIC code"));
5859 /* Quiet this warning. */
5860 mips_frame_reg_valid = 1;
5861 }
5862 if (! mips_cprestore_valid)
5863 {
5864 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5865 /* Quiet this warning. */
5866 mips_cprestore_valid = 1;
5867 }
5868 expr1.X_add_number = mips_cprestore_offset;
5869 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5870 mips_gp_register,
5871 mips_frame_reg,
5872 HAVE_64BIT_ADDRESSES);
5873 }
5874 }
5875 }
5876
5877 break;
5878
5879 case M_JAL_A:
5880 if (mips_pic == NO_PIC)
5881 macro_build (&offset_expr, "jal", "a");
5882 else if (mips_pic == SVR4_PIC)
5883 {
5884 /* If this is a reference to an external symbol, and we are
5885 using a small GOT, we want
5886 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5887 nop
5888 jalr $ra,$25
5889 nop
5890 lw $gp,cprestore($sp)
5891 The cprestore value is set using the .cprestore
5892 pseudo-op. If we are using a big GOT, we want
5893 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5894 addu $25,$25,$gp
5895 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5896 nop
5897 jalr $ra,$25
5898 nop
5899 lw $gp,cprestore($sp)
5900 If the symbol is not external, we want
5901 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5902 nop
5903 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5904 jalr $ra,$25
5905 nop
5906 lw $gp,cprestore($sp)
5907
5908 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5909 sequences above, minus nops, unless the symbol is local,
5910 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5911 GOT_DISP. */
5912 if (HAVE_NEWABI)
5913 {
5914 if (! mips_big_got)
5915 {
5916 relax_start (offset_expr.X_add_symbol);
5917 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5918 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5919 mips_gp_register);
5920 relax_switch ();
5921 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5922 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
5923 mips_gp_register);
5924 relax_end ();
5925 }
5926 else
5927 {
5928 relax_start (offset_expr.X_add_symbol);
5929 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5930 BFD_RELOC_MIPS_CALL_HI16);
5931 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5932 PIC_CALL_REG, mips_gp_register);
5933 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5934 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5935 PIC_CALL_REG);
5936 relax_switch ();
5937 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5938 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5939 mips_gp_register);
5940 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5941 PIC_CALL_REG, PIC_CALL_REG,
5942 BFD_RELOC_MIPS_GOT_OFST);
5943 relax_end ();
5944 }
5945
5946 macro_build_jalr (&offset_expr);
5947 }
5948 else
5949 {
5950 relax_start (offset_expr.X_add_symbol);
5951 if (! mips_big_got)
5952 {
5953 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5954 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5955 mips_gp_register);
5956 load_delay_nop ();
5957 relax_switch ();
5958 }
5959 else
5960 {
5961 int gpdelay;
5962
5963 gpdelay = reg_needs_delay (mips_gp_register);
5964 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5965 BFD_RELOC_MIPS_CALL_HI16);
5966 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5967 PIC_CALL_REG, mips_gp_register);
5968 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5969 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5970 PIC_CALL_REG);
5971 load_delay_nop ();
5972 relax_switch ();
5973 if (gpdelay)
5974 macro_build (NULL, "nop", "");
5975 }
5976 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5977 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5978 mips_gp_register);
5979 load_delay_nop ();
5980 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5981 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
5982 relax_end ();
5983 macro_build_jalr (&offset_expr);
5984
5985 if (mips_cprestore_offset < 0)
5986 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5987 else
5988 {
5989 if (! mips_frame_reg_valid)
5990 {
5991 as_warn (_("No .frame pseudo-op used in PIC code"));
5992 /* Quiet this warning. */
5993 mips_frame_reg_valid = 1;
5994 }
5995 if (! mips_cprestore_valid)
5996 {
5997 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5998 /* Quiet this warning. */
5999 mips_cprestore_valid = 1;
6000 }
6001 if (mips_opts.noreorder)
6002 macro_build (NULL, "nop", "");
6003 expr1.X_add_number = mips_cprestore_offset;
6004 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
6005 mips_gp_register,
6006 mips_frame_reg,
6007 HAVE_64BIT_ADDRESSES);
6008 }
6009 }
6010 }
6011 else if (mips_pic == VXWORKS_PIC)
6012 as_bad (_("Non-PIC jump used in PIC library"));
6013 else
6014 abort ();
6015
6016 break;
6017
6018 case M_LB_AB:
6019 s = "lb";
6020 goto ld;
6021 case M_LBU_AB:
6022 s = "lbu";
6023 goto ld;
6024 case M_LH_AB:
6025 s = "lh";
6026 goto ld;
6027 case M_LHU_AB:
6028 s = "lhu";
6029 goto ld;
6030 case M_LW_AB:
6031 s = "lw";
6032 goto ld;
6033 case M_LWC0_AB:
6034 s = "lwc0";
6035 /* Itbl support may require additional care here. */
6036 coproc = 1;
6037 goto ld;
6038 case M_LWC1_AB:
6039 s = "lwc1";
6040 /* Itbl support may require additional care here. */
6041 coproc = 1;
6042 goto ld;
6043 case M_LWC2_AB:
6044 s = "lwc2";
6045 /* Itbl support may require additional care here. */
6046 coproc = 1;
6047 goto ld;
6048 case M_LWC3_AB:
6049 s = "lwc3";
6050 /* Itbl support may require additional care here. */
6051 coproc = 1;
6052 goto ld;
6053 case M_LWL_AB:
6054 s = "lwl";
6055 lr = 1;
6056 goto ld;
6057 case M_LWR_AB:
6058 s = "lwr";
6059 lr = 1;
6060 goto ld;
6061 case M_LDC1_AB:
6062 if (mips_opts.arch == CPU_R4650)
6063 {
6064 as_bad (_("opcode not supported on this processor"));
6065 break;
6066 }
6067 s = "ldc1";
6068 /* Itbl support may require additional care here. */
6069 coproc = 1;
6070 goto ld;
6071 case M_LDC2_AB:
6072 s = "ldc2";
6073 /* Itbl support may require additional care here. */
6074 coproc = 1;
6075 goto ld;
6076 case M_LDC3_AB:
6077 s = "ldc3";
6078 /* Itbl support may require additional care here. */
6079 coproc = 1;
6080 goto ld;
6081 case M_LDL_AB:
6082 s = "ldl";
6083 lr = 1;
6084 goto ld;
6085 case M_LDR_AB:
6086 s = "ldr";
6087 lr = 1;
6088 goto ld;
6089 case M_LL_AB:
6090 s = "ll";
6091 goto ld;
6092 case M_LLD_AB:
6093 s = "lld";
6094 goto ld;
6095 case M_LWU_AB:
6096 s = "lwu";
6097 ld:
6098 if (breg == treg || coproc || lr)
6099 {
6100 tempreg = AT;
6101 used_at = 1;
6102 }
6103 else
6104 {
6105 tempreg = treg;
6106 }
6107 goto ld_st;
6108 case M_SB_AB:
6109 s = "sb";
6110 goto st;
6111 case M_SH_AB:
6112 s = "sh";
6113 goto st;
6114 case M_SW_AB:
6115 s = "sw";
6116 goto st;
6117 case M_SWC0_AB:
6118 s = "swc0";
6119 /* Itbl support may require additional care here. */
6120 coproc = 1;
6121 goto st;
6122 case M_SWC1_AB:
6123 s = "swc1";
6124 /* Itbl support may require additional care here. */
6125 coproc = 1;
6126 goto st;
6127 case M_SWC2_AB:
6128 s = "swc2";
6129 /* Itbl support may require additional care here. */
6130 coproc = 1;
6131 goto st;
6132 case M_SWC3_AB:
6133 s = "swc3";
6134 /* Itbl support may require additional care here. */
6135 coproc = 1;
6136 goto st;
6137 case M_SWL_AB:
6138 s = "swl";
6139 goto st;
6140 case M_SWR_AB:
6141 s = "swr";
6142 goto st;
6143 case M_SC_AB:
6144 s = "sc";
6145 goto st;
6146 case M_SCD_AB:
6147 s = "scd";
6148 goto st;
6149 case M_CACHE_AB:
6150 s = "cache";
6151 goto st;
6152 case M_SDC1_AB:
6153 if (mips_opts.arch == CPU_R4650)
6154 {
6155 as_bad (_("opcode not supported on this processor"));
6156 break;
6157 }
6158 s = "sdc1";
6159 coproc = 1;
6160 /* Itbl support may require additional care here. */
6161 goto st;
6162 case M_SDC2_AB:
6163 s = "sdc2";
6164 /* Itbl support may require additional care here. */
6165 coproc = 1;
6166 goto st;
6167 case M_SDC3_AB:
6168 s = "sdc3";
6169 /* Itbl support may require additional care here. */
6170 coproc = 1;
6171 goto st;
6172 case M_SDL_AB:
6173 s = "sdl";
6174 goto st;
6175 case M_SDR_AB:
6176 s = "sdr";
6177 st:
6178 tempreg = AT;
6179 used_at = 1;
6180 ld_st:
6181 /* Itbl support may require additional care here. */
6182 if (mask == M_LWC1_AB
6183 || mask == M_SWC1_AB
6184 || mask == M_LDC1_AB
6185 || mask == M_SDC1_AB
6186 || mask == M_L_DAB
6187 || mask == M_S_DAB)
6188 fmt = "T,o(b)";
6189 else if (mask == M_CACHE_AB)
6190 fmt = "k,o(b)";
6191 else if (coproc)
6192 fmt = "E,o(b)";
6193 else
6194 fmt = "t,o(b)";
6195
6196 if (offset_expr.X_op != O_constant
6197 && offset_expr.X_op != O_symbol)
6198 {
6199 as_bad (_("expression too complex"));
6200 offset_expr.X_op = O_constant;
6201 }
6202
6203 if (HAVE_32BIT_ADDRESSES
6204 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6205 {
6206 char value [32];
6207
6208 sprintf_vma (value, offset_expr.X_add_number);
6209 as_bad (_("Number (0x%s) larger than 32 bits"), value);
6210 }
6211
6212 /* A constant expression in PIC code can be handled just as it
6213 is in non PIC code. */
6214 if (offset_expr.X_op == O_constant)
6215 {
6216 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
6217 & ~(bfd_vma) 0xffff);
6218 normalize_address_expr (&expr1);
6219 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
6220 if (breg != 0)
6221 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6222 tempreg, tempreg, breg);
6223 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6224 }
6225 else if (mips_pic == NO_PIC)
6226 {
6227 /* If this is a reference to a GP relative symbol, and there
6228 is no base register, we want
6229 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6230 Otherwise, if there is no base register, we want
6231 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6232 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6233 If we have a constant, we need two instructions anyhow,
6234 so we always use the latter form.
6235
6236 If we have a base register, and this is a reference to a
6237 GP relative symbol, we want
6238 addu $tempreg,$breg,$gp
6239 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
6240 Otherwise we want
6241 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6242 addu $tempreg,$tempreg,$breg
6243 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6244 With a constant we always use the latter case.
6245
6246 With 64bit address space and no base register and $at usable,
6247 we want
6248 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6249 lui $at,<sym> (BFD_RELOC_HI16_S)
6250 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6251 dsll32 $tempreg,0
6252 daddu $tempreg,$at
6253 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6254 If we have a base register, we want
6255 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6256 lui $at,<sym> (BFD_RELOC_HI16_S)
6257 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6258 daddu $at,$breg
6259 dsll32 $tempreg,0
6260 daddu $tempreg,$at
6261 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6262
6263 Without $at we can't generate the optimal path for superscalar
6264 processors here since this would require two temporary registers.
6265 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6266 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6267 dsll $tempreg,16
6268 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6269 dsll $tempreg,16
6270 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6271 If we have a base register, we want
6272 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6273 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6274 dsll $tempreg,16
6275 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6276 dsll $tempreg,16
6277 daddu $tempreg,$tempreg,$breg
6278 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6279
6280 For GP relative symbols in 64bit address space we can use
6281 the same sequence as in 32bit address space. */
6282 if (HAVE_64BIT_SYMBOLS)
6283 {
6284 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6285 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6286 {
6287 relax_start (offset_expr.X_add_symbol);
6288 if (breg == 0)
6289 {
6290 macro_build (&offset_expr, s, fmt, treg,
6291 BFD_RELOC_GPREL16, mips_gp_register);
6292 }
6293 else
6294 {
6295 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6296 tempreg, breg, mips_gp_register);
6297 macro_build (&offset_expr, s, fmt, treg,
6298 BFD_RELOC_GPREL16, tempreg);
6299 }
6300 relax_switch ();
6301 }
6302
6303 if (used_at == 0 && !mips_opts.noat)
6304 {
6305 macro_build (&offset_expr, "lui", "t,u", tempreg,
6306 BFD_RELOC_MIPS_HIGHEST);
6307 macro_build (&offset_expr, "lui", "t,u", AT,
6308 BFD_RELOC_HI16_S);
6309 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6310 tempreg, BFD_RELOC_MIPS_HIGHER);
6311 if (breg != 0)
6312 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
6313 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
6314 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
6315 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
6316 tempreg);
6317 used_at = 1;
6318 }
6319 else
6320 {
6321 macro_build (&offset_expr, "lui", "t,u", tempreg,
6322 BFD_RELOC_MIPS_HIGHEST);
6323 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6324 tempreg, BFD_RELOC_MIPS_HIGHER);
6325 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6326 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6327 tempreg, BFD_RELOC_HI16_S);
6328 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6329 if (breg != 0)
6330 macro_build (NULL, "daddu", "d,v,t",
6331 tempreg, tempreg, breg);
6332 macro_build (&offset_expr, s, fmt, treg,
6333 BFD_RELOC_LO16, tempreg);
6334 }
6335
6336 if (mips_relax.sequence)
6337 relax_end ();
6338 break;
6339 }
6340
6341 if (breg == 0)
6342 {
6343 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6344 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6345 {
6346 relax_start (offset_expr.X_add_symbol);
6347 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6348 mips_gp_register);
6349 relax_switch ();
6350 }
6351 macro_build_lui (&offset_expr, tempreg);
6352 macro_build (&offset_expr, s, fmt, treg,
6353 BFD_RELOC_LO16, tempreg);
6354 if (mips_relax.sequence)
6355 relax_end ();
6356 }
6357 else
6358 {
6359 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6360 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6361 {
6362 relax_start (offset_expr.X_add_symbol);
6363 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6364 tempreg, breg, mips_gp_register);
6365 macro_build (&offset_expr, s, fmt, treg,
6366 BFD_RELOC_GPREL16, tempreg);
6367 relax_switch ();
6368 }
6369 macro_build_lui (&offset_expr, tempreg);
6370 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6371 tempreg, tempreg, breg);
6372 macro_build (&offset_expr, s, fmt, treg,
6373 BFD_RELOC_LO16, tempreg);
6374 if (mips_relax.sequence)
6375 relax_end ();
6376 }
6377 }
6378 else if (!mips_big_got)
6379 {
6380 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6381
6382 /* If this is a reference to an external symbol, we want
6383 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6384 nop
6385 <op> $treg,0($tempreg)
6386 Otherwise we want
6387 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6388 nop
6389 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6390 <op> $treg,0($tempreg)
6391
6392 For NewABI, we want
6393 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6394 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6395
6396 If there is a base register, we add it to $tempreg before
6397 the <op>. If there is a constant, we stick it in the
6398 <op> instruction. We don't handle constants larger than
6399 16 bits, because we have no way to load the upper 16 bits
6400 (actually, we could handle them for the subset of cases
6401 in which we are not using $at). */
6402 assert (offset_expr.X_op == O_symbol);
6403 if (HAVE_NEWABI)
6404 {
6405 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6406 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6407 if (breg != 0)
6408 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6409 tempreg, tempreg, breg);
6410 macro_build (&offset_expr, s, fmt, treg,
6411 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6412 break;
6413 }
6414 expr1.X_add_number = offset_expr.X_add_number;
6415 offset_expr.X_add_number = 0;
6416 if (expr1.X_add_number < -0x8000
6417 || expr1.X_add_number >= 0x8000)
6418 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6419 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6420 lw_reloc_type, mips_gp_register);
6421 load_delay_nop ();
6422 relax_start (offset_expr.X_add_symbol);
6423 relax_switch ();
6424 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6425 tempreg, BFD_RELOC_LO16);
6426 relax_end ();
6427 if (breg != 0)
6428 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6429 tempreg, tempreg, breg);
6430 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6431 }
6432 else if (mips_big_got && !HAVE_NEWABI)
6433 {
6434 int gpdelay;
6435
6436 /* If this is a reference to an external symbol, we want
6437 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6438 addu $tempreg,$tempreg,$gp
6439 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6440 <op> $treg,0($tempreg)
6441 Otherwise we want
6442 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6443 nop
6444 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6445 <op> $treg,0($tempreg)
6446 If there is a base register, we add it to $tempreg before
6447 the <op>. If there is a constant, we stick it in the
6448 <op> instruction. We don't handle constants larger than
6449 16 bits, because we have no way to load the upper 16 bits
6450 (actually, we could handle them for the subset of cases
6451 in which we are not using $at). */
6452 assert (offset_expr.X_op == O_symbol);
6453 expr1.X_add_number = offset_expr.X_add_number;
6454 offset_expr.X_add_number = 0;
6455 if (expr1.X_add_number < -0x8000
6456 || expr1.X_add_number >= 0x8000)
6457 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6458 gpdelay = reg_needs_delay (mips_gp_register);
6459 relax_start (offset_expr.X_add_symbol);
6460 macro_build (&offset_expr, "lui", "t,u", tempreg,
6461 BFD_RELOC_MIPS_GOT_HI16);
6462 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6463 mips_gp_register);
6464 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6465 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6466 relax_switch ();
6467 if (gpdelay)
6468 macro_build (NULL, "nop", "");
6469 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6470 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6471 load_delay_nop ();
6472 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6473 tempreg, BFD_RELOC_LO16);
6474 relax_end ();
6475
6476 if (breg != 0)
6477 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6478 tempreg, tempreg, breg);
6479 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6480 }
6481 else if (mips_big_got && HAVE_NEWABI)
6482 {
6483 /* If this is a reference to an external symbol, we want
6484 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6485 add $tempreg,$tempreg,$gp
6486 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6487 <op> $treg,<ofst>($tempreg)
6488 Otherwise, for local symbols, we want:
6489 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6490 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6491 assert (offset_expr.X_op == O_symbol);
6492 expr1.X_add_number = offset_expr.X_add_number;
6493 offset_expr.X_add_number = 0;
6494 if (expr1.X_add_number < -0x8000
6495 || expr1.X_add_number >= 0x8000)
6496 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6497 relax_start (offset_expr.X_add_symbol);
6498 macro_build (&offset_expr, "lui", "t,u", tempreg,
6499 BFD_RELOC_MIPS_GOT_HI16);
6500 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6501 mips_gp_register);
6502 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6503 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6504 if (breg != 0)
6505 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6506 tempreg, tempreg, breg);
6507 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6508
6509 relax_switch ();
6510 offset_expr.X_add_number = expr1.X_add_number;
6511 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6512 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6513 if (breg != 0)
6514 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6515 tempreg, tempreg, breg);
6516 macro_build (&offset_expr, s, fmt, treg,
6517 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6518 relax_end ();
6519 }
6520 else
6521 abort ();
6522
6523 break;
6524
6525 case M_LI:
6526 case M_LI_S:
6527 load_register (treg, &imm_expr, 0);
6528 break;
6529
6530 case M_DLI:
6531 load_register (treg, &imm_expr, 1);
6532 break;
6533
6534 case M_LI_SS:
6535 if (imm_expr.X_op == O_constant)
6536 {
6537 used_at = 1;
6538 load_register (AT, &imm_expr, 0);
6539 macro_build (NULL, "mtc1", "t,G", AT, treg);
6540 break;
6541 }
6542 else
6543 {
6544 assert (offset_expr.X_op == O_symbol
6545 && strcmp (segment_name (S_GET_SEGMENT
6546 (offset_expr.X_add_symbol)),
6547 ".lit4") == 0
6548 && offset_expr.X_add_number == 0);
6549 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
6550 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6551 break;
6552 }
6553
6554 case M_LI_D:
6555 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6556 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6557 order 32 bits of the value and the low order 32 bits are either
6558 zero or in OFFSET_EXPR. */
6559 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6560 {
6561 if (HAVE_64BIT_GPRS)
6562 load_register (treg, &imm_expr, 1);
6563 else
6564 {
6565 int hreg, lreg;
6566
6567 if (target_big_endian)
6568 {
6569 hreg = treg;
6570 lreg = treg + 1;
6571 }
6572 else
6573 {
6574 hreg = treg + 1;
6575 lreg = treg;
6576 }
6577
6578 if (hreg <= 31)
6579 load_register (hreg, &imm_expr, 0);
6580 if (lreg <= 31)
6581 {
6582 if (offset_expr.X_op == O_absent)
6583 move_register (lreg, 0);
6584 else
6585 {
6586 assert (offset_expr.X_op == O_constant);
6587 load_register (lreg, &offset_expr, 0);
6588 }
6589 }
6590 }
6591 break;
6592 }
6593
6594 /* We know that sym is in the .rdata section. First we get the
6595 upper 16 bits of the address. */
6596 if (mips_pic == NO_PIC)
6597 {
6598 macro_build_lui (&offset_expr, AT);
6599 used_at = 1;
6600 }
6601 else
6602 {
6603 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6604 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6605 used_at = 1;
6606 }
6607
6608 /* Now we load the register(s). */
6609 if (HAVE_64BIT_GPRS)
6610 {
6611 used_at = 1;
6612 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6613 }
6614 else
6615 {
6616 used_at = 1;
6617 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6618 if (treg != RA)
6619 {
6620 /* FIXME: How in the world do we deal with the possible
6621 overflow here? */
6622 offset_expr.X_add_number += 4;
6623 macro_build (&offset_expr, "lw", "t,o(b)",
6624 treg + 1, BFD_RELOC_LO16, AT);
6625 }
6626 }
6627 break;
6628
6629 case M_LI_DD:
6630 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6631 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6632 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6633 the value and the low order 32 bits are either zero or in
6634 OFFSET_EXPR. */
6635 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6636 {
6637 used_at = 1;
6638 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
6639 if (HAVE_64BIT_FPRS)
6640 {
6641 assert (HAVE_64BIT_GPRS);
6642 macro_build (NULL, "dmtc1", "t,S", AT, treg);
6643 }
6644 else
6645 {
6646 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
6647 if (offset_expr.X_op == O_absent)
6648 macro_build (NULL, "mtc1", "t,G", 0, treg);
6649 else
6650 {
6651 assert (offset_expr.X_op == O_constant);
6652 load_register (AT, &offset_expr, 0);
6653 macro_build (NULL, "mtc1", "t,G", AT, treg);
6654 }
6655 }
6656 break;
6657 }
6658
6659 assert (offset_expr.X_op == O_symbol
6660 && offset_expr.X_add_number == 0);
6661 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6662 if (strcmp (s, ".lit8") == 0)
6663 {
6664 if (mips_opts.isa != ISA_MIPS1)
6665 {
6666 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
6667 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6668 break;
6669 }
6670 breg = mips_gp_register;
6671 r = BFD_RELOC_MIPS_LITERAL;
6672 goto dob;
6673 }
6674 else
6675 {
6676 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6677 used_at = 1;
6678 if (mips_pic != NO_PIC)
6679 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6680 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6681 else
6682 {
6683 /* FIXME: This won't work for a 64 bit address. */
6684 macro_build_lui (&offset_expr, AT);
6685 }
6686
6687 if (mips_opts.isa != ISA_MIPS1)
6688 {
6689 macro_build (&offset_expr, "ldc1", "T,o(b)",
6690 treg, BFD_RELOC_LO16, AT);
6691 break;
6692 }
6693 breg = AT;
6694 r = BFD_RELOC_LO16;
6695 goto dob;
6696 }
6697
6698 case M_L_DOB:
6699 if (mips_opts.arch == CPU_R4650)
6700 {
6701 as_bad (_("opcode not supported on this processor"));
6702 break;
6703 }
6704 /* Even on a big endian machine $fn comes before $fn+1. We have
6705 to adjust when loading from memory. */
6706 r = BFD_RELOC_LO16;
6707 dob:
6708 assert (mips_opts.isa == ISA_MIPS1);
6709 macro_build (&offset_expr, "lwc1", "T,o(b)",
6710 target_big_endian ? treg + 1 : treg, r, breg);
6711 /* FIXME: A possible overflow which I don't know how to deal
6712 with. */
6713 offset_expr.X_add_number += 4;
6714 macro_build (&offset_expr, "lwc1", "T,o(b)",
6715 target_big_endian ? treg : treg + 1, r, breg);
6716 break;
6717
6718 case M_L_DAB:
6719 /*
6720 * The MIPS assembler seems to check for X_add_number not
6721 * being double aligned and generating:
6722 * lui at,%hi(foo+1)
6723 * addu at,at,v1
6724 * addiu at,at,%lo(foo+1)
6725 * lwc1 f2,0(at)
6726 * lwc1 f3,4(at)
6727 * But, the resulting address is the same after relocation so why
6728 * generate the extra instruction?
6729 */
6730 if (mips_opts.arch == CPU_R4650)
6731 {
6732 as_bad (_("opcode not supported on this processor"));
6733 break;
6734 }
6735 /* Itbl support may require additional care here. */
6736 coproc = 1;
6737 if (mips_opts.isa != ISA_MIPS1)
6738 {
6739 s = "ldc1";
6740 goto ld;
6741 }
6742
6743 s = "lwc1";
6744 fmt = "T,o(b)";
6745 goto ldd_std;
6746
6747 case M_S_DAB:
6748 if (mips_opts.arch == CPU_R4650)
6749 {
6750 as_bad (_("opcode not supported on this processor"));
6751 break;
6752 }
6753
6754 if (mips_opts.isa != ISA_MIPS1)
6755 {
6756 s = "sdc1";
6757 goto st;
6758 }
6759
6760 s = "swc1";
6761 fmt = "T,o(b)";
6762 /* Itbl support may require additional care here. */
6763 coproc = 1;
6764 goto ldd_std;
6765
6766 case M_LD_AB:
6767 if (HAVE_64BIT_GPRS)
6768 {
6769 s = "ld";
6770 goto ld;
6771 }
6772
6773 s = "lw";
6774 fmt = "t,o(b)";
6775 goto ldd_std;
6776
6777 case M_SD_AB:
6778 if (HAVE_64BIT_GPRS)
6779 {
6780 s = "sd";
6781 goto st;
6782 }
6783
6784 s = "sw";
6785 fmt = "t,o(b)";
6786
6787 ldd_std:
6788 if (offset_expr.X_op != O_symbol
6789 && offset_expr.X_op != O_constant)
6790 {
6791 as_bad (_("expression too complex"));
6792 offset_expr.X_op = O_constant;
6793 }
6794
6795 if (HAVE_32BIT_ADDRESSES
6796 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6797 {
6798 char value [32];
6799
6800 sprintf_vma (value, offset_expr.X_add_number);
6801 as_bad (_("Number (0x%s) larger than 32 bits"), value);
6802 }
6803
6804 /* Even on a big endian machine $fn comes before $fn+1. We have
6805 to adjust when loading from memory. We set coproc if we must
6806 load $fn+1 first. */
6807 /* Itbl support may require additional care here. */
6808 if (! target_big_endian)
6809 coproc = 0;
6810
6811 if (mips_pic == NO_PIC
6812 || offset_expr.X_op == O_constant)
6813 {
6814 /* If this is a reference to a GP relative symbol, we want
6815 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6816 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6817 If we have a base register, we use this
6818 addu $at,$breg,$gp
6819 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6820 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6821 If this is not a GP relative symbol, we want
6822 lui $at,<sym> (BFD_RELOC_HI16_S)
6823 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6824 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6825 If there is a base register, we add it to $at after the
6826 lui instruction. If there is a constant, we always use
6827 the last case. */
6828 if (offset_expr.X_op == O_symbol
6829 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6830 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6831 {
6832 relax_start (offset_expr.X_add_symbol);
6833 if (breg == 0)
6834 {
6835 tempreg = mips_gp_register;
6836 }
6837 else
6838 {
6839 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6840 AT, breg, mips_gp_register);
6841 tempreg = AT;
6842 used_at = 1;
6843 }
6844
6845 /* Itbl support may require additional care here. */
6846 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6847 BFD_RELOC_GPREL16, tempreg);
6848 offset_expr.X_add_number += 4;
6849
6850 /* Set mips_optimize to 2 to avoid inserting an
6851 undesired nop. */
6852 hold_mips_optimize = mips_optimize;
6853 mips_optimize = 2;
6854 /* Itbl support may require additional care here. */
6855 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6856 BFD_RELOC_GPREL16, tempreg);
6857 mips_optimize = hold_mips_optimize;
6858
6859 relax_switch ();
6860
6861 /* We just generated two relocs. When tc_gen_reloc
6862 handles this case, it will skip the first reloc and
6863 handle the second. The second reloc already has an
6864 extra addend of 4, which we added above. We must
6865 subtract it out, and then subtract another 4 to make
6866 the first reloc come out right. The second reloc
6867 will come out right because we are going to add 4 to
6868 offset_expr when we build its instruction below.
6869
6870 If we have a symbol, then we don't want to include
6871 the offset, because it will wind up being included
6872 when we generate the reloc. */
6873
6874 if (offset_expr.X_op == O_constant)
6875 offset_expr.X_add_number -= 8;
6876 else
6877 {
6878 offset_expr.X_add_number = -4;
6879 offset_expr.X_op = O_constant;
6880 }
6881 }
6882 used_at = 1;
6883 macro_build_lui (&offset_expr, AT);
6884 if (breg != 0)
6885 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6886 /* Itbl support may require additional care here. */
6887 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6888 BFD_RELOC_LO16, AT);
6889 /* FIXME: How do we handle overflow here? */
6890 offset_expr.X_add_number += 4;
6891 /* Itbl support may require additional care here. */
6892 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6893 BFD_RELOC_LO16, AT);
6894 if (mips_relax.sequence)
6895 relax_end ();
6896 }
6897 else if (!mips_big_got)
6898 {
6899 /* If this is a reference to an external symbol, we want
6900 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6901 nop
6902 <op> $treg,0($at)
6903 <op> $treg+1,4($at)
6904 Otherwise we want
6905 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6906 nop
6907 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6908 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6909 If there is a base register we add it to $at before the
6910 lwc1 instructions. If there is a constant we include it
6911 in the lwc1 instructions. */
6912 used_at = 1;
6913 expr1.X_add_number = offset_expr.X_add_number;
6914 if (expr1.X_add_number < -0x8000
6915 || expr1.X_add_number >= 0x8000 - 4)
6916 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6917 load_got_offset (AT, &offset_expr);
6918 load_delay_nop ();
6919 if (breg != 0)
6920 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6921
6922 /* Set mips_optimize to 2 to avoid inserting an undesired
6923 nop. */
6924 hold_mips_optimize = mips_optimize;
6925 mips_optimize = 2;
6926
6927 /* Itbl support may require additional care here. */
6928 relax_start (offset_expr.X_add_symbol);
6929 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6930 BFD_RELOC_LO16, AT);
6931 expr1.X_add_number += 4;
6932 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6933 BFD_RELOC_LO16, AT);
6934 relax_switch ();
6935 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6936 BFD_RELOC_LO16, AT);
6937 offset_expr.X_add_number += 4;
6938 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6939 BFD_RELOC_LO16, AT);
6940 relax_end ();
6941
6942 mips_optimize = hold_mips_optimize;
6943 }
6944 else if (mips_big_got)
6945 {
6946 int gpdelay;
6947
6948 /* If this is a reference to an external symbol, we want
6949 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6950 addu $at,$at,$gp
6951 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6952 nop
6953 <op> $treg,0($at)
6954 <op> $treg+1,4($at)
6955 Otherwise we want
6956 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6957 nop
6958 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6959 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6960 If there is a base register we add it to $at before the
6961 lwc1 instructions. If there is a constant we include it
6962 in the lwc1 instructions. */
6963 used_at = 1;
6964 expr1.X_add_number = offset_expr.X_add_number;
6965 offset_expr.X_add_number = 0;
6966 if (expr1.X_add_number < -0x8000
6967 || expr1.X_add_number >= 0x8000 - 4)
6968 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6969 gpdelay = reg_needs_delay (mips_gp_register);
6970 relax_start (offset_expr.X_add_symbol);
6971 macro_build (&offset_expr, "lui", "t,u",
6972 AT, BFD_RELOC_MIPS_GOT_HI16);
6973 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6974 AT, AT, mips_gp_register);
6975 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6976 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6977 load_delay_nop ();
6978 if (breg != 0)
6979 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6980 /* Itbl support may require additional care here. */
6981 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6982 BFD_RELOC_LO16, AT);
6983 expr1.X_add_number += 4;
6984
6985 /* Set mips_optimize to 2 to avoid inserting an undesired
6986 nop. */
6987 hold_mips_optimize = mips_optimize;
6988 mips_optimize = 2;
6989 /* Itbl support may require additional care here. */
6990 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6991 BFD_RELOC_LO16, AT);
6992 mips_optimize = hold_mips_optimize;
6993 expr1.X_add_number -= 4;
6994
6995 relax_switch ();
6996 offset_expr.X_add_number = expr1.X_add_number;
6997 if (gpdelay)
6998 macro_build (NULL, "nop", "");
6999 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
7000 BFD_RELOC_MIPS_GOT16, mips_gp_register);
7001 load_delay_nop ();
7002 if (breg != 0)
7003 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
7004 /* Itbl support may require additional care here. */
7005 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
7006 BFD_RELOC_LO16, AT);
7007 offset_expr.X_add_number += 4;
7008
7009 /* Set mips_optimize to 2 to avoid inserting an undesired
7010 nop. */
7011 hold_mips_optimize = mips_optimize;
7012 mips_optimize = 2;
7013 /* Itbl support may require additional care here. */
7014 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
7015 BFD_RELOC_LO16, AT);
7016 mips_optimize = hold_mips_optimize;
7017 relax_end ();
7018 }
7019 else
7020 abort ();
7021
7022 break;
7023
7024 case M_LD_OB:
7025 s = "lw";
7026 goto sd_ob;
7027 case M_SD_OB:
7028 s = "sw";
7029 sd_ob:
7030 assert (HAVE_32BIT_ADDRESSES);
7031 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7032 offset_expr.X_add_number += 4;
7033 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
7034 break;
7035
7036 /* New code added to support COPZ instructions.
7037 This code builds table entries out of the macros in mip_opcodes.
7038 R4000 uses interlocks to handle coproc delays.
7039 Other chips (like the R3000) require nops to be inserted for delays.
7040
7041 FIXME: Currently, we require that the user handle delays.
7042 In order to fill delay slots for non-interlocked chips,
7043 we must have a way to specify delays based on the coprocessor.
7044 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7045 What are the side-effects of the cop instruction?
7046 What cache support might we have and what are its effects?
7047 Both coprocessor & memory require delays. how long???
7048 What registers are read/set/modified?
7049
7050 If an itbl is provided to interpret cop instructions,
7051 this knowledge can be encoded in the itbl spec. */
7052
7053 case M_COP0:
7054 s = "c0";
7055 goto copz;
7056 case M_COP1:
7057 s = "c1";
7058 goto copz;
7059 case M_COP2:
7060 s = "c2";
7061 goto copz;
7062 case M_COP3:
7063 s = "c3";
7064 copz:
7065 /* For now we just do C (same as Cz). The parameter will be
7066 stored in insn_opcode by mips_ip. */
7067 macro_build (NULL, s, "C", ip->insn_opcode);
7068 break;
7069
7070 case M_MOVE:
7071 move_register (dreg, sreg);
7072 break;
7073
7074 #ifdef LOSING_COMPILER
7075 default:
7076 /* Try and see if this is a new itbl instruction.
7077 This code builds table entries out of the macros in mip_opcodes.
7078 FIXME: For now we just assemble the expression and pass it's
7079 value along as a 32-bit immediate.
7080 We may want to have the assembler assemble this value,
7081 so that we gain the assembler's knowledge of delay slots,
7082 symbols, etc.
7083 Would it be more efficient to use mask (id) here? */
7084 if (itbl_have_entries
7085 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
7086 {
7087 s = ip->insn_mo->name;
7088 s2 = "cop3";
7089 coproc = ITBL_DECODE_PNUM (immed_expr);;
7090 macro_build (&immed_expr, s, "C");
7091 break;
7092 }
7093 macro2 (ip);
7094 break;
7095 }
7096 if (mips_opts.noat && used_at)
7097 as_bad (_("Macro used $at after \".set noat\""));
7098 }
7099
7100 static void
7101 macro2 (struct mips_cl_insn *ip)
7102 {
7103 int treg, sreg, dreg, breg;
7104 int tempreg;
7105 int mask;
7106 int used_at;
7107 expressionS expr1;
7108 const char *s;
7109 const char *s2;
7110 const char *fmt;
7111 int likely = 0;
7112 int dbl = 0;
7113 int coproc = 0;
7114 int lr = 0;
7115 int imm = 0;
7116 int off;
7117 offsetT maxnum;
7118 bfd_reloc_code_real_type r;
7119
7120 treg = (ip->insn_opcode >> 16) & 0x1f;
7121 dreg = (ip->insn_opcode >> 11) & 0x1f;
7122 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7123 mask = ip->insn_mo->mask;
7124
7125 expr1.X_op = O_constant;
7126 expr1.X_op_symbol = NULL;
7127 expr1.X_add_symbol = NULL;
7128 expr1.X_add_number = 1;
7129
7130 switch (mask)
7131 {
7132 #endif /* LOSING_COMPILER */
7133
7134 case M_DMUL:
7135 dbl = 1;
7136 case M_MUL:
7137 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
7138 macro_build (NULL, "mflo", "d", dreg);
7139 break;
7140
7141 case M_DMUL_I:
7142 dbl = 1;
7143 case M_MUL_I:
7144 /* The MIPS assembler some times generates shifts and adds. I'm
7145 not trying to be that fancy. GCC should do this for us
7146 anyway. */
7147 used_at = 1;
7148 load_register (AT, &imm_expr, dbl);
7149 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
7150 macro_build (NULL, "mflo", "d", dreg);
7151 break;
7152
7153 case M_DMULO_I:
7154 dbl = 1;
7155 case M_MULO_I:
7156 imm = 1;
7157 goto do_mulo;
7158
7159 case M_DMULO:
7160 dbl = 1;
7161 case M_MULO:
7162 do_mulo:
7163 start_noreorder ();
7164 used_at = 1;
7165 if (imm)
7166 load_register (AT, &imm_expr, dbl);
7167 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
7168 macro_build (NULL, "mflo", "d", dreg);
7169 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
7170 macro_build (NULL, "mfhi", "d", AT);
7171 if (mips_trap)
7172 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
7173 else
7174 {
7175 expr1.X_add_number = 8;
7176 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
7177 macro_build (NULL, "nop", "", 0);
7178 macro_build (NULL, "break", "c", 6);
7179 }
7180 end_noreorder ();
7181 macro_build (NULL, "mflo", "d", dreg);
7182 break;
7183
7184 case M_DMULOU_I:
7185 dbl = 1;
7186 case M_MULOU_I:
7187 imm = 1;
7188 goto do_mulou;
7189
7190 case M_DMULOU:
7191 dbl = 1;
7192 case M_MULOU:
7193 do_mulou:
7194 start_noreorder ();
7195 used_at = 1;
7196 if (imm)
7197 load_register (AT, &imm_expr, dbl);
7198 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
7199 sreg, imm ? AT : treg);
7200 macro_build (NULL, "mfhi", "d", AT);
7201 macro_build (NULL, "mflo", "d", dreg);
7202 if (mips_trap)
7203 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
7204 else
7205 {
7206 expr1.X_add_number = 8;
7207 macro_build (&expr1, "beq", "s,t,p", AT, 0);
7208 macro_build (NULL, "nop", "", 0);
7209 macro_build (NULL, "break", "c", 6);
7210 }
7211 end_noreorder ();
7212 break;
7213
7214 case M_DROL:
7215 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7216 {
7217 if (dreg == sreg)
7218 {
7219 tempreg = AT;
7220 used_at = 1;
7221 }
7222 else
7223 {
7224 tempreg = dreg;
7225 }
7226 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
7227 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
7228 break;
7229 }
7230 used_at = 1;
7231 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7232 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7233 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7234 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7235 break;
7236
7237 case M_ROL:
7238 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7239 {
7240 if (dreg == sreg)
7241 {
7242 tempreg = AT;
7243 used_at = 1;
7244 }
7245 else
7246 {
7247 tempreg = dreg;
7248 }
7249 macro_build (NULL, "negu", "d,w", tempreg, treg);
7250 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
7251 break;
7252 }
7253 used_at = 1;
7254 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7255 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
7256 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
7257 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7258 break;
7259
7260 case M_DROL_I:
7261 {
7262 unsigned int rot;
7263 char *l, *r;
7264
7265 if (imm_expr.X_op != O_constant)
7266 as_bad (_("Improper rotate count"));
7267 rot = imm_expr.X_add_number & 0x3f;
7268 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7269 {
7270 rot = (64 - rot) & 0x3f;
7271 if (rot >= 32)
7272 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
7273 else
7274 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
7275 break;
7276 }
7277 if (rot == 0)
7278 {
7279 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7280 break;
7281 }
7282 l = (rot < 0x20) ? "dsll" : "dsll32";
7283 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7284 rot &= 0x1f;
7285 used_at = 1;
7286 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
7287 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7288 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7289 }
7290 break;
7291
7292 case M_ROL_I:
7293 {
7294 unsigned int rot;
7295
7296 if (imm_expr.X_op != O_constant)
7297 as_bad (_("Improper rotate count"));
7298 rot = imm_expr.X_add_number & 0x1f;
7299 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7300 {
7301 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7302 break;
7303 }
7304 if (rot == 0)
7305 {
7306 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
7307 break;
7308 }
7309 used_at = 1;
7310 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
7311 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7312 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7313 }
7314 break;
7315
7316 case M_DROR:
7317 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7318 {
7319 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
7320 break;
7321 }
7322 used_at = 1;
7323 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7324 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
7325 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7326 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7327 break;
7328
7329 case M_ROR:
7330 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7331 {
7332 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
7333 break;
7334 }
7335 used_at = 1;
7336 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7337 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7338 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7339 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7340 break;
7341
7342 case M_DROR_I:
7343 {
7344 unsigned int rot;
7345 char *l, *r;
7346
7347 if (imm_expr.X_op != O_constant)
7348 as_bad (_("Improper rotate count"));
7349 rot = imm_expr.X_add_number & 0x3f;
7350 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7351 {
7352 if (rot >= 32)
7353 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
7354 else
7355 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
7356 break;
7357 }
7358 if (rot == 0)
7359 {
7360 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7361 break;
7362 }
7363 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7364 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7365 rot &= 0x1f;
7366 used_at = 1;
7367 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7368 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7369 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7370 }
7371 break;
7372
7373 case M_ROR_I:
7374 {
7375 unsigned int rot;
7376
7377 if (imm_expr.X_op != O_constant)
7378 as_bad (_("Improper rotate count"));
7379 rot = imm_expr.X_add_number & 0x1f;
7380 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7381 {
7382 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
7383 break;
7384 }
7385 if (rot == 0)
7386 {
7387 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
7388 break;
7389 }
7390 used_at = 1;
7391 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7392 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7393 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7394 }
7395 break;
7396
7397 case M_S_DOB:
7398 if (mips_opts.arch == CPU_R4650)
7399 {
7400 as_bad (_("opcode not supported on this processor"));
7401 break;
7402 }
7403 assert (mips_opts.isa == ISA_MIPS1);
7404 /* Even on a big endian machine $fn comes before $fn+1. We have
7405 to adjust when storing to memory. */
7406 macro_build (&offset_expr, "swc1", "T,o(b)",
7407 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
7408 offset_expr.X_add_number += 4;
7409 macro_build (&offset_expr, "swc1", "T,o(b)",
7410 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
7411 break;
7412
7413 case M_SEQ:
7414 if (sreg == 0)
7415 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
7416 else if (treg == 0)
7417 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7418 else
7419 {
7420 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7421 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7422 }
7423 break;
7424
7425 case M_SEQ_I:
7426 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7427 {
7428 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7429 break;
7430 }
7431 if (sreg == 0)
7432 {
7433 as_warn (_("Instruction %s: result is always false"),
7434 ip->insn_mo->name);
7435 move_register (dreg, 0);
7436 break;
7437 }
7438 if (imm_expr.X_op == O_constant
7439 && imm_expr.X_add_number >= 0
7440 && imm_expr.X_add_number < 0x10000)
7441 {
7442 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7443 }
7444 else if (imm_expr.X_op == O_constant
7445 && imm_expr.X_add_number > -0x8000
7446 && imm_expr.X_add_number < 0)
7447 {
7448 imm_expr.X_add_number = -imm_expr.X_add_number;
7449 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7450 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7451 }
7452 else
7453 {
7454 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7455 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7456 used_at = 1;
7457 }
7458 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7459 break;
7460
7461 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7462 s = "slt";
7463 goto sge;
7464 case M_SGEU:
7465 s = "sltu";
7466 sge:
7467 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7468 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7469 break;
7470
7471 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7472 case M_SGEU_I:
7473 if (imm_expr.X_op == O_constant
7474 && imm_expr.X_add_number >= -0x8000
7475 && imm_expr.X_add_number < 0x8000)
7476 {
7477 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7478 dreg, sreg, BFD_RELOC_LO16);
7479 }
7480 else
7481 {
7482 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7483 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7484 dreg, sreg, AT);
7485 used_at = 1;
7486 }
7487 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7488 break;
7489
7490 case M_SGT: /* sreg > treg <==> treg < sreg */
7491 s = "slt";
7492 goto sgt;
7493 case M_SGTU:
7494 s = "sltu";
7495 sgt:
7496 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7497 break;
7498
7499 case M_SGT_I: /* sreg > I <==> I < sreg */
7500 s = "slt";
7501 goto sgti;
7502 case M_SGTU_I:
7503 s = "sltu";
7504 sgti:
7505 used_at = 1;
7506 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7507 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7508 break;
7509
7510 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7511 s = "slt";
7512 goto sle;
7513 case M_SLEU:
7514 s = "sltu";
7515 sle:
7516 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7517 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7518 break;
7519
7520 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7521 s = "slt";
7522 goto slei;
7523 case M_SLEU_I:
7524 s = "sltu";
7525 slei:
7526 used_at = 1;
7527 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7528 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7529 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7530 break;
7531
7532 case M_SLT_I:
7533 if (imm_expr.X_op == O_constant
7534 && imm_expr.X_add_number >= -0x8000
7535 && imm_expr.X_add_number < 0x8000)
7536 {
7537 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7538 break;
7539 }
7540 used_at = 1;
7541 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7542 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
7543 break;
7544
7545 case M_SLTU_I:
7546 if (imm_expr.X_op == O_constant
7547 && imm_expr.X_add_number >= -0x8000
7548 && imm_expr.X_add_number < 0x8000)
7549 {
7550 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
7551 BFD_RELOC_LO16);
7552 break;
7553 }
7554 used_at = 1;
7555 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7556 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
7557 break;
7558
7559 case M_SNE:
7560 if (sreg == 0)
7561 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
7562 else if (treg == 0)
7563 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7564 else
7565 {
7566 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7567 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7568 }
7569 break;
7570
7571 case M_SNE_I:
7572 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7573 {
7574 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7575 break;
7576 }
7577 if (sreg == 0)
7578 {
7579 as_warn (_("Instruction %s: result is always true"),
7580 ip->insn_mo->name);
7581 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7582 dreg, 0, BFD_RELOC_LO16);
7583 break;
7584 }
7585 if (imm_expr.X_op == O_constant
7586 && imm_expr.X_add_number >= 0
7587 && imm_expr.X_add_number < 0x10000)
7588 {
7589 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7590 }
7591 else if (imm_expr.X_op == O_constant
7592 && imm_expr.X_add_number > -0x8000
7593 && imm_expr.X_add_number < 0)
7594 {
7595 imm_expr.X_add_number = -imm_expr.X_add_number;
7596 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7597 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7598 }
7599 else
7600 {
7601 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7602 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7603 used_at = 1;
7604 }
7605 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7606 break;
7607
7608 case M_DSUB_I:
7609 dbl = 1;
7610 case M_SUB_I:
7611 if (imm_expr.X_op == O_constant
7612 && imm_expr.X_add_number > -0x8000
7613 && imm_expr.X_add_number <= 0x8000)
7614 {
7615 imm_expr.X_add_number = -imm_expr.X_add_number;
7616 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7617 dreg, sreg, BFD_RELOC_LO16);
7618 break;
7619 }
7620 used_at = 1;
7621 load_register (AT, &imm_expr, dbl);
7622 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7623 break;
7624
7625 case M_DSUBU_I:
7626 dbl = 1;
7627 case M_SUBU_I:
7628 if (imm_expr.X_op == O_constant
7629 && imm_expr.X_add_number > -0x8000
7630 && imm_expr.X_add_number <= 0x8000)
7631 {
7632 imm_expr.X_add_number = -imm_expr.X_add_number;
7633 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7634 dreg, sreg, BFD_RELOC_LO16);
7635 break;
7636 }
7637 used_at = 1;
7638 load_register (AT, &imm_expr, dbl);
7639 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7640 break;
7641
7642 case M_TEQ_I:
7643 s = "teq";
7644 goto trap;
7645 case M_TGE_I:
7646 s = "tge";
7647 goto trap;
7648 case M_TGEU_I:
7649 s = "tgeu";
7650 goto trap;
7651 case M_TLT_I:
7652 s = "tlt";
7653 goto trap;
7654 case M_TLTU_I:
7655 s = "tltu";
7656 goto trap;
7657 case M_TNE_I:
7658 s = "tne";
7659 trap:
7660 used_at = 1;
7661 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7662 macro_build (NULL, s, "s,t", sreg, AT);
7663 break;
7664
7665 case M_TRUNCWS:
7666 case M_TRUNCWD:
7667 assert (mips_opts.isa == ISA_MIPS1);
7668 used_at = 1;
7669 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7670 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7671
7672 /*
7673 * Is the double cfc1 instruction a bug in the mips assembler;
7674 * or is there a reason for it?
7675 */
7676 start_noreorder ();
7677 macro_build (NULL, "cfc1", "t,G", treg, RA);
7678 macro_build (NULL, "cfc1", "t,G", treg, RA);
7679 macro_build (NULL, "nop", "");
7680 expr1.X_add_number = 3;
7681 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
7682 expr1.X_add_number = 2;
7683 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7684 macro_build (NULL, "ctc1", "t,G", AT, RA);
7685 macro_build (NULL, "nop", "");
7686 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7687 dreg, sreg);
7688 macro_build (NULL, "ctc1", "t,G", treg, RA);
7689 macro_build (NULL, "nop", "");
7690 end_noreorder ();
7691 break;
7692
7693 case M_ULH:
7694 s = "lb";
7695 goto ulh;
7696 case M_ULHU:
7697 s = "lbu";
7698 ulh:
7699 used_at = 1;
7700 if (offset_expr.X_add_number >= 0x7fff)
7701 as_bad (_("operand overflow"));
7702 if (! target_big_endian)
7703 ++offset_expr.X_add_number;
7704 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
7705 if (! target_big_endian)
7706 --offset_expr.X_add_number;
7707 else
7708 ++offset_expr.X_add_number;
7709 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7710 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7711 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7712 break;
7713
7714 case M_ULD:
7715 s = "ldl";
7716 s2 = "ldr";
7717 off = 7;
7718 goto ulw;
7719 case M_ULW:
7720 s = "lwl";
7721 s2 = "lwr";
7722 off = 3;
7723 ulw:
7724 if (offset_expr.X_add_number >= 0x8000 - off)
7725 as_bad (_("operand overflow"));
7726 if (treg != breg)
7727 tempreg = treg;
7728 else
7729 {
7730 used_at = 1;
7731 tempreg = AT;
7732 }
7733 if (! target_big_endian)
7734 offset_expr.X_add_number += off;
7735 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7736 if (! target_big_endian)
7737 offset_expr.X_add_number -= off;
7738 else
7739 offset_expr.X_add_number += off;
7740 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7741
7742 /* If necessary, move the result in tempreg the final destination. */
7743 if (treg == tempreg)
7744 break;
7745 /* Protect second load's delay slot. */
7746 load_delay_nop ();
7747 move_register (treg, tempreg);
7748 break;
7749
7750 case M_ULD_A:
7751 s = "ldl";
7752 s2 = "ldr";
7753 off = 7;
7754 goto ulwa;
7755 case M_ULW_A:
7756 s = "lwl";
7757 s2 = "lwr";
7758 off = 3;
7759 ulwa:
7760 used_at = 1;
7761 load_address (AT, &offset_expr, &used_at);
7762 if (breg != 0)
7763 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7764 if (! target_big_endian)
7765 expr1.X_add_number = off;
7766 else
7767 expr1.X_add_number = 0;
7768 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7769 if (! target_big_endian)
7770 expr1.X_add_number = 0;
7771 else
7772 expr1.X_add_number = off;
7773 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7774 break;
7775
7776 case M_ULH_A:
7777 case M_ULHU_A:
7778 used_at = 1;
7779 load_address (AT, &offset_expr, &used_at);
7780 if (breg != 0)
7781 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7782 if (target_big_endian)
7783 expr1.X_add_number = 0;
7784 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7785 treg, BFD_RELOC_LO16, AT);
7786 if (target_big_endian)
7787 expr1.X_add_number = 1;
7788 else
7789 expr1.X_add_number = 0;
7790 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7791 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7792 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7793 break;
7794
7795 case M_USH:
7796 used_at = 1;
7797 if (offset_expr.X_add_number >= 0x7fff)
7798 as_bad (_("operand overflow"));
7799 if (target_big_endian)
7800 ++offset_expr.X_add_number;
7801 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7802 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
7803 if (target_big_endian)
7804 --offset_expr.X_add_number;
7805 else
7806 ++offset_expr.X_add_number;
7807 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
7808 break;
7809
7810 case M_USD:
7811 s = "sdl";
7812 s2 = "sdr";
7813 off = 7;
7814 goto usw;
7815 case M_USW:
7816 s = "swl";
7817 s2 = "swr";
7818 off = 3;
7819 usw:
7820 if (offset_expr.X_add_number >= 0x8000 - off)
7821 as_bad (_("operand overflow"));
7822 if (! target_big_endian)
7823 offset_expr.X_add_number += off;
7824 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7825 if (! target_big_endian)
7826 offset_expr.X_add_number -= off;
7827 else
7828 offset_expr.X_add_number += off;
7829 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7830 break;
7831
7832 case M_USD_A:
7833 s = "sdl";
7834 s2 = "sdr";
7835 off = 7;
7836 goto uswa;
7837 case M_USW_A:
7838 s = "swl";
7839 s2 = "swr";
7840 off = 3;
7841 uswa:
7842 used_at = 1;
7843 load_address (AT, &offset_expr, &used_at);
7844 if (breg != 0)
7845 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7846 if (! target_big_endian)
7847 expr1.X_add_number = off;
7848 else
7849 expr1.X_add_number = 0;
7850 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7851 if (! target_big_endian)
7852 expr1.X_add_number = 0;
7853 else
7854 expr1.X_add_number = off;
7855 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7856 break;
7857
7858 case M_USH_A:
7859 used_at = 1;
7860 load_address (AT, &offset_expr, &used_at);
7861 if (breg != 0)
7862 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7863 if (! target_big_endian)
7864 expr1.X_add_number = 0;
7865 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7866 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
7867 if (! target_big_endian)
7868 expr1.X_add_number = 1;
7869 else
7870 expr1.X_add_number = 0;
7871 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7872 if (! target_big_endian)
7873 expr1.X_add_number = 0;
7874 else
7875 expr1.X_add_number = 1;
7876 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7877 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7878 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7879 break;
7880
7881 default:
7882 /* FIXME: Check if this is one of the itbl macros, since they
7883 are added dynamically. */
7884 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7885 break;
7886 }
7887 if (mips_opts.noat && used_at)
7888 as_bad (_("Macro used $at after \".set noat\""));
7889 }
7890
7891 /* Implement macros in mips16 mode. */
7892
7893 static void
7894 mips16_macro (struct mips_cl_insn *ip)
7895 {
7896 int mask;
7897 int xreg, yreg, zreg, tmp;
7898 expressionS expr1;
7899 int dbl;
7900 const char *s, *s2, *s3;
7901
7902 mask = ip->insn_mo->mask;
7903
7904 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
7905 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
7906 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
7907
7908 expr1.X_op = O_constant;
7909 expr1.X_op_symbol = NULL;
7910 expr1.X_add_symbol = NULL;
7911 expr1.X_add_number = 1;
7912
7913 dbl = 0;
7914
7915 switch (mask)
7916 {
7917 default:
7918 internalError ();
7919
7920 case M_DDIV_3:
7921 dbl = 1;
7922 case M_DIV_3:
7923 s = "mflo";
7924 goto do_div3;
7925 case M_DREM_3:
7926 dbl = 1;
7927 case M_REM_3:
7928 s = "mfhi";
7929 do_div3:
7930 start_noreorder ();
7931 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
7932 expr1.X_add_number = 2;
7933 macro_build (&expr1, "bnez", "x,p", yreg);
7934 macro_build (NULL, "break", "6", 7);
7935
7936 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7937 since that causes an overflow. We should do that as well,
7938 but I don't see how to do the comparisons without a temporary
7939 register. */
7940 end_noreorder ();
7941 macro_build (NULL, s, "x", zreg);
7942 break;
7943
7944 case M_DIVU_3:
7945 s = "divu";
7946 s2 = "mflo";
7947 goto do_divu3;
7948 case M_REMU_3:
7949 s = "divu";
7950 s2 = "mfhi";
7951 goto do_divu3;
7952 case M_DDIVU_3:
7953 s = "ddivu";
7954 s2 = "mflo";
7955 goto do_divu3;
7956 case M_DREMU_3:
7957 s = "ddivu";
7958 s2 = "mfhi";
7959 do_divu3:
7960 start_noreorder ();
7961 macro_build (NULL, s, "0,x,y", xreg, yreg);
7962 expr1.X_add_number = 2;
7963 macro_build (&expr1, "bnez", "x,p", yreg);
7964 macro_build (NULL, "break", "6", 7);
7965 end_noreorder ();
7966 macro_build (NULL, s2, "x", zreg);
7967 break;
7968
7969 case M_DMUL:
7970 dbl = 1;
7971 case M_MUL:
7972 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7973 macro_build (NULL, "mflo", "x", zreg);
7974 break;
7975
7976 case M_DSUBU_I:
7977 dbl = 1;
7978 goto do_subu;
7979 case M_SUBU_I:
7980 do_subu:
7981 if (imm_expr.X_op != O_constant)
7982 as_bad (_("Unsupported large constant"));
7983 imm_expr.X_add_number = -imm_expr.X_add_number;
7984 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7985 break;
7986
7987 case M_SUBU_I_2:
7988 if (imm_expr.X_op != O_constant)
7989 as_bad (_("Unsupported large constant"));
7990 imm_expr.X_add_number = -imm_expr.X_add_number;
7991 macro_build (&imm_expr, "addiu", "x,k", xreg);
7992 break;
7993
7994 case M_DSUBU_I_2:
7995 if (imm_expr.X_op != O_constant)
7996 as_bad (_("Unsupported large constant"));
7997 imm_expr.X_add_number = -imm_expr.X_add_number;
7998 macro_build (&imm_expr, "daddiu", "y,j", yreg);
7999 break;
8000
8001 case M_BEQ:
8002 s = "cmp";
8003 s2 = "bteqz";
8004 goto do_branch;
8005 case M_BNE:
8006 s = "cmp";
8007 s2 = "btnez";
8008 goto do_branch;
8009 case M_BLT:
8010 s = "slt";
8011 s2 = "btnez";
8012 goto do_branch;
8013 case M_BLTU:
8014 s = "sltu";
8015 s2 = "btnez";
8016 goto do_branch;
8017 case M_BLE:
8018 s = "slt";
8019 s2 = "bteqz";
8020 goto do_reverse_branch;
8021 case M_BLEU:
8022 s = "sltu";
8023 s2 = "bteqz";
8024 goto do_reverse_branch;
8025 case M_BGE:
8026 s = "slt";
8027 s2 = "bteqz";
8028 goto do_branch;
8029 case M_BGEU:
8030 s = "sltu";
8031 s2 = "bteqz";
8032 goto do_branch;
8033 case M_BGT:
8034 s = "slt";
8035 s2 = "btnez";
8036 goto do_reverse_branch;
8037 case M_BGTU:
8038 s = "sltu";
8039 s2 = "btnez";
8040
8041 do_reverse_branch:
8042 tmp = xreg;
8043 xreg = yreg;
8044 yreg = tmp;
8045
8046 do_branch:
8047 macro_build (NULL, s, "x,y", xreg, yreg);
8048 macro_build (&offset_expr, s2, "p");
8049 break;
8050
8051 case M_BEQ_I:
8052 s = "cmpi";
8053 s2 = "bteqz";
8054 s3 = "x,U";
8055 goto do_branch_i;
8056 case M_BNE_I:
8057 s = "cmpi";
8058 s2 = "btnez";
8059 s3 = "x,U";
8060 goto do_branch_i;
8061 case M_BLT_I:
8062 s = "slti";
8063 s2 = "btnez";
8064 s3 = "x,8";
8065 goto do_branch_i;
8066 case M_BLTU_I:
8067 s = "sltiu";
8068 s2 = "btnez";
8069 s3 = "x,8";
8070 goto do_branch_i;
8071 case M_BLE_I:
8072 s = "slti";
8073 s2 = "btnez";
8074 s3 = "x,8";
8075 goto do_addone_branch_i;
8076 case M_BLEU_I:
8077 s = "sltiu";
8078 s2 = "btnez";
8079 s3 = "x,8";
8080 goto do_addone_branch_i;
8081 case M_BGE_I:
8082 s = "slti";
8083 s2 = "bteqz";
8084 s3 = "x,8";
8085 goto do_branch_i;
8086 case M_BGEU_I:
8087 s = "sltiu";
8088 s2 = "bteqz";
8089 s3 = "x,8";
8090 goto do_branch_i;
8091 case M_BGT_I:
8092 s = "slti";
8093 s2 = "bteqz";
8094 s3 = "x,8";
8095 goto do_addone_branch_i;
8096 case M_BGTU_I:
8097 s = "sltiu";
8098 s2 = "bteqz";
8099 s3 = "x,8";
8100
8101 do_addone_branch_i:
8102 if (imm_expr.X_op != O_constant)
8103 as_bad (_("Unsupported large constant"));
8104 ++imm_expr.X_add_number;
8105
8106 do_branch_i:
8107 macro_build (&imm_expr, s, s3, xreg);
8108 macro_build (&offset_expr, s2, "p");
8109 break;
8110
8111 case M_ABS:
8112 expr1.X_add_number = 0;
8113 macro_build (&expr1, "slti", "x,8", yreg);
8114 if (xreg != yreg)
8115 move_register (xreg, yreg);
8116 expr1.X_add_number = 2;
8117 macro_build (&expr1, "bteqz", "p");
8118 macro_build (NULL, "neg", "x,w", xreg, xreg);
8119 }
8120 }
8121
8122 /* For consistency checking, verify that all bits are specified either
8123 by the match/mask part of the instruction definition, or by the
8124 operand list. */
8125 static int
8126 validate_mips_insn (const struct mips_opcode *opc)
8127 {
8128 const char *p = opc->args;
8129 char c;
8130 unsigned long used_bits = opc->mask;
8131
8132 if ((used_bits & opc->match) != opc->match)
8133 {
8134 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8135 opc->name, opc->args);
8136 return 0;
8137 }
8138 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
8139 while (*p)
8140 switch (c = *p++)
8141 {
8142 case ',': break;
8143 case '(': break;
8144 case ')': break;
8145 case '+':
8146 switch (c = *p++)
8147 {
8148 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
8149 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
8150 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
8151 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
8152 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8153 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8154 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8155 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
8156 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
8157 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8158 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8159 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8160 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8161 case 'I': break;
8162 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8163 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
8164 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
8165 default:
8166 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8167 c, opc->name, opc->args);
8168 return 0;
8169 }
8170 break;
8171 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8172 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8173 case 'A': break;
8174 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
8175 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8176 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8177 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8178 case 'F': break;
8179 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8180 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
8181 case 'I': break;
8182 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
8183 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8184 case 'L': break;
8185 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8186 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
8187 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8188 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8189 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8190 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8191 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8192 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8193 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8194 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8195 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8196 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8197 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8198 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8199 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8200 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8201 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8202 case 'f': break;
8203 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8204 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8205 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8206 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8207 case 'l': break;
8208 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8209 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8210 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8211 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8212 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8213 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8214 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8215 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8216 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8217 case 'x': break;
8218 case 'z': break;
8219 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
8220 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8221 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8222 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8223 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8224 case '[': break;
8225 case ']': break;
8226 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
8227 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
8228 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
8229 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8230 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
8231 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
8232 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
8233 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
8234 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
8235 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
8236 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
8237 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
8238 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
8239 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
8240 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
8241 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8242 default:
8243 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8244 c, opc->name, opc->args);
8245 return 0;
8246 }
8247 #undef USE_BITS
8248 if (used_bits != 0xffffffff)
8249 {
8250 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8251 ~used_bits & 0xffffffff, opc->name, opc->args);
8252 return 0;
8253 }
8254 return 1;
8255 }
8256
8257 /* UDI immediates. */
8258 struct mips_immed {
8259 char type;
8260 unsigned int shift;
8261 unsigned long mask;
8262 const char * desc;
8263 };
8264
8265 static const struct mips_immed mips_immed[] = {
8266 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
8267 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
8268 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
8269 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
8270 { 0,0,0,0 }
8271 };
8272
8273 /* Check whether an odd floating-point register is allowed. */
8274 static int
8275 mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
8276 {
8277 const char *s = insn->name;
8278
8279 if (insn->pinfo == INSN_MACRO)
8280 /* Let a macro pass, we'll catch it later when it is expanded. */
8281 return 1;
8282
8283 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa))
8284 {
8285 /* Allow odd registers for single-precision ops. */
8286 switch (insn->pinfo & (FP_S | FP_D))
8287 {
8288 case FP_S:
8289 case 0:
8290 return 1; /* both single precision - ok */
8291 case FP_D:
8292 return 0; /* both double precision - fail */
8293 default:
8294 break;
8295 }
8296
8297 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
8298 s = strchr (insn->name, '.');
8299 if (argnum == 2)
8300 s = s != NULL ? strchr (s + 1, '.') : NULL;
8301 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
8302 }
8303
8304 /* Single-precision coprocessor loads and moves are OK too. */
8305 if ((insn->pinfo & FP_S)
8306 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
8307 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
8308 return 1;
8309
8310 return 0;
8311 }
8312
8313 /* This routine assembles an instruction into its binary format. As a
8314 side effect, it sets one of the global variables imm_reloc or
8315 offset_reloc to the type of relocation to do if one of the operands
8316 is an address expression. */
8317
8318 static void
8319 mips_ip (char *str, struct mips_cl_insn *ip)
8320 {
8321 char *s;
8322 const char *args;
8323 char c = 0;
8324 struct mips_opcode *insn;
8325 char *argsStart;
8326 unsigned int regno;
8327 unsigned int lastregno = 0;
8328 unsigned int lastpos = 0;
8329 unsigned int limlo, limhi;
8330 char *s_reset;
8331 char save_c = 0;
8332 offsetT min_range, max_range;
8333 int argnum;
8334 unsigned int rtype;
8335
8336 insn_error = NULL;
8337
8338 /* If the instruction contains a '.', we first try to match an instruction
8339 including the '.'. Then we try again without the '.'. */
8340 insn = NULL;
8341 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8342 continue;
8343
8344 /* If we stopped on whitespace, then replace the whitespace with null for
8345 the call to hash_find. Save the character we replaced just in case we
8346 have to re-parse the instruction. */
8347 if (ISSPACE (*s))
8348 {
8349 save_c = *s;
8350 *s++ = '\0';
8351 }
8352
8353 insn = (struct mips_opcode *) hash_find (op_hash, str);
8354
8355 /* If we didn't find the instruction in the opcode table, try again, but
8356 this time with just the instruction up to, but not including the
8357 first '.'. */
8358 if (insn == NULL)
8359 {
8360 /* Restore the character we overwrite above (if any). */
8361 if (save_c)
8362 *(--s) = save_c;
8363
8364 /* Scan up to the first '.' or whitespace. */
8365 for (s = str;
8366 *s != '\0' && *s != '.' && !ISSPACE (*s);
8367 ++s)
8368 continue;
8369
8370 /* If we did not find a '.', then we can quit now. */
8371 if (*s != '.')
8372 {
8373 insn_error = "unrecognized opcode";
8374 return;
8375 }
8376
8377 /* Lookup the instruction in the hash table. */
8378 *s++ = '\0';
8379 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8380 {
8381 insn_error = "unrecognized opcode";
8382 return;
8383 }
8384 }
8385
8386 argsStart = s;
8387 for (;;)
8388 {
8389 bfd_boolean ok;
8390
8391 assert (strcmp (insn->name, str) == 0);
8392
8393 if (OPCODE_IS_MEMBER (insn,
8394 (mips_opts.isa
8395 /* We don't check for mips_opts.mips16 here since
8396 we want to allow jalx if -mips16 was specified
8397 on the command line. */
8398 | (file_ase_mips16 ? INSN_MIPS16 : 0)
8399 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8400 | (mips_opts.ase_dsp ? INSN_DSP : 0)
8401 | ((mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
8402 ? INSN_DSP64 : 0)
8403 | (mips_opts.ase_mt ? INSN_MT : 0)
8404 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)
8405 | (mips_opts.ase_smartmips ? INSN_SMARTMIPS : 0)),
8406 mips_opts.arch))
8407 ok = TRUE;
8408 else
8409 ok = FALSE;
8410
8411 if (insn->pinfo != INSN_MACRO)
8412 {
8413 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8414 ok = FALSE;
8415 }
8416
8417 if (! ok)
8418 {
8419 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8420 && strcmp (insn->name, insn[1].name) == 0)
8421 {
8422 ++insn;
8423 continue;
8424 }
8425 else
8426 {
8427 if (!insn_error)
8428 {
8429 static char buf[100];
8430 sprintf (buf,
8431 _("opcode not supported on this processor: %s (%s)"),
8432 mips_cpu_info_from_arch (mips_opts.arch)->name,
8433 mips_cpu_info_from_isa (mips_opts.isa)->name);
8434 insn_error = buf;
8435 }
8436 if (save_c)
8437 *(--s) = save_c;
8438 return;
8439 }
8440 }
8441
8442 create_insn (ip, insn);
8443 insn_error = NULL;
8444 argnum = 1;
8445 for (args = insn->args;; ++args)
8446 {
8447 int is_mdmx;
8448
8449 s += strspn (s, " \t");
8450 is_mdmx = 0;
8451 switch (*args)
8452 {
8453 case '\0': /* end of args */
8454 if (*s == '\0')
8455 return;
8456 break;
8457
8458 case '3': /* dsp 3-bit unsigned immediate in bit 21 */
8459 my_getExpression (&imm_expr, s);
8460 check_absolute_expr (ip, &imm_expr);
8461 if (imm_expr.X_add_number & ~OP_MASK_SA3)
8462 {
8463 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8464 OP_MASK_SA3, (unsigned long) imm_expr.X_add_number);
8465 }
8466 INSERT_OPERAND (SA3, *ip, imm_expr.X_add_number);
8467 imm_expr.X_op = O_absent;
8468 s = expr_end;
8469 continue;
8470
8471 case '4': /* dsp 4-bit unsigned immediate in bit 21 */
8472 my_getExpression (&imm_expr, s);
8473 check_absolute_expr (ip, &imm_expr);
8474 if (imm_expr.X_add_number & ~OP_MASK_SA4)
8475 {
8476 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8477 OP_MASK_SA4, (unsigned long) imm_expr.X_add_number);
8478 }
8479 INSERT_OPERAND (SA4, *ip, imm_expr.X_add_number);
8480 imm_expr.X_op = O_absent;
8481 s = expr_end;
8482 continue;
8483
8484 case '5': /* dsp 8-bit unsigned immediate in bit 16 */
8485 my_getExpression (&imm_expr, s);
8486 check_absolute_expr (ip, &imm_expr);
8487 if (imm_expr.X_add_number & ~OP_MASK_IMM8)
8488 {
8489 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8490 OP_MASK_IMM8, (unsigned long) imm_expr.X_add_number);
8491 }
8492 INSERT_OPERAND (IMM8, *ip, imm_expr.X_add_number);
8493 imm_expr.X_op = O_absent;
8494 s = expr_end;
8495 continue;
8496
8497 case '6': /* dsp 5-bit unsigned immediate in bit 21 */
8498 my_getExpression (&imm_expr, s);
8499 check_absolute_expr (ip, &imm_expr);
8500 if (imm_expr.X_add_number & ~OP_MASK_RS)
8501 {
8502 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8503 OP_MASK_RS, (unsigned long) imm_expr.X_add_number);
8504 }
8505 INSERT_OPERAND (RS, *ip, imm_expr.X_add_number);
8506 imm_expr.X_op = O_absent;
8507 s = expr_end;
8508 continue;
8509
8510 case '7': /* four dsp accumulators in bits 11,12 */
8511 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8512 s[3] >= '0' && s[3] <= '3')
8513 {
8514 regno = s[3] - '0';
8515 s += 4;
8516 INSERT_OPERAND (DSPACC, *ip, regno);
8517 continue;
8518 }
8519 else
8520 as_bad (_("Invalid dsp acc register"));
8521 break;
8522
8523 case '8': /* dsp 6-bit unsigned immediate in bit 11 */
8524 my_getExpression (&imm_expr, s);
8525 check_absolute_expr (ip, &imm_expr);
8526 if (imm_expr.X_add_number & ~OP_MASK_WRDSP)
8527 {
8528 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8529 OP_MASK_WRDSP,
8530 (unsigned long) imm_expr.X_add_number);
8531 }
8532 INSERT_OPERAND (WRDSP, *ip, imm_expr.X_add_number);
8533 imm_expr.X_op = O_absent;
8534 s = expr_end;
8535 continue;
8536
8537 case '9': /* four dsp accumulators in bits 21,22 */
8538 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8539 s[3] >= '0' && s[3] <= '3')
8540 {
8541 regno = s[3] - '0';
8542 s += 4;
8543 INSERT_OPERAND (DSPACC_S, *ip, regno);
8544 continue;
8545 }
8546 else
8547 as_bad (_("Invalid dsp acc register"));
8548 break;
8549
8550 case '0': /* dsp 6-bit signed immediate in bit 20 */
8551 my_getExpression (&imm_expr, s);
8552 check_absolute_expr (ip, &imm_expr);
8553 min_range = -((OP_MASK_DSPSFT + 1) >> 1);
8554 max_range = ((OP_MASK_DSPSFT + 1) >> 1) - 1;
8555 if (imm_expr.X_add_number < min_range ||
8556 imm_expr.X_add_number > max_range)
8557 {
8558 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8559 (long) min_range, (long) max_range,
8560 (long) imm_expr.X_add_number);
8561 }
8562 INSERT_OPERAND (DSPSFT, *ip, imm_expr.X_add_number);
8563 imm_expr.X_op = O_absent;
8564 s = expr_end;
8565 continue;
8566
8567 case '\'': /* dsp 6-bit unsigned immediate in bit 16 */
8568 my_getExpression (&imm_expr, s);
8569 check_absolute_expr (ip, &imm_expr);
8570 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
8571 {
8572 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8573 OP_MASK_RDDSP,
8574 (unsigned long) imm_expr.X_add_number);
8575 }
8576 INSERT_OPERAND (RDDSP, *ip, imm_expr.X_add_number);
8577 imm_expr.X_op = O_absent;
8578 s = expr_end;
8579 continue;
8580
8581 case ':': /* dsp 7-bit signed immediate in bit 19 */
8582 my_getExpression (&imm_expr, s);
8583 check_absolute_expr (ip, &imm_expr);
8584 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
8585 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
8586 if (imm_expr.X_add_number < min_range ||
8587 imm_expr.X_add_number > max_range)
8588 {
8589 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8590 (long) min_range, (long) max_range,
8591 (long) imm_expr.X_add_number);
8592 }
8593 INSERT_OPERAND (DSPSFT_7, *ip, imm_expr.X_add_number);
8594 imm_expr.X_op = O_absent;
8595 s = expr_end;
8596 continue;
8597
8598 case '@': /* dsp 10-bit signed immediate in bit 16 */
8599 my_getExpression (&imm_expr, s);
8600 check_absolute_expr (ip, &imm_expr);
8601 min_range = -((OP_MASK_IMM10 + 1) >> 1);
8602 max_range = ((OP_MASK_IMM10 + 1) >> 1) - 1;
8603 if (imm_expr.X_add_number < min_range ||
8604 imm_expr.X_add_number > max_range)
8605 {
8606 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8607 (long) min_range, (long) max_range,
8608 (long) imm_expr.X_add_number);
8609 }
8610 INSERT_OPERAND (IMM10, *ip, imm_expr.X_add_number);
8611 imm_expr.X_op = O_absent;
8612 s = expr_end;
8613 continue;
8614
8615 case '!': /* MT usermode flag bit. */
8616 my_getExpression (&imm_expr, s);
8617 check_absolute_expr (ip, &imm_expr);
8618 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
8619 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
8620 (unsigned long) imm_expr.X_add_number);
8621 INSERT_OPERAND (MT_U, *ip, imm_expr.X_add_number);
8622 imm_expr.X_op = O_absent;
8623 s = expr_end;
8624 continue;
8625
8626 case '$': /* MT load high flag bit. */
8627 my_getExpression (&imm_expr, s);
8628 check_absolute_expr (ip, &imm_expr);
8629 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
8630 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
8631 (unsigned long) imm_expr.X_add_number);
8632 INSERT_OPERAND (MT_H, *ip, imm_expr.X_add_number);
8633 imm_expr.X_op = O_absent;
8634 s = expr_end;
8635 continue;
8636
8637 case '*': /* four dsp accumulators in bits 18,19 */
8638 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8639 s[3] >= '0' && s[3] <= '3')
8640 {
8641 regno = s[3] - '0';
8642 s += 4;
8643 INSERT_OPERAND (MTACC_T, *ip, regno);
8644 continue;
8645 }
8646 else
8647 as_bad (_("Invalid dsp/smartmips acc register"));
8648 break;
8649
8650 case '&': /* four dsp accumulators in bits 13,14 */
8651 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8652 s[3] >= '0' && s[3] <= '3')
8653 {
8654 regno = s[3] - '0';
8655 s += 4;
8656 INSERT_OPERAND (MTACC_D, *ip, regno);
8657 continue;
8658 }
8659 else
8660 as_bad (_("Invalid dsp/smartmips acc register"));
8661 break;
8662
8663 case ',':
8664 ++argnum;
8665 if (*s++ == *args)
8666 continue;
8667 s--;
8668 switch (*++args)
8669 {
8670 case 'r':
8671 case 'v':
8672 INSERT_OPERAND (RS, *ip, lastregno);
8673 continue;
8674
8675 case 'w':
8676 INSERT_OPERAND (RT, *ip, lastregno);
8677 continue;
8678
8679 case 'W':
8680 INSERT_OPERAND (FT, *ip, lastregno);
8681 continue;
8682
8683 case 'V':
8684 INSERT_OPERAND (FS, *ip, lastregno);
8685 continue;
8686 }
8687 break;
8688
8689 case '(':
8690 /* Handle optional base register.
8691 Either the base register is omitted or
8692 we must have a left paren. */
8693 /* This is dependent on the next operand specifier
8694 is a base register specification. */
8695 assert (args[1] == 'b' || args[1] == '5'
8696 || args[1] == '-' || args[1] == '4');
8697 if (*s == '\0')
8698 return;
8699
8700 case ')': /* these must match exactly */
8701 case '[':
8702 case ']':
8703 if (*s++ == *args)
8704 continue;
8705 break;
8706
8707 case '+': /* Opcode extension character. */
8708 switch (*++args)
8709 {
8710 case '1': /* UDI immediates. */
8711 case '2':
8712 case '3':
8713 case '4':
8714 {
8715 const struct mips_immed *imm = mips_immed;
8716
8717 while (imm->type && imm->type != *args)
8718 ++imm;
8719 if (! imm->type)
8720 internalError ();
8721 my_getExpression (&imm_expr, s);
8722 check_absolute_expr (ip, &imm_expr);
8723 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
8724 {
8725 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
8726 imm->desc ? imm->desc : ip->insn_mo->name,
8727 (unsigned long) imm_expr.X_add_number,
8728 (unsigned long) imm_expr.X_add_number);
8729 imm_expr.X_add_number &= imm->mask;
8730 }
8731 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8732 << imm->shift);
8733 imm_expr.X_op = O_absent;
8734 s = expr_end;
8735 }
8736 continue;
8737
8738 case 'A': /* ins/ext position, becomes LSB. */
8739 limlo = 0;
8740 limhi = 31;
8741 goto do_lsb;
8742 case 'E':
8743 limlo = 32;
8744 limhi = 63;
8745 goto do_lsb;
8746 do_lsb:
8747 my_getExpression (&imm_expr, s);
8748 check_absolute_expr (ip, &imm_expr);
8749 if ((unsigned long) imm_expr.X_add_number < limlo
8750 || (unsigned long) imm_expr.X_add_number > limhi)
8751 {
8752 as_bad (_("Improper position (%lu)"),
8753 (unsigned long) imm_expr.X_add_number);
8754 imm_expr.X_add_number = limlo;
8755 }
8756 lastpos = imm_expr.X_add_number;
8757 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8758 imm_expr.X_op = O_absent;
8759 s = expr_end;
8760 continue;
8761
8762 case 'B': /* ins size, becomes MSB. */
8763 limlo = 1;
8764 limhi = 32;
8765 goto do_msb;
8766 case 'F':
8767 limlo = 33;
8768 limhi = 64;
8769 goto do_msb;
8770 do_msb:
8771 my_getExpression (&imm_expr, s);
8772 check_absolute_expr (ip, &imm_expr);
8773 /* Check for negative input so that small negative numbers
8774 will not succeed incorrectly. The checks against
8775 (pos+size) transitively check "size" itself,
8776 assuming that "pos" is reasonable. */
8777 if ((long) imm_expr.X_add_number < 0
8778 || ((unsigned long) imm_expr.X_add_number
8779 + lastpos) < limlo
8780 || ((unsigned long) imm_expr.X_add_number
8781 + lastpos) > limhi)
8782 {
8783 as_bad (_("Improper insert size (%lu, position %lu)"),
8784 (unsigned long) imm_expr.X_add_number,
8785 (unsigned long) lastpos);
8786 imm_expr.X_add_number = limlo - lastpos;
8787 }
8788 INSERT_OPERAND (INSMSB, *ip,
8789 lastpos + imm_expr.X_add_number - 1);
8790 imm_expr.X_op = O_absent;
8791 s = expr_end;
8792 continue;
8793
8794 case 'C': /* ext size, becomes MSBD. */
8795 limlo = 1;
8796 limhi = 32;
8797 goto do_msbd;
8798 case 'G':
8799 limlo = 33;
8800 limhi = 64;
8801 goto do_msbd;
8802 case 'H':
8803 limlo = 33;
8804 limhi = 64;
8805 goto do_msbd;
8806 do_msbd:
8807 my_getExpression (&imm_expr, s);
8808 check_absolute_expr (ip, &imm_expr);
8809 /* Check for negative input so that small negative numbers
8810 will not succeed incorrectly. The checks against
8811 (pos+size) transitively check "size" itself,
8812 assuming that "pos" is reasonable. */
8813 if ((long) imm_expr.X_add_number < 0
8814 || ((unsigned long) imm_expr.X_add_number
8815 + lastpos) < limlo
8816 || ((unsigned long) imm_expr.X_add_number
8817 + lastpos) > limhi)
8818 {
8819 as_bad (_("Improper extract size (%lu, position %lu)"),
8820 (unsigned long) imm_expr.X_add_number,
8821 (unsigned long) lastpos);
8822 imm_expr.X_add_number = limlo - lastpos;
8823 }
8824 INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
8825 imm_expr.X_op = O_absent;
8826 s = expr_end;
8827 continue;
8828
8829 case 'D':
8830 /* +D is for disassembly only; never match. */
8831 break;
8832
8833 case 'I':
8834 /* "+I" is like "I", except that imm2_expr is used. */
8835 my_getExpression (&imm2_expr, s);
8836 if (imm2_expr.X_op != O_big
8837 && imm2_expr.X_op != O_constant)
8838 insn_error = _("absolute expression required");
8839 if (HAVE_32BIT_GPRS)
8840 normalize_constant_expr (&imm2_expr);
8841 s = expr_end;
8842 continue;
8843
8844 case 'T': /* Coprocessor register. */
8845 /* +T is for disassembly only; never match. */
8846 break;
8847
8848 case 't': /* Coprocessor register number. */
8849 if (s[0] == '$' && ISDIGIT (s[1]))
8850 {
8851 ++s;
8852 regno = 0;
8853 do
8854 {
8855 regno *= 10;
8856 regno += *s - '0';
8857 ++s;
8858 }
8859 while (ISDIGIT (*s));
8860 if (regno > 31)
8861 as_bad (_("Invalid register number (%d)"), regno);
8862 else
8863 {
8864 INSERT_OPERAND (RT, *ip, regno);
8865 continue;
8866 }
8867 }
8868 else
8869 as_bad (_("Invalid coprocessor 0 register number"));
8870 break;
8871
8872 default:
8873 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8874 *args, insn->name, insn->args);
8875 /* Further processing is fruitless. */
8876 return;
8877 }
8878 break;
8879
8880 case '<': /* must be at least one digit */
8881 /*
8882 * According to the manual, if the shift amount is greater
8883 * than 31 or less than 0, then the shift amount should be
8884 * mod 32. In reality the mips assembler issues an error.
8885 * We issue a warning and mask out all but the low 5 bits.
8886 */
8887 my_getExpression (&imm_expr, s);
8888 check_absolute_expr (ip, &imm_expr);
8889 if ((unsigned long) imm_expr.X_add_number > 31)
8890 as_warn (_("Improper shift amount (%lu)"),
8891 (unsigned long) imm_expr.X_add_number);
8892 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8893 imm_expr.X_op = O_absent;
8894 s = expr_end;
8895 continue;
8896
8897 case '>': /* shift amount minus 32 */
8898 my_getExpression (&imm_expr, s);
8899 check_absolute_expr (ip, &imm_expr);
8900 if ((unsigned long) imm_expr.X_add_number < 32
8901 || (unsigned long) imm_expr.X_add_number > 63)
8902 break;
8903 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
8904 imm_expr.X_op = O_absent;
8905 s = expr_end;
8906 continue;
8907
8908 case 'k': /* cache code */
8909 case 'h': /* prefx code */
8910 my_getExpression (&imm_expr, s);
8911 check_absolute_expr (ip, &imm_expr);
8912 if ((unsigned long) imm_expr.X_add_number > 31)
8913 as_warn (_("Invalid value for `%s' (%lu)"),
8914 ip->insn_mo->name,
8915 (unsigned long) imm_expr.X_add_number);
8916 if (*args == 'k')
8917 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
8918 else
8919 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
8920 imm_expr.X_op = O_absent;
8921 s = expr_end;
8922 continue;
8923
8924 case 'c': /* break code */
8925 my_getExpression (&imm_expr, s);
8926 check_absolute_expr (ip, &imm_expr);
8927 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE)
8928 as_warn (_("Code for %s not in range 0..1023 (%lu)"),
8929 ip->insn_mo->name,
8930 (unsigned long) imm_expr.X_add_number);
8931 INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
8932 imm_expr.X_op = O_absent;
8933 s = expr_end;
8934 continue;
8935
8936 case 'q': /* lower break code */
8937 my_getExpression (&imm_expr, s);
8938 check_absolute_expr (ip, &imm_expr);
8939 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE2)
8940 as_warn (_("Lower code for %s not in range 0..1023 (%lu)"),
8941 ip->insn_mo->name,
8942 (unsigned long) imm_expr.X_add_number);
8943 INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
8944 imm_expr.X_op = O_absent;
8945 s = expr_end;
8946 continue;
8947
8948 case 'B': /* 20-bit syscall/break code. */
8949 my_getExpression (&imm_expr, s);
8950 check_absolute_expr (ip, &imm_expr);
8951 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8952 as_warn (_("Code for %s not in range 0..1048575 (%lu)"),
8953 ip->insn_mo->name,
8954 (unsigned long) imm_expr.X_add_number);
8955 INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
8956 imm_expr.X_op = O_absent;
8957 s = expr_end;
8958 continue;
8959
8960 case 'C': /* Coprocessor code */
8961 my_getExpression (&imm_expr, s);
8962 check_absolute_expr (ip, &imm_expr);
8963 if ((unsigned long) imm_expr.X_add_number > OP_MASK_COPZ)
8964 {
8965 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8966 (unsigned long) imm_expr.X_add_number);
8967 imm_expr.X_add_number &= OP_MASK_COPZ;
8968 }
8969 INSERT_OPERAND (COPZ, *ip, imm_expr.X_add_number);
8970 imm_expr.X_op = O_absent;
8971 s = expr_end;
8972 continue;
8973
8974 case 'J': /* 19-bit wait code. */
8975 my_getExpression (&imm_expr, s);
8976 check_absolute_expr (ip, &imm_expr);
8977 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8978 {
8979 as_warn (_("Illegal 19-bit code (%lu)"),
8980 (unsigned long) imm_expr.X_add_number);
8981 imm_expr.X_add_number &= OP_MASK_CODE19;
8982 }
8983 INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
8984 imm_expr.X_op = O_absent;
8985 s = expr_end;
8986 continue;
8987
8988 case 'P': /* Performance register. */
8989 my_getExpression (&imm_expr, s);
8990 check_absolute_expr (ip, &imm_expr);
8991 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8992 as_warn (_("Invalid performance register (%lu)"),
8993 (unsigned long) imm_expr.X_add_number);
8994 INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
8995 imm_expr.X_op = O_absent;
8996 s = expr_end;
8997 continue;
8998
8999 case 'G': /* Coprocessor destination register. */
9000 if (((ip->insn_opcode >> OP_SH_OP) & OP_MASK_OP) == OP_OP_COP0)
9001 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_CP0, &regno);
9002 else
9003 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
9004 INSERT_OPERAND (RD, *ip, regno);
9005 if (ok)
9006 {
9007 lastregno = regno;
9008 continue;
9009 }
9010 else
9011 break;
9012
9013 case 'b': /* base register */
9014 case 'd': /* destination register */
9015 case 's': /* source register */
9016 case 't': /* target register */
9017 case 'r': /* both target and source */
9018 case 'v': /* both dest and source */
9019 case 'w': /* both dest and target */
9020 case 'E': /* coprocessor target register */
9021 case 'K': /* 'rdhwr' destination register */
9022 case 'x': /* ignore register name */
9023 case 'z': /* must be zero register */
9024 case 'U': /* destination register (clo/clz). */
9025 case 'g': /* coprocessor destination register */
9026 s_reset = s;
9027 if (*args == 'E' || *args == 'K')
9028 ok = reg_lookup (&s, RTYPE_NUM, &regno);
9029 else
9030 {
9031 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
9032 if (regno == AT && ! mips_opts.noat)
9033 as_warn ("Used $at without \".set noat\"");
9034 }
9035 if (ok)
9036 {
9037 c = *args;
9038 if (*s == ' ')
9039 ++s;
9040 if (args[1] != *s)
9041 {
9042 if (c == 'r' || c == 'v' || c == 'w')
9043 {
9044 regno = lastregno;
9045 s = s_reset;
9046 ++args;
9047 }
9048 }
9049 /* 'z' only matches $0. */
9050 if (c == 'z' && regno != 0)
9051 break;
9052
9053 /* Now that we have assembled one operand, we use the args string
9054 * to figure out where it goes in the instruction. */
9055 switch (c)
9056 {
9057 case 'r':
9058 case 's':
9059 case 'v':
9060 case 'b':
9061 INSERT_OPERAND (RS, *ip, regno);
9062 break;
9063 case 'd':
9064 case 'G':
9065 case 'K':
9066 case 'g':
9067 INSERT_OPERAND (RD, *ip, regno);
9068 break;
9069 case 'U':
9070 INSERT_OPERAND (RD, *ip, regno);
9071 INSERT_OPERAND (RT, *ip, regno);
9072 break;
9073 case 'w':
9074 case 't':
9075 case 'E':
9076 INSERT_OPERAND (RT, *ip, regno);
9077 break;
9078 case 'x':
9079 /* This case exists because on the r3000 trunc
9080 expands into a macro which requires a gp
9081 register. On the r6000 or r4000 it is
9082 assembled into a single instruction which
9083 ignores the register. Thus the insn version
9084 is MIPS_ISA2 and uses 'x', and the macro
9085 version is MIPS_ISA1 and uses 't'. */
9086 break;
9087 case 'z':
9088 /* This case is for the div instruction, which
9089 acts differently if the destination argument
9090 is $0. This only matches $0, and is checked
9091 outside the switch. */
9092 break;
9093 case 'D':
9094 /* Itbl operand; not yet implemented. FIXME ?? */
9095 break;
9096 /* What about all other operands like 'i', which
9097 can be specified in the opcode table? */
9098 }
9099 lastregno = regno;
9100 continue;
9101 }
9102 switch (*args++)
9103 {
9104 case 'r':
9105 case 'v':
9106 INSERT_OPERAND (RS, *ip, lastregno);
9107 continue;
9108 case 'w':
9109 INSERT_OPERAND (RT, *ip, lastregno);
9110 continue;
9111 }
9112 break;
9113
9114 case 'O': /* MDMX alignment immediate constant. */
9115 my_getExpression (&imm_expr, s);
9116 check_absolute_expr (ip, &imm_expr);
9117 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
9118 as_warn ("Improper align amount (%ld), using low bits",
9119 (long) imm_expr.X_add_number);
9120 INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
9121 imm_expr.X_op = O_absent;
9122 s = expr_end;
9123 continue;
9124
9125 case 'Q': /* MDMX vector, element sel, or const. */
9126 if (s[0] != '$')
9127 {
9128 /* MDMX Immediate. */
9129 my_getExpression (&imm_expr, s);
9130 check_absolute_expr (ip, &imm_expr);
9131 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
9132 as_warn (_("Invalid MDMX Immediate (%ld)"),
9133 (long) imm_expr.X_add_number);
9134 INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
9135 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9136 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
9137 else
9138 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
9139 imm_expr.X_op = O_absent;
9140 s = expr_end;
9141 continue;
9142 }
9143 /* Not MDMX Immediate. Fall through. */
9144 case 'X': /* MDMX destination register. */
9145 case 'Y': /* MDMX source register. */
9146 case 'Z': /* MDMX target register. */
9147 is_mdmx = 1;
9148 case 'D': /* floating point destination register */
9149 case 'S': /* floating point source register */
9150 case 'T': /* floating point target register */
9151 case 'R': /* floating point source register */
9152 case 'V':
9153 case 'W':
9154 rtype = RTYPE_FPU;
9155 if (is_mdmx
9156 || (mips_opts.ase_mdmx
9157 && (ip->insn_mo->pinfo & FP_D)
9158 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
9159 | INSN_COPROC_MEMORY_DELAY
9160 | INSN_LOAD_COPROC_DELAY
9161 | INSN_LOAD_MEMORY_DELAY
9162 | INSN_STORE_MEMORY))))
9163 rtype |= RTYPE_VEC;
9164 s_reset = s;
9165 if (reg_lookup (&s, rtype, &regno))
9166 {
9167 if ((regno & 1) != 0
9168 && HAVE_32BIT_FPRS
9169 && ! mips_oddfpreg_ok (ip->insn_mo, argnum))
9170 as_warn (_("Float register should be even, was %d"),
9171 regno);
9172
9173 c = *args;
9174 if (*s == ' ')
9175 ++s;
9176 if (args[1] != *s)
9177 {
9178 if (c == 'V' || c == 'W')
9179 {
9180 regno = lastregno;
9181 s = s_reset;
9182 ++args;
9183 }
9184 }
9185 switch (c)
9186 {
9187 case 'D':
9188 case 'X':
9189 INSERT_OPERAND (FD, *ip, regno);
9190 break;
9191 case 'V':
9192 case 'S':
9193 case 'Y':
9194 INSERT_OPERAND (FS, *ip, regno);
9195 break;
9196 case 'Q':
9197 /* This is like 'Z', but also needs to fix the MDMX
9198 vector/scalar select bits. Note that the
9199 scalar immediate case is handled above. */
9200 if (*s == '[')
9201 {
9202 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9203 int max_el = (is_qh ? 3 : 7);
9204 s++;
9205 my_getExpression(&imm_expr, s);
9206 check_absolute_expr (ip, &imm_expr);
9207 s = expr_end;
9208 if (imm_expr.X_add_number > max_el)
9209 as_bad(_("Bad element selector %ld"),
9210 (long) imm_expr.X_add_number);
9211 imm_expr.X_add_number &= max_el;
9212 ip->insn_opcode |= (imm_expr.X_add_number
9213 << (OP_SH_VSEL +
9214 (is_qh ? 2 : 1)));
9215 imm_expr.X_op = O_absent;
9216 if (*s != ']')
9217 as_warn(_("Expecting ']' found '%s'"), s);
9218 else
9219 s++;
9220 }
9221 else
9222 {
9223 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9224 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9225 << OP_SH_VSEL);
9226 else
9227 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9228 OP_SH_VSEL);
9229 }
9230 /* Fall through */
9231 case 'W':
9232 case 'T':
9233 case 'Z':
9234 INSERT_OPERAND (FT, *ip, regno);
9235 break;
9236 case 'R':
9237 INSERT_OPERAND (FR, *ip, regno);
9238 break;
9239 }
9240 lastregno = regno;
9241 continue;
9242 }
9243
9244 switch (*args++)
9245 {
9246 case 'V':
9247 INSERT_OPERAND (FS, *ip, lastregno);
9248 continue;
9249 case 'W':
9250 INSERT_OPERAND (FT, *ip, lastregno);
9251 continue;
9252 }
9253 break;
9254
9255 case 'I':
9256 my_getExpression (&imm_expr, s);
9257 if (imm_expr.X_op != O_big
9258 && imm_expr.X_op != O_constant)
9259 insn_error = _("absolute expression required");
9260 if (HAVE_32BIT_GPRS)
9261 normalize_constant_expr (&imm_expr);
9262 s = expr_end;
9263 continue;
9264
9265 case 'A':
9266 my_getExpression (&offset_expr, s);
9267 normalize_address_expr (&offset_expr);
9268 *imm_reloc = BFD_RELOC_32;
9269 s = expr_end;
9270 continue;
9271
9272 case 'F':
9273 case 'L':
9274 case 'f':
9275 case 'l':
9276 {
9277 int f64;
9278 int using_gprs;
9279 char *save_in;
9280 char *err;
9281 unsigned char temp[8];
9282 int len;
9283 unsigned int length;
9284 segT seg;
9285 subsegT subseg;
9286 char *p;
9287
9288 /* These only appear as the last operand in an
9289 instruction, and every instruction that accepts
9290 them in any variant accepts them in all variants.
9291 This means we don't have to worry about backing out
9292 any changes if the instruction does not match.
9293
9294 The difference between them is the size of the
9295 floating point constant and where it goes. For 'F'
9296 and 'L' the constant is 64 bits; for 'f' and 'l' it
9297 is 32 bits. Where the constant is placed is based
9298 on how the MIPS assembler does things:
9299 F -- .rdata
9300 L -- .lit8
9301 f -- immediate value
9302 l -- .lit4
9303
9304 The .lit4 and .lit8 sections are only used if
9305 permitted by the -G argument.
9306
9307 The code below needs to know whether the target register
9308 is 32 or 64 bits wide. It relies on the fact 'f' and
9309 'F' are used with GPR-based instructions and 'l' and
9310 'L' are used with FPR-based instructions. */
9311
9312 f64 = *args == 'F' || *args == 'L';
9313 using_gprs = *args == 'F' || *args == 'f';
9314
9315 save_in = input_line_pointer;
9316 input_line_pointer = s;
9317 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9318 length = len;
9319 s = input_line_pointer;
9320 input_line_pointer = save_in;
9321 if (err != NULL && *err != '\0')
9322 {
9323 as_bad (_("Bad floating point constant: %s"), err);
9324 memset (temp, '\0', sizeof temp);
9325 length = f64 ? 8 : 4;
9326 }
9327
9328 assert (length == (unsigned) (f64 ? 8 : 4));
9329
9330 if (*args == 'f'
9331 || (*args == 'l'
9332 && (g_switch_value < 4
9333 || (temp[0] == 0 && temp[1] == 0)
9334 || (temp[2] == 0 && temp[3] == 0))))
9335 {
9336 imm_expr.X_op = O_constant;
9337 if (! target_big_endian)
9338 imm_expr.X_add_number = bfd_getl32 (temp);
9339 else
9340 imm_expr.X_add_number = bfd_getb32 (temp);
9341 }
9342 else if (length > 4
9343 && ! mips_disable_float_construction
9344 /* Constants can only be constructed in GPRs and
9345 copied to FPRs if the GPRs are at least as wide
9346 as the FPRs. Force the constant into memory if
9347 we are using 64-bit FPRs but the GPRs are only
9348 32 bits wide. */
9349 && (using_gprs
9350 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
9351 && ((temp[0] == 0 && temp[1] == 0)
9352 || (temp[2] == 0 && temp[3] == 0))
9353 && ((temp[4] == 0 && temp[5] == 0)
9354 || (temp[6] == 0 && temp[7] == 0)))
9355 {
9356 /* The value is simple enough to load with a couple of
9357 instructions. If using 32-bit registers, set
9358 imm_expr to the high order 32 bits and offset_expr to
9359 the low order 32 bits. Otherwise, set imm_expr to
9360 the entire 64 bit constant. */
9361 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
9362 {
9363 imm_expr.X_op = O_constant;
9364 offset_expr.X_op = O_constant;
9365 if (! target_big_endian)
9366 {
9367 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9368 offset_expr.X_add_number = bfd_getl32 (temp);
9369 }
9370 else
9371 {
9372 imm_expr.X_add_number = bfd_getb32 (temp);
9373 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9374 }
9375 if (offset_expr.X_add_number == 0)
9376 offset_expr.X_op = O_absent;
9377 }
9378 else if (sizeof (imm_expr.X_add_number) > 4)
9379 {
9380 imm_expr.X_op = O_constant;
9381 if (! target_big_endian)
9382 imm_expr.X_add_number = bfd_getl64 (temp);
9383 else
9384 imm_expr.X_add_number = bfd_getb64 (temp);
9385 }
9386 else
9387 {
9388 imm_expr.X_op = O_big;
9389 imm_expr.X_add_number = 4;
9390 if (! target_big_endian)
9391 {
9392 generic_bignum[0] = bfd_getl16 (temp);
9393 generic_bignum[1] = bfd_getl16 (temp + 2);
9394 generic_bignum[2] = bfd_getl16 (temp + 4);
9395 generic_bignum[3] = bfd_getl16 (temp + 6);
9396 }
9397 else
9398 {
9399 generic_bignum[0] = bfd_getb16 (temp + 6);
9400 generic_bignum[1] = bfd_getb16 (temp + 4);
9401 generic_bignum[2] = bfd_getb16 (temp + 2);
9402 generic_bignum[3] = bfd_getb16 (temp);
9403 }
9404 }
9405 }
9406 else
9407 {
9408 const char *newname;
9409 segT new_seg;
9410
9411 /* Switch to the right section. */
9412 seg = now_seg;
9413 subseg = now_subseg;
9414 switch (*args)
9415 {
9416 default: /* unused default case avoids warnings. */
9417 case 'L':
9418 newname = RDATA_SECTION_NAME;
9419 if (g_switch_value >= 8)
9420 newname = ".lit8";
9421 break;
9422 case 'F':
9423 newname = RDATA_SECTION_NAME;
9424 break;
9425 case 'l':
9426 assert (g_switch_value >= 4);
9427 newname = ".lit4";
9428 break;
9429 }
9430 new_seg = subseg_new (newname, (subsegT) 0);
9431 if (IS_ELF)
9432 bfd_set_section_flags (stdoutput, new_seg,
9433 (SEC_ALLOC
9434 | SEC_LOAD
9435 | SEC_READONLY
9436 | SEC_DATA));
9437 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9438 if (IS_ELF && strcmp (TARGET_OS, "elf") != 0)
9439 record_alignment (new_seg, 4);
9440 else
9441 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9442 if (seg == now_seg)
9443 as_bad (_("Can't use floating point insn in this section"));
9444
9445 /* Set the argument to the current address in the
9446 section. */
9447 offset_expr.X_op = O_symbol;
9448 offset_expr.X_add_symbol =
9449 symbol_new ("L0\001", now_seg,
9450 (valueT) frag_now_fix (), frag_now);
9451 offset_expr.X_add_number = 0;
9452
9453 /* Put the floating point number into the section. */
9454 p = frag_more ((int) length);
9455 memcpy (p, temp, length);
9456
9457 /* Switch back to the original section. */
9458 subseg_set (seg, subseg);
9459 }
9460 }
9461 continue;
9462
9463 case 'i': /* 16 bit unsigned immediate */
9464 case 'j': /* 16 bit signed immediate */
9465 *imm_reloc = BFD_RELOC_LO16;
9466 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9467 {
9468 int more;
9469 offsetT minval, maxval;
9470
9471 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9472 && strcmp (insn->name, insn[1].name) == 0);
9473
9474 /* If the expression was written as an unsigned number,
9475 only treat it as signed if there are no more
9476 alternatives. */
9477 if (more
9478 && *args == 'j'
9479 && sizeof (imm_expr.X_add_number) <= 4
9480 && imm_expr.X_op == O_constant
9481 && imm_expr.X_add_number < 0
9482 && imm_expr.X_unsigned
9483 && HAVE_64BIT_GPRS)
9484 break;
9485
9486 /* For compatibility with older assemblers, we accept
9487 0x8000-0xffff as signed 16-bit numbers when only
9488 signed numbers are allowed. */
9489 if (*args == 'i')
9490 minval = 0, maxval = 0xffff;
9491 else if (more)
9492 minval = -0x8000, maxval = 0x7fff;
9493 else
9494 minval = -0x8000, maxval = 0xffff;
9495
9496 if (imm_expr.X_op != O_constant
9497 || imm_expr.X_add_number < minval
9498 || imm_expr.X_add_number > maxval)
9499 {
9500 if (more)
9501 break;
9502 if (imm_expr.X_op == O_constant
9503 || imm_expr.X_op == O_big)
9504 as_bad (_("expression out of range"));
9505 }
9506 }
9507 s = expr_end;
9508 continue;
9509
9510 case 'o': /* 16 bit offset */
9511 /* Check whether there is only a single bracketed expression
9512 left. If so, it must be the base register and the
9513 constant must be zero. */
9514 if (*s == '(' && strchr (s + 1, '(') == 0)
9515 {
9516 offset_expr.X_op = O_constant;
9517 offset_expr.X_add_number = 0;
9518 continue;
9519 }
9520
9521 /* If this value won't fit into a 16 bit offset, then go
9522 find a macro that will generate the 32 bit offset
9523 code pattern. */
9524 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9525 && (offset_expr.X_op != O_constant
9526 || offset_expr.X_add_number >= 0x8000
9527 || offset_expr.X_add_number < -0x8000))
9528 break;
9529
9530 s = expr_end;
9531 continue;
9532
9533 case 'p': /* pc relative offset */
9534 *offset_reloc = BFD_RELOC_16_PCREL_S2;
9535 my_getExpression (&offset_expr, s);
9536 s = expr_end;
9537 continue;
9538
9539 case 'u': /* upper 16 bits */
9540 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9541 && imm_expr.X_op == O_constant
9542 && (imm_expr.X_add_number < 0
9543 || imm_expr.X_add_number >= 0x10000))
9544 as_bad (_("lui expression not in range 0..65535"));
9545 s = expr_end;
9546 continue;
9547
9548 case 'a': /* 26 bit address */
9549 my_getExpression (&offset_expr, s);
9550 s = expr_end;
9551 *offset_reloc = BFD_RELOC_MIPS_JMP;
9552 continue;
9553
9554 case 'N': /* 3 bit branch condition code */
9555 case 'M': /* 3 bit compare condition code */
9556 rtype = RTYPE_CCC;
9557 if (ip->insn_mo->pinfo & (FP_D| FP_S))
9558 rtype |= RTYPE_FCC;
9559 if (!reg_lookup (&s, rtype, &regno))
9560 break;
9561 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9562 || strcmp(str + strlen(str) - 5, "any2f") == 0
9563 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9564 && (regno & 1) != 0)
9565 as_warn(_("Condition code register should be even for %s, was %d"),
9566 str, regno);
9567 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9568 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9569 && (regno & 3) != 0)
9570 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9571 str, regno);
9572 if (*args == 'N')
9573 INSERT_OPERAND (BCC, *ip, regno);
9574 else
9575 INSERT_OPERAND (CCC, *ip, regno);
9576 continue;
9577
9578 case 'H':
9579 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9580 s += 2;
9581 if (ISDIGIT (*s))
9582 {
9583 c = 0;
9584 do
9585 {
9586 c *= 10;
9587 c += *s - '0';
9588 ++s;
9589 }
9590 while (ISDIGIT (*s));
9591 }
9592 else
9593 c = 8; /* Invalid sel value. */
9594
9595 if (c > 7)
9596 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9597 ip->insn_opcode |= c;
9598 continue;
9599
9600 case 'e':
9601 /* Must be at least one digit. */
9602 my_getExpression (&imm_expr, s);
9603 check_absolute_expr (ip, &imm_expr);
9604
9605 if ((unsigned long) imm_expr.X_add_number
9606 > (unsigned long) OP_MASK_VECBYTE)
9607 {
9608 as_bad (_("bad byte vector index (%ld)"),
9609 (long) imm_expr.X_add_number);
9610 imm_expr.X_add_number = 0;
9611 }
9612
9613 INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
9614 imm_expr.X_op = O_absent;
9615 s = expr_end;
9616 continue;
9617
9618 case '%':
9619 my_getExpression (&imm_expr, s);
9620 check_absolute_expr (ip, &imm_expr);
9621
9622 if ((unsigned long) imm_expr.X_add_number
9623 > (unsigned long) OP_MASK_VECALIGN)
9624 {
9625 as_bad (_("bad byte vector index (%ld)"),
9626 (long) imm_expr.X_add_number);
9627 imm_expr.X_add_number = 0;
9628 }
9629
9630 INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
9631 imm_expr.X_op = O_absent;
9632 s = expr_end;
9633 continue;
9634
9635 default:
9636 as_bad (_("bad char = '%c'\n"), *args);
9637 internalError ();
9638 }
9639 break;
9640 }
9641 /* Args don't match. */
9642 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9643 !strcmp (insn->name, insn[1].name))
9644 {
9645 ++insn;
9646 s = argsStart;
9647 insn_error = _("illegal operands");
9648 continue;
9649 }
9650 if (save_c)
9651 *(--s) = save_c;
9652 insn_error = _("illegal operands");
9653 return;
9654 }
9655 }
9656
9657 #define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
9658
9659 /* This routine assembles an instruction into its binary format when
9660 assembling for the mips16. As a side effect, it sets one of the
9661 global variables imm_reloc or offset_reloc to the type of
9662 relocation to do if one of the operands is an address expression.
9663 It also sets mips16_small and mips16_ext if the user explicitly
9664 requested a small or extended instruction. */
9665
9666 static void
9667 mips16_ip (char *str, struct mips_cl_insn *ip)
9668 {
9669 char *s;
9670 const char *args;
9671 struct mips_opcode *insn;
9672 char *argsstart;
9673 unsigned int regno;
9674 unsigned int lastregno = 0;
9675 char *s_reset;
9676 size_t i;
9677
9678 insn_error = NULL;
9679
9680 mips16_small = FALSE;
9681 mips16_ext = FALSE;
9682
9683 for (s = str; ISLOWER (*s); ++s)
9684 ;
9685 switch (*s)
9686 {
9687 case '\0':
9688 break;
9689
9690 case ' ':
9691 *s++ = '\0';
9692 break;
9693
9694 case '.':
9695 if (s[1] == 't' && s[2] == ' ')
9696 {
9697 *s = '\0';
9698 mips16_small = TRUE;
9699 s += 3;
9700 break;
9701 }
9702 else if (s[1] == 'e' && s[2] == ' ')
9703 {
9704 *s = '\0';
9705 mips16_ext = TRUE;
9706 s += 3;
9707 break;
9708 }
9709 /* Fall through. */
9710 default:
9711 insn_error = _("unknown opcode");
9712 return;
9713 }
9714
9715 if (mips_opts.noautoextend && ! mips16_ext)
9716 mips16_small = TRUE;
9717
9718 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9719 {
9720 insn_error = _("unrecognized opcode");
9721 return;
9722 }
9723
9724 argsstart = s;
9725 for (;;)
9726 {
9727 bfd_boolean ok;
9728
9729 assert (strcmp (insn->name, str) == 0);
9730
9731 if (OPCODE_IS_MEMBER (insn, mips_opts.isa, mips_opts.arch))
9732 ok = TRUE;
9733 else
9734 ok = FALSE;
9735
9736 if (! ok)
9737 {
9738 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
9739 && strcmp (insn->name, insn[1].name) == 0)
9740 {
9741 ++insn;
9742 continue;
9743 }
9744 else
9745 {
9746 if (!insn_error)
9747 {
9748 static char buf[100];
9749 sprintf (buf,
9750 _("opcode not supported on this processor: %s (%s)"),
9751 mips_cpu_info_from_arch (mips_opts.arch)->name,
9752 mips_cpu_info_from_isa (mips_opts.isa)->name);
9753 insn_error = buf;
9754 }
9755 return;
9756 }
9757 }
9758
9759 create_insn (ip, insn);
9760 imm_expr.X_op = O_absent;
9761 imm_reloc[0] = BFD_RELOC_UNUSED;
9762 imm_reloc[1] = BFD_RELOC_UNUSED;
9763 imm_reloc[2] = BFD_RELOC_UNUSED;
9764 imm2_expr.X_op = O_absent;
9765 offset_expr.X_op = O_absent;
9766 offset_reloc[0] = BFD_RELOC_UNUSED;
9767 offset_reloc[1] = BFD_RELOC_UNUSED;
9768 offset_reloc[2] = BFD_RELOC_UNUSED;
9769 for (args = insn->args; 1; ++args)
9770 {
9771 int c;
9772
9773 if (*s == ' ')
9774 ++s;
9775
9776 /* In this switch statement we call break if we did not find
9777 a match, continue if we did find a match, or return if we
9778 are done. */
9779
9780 c = *args;
9781 switch (c)
9782 {
9783 case '\0':
9784 if (*s == '\0')
9785 {
9786 /* Stuff the immediate value in now, if we can. */
9787 if (imm_expr.X_op == O_constant
9788 && *imm_reloc > BFD_RELOC_UNUSED
9789 && insn->pinfo != INSN_MACRO)
9790 {
9791 valueT tmp;
9792
9793 switch (*offset_reloc)
9794 {
9795 case BFD_RELOC_MIPS16_HI16_S:
9796 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9797 break;
9798
9799 case BFD_RELOC_MIPS16_HI16:
9800 tmp = imm_expr.X_add_number >> 16;
9801 break;
9802
9803 case BFD_RELOC_MIPS16_LO16:
9804 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9805 - 0x8000;
9806 break;
9807
9808 case BFD_RELOC_UNUSED:
9809 tmp = imm_expr.X_add_number;
9810 break;
9811
9812 default:
9813 internalError ();
9814 }
9815 *offset_reloc = BFD_RELOC_UNUSED;
9816
9817 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9818 tmp, TRUE, mips16_small,
9819 mips16_ext, &ip->insn_opcode,
9820 &ip->use_extend, &ip->extend);
9821 imm_expr.X_op = O_absent;
9822 *imm_reloc = BFD_RELOC_UNUSED;
9823 }
9824
9825 return;
9826 }
9827 break;
9828
9829 case ',':
9830 if (*s++ == c)
9831 continue;
9832 s--;
9833 switch (*++args)
9834 {
9835 case 'v':
9836 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9837 continue;
9838 case 'w':
9839 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9840 continue;
9841 }
9842 break;
9843
9844 case '(':
9845 case ')':
9846 if (*s++ == c)
9847 continue;
9848 break;
9849
9850 case 'v':
9851 case 'w':
9852 if (s[0] != '$')
9853 {
9854 if (c == 'v')
9855 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9856 else
9857 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9858 ++args;
9859 continue;
9860 }
9861 /* Fall through. */
9862 case 'x':
9863 case 'y':
9864 case 'z':
9865 case 'Z':
9866 case '0':
9867 case 'S':
9868 case 'R':
9869 case 'X':
9870 case 'Y':
9871 s_reset = s;
9872 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
9873 {
9874 if (c == 'v' || c == 'w')
9875 {
9876 if (c == 'v')
9877 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9878 else
9879 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9880 ++args;
9881 continue;
9882 }
9883 break;
9884 }
9885
9886 if (*s == ' ')
9887 ++s;
9888 if (args[1] != *s)
9889 {
9890 if (c == 'v' || c == 'w')
9891 {
9892 regno = mips16_to_32_reg_map[lastregno];
9893 s = s_reset;
9894 ++args;
9895 }
9896 }
9897
9898 switch (c)
9899 {
9900 case 'x':
9901 case 'y':
9902 case 'z':
9903 case 'v':
9904 case 'w':
9905 case 'Z':
9906 regno = mips32_to_16_reg_map[regno];
9907 break;
9908
9909 case '0':
9910 if (regno != 0)
9911 regno = ILLEGAL_REG;
9912 break;
9913
9914 case 'S':
9915 if (regno != SP)
9916 regno = ILLEGAL_REG;
9917 break;
9918
9919 case 'R':
9920 if (regno != RA)
9921 regno = ILLEGAL_REG;
9922 break;
9923
9924 case 'X':
9925 case 'Y':
9926 if (regno == AT && ! mips_opts.noat)
9927 as_warn (_("used $at without \".set noat\""));
9928 break;
9929
9930 default:
9931 internalError ();
9932 }
9933
9934 if (regno == ILLEGAL_REG)
9935 break;
9936
9937 switch (c)
9938 {
9939 case 'x':
9940 case 'v':
9941 MIPS16_INSERT_OPERAND (RX, *ip, regno);
9942 break;
9943 case 'y':
9944 case 'w':
9945 MIPS16_INSERT_OPERAND (RY, *ip, regno);
9946 break;
9947 case 'z':
9948 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
9949 break;
9950 case 'Z':
9951 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
9952 case '0':
9953 case 'S':
9954 case 'R':
9955 break;
9956 case 'X':
9957 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
9958 break;
9959 case 'Y':
9960 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9961 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
9962 break;
9963 default:
9964 internalError ();
9965 }
9966
9967 lastregno = regno;
9968 continue;
9969
9970 case 'P':
9971 if (strncmp (s, "$pc", 3) == 0)
9972 {
9973 s += 3;
9974 continue;
9975 }
9976 break;
9977
9978 case '5':
9979 case 'H':
9980 case 'W':
9981 case 'D':
9982 case 'j':
9983 case 'V':
9984 case 'C':
9985 case 'U':
9986 case 'k':
9987 case 'K':
9988 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9989 if (i > 0)
9990 {
9991 if (imm_expr.X_op != O_constant)
9992 {
9993 mips16_ext = TRUE;
9994 ip->use_extend = TRUE;
9995 ip->extend = 0;
9996 }
9997 else
9998 {
9999 /* We need to relax this instruction. */
10000 *offset_reloc = *imm_reloc;
10001 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10002 }
10003 s = expr_end;
10004 continue;
10005 }
10006 *imm_reloc = BFD_RELOC_UNUSED;
10007 /* Fall through. */
10008 case '<':
10009 case '>':
10010 case '[':
10011 case ']':
10012 case '4':
10013 case '8':
10014 my_getExpression (&imm_expr, s);
10015 if (imm_expr.X_op == O_register)
10016 {
10017 /* What we thought was an expression turned out to
10018 be a register. */
10019
10020 if (s[0] == '(' && args[1] == '(')
10021 {
10022 /* It looks like the expression was omitted
10023 before a register indirection, which means
10024 that the expression is implicitly zero. We
10025 still set up imm_expr, so that we handle
10026 explicit extensions correctly. */
10027 imm_expr.X_op = O_constant;
10028 imm_expr.X_add_number = 0;
10029 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10030 continue;
10031 }
10032
10033 break;
10034 }
10035
10036 /* We need to relax this instruction. */
10037 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10038 s = expr_end;
10039 continue;
10040
10041 case 'p':
10042 case 'q':
10043 case 'A':
10044 case 'B':
10045 case 'E':
10046 /* We use offset_reloc rather than imm_reloc for the PC
10047 relative operands. This lets macros with both
10048 immediate and address operands work correctly. */
10049 my_getExpression (&offset_expr, s);
10050
10051 if (offset_expr.X_op == O_register)
10052 break;
10053
10054 /* We need to relax this instruction. */
10055 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
10056 s = expr_end;
10057 continue;
10058
10059 case '6': /* break code */
10060 my_getExpression (&imm_expr, s);
10061 check_absolute_expr (ip, &imm_expr);
10062 if ((unsigned long) imm_expr.X_add_number > 63)
10063 as_warn (_("Invalid value for `%s' (%lu)"),
10064 ip->insn_mo->name,
10065 (unsigned long) imm_expr.X_add_number);
10066 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
10067 imm_expr.X_op = O_absent;
10068 s = expr_end;
10069 continue;
10070
10071 case 'a': /* 26 bit address */
10072 my_getExpression (&offset_expr, s);
10073 s = expr_end;
10074 *offset_reloc = BFD_RELOC_MIPS16_JMP;
10075 ip->insn_opcode <<= 16;
10076 continue;
10077
10078 case 'l': /* register list for entry macro */
10079 case 'L': /* register list for exit macro */
10080 {
10081 int mask;
10082
10083 if (c == 'l')
10084 mask = 0;
10085 else
10086 mask = 7 << 3;
10087 while (*s != '\0')
10088 {
10089 unsigned int freg, reg1, reg2;
10090
10091 while (*s == ' ' || *s == ',')
10092 ++s;
10093 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
10094 freg = 0;
10095 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
10096 freg = 1;
10097 else
10098 {
10099 as_bad (_("can't parse register list"));
10100 break;
10101 }
10102 if (*s == ' ')
10103 ++s;
10104 if (*s != '-')
10105 reg2 = reg1;
10106 else
10107 {
10108 ++s;
10109 if (!reg_lookup (&s, freg ? RTYPE_FPU
10110 : (RTYPE_GP | RTYPE_NUM), &reg2))
10111 {
10112 as_bad (_("invalid register list"));
10113 break;
10114 }
10115 }
10116 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
10117 {
10118 mask &= ~ (7 << 3);
10119 mask |= 5 << 3;
10120 }
10121 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
10122 {
10123 mask &= ~ (7 << 3);
10124 mask |= 6 << 3;
10125 }
10126 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
10127 mask |= (reg2 - 3) << 3;
10128 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
10129 mask |= (reg2 - 15) << 1;
10130 else if (reg1 == RA && reg2 == RA)
10131 mask |= 1;
10132 else
10133 {
10134 as_bad (_("invalid register list"));
10135 break;
10136 }
10137 }
10138 /* The mask is filled in in the opcode table for the
10139 benefit of the disassembler. We remove it before
10140 applying the actual mask. */
10141 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
10142 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
10143 }
10144 continue;
10145
10146 case 'm': /* Register list for save insn. */
10147 case 'M': /* Register list for restore insn. */
10148 {
10149 int opcode = 0;
10150 int framesz = 0, seen_framesz = 0;
10151 int args = 0, statics = 0, sregs = 0;
10152
10153 while (*s != '\0')
10154 {
10155 unsigned int reg1, reg2;
10156
10157 SKIP_SPACE_TABS (s);
10158 while (*s == ',')
10159 ++s;
10160 SKIP_SPACE_TABS (s);
10161
10162 my_getExpression (&imm_expr, s);
10163 if (imm_expr.X_op == O_constant)
10164 {
10165 /* Handle the frame size. */
10166 if (seen_framesz)
10167 {
10168 as_bad (_("more than one frame size in list"));
10169 break;
10170 }
10171 seen_framesz = 1;
10172 framesz = imm_expr.X_add_number;
10173 imm_expr.X_op = O_absent;
10174 s = expr_end;
10175 continue;
10176 }
10177
10178 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
10179 {
10180 as_bad (_("can't parse register list"));
10181 break;
10182 }
10183
10184 while (*s == ' ')
10185 ++s;
10186
10187 if (*s != '-')
10188 reg2 = reg1;
10189 else
10190 {
10191 ++s;
10192 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
10193 || reg2 < reg1)
10194 {
10195 as_bad (_("can't parse register list"));
10196 break;
10197 }
10198 }
10199
10200 while (reg1 <= reg2)
10201 {
10202 if (reg1 >= 4 && reg1 <= 7)
10203 {
10204 if (!seen_framesz)
10205 /* args $a0-$a3 */
10206 args |= 1 << (reg1 - 4);
10207 else
10208 /* statics $a0-$a3 */
10209 statics |= 1 << (reg1 - 4);
10210 }
10211 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
10212 {
10213 /* $s0-$s8 */
10214 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
10215 }
10216 else if (reg1 == 31)
10217 {
10218 /* Add $ra to insn. */
10219 opcode |= 0x40;
10220 }
10221 else
10222 {
10223 as_bad (_("unexpected register in list"));
10224 break;
10225 }
10226 if (++reg1 == 24)
10227 reg1 = 30;
10228 }
10229 }
10230
10231 /* Encode args/statics combination. */
10232 if (args & statics)
10233 as_bad (_("arg/static registers overlap"));
10234 else if (args == 0xf)
10235 /* All $a0-$a3 are args. */
10236 opcode |= MIPS16_ALL_ARGS << 16;
10237 else if (statics == 0xf)
10238 /* All $a0-$a3 are statics. */
10239 opcode |= MIPS16_ALL_STATICS << 16;
10240 else
10241 {
10242 int narg = 0, nstat = 0;
10243
10244 /* Count arg registers. */
10245 while (args & 0x1)
10246 {
10247 args >>= 1;
10248 narg++;
10249 }
10250 if (args != 0)
10251 as_bad (_("invalid arg register list"));
10252
10253 /* Count static registers. */
10254 while (statics & 0x8)
10255 {
10256 statics = (statics << 1) & 0xf;
10257 nstat++;
10258 }
10259 if (statics != 0)
10260 as_bad (_("invalid static register list"));
10261
10262 /* Encode args/statics. */
10263 opcode |= ((narg << 2) | nstat) << 16;
10264 }
10265
10266 /* Encode $s0/$s1. */
10267 if (sregs & (1 << 0)) /* $s0 */
10268 opcode |= 0x20;
10269 if (sregs & (1 << 1)) /* $s1 */
10270 opcode |= 0x10;
10271 sregs >>= 2;
10272
10273 if (sregs != 0)
10274 {
10275 /* Count regs $s2-$s8. */
10276 int nsreg = 0;
10277 while (sregs & 1)
10278 {
10279 sregs >>= 1;
10280 nsreg++;
10281 }
10282 if (sregs != 0)
10283 as_bad (_("invalid static register list"));
10284 /* Encode $s2-$s8. */
10285 opcode |= nsreg << 24;
10286 }
10287
10288 /* Encode frame size. */
10289 if (!seen_framesz)
10290 as_bad (_("missing frame size"));
10291 else if ((framesz & 7) != 0 || framesz < 0
10292 || framesz > 0xff * 8)
10293 as_bad (_("invalid frame size"));
10294 else if (framesz != 128 || (opcode >> 16) != 0)
10295 {
10296 framesz /= 8;
10297 opcode |= (((framesz & 0xf0) << 16)
10298 | (framesz & 0x0f));
10299 }
10300
10301 /* Finally build the instruction. */
10302 if ((opcode >> 16) != 0 || framesz == 0)
10303 {
10304 ip->use_extend = TRUE;
10305 ip->extend = opcode >> 16;
10306 }
10307 ip->insn_opcode |= opcode & 0x7f;
10308 }
10309 continue;
10310
10311 case 'e': /* extend code */
10312 my_getExpression (&imm_expr, s);
10313 check_absolute_expr (ip, &imm_expr);
10314 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10315 {
10316 as_warn (_("Invalid value for `%s' (%lu)"),
10317 ip->insn_mo->name,
10318 (unsigned long) imm_expr.X_add_number);
10319 imm_expr.X_add_number &= 0x7ff;
10320 }
10321 ip->insn_opcode |= imm_expr.X_add_number;
10322 imm_expr.X_op = O_absent;
10323 s = expr_end;
10324 continue;
10325
10326 default:
10327 internalError ();
10328 }
10329 break;
10330 }
10331
10332 /* Args don't match. */
10333 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10334 strcmp (insn->name, insn[1].name) == 0)
10335 {
10336 ++insn;
10337 s = argsstart;
10338 continue;
10339 }
10340
10341 insn_error = _("illegal operands");
10342
10343 return;
10344 }
10345 }
10346
10347 /* This structure holds information we know about a mips16 immediate
10348 argument type. */
10349
10350 struct mips16_immed_operand
10351 {
10352 /* The type code used in the argument string in the opcode table. */
10353 int type;
10354 /* The number of bits in the short form of the opcode. */
10355 int nbits;
10356 /* The number of bits in the extended form of the opcode. */
10357 int extbits;
10358 /* The amount by which the short form is shifted when it is used;
10359 for example, the sw instruction has a shift count of 2. */
10360 int shift;
10361 /* The amount by which the short form is shifted when it is stored
10362 into the instruction code. */
10363 int op_shift;
10364 /* Non-zero if the short form is unsigned. */
10365 int unsp;
10366 /* Non-zero if the extended form is unsigned. */
10367 int extu;
10368 /* Non-zero if the value is PC relative. */
10369 int pcrel;
10370 };
10371
10372 /* The mips16 immediate operand types. */
10373
10374 static const struct mips16_immed_operand mips16_immed_operands[] =
10375 {
10376 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10377 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10378 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10379 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10380 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10381 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10382 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10383 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10384 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10385 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10386 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10387 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10388 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10389 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10390 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10391 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10392 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10393 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10394 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10395 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10396 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10397 };
10398
10399 #define MIPS16_NUM_IMMED \
10400 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10401
10402 /* Handle a mips16 instruction with an immediate value. This or's the
10403 small immediate value into *INSN. It sets *USE_EXTEND to indicate
10404 whether an extended value is needed; if one is needed, it sets
10405 *EXTEND to the value. The argument type is TYPE. The value is VAL.
10406 If SMALL is true, an unextended opcode was explicitly requested.
10407 If EXT is true, an extended opcode was explicitly requested. If
10408 WARN is true, warn if EXT does not match reality. */
10409
10410 static void
10411 mips16_immed (char *file, unsigned int line, int type, offsetT val,
10412 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10413 unsigned long *insn, bfd_boolean *use_extend,
10414 unsigned short *extend)
10415 {
10416 const struct mips16_immed_operand *op;
10417 int mintiny, maxtiny;
10418 bfd_boolean needext;
10419
10420 op = mips16_immed_operands;
10421 while (op->type != type)
10422 {
10423 ++op;
10424 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10425 }
10426
10427 if (op->unsp)
10428 {
10429 if (type == '<' || type == '>' || type == '[' || type == ']')
10430 {
10431 mintiny = 1;
10432 maxtiny = 1 << op->nbits;
10433 }
10434 else
10435 {
10436 mintiny = 0;
10437 maxtiny = (1 << op->nbits) - 1;
10438 }
10439 }
10440 else
10441 {
10442 mintiny = - (1 << (op->nbits - 1));
10443 maxtiny = (1 << (op->nbits - 1)) - 1;
10444 }
10445
10446 /* Branch offsets have an implicit 0 in the lowest bit. */
10447 if (type == 'p' || type == 'q')
10448 val /= 2;
10449
10450 if ((val & ((1 << op->shift) - 1)) != 0
10451 || val < (mintiny << op->shift)
10452 || val > (maxtiny << op->shift))
10453 needext = TRUE;
10454 else
10455 needext = FALSE;
10456
10457 if (warn && ext && ! needext)
10458 as_warn_where (file, line,
10459 _("extended operand requested but not required"));
10460 if (small && needext)
10461 as_bad_where (file, line, _("invalid unextended operand value"));
10462
10463 if (small || (! ext && ! needext))
10464 {
10465 int insnval;
10466
10467 *use_extend = FALSE;
10468 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10469 insnval <<= op->op_shift;
10470 *insn |= insnval;
10471 }
10472 else
10473 {
10474 long minext, maxext;
10475 int extval;
10476
10477 if (op->extu)
10478 {
10479 minext = 0;
10480 maxext = (1 << op->extbits) - 1;
10481 }
10482 else
10483 {
10484 minext = - (1 << (op->extbits - 1));
10485 maxext = (1 << (op->extbits - 1)) - 1;
10486 }
10487 if (val < minext || val > maxext)
10488 as_bad_where (file, line,
10489 _("operand value out of range for instruction"));
10490
10491 *use_extend = TRUE;
10492 if (op->extbits == 16)
10493 {
10494 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10495 val &= 0x1f;
10496 }
10497 else if (op->extbits == 15)
10498 {
10499 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10500 val &= 0xf;
10501 }
10502 else
10503 {
10504 extval = ((val & 0x1f) << 6) | (val & 0x20);
10505 val = 0;
10506 }
10507
10508 *extend = (unsigned short) extval;
10509 *insn |= val;
10510 }
10511 }
10512 \f
10513 struct percent_op_match
10514 {
10515 const char *str;
10516 bfd_reloc_code_real_type reloc;
10517 };
10518
10519 static const struct percent_op_match mips_percent_op[] =
10520 {
10521 {"%lo", BFD_RELOC_LO16},
10522 #ifdef OBJ_ELF
10523 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10524 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10525 {"%call16", BFD_RELOC_MIPS_CALL16},
10526 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10527 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10528 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10529 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10530 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10531 {"%got", BFD_RELOC_MIPS_GOT16},
10532 {"%gp_rel", BFD_RELOC_GPREL16},
10533 {"%half", BFD_RELOC_16},
10534 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10535 {"%higher", BFD_RELOC_MIPS_HIGHER},
10536 {"%neg", BFD_RELOC_MIPS_SUB},
10537 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
10538 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
10539 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
10540 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
10541 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
10542 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
10543 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
10544 #endif
10545 {"%hi", BFD_RELOC_HI16_S}
10546 };
10547
10548 static const struct percent_op_match mips16_percent_op[] =
10549 {
10550 {"%lo", BFD_RELOC_MIPS16_LO16},
10551 {"%gprel", BFD_RELOC_MIPS16_GPREL},
10552 {"%hi", BFD_RELOC_MIPS16_HI16_S}
10553 };
10554
10555
10556 /* Return true if *STR points to a relocation operator. When returning true,
10557 move *STR over the operator and store its relocation code in *RELOC.
10558 Leave both *STR and *RELOC alone when returning false. */
10559
10560 static bfd_boolean
10561 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
10562 {
10563 const struct percent_op_match *percent_op;
10564 size_t limit, i;
10565
10566 if (mips_opts.mips16)
10567 {
10568 percent_op = mips16_percent_op;
10569 limit = ARRAY_SIZE (mips16_percent_op);
10570 }
10571 else
10572 {
10573 percent_op = mips_percent_op;
10574 limit = ARRAY_SIZE (mips_percent_op);
10575 }
10576
10577 for (i = 0; i < limit; i++)
10578 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10579 {
10580 int len = strlen (percent_op[i].str);
10581
10582 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
10583 continue;
10584
10585 *str += strlen (percent_op[i].str);
10586 *reloc = percent_op[i].reloc;
10587
10588 /* Check whether the output BFD supports this relocation.
10589 If not, issue an error and fall back on something safe. */
10590 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10591 {
10592 as_bad ("relocation %s isn't supported by the current ABI",
10593 percent_op[i].str);
10594 *reloc = BFD_RELOC_UNUSED;
10595 }
10596 return TRUE;
10597 }
10598 return FALSE;
10599 }
10600
10601
10602 /* Parse string STR as a 16-bit relocatable operand. Store the
10603 expression in *EP and the relocations in the array starting
10604 at RELOC. Return the number of relocation operators used.
10605
10606 On exit, EXPR_END points to the first character after the expression. */
10607
10608 static size_t
10609 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10610 char *str)
10611 {
10612 bfd_reloc_code_real_type reversed_reloc[3];
10613 size_t reloc_index, i;
10614 int crux_depth, str_depth;
10615 char *crux;
10616
10617 /* Search for the start of the main expression, recoding relocations
10618 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10619 of the main expression and with CRUX_DEPTH containing the number
10620 of open brackets at that point. */
10621 reloc_index = -1;
10622 str_depth = 0;
10623 do
10624 {
10625 reloc_index++;
10626 crux = str;
10627 crux_depth = str_depth;
10628
10629 /* Skip over whitespace and brackets, keeping count of the number
10630 of brackets. */
10631 while (*str == ' ' || *str == '\t' || *str == '(')
10632 if (*str++ == '(')
10633 str_depth++;
10634 }
10635 while (*str == '%'
10636 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10637 && parse_relocation (&str, &reversed_reloc[reloc_index]));
10638
10639 my_getExpression (ep, crux);
10640 str = expr_end;
10641
10642 /* Match every open bracket. */
10643 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10644 if (*str++ == ')')
10645 crux_depth--;
10646
10647 if (crux_depth > 0)
10648 as_bad ("unclosed '('");
10649
10650 expr_end = str;
10651
10652 if (reloc_index != 0)
10653 {
10654 prev_reloc_op_frag = frag_now;
10655 for (i = 0; i < reloc_index; i++)
10656 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10657 }
10658
10659 return reloc_index;
10660 }
10661
10662 static void
10663 my_getExpression (expressionS *ep, char *str)
10664 {
10665 char *save_in;
10666 valueT val;
10667
10668 save_in = input_line_pointer;
10669 input_line_pointer = str;
10670 expression (ep);
10671 expr_end = input_line_pointer;
10672 input_line_pointer = save_in;
10673
10674 /* If we are in mips16 mode, and this is an expression based on `.',
10675 then we bump the value of the symbol by 1 since that is how other
10676 text symbols are handled. We don't bother to handle complex
10677 expressions, just `.' plus or minus a constant. */
10678 if (mips_opts.mips16
10679 && ep->X_op == O_symbol
10680 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10681 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10682 && symbol_get_frag (ep->X_add_symbol) == frag_now
10683 && symbol_constant_p (ep->X_add_symbol)
10684 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10685 S_SET_VALUE (ep->X_add_symbol, val + 1);
10686 }
10687
10688 /* Turn a string in input_line_pointer into a floating point constant
10689 of type TYPE, and store the appropriate bytes in *LITP. The number
10690 of LITTLENUMS emitted is stored in *SIZEP. An error message is
10691 returned, or NULL on OK. */
10692
10693 char *
10694 md_atof (int type, char *litP, int *sizeP)
10695 {
10696 int prec;
10697 LITTLENUM_TYPE words[4];
10698 char *t;
10699 int i;
10700
10701 switch (type)
10702 {
10703 case 'f':
10704 prec = 2;
10705 break;
10706
10707 case 'd':
10708 prec = 4;
10709 break;
10710
10711 default:
10712 *sizeP = 0;
10713 return _("bad call to md_atof");
10714 }
10715
10716 t = atof_ieee (input_line_pointer, type, words);
10717 if (t)
10718 input_line_pointer = t;
10719
10720 *sizeP = prec * 2;
10721
10722 if (! target_big_endian)
10723 {
10724 for (i = prec - 1; i >= 0; i--)
10725 {
10726 md_number_to_chars (litP, words[i], 2);
10727 litP += 2;
10728 }
10729 }
10730 else
10731 {
10732 for (i = 0; i < prec; i++)
10733 {
10734 md_number_to_chars (litP, words[i], 2);
10735 litP += 2;
10736 }
10737 }
10738
10739 return NULL;
10740 }
10741
10742 void
10743 md_number_to_chars (char *buf, valueT val, int n)
10744 {
10745 if (target_big_endian)
10746 number_to_chars_bigendian (buf, val, n);
10747 else
10748 number_to_chars_littleendian (buf, val, n);
10749 }
10750 \f
10751 #ifdef OBJ_ELF
10752 static int support_64bit_objects(void)
10753 {
10754 const char **list, **l;
10755 int yes;
10756
10757 list = bfd_target_list ();
10758 for (l = list; *l != NULL; l++)
10759 #ifdef TE_TMIPS
10760 /* This is traditional mips */
10761 if (strcmp (*l, "elf64-tradbigmips") == 0
10762 || strcmp (*l, "elf64-tradlittlemips") == 0)
10763 #else
10764 if (strcmp (*l, "elf64-bigmips") == 0
10765 || strcmp (*l, "elf64-littlemips") == 0)
10766 #endif
10767 break;
10768 yes = (*l != NULL);
10769 free (list);
10770 return yes;
10771 }
10772 #endif /* OBJ_ELF */
10773
10774 const char *md_shortopts = "O::g::G:";
10775
10776 struct option md_longopts[] =
10777 {
10778 /* Options which specify architecture. */
10779 #define OPTION_ARCH_BASE (OPTION_MD_BASE)
10780 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10781 {"march", required_argument, NULL, OPTION_MARCH},
10782 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10783 {"mtune", required_argument, NULL, OPTION_MTUNE},
10784 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10785 {"mips0", no_argument, NULL, OPTION_MIPS1},
10786 {"mips1", no_argument, NULL, OPTION_MIPS1},
10787 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10788 {"mips2", no_argument, NULL, OPTION_MIPS2},
10789 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10790 {"mips3", no_argument, NULL, OPTION_MIPS3},
10791 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10792 {"mips4", no_argument, NULL, OPTION_MIPS4},
10793 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10794 {"mips5", no_argument, NULL, OPTION_MIPS5},
10795 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10796 {"mips32", no_argument, NULL, OPTION_MIPS32},
10797 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10798 {"mips64", no_argument, NULL, OPTION_MIPS64},
10799 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10800 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10801 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10802 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
10803
10804 /* Options which specify Application Specific Extensions (ASEs). */
10805 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
10806 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10807 {"mips16", no_argument, NULL, OPTION_MIPS16},
10808 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10809 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10810 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10811 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10812 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10813 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10814 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10815 {"mdmx", no_argument, NULL, OPTION_MDMX},
10816 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10817 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10818 #define OPTION_DSP (OPTION_ASE_BASE + 6)
10819 {"mdsp", no_argument, NULL, OPTION_DSP},
10820 #define OPTION_NO_DSP (OPTION_ASE_BASE + 7)
10821 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
10822 #define OPTION_MT (OPTION_ASE_BASE + 8)
10823 {"mmt", no_argument, NULL, OPTION_MT},
10824 #define OPTION_NO_MT (OPTION_ASE_BASE + 9)
10825 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
10826 #define OPTION_SMARTMIPS (OPTION_ASE_BASE + 10)
10827 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
10828 #define OPTION_NO_SMARTMIPS (OPTION_ASE_BASE + 11)
10829 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
10830
10831 /* Old-style architecture options. Don't add more of these. */
10832 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 12)
10833 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10834 {"m4650", no_argument, NULL, OPTION_M4650},
10835 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10836 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10837 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10838 {"m4010", no_argument, NULL, OPTION_M4010},
10839 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10840 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10841 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10842 {"m4100", no_argument, NULL, OPTION_M4100},
10843 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10844 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10845 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10846 {"m3900", no_argument, NULL, OPTION_M3900},
10847 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10848 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10849
10850 /* Options which enable bug fixes. */
10851 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10852 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10853 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10854 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10855 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10856 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10857 #define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10858 #define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10859 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10860 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
10861 #define OPTION_FIX_VR4130 (OPTION_FIX_BASE + 4)
10862 #define OPTION_NO_FIX_VR4130 (OPTION_FIX_BASE + 5)
10863 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
10864 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
10865
10866 /* Miscellaneous options. */
10867 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 6)
10868 #define OPTION_TRAP (OPTION_MISC_BASE + 0)
10869 {"trap", no_argument, NULL, OPTION_TRAP},
10870 {"no-break", no_argument, NULL, OPTION_TRAP},
10871 #define OPTION_BREAK (OPTION_MISC_BASE + 1)
10872 {"break", no_argument, NULL, OPTION_BREAK},
10873 {"no-trap", no_argument, NULL, OPTION_BREAK},
10874 #define OPTION_EB (OPTION_MISC_BASE + 2)
10875 {"EB", no_argument, NULL, OPTION_EB},
10876 #define OPTION_EL (OPTION_MISC_BASE + 3)
10877 {"EL", no_argument, NULL, OPTION_EL},
10878 #define OPTION_FP32 (OPTION_MISC_BASE + 4)
10879 {"mfp32", no_argument, NULL, OPTION_FP32},
10880 #define OPTION_GP32 (OPTION_MISC_BASE + 5)
10881 {"mgp32", no_argument, NULL, OPTION_GP32},
10882 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
10883 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10884 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10885 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10886 #define OPTION_FP64 (OPTION_MISC_BASE + 8)
10887 {"mfp64", no_argument, NULL, OPTION_FP64},
10888 #define OPTION_GP64 (OPTION_MISC_BASE + 9)
10889 {"mgp64", no_argument, NULL, OPTION_GP64},
10890 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10891 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10892 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10893 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10894 #define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10895 #define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10896 {"mshared", no_argument, NULL, OPTION_MSHARED},
10897 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
10898 #define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10899 #define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10900 {"msym32", no_argument, NULL, OPTION_MSYM32},
10901 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
10902
10903 /* ELF-specific options. */
10904 #ifdef OBJ_ELF
10905 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 16)
10906 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10907 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10908 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10909 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10910 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10911 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10912 {"xgot", no_argument, NULL, OPTION_XGOT},
10913 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10914 {"mabi", required_argument, NULL, OPTION_MABI},
10915 #define OPTION_32 (OPTION_ELF_BASE + 4)
10916 {"32", no_argument, NULL, OPTION_32},
10917 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10918 {"n32", no_argument, NULL, OPTION_N32},
10919 #define OPTION_64 (OPTION_ELF_BASE + 6)
10920 {"64", no_argument, NULL, OPTION_64},
10921 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10922 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10923 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10924 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10925 #define OPTION_PDR (OPTION_ELF_BASE + 9)
10926 {"mpdr", no_argument, NULL, OPTION_PDR},
10927 #define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10928 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10929 #define OPTION_MVXWORKS_PIC (OPTION_ELF_BASE + 11)
10930 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
10931 #endif /* OBJ_ELF */
10932
10933 {NULL, no_argument, NULL, 0}
10934 };
10935 size_t md_longopts_size = sizeof (md_longopts);
10936
10937 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10938 NEW_VALUE. Warn if another value was already specified. Note:
10939 we have to defer parsing the -march and -mtune arguments in order
10940 to handle 'from-abi' correctly, since the ABI might be specified
10941 in a later argument. */
10942
10943 static void
10944 mips_set_option_string (const char **string_ptr, const char *new_value)
10945 {
10946 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10947 as_warn (_("A different %s was already specified, is now %s"),
10948 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10949 new_value);
10950
10951 *string_ptr = new_value;
10952 }
10953
10954 int
10955 md_parse_option (int c, char *arg)
10956 {
10957 switch (c)
10958 {
10959 case OPTION_CONSTRUCT_FLOATS:
10960 mips_disable_float_construction = 0;
10961 break;
10962
10963 case OPTION_NO_CONSTRUCT_FLOATS:
10964 mips_disable_float_construction = 1;
10965 break;
10966
10967 case OPTION_TRAP:
10968 mips_trap = 1;
10969 break;
10970
10971 case OPTION_BREAK:
10972 mips_trap = 0;
10973 break;
10974
10975 case OPTION_EB:
10976 target_big_endian = 1;
10977 break;
10978
10979 case OPTION_EL:
10980 target_big_endian = 0;
10981 break;
10982
10983 case 'O':
10984 if (arg && arg[1] == '0')
10985 mips_optimize = 1;
10986 else
10987 mips_optimize = 2;
10988 break;
10989
10990 case 'g':
10991 if (arg == NULL)
10992 mips_debug = 2;
10993 else
10994 mips_debug = atoi (arg);
10995 break;
10996
10997 case OPTION_MIPS1:
10998 file_mips_isa = ISA_MIPS1;
10999 break;
11000
11001 case OPTION_MIPS2:
11002 file_mips_isa = ISA_MIPS2;
11003 break;
11004
11005 case OPTION_MIPS3:
11006 file_mips_isa = ISA_MIPS3;
11007 break;
11008
11009 case OPTION_MIPS4:
11010 file_mips_isa = ISA_MIPS4;
11011 break;
11012
11013 case OPTION_MIPS5:
11014 file_mips_isa = ISA_MIPS5;
11015 break;
11016
11017 case OPTION_MIPS32:
11018 file_mips_isa = ISA_MIPS32;
11019 break;
11020
11021 case OPTION_MIPS32R2:
11022 file_mips_isa = ISA_MIPS32R2;
11023 break;
11024
11025 case OPTION_MIPS64R2:
11026 file_mips_isa = ISA_MIPS64R2;
11027 break;
11028
11029 case OPTION_MIPS64:
11030 file_mips_isa = ISA_MIPS64;
11031 break;
11032
11033 case OPTION_MTUNE:
11034 mips_set_option_string (&mips_tune_string, arg);
11035 break;
11036
11037 case OPTION_MARCH:
11038 mips_set_option_string (&mips_arch_string, arg);
11039 break;
11040
11041 case OPTION_M4650:
11042 mips_set_option_string (&mips_arch_string, "4650");
11043 mips_set_option_string (&mips_tune_string, "4650");
11044 break;
11045
11046 case OPTION_NO_M4650:
11047 break;
11048
11049 case OPTION_M4010:
11050 mips_set_option_string (&mips_arch_string, "4010");
11051 mips_set_option_string (&mips_tune_string, "4010");
11052 break;
11053
11054 case OPTION_NO_M4010:
11055 break;
11056
11057 case OPTION_M4100:
11058 mips_set_option_string (&mips_arch_string, "4100");
11059 mips_set_option_string (&mips_tune_string, "4100");
11060 break;
11061
11062 case OPTION_NO_M4100:
11063 break;
11064
11065 case OPTION_M3900:
11066 mips_set_option_string (&mips_arch_string, "3900");
11067 mips_set_option_string (&mips_tune_string, "3900");
11068 break;
11069
11070 case OPTION_NO_M3900:
11071 break;
11072
11073 case OPTION_MDMX:
11074 mips_opts.ase_mdmx = 1;
11075 break;
11076
11077 case OPTION_NO_MDMX:
11078 mips_opts.ase_mdmx = 0;
11079 break;
11080
11081 case OPTION_DSP:
11082 mips_opts.ase_dsp = 1;
11083 break;
11084
11085 case OPTION_NO_DSP:
11086 mips_opts.ase_dsp = 0;
11087 break;
11088
11089 case OPTION_MT:
11090 mips_opts.ase_mt = 1;
11091 break;
11092
11093 case OPTION_NO_MT:
11094 mips_opts.ase_mt = 0;
11095 break;
11096
11097 case OPTION_MIPS16:
11098 mips_opts.mips16 = 1;
11099 mips_no_prev_insn ();
11100 break;
11101
11102 case OPTION_NO_MIPS16:
11103 mips_opts.mips16 = 0;
11104 mips_no_prev_insn ();
11105 break;
11106
11107 case OPTION_MIPS3D:
11108 mips_opts.ase_mips3d = 1;
11109 break;
11110
11111 case OPTION_NO_MIPS3D:
11112 mips_opts.ase_mips3d = 0;
11113 break;
11114
11115 case OPTION_SMARTMIPS:
11116 mips_opts.ase_smartmips = 1;
11117 break;
11118
11119 case OPTION_NO_SMARTMIPS:
11120 mips_opts.ase_smartmips = 0;
11121 break;
11122
11123 case OPTION_FIX_VR4120:
11124 mips_fix_vr4120 = 1;
11125 break;
11126
11127 case OPTION_NO_FIX_VR4120:
11128 mips_fix_vr4120 = 0;
11129 break;
11130
11131 case OPTION_FIX_VR4130:
11132 mips_fix_vr4130 = 1;
11133 break;
11134
11135 case OPTION_NO_FIX_VR4130:
11136 mips_fix_vr4130 = 0;
11137 break;
11138
11139 case OPTION_RELAX_BRANCH:
11140 mips_relax_branch = 1;
11141 break;
11142
11143 case OPTION_NO_RELAX_BRANCH:
11144 mips_relax_branch = 0;
11145 break;
11146
11147 case OPTION_MSHARED:
11148 mips_in_shared = TRUE;
11149 break;
11150
11151 case OPTION_MNO_SHARED:
11152 mips_in_shared = FALSE;
11153 break;
11154
11155 case OPTION_MSYM32:
11156 mips_opts.sym32 = TRUE;
11157 break;
11158
11159 case OPTION_MNO_SYM32:
11160 mips_opts.sym32 = FALSE;
11161 break;
11162
11163 #ifdef OBJ_ELF
11164 /* When generating ELF code, we permit -KPIC and -call_shared to
11165 select SVR4_PIC, and -non_shared to select no PIC. This is
11166 intended to be compatible with Irix 5. */
11167 case OPTION_CALL_SHARED:
11168 if (!IS_ELF)
11169 {
11170 as_bad (_("-call_shared is supported only for ELF format"));
11171 return 0;
11172 }
11173 mips_pic = SVR4_PIC;
11174 mips_abicalls = TRUE;
11175 break;
11176
11177 case OPTION_NON_SHARED:
11178 if (!IS_ELF)
11179 {
11180 as_bad (_("-non_shared is supported only for ELF format"));
11181 return 0;
11182 }
11183 mips_pic = NO_PIC;
11184 mips_abicalls = FALSE;
11185 break;
11186
11187 /* The -xgot option tells the assembler to use 32 bit offsets
11188 when accessing the got in SVR4_PIC mode. It is for Irix
11189 compatibility. */
11190 case OPTION_XGOT:
11191 mips_big_got = 1;
11192 break;
11193 #endif /* OBJ_ELF */
11194
11195 case 'G':
11196 g_switch_value = atoi (arg);
11197 g_switch_seen = 1;
11198 break;
11199
11200 #ifdef OBJ_ELF
11201 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
11202 and -mabi=64. */
11203 case OPTION_32:
11204 if (!IS_ELF)
11205 {
11206 as_bad (_("-32 is supported for ELF format only"));
11207 return 0;
11208 }
11209 mips_abi = O32_ABI;
11210 break;
11211
11212 case OPTION_N32:
11213 if (!IS_ELF)
11214 {
11215 as_bad (_("-n32 is supported for ELF format only"));
11216 return 0;
11217 }
11218 mips_abi = N32_ABI;
11219 break;
11220
11221 case OPTION_64:
11222 if (!IS_ELF)
11223 {
11224 as_bad (_("-64 is supported for ELF format only"));
11225 return 0;
11226 }
11227 mips_abi = N64_ABI;
11228 if (!support_64bit_objects())
11229 as_fatal (_("No compiled in support for 64 bit object file format"));
11230 break;
11231 #endif /* OBJ_ELF */
11232
11233 case OPTION_GP32:
11234 file_mips_gp32 = 1;
11235 break;
11236
11237 case OPTION_GP64:
11238 file_mips_gp32 = 0;
11239 break;
11240
11241 case OPTION_FP32:
11242 file_mips_fp32 = 1;
11243 break;
11244
11245 case OPTION_FP64:
11246 file_mips_fp32 = 0;
11247 break;
11248
11249 #ifdef OBJ_ELF
11250 case OPTION_MABI:
11251 if (!IS_ELF)
11252 {
11253 as_bad (_("-mabi is supported for ELF format only"));
11254 return 0;
11255 }
11256 if (strcmp (arg, "32") == 0)
11257 mips_abi = O32_ABI;
11258 else if (strcmp (arg, "o64") == 0)
11259 mips_abi = O64_ABI;
11260 else if (strcmp (arg, "n32") == 0)
11261 mips_abi = N32_ABI;
11262 else if (strcmp (arg, "64") == 0)
11263 {
11264 mips_abi = N64_ABI;
11265 if (! support_64bit_objects())
11266 as_fatal (_("No compiled in support for 64 bit object file "
11267 "format"));
11268 }
11269 else if (strcmp (arg, "eabi") == 0)
11270 mips_abi = EABI_ABI;
11271 else
11272 {
11273 as_fatal (_("invalid abi -mabi=%s"), arg);
11274 return 0;
11275 }
11276 break;
11277 #endif /* OBJ_ELF */
11278
11279 case OPTION_M7000_HILO_FIX:
11280 mips_7000_hilo_fix = TRUE;
11281 break;
11282
11283 case OPTION_MNO_7000_HILO_FIX:
11284 mips_7000_hilo_fix = FALSE;
11285 break;
11286
11287 #ifdef OBJ_ELF
11288 case OPTION_MDEBUG:
11289 mips_flag_mdebug = TRUE;
11290 break;
11291
11292 case OPTION_NO_MDEBUG:
11293 mips_flag_mdebug = FALSE;
11294 break;
11295
11296 case OPTION_PDR:
11297 mips_flag_pdr = TRUE;
11298 break;
11299
11300 case OPTION_NO_PDR:
11301 mips_flag_pdr = FALSE;
11302 break;
11303
11304 case OPTION_MVXWORKS_PIC:
11305 mips_pic = VXWORKS_PIC;
11306 break;
11307 #endif /* OBJ_ELF */
11308
11309 default:
11310 return 0;
11311 }
11312
11313 return 1;
11314 }
11315 \f
11316 /* Set up globals to generate code for the ISA or processor
11317 described by INFO. */
11318
11319 static void
11320 mips_set_architecture (const struct mips_cpu_info *info)
11321 {
11322 if (info != 0)
11323 {
11324 file_mips_arch = info->cpu;
11325 mips_opts.arch = info->cpu;
11326 mips_opts.isa = info->isa;
11327 }
11328 }
11329
11330
11331 /* Likewise for tuning. */
11332
11333 static void
11334 mips_set_tune (const struct mips_cpu_info *info)
11335 {
11336 if (info != 0)
11337 mips_tune = info->cpu;
11338 }
11339
11340
11341 void
11342 mips_after_parse_args (void)
11343 {
11344 const struct mips_cpu_info *arch_info = 0;
11345 const struct mips_cpu_info *tune_info = 0;
11346
11347 /* GP relative stuff not working for PE */
11348 if (strncmp (TARGET_OS, "pe", 2) == 0)
11349 {
11350 if (g_switch_seen && g_switch_value != 0)
11351 as_bad (_("-G not supported in this configuration."));
11352 g_switch_value = 0;
11353 }
11354
11355 if (mips_abi == NO_ABI)
11356 mips_abi = MIPS_DEFAULT_ABI;
11357
11358 /* The following code determines the architecture and register size.
11359 Similar code was added to GCC 3.3 (see override_options() in
11360 config/mips/mips.c). The GAS and GCC code should be kept in sync
11361 as much as possible. */
11362
11363 if (mips_arch_string != 0)
11364 arch_info = mips_parse_cpu ("-march", mips_arch_string);
11365
11366 if (file_mips_isa != ISA_UNKNOWN)
11367 {
11368 /* Handle -mipsN. At this point, file_mips_isa contains the
11369 ISA level specified by -mipsN, while arch_info->isa contains
11370 the -march selection (if any). */
11371 if (arch_info != 0)
11372 {
11373 /* -march takes precedence over -mipsN, since it is more descriptive.
11374 There's no harm in specifying both as long as the ISA levels
11375 are the same. */
11376 if (file_mips_isa != arch_info->isa)
11377 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11378 mips_cpu_info_from_isa (file_mips_isa)->name,
11379 mips_cpu_info_from_isa (arch_info->isa)->name);
11380 }
11381 else
11382 arch_info = mips_cpu_info_from_isa (file_mips_isa);
11383 }
11384
11385 if (arch_info == 0)
11386 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
11387
11388 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
11389 as_bad ("-march=%s is not compatible with the selected ABI",
11390 arch_info->name);
11391
11392 mips_set_architecture (arch_info);
11393
11394 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
11395 if (mips_tune_string != 0)
11396 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
11397
11398 if (tune_info == 0)
11399 mips_set_tune (arch_info);
11400 else
11401 mips_set_tune (tune_info);
11402
11403 if (file_mips_gp32 >= 0)
11404 {
11405 /* The user specified the size of the integer registers. Make sure
11406 it agrees with the ABI and ISA. */
11407 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11408 as_bad (_("-mgp64 used with a 32-bit processor"));
11409 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11410 as_bad (_("-mgp32 used with a 64-bit ABI"));
11411 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11412 as_bad (_("-mgp64 used with a 32-bit ABI"));
11413 }
11414 else
11415 {
11416 /* Infer the integer register size from the ABI and processor.
11417 Restrict ourselves to 32-bit registers if that's all the
11418 processor has, or if the ABI cannot handle 64-bit registers. */
11419 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11420 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
11421 }
11422
11423 switch (file_mips_fp32)
11424 {
11425 default:
11426 case -1:
11427 /* No user specified float register size.
11428 ??? GAS treats single-float processors as though they had 64-bit
11429 float registers (although it complains when double-precision
11430 instructions are used). As things stand, saying they have 32-bit
11431 registers would lead to spurious "register must be even" messages.
11432 So here we assume float registers are never smaller than the
11433 integer ones. */
11434 if (file_mips_gp32 == 0)
11435 /* 64-bit integer registers implies 64-bit float registers. */
11436 file_mips_fp32 = 0;
11437 else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
11438 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
11439 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
11440 file_mips_fp32 = 0;
11441 else
11442 /* 32-bit float registers. */
11443 file_mips_fp32 = 1;
11444 break;
11445
11446 /* The user specified the size of the float registers. Check if it
11447 agrees with the ABI and ISA. */
11448 case 0:
11449 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
11450 as_bad (_("-mfp64 used with a 32-bit fpu"));
11451 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
11452 && !ISA_HAS_MXHC1 (mips_opts.isa))
11453 as_warn (_("-mfp64 used with a 32-bit ABI"));
11454 break;
11455 case 1:
11456 if (ABI_NEEDS_64BIT_REGS (mips_abi))
11457 as_warn (_("-mfp32 used with a 64-bit ABI"));
11458 break;
11459 }
11460
11461 /* End of GCC-shared inference code. */
11462
11463 /* This flag is set when we have a 64-bit capable CPU but use only
11464 32-bit wide registers. Note that EABI does not use it. */
11465 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11466 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11467 || mips_abi == O32_ABI))
11468 mips_32bitmode = 1;
11469
11470 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11471 as_bad (_("trap exception not supported at ISA 1"));
11472
11473 /* If the selected architecture includes support for ASEs, enable
11474 generation of code for them. */
11475 if (mips_opts.mips16 == -1)
11476 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
11477 if (mips_opts.ase_mips3d == -1)
11478 mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
11479 && file_mips_fp32 == 0) ? 1 : 0;
11480 if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
11481 as_bad (_("-mfp32 used with -mips3d"));
11482
11483 if (mips_opts.ase_mdmx == -1)
11484 mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
11485 && file_mips_fp32 == 0) ? 1 : 0;
11486 if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
11487 as_bad (_("-mfp32 used with -mdmx"));
11488
11489 if (mips_opts.ase_smartmips == -1)
11490 mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
11491 if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
11492 as_warn ("%s ISA does not support SmartMIPS",
11493 mips_cpu_info_from_isa (mips_opts.isa)->name);
11494
11495 if (mips_opts.ase_dsp == -1)
11496 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
11497 if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
11498 as_warn ("%s ISA does not support DSP ASE",
11499 mips_cpu_info_from_isa (mips_opts.isa)->name);
11500
11501 if (mips_opts.ase_mt == -1)
11502 mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
11503 if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
11504 as_warn ("%s ISA does not support MT ASE",
11505 mips_cpu_info_from_isa (mips_opts.isa)->name);
11506
11507 file_mips_isa = mips_opts.isa;
11508 file_ase_mips16 = mips_opts.mips16;
11509 file_ase_mips3d = mips_opts.ase_mips3d;
11510 file_ase_mdmx = mips_opts.ase_mdmx;
11511 file_ase_smartmips = mips_opts.ase_smartmips;
11512 file_ase_dsp = mips_opts.ase_dsp;
11513 file_ase_mt = mips_opts.ase_mt;
11514 mips_opts.gp32 = file_mips_gp32;
11515 mips_opts.fp32 = file_mips_fp32;
11516
11517 if (mips_flag_mdebug < 0)
11518 {
11519 #ifdef OBJ_MAYBE_ECOFF
11520 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11521 mips_flag_mdebug = 1;
11522 else
11523 #endif /* OBJ_MAYBE_ECOFF */
11524 mips_flag_mdebug = 0;
11525 }
11526 }
11527 \f
11528 void
11529 mips_init_after_args (void)
11530 {
11531 /* initialize opcodes */
11532 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
11533 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
11534 }
11535
11536 long
11537 md_pcrel_from (fixS *fixP)
11538 {
11539 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11540 switch (fixP->fx_r_type)
11541 {
11542 case BFD_RELOC_16_PCREL_S2:
11543 case BFD_RELOC_MIPS_JMP:
11544 /* Return the address of the delay slot. */
11545 return addr + 4;
11546 default:
11547 return addr;
11548 }
11549 }
11550
11551 /* This is called before the symbol table is processed. In order to
11552 work with gcc when using mips-tfile, we must keep all local labels.
11553 However, in other cases, we want to discard them. If we were
11554 called with -g, but we didn't see any debugging information, it may
11555 mean that gcc is smuggling debugging information through to
11556 mips-tfile, in which case we must generate all local labels. */
11557
11558 void
11559 mips_frob_file_before_adjust (void)
11560 {
11561 #ifndef NO_ECOFF_DEBUGGING
11562 if (ECOFF_DEBUGGING
11563 && mips_debug != 0
11564 && ! ecoff_debugging_seen)
11565 flag_keep_locals = 1;
11566 #endif
11567 }
11568
11569 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
11570 the corresponding LO16 reloc. This is called before md_apply_fix and
11571 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
11572 relocation operators.
11573
11574 For our purposes, a %lo() expression matches a %got() or %hi()
11575 expression if:
11576
11577 (a) it refers to the same symbol; and
11578 (b) the offset applied in the %lo() expression is no lower than
11579 the offset applied in the %got() or %hi().
11580
11581 (b) allows us to cope with code like:
11582
11583 lui $4,%hi(foo)
11584 lh $4,%lo(foo+2)($4)
11585
11586 ...which is legal on RELA targets, and has a well-defined behaviour
11587 if the user knows that adding 2 to "foo" will not induce a carry to
11588 the high 16 bits.
11589
11590 When several %lo()s match a particular %got() or %hi(), we use the
11591 following rules to distinguish them:
11592
11593 (1) %lo()s with smaller offsets are a better match than %lo()s with
11594 higher offsets.
11595
11596 (2) %lo()s with no matching %got() or %hi() are better than those
11597 that already have a matching %got() or %hi().
11598
11599 (3) later %lo()s are better than earlier %lo()s.
11600
11601 These rules are applied in order.
11602
11603 (1) means, among other things, that %lo()s with identical offsets are
11604 chosen if they exist.
11605
11606 (2) means that we won't associate several high-part relocations with
11607 the same low-part relocation unless there's no alternative. Having
11608 several high parts for the same low part is a GNU extension; this rule
11609 allows careful users to avoid it.
11610
11611 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
11612 with the last high-part relocation being at the front of the list.
11613 It therefore makes sense to choose the last matching low-part
11614 relocation, all other things being equal. It's also easier
11615 to code that way. */
11616
11617 void
11618 mips_frob_file (void)
11619 {
11620 struct mips_hi_fixup *l;
11621
11622 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11623 {
11624 segment_info_type *seginfo;
11625 bfd_boolean matched_lo_p;
11626 fixS **hi_pos, **lo_pos, **pos;
11627
11628 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
11629
11630 /* If a GOT16 relocation turns out to be against a global symbol,
11631 there isn't supposed to be a matching LO. */
11632 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11633 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11634 continue;
11635
11636 /* Check quickly whether the next fixup happens to be a matching %lo. */
11637 if (fixup_has_matching_lo_p (l->fixp))
11638 continue;
11639
11640 seginfo = seg_info (l->seg);
11641
11642 /* Set HI_POS to the position of this relocation in the chain.
11643 Set LO_POS to the position of the chosen low-part relocation.
11644 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
11645 relocation that matches an immediately-preceding high-part
11646 relocation. */
11647 hi_pos = NULL;
11648 lo_pos = NULL;
11649 matched_lo_p = FALSE;
11650 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
11651 {
11652 if (*pos == l->fixp)
11653 hi_pos = pos;
11654
11655 if (((*pos)->fx_r_type == BFD_RELOC_LO16
11656 || (*pos)->fx_r_type == BFD_RELOC_MIPS16_LO16)
11657 && (*pos)->fx_addsy == l->fixp->fx_addsy
11658 && (*pos)->fx_offset >= l->fixp->fx_offset
11659 && (lo_pos == NULL
11660 || (*pos)->fx_offset < (*lo_pos)->fx_offset
11661 || (!matched_lo_p
11662 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
11663 lo_pos = pos;
11664
11665 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
11666 && fixup_has_matching_lo_p (*pos));
11667 }
11668
11669 /* If we found a match, remove the high-part relocation from its
11670 current position and insert it before the low-part relocation.
11671 Make the offsets match so that fixup_has_matching_lo_p()
11672 will return true.
11673
11674 We don't warn about unmatched high-part relocations since some
11675 versions of gcc have been known to emit dead "lui ...%hi(...)"
11676 instructions. */
11677 if (lo_pos != NULL)
11678 {
11679 l->fixp->fx_offset = (*lo_pos)->fx_offset;
11680 if (l->fixp->fx_next != *lo_pos)
11681 {
11682 *hi_pos = l->fixp->fx_next;
11683 l->fixp->fx_next = *lo_pos;
11684 *lo_pos = l->fixp;
11685 }
11686 }
11687 }
11688 }
11689
11690 /* We may have combined relocations without symbols in the N32/N64 ABI.
11691 We have to prevent gas from dropping them. */
11692
11693 int
11694 mips_force_relocation (fixS *fixp)
11695 {
11696 if (generic_force_reloc (fixp))
11697 return 1;
11698
11699 if (HAVE_NEWABI
11700 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11701 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11702 || fixp->fx_r_type == BFD_RELOC_HI16_S
11703 || fixp->fx_r_type == BFD_RELOC_LO16))
11704 return 1;
11705
11706 return 0;
11707 }
11708
11709 /* Apply a fixup to the object file. */
11710
11711 void
11712 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
11713 {
11714 bfd_byte *buf;
11715 long insn;
11716 reloc_howto_type *howto;
11717
11718 /* We ignore generic BFD relocations we don't know about. */
11719 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11720 if (! howto)
11721 return;
11722
11723 assert (fixP->fx_size == 4
11724 || fixP->fx_r_type == BFD_RELOC_16
11725 || fixP->fx_r_type == BFD_RELOC_64
11726 || fixP->fx_r_type == BFD_RELOC_CTOR
11727 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11728 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11729 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11730
11731 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11732
11733 assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2);
11734
11735 /* Don't treat parts of a composite relocation as done. There are two
11736 reasons for this:
11737
11738 (1) The second and third parts will be against 0 (RSS_UNDEF) but
11739 should nevertheless be emitted if the first part is.
11740
11741 (2) In normal usage, composite relocations are never assembly-time
11742 constants. The easiest way of dealing with the pathological
11743 exceptions is to generate a relocation against STN_UNDEF and
11744 leave everything up to the linker. */
11745 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
11746 fixP->fx_done = 1;
11747
11748 switch (fixP->fx_r_type)
11749 {
11750 case BFD_RELOC_MIPS_TLS_GD:
11751 case BFD_RELOC_MIPS_TLS_LDM:
11752 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
11753 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
11754 case BFD_RELOC_MIPS_TLS_GOTTPREL:
11755 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
11756 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
11757 S_SET_THREAD_LOCAL (fixP->fx_addsy);
11758 /* fall through */
11759
11760 case BFD_RELOC_MIPS_JMP:
11761 case BFD_RELOC_MIPS_SHIFT5:
11762 case BFD_RELOC_MIPS_SHIFT6:
11763 case BFD_RELOC_MIPS_GOT_DISP:
11764 case BFD_RELOC_MIPS_GOT_PAGE:
11765 case BFD_RELOC_MIPS_GOT_OFST:
11766 case BFD_RELOC_MIPS_SUB:
11767 case BFD_RELOC_MIPS_INSERT_A:
11768 case BFD_RELOC_MIPS_INSERT_B:
11769 case BFD_RELOC_MIPS_DELETE:
11770 case BFD_RELOC_MIPS_HIGHEST:
11771 case BFD_RELOC_MIPS_HIGHER:
11772 case BFD_RELOC_MIPS_SCN_DISP:
11773 case BFD_RELOC_MIPS_REL16:
11774 case BFD_RELOC_MIPS_RELGOT:
11775 case BFD_RELOC_MIPS_JALR:
11776 case BFD_RELOC_HI16:
11777 case BFD_RELOC_HI16_S:
11778 case BFD_RELOC_GPREL16:
11779 case BFD_RELOC_MIPS_LITERAL:
11780 case BFD_RELOC_MIPS_CALL16:
11781 case BFD_RELOC_MIPS_GOT16:
11782 case BFD_RELOC_GPREL32:
11783 case BFD_RELOC_MIPS_GOT_HI16:
11784 case BFD_RELOC_MIPS_GOT_LO16:
11785 case BFD_RELOC_MIPS_CALL_HI16:
11786 case BFD_RELOC_MIPS_CALL_LO16:
11787 case BFD_RELOC_MIPS16_GPREL:
11788 case BFD_RELOC_MIPS16_HI16:
11789 case BFD_RELOC_MIPS16_HI16_S:
11790 case BFD_RELOC_MIPS16_JMP:
11791 /* Nothing needed to do. The value comes from the reloc entry. */
11792 break;
11793
11794 case BFD_RELOC_64:
11795 /* This is handled like BFD_RELOC_32, but we output a sign
11796 extended value if we are only 32 bits. */
11797 if (fixP->fx_done)
11798 {
11799 if (8 <= sizeof (valueT))
11800 md_number_to_chars ((char *) buf, *valP, 8);
11801 else
11802 {
11803 valueT hiv;
11804
11805 if ((*valP & 0x80000000) != 0)
11806 hiv = 0xffffffff;
11807 else
11808 hiv = 0;
11809 md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
11810 *valP, 4);
11811 md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
11812 hiv, 4);
11813 }
11814 }
11815 break;
11816
11817 case BFD_RELOC_RVA:
11818 case BFD_RELOC_32:
11819 case BFD_RELOC_16:
11820 /* If we are deleting this reloc entry, we must fill in the
11821 value now. This can happen if we have a .word which is not
11822 resolved when it appears but is later defined. */
11823 if (fixP->fx_done)
11824 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
11825 break;
11826
11827 case BFD_RELOC_LO16:
11828 case BFD_RELOC_MIPS16_LO16:
11829 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11830 may be safe to remove, but if so it's not obvious. */
11831 /* When handling an embedded PIC switch statement, we can wind
11832 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11833 if (fixP->fx_done)
11834 {
11835 if (*valP + 0x8000 > 0xffff)
11836 as_bad_where (fixP->fx_file, fixP->fx_line,
11837 _("relocation overflow"));
11838 if (target_big_endian)
11839 buf += 2;
11840 md_number_to_chars ((char *) buf, *valP, 2);
11841 }
11842 break;
11843
11844 case BFD_RELOC_16_PCREL_S2:
11845 if ((*valP & 0x3) != 0)
11846 as_bad_where (fixP->fx_file, fixP->fx_line,
11847 _("Branch to misaligned address (%lx)"), (long) *valP);
11848
11849 /* We need to save the bits in the instruction since fixup_segment()
11850 might be deleting the relocation entry (i.e., a branch within
11851 the current segment). */
11852 if (! fixP->fx_done)
11853 break;
11854
11855 /* Update old instruction data. */
11856 if (target_big_endian)
11857 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11858 else
11859 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11860
11861 if (*valP + 0x20000 <= 0x3ffff)
11862 {
11863 insn |= (*valP >> 2) & 0xffff;
11864 md_number_to_chars ((char *) buf, insn, 4);
11865 }
11866 else if (mips_pic == NO_PIC
11867 && fixP->fx_done
11868 && fixP->fx_frag->fr_address >= text_section->vma
11869 && (fixP->fx_frag->fr_address
11870 < text_section->vma + bfd_get_section_size (text_section))
11871 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11872 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11873 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11874 {
11875 /* The branch offset is too large. If this is an
11876 unconditional branch, and we are not generating PIC code,
11877 we can convert it to an absolute jump instruction. */
11878 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11879 insn = 0x0c000000; /* jal */
11880 else
11881 insn = 0x08000000; /* j */
11882 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11883 fixP->fx_done = 0;
11884 fixP->fx_addsy = section_symbol (text_section);
11885 *valP += md_pcrel_from (fixP);
11886 md_number_to_chars ((char *) buf, insn, 4);
11887 }
11888 else
11889 {
11890 /* If we got here, we have branch-relaxation disabled,
11891 and there's nothing we can do to fix this instruction
11892 without turning it into a longer sequence. */
11893 as_bad_where (fixP->fx_file, fixP->fx_line,
11894 _("Branch out of range"));
11895 }
11896 break;
11897
11898 case BFD_RELOC_VTABLE_INHERIT:
11899 fixP->fx_done = 0;
11900 if (fixP->fx_addsy
11901 && !S_IS_DEFINED (fixP->fx_addsy)
11902 && !S_IS_WEAK (fixP->fx_addsy))
11903 S_SET_WEAK (fixP->fx_addsy);
11904 break;
11905
11906 case BFD_RELOC_VTABLE_ENTRY:
11907 fixP->fx_done = 0;
11908 break;
11909
11910 default:
11911 internalError ();
11912 }
11913
11914 /* Remember value for tc_gen_reloc. */
11915 fixP->fx_addnumber = *valP;
11916 }
11917
11918 static symbolS *
11919 get_symbol (void)
11920 {
11921 int c;
11922 char *name;
11923 symbolS *p;
11924
11925 name = input_line_pointer;
11926 c = get_symbol_end ();
11927 p = (symbolS *) symbol_find_or_make (name);
11928 *input_line_pointer = c;
11929 return p;
11930 }
11931
11932 /* Align the current frag to a given power of two. The MIPS assembler
11933 also automatically adjusts any preceding label. */
11934
11935 static void
11936 mips_align (int to, int fill, symbolS *label)
11937 {
11938 mips_emit_delays ();
11939 frag_align (to, fill, 0);
11940 record_alignment (now_seg, to);
11941 if (label != NULL)
11942 {
11943 assert (S_GET_SEGMENT (label) == now_seg);
11944 symbol_set_frag (label, frag_now);
11945 S_SET_VALUE (label, (valueT) frag_now_fix ());
11946 }
11947 }
11948
11949 /* Align to a given power of two. .align 0 turns off the automatic
11950 alignment used by the data creating pseudo-ops. */
11951
11952 static void
11953 s_align (int x ATTRIBUTE_UNUSED)
11954 {
11955 int temp;
11956 long temp_fill;
11957 long max_alignment = 15;
11958
11959 /* o Note that the assembler pulls down any immediately preceding label
11960 to the aligned address.
11961 o It's not documented but auto alignment is reinstated by
11962 a .align pseudo instruction.
11963 o Note also that after auto alignment is turned off the mips assembler
11964 issues an error on attempt to assemble an improperly aligned data item.
11965 We don't. */
11966
11967 temp = get_absolute_expression ();
11968 if (temp > max_alignment)
11969 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11970 else if (temp < 0)
11971 {
11972 as_warn (_("Alignment negative: 0 assumed."));
11973 temp = 0;
11974 }
11975 if (*input_line_pointer == ',')
11976 {
11977 ++input_line_pointer;
11978 temp_fill = get_absolute_expression ();
11979 }
11980 else
11981 temp_fill = 0;
11982 if (temp)
11983 {
11984 segment_info_type *si = seg_info (now_seg);
11985 struct insn_label_list *l = si->label_list;
11986 /* Auto alignment should be switched on by next section change. */
11987 auto_align = 1;
11988 mips_align (temp, (int) temp_fill, l != NULL ? l->label : NULL);
11989 }
11990 else
11991 {
11992 auto_align = 0;
11993 }
11994
11995 demand_empty_rest_of_line ();
11996 }
11997
11998 static void
11999 s_change_sec (int sec)
12000 {
12001 segT seg;
12002
12003 #ifdef OBJ_ELF
12004 /* The ELF backend needs to know that we are changing sections, so
12005 that .previous works correctly. We could do something like check
12006 for an obj_section_change_hook macro, but that might be confusing
12007 as it would not be appropriate to use it in the section changing
12008 functions in read.c, since obj-elf.c intercepts those. FIXME:
12009 This should be cleaner, somehow. */
12010 if (IS_ELF)
12011 obj_elf_section_change_hook ();
12012 #endif
12013
12014 mips_emit_delays ();
12015 switch (sec)
12016 {
12017 case 't':
12018 s_text (0);
12019 break;
12020 case 'd':
12021 s_data (0);
12022 break;
12023 case 'b':
12024 subseg_set (bss_section, (subsegT) get_absolute_expression ());
12025 demand_empty_rest_of_line ();
12026 break;
12027
12028 case 'r':
12029 seg = subseg_new (RDATA_SECTION_NAME,
12030 (subsegT) get_absolute_expression ());
12031 if (IS_ELF)
12032 {
12033 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
12034 | SEC_READONLY | SEC_RELOC
12035 | SEC_DATA));
12036 if (strcmp (TARGET_OS, "elf") != 0)
12037 record_alignment (seg, 4);
12038 }
12039 demand_empty_rest_of_line ();
12040 break;
12041
12042 case 's':
12043 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
12044 if (IS_ELF)
12045 {
12046 bfd_set_section_flags (stdoutput, seg,
12047 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
12048 if (strcmp (TARGET_OS, "elf") != 0)
12049 record_alignment (seg, 4);
12050 }
12051 demand_empty_rest_of_line ();
12052 break;
12053 }
12054
12055 auto_align = 1;
12056 }
12057
12058 void
12059 s_change_section (int ignore ATTRIBUTE_UNUSED)
12060 {
12061 #ifdef OBJ_ELF
12062 char *section_name;
12063 char c;
12064 char next_c = 0;
12065 int section_type;
12066 int section_flag;
12067 int section_entry_size;
12068 int section_alignment;
12069
12070 if (!IS_ELF)
12071 return;
12072
12073 section_name = input_line_pointer;
12074 c = get_symbol_end ();
12075 if (c)
12076 next_c = *(input_line_pointer + 1);
12077
12078 /* Do we have .section Name<,"flags">? */
12079 if (c != ',' || (c == ',' && next_c == '"'))
12080 {
12081 /* just after name is now '\0'. */
12082 *input_line_pointer = c;
12083 input_line_pointer = section_name;
12084 obj_elf_section (ignore);
12085 return;
12086 }
12087 input_line_pointer++;
12088
12089 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
12090 if (c == ',')
12091 section_type = get_absolute_expression ();
12092 else
12093 section_type = 0;
12094 if (*input_line_pointer++ == ',')
12095 section_flag = get_absolute_expression ();
12096 else
12097 section_flag = 0;
12098 if (*input_line_pointer++ == ',')
12099 section_entry_size = get_absolute_expression ();
12100 else
12101 section_entry_size = 0;
12102 if (*input_line_pointer++ == ',')
12103 section_alignment = get_absolute_expression ();
12104 else
12105 section_alignment = 0;
12106
12107 section_name = xstrdup (section_name);
12108
12109 /* When using the generic form of .section (as implemented by obj-elf.c),
12110 there's no way to set the section type to SHT_MIPS_DWARF. Users have
12111 traditionally had to fall back on the more common @progbits instead.
12112
12113 There's nothing really harmful in this, since bfd will correct
12114 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
12115 means that, for backwards compatibility, the special_section entries
12116 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
12117
12118 Even so, we shouldn't force users of the MIPS .section syntax to
12119 incorrectly label the sections as SHT_PROGBITS. The best compromise
12120 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
12121 generic type-checking code. */
12122 if (section_type == SHT_MIPS_DWARF)
12123 section_type = SHT_PROGBITS;
12124
12125 obj_elf_change_section (section_name, section_type, section_flag,
12126 section_entry_size, 0, 0, 0);
12127
12128 if (now_seg->name != section_name)
12129 free (section_name);
12130 #endif /* OBJ_ELF */
12131 }
12132
12133 void
12134 mips_enable_auto_align (void)
12135 {
12136 auto_align = 1;
12137 }
12138
12139 static void
12140 s_cons (int log_size)
12141 {
12142 segment_info_type *si = seg_info (now_seg);
12143 struct insn_label_list *l = si->label_list;
12144 symbolS *label;
12145
12146 label = l != NULL ? l->label : NULL;
12147 mips_emit_delays ();
12148 if (log_size > 0 && auto_align)
12149 mips_align (log_size, 0, label);
12150 mips_clear_insn_labels ();
12151 cons (1 << log_size);
12152 }
12153
12154 static void
12155 s_float_cons (int type)
12156 {
12157 segment_info_type *si = seg_info (now_seg);
12158 struct insn_label_list *l = si->label_list;
12159 symbolS *label;
12160
12161 label = l != NULL ? l->label : NULL;
12162
12163 mips_emit_delays ();
12164
12165 if (auto_align)
12166 {
12167 if (type == 'd')
12168 mips_align (3, 0, label);
12169 else
12170 mips_align (2, 0, label);
12171 }
12172
12173 mips_clear_insn_labels ();
12174
12175 float_cons (type);
12176 }
12177
12178 /* Handle .globl. We need to override it because on Irix 5 you are
12179 permitted to say
12180 .globl foo .text
12181 where foo is an undefined symbol, to mean that foo should be
12182 considered to be the address of a function. */
12183
12184 static void
12185 s_mips_globl (int x ATTRIBUTE_UNUSED)
12186 {
12187 char *name;
12188 int c;
12189 symbolS *symbolP;
12190 flagword flag;
12191
12192 do
12193 {
12194 name = input_line_pointer;
12195 c = get_symbol_end ();
12196 symbolP = symbol_find_or_make (name);
12197 S_SET_EXTERNAL (symbolP);
12198
12199 *input_line_pointer = c;
12200 SKIP_WHITESPACE ();
12201
12202 /* On Irix 5, every global symbol that is not explicitly labelled as
12203 being a function is apparently labelled as being an object. */
12204 flag = BSF_OBJECT;
12205
12206 if (!is_end_of_line[(unsigned char) *input_line_pointer]
12207 && (*input_line_pointer != ','))
12208 {
12209 char *secname;
12210 asection *sec;
12211
12212 secname = input_line_pointer;
12213 c = get_symbol_end ();
12214 sec = bfd_get_section_by_name (stdoutput, secname);
12215 if (sec == NULL)
12216 as_bad (_("%s: no such section"), secname);
12217 *input_line_pointer = c;
12218
12219 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12220 flag = BSF_FUNCTION;
12221 }
12222
12223 symbol_get_bfdsym (symbolP)->flags |= flag;
12224
12225 c = *input_line_pointer;
12226 if (c == ',')
12227 {
12228 input_line_pointer++;
12229 SKIP_WHITESPACE ();
12230 if (is_end_of_line[(unsigned char) *input_line_pointer])
12231 c = '\n';
12232 }
12233 }
12234 while (c == ',');
12235
12236 demand_empty_rest_of_line ();
12237 }
12238
12239 static void
12240 s_option (int x ATTRIBUTE_UNUSED)
12241 {
12242 char *opt;
12243 char c;
12244
12245 opt = input_line_pointer;
12246 c = get_symbol_end ();
12247
12248 if (*opt == 'O')
12249 {
12250 /* FIXME: What does this mean? */
12251 }
12252 else if (strncmp (opt, "pic", 3) == 0)
12253 {
12254 int i;
12255
12256 i = atoi (opt + 3);
12257 if (i == 0)
12258 mips_pic = NO_PIC;
12259 else if (i == 2)
12260 {
12261 mips_pic = SVR4_PIC;
12262 mips_abicalls = TRUE;
12263 }
12264 else
12265 as_bad (_(".option pic%d not supported"), i);
12266
12267 if (mips_pic == SVR4_PIC)
12268 {
12269 if (g_switch_seen && g_switch_value != 0)
12270 as_warn (_("-G may not be used with SVR4 PIC code"));
12271 g_switch_value = 0;
12272 bfd_set_gp_size (stdoutput, 0);
12273 }
12274 }
12275 else
12276 as_warn (_("Unrecognized option \"%s\""), opt);
12277
12278 *input_line_pointer = c;
12279 demand_empty_rest_of_line ();
12280 }
12281
12282 /* This structure is used to hold a stack of .set values. */
12283
12284 struct mips_option_stack
12285 {
12286 struct mips_option_stack *next;
12287 struct mips_set_options options;
12288 };
12289
12290 static struct mips_option_stack *mips_opts_stack;
12291
12292 /* Handle the .set pseudo-op. */
12293
12294 static void
12295 s_mipsset (int x ATTRIBUTE_UNUSED)
12296 {
12297 char *name = input_line_pointer, ch;
12298
12299 while (!is_end_of_line[(unsigned char) *input_line_pointer])
12300 ++input_line_pointer;
12301 ch = *input_line_pointer;
12302 *input_line_pointer = '\0';
12303
12304 if (strcmp (name, "reorder") == 0)
12305 {
12306 if (mips_opts.noreorder)
12307 end_noreorder ();
12308 }
12309 else if (strcmp (name, "noreorder") == 0)
12310 {
12311 if (!mips_opts.noreorder)
12312 start_noreorder ();
12313 }
12314 else if (strcmp (name, "at") == 0)
12315 {
12316 mips_opts.noat = 0;
12317 }
12318 else if (strcmp (name, "noat") == 0)
12319 {
12320 mips_opts.noat = 1;
12321 }
12322 else if (strcmp (name, "macro") == 0)
12323 {
12324 mips_opts.warn_about_macros = 0;
12325 }
12326 else if (strcmp (name, "nomacro") == 0)
12327 {
12328 if (mips_opts.noreorder == 0)
12329 as_bad (_("`noreorder' must be set before `nomacro'"));
12330 mips_opts.warn_about_macros = 1;
12331 }
12332 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12333 {
12334 mips_opts.nomove = 0;
12335 }
12336 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12337 {
12338 mips_opts.nomove = 1;
12339 }
12340 else if (strcmp (name, "bopt") == 0)
12341 {
12342 mips_opts.nobopt = 0;
12343 }
12344 else if (strcmp (name, "nobopt") == 0)
12345 {
12346 mips_opts.nobopt = 1;
12347 }
12348 else if (strcmp (name, "gp=default") == 0)
12349 mips_opts.gp32 = file_mips_gp32;
12350 else if (strcmp (name, "gp=32") == 0)
12351 mips_opts.gp32 = 1;
12352 else if (strcmp (name, "gp=64") == 0)
12353 {
12354 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
12355 as_warn ("%s isa does not support 64-bit registers",
12356 mips_cpu_info_from_isa (mips_opts.isa)->name);
12357 mips_opts.gp32 = 0;
12358 }
12359 else if (strcmp (name, "fp=default") == 0)
12360 mips_opts.fp32 = file_mips_fp32;
12361 else if (strcmp (name, "fp=32") == 0)
12362 mips_opts.fp32 = 1;
12363 else if (strcmp (name, "fp=64") == 0)
12364 {
12365 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
12366 as_warn ("%s isa does not support 64-bit floating point registers",
12367 mips_cpu_info_from_isa (mips_opts.isa)->name);
12368 mips_opts.fp32 = 0;
12369 }
12370 else if (strcmp (name, "mips16") == 0
12371 || strcmp (name, "MIPS-16") == 0)
12372 mips_opts.mips16 = 1;
12373 else if (strcmp (name, "nomips16") == 0
12374 || strcmp (name, "noMIPS-16") == 0)
12375 mips_opts.mips16 = 0;
12376 else if (strcmp (name, "smartmips") == 0)
12377 {
12378 if (!ISA_SUPPORTS_SMARTMIPS)
12379 as_warn ("%s ISA does not support SmartMIPS ASE",
12380 mips_cpu_info_from_isa (mips_opts.isa)->name);
12381 mips_opts.ase_smartmips = 1;
12382 }
12383 else if (strcmp (name, "nosmartmips") == 0)
12384 mips_opts.ase_smartmips = 0;
12385 else if (strcmp (name, "mips3d") == 0)
12386 mips_opts.ase_mips3d = 1;
12387 else if (strcmp (name, "nomips3d") == 0)
12388 mips_opts.ase_mips3d = 0;
12389 else if (strcmp (name, "mdmx") == 0)
12390 mips_opts.ase_mdmx = 1;
12391 else if (strcmp (name, "nomdmx") == 0)
12392 mips_opts.ase_mdmx = 0;
12393 else if (strcmp (name, "dsp") == 0)
12394 {
12395 if (!ISA_SUPPORTS_DSP_ASE)
12396 as_warn ("%s ISA does not support DSP ASE",
12397 mips_cpu_info_from_isa (mips_opts.isa)->name);
12398 mips_opts.ase_dsp = 1;
12399 }
12400 else if (strcmp (name, "nodsp") == 0)
12401 mips_opts.ase_dsp = 0;
12402 else if (strcmp (name, "mt") == 0)
12403 {
12404 if (!ISA_SUPPORTS_MT_ASE)
12405 as_warn ("%s ISA does not support MT ASE",
12406 mips_cpu_info_from_isa (mips_opts.isa)->name);
12407 mips_opts.ase_mt = 1;
12408 }
12409 else if (strcmp (name, "nomt") == 0)
12410 mips_opts.ase_mt = 0;
12411 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
12412 {
12413 int reset = 0;
12414
12415 /* Permit the user to change the ISA and architecture on the fly.
12416 Needless to say, misuse can cause serious problems. */
12417 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
12418 {
12419 reset = 1;
12420 mips_opts.isa = file_mips_isa;
12421 mips_opts.arch = file_mips_arch;
12422 }
12423 else if (strncmp (name, "arch=", 5) == 0)
12424 {
12425 const struct mips_cpu_info *p;
12426
12427 p = mips_parse_cpu("internal use", name + 5);
12428 if (!p)
12429 as_bad (_("unknown architecture %s"), name + 5);
12430 else
12431 {
12432 mips_opts.arch = p->cpu;
12433 mips_opts.isa = p->isa;
12434 }
12435 }
12436 else if (strncmp (name, "mips", 4) == 0)
12437 {
12438 const struct mips_cpu_info *p;
12439
12440 p = mips_parse_cpu("internal use", name);
12441 if (!p)
12442 as_bad (_("unknown ISA level %s"), name + 4);
12443 else
12444 {
12445 mips_opts.arch = p->cpu;
12446 mips_opts.isa = p->isa;
12447 }
12448 }
12449 else
12450 as_bad (_("unknown ISA or architecture %s"), name);
12451
12452 switch (mips_opts.isa)
12453 {
12454 case 0:
12455 break;
12456 case ISA_MIPS1:
12457 case ISA_MIPS2:
12458 case ISA_MIPS32:
12459 case ISA_MIPS32R2:
12460 mips_opts.gp32 = 1;
12461 mips_opts.fp32 = 1;
12462 break;
12463 case ISA_MIPS3:
12464 case ISA_MIPS4:
12465 case ISA_MIPS5:
12466 case ISA_MIPS64:
12467 case ISA_MIPS64R2:
12468 mips_opts.gp32 = 0;
12469 mips_opts.fp32 = 0;
12470 break;
12471 default:
12472 as_bad (_("unknown ISA level %s"), name + 4);
12473 break;
12474 }
12475 if (reset)
12476 {
12477 mips_opts.gp32 = file_mips_gp32;
12478 mips_opts.fp32 = file_mips_fp32;
12479 }
12480 }
12481 else if (strcmp (name, "autoextend") == 0)
12482 mips_opts.noautoextend = 0;
12483 else if (strcmp (name, "noautoextend") == 0)
12484 mips_opts.noautoextend = 1;
12485 else if (strcmp (name, "push") == 0)
12486 {
12487 struct mips_option_stack *s;
12488
12489 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12490 s->next = mips_opts_stack;
12491 s->options = mips_opts;
12492 mips_opts_stack = s;
12493 }
12494 else if (strcmp (name, "pop") == 0)
12495 {
12496 struct mips_option_stack *s;
12497
12498 s = mips_opts_stack;
12499 if (s == NULL)
12500 as_bad (_(".set pop with no .set push"));
12501 else
12502 {
12503 /* If we're changing the reorder mode we need to handle
12504 delay slots correctly. */
12505 if (s->options.noreorder && ! mips_opts.noreorder)
12506 start_noreorder ();
12507 else if (! s->options.noreorder && mips_opts.noreorder)
12508 end_noreorder ();
12509
12510 mips_opts = s->options;
12511 mips_opts_stack = s->next;
12512 free (s);
12513 }
12514 }
12515 else if (strcmp (name, "sym32") == 0)
12516 mips_opts.sym32 = TRUE;
12517 else if (strcmp (name, "nosym32") == 0)
12518 mips_opts.sym32 = FALSE;
12519 else
12520 {
12521 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12522 }
12523 *input_line_pointer = ch;
12524 demand_empty_rest_of_line ();
12525 }
12526
12527 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
12528 .option pic2. It means to generate SVR4 PIC calls. */
12529
12530 static void
12531 s_abicalls (int ignore ATTRIBUTE_UNUSED)
12532 {
12533 mips_pic = SVR4_PIC;
12534 mips_abicalls = TRUE;
12535
12536 if (g_switch_seen && g_switch_value != 0)
12537 as_warn (_("-G may not be used with SVR4 PIC code"));
12538 g_switch_value = 0;
12539
12540 bfd_set_gp_size (stdoutput, 0);
12541 demand_empty_rest_of_line ();
12542 }
12543
12544 /* Handle the .cpload pseudo-op. This is used when generating SVR4
12545 PIC code. It sets the $gp register for the function based on the
12546 function address, which is in the register named in the argument.
12547 This uses a relocation against _gp_disp, which is handled specially
12548 by the linker. The result is:
12549 lui $gp,%hi(_gp_disp)
12550 addiu $gp,$gp,%lo(_gp_disp)
12551 addu $gp,$gp,.cpload argument
12552 The .cpload argument is normally $25 == $t9.
12553
12554 The -mno-shared option changes this to:
12555 lui $gp,%hi(__gnu_local_gp)
12556 addiu $gp,$gp,%lo(__gnu_local_gp)
12557 and the argument is ignored. This saves an instruction, but the
12558 resulting code is not position independent; it uses an absolute
12559 address for __gnu_local_gp. Thus code assembled with -mno-shared
12560 can go into an ordinary executable, but not into a shared library. */
12561
12562 static void
12563 s_cpload (int ignore ATTRIBUTE_UNUSED)
12564 {
12565 expressionS ex;
12566 int reg;
12567 int in_shared;
12568
12569 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12570 .cpload is ignored. */
12571 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12572 {
12573 s_ignore (0);
12574 return;
12575 }
12576
12577 /* .cpload should be in a .set noreorder section. */
12578 if (mips_opts.noreorder == 0)
12579 as_warn (_(".cpload not in noreorder section"));
12580
12581 reg = tc_get_register (0);
12582
12583 /* If we need to produce a 64-bit address, we are better off using
12584 the default instruction sequence. */
12585 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
12586
12587 ex.X_op = O_symbol;
12588 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
12589 "__gnu_local_gp");
12590 ex.X_op_symbol = NULL;
12591 ex.X_add_number = 0;
12592
12593 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12594 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12595
12596 macro_start ();
12597 macro_build_lui (&ex, mips_gp_register);
12598 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12599 mips_gp_register, BFD_RELOC_LO16);
12600 if (in_shared)
12601 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
12602 mips_gp_register, reg);
12603 macro_end ();
12604
12605 demand_empty_rest_of_line ();
12606 }
12607
12608 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12609 .cpsetup $reg1, offset|$reg2, label
12610
12611 If offset is given, this results in:
12612 sd $gp, offset($sp)
12613 lui $gp, %hi(%neg(%gp_rel(label)))
12614 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12615 daddu $gp, $gp, $reg1
12616
12617 If $reg2 is given, this results in:
12618 daddu $reg2, $gp, $0
12619 lui $gp, %hi(%neg(%gp_rel(label)))
12620 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12621 daddu $gp, $gp, $reg1
12622 $reg1 is normally $25 == $t9.
12623
12624 The -mno-shared option replaces the last three instructions with
12625 lui $gp,%hi(_gp)
12626 addiu $gp,$gp,%lo(_gp) */
12627
12628 static void
12629 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
12630 {
12631 expressionS ex_off;
12632 expressionS ex_sym;
12633 int reg1;
12634
12635 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12636 We also need NewABI support. */
12637 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12638 {
12639 s_ignore (0);
12640 return;
12641 }
12642
12643 reg1 = tc_get_register (0);
12644 SKIP_WHITESPACE ();
12645 if (*input_line_pointer != ',')
12646 {
12647 as_bad (_("missing argument separator ',' for .cpsetup"));
12648 return;
12649 }
12650 else
12651 ++input_line_pointer;
12652 SKIP_WHITESPACE ();
12653 if (*input_line_pointer == '$')
12654 {
12655 mips_cpreturn_register = tc_get_register (0);
12656 mips_cpreturn_offset = -1;
12657 }
12658 else
12659 {
12660 mips_cpreturn_offset = get_absolute_expression ();
12661 mips_cpreturn_register = -1;
12662 }
12663 SKIP_WHITESPACE ();
12664 if (*input_line_pointer != ',')
12665 {
12666 as_bad (_("missing argument separator ',' for .cpsetup"));
12667 return;
12668 }
12669 else
12670 ++input_line_pointer;
12671 SKIP_WHITESPACE ();
12672 expression (&ex_sym);
12673
12674 macro_start ();
12675 if (mips_cpreturn_register == -1)
12676 {
12677 ex_off.X_op = O_constant;
12678 ex_off.X_add_symbol = NULL;
12679 ex_off.X_op_symbol = NULL;
12680 ex_off.X_add_number = mips_cpreturn_offset;
12681
12682 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
12683 BFD_RELOC_LO16, SP);
12684 }
12685 else
12686 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
12687 mips_gp_register, 0);
12688
12689 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
12690 {
12691 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
12692 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
12693 BFD_RELOC_HI16_S);
12694
12695 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
12696 mips_gp_register, -1, BFD_RELOC_GPREL16,
12697 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
12698
12699 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12700 mips_gp_register, reg1);
12701 }
12702 else
12703 {
12704 expressionS ex;
12705
12706 ex.X_op = O_symbol;
12707 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
12708 ex.X_op_symbol = NULL;
12709 ex.X_add_number = 0;
12710
12711 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12712 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12713
12714 macro_build_lui (&ex, mips_gp_register);
12715 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12716 mips_gp_register, BFD_RELOC_LO16);
12717 }
12718
12719 macro_end ();
12720
12721 demand_empty_rest_of_line ();
12722 }
12723
12724 static void
12725 s_cplocal (int ignore ATTRIBUTE_UNUSED)
12726 {
12727 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12728 .cplocal is ignored. */
12729 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12730 {
12731 s_ignore (0);
12732 return;
12733 }
12734
12735 mips_gp_register = tc_get_register (0);
12736 demand_empty_rest_of_line ();
12737 }
12738
12739 /* Handle the .cprestore pseudo-op. This stores $gp into a given
12740 offset from $sp. The offset is remembered, and after making a PIC
12741 call $gp is restored from that location. */
12742
12743 static void
12744 s_cprestore (int ignore ATTRIBUTE_UNUSED)
12745 {
12746 expressionS ex;
12747
12748 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12749 .cprestore is ignored. */
12750 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12751 {
12752 s_ignore (0);
12753 return;
12754 }
12755
12756 mips_cprestore_offset = get_absolute_expression ();
12757 mips_cprestore_valid = 1;
12758
12759 ex.X_op = O_constant;
12760 ex.X_add_symbol = NULL;
12761 ex.X_op_symbol = NULL;
12762 ex.X_add_number = mips_cprestore_offset;
12763
12764 macro_start ();
12765 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12766 SP, HAVE_64BIT_ADDRESSES);
12767 macro_end ();
12768
12769 demand_empty_rest_of_line ();
12770 }
12771
12772 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12773 was given in the preceding .cpsetup, it results in:
12774 ld $gp, offset($sp)
12775
12776 If a register $reg2 was given there, it results in:
12777 daddu $gp, $reg2, $0 */
12778
12779 static void
12780 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
12781 {
12782 expressionS ex;
12783
12784 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12785 We also need NewABI support. */
12786 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12787 {
12788 s_ignore (0);
12789 return;
12790 }
12791
12792 macro_start ();
12793 if (mips_cpreturn_register == -1)
12794 {
12795 ex.X_op = O_constant;
12796 ex.X_add_symbol = NULL;
12797 ex.X_op_symbol = NULL;
12798 ex.X_add_number = mips_cpreturn_offset;
12799
12800 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
12801 }
12802 else
12803 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
12804 mips_cpreturn_register, 0);
12805 macro_end ();
12806
12807 demand_empty_rest_of_line ();
12808 }
12809
12810 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12811 code. It sets the offset to use in gp_rel relocations. */
12812
12813 static void
12814 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
12815 {
12816 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12817 We also need NewABI support. */
12818 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12819 {
12820 s_ignore (0);
12821 return;
12822 }
12823
12824 mips_gprel_offset = get_absolute_expression ();
12825
12826 demand_empty_rest_of_line ();
12827 }
12828
12829 /* Handle the .gpword pseudo-op. This is used when generating PIC
12830 code. It generates a 32 bit GP relative reloc. */
12831
12832 static void
12833 s_gpword (int ignore ATTRIBUTE_UNUSED)
12834 {
12835 segment_info_type *si;
12836 struct insn_label_list *l;
12837 symbolS *label;
12838 expressionS ex;
12839 char *p;
12840
12841 /* When not generating PIC code, this is treated as .word. */
12842 if (mips_pic != SVR4_PIC)
12843 {
12844 s_cons (2);
12845 return;
12846 }
12847
12848 si = seg_info (now_seg);
12849 l = si->label_list;
12850 label = l != NULL ? l->label : NULL;
12851 mips_emit_delays ();
12852 if (auto_align)
12853 mips_align (2, 0, label);
12854 mips_clear_insn_labels ();
12855
12856 expression (&ex);
12857
12858 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12859 {
12860 as_bad (_("Unsupported use of .gpword"));
12861 ignore_rest_of_line ();
12862 }
12863
12864 p = frag_more (4);
12865 md_number_to_chars (p, 0, 4);
12866 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12867 BFD_RELOC_GPREL32);
12868
12869 demand_empty_rest_of_line ();
12870 }
12871
12872 static void
12873 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12874 {
12875 segment_info_type *si;
12876 struct insn_label_list *l;
12877 symbolS *label;
12878 expressionS ex;
12879 char *p;
12880
12881 /* When not generating PIC code, this is treated as .dword. */
12882 if (mips_pic != SVR4_PIC)
12883 {
12884 s_cons (3);
12885 return;
12886 }
12887
12888 si = seg_info (now_seg);
12889 l = si->label_list;
12890 label = l != NULL ? l->label : NULL;
12891 mips_emit_delays ();
12892 if (auto_align)
12893 mips_align (3, 0, label);
12894 mips_clear_insn_labels ();
12895
12896 expression (&ex);
12897
12898 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12899 {
12900 as_bad (_("Unsupported use of .gpdword"));
12901 ignore_rest_of_line ();
12902 }
12903
12904 p = frag_more (8);
12905 md_number_to_chars (p, 0, 8);
12906 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12907 BFD_RELOC_GPREL32)->fx_tcbit = 1;
12908
12909 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12910 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12911 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
12912
12913 demand_empty_rest_of_line ();
12914 }
12915
12916 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12917 tables in SVR4 PIC code. */
12918
12919 static void
12920 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12921 {
12922 int reg;
12923
12924 /* This is ignored when not generating SVR4 PIC code. */
12925 if (mips_pic != SVR4_PIC)
12926 {
12927 s_ignore (0);
12928 return;
12929 }
12930
12931 /* Add $gp to the register named as an argument. */
12932 macro_start ();
12933 reg = tc_get_register (0);
12934 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
12935 macro_end ();
12936
12937 demand_empty_rest_of_line ();
12938 }
12939
12940 /* Handle the .insn pseudo-op. This marks instruction labels in
12941 mips16 mode. This permits the linker to handle them specially,
12942 such as generating jalx instructions when needed. We also make
12943 them odd for the duration of the assembly, in order to generate the
12944 right sort of code. We will make them even in the adjust_symtab
12945 routine, while leaving them marked. This is convenient for the
12946 debugger and the disassembler. The linker knows to make them odd
12947 again. */
12948
12949 static void
12950 s_insn (int ignore ATTRIBUTE_UNUSED)
12951 {
12952 mips16_mark_labels ();
12953
12954 demand_empty_rest_of_line ();
12955 }
12956
12957 /* Handle a .stabn directive. We need these in order to mark a label
12958 as being a mips16 text label correctly. Sometimes the compiler
12959 will emit a label, followed by a .stabn, and then switch sections.
12960 If the label and .stabn are in mips16 mode, then the label is
12961 really a mips16 text label. */
12962
12963 static void
12964 s_mips_stab (int type)
12965 {
12966 if (type == 'n')
12967 mips16_mark_labels ();
12968
12969 s_stab (type);
12970 }
12971
12972 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
12973
12974 static void
12975 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12976 {
12977 char *name;
12978 int c;
12979 symbolS *symbolP;
12980 expressionS exp;
12981
12982 name = input_line_pointer;
12983 c = get_symbol_end ();
12984 symbolP = symbol_find_or_make (name);
12985 S_SET_WEAK (symbolP);
12986 *input_line_pointer = c;
12987
12988 SKIP_WHITESPACE ();
12989
12990 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12991 {
12992 if (S_IS_DEFINED (symbolP))
12993 {
12994 as_bad ("ignoring attempt to redefine symbol %s",
12995 S_GET_NAME (symbolP));
12996 ignore_rest_of_line ();
12997 return;
12998 }
12999
13000 if (*input_line_pointer == ',')
13001 {
13002 ++input_line_pointer;
13003 SKIP_WHITESPACE ();
13004 }
13005
13006 expression (&exp);
13007 if (exp.X_op != O_symbol)
13008 {
13009 as_bad ("bad .weakext directive");
13010 ignore_rest_of_line ();
13011 return;
13012 }
13013 symbol_set_value_expression (symbolP, &exp);
13014 }
13015
13016 demand_empty_rest_of_line ();
13017 }
13018
13019 /* Parse a register string into a number. Called from the ECOFF code
13020 to parse .frame. The argument is non-zero if this is the frame
13021 register, so that we can record it in mips_frame_reg. */
13022
13023 int
13024 tc_get_register (int frame)
13025 {
13026 unsigned int reg;
13027
13028 SKIP_WHITESPACE ();
13029 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
13030 reg = 0;
13031 if (frame)
13032 {
13033 mips_frame_reg = reg != 0 ? reg : SP;
13034 mips_frame_reg_valid = 1;
13035 mips_cprestore_valid = 0;
13036 }
13037 return reg;
13038 }
13039
13040 valueT
13041 md_section_align (asection *seg, valueT addr)
13042 {
13043 int align = bfd_get_section_alignment (stdoutput, seg);
13044
13045 if (IS_ELF)
13046 {
13047 /* We don't need to align ELF sections to the full alignment.
13048 However, Irix 5 may prefer that we align them at least to a 16
13049 byte boundary. We don't bother to align the sections if we
13050 are targeted for an embedded system. */
13051 if (strcmp (TARGET_OS, "elf") == 0)
13052 return addr;
13053 if (align > 4)
13054 align = 4;
13055 }
13056
13057 return ((addr + (1 << align) - 1) & (-1 << align));
13058 }
13059
13060 /* Utility routine, called from above as well. If called while the
13061 input file is still being read, it's only an approximation. (For
13062 example, a symbol may later become defined which appeared to be
13063 undefined earlier.) */
13064
13065 static int
13066 nopic_need_relax (symbolS *sym, int before_relaxing)
13067 {
13068 if (sym == 0)
13069 return 0;
13070
13071 if (g_switch_value > 0)
13072 {
13073 const char *symname;
13074 int change;
13075
13076 /* Find out whether this symbol can be referenced off the $gp
13077 register. It can be if it is smaller than the -G size or if
13078 it is in the .sdata or .sbss section. Certain symbols can
13079 not be referenced off the $gp, although it appears as though
13080 they can. */
13081 symname = S_GET_NAME (sym);
13082 if (symname != (const char *) NULL
13083 && (strcmp (symname, "eprol") == 0
13084 || strcmp (symname, "etext") == 0
13085 || strcmp (symname, "_gp") == 0
13086 || strcmp (symname, "edata") == 0
13087 || strcmp (symname, "_fbss") == 0
13088 || strcmp (symname, "_fdata") == 0
13089 || strcmp (symname, "_ftext") == 0
13090 || strcmp (symname, "end") == 0
13091 || strcmp (symname, "_gp_disp") == 0))
13092 change = 1;
13093 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
13094 && (0
13095 #ifndef NO_ECOFF_DEBUGGING
13096 || (symbol_get_obj (sym)->ecoff_extern_size != 0
13097 && (symbol_get_obj (sym)->ecoff_extern_size
13098 <= g_switch_value))
13099 #endif
13100 /* We must defer this decision until after the whole
13101 file has been read, since there might be a .extern
13102 after the first use of this symbol. */
13103 || (before_relaxing
13104 #ifndef NO_ECOFF_DEBUGGING
13105 && symbol_get_obj (sym)->ecoff_extern_size == 0
13106 #endif
13107 && S_GET_VALUE (sym) == 0)
13108 || (S_GET_VALUE (sym) != 0
13109 && S_GET_VALUE (sym) <= g_switch_value)))
13110 change = 0;
13111 else
13112 {
13113 const char *segname;
13114
13115 segname = segment_name (S_GET_SEGMENT (sym));
13116 assert (strcmp (segname, ".lit8") != 0
13117 && strcmp (segname, ".lit4") != 0);
13118 change = (strcmp (segname, ".sdata") != 0
13119 && strcmp (segname, ".sbss") != 0
13120 && strncmp (segname, ".sdata.", 7) != 0
13121 && strncmp (segname, ".sbss.", 6) != 0
13122 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
13123 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
13124 }
13125 return change;
13126 }
13127 else
13128 /* We are not optimizing for the $gp register. */
13129 return 1;
13130 }
13131
13132
13133 /* Return true if the given symbol should be considered local for SVR4 PIC. */
13134
13135 static bfd_boolean
13136 pic_need_relax (symbolS *sym, asection *segtype)
13137 {
13138 asection *symsec;
13139
13140 /* Handle the case of a symbol equated to another symbol. */
13141 while (symbol_equated_reloc_p (sym))
13142 {
13143 symbolS *n;
13144
13145 /* It's possible to get a loop here in a badly written program. */
13146 n = symbol_get_value_expression (sym)->X_add_symbol;
13147 if (n == sym)
13148 break;
13149 sym = n;
13150 }
13151
13152 symsec = S_GET_SEGMENT (sym);
13153
13154 /* This must duplicate the test in adjust_reloc_syms. */
13155 return (symsec != &bfd_und_section
13156 && symsec != &bfd_abs_section
13157 && !bfd_is_com_section (symsec)
13158 && !s_is_linkonce (sym, segtype)
13159 #ifdef OBJ_ELF
13160 /* A global or weak symbol is treated as external. */
13161 && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
13162 #endif
13163 );
13164 }
13165
13166
13167 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13168 extended opcode. SEC is the section the frag is in. */
13169
13170 static int
13171 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
13172 {
13173 int type;
13174 const struct mips16_immed_operand *op;
13175 offsetT val;
13176 int mintiny, maxtiny;
13177 segT symsec;
13178 fragS *sym_frag;
13179
13180 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13181 return 0;
13182 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13183 return 1;
13184
13185 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13186 op = mips16_immed_operands;
13187 while (op->type != type)
13188 {
13189 ++op;
13190 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13191 }
13192
13193 if (op->unsp)
13194 {
13195 if (type == '<' || type == '>' || type == '[' || type == ']')
13196 {
13197 mintiny = 1;
13198 maxtiny = 1 << op->nbits;
13199 }
13200 else
13201 {
13202 mintiny = 0;
13203 maxtiny = (1 << op->nbits) - 1;
13204 }
13205 }
13206 else
13207 {
13208 mintiny = - (1 << (op->nbits - 1));
13209 maxtiny = (1 << (op->nbits - 1)) - 1;
13210 }
13211
13212 sym_frag = symbol_get_frag (fragp->fr_symbol);
13213 val = S_GET_VALUE (fragp->fr_symbol);
13214 symsec = S_GET_SEGMENT (fragp->fr_symbol);
13215
13216 if (op->pcrel)
13217 {
13218 addressT addr;
13219
13220 /* We won't have the section when we are called from
13221 mips_relax_frag. However, we will always have been called
13222 from md_estimate_size_before_relax first. If this is a
13223 branch to a different section, we mark it as such. If SEC is
13224 NULL, and the frag is not marked, then it must be a branch to
13225 the same section. */
13226 if (sec == NULL)
13227 {
13228 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13229 return 1;
13230 }
13231 else
13232 {
13233 /* Must have been called from md_estimate_size_before_relax. */
13234 if (symsec != sec)
13235 {
13236 fragp->fr_subtype =
13237 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13238
13239 /* FIXME: We should support this, and let the linker
13240 catch branches and loads that are out of range. */
13241 as_bad_where (fragp->fr_file, fragp->fr_line,
13242 _("unsupported PC relative reference to different section"));
13243
13244 return 1;
13245 }
13246 if (fragp != sym_frag && sym_frag->fr_address == 0)
13247 /* Assume non-extended on the first relaxation pass.
13248 The address we have calculated will be bogus if this is
13249 a forward branch to another frag, as the forward frag
13250 will have fr_address == 0. */
13251 return 0;
13252 }
13253
13254 /* In this case, we know for sure that the symbol fragment is in
13255 the same section. If the relax_marker of the symbol fragment
13256 differs from the relax_marker of this fragment, we have not
13257 yet adjusted the symbol fragment fr_address. We want to add
13258 in STRETCH in order to get a better estimate of the address.
13259 This particularly matters because of the shift bits. */
13260 if (stretch != 0
13261 && sym_frag->relax_marker != fragp->relax_marker)
13262 {
13263 fragS *f;
13264
13265 /* Adjust stretch for any alignment frag. Note that if have
13266 been expanding the earlier code, the symbol may be
13267 defined in what appears to be an earlier frag. FIXME:
13268 This doesn't handle the fr_subtype field, which specifies
13269 a maximum number of bytes to skip when doing an
13270 alignment. */
13271 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
13272 {
13273 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13274 {
13275 if (stretch < 0)
13276 stretch = - ((- stretch)
13277 & ~ ((1 << (int) f->fr_offset) - 1));
13278 else
13279 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13280 if (stretch == 0)
13281 break;
13282 }
13283 }
13284 if (f != NULL)
13285 val += stretch;
13286 }
13287
13288 addr = fragp->fr_address + fragp->fr_fix;
13289
13290 /* The base address rules are complicated. The base address of
13291 a branch is the following instruction. The base address of a
13292 PC relative load or add is the instruction itself, but if it
13293 is in a delay slot (in which case it can not be extended) use
13294 the address of the instruction whose delay slot it is in. */
13295 if (type == 'p' || type == 'q')
13296 {
13297 addr += 2;
13298
13299 /* If we are currently assuming that this frag should be
13300 extended, then, the current address is two bytes
13301 higher. */
13302 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13303 addr += 2;
13304
13305 /* Ignore the low bit in the target, since it will be set
13306 for a text label. */
13307 if ((val & 1) != 0)
13308 --val;
13309 }
13310 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13311 addr -= 4;
13312 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13313 addr -= 2;
13314
13315 val -= addr & ~ ((1 << op->shift) - 1);
13316
13317 /* Branch offsets have an implicit 0 in the lowest bit. */
13318 if (type == 'p' || type == 'q')
13319 val /= 2;
13320
13321 /* If any of the shifted bits are set, we must use an extended
13322 opcode. If the address depends on the size of this
13323 instruction, this can lead to a loop, so we arrange to always
13324 use an extended opcode. We only check this when we are in
13325 the main relaxation loop, when SEC is NULL. */
13326 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13327 {
13328 fragp->fr_subtype =
13329 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13330 return 1;
13331 }
13332
13333 /* If we are about to mark a frag as extended because the value
13334 is precisely maxtiny + 1, then there is a chance of an
13335 infinite loop as in the following code:
13336 la $4,foo
13337 .skip 1020
13338 .align 2
13339 foo:
13340 In this case when the la is extended, foo is 0x3fc bytes
13341 away, so the la can be shrunk, but then foo is 0x400 away, so
13342 the la must be extended. To avoid this loop, we mark the
13343 frag as extended if it was small, and is about to become
13344 extended with a value of maxtiny + 1. */
13345 if (val == ((maxtiny + 1) << op->shift)
13346 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13347 && sec == NULL)
13348 {
13349 fragp->fr_subtype =
13350 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13351 return 1;
13352 }
13353 }
13354 else if (symsec != absolute_section && sec != NULL)
13355 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13356
13357 if ((val & ((1 << op->shift) - 1)) != 0
13358 || val < (mintiny << op->shift)
13359 || val > (maxtiny << op->shift))
13360 return 1;
13361 else
13362 return 0;
13363 }
13364
13365 /* Compute the length of a branch sequence, and adjust the
13366 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13367 worst-case length is computed, with UPDATE being used to indicate
13368 whether an unconditional (-1), branch-likely (+1) or regular (0)
13369 branch is to be computed. */
13370 static int
13371 relaxed_branch_length (fragS *fragp, asection *sec, int update)
13372 {
13373 bfd_boolean toofar;
13374 int length;
13375
13376 if (fragp
13377 && S_IS_DEFINED (fragp->fr_symbol)
13378 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13379 {
13380 addressT addr;
13381 offsetT val;
13382
13383 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13384
13385 addr = fragp->fr_address + fragp->fr_fix + 4;
13386
13387 val -= addr;
13388
13389 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13390 }
13391 else if (fragp)
13392 /* If the symbol is not defined or it's in a different segment,
13393 assume the user knows what's going on and emit a short
13394 branch. */
13395 toofar = FALSE;
13396 else
13397 toofar = TRUE;
13398
13399 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13400 fragp->fr_subtype
13401 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13402 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13403 RELAX_BRANCH_LINK (fragp->fr_subtype),
13404 toofar);
13405
13406 length = 4;
13407 if (toofar)
13408 {
13409 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13410 length += 8;
13411
13412 if (mips_pic != NO_PIC)
13413 {
13414 /* Additional space for PIC loading of target address. */
13415 length += 8;
13416 if (mips_opts.isa == ISA_MIPS1)
13417 /* Additional space for $at-stabilizing nop. */
13418 length += 4;
13419 }
13420
13421 /* If branch is conditional. */
13422 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13423 length += 8;
13424 }
13425
13426 return length;
13427 }
13428
13429 /* Estimate the size of a frag before relaxing. Unless this is the
13430 mips16, we are not really relaxing here, and the final size is
13431 encoded in the subtype information. For the mips16, we have to
13432 decide whether we are using an extended opcode or not. */
13433
13434 int
13435 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
13436 {
13437 int change;
13438
13439 if (RELAX_BRANCH_P (fragp->fr_subtype))
13440 {
13441
13442 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13443
13444 return fragp->fr_var;
13445 }
13446
13447 if (RELAX_MIPS16_P (fragp->fr_subtype))
13448 /* We don't want to modify the EXTENDED bit here; it might get us
13449 into infinite loops. We change it only in mips_relax_frag(). */
13450 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13451
13452 if (mips_pic == NO_PIC)
13453 change = nopic_need_relax (fragp->fr_symbol, 0);
13454 else if (mips_pic == SVR4_PIC)
13455 change = pic_need_relax (fragp->fr_symbol, segtype);
13456 else if (mips_pic == VXWORKS_PIC)
13457 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
13458 change = 0;
13459 else
13460 abort ();
13461
13462 if (change)
13463 {
13464 fragp->fr_subtype |= RELAX_USE_SECOND;
13465 return -RELAX_FIRST (fragp->fr_subtype);
13466 }
13467 else
13468 return -RELAX_SECOND (fragp->fr_subtype);
13469 }
13470
13471 /* This is called to see whether a reloc against a defined symbol
13472 should be converted into a reloc against a section. */
13473
13474 int
13475 mips_fix_adjustable (fixS *fixp)
13476 {
13477 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13478 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13479 return 0;
13480
13481 if (fixp->fx_addsy == NULL)
13482 return 1;
13483
13484 /* If symbol SYM is in a mergeable section, relocations of the form
13485 SYM + 0 can usually be made section-relative. The mergeable data
13486 is then identified by the section offset rather than by the symbol.
13487
13488 However, if we're generating REL LO16 relocations, the offset is split
13489 between the LO16 and parterning high part relocation. The linker will
13490 need to recalculate the complete offset in order to correctly identify
13491 the merge data.
13492
13493 The linker has traditionally not looked for the parterning high part
13494 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
13495 placed anywhere. Rather than break backwards compatibility by changing
13496 this, it seems better not to force the issue, and instead keep the
13497 original symbol. This will work with either linker behavior. */
13498 if ((fixp->fx_r_type == BFD_RELOC_LO16
13499 || fixp->fx_r_type == BFD_RELOC_MIPS16_LO16
13500 || reloc_needs_lo_p (fixp->fx_r_type))
13501 && HAVE_IN_PLACE_ADDENDS
13502 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
13503 return 0;
13504
13505 #ifdef OBJ_ELF
13506 /* Don't adjust relocations against mips16 symbols, so that the linker
13507 can find them if it needs to set up a stub. */
13508 if (IS_ELF
13509 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13510 && fixp->fx_subsy == NULL)
13511 return 0;
13512 #endif
13513
13514 return 1;
13515 }
13516
13517 /* Translate internal representation of relocation info to BFD target
13518 format. */
13519
13520 arelent **
13521 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
13522 {
13523 static arelent *retval[4];
13524 arelent *reloc;
13525 bfd_reloc_code_real_type code;
13526
13527 memset (retval, 0, sizeof(retval));
13528 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
13529 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13530 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13531 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13532
13533 if (fixp->fx_pcrel)
13534 {
13535 assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2);
13536
13537 /* At this point, fx_addnumber is "symbol offset - pcrel address".
13538 Relocations want only the symbol offset. */
13539 reloc->addend = fixp->fx_addnumber + reloc->address;
13540 if (!IS_ELF)
13541 {
13542 /* A gruesome hack which is a result of the gruesome gas
13543 reloc handling. What's worse, for COFF (as opposed to
13544 ECOFF), we might need yet another copy of reloc->address.
13545 See bfd_install_relocation. */
13546 reloc->addend += reloc->address;
13547 }
13548 }
13549 else
13550 reloc->addend = fixp->fx_addnumber;
13551
13552 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13553 entry to be used in the relocation's section offset. */
13554 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13555 {
13556 reloc->address = reloc->addend;
13557 reloc->addend = 0;
13558 }
13559
13560 code = fixp->fx_r_type;
13561
13562 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13563 if (reloc->howto == NULL)
13564 {
13565 as_bad_where (fixp->fx_file, fixp->fx_line,
13566 _("Can not represent %s relocation in this object file format"),
13567 bfd_get_reloc_code_name (code));
13568 retval[0] = NULL;
13569 }
13570
13571 return retval;
13572 }
13573
13574 /* Relax a machine dependent frag. This returns the amount by which
13575 the current size of the frag should change. */
13576
13577 int
13578 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
13579 {
13580 if (RELAX_BRANCH_P (fragp->fr_subtype))
13581 {
13582 offsetT old_var = fragp->fr_var;
13583
13584 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13585
13586 return fragp->fr_var - old_var;
13587 }
13588
13589 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13590 return 0;
13591
13592 if (mips16_extended_frag (fragp, NULL, stretch))
13593 {
13594 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13595 return 0;
13596 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13597 return 2;
13598 }
13599 else
13600 {
13601 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13602 return 0;
13603 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13604 return -2;
13605 }
13606
13607 return 0;
13608 }
13609
13610 /* Convert a machine dependent frag. */
13611
13612 void
13613 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
13614 {
13615 if (RELAX_BRANCH_P (fragp->fr_subtype))
13616 {
13617 bfd_byte *buf;
13618 unsigned long insn;
13619 expressionS exp;
13620 fixS *fixp;
13621
13622 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13623
13624 if (target_big_endian)
13625 insn = bfd_getb32 (buf);
13626 else
13627 insn = bfd_getl32 (buf);
13628
13629 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13630 {
13631 /* We generate a fixup instead of applying it right now
13632 because, if there are linker relaxations, we're going to
13633 need the relocations. */
13634 exp.X_op = O_symbol;
13635 exp.X_add_symbol = fragp->fr_symbol;
13636 exp.X_add_number = fragp->fr_offset;
13637
13638 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13639 4, &exp, TRUE, BFD_RELOC_16_PCREL_S2);
13640 fixp->fx_file = fragp->fr_file;
13641 fixp->fx_line = fragp->fr_line;
13642
13643 md_number_to_chars ((char *) buf, insn, 4);
13644 buf += 4;
13645 }
13646 else
13647 {
13648 int i;
13649
13650 as_warn_where (fragp->fr_file, fragp->fr_line,
13651 _("relaxed out-of-range branch into a jump"));
13652
13653 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13654 goto uncond;
13655
13656 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13657 {
13658 /* Reverse the branch. */
13659 switch ((insn >> 28) & 0xf)
13660 {
13661 case 4:
13662 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13663 have the condition reversed by tweaking a single
13664 bit, and their opcodes all have 0x4???????. */
13665 assert ((insn & 0xf1000000) == 0x41000000);
13666 insn ^= 0x00010000;
13667 break;
13668
13669 case 0:
13670 /* bltz 0x04000000 bgez 0x04010000
13671 bltzal 0x04100000 bgezal 0x04110000 */
13672 assert ((insn & 0xfc0e0000) == 0x04000000);
13673 insn ^= 0x00010000;
13674 break;
13675
13676 case 1:
13677 /* beq 0x10000000 bne 0x14000000
13678 blez 0x18000000 bgtz 0x1c000000 */
13679 insn ^= 0x04000000;
13680 break;
13681
13682 default:
13683 abort ();
13684 }
13685 }
13686
13687 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13688 {
13689 /* Clear the and-link bit. */
13690 assert ((insn & 0xfc1c0000) == 0x04100000);
13691
13692 /* bltzal 0x04100000 bgezal 0x04110000
13693 bltzall 0x04120000 bgezall 0x04130000 */
13694 insn &= ~0x00100000;
13695 }
13696
13697 /* Branch over the branch (if the branch was likely) or the
13698 full jump (not likely case). Compute the offset from the
13699 current instruction to branch to. */
13700 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13701 i = 16;
13702 else
13703 {
13704 /* How many bytes in instructions we've already emitted? */
13705 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13706 /* How many bytes in instructions from here to the end? */
13707 i = fragp->fr_var - i;
13708 }
13709 /* Convert to instruction count. */
13710 i >>= 2;
13711 /* Branch counts from the next instruction. */
13712 i--;
13713 insn |= i;
13714 /* Branch over the jump. */
13715 md_number_to_chars ((char *) buf, insn, 4);
13716 buf += 4;
13717
13718 /* nop */
13719 md_number_to_chars ((char *) buf, 0, 4);
13720 buf += 4;
13721
13722 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13723 {
13724 /* beql $0, $0, 2f */
13725 insn = 0x50000000;
13726 /* Compute the PC offset from the current instruction to
13727 the end of the variable frag. */
13728 /* How many bytes in instructions we've already emitted? */
13729 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13730 /* How many bytes in instructions from here to the end? */
13731 i = fragp->fr_var - i;
13732 /* Convert to instruction count. */
13733 i >>= 2;
13734 /* Don't decrement i, because we want to branch over the
13735 delay slot. */
13736
13737 insn |= i;
13738 md_number_to_chars ((char *) buf, insn, 4);
13739 buf += 4;
13740
13741 md_number_to_chars ((char *) buf, 0, 4);
13742 buf += 4;
13743 }
13744
13745 uncond:
13746 if (mips_pic == NO_PIC)
13747 {
13748 /* j or jal. */
13749 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13750 ? 0x0c000000 : 0x08000000);
13751 exp.X_op = O_symbol;
13752 exp.X_add_symbol = fragp->fr_symbol;
13753 exp.X_add_number = fragp->fr_offset;
13754
13755 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13756 4, &exp, FALSE, BFD_RELOC_MIPS_JMP);
13757 fixp->fx_file = fragp->fr_file;
13758 fixp->fx_line = fragp->fr_line;
13759
13760 md_number_to_chars ((char *) buf, insn, 4);
13761 buf += 4;
13762 }
13763 else
13764 {
13765 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13766 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13767 exp.X_op = O_symbol;
13768 exp.X_add_symbol = fragp->fr_symbol;
13769 exp.X_add_number = fragp->fr_offset;
13770
13771 if (fragp->fr_offset)
13772 {
13773 exp.X_add_symbol = make_expr_symbol (&exp);
13774 exp.X_add_number = 0;
13775 }
13776
13777 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13778 4, &exp, FALSE, BFD_RELOC_MIPS_GOT16);
13779 fixp->fx_file = fragp->fr_file;
13780 fixp->fx_line = fragp->fr_line;
13781
13782 md_number_to_chars ((char *) buf, insn, 4);
13783 buf += 4;
13784
13785 if (mips_opts.isa == ISA_MIPS1)
13786 {
13787 /* nop */
13788 md_number_to_chars ((char *) buf, 0, 4);
13789 buf += 4;
13790 }
13791
13792 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13793 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13794
13795 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13796 4, &exp, FALSE, BFD_RELOC_LO16);
13797 fixp->fx_file = fragp->fr_file;
13798 fixp->fx_line = fragp->fr_line;
13799
13800 md_number_to_chars ((char *) buf, insn, 4);
13801 buf += 4;
13802
13803 /* j(al)r $at. */
13804 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13805 insn = 0x0020f809;
13806 else
13807 insn = 0x00200008;
13808
13809 md_number_to_chars ((char *) buf, insn, 4);
13810 buf += 4;
13811 }
13812 }
13813
13814 assert (buf == (bfd_byte *)fragp->fr_literal
13815 + fragp->fr_fix + fragp->fr_var);
13816
13817 fragp->fr_fix += fragp->fr_var;
13818
13819 return;
13820 }
13821
13822 if (RELAX_MIPS16_P (fragp->fr_subtype))
13823 {
13824 int type;
13825 const struct mips16_immed_operand *op;
13826 bfd_boolean small, ext;
13827 offsetT val;
13828 bfd_byte *buf;
13829 unsigned long insn;
13830 bfd_boolean use_extend;
13831 unsigned short extend;
13832
13833 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13834 op = mips16_immed_operands;
13835 while (op->type != type)
13836 ++op;
13837
13838 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13839 {
13840 small = FALSE;
13841 ext = TRUE;
13842 }
13843 else
13844 {
13845 small = TRUE;
13846 ext = FALSE;
13847 }
13848
13849 resolve_symbol_value (fragp->fr_symbol);
13850 val = S_GET_VALUE (fragp->fr_symbol);
13851 if (op->pcrel)
13852 {
13853 addressT addr;
13854
13855 addr = fragp->fr_address + fragp->fr_fix;
13856
13857 /* The rules for the base address of a PC relative reloc are
13858 complicated; see mips16_extended_frag. */
13859 if (type == 'p' || type == 'q')
13860 {
13861 addr += 2;
13862 if (ext)
13863 addr += 2;
13864 /* Ignore the low bit in the target, since it will be
13865 set for a text label. */
13866 if ((val & 1) != 0)
13867 --val;
13868 }
13869 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13870 addr -= 4;
13871 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13872 addr -= 2;
13873
13874 addr &= ~ (addressT) ((1 << op->shift) - 1);
13875 val -= addr;
13876
13877 /* Make sure the section winds up with the alignment we have
13878 assumed. */
13879 if (op->shift > 0)
13880 record_alignment (asec, op->shift);
13881 }
13882
13883 if (ext
13884 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13885 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13886 as_warn_where (fragp->fr_file, fragp->fr_line,
13887 _("extended instruction in delay slot"));
13888
13889 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13890
13891 if (target_big_endian)
13892 insn = bfd_getb16 (buf);
13893 else
13894 insn = bfd_getl16 (buf);
13895
13896 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13897 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13898 small, ext, &insn, &use_extend, &extend);
13899
13900 if (use_extend)
13901 {
13902 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13903 fragp->fr_fix += 2;
13904 buf += 2;
13905 }
13906
13907 md_number_to_chars ((char *) buf, insn, 2);
13908 fragp->fr_fix += 2;
13909 buf += 2;
13910 }
13911 else
13912 {
13913 int first, second;
13914 fixS *fixp;
13915
13916 first = RELAX_FIRST (fragp->fr_subtype);
13917 second = RELAX_SECOND (fragp->fr_subtype);
13918 fixp = (fixS *) fragp->fr_opcode;
13919
13920 /* Possibly emit a warning if we've chosen the longer option. */
13921 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13922 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13923 {
13924 const char *msg = macro_warning (fragp->fr_subtype);
13925 if (msg != 0)
13926 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13927 }
13928
13929 /* Go through all the fixups for the first sequence. Disable them
13930 (by marking them as done) if we're going to use the second
13931 sequence instead. */
13932 while (fixp
13933 && fixp->fx_frag == fragp
13934 && fixp->fx_where < fragp->fr_fix - second)
13935 {
13936 if (fragp->fr_subtype & RELAX_USE_SECOND)
13937 fixp->fx_done = 1;
13938 fixp = fixp->fx_next;
13939 }
13940
13941 /* Go through the fixups for the second sequence. Disable them if
13942 we're going to use the first sequence, otherwise adjust their
13943 addresses to account for the relaxation. */
13944 while (fixp && fixp->fx_frag == fragp)
13945 {
13946 if (fragp->fr_subtype & RELAX_USE_SECOND)
13947 fixp->fx_where -= first;
13948 else
13949 fixp->fx_done = 1;
13950 fixp = fixp->fx_next;
13951 }
13952
13953 /* Now modify the frag contents. */
13954 if (fragp->fr_subtype & RELAX_USE_SECOND)
13955 {
13956 char *start;
13957
13958 start = fragp->fr_literal + fragp->fr_fix - first - second;
13959 memmove (start, start + first, second);
13960 fragp->fr_fix -= first;
13961 }
13962 else
13963 fragp->fr_fix -= second;
13964 }
13965 }
13966
13967 #ifdef OBJ_ELF
13968
13969 /* This function is called after the relocs have been generated.
13970 We've been storing mips16 text labels as odd. Here we convert them
13971 back to even for the convenience of the debugger. */
13972
13973 void
13974 mips_frob_file_after_relocs (void)
13975 {
13976 asymbol **syms;
13977 unsigned int count, i;
13978
13979 if (!IS_ELF)
13980 return;
13981
13982 syms = bfd_get_outsymbols (stdoutput);
13983 count = bfd_get_symcount (stdoutput);
13984 for (i = 0; i < count; i++, syms++)
13985 {
13986 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13987 && ((*syms)->value & 1) != 0)
13988 {
13989 (*syms)->value &= ~1;
13990 /* If the symbol has an odd size, it was probably computed
13991 incorrectly, so adjust that as well. */
13992 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13993 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13994 }
13995 }
13996 }
13997
13998 #endif
13999
14000 /* This function is called whenever a label is defined. It is used
14001 when handling branch delays; if a branch has a label, we assume we
14002 can not move it. */
14003
14004 void
14005 mips_define_label (symbolS *sym)
14006 {
14007 segment_info_type *si = seg_info (now_seg);
14008 struct insn_label_list *l;
14009
14010 if (free_insn_labels == NULL)
14011 l = (struct insn_label_list *) xmalloc (sizeof *l);
14012 else
14013 {
14014 l = free_insn_labels;
14015 free_insn_labels = l->next;
14016 }
14017
14018 l->label = sym;
14019 l->next = si->label_list;
14020 si->label_list = l;
14021
14022 #ifdef OBJ_ELF
14023 dwarf2_emit_label (sym);
14024 #endif
14025 }
14026 \f
14027 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14028
14029 /* Some special processing for a MIPS ELF file. */
14030
14031 void
14032 mips_elf_final_processing (void)
14033 {
14034 /* Write out the register information. */
14035 if (mips_abi != N64_ABI)
14036 {
14037 Elf32_RegInfo s;
14038
14039 s.ri_gprmask = mips_gprmask;
14040 s.ri_cprmask[0] = mips_cprmask[0];
14041 s.ri_cprmask[1] = mips_cprmask[1];
14042 s.ri_cprmask[2] = mips_cprmask[2];
14043 s.ri_cprmask[3] = mips_cprmask[3];
14044 /* The gp_value field is set by the MIPS ELF backend. */
14045
14046 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14047 ((Elf32_External_RegInfo *)
14048 mips_regmask_frag));
14049 }
14050 else
14051 {
14052 Elf64_Internal_RegInfo s;
14053
14054 s.ri_gprmask = mips_gprmask;
14055 s.ri_pad = 0;
14056 s.ri_cprmask[0] = mips_cprmask[0];
14057 s.ri_cprmask[1] = mips_cprmask[1];
14058 s.ri_cprmask[2] = mips_cprmask[2];
14059 s.ri_cprmask[3] = mips_cprmask[3];
14060 /* The gp_value field is set by the MIPS ELF backend. */
14061
14062 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14063 ((Elf64_External_RegInfo *)
14064 mips_regmask_frag));
14065 }
14066
14067 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
14068 sort of BFD interface for this. */
14069 if (mips_any_noreorder)
14070 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14071 if (mips_pic != NO_PIC)
14072 {
14073 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
14074 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14075 }
14076 if (mips_abicalls)
14077 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14078
14079 /* Set MIPS ELF flags for ASEs. */
14080 /* We may need to define a new flag for DSP ASE, and set this flag when
14081 file_ase_dsp is true. */
14082 /* We may need to define a new flag for MT ASE, and set this flag when
14083 file_ase_mt is true. */
14084 if (file_ase_mips16)
14085 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
14086 #if 0 /* XXX FIXME */
14087 if (file_ase_mips3d)
14088 elf_elfheader (stdoutput)->e_flags |= ???;
14089 #endif
14090 if (file_ase_mdmx)
14091 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
14092
14093 /* Set the MIPS ELF ABI flags. */
14094 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
14095 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
14096 else if (mips_abi == O64_ABI)
14097 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
14098 else if (mips_abi == EABI_ABI)
14099 {
14100 if (!file_mips_gp32)
14101 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14102 else
14103 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14104 }
14105 else if (mips_abi == N32_ABI)
14106 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14107
14108 /* Nothing to do for N64_ABI. */
14109
14110 if (mips_32bitmode)
14111 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14112
14113 #if 0 /* XXX FIXME */
14114 /* 32 bit code with 64 bit FP registers. */
14115 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
14116 elf_elfheader (stdoutput)->e_flags |= ???;
14117 #endif
14118 }
14119
14120 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14121 \f
14122 typedef struct proc {
14123 symbolS *func_sym;
14124 symbolS *func_end_sym;
14125 unsigned long reg_mask;
14126 unsigned long reg_offset;
14127 unsigned long fpreg_mask;
14128 unsigned long fpreg_offset;
14129 unsigned long frame_offset;
14130 unsigned long frame_reg;
14131 unsigned long pc_reg;
14132 } procS;
14133
14134 static procS cur_proc;
14135 static procS *cur_proc_ptr;
14136 static int numprocs;
14137
14138 /* Fill in an rs_align_code fragment. */
14139
14140 void
14141 mips_handle_align (fragS *fragp)
14142 {
14143 if (fragp->fr_type != rs_align_code)
14144 return;
14145
14146 if (mips_opts.mips16)
14147 {
14148 static const unsigned char be_nop[] = { 0x65, 0x00 };
14149 static const unsigned char le_nop[] = { 0x00, 0x65 };
14150
14151 int bytes;
14152 char *p;
14153
14154 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14155 p = fragp->fr_literal + fragp->fr_fix;
14156
14157 if (bytes & 1)
14158 {
14159 *p++ = 0;
14160 fragp->fr_fix++;
14161 }
14162
14163 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14164 fragp->fr_var = 2;
14165 }
14166
14167 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
14168 }
14169
14170 static void
14171 md_obj_begin (void)
14172 {
14173 }
14174
14175 static void
14176 md_obj_end (void)
14177 {
14178 /* Check for premature end, nesting errors, etc. */
14179 if (cur_proc_ptr)
14180 as_warn (_("missing .end at end of assembly"));
14181 }
14182
14183 static long
14184 get_number (void)
14185 {
14186 int negative = 0;
14187 long val = 0;
14188
14189 if (*input_line_pointer == '-')
14190 {
14191 ++input_line_pointer;
14192 negative = 1;
14193 }
14194 if (!ISDIGIT (*input_line_pointer))
14195 as_bad (_("expected simple number"));
14196 if (input_line_pointer[0] == '0')
14197 {
14198 if (input_line_pointer[1] == 'x')
14199 {
14200 input_line_pointer += 2;
14201 while (ISXDIGIT (*input_line_pointer))
14202 {
14203 val <<= 4;
14204 val |= hex_value (*input_line_pointer++);
14205 }
14206 return negative ? -val : val;
14207 }
14208 else
14209 {
14210 ++input_line_pointer;
14211 while (ISDIGIT (*input_line_pointer))
14212 {
14213 val <<= 3;
14214 val |= *input_line_pointer++ - '0';
14215 }
14216 return negative ? -val : val;
14217 }
14218 }
14219 if (!ISDIGIT (*input_line_pointer))
14220 {
14221 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14222 *input_line_pointer, *input_line_pointer);
14223 as_warn (_("invalid number"));
14224 return -1;
14225 }
14226 while (ISDIGIT (*input_line_pointer))
14227 {
14228 val *= 10;
14229 val += *input_line_pointer++ - '0';
14230 }
14231 return negative ? -val : val;
14232 }
14233
14234 /* The .file directive; just like the usual .file directive, but there
14235 is an initial number which is the ECOFF file index. In the non-ECOFF
14236 case .file implies DWARF-2. */
14237
14238 static void
14239 s_mips_file (int x ATTRIBUTE_UNUSED)
14240 {
14241 static int first_file_directive = 0;
14242
14243 if (ECOFF_DEBUGGING)
14244 {
14245 get_number ();
14246 s_app_file (0);
14247 }
14248 else
14249 {
14250 char *filename;
14251
14252 filename = dwarf2_directive_file (0);
14253
14254 /* Versions of GCC up to 3.1 start files with a ".file"
14255 directive even for stabs output. Make sure that this
14256 ".file" is handled. Note that you need a version of GCC
14257 after 3.1 in order to support DWARF-2 on MIPS. */
14258 if (filename != NULL && ! first_file_directive)
14259 {
14260 (void) new_logical_line (filename, -1);
14261 s_app_file_string (filename, 0);
14262 }
14263 first_file_directive = 1;
14264 }
14265 }
14266
14267 /* The .loc directive, implying DWARF-2. */
14268
14269 static void
14270 s_mips_loc (int x ATTRIBUTE_UNUSED)
14271 {
14272 if (!ECOFF_DEBUGGING)
14273 dwarf2_directive_loc (0);
14274 }
14275
14276 /* The .end directive. */
14277
14278 static void
14279 s_mips_end (int x ATTRIBUTE_UNUSED)
14280 {
14281 symbolS *p;
14282
14283 /* Following functions need their own .frame and .cprestore directives. */
14284 mips_frame_reg_valid = 0;
14285 mips_cprestore_valid = 0;
14286
14287 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14288 {
14289 p = get_symbol ();
14290 demand_empty_rest_of_line ();
14291 }
14292 else
14293 p = NULL;
14294
14295 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14296 as_warn (_(".end not in text section"));
14297
14298 if (!cur_proc_ptr)
14299 {
14300 as_warn (_(".end directive without a preceding .ent directive."));
14301 demand_empty_rest_of_line ();
14302 return;
14303 }
14304
14305 if (p != NULL)
14306 {
14307 assert (S_GET_NAME (p));
14308 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
14309 as_warn (_(".end symbol does not match .ent symbol."));
14310
14311 if (debug_type == DEBUG_STABS)
14312 stabs_generate_asm_endfunc (S_GET_NAME (p),
14313 S_GET_NAME (p));
14314 }
14315 else
14316 as_warn (_(".end directive missing or unknown symbol"));
14317
14318 #ifdef OBJ_ELF
14319 /* Create an expression to calculate the size of the function. */
14320 if (p && cur_proc_ptr)
14321 {
14322 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
14323 expressionS *exp = xmalloc (sizeof (expressionS));
14324
14325 obj->size = exp;
14326 exp->X_op = O_subtract;
14327 exp->X_add_symbol = symbol_temp_new_now ();
14328 exp->X_op_symbol = p;
14329 exp->X_add_number = 0;
14330
14331 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
14332 }
14333
14334 /* Generate a .pdr section. */
14335 if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
14336 {
14337 segT saved_seg = now_seg;
14338 subsegT saved_subseg = now_subseg;
14339 valueT dot;
14340 expressionS exp;
14341 char *fragp;
14342
14343 dot = frag_now_fix ();
14344
14345 #ifdef md_flush_pending_output
14346 md_flush_pending_output ();
14347 #endif
14348
14349 assert (pdr_seg);
14350 subseg_set (pdr_seg, 0);
14351
14352 /* Write the symbol. */
14353 exp.X_op = O_symbol;
14354 exp.X_add_symbol = p;
14355 exp.X_add_number = 0;
14356 emit_expr (&exp, 4);
14357
14358 fragp = frag_more (7 * 4);
14359
14360 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14361 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14362 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14363 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14364 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14365 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14366 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
14367
14368 subseg_set (saved_seg, saved_subseg);
14369 }
14370 #endif /* OBJ_ELF */
14371
14372 cur_proc_ptr = NULL;
14373 }
14374
14375 /* The .aent and .ent directives. */
14376
14377 static void
14378 s_mips_ent (int aent)
14379 {
14380 symbolS *symbolP;
14381
14382 symbolP = get_symbol ();
14383 if (*input_line_pointer == ',')
14384 ++input_line_pointer;
14385 SKIP_WHITESPACE ();
14386 if (ISDIGIT (*input_line_pointer)
14387 || *input_line_pointer == '-')
14388 get_number ();
14389
14390 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14391 as_warn (_(".ent or .aent not in text section."));
14392
14393 if (!aent && cur_proc_ptr)
14394 as_warn (_("missing .end"));
14395
14396 if (!aent)
14397 {
14398 /* This function needs its own .frame and .cprestore directives. */
14399 mips_frame_reg_valid = 0;
14400 mips_cprestore_valid = 0;
14401
14402 cur_proc_ptr = &cur_proc;
14403 memset (cur_proc_ptr, '\0', sizeof (procS));
14404
14405 cur_proc_ptr->func_sym = symbolP;
14406
14407 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14408
14409 ++numprocs;
14410
14411 if (debug_type == DEBUG_STABS)
14412 stabs_generate_asm_func (S_GET_NAME (symbolP),
14413 S_GET_NAME (symbolP));
14414 }
14415
14416 demand_empty_rest_of_line ();
14417 }
14418
14419 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14420 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14421 s_mips_frame is used so that we can set the PDR information correctly.
14422 We can't use the ecoff routines because they make reference to the ecoff
14423 symbol table (in the mdebug section). */
14424
14425 static void
14426 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
14427 {
14428 #ifdef OBJ_ELF
14429 if (IS_ELF && !ECOFF_DEBUGGING)
14430 {
14431 long val;
14432
14433 if (cur_proc_ptr == (procS *) NULL)
14434 {
14435 as_warn (_(".frame outside of .ent"));
14436 demand_empty_rest_of_line ();
14437 return;
14438 }
14439
14440 cur_proc_ptr->frame_reg = tc_get_register (1);
14441
14442 SKIP_WHITESPACE ();
14443 if (*input_line_pointer++ != ','
14444 || get_absolute_expression_and_terminator (&val) != ',')
14445 {
14446 as_warn (_("Bad .frame directive"));
14447 --input_line_pointer;
14448 demand_empty_rest_of_line ();
14449 return;
14450 }
14451
14452 cur_proc_ptr->frame_offset = val;
14453 cur_proc_ptr->pc_reg = tc_get_register (0);
14454
14455 demand_empty_rest_of_line ();
14456 }
14457 else
14458 #endif /* OBJ_ELF */
14459 s_ignore (ignore);
14460 }
14461
14462 /* The .fmask and .mask directives. If the mdebug section is present
14463 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14464 embedded targets, s_mips_mask is used so that we can set the PDR
14465 information correctly. We can't use the ecoff routines because they
14466 make reference to the ecoff symbol table (in the mdebug section). */
14467
14468 static void
14469 s_mips_mask (int reg_type)
14470 {
14471 #ifdef OBJ_ELF
14472 if (IS_ELF && !ECOFF_DEBUGGING)
14473 {
14474 long mask, off;
14475
14476 if (cur_proc_ptr == (procS *) NULL)
14477 {
14478 as_warn (_(".mask/.fmask outside of .ent"));
14479 demand_empty_rest_of_line ();
14480 return;
14481 }
14482
14483 if (get_absolute_expression_and_terminator (&mask) != ',')
14484 {
14485 as_warn (_("Bad .mask/.fmask directive"));
14486 --input_line_pointer;
14487 demand_empty_rest_of_line ();
14488 return;
14489 }
14490
14491 off = get_absolute_expression ();
14492
14493 if (reg_type == 'F')
14494 {
14495 cur_proc_ptr->fpreg_mask = mask;
14496 cur_proc_ptr->fpreg_offset = off;
14497 }
14498 else
14499 {
14500 cur_proc_ptr->reg_mask = mask;
14501 cur_proc_ptr->reg_offset = off;
14502 }
14503
14504 demand_empty_rest_of_line ();
14505 }
14506 else
14507 #endif /* OBJ_ELF */
14508 s_ignore (reg_type);
14509 }
14510
14511 /* A table describing all the processors gas knows about. Names are
14512 matched in the order listed.
14513
14514 To ease comparison, please keep this table in the same order as
14515 gcc's mips_cpu_info_table[]. */
14516 static const struct mips_cpu_info mips_cpu_info_table[] =
14517 {
14518 /* Entries for generic ISAs */
14519 { "mips1", MIPS_CPU_IS_ISA, ISA_MIPS1, CPU_R3000 },
14520 { "mips2", MIPS_CPU_IS_ISA, ISA_MIPS2, CPU_R6000 },
14521 { "mips3", MIPS_CPU_IS_ISA, ISA_MIPS3, CPU_R4000 },
14522 { "mips4", MIPS_CPU_IS_ISA, ISA_MIPS4, CPU_R8000 },
14523 { "mips5", MIPS_CPU_IS_ISA, ISA_MIPS5, CPU_MIPS5 },
14524 { "mips32", MIPS_CPU_IS_ISA, ISA_MIPS32, CPU_MIPS32 },
14525 { "mips32r2", MIPS_CPU_IS_ISA, ISA_MIPS32R2, CPU_MIPS32R2 },
14526 { "mips64", MIPS_CPU_IS_ISA, ISA_MIPS64, CPU_MIPS64 },
14527 { "mips64r2", MIPS_CPU_IS_ISA, ISA_MIPS64R2, CPU_MIPS64R2 },
14528
14529 /* MIPS I */
14530 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14531 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14532 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14533
14534 /* MIPS II */
14535 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14536
14537 /* MIPS III */
14538 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14539 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14540 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14541 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
14542 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14543 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14544 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
14545 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14546 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14547 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14548 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14549 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14550
14551 /* MIPS IV */
14552 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14553 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14554 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14555 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
14556 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14557 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
14558 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14559 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14560 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14561 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14562 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14563 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
14564 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
14565
14566 /* MIPS 32 */
14567 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
14568 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14569 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
14570 { "4ksc", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
14571
14572 /* MIPS 32 Release 2 */
14573 { "4kec", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14574 { "4kem", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14575 { "4kep", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14576 { "4ksd", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
14577 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14578 { "m4kp", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14579 { "24k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14580 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14581 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14582 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14583 /* 24ke is a 24k with DSP ASE, other ASEs are optional. */
14584 { "24ke", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
14585 { "24kec", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
14586 { "24kef", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
14587 { "24kex", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
14588 /* 34k is a 24k with MT ASE, other ASEs are optional. */
14589 { "34kc", MIPS_CPU_ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
14590 { "34kf", MIPS_CPU_ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
14591 { "34kx", MIPS_CPU_ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
14592
14593 /* MIPS 64 */
14594 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14595 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
14596 { "20kc", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
14597
14598 /* MIPS 64 Release 2 */
14599 { "25kf", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64R2, CPU_MIPS64R2 },
14600
14601 /* Broadcom SB-1 CPU core */
14602 { "sb1", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
14603 ISA_MIPS64, CPU_SB1 },
14604 /* Broadcom SB-1A CPU core */
14605 { "sb1a", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
14606 ISA_MIPS64, CPU_SB1 },
14607
14608 /* End marker */
14609 { NULL, 0, 0, 0 }
14610 };
14611
14612
14613 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14614 with a final "000" replaced by "k". Ignore case.
14615
14616 Note: this function is shared between GCC and GAS. */
14617
14618 static bfd_boolean
14619 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
14620 {
14621 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14622 given++, canonical++;
14623
14624 return ((*given == 0 && *canonical == 0)
14625 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14626 }
14627
14628
14629 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14630 CPU name. We've traditionally allowed a lot of variation here.
14631
14632 Note: this function is shared between GCC and GAS. */
14633
14634 static bfd_boolean
14635 mips_matching_cpu_name_p (const char *canonical, const char *given)
14636 {
14637 /* First see if the name matches exactly, or with a final "000"
14638 turned into "k". */
14639 if (mips_strict_matching_cpu_name_p (canonical, given))
14640 return TRUE;
14641
14642 /* If not, try comparing based on numerical designation alone.
14643 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14644 if (TOLOWER (*given) == 'r')
14645 given++;
14646 if (!ISDIGIT (*given))
14647 return FALSE;
14648
14649 /* Skip over some well-known prefixes in the canonical name,
14650 hoping to find a number there too. */
14651 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14652 canonical += 2;
14653 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14654 canonical += 2;
14655 else if (TOLOWER (canonical[0]) == 'r')
14656 canonical += 1;
14657
14658 return mips_strict_matching_cpu_name_p (canonical, given);
14659 }
14660
14661
14662 /* Parse an option that takes the name of a processor as its argument.
14663 OPTION is the name of the option and CPU_STRING is the argument.
14664 Return the corresponding processor enumeration if the CPU_STRING is
14665 recognized, otherwise report an error and return null.
14666
14667 A similar function exists in GCC. */
14668
14669 static const struct mips_cpu_info *
14670 mips_parse_cpu (const char *option, const char *cpu_string)
14671 {
14672 const struct mips_cpu_info *p;
14673
14674 /* 'from-abi' selects the most compatible architecture for the given
14675 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14676 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14677 version. Look first at the -mgp options, if given, otherwise base
14678 the choice on MIPS_DEFAULT_64BIT.
14679
14680 Treat NO_ABI like the EABIs. One reason to do this is that the
14681 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14682 architecture. This code picks MIPS I for 'mips' and MIPS III for
14683 'mips64', just as we did in the days before 'from-abi'. */
14684 if (strcasecmp (cpu_string, "from-abi") == 0)
14685 {
14686 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14687 return mips_cpu_info_from_isa (ISA_MIPS1);
14688
14689 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14690 return mips_cpu_info_from_isa (ISA_MIPS3);
14691
14692 if (file_mips_gp32 >= 0)
14693 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14694
14695 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14696 ? ISA_MIPS3
14697 : ISA_MIPS1);
14698 }
14699
14700 /* 'default' has traditionally been a no-op. Probably not very useful. */
14701 if (strcasecmp (cpu_string, "default") == 0)
14702 return 0;
14703
14704 for (p = mips_cpu_info_table; p->name != 0; p++)
14705 if (mips_matching_cpu_name_p (p->name, cpu_string))
14706 return p;
14707
14708 as_bad ("Bad value (%s) for %s", cpu_string, option);
14709 return 0;
14710 }
14711
14712 /* Return the canonical processor information for ISA (a member of the
14713 ISA_MIPS* enumeration). */
14714
14715 static const struct mips_cpu_info *
14716 mips_cpu_info_from_isa (int isa)
14717 {
14718 int i;
14719
14720 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14721 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
14722 && isa == mips_cpu_info_table[i].isa)
14723 return (&mips_cpu_info_table[i]);
14724
14725 return NULL;
14726 }
14727
14728 static const struct mips_cpu_info *
14729 mips_cpu_info_from_arch (int arch)
14730 {
14731 int i;
14732
14733 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14734 if (arch == mips_cpu_info_table[i].cpu)
14735 return (&mips_cpu_info_table[i]);
14736
14737 return NULL;
14738 }
14739 \f
14740 static void
14741 show (FILE *stream, const char *string, int *col_p, int *first_p)
14742 {
14743 if (*first_p)
14744 {
14745 fprintf (stream, "%24s", "");
14746 *col_p = 24;
14747 }
14748 else
14749 {
14750 fprintf (stream, ", ");
14751 *col_p += 2;
14752 }
14753
14754 if (*col_p + strlen (string) > 72)
14755 {
14756 fprintf (stream, "\n%24s", "");
14757 *col_p = 24;
14758 }
14759
14760 fprintf (stream, "%s", string);
14761 *col_p += strlen (string);
14762
14763 *first_p = 0;
14764 }
14765
14766 void
14767 md_show_usage (FILE *stream)
14768 {
14769 int column, first;
14770 size_t i;
14771
14772 fprintf (stream, _("\
14773 MIPS options:\n\
14774 -EB generate big endian output\n\
14775 -EL generate little endian output\n\
14776 -g, -g2 do not remove unneeded NOPs or swap branches\n\
14777 -G NUM allow referencing objects up to NUM bytes\n\
14778 implicitly with the gp register [default 8]\n"));
14779 fprintf (stream, _("\
14780 -mips1 generate MIPS ISA I instructions\n\
14781 -mips2 generate MIPS ISA II instructions\n\
14782 -mips3 generate MIPS ISA III instructions\n\
14783 -mips4 generate MIPS ISA IV instructions\n\
14784 -mips5 generate MIPS ISA V instructions\n\
14785 -mips32 generate MIPS32 ISA instructions\n\
14786 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
14787 -mips64 generate MIPS64 ISA instructions\n\
14788 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
14789 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14790
14791 first = 1;
14792
14793 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14794 show (stream, mips_cpu_info_table[i].name, &column, &first);
14795 show (stream, "from-abi", &column, &first);
14796 fputc ('\n', stream);
14797
14798 fprintf (stream, _("\
14799 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14800 -no-mCPU don't generate code specific to CPU.\n\
14801 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14802
14803 first = 1;
14804
14805 show (stream, "3900", &column, &first);
14806 show (stream, "4010", &column, &first);
14807 show (stream, "4100", &column, &first);
14808 show (stream, "4650", &column, &first);
14809 fputc ('\n', stream);
14810
14811 fprintf (stream, _("\
14812 -mips16 generate mips16 instructions\n\
14813 -no-mips16 do not generate mips16 instructions\n"));
14814 fprintf (stream, _("\
14815 -msmartmips generate smartmips instructions\n\
14816 -mno-smartmips do not generate smartmips instructions\n"));
14817 fprintf (stream, _("\
14818 -mdsp generate DSP instructions\n\
14819 -mno-dsp do not generate DSP instructions\n"));
14820 fprintf (stream, _("\
14821 -mmt generate MT instructions\n\
14822 -mno-mt do not generate MT instructions\n"));
14823 fprintf (stream, _("\
14824 -mfix-vr4120 work around certain VR4120 errata\n\
14825 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
14826 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14827 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14828 -mno-shared optimize output for executables\n\
14829 -msym32 assume all symbols have 32-bit values\n\
14830 -O0 remove unneeded NOPs, do not swap branches\n\
14831 -O remove unneeded NOPs and swap branches\n\
14832 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14833 --trap, --no-break trap exception on div by 0 and mult overflow\n\
14834 --break, --no-trap break exception on div by 0 and mult overflow\n"));
14835 #ifdef OBJ_ELF
14836 fprintf (stream, _("\
14837 -KPIC, -call_shared generate SVR4 position independent code\n\
14838 -non_shared do not generate position independent code\n\
14839 -xgot assume a 32 bit GOT\n\
14840 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
14841 -mshared, -mno-shared disable/enable .cpload optimization for\n\
14842 non-shared code\n\
14843 -mabi=ABI create ABI conformant object file for:\n"));
14844
14845 first = 1;
14846
14847 show (stream, "32", &column, &first);
14848 show (stream, "o64", &column, &first);
14849 show (stream, "n32", &column, &first);
14850 show (stream, "64", &column, &first);
14851 show (stream, "eabi", &column, &first);
14852
14853 fputc ('\n', stream);
14854
14855 fprintf (stream, _("\
14856 -32 create o32 ABI object file (default)\n\
14857 -n32 create n32 ABI object file\n\
14858 -64 create 64 ABI object file\n"));
14859 #endif
14860 }
14861
14862 enum dwarf2_format
14863 mips_dwarf2_format (void)
14864 {
14865 if (mips_abi == N64_ABI)
14866 {
14867 #ifdef TE_IRIX
14868 return dwarf2_format_64bit_irix;
14869 #else
14870 return dwarf2_format_64bit;
14871 #endif
14872 }
14873 else
14874 return dwarf2_format_32bit;
14875 }
14876
14877 int
14878 mips_dwarf2_addr_size (void)
14879 {
14880 if (mips_abi == N64_ABI)
14881 return 8;
14882 else
14883 return 4;
14884 }
14885
14886 /* Standard calling conventions leave the CFA at SP on entry. */
14887 void
14888 mips_cfi_frame_initial_instructions (void)
14889 {
14890 cfi_add_CFA_def_cfa_register (SP);
14891 }
14892
14893 int
14894 tc_mips_regname_to_dw2regnum (char *regname)
14895 {
14896 unsigned int regnum = -1;
14897 unsigned int reg;
14898
14899 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
14900 regnum = reg;
14901
14902 return regnum;
14903 }
This page took 0.511378 seconds and 5 git commands to generate.