* config/tc-mips.c (normalize_constant_expr): New function to fix sign
[binutils.git] / gas / config / tc-mips.c
blobc34197223e22bc4e474064dddfaa41452d8859a6
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 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.
9 This file is part of GAS.
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.
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.
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, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
31 #include <stdarg.h>
33 #include "opcode/mips.h"
34 #include "itbl-ops.h"
35 #include "dwarf2dbg.h"
37 #ifdef DEBUG
38 #define DBG(x) printf x
39 #else
40 #define DBG(x)
41 #endif
43 #ifdef OBJ_MAYBE_ELF
44 /* Clean up namespace so we can include obj-elf.h too. */
45 static int mips_output_flavor (void);
46 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
47 #undef OBJ_PROCESS_STAB
48 #undef OUTPUT_FLAVOR
49 #undef S_GET_ALIGN
50 #undef S_GET_SIZE
51 #undef S_SET_ALIGN
52 #undef S_SET_SIZE
53 #undef obj_frob_file
54 #undef obj_frob_file_after_relocs
55 #undef obj_frob_symbol
56 #undef obj_pop_insert
57 #undef obj_sec_sym_ok_for_reloc
58 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
60 #include "obj-elf.h"
61 /* Fix any of them that we actually care about. */
62 #undef OUTPUT_FLAVOR
63 #define OUTPUT_FLAVOR mips_output_flavor()
64 #endif
66 #if defined (OBJ_ELF)
67 #include "elf/mips.h"
68 #endif
70 #ifndef ECOFF_DEBUGGING
71 #define NO_ECOFF_DEBUGGING
72 #define ECOFF_DEBUGGING 0
73 #endif
75 int mips_flag_mdebug = -1;
77 /* Control generation of .pdr sections. Off by default on IRIX: the native
78 linker doesn't know about and discards them, but relocations against them
79 remain, leading to rld crashes. */
80 #ifdef TE_IRIX
81 int mips_flag_pdr = FALSE;
82 #else
83 int mips_flag_pdr = TRUE;
84 #endif
86 #include "ecoff.h"
88 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89 static char *mips_regmask_frag;
90 #endif
92 #define ZERO 0
93 #define AT 1
94 #define TREG 24
95 #define PIC_CALL_REG 25
96 #define KT0 26
97 #define KT1 27
98 #define GP 28
99 #define SP 29
100 #define FP 30
101 #define RA 31
103 #define ILLEGAL_REG (32)
105 /* Allow override of standard little-endian ECOFF format. */
107 #ifndef ECOFF_LITTLE_FORMAT
108 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
109 #endif
111 extern int target_big_endian;
113 /* The name of the readonly data section. */
114 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
115 ? ".data" \
116 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
117 ? ".rdata" \
118 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
119 ? ".rdata" \
120 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
121 ? ".rodata" \
122 : (abort (), ""))
124 /* The ABI to use. */
125 enum mips_abi_level
127 NO_ABI = 0,
128 O32_ABI,
129 O64_ABI,
130 N32_ABI,
131 N64_ABI,
132 EABI_ABI
135 /* MIPS ABI we are using for this output file. */
136 static enum mips_abi_level mips_abi = NO_ABI;
138 /* Whether or not we have code that can call pic code. */
139 int mips_abicalls = FALSE;
141 /* This is the set of options which may be modified by the .set
142 pseudo-op. We use a struct so that .set push and .set pop are more
143 reliable. */
145 struct mips_set_options
147 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
148 if it has not been initialized. Changed by `.set mipsN', and the
149 -mipsN command line option, and the default CPU. */
150 int isa;
151 /* Enabled Application Specific Extensions (ASEs). These are set to -1
152 if they have not been initialized. Changed by `.set <asename>', by
153 command line options, and based on the default architecture. */
154 int ase_mips3d;
155 int ase_mdmx;
156 /* Whether we are assembling for the mips16 processor. 0 if we are
157 not, 1 if we are, and -1 if the value has not been initialized.
158 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
159 -nomips16 command line options, and the default CPU. */
160 int mips16;
161 /* Non-zero if we should not reorder instructions. Changed by `.set
162 reorder' and `.set noreorder'. */
163 int noreorder;
164 /* Non-zero if we should not permit the $at ($1) register to be used
165 in instructions. Changed by `.set at' and `.set noat'. */
166 int noat;
167 /* Non-zero if we should warn when a macro instruction expands into
168 more than one machine instruction. Changed by `.set nomacro' and
169 `.set macro'. */
170 int warn_about_macros;
171 /* Non-zero if we should not move instructions. Changed by `.set
172 move', `.set volatile', `.set nomove', and `.set novolatile'. */
173 int nomove;
174 /* Non-zero if we should not optimize branches by moving the target
175 of the branch into the delay slot. Actually, we don't perform
176 this optimization anyhow. Changed by `.set bopt' and `.set
177 nobopt'. */
178 int nobopt;
179 /* Non-zero if we should not autoextend mips16 instructions.
180 Changed by `.set autoextend' and `.set noautoextend'. */
181 int noautoextend;
182 /* Restrict general purpose registers and floating point registers
183 to 32 bit. This is initially determined when -mgp32 or -mfp32
184 is passed but can changed if the assembler code uses .set mipsN. */
185 int gp32;
186 int fp32;
187 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
188 command line option, and the default CPU. */
189 int arch;
192 /* True if -mgp32 was passed. */
193 static int file_mips_gp32 = -1;
195 /* True if -mfp32 was passed. */
196 static int file_mips_fp32 = -1;
198 /* This is the struct we use to hold the current set of options. Note
199 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
200 -1 to indicate that they have not been initialized. */
202 static struct mips_set_options mips_opts =
204 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN
207 /* These variables are filled in with the masks of registers used.
208 The object format code reads them and puts them in the appropriate
209 place. */
210 unsigned long mips_gprmask;
211 unsigned long mips_cprmask[4];
213 /* MIPS ISA we are using for this output file. */
214 static int file_mips_isa = ISA_UNKNOWN;
216 /* True if -mips16 was passed or implied by arguments passed on the
217 command line (e.g., by -march). */
218 static int file_ase_mips16;
220 /* True if -mips3d was passed or implied by arguments passed on the
221 command line (e.g., by -march). */
222 static int file_ase_mips3d;
224 /* True if -mdmx was passed or implied by arguments passed on the
225 command line (e.g., by -march). */
226 static int file_ase_mdmx;
228 /* The argument of the -march= flag. The architecture we are assembling. */
229 static int file_mips_arch = CPU_UNKNOWN;
230 static const char *mips_arch_string;
232 /* The argument of the -mtune= flag. The architecture for which we
233 are optimizing. */
234 static int mips_tune = CPU_UNKNOWN;
235 static const char *mips_tune_string;
237 /* True when generating 32-bit code for a 64-bit processor. */
238 static int mips_32bitmode = 0;
240 /* Some ISA's have delay slots for instructions which read or write
241 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
242 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
243 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
244 delay slot in this ISA. The uses of this macro assume that any
245 ISA that has delay slots for one of these, has them for all. They
246 also assume that ISAs which don't have delays for these insns, don't
247 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
248 #define ISA_HAS_COPROC_DELAYS(ISA) ( \
249 (ISA) == ISA_MIPS1 \
250 || (ISA) == ISA_MIPS2 \
251 || (ISA) == ISA_MIPS3 \
254 /* True if the given ABI requires 32-bit registers. */
255 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
257 /* Likewise 64-bit registers. */
258 #define ABI_NEEDS_64BIT_REGS(ABI) \
259 ((ABI) == N32_ABI \
260 || (ABI) == N64_ABI \
261 || (ABI) == O64_ABI)
263 /* Return true if ISA supports 64 bit gp register instructions. */
264 #define ISA_HAS_64BIT_REGS(ISA) ( \
265 (ISA) == ISA_MIPS3 \
266 || (ISA) == ISA_MIPS4 \
267 || (ISA) == ISA_MIPS5 \
268 || (ISA) == ISA_MIPS64 \
269 || (ISA) == ISA_MIPS64R2 \
272 /* Return true if ISA supports 64-bit right rotate (dror et al.)
273 instructions. */
274 #define ISA_HAS_DROR(ISA) ( \
275 (ISA) == ISA_MIPS64R2 \
278 /* Return true if ISA supports 32-bit right rotate (ror et al.)
279 instructions. */
280 #define ISA_HAS_ROR(ISA) ( \
281 (ISA) == ISA_MIPS32R2 \
282 || (ISA) == ISA_MIPS64R2 \
285 #define HAVE_32BIT_GPRS \
286 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
288 #define HAVE_32BIT_FPRS \
289 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
291 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
292 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
294 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
296 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
298 /* We can only have 64bit addresses if the object file format
299 supports it. */
300 #define HAVE_32BIT_ADDRESSES \
301 (HAVE_32BIT_GPRS \
302 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
303 || ! HAVE_64BIT_OBJECTS) \
304 && mips_pic != EMBEDDED_PIC))
306 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
308 /* Addresses are loaded in different ways, depending on the address size
309 in use. The n32 ABI Documentation also mandates the use of additions
310 with overflow checking, but existing implementations don't follow it. */
311 #define ADDRESS_ADD_INSN \
312 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
314 #define ADDRESS_ADDI_INSN \
315 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
317 #define ADDRESS_LOAD_INSN \
318 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
320 #define ADDRESS_STORE_INSN \
321 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
323 /* Return true if the given CPU supports the MIPS16 ASE. */
324 #define CPU_HAS_MIPS16(cpu) \
325 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
326 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
328 /* Return true if the given CPU supports the MIPS3D ASE. */
329 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
332 /* Return true if the given CPU supports the MDMX ASE. */
333 #define CPU_HAS_MDMX(cpu) (FALSE \
336 /* True if CPU has a dror instruction. */
337 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
339 /* True if CPU has a ror instruction. */
340 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
342 /* Whether the processor uses hardware interlocks to protect
343 reads from the HI and LO registers, and thus does not
344 require nops to be inserted. */
346 #define hilo_interlocks (mips_opts.arch == CPU_R4010 \
347 || mips_opts.arch == CPU_VR5500 \
348 || mips_opts.arch == CPU_RM7000 \
349 || mips_opts.arch == CPU_SB1 \
352 /* Whether the processor uses hardware interlocks to protect reads
353 from the GPRs, and thus does not require nops to be inserted. */
354 #define gpr_interlocks \
355 (mips_opts.isa != ISA_MIPS1 \
356 || mips_opts.arch == CPU_VR5400 \
357 || mips_opts.arch == CPU_VR5500 \
358 || mips_opts.arch == CPU_R3900)
360 /* As with other "interlocks" this is used by hardware that has FP
361 (co-processor) interlocks. */
362 /* Itbl support may require additional care here. */
363 #define cop_interlocks (mips_opts.arch == CPU_R4300 \
364 || mips_opts.arch == CPU_VR5400 \
365 || mips_opts.arch == CPU_VR5500 \
366 || mips_opts.arch == CPU_SB1 \
369 /* Is this a mfhi or mflo instruction? */
370 #define MF_HILO_INSN(PINFO) \
371 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
373 /* MIPS PIC level. */
375 enum mips_pic_level mips_pic;
377 /* Warn about all NOPS that the assembler generates. */
378 static int warn_nops = 0;
380 /* 1 if we should generate 32 bit offsets from the $gp register in
381 SVR4_PIC mode. Currently has no meaning in other modes. */
382 static int mips_big_got = 0;
384 /* 1 if trap instructions should used for overflow rather than break
385 instructions. */
386 static int mips_trap = 0;
388 /* 1 if double width floating point constants should not be constructed
389 by assembling two single width halves into two single width floating
390 point registers which just happen to alias the double width destination
391 register. On some architectures this aliasing can be disabled by a bit
392 in the status register, and the setting of this bit cannot be determined
393 automatically at assemble time. */
394 static int mips_disable_float_construction;
396 /* Non-zero if any .set noreorder directives were used. */
398 static int mips_any_noreorder;
400 /* Non-zero if nops should be inserted when the register referenced in
401 an mfhi/mflo instruction is read in the next two instructions. */
402 static int mips_7000_hilo_fix;
404 /* The size of the small data section. */
405 static unsigned int g_switch_value = 8;
406 /* Whether the -G option was used. */
407 static int g_switch_seen = 0;
409 #define N_RMASK 0xc4
410 #define N_VFP 0xd4
412 /* If we can determine in advance that GP optimization won't be
413 possible, we can skip the relaxation stuff that tries to produce
414 GP-relative references. This makes delay slot optimization work
415 better.
417 This function can only provide a guess, but it seems to work for
418 gcc output. It needs to guess right for gcc, otherwise gcc
419 will put what it thinks is a GP-relative instruction in a branch
420 delay slot.
422 I don't know if a fix is needed for the SVR4_PIC mode. I've only
423 fixed it for the non-PIC mode. KR 95/04/07 */
424 static int nopic_need_relax (symbolS *, int);
426 /* handle of the OPCODE hash table */
427 static struct hash_control *op_hash = NULL;
429 /* The opcode hash table we use for the mips16. */
430 static struct hash_control *mips16_op_hash = NULL;
432 /* This array holds the chars that always start a comment. If the
433 pre-processor is disabled, these aren't very useful */
434 const char comment_chars[] = "#";
436 /* This array holds the chars that only start a comment at the beginning of
437 a line. If the line seems to have the form '# 123 filename'
438 .line and .file directives will appear in the pre-processed output */
439 /* Note that input_file.c hand checks for '#' at the beginning of the
440 first line of the input file. This is because the compiler outputs
441 #NO_APP at the beginning of its output. */
442 /* Also note that C style comments are always supported. */
443 const char line_comment_chars[] = "#";
445 /* This array holds machine specific line separator characters. */
446 const char line_separator_chars[] = ";";
448 /* Chars that can be used to separate mant from exp in floating point nums */
449 const char EXP_CHARS[] = "eE";
451 /* Chars that mean this number is a floating point constant */
452 /* As in 0f12.456 */
453 /* or 0d1.2345e12 */
454 const char FLT_CHARS[] = "rRsSfFdDxXpP";
456 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
457 changed in read.c . Ideally it shouldn't have to know about it at all,
458 but nothing is ideal around here.
461 static char *insn_error;
463 static int auto_align = 1;
465 /* When outputting SVR4 PIC code, the assembler needs to know the
466 offset in the stack frame from which to restore the $gp register.
467 This is set by the .cprestore pseudo-op, and saved in this
468 variable. */
469 static offsetT mips_cprestore_offset = -1;
471 /* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
472 more optimizations, it can use a register value instead of a memory-saved
473 offset and even an other register than $gp as global pointer. */
474 static offsetT mips_cpreturn_offset = -1;
475 static int mips_cpreturn_register = -1;
476 static int mips_gp_register = GP;
477 static int mips_gprel_offset = 0;
479 /* Whether mips_cprestore_offset has been set in the current function
480 (or whether it has already been warned about, if not). */
481 static int mips_cprestore_valid = 0;
483 /* This is the register which holds the stack frame, as set by the
484 .frame pseudo-op. This is needed to implement .cprestore. */
485 static int mips_frame_reg = SP;
487 /* Whether mips_frame_reg has been set in the current function
488 (or whether it has already been warned about, if not). */
489 static int mips_frame_reg_valid = 0;
491 /* To output NOP instructions correctly, we need to keep information
492 about the previous two instructions. */
494 /* Whether we are optimizing. The default value of 2 means to remove
495 unneeded NOPs and swap branch instructions when possible. A value
496 of 1 means to not swap branches. A value of 0 means to always
497 insert NOPs. */
498 static int mips_optimize = 2;
500 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
501 equivalent to seeing no -g option at all. */
502 static int mips_debug = 0;
504 /* The previous instruction. */
505 static struct mips_cl_insn prev_insn;
507 /* The instruction before prev_insn. */
508 static struct mips_cl_insn prev_prev_insn;
510 /* If we don't want information for prev_insn or prev_prev_insn, we
511 point the insn_mo field at this dummy integer. */
512 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
514 /* Non-zero if prev_insn is valid. */
515 static int prev_insn_valid;
517 /* The frag for the previous instruction. */
518 static struct frag *prev_insn_frag;
520 /* The offset into prev_insn_frag for the previous instruction. */
521 static long prev_insn_where;
523 /* The reloc type for the previous instruction, if any. */
524 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
526 /* The reloc for the previous instruction, if any. */
527 static fixS *prev_insn_fixp[3];
529 /* Non-zero if the previous instruction was in a delay slot. */
530 static int prev_insn_is_delay_slot;
532 /* Non-zero if the previous instruction was in a .set noreorder. */
533 static int prev_insn_unreordered;
535 /* Non-zero if the previous instruction uses an extend opcode (if
536 mips16). */
537 static int prev_insn_extended;
539 /* Non-zero if the previous previous instruction was in a .set
540 noreorder. */
541 static int prev_prev_insn_unreordered;
543 /* If this is set, it points to a frag holding nop instructions which
544 were inserted before the start of a noreorder section. If those
545 nops turn out to be unnecessary, the size of the frag can be
546 decreased. */
547 static fragS *prev_nop_frag;
549 /* The number of nop instructions we created in prev_nop_frag. */
550 static int prev_nop_frag_holds;
552 /* The number of nop instructions that we know we need in
553 prev_nop_frag. */
554 static int prev_nop_frag_required;
556 /* The number of instructions we've seen since prev_nop_frag. */
557 static int prev_nop_frag_since;
559 /* For ECOFF and ELF, relocations against symbols are done in two
560 parts, with a HI relocation and a LO relocation. Each relocation
561 has only 16 bits of space to store an addend. This means that in
562 order for the linker to handle carries correctly, it must be able
563 to locate both the HI and the LO relocation. This means that the
564 relocations must appear in order in the relocation table.
566 In order to implement this, we keep track of each unmatched HI
567 relocation. We then sort them so that they immediately precede the
568 corresponding LO relocation. */
570 struct mips_hi_fixup
572 /* Next HI fixup. */
573 struct mips_hi_fixup *next;
574 /* This fixup. */
575 fixS *fixp;
576 /* The section this fixup is in. */
577 segT seg;
580 /* The list of unmatched HI relocs. */
582 static struct mips_hi_fixup *mips_hi_fixup_list;
584 /* The frag containing the last explicit relocation operator.
585 Null if explicit relocations have not been used. */
587 static fragS *prev_reloc_op_frag;
589 /* Map normal MIPS register numbers to mips16 register numbers. */
591 #define X ILLEGAL_REG
592 static const int mips32_to_16_reg_map[] =
594 X, X, 2, 3, 4, 5, 6, 7,
595 X, X, X, X, X, X, X, X,
596 0, 1, X, X, X, X, X, X,
597 X, X, X, X, X, X, X, X
599 #undef X
601 /* Map mips16 register numbers to normal MIPS register numbers. */
603 static const unsigned int mips16_to_32_reg_map[] =
605 16, 17, 2, 3, 4, 5, 6, 7
608 static int mips_fix_4122_bugs;
610 /* We don't relax branches by default, since this causes us to expand
611 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
612 fail to compute the offset before expanding the macro to the most
613 efficient expansion. */
615 static int mips_relax_branch;
617 /* Since the MIPS does not have multiple forms of PC relative
618 instructions, we do not have to do relaxing as is done on other
619 platforms. However, we do have to handle GP relative addressing
620 correctly, which turns out to be a similar problem.
622 Every macro that refers to a symbol can occur in (at least) two
623 forms, one with GP relative addressing and one without. For
624 example, loading a global variable into a register generally uses
625 a macro instruction like this:
626 lw $4,i
627 If i can be addressed off the GP register (this is true if it is in
628 the .sbss or .sdata section, or if it is known to be smaller than
629 the -G argument) this will generate the following instruction:
630 lw $4,i($gp)
631 This instruction will use a GPREL reloc. If i can not be addressed
632 off the GP register, the following instruction sequence will be used:
633 lui $at,i
634 lw $4,i($at)
635 In this case the first instruction will have a HI16 reloc, and the
636 second reloc will have a LO16 reloc. Both relocs will be against
637 the symbol i.
639 The issue here is that we may not know whether i is GP addressable
640 until after we see the instruction that uses it. Therefore, we
641 want to be able to choose the final instruction sequence only at
642 the end of the assembly. This is similar to the way other
643 platforms choose the size of a PC relative instruction only at the
644 end of assembly.
646 When generating position independent code we do not use GP
647 addressing in quite the same way, but the issue still arises as
648 external symbols and local symbols must be handled differently.
650 We handle these issues by actually generating both possible
651 instruction sequences. The longer one is put in a frag_var with
652 type rs_machine_dependent. We encode what to do with the frag in
653 the subtype field. We encode (1) the number of existing bytes to
654 replace, (2) the number of new bytes to use, (3) the offset from
655 the start of the existing bytes to the first reloc we must generate
656 (that is, the offset is applied from the start of the existing
657 bytes after they are replaced by the new bytes, if any), (4) the
658 offset from the start of the existing bytes to the second reloc,
659 (5) whether a third reloc is needed (the third reloc is always four
660 bytes after the second reloc), and (6) whether to warn if this
661 variant is used (this is sometimes needed if .set nomacro or .set
662 noat is in effect). All these numbers are reasonably small.
664 Generating two instruction sequences must be handled carefully to
665 ensure that delay slots are handled correctly. Fortunately, there
666 are a limited number of cases. When the second instruction
667 sequence is generated, append_insn is directed to maintain the
668 existing delay slot information, so it continues to apply to any
669 code after the second instruction sequence. This means that the
670 second instruction sequence must not impose any requirements not
671 required by the first instruction sequence.
673 These variant frags are then handled in functions called by the
674 machine independent code. md_estimate_size_before_relax returns
675 the final size of the frag. md_convert_frag sets up the final form
676 of the frag. tc_gen_reloc adjust the first reloc and adds a second
677 one if needed. */
678 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
679 ((relax_substateT) \
680 (((old) << 23) \
681 | ((new) << 16) \
682 | (((reloc1) + 64) << 9) \
683 | (((reloc2) + 64) << 2) \
684 | ((reloc3) ? (1 << 1) : 0) \
685 | ((warn) ? 1 : 0)))
686 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
687 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
688 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
689 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
690 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
691 #define RELAX_WARN(i) ((i) & 1)
693 /* Branch without likely bit. If label is out of range, we turn:
695 beq reg1, reg2, label
696 delay slot
698 into
700 bne reg1, reg2, 0f
702 j label
703 0: delay slot
705 with the following opcode replacements:
707 beq <-> bne
708 blez <-> bgtz
709 bltz <-> bgez
710 bc1f <-> bc1t
712 bltzal <-> bgezal (with jal label instead of j label)
714 Even though keeping the delay slot instruction in the delay slot of
715 the branch would be more efficient, it would be very tricky to do
716 correctly, because we'd have to introduce a variable frag *after*
717 the delay slot instruction, and expand that instead. Let's do it
718 the easy way for now, even if the branch-not-taken case now costs
719 one additional instruction. Out-of-range branches are not supposed
720 to be common, anyway.
722 Branch likely. If label is out of range, we turn:
724 beql reg1, reg2, label
725 delay slot (annulled if branch not taken)
727 into
729 beql reg1, reg2, 1f
731 beql $0, $0, 2f
733 1: j[al] label
734 delay slot (executed only if branch taken)
737 It would be possible to generate a shorter sequence by losing the
738 likely bit, generating something like:
740 bne reg1, reg2, 0f
742 j[al] label
743 delay slot (executed only if branch taken)
746 beql -> bne
747 bnel -> beq
748 blezl -> bgtz
749 bgtzl -> blez
750 bltzl -> bgez
751 bgezl -> bltz
752 bc1fl -> bc1t
753 bc1tl -> bc1f
755 bltzall -> bgezal (with jal label instead of j label)
756 bgezall -> bltzal (ditto)
759 but it's not clear that it would actually improve performance. */
760 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
761 ((relax_substateT) \
762 (0xc0000000 \
763 | ((toofar) ? 1 : 0) \
764 | ((link) ? 2 : 0) \
765 | ((likely) ? 4 : 0) \
766 | ((uncond) ? 8 : 0)))
767 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
768 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
769 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
770 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
771 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
773 /* For mips16 code, we use an entirely different form of relaxation.
774 mips16 supports two versions of most instructions which take
775 immediate values: a small one which takes some small value, and a
776 larger one which takes a 16 bit value. Since branches also follow
777 this pattern, relaxing these values is required.
779 We can assemble both mips16 and normal MIPS code in a single
780 object. Therefore, we need to support this type of relaxation at
781 the same time that we support the relaxation described above. We
782 use the high bit of the subtype field to distinguish these cases.
784 The information we store for this type of relaxation is the
785 argument code found in the opcode file for this relocation, whether
786 the user explicitly requested a small or extended form, and whether
787 the relocation is in a jump or jal delay slot. That tells us the
788 size of the value, and how it should be stored. We also store
789 whether the fragment is considered to be extended or not. We also
790 store whether this is known to be a branch to a different section,
791 whether we have tried to relax this frag yet, and whether we have
792 ever extended a PC relative fragment because of a shift count. */
793 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
794 (0x80000000 \
795 | ((type) & 0xff) \
796 | ((small) ? 0x100 : 0) \
797 | ((ext) ? 0x200 : 0) \
798 | ((dslot) ? 0x400 : 0) \
799 | ((jal_dslot) ? 0x800 : 0))
800 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
801 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
802 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
803 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
804 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
805 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
806 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
807 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
808 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
809 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
810 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
811 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
813 /* Is the given value a sign-extended 32-bit value? */
814 #define IS_SEXT_32BIT_NUM(x) \
815 (((x) &~ (offsetT) 0x7fffffff) == 0 \
816 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
818 /* Is the given value a sign-extended 16-bit value? */
819 #define IS_SEXT_16BIT_NUM(x) \
820 (((x) &~ (offsetT) 0x7fff) == 0 \
821 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
824 /* Prototypes for static functions. */
826 #define internalError() \
827 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
829 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
831 static void append_insn
832 (char *place, struct mips_cl_insn *ip, expressionS *p,
833 bfd_reloc_code_real_type *r);
834 static void mips_no_prev_insn (int);
835 static void mips16_macro_build
836 (char *, int *, expressionS *, const char *, const char *, va_list);
837 static void load_register (int *, int, expressionS *, int);
838 static void macro (struct mips_cl_insn * ip);
839 static void mips16_macro (struct mips_cl_insn * ip);
840 #ifdef LOSING_COMPILER
841 static void macro2 (struct mips_cl_insn * ip);
842 #endif
843 static void mips_ip (char *str, struct mips_cl_insn * ip);
844 static void mips16_ip (char *str, struct mips_cl_insn * ip);
845 static void mips16_immed
846 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
847 unsigned long *, bfd_boolean *, unsigned short *);
848 static size_t my_getSmallExpression
849 (expressionS *, bfd_reloc_code_real_type *, char *);
850 static void my_getExpression (expressionS *, char *);
851 static void s_align (int);
852 static void s_change_sec (int);
853 static void s_change_section (int);
854 static void s_cons (int);
855 static void s_float_cons (int);
856 static void s_mips_globl (int);
857 static void s_option (int);
858 static void s_mipsset (int);
859 static void s_abicalls (int);
860 static void s_cpload (int);
861 static void s_cpsetup (int);
862 static void s_cplocal (int);
863 static void s_cprestore (int);
864 static void s_cpreturn (int);
865 static void s_gpvalue (int);
866 static void s_gpword (int);
867 static void s_gpdword (int);
868 static void s_cpadd (int);
869 static void s_insn (int);
870 static void md_obj_begin (void);
871 static void md_obj_end (void);
872 static void s_mips_ent (int);
873 static void s_mips_end (int);
874 static void s_mips_frame (int);
875 static void s_mips_mask (int reg_type);
876 static void s_mips_stab (int);
877 static void s_mips_weakext (int);
878 static void s_mips_file (int);
879 static void s_mips_loc (int);
880 static bfd_boolean pic_need_relax (symbolS *, asection *);
881 static int relaxed_branch_length (fragS *, asection *, int);
882 static int validate_mips_insn (const struct mips_opcode *);
884 /* Table and functions used to map between CPU/ISA names, and
885 ISA levels, and CPU numbers. */
887 struct mips_cpu_info
889 const char *name; /* CPU or ISA name. */
890 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
891 int isa; /* ISA level. */
892 int cpu; /* CPU number (default CPU if ISA). */
895 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
896 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
897 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
899 /* Pseudo-op table.
901 The following pseudo-ops from the Kane and Heinrich MIPS book
902 should be defined here, but are currently unsupported: .alias,
903 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
905 The following pseudo-ops from the Kane and Heinrich MIPS book are
906 specific to the type of debugging information being generated, and
907 should be defined by the object format: .aent, .begin, .bend,
908 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
909 .vreg.
911 The following pseudo-ops from the Kane and Heinrich MIPS book are
912 not MIPS CPU specific, but are also not specific to the object file
913 format. This file is probably the best place to define them, but
914 they are not currently supported: .asm0, .endr, .lab, .repeat,
915 .struct. */
917 static const pseudo_typeS mips_pseudo_table[] =
919 /* MIPS specific pseudo-ops. */
920 {"option", s_option, 0},
921 {"set", s_mipsset, 0},
922 {"rdata", s_change_sec, 'r'},
923 {"sdata", s_change_sec, 's'},
924 {"livereg", s_ignore, 0},
925 {"abicalls", s_abicalls, 0},
926 {"cpload", s_cpload, 0},
927 {"cpsetup", s_cpsetup, 0},
928 {"cplocal", s_cplocal, 0},
929 {"cprestore", s_cprestore, 0},
930 {"cpreturn", s_cpreturn, 0},
931 {"gpvalue", s_gpvalue, 0},
932 {"gpword", s_gpword, 0},
933 {"gpdword", s_gpdword, 0},
934 {"cpadd", s_cpadd, 0},
935 {"insn", s_insn, 0},
937 /* Relatively generic pseudo-ops that happen to be used on MIPS
938 chips. */
939 {"asciiz", stringer, 1},
940 {"bss", s_change_sec, 'b'},
941 {"err", s_err, 0},
942 {"half", s_cons, 1},
943 {"dword", s_cons, 3},
944 {"weakext", s_mips_weakext, 0},
946 /* These pseudo-ops are defined in read.c, but must be overridden
947 here for one reason or another. */
948 {"align", s_align, 0},
949 {"byte", s_cons, 0},
950 {"data", s_change_sec, 'd'},
951 {"double", s_float_cons, 'd'},
952 {"float", s_float_cons, 'f'},
953 {"globl", s_mips_globl, 0},
954 {"global", s_mips_globl, 0},
955 {"hword", s_cons, 1},
956 {"int", s_cons, 2},
957 {"long", s_cons, 2},
958 {"octa", s_cons, 4},
959 {"quad", s_cons, 3},
960 {"section", s_change_section, 0},
961 {"short", s_cons, 1},
962 {"single", s_float_cons, 'f'},
963 {"stabn", s_mips_stab, 'n'},
964 {"text", s_change_sec, 't'},
965 {"word", s_cons, 2},
967 { "extern", ecoff_directive_extern, 0},
969 { NULL, NULL, 0 },
972 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
974 /* These pseudo-ops should be defined by the object file format.
975 However, a.out doesn't support them, so we have versions here. */
976 {"aent", s_mips_ent, 1},
977 {"bgnb", s_ignore, 0},
978 {"end", s_mips_end, 0},
979 {"endb", s_ignore, 0},
980 {"ent", s_mips_ent, 0},
981 {"file", s_mips_file, 0},
982 {"fmask", s_mips_mask, 'F'},
983 {"frame", s_mips_frame, 0},
984 {"loc", s_mips_loc, 0},
985 {"mask", s_mips_mask, 'R'},
986 {"verstamp", s_ignore, 0},
987 { NULL, NULL, 0 },
990 extern void pop_insert (const pseudo_typeS *);
992 void
993 mips_pop_insert (void)
995 pop_insert (mips_pseudo_table);
996 if (! ECOFF_DEBUGGING)
997 pop_insert (mips_nonecoff_pseudo_table);
1000 /* Symbols labelling the current insn. */
1002 struct insn_label_list
1004 struct insn_label_list *next;
1005 symbolS *label;
1008 static struct insn_label_list *insn_labels;
1009 static struct insn_label_list *free_insn_labels;
1011 static void mips_clear_insn_labels (void);
1013 static inline void
1014 mips_clear_insn_labels (void)
1016 register struct insn_label_list **pl;
1018 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1020 *pl = insn_labels;
1021 insn_labels = NULL;
1024 static char *expr_end;
1026 /* Expressions which appear in instructions. These are set by
1027 mips_ip. */
1029 static expressionS imm_expr;
1030 static expressionS imm2_expr;
1031 static expressionS offset_expr;
1033 /* Relocs associated with imm_expr and offset_expr. */
1035 static bfd_reloc_code_real_type imm_reloc[3]
1036 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1037 static bfd_reloc_code_real_type offset_reloc[3]
1038 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1040 /* These are set by mips16_ip if an explicit extension is used. */
1042 static bfd_boolean mips16_small, mips16_ext;
1044 #ifdef OBJ_ELF
1045 /* The pdr segment for per procedure frame/regmask info. Not used for
1046 ECOFF debugging. */
1048 static segT pdr_seg;
1049 #endif
1051 /* The default target format to use. */
1053 const char *
1054 mips_target_format (void)
1056 switch (OUTPUT_FLAVOR)
1058 case bfd_target_aout_flavour:
1059 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1060 case bfd_target_ecoff_flavour:
1061 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1062 case bfd_target_coff_flavour:
1063 return "pe-mips";
1064 case bfd_target_elf_flavour:
1065 #ifdef TE_TMIPS
1066 /* This is traditional mips. */
1067 return (target_big_endian
1068 ? (HAVE_64BIT_OBJECTS
1069 ? "elf64-tradbigmips"
1070 : (HAVE_NEWABI
1071 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1072 : (HAVE_64BIT_OBJECTS
1073 ? "elf64-tradlittlemips"
1074 : (HAVE_NEWABI
1075 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1076 #else
1077 return (target_big_endian
1078 ? (HAVE_64BIT_OBJECTS
1079 ? "elf64-bigmips"
1080 : (HAVE_NEWABI
1081 ? "elf32-nbigmips" : "elf32-bigmips"))
1082 : (HAVE_64BIT_OBJECTS
1083 ? "elf64-littlemips"
1084 : (HAVE_NEWABI
1085 ? "elf32-nlittlemips" : "elf32-littlemips")));
1086 #endif
1087 default:
1088 abort ();
1089 return NULL;
1093 /* This function is called once, at assembler startup time. It should
1094 set up all the tables, etc. that the MD part of the assembler will need. */
1096 void
1097 md_begin (void)
1099 register const char *retval = NULL;
1100 int i = 0;
1101 int broken = 0;
1103 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1104 as_warn (_("Could not set architecture and machine"));
1106 op_hash = hash_new ();
1108 for (i = 0; i < NUMOPCODES;)
1110 const char *name = mips_opcodes[i].name;
1112 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1113 if (retval != NULL)
1115 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1116 mips_opcodes[i].name, retval);
1117 /* Probably a memory allocation problem? Give up now. */
1118 as_fatal (_("Broken assembler. No assembly attempted."));
1122 if (mips_opcodes[i].pinfo != INSN_MACRO)
1124 if (!validate_mips_insn (&mips_opcodes[i]))
1125 broken = 1;
1127 ++i;
1129 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1132 mips16_op_hash = hash_new ();
1134 i = 0;
1135 while (i < bfd_mips16_num_opcodes)
1137 const char *name = mips16_opcodes[i].name;
1139 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1140 if (retval != NULL)
1141 as_fatal (_("internal: can't hash `%s': %s"),
1142 mips16_opcodes[i].name, retval);
1145 if (mips16_opcodes[i].pinfo != INSN_MACRO
1146 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1147 != mips16_opcodes[i].match))
1149 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1150 mips16_opcodes[i].name, mips16_opcodes[i].args);
1151 broken = 1;
1153 ++i;
1155 while (i < bfd_mips16_num_opcodes
1156 && strcmp (mips16_opcodes[i].name, name) == 0);
1159 if (broken)
1160 as_fatal (_("Broken assembler. No assembly attempted."));
1162 /* We add all the general register names to the symbol table. This
1163 helps us detect invalid uses of them. */
1164 for (i = 0; i < 32; i++)
1166 char buf[5];
1168 sprintf (buf, "$%d", i);
1169 symbol_table_insert (symbol_new (buf, reg_section, i,
1170 &zero_address_frag));
1172 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1173 &zero_address_frag));
1174 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1175 &zero_address_frag));
1176 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1177 &zero_address_frag));
1178 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1179 &zero_address_frag));
1180 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1181 &zero_address_frag));
1182 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1183 &zero_address_frag));
1184 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1185 &zero_address_frag));
1186 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1187 &zero_address_frag));
1188 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1189 &zero_address_frag));
1191 /* If we don't add these register names to the symbol table, they
1192 may end up being added as regular symbols by operand(), and then
1193 make it to the object file as undefined in case they're not
1194 regarded as local symbols. They're local in o32, since `$' is a
1195 local symbol prefix, but not in n32 or n64. */
1196 for (i = 0; i < 8; i++)
1198 char buf[6];
1200 sprintf (buf, "$fcc%i", i);
1201 symbol_table_insert (symbol_new (buf, reg_section, -1,
1202 &zero_address_frag));
1205 mips_no_prev_insn (FALSE);
1207 mips_gprmask = 0;
1208 mips_cprmask[0] = 0;
1209 mips_cprmask[1] = 0;
1210 mips_cprmask[2] = 0;
1211 mips_cprmask[3] = 0;
1213 /* set the default alignment for the text section (2**2) */
1214 record_alignment (text_section, 2);
1216 if (USE_GLOBAL_POINTER_OPT)
1217 bfd_set_gp_size (stdoutput, g_switch_value);
1219 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1221 /* On a native system, sections must be aligned to 16 byte
1222 boundaries. When configured for an embedded ELF target, we
1223 don't bother. */
1224 if (strcmp (TARGET_OS, "elf") != 0)
1226 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1227 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1228 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1231 /* Create a .reginfo section for register masks and a .mdebug
1232 section for debugging information. */
1234 segT seg;
1235 subsegT subseg;
1236 flagword flags;
1237 segT sec;
1239 seg = now_seg;
1240 subseg = now_subseg;
1242 /* The ABI says this section should be loaded so that the
1243 running program can access it. However, we don't load it
1244 if we are configured for an embedded target */
1245 flags = SEC_READONLY | SEC_DATA;
1246 if (strcmp (TARGET_OS, "elf") != 0)
1247 flags |= SEC_ALLOC | SEC_LOAD;
1249 if (mips_abi != N64_ABI)
1251 sec = subseg_new (".reginfo", (subsegT) 0);
1253 bfd_set_section_flags (stdoutput, sec, flags);
1254 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1256 #ifdef OBJ_ELF
1257 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1258 #endif
1260 else
1262 /* The 64-bit ABI uses a .MIPS.options section rather than
1263 .reginfo section. */
1264 sec = subseg_new (".MIPS.options", (subsegT) 0);
1265 bfd_set_section_flags (stdoutput, sec, flags);
1266 bfd_set_section_alignment (stdoutput, sec, 3);
1268 #ifdef OBJ_ELF
1269 /* Set up the option header. */
1271 Elf_Internal_Options opthdr;
1272 char *f;
1274 opthdr.kind = ODK_REGINFO;
1275 opthdr.size = (sizeof (Elf_External_Options)
1276 + sizeof (Elf64_External_RegInfo));
1277 opthdr.section = 0;
1278 opthdr.info = 0;
1279 f = frag_more (sizeof (Elf_External_Options));
1280 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1281 (Elf_External_Options *) f);
1283 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1285 #endif
1288 if (ECOFF_DEBUGGING)
1290 sec = subseg_new (".mdebug", (subsegT) 0);
1291 (void) bfd_set_section_flags (stdoutput, sec,
1292 SEC_HAS_CONTENTS | SEC_READONLY);
1293 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1295 #ifdef OBJ_ELF
1296 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1298 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1299 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1300 SEC_READONLY | SEC_RELOC
1301 | SEC_DEBUGGING);
1302 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1304 #endif
1306 subseg_set (seg, subseg);
1310 if (! ECOFF_DEBUGGING)
1311 md_obj_begin ();
1314 void
1315 md_mips_end (void)
1317 if (! ECOFF_DEBUGGING)
1318 md_obj_end ();
1321 void
1322 md_assemble (char *str)
1324 struct mips_cl_insn insn;
1325 bfd_reloc_code_real_type unused_reloc[3]
1326 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1328 imm_expr.X_op = O_absent;
1329 imm2_expr.X_op = O_absent;
1330 offset_expr.X_op = O_absent;
1331 imm_reloc[0] = BFD_RELOC_UNUSED;
1332 imm_reloc[1] = BFD_RELOC_UNUSED;
1333 imm_reloc[2] = BFD_RELOC_UNUSED;
1334 offset_reloc[0] = BFD_RELOC_UNUSED;
1335 offset_reloc[1] = BFD_RELOC_UNUSED;
1336 offset_reloc[2] = BFD_RELOC_UNUSED;
1338 if (mips_opts.mips16)
1339 mips16_ip (str, &insn);
1340 else
1342 mips_ip (str, &insn);
1343 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1344 str, insn.insn_opcode));
1347 if (insn_error)
1349 as_bad ("%s `%s'", insn_error, str);
1350 return;
1353 if (insn.insn_mo->pinfo == INSN_MACRO)
1355 if (mips_opts.mips16)
1356 mips16_macro (&insn);
1357 else
1358 macro (&insn);
1360 else
1362 if (imm_expr.X_op != O_absent)
1363 append_insn (NULL, &insn, &imm_expr, imm_reloc);
1364 else if (offset_expr.X_op != O_absent)
1365 append_insn (NULL, &insn, &offset_expr, offset_reloc);
1366 else
1367 append_insn (NULL, &insn, NULL, unused_reloc);
1371 /* Return true if the given relocation might need a matching %lo().
1372 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1373 applied to local symbols. */
1375 static inline bfd_boolean
1376 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
1378 return (reloc == BFD_RELOC_HI16_S
1379 || reloc == BFD_RELOC_MIPS_GOT16);
1382 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1383 relocation. */
1385 static inline bfd_boolean
1386 fixup_has_matching_lo_p (fixS *fixp)
1388 return (fixp->fx_next != NULL
1389 && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1390 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1391 && fixp->fx_offset == fixp->fx_next->fx_offset);
1394 /* See whether instruction IP reads register REG. CLASS is the type
1395 of register. */
1397 static int
1398 insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1399 enum mips_regclass class)
1401 if (class == MIPS16_REG)
1403 assert (mips_opts.mips16);
1404 reg = mips16_to_32_reg_map[reg];
1405 class = MIPS_GR_REG;
1408 /* Don't report on general register ZERO, since it never changes. */
1409 if (class == MIPS_GR_REG && reg == ZERO)
1410 return 0;
1412 if (class == MIPS_FP_REG)
1414 assert (! mips_opts.mips16);
1415 /* If we are called with either $f0 or $f1, we must check $f0.
1416 This is not optimal, because it will introduce an unnecessary
1417 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1418 need to distinguish reading both $f0 and $f1 or just one of
1419 them. Note that we don't have to check the other way,
1420 because there is no instruction that sets both $f0 and $f1
1421 and requires a delay. */
1422 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1423 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1424 == (reg &~ (unsigned) 1)))
1425 return 1;
1426 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1427 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1428 == (reg &~ (unsigned) 1)))
1429 return 1;
1431 else if (! mips_opts.mips16)
1433 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1434 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1435 return 1;
1436 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1437 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1438 return 1;
1440 else
1442 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1443 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1444 & MIPS16OP_MASK_RX)]
1445 == reg))
1446 return 1;
1447 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1448 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1449 & MIPS16OP_MASK_RY)]
1450 == reg))
1451 return 1;
1452 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1453 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1454 & MIPS16OP_MASK_MOVE32Z)]
1455 == reg))
1456 return 1;
1457 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1458 return 1;
1459 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1460 return 1;
1461 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1462 return 1;
1463 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1464 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1465 & MIPS16OP_MASK_REGR32) == reg)
1466 return 1;
1469 return 0;
1472 /* This function returns true if modifying a register requires a
1473 delay. */
1475 static int
1476 reg_needs_delay (unsigned int reg)
1478 unsigned long prev_pinfo;
1480 prev_pinfo = prev_insn.insn_mo->pinfo;
1481 if (! mips_opts.noreorder
1482 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1483 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1484 || (! gpr_interlocks
1485 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1487 /* A load from a coprocessor or from memory. All load
1488 delays delay the use of general register rt for one
1489 instruction on the r3000. The r6000 and r4000 use
1490 interlocks. */
1491 /* Itbl support may require additional care here. */
1492 know (prev_pinfo & INSN_WRITE_GPR_T);
1493 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1494 return 1;
1497 return 0;
1500 /* Mark instruction labels in mips16 mode. This permits the linker to
1501 handle them specially, such as generating jalx instructions when
1502 needed. We also make them odd for the duration of the assembly, in
1503 order to generate the right sort of code. We will make them even
1504 in the adjust_symtab routine, while leaving them marked. This is
1505 convenient for the debugger and the disassembler. The linker knows
1506 to make them odd again. */
1508 static void
1509 mips16_mark_labels (void)
1511 if (mips_opts.mips16)
1513 struct insn_label_list *l;
1514 valueT val;
1516 for (l = insn_labels; l != NULL; l = l->next)
1518 #ifdef OBJ_ELF
1519 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1520 S_SET_OTHER (l->label, STO_MIPS16);
1521 #endif
1522 val = S_GET_VALUE (l->label);
1523 if ((val & 1) == 0)
1524 S_SET_VALUE (l->label, val + 1);
1529 /* Output an instruction. PLACE is where to put the instruction; if
1530 it is NULL, this uses frag_more to get room. IP is the instruction
1531 information. ADDRESS_EXPR is an operand of the instruction to be
1532 used with RELOC_TYPE. */
1534 static void
1535 append_insn (char *place, struct mips_cl_insn *ip, expressionS *address_expr,
1536 bfd_reloc_code_real_type *reloc_type)
1538 register unsigned long prev_pinfo, pinfo;
1539 char *f;
1540 fixS *fixp[3];
1541 int nops = 0;
1542 bfd_boolean force_new_frag = FALSE;
1544 /* Mark instruction labels in mips16 mode. */
1545 mips16_mark_labels ();
1547 prev_pinfo = prev_insn.insn_mo->pinfo;
1548 pinfo = ip->insn_mo->pinfo;
1550 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1552 int prev_prev_nop;
1554 /* If the previous insn required any delay slots, see if we need
1555 to insert a NOP or two. There are eight kinds of possible
1556 hazards, of which an instruction can have at most one type.
1557 (1) a load from memory delay
1558 (2) a load from a coprocessor delay
1559 (3) an unconditional branch delay
1560 (4) a conditional branch delay
1561 (5) a move to coprocessor register delay
1562 (6) a load coprocessor register from memory delay
1563 (7) a coprocessor condition code delay
1564 (8) a HI/LO special register delay
1566 There are a lot of optimizations we could do that we don't.
1567 In particular, we do not, in general, reorder instructions.
1568 If you use gcc with optimization, it will reorder
1569 instructions and generally do much more optimization then we
1570 do here; repeating all that work in the assembler would only
1571 benefit hand written assembly code, and does not seem worth
1572 it. */
1574 /* This is how a NOP is emitted. */
1575 #define emit_nop() \
1576 (mips_opts.mips16 \
1577 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1578 : md_number_to_chars (frag_more (4), 0, 4))
1580 /* The previous insn might require a delay slot, depending upon
1581 the contents of the current insn. */
1582 if (! mips_opts.mips16
1583 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1584 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1585 && ! cop_interlocks)
1586 || (! gpr_interlocks
1587 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1589 /* A load from a coprocessor or from memory. All load
1590 delays delay the use of general register rt for one
1591 instruction on the r3000. The r6000 and r4000 use
1592 interlocks. */
1593 /* Itbl support may require additional care here. */
1594 know (prev_pinfo & INSN_WRITE_GPR_T);
1595 if (mips_optimize == 0
1596 || insn_uses_reg (ip,
1597 ((prev_insn.insn_opcode >> OP_SH_RT)
1598 & OP_MASK_RT),
1599 MIPS_GR_REG))
1600 ++nops;
1602 else if (! mips_opts.mips16
1603 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1604 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1605 && ! cop_interlocks)
1606 || (mips_opts.isa == ISA_MIPS1
1607 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1609 /* A generic coprocessor delay. The previous instruction
1610 modified a coprocessor general or control register. If
1611 it modified a control register, we need to avoid any
1612 coprocessor instruction (this is probably not always
1613 required, but it sometimes is). If it modified a general
1614 register, we avoid using that register.
1616 On the r6000 and r4000 loading a coprocessor register
1617 from memory is interlocked, and does not require a delay.
1619 This case is not handled very well. There is no special
1620 knowledge of CP0 handling, and the coprocessors other
1621 than the floating point unit are not distinguished at
1622 all. */
1623 /* Itbl support may require additional care here. FIXME!
1624 Need to modify this to include knowledge about
1625 user specified delays! */
1626 if (prev_pinfo & INSN_WRITE_FPR_T)
1628 if (mips_optimize == 0
1629 || insn_uses_reg (ip,
1630 ((prev_insn.insn_opcode >> OP_SH_FT)
1631 & OP_MASK_FT),
1632 MIPS_FP_REG))
1633 ++nops;
1635 else if (prev_pinfo & INSN_WRITE_FPR_S)
1637 if (mips_optimize == 0
1638 || insn_uses_reg (ip,
1639 ((prev_insn.insn_opcode >> OP_SH_FS)
1640 & OP_MASK_FS),
1641 MIPS_FP_REG))
1642 ++nops;
1644 else
1646 /* We don't know exactly what the previous instruction
1647 does. If the current instruction uses a coprocessor
1648 register, we must insert a NOP. If previous
1649 instruction may set the condition codes, and the
1650 current instruction uses them, we must insert two
1651 NOPS. */
1652 /* Itbl support may require additional care here. */
1653 if (mips_optimize == 0
1654 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1655 && (pinfo & INSN_READ_COND_CODE)))
1656 nops += 2;
1657 else if (pinfo & INSN_COP)
1658 ++nops;
1661 else if (! mips_opts.mips16
1662 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1663 && (prev_pinfo & INSN_WRITE_COND_CODE)
1664 && ! cop_interlocks)
1666 /* The previous instruction sets the coprocessor condition
1667 codes, but does not require a general coprocessor delay
1668 (this means it is a floating point comparison
1669 instruction). If this instruction uses the condition
1670 codes, we need to insert a single NOP. */
1671 /* Itbl support may require additional care here. */
1672 if (mips_optimize == 0
1673 || (pinfo & INSN_READ_COND_CODE))
1674 ++nops;
1677 /* If we're fixing up mfhi/mflo for the r7000 and the
1678 previous insn was an mfhi/mflo and the current insn
1679 reads the register that the mfhi/mflo wrote to, then
1680 insert two nops. */
1682 else if (mips_7000_hilo_fix
1683 && MF_HILO_INSN (prev_pinfo)
1684 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1685 & OP_MASK_RD),
1686 MIPS_GR_REG))
1688 nops += 2;
1691 /* If we're fixing up mfhi/mflo for the r7000 and the
1692 2nd previous insn was an mfhi/mflo and the current insn
1693 reads the register that the mfhi/mflo wrote to, then
1694 insert one nop. */
1696 else if (mips_7000_hilo_fix
1697 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1698 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1699 & OP_MASK_RD),
1700 MIPS_GR_REG))
1703 ++nops;
1706 else if (prev_pinfo & INSN_READ_LO)
1708 /* The previous instruction reads the LO register; if the
1709 current instruction writes to the LO register, we must
1710 insert two NOPS. Some newer processors have interlocks.
1711 Also the tx39's multiply instructions can be exectuted
1712 immediatly after a read from HI/LO (without the delay),
1713 though the tx39's divide insns still do require the
1714 delay. */
1715 if (! (hilo_interlocks
1716 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1717 && (mips_optimize == 0
1718 || (pinfo & INSN_WRITE_LO)))
1719 nops += 2;
1720 /* Most mips16 branch insns don't have a delay slot.
1721 If a read from LO is immediately followed by a branch
1722 to a write to LO we have a read followed by a write
1723 less than 2 insns away. We assume the target of
1724 a branch might be a write to LO, and insert a nop
1725 between a read and an immediately following branch. */
1726 else if (mips_opts.mips16
1727 && (mips_optimize == 0
1728 || (pinfo & MIPS16_INSN_BRANCH)))
1729 ++nops;
1731 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1733 /* The previous instruction reads the HI register; if the
1734 current instruction writes to the HI register, we must
1735 insert a NOP. Some newer processors have interlocks.
1736 Also the note tx39's multiply above. */
1737 if (! (hilo_interlocks
1738 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1739 && (mips_optimize == 0
1740 || (pinfo & INSN_WRITE_HI)))
1741 nops += 2;
1742 /* Most mips16 branch insns don't have a delay slot.
1743 If a read from HI is immediately followed by a branch
1744 to a write to HI we have a read followed by a write
1745 less than 2 insns away. We assume the target of
1746 a branch might be a write to HI, and insert a nop
1747 between a read and an immediately following branch. */
1748 else if (mips_opts.mips16
1749 && (mips_optimize == 0
1750 || (pinfo & MIPS16_INSN_BRANCH)))
1751 ++nops;
1754 /* If the previous instruction was in a noreorder section, then
1755 we don't want to insert the nop after all. */
1756 /* Itbl support may require additional care here. */
1757 if (prev_insn_unreordered)
1758 nops = 0;
1760 /* There are two cases which require two intervening
1761 instructions: 1) setting the condition codes using a move to
1762 coprocessor instruction which requires a general coprocessor
1763 delay and then reading the condition codes 2) reading the HI
1764 or LO register and then writing to it (except on processors
1765 which have interlocks). If we are not already emitting a NOP
1766 instruction, we must check for these cases compared to the
1767 instruction previous to the previous instruction. */
1768 if ((! mips_opts.mips16
1769 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1770 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1771 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1772 && (pinfo & INSN_READ_COND_CODE)
1773 && ! cop_interlocks)
1774 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1775 && (pinfo & INSN_WRITE_LO)
1776 && ! (hilo_interlocks
1777 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1778 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1779 && (pinfo & INSN_WRITE_HI)
1780 && ! (hilo_interlocks
1781 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1782 prev_prev_nop = 1;
1783 else
1784 prev_prev_nop = 0;
1786 if (prev_prev_insn_unreordered)
1787 prev_prev_nop = 0;
1789 if (prev_prev_nop && nops == 0)
1790 ++nops;
1792 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1794 /* We're out of bits in pinfo, so we must resort to string
1795 ops here. Shortcuts are selected based on opcodes being
1796 limited to the VR4122 instruction set. */
1797 int min_nops = 0;
1798 const char *pn = prev_insn.insn_mo->name;
1799 const char *tn = ip->insn_mo->name;
1800 if (strncmp(pn, "macc", 4) == 0
1801 || strncmp(pn, "dmacc", 5) == 0)
1803 /* Errata 21 - [D]DIV[U] after [D]MACC */
1804 if (strstr (tn, "div"))
1806 min_nops = 1;
1809 /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1810 if (pn[0] == 'd' /* dmacc */
1811 && (strncmp(tn, "dmult", 5) == 0
1812 || strncmp(tn, "dmacc", 5) == 0))
1814 min_nops = 1;
1817 /* Errata 24 - MT{LO,HI} after [D]MACC */
1818 if (strcmp (tn, "mtlo") == 0
1819 || strcmp (tn, "mthi") == 0)
1821 min_nops = 1;
1825 else if (strncmp(pn, "dmult", 5) == 0
1826 && (strncmp(tn, "dmult", 5) == 0
1827 || strncmp(tn, "dmacc", 5) == 0))
1829 /* Here is the rest of errata 23. */
1830 min_nops = 1;
1832 if (nops < min_nops)
1833 nops = min_nops;
1836 /* If we are being given a nop instruction, don't bother with
1837 one of the nops we would otherwise output. This will only
1838 happen when a nop instruction is used with mips_optimize set
1839 to 0. */
1840 if (nops > 0
1841 && ! mips_opts.noreorder
1842 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1843 --nops;
1845 /* Now emit the right number of NOP instructions. */
1846 if (nops > 0 && ! mips_opts.noreorder)
1848 fragS *old_frag;
1849 unsigned long old_frag_offset;
1850 int i;
1851 struct insn_label_list *l;
1853 old_frag = frag_now;
1854 old_frag_offset = frag_now_fix ();
1856 for (i = 0; i < nops; i++)
1857 emit_nop ();
1859 if (listing)
1861 listing_prev_line ();
1862 /* We may be at the start of a variant frag. In case we
1863 are, make sure there is enough space for the frag
1864 after the frags created by listing_prev_line. The
1865 argument to frag_grow here must be at least as large
1866 as the argument to all other calls to frag_grow in
1867 this file. We don't have to worry about being in the
1868 middle of a variant frag, because the variants insert
1869 all needed nop instructions themselves. */
1870 frag_grow (40);
1873 for (l = insn_labels; l != NULL; l = l->next)
1875 valueT val;
1877 assert (S_GET_SEGMENT (l->label) == now_seg);
1878 symbol_set_frag (l->label, frag_now);
1879 val = (valueT) frag_now_fix ();
1880 /* mips16 text labels are stored as odd. */
1881 if (mips_opts.mips16)
1882 ++val;
1883 S_SET_VALUE (l->label, val);
1886 #ifndef NO_ECOFF_DEBUGGING
1887 if (ECOFF_DEBUGGING)
1888 ecoff_fix_loc (old_frag, old_frag_offset);
1889 #endif
1891 else if (prev_nop_frag != NULL)
1893 /* We have a frag holding nops we may be able to remove. If
1894 we don't need any nops, we can decrease the size of
1895 prev_nop_frag by the size of one instruction. If we do
1896 need some nops, we count them in prev_nops_required. */
1897 if (prev_nop_frag_since == 0)
1899 if (nops == 0)
1901 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1902 --prev_nop_frag_holds;
1904 else
1905 prev_nop_frag_required += nops;
1907 else
1909 if (prev_prev_nop == 0)
1911 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1912 --prev_nop_frag_holds;
1914 else
1915 ++prev_nop_frag_required;
1918 if (prev_nop_frag_holds <= prev_nop_frag_required)
1919 prev_nop_frag = NULL;
1921 ++prev_nop_frag_since;
1923 /* Sanity check: by the time we reach the second instruction
1924 after prev_nop_frag, we should have used up all the nops
1925 one way or another. */
1926 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1930 if (place == NULL
1931 && address_expr
1932 && *reloc_type == BFD_RELOC_16_PCREL_S2
1933 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
1934 || pinfo & INSN_COND_BRANCH_LIKELY)
1935 && mips_relax_branch
1936 /* Don't try branch relaxation within .set nomacro, or within
1937 .set noat if we use $at for PIC computations. If it turns
1938 out that the branch was out-of-range, we'll get an error. */
1939 && !mips_opts.warn_about_macros
1940 && !(mips_opts.noat && mips_pic != NO_PIC)
1941 && !mips_opts.mips16)
1943 f = frag_var (rs_machine_dependent,
1944 relaxed_branch_length
1945 (NULL, NULL,
1946 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
1947 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
1948 RELAX_BRANCH_ENCODE
1949 (pinfo & INSN_UNCOND_BRANCH_DELAY,
1950 pinfo & INSN_COND_BRANCH_LIKELY,
1951 pinfo & INSN_WRITE_GPR_31,
1953 address_expr->X_add_symbol,
1954 address_expr->X_add_number,
1956 *reloc_type = BFD_RELOC_UNUSED;
1958 else if (*reloc_type > BFD_RELOC_UNUSED)
1960 /* We need to set up a variant frag. */
1961 assert (mips_opts.mips16 && address_expr != NULL);
1962 f = frag_var (rs_machine_dependent, 4, 0,
1963 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1964 mips16_small, mips16_ext,
1965 (prev_pinfo
1966 & INSN_UNCOND_BRANCH_DELAY),
1967 (*prev_insn_reloc_type
1968 == BFD_RELOC_MIPS16_JMP)),
1969 make_expr_symbol (address_expr), 0, NULL);
1971 else if (place != NULL)
1972 f = place;
1973 else if (mips_opts.mips16
1974 && ! ip->use_extend
1975 && *reloc_type != BFD_RELOC_MIPS16_JMP)
1977 /* Make sure there is enough room to swap this instruction with
1978 a following jump instruction. */
1979 frag_grow (6);
1980 f = frag_more (2);
1982 else
1984 if (mips_opts.mips16
1985 && mips_opts.noreorder
1986 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1987 as_warn (_("extended instruction in delay slot"));
1989 f = frag_more (4);
1992 fixp[0] = fixp[1] = fixp[2] = NULL;
1993 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1995 if (address_expr->X_op == O_constant)
1997 valueT tmp;
1999 switch (*reloc_type)
2001 case BFD_RELOC_32:
2002 ip->insn_opcode |= address_expr->X_add_number;
2003 break;
2005 case BFD_RELOC_MIPS_HIGHEST:
2006 tmp = (address_expr->X_add_number
2007 + ((valueT) 0x8000 << 32) + 0x80008000) >> 16;
2008 tmp >>= 16;
2009 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2010 break;
2012 case BFD_RELOC_MIPS_HIGHER:
2013 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2014 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2015 break;
2017 case BFD_RELOC_HI16_S:
2018 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2019 >> 16) & 0xffff;
2020 break;
2022 case BFD_RELOC_HI16:
2023 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2024 break;
2026 case BFD_RELOC_LO16:
2027 case BFD_RELOC_MIPS_GOT_DISP:
2028 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2029 break;
2031 case BFD_RELOC_MIPS_JMP:
2032 if ((address_expr->X_add_number & 3) != 0)
2033 as_bad (_("jump to misaligned address (0x%lx)"),
2034 (unsigned long) address_expr->X_add_number);
2035 if (address_expr->X_add_number & ~0xfffffff)
2036 as_bad (_("jump address range overflow (0x%lx)"),
2037 (unsigned long) address_expr->X_add_number);
2038 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2039 break;
2041 case BFD_RELOC_MIPS16_JMP:
2042 if ((address_expr->X_add_number & 3) != 0)
2043 as_bad (_("jump to misaligned address (0x%lx)"),
2044 (unsigned long) address_expr->X_add_number);
2045 if (address_expr->X_add_number & ~0xfffffff)
2046 as_bad (_("jump address range overflow (0x%lx)"),
2047 (unsigned long) address_expr->X_add_number);
2048 ip->insn_opcode |=
2049 (((address_expr->X_add_number & 0x7c0000) << 3)
2050 | ((address_expr->X_add_number & 0xf800000) >> 7)
2051 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2052 break;
2054 case BFD_RELOC_16_PCREL_S2:
2055 goto need_reloc;
2057 default:
2058 internalError ();
2061 else
2063 need_reloc:
2064 /* Don't generate a reloc if we are writing into a variant frag. */
2065 if (place == NULL)
2067 reloc_howto_type *howto;
2068 int i;
2070 /* In a compound relocation, it is the final (outermost)
2071 operator that determines the relocated field. */
2072 for (i = 1; i < 3; i++)
2073 if (reloc_type[i] == BFD_RELOC_UNUSED)
2074 break;
2076 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2077 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2078 bfd_get_reloc_size(howto),
2079 address_expr,
2080 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2081 reloc_type[0]);
2083 /* These relocations can have an addend that won't fit in
2084 4 octets for 64bit assembly. */
2085 if (HAVE_64BIT_GPRS
2086 && ! howto->partial_inplace
2087 && (reloc_type[0] == BFD_RELOC_16
2088 || reloc_type[0] == BFD_RELOC_32
2089 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2090 || reloc_type[0] == BFD_RELOC_HI16_S
2091 || reloc_type[0] == BFD_RELOC_LO16
2092 || reloc_type[0] == BFD_RELOC_GPREL16
2093 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2094 || reloc_type[0] == BFD_RELOC_GPREL32
2095 || reloc_type[0] == BFD_RELOC_64
2096 || reloc_type[0] == BFD_RELOC_CTOR
2097 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2098 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2099 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2100 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2101 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2102 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
2103 fixp[0]->fx_no_overflow = 1;
2105 if (reloc_needs_lo_p (*reloc_type))
2107 struct mips_hi_fixup *hi_fixup;
2109 /* Reuse the last entry if it already has a matching %lo. */
2110 hi_fixup = mips_hi_fixup_list;
2111 if (hi_fixup == 0
2112 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2114 hi_fixup = ((struct mips_hi_fixup *)
2115 xmalloc (sizeof (struct mips_hi_fixup)));
2116 hi_fixup->next = mips_hi_fixup_list;
2117 mips_hi_fixup_list = hi_fixup;
2119 hi_fixup->fixp = fixp[0];
2120 hi_fixup->seg = now_seg;
2123 /* Add fixups for the second and third relocations, if given.
2124 Note that the ABI allows the second relocation to be
2125 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2126 moment we only use RSS_UNDEF, but we could add support
2127 for the others if it ever becomes necessary. */
2128 for (i = 1; i < 3; i++)
2129 if (reloc_type[i] != BFD_RELOC_UNUSED)
2131 address_expr->X_op = O_absent;
2132 address_expr->X_add_symbol = 0;
2133 address_expr->X_add_number = 0;
2135 fixp[i] = fix_new_exp (frag_now, fixp[0]->fx_where,
2136 fixp[0]->fx_size, address_expr,
2137 FALSE, reloc_type[i]);
2143 if (! mips_opts.mips16)
2145 md_number_to_chars (f, ip->insn_opcode, 4);
2146 #ifdef OBJ_ELF
2147 dwarf2_emit_insn (4);
2148 #endif
2150 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2152 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2153 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2154 #ifdef OBJ_ELF
2155 dwarf2_emit_insn (4);
2156 #endif
2158 else
2160 if (ip->use_extend)
2162 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2163 f += 2;
2165 md_number_to_chars (f, ip->insn_opcode, 2);
2166 #ifdef OBJ_ELF
2167 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2168 #endif
2171 /* Update the register mask information. */
2172 if (! mips_opts.mips16)
2174 if (pinfo & INSN_WRITE_GPR_D)
2175 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2176 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2177 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2178 if (pinfo & INSN_READ_GPR_S)
2179 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2180 if (pinfo & INSN_WRITE_GPR_31)
2181 mips_gprmask |= 1 << RA;
2182 if (pinfo & INSN_WRITE_FPR_D)
2183 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2184 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2185 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2186 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2187 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2188 if ((pinfo & INSN_READ_FPR_R) != 0)
2189 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2190 if (pinfo & INSN_COP)
2192 /* We don't keep enough information to sort these cases out.
2193 The itbl support does keep this information however, although
2194 we currently don't support itbl fprmats as part of the cop
2195 instruction. May want to add this support in the future. */
2197 /* Never set the bit for $0, which is always zero. */
2198 mips_gprmask &= ~1 << 0;
2200 else
2202 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2203 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2204 & MIPS16OP_MASK_RX);
2205 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2206 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2207 & MIPS16OP_MASK_RY);
2208 if (pinfo & MIPS16_INSN_WRITE_Z)
2209 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2210 & MIPS16OP_MASK_RZ);
2211 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2212 mips_gprmask |= 1 << TREG;
2213 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2214 mips_gprmask |= 1 << SP;
2215 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2216 mips_gprmask |= 1 << RA;
2217 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2218 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2219 if (pinfo & MIPS16_INSN_READ_Z)
2220 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2221 & MIPS16OP_MASK_MOVE32Z);
2222 if (pinfo & MIPS16_INSN_READ_GPR_X)
2223 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2224 & MIPS16OP_MASK_REGR32);
2227 if (place == NULL && ! mips_opts.noreorder)
2229 /* Filling the branch delay slot is more complex. We try to
2230 switch the branch with the previous instruction, which we can
2231 do if the previous instruction does not set up a condition
2232 that the branch tests and if the branch is not itself the
2233 target of any branch. */
2234 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2235 || (pinfo & INSN_COND_BRANCH_DELAY))
2237 if (mips_optimize < 2
2238 /* If we have seen .set volatile or .set nomove, don't
2239 optimize. */
2240 || mips_opts.nomove != 0
2241 /* If we had to emit any NOP instructions, then we
2242 already know we can not swap. */
2243 || nops != 0
2244 /* If we don't even know the previous insn, we can not
2245 swap. */
2246 || ! prev_insn_valid
2247 /* If the previous insn is already in a branch delay
2248 slot, then we can not swap. */
2249 || prev_insn_is_delay_slot
2250 /* If the previous previous insn was in a .set
2251 noreorder, we can't swap. Actually, the MIPS
2252 assembler will swap in this situation. However, gcc
2253 configured -with-gnu-as will generate code like
2254 .set noreorder
2255 lw $4,XXX
2256 .set reorder
2257 INSN
2258 bne $4,$0,foo
2259 in which we can not swap the bne and INSN. If gcc is
2260 not configured -with-gnu-as, it does not output the
2261 .set pseudo-ops. We don't have to check
2262 prev_insn_unreordered, because prev_insn_valid will
2263 be 0 in that case. We don't want to use
2264 prev_prev_insn_valid, because we do want to be able
2265 to swap at the start of a function. */
2266 || prev_prev_insn_unreordered
2267 /* If the branch is itself the target of a branch, we
2268 can not swap. We cheat on this; all we check for is
2269 whether there is a label on this instruction. If
2270 there are any branches to anything other than a
2271 label, users must use .set noreorder. */
2272 || insn_labels != NULL
2273 /* If the previous instruction is in a variant frag, we
2274 can not do the swap. This does not apply to the
2275 mips16, which uses variant frags for different
2276 purposes. */
2277 || (! mips_opts.mips16
2278 && prev_insn_frag->fr_type == rs_machine_dependent)
2279 /* If the branch reads the condition codes, we don't
2280 even try to swap, because in the sequence
2281 ctc1 $X,$31
2282 INSN
2283 INSN
2284 bc1t LABEL
2285 we can not swap, and I don't feel like handling that
2286 case. */
2287 || (! mips_opts.mips16
2288 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2289 && (pinfo & INSN_READ_COND_CODE))
2290 /* We can not swap with an instruction that requires a
2291 delay slot, becase the target of the branch might
2292 interfere with that instruction. */
2293 || (! mips_opts.mips16
2294 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2295 && (prev_pinfo
2296 /* Itbl support may require additional care here. */
2297 & (INSN_LOAD_COPROC_DELAY
2298 | INSN_COPROC_MOVE_DELAY
2299 | INSN_WRITE_COND_CODE)))
2300 || (! (hilo_interlocks
2301 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2302 && (prev_pinfo
2303 & (INSN_READ_LO
2304 | INSN_READ_HI)))
2305 || (! mips_opts.mips16
2306 && ! gpr_interlocks
2307 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2308 || (! mips_opts.mips16
2309 && mips_opts.isa == ISA_MIPS1
2310 /* Itbl support may require additional care here. */
2311 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2312 /* We can not swap with a branch instruction. */
2313 || (prev_pinfo
2314 & (INSN_UNCOND_BRANCH_DELAY
2315 | INSN_COND_BRANCH_DELAY
2316 | INSN_COND_BRANCH_LIKELY))
2317 /* We do not swap with a trap instruction, since it
2318 complicates trap handlers to have the trap
2319 instruction be in a delay slot. */
2320 || (prev_pinfo & INSN_TRAP)
2321 /* If the branch reads a register that the previous
2322 instruction sets, we can not swap. */
2323 || (! mips_opts.mips16
2324 && (prev_pinfo & INSN_WRITE_GPR_T)
2325 && insn_uses_reg (ip,
2326 ((prev_insn.insn_opcode >> OP_SH_RT)
2327 & OP_MASK_RT),
2328 MIPS_GR_REG))
2329 || (! mips_opts.mips16
2330 && (prev_pinfo & INSN_WRITE_GPR_D)
2331 && insn_uses_reg (ip,
2332 ((prev_insn.insn_opcode >> OP_SH_RD)
2333 & OP_MASK_RD),
2334 MIPS_GR_REG))
2335 || (mips_opts.mips16
2336 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2337 && insn_uses_reg (ip,
2338 ((prev_insn.insn_opcode
2339 >> MIPS16OP_SH_RX)
2340 & MIPS16OP_MASK_RX),
2341 MIPS16_REG))
2342 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2343 && insn_uses_reg (ip,
2344 ((prev_insn.insn_opcode
2345 >> MIPS16OP_SH_RY)
2346 & MIPS16OP_MASK_RY),
2347 MIPS16_REG))
2348 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2349 && insn_uses_reg (ip,
2350 ((prev_insn.insn_opcode
2351 >> MIPS16OP_SH_RZ)
2352 & MIPS16OP_MASK_RZ),
2353 MIPS16_REG))
2354 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2355 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2356 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2357 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2358 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2359 && insn_uses_reg (ip,
2360 MIPS16OP_EXTRACT_REG32R (prev_insn.
2361 insn_opcode),
2362 MIPS_GR_REG))))
2363 /* If the branch writes a register that the previous
2364 instruction sets, we can not swap (we know that
2365 branches write only to RD or to $31). */
2366 || (! mips_opts.mips16
2367 && (prev_pinfo & INSN_WRITE_GPR_T)
2368 && (((pinfo & INSN_WRITE_GPR_D)
2369 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2370 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2371 || ((pinfo & INSN_WRITE_GPR_31)
2372 && (((prev_insn.insn_opcode >> OP_SH_RT)
2373 & OP_MASK_RT)
2374 == RA))))
2375 || (! mips_opts.mips16
2376 && (prev_pinfo & INSN_WRITE_GPR_D)
2377 && (((pinfo & INSN_WRITE_GPR_D)
2378 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2379 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2380 || ((pinfo & INSN_WRITE_GPR_31)
2381 && (((prev_insn.insn_opcode >> OP_SH_RD)
2382 & OP_MASK_RD)
2383 == RA))))
2384 || (mips_opts.mips16
2385 && (pinfo & MIPS16_INSN_WRITE_31)
2386 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2387 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2388 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2389 == RA))))
2390 /* If the branch writes a register that the previous
2391 instruction reads, we can not swap (we know that
2392 branches only write to RD or to $31). */
2393 || (! mips_opts.mips16
2394 && (pinfo & INSN_WRITE_GPR_D)
2395 && insn_uses_reg (&prev_insn,
2396 ((ip->insn_opcode >> OP_SH_RD)
2397 & OP_MASK_RD),
2398 MIPS_GR_REG))
2399 || (! mips_opts.mips16
2400 && (pinfo & INSN_WRITE_GPR_31)
2401 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2402 || (mips_opts.mips16
2403 && (pinfo & MIPS16_INSN_WRITE_31)
2404 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2405 /* If we are generating embedded PIC code, the branch
2406 might be expanded into a sequence which uses $at, so
2407 we can't swap with an instruction which reads it. */
2408 || (mips_pic == EMBEDDED_PIC
2409 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2410 /* If the previous previous instruction has a load
2411 delay, and sets a register that the branch reads, we
2412 can not swap. */
2413 || (! mips_opts.mips16
2414 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2415 /* Itbl support may require additional care here. */
2416 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2417 || (! gpr_interlocks
2418 && (prev_prev_insn.insn_mo->pinfo
2419 & INSN_LOAD_MEMORY_DELAY)))
2420 && insn_uses_reg (ip,
2421 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2422 & OP_MASK_RT),
2423 MIPS_GR_REG))
2424 /* If one instruction sets a condition code and the
2425 other one uses a condition code, we can not swap. */
2426 || ((pinfo & INSN_READ_COND_CODE)
2427 && (prev_pinfo & INSN_WRITE_COND_CODE))
2428 || ((pinfo & INSN_WRITE_COND_CODE)
2429 && (prev_pinfo & INSN_READ_COND_CODE))
2430 /* If the previous instruction uses the PC, we can not
2431 swap. */
2432 || (mips_opts.mips16
2433 && (prev_pinfo & MIPS16_INSN_READ_PC))
2434 /* If the previous instruction was extended, we can not
2435 swap. */
2436 || (mips_opts.mips16 && prev_insn_extended)
2437 /* If the previous instruction had a fixup in mips16
2438 mode, we can not swap. This normally means that the
2439 previous instruction was a 4 byte branch anyhow. */
2440 || (mips_opts.mips16 && prev_insn_fixp[0])
2441 /* If the previous instruction is a sync, sync.l, or
2442 sync.p, we can not swap. */
2443 || (prev_pinfo & INSN_SYNC))
2445 /* We could do even better for unconditional branches to
2446 portions of this object file; we could pick up the
2447 instruction at the destination, put it in the delay
2448 slot, and bump the destination address. */
2449 emit_nop ();
2450 /* Update the previous insn information. */
2451 prev_prev_insn = *ip;
2452 prev_insn.insn_mo = &dummy_opcode;
2454 else
2456 /* It looks like we can actually do the swap. */
2457 if (! mips_opts.mips16)
2459 char *prev_f;
2460 char temp[4];
2462 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2463 memcpy (temp, prev_f, 4);
2464 memcpy (prev_f, f, 4);
2465 memcpy (f, temp, 4);
2466 if (prev_insn_fixp[0])
2468 prev_insn_fixp[0]->fx_frag = frag_now;
2469 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2471 if (prev_insn_fixp[1])
2473 prev_insn_fixp[1]->fx_frag = frag_now;
2474 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2476 if (prev_insn_fixp[2])
2478 prev_insn_fixp[2]->fx_frag = frag_now;
2479 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2481 if (prev_insn_fixp[0] && HAVE_NEWABI
2482 && prev_insn_frag != frag_now
2483 && (prev_insn_fixp[0]->fx_r_type
2484 == BFD_RELOC_MIPS_GOT_DISP
2485 || (prev_insn_fixp[0]->fx_r_type
2486 == BFD_RELOC_MIPS_CALL16)))
2488 /* To avoid confusion in tc_gen_reloc, we must
2489 ensure that this does not become a variant
2490 frag. */
2491 force_new_frag = TRUE;
2493 if (fixp[0])
2495 fixp[0]->fx_frag = prev_insn_frag;
2496 fixp[0]->fx_where = prev_insn_where;
2498 if (fixp[1])
2500 fixp[1]->fx_frag = prev_insn_frag;
2501 fixp[1]->fx_where = prev_insn_where;
2503 if (fixp[2])
2505 fixp[2]->fx_frag = prev_insn_frag;
2506 fixp[2]->fx_where = prev_insn_where;
2509 else
2511 char *prev_f;
2512 char temp[2];
2514 assert (prev_insn_fixp[0] == NULL);
2515 assert (prev_insn_fixp[1] == NULL);
2516 assert (prev_insn_fixp[2] == NULL);
2517 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2518 memcpy (temp, prev_f, 2);
2519 memcpy (prev_f, f, 2);
2520 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2522 assert (*reloc_type == BFD_RELOC_UNUSED);
2523 memcpy (f, temp, 2);
2525 else
2527 memcpy (f, f + 2, 2);
2528 memcpy (f + 2, temp, 2);
2530 if (fixp[0])
2532 fixp[0]->fx_frag = prev_insn_frag;
2533 fixp[0]->fx_where = prev_insn_where;
2535 if (fixp[1])
2537 fixp[1]->fx_frag = prev_insn_frag;
2538 fixp[1]->fx_where = prev_insn_where;
2540 if (fixp[2])
2542 fixp[2]->fx_frag = prev_insn_frag;
2543 fixp[2]->fx_where = prev_insn_where;
2547 /* Update the previous insn information; leave prev_insn
2548 unchanged. */
2549 prev_prev_insn = *ip;
2551 prev_insn_is_delay_slot = 1;
2553 /* If that was an unconditional branch, forget the previous
2554 insn information. */
2555 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2557 prev_prev_insn.insn_mo = &dummy_opcode;
2558 prev_insn.insn_mo = &dummy_opcode;
2561 prev_insn_fixp[0] = NULL;
2562 prev_insn_fixp[1] = NULL;
2563 prev_insn_fixp[2] = NULL;
2564 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2565 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2566 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2567 prev_insn_extended = 0;
2569 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2571 /* We don't yet optimize a branch likely. What we should do
2572 is look at the target, copy the instruction found there
2573 into the delay slot, and increment the branch to jump to
2574 the next instruction. */
2575 emit_nop ();
2576 /* Update the previous insn information. */
2577 prev_prev_insn = *ip;
2578 prev_insn.insn_mo = &dummy_opcode;
2579 prev_insn_fixp[0] = NULL;
2580 prev_insn_fixp[1] = NULL;
2581 prev_insn_fixp[2] = NULL;
2582 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2583 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2584 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2585 prev_insn_extended = 0;
2587 else
2589 /* Update the previous insn information. */
2590 if (nops > 0)
2591 prev_prev_insn.insn_mo = &dummy_opcode;
2592 else
2593 prev_prev_insn = prev_insn;
2594 prev_insn = *ip;
2596 /* Any time we see a branch, we always fill the delay slot
2597 immediately; since this insn is not a branch, we know it
2598 is not in a delay slot. */
2599 prev_insn_is_delay_slot = 0;
2601 prev_insn_fixp[0] = fixp[0];
2602 prev_insn_fixp[1] = fixp[1];
2603 prev_insn_fixp[2] = fixp[2];
2604 prev_insn_reloc_type[0] = reloc_type[0];
2605 prev_insn_reloc_type[1] = reloc_type[1];
2606 prev_insn_reloc_type[2] = reloc_type[2];
2607 if (mips_opts.mips16)
2608 prev_insn_extended = (ip->use_extend
2609 || *reloc_type > BFD_RELOC_UNUSED);
2612 prev_prev_insn_unreordered = prev_insn_unreordered;
2613 prev_insn_unreordered = 0;
2614 prev_insn_frag = frag_now;
2615 prev_insn_where = f - frag_now->fr_literal;
2616 prev_insn_valid = 1;
2618 else if (place == NULL)
2620 /* We need to record a bit of information even when we are not
2621 reordering, in order to determine the base address for mips16
2622 PC relative relocs. */
2623 prev_prev_insn = prev_insn;
2624 prev_insn = *ip;
2625 prev_insn_reloc_type[0] = reloc_type[0];
2626 prev_insn_reloc_type[1] = reloc_type[1];
2627 prev_insn_reloc_type[2] = reloc_type[2];
2628 prev_prev_insn_unreordered = prev_insn_unreordered;
2629 prev_insn_unreordered = 1;
2632 /* We just output an insn, so the next one doesn't have a label. */
2633 mips_clear_insn_labels ();
2635 /* We must ensure that the frag to which an instruction that was
2636 moved from a non-variant frag doesn't become a variant frag,
2637 otherwise tc_gen_reloc may get confused. */
2638 if (force_new_frag)
2640 frag_wane (frag_now);
2641 frag_new (0);
2645 /* This function forgets that there was any previous instruction or
2646 label. If PRESERVE is non-zero, it remembers enough information to
2647 know whether nops are needed before a noreorder section. */
2649 static void
2650 mips_no_prev_insn (int preserve)
2652 if (! preserve)
2654 prev_insn.insn_mo = &dummy_opcode;
2655 prev_prev_insn.insn_mo = &dummy_opcode;
2656 prev_nop_frag = NULL;
2657 prev_nop_frag_holds = 0;
2658 prev_nop_frag_required = 0;
2659 prev_nop_frag_since = 0;
2661 prev_insn_valid = 0;
2662 prev_insn_is_delay_slot = 0;
2663 prev_insn_unreordered = 0;
2664 prev_insn_extended = 0;
2665 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2666 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2667 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2668 prev_prev_insn_unreordered = 0;
2669 mips_clear_insn_labels ();
2672 /* This function must be called whenever we turn on noreorder or emit
2673 something other than instructions. It inserts any NOPS which might
2674 be needed by the previous instruction, and clears the information
2675 kept for the previous instructions. The INSNS parameter is true if
2676 instructions are to follow. */
2678 static void
2679 mips_emit_delays (bfd_boolean insns)
2681 if (! mips_opts.noreorder)
2683 int nops;
2685 nops = 0;
2686 if ((! mips_opts.mips16
2687 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2688 && (! cop_interlocks
2689 && (prev_insn.insn_mo->pinfo
2690 & (INSN_LOAD_COPROC_DELAY
2691 | INSN_COPROC_MOVE_DELAY
2692 | INSN_WRITE_COND_CODE))))
2693 || (! hilo_interlocks
2694 && (prev_insn.insn_mo->pinfo
2695 & (INSN_READ_LO
2696 | INSN_READ_HI)))
2697 || (! mips_opts.mips16
2698 && ! gpr_interlocks
2699 && (prev_insn.insn_mo->pinfo
2700 & INSN_LOAD_MEMORY_DELAY))
2701 || (! mips_opts.mips16
2702 && mips_opts.isa == ISA_MIPS1
2703 && (prev_insn.insn_mo->pinfo
2704 & INSN_COPROC_MEMORY_DELAY)))
2706 /* Itbl support may require additional care here. */
2707 ++nops;
2708 if ((! mips_opts.mips16
2709 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2710 && (! cop_interlocks
2711 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2712 || (! hilo_interlocks
2713 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2714 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2715 ++nops;
2717 if (prev_insn_unreordered)
2718 nops = 0;
2720 else if ((! mips_opts.mips16
2721 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2722 && (! cop_interlocks
2723 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2724 || (! hilo_interlocks
2725 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2726 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2728 /* Itbl support may require additional care here. */
2729 if (! prev_prev_insn_unreordered)
2730 ++nops;
2733 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2735 int min_nops = 0;
2736 const char *pn = prev_insn.insn_mo->name;
2737 if (strncmp(pn, "macc", 4) == 0
2738 || strncmp(pn, "dmacc", 5) == 0
2739 || strncmp(pn, "dmult", 5) == 0)
2741 min_nops = 1;
2743 if (nops < min_nops)
2744 nops = min_nops;
2747 if (nops > 0)
2749 struct insn_label_list *l;
2751 if (insns)
2753 /* Record the frag which holds the nop instructions, so
2754 that we can remove them if we don't need them. */
2755 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2756 prev_nop_frag = frag_now;
2757 prev_nop_frag_holds = nops;
2758 prev_nop_frag_required = 0;
2759 prev_nop_frag_since = 0;
2762 for (; nops > 0; --nops)
2763 emit_nop ();
2765 if (insns)
2767 /* Move on to a new frag, so that it is safe to simply
2768 decrease the size of prev_nop_frag. */
2769 frag_wane (frag_now);
2770 frag_new (0);
2773 for (l = insn_labels; l != NULL; l = l->next)
2775 valueT val;
2777 assert (S_GET_SEGMENT (l->label) == now_seg);
2778 symbol_set_frag (l->label, frag_now);
2779 val = (valueT) frag_now_fix ();
2780 /* mips16 text labels are stored as odd. */
2781 if (mips_opts.mips16)
2782 ++val;
2783 S_SET_VALUE (l->label, val);
2788 /* Mark instruction labels in mips16 mode. */
2789 if (insns)
2790 mips16_mark_labels ();
2792 mips_no_prev_insn (insns);
2795 /* Build an instruction created by a macro expansion. This is passed
2796 a pointer to the count of instructions created so far, an
2797 expression, the name of the instruction to build, an operand format
2798 string, and corresponding arguments. */
2800 static void
2801 macro_build (char *place, int *counter, expressionS *ep, const char *name,
2802 const char *fmt, ...)
2804 struct mips_cl_insn insn;
2805 bfd_reloc_code_real_type r[3];
2806 va_list args;
2808 va_start (args, fmt);
2811 * If the macro is about to expand into a second instruction,
2812 * print a warning if needed. We need to pass ip as a parameter
2813 * to generate a better warning message here...
2815 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2816 as_warn (_("Macro instruction expanded into multiple instructions"));
2819 * If the macro is about to expand into a second instruction,
2820 * and it is in a delay slot, print a warning.
2822 if (place == NULL
2823 && *counter == 1
2824 && mips_opts.noreorder
2825 && (prev_prev_insn.insn_mo->pinfo
2826 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2827 | INSN_COND_BRANCH_LIKELY)) != 0)
2828 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2830 if (place == NULL)
2831 ++*counter; /* bump instruction counter */
2833 if (mips_opts.mips16)
2835 mips16_macro_build (place, counter, ep, name, fmt, args);
2836 va_end (args);
2837 return;
2840 r[0] = BFD_RELOC_UNUSED;
2841 r[1] = BFD_RELOC_UNUSED;
2842 r[2] = BFD_RELOC_UNUSED;
2843 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2844 assert (insn.insn_mo);
2845 assert (strcmp (name, insn.insn_mo->name) == 0);
2847 /* Search until we get a match for NAME. */
2848 while (1)
2850 /* It is assumed here that macros will never generate
2851 MDMX or MIPS-3D instructions. */
2852 if (strcmp (fmt, insn.insn_mo->args) == 0
2853 && insn.insn_mo->pinfo != INSN_MACRO
2854 && OPCODE_IS_MEMBER (insn.insn_mo,
2855 (mips_opts.isa
2856 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2857 mips_opts.arch)
2858 && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2859 break;
2861 ++insn.insn_mo;
2862 assert (insn.insn_mo->name);
2863 assert (strcmp (name, insn.insn_mo->name) == 0);
2866 insn.insn_opcode = insn.insn_mo->match;
2867 for (;;)
2869 switch (*fmt++)
2871 case '\0':
2872 break;
2874 case ',':
2875 case '(':
2876 case ')':
2877 continue;
2879 case '+':
2880 switch (*fmt++)
2882 case 'A':
2883 case 'E':
2884 insn.insn_opcode |= (va_arg (args, int)
2885 & OP_MASK_SHAMT) << OP_SH_SHAMT;
2886 continue;
2888 case 'B':
2889 case 'F':
2890 /* Note that in the macro case, these arguments are already
2891 in MSB form. (When handling the instruction in the
2892 non-macro case, these arguments are sizes from which
2893 MSB values must be calculated.) */
2894 insn.insn_opcode |= (va_arg (args, int)
2895 & OP_MASK_INSMSB) << OP_SH_INSMSB;
2896 continue;
2898 case 'C':
2899 case 'G':
2900 case 'H':
2901 /* Note that in the macro case, these arguments are already
2902 in MSBD form. (When handling the instruction in the
2903 non-macro case, these arguments are sizes from which
2904 MSBD values must be calculated.) */
2905 insn.insn_opcode |= (va_arg (args, int)
2906 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
2907 continue;
2909 default:
2910 internalError ();
2912 continue;
2914 case 't':
2915 case 'w':
2916 case 'E':
2917 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2918 continue;
2920 case 'c':
2921 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2922 continue;
2924 case 'T':
2925 case 'W':
2926 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2927 continue;
2929 case 'd':
2930 case 'G':
2931 case 'K':
2932 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2933 continue;
2935 case 'U':
2937 int tmp = va_arg (args, int);
2939 insn.insn_opcode |= tmp << OP_SH_RT;
2940 insn.insn_opcode |= tmp << OP_SH_RD;
2941 continue;
2944 case 'V':
2945 case 'S':
2946 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2947 continue;
2949 case 'z':
2950 continue;
2952 case '<':
2953 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2954 continue;
2956 case 'D':
2957 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2958 continue;
2960 case 'B':
2961 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2962 continue;
2964 case 'J':
2965 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2966 continue;
2968 case 'q':
2969 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2970 continue;
2972 case 'b':
2973 case 's':
2974 case 'r':
2975 case 'v':
2976 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2977 continue;
2979 case 'i':
2980 case 'j':
2981 case 'o':
2982 *r = (bfd_reloc_code_real_type) va_arg (args, int);
2983 assert (*r == BFD_RELOC_GPREL16
2984 || *r == BFD_RELOC_MIPS_LITERAL
2985 || *r == BFD_RELOC_MIPS_HIGHER
2986 || *r == BFD_RELOC_HI16_S
2987 || *r == BFD_RELOC_LO16
2988 || *r == BFD_RELOC_MIPS_GOT16
2989 || *r == BFD_RELOC_MIPS_CALL16
2990 || *r == BFD_RELOC_MIPS_GOT_DISP
2991 || *r == BFD_RELOC_MIPS_GOT_PAGE
2992 || *r == BFD_RELOC_MIPS_GOT_OFST
2993 || *r == BFD_RELOC_MIPS_GOT_LO16
2994 || *r == BFD_RELOC_MIPS_CALL_LO16
2995 || (ep->X_op == O_subtract
2996 && *r == BFD_RELOC_PCREL_LO16));
2997 continue;
2999 case 'u':
3000 *r = (bfd_reloc_code_real_type) va_arg (args, int);
3001 assert (ep != NULL
3002 && (ep->X_op == O_constant
3003 || (ep->X_op == O_symbol
3004 && (*r == BFD_RELOC_MIPS_HIGHEST
3005 || *r == BFD_RELOC_HI16_S
3006 || *r == BFD_RELOC_HI16
3007 || *r == BFD_RELOC_GPREL16
3008 || *r == BFD_RELOC_MIPS_GOT_HI16
3009 || *r == BFD_RELOC_MIPS_CALL_HI16))
3010 || (ep->X_op == O_subtract
3011 && *r == BFD_RELOC_PCREL_HI16_S)));
3012 continue;
3014 case 'p':
3015 assert (ep != NULL);
3017 * This allows macro() to pass an immediate expression for
3018 * creating short branches without creating a symbol.
3019 * Note that the expression still might come from the assembly
3020 * input, in which case the value is not checked for range nor
3021 * is a relocation entry generated (yuck).
3023 if (ep->X_op == O_constant)
3025 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3026 ep = NULL;
3028 else
3029 *r = BFD_RELOC_16_PCREL_S2;
3030 continue;
3032 case 'a':
3033 assert (ep != NULL);
3034 *r = BFD_RELOC_MIPS_JMP;
3035 continue;
3037 case 'C':
3038 insn.insn_opcode |= va_arg (args, unsigned long);
3039 continue;
3041 default:
3042 internalError ();
3044 break;
3046 va_end (args);
3047 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3049 append_insn (place, &insn, ep, r);
3052 static void
3053 mips16_macro_build (char *place, int *counter ATTRIBUTE_UNUSED,
3054 expressionS *ep, const char *name, const char *fmt,
3055 va_list args)
3057 struct mips_cl_insn insn;
3058 bfd_reloc_code_real_type r[3]
3059 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3061 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3062 assert (insn.insn_mo);
3063 assert (strcmp (name, insn.insn_mo->name) == 0);
3065 while (strcmp (fmt, insn.insn_mo->args) != 0
3066 || insn.insn_mo->pinfo == INSN_MACRO)
3068 ++insn.insn_mo;
3069 assert (insn.insn_mo->name);
3070 assert (strcmp (name, insn.insn_mo->name) == 0);
3073 insn.insn_opcode = insn.insn_mo->match;
3074 insn.use_extend = FALSE;
3076 for (;;)
3078 int c;
3080 c = *fmt++;
3081 switch (c)
3083 case '\0':
3084 break;
3086 case ',':
3087 case '(':
3088 case ')':
3089 continue;
3091 case 'y':
3092 case 'w':
3093 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3094 continue;
3096 case 'x':
3097 case 'v':
3098 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3099 continue;
3101 case 'z':
3102 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3103 continue;
3105 case 'Z':
3106 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3107 continue;
3109 case '0':
3110 case 'S':
3111 case 'P':
3112 case 'R':
3113 continue;
3115 case 'X':
3116 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3117 continue;
3119 case 'Y':
3121 int regno;
3123 regno = va_arg (args, int);
3124 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3125 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3127 continue;
3129 case '<':
3130 case '>':
3131 case '4':
3132 case '5':
3133 case 'H':
3134 case 'W':
3135 case 'D':
3136 case 'j':
3137 case '8':
3138 case 'V':
3139 case 'C':
3140 case 'U':
3141 case 'k':
3142 case 'K':
3143 case 'p':
3144 case 'q':
3146 assert (ep != NULL);
3148 if (ep->X_op != O_constant)
3149 *r = (int) BFD_RELOC_UNUSED + c;
3150 else
3152 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3153 FALSE, &insn.insn_opcode, &insn.use_extend,
3154 &insn.extend);
3155 ep = NULL;
3156 *r = BFD_RELOC_UNUSED;
3159 continue;
3161 case '6':
3162 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3163 continue;
3166 break;
3169 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3171 append_insn (place, &insn, ep, r);
3175 * Generate a "jalr" instruction with a relocation hint to the called
3176 * function. This occurs in NewABI PIC code.
3178 static void
3179 macro_build_jalr (int icnt, expressionS *ep)
3181 char *f = NULL;
3183 if (HAVE_NEWABI)
3185 frag_grow (4);
3186 f = frag_more (0);
3188 macro_build (NULL, &icnt, NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3189 if (HAVE_NEWABI)
3190 fix_new_exp (frag_now, f - frag_now->fr_literal,
3191 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3195 * Generate a "lui" instruction.
3197 static void
3198 macro_build_lui (char *place, int *counter, expressionS *ep, int regnum)
3200 expressionS high_expr;
3201 struct mips_cl_insn insn;
3202 bfd_reloc_code_real_type r[3]
3203 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3204 const char *name = "lui";
3205 const char *fmt = "t,u";
3207 assert (! mips_opts.mips16);
3209 if (place == NULL)
3210 high_expr = *ep;
3211 else
3213 high_expr.X_op = O_constant;
3214 high_expr.X_add_number = ep->X_add_number;
3217 if (high_expr.X_op == O_constant)
3219 /* we can compute the instruction now without a relocation entry */
3220 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3221 >> 16) & 0xffff;
3222 *r = BFD_RELOC_UNUSED;
3224 else
3226 assert (ep->X_op == O_symbol);
3227 /* _gp_disp is a special case, used from s_cpload. */
3228 assert (mips_pic == NO_PIC
3229 || (! HAVE_NEWABI
3230 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3231 *r = BFD_RELOC_HI16_S;
3235 * If the macro is about to expand into a second instruction,
3236 * print a warning if needed. We need to pass ip as a parameter
3237 * to generate a better warning message here...
3239 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3240 as_warn (_("Macro instruction expanded into multiple instructions"));
3242 if (place == NULL)
3243 ++*counter; /* bump instruction counter */
3245 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3246 assert (insn.insn_mo);
3247 assert (strcmp (name, insn.insn_mo->name) == 0);
3248 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3250 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3251 if (*r == BFD_RELOC_UNUSED)
3253 insn.insn_opcode |= high_expr.X_add_number;
3254 append_insn (place, &insn, NULL, r);
3256 else
3257 append_insn (place, &insn, &high_expr, r);
3260 /* Generate a sequence of instructions to do a load or store from a constant
3261 offset off of a base register (breg) into/from a target register (treg),
3262 using AT if necessary. */
3263 static void
3264 macro_build_ldst_constoffset (char *place, int *counter, expressionS *ep,
3265 const char *op, int treg, int breg, int dbl)
3267 assert (ep->X_op == O_constant);
3269 /* Sign-extending 32-bit constants makes their handling easier. */
3270 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3271 == ~((bfd_vma) 0x7fffffff)))
3273 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3274 as_bad (_("constant too large"));
3276 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3277 - 0x80000000);
3280 /* Right now, this routine can only handle signed 32-bit contants. */
3281 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3282 as_warn (_("operand overflow"));
3284 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3286 /* Signed 16-bit offset will fit in the op. Easy! */
3287 macro_build (place, counter, ep, op, "t,o(b)", treg, BFD_RELOC_LO16,
3288 breg);
3290 else
3292 /* 32-bit offset, need multiple instructions and AT, like:
3293 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3294 addu $tempreg,$tempreg,$breg
3295 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3296 to handle the complete offset. */
3297 macro_build_lui (place, counter, ep, AT);
3298 if (place != NULL)
3299 place += 4;
3300 macro_build (place, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT,
3301 breg);
3302 if (place != NULL)
3303 place += 4;
3304 macro_build (place, counter, ep, op, "t,o(b)", treg, BFD_RELOC_LO16,
3305 AT);
3307 if (mips_opts.noat)
3308 as_warn (_("Macro used $at after \".set noat\""));
3312 /* set_at()
3313 * Generates code to set the $at register to true (one)
3314 * if reg is less than the immediate expression.
3316 static void
3317 set_at (int *counter, int reg, int unsignedp)
3319 if (imm_expr.X_op == O_constant
3320 && imm_expr.X_add_number >= -0x8000
3321 && imm_expr.X_add_number < 0x8000)
3322 macro_build (NULL, counter, &imm_expr, unsignedp ? "sltiu" : "slti",
3323 "t,r,j", AT, reg, BFD_RELOC_LO16);
3324 else
3326 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3327 macro_build (NULL, counter, NULL, unsignedp ? "sltu" : "slt",
3328 "d,v,t", AT, reg, AT);
3332 static void
3333 normalize_constant_expr (expressionS *ex)
3335 if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3336 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3337 - 0x80000000);
3340 /* Warn if an expression is not a constant. */
3342 static void
3343 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3345 if (ex->X_op == O_big)
3346 as_bad (_("unsupported large constant"));
3347 else if (ex->X_op != O_constant)
3348 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3350 normalize_constant_expr (ex);
3353 /* Count the leading zeroes by performing a binary chop. This is a
3354 bulky bit of source, but performance is a LOT better for the
3355 majority of values than a simple loop to count the bits:
3356 for (lcnt = 0; (lcnt < 32); lcnt++)
3357 if ((v) & (1 << (31 - lcnt)))
3358 break;
3359 However it is not code size friendly, and the gain will drop a bit
3360 on certain cached systems.
3362 #define COUNT_TOP_ZEROES(v) \
3363 (((v) & ~0xffff) == 0 \
3364 ? ((v) & ~0xff) == 0 \
3365 ? ((v) & ~0xf) == 0 \
3366 ? ((v) & ~0x3) == 0 \
3367 ? ((v) & ~0x1) == 0 \
3368 ? !(v) \
3369 ? 32 \
3370 : 31 \
3371 : 30 \
3372 : ((v) & ~0x7) == 0 \
3373 ? 29 \
3374 : 28 \
3375 : ((v) & ~0x3f) == 0 \
3376 ? ((v) & ~0x1f) == 0 \
3377 ? 27 \
3378 : 26 \
3379 : ((v) & ~0x7f) == 0 \
3380 ? 25 \
3381 : 24 \
3382 : ((v) & ~0xfff) == 0 \
3383 ? ((v) & ~0x3ff) == 0 \
3384 ? ((v) & ~0x1ff) == 0 \
3385 ? 23 \
3386 : 22 \
3387 : ((v) & ~0x7ff) == 0 \
3388 ? 21 \
3389 : 20 \
3390 : ((v) & ~0x3fff) == 0 \
3391 ? ((v) & ~0x1fff) == 0 \
3392 ? 19 \
3393 : 18 \
3394 : ((v) & ~0x7fff) == 0 \
3395 ? 17 \
3396 : 16 \
3397 : ((v) & ~0xffffff) == 0 \
3398 ? ((v) & ~0xfffff) == 0 \
3399 ? ((v) & ~0x3ffff) == 0 \
3400 ? ((v) & ~0x1ffff) == 0 \
3401 ? 15 \
3402 : 14 \
3403 : ((v) & ~0x7ffff) == 0 \
3404 ? 13 \
3405 : 12 \
3406 : ((v) & ~0x3fffff) == 0 \
3407 ? ((v) & ~0x1fffff) == 0 \
3408 ? 11 \
3409 : 10 \
3410 : ((v) & ~0x7fffff) == 0 \
3411 ? 9 \
3412 : 8 \
3413 : ((v) & ~0xfffffff) == 0 \
3414 ? ((v) & ~0x3ffffff) == 0 \
3415 ? ((v) & ~0x1ffffff) == 0 \
3416 ? 7 \
3417 : 6 \
3418 : ((v) & ~0x7ffffff) == 0 \
3419 ? 5 \
3420 : 4 \
3421 : ((v) & ~0x3fffffff) == 0 \
3422 ? ((v) & ~0x1fffffff) == 0 \
3423 ? 3 \
3424 : 2 \
3425 : ((v) & ~0x7fffffff) == 0 \
3426 ? 1 \
3427 : 0)
3429 /* load_register()
3430 * This routine generates the least number of instructions neccessary to load
3431 * an absolute expression value into a register.
3433 static void
3434 load_register (int *counter, int reg, expressionS *ep, int dbl)
3436 int freg;
3437 expressionS hi32, lo32;
3439 if (ep->X_op != O_big)
3441 assert (ep->X_op == O_constant);
3443 /* Sign-extending 32-bit constants makes their handling easier. */
3444 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3445 == ~((bfd_vma) 0x7fffffff)))
3447 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3448 as_bad (_("constant too large"));
3450 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3451 - 0x80000000);
3454 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3456 /* We can handle 16 bit signed values with an addiu to
3457 $zero. No need to ever use daddiu here, since $zero and
3458 the result are always correct in 32 bit mode. */
3459 macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3460 BFD_RELOC_LO16);
3461 return;
3463 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3465 /* We can handle 16 bit unsigned values with an ori to
3466 $zero. */
3467 macro_build (NULL, counter, ep, "ori", "t,r,i", reg, 0,
3468 BFD_RELOC_LO16);
3469 return;
3471 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3473 /* 32 bit values require an lui. */
3474 macro_build (NULL, counter, ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3475 if ((ep->X_add_number & 0xffff) != 0)
3476 macro_build (NULL, counter, ep, "ori", "t,r,i", reg, reg,
3477 BFD_RELOC_LO16);
3478 return;
3482 /* The value is larger than 32 bits. */
3484 if (HAVE_32BIT_GPRS)
3486 as_bad (_("Number (0x%lx) larger than 32 bits"),
3487 (unsigned long) ep->X_add_number);
3488 macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3489 BFD_RELOC_LO16);
3490 return;
3493 if (ep->X_op != O_big)
3495 hi32 = *ep;
3496 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3497 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3498 hi32.X_add_number &= 0xffffffff;
3499 lo32 = *ep;
3500 lo32.X_add_number &= 0xffffffff;
3502 else
3504 assert (ep->X_add_number > 2);
3505 if (ep->X_add_number == 3)
3506 generic_bignum[3] = 0;
3507 else if (ep->X_add_number > 4)
3508 as_bad (_("Number larger than 64 bits"));
3509 lo32.X_op = O_constant;
3510 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3511 hi32.X_op = O_constant;
3512 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3515 if (hi32.X_add_number == 0)
3516 freg = 0;
3517 else
3519 int shift, bit;
3520 unsigned long hi, lo;
3522 if (hi32.X_add_number == (offsetT) 0xffffffff)
3524 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3526 macro_build (NULL, counter, &lo32, "addiu", "t,r,j", reg, 0,
3527 BFD_RELOC_LO16);
3528 return;
3530 if (lo32.X_add_number & 0x80000000)
3532 macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3533 BFD_RELOC_HI16);
3534 if (lo32.X_add_number & 0xffff)
3535 macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, reg,
3536 BFD_RELOC_LO16);
3537 return;
3541 /* Check for 16bit shifted constant. We know that hi32 is
3542 non-zero, so start the mask on the first bit of the hi32
3543 value. */
3544 shift = 17;
3547 unsigned long himask, lomask;
3549 if (shift < 32)
3551 himask = 0xffff >> (32 - shift);
3552 lomask = (0xffff << shift) & 0xffffffff;
3554 else
3556 himask = 0xffff << (shift - 32);
3557 lomask = 0;
3559 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3560 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3562 expressionS tmp;
3564 tmp.X_op = O_constant;
3565 if (shift < 32)
3566 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3567 | (lo32.X_add_number >> shift));
3568 else
3569 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3570 macro_build (NULL, counter, &tmp, "ori", "t,r,i", reg, 0,
3571 BFD_RELOC_LO16);
3572 macro_build (NULL, counter, NULL,
3573 (shift >= 32) ? "dsll32" : "dsll",
3574 "d,w,<", reg, reg,
3575 (shift >= 32) ? shift - 32 : shift);
3576 return;
3578 ++shift;
3580 while (shift <= (64 - 16));
3582 /* Find the bit number of the lowest one bit, and store the
3583 shifted value in hi/lo. */
3584 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3585 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3586 if (lo != 0)
3588 bit = 0;
3589 while ((lo & 1) == 0)
3591 lo >>= 1;
3592 ++bit;
3594 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3595 hi >>= bit;
3597 else
3599 bit = 32;
3600 while ((hi & 1) == 0)
3602 hi >>= 1;
3603 ++bit;
3605 lo = hi;
3606 hi = 0;
3609 /* Optimize if the shifted value is a (power of 2) - 1. */
3610 if ((hi == 0 && ((lo + 1) & lo) == 0)
3611 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3613 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3614 if (shift != 0)
3616 expressionS tmp;
3618 /* This instruction will set the register to be all
3619 ones. */
3620 tmp.X_op = O_constant;
3621 tmp.X_add_number = (offsetT) -1;
3622 macro_build (NULL, counter, &tmp, "addiu", "t,r,j", reg, 0,
3623 BFD_RELOC_LO16);
3624 if (bit != 0)
3626 bit += shift;
3627 macro_build (NULL, counter, NULL,
3628 (bit >= 32) ? "dsll32" : "dsll",
3629 "d,w,<", reg, reg,
3630 (bit >= 32) ? bit - 32 : bit);
3632 macro_build (NULL, counter, NULL,
3633 (shift >= 32) ? "dsrl32" : "dsrl",
3634 "d,w,<", reg, reg,
3635 (shift >= 32) ? shift - 32 : shift);
3636 return;
3640 /* Sign extend hi32 before calling load_register, because we can
3641 generally get better code when we load a sign extended value. */
3642 if ((hi32.X_add_number & 0x80000000) != 0)
3643 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3644 load_register (counter, reg, &hi32, 0);
3645 freg = reg;
3647 if ((lo32.X_add_number & 0xffff0000) == 0)
3649 if (freg != 0)
3651 macro_build (NULL, counter, NULL, "dsll32", "d,w,<", reg, freg, 0);
3652 freg = reg;
3655 else
3657 expressionS mid16;
3659 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3661 macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3662 BFD_RELOC_HI16);
3663 macro_build (NULL, counter, NULL, "dsrl32", "d,w,<", reg, reg, 0);
3664 return;
3667 if (freg != 0)
3669 macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, freg, 16);
3670 freg = reg;
3672 mid16 = lo32;
3673 mid16.X_add_number >>= 16;
3674 macro_build (NULL, counter, &mid16, "ori", "t,r,i", reg, freg,
3675 BFD_RELOC_LO16);
3676 macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
3677 freg = reg;
3679 if ((lo32.X_add_number & 0xffff) != 0)
3680 macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3681 BFD_RELOC_LO16);
3684 /* Load an address into a register. */
3686 static void
3687 load_address (int *counter, int reg, expressionS *ep, int *used_at)
3689 char *p = NULL;
3691 if (ep->X_op != O_constant
3692 && ep->X_op != O_symbol)
3694 as_bad (_("expression too complex"));
3695 ep->X_op = O_constant;
3698 if (ep->X_op == O_constant)
3700 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3701 return;
3704 if (mips_pic == NO_PIC)
3706 /* If this is a reference to a GP relative symbol, we want
3707 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3708 Otherwise we want
3709 lui $reg,<sym> (BFD_RELOC_HI16_S)
3710 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3711 If we have an addend, we always use the latter form.
3713 With 64bit address space and a usable $at we want
3714 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3715 lui $at,<sym> (BFD_RELOC_HI16_S)
3716 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3717 daddiu $at,<sym> (BFD_RELOC_LO16)
3718 dsll32 $reg,0
3719 daddu $reg,$reg,$at
3721 If $at is already in use, we use a path which is suboptimal
3722 on superscalar processors.
3723 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3724 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3725 dsll $reg,16
3726 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3727 dsll $reg,16
3728 daddiu $reg,<sym> (BFD_RELOC_LO16)
3730 if (HAVE_64BIT_ADDRESSES)
3732 /* We don't do GP optimization for now because RELAX_ENCODE can't
3733 hold the data for such large chunks. */
3735 if (*used_at == 0 && ! mips_opts.noat)
3737 macro_build (p, counter, ep, "lui", "t,u",
3738 reg, BFD_RELOC_MIPS_HIGHEST);
3739 macro_build (p, counter, ep, "lui", "t,u",
3740 AT, BFD_RELOC_HI16_S);
3741 macro_build (p, counter, ep, "daddiu", "t,r,j",
3742 reg, reg, BFD_RELOC_MIPS_HIGHER);
3743 macro_build (p, counter, ep, "daddiu", "t,r,j",
3744 AT, AT, BFD_RELOC_LO16);
3745 macro_build (p, counter, NULL, "dsll32", "d,w,<", reg, reg, 0);
3746 macro_build (p, counter, NULL, "daddu", "d,v,t", reg, reg, AT);
3747 *used_at = 1;
3749 else
3751 macro_build (p, counter, ep, "lui", "t,u",
3752 reg, BFD_RELOC_MIPS_HIGHEST);
3753 macro_build (p, counter, ep, "daddiu", "t,r,j",
3754 reg, reg, BFD_RELOC_MIPS_HIGHER);
3755 macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
3756 macro_build (p, counter, ep, "daddiu", "t,r,j",
3757 reg, reg, BFD_RELOC_HI16_S);
3758 macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
3759 macro_build (p, counter, ep, "daddiu", "t,r,j",
3760 reg, reg, BFD_RELOC_LO16);
3763 else
3765 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3766 && ! nopic_need_relax (ep->X_add_symbol, 1))
3768 frag_grow (20);
3769 macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3770 mips_gp_register, BFD_RELOC_GPREL16);
3771 p = frag_var (rs_machine_dependent, 8, 0,
3772 RELAX_ENCODE (4, 8, 0, 4, 0,
3773 mips_opts.warn_about_macros),
3774 ep->X_add_symbol, 0, NULL);
3776 macro_build_lui (p, counter, ep, reg);
3777 if (p != NULL)
3778 p += 4;
3779 macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3780 BFD_RELOC_LO16);
3783 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3785 expressionS ex;
3787 /* If this is a reference to an external symbol, we want
3788 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3789 Otherwise we want
3790 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3792 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3793 If there is a constant, it must be added in after.
3795 If we have NewABI, we want
3796 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3797 unless we're referencing a global symbol with a non-zero
3798 offset, in which case cst must be added separately. */
3799 if (HAVE_NEWABI)
3801 frag_grow (12);
3803 if (ep->X_add_number)
3805 frag_now->tc_frag_data.tc_fr_offset =
3806 ex.X_add_number = ep->X_add_number;
3807 ep->X_add_number = 0;
3808 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)",
3809 reg, BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3810 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3811 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3812 ex.X_op = O_constant;
3813 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3814 reg, reg, BFD_RELOC_LO16);
3815 p = frag_var (rs_machine_dependent, 8, 0,
3816 RELAX_ENCODE (8, 4, 0, 0, 0,
3817 mips_opts.warn_about_macros),
3818 ep->X_add_symbol, 0, NULL);
3819 ep->X_add_number = ex.X_add_number;
3822 macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3823 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3825 if (! p)
3827 /* To avoid confusion in tc_gen_reloc, we must ensure
3828 that this does not become a variant frag. */
3829 frag_wane (frag_now);
3830 frag_new (0);
3833 else
3835 ex.X_add_number = ep->X_add_number;
3836 ep->X_add_number = 0;
3837 frag_grow (20);
3838 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3839 BFD_RELOC_MIPS_GOT16,
3840 mips_gp_register);
3841 macro_build (NULL, counter, NULL, "nop", "");
3842 p = frag_var (rs_machine_dependent, 4, 0,
3843 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3844 ep->X_add_symbol, 0, NULL);
3845 macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3846 BFD_RELOC_LO16);
3848 if (ex.X_add_number != 0)
3850 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3851 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3852 ex.X_op = O_constant;
3853 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3854 reg, reg, BFD_RELOC_LO16);
3858 else if (mips_pic == SVR4_PIC)
3860 expressionS ex;
3861 int off;
3863 /* This is the large GOT case. If this is a reference to an
3864 external symbol, we want
3865 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3866 addu $reg,$reg,$gp
3867 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3869 Otherwise, for a reference to a local symbol in old ABI, we want
3870 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3872 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3873 If there is a constant, it must be added in after.
3875 In the NewABI, for local symbols, with or without offsets, we want:
3876 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3877 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
3879 if (HAVE_NEWABI)
3881 frag_grow (24);
3883 frag_now->tc_frag_data.tc_fr_offset =
3884 ex.X_add_number = ep->X_add_number;
3885 ep->X_add_number = 0;
3886 macro_build (NULL, counter, ep, "lui", "t,u", reg,
3887 BFD_RELOC_MIPS_GOT_HI16);
3888 macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3889 reg, mips_gp_register);
3890 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3891 BFD_RELOC_MIPS_GOT_LO16, reg);
3892 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3893 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3894 else if (ex.X_add_number)
3896 ex.X_op = O_constant;
3897 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3898 reg, reg, BFD_RELOC_LO16);
3901 ep->X_add_number = ex.X_add_number;
3902 p = frag_var (rs_machine_dependent, 8, 0,
3903 RELAX_ENCODE (ex.X_add_number ? 16 : 12, 8, 0, 4, 0,
3904 mips_opts.warn_about_macros),
3905 ep->X_add_symbol, 0, NULL);
3906 macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3907 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3908 macro_build (p + 4, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3909 reg, BFD_RELOC_MIPS_GOT_OFST);
3911 else
3913 ex.X_add_number = ep->X_add_number;
3914 ep->X_add_number = 0;
3915 if (reg_needs_delay (mips_gp_register))
3916 off = 4;
3917 else
3918 off = 0;
3919 frag_grow (32);
3920 macro_build (NULL, counter, ep, "lui", "t,u", reg,
3921 BFD_RELOC_MIPS_GOT_HI16);
3922 macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3923 reg, mips_gp_register);
3924 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3925 BFD_RELOC_MIPS_GOT_LO16, reg);
3926 p = frag_var (rs_machine_dependent, 12 + off, 0,
3927 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3928 mips_opts.warn_about_macros),
3929 ep->X_add_symbol, 0, NULL);
3930 if (off > 0)
3932 /* We need a nop before loading from $gp. This special
3933 check is required because the lui which starts the main
3934 instruction stream does not refer to $gp, and so will not
3935 insert the nop which may be required. */
3936 macro_build (p, counter, NULL, "nop", "");
3937 p += 4;
3939 macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3940 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3941 p += 4;
3942 macro_build (p, counter, NULL, "nop", "");
3943 p += 4;
3944 macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3945 BFD_RELOC_LO16);
3947 if (ex.X_add_number != 0)
3949 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3950 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3951 ex.X_op = O_constant;
3952 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3953 reg, reg, BFD_RELOC_LO16);
3957 else if (mips_pic == EMBEDDED_PIC)
3959 /* We always do
3960 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3962 macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3963 mips_gp_register, BFD_RELOC_GPREL16);
3965 else
3966 abort ();
3969 /* Move the contents of register SOURCE into register DEST. */
3971 static void
3972 move_register (int *counter, int dest, int source)
3974 macro_build (NULL, counter, NULL, HAVE_32BIT_GPRS ? "addu" : "daddu",
3975 "d,v,t", dest, source, 0);
3979 * Build macros
3980 * This routine implements the seemingly endless macro or synthesized
3981 * instructions and addressing modes in the mips assembly language. Many
3982 * of these macros are simple and are similar to each other. These could
3983 * probably be handled by some kind of table or grammer aproach instead of
3984 * this verbose method. Others are not simple macros but are more like
3985 * optimizing code generation.
3986 * One interesting optimization is when several store macros appear
3987 * consecutivly that would load AT with the upper half of the same address.
3988 * The ensuing load upper instructions are ommited. This implies some kind
3989 * of global optimization. We currently only optimize within a single macro.
3990 * For many of the load and store macros if the address is specified as a
3991 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3992 * first load register 'at' with zero and use it as the base register. The
3993 * mips assembler simply uses register $zero. Just one tiny optimization
3994 * we're missing.
3996 static void
3997 macro (struct mips_cl_insn *ip)
3999 register int treg, sreg, dreg, breg;
4000 int tempreg;
4001 int mask;
4002 int icnt = 0;
4003 int used_at = 0;
4004 expressionS expr1;
4005 const char *s;
4006 const char *s2;
4007 const char *fmt;
4008 int likely = 0;
4009 int dbl = 0;
4010 int coproc = 0;
4011 int lr = 0;
4012 int imm = 0;
4013 offsetT maxnum;
4014 int off;
4015 bfd_reloc_code_real_type r;
4016 int hold_mips_optimize;
4018 assert (! mips_opts.mips16);
4020 treg = (ip->insn_opcode >> 16) & 0x1f;
4021 dreg = (ip->insn_opcode >> 11) & 0x1f;
4022 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4023 mask = ip->insn_mo->mask;
4025 expr1.X_op = O_constant;
4026 expr1.X_op_symbol = NULL;
4027 expr1.X_add_symbol = NULL;
4028 expr1.X_add_number = 1;
4030 /* Umatched fixups should not be put in the same frag as a relaxable
4031 macro. For example, suppose we have:
4033 lui $4,%hi(l1) # 1
4034 la $5,l2 # 2
4035 addiu $4,$4,%lo(l1) # 3
4037 If instructions 1 and 2 were put in the same frag, md_frob_file would
4038 move the fixup for #1 after the fixups for the "unrelaxed" version of
4039 #2. This would confuse tc_gen_reloc, which expects the relocations
4040 for #2 to be the last for that frag.
4042 Also, if tc_gen_reloc sees certain relocations in a variant frag,
4043 it assumes that they belong to a relaxable macro. We mustn't put
4044 other uses of such relocations into a variant frag.
4046 To avoid both problems, finish the current frag it contains a
4047 %reloc() operator. The macro then goes into a new frag. */
4048 if (prev_reloc_op_frag == frag_now)
4050 frag_wane (frag_now);
4051 frag_new (0);
4054 switch (mask)
4056 case M_DABS:
4057 dbl = 1;
4058 case M_ABS:
4059 /* bgez $a0,.+12
4060 move v0,$a0
4061 sub v0,$zero,$a0
4064 mips_emit_delays (TRUE);
4065 ++mips_opts.noreorder;
4066 mips_any_noreorder = 1;
4068 expr1.X_add_number = 8;
4069 macro_build (NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4070 if (dreg == sreg)
4071 macro_build (NULL, &icnt, NULL, "nop", "", 0);
4072 else
4073 move_register (&icnt, dreg, sreg);
4074 macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0,
4075 sreg);
4077 --mips_opts.noreorder;
4078 return;
4080 case M_ADD_I:
4081 s = "addi";
4082 s2 = "add";
4083 goto do_addi;
4084 case M_ADDU_I:
4085 s = "addiu";
4086 s2 = "addu";
4087 goto do_addi;
4088 case M_DADD_I:
4089 dbl = 1;
4090 s = "daddi";
4091 s2 = "dadd";
4092 goto do_addi;
4093 case M_DADDU_I:
4094 dbl = 1;
4095 s = "daddiu";
4096 s2 = "daddu";
4097 do_addi:
4098 if (imm_expr.X_op == O_constant
4099 && imm_expr.X_add_number >= -0x8000
4100 && imm_expr.X_add_number < 0x8000)
4102 macro_build (NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4103 BFD_RELOC_LO16);
4104 return;
4106 load_register (&icnt, AT, &imm_expr, dbl);
4107 macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
4108 break;
4110 case M_AND_I:
4111 s = "andi";
4112 s2 = "and";
4113 goto do_bit;
4114 case M_OR_I:
4115 s = "ori";
4116 s2 = "or";
4117 goto do_bit;
4118 case M_NOR_I:
4119 s = "";
4120 s2 = "nor";
4121 goto do_bit;
4122 case M_XOR_I:
4123 s = "xori";
4124 s2 = "xor";
4125 do_bit:
4126 if (imm_expr.X_op == O_constant
4127 && imm_expr.X_add_number >= 0
4128 && imm_expr.X_add_number < 0x10000)
4130 if (mask != M_NOR_I)
4131 macro_build (NULL, &icnt, &imm_expr, s, "t,r,i", treg, sreg,
4132 BFD_RELOC_LO16);
4133 else
4135 macro_build (NULL, &icnt, &imm_expr, "ori", "t,r,i", treg, sreg,
4136 BFD_RELOC_LO16);
4137 macro_build (NULL, &icnt, NULL, "nor", "d,v,t", treg, treg, 0);
4139 return;
4142 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4143 macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
4144 break;
4146 case M_BEQ_I:
4147 s = "beq";
4148 goto beq_i;
4149 case M_BEQL_I:
4150 s = "beql";
4151 likely = 1;
4152 goto beq_i;
4153 case M_BNE_I:
4154 s = "bne";
4155 goto beq_i;
4156 case M_BNEL_I:
4157 s = "bnel";
4158 likely = 1;
4159 beq_i:
4160 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4162 macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, 0);
4163 return;
4165 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4166 macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4167 break;
4169 case M_BGEL:
4170 likely = 1;
4171 case M_BGE:
4172 if (treg == 0)
4174 macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4175 "s,p", sreg);
4176 return;
4178 if (sreg == 0)
4180 macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4181 "s,p", treg);
4182 return;
4184 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4185 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4186 "s,t,p", AT, 0);
4187 break;
4189 case M_BGTL_I:
4190 likely = 1;
4191 case M_BGT_I:
4192 /* check for > max integer */
4193 maxnum = 0x7fffffff;
4194 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4196 maxnum <<= 16;
4197 maxnum |= 0xffff;
4198 maxnum <<= 16;
4199 maxnum |= 0xffff;
4201 if (imm_expr.X_op == O_constant
4202 && imm_expr.X_add_number >= maxnum
4203 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4205 do_false:
4206 /* result is always false */
4207 if (! likely)
4209 if (warn_nops)
4210 as_warn (_("Branch %s is always false (nop)"),
4211 ip->insn_mo->name);
4212 macro_build (NULL, &icnt, NULL, "nop", "", 0);
4214 else
4216 if (warn_nops)
4217 as_warn (_("Branch likely %s is always false"),
4218 ip->insn_mo->name);
4219 macro_build (NULL, &icnt, &offset_expr, "bnel", "s,t,p", 0, 0);
4221 return;
4223 if (imm_expr.X_op != O_constant)
4224 as_bad (_("Unsupported large constant"));
4225 ++imm_expr.X_add_number;
4226 /* FALLTHROUGH */
4227 case M_BGE_I:
4228 case M_BGEL_I:
4229 if (mask == M_BGEL_I)
4230 likely = 1;
4231 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4233 macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4234 "s,p", sreg);
4235 return;
4237 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4239 macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4240 "s,p", sreg);
4241 return;
4243 maxnum = 0x7fffffff;
4244 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4246 maxnum <<= 16;
4247 maxnum |= 0xffff;
4248 maxnum <<= 16;
4249 maxnum |= 0xffff;
4251 maxnum = - maxnum - 1;
4252 if (imm_expr.X_op == O_constant
4253 && imm_expr.X_add_number <= maxnum
4254 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4256 do_true:
4257 /* result is always true */
4258 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4259 macro_build (NULL, &icnt, &offset_expr, "b", "p");
4260 return;
4262 set_at (&icnt, sreg, 0);
4263 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4264 "s,t,p", AT, 0);
4265 break;
4267 case M_BGEUL:
4268 likely = 1;
4269 case M_BGEU:
4270 if (treg == 0)
4271 goto do_true;
4272 if (sreg == 0)
4274 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4275 "s,t,p", 0, treg);
4276 return;
4278 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4279 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4280 "s,t,p", AT, 0);
4281 break;
4283 case M_BGTUL_I:
4284 likely = 1;
4285 case M_BGTU_I:
4286 if (sreg == 0
4287 || (HAVE_32BIT_GPRS
4288 && imm_expr.X_op == O_constant
4289 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4290 goto do_false;
4291 if (imm_expr.X_op != O_constant)
4292 as_bad (_("Unsupported large constant"));
4293 ++imm_expr.X_add_number;
4294 /* FALLTHROUGH */
4295 case M_BGEU_I:
4296 case M_BGEUL_I:
4297 if (mask == M_BGEUL_I)
4298 likely = 1;
4299 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4300 goto do_true;
4301 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4303 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4304 "s,t,p", sreg, 0);
4305 return;
4307 set_at (&icnt, sreg, 1);
4308 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4309 "s,t,p", AT, 0);
4310 break;
4312 case M_BGTL:
4313 likely = 1;
4314 case M_BGT:
4315 if (treg == 0)
4317 macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4318 "s,p", sreg);
4319 return;
4321 if (sreg == 0)
4323 macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4324 "s,p", treg);
4325 return;
4327 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4328 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4329 "s,t,p", AT, 0);
4330 break;
4332 case M_BGTUL:
4333 likely = 1;
4334 case M_BGTU:
4335 if (treg == 0)
4337 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4338 "s,t,p", sreg, 0);
4339 return;
4341 if (sreg == 0)
4342 goto do_false;
4343 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4344 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4345 "s,t,p", AT, 0);
4346 break;
4348 case M_BLEL:
4349 likely = 1;
4350 case M_BLE:
4351 if (treg == 0)
4353 macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4354 "s,p", sreg);
4355 return;
4357 if (sreg == 0)
4359 macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4360 "s,p", treg);
4361 return;
4363 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4364 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4365 "s,t,p", AT, 0);
4366 break;
4368 case M_BLEL_I:
4369 likely = 1;
4370 case M_BLE_I:
4371 maxnum = 0x7fffffff;
4372 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4374 maxnum <<= 16;
4375 maxnum |= 0xffff;
4376 maxnum <<= 16;
4377 maxnum |= 0xffff;
4379 if (imm_expr.X_op == O_constant
4380 && imm_expr.X_add_number >= maxnum
4381 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4382 goto do_true;
4383 if (imm_expr.X_op != O_constant)
4384 as_bad (_("Unsupported large constant"));
4385 ++imm_expr.X_add_number;
4386 /* FALLTHROUGH */
4387 case M_BLT_I:
4388 case M_BLTL_I:
4389 if (mask == M_BLTL_I)
4390 likely = 1;
4391 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4393 macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4394 "s,p", sreg);
4395 return;
4397 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4399 macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4400 "s,p", sreg);
4401 return;
4403 set_at (&icnt, sreg, 0);
4404 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4405 "s,t,p", AT, 0);
4406 break;
4408 case M_BLEUL:
4409 likely = 1;
4410 case M_BLEU:
4411 if (treg == 0)
4413 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4414 "s,t,p", sreg, 0);
4415 return;
4417 if (sreg == 0)
4418 goto do_true;
4419 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4420 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4421 "s,t,p", AT, 0);
4422 break;
4424 case M_BLEUL_I:
4425 likely = 1;
4426 case M_BLEU_I:
4427 if (sreg == 0
4428 || (HAVE_32BIT_GPRS
4429 && imm_expr.X_op == O_constant
4430 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4431 goto do_true;
4432 if (imm_expr.X_op != O_constant)
4433 as_bad (_("Unsupported large constant"));
4434 ++imm_expr.X_add_number;
4435 /* FALLTHROUGH */
4436 case M_BLTU_I:
4437 case M_BLTUL_I:
4438 if (mask == M_BLTUL_I)
4439 likely = 1;
4440 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4441 goto do_false;
4442 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4444 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4445 "s,t,p", sreg, 0);
4446 return;
4448 set_at (&icnt, sreg, 1);
4449 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4450 "s,t,p", AT, 0);
4451 break;
4453 case M_BLTL:
4454 likely = 1;
4455 case M_BLT:
4456 if (treg == 0)
4458 macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4459 "s,p", sreg);
4460 return;
4462 if (sreg == 0)
4464 macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4465 "s,p", treg);
4466 return;
4468 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4469 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4470 "s,t,p", AT, 0);
4471 break;
4473 case M_BLTUL:
4474 likely = 1;
4475 case M_BLTU:
4476 if (treg == 0)
4477 goto do_false;
4478 if (sreg == 0)
4480 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4481 "s,t,p", 0, treg);
4482 return;
4484 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4485 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4486 "s,t,p", AT, 0);
4487 break;
4489 case M_DEXT:
4491 unsigned long pos;
4492 unsigned long size;
4494 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4496 as_bad (_("Unsupported large constant"));
4497 pos = size = 1;
4499 else
4501 pos = (unsigned long) imm_expr.X_add_number;
4502 size = (unsigned long) imm2_expr.X_add_number;
4505 if (pos > 63)
4507 as_bad (_("Improper position (%lu)"), pos);
4508 pos = 1;
4510 if (size == 0 || size > 64
4511 || (pos + size - 1) > 63)
4513 as_bad (_("Improper extract size (%lu, position %lu)"),
4514 size, pos);
4515 size = 1;
4518 if (size <= 32 && pos < 32)
4520 s = "dext";
4521 fmt = "t,r,+A,+C";
4523 else if (size <= 32)
4525 s = "dextu";
4526 fmt = "t,r,+E,+H";
4528 else
4530 s = "dextm";
4531 fmt = "t,r,+A,+G";
4533 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4534 fmt, treg, sreg, pos, size - 1);
4536 return;
4538 case M_DINS:
4540 unsigned long pos;
4541 unsigned long size;
4543 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4545 as_bad (_("Unsupported large constant"));
4546 pos = size = 1;
4548 else
4550 pos = (unsigned long) imm_expr.X_add_number;
4551 size = (unsigned long) imm2_expr.X_add_number;
4554 if (pos > 63)
4556 as_bad (_("Improper position (%lu)"), pos);
4557 pos = 1;
4559 if (size == 0 || size > 64
4560 || (pos + size - 1) > 63)
4562 as_bad (_("Improper insert size (%lu, position %lu)"),
4563 size, pos);
4564 size = 1;
4567 if (pos < 32 && (pos + size - 1) < 32)
4569 s = "dins";
4570 fmt = "t,r,+A,+B";
4572 else if (pos >= 32)
4574 s = "dinsu";
4575 fmt = "t,r,+E,+F";
4577 else
4579 s = "dinsm";
4580 fmt = "t,r,+A,+F";
4582 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4583 fmt, treg, sreg, pos, pos + size - 1);
4585 return;
4587 case M_DDIV_3:
4588 dbl = 1;
4589 case M_DIV_3:
4590 s = "mflo";
4591 goto do_div3;
4592 case M_DREM_3:
4593 dbl = 1;
4594 case M_REM_3:
4595 s = "mfhi";
4596 do_div3:
4597 if (treg == 0)
4599 as_warn (_("Divide by zero."));
4600 if (mips_trap)
4601 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
4602 else
4603 macro_build (NULL, &icnt, NULL, "break", "c", 7);
4604 return;
4607 mips_emit_delays (TRUE);
4608 ++mips_opts.noreorder;
4609 mips_any_noreorder = 1;
4610 if (mips_trap)
4612 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4613 macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4614 sreg, treg);
4616 else
4618 expr1.X_add_number = 8;
4619 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4620 macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4621 sreg, treg);
4622 macro_build (NULL, &icnt,NULL, "break", "c", 7);
4624 expr1.X_add_number = -1;
4625 macro_build (NULL, &icnt, &expr1, dbl ? "daddiu" : "addiu", "t,r,j",
4626 AT, 0, BFD_RELOC_LO16);
4627 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4628 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4629 if (dbl)
4631 expr1.X_add_number = 1;
4632 macro_build (NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4633 BFD_RELOC_LO16);
4634 macro_build (NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT, 31);
4636 else
4638 expr1.X_add_number = 0x80000000;
4639 macro_build (NULL, &icnt, &expr1, "lui", "t,u", AT,
4640 BFD_RELOC_HI16);
4642 if (mips_trap)
4644 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", sreg, AT, 6);
4645 /* We want to close the noreorder block as soon as possible, so
4646 that later insns are available for delay slot filling. */
4647 --mips_opts.noreorder;
4649 else
4651 expr1.X_add_number = 8;
4652 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4653 macro_build (NULL, &icnt, NULL, "nop", "", 0);
4655 /* We want to close the noreorder block as soon as possible, so
4656 that later insns are available for delay slot filling. */
4657 --mips_opts.noreorder;
4659 macro_build (NULL, &icnt, NULL, "break", "c", 6);
4661 macro_build (NULL, &icnt, NULL, s, "d", dreg);
4662 break;
4664 case M_DIV_3I:
4665 s = "div";
4666 s2 = "mflo";
4667 goto do_divi;
4668 case M_DIVU_3I:
4669 s = "divu";
4670 s2 = "mflo";
4671 goto do_divi;
4672 case M_REM_3I:
4673 s = "div";
4674 s2 = "mfhi";
4675 goto do_divi;
4676 case M_REMU_3I:
4677 s = "divu";
4678 s2 = "mfhi";
4679 goto do_divi;
4680 case M_DDIV_3I:
4681 dbl = 1;
4682 s = "ddiv";
4683 s2 = "mflo";
4684 goto do_divi;
4685 case M_DDIVU_3I:
4686 dbl = 1;
4687 s = "ddivu";
4688 s2 = "mflo";
4689 goto do_divi;
4690 case M_DREM_3I:
4691 dbl = 1;
4692 s = "ddiv";
4693 s2 = "mfhi";
4694 goto do_divi;
4695 case M_DREMU_3I:
4696 dbl = 1;
4697 s = "ddivu";
4698 s2 = "mfhi";
4699 do_divi:
4700 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4702 as_warn (_("Divide by zero."));
4703 if (mips_trap)
4704 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
4705 else
4706 macro_build (NULL, &icnt, NULL, "break", "c", 7);
4707 return;
4709 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4711 if (strcmp (s2, "mflo") == 0)
4712 move_register (&icnt, dreg, sreg);
4713 else
4714 move_register (&icnt, dreg, 0);
4715 return;
4717 if (imm_expr.X_op == O_constant
4718 && imm_expr.X_add_number == -1
4719 && s[strlen (s) - 1] != 'u')
4721 if (strcmp (s2, "mflo") == 0)
4723 macro_build (NULL, &icnt, NULL, dbl ? "dneg" : "neg", "d,w",
4724 dreg, sreg);
4726 else
4727 move_register (&icnt, dreg, 0);
4728 return;
4731 load_register (&icnt, AT, &imm_expr, dbl);
4732 macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
4733 macro_build (NULL, &icnt, NULL, s2, "d", dreg);
4734 break;
4736 case M_DIVU_3:
4737 s = "divu";
4738 s2 = "mflo";
4739 goto do_divu3;
4740 case M_REMU_3:
4741 s = "divu";
4742 s2 = "mfhi";
4743 goto do_divu3;
4744 case M_DDIVU_3:
4745 s = "ddivu";
4746 s2 = "mflo";
4747 goto do_divu3;
4748 case M_DREMU_3:
4749 s = "ddivu";
4750 s2 = "mfhi";
4751 do_divu3:
4752 mips_emit_delays (TRUE);
4753 ++mips_opts.noreorder;
4754 mips_any_noreorder = 1;
4755 if (mips_trap)
4757 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4758 macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4759 /* We want to close the noreorder block as soon as possible, so
4760 that later insns are available for delay slot filling. */
4761 --mips_opts.noreorder;
4763 else
4765 expr1.X_add_number = 8;
4766 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4767 macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4769 /* We want to close the noreorder block as soon as possible, so
4770 that later insns are available for delay slot filling. */
4771 --mips_opts.noreorder;
4772 macro_build (NULL, &icnt, NULL, "break", "c", 7);
4774 macro_build (NULL, &icnt, NULL, s2, "d", dreg);
4775 return;
4777 case M_DLA_AB:
4778 dbl = 1;
4779 case M_LA_AB:
4780 /* Load the address of a symbol into a register. If breg is not
4781 zero, we then add a base register to it. */
4783 if (dbl && HAVE_32BIT_GPRS)
4784 as_warn (_("dla used to load 32-bit register"));
4786 if (! dbl && HAVE_64BIT_OBJECTS)
4787 as_warn (_("la used to load 64-bit address"));
4789 if (offset_expr.X_op == O_constant
4790 && offset_expr.X_add_number >= -0x8000
4791 && offset_expr.X_add_number < 0x8000)
4793 macro_build (NULL, &icnt, &offset_expr,
4794 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4795 "t,r,j", treg, sreg, BFD_RELOC_LO16);
4796 return;
4799 if (treg == breg)
4801 tempreg = AT;
4802 used_at = 1;
4804 else
4806 tempreg = treg;
4807 used_at = 0;
4810 /* When generating embedded PIC code, we permit expressions of
4811 the form
4812 la $treg,foo-bar
4813 la $treg,foo-bar($breg)
4814 where bar is an address in the current section. These are used
4815 when getting the addresses of functions. We don't permit
4816 X_add_number to be non-zero, because if the symbol is
4817 external the relaxing code needs to know that any addend is
4818 purely the offset to X_op_symbol. */
4819 if (mips_pic == EMBEDDED_PIC
4820 && offset_expr.X_op == O_subtract
4821 && (symbol_constant_p (offset_expr.X_op_symbol)
4822 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4823 : (symbol_equated_p (offset_expr.X_op_symbol)
4824 && (S_GET_SEGMENT
4825 (symbol_get_value_expression (offset_expr.X_op_symbol)
4826 ->X_add_symbol)
4827 == now_seg)))
4828 && (offset_expr.X_add_number == 0
4829 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4831 if (breg == 0)
4833 tempreg = treg;
4834 used_at = 0;
4835 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4836 BFD_RELOC_PCREL_HI16_S);
4838 else
4840 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4841 BFD_RELOC_PCREL_HI16_S);
4842 macro_build (NULL, &icnt, NULL,
4843 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4844 "d,v,t", tempreg, tempreg, breg);
4846 macro_build (NULL, &icnt, &offset_expr,
4847 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4848 "t,r,j", treg, tempreg, BFD_RELOC_PCREL_LO16);
4849 if (! used_at)
4850 return;
4851 break;
4854 if (offset_expr.X_op != O_symbol
4855 && offset_expr.X_op != O_constant)
4857 as_bad (_("expression too complex"));
4858 offset_expr.X_op = O_constant;
4861 if (offset_expr.X_op == O_constant)
4862 load_register (&icnt, tempreg, &offset_expr,
4863 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4864 ? (dbl || HAVE_64BIT_ADDRESSES)
4865 : HAVE_64BIT_ADDRESSES));
4866 else if (mips_pic == NO_PIC)
4868 /* If this is a reference to a GP relative symbol, we want
4869 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4870 Otherwise we want
4871 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4872 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4873 If we have a constant, we need two instructions anyhow,
4874 so we may as well always use the latter form.
4876 With 64bit address space and a usable $at we want
4877 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4878 lui $at,<sym> (BFD_RELOC_HI16_S)
4879 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4880 daddiu $at,<sym> (BFD_RELOC_LO16)
4881 dsll32 $tempreg,0
4882 daddu $tempreg,$tempreg,$at
4884 If $at is already in use, we use a path which is suboptimal
4885 on superscalar processors.
4886 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4887 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4888 dsll $tempreg,16
4889 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4890 dsll $tempreg,16
4891 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4893 char *p = NULL;
4894 if (HAVE_64BIT_ADDRESSES)
4896 /* We don't do GP optimization for now because RELAX_ENCODE can't
4897 hold the data for such large chunks. */
4899 if (used_at == 0 && ! mips_opts.noat)
4901 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4902 tempreg, BFD_RELOC_MIPS_HIGHEST);
4903 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4904 AT, BFD_RELOC_HI16_S);
4905 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4906 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4907 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4908 AT, AT, BFD_RELOC_LO16);
4909 macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
4910 tempreg, tempreg, 0);
4911 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
4912 tempreg, tempreg, AT);
4913 used_at = 1;
4915 else
4917 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4918 tempreg, BFD_RELOC_MIPS_HIGHEST);
4919 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4920 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4921 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
4922 tempreg, tempreg, 16);
4923 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4924 tempreg, tempreg, BFD_RELOC_HI16_S);
4925 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
4926 tempreg, tempreg, 16);
4927 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4928 tempreg, tempreg, BFD_RELOC_LO16);
4931 else
4933 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4934 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4936 frag_grow (20);
4937 macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
4938 "t,r,j", tempreg, mips_gp_register,
4939 BFD_RELOC_GPREL16);
4940 p = frag_var (rs_machine_dependent, 8, 0,
4941 RELAX_ENCODE (4, 8, 0, 4, 0,
4942 mips_opts.warn_about_macros),
4943 offset_expr.X_add_symbol, 0, NULL);
4945 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4946 if (p != NULL)
4947 p += 4;
4948 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
4949 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
4952 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
4954 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4956 /* If this is a reference to an external symbol, and there
4957 is no constant, we want
4958 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4959 or if tempreg is PIC_CALL_REG
4960 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4961 For a local symbol, we want
4962 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4964 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4966 If we have a small constant, and this is a reference to
4967 an external symbol, we want
4968 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4970 addiu $tempreg,$tempreg,<constant>
4971 For a local symbol, we want the same instruction
4972 sequence, but we output a BFD_RELOC_LO16 reloc on the
4973 addiu instruction.
4975 If we have a large constant, and this is a reference to
4976 an external symbol, we want
4977 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4978 lui $at,<hiconstant>
4979 addiu $at,$at,<loconstant>
4980 addu $tempreg,$tempreg,$at
4981 For a local symbol, we want the same instruction
4982 sequence, but we output a BFD_RELOC_LO16 reloc on the
4983 addiu instruction.
4986 expr1.X_add_number = offset_expr.X_add_number;
4987 offset_expr.X_add_number = 0;
4988 frag_grow (32);
4989 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4990 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4991 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
4992 tempreg, lw_reloc_type, mips_gp_register);
4993 if (expr1.X_add_number == 0)
4995 int off;
4996 char *p;
4998 if (breg == 0)
4999 off = 0;
5000 else
5002 /* We're going to put in an addu instruction using
5003 tempreg, so we may as well insert the nop right
5004 now. */
5005 macro_build (NULL, &icnt, NULL, "nop", "");
5006 off = 4;
5008 p = frag_var (rs_machine_dependent, 8 - off, 0,
5009 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5010 (breg == 0
5011 ? mips_opts.warn_about_macros
5012 : 0)),
5013 offset_expr.X_add_symbol, 0, NULL);
5014 if (breg == 0)
5016 macro_build (p, &icnt, NULL, "nop", "");
5017 p += 4;
5019 macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
5020 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
5021 /* FIXME: If breg == 0, and the next instruction uses
5022 $tempreg, then if this variant case is used an extra
5023 nop will be generated. */
5025 else if (expr1.X_add_number >= -0x8000
5026 && expr1.X_add_number < 0x8000)
5028 macro_build (NULL, &icnt, NULL, "nop", "");
5029 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5030 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
5031 frag_var (rs_machine_dependent, 0, 0,
5032 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5033 offset_expr.X_add_symbol, 0, NULL);
5035 else
5037 int off1;
5039 /* If we are going to add in a base register, and the
5040 target register and the base register are the same,
5041 then we are using AT as a temporary register. Since
5042 we want to load the constant into AT, we add our
5043 current AT (from the global offset table) and the
5044 register into the register now, and pretend we were
5045 not using a base register. */
5046 if (breg != treg)
5047 off1 = 0;
5048 else
5050 macro_build (NULL, &icnt, NULL, "nop", "");
5051 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5052 treg, AT, breg);
5053 breg = 0;
5054 tempreg = treg;
5055 off1 = -8;
5058 /* Set mips_optimize around the lui instruction to avoid
5059 inserting an unnecessary nop after the lw. */
5060 hold_mips_optimize = mips_optimize;
5061 mips_optimize = 2;
5062 macro_build_lui (NULL, &icnt, &expr1, AT);
5063 mips_optimize = hold_mips_optimize;
5065 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5066 AT, AT, BFD_RELOC_LO16);
5067 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5068 tempreg, tempreg, AT);
5069 frag_var (rs_machine_dependent, 0, 0,
5070 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5071 offset_expr.X_add_symbol, 0, NULL);
5072 used_at = 1;
5075 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5077 char *p = NULL;
5078 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5079 int adj = 0;
5081 /* If this is a reference to an external, and there is no
5082 constant, or local symbol (*), with or without a
5083 constant, we want
5084 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5085 or if tempreg is PIC_CALL_REG
5086 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5088 If we have a small constant, and this is a reference to
5089 an external symbol, we want
5090 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5091 addiu $tempreg,$tempreg,<constant>
5093 If we have a large constant, and this is a reference to
5094 an external symbol, we want
5095 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5096 lui $at,<hiconstant>
5097 addiu $at,$at,<loconstant>
5098 addu $tempreg,$tempreg,$at
5100 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5101 local symbols, even though it introduces an additional
5102 instruction. */
5104 frag_grow (28);
5105 if (offset_expr.X_add_number == 0 && tempreg == PIC_CALL_REG)
5106 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5107 if (offset_expr.X_add_number)
5109 frag_now->tc_frag_data.tc_fr_offset =
5110 expr1.X_add_number = offset_expr.X_add_number;
5111 offset_expr.X_add_number = 0;
5113 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5114 "t,o(b)", tempreg, lw_reloc_type,
5115 mips_gp_register);
5117 if (expr1.X_add_number >= -0x8000
5118 && expr1.X_add_number < 0x8000)
5120 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5121 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
5122 p = frag_var (rs_machine_dependent, 4, 0,
5123 RELAX_ENCODE (8, 4, 0, 0, 0, 0),
5124 offset_expr.X_add_symbol, 0, NULL);
5126 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5128 int dreg;
5130 /* If we are going to add in a base register, and the
5131 target register and the base register are the same,
5132 then we are using AT as a temporary register. Since
5133 we want to load the constant into AT, we add our
5134 current AT (from the global offset table) and the
5135 register into the register now, and pretend we were
5136 not using a base register. */
5137 if (breg != treg)
5138 dreg = tempreg;
5139 else
5141 assert (tempreg == AT);
5142 macro_build (NULL, &icnt,NULL, ADDRESS_ADD_INSN,
5143 "d,v,t", treg, AT, breg);
5144 dreg = treg;
5145 adj = 4;
5148 macro_build_lui (NULL, &icnt, &expr1, AT);
5149 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5150 "t,r,j", AT, AT, BFD_RELOC_LO16);
5151 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5152 dreg, dreg, AT);
5154 p = frag_var (rs_machine_dependent, 4 + adj, 0,
5155 RELAX_ENCODE (16 + adj, 4 + adj,
5156 0, 0, 0, 0),
5157 offset_expr.X_add_symbol, 0, NULL);
5159 used_at = 1;
5161 else
5162 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5164 offset_expr.X_add_number = expr1.X_add_number;
5166 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5167 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_DISP,
5168 mips_gp_register);
5169 if (adj)
5171 macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5172 treg, tempreg, breg);
5173 breg = 0;
5174 tempreg = treg;
5177 else
5179 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5180 "t,o(b)", tempreg, lw_reloc_type,
5181 mips_gp_register);
5182 if (lw_reloc_type != BFD_RELOC_MIPS_GOT_DISP)
5183 p = frag_var (rs_machine_dependent, 0, 0,
5184 RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5185 offset_expr.X_add_symbol, 0, NULL);
5188 if (! p)
5190 /* To avoid confusion in tc_gen_reloc, we must ensure
5191 that this does not become a variant frag. */
5192 frag_wane (frag_now);
5193 frag_new (0);
5196 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5198 int gpdel;
5199 char *p;
5200 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5201 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5202 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5204 /* This is the large GOT case. If this is a reference to an
5205 external symbol, and there is no constant, we want
5206 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5207 addu $tempreg,$tempreg,$gp
5208 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5209 or if tempreg is PIC_CALL_REG
5210 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5211 addu $tempreg,$tempreg,$gp
5212 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5213 For a local symbol, we want
5214 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5216 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5218 If we have a small constant, and this is a reference to
5219 an external symbol, we want
5220 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5221 addu $tempreg,$tempreg,$gp
5222 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5224 addiu $tempreg,$tempreg,<constant>
5225 For a local symbol, we want
5226 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5228 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5230 If we have a large constant, and this is a reference to
5231 an external symbol, we want
5232 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5233 addu $tempreg,$tempreg,$gp
5234 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5235 lui $at,<hiconstant>
5236 addiu $at,$at,<loconstant>
5237 addu $tempreg,$tempreg,$at
5238 For a local symbol, we want
5239 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5240 lui $at,<hiconstant>
5241 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5242 addu $tempreg,$tempreg,$at
5245 expr1.X_add_number = offset_expr.X_add_number;
5246 offset_expr.X_add_number = 0;
5247 frag_grow (52);
5248 if (reg_needs_delay (mips_gp_register))
5249 gpdel = 4;
5250 else
5251 gpdel = 0;
5252 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5254 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5255 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5257 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5258 tempreg, lui_reloc_type);
5259 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5260 tempreg, tempreg, mips_gp_register);
5261 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5262 tempreg, lw_reloc_type, tempreg);
5263 if (expr1.X_add_number == 0)
5265 int off;
5267 if (breg == 0)
5268 off = 0;
5269 else
5271 /* We're going to put in an addu instruction using
5272 tempreg, so we may as well insert the nop right
5273 now. */
5274 macro_build (NULL, &icnt, NULL, "nop", "");
5275 off = 4;
5278 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5279 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5280 8 + gpdel, 0,
5281 (breg == 0
5282 ? mips_opts.warn_about_macros
5283 : 0)),
5284 offset_expr.X_add_symbol, 0, NULL);
5286 else if (expr1.X_add_number >= -0x8000
5287 && expr1.X_add_number < 0x8000)
5289 macro_build (NULL, &icnt, NULL, "nop", "");
5290 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5291 tempreg, tempreg, BFD_RELOC_LO16);
5293 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5294 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5295 (breg == 0
5296 ? mips_opts.warn_about_macros
5297 : 0)),
5298 offset_expr.X_add_symbol, 0, NULL);
5300 else
5302 int adj, dreg;
5304 /* If we are going to add in a base register, and the
5305 target register and the base register are the same,
5306 then we are using AT as a temporary register. Since
5307 we want to load the constant into AT, we add our
5308 current AT (from the global offset table) and the
5309 register into the register now, and pretend we were
5310 not using a base register. */
5311 if (breg != treg)
5313 adj = 0;
5314 dreg = tempreg;
5316 else
5318 assert (tempreg == AT);
5319 macro_build (NULL, &icnt, NULL, "nop", "");
5320 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5321 treg, AT, breg);
5322 dreg = treg;
5323 adj = 8;
5326 /* Set mips_optimize around the lui instruction to avoid
5327 inserting an unnecessary nop after the lw. */
5328 hold_mips_optimize = mips_optimize;
5329 mips_optimize = 2;
5330 macro_build_lui (NULL, &icnt, &expr1, AT);
5331 mips_optimize = hold_mips_optimize;
5333 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5334 AT, AT, BFD_RELOC_LO16);
5335 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5336 dreg, dreg, AT);
5338 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5339 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5340 8 + gpdel, 0,
5341 (breg == 0
5342 ? mips_opts.warn_about_macros
5343 : 0)),
5344 offset_expr.X_add_symbol, 0, NULL);
5346 used_at = 1;
5349 if (gpdel > 0)
5351 /* This is needed because this instruction uses $gp, but
5352 the first instruction on the main stream does not. */
5353 macro_build (p, &icnt, NULL, "nop", "");
5354 p += 4;
5357 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5358 tempreg, local_reloc_type, mips_gp_register);
5359 p += 4;
5360 if (expr1.X_add_number >= -0x8000
5361 && expr1.X_add_number < 0x8000)
5363 macro_build (p, &icnt, NULL, "nop", "");
5364 p += 4;
5365 macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
5366 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
5367 /* FIXME: If add_number is 0, and there was no base
5368 register, the external symbol case ended with a load,
5369 so if the symbol turns out to not be external, and
5370 the next instruction uses tempreg, an unnecessary nop
5371 will be inserted. */
5373 else
5375 if (breg == treg)
5377 /* We must add in the base register now, as in the
5378 external symbol case. */
5379 assert (tempreg == AT);
5380 macro_build (p, &icnt, NULL, "nop", "");
5381 p += 4;
5382 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5383 treg, AT, breg);
5384 p += 4;
5385 tempreg = treg;
5386 /* We set breg to 0 because we have arranged to add
5387 it in in both cases. */
5388 breg = 0;
5391 macro_build_lui (p, &icnt, &expr1, AT);
5392 p += 4;
5393 macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5394 AT, AT, BFD_RELOC_LO16);
5395 p += 4;
5396 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5397 tempreg, tempreg, AT);
5398 p += 4;
5401 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5403 char *p = NULL;
5404 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5405 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5406 int adj = 0;
5408 /* This is the large GOT case. If this is a reference to an
5409 external symbol, and there is no constant, we want
5410 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5411 add $tempreg,$tempreg,$gp
5412 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5413 or if tempreg is PIC_CALL_REG
5414 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5415 add $tempreg,$tempreg,$gp
5416 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5418 If we have a small constant, and this is a reference to
5419 an external symbol, we want
5420 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5421 add $tempreg,$tempreg,$gp
5422 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5423 addi $tempreg,$tempreg,<constant>
5425 If we have a large constant, and this is a reference to
5426 an external symbol, we want
5427 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5428 addu $tempreg,$tempreg,$gp
5429 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5430 lui $at,<hiconstant>
5431 addi $at,$at,<loconstant>
5432 add $tempreg,$tempreg,$at
5434 If we have NewABI, and we know it's a local symbol, we want
5435 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5436 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5437 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5439 frag_grow (40);
5441 frag_now->tc_frag_data.tc_fr_offset =
5442 expr1.X_add_number = offset_expr.X_add_number;
5443 offset_expr.X_add_number = 0;
5445 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5447 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5448 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5450 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5451 tempreg, lui_reloc_type);
5452 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5453 tempreg, tempreg, mips_gp_register);
5454 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5455 "t,o(b)", tempreg, lw_reloc_type, tempreg);
5457 if (expr1.X_add_number == 0)
5459 p = frag_var (rs_machine_dependent, 8, 0,
5460 RELAX_ENCODE (12, 8, 0, 4, 0,
5461 mips_opts.warn_about_macros),
5462 offset_expr.X_add_symbol, 0, NULL);
5464 else if (expr1.X_add_number >= -0x8000
5465 && expr1.X_add_number < 0x8000)
5467 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5468 tempreg, tempreg, BFD_RELOC_LO16);
5469 p = frag_var (rs_machine_dependent, 8, 0,
5470 RELAX_ENCODE (16, 8, 0, 4, 0,
5471 mips_opts.warn_about_macros),
5472 offset_expr.X_add_symbol, 0, NULL);
5474 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5476 int dreg;
5478 /* If we are going to add in a base register, and the
5479 target register and the base register are the same,
5480 then we are using AT as a temporary register. Since
5481 we want to load the constant into AT, we add our
5482 current AT (from the global offset table) and the
5483 register into the register now, and pretend we were
5484 not using a base register. */
5485 if (breg != treg)
5486 dreg = tempreg;
5487 else
5489 assert (tempreg == AT);
5490 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5491 treg, AT, breg);
5492 dreg = treg;
5493 adj = 4;
5496 /* Set mips_optimize around the lui instruction to avoid
5497 inserting an unnecessary nop after the lw. */
5498 macro_build_lui (NULL, &icnt, &expr1, AT);
5499 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5500 "t,r,j", AT, AT, BFD_RELOC_LO16);
5501 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5502 dreg, dreg, AT);
5504 p = frag_var (rs_machine_dependent, 8 + adj, 0,
5505 RELAX_ENCODE (24 + adj, 8 + adj,
5506 0, 4, 0,
5507 (breg == 0
5508 ? mips_opts.warn_about_macros
5509 : 0)),
5510 offset_expr.X_add_symbol, 0, NULL);
5512 used_at = 1;
5514 else
5515 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5517 offset_expr.X_add_number = expr1.X_add_number;
5518 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5519 tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5520 macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5521 tempreg, tempreg, BFD_RELOC_MIPS_GOT_OFST);
5522 if (adj)
5524 macro_build (p + 8, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5525 treg, tempreg, breg);
5526 breg = 0;
5527 tempreg = treg;
5530 else if (mips_pic == EMBEDDED_PIC)
5532 /* We use
5533 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
5535 macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5536 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5538 else
5539 abort ();
5541 if (breg != 0)
5543 char *s;
5545 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5546 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5547 else
5548 s = ADDRESS_ADD_INSN;
5550 macro_build (NULL, &icnt, NULL, s, "d,v,t", treg, tempreg, breg);
5553 if (! used_at)
5554 return;
5556 break;
5558 case M_J_A:
5559 /* The j instruction may not be used in PIC code, since it
5560 requires an absolute address. We convert it to a b
5561 instruction. */
5562 if (mips_pic == NO_PIC)
5563 macro_build (NULL, &icnt, &offset_expr, "j", "a");
5564 else
5565 macro_build (NULL, &icnt, &offset_expr, "b", "p");
5566 return;
5568 /* The jal instructions must be handled as macros because when
5569 generating PIC code they expand to multi-instruction
5570 sequences. Normally they are simple instructions. */
5571 case M_JAL_1:
5572 dreg = RA;
5573 /* Fall through. */
5574 case M_JAL_2:
5575 if (mips_pic == NO_PIC
5576 || mips_pic == EMBEDDED_PIC)
5577 macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
5578 else if (mips_pic == SVR4_PIC)
5580 if (sreg != PIC_CALL_REG)
5581 as_warn (_("MIPS PIC call to register other than $25"));
5583 macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
5584 if (! HAVE_NEWABI)
5586 if (mips_cprestore_offset < 0)
5587 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5588 else
5590 if (! mips_frame_reg_valid)
5592 as_warn (_("No .frame pseudo-op used in PIC code"));
5593 /* Quiet this warning. */
5594 mips_frame_reg_valid = 1;
5596 if (! mips_cprestore_valid)
5598 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5599 /* Quiet this warning. */
5600 mips_cprestore_valid = 1;
5602 expr1.X_add_number = mips_cprestore_offset;
5603 macro_build_ldst_constoffset (NULL, &icnt, &expr1,
5604 ADDRESS_LOAD_INSN,
5605 mips_gp_register,
5606 mips_frame_reg,
5607 HAVE_64BIT_ADDRESSES);
5611 else
5612 abort ();
5614 return;
5616 case M_JAL_A:
5617 if (mips_pic == NO_PIC)
5618 macro_build (NULL, &icnt, &offset_expr, "jal", "a");
5619 else if (mips_pic == SVR4_PIC)
5621 char *p;
5623 /* If this is a reference to an external symbol, and we are
5624 using a small GOT, we want
5625 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5627 jalr $ra,$25
5629 lw $gp,cprestore($sp)
5630 The cprestore value is set using the .cprestore
5631 pseudo-op. If we are using a big GOT, we want
5632 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5633 addu $25,$25,$gp
5634 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5636 jalr $ra,$25
5638 lw $gp,cprestore($sp)
5639 If the symbol is not external, we want
5640 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5642 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5643 jalr $ra,$25
5645 lw $gp,cprestore($sp)
5647 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5648 sequences above, minus nops, unless the symbol is local,
5649 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5650 GOT_DISP. */
5651 if (HAVE_NEWABI)
5653 if (! mips_big_got)
5655 frag_grow (4);
5656 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5657 "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5658 mips_gp_register);
5659 frag_var (rs_machine_dependent, 0, 0,
5660 RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5661 offset_expr.X_add_symbol, 0, NULL);
5663 else
5665 frag_grow (20);
5666 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5667 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5668 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5669 PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5670 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5671 "t,o(b)", PIC_CALL_REG,
5672 BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5673 p = frag_var (rs_machine_dependent, 8, 0,
5674 RELAX_ENCODE (12, 8, 0, 4, 0, 0),
5675 offset_expr.X_add_symbol, 0, NULL);
5676 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5677 "t,o(b)", PIC_CALL_REG,
5678 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5679 macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
5680 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5681 BFD_RELOC_MIPS_GOT_OFST);
5684 macro_build_jalr (icnt, &offset_expr);
5686 else
5688 frag_grow (40);
5689 if (! mips_big_got)
5691 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5692 "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5693 mips_gp_register);
5694 macro_build (NULL, &icnt, NULL, "nop", "");
5695 p = frag_var (rs_machine_dependent, 4, 0,
5696 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5697 offset_expr.X_add_symbol, 0, NULL);
5699 else
5701 int gpdel;
5703 if (reg_needs_delay (mips_gp_register))
5704 gpdel = 4;
5705 else
5706 gpdel = 0;
5707 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5708 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5709 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5710 PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5711 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5712 "t,o(b)", PIC_CALL_REG,
5713 BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5714 macro_build (NULL, &icnt, NULL, "nop", "");
5715 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5716 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5717 8 + gpdel, 0, 0),
5718 offset_expr.X_add_symbol, 0, NULL);
5719 if (gpdel > 0)
5721 macro_build (p, &icnt, NULL, "nop", "");
5722 p += 4;
5724 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5725 "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5726 mips_gp_register);
5727 p += 4;
5728 macro_build (p, &icnt, NULL, "nop", "");
5729 p += 4;
5731 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
5732 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5733 BFD_RELOC_LO16);
5734 macro_build_jalr (icnt, &offset_expr);
5736 if (mips_cprestore_offset < 0)
5737 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5738 else
5740 if (! mips_frame_reg_valid)
5742 as_warn (_("No .frame pseudo-op used in PIC code"));
5743 /* Quiet this warning. */
5744 mips_frame_reg_valid = 1;
5746 if (! mips_cprestore_valid)
5748 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5749 /* Quiet this warning. */
5750 mips_cprestore_valid = 1;
5752 if (mips_opts.noreorder)
5753 macro_build (NULL, &icnt, NULL, "nop", "");
5754 expr1.X_add_number = mips_cprestore_offset;
5755 macro_build_ldst_constoffset (NULL, &icnt, &expr1,
5756 ADDRESS_LOAD_INSN,
5757 mips_gp_register,
5758 mips_frame_reg,
5759 HAVE_64BIT_ADDRESSES);
5763 else if (mips_pic == EMBEDDED_PIC)
5765 macro_build (NULL, &icnt, &offset_expr, "bal", "p");
5766 /* The linker may expand the call to a longer sequence which
5767 uses $at, so we must break rather than return. */
5768 break;
5770 else
5771 abort ();
5773 return;
5775 case M_LB_AB:
5776 s = "lb";
5777 goto ld;
5778 case M_LBU_AB:
5779 s = "lbu";
5780 goto ld;
5781 case M_LH_AB:
5782 s = "lh";
5783 goto ld;
5784 case M_LHU_AB:
5785 s = "lhu";
5786 goto ld;
5787 case M_LW_AB:
5788 s = "lw";
5789 goto ld;
5790 case M_LWC0_AB:
5791 s = "lwc0";
5792 /* Itbl support may require additional care here. */
5793 coproc = 1;
5794 goto ld;
5795 case M_LWC1_AB:
5796 s = "lwc1";
5797 /* Itbl support may require additional care here. */
5798 coproc = 1;
5799 goto ld;
5800 case M_LWC2_AB:
5801 s = "lwc2";
5802 /* Itbl support may require additional care here. */
5803 coproc = 1;
5804 goto ld;
5805 case M_LWC3_AB:
5806 s = "lwc3";
5807 /* Itbl support may require additional care here. */
5808 coproc = 1;
5809 goto ld;
5810 case M_LWL_AB:
5811 s = "lwl";
5812 lr = 1;
5813 goto ld;
5814 case M_LWR_AB:
5815 s = "lwr";
5816 lr = 1;
5817 goto ld;
5818 case M_LDC1_AB:
5819 if (mips_opts.arch == CPU_R4650)
5821 as_bad (_("opcode not supported on this processor"));
5822 return;
5824 s = "ldc1";
5825 /* Itbl support may require additional care here. */
5826 coproc = 1;
5827 goto ld;
5828 case M_LDC2_AB:
5829 s = "ldc2";
5830 /* Itbl support may require additional care here. */
5831 coproc = 1;
5832 goto ld;
5833 case M_LDC3_AB:
5834 s = "ldc3";
5835 /* Itbl support may require additional care here. */
5836 coproc = 1;
5837 goto ld;
5838 case M_LDL_AB:
5839 s = "ldl";
5840 lr = 1;
5841 goto ld;
5842 case M_LDR_AB:
5843 s = "ldr";
5844 lr = 1;
5845 goto ld;
5846 case M_LL_AB:
5847 s = "ll";
5848 goto ld;
5849 case M_LLD_AB:
5850 s = "lld";
5851 goto ld;
5852 case M_LWU_AB:
5853 s = "lwu";
5855 if (breg == treg || coproc || lr)
5857 tempreg = AT;
5858 used_at = 1;
5860 else
5862 tempreg = treg;
5863 used_at = 0;
5865 goto ld_st;
5866 case M_SB_AB:
5867 s = "sb";
5868 goto st;
5869 case M_SH_AB:
5870 s = "sh";
5871 goto st;
5872 case M_SW_AB:
5873 s = "sw";
5874 goto st;
5875 case M_SWC0_AB:
5876 s = "swc0";
5877 /* Itbl support may require additional care here. */
5878 coproc = 1;
5879 goto st;
5880 case M_SWC1_AB:
5881 s = "swc1";
5882 /* Itbl support may require additional care here. */
5883 coproc = 1;
5884 goto st;
5885 case M_SWC2_AB:
5886 s = "swc2";
5887 /* Itbl support may require additional care here. */
5888 coproc = 1;
5889 goto st;
5890 case M_SWC3_AB:
5891 s = "swc3";
5892 /* Itbl support may require additional care here. */
5893 coproc = 1;
5894 goto st;
5895 case M_SWL_AB:
5896 s = "swl";
5897 goto st;
5898 case M_SWR_AB:
5899 s = "swr";
5900 goto st;
5901 case M_SC_AB:
5902 s = "sc";
5903 goto st;
5904 case M_SCD_AB:
5905 s = "scd";
5906 goto st;
5907 case M_SDC1_AB:
5908 if (mips_opts.arch == CPU_R4650)
5910 as_bad (_("opcode not supported on this processor"));
5911 return;
5913 s = "sdc1";
5914 coproc = 1;
5915 /* Itbl support may require additional care here. */
5916 goto st;
5917 case M_SDC2_AB:
5918 s = "sdc2";
5919 /* Itbl support may require additional care here. */
5920 coproc = 1;
5921 goto st;
5922 case M_SDC3_AB:
5923 s = "sdc3";
5924 /* Itbl support may require additional care here. */
5925 coproc = 1;
5926 goto st;
5927 case M_SDL_AB:
5928 s = "sdl";
5929 goto st;
5930 case M_SDR_AB:
5931 s = "sdr";
5933 tempreg = AT;
5934 used_at = 1;
5935 ld_st:
5936 /* Itbl support may require additional care here. */
5937 if (mask == M_LWC1_AB
5938 || mask == M_SWC1_AB
5939 || mask == M_LDC1_AB
5940 || mask == M_SDC1_AB
5941 || mask == M_L_DAB
5942 || mask == M_S_DAB)
5943 fmt = "T,o(b)";
5944 else if (coproc)
5945 fmt = "E,o(b)";
5946 else
5947 fmt = "t,o(b)";
5949 /* Sign-extending 32-bit constants makes their handling easier.
5950 The HAVE_64BIT_GPRS... part is due to the linux kernel hack
5951 described below. */
5952 if ((! HAVE_64BIT_ADDRESSES
5953 && (! HAVE_64BIT_GPRS && offset_expr.X_op == O_constant))
5954 && (offset_expr.X_op == O_constant)
5955 && ! ((offset_expr.X_add_number & ~((bfd_vma) 0x7fffffff))
5956 == ~((bfd_vma) 0x7fffffff)))
5958 if (offset_expr.X_add_number & ~((bfd_vma) 0xffffffff))
5959 as_bad (_("constant too large"));
5961 offset_expr.X_add_number = (((offset_expr.X_add_number & 0xffffffff)
5962 ^ 0x80000000) - 0x80000000);
5965 /* For embedded PIC, we allow loads where the offset is calculated
5966 by subtracting a symbol in the current segment from an unknown
5967 symbol, relative to a base register, e.g.:
5968 <op> $treg, <sym>-<localsym>($breg)
5969 This is used by the compiler for switch statements. */
5970 if (mips_pic == EMBEDDED_PIC
5971 && offset_expr.X_op == O_subtract
5972 && (symbol_constant_p (offset_expr.X_op_symbol)
5973 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5974 : (symbol_equated_p (offset_expr.X_op_symbol)
5975 && (S_GET_SEGMENT
5976 (symbol_get_value_expression (offset_expr.X_op_symbol)
5977 ->X_add_symbol)
5978 == now_seg)))
5979 && breg != 0
5980 && (offset_expr.X_add_number == 0
5981 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5983 /* For this case, we output the instructions:
5984 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5985 addiu $tempreg,$tempreg,$breg
5986 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5987 If the relocation would fit entirely in 16 bits, it would be
5988 nice to emit:
5989 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5990 instead, but that seems quite difficult. */
5991 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
5992 BFD_RELOC_PCREL_HI16_S);
5993 macro_build (NULL, &icnt, NULL,
5994 ((bfd_arch_bits_per_address (stdoutput) == 32
5995 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5996 ? "addu" : "daddu"),
5997 "d,v,t", tempreg, tempreg, breg);
5998 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
5999 BFD_RELOC_PCREL_LO16, tempreg);
6000 if (! used_at)
6001 return;
6002 break;
6005 if (offset_expr.X_op != O_constant
6006 && offset_expr.X_op != O_symbol)
6008 as_bad (_("expression too complex"));
6009 offset_expr.X_op = O_constant;
6012 /* A constant expression in PIC code can be handled just as it
6013 is in non PIC code. */
6014 if (mips_pic == NO_PIC
6015 || offset_expr.X_op == O_constant)
6017 char *p;
6019 /* If this is a reference to a GP relative symbol, and there
6020 is no base register, we want
6021 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6022 Otherwise, if there is no base register, we want
6023 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6024 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6025 If we have a constant, we need two instructions anyhow,
6026 so we always use the latter form.
6028 If we have a base register, and this is a reference to a
6029 GP relative symbol, we want
6030 addu $tempreg,$breg,$gp
6031 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
6032 Otherwise we want
6033 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6034 addu $tempreg,$tempreg,$breg
6035 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6036 With a constant we always use the latter case.
6038 With 64bit address space and no base register and $at usable,
6039 we want
6040 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6041 lui $at,<sym> (BFD_RELOC_HI16_S)
6042 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6043 dsll32 $tempreg,0
6044 daddu $tempreg,$at
6045 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6046 If we have a base register, we want
6047 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6048 lui $at,<sym> (BFD_RELOC_HI16_S)
6049 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6050 daddu $at,$breg
6051 dsll32 $tempreg,0
6052 daddu $tempreg,$at
6053 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6055 Without $at we can't generate the optimal path for superscalar
6056 processors here since this would require two temporary registers.
6057 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6058 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6059 dsll $tempreg,16
6060 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6061 dsll $tempreg,16
6062 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6063 If we have a base register, we want
6064 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6065 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6066 dsll $tempreg,16
6067 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6068 dsll $tempreg,16
6069 daddu $tempreg,$tempreg,$breg
6070 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6072 If we have 64-bit addresses, as an optimization, for
6073 addresses which are 32-bit constants (e.g. kseg0/kseg1
6074 addresses) we fall back to the 32-bit address generation
6075 mechanism since it is more efficient. Note that due to
6076 the signed offset used by memory operations, the 32-bit
6077 range is shifted down by 32768 here. This code should
6078 probably attempt to generate 64-bit constants more
6079 efficiently in general.
6081 As an extension for architectures with 64-bit registers,
6082 we don't truncate 64-bit addresses given as literal
6083 constants down to 32 bits, to support existing practice
6084 in the mips64 Linux (the kernel), that compiles source
6085 files with -mabi=64, assembling them as o32 or n32 (with
6086 -Wa,-32 or -Wa,-n32). This is not beautiful, but since
6087 the whole kernel is loaded into a memory region that is
6088 addressible with sign-extended 32-bit addresses, it is
6089 wasteful to compute the upper 32 bits of every
6090 non-literal address, that takes more space and time.
6091 Some day this should probably be implemented as an
6092 assembler option, such that the kernel doesn't have to
6093 use such ugly hacks, even though it will still have to
6094 end up converting the binary to ELF32 for a number of
6095 platforms whose boot loaders don't support ELF64
6096 binaries. */
6097 if ((HAVE_64BIT_ADDRESSES
6098 && ! (offset_expr.X_op == O_constant
6099 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
6100 || (HAVE_64BIT_GPRS
6101 && offset_expr.X_op == O_constant
6102 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
6104 p = NULL;
6106 /* We don't do GP optimization for now because RELAX_ENCODE can't
6107 hold the data for such large chunks. */
6109 if (used_at == 0 && ! mips_opts.noat)
6111 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6112 tempreg, BFD_RELOC_MIPS_HIGHEST);
6113 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6114 AT, BFD_RELOC_HI16_S);
6115 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6116 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
6117 if (breg != 0)
6118 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6119 AT, AT, breg);
6120 macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
6121 tempreg, tempreg, 0);
6122 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6123 tempreg, tempreg, AT);
6124 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6125 BFD_RELOC_LO16, tempreg);
6126 used_at = 1;
6128 else
6130 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6131 tempreg, BFD_RELOC_MIPS_HIGHEST);
6132 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6133 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
6134 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
6135 tempreg, tempreg, 16);
6136 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6137 tempreg, tempreg, BFD_RELOC_HI16_S);
6138 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
6139 tempreg, tempreg, 16);
6140 if (breg != 0)
6141 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6142 tempreg, tempreg, breg);
6143 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6144 BFD_RELOC_LO16, tempreg);
6147 return;
6150 if (offset_expr.X_op == O_constant
6151 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000))
6152 as_bad (_("load/store address overflow (max 32 bits)"));
6154 if (breg == 0)
6156 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6157 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6158 p = NULL;
6159 else
6161 frag_grow (20);
6162 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6163 BFD_RELOC_GPREL16, mips_gp_register);
6164 p = frag_var (rs_machine_dependent, 8, 0,
6165 RELAX_ENCODE (4, 8, 0, 4, 0,
6166 (mips_opts.warn_about_macros
6167 || (used_at
6168 && mips_opts.noat))),
6169 offset_expr.X_add_symbol, 0, NULL);
6170 used_at = 0;
6172 macro_build_lui (p, &icnt, &offset_expr, tempreg);
6173 if (p != NULL)
6174 p += 4;
6175 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6176 BFD_RELOC_LO16, tempreg);
6178 else
6180 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6181 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6182 p = NULL;
6183 else
6185 frag_grow (28);
6186 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6187 tempreg, breg, mips_gp_register);
6188 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6189 BFD_RELOC_GPREL16, tempreg);
6190 p = frag_var (rs_machine_dependent, 12, 0,
6191 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
6192 offset_expr.X_add_symbol, 0, NULL);
6194 macro_build_lui (p, &icnt, &offset_expr, tempreg);
6195 if (p != NULL)
6196 p += 4;
6197 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6198 tempreg, tempreg, breg);
6199 if (p != NULL)
6200 p += 4;
6201 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6202 BFD_RELOC_LO16, tempreg);
6205 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6207 char *p;
6208 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6210 /* If this is a reference to an external symbol, we want
6211 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6213 <op> $treg,0($tempreg)
6214 Otherwise we want
6215 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6217 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6218 <op> $treg,0($tempreg)
6220 For NewABI, we want
6221 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6222 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6224 If there is a base register, we add it to $tempreg before
6225 the <op>. If there is a constant, we stick it in the
6226 <op> instruction. We don't handle constants larger than
6227 16 bits, because we have no way to load the upper 16 bits
6228 (actually, we could handle them for the subset of cases
6229 in which we are not using $at). */
6230 assert (offset_expr.X_op == O_symbol);
6231 if (HAVE_NEWABI)
6233 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6234 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6235 mips_gp_register);
6236 if (breg != 0)
6237 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6238 tempreg, tempreg, breg);
6239 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6240 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6242 if (! used_at)
6243 return;
6245 break;
6247 expr1.X_add_number = offset_expr.X_add_number;
6248 offset_expr.X_add_number = 0;
6249 if (expr1.X_add_number < -0x8000
6250 || expr1.X_add_number >= 0x8000)
6251 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6252 frag_grow (20);
6253 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6254 tempreg, lw_reloc_type, mips_gp_register);
6255 macro_build (NULL, &icnt, NULL, "nop", "");
6256 p = frag_var (rs_machine_dependent, 4, 0,
6257 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
6258 offset_expr.X_add_symbol, 0, NULL);
6259 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
6260 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
6261 if (breg != 0)
6262 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6263 tempreg, tempreg, breg);
6264 macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6265 tempreg);
6267 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6269 int gpdel;
6270 char *p;
6272 /* If this is a reference to an external symbol, we want
6273 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6274 addu $tempreg,$tempreg,$gp
6275 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6276 <op> $treg,0($tempreg)
6277 Otherwise we want
6278 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6280 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6281 <op> $treg,0($tempreg)
6282 If there is a base register, we add it to $tempreg before
6283 the <op>. If there is a constant, we stick it in the
6284 <op> instruction. We don't handle constants larger than
6285 16 bits, because we have no way to load the upper 16 bits
6286 (actually, we could handle them for the subset of cases
6287 in which we are not using $at). */
6288 assert (offset_expr.X_op == O_symbol);
6289 expr1.X_add_number = offset_expr.X_add_number;
6290 offset_expr.X_add_number = 0;
6291 if (expr1.X_add_number < -0x8000
6292 || expr1.X_add_number >= 0x8000)
6293 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6294 if (reg_needs_delay (mips_gp_register))
6295 gpdel = 4;
6296 else
6297 gpdel = 0;
6298 frag_grow (36);
6299 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6300 BFD_RELOC_MIPS_GOT_HI16);
6301 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6302 tempreg, tempreg, mips_gp_register);
6303 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6304 tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
6305 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6306 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6307 offset_expr.X_add_symbol, 0, NULL);
6308 if (gpdel > 0)
6310 macro_build (p, &icnt, NULL, "nop", "");
6311 p += 4;
6313 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6314 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6315 p += 4;
6316 macro_build (p, &icnt, NULL, "nop", "");
6317 p += 4;
6318 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6319 tempreg, tempreg, BFD_RELOC_LO16);
6320 if (breg != 0)
6321 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6322 tempreg, tempreg, breg);
6323 macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6324 tempreg);
6326 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6328 char *p;
6329 int bregsz = breg != 0 ? 4 : 0;
6331 /* If this is a reference to an external symbol, we want
6332 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6333 add $tempreg,$tempreg,$gp
6334 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6335 <op> $treg,<ofst>($tempreg)
6336 Otherwise, for local symbols, we want:
6337 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6338 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6339 assert (offset_expr.X_op == O_symbol);
6340 frag_now->tc_frag_data.tc_fr_offset =
6341 expr1.X_add_number = offset_expr.X_add_number;
6342 offset_expr.X_add_number = 0;
6343 if (expr1.X_add_number < -0x8000
6344 || expr1.X_add_number >= 0x8000)
6345 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6346 frag_grow (36);
6347 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6348 BFD_RELOC_MIPS_GOT_HI16);
6349 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6350 tempreg, tempreg, mips_gp_register);
6351 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6352 tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
6353 if (breg != 0)
6354 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6355 tempreg, tempreg, breg);
6356 macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6357 tempreg);
6359 offset_expr.X_add_number = expr1.X_add_number;
6360 p = frag_var (rs_machine_dependent, 12 + bregsz, 0,
6361 RELAX_ENCODE (16 + bregsz, 8 + bregsz,
6362 0, 4 + bregsz, 0, 0),
6363 offset_expr.X_add_symbol, 0, NULL);
6364 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6365 tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6366 if (breg != 0)
6367 macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6368 tempreg, tempreg, breg);
6369 macro_build (p + 4 + bregsz, &icnt, &offset_expr, s, fmt, treg,
6370 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6372 else if (mips_pic == EMBEDDED_PIC)
6374 /* If there is no base register, we want
6375 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6376 If there is a base register, we want
6377 addu $tempreg,$breg,$gp
6378 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
6380 assert (offset_expr.X_op == O_symbol);
6381 if (breg == 0)
6383 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6384 BFD_RELOC_GPREL16, mips_gp_register);
6385 used_at = 0;
6387 else
6389 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6390 tempreg, breg, mips_gp_register);
6391 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6392 BFD_RELOC_GPREL16, tempreg);
6395 else
6396 abort ();
6398 if (! used_at)
6399 return;
6401 break;
6403 case M_LI:
6404 case M_LI_S:
6405 load_register (&icnt, treg, &imm_expr, 0);
6406 return;
6408 case M_DLI:
6409 load_register (&icnt, treg, &imm_expr, 1);
6410 return;
6412 case M_LI_SS:
6413 if (imm_expr.X_op == O_constant)
6415 load_register (&icnt, AT, &imm_expr, 0);
6416 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
6417 break;
6419 else
6421 assert (offset_expr.X_op == O_symbol
6422 && strcmp (segment_name (S_GET_SEGMENT
6423 (offset_expr.X_add_symbol)),
6424 ".lit4") == 0
6425 && offset_expr.X_add_number == 0);
6426 macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)", treg,
6427 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6428 return;
6431 case M_LI_D:
6432 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6433 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6434 order 32 bits of the value and the low order 32 bits are either
6435 zero or in OFFSET_EXPR. */
6436 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6438 if (HAVE_64BIT_GPRS)
6439 load_register (&icnt, treg, &imm_expr, 1);
6440 else
6442 int hreg, lreg;
6444 if (target_big_endian)
6446 hreg = treg;
6447 lreg = treg + 1;
6449 else
6451 hreg = treg + 1;
6452 lreg = treg;
6455 if (hreg <= 31)
6456 load_register (&icnt, hreg, &imm_expr, 0);
6457 if (lreg <= 31)
6459 if (offset_expr.X_op == O_absent)
6460 move_register (&icnt, lreg, 0);
6461 else
6463 assert (offset_expr.X_op == O_constant);
6464 load_register (&icnt, lreg, &offset_expr, 0);
6468 return;
6471 /* We know that sym is in the .rdata section. First we get the
6472 upper 16 bits of the address. */
6473 if (mips_pic == NO_PIC)
6475 macro_build_lui (NULL, &icnt, &offset_expr, AT);
6477 else if (mips_pic == SVR4_PIC)
6479 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6480 AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6482 else if (mips_pic == EMBEDDED_PIC)
6484 /* For embedded PIC we pick up the entire address off $gp in
6485 a single instruction. */
6486 macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6487 AT, mips_gp_register, BFD_RELOC_GPREL16);
6488 offset_expr.X_op = O_constant;
6489 offset_expr.X_add_number = 0;
6491 else
6492 abort ();
6494 /* Now we load the register(s). */
6495 if (HAVE_64BIT_GPRS)
6496 macro_build (NULL, &icnt, &offset_expr, "ld", "t,o(b)", treg,
6497 BFD_RELOC_LO16, AT);
6498 else
6500 macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)", treg,
6501 BFD_RELOC_LO16, AT);
6502 if (treg != RA)
6504 /* FIXME: How in the world do we deal with the possible
6505 overflow here? */
6506 offset_expr.X_add_number += 4;
6507 macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6508 treg + 1, BFD_RELOC_LO16, AT);
6512 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6513 does not become a variant frag. */
6514 frag_wane (frag_now);
6515 frag_new (0);
6517 break;
6519 case M_LI_DD:
6520 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6521 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6522 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6523 the value and the low order 32 bits are either zero or in
6524 OFFSET_EXPR. */
6525 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6527 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6528 if (HAVE_64BIT_FPRS)
6530 assert (HAVE_64BIT_GPRS);
6531 macro_build (NULL, &icnt, NULL, "dmtc1", "t,S", AT, treg);
6533 else
6535 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg + 1);
6536 if (offset_expr.X_op == O_absent)
6537 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", 0, treg);
6538 else
6540 assert (offset_expr.X_op == O_constant);
6541 load_register (&icnt, AT, &offset_expr, 0);
6542 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
6545 break;
6548 assert (offset_expr.X_op == O_symbol
6549 && offset_expr.X_add_number == 0);
6550 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6551 if (strcmp (s, ".lit8") == 0)
6553 if (mips_opts.isa != ISA_MIPS1)
6555 macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6556 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6557 return;
6559 breg = mips_gp_register;
6560 r = BFD_RELOC_MIPS_LITERAL;
6561 goto dob;
6563 else
6565 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6566 if (mips_pic == SVR4_PIC)
6567 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6568 "t,o(b)", AT, BFD_RELOC_MIPS_GOT16,
6569 mips_gp_register);
6570 else
6572 /* FIXME: This won't work for a 64 bit address. */
6573 macro_build_lui (NULL, &icnt, &offset_expr, AT);
6576 if (mips_opts.isa != ISA_MIPS1)
6578 macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6579 BFD_RELOC_LO16, AT);
6581 /* To avoid confusion in tc_gen_reloc, we must ensure
6582 that this does not become a variant frag. */
6583 frag_wane (frag_now);
6584 frag_new (0);
6586 break;
6588 breg = AT;
6589 r = BFD_RELOC_LO16;
6590 goto dob;
6593 case M_L_DOB:
6594 if (mips_opts.arch == CPU_R4650)
6596 as_bad (_("opcode not supported on this processor"));
6597 return;
6599 /* Even on a big endian machine $fn comes before $fn+1. We have
6600 to adjust when loading from memory. */
6601 r = BFD_RELOC_LO16;
6602 dob:
6603 assert (mips_opts.isa == ISA_MIPS1);
6604 macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6605 target_big_endian ? treg + 1 : treg, r, breg);
6606 /* FIXME: A possible overflow which I don't know how to deal
6607 with. */
6608 offset_expr.X_add_number += 4;
6609 macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6610 target_big_endian ? treg : treg + 1, r, breg);
6612 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6613 does not become a variant frag. */
6614 frag_wane (frag_now);
6615 frag_new (0);
6617 if (breg != AT)
6618 return;
6619 break;
6621 case M_L_DAB:
6623 * The MIPS assembler seems to check for X_add_number not
6624 * being double aligned and generating:
6625 * lui at,%hi(foo+1)
6626 * addu at,at,v1
6627 * addiu at,at,%lo(foo+1)
6628 * lwc1 f2,0(at)
6629 * lwc1 f3,4(at)
6630 * But, the resulting address is the same after relocation so why
6631 * generate the extra instruction?
6633 if (mips_opts.arch == CPU_R4650)
6635 as_bad (_("opcode not supported on this processor"));
6636 return;
6638 /* Itbl support may require additional care here. */
6639 coproc = 1;
6640 if (mips_opts.isa != ISA_MIPS1)
6642 s = "ldc1";
6643 goto ld;
6646 s = "lwc1";
6647 fmt = "T,o(b)";
6648 goto ldd_std;
6650 case M_S_DAB:
6651 if (mips_opts.arch == CPU_R4650)
6653 as_bad (_("opcode not supported on this processor"));
6654 return;
6657 if (mips_opts.isa != ISA_MIPS1)
6659 s = "sdc1";
6660 goto st;
6663 s = "swc1";
6664 fmt = "T,o(b)";
6665 /* Itbl support may require additional care here. */
6666 coproc = 1;
6667 goto ldd_std;
6669 case M_LD_AB:
6670 if (HAVE_64BIT_GPRS)
6672 s = "ld";
6673 goto ld;
6676 s = "lw";
6677 fmt = "t,o(b)";
6678 goto ldd_std;
6680 case M_SD_AB:
6681 if (HAVE_64BIT_GPRS)
6683 s = "sd";
6684 goto st;
6687 s = "sw";
6688 fmt = "t,o(b)";
6690 ldd_std:
6691 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6692 loads for the case of doing a pair of loads to simulate an 'ld'.
6693 This is not currently done by the compiler, and assembly coders
6694 writing embedded-pic code can cope. */
6696 if (offset_expr.X_op != O_symbol
6697 && offset_expr.X_op != O_constant)
6699 as_bad (_("expression too complex"));
6700 offset_expr.X_op = O_constant;
6703 /* Even on a big endian machine $fn comes before $fn+1. We have
6704 to adjust when loading from memory. We set coproc if we must
6705 load $fn+1 first. */
6706 /* Itbl support may require additional care here. */
6707 if (! target_big_endian)
6708 coproc = 0;
6710 if (mips_pic == NO_PIC
6711 || offset_expr.X_op == O_constant)
6713 char *p;
6715 /* If this is a reference to a GP relative symbol, we want
6716 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6717 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6718 If we have a base register, we use this
6719 addu $at,$breg,$gp
6720 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6721 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6722 If this is not a GP relative symbol, we want
6723 lui $at,<sym> (BFD_RELOC_HI16_S)
6724 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6725 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6726 If there is a base register, we add it to $at after the
6727 lui instruction. If there is a constant, we always use
6728 the last case. */
6729 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6730 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6732 p = NULL;
6733 used_at = 1;
6735 else
6737 int off;
6739 if (breg == 0)
6741 frag_grow (28);
6742 tempreg = mips_gp_register;
6743 off = 0;
6744 used_at = 0;
6746 else
6748 frag_grow (36);
6749 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6750 AT, breg, mips_gp_register);
6751 tempreg = AT;
6752 off = 4;
6753 used_at = 1;
6756 /* Itbl support may require additional care here. */
6757 macro_build (NULL, &icnt, &offset_expr, s, fmt,
6758 coproc ? treg + 1 : treg,
6759 BFD_RELOC_GPREL16, tempreg);
6760 offset_expr.X_add_number += 4;
6762 /* Set mips_optimize to 2 to avoid inserting an
6763 undesired nop. */
6764 hold_mips_optimize = mips_optimize;
6765 mips_optimize = 2;
6766 /* Itbl support may require additional care here. */
6767 macro_build (NULL, &icnt, &offset_expr, s, fmt,
6768 coproc ? treg : treg + 1,
6769 BFD_RELOC_GPREL16, tempreg);
6770 mips_optimize = hold_mips_optimize;
6772 p = frag_var (rs_machine_dependent, 12 + off, 0,
6773 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6774 used_at && mips_opts.noat),
6775 offset_expr.X_add_symbol, 0, NULL);
6777 /* We just generated two relocs. When tc_gen_reloc
6778 handles this case, it will skip the first reloc and
6779 handle the second. The second reloc already has an
6780 extra addend of 4, which we added above. We must
6781 subtract it out, and then subtract another 4 to make
6782 the first reloc come out right. The second reloc
6783 will come out right because we are going to add 4 to
6784 offset_expr when we build its instruction below.
6786 If we have a symbol, then we don't want to include
6787 the offset, because it will wind up being included
6788 when we generate the reloc. */
6790 if (offset_expr.X_op == O_constant)
6791 offset_expr.X_add_number -= 8;
6792 else
6794 offset_expr.X_add_number = -4;
6795 offset_expr.X_op = O_constant;
6798 macro_build_lui (p, &icnt, &offset_expr, AT);
6799 if (p != NULL)
6800 p += 4;
6801 if (breg != 0)
6803 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6804 AT, breg, AT);
6805 if (p != NULL)
6806 p += 4;
6808 /* Itbl support may require additional care here. */
6809 macro_build (p, &icnt, &offset_expr, s, fmt,
6810 coproc ? treg + 1 : treg,
6811 BFD_RELOC_LO16, AT);
6812 if (p != NULL)
6813 p += 4;
6814 /* FIXME: How do we handle overflow here? */
6815 offset_expr.X_add_number += 4;
6816 /* Itbl support may require additional care here. */
6817 macro_build (p, &icnt, &offset_expr, s, fmt,
6818 coproc ? treg : treg + 1,
6819 BFD_RELOC_LO16, AT);
6821 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6823 int off;
6825 /* If this is a reference to an external symbol, we want
6826 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6828 <op> $treg,0($at)
6829 <op> $treg+1,4($at)
6830 Otherwise we want
6831 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6833 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6834 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6835 If there is a base register we add it to $at before the
6836 lwc1 instructions. If there is a constant we include it
6837 in the lwc1 instructions. */
6838 used_at = 1;
6839 expr1.X_add_number = offset_expr.X_add_number;
6840 offset_expr.X_add_number = 0;
6841 if (expr1.X_add_number < -0x8000
6842 || expr1.X_add_number >= 0x8000 - 4)
6843 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6844 if (breg == 0)
6845 off = 0;
6846 else
6847 off = 4;
6848 frag_grow (24 + off);
6849 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6850 AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6851 macro_build (NULL, &icnt, NULL, "nop", "");
6852 if (breg != 0)
6853 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6854 AT, breg, AT);
6855 /* Itbl support may require additional care here. */
6856 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6857 BFD_RELOC_LO16, AT);
6858 expr1.X_add_number += 4;
6860 /* Set mips_optimize to 2 to avoid inserting an undesired
6861 nop. */
6862 hold_mips_optimize = mips_optimize;
6863 mips_optimize = 2;
6864 /* Itbl support may require additional care here. */
6865 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6866 BFD_RELOC_LO16, AT);
6867 mips_optimize = hold_mips_optimize;
6869 (void) frag_var (rs_machine_dependent, 0, 0,
6870 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6871 offset_expr.X_add_symbol, 0, NULL);
6873 else if (mips_pic == SVR4_PIC)
6875 int gpdel, off;
6876 char *p;
6878 /* If this is a reference to an external symbol, we want
6879 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6880 addu $at,$at,$gp
6881 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6883 <op> $treg,0($at)
6884 <op> $treg+1,4($at)
6885 Otherwise we want
6886 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6888 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6889 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6890 If there is a base register we add it to $at before the
6891 lwc1 instructions. If there is a constant we include it
6892 in the lwc1 instructions. */
6893 used_at = 1;
6894 expr1.X_add_number = offset_expr.X_add_number;
6895 offset_expr.X_add_number = 0;
6896 if (expr1.X_add_number < -0x8000
6897 || expr1.X_add_number >= 0x8000 - 4)
6898 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6899 if (reg_needs_delay (mips_gp_register))
6900 gpdel = 4;
6901 else
6902 gpdel = 0;
6903 if (breg == 0)
6904 off = 0;
6905 else
6906 off = 4;
6907 frag_grow (56);
6908 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", AT,
6909 BFD_RELOC_MIPS_GOT_HI16);
6910 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6911 AT, AT, mips_gp_register);
6912 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6913 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6914 macro_build (NULL, &icnt, NULL, "nop", "");
6915 if (breg != 0)
6916 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6917 AT, breg, AT);
6918 /* Itbl support may require additional care here. */
6919 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6920 BFD_RELOC_LO16, AT);
6921 expr1.X_add_number += 4;
6923 /* Set mips_optimize to 2 to avoid inserting an undesired
6924 nop. */
6925 hold_mips_optimize = mips_optimize;
6926 mips_optimize = 2;
6927 /* Itbl support may require additional care here. */
6928 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6929 BFD_RELOC_LO16, AT);
6930 mips_optimize = hold_mips_optimize;
6931 expr1.X_add_number -= 4;
6933 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6934 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6935 8 + gpdel + off, 1, 0),
6936 offset_expr.X_add_symbol, 0, NULL);
6937 if (gpdel > 0)
6939 macro_build (p, &icnt, NULL, "nop", "");
6940 p += 4;
6942 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6943 AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6944 p += 4;
6945 macro_build (p, &icnt, NULL, "nop", "");
6946 p += 4;
6947 if (breg != 0)
6949 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6950 AT, breg, AT);
6951 p += 4;
6953 /* Itbl support may require additional care here. */
6954 macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6955 BFD_RELOC_LO16, AT);
6956 p += 4;
6957 expr1.X_add_number += 4;
6959 /* Set mips_optimize to 2 to avoid inserting an undesired
6960 nop. */
6961 hold_mips_optimize = mips_optimize;
6962 mips_optimize = 2;
6963 /* Itbl support may require additional care here. */
6964 macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6965 BFD_RELOC_LO16, AT);
6966 mips_optimize = hold_mips_optimize;
6968 else if (mips_pic == EMBEDDED_PIC)
6970 /* If there is no base register, we use
6971 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6972 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6973 If we have a base register, we use
6974 addu $at,$breg,$gp
6975 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6976 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6978 if (breg == 0)
6980 tempreg = mips_gp_register;
6981 used_at = 0;
6983 else
6985 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6986 AT, breg, mips_gp_register);
6987 tempreg = AT;
6988 used_at = 1;
6991 /* Itbl support may require additional care here. */
6992 macro_build (NULL, &icnt, &offset_expr, s, fmt,
6993 coproc ? treg + 1 : treg,
6994 BFD_RELOC_GPREL16, tempreg);
6995 offset_expr.X_add_number += 4;
6996 /* Itbl support may require additional care here. */
6997 macro_build (NULL, &icnt, &offset_expr, s, fmt,
6998 coproc ? treg : treg + 1,
6999 BFD_RELOC_GPREL16, tempreg);
7001 else
7002 abort ();
7004 if (! used_at)
7005 return;
7007 break;
7009 case M_LD_OB:
7010 s = "lw";
7011 goto sd_ob;
7012 case M_SD_OB:
7013 s = "sw";
7014 sd_ob:
7015 assert (HAVE_32BIT_ADDRESSES);
7016 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7017 BFD_RELOC_LO16, breg);
7018 offset_expr.X_add_number += 4;
7019 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
7020 BFD_RELOC_LO16, breg);
7021 return;
7023 /* New code added to support COPZ instructions.
7024 This code builds table entries out of the macros in mip_opcodes.
7025 R4000 uses interlocks to handle coproc delays.
7026 Other chips (like the R3000) require nops to be inserted for delays.
7028 FIXME: Currently, we require that the user handle delays.
7029 In order to fill delay slots for non-interlocked chips,
7030 we must have a way to specify delays based on the coprocessor.
7031 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7032 What are the side-effects of the cop instruction?
7033 What cache support might we have and what are its effects?
7034 Both coprocessor & memory require delays. how long???
7035 What registers are read/set/modified?
7037 If an itbl is provided to interpret cop instructions,
7038 this knowledge can be encoded in the itbl spec. */
7040 case M_COP0:
7041 s = "c0";
7042 goto copz;
7043 case M_COP1:
7044 s = "c1";
7045 goto copz;
7046 case M_COP2:
7047 s = "c2";
7048 goto copz;
7049 case M_COP3:
7050 s = "c3";
7051 copz:
7052 /* For now we just do C (same as Cz). The parameter will be
7053 stored in insn_opcode by mips_ip. */
7054 macro_build (NULL, &icnt, NULL, s, "C", ip->insn_opcode);
7055 return;
7057 case M_MOVE:
7058 move_register (&icnt, dreg, sreg);
7059 return;
7061 #ifdef LOSING_COMPILER
7062 default:
7063 /* Try and see if this is a new itbl instruction.
7064 This code builds table entries out of the macros in mip_opcodes.
7065 FIXME: For now we just assemble the expression and pass it's
7066 value along as a 32-bit immediate.
7067 We may want to have the assembler assemble this value,
7068 so that we gain the assembler's knowledge of delay slots,
7069 symbols, etc.
7070 Would it be more efficient to use mask (id) here? */
7071 if (itbl_have_entries
7072 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
7074 s = ip->insn_mo->name;
7075 s2 = "cop3";
7076 coproc = ITBL_DECODE_PNUM (immed_expr);;
7077 macro_build (NULL, &icnt, &immed_expr, s, "C");
7078 return;
7080 macro2 (ip);
7081 return;
7083 if (mips_opts.noat)
7084 as_warn (_("Macro used $at after \".set noat\""));
7087 static void
7088 macro2 (struct mips_cl_insn *ip)
7090 register int treg, sreg, dreg, breg;
7091 int tempreg;
7092 int mask;
7093 int icnt = 0;
7094 int used_at;
7095 expressionS expr1;
7096 const char *s;
7097 const char *s2;
7098 const char *fmt;
7099 int likely = 0;
7100 int dbl = 0;
7101 int coproc = 0;
7102 int lr = 0;
7103 int imm = 0;
7104 int off;
7105 offsetT maxnum;
7106 bfd_reloc_code_real_type r;
7107 char *p;
7109 treg = (ip->insn_opcode >> 16) & 0x1f;
7110 dreg = (ip->insn_opcode >> 11) & 0x1f;
7111 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7112 mask = ip->insn_mo->mask;
7114 expr1.X_op = O_constant;
7115 expr1.X_op_symbol = NULL;
7116 expr1.X_add_symbol = NULL;
7117 expr1.X_add_number = 1;
7119 switch (mask)
7121 #endif /* LOSING_COMPILER */
7123 case M_DMUL:
7124 dbl = 1;
7125 case M_MUL:
7126 macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
7127 sreg, treg);
7128 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7129 return;
7131 case M_DMUL_I:
7132 dbl = 1;
7133 case M_MUL_I:
7134 /* The MIPS assembler some times generates shifts and adds. I'm
7135 not trying to be that fancy. GCC should do this for us
7136 anyway. */
7137 load_register (&icnt, AT, &imm_expr, dbl);
7138 macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
7139 sreg, AT);
7140 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7141 break;
7143 case M_DMULO_I:
7144 dbl = 1;
7145 case M_MULO_I:
7146 imm = 1;
7147 goto do_mulo;
7149 case M_DMULO:
7150 dbl = 1;
7151 case M_MULO:
7152 do_mulo:
7153 mips_emit_delays (TRUE);
7154 ++mips_opts.noreorder;
7155 mips_any_noreorder = 1;
7156 if (imm)
7157 load_register (&icnt, AT, &imm_expr, dbl);
7158 macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
7159 sreg, imm ? AT : treg);
7160 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7161 macro_build (NULL, &icnt, NULL, dbl ? "dsra32" : "sra", "d,w,<",
7162 dreg, dreg, RA);
7163 macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
7164 if (mips_trap)
7165 macro_build (NULL, &icnt, NULL, "tne", "s,t,q", dreg, AT, 6);
7166 else
7168 expr1.X_add_number = 8;
7169 macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
7170 macro_build (NULL, &icnt, NULL, "nop", "", 0);
7171 macro_build (NULL, &icnt, NULL, "break", "c", 6);
7173 --mips_opts.noreorder;
7174 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7175 break;
7177 case M_DMULOU_I:
7178 dbl = 1;
7179 case M_MULOU_I:
7180 imm = 1;
7181 goto do_mulou;
7183 case M_DMULOU:
7184 dbl = 1;
7185 case M_MULOU:
7186 do_mulou:
7187 mips_emit_delays (TRUE);
7188 ++mips_opts.noreorder;
7189 mips_any_noreorder = 1;
7190 if (imm)
7191 load_register (&icnt, AT, &imm_expr, dbl);
7192 macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
7193 sreg, imm ? AT : treg);
7194 macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
7195 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7196 if (mips_trap)
7197 macro_build (NULL, &icnt, NULL, "tne", "s,t,q", AT, 0, 6);
7198 else
7200 expr1.X_add_number = 8;
7201 macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
7202 macro_build (NULL, &icnt, NULL, "nop", "", 0);
7203 macro_build (NULL, &icnt, NULL, "break", "c", 6);
7205 --mips_opts.noreorder;
7206 break;
7208 case M_DROL:
7209 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7211 if (dreg == sreg)
7213 tempreg = AT;
7214 used_at = 1;
7216 else
7218 tempreg = dreg;
7219 used_at = 0;
7221 macro_build (NULL, &icnt, NULL, "dnegu", "d,w", tempreg, treg);
7222 macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg,
7223 tempreg);
7224 if (used_at)
7225 break;
7226 return;
7228 macro_build (NULL, &icnt, NULL, "dsubu", "d,v,t", AT, 0, treg);
7229 macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7230 macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7231 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7232 break;
7234 case M_ROL:
7235 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7237 if (dreg == sreg)
7239 tempreg = AT;
7240 used_at = 1;
7242 else
7244 tempreg = dreg;
7245 used_at = 0;
7247 macro_build (NULL, &icnt, NULL, "negu", "d,w", tempreg, treg);
7248 macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg,
7249 tempreg);
7250 if (used_at)
7251 break;
7252 return;
7254 macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7255 macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
7256 macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg, treg);
7257 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7258 break;
7260 case M_DROL_I:
7262 unsigned int rot;
7263 char *l, *r;
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))
7270 rot = (64 - rot) & 0x3f;
7271 if (rot >= 32)
7272 macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7273 dreg, sreg, rot - 32);
7274 else
7275 macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7276 dreg, sreg, rot);
7277 return;
7279 if (rot == 0)
7281 macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7282 return;
7284 l = (rot < 0x20) ? "dsll" : "dsll32";
7285 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7286 rot &= 0x1f;
7287 macro_build (NULL, &icnt, NULL, l, "d,w,<", AT, sreg, rot);
7288 macro_build (NULL, &icnt, NULL, r, "d,w,<", dreg, sreg,
7289 (0x20 - rot) & 0x1f);
7290 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7292 break;
7294 case M_ROL_I:
7296 unsigned int rot;
7298 if (imm_expr.X_op != O_constant)
7299 as_bad (_("Improper rotate count"));
7300 rot = imm_expr.X_add_number & 0x1f;
7301 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7303 macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg,
7304 (32 - rot) & 0x1f);
7305 return;
7307 if (rot == 0)
7309 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
7310 return;
7312 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg, rot);
7313 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
7314 (0x20 - rot) & 0x1f);
7315 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7317 break;
7319 case M_DROR:
7320 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7322 macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg, treg);
7323 return;
7325 macro_build (NULL, &icnt,NULL, "dsubu", "d,v,t", AT, 0, treg);
7326 macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", AT, sreg, AT);
7327 macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7328 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7329 break;
7331 case M_ROR:
7332 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7334 macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg, treg);
7335 return;
7337 macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7338 macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
7339 macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg, treg);
7340 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7341 break;
7343 case M_DROR_I:
7345 unsigned int rot;
7346 char *l, *r;
7348 if (imm_expr.X_op != O_constant)
7349 as_bad (_("Improper rotate count"));
7350 rot = imm_expr.X_add_number & 0x3f;
7351 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7353 if (rot >= 32)
7354 macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7355 dreg, sreg, rot - 32);
7356 else
7357 macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7358 dreg, sreg, rot);
7359 return;
7361 if (rot == 0)
7363 macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7364 return;
7366 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7367 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7368 rot &= 0x1f;
7369 macro_build ( NULL, &icnt,NULL, r, "d,w,<", AT, sreg, rot);
7370 macro_build (NULL, &icnt, NULL, l, "d,w,<", dreg, sreg,
7371 (0x20 - rot) & 0x1f);
7372 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7374 break;
7376 case M_ROR_I:
7378 unsigned int rot;
7380 if (imm_expr.X_op != O_constant)
7381 as_bad (_("Improper rotate count"));
7382 rot = imm_expr.X_add_number & 0x1f;
7383 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7385 macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg, rot);
7386 return;
7388 if (rot == 0)
7390 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
7391 return;
7393 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg, rot);
7394 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
7395 (0x20 - rot) & 0x1f);
7396 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7398 break;
7400 case M_S_DOB:
7401 if (mips_opts.arch == CPU_R4650)
7403 as_bad (_("opcode not supported on this processor"));
7404 return;
7406 assert (mips_opts.isa == ISA_MIPS1);
7407 /* Even on a big endian machine $fn comes before $fn+1. We have
7408 to adjust when storing to memory. */
7409 macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7410 target_big_endian ? treg + 1 : treg,
7411 BFD_RELOC_LO16, breg);
7412 offset_expr.X_add_number += 4;
7413 macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7414 target_big_endian ? treg : treg + 1,
7415 BFD_RELOC_LO16, breg);
7416 return;
7418 case M_SEQ:
7419 if (sreg == 0)
7420 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, treg,
7421 BFD_RELOC_LO16);
7422 else if (treg == 0)
7423 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7424 BFD_RELOC_LO16);
7425 else
7427 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7428 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7429 BFD_RELOC_LO16);
7431 return;
7433 case M_SEQ_I:
7434 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7436 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7437 BFD_RELOC_LO16);
7438 return;
7440 if (sreg == 0)
7442 as_warn (_("Instruction %s: result is always false"),
7443 ip->insn_mo->name);
7444 move_register (&icnt, dreg, 0);
7445 return;
7447 if (imm_expr.X_op == O_constant
7448 && imm_expr.X_add_number >= 0
7449 && imm_expr.X_add_number < 0x10000)
7451 macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7452 BFD_RELOC_LO16);
7453 used_at = 0;
7455 else if (imm_expr.X_op == O_constant
7456 && imm_expr.X_add_number > -0x8000
7457 && imm_expr.X_add_number < 0)
7459 imm_expr.X_add_number = -imm_expr.X_add_number;
7460 macro_build (NULL, &icnt, &imm_expr,
7461 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7462 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7463 used_at = 0;
7465 else
7467 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7468 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
7469 used_at = 1;
7471 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7472 BFD_RELOC_LO16);
7473 if (used_at)
7474 break;
7475 return;
7477 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7478 s = "slt";
7479 goto sge;
7480 case M_SGEU:
7481 s = "sltu";
7482 sge:
7483 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
7484 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7485 BFD_RELOC_LO16);
7486 return;
7488 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7489 case M_SGEU_I:
7490 if (imm_expr.X_op == O_constant
7491 && imm_expr.X_add_number >= -0x8000
7492 && imm_expr.X_add_number < 0x8000)
7494 macro_build (NULL, &icnt, &imm_expr,
7495 mask == M_SGE_I ? "slti" : "sltiu",
7496 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7497 used_at = 0;
7499 else
7501 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7502 macro_build (NULL, &icnt, NULL, mask == M_SGE_I ? "slt" : "sltu",
7503 "d,v,t", dreg, sreg, AT);
7504 used_at = 1;
7506 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7507 BFD_RELOC_LO16);
7508 if (used_at)
7509 break;
7510 return;
7512 case M_SGT: /* sreg > treg <==> treg < sreg */
7513 s = "slt";
7514 goto sgt;
7515 case M_SGTU:
7516 s = "sltu";
7517 sgt:
7518 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
7519 return;
7521 case M_SGT_I: /* sreg > I <==> I < sreg */
7522 s = "slt";
7523 goto sgti;
7524 case M_SGTU_I:
7525 s = "sltu";
7526 sgti:
7527 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7528 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
7529 break;
7531 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7532 s = "slt";
7533 goto sle;
7534 case M_SLEU:
7535 s = "sltu";
7536 sle:
7537 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
7538 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7539 BFD_RELOC_LO16);
7540 return;
7542 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7543 s = "slt";
7544 goto slei;
7545 case M_SLEU_I:
7546 s = "sltu";
7547 slei:
7548 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7549 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
7550 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7551 BFD_RELOC_LO16);
7552 break;
7554 case M_SLT_I:
7555 if (imm_expr.X_op == O_constant
7556 && imm_expr.X_add_number >= -0x8000
7557 && imm_expr.X_add_number < 0x8000)
7559 macro_build (NULL, &icnt, &imm_expr, "slti", "t,r,j", dreg, sreg,
7560 BFD_RELOC_LO16);
7561 return;
7563 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7564 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
7565 break;
7567 case M_SLTU_I:
7568 if (imm_expr.X_op == O_constant
7569 && imm_expr.X_add_number >= -0x8000
7570 && imm_expr.X_add_number < 0x8000)
7572 macro_build (NULL, &icnt, &imm_expr, "sltiu", "t,r,j", dreg, sreg,
7573 BFD_RELOC_LO16);
7574 return;
7576 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7577 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg, AT);
7578 break;
7580 case M_SNE:
7581 if (sreg == 0)
7582 macro_build (NULL, &icnt,NULL, "sltu","d,v,t", dreg, 0, treg);
7583 else if (treg == 0)
7584 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
7585 else
7587 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7588 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
7590 return;
7592 case M_SNE_I:
7593 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7595 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
7596 return;
7598 if (sreg == 0)
7600 as_warn (_("Instruction %s: result is always true"),
7601 ip->insn_mo->name);
7602 macro_build (NULL, &icnt, &expr1,
7603 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7604 "t,r,j", dreg, 0, BFD_RELOC_LO16);
7605 return;
7607 if (imm_expr.X_op == O_constant
7608 && imm_expr.X_add_number >= 0
7609 && imm_expr.X_add_number < 0x10000)
7611 macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7612 BFD_RELOC_LO16);
7613 used_at = 0;
7615 else if (imm_expr.X_op == O_constant
7616 && imm_expr.X_add_number > -0x8000
7617 && imm_expr.X_add_number < 0)
7619 imm_expr.X_add_number = -imm_expr.X_add_number;
7620 macro_build (NULL, &icnt, &imm_expr,
7621 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7622 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7623 used_at = 0;
7625 else
7627 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7628 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
7629 used_at = 1;
7631 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
7632 if (used_at)
7633 break;
7634 return;
7636 case M_DSUB_I:
7637 dbl = 1;
7638 case M_SUB_I:
7639 if (imm_expr.X_op == O_constant
7640 && imm_expr.X_add_number > -0x8000
7641 && imm_expr.X_add_number <= 0x8000)
7643 imm_expr.X_add_number = -imm_expr.X_add_number;
7644 macro_build (NULL, &icnt, &imm_expr, dbl ? "daddi" : "addi",
7645 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7646 return;
7648 load_register (&icnt, AT, &imm_expr, dbl);
7649 macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t",
7650 dreg, sreg, AT);
7651 break;
7653 case M_DSUBU_I:
7654 dbl = 1;
7655 case M_SUBU_I:
7656 if (imm_expr.X_op == O_constant
7657 && imm_expr.X_add_number > -0x8000
7658 && imm_expr.X_add_number <= 0x8000)
7660 imm_expr.X_add_number = -imm_expr.X_add_number;
7661 macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu",
7662 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7663 return;
7665 load_register (&icnt, AT, &imm_expr, dbl);
7666 macro_build (NULL, &icnt, NULL, dbl ? "dsubu" : "subu", "d,v,t",
7667 dreg, sreg, AT);
7668 break;
7670 case M_TEQ_I:
7671 s = "teq";
7672 goto trap;
7673 case M_TGE_I:
7674 s = "tge";
7675 goto trap;
7676 case M_TGEU_I:
7677 s = "tgeu";
7678 goto trap;
7679 case M_TLT_I:
7680 s = "tlt";
7681 goto trap;
7682 case M_TLTU_I:
7683 s = "tltu";
7684 goto trap;
7685 case M_TNE_I:
7686 s = "tne";
7687 trap:
7688 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7689 macro_build (NULL, &icnt, NULL, s, "s,t", sreg, AT);
7690 break;
7692 case M_TRUNCWS:
7693 case M_TRUNCWD:
7694 assert (mips_opts.isa == ISA_MIPS1);
7695 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7696 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7699 * Is the double cfc1 instruction a bug in the mips assembler;
7700 * or is there a reason for it?
7702 mips_emit_delays (TRUE);
7703 ++mips_opts.noreorder;
7704 mips_any_noreorder = 1;
7705 macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7706 macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7707 macro_build (NULL, &icnt, NULL, "nop", "");
7708 expr1.X_add_number = 3;
7709 macro_build (NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7710 BFD_RELOC_LO16);
7711 expr1.X_add_number = 2;
7712 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7713 BFD_RELOC_LO16);
7714 macro_build (NULL, &icnt, NULL, "ctc1", "t,G", AT, RA);
7715 macro_build (NULL, &icnt, NULL, "nop", "");
7716 macro_build (NULL, &icnt, NULL,
7717 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s",
7718 "D,S", dreg, sreg);
7719 macro_build (NULL, &icnt, NULL, "ctc1", "t,G", treg, RA);
7720 macro_build (NULL, &icnt, NULL, "nop", "");
7721 --mips_opts.noreorder;
7722 break;
7724 case M_ULH:
7725 s = "lb";
7726 goto ulh;
7727 case M_ULHU:
7728 s = "lbu";
7729 ulh:
7730 if (offset_expr.X_add_number >= 0x7fff)
7731 as_bad (_("operand overflow"));
7732 if (! target_big_endian)
7733 ++offset_expr.X_add_number;
7734 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
7735 BFD_RELOC_LO16, breg);
7736 if (! target_big_endian)
7737 --offset_expr.X_add_number;
7738 else
7739 ++offset_expr.X_add_number;
7740 macro_build (NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
7741 BFD_RELOC_LO16, breg);
7742 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, AT, 8);
7743 macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
7744 break;
7746 case M_ULD:
7747 s = "ldl";
7748 s2 = "ldr";
7749 off = 7;
7750 goto ulw;
7751 case M_ULW:
7752 s = "lwl";
7753 s2 = "lwr";
7754 off = 3;
7755 ulw:
7756 if (offset_expr.X_add_number >= 0x8000 - off)
7757 as_bad (_("operand overflow"));
7758 if (treg != breg)
7759 tempreg = treg;
7760 else
7761 tempreg = AT;
7762 if (! target_big_endian)
7763 offset_expr.X_add_number += off;
7764 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
7765 BFD_RELOC_LO16, breg);
7766 if (! target_big_endian)
7767 offset_expr.X_add_number -= off;
7768 else
7769 offset_expr.X_add_number += off;
7770 macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
7771 BFD_RELOC_LO16, breg);
7773 /* If necessary, move the result in tempreg the final destination. */
7774 if (treg == tempreg)
7775 return;
7776 /* Protect second load's delay slot. */
7777 if (!gpr_interlocks)
7778 macro_build (NULL, &icnt, NULL, "nop", "");
7779 move_register (&icnt, treg, tempreg);
7780 break;
7782 case M_ULD_A:
7783 s = "ldl";
7784 s2 = "ldr";
7785 off = 7;
7786 goto ulwa;
7787 case M_ULW_A:
7788 s = "lwl";
7789 s2 = "lwr";
7790 off = 3;
7791 ulwa:
7792 used_at = 1;
7793 load_address (&icnt, AT, &offset_expr, &used_at);
7794 if (breg != 0)
7795 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7796 AT, AT, breg);
7797 if (! target_big_endian)
7798 expr1.X_add_number = off;
7799 else
7800 expr1.X_add_number = 0;
7801 macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7802 BFD_RELOC_LO16, AT);
7803 if (! target_big_endian)
7804 expr1.X_add_number = 0;
7805 else
7806 expr1.X_add_number = off;
7807 macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7808 BFD_RELOC_LO16, AT);
7809 break;
7811 case M_ULH_A:
7812 case M_ULHU_A:
7813 used_at = 1;
7814 load_address (&icnt, AT, &offset_expr, &used_at);
7815 if (breg != 0)
7816 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7817 AT, AT, breg);
7818 if (target_big_endian)
7819 expr1.X_add_number = 0;
7820 macro_build (NULL, &icnt, &expr1,
7821 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7822 treg, BFD_RELOC_LO16, AT);
7823 if (target_big_endian)
7824 expr1.X_add_number = 1;
7825 else
7826 expr1.X_add_number = 0;
7827 macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)",
7828 AT, BFD_RELOC_LO16, AT);
7829 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7830 macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
7831 break;
7833 case M_USH:
7834 if (offset_expr.X_add_number >= 0x7fff)
7835 as_bad (_("operand overflow"));
7836 if (target_big_endian)
7837 ++offset_expr.X_add_number;
7838 macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7839 BFD_RELOC_LO16, breg);
7840 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
7841 if (target_big_endian)
7842 --offset_expr.X_add_number;
7843 else
7844 ++offset_expr.X_add_number;
7845 macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7846 BFD_RELOC_LO16, breg);
7847 break;
7849 case M_USD:
7850 s = "sdl";
7851 s2 = "sdr";
7852 off = 7;
7853 goto usw;
7854 case M_USW:
7855 s = "swl";
7856 s2 = "swr";
7857 off = 3;
7858 usw:
7859 if (offset_expr.X_add_number >= 0x8000 - off)
7860 as_bad (_("operand overflow"));
7861 if (! target_big_endian)
7862 offset_expr.X_add_number += off;
7863 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7864 BFD_RELOC_LO16, breg);
7865 if (! target_big_endian)
7866 offset_expr.X_add_number -= off;
7867 else
7868 offset_expr.X_add_number += off;
7869 macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7870 BFD_RELOC_LO16, breg);
7871 return;
7873 case M_USD_A:
7874 s = "sdl";
7875 s2 = "sdr";
7876 off = 7;
7877 goto uswa;
7878 case M_USW_A:
7879 s = "swl";
7880 s2 = "swr";
7881 off = 3;
7882 uswa:
7883 used_at = 1;
7884 load_address (&icnt, AT, &offset_expr, &used_at);
7885 if (breg != 0)
7886 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7887 AT, AT, breg);
7888 if (! target_big_endian)
7889 expr1.X_add_number = off;
7890 else
7891 expr1.X_add_number = 0;
7892 macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7893 BFD_RELOC_LO16, AT);
7894 if (! target_big_endian)
7895 expr1.X_add_number = 0;
7896 else
7897 expr1.X_add_number = off;
7898 macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7899 BFD_RELOC_LO16, AT);
7900 break;
7902 case M_USH_A:
7903 used_at = 1;
7904 load_address (&icnt, AT, &offset_expr, &used_at);
7905 if (breg != 0)
7906 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7907 AT, AT, breg);
7908 if (! target_big_endian)
7909 expr1.X_add_number = 0;
7910 macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7911 BFD_RELOC_LO16, AT);
7912 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", treg, treg, 8);
7913 if (! target_big_endian)
7914 expr1.X_add_number = 1;
7915 else
7916 expr1.X_add_number = 0;
7917 macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7918 BFD_RELOC_LO16, AT);
7919 if (! target_big_endian)
7920 expr1.X_add_number = 0;
7921 else
7922 expr1.X_add_number = 1;
7923 macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7924 BFD_RELOC_LO16, AT);
7925 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7926 macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
7927 break;
7929 default:
7930 /* FIXME: Check if this is one of the itbl macros, since they
7931 are added dynamically. */
7932 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7933 break;
7935 if (mips_opts.noat)
7936 as_warn (_("Macro used $at after \".set noat\""));
7939 /* Implement macros in mips16 mode. */
7941 static void
7942 mips16_macro (struct mips_cl_insn *ip)
7944 int mask;
7945 int xreg, yreg, zreg, tmp;
7946 int icnt;
7947 expressionS expr1;
7948 int dbl;
7949 const char *s, *s2, *s3;
7951 mask = ip->insn_mo->mask;
7953 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7954 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7955 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7957 icnt = 0;
7959 expr1.X_op = O_constant;
7960 expr1.X_op_symbol = NULL;
7961 expr1.X_add_symbol = NULL;
7962 expr1.X_add_number = 1;
7964 dbl = 0;
7966 switch (mask)
7968 default:
7969 internalError ();
7971 case M_DDIV_3:
7972 dbl = 1;
7973 case M_DIV_3:
7974 s = "mflo";
7975 goto do_div3;
7976 case M_DREM_3:
7977 dbl = 1;
7978 case M_REM_3:
7979 s = "mfhi";
7980 do_div3:
7981 mips_emit_delays (TRUE);
7982 ++mips_opts.noreorder;
7983 mips_any_noreorder = 1;
7984 macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "0,x,y",
7985 xreg, yreg);
7986 expr1.X_add_number = 2;
7987 macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7988 macro_build (NULL, &icnt, NULL, "break", "6", 7);
7990 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7991 since that causes an overflow. We should do that as well,
7992 but I don't see how to do the comparisons without a temporary
7993 register. */
7994 --mips_opts.noreorder;
7995 macro_build (NULL, &icnt, NULL, s, "x", zreg);
7996 break;
7998 case M_DIVU_3:
7999 s = "divu";
8000 s2 = "mflo";
8001 goto do_divu3;
8002 case M_REMU_3:
8003 s = "divu";
8004 s2 = "mfhi";
8005 goto do_divu3;
8006 case M_DDIVU_3:
8007 s = "ddivu";
8008 s2 = "mflo";
8009 goto do_divu3;
8010 case M_DREMU_3:
8011 s = "ddivu";
8012 s2 = "mfhi";
8013 do_divu3:
8014 mips_emit_delays (TRUE);
8015 ++mips_opts.noreorder;
8016 mips_any_noreorder = 1;
8017 macro_build (NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
8018 expr1.X_add_number = 2;
8019 macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8020 macro_build (NULL, &icnt, NULL, "break", "6", 7);
8021 --mips_opts.noreorder;
8022 macro_build (NULL, &icnt, NULL, s2, "x", zreg);
8023 break;
8025 case M_DMUL:
8026 dbl = 1;
8027 case M_MUL:
8028 macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "x,y",
8029 xreg, yreg);
8030 macro_build (NULL, &icnt, NULL, "mflo", "x", zreg);
8031 return;
8033 case M_DSUBU_I:
8034 dbl = 1;
8035 goto do_subu;
8036 case M_SUBU_I:
8037 do_subu:
8038 if (imm_expr.X_op != O_constant)
8039 as_bad (_("Unsupported large constant"));
8040 imm_expr.X_add_number = -imm_expr.X_add_number;
8041 macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu", "y,x,4",
8042 yreg, xreg);
8043 break;
8045 case M_SUBU_I_2:
8046 if (imm_expr.X_op != O_constant)
8047 as_bad (_("Unsupported large constant"));
8048 imm_expr.X_add_number = -imm_expr.X_add_number;
8049 macro_build (NULL, &icnt, &imm_expr, "addiu", "x,k", xreg);
8050 break;
8052 case M_DSUBU_I_2:
8053 if (imm_expr.X_op != O_constant)
8054 as_bad (_("Unsupported large constant"));
8055 imm_expr.X_add_number = -imm_expr.X_add_number;
8056 macro_build (NULL, &icnt, &imm_expr, "daddiu", "y,j", yreg);
8057 break;
8059 case M_BEQ:
8060 s = "cmp";
8061 s2 = "bteqz";
8062 goto do_branch;
8063 case M_BNE:
8064 s = "cmp";
8065 s2 = "btnez";
8066 goto do_branch;
8067 case M_BLT:
8068 s = "slt";
8069 s2 = "btnez";
8070 goto do_branch;
8071 case M_BLTU:
8072 s = "sltu";
8073 s2 = "btnez";
8074 goto do_branch;
8075 case M_BLE:
8076 s = "slt";
8077 s2 = "bteqz";
8078 goto do_reverse_branch;
8079 case M_BLEU:
8080 s = "sltu";
8081 s2 = "bteqz";
8082 goto do_reverse_branch;
8083 case M_BGE:
8084 s = "slt";
8085 s2 = "bteqz";
8086 goto do_branch;
8087 case M_BGEU:
8088 s = "sltu";
8089 s2 = "bteqz";
8090 goto do_branch;
8091 case M_BGT:
8092 s = "slt";
8093 s2 = "btnez";
8094 goto do_reverse_branch;
8095 case M_BGTU:
8096 s = "sltu";
8097 s2 = "btnez";
8099 do_reverse_branch:
8100 tmp = xreg;
8101 xreg = yreg;
8102 yreg = tmp;
8104 do_branch:
8105 macro_build (NULL, &icnt, NULL, s, "x,y", xreg, yreg);
8106 macro_build (NULL, &icnt, &offset_expr, s2, "p");
8107 break;
8109 case M_BEQ_I:
8110 s = "cmpi";
8111 s2 = "bteqz";
8112 s3 = "x,U";
8113 goto do_branch_i;
8114 case M_BNE_I:
8115 s = "cmpi";
8116 s2 = "btnez";
8117 s3 = "x,U";
8118 goto do_branch_i;
8119 case M_BLT_I:
8120 s = "slti";
8121 s2 = "btnez";
8122 s3 = "x,8";
8123 goto do_branch_i;
8124 case M_BLTU_I:
8125 s = "sltiu";
8126 s2 = "btnez";
8127 s3 = "x,8";
8128 goto do_branch_i;
8129 case M_BLE_I:
8130 s = "slti";
8131 s2 = "btnez";
8132 s3 = "x,8";
8133 goto do_addone_branch_i;
8134 case M_BLEU_I:
8135 s = "sltiu";
8136 s2 = "btnez";
8137 s3 = "x,8";
8138 goto do_addone_branch_i;
8139 case M_BGE_I:
8140 s = "slti";
8141 s2 = "bteqz";
8142 s3 = "x,8";
8143 goto do_branch_i;
8144 case M_BGEU_I:
8145 s = "sltiu";
8146 s2 = "bteqz";
8147 s3 = "x,8";
8148 goto do_branch_i;
8149 case M_BGT_I:
8150 s = "slti";
8151 s2 = "bteqz";
8152 s3 = "x,8";
8153 goto do_addone_branch_i;
8154 case M_BGTU_I:
8155 s = "sltiu";
8156 s2 = "bteqz";
8157 s3 = "x,8";
8159 do_addone_branch_i:
8160 if (imm_expr.X_op != O_constant)
8161 as_bad (_("Unsupported large constant"));
8162 ++imm_expr.X_add_number;
8164 do_branch_i:
8165 macro_build (NULL, &icnt, &imm_expr, s, s3, xreg);
8166 macro_build (NULL, &icnt, &offset_expr, s2, "p");
8167 break;
8169 case M_ABS:
8170 expr1.X_add_number = 0;
8171 macro_build (NULL, &icnt, &expr1, "slti", "x,8", yreg);
8172 if (xreg != yreg)
8173 move_register (&icnt, xreg, yreg);
8174 expr1.X_add_number = 2;
8175 macro_build (NULL, &icnt, &expr1, "bteqz", "p");
8176 macro_build (NULL, &icnt, NULL, "neg", "x,w", xreg, xreg);
8180 /* For consistency checking, verify that all bits are specified either
8181 by the match/mask part of the instruction definition, or by the
8182 operand list. */
8183 static int
8184 validate_mips_insn (const struct mips_opcode *opc)
8186 const char *p = opc->args;
8187 char c;
8188 unsigned long used_bits = opc->mask;
8190 if ((used_bits & opc->match) != opc->match)
8192 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8193 opc->name, opc->args);
8194 return 0;
8196 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
8197 while (*p)
8198 switch (c = *p++)
8200 case ',': break;
8201 case '(': break;
8202 case ')': break;
8203 case '+':
8204 switch (c = *p++)
8206 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8207 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8208 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8209 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
8210 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
8211 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8212 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8213 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8214 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8215 case 'I': break;
8216 default:
8217 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8218 c, opc->name, opc->args);
8219 return 0;
8221 break;
8222 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8223 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8224 case 'A': break;
8225 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
8226 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8227 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8228 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8229 case 'F': break;
8230 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8231 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
8232 case 'I': break;
8233 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
8234 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8235 case 'L': break;
8236 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8237 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
8238 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8239 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8240 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8241 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8242 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8243 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8244 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8245 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8246 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8247 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8248 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8249 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8250 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8251 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8252 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8253 case 'f': break;
8254 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8255 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8256 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8257 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8258 case 'l': break;
8259 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8260 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8261 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8262 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8263 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8264 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8265 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8266 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8267 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8268 case 'x': break;
8269 case 'z': break;
8270 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
8271 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8272 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8273 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8274 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8275 case '[': break;
8276 case ']': break;
8277 default:
8278 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8279 c, opc->name, opc->args);
8280 return 0;
8282 #undef USE_BITS
8283 if (used_bits != 0xffffffff)
8285 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8286 ~used_bits & 0xffffffff, opc->name, opc->args);
8287 return 0;
8289 return 1;
8292 /* This routine assembles an instruction into its binary format. As a
8293 side effect, it sets one of the global variables imm_reloc or
8294 offset_reloc to the type of relocation to do if one of the operands
8295 is an address expression. */
8297 static void
8298 mips_ip (char *str, struct mips_cl_insn *ip)
8300 char *s;
8301 const char *args;
8302 char c = 0;
8303 struct mips_opcode *insn;
8304 char *argsStart;
8305 unsigned int regno;
8306 unsigned int lastregno = 0;
8307 unsigned int lastpos = 0;
8308 unsigned int limlo, limhi;
8309 char *s_reset;
8310 char save_c = 0;
8312 insn_error = NULL;
8314 /* If the instruction contains a '.', we first try to match an instruction
8315 including the '.'. Then we try again without the '.'. */
8316 insn = NULL;
8317 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8318 continue;
8320 /* If we stopped on whitespace, then replace the whitespace with null for
8321 the call to hash_find. Save the character we replaced just in case we
8322 have to re-parse the instruction. */
8323 if (ISSPACE (*s))
8325 save_c = *s;
8326 *s++ = '\0';
8329 insn = (struct mips_opcode *) hash_find (op_hash, str);
8331 /* If we didn't find the instruction in the opcode table, try again, but
8332 this time with just the instruction up to, but not including the
8333 first '.'. */
8334 if (insn == NULL)
8336 /* Restore the character we overwrite above (if any). */
8337 if (save_c)
8338 *(--s) = save_c;
8340 /* Scan up to the first '.' or whitespace. */
8341 for (s = str;
8342 *s != '\0' && *s != '.' && !ISSPACE (*s);
8343 ++s)
8344 continue;
8346 /* If we did not find a '.', then we can quit now. */
8347 if (*s != '.')
8349 insn_error = "unrecognized opcode";
8350 return;
8353 /* Lookup the instruction in the hash table. */
8354 *s++ = '\0';
8355 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8357 insn_error = "unrecognized opcode";
8358 return;
8362 argsStart = s;
8363 for (;;)
8365 bfd_boolean ok;
8367 assert (strcmp (insn->name, str) == 0);
8369 if (OPCODE_IS_MEMBER (insn,
8370 (mips_opts.isa
8371 | (file_ase_mips16 ? INSN_MIPS16 : 0)
8372 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8373 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8374 mips_opts.arch))
8375 ok = TRUE;
8376 else
8377 ok = FALSE;
8379 if (insn->pinfo != INSN_MACRO)
8381 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8382 ok = FALSE;
8385 if (! ok)
8387 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8388 && strcmp (insn->name, insn[1].name) == 0)
8390 ++insn;
8391 continue;
8393 else
8395 if (!insn_error)
8397 static char buf[100];
8398 sprintf (buf,
8399 _("opcode not supported on this processor: %s (%s)"),
8400 mips_cpu_info_from_arch (mips_opts.arch)->name,
8401 mips_cpu_info_from_isa (mips_opts.isa)->name);
8402 insn_error = buf;
8404 if (save_c)
8405 *(--s) = save_c;
8406 return;
8410 ip->insn_mo = insn;
8411 ip->insn_opcode = insn->match;
8412 insn_error = NULL;
8413 for (args = insn->args;; ++args)
8415 int is_mdmx;
8417 s += strspn (s, " \t");
8418 is_mdmx = 0;
8419 switch (*args)
8421 case '\0': /* end of args */
8422 if (*s == '\0')
8423 return;
8424 break;
8426 case ',':
8427 if (*s++ == *args)
8428 continue;
8429 s--;
8430 switch (*++args)
8432 case 'r':
8433 case 'v':
8434 ip->insn_opcode |= lastregno << OP_SH_RS;
8435 continue;
8437 case 'w':
8438 ip->insn_opcode |= lastregno << OP_SH_RT;
8439 continue;
8441 case 'W':
8442 ip->insn_opcode |= lastregno << OP_SH_FT;
8443 continue;
8445 case 'V':
8446 ip->insn_opcode |= lastregno << OP_SH_FS;
8447 continue;
8449 break;
8451 case '(':
8452 /* Handle optional base register.
8453 Either the base register is omitted or
8454 we must have a left paren. */
8455 /* This is dependent on the next operand specifier
8456 is a base register specification. */
8457 assert (args[1] == 'b' || args[1] == '5'
8458 || args[1] == '-' || args[1] == '4');
8459 if (*s == '\0')
8460 return;
8462 case ')': /* these must match exactly */
8463 case '[':
8464 case ']':
8465 if (*s++ == *args)
8466 continue;
8467 break;
8469 case '+': /* Opcode extension character. */
8470 switch (*++args)
8472 case 'A': /* ins/ext position, becomes LSB. */
8473 limlo = 0;
8474 limhi = 31;
8475 goto do_lsb;
8476 case 'E':
8477 limlo = 32;
8478 limhi = 63;
8479 goto do_lsb;
8480 do_lsb:
8481 my_getExpression (&imm_expr, s);
8482 check_absolute_expr (ip, &imm_expr);
8483 if ((unsigned long) imm_expr.X_add_number < limlo
8484 || (unsigned long) imm_expr.X_add_number > limhi)
8486 as_bad (_("Improper position (%lu)"),
8487 (unsigned long) imm_expr.X_add_number);
8488 imm_expr.X_add_number = limlo;
8490 lastpos = imm_expr.X_add_number;
8491 ip->insn_opcode |= (imm_expr.X_add_number
8492 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8493 imm_expr.X_op = O_absent;
8494 s = expr_end;
8495 continue;
8497 case 'B': /* ins size, becomes MSB. */
8498 limlo = 1;
8499 limhi = 32;
8500 goto do_msb;
8501 case 'F':
8502 limlo = 33;
8503 limhi = 64;
8504 goto do_msb;
8505 do_msb:
8506 my_getExpression (&imm_expr, s);
8507 check_absolute_expr (ip, &imm_expr);
8508 /* Check for negative input so that small negative numbers
8509 will not succeed incorrectly. The checks against
8510 (pos+size) transitively check "size" itself,
8511 assuming that "pos" is reasonable. */
8512 if ((long) imm_expr.X_add_number < 0
8513 || ((unsigned long) imm_expr.X_add_number
8514 + lastpos) < limlo
8515 || ((unsigned long) imm_expr.X_add_number
8516 + lastpos) > limhi)
8518 as_bad (_("Improper insert size (%lu, position %lu)"),
8519 (unsigned long) imm_expr.X_add_number,
8520 (unsigned long) lastpos);
8521 imm_expr.X_add_number = limlo - lastpos;
8523 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8524 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8525 imm_expr.X_op = O_absent;
8526 s = expr_end;
8527 continue;
8529 case 'C': /* ext size, becomes MSBD. */
8530 limlo = 1;
8531 limhi = 32;
8532 goto do_msbd;
8533 case 'G':
8534 limlo = 33;
8535 limhi = 64;
8536 goto do_msbd;
8537 case 'H':
8538 limlo = 33;
8539 limhi = 64;
8540 goto do_msbd;
8541 do_msbd:
8542 my_getExpression (&imm_expr, s);
8543 check_absolute_expr (ip, &imm_expr);
8544 /* Check for negative input so that small negative numbers
8545 will not succeed incorrectly. The checks against
8546 (pos+size) transitively check "size" itself,
8547 assuming that "pos" is reasonable. */
8548 if ((long) imm_expr.X_add_number < 0
8549 || ((unsigned long) imm_expr.X_add_number
8550 + lastpos) < limlo
8551 || ((unsigned long) imm_expr.X_add_number
8552 + lastpos) > limhi)
8554 as_bad (_("Improper extract size (%lu, position %lu)"),
8555 (unsigned long) imm_expr.X_add_number,
8556 (unsigned long) lastpos);
8557 imm_expr.X_add_number = limlo - lastpos;
8559 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8560 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8561 imm_expr.X_op = O_absent;
8562 s = expr_end;
8563 continue;
8565 case 'D':
8566 /* +D is for disassembly only; never match. */
8567 break;
8569 case 'I':
8570 /* "+I" is like "I", except that imm2_expr is used. */
8571 my_getExpression (&imm2_expr, s);
8572 if (imm2_expr.X_op != O_big
8573 && imm2_expr.X_op != O_constant)
8574 insn_error = _("absolute expression required");
8575 normalize_constant_expr (&imm2_expr);
8576 s = expr_end;
8577 continue;
8579 default:
8580 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8581 *args, insn->name, insn->args);
8582 /* Further processing is fruitless. */
8583 return;
8585 break;
8587 case '<': /* must be at least one digit */
8589 * According to the manual, if the shift amount is greater
8590 * than 31 or less than 0, then the shift amount should be
8591 * mod 32. In reality the mips assembler issues an error.
8592 * We issue a warning and mask out all but the low 5 bits.
8594 my_getExpression (&imm_expr, s);
8595 check_absolute_expr (ip, &imm_expr);
8596 if ((unsigned long) imm_expr.X_add_number > 31)
8598 as_warn (_("Improper shift amount (%lu)"),
8599 (unsigned long) imm_expr.X_add_number);
8600 imm_expr.X_add_number &= OP_MASK_SHAMT;
8602 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8603 imm_expr.X_op = O_absent;
8604 s = expr_end;
8605 continue;
8607 case '>': /* shift amount minus 32 */
8608 my_getExpression (&imm_expr, s);
8609 check_absolute_expr (ip, &imm_expr);
8610 if ((unsigned long) imm_expr.X_add_number < 32
8611 || (unsigned long) imm_expr.X_add_number > 63)
8612 break;
8613 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8614 imm_expr.X_op = O_absent;
8615 s = expr_end;
8616 continue;
8618 case 'k': /* cache code */
8619 case 'h': /* prefx code */
8620 my_getExpression (&imm_expr, s);
8621 check_absolute_expr (ip, &imm_expr);
8622 if ((unsigned long) imm_expr.X_add_number > 31)
8624 as_warn (_("Invalid value for `%s' (%lu)"),
8625 ip->insn_mo->name,
8626 (unsigned long) imm_expr.X_add_number);
8627 imm_expr.X_add_number &= 0x1f;
8629 if (*args == 'k')
8630 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8631 else
8632 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8633 imm_expr.X_op = O_absent;
8634 s = expr_end;
8635 continue;
8637 case 'c': /* break code */
8638 my_getExpression (&imm_expr, s);
8639 check_absolute_expr (ip, &imm_expr);
8640 if ((unsigned long) imm_expr.X_add_number > 1023)
8642 as_warn (_("Illegal break code (%lu)"),
8643 (unsigned long) imm_expr.X_add_number);
8644 imm_expr.X_add_number &= OP_MASK_CODE;
8646 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8647 imm_expr.X_op = O_absent;
8648 s = expr_end;
8649 continue;
8651 case 'q': /* lower break code */
8652 my_getExpression (&imm_expr, s);
8653 check_absolute_expr (ip, &imm_expr);
8654 if ((unsigned long) imm_expr.X_add_number > 1023)
8656 as_warn (_("Illegal lower break code (%lu)"),
8657 (unsigned long) imm_expr.X_add_number);
8658 imm_expr.X_add_number &= OP_MASK_CODE2;
8660 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8661 imm_expr.X_op = O_absent;
8662 s = expr_end;
8663 continue;
8665 case 'B': /* 20-bit syscall/break code. */
8666 my_getExpression (&imm_expr, s);
8667 check_absolute_expr (ip, &imm_expr);
8668 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8669 as_warn (_("Illegal 20-bit code (%lu)"),
8670 (unsigned long) imm_expr.X_add_number);
8671 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8672 imm_expr.X_op = O_absent;
8673 s = expr_end;
8674 continue;
8676 case 'C': /* Coprocessor code */
8677 my_getExpression (&imm_expr, s);
8678 check_absolute_expr (ip, &imm_expr);
8679 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8681 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8682 (unsigned long) imm_expr.X_add_number);
8683 imm_expr.X_add_number &= ((1 << 25) - 1);
8685 ip->insn_opcode |= imm_expr.X_add_number;
8686 imm_expr.X_op = O_absent;
8687 s = expr_end;
8688 continue;
8690 case 'J': /* 19-bit wait code. */
8691 my_getExpression (&imm_expr, s);
8692 check_absolute_expr (ip, &imm_expr);
8693 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8694 as_warn (_("Illegal 19-bit code (%lu)"),
8695 (unsigned long) imm_expr.X_add_number);
8696 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8697 imm_expr.X_op = O_absent;
8698 s = expr_end;
8699 continue;
8701 case 'P': /* Performance register */
8702 my_getExpression (&imm_expr, s);
8703 check_absolute_expr (ip, &imm_expr);
8704 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8706 as_warn (_("Invalid performance register (%lu)"),
8707 (unsigned long) imm_expr.X_add_number);
8708 imm_expr.X_add_number &= OP_MASK_PERFREG;
8710 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8711 imm_expr.X_op = O_absent;
8712 s = expr_end;
8713 continue;
8715 case 'b': /* base register */
8716 case 'd': /* destination register */
8717 case 's': /* source register */
8718 case 't': /* target register */
8719 case 'r': /* both target and source */
8720 case 'v': /* both dest and source */
8721 case 'w': /* both dest and target */
8722 case 'E': /* coprocessor target register */
8723 case 'G': /* coprocessor destination register */
8724 case 'K': /* 'rdhwr' destination register */
8725 case 'x': /* ignore register name */
8726 case 'z': /* must be zero register */
8727 case 'U': /* destination register (clo/clz). */
8728 s_reset = s;
8729 if (s[0] == '$')
8732 if (ISDIGIT (s[1]))
8734 ++s;
8735 regno = 0;
8738 regno *= 10;
8739 regno += *s - '0';
8740 ++s;
8742 while (ISDIGIT (*s));
8743 if (regno > 31)
8744 as_bad (_("Invalid register number (%d)"), regno);
8746 else if (*args == 'E' || *args == 'G' || *args == 'K')
8747 goto notreg;
8748 else
8750 if (s[1] == 'r' && s[2] == 'a')
8752 s += 3;
8753 regno = RA;
8755 else if (s[1] == 'f' && s[2] == 'p')
8757 s += 3;
8758 regno = FP;
8760 else if (s[1] == 's' && s[2] == 'p')
8762 s += 3;
8763 regno = SP;
8765 else if (s[1] == 'g' && s[2] == 'p')
8767 s += 3;
8768 regno = GP;
8770 else if (s[1] == 'a' && s[2] == 't')
8772 s += 3;
8773 regno = AT;
8775 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8777 s += 4;
8778 regno = KT0;
8780 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8782 s += 4;
8783 regno = KT1;
8785 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8787 s += 5;
8788 regno = ZERO;
8790 else if (itbl_have_entries)
8792 char *p, *n;
8793 unsigned long r;
8795 p = s + 1; /* advance past '$' */
8796 n = itbl_get_field (&p); /* n is name */
8798 /* See if this is a register defined in an
8799 itbl entry. */
8800 if (itbl_get_reg_val (n, &r))
8802 /* Get_field advances to the start of
8803 the next field, so we need to back
8804 rack to the end of the last field. */
8805 if (p)
8806 s = p - 1;
8807 else
8808 s = strchr (s, '\0');
8809 regno = r;
8811 else
8812 goto notreg;
8814 else
8815 goto notreg;
8817 if (regno == AT
8818 && ! mips_opts.noat
8819 && *args != 'E'
8820 && *args != 'G'
8821 && *args != 'K')
8822 as_warn (_("Used $at without \".set noat\""));
8823 c = *args;
8824 if (*s == ' ')
8825 ++s;
8826 if (args[1] != *s)
8828 if (c == 'r' || c == 'v' || c == 'w')
8830 regno = lastregno;
8831 s = s_reset;
8832 ++args;
8835 /* 'z' only matches $0. */
8836 if (c == 'z' && regno != 0)
8837 break;
8839 /* Now that we have assembled one operand, we use the args string
8840 * to figure out where it goes in the instruction. */
8841 switch (c)
8843 case 'r':
8844 case 's':
8845 case 'v':
8846 case 'b':
8847 ip->insn_opcode |= regno << OP_SH_RS;
8848 break;
8849 case 'd':
8850 case 'G':
8851 case 'K':
8852 ip->insn_opcode |= regno << OP_SH_RD;
8853 break;
8854 case 'U':
8855 ip->insn_opcode |= regno << OP_SH_RD;
8856 ip->insn_opcode |= regno << OP_SH_RT;
8857 break;
8858 case 'w':
8859 case 't':
8860 case 'E':
8861 ip->insn_opcode |= regno << OP_SH_RT;
8862 break;
8863 case 'x':
8864 /* This case exists because on the r3000 trunc
8865 expands into a macro which requires a gp
8866 register. On the r6000 or r4000 it is
8867 assembled into a single instruction which
8868 ignores the register. Thus the insn version
8869 is MIPS_ISA2 and uses 'x', and the macro
8870 version is MIPS_ISA1 and uses 't'. */
8871 break;
8872 case 'z':
8873 /* This case is for the div instruction, which
8874 acts differently if the destination argument
8875 is $0. This only matches $0, and is checked
8876 outside the switch. */
8877 break;
8878 case 'D':
8879 /* Itbl operand; not yet implemented. FIXME ?? */
8880 break;
8881 /* What about all other operands like 'i', which
8882 can be specified in the opcode table? */
8884 lastregno = regno;
8885 continue;
8887 notreg:
8888 switch (*args++)
8890 case 'r':
8891 case 'v':
8892 ip->insn_opcode |= lastregno << OP_SH_RS;
8893 continue;
8894 case 'w':
8895 ip->insn_opcode |= lastregno << OP_SH_RT;
8896 continue;
8898 break;
8900 case 'O': /* MDMX alignment immediate constant. */
8901 my_getExpression (&imm_expr, s);
8902 check_absolute_expr (ip, &imm_expr);
8903 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8905 as_warn ("Improper align amount (%ld), using low bits",
8906 (long) imm_expr.X_add_number);
8907 imm_expr.X_add_number &= OP_MASK_ALN;
8909 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8910 imm_expr.X_op = O_absent;
8911 s = expr_end;
8912 continue;
8914 case 'Q': /* MDMX vector, element sel, or const. */
8915 if (s[0] != '$')
8917 /* MDMX Immediate. */
8918 my_getExpression (&imm_expr, s);
8919 check_absolute_expr (ip, &imm_expr);
8920 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8922 as_warn (_("Invalid MDMX Immediate (%ld)"),
8923 (long) imm_expr.X_add_number);
8924 imm_expr.X_add_number &= OP_MASK_FT;
8926 imm_expr.X_add_number &= OP_MASK_FT;
8927 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8928 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8929 else
8930 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8931 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8932 imm_expr.X_op = O_absent;
8933 s = expr_end;
8934 continue;
8936 /* Not MDMX Immediate. Fall through. */
8937 case 'X': /* MDMX destination register. */
8938 case 'Y': /* MDMX source register. */
8939 case 'Z': /* MDMX target register. */
8940 is_mdmx = 1;
8941 case 'D': /* floating point destination register */
8942 case 'S': /* floating point source register */
8943 case 'T': /* floating point target register */
8944 case 'R': /* floating point source register */
8945 case 'V':
8946 case 'W':
8947 s_reset = s;
8948 /* Accept $fN for FP and MDMX register numbers, and in
8949 addition accept $vN for MDMX register numbers. */
8950 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8951 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8952 && ISDIGIT (s[2])))
8954 s += 2;
8955 regno = 0;
8958 regno *= 10;
8959 regno += *s - '0';
8960 ++s;
8962 while (ISDIGIT (*s));
8964 if (regno > 31)
8965 as_bad (_("Invalid float register number (%d)"), regno);
8967 if ((regno & 1) != 0
8968 && HAVE_32BIT_FPRS
8969 && ! (strcmp (str, "mtc1") == 0
8970 || strcmp (str, "mfc1") == 0
8971 || strcmp (str, "lwc1") == 0
8972 || strcmp (str, "swc1") == 0
8973 || strcmp (str, "l.s") == 0
8974 || strcmp (str, "s.s") == 0))
8975 as_warn (_("Float register should be even, was %d"),
8976 regno);
8978 c = *args;
8979 if (*s == ' ')
8980 ++s;
8981 if (args[1] != *s)
8983 if (c == 'V' || c == 'W')
8985 regno = lastregno;
8986 s = s_reset;
8987 ++args;
8990 switch (c)
8992 case 'D':
8993 case 'X':
8994 ip->insn_opcode |= regno << OP_SH_FD;
8995 break;
8996 case 'V':
8997 case 'S':
8998 case 'Y':
8999 ip->insn_opcode |= regno << OP_SH_FS;
9000 break;
9001 case 'Q':
9002 /* This is like 'Z', but also needs to fix the MDMX
9003 vector/scalar select bits. Note that the
9004 scalar immediate case is handled above. */
9005 if (*s == '[')
9007 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9008 int max_el = (is_qh ? 3 : 7);
9009 s++;
9010 my_getExpression(&imm_expr, s);
9011 check_absolute_expr (ip, &imm_expr);
9012 s = expr_end;
9013 if (imm_expr.X_add_number > max_el)
9014 as_bad(_("Bad element selector %ld"),
9015 (long) imm_expr.X_add_number);
9016 imm_expr.X_add_number &= max_el;
9017 ip->insn_opcode |= (imm_expr.X_add_number
9018 << (OP_SH_VSEL +
9019 (is_qh ? 2 : 1)));
9020 if (*s != ']')
9021 as_warn(_("Expecting ']' found '%s'"), s);
9022 else
9023 s++;
9025 else
9027 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9028 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9029 << OP_SH_VSEL);
9030 else
9031 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9032 OP_SH_VSEL);
9034 /* Fall through */
9035 case 'W':
9036 case 'T':
9037 case 'Z':
9038 ip->insn_opcode |= regno << OP_SH_FT;
9039 break;
9040 case 'R':
9041 ip->insn_opcode |= regno << OP_SH_FR;
9042 break;
9044 lastregno = regno;
9045 continue;
9048 switch (*args++)
9050 case 'V':
9051 ip->insn_opcode |= lastregno << OP_SH_FS;
9052 continue;
9053 case 'W':
9054 ip->insn_opcode |= lastregno << OP_SH_FT;
9055 continue;
9057 break;
9059 case 'I':
9060 my_getExpression (&imm_expr, s);
9061 if (imm_expr.X_op != O_big
9062 && imm_expr.X_op != O_constant)
9063 insn_error = _("absolute expression required");
9064 normalize_constant_expr (&imm_expr);
9065 s = expr_end;
9066 continue;
9068 case 'A':
9069 my_getExpression (&offset_expr, s);
9070 *imm_reloc = BFD_RELOC_32;
9071 s = expr_end;
9072 continue;
9074 case 'F':
9075 case 'L':
9076 case 'f':
9077 case 'l':
9079 int f64;
9080 int using_gprs;
9081 char *save_in;
9082 char *err;
9083 unsigned char temp[8];
9084 int len;
9085 unsigned int length;
9086 segT seg;
9087 subsegT subseg;
9088 char *p;
9090 /* These only appear as the last operand in an
9091 instruction, and every instruction that accepts
9092 them in any variant accepts them in all variants.
9093 This means we don't have to worry about backing out
9094 any changes if the instruction does not match.
9096 The difference between them is the size of the
9097 floating point constant and where it goes. For 'F'
9098 and 'L' the constant is 64 bits; for 'f' and 'l' it
9099 is 32 bits. Where the constant is placed is based
9100 on how the MIPS assembler does things:
9101 F -- .rdata
9102 L -- .lit8
9103 f -- immediate value
9104 l -- .lit4
9106 The .lit4 and .lit8 sections are only used if
9107 permitted by the -G argument.
9109 When generating embedded PIC code, we use the
9110 .lit8 section but not the .lit4 section (we can do
9111 .lit4 inline easily; we need to put .lit8
9112 somewhere in the data segment, and using .lit8
9113 permits the linker to eventually combine identical
9114 .lit8 entries).
9116 The code below needs to know whether the target register
9117 is 32 or 64 bits wide. It relies on the fact 'f' and
9118 'F' are used with GPR-based instructions and 'l' and
9119 'L' are used with FPR-based instructions. */
9121 f64 = *args == 'F' || *args == 'L';
9122 using_gprs = *args == 'F' || *args == 'f';
9124 save_in = input_line_pointer;
9125 input_line_pointer = s;
9126 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9127 length = len;
9128 s = input_line_pointer;
9129 input_line_pointer = save_in;
9130 if (err != NULL && *err != '\0')
9132 as_bad (_("Bad floating point constant: %s"), err);
9133 memset (temp, '\0', sizeof temp);
9134 length = f64 ? 8 : 4;
9137 assert (length == (unsigned) (f64 ? 8 : 4));
9139 if (*args == 'f'
9140 || (*args == 'l'
9141 && (! USE_GLOBAL_POINTER_OPT
9142 || mips_pic == EMBEDDED_PIC
9143 || g_switch_value < 4
9144 || (temp[0] == 0 && temp[1] == 0)
9145 || (temp[2] == 0 && temp[3] == 0))))
9147 imm_expr.X_op = O_constant;
9148 if (! target_big_endian)
9149 imm_expr.X_add_number = bfd_getl32 (temp);
9150 else
9151 imm_expr.X_add_number = bfd_getb32 (temp);
9153 else if (length > 4
9154 && ! mips_disable_float_construction
9155 /* Constants can only be constructed in GPRs and
9156 copied to FPRs if the GPRs are at least as wide
9157 as the FPRs. Force the constant into memory if
9158 we are using 64-bit FPRs but the GPRs are only
9159 32 bits wide. */
9160 && (using_gprs
9161 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
9162 && ((temp[0] == 0 && temp[1] == 0)
9163 || (temp[2] == 0 && temp[3] == 0))
9164 && ((temp[4] == 0 && temp[5] == 0)
9165 || (temp[6] == 0 && temp[7] == 0)))
9167 /* The value is simple enough to load with a couple of
9168 instructions. If using 32-bit registers, set
9169 imm_expr to the high order 32 bits and offset_expr to
9170 the low order 32 bits. Otherwise, set imm_expr to
9171 the entire 64 bit constant. */
9172 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
9174 imm_expr.X_op = O_constant;
9175 offset_expr.X_op = O_constant;
9176 if (! target_big_endian)
9178 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9179 offset_expr.X_add_number = bfd_getl32 (temp);
9181 else
9183 imm_expr.X_add_number = bfd_getb32 (temp);
9184 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9186 if (offset_expr.X_add_number == 0)
9187 offset_expr.X_op = O_absent;
9189 else if (sizeof (imm_expr.X_add_number) > 4)
9191 imm_expr.X_op = O_constant;
9192 if (! target_big_endian)
9193 imm_expr.X_add_number = bfd_getl64 (temp);
9194 else
9195 imm_expr.X_add_number = bfd_getb64 (temp);
9197 else
9199 imm_expr.X_op = O_big;
9200 imm_expr.X_add_number = 4;
9201 if (! target_big_endian)
9203 generic_bignum[0] = bfd_getl16 (temp);
9204 generic_bignum[1] = bfd_getl16 (temp + 2);
9205 generic_bignum[2] = bfd_getl16 (temp + 4);
9206 generic_bignum[3] = bfd_getl16 (temp + 6);
9208 else
9210 generic_bignum[0] = bfd_getb16 (temp + 6);
9211 generic_bignum[1] = bfd_getb16 (temp + 4);
9212 generic_bignum[2] = bfd_getb16 (temp + 2);
9213 generic_bignum[3] = bfd_getb16 (temp);
9217 else
9219 const char *newname;
9220 segT new_seg;
9222 /* Switch to the right section. */
9223 seg = now_seg;
9224 subseg = now_subseg;
9225 switch (*args)
9227 default: /* unused default case avoids warnings. */
9228 case 'L':
9229 newname = RDATA_SECTION_NAME;
9230 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9231 || mips_pic == EMBEDDED_PIC)
9232 newname = ".lit8";
9233 break;
9234 case 'F':
9235 if (mips_pic == EMBEDDED_PIC)
9236 newname = ".lit8";
9237 else
9238 newname = RDATA_SECTION_NAME;
9239 break;
9240 case 'l':
9241 assert (!USE_GLOBAL_POINTER_OPT
9242 || g_switch_value >= 4);
9243 newname = ".lit4";
9244 break;
9246 new_seg = subseg_new (newname, (subsegT) 0);
9247 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9248 bfd_set_section_flags (stdoutput, new_seg,
9249 (SEC_ALLOC
9250 | SEC_LOAD
9251 | SEC_READONLY
9252 | SEC_DATA));
9253 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9254 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9255 && strcmp (TARGET_OS, "elf") != 0)
9256 record_alignment (new_seg, 4);
9257 else
9258 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9259 if (seg == now_seg)
9260 as_bad (_("Can't use floating point insn in this section"));
9262 /* Set the argument to the current address in the
9263 section. */
9264 offset_expr.X_op = O_symbol;
9265 offset_expr.X_add_symbol =
9266 symbol_new ("L0\001", now_seg,
9267 (valueT) frag_now_fix (), frag_now);
9268 offset_expr.X_add_number = 0;
9270 /* Put the floating point number into the section. */
9271 p = frag_more ((int) length);
9272 memcpy (p, temp, length);
9274 /* Switch back to the original section. */
9275 subseg_set (seg, subseg);
9278 continue;
9280 case 'i': /* 16 bit unsigned immediate */
9281 case 'j': /* 16 bit signed immediate */
9282 *imm_reloc = BFD_RELOC_LO16;
9283 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9285 int more;
9286 offsetT minval, maxval;
9288 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9289 && strcmp (insn->name, insn[1].name) == 0);
9291 /* If the expression was written as an unsigned number,
9292 only treat it as signed if there are no more
9293 alternatives. */
9294 if (more
9295 && *args == 'j'
9296 && sizeof (imm_expr.X_add_number) <= 4
9297 && imm_expr.X_op == O_constant
9298 && imm_expr.X_add_number < 0
9299 && imm_expr.X_unsigned
9300 && HAVE_64BIT_GPRS)
9301 break;
9303 /* For compatibility with older assemblers, we accept
9304 0x8000-0xffff as signed 16-bit numbers when only
9305 signed numbers are allowed. */
9306 if (*args == 'i')
9307 minval = 0, maxval = 0xffff;
9308 else if (more)
9309 minval = -0x8000, maxval = 0x7fff;
9310 else
9311 minval = -0x8000, maxval = 0xffff;
9313 if (imm_expr.X_op != O_constant
9314 || imm_expr.X_add_number < minval
9315 || imm_expr.X_add_number > maxval)
9317 if (more)
9318 break;
9319 if (imm_expr.X_op == O_constant
9320 || imm_expr.X_op == O_big)
9321 as_bad (_("expression out of range"));
9324 s = expr_end;
9325 continue;
9327 case 'o': /* 16 bit offset */
9328 /* Check whether there is only a single bracketed expression
9329 left. If so, it must be the base register and the
9330 constant must be zero. */
9331 if (*s == '(' && strchr (s + 1, '(') == 0)
9333 offset_expr.X_op = O_constant;
9334 offset_expr.X_add_number = 0;
9335 continue;
9338 /* If this value won't fit into a 16 bit offset, then go
9339 find a macro that will generate the 32 bit offset
9340 code pattern. */
9341 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9342 && (offset_expr.X_op != O_constant
9343 || offset_expr.X_add_number >= 0x8000
9344 || offset_expr.X_add_number < -0x8000))
9345 break;
9347 s = expr_end;
9348 continue;
9350 case 'p': /* pc relative offset */
9351 *offset_reloc = BFD_RELOC_16_PCREL_S2;
9352 my_getExpression (&offset_expr, s);
9353 s = expr_end;
9354 continue;
9356 case 'u': /* upper 16 bits */
9357 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9358 && imm_expr.X_op == O_constant
9359 && (imm_expr.X_add_number < 0
9360 || imm_expr.X_add_number >= 0x10000))
9361 as_bad (_("lui expression not in range 0..65535"));
9362 s = expr_end;
9363 continue;
9365 case 'a': /* 26 bit address */
9366 my_getExpression (&offset_expr, s);
9367 s = expr_end;
9368 *offset_reloc = BFD_RELOC_MIPS_JMP;
9369 continue;
9371 case 'N': /* 3 bit branch condition code */
9372 case 'M': /* 3 bit compare condition code */
9373 if (strncmp (s, "$fcc", 4) != 0)
9374 break;
9375 s += 4;
9376 regno = 0;
9379 regno *= 10;
9380 regno += *s - '0';
9381 ++s;
9383 while (ISDIGIT (*s));
9384 if (regno > 7)
9385 as_bad (_("Invalid condition code register $fcc%d"), regno);
9386 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9387 || strcmp(str + strlen(str) - 5, "any2f") == 0
9388 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9389 && (regno & 1) != 0)
9390 as_warn(_("Condition code register should be even for %s, was %d"),
9391 str, regno);
9392 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9393 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9394 && (regno & 3) != 0)
9395 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9396 str, regno);
9397 if (*args == 'N')
9398 ip->insn_opcode |= regno << OP_SH_BCC;
9399 else
9400 ip->insn_opcode |= regno << OP_SH_CCC;
9401 continue;
9403 case 'H':
9404 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9405 s += 2;
9406 if (ISDIGIT (*s))
9408 c = 0;
9411 c *= 10;
9412 c += *s - '0';
9413 ++s;
9415 while (ISDIGIT (*s));
9417 else
9418 c = 8; /* Invalid sel value. */
9420 if (c > 7)
9421 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9422 ip->insn_opcode |= c;
9423 continue;
9425 case 'e':
9426 /* Must be at least one digit. */
9427 my_getExpression (&imm_expr, s);
9428 check_absolute_expr (ip, &imm_expr);
9430 if ((unsigned long) imm_expr.X_add_number
9431 > (unsigned long) OP_MASK_VECBYTE)
9433 as_bad (_("bad byte vector index (%ld)"),
9434 (long) imm_expr.X_add_number);
9435 imm_expr.X_add_number = 0;
9438 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9439 imm_expr.X_op = O_absent;
9440 s = expr_end;
9441 continue;
9443 case '%':
9444 my_getExpression (&imm_expr, s);
9445 check_absolute_expr (ip, &imm_expr);
9447 if ((unsigned long) imm_expr.X_add_number
9448 > (unsigned long) OP_MASK_VECALIGN)
9450 as_bad (_("bad byte vector index (%ld)"),
9451 (long) imm_expr.X_add_number);
9452 imm_expr.X_add_number = 0;
9455 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9456 imm_expr.X_op = O_absent;
9457 s = expr_end;
9458 continue;
9460 default:
9461 as_bad (_("bad char = '%c'\n"), *args);
9462 internalError ();
9464 break;
9466 /* Args don't match. */
9467 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9468 !strcmp (insn->name, insn[1].name))
9470 ++insn;
9471 s = argsStart;
9472 insn_error = _("illegal operands");
9473 continue;
9475 if (save_c)
9476 *(--s) = save_c;
9477 insn_error = _("illegal operands");
9478 return;
9482 /* This routine assembles an instruction into its binary format when
9483 assembling for the mips16. As a side effect, it sets one of the
9484 global variables imm_reloc or offset_reloc to the type of
9485 relocation to do if one of the operands is an address expression.
9486 It also sets mips16_small and mips16_ext if the user explicitly
9487 requested a small or extended instruction. */
9489 static void
9490 mips16_ip (char *str, struct mips_cl_insn *ip)
9492 char *s;
9493 const char *args;
9494 struct mips_opcode *insn;
9495 char *argsstart;
9496 unsigned int regno;
9497 unsigned int lastregno = 0;
9498 char *s_reset;
9500 insn_error = NULL;
9502 mips16_small = FALSE;
9503 mips16_ext = FALSE;
9505 for (s = str; ISLOWER (*s); ++s)
9507 switch (*s)
9509 case '\0':
9510 break;
9512 case ' ':
9513 *s++ = '\0';
9514 break;
9516 case '.':
9517 if (s[1] == 't' && s[2] == ' ')
9519 *s = '\0';
9520 mips16_small = TRUE;
9521 s += 3;
9522 break;
9524 else if (s[1] == 'e' && s[2] == ' ')
9526 *s = '\0';
9527 mips16_ext = TRUE;
9528 s += 3;
9529 break;
9531 /* Fall through. */
9532 default:
9533 insn_error = _("unknown opcode");
9534 return;
9537 if (mips_opts.noautoextend && ! mips16_ext)
9538 mips16_small = TRUE;
9540 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9542 insn_error = _("unrecognized opcode");
9543 return;
9546 argsstart = s;
9547 for (;;)
9549 assert (strcmp (insn->name, str) == 0);
9551 ip->insn_mo = insn;
9552 ip->insn_opcode = insn->match;
9553 ip->use_extend = FALSE;
9554 imm_expr.X_op = O_absent;
9555 imm_reloc[0] = BFD_RELOC_UNUSED;
9556 imm_reloc[1] = BFD_RELOC_UNUSED;
9557 imm_reloc[2] = BFD_RELOC_UNUSED;
9558 imm2_expr.X_op = O_absent;
9559 offset_expr.X_op = O_absent;
9560 offset_reloc[0] = BFD_RELOC_UNUSED;
9561 offset_reloc[1] = BFD_RELOC_UNUSED;
9562 offset_reloc[2] = BFD_RELOC_UNUSED;
9563 for (args = insn->args; 1; ++args)
9565 int c;
9567 if (*s == ' ')
9568 ++s;
9570 /* In this switch statement we call break if we did not find
9571 a match, continue if we did find a match, or return if we
9572 are done. */
9574 c = *args;
9575 switch (c)
9577 case '\0':
9578 if (*s == '\0')
9580 /* Stuff the immediate value in now, if we can. */
9581 if (imm_expr.X_op == O_constant
9582 && *imm_reloc > BFD_RELOC_UNUSED
9583 && insn->pinfo != INSN_MACRO)
9585 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9586 imm_expr.X_add_number, TRUE, mips16_small,
9587 mips16_ext, &ip->insn_opcode,
9588 &ip->use_extend, &ip->extend);
9589 imm_expr.X_op = O_absent;
9590 *imm_reloc = BFD_RELOC_UNUSED;
9593 return;
9595 break;
9597 case ',':
9598 if (*s++ == c)
9599 continue;
9600 s--;
9601 switch (*++args)
9603 case 'v':
9604 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9605 continue;
9606 case 'w':
9607 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9608 continue;
9610 break;
9612 case '(':
9613 case ')':
9614 if (*s++ == c)
9615 continue;
9616 break;
9618 case 'v':
9619 case 'w':
9620 if (s[0] != '$')
9622 if (c == 'v')
9623 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9624 else
9625 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9626 ++args;
9627 continue;
9629 /* Fall through. */
9630 case 'x':
9631 case 'y':
9632 case 'z':
9633 case 'Z':
9634 case '0':
9635 case 'S':
9636 case 'R':
9637 case 'X':
9638 case 'Y':
9639 if (s[0] != '$')
9640 break;
9641 s_reset = s;
9642 if (ISDIGIT (s[1]))
9644 ++s;
9645 regno = 0;
9648 regno *= 10;
9649 regno += *s - '0';
9650 ++s;
9652 while (ISDIGIT (*s));
9653 if (regno > 31)
9655 as_bad (_("invalid register number (%d)"), regno);
9656 regno = 2;
9659 else
9661 if (s[1] == 'r' && s[2] == 'a')
9663 s += 3;
9664 regno = RA;
9666 else if (s[1] == 'f' && s[2] == 'p')
9668 s += 3;
9669 regno = FP;
9671 else if (s[1] == 's' && s[2] == 'p')
9673 s += 3;
9674 regno = SP;
9676 else if (s[1] == 'g' && s[2] == 'p')
9678 s += 3;
9679 regno = GP;
9681 else if (s[1] == 'a' && s[2] == 't')
9683 s += 3;
9684 regno = AT;
9686 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9688 s += 4;
9689 regno = KT0;
9691 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9693 s += 4;
9694 regno = KT1;
9696 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9698 s += 5;
9699 regno = ZERO;
9701 else
9702 break;
9705 if (*s == ' ')
9706 ++s;
9707 if (args[1] != *s)
9709 if (c == 'v' || c == 'w')
9711 regno = mips16_to_32_reg_map[lastregno];
9712 s = s_reset;
9713 ++args;
9717 switch (c)
9719 case 'x':
9720 case 'y':
9721 case 'z':
9722 case 'v':
9723 case 'w':
9724 case 'Z':
9725 regno = mips32_to_16_reg_map[regno];
9726 break;
9728 case '0':
9729 if (regno != 0)
9730 regno = ILLEGAL_REG;
9731 break;
9733 case 'S':
9734 if (regno != SP)
9735 regno = ILLEGAL_REG;
9736 break;
9738 case 'R':
9739 if (regno != RA)
9740 regno = ILLEGAL_REG;
9741 break;
9743 case 'X':
9744 case 'Y':
9745 if (regno == AT && ! mips_opts.noat)
9746 as_warn (_("used $at without \".set noat\""));
9747 break;
9749 default:
9750 internalError ();
9753 if (regno == ILLEGAL_REG)
9754 break;
9756 switch (c)
9758 case 'x':
9759 case 'v':
9760 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9761 break;
9762 case 'y':
9763 case 'w':
9764 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9765 break;
9766 case 'z':
9767 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9768 break;
9769 case 'Z':
9770 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9771 case '0':
9772 case 'S':
9773 case 'R':
9774 break;
9775 case 'X':
9776 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9777 break;
9778 case 'Y':
9779 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9780 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9781 break;
9782 default:
9783 internalError ();
9786 lastregno = regno;
9787 continue;
9789 case 'P':
9790 if (strncmp (s, "$pc", 3) == 0)
9792 s += 3;
9793 continue;
9795 break;
9797 case '<':
9798 case '>':
9799 case '[':
9800 case ']':
9801 case '4':
9802 case '5':
9803 case 'H':
9804 case 'W':
9805 case 'D':
9806 case 'j':
9807 case '8':
9808 case 'V':
9809 case 'C':
9810 case 'U':
9811 case 'k':
9812 case 'K':
9813 if (s[0] == '%'
9814 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9816 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9817 and generate the appropriate reloc. If the text
9818 inside %gprel is not a symbol name with an
9819 optional offset, then we generate a normal reloc
9820 and will probably fail later. */
9821 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9822 if (imm_expr.X_op == O_symbol)
9824 mips16_ext = TRUE;
9825 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9826 s = expr_end;
9827 ip->use_extend = TRUE;
9828 ip->extend = 0;
9829 continue;
9832 else
9834 /* Just pick up a normal expression. */
9835 my_getExpression (&imm_expr, s);
9838 if (imm_expr.X_op == O_register)
9840 /* What we thought was an expression turned out to
9841 be a register. */
9843 if (s[0] == '(' && args[1] == '(')
9845 /* It looks like the expression was omitted
9846 before a register indirection, which means
9847 that the expression is implicitly zero. We
9848 still set up imm_expr, so that we handle
9849 explicit extensions correctly. */
9850 imm_expr.X_op = O_constant;
9851 imm_expr.X_add_number = 0;
9852 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9853 continue;
9856 break;
9859 /* We need to relax this instruction. */
9860 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9861 s = expr_end;
9862 continue;
9864 case 'p':
9865 case 'q':
9866 case 'A':
9867 case 'B':
9868 case 'E':
9869 /* We use offset_reloc rather than imm_reloc for the PC
9870 relative operands. This lets macros with both
9871 immediate and address operands work correctly. */
9872 my_getExpression (&offset_expr, s);
9874 if (offset_expr.X_op == O_register)
9875 break;
9877 /* We need to relax this instruction. */
9878 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9879 s = expr_end;
9880 continue;
9882 case '6': /* break code */
9883 my_getExpression (&imm_expr, s);
9884 check_absolute_expr (ip, &imm_expr);
9885 if ((unsigned long) imm_expr.X_add_number > 63)
9887 as_warn (_("Invalid value for `%s' (%lu)"),
9888 ip->insn_mo->name,
9889 (unsigned long) imm_expr.X_add_number);
9890 imm_expr.X_add_number &= 0x3f;
9892 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9893 imm_expr.X_op = O_absent;
9894 s = expr_end;
9895 continue;
9897 case 'a': /* 26 bit address */
9898 my_getExpression (&offset_expr, s);
9899 s = expr_end;
9900 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9901 ip->insn_opcode <<= 16;
9902 continue;
9904 case 'l': /* register list for entry macro */
9905 case 'L': /* register list for exit macro */
9907 int mask;
9909 if (c == 'l')
9910 mask = 0;
9911 else
9912 mask = 7 << 3;
9913 while (*s != '\0')
9915 int freg, reg1, reg2;
9917 while (*s == ' ' || *s == ',')
9918 ++s;
9919 if (*s != '$')
9921 as_bad (_("can't parse register list"));
9922 break;
9924 ++s;
9925 if (*s != 'f')
9926 freg = 0;
9927 else
9929 freg = 1;
9930 ++s;
9932 reg1 = 0;
9933 while (ISDIGIT (*s))
9935 reg1 *= 10;
9936 reg1 += *s - '0';
9937 ++s;
9939 if (*s == ' ')
9940 ++s;
9941 if (*s != '-')
9942 reg2 = reg1;
9943 else
9945 ++s;
9946 if (*s != '$')
9947 break;
9948 ++s;
9949 if (freg)
9951 if (*s == 'f')
9952 ++s;
9953 else
9955 as_bad (_("invalid register list"));
9956 break;
9959 reg2 = 0;
9960 while (ISDIGIT (*s))
9962 reg2 *= 10;
9963 reg2 += *s - '0';
9964 ++s;
9967 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9969 mask &= ~ (7 << 3);
9970 mask |= 5 << 3;
9972 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9974 mask &= ~ (7 << 3);
9975 mask |= 6 << 3;
9977 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9978 mask |= (reg2 - 3) << 3;
9979 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9980 mask |= (reg2 - 15) << 1;
9981 else if (reg1 == RA && reg2 == RA)
9982 mask |= 1;
9983 else
9985 as_bad (_("invalid register list"));
9986 break;
9989 /* The mask is filled in in the opcode table for the
9990 benefit of the disassembler. We remove it before
9991 applying the actual mask. */
9992 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9993 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9995 continue;
9997 case 'e': /* extend code */
9998 my_getExpression (&imm_expr, s);
9999 check_absolute_expr (ip, &imm_expr);
10000 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10002 as_warn (_("Invalid value for `%s' (%lu)"),
10003 ip->insn_mo->name,
10004 (unsigned long) imm_expr.X_add_number);
10005 imm_expr.X_add_number &= 0x7ff;
10007 ip->insn_opcode |= imm_expr.X_add_number;
10008 imm_expr.X_op = O_absent;
10009 s = expr_end;
10010 continue;
10012 default:
10013 internalError ();
10015 break;
10018 /* Args don't match. */
10019 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10020 strcmp (insn->name, insn[1].name) == 0)
10022 ++insn;
10023 s = argsstart;
10024 continue;
10027 insn_error = _("illegal operands");
10029 return;
10033 /* This structure holds information we know about a mips16 immediate
10034 argument type. */
10036 struct mips16_immed_operand
10038 /* The type code used in the argument string in the opcode table. */
10039 int type;
10040 /* The number of bits in the short form of the opcode. */
10041 int nbits;
10042 /* The number of bits in the extended form of the opcode. */
10043 int extbits;
10044 /* The amount by which the short form is shifted when it is used;
10045 for example, the sw instruction has a shift count of 2. */
10046 int shift;
10047 /* The amount by which the short form is shifted when it is stored
10048 into the instruction code. */
10049 int op_shift;
10050 /* Non-zero if the short form is unsigned. */
10051 int unsp;
10052 /* Non-zero if the extended form is unsigned. */
10053 int extu;
10054 /* Non-zero if the value is PC relative. */
10055 int pcrel;
10058 /* The mips16 immediate operand types. */
10060 static const struct mips16_immed_operand mips16_immed_operands[] =
10062 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10063 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10064 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10065 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10066 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10067 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10068 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10069 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10070 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10071 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10072 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10073 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10074 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10075 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10076 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10077 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10078 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10079 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10080 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10081 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10082 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10085 #define MIPS16_NUM_IMMED \
10086 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10088 /* Handle a mips16 instruction with an immediate value. This or's the
10089 small immediate value into *INSN. It sets *USE_EXTEND to indicate
10090 whether an extended value is needed; if one is needed, it sets
10091 *EXTEND to the value. The argument type is TYPE. The value is VAL.
10092 If SMALL is true, an unextended opcode was explicitly requested.
10093 If EXT is true, an extended opcode was explicitly requested. If
10094 WARN is true, warn if EXT does not match reality. */
10096 static void
10097 mips16_immed (char *file, unsigned int line, int type, offsetT val,
10098 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10099 unsigned long *insn, bfd_boolean *use_extend,
10100 unsigned short *extend)
10102 register const struct mips16_immed_operand *op;
10103 int mintiny, maxtiny;
10104 bfd_boolean needext;
10106 op = mips16_immed_operands;
10107 while (op->type != type)
10109 ++op;
10110 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10113 if (op->unsp)
10115 if (type == '<' || type == '>' || type == '[' || type == ']')
10117 mintiny = 1;
10118 maxtiny = 1 << op->nbits;
10120 else
10122 mintiny = 0;
10123 maxtiny = (1 << op->nbits) - 1;
10126 else
10128 mintiny = - (1 << (op->nbits - 1));
10129 maxtiny = (1 << (op->nbits - 1)) - 1;
10132 /* Branch offsets have an implicit 0 in the lowest bit. */
10133 if (type == 'p' || type == 'q')
10134 val /= 2;
10136 if ((val & ((1 << op->shift) - 1)) != 0
10137 || val < (mintiny << op->shift)
10138 || val > (maxtiny << op->shift))
10139 needext = TRUE;
10140 else
10141 needext = FALSE;
10143 if (warn && ext && ! needext)
10144 as_warn_where (file, line,
10145 _("extended operand requested but not required"));
10146 if (small && needext)
10147 as_bad_where (file, line, _("invalid unextended operand value"));
10149 if (small || (! ext && ! needext))
10151 int insnval;
10153 *use_extend = FALSE;
10154 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10155 insnval <<= op->op_shift;
10156 *insn |= insnval;
10158 else
10160 long minext, maxext;
10161 int extval;
10163 if (op->extu)
10165 minext = 0;
10166 maxext = (1 << op->extbits) - 1;
10168 else
10170 minext = - (1 << (op->extbits - 1));
10171 maxext = (1 << (op->extbits - 1)) - 1;
10173 if (val < minext || val > maxext)
10174 as_bad_where (file, line,
10175 _("operand value out of range for instruction"));
10177 *use_extend = TRUE;
10178 if (op->extbits == 16)
10180 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10181 val &= 0x1f;
10183 else if (op->extbits == 15)
10185 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10186 val &= 0xf;
10188 else
10190 extval = ((val & 0x1f) << 6) | (val & 0x20);
10191 val = 0;
10194 *extend = (unsigned short) extval;
10195 *insn |= val;
10199 static const struct percent_op_match
10201 const char *str;
10202 bfd_reloc_code_real_type reloc;
10203 } percent_op[] =
10205 {"%lo", BFD_RELOC_LO16},
10206 #ifdef OBJ_ELF
10207 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10208 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10209 {"%call16", BFD_RELOC_MIPS_CALL16},
10210 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10211 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10212 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10213 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10214 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10215 {"%got", BFD_RELOC_MIPS_GOT16},
10216 {"%gp_rel", BFD_RELOC_GPREL16},
10217 {"%half", BFD_RELOC_16},
10218 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10219 {"%higher", BFD_RELOC_MIPS_HIGHER},
10220 {"%neg", BFD_RELOC_MIPS_SUB},
10221 #endif
10222 {"%hi", BFD_RELOC_HI16_S}
10226 /* Return true if *STR points to a relocation operator. When returning true,
10227 move *STR over the operator and store its relocation code in *RELOC.
10228 Leave both *STR and *RELOC alone when returning false. */
10230 static bfd_boolean
10231 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
10233 size_t i;
10235 for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10236 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10238 *str += strlen (percent_op[i].str);
10239 *reloc = percent_op[i].reloc;
10241 /* Check whether the output BFD supports this relocation.
10242 If not, issue an error and fall back on something safe. */
10243 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10245 as_bad ("relocation %s isn't supported by the current ABI",
10246 percent_op[i].str);
10247 *reloc = BFD_RELOC_LO16;
10249 return TRUE;
10251 return FALSE;
10255 /* Parse string STR as a 16-bit relocatable operand. Store the
10256 expression in *EP and the relocations in the array starting
10257 at RELOC. Return the number of relocation operators used.
10259 On exit, EXPR_END points to the first character after the expression.
10260 If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16. */
10262 static size_t
10263 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10264 char *str)
10266 bfd_reloc_code_real_type reversed_reloc[3];
10267 size_t reloc_index, i;
10268 int crux_depth, str_depth;
10269 char *crux;
10271 /* Search for the start of the main expression, recoding relocations
10272 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10273 of the main expression and with CRUX_DEPTH containing the number
10274 of open brackets at that point. */
10275 reloc_index = -1;
10276 str_depth = 0;
10279 reloc_index++;
10280 crux = str;
10281 crux_depth = str_depth;
10283 /* Skip over whitespace and brackets, keeping count of the number
10284 of brackets. */
10285 while (*str == ' ' || *str == '\t' || *str == '(')
10286 if (*str++ == '(')
10287 str_depth++;
10289 while (*str == '%'
10290 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10291 && parse_relocation (&str, &reversed_reloc[reloc_index]));
10293 my_getExpression (ep, crux);
10294 str = expr_end;
10296 /* Match every open bracket. */
10297 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10298 if (*str++ == ')')
10299 crux_depth--;
10301 if (crux_depth > 0)
10302 as_bad ("unclosed '('");
10304 expr_end = str;
10306 if (reloc_index == 0)
10307 reloc[0] = BFD_RELOC_LO16;
10308 else
10310 prev_reloc_op_frag = frag_now;
10311 for (i = 0; i < reloc_index; i++)
10312 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10315 return reloc_index;
10318 static void
10319 my_getExpression (expressionS *ep, char *str)
10321 char *save_in;
10322 valueT val;
10324 save_in = input_line_pointer;
10325 input_line_pointer = str;
10326 expression (ep);
10327 expr_end = input_line_pointer;
10328 input_line_pointer = save_in;
10330 /* If we are in mips16 mode, and this is an expression based on `.',
10331 then we bump the value of the symbol by 1 since that is how other
10332 text symbols are handled. We don't bother to handle complex
10333 expressions, just `.' plus or minus a constant. */
10334 if (mips_opts.mips16
10335 && ep->X_op == O_symbol
10336 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10337 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10338 && symbol_get_frag (ep->X_add_symbol) == frag_now
10339 && symbol_constant_p (ep->X_add_symbol)
10340 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10341 S_SET_VALUE (ep->X_add_symbol, val + 1);
10344 /* Turn a string in input_line_pointer into a floating point constant
10345 of type TYPE, and store the appropriate bytes in *LITP. The number
10346 of LITTLENUMS emitted is stored in *SIZEP. An error message is
10347 returned, or NULL on OK. */
10349 char *
10350 md_atof (int type, char *litP, int *sizeP)
10352 int prec;
10353 LITTLENUM_TYPE words[4];
10354 char *t;
10355 int i;
10357 switch (type)
10359 case 'f':
10360 prec = 2;
10361 break;
10363 case 'd':
10364 prec = 4;
10365 break;
10367 default:
10368 *sizeP = 0;
10369 return _("bad call to md_atof");
10372 t = atof_ieee (input_line_pointer, type, words);
10373 if (t)
10374 input_line_pointer = t;
10376 *sizeP = prec * 2;
10378 if (! target_big_endian)
10380 for (i = prec - 1; i >= 0; i--)
10382 md_number_to_chars (litP, words[i], 2);
10383 litP += 2;
10386 else
10388 for (i = 0; i < prec; i++)
10390 md_number_to_chars (litP, words[i], 2);
10391 litP += 2;
10395 return NULL;
10398 void
10399 md_number_to_chars (char *buf, valueT val, int n)
10401 if (target_big_endian)
10402 number_to_chars_bigendian (buf, val, n);
10403 else
10404 number_to_chars_littleendian (buf, val, n);
10407 #ifdef OBJ_ELF
10408 static int support_64bit_objects(void)
10410 const char **list, **l;
10411 int yes;
10413 list = bfd_target_list ();
10414 for (l = list; *l != NULL; l++)
10415 #ifdef TE_TMIPS
10416 /* This is traditional mips */
10417 if (strcmp (*l, "elf64-tradbigmips") == 0
10418 || strcmp (*l, "elf64-tradlittlemips") == 0)
10419 #else
10420 if (strcmp (*l, "elf64-bigmips") == 0
10421 || strcmp (*l, "elf64-littlemips") == 0)
10422 #endif
10423 break;
10424 yes = (*l != NULL);
10425 free (list);
10426 return yes;
10428 #endif /* OBJ_ELF */
10430 const char *md_shortopts = "nO::g::G:";
10432 struct option md_longopts[] =
10434 /* Options which specify architecture. */
10435 #define OPTION_ARCH_BASE (OPTION_MD_BASE)
10436 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10437 {"march", required_argument, NULL, OPTION_MARCH},
10438 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10439 {"mtune", required_argument, NULL, OPTION_MTUNE},
10440 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10441 {"mips0", no_argument, NULL, OPTION_MIPS1},
10442 {"mips1", no_argument, NULL, OPTION_MIPS1},
10443 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10444 {"mips2", no_argument, NULL, OPTION_MIPS2},
10445 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10446 {"mips3", no_argument, NULL, OPTION_MIPS3},
10447 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10448 {"mips4", no_argument, NULL, OPTION_MIPS4},
10449 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10450 {"mips5", no_argument, NULL, OPTION_MIPS5},
10451 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10452 {"mips32", no_argument, NULL, OPTION_MIPS32},
10453 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10454 {"mips64", no_argument, NULL, OPTION_MIPS64},
10455 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10456 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10457 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10458 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
10460 /* Options which specify Application Specific Extensions (ASEs). */
10461 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
10462 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10463 {"mips16", no_argument, NULL, OPTION_MIPS16},
10464 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10465 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10466 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10467 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10468 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10469 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10470 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10471 {"mdmx", no_argument, NULL, OPTION_MDMX},
10472 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10473 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10475 /* Old-style architecture options. Don't add more of these. */
10476 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10477 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10478 {"m4650", no_argument, NULL, OPTION_M4650},
10479 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10480 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10481 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10482 {"m4010", no_argument, NULL, OPTION_M4010},
10483 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10484 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10485 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10486 {"m4100", no_argument, NULL, OPTION_M4100},
10487 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10488 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10489 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10490 {"m3900", no_argument, NULL, OPTION_M3900},
10491 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10492 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10494 /* Options which enable bug fixes. */
10495 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10496 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10497 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10498 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10499 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10500 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10501 #define OPTION_FIX_VR4122 (OPTION_FIX_BASE + 2)
10502 #define OPTION_NO_FIX_VR4122 (OPTION_FIX_BASE + 3)
10503 {"mfix-vr4122-bugs", no_argument, NULL, OPTION_FIX_VR4122},
10504 {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10506 /* Miscellaneous options. */
10507 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10508 #define OPTION_MEMBEDDED_PIC (OPTION_MISC_BASE + 0)
10509 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10510 #define OPTION_TRAP (OPTION_MISC_BASE + 1)
10511 {"trap", no_argument, NULL, OPTION_TRAP},
10512 {"no-break", no_argument, NULL, OPTION_TRAP},
10513 #define OPTION_BREAK (OPTION_MISC_BASE + 2)
10514 {"break", no_argument, NULL, OPTION_BREAK},
10515 {"no-trap", no_argument, NULL, OPTION_BREAK},
10516 #define OPTION_EB (OPTION_MISC_BASE + 3)
10517 {"EB", no_argument, NULL, OPTION_EB},
10518 #define OPTION_EL (OPTION_MISC_BASE + 4)
10519 {"EL", no_argument, NULL, OPTION_EL},
10520 #define OPTION_FP32 (OPTION_MISC_BASE + 5)
10521 {"mfp32", no_argument, NULL, OPTION_FP32},
10522 #define OPTION_GP32 (OPTION_MISC_BASE + 6)
10523 {"mgp32", no_argument, NULL, OPTION_GP32},
10524 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10525 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10526 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 8)
10527 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10528 #define OPTION_FP64 (OPTION_MISC_BASE + 9)
10529 {"mfp64", no_argument, NULL, OPTION_FP64},
10530 #define OPTION_GP64 (OPTION_MISC_BASE + 10)
10531 {"mgp64", no_argument, NULL, OPTION_GP64},
10532 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10533 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 12)
10534 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10535 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10537 /* ELF-specific options. */
10538 #ifdef OBJ_ELF
10539 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 13)
10540 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10541 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10542 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10543 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10544 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10545 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10546 {"xgot", no_argument, NULL, OPTION_XGOT},
10547 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10548 {"mabi", required_argument, NULL, OPTION_MABI},
10549 #define OPTION_32 (OPTION_ELF_BASE + 4)
10550 {"32", no_argument, NULL, OPTION_32},
10551 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10552 {"n32", no_argument, NULL, OPTION_N32},
10553 #define OPTION_64 (OPTION_ELF_BASE + 6)
10554 {"64", no_argument, NULL, OPTION_64},
10555 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10556 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10557 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10558 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10559 #define OPTION_PDR (OPTION_ELF_BASE + 9)
10560 {"mpdr", no_argument, NULL, OPTION_PDR},
10561 #define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10562 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10563 #endif /* OBJ_ELF */
10565 {NULL, no_argument, NULL, 0}
10567 size_t md_longopts_size = sizeof (md_longopts);
10569 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10570 NEW_VALUE. Warn if another value was already specified. Note:
10571 we have to defer parsing the -march and -mtune arguments in order
10572 to handle 'from-abi' correctly, since the ABI might be specified
10573 in a later argument. */
10575 static void
10576 mips_set_option_string (const char **string_ptr, const char *new_value)
10578 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10579 as_warn (_("A different %s was already specified, is now %s"),
10580 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10581 new_value);
10583 *string_ptr = new_value;
10587 md_parse_option (int c, char *arg)
10589 switch (c)
10591 case OPTION_CONSTRUCT_FLOATS:
10592 mips_disable_float_construction = 0;
10593 break;
10595 case OPTION_NO_CONSTRUCT_FLOATS:
10596 mips_disable_float_construction = 1;
10597 break;
10599 case OPTION_TRAP:
10600 mips_trap = 1;
10601 break;
10603 case OPTION_BREAK:
10604 mips_trap = 0;
10605 break;
10607 case OPTION_EB:
10608 target_big_endian = 1;
10609 break;
10611 case OPTION_EL:
10612 target_big_endian = 0;
10613 break;
10615 case 'n':
10616 warn_nops = 1;
10617 break;
10619 case 'O':
10620 if (arg && arg[1] == '0')
10621 mips_optimize = 1;
10622 else
10623 mips_optimize = 2;
10624 break;
10626 case 'g':
10627 if (arg == NULL)
10628 mips_debug = 2;
10629 else
10630 mips_debug = atoi (arg);
10631 /* When the MIPS assembler sees -g or -g2, it does not do
10632 optimizations which limit full symbolic debugging. We take
10633 that to be equivalent to -O0. */
10634 if (mips_debug == 2)
10635 mips_optimize = 1;
10636 break;
10638 case OPTION_MIPS1:
10639 file_mips_isa = ISA_MIPS1;
10640 break;
10642 case OPTION_MIPS2:
10643 file_mips_isa = ISA_MIPS2;
10644 break;
10646 case OPTION_MIPS3:
10647 file_mips_isa = ISA_MIPS3;
10648 break;
10650 case OPTION_MIPS4:
10651 file_mips_isa = ISA_MIPS4;
10652 break;
10654 case OPTION_MIPS5:
10655 file_mips_isa = ISA_MIPS5;
10656 break;
10658 case OPTION_MIPS32:
10659 file_mips_isa = ISA_MIPS32;
10660 break;
10662 case OPTION_MIPS32R2:
10663 file_mips_isa = ISA_MIPS32R2;
10664 break;
10666 case OPTION_MIPS64R2:
10667 file_mips_isa = ISA_MIPS64R2;
10668 break;
10670 case OPTION_MIPS64:
10671 file_mips_isa = ISA_MIPS64;
10672 break;
10674 case OPTION_MTUNE:
10675 mips_set_option_string (&mips_tune_string, arg);
10676 break;
10678 case OPTION_MARCH:
10679 mips_set_option_string (&mips_arch_string, arg);
10680 break;
10682 case OPTION_M4650:
10683 mips_set_option_string (&mips_arch_string, "4650");
10684 mips_set_option_string (&mips_tune_string, "4650");
10685 break;
10687 case OPTION_NO_M4650:
10688 break;
10690 case OPTION_M4010:
10691 mips_set_option_string (&mips_arch_string, "4010");
10692 mips_set_option_string (&mips_tune_string, "4010");
10693 break;
10695 case OPTION_NO_M4010:
10696 break;
10698 case OPTION_M4100:
10699 mips_set_option_string (&mips_arch_string, "4100");
10700 mips_set_option_string (&mips_tune_string, "4100");
10701 break;
10703 case OPTION_NO_M4100:
10704 break;
10706 case OPTION_M3900:
10707 mips_set_option_string (&mips_arch_string, "3900");
10708 mips_set_option_string (&mips_tune_string, "3900");
10709 break;
10711 case OPTION_NO_M3900:
10712 break;
10714 case OPTION_MDMX:
10715 mips_opts.ase_mdmx = 1;
10716 break;
10718 case OPTION_NO_MDMX:
10719 mips_opts.ase_mdmx = 0;
10720 break;
10722 case OPTION_MIPS16:
10723 mips_opts.mips16 = 1;
10724 mips_no_prev_insn (FALSE);
10725 break;
10727 case OPTION_NO_MIPS16:
10728 mips_opts.mips16 = 0;
10729 mips_no_prev_insn (FALSE);
10730 break;
10732 case OPTION_MIPS3D:
10733 mips_opts.ase_mips3d = 1;
10734 break;
10736 case OPTION_NO_MIPS3D:
10737 mips_opts.ase_mips3d = 0;
10738 break;
10740 case OPTION_MEMBEDDED_PIC:
10741 mips_pic = EMBEDDED_PIC;
10742 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10744 as_bad (_("-G may not be used with embedded PIC code"));
10745 return 0;
10747 g_switch_value = 0x7fffffff;
10748 break;
10750 case OPTION_FIX_VR4122:
10751 mips_fix_4122_bugs = 1;
10752 break;
10754 case OPTION_NO_FIX_VR4122:
10755 mips_fix_4122_bugs = 0;
10756 break;
10758 case OPTION_RELAX_BRANCH:
10759 mips_relax_branch = 1;
10760 break;
10762 case OPTION_NO_RELAX_BRANCH:
10763 mips_relax_branch = 0;
10764 break;
10766 #ifdef OBJ_ELF
10767 /* When generating ELF code, we permit -KPIC and -call_shared to
10768 select SVR4_PIC, and -non_shared to select no PIC. This is
10769 intended to be compatible with Irix 5. */
10770 case OPTION_CALL_SHARED:
10771 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10773 as_bad (_("-call_shared is supported only for ELF format"));
10774 return 0;
10776 mips_pic = SVR4_PIC;
10777 mips_abicalls = TRUE;
10778 if (g_switch_seen && g_switch_value != 0)
10780 as_bad (_("-G may not be used with SVR4 PIC code"));
10781 return 0;
10783 g_switch_value = 0;
10784 break;
10786 case OPTION_NON_SHARED:
10787 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10789 as_bad (_("-non_shared is supported only for ELF format"));
10790 return 0;
10792 mips_pic = NO_PIC;
10793 mips_abicalls = FALSE;
10794 break;
10796 /* The -xgot option tells the assembler to use 32 offsets when
10797 accessing the got in SVR4_PIC mode. It is for Irix
10798 compatibility. */
10799 case OPTION_XGOT:
10800 mips_big_got = 1;
10801 break;
10802 #endif /* OBJ_ELF */
10804 case 'G':
10805 if (! USE_GLOBAL_POINTER_OPT)
10807 as_bad (_("-G is not supported for this configuration"));
10808 return 0;
10810 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10812 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10813 return 0;
10815 else
10816 g_switch_value = atoi (arg);
10817 g_switch_seen = 1;
10818 break;
10820 #ifdef OBJ_ELF
10821 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10822 and -mabi=64. */
10823 case OPTION_32:
10824 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10826 as_bad (_("-32 is supported for ELF format only"));
10827 return 0;
10829 mips_abi = O32_ABI;
10830 break;
10832 case OPTION_N32:
10833 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10835 as_bad (_("-n32 is supported for ELF format only"));
10836 return 0;
10838 mips_abi = N32_ABI;
10839 break;
10841 case OPTION_64:
10842 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10844 as_bad (_("-64 is supported for ELF format only"));
10845 return 0;
10847 mips_abi = N64_ABI;
10848 if (! support_64bit_objects())
10849 as_fatal (_("No compiled in support for 64 bit object file format"));
10850 break;
10851 #endif /* OBJ_ELF */
10853 case OPTION_GP32:
10854 file_mips_gp32 = 1;
10855 break;
10857 case OPTION_GP64:
10858 file_mips_gp32 = 0;
10859 break;
10861 case OPTION_FP32:
10862 file_mips_fp32 = 1;
10863 break;
10865 case OPTION_FP64:
10866 file_mips_fp32 = 0;
10867 break;
10869 #ifdef OBJ_ELF
10870 case OPTION_MABI:
10871 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10873 as_bad (_("-mabi is supported for ELF format only"));
10874 return 0;
10876 if (strcmp (arg, "32") == 0)
10877 mips_abi = O32_ABI;
10878 else if (strcmp (arg, "o64") == 0)
10879 mips_abi = O64_ABI;
10880 else if (strcmp (arg, "n32") == 0)
10881 mips_abi = N32_ABI;
10882 else if (strcmp (arg, "64") == 0)
10884 mips_abi = N64_ABI;
10885 if (! support_64bit_objects())
10886 as_fatal (_("No compiled in support for 64 bit object file "
10887 "format"));
10889 else if (strcmp (arg, "eabi") == 0)
10890 mips_abi = EABI_ABI;
10891 else
10893 as_fatal (_("invalid abi -mabi=%s"), arg);
10894 return 0;
10896 break;
10897 #endif /* OBJ_ELF */
10899 case OPTION_M7000_HILO_FIX:
10900 mips_7000_hilo_fix = TRUE;
10901 break;
10903 case OPTION_MNO_7000_HILO_FIX:
10904 mips_7000_hilo_fix = FALSE;
10905 break;
10907 #ifdef OBJ_ELF
10908 case OPTION_MDEBUG:
10909 mips_flag_mdebug = TRUE;
10910 break;
10912 case OPTION_NO_MDEBUG:
10913 mips_flag_mdebug = FALSE;
10914 break;
10916 case OPTION_PDR:
10917 mips_flag_pdr = TRUE;
10918 break;
10920 case OPTION_NO_PDR:
10921 mips_flag_pdr = FALSE;
10922 break;
10923 #endif /* OBJ_ELF */
10925 default:
10926 return 0;
10929 return 1;
10932 /* Set up globals to generate code for the ISA or processor
10933 described by INFO. */
10935 static void
10936 mips_set_architecture (const struct mips_cpu_info *info)
10938 if (info != 0)
10940 file_mips_arch = info->cpu;
10941 mips_opts.arch = info->cpu;
10942 mips_opts.isa = info->isa;
10947 /* Likewise for tuning. */
10949 static void
10950 mips_set_tune (const struct mips_cpu_info *info)
10952 if (info != 0)
10953 mips_tune = info->cpu;
10957 void
10958 mips_after_parse_args (void)
10960 const struct mips_cpu_info *arch_info = 0;
10961 const struct mips_cpu_info *tune_info = 0;
10963 /* GP relative stuff not working for PE */
10964 if (strncmp (TARGET_OS, "pe", 2) == 0
10965 && g_switch_value != 0)
10967 if (g_switch_seen)
10968 as_bad (_("-G not supported in this configuration."));
10969 g_switch_value = 0;
10972 if (mips_abi == NO_ABI)
10973 mips_abi = MIPS_DEFAULT_ABI;
10975 /* The following code determines the architecture and register size.
10976 Similar code was added to GCC 3.3 (see override_options() in
10977 config/mips/mips.c). The GAS and GCC code should be kept in sync
10978 as much as possible. */
10980 if (mips_arch_string != 0)
10981 arch_info = mips_parse_cpu ("-march", mips_arch_string);
10983 if (file_mips_isa != ISA_UNKNOWN)
10985 /* Handle -mipsN. At this point, file_mips_isa contains the
10986 ISA level specified by -mipsN, while arch_info->isa contains
10987 the -march selection (if any). */
10988 if (arch_info != 0)
10990 /* -march takes precedence over -mipsN, since it is more descriptive.
10991 There's no harm in specifying both as long as the ISA levels
10992 are the same. */
10993 if (file_mips_isa != arch_info->isa)
10994 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10995 mips_cpu_info_from_isa (file_mips_isa)->name,
10996 mips_cpu_info_from_isa (arch_info->isa)->name);
10998 else
10999 arch_info = mips_cpu_info_from_isa (file_mips_isa);
11002 if (arch_info == 0)
11003 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
11005 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
11006 as_bad ("-march=%s is not compatible with the selected ABI",
11007 arch_info->name);
11009 mips_set_architecture (arch_info);
11011 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
11012 if (mips_tune_string != 0)
11013 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
11015 if (tune_info == 0)
11016 mips_set_tune (arch_info);
11017 else
11018 mips_set_tune (tune_info);
11020 if (file_mips_gp32 >= 0)
11022 /* The user specified the size of the integer registers. Make sure
11023 it agrees with the ABI and ISA. */
11024 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11025 as_bad (_("-mgp64 used with a 32-bit processor"));
11026 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11027 as_bad (_("-mgp32 used with a 64-bit ABI"));
11028 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11029 as_bad (_("-mgp64 used with a 32-bit ABI"));
11031 else
11033 /* Infer the integer register size from the ABI and processor.
11034 Restrict ourselves to 32-bit registers if that's all the
11035 processor has, or if the ABI cannot handle 64-bit registers. */
11036 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11037 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
11040 /* ??? GAS treats single-float processors as though they had 64-bit
11041 float registers (although it complains when double-precision
11042 instructions are used). As things stand, saying they have 32-bit
11043 registers would lead to spurious "register must be even" messages.
11044 So here we assume float registers are always the same size as
11045 integer ones, unless the user says otherwise. */
11046 if (file_mips_fp32 < 0)
11047 file_mips_fp32 = file_mips_gp32;
11049 /* End of GCC-shared inference code. */
11051 /* This flag is set when we have a 64-bit capable CPU but use only
11052 32-bit wide registers. Note that EABI does not use it. */
11053 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11054 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11055 || mips_abi == O32_ABI))
11056 mips_32bitmode = 1;
11058 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11059 as_bad (_("trap exception not supported at ISA 1"));
11061 /* If the selected architecture includes support for ASEs, enable
11062 generation of code for them. */
11063 if (mips_opts.mips16 == -1)
11064 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
11065 if (mips_opts.ase_mips3d == -1)
11066 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
11067 if (mips_opts.ase_mdmx == -1)
11068 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
11070 file_mips_isa = mips_opts.isa;
11071 file_ase_mips16 = mips_opts.mips16;
11072 file_ase_mips3d = mips_opts.ase_mips3d;
11073 file_ase_mdmx = mips_opts.ase_mdmx;
11074 mips_opts.gp32 = file_mips_gp32;
11075 mips_opts.fp32 = file_mips_fp32;
11077 if (mips_flag_mdebug < 0)
11079 #ifdef OBJ_MAYBE_ECOFF
11080 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11081 mips_flag_mdebug = 1;
11082 else
11083 #endif /* OBJ_MAYBE_ECOFF */
11084 mips_flag_mdebug = 0;
11088 void
11089 mips_init_after_args (void)
11091 /* initialize opcodes */
11092 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
11093 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
11096 long
11097 md_pcrel_from (fixS *fixP)
11099 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11100 switch (fixP->fx_r_type)
11102 case BFD_RELOC_16_PCREL_S2:
11103 case BFD_RELOC_MIPS_JMP:
11104 /* Return the address of the delay slot. */
11105 return addr + 4;
11106 default:
11107 return addr;
11111 /* This is called before the symbol table is processed. In order to
11112 work with gcc when using mips-tfile, we must keep all local labels.
11113 However, in other cases, we want to discard them. If we were
11114 called with -g, but we didn't see any debugging information, it may
11115 mean that gcc is smuggling debugging information through to
11116 mips-tfile, in which case we must generate all local labels. */
11118 void
11119 mips_frob_file_before_adjust (void)
11121 #ifndef NO_ECOFF_DEBUGGING
11122 if (ECOFF_DEBUGGING
11123 && mips_debug != 0
11124 && ! ecoff_debugging_seen)
11125 flag_keep_locals = 1;
11126 #endif
11129 /* Sort any unmatched HI16_S relocs so that they immediately precede
11130 the corresponding LO reloc. This is called before md_apply_fix3 and
11131 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
11132 explicit use of the %hi modifier. */
11134 void
11135 mips_frob_file (void)
11137 struct mips_hi_fixup *l;
11139 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11141 segment_info_type *seginfo;
11142 int pass;
11144 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
11146 /* If a GOT16 relocation turns out to be against a global symbol,
11147 there isn't supposed to be a matching LO. */
11148 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11149 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11150 continue;
11152 /* Check quickly whether the next fixup happens to be a matching %lo. */
11153 if (fixup_has_matching_lo_p (l->fixp))
11154 continue;
11156 /* Look through the fixups for this segment for a matching %lo.
11157 When we find one, move the %hi just in front of it. We do
11158 this in two passes. In the first pass, we try to find a
11159 unique %lo. In the second pass, we permit multiple %hi
11160 relocs for a single %lo (this is a GNU extension). */
11161 seginfo = seg_info (l->seg);
11162 for (pass = 0; pass < 2; pass++)
11164 fixS *f, *prev;
11166 prev = NULL;
11167 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
11169 /* Check whether this is a %lo fixup which matches l->fixp. */
11170 if (f->fx_r_type == BFD_RELOC_LO16
11171 && f->fx_addsy == l->fixp->fx_addsy
11172 && f->fx_offset == l->fixp->fx_offset
11173 && (pass == 1
11174 || prev == NULL
11175 || !reloc_needs_lo_p (prev->fx_r_type)
11176 || !fixup_has_matching_lo_p (prev)))
11178 fixS **pf;
11180 /* Move l->fixp before f. */
11181 for (pf = &seginfo->fix_root;
11182 *pf != l->fixp;
11183 pf = &(*pf)->fx_next)
11184 assert (*pf != NULL);
11186 *pf = l->fixp->fx_next;
11188 l->fixp->fx_next = f;
11189 if (prev == NULL)
11190 seginfo->fix_root = l->fixp;
11191 else
11192 prev->fx_next = l->fixp;
11194 break;
11197 prev = f;
11200 if (f != NULL)
11201 break;
11203 #if 0 /* GCC code motion plus incomplete dead code elimination
11204 can leave a %hi without a %lo. */
11205 if (pass == 1)
11206 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11207 _("Unmatched %%hi reloc"));
11208 #endif
11213 /* When generating embedded PIC code we need to use a special
11214 relocation to represent the difference of two symbols in the .text
11215 section (switch tables use a difference of this sort). See
11216 include/coff/mips.h for details. This macro checks whether this
11217 fixup requires the special reloc. */
11218 #define SWITCH_TABLE(fixp) \
11219 ((fixp)->fx_r_type == BFD_RELOC_32 \
11220 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
11221 && (fixp)->fx_addsy != NULL \
11222 && (fixp)->fx_subsy != NULL \
11223 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11224 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11226 /* When generating embedded PIC code we must keep all PC relative
11227 relocations, in case the linker has to relax a call. We also need
11228 to keep relocations for switch table entries.
11230 We may have combined relocations without symbols in the N32/N64 ABI.
11231 We have to prevent gas from dropping them. */
11234 mips_force_relocation (fixS *fixp)
11236 if (generic_force_reloc (fixp))
11237 return 1;
11239 if (HAVE_NEWABI
11240 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11241 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11242 || fixp->fx_r_type == BFD_RELOC_HI16_S
11243 || fixp->fx_r_type == BFD_RELOC_LO16))
11244 return 1;
11246 return (mips_pic == EMBEDDED_PIC
11247 && (fixp->fx_pcrel
11248 || SWITCH_TABLE (fixp)
11249 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11250 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11253 /* This hook is called before a fix is simplified. We don't really
11254 decide whether to skip a fix here. Rather, we turn global symbols
11255 used as branch targets into local symbols, such that they undergo
11256 simplification. We can only do this if the symbol is defined and
11257 it is in the same section as the branch. If this doesn't hold, we
11258 emit a better error message than just saying the relocation is not
11259 valid for the selected object format.
11261 FIXP is the fix-up we're going to try to simplify, SEG is the
11262 segment in which the fix up occurs. The return value should be
11263 non-zero to indicate the fix-up is valid for further
11264 simplifications. */
11267 mips_validate_fix (struct fix *fixP, asection *seg)
11269 /* There's a lot of discussion on whether it should be possible to
11270 use R_MIPS_PC16 to represent branch relocations. The outcome
11271 seems to be that it can, but gas/bfd are very broken in creating
11272 RELA relocations for this, so for now we only accept branches to
11273 symbols in the same section. Anything else is of dubious value,
11274 since there's no guarantee that at link time the symbol would be
11275 in range. Even for branches to local symbols this is arguably
11276 wrong, since it we assume the symbol is not going to be
11277 overridden, which should be possible per ELF library semantics,
11278 but then, there isn't a dynamic relocation that could be used to
11279 this effect, and the target would likely be out of range as well.
11281 Unfortunately, it seems that there is too much code out there
11282 that relies on branches to symbols that are global to be resolved
11283 as if they were local, like the IRIX tools do, so we do it as
11284 well, but with a warning so that people are reminded to fix their
11285 code. If we ever get back to using R_MIPS_PC16 for branch
11286 targets, this entire block should go away (and probably the
11287 whole function). */
11289 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11290 && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11291 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11292 && mips_pic != EMBEDDED_PIC)
11293 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11294 && fixP->fx_addsy)
11296 if (! S_IS_DEFINED (fixP->fx_addsy))
11298 as_bad_where (fixP->fx_file, fixP->fx_line,
11299 _("Cannot branch to undefined symbol."));
11300 /* Avoid any further errors about this fixup. */
11301 fixP->fx_done = 1;
11303 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11305 as_bad_where (fixP->fx_file, fixP->fx_line,
11306 _("Cannot branch to symbol in another section."));
11307 fixP->fx_done = 1;
11309 else if (S_IS_EXTERNAL (fixP->fx_addsy))
11311 symbolS *sym = fixP->fx_addsy;
11313 if (mips_pic == SVR4_PIC)
11314 as_warn_where (fixP->fx_file, fixP->fx_line,
11315 _("Pretending global symbol used as branch target is local."));
11317 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11318 S_GET_SEGMENT (sym),
11319 S_GET_VALUE (sym),
11320 symbol_get_frag (sym));
11321 copy_symbol_attributes (fixP->fx_addsy, sym);
11322 S_CLEAR_EXTERNAL (fixP->fx_addsy);
11323 assert (symbol_resolved_p (sym));
11324 symbol_mark_resolved (fixP->fx_addsy);
11328 return 1;
11331 #ifdef OBJ_ELF
11332 static int
11333 mips_need_elf_addend_fixup (fixS *fixP)
11335 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11336 return 1;
11337 if (mips_pic == EMBEDDED_PIC
11338 && S_IS_WEAK (fixP->fx_addsy))
11339 return 1;
11340 if (mips_pic != EMBEDDED_PIC
11341 && (S_IS_WEAK (fixP->fx_addsy)
11342 || S_IS_EXTERNAL (fixP->fx_addsy))
11343 && !S_IS_COMMON (fixP->fx_addsy))
11344 return 1;
11345 if (((bfd_get_section_flags (stdoutput,
11346 S_GET_SEGMENT (fixP->fx_addsy))
11347 & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11348 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11349 ".gnu.linkonce",
11350 sizeof (".gnu.linkonce") - 1))
11351 return 1;
11352 return 0;
11354 #endif
11356 /* Apply a fixup to the object file. */
11358 void
11359 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
11361 bfd_byte *buf;
11362 long insn;
11363 static int previous_fx_r_type = 0;
11364 reloc_howto_type *howto;
11366 /* We ignore generic BFD relocations we don't know about. */
11367 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11368 if (! howto)
11369 return;
11371 assert (fixP->fx_size == 4
11372 || fixP->fx_r_type == BFD_RELOC_16
11373 || fixP->fx_r_type == BFD_RELOC_64
11374 || fixP->fx_r_type == BFD_RELOC_CTOR
11375 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11376 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11377 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11379 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11381 /* If we aren't adjusting this fixup to be against the section
11382 symbol, we need to adjust the value. */
11383 #ifdef OBJ_ELF
11384 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11386 if (mips_need_elf_addend_fixup (fixP)
11387 && howto->partial_inplace
11388 && fixP->fx_r_type != BFD_RELOC_GPREL16
11389 && fixP->fx_r_type != BFD_RELOC_GPREL32
11390 && fixP->fx_r_type != BFD_RELOC_MIPS16_GPREL)
11392 /* In this case, the bfd_install_relocation routine will
11393 incorrectly add the symbol value back in. We just want
11394 the addend to appear in the object file.
11396 The condition above used to include
11397 "&& (! fixP->fx_pcrel || howto->pcrel_offset)".
11399 However, howto can't be trusted here, because we
11400 might change the reloc type in tc_gen_reloc. We can
11401 check howto->partial_inplace because that conversion
11402 happens to preserve howto->partial_inplace; but it
11403 does not preserve howto->pcrel_offset. I've just
11404 eliminated the check, because all MIPS PC-relative
11405 relocations are marked howto->pcrel_offset.
11407 howto->pcrel_offset was originally added for
11408 R_MIPS_PC16, which is generated for code like
11410 globl g1 .text
11411 .text
11412 .space 20
11415 bal g1
11417 *valP -= S_GET_VALUE (fixP->fx_addsy);
11420 /* This code was generated using trial and error and so is
11421 fragile and not trustworthy. If you change it, you should
11422 rerun the elf-rel, elf-rel2, and empic testcases and ensure
11423 they still pass. */
11424 if (fixP->fx_pcrel)
11426 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11428 /* BFD's REL handling, for MIPS, is _very_ weird.
11429 This gives the right results, but it can't possibly
11430 be the way things are supposed to work. */
11431 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11434 #endif
11436 /* We are not done if this is a composite relocation to set up gp. */
11437 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11438 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11439 || (fixP->fx_r_type == BFD_RELOC_64
11440 && (previous_fx_r_type == BFD_RELOC_GPREL32
11441 || previous_fx_r_type == BFD_RELOC_GPREL16))
11442 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11443 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11444 || fixP->fx_r_type == BFD_RELOC_LO16))))
11445 fixP->fx_done = 1;
11446 previous_fx_r_type = fixP->fx_r_type;
11448 switch (fixP->fx_r_type)
11450 case BFD_RELOC_MIPS_JMP:
11451 case BFD_RELOC_MIPS_SHIFT5:
11452 case BFD_RELOC_MIPS_SHIFT6:
11453 case BFD_RELOC_MIPS_GOT_DISP:
11454 case BFD_RELOC_MIPS_GOT_PAGE:
11455 case BFD_RELOC_MIPS_GOT_OFST:
11456 case BFD_RELOC_MIPS_SUB:
11457 case BFD_RELOC_MIPS_INSERT_A:
11458 case BFD_RELOC_MIPS_INSERT_B:
11459 case BFD_RELOC_MIPS_DELETE:
11460 case BFD_RELOC_MIPS_HIGHEST:
11461 case BFD_RELOC_MIPS_HIGHER:
11462 case BFD_RELOC_MIPS_SCN_DISP:
11463 case BFD_RELOC_MIPS_REL16:
11464 case BFD_RELOC_MIPS_RELGOT:
11465 case BFD_RELOC_MIPS_JALR:
11466 case BFD_RELOC_HI16:
11467 case BFD_RELOC_HI16_S:
11468 case BFD_RELOC_GPREL16:
11469 case BFD_RELOC_MIPS_LITERAL:
11470 case BFD_RELOC_MIPS_CALL16:
11471 case BFD_RELOC_MIPS_GOT16:
11472 case BFD_RELOC_GPREL32:
11473 case BFD_RELOC_MIPS_GOT_HI16:
11474 case BFD_RELOC_MIPS_GOT_LO16:
11475 case BFD_RELOC_MIPS_CALL_HI16:
11476 case BFD_RELOC_MIPS_CALL_LO16:
11477 case BFD_RELOC_MIPS16_GPREL:
11478 if (fixP->fx_pcrel)
11479 as_bad_where (fixP->fx_file, fixP->fx_line,
11480 _("Invalid PC relative reloc"));
11481 /* Nothing needed to do. The value comes from the reloc entry */
11482 break;
11484 case BFD_RELOC_MIPS16_JMP:
11485 /* We currently always generate a reloc against a symbol, which
11486 means that we don't want an addend even if the symbol is
11487 defined. */
11488 *valP = 0;
11489 break;
11491 case BFD_RELOC_PCREL_HI16_S:
11492 /* The addend for this is tricky if it is internal, so we just
11493 do everything here rather than in bfd_install_relocation. */
11494 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11495 break;
11496 if (fixP->fx_addsy
11497 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11499 /* For an external symbol adjust by the address to make it
11500 pcrel_offset. We use the address of the RELLO reloc
11501 which follows this one. */
11502 *valP += (fixP->fx_next->fx_frag->fr_address
11503 + fixP->fx_next->fx_where);
11505 *valP = ((*valP + 0x8000) >> 16) & 0xffff;
11506 if (target_big_endian)
11507 buf += 2;
11508 md_number_to_chars (buf, *valP, 2);
11509 break;
11511 case BFD_RELOC_PCREL_LO16:
11512 /* The addend for this is tricky if it is internal, so we just
11513 do everything here rather than in bfd_install_relocation. */
11514 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11515 break;
11516 if (fixP->fx_addsy
11517 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11518 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11519 if (target_big_endian)
11520 buf += 2;
11521 md_number_to_chars (buf, *valP, 2);
11522 break;
11524 case BFD_RELOC_64:
11525 /* This is handled like BFD_RELOC_32, but we output a sign
11526 extended value if we are only 32 bits. */
11527 if (fixP->fx_done
11528 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11530 if (8 <= sizeof (valueT))
11531 md_number_to_chars (buf, *valP, 8);
11532 else
11534 valueT hiv;
11536 if ((*valP & 0x80000000) != 0)
11537 hiv = 0xffffffff;
11538 else
11539 hiv = 0;
11540 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11541 *valP, 4);
11542 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11543 hiv, 4);
11546 break;
11548 case BFD_RELOC_RVA:
11549 case BFD_RELOC_32:
11550 /* If we are deleting this reloc entry, we must fill in the
11551 value now. This can happen if we have a .word which is not
11552 resolved when it appears but is later defined. We also need
11553 to fill in the value if this is an embedded PIC switch table
11554 entry. */
11555 if (fixP->fx_done
11556 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11557 md_number_to_chars (buf, *valP, 4);
11558 break;
11560 case BFD_RELOC_16:
11561 /* If we are deleting this reloc entry, we must fill in the
11562 value now. */
11563 assert (fixP->fx_size == 2);
11564 if (fixP->fx_done)
11565 md_number_to_chars (buf, *valP, 2);
11566 break;
11568 case BFD_RELOC_LO16:
11569 /* When handling an embedded PIC switch statement, we can wind
11570 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11571 if (fixP->fx_done)
11573 if (*valP + 0x8000 > 0xffff)
11574 as_bad_where (fixP->fx_file, fixP->fx_line,
11575 _("relocation overflow"));
11576 if (target_big_endian)
11577 buf += 2;
11578 md_number_to_chars (buf, *valP, 2);
11580 break;
11582 case BFD_RELOC_16_PCREL_S2:
11583 if ((*valP & 0x3) != 0)
11584 as_bad_where (fixP->fx_file, fixP->fx_line,
11585 _("Branch to odd address (%lx)"), (long) *valP);
11588 * We need to save the bits in the instruction since fixup_segment()
11589 * might be deleting the relocation entry (i.e., a branch within
11590 * the current segment).
11592 if (! fixP->fx_done)
11593 break;
11595 /* update old instruction data */
11596 if (target_big_endian)
11597 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11598 else
11599 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11601 if (*valP + 0x20000 <= 0x3ffff)
11603 insn |= (*valP >> 2) & 0xffff;
11604 md_number_to_chars (buf, insn, 4);
11606 else if (mips_pic == NO_PIC
11607 && fixP->fx_done
11608 && fixP->fx_frag->fr_address >= text_section->vma
11609 && (fixP->fx_frag->fr_address
11610 < text_section->vma + text_section->_raw_size)
11611 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11612 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11613 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11615 /* The branch offset is too large. If this is an
11616 unconditional branch, and we are not generating PIC code,
11617 we can convert it to an absolute jump instruction. */
11618 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11619 insn = 0x0c000000; /* jal */
11620 else
11621 insn = 0x08000000; /* j */
11622 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11623 fixP->fx_done = 0;
11624 fixP->fx_addsy = section_symbol (text_section);
11625 *valP += md_pcrel_from (fixP);
11626 md_number_to_chars (buf, insn, 4);
11628 else
11630 /* If we got here, we have branch-relaxation disabled,
11631 and there's nothing we can do to fix this instruction
11632 without turning it into a longer sequence. */
11633 as_bad_where (fixP->fx_file, fixP->fx_line,
11634 _("Branch out of range"));
11636 break;
11638 case BFD_RELOC_VTABLE_INHERIT:
11639 fixP->fx_done = 0;
11640 if (fixP->fx_addsy
11641 && !S_IS_DEFINED (fixP->fx_addsy)
11642 && !S_IS_WEAK (fixP->fx_addsy))
11643 S_SET_WEAK (fixP->fx_addsy);
11644 break;
11646 case BFD_RELOC_VTABLE_ENTRY:
11647 fixP->fx_done = 0;
11648 break;
11650 default:
11651 internalError ();
11654 /* Remember value for tc_gen_reloc. */
11655 fixP->fx_addnumber = *valP;
11658 #if 0
11659 void
11660 printInsn (unsigned long oc)
11662 const struct mips_opcode *p;
11663 int treg, sreg, dreg, shamt;
11664 short imm;
11665 const char *args;
11666 int i;
11668 for (i = 0; i < NUMOPCODES; ++i)
11670 p = &mips_opcodes[i];
11671 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11673 printf ("%08lx %s\t", oc, p->name);
11674 treg = (oc >> 16) & 0x1f;
11675 sreg = (oc >> 21) & 0x1f;
11676 dreg = (oc >> 11) & 0x1f;
11677 shamt = (oc >> 6) & 0x1f;
11678 imm = oc;
11679 for (args = p->args;; ++args)
11681 switch (*args)
11683 case '\0':
11684 printf ("\n");
11685 break;
11687 case ',':
11688 case '(':
11689 case ')':
11690 printf ("%c", *args);
11691 continue;
11693 case 'r':
11694 assert (treg == sreg);
11695 printf ("$%d,$%d", treg, sreg);
11696 continue;
11698 case 'd':
11699 case 'G':
11700 printf ("$%d", dreg);
11701 continue;
11703 case 't':
11704 case 'E':
11705 printf ("$%d", treg);
11706 continue;
11708 case 'k':
11709 printf ("0x%x", treg);
11710 continue;
11712 case 'b':
11713 case 's':
11714 printf ("$%d", sreg);
11715 continue;
11717 case 'a':
11718 printf ("0x%08lx", oc & 0x1ffffff);
11719 continue;
11721 case 'i':
11722 case 'j':
11723 case 'o':
11724 case 'u':
11725 printf ("%d", imm);
11726 continue;
11728 case '<':
11729 case '>':
11730 printf ("$%d", shamt);
11731 continue;
11733 default:
11734 internalError ();
11736 break;
11738 return;
11741 printf (_("%08lx UNDEFINED\n"), oc);
11743 #endif
11745 static symbolS *
11746 get_symbol (void)
11748 int c;
11749 char *name;
11750 symbolS *p;
11752 name = input_line_pointer;
11753 c = get_symbol_end ();
11754 p = (symbolS *) symbol_find_or_make (name);
11755 *input_line_pointer = c;
11756 return p;
11759 /* Align the current frag to a given power of two. The MIPS assembler
11760 also automatically adjusts any preceding label. */
11762 static void
11763 mips_align (int to, int fill, symbolS *label)
11765 mips_emit_delays (FALSE);
11766 frag_align (to, fill, 0);
11767 record_alignment (now_seg, to);
11768 if (label != NULL)
11770 assert (S_GET_SEGMENT (label) == now_seg);
11771 symbol_set_frag (label, frag_now);
11772 S_SET_VALUE (label, (valueT) frag_now_fix ());
11776 /* Align to a given power of two. .align 0 turns off the automatic
11777 alignment used by the data creating pseudo-ops. */
11779 static void
11780 s_align (int x ATTRIBUTE_UNUSED)
11782 register int temp;
11783 register long temp_fill;
11784 long max_alignment = 15;
11788 o Note that the assembler pulls down any immediately preceeding label
11789 to the aligned address.
11790 o It's not documented but auto alignment is reinstated by
11791 a .align pseudo instruction.
11792 o Note also that after auto alignment is turned off the mips assembler
11793 issues an error on attempt to assemble an improperly aligned data item.
11794 We don't.
11798 temp = get_absolute_expression ();
11799 if (temp > max_alignment)
11800 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11801 else if (temp < 0)
11803 as_warn (_("Alignment negative: 0 assumed."));
11804 temp = 0;
11806 if (*input_line_pointer == ',')
11808 ++input_line_pointer;
11809 temp_fill = get_absolute_expression ();
11811 else
11812 temp_fill = 0;
11813 if (temp)
11815 auto_align = 1;
11816 mips_align (temp, (int) temp_fill,
11817 insn_labels != NULL ? insn_labels->label : NULL);
11819 else
11821 auto_align = 0;
11824 demand_empty_rest_of_line ();
11827 void
11828 mips_flush_pending_output (void)
11830 mips_emit_delays (FALSE);
11831 mips_clear_insn_labels ();
11834 static void
11835 s_change_sec (int sec)
11837 segT seg;
11839 /* When generating embedded PIC code, we only use the .text, .lit8,
11840 .sdata and .sbss sections. We change the .data and .rdata
11841 pseudo-ops to use .sdata. */
11842 if (mips_pic == EMBEDDED_PIC
11843 && (sec == 'd' || sec == 'r'))
11844 sec = 's';
11846 #ifdef OBJ_ELF
11847 /* The ELF backend needs to know that we are changing sections, so
11848 that .previous works correctly. We could do something like check
11849 for an obj_section_change_hook macro, but that might be confusing
11850 as it would not be appropriate to use it in the section changing
11851 functions in read.c, since obj-elf.c intercepts those. FIXME:
11852 This should be cleaner, somehow. */
11853 obj_elf_section_change_hook ();
11854 #endif
11856 mips_emit_delays (FALSE);
11857 switch (sec)
11859 case 't':
11860 s_text (0);
11861 break;
11862 case 'd':
11863 s_data (0);
11864 break;
11865 case 'b':
11866 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11867 demand_empty_rest_of_line ();
11868 break;
11870 case 'r':
11871 if (USE_GLOBAL_POINTER_OPT)
11873 seg = subseg_new (RDATA_SECTION_NAME,
11874 (subsegT) get_absolute_expression ());
11875 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11877 bfd_set_section_flags (stdoutput, seg,
11878 (SEC_ALLOC
11879 | SEC_LOAD
11880 | SEC_READONLY
11881 | SEC_RELOC
11882 | SEC_DATA));
11883 if (strcmp (TARGET_OS, "elf") != 0)
11884 record_alignment (seg, 4);
11886 demand_empty_rest_of_line ();
11888 else
11890 as_bad (_("No read only data section in this object file format"));
11891 demand_empty_rest_of_line ();
11892 return;
11894 break;
11896 case 's':
11897 if (USE_GLOBAL_POINTER_OPT)
11899 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11900 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11902 bfd_set_section_flags (stdoutput, seg,
11903 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11904 | SEC_DATA);
11905 if (strcmp (TARGET_OS, "elf") != 0)
11906 record_alignment (seg, 4);
11908 demand_empty_rest_of_line ();
11909 break;
11911 else
11913 as_bad (_("Global pointers not supported; recompile -G 0"));
11914 demand_empty_rest_of_line ();
11915 return;
11919 auto_align = 1;
11922 void
11923 s_change_section (int ignore ATTRIBUTE_UNUSED)
11925 #ifdef OBJ_ELF
11926 char *section_name;
11927 char c;
11928 char next_c = 0;
11929 int section_type;
11930 int section_flag;
11931 int section_entry_size;
11932 int section_alignment;
11934 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11935 return;
11937 section_name = input_line_pointer;
11938 c = get_symbol_end ();
11939 if (c)
11940 next_c = *(input_line_pointer + 1);
11942 /* Do we have .section Name<,"flags">? */
11943 if (c != ',' || (c == ',' && next_c == '"'))
11945 /* just after name is now '\0'. */
11946 *input_line_pointer = c;
11947 input_line_pointer = section_name;
11948 obj_elf_section (ignore);
11949 return;
11951 input_line_pointer++;
11953 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11954 if (c == ',')
11955 section_type = get_absolute_expression ();
11956 else
11957 section_type = 0;
11958 if (*input_line_pointer++ == ',')
11959 section_flag = get_absolute_expression ();
11960 else
11961 section_flag = 0;
11962 if (*input_line_pointer++ == ',')
11963 section_entry_size = get_absolute_expression ();
11964 else
11965 section_entry_size = 0;
11966 if (*input_line_pointer++ == ',')
11967 section_alignment = get_absolute_expression ();
11968 else
11969 section_alignment = 0;
11971 section_name = xstrdup (section_name);
11973 obj_elf_change_section (section_name, section_type, section_flag,
11974 section_entry_size, 0, 0, 0);
11976 if (now_seg->name != section_name)
11977 free (section_name);
11978 #endif /* OBJ_ELF */
11981 void
11982 mips_enable_auto_align (void)
11984 auto_align = 1;
11987 static void
11988 s_cons (int log_size)
11990 symbolS *label;
11992 label = insn_labels != NULL ? insn_labels->label : NULL;
11993 mips_emit_delays (FALSE);
11994 if (log_size > 0 && auto_align)
11995 mips_align (log_size, 0, label);
11996 mips_clear_insn_labels ();
11997 cons (1 << log_size);
12000 static void
12001 s_float_cons (int type)
12003 symbolS *label;
12005 label = insn_labels != NULL ? insn_labels->label : NULL;
12007 mips_emit_delays (FALSE);
12009 if (auto_align)
12011 if (type == 'd')
12012 mips_align (3, 0, label);
12013 else
12014 mips_align (2, 0, label);
12017 mips_clear_insn_labels ();
12019 float_cons (type);
12022 /* Handle .globl. We need to override it because on Irix 5 you are
12023 permitted to say
12024 .globl foo .text
12025 where foo is an undefined symbol, to mean that foo should be
12026 considered to be the address of a function. */
12028 static void
12029 s_mips_globl (int x ATTRIBUTE_UNUSED)
12031 char *name;
12032 int c;
12033 symbolS *symbolP;
12034 flagword flag;
12036 name = input_line_pointer;
12037 c = get_symbol_end ();
12038 symbolP = symbol_find_or_make (name);
12039 *input_line_pointer = c;
12040 SKIP_WHITESPACE ();
12042 /* On Irix 5, every global symbol that is not explicitly labelled as
12043 being a function is apparently labelled as being an object. */
12044 flag = BSF_OBJECT;
12046 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12048 char *secname;
12049 asection *sec;
12051 secname = input_line_pointer;
12052 c = get_symbol_end ();
12053 sec = bfd_get_section_by_name (stdoutput, secname);
12054 if (sec == NULL)
12055 as_bad (_("%s: no such section"), secname);
12056 *input_line_pointer = c;
12058 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12059 flag = BSF_FUNCTION;
12062 symbol_get_bfdsym (symbolP)->flags |= flag;
12064 S_SET_EXTERNAL (symbolP);
12065 demand_empty_rest_of_line ();
12068 static void
12069 s_option (int x ATTRIBUTE_UNUSED)
12071 char *opt;
12072 char c;
12074 opt = input_line_pointer;
12075 c = get_symbol_end ();
12077 if (*opt == 'O')
12079 /* FIXME: What does this mean? */
12081 else if (strncmp (opt, "pic", 3) == 0)
12083 int i;
12085 i = atoi (opt + 3);
12086 if (i == 0)
12087 mips_pic = NO_PIC;
12088 else if (i == 2)
12090 mips_pic = SVR4_PIC;
12091 mips_abicalls = TRUE;
12093 else
12094 as_bad (_(".option pic%d not supported"), i);
12096 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
12098 if (g_switch_seen && g_switch_value != 0)
12099 as_warn (_("-G may not be used with SVR4 PIC code"));
12100 g_switch_value = 0;
12101 bfd_set_gp_size (stdoutput, 0);
12104 else
12105 as_warn (_("Unrecognized option \"%s\""), opt);
12107 *input_line_pointer = c;
12108 demand_empty_rest_of_line ();
12111 /* This structure is used to hold a stack of .set values. */
12113 struct mips_option_stack
12115 struct mips_option_stack *next;
12116 struct mips_set_options options;
12119 static struct mips_option_stack *mips_opts_stack;
12121 /* Handle the .set pseudo-op. */
12123 static void
12124 s_mipsset (int x ATTRIBUTE_UNUSED)
12126 char *name = input_line_pointer, ch;
12128 while (!is_end_of_line[(unsigned char) *input_line_pointer])
12129 ++input_line_pointer;
12130 ch = *input_line_pointer;
12131 *input_line_pointer = '\0';
12133 if (strcmp (name, "reorder") == 0)
12135 if (mips_opts.noreorder && prev_nop_frag != NULL)
12137 /* If we still have pending nops, we can discard them. The
12138 usual nop handling will insert any that are still
12139 needed. */
12140 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12141 * (mips_opts.mips16 ? 2 : 4));
12142 prev_nop_frag = NULL;
12144 mips_opts.noreorder = 0;
12146 else if (strcmp (name, "noreorder") == 0)
12148 mips_emit_delays (TRUE);
12149 mips_opts.noreorder = 1;
12150 mips_any_noreorder = 1;
12152 else if (strcmp (name, "at") == 0)
12154 mips_opts.noat = 0;
12156 else if (strcmp (name, "noat") == 0)
12158 mips_opts.noat = 1;
12160 else if (strcmp (name, "macro") == 0)
12162 mips_opts.warn_about_macros = 0;
12164 else if (strcmp (name, "nomacro") == 0)
12166 if (mips_opts.noreorder == 0)
12167 as_bad (_("`noreorder' must be set before `nomacro'"));
12168 mips_opts.warn_about_macros = 1;
12170 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12172 mips_opts.nomove = 0;
12174 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12176 mips_opts.nomove = 1;
12178 else if (strcmp (name, "bopt") == 0)
12180 mips_opts.nobopt = 0;
12182 else if (strcmp (name, "nobopt") == 0)
12184 mips_opts.nobopt = 1;
12186 else if (strcmp (name, "mips16") == 0
12187 || strcmp (name, "MIPS-16") == 0)
12188 mips_opts.mips16 = 1;
12189 else if (strcmp (name, "nomips16") == 0
12190 || strcmp (name, "noMIPS-16") == 0)
12191 mips_opts.mips16 = 0;
12192 else if (strcmp (name, "mips3d") == 0)
12193 mips_opts.ase_mips3d = 1;
12194 else if (strcmp (name, "nomips3d") == 0)
12195 mips_opts.ase_mips3d = 0;
12196 else if (strcmp (name, "mdmx") == 0)
12197 mips_opts.ase_mdmx = 1;
12198 else if (strcmp (name, "nomdmx") == 0)
12199 mips_opts.ase_mdmx = 0;
12200 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
12202 int reset = 0;
12204 /* Permit the user to change the ISA and architecture on the fly.
12205 Needless to say, misuse can cause serious problems. */
12206 if (strcmp (name, "mips0") == 0)
12208 reset = 1;
12209 mips_opts.isa = file_mips_isa;
12211 else if (strcmp (name, "mips1") == 0)
12212 mips_opts.isa = ISA_MIPS1;
12213 else if (strcmp (name, "mips2") == 0)
12214 mips_opts.isa = ISA_MIPS2;
12215 else if (strcmp (name, "mips3") == 0)
12216 mips_opts.isa = ISA_MIPS3;
12217 else if (strcmp (name, "mips4") == 0)
12218 mips_opts.isa = ISA_MIPS4;
12219 else if (strcmp (name, "mips5") == 0)
12220 mips_opts.isa = ISA_MIPS5;
12221 else if (strcmp (name, "mips32") == 0)
12222 mips_opts.isa = ISA_MIPS32;
12223 else if (strcmp (name, "mips32r2") == 0)
12224 mips_opts.isa = ISA_MIPS32R2;
12225 else if (strcmp (name, "mips64") == 0)
12226 mips_opts.isa = ISA_MIPS64;
12227 else if (strcmp (name, "mips64r2") == 0)
12228 mips_opts.isa = ISA_MIPS64R2;
12229 else if (strcmp (name, "arch=default") == 0)
12231 reset = 1;
12232 mips_opts.arch = file_mips_arch;
12233 mips_opts.isa = file_mips_isa;
12235 else if (strncmp (name, "arch=", 5) == 0)
12237 const struct mips_cpu_info *p;
12239 p = mips_parse_cpu("internal use", name + 5);
12240 if (!p)
12241 as_bad (_("unknown architecture %s"), name + 5);
12242 else
12244 mips_opts.arch = p->cpu;
12245 mips_opts.isa = p->isa;
12248 else
12249 as_bad (_("unknown ISA level %s"), name + 4);
12251 switch (mips_opts.isa)
12253 case 0:
12254 break;
12255 case ISA_MIPS1:
12256 case ISA_MIPS2:
12257 case ISA_MIPS32:
12258 case ISA_MIPS32R2:
12259 mips_opts.gp32 = 1;
12260 mips_opts.fp32 = 1;
12261 break;
12262 case ISA_MIPS3:
12263 case ISA_MIPS4:
12264 case ISA_MIPS5:
12265 case ISA_MIPS64:
12266 case ISA_MIPS64R2:
12267 mips_opts.gp32 = 0;
12268 mips_opts.fp32 = 0;
12269 break;
12270 default:
12271 as_bad (_("unknown ISA level %s"), name + 4);
12272 break;
12274 if (reset)
12276 mips_opts.gp32 = file_mips_gp32;
12277 mips_opts.fp32 = file_mips_fp32;
12280 else if (strcmp (name, "autoextend") == 0)
12281 mips_opts.noautoextend = 0;
12282 else if (strcmp (name, "noautoextend") == 0)
12283 mips_opts.noautoextend = 1;
12284 else if (strcmp (name, "push") == 0)
12286 struct mips_option_stack *s;
12288 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12289 s->next = mips_opts_stack;
12290 s->options = mips_opts;
12291 mips_opts_stack = s;
12293 else if (strcmp (name, "pop") == 0)
12295 struct mips_option_stack *s;
12297 s = mips_opts_stack;
12298 if (s == NULL)
12299 as_bad (_(".set pop with no .set push"));
12300 else
12302 /* If we're changing the reorder mode we need to handle
12303 delay slots correctly. */
12304 if (s->options.noreorder && ! mips_opts.noreorder)
12305 mips_emit_delays (TRUE);
12306 else if (! s->options.noreorder && mips_opts.noreorder)
12308 if (prev_nop_frag != NULL)
12310 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12311 * (mips_opts.mips16 ? 2 : 4));
12312 prev_nop_frag = NULL;
12316 mips_opts = s->options;
12317 mips_opts_stack = s->next;
12318 free (s);
12321 else
12323 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12325 *input_line_pointer = ch;
12326 demand_empty_rest_of_line ();
12329 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
12330 .option pic2. It means to generate SVR4 PIC calls. */
12332 static void
12333 s_abicalls (int ignore ATTRIBUTE_UNUSED)
12335 mips_pic = SVR4_PIC;
12336 mips_abicalls = TRUE;
12337 if (USE_GLOBAL_POINTER_OPT)
12339 if (g_switch_seen && g_switch_value != 0)
12340 as_warn (_("-G may not be used with SVR4 PIC code"));
12341 g_switch_value = 0;
12343 bfd_set_gp_size (stdoutput, 0);
12344 demand_empty_rest_of_line ();
12347 /* Handle the .cpload pseudo-op. This is used when generating SVR4
12348 PIC code. It sets the $gp register for the function based on the
12349 function address, which is in the register named in the argument.
12350 This uses a relocation against _gp_disp, which is handled specially
12351 by the linker. The result is:
12352 lui $gp,%hi(_gp_disp)
12353 addiu $gp,$gp,%lo(_gp_disp)
12354 addu $gp,$gp,.cpload argument
12355 The .cpload argument is normally $25 == $t9. */
12357 static void
12358 s_cpload (int ignore ATTRIBUTE_UNUSED)
12360 expressionS ex;
12361 int icnt = 0;
12363 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12364 .cpload is ignored. */
12365 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12367 s_ignore (0);
12368 return;
12371 /* .cpload should be in a .set noreorder section. */
12372 if (mips_opts.noreorder == 0)
12373 as_warn (_(".cpload not in noreorder section"));
12375 ex.X_op = O_symbol;
12376 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12377 ex.X_op_symbol = NULL;
12378 ex.X_add_number = 0;
12380 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12381 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12383 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12384 macro_build (NULL, &icnt, &ex, "addiu", "t,r,j", mips_gp_register,
12385 mips_gp_register, BFD_RELOC_LO16);
12387 macro_build (NULL, &icnt, NULL, "addu", "d,v,t", mips_gp_register,
12388 mips_gp_register, tc_get_register (0));
12390 demand_empty_rest_of_line ();
12393 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12394 .cpsetup $reg1, offset|$reg2, label
12396 If offset is given, this results in:
12397 sd $gp, offset($sp)
12398 lui $gp, %hi(%neg(%gp_rel(label)))
12399 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12400 daddu $gp, $gp, $reg1
12402 If $reg2 is given, this results in:
12403 daddu $reg2, $gp, $0
12404 lui $gp, %hi(%neg(%gp_rel(label)))
12405 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12406 daddu $gp, $gp, $reg1
12407 $reg1 is normally $25 == $t9. */
12408 static void
12409 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
12411 expressionS ex_off;
12412 expressionS ex_sym;
12413 int reg1;
12414 int icnt = 0;
12415 char *f;
12417 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12418 We also need NewABI support. */
12419 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12421 s_ignore (0);
12422 return;
12425 reg1 = tc_get_register (0);
12426 SKIP_WHITESPACE ();
12427 if (*input_line_pointer != ',')
12429 as_bad (_("missing argument separator ',' for .cpsetup"));
12430 return;
12432 else
12433 ++input_line_pointer;
12434 SKIP_WHITESPACE ();
12435 if (*input_line_pointer == '$')
12437 mips_cpreturn_register = tc_get_register (0);
12438 mips_cpreturn_offset = -1;
12440 else
12442 mips_cpreturn_offset = get_absolute_expression ();
12443 mips_cpreturn_register = -1;
12445 SKIP_WHITESPACE ();
12446 if (*input_line_pointer != ',')
12448 as_bad (_("missing argument separator ',' for .cpsetup"));
12449 return;
12451 else
12452 ++input_line_pointer;
12453 SKIP_WHITESPACE ();
12454 expression (&ex_sym);
12456 if (mips_cpreturn_register == -1)
12458 ex_off.X_op = O_constant;
12459 ex_off.X_add_symbol = NULL;
12460 ex_off.X_op_symbol = NULL;
12461 ex_off.X_add_number = mips_cpreturn_offset;
12463 macro_build (NULL, &icnt, &ex_off, "sd", "t,o(b)", mips_gp_register,
12464 BFD_RELOC_LO16, SP);
12466 else
12467 macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_cpreturn_register,
12468 mips_gp_register, 0);
12470 /* Ensure there's room for the next two instructions, so that `f'
12471 doesn't end up with an address in the wrong frag. */
12472 frag_grow (8);
12473 f = frag_more (0);
12474 macro_build (NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12475 BFD_RELOC_GPREL16);
12476 fix_new (frag_now, f - frag_now->fr_literal,
12477 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12478 fix_new (frag_now, f - frag_now->fr_literal,
12479 4, NULL, 0, 0, BFD_RELOC_HI16_S);
12481 f = frag_more (0);
12482 macro_build (NULL, &icnt, &ex_sym, "addiu", "t,r,j", mips_gp_register,
12483 mips_gp_register, BFD_RELOC_GPREL16);
12484 fix_new (frag_now, f - frag_now->fr_literal,
12485 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12486 fix_new (frag_now, f - frag_now->fr_literal,
12487 4, NULL, 0, 0, BFD_RELOC_LO16);
12489 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12490 mips_gp_register, reg1);
12492 demand_empty_rest_of_line ();
12495 static void
12496 s_cplocal (int ignore ATTRIBUTE_UNUSED)
12498 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12499 .cplocal is ignored. */
12500 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12502 s_ignore (0);
12503 return;
12506 mips_gp_register = tc_get_register (0);
12507 demand_empty_rest_of_line ();
12510 /* Handle the .cprestore pseudo-op. This stores $gp into a given
12511 offset from $sp. The offset is remembered, and after making a PIC
12512 call $gp is restored from that location. */
12514 static void
12515 s_cprestore (int ignore ATTRIBUTE_UNUSED)
12517 expressionS ex;
12518 int icnt = 0;
12520 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12521 .cprestore is ignored. */
12522 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12524 s_ignore (0);
12525 return;
12528 mips_cprestore_offset = get_absolute_expression ();
12529 mips_cprestore_valid = 1;
12531 ex.X_op = O_constant;
12532 ex.X_add_symbol = NULL;
12533 ex.X_op_symbol = NULL;
12534 ex.X_add_number = mips_cprestore_offset;
12536 macro_build_ldst_constoffset (NULL, &icnt, &ex, ADDRESS_STORE_INSN,
12537 mips_gp_register, SP, HAVE_64BIT_ADDRESSES);
12539 demand_empty_rest_of_line ();
12542 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12543 was given in the preceeding .cpsetup, it results in:
12544 ld $gp, offset($sp)
12546 If a register $reg2 was given there, it results in:
12547 daddu $gp, $reg2, $0
12549 static void
12550 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
12552 expressionS ex;
12553 int icnt = 0;
12555 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12556 We also need NewABI support. */
12557 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12559 s_ignore (0);
12560 return;
12563 if (mips_cpreturn_register == -1)
12565 ex.X_op = O_constant;
12566 ex.X_add_symbol = NULL;
12567 ex.X_op_symbol = NULL;
12568 ex.X_add_number = mips_cpreturn_offset;
12570 macro_build (NULL, &icnt, &ex, "ld", "t,o(b)", mips_gp_register,
12571 BFD_RELOC_LO16, SP);
12573 else
12574 macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_gp_register,
12575 mips_cpreturn_register, 0);
12577 demand_empty_rest_of_line ();
12580 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12581 code. It sets the offset to use in gp_rel relocations. */
12583 static void
12584 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
12586 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12587 We also need NewABI support. */
12588 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12590 s_ignore (0);
12591 return;
12594 mips_gprel_offset = get_absolute_expression ();
12596 demand_empty_rest_of_line ();
12599 /* Handle the .gpword pseudo-op. This is used when generating PIC
12600 code. It generates a 32 bit GP relative reloc. */
12602 static void
12603 s_gpword (int ignore ATTRIBUTE_UNUSED)
12605 symbolS *label;
12606 expressionS ex;
12607 char *p;
12609 /* When not generating PIC code, this is treated as .word. */
12610 if (mips_pic != SVR4_PIC)
12612 s_cons (2);
12613 return;
12616 label = insn_labels != NULL ? insn_labels->label : NULL;
12617 mips_emit_delays (TRUE);
12618 if (auto_align)
12619 mips_align (2, 0, label);
12620 mips_clear_insn_labels ();
12622 expression (&ex);
12624 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12626 as_bad (_("Unsupported use of .gpword"));
12627 ignore_rest_of_line ();
12630 p = frag_more (4);
12631 md_number_to_chars (p, 0, 4);
12632 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12633 BFD_RELOC_GPREL32);
12635 demand_empty_rest_of_line ();
12638 static void
12639 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12641 symbolS *label;
12642 expressionS ex;
12643 char *p;
12645 /* When not generating PIC code, this is treated as .dword. */
12646 if (mips_pic != SVR4_PIC)
12648 s_cons (3);
12649 return;
12652 label = insn_labels != NULL ? insn_labels->label : NULL;
12653 mips_emit_delays (TRUE);
12654 if (auto_align)
12655 mips_align (3, 0, label);
12656 mips_clear_insn_labels ();
12658 expression (&ex);
12660 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12662 as_bad (_("Unsupported use of .gpdword"));
12663 ignore_rest_of_line ();
12666 p = frag_more (8);
12667 md_number_to_chars (p, 0, 8);
12668 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12669 BFD_RELOC_GPREL32);
12671 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12672 ex.X_op = O_absent;
12673 ex.X_add_symbol = 0;
12674 ex.X_add_number = 0;
12675 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12676 BFD_RELOC_64);
12678 demand_empty_rest_of_line ();
12681 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12682 tables in SVR4 PIC code. */
12684 static void
12685 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12687 int icnt = 0;
12688 int reg;
12690 /* This is ignored when not generating SVR4 PIC code. */
12691 if (mips_pic != SVR4_PIC)
12693 s_ignore (0);
12694 return;
12697 /* Add $gp to the register named as an argument. */
12698 reg = tc_get_register (0);
12699 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
12700 reg, reg, mips_gp_register);
12702 demand_empty_rest_of_line ();
12705 /* Handle the .insn pseudo-op. This marks instruction labels in
12706 mips16 mode. This permits the linker to handle them specially,
12707 such as generating jalx instructions when needed. We also make
12708 them odd for the duration of the assembly, in order to generate the
12709 right sort of code. We will make them even in the adjust_symtab
12710 routine, while leaving them marked. This is convenient for the
12711 debugger and the disassembler. The linker knows to make them odd
12712 again. */
12714 static void
12715 s_insn (int ignore ATTRIBUTE_UNUSED)
12717 mips16_mark_labels ();
12719 demand_empty_rest_of_line ();
12722 /* Handle a .stabn directive. We need these in order to mark a label
12723 as being a mips16 text label correctly. Sometimes the compiler
12724 will emit a label, followed by a .stabn, and then switch sections.
12725 If the label and .stabn are in mips16 mode, then the label is
12726 really a mips16 text label. */
12728 static void
12729 s_mips_stab (int type)
12731 if (type == 'n')
12732 mips16_mark_labels ();
12734 s_stab (type);
12737 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12740 static void
12741 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12743 char *name;
12744 int c;
12745 symbolS *symbolP;
12746 expressionS exp;
12748 name = input_line_pointer;
12749 c = get_symbol_end ();
12750 symbolP = symbol_find_or_make (name);
12751 S_SET_WEAK (symbolP);
12752 *input_line_pointer = c;
12754 SKIP_WHITESPACE ();
12756 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12758 if (S_IS_DEFINED (symbolP))
12760 as_bad ("ignoring attempt to redefine symbol %s",
12761 S_GET_NAME (symbolP));
12762 ignore_rest_of_line ();
12763 return;
12766 if (*input_line_pointer == ',')
12768 ++input_line_pointer;
12769 SKIP_WHITESPACE ();
12772 expression (&exp);
12773 if (exp.X_op != O_symbol)
12775 as_bad ("bad .weakext directive");
12776 ignore_rest_of_line ();
12777 return;
12779 symbol_set_value_expression (symbolP, &exp);
12782 demand_empty_rest_of_line ();
12785 /* Parse a register string into a number. Called from the ECOFF code
12786 to parse .frame. The argument is non-zero if this is the frame
12787 register, so that we can record it in mips_frame_reg. */
12790 tc_get_register (int frame)
12792 int reg;
12794 SKIP_WHITESPACE ();
12795 if (*input_line_pointer++ != '$')
12797 as_warn (_("expected `$'"));
12798 reg = ZERO;
12800 else if (ISDIGIT (*input_line_pointer))
12802 reg = get_absolute_expression ();
12803 if (reg < 0 || reg >= 32)
12805 as_warn (_("Bad register number"));
12806 reg = ZERO;
12809 else
12811 if (strncmp (input_line_pointer, "ra", 2) == 0)
12813 reg = RA;
12814 input_line_pointer += 2;
12816 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12818 reg = FP;
12819 input_line_pointer += 2;
12821 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12823 reg = SP;
12824 input_line_pointer += 2;
12826 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12828 reg = GP;
12829 input_line_pointer += 2;
12831 else if (strncmp (input_line_pointer, "at", 2) == 0)
12833 reg = AT;
12834 input_line_pointer += 2;
12836 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12838 reg = KT0;
12839 input_line_pointer += 3;
12841 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12843 reg = KT1;
12844 input_line_pointer += 3;
12846 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12848 reg = ZERO;
12849 input_line_pointer += 4;
12851 else
12853 as_warn (_("Unrecognized register name"));
12854 reg = ZERO;
12855 while (ISALNUM(*input_line_pointer))
12856 input_line_pointer++;
12859 if (frame)
12861 mips_frame_reg = reg != 0 ? reg : SP;
12862 mips_frame_reg_valid = 1;
12863 mips_cprestore_valid = 0;
12865 return reg;
12868 valueT
12869 md_section_align (asection *seg, valueT addr)
12871 int align = bfd_get_section_alignment (stdoutput, seg);
12873 #ifdef OBJ_ELF
12874 /* We don't need to align ELF sections to the full alignment.
12875 However, Irix 5 may prefer that we align them at least to a 16
12876 byte boundary. We don't bother to align the sections if we are
12877 targeted for an embedded system. */
12878 if (strcmp (TARGET_OS, "elf") == 0)
12879 return addr;
12880 if (align > 4)
12881 align = 4;
12882 #endif
12884 return ((addr + (1 << align) - 1) & (-1 << align));
12887 /* Utility routine, called from above as well. If called while the
12888 input file is still being read, it's only an approximation. (For
12889 example, a symbol may later become defined which appeared to be
12890 undefined earlier.) */
12892 static int
12893 nopic_need_relax (symbolS *sym, int before_relaxing)
12895 if (sym == 0)
12896 return 0;
12898 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12900 const char *symname;
12901 int change;
12903 /* Find out whether this symbol can be referenced off the $gp
12904 register. It can be if it is smaller than the -G size or if
12905 it is in the .sdata or .sbss section. Certain symbols can
12906 not be referenced off the $gp, although it appears as though
12907 they can. */
12908 symname = S_GET_NAME (sym);
12909 if (symname != (const char *) NULL
12910 && (strcmp (symname, "eprol") == 0
12911 || strcmp (symname, "etext") == 0
12912 || strcmp (symname, "_gp") == 0
12913 || strcmp (symname, "edata") == 0
12914 || strcmp (symname, "_fbss") == 0
12915 || strcmp (symname, "_fdata") == 0
12916 || strcmp (symname, "_ftext") == 0
12917 || strcmp (symname, "end") == 0
12918 || strcmp (symname, "_gp_disp") == 0))
12919 change = 1;
12920 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12921 && (0
12922 #ifndef NO_ECOFF_DEBUGGING
12923 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12924 && (symbol_get_obj (sym)->ecoff_extern_size
12925 <= g_switch_value))
12926 #endif
12927 /* We must defer this decision until after the whole
12928 file has been read, since there might be a .extern
12929 after the first use of this symbol. */
12930 || (before_relaxing
12931 #ifndef NO_ECOFF_DEBUGGING
12932 && symbol_get_obj (sym)->ecoff_extern_size == 0
12933 #endif
12934 && S_GET_VALUE (sym) == 0)
12935 || (S_GET_VALUE (sym) != 0
12936 && S_GET_VALUE (sym) <= g_switch_value)))
12937 change = 0;
12938 else
12940 const char *segname;
12942 segname = segment_name (S_GET_SEGMENT (sym));
12943 assert (strcmp (segname, ".lit8") != 0
12944 && strcmp (segname, ".lit4") != 0);
12945 change = (strcmp (segname, ".sdata") != 0
12946 && strcmp (segname, ".sbss") != 0
12947 && strncmp (segname, ".sdata.", 7) != 0
12948 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12950 return change;
12952 else
12953 /* We are not optimizing for the $gp register. */
12954 return 1;
12958 /* Return true if the given symbol should be considered local for SVR4 PIC. */
12960 static bfd_boolean
12961 pic_need_relax (symbolS *sym, asection *segtype)
12963 asection *symsec;
12964 bfd_boolean linkonce;
12966 /* Handle the case of a symbol equated to another symbol. */
12967 while (symbol_equated_reloc_p (sym))
12969 symbolS *n;
12971 /* It's possible to get a loop here in a badly written
12972 program. */
12973 n = symbol_get_value_expression (sym)->X_add_symbol;
12974 if (n == sym)
12975 break;
12976 sym = n;
12979 symsec = S_GET_SEGMENT (sym);
12981 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12982 linkonce = FALSE;
12983 if (symsec != segtype && ! S_IS_LOCAL (sym))
12985 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12986 != 0)
12987 linkonce = TRUE;
12989 /* The GNU toolchain uses an extension for ELF: a section
12990 beginning with the magic string .gnu.linkonce is a linkonce
12991 section. */
12992 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12993 sizeof ".gnu.linkonce" - 1) == 0)
12994 linkonce = TRUE;
12997 /* This must duplicate the test in adjust_reloc_syms. */
12998 return (symsec != &bfd_und_section
12999 && symsec != &bfd_abs_section
13000 && ! bfd_is_com_section (symsec)
13001 && !linkonce
13002 #ifdef OBJ_ELF
13003 /* A global or weak symbol is treated as external. */
13004 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
13005 || (! S_IS_WEAK (sym)
13006 && (! S_IS_EXTERNAL (sym)
13007 || mips_pic == EMBEDDED_PIC)))
13008 #endif
13013 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13014 extended opcode. SEC is the section the frag is in. */
13016 static int
13017 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
13019 int type;
13020 register const struct mips16_immed_operand *op;
13021 offsetT val;
13022 int mintiny, maxtiny;
13023 segT symsec;
13024 fragS *sym_frag;
13026 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13027 return 0;
13028 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13029 return 1;
13031 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13032 op = mips16_immed_operands;
13033 while (op->type != type)
13035 ++op;
13036 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13039 if (op->unsp)
13041 if (type == '<' || type == '>' || type == '[' || type == ']')
13043 mintiny = 1;
13044 maxtiny = 1 << op->nbits;
13046 else
13048 mintiny = 0;
13049 maxtiny = (1 << op->nbits) - 1;
13052 else
13054 mintiny = - (1 << (op->nbits - 1));
13055 maxtiny = (1 << (op->nbits - 1)) - 1;
13058 sym_frag = symbol_get_frag (fragp->fr_symbol);
13059 val = S_GET_VALUE (fragp->fr_symbol);
13060 symsec = S_GET_SEGMENT (fragp->fr_symbol);
13062 if (op->pcrel)
13064 addressT addr;
13066 /* We won't have the section when we are called from
13067 mips_relax_frag. However, we will always have been called
13068 from md_estimate_size_before_relax first. If this is a
13069 branch to a different section, we mark it as such. If SEC is
13070 NULL, and the frag is not marked, then it must be a branch to
13071 the same section. */
13072 if (sec == NULL)
13074 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13075 return 1;
13077 else
13079 /* Must have been called from md_estimate_size_before_relax. */
13080 if (symsec != sec)
13082 fragp->fr_subtype =
13083 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13085 /* FIXME: We should support this, and let the linker
13086 catch branches and loads that are out of range. */
13087 as_bad_where (fragp->fr_file, fragp->fr_line,
13088 _("unsupported PC relative reference to different section"));
13090 return 1;
13092 if (fragp != sym_frag && sym_frag->fr_address == 0)
13093 /* Assume non-extended on the first relaxation pass.
13094 The address we have calculated will be bogus if this is
13095 a forward branch to another frag, as the forward frag
13096 will have fr_address == 0. */
13097 return 0;
13100 /* In this case, we know for sure that the symbol fragment is in
13101 the same section. If the relax_marker of the symbol fragment
13102 differs from the relax_marker of this fragment, we have not
13103 yet adjusted the symbol fragment fr_address. We want to add
13104 in STRETCH in order to get a better estimate of the address.
13105 This particularly matters because of the shift bits. */
13106 if (stretch != 0
13107 && sym_frag->relax_marker != fragp->relax_marker)
13109 fragS *f;
13111 /* Adjust stretch for any alignment frag. Note that if have
13112 been expanding the earlier code, the symbol may be
13113 defined in what appears to be an earlier frag. FIXME:
13114 This doesn't handle the fr_subtype field, which specifies
13115 a maximum number of bytes to skip when doing an
13116 alignment. */
13117 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
13119 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13121 if (stretch < 0)
13122 stretch = - ((- stretch)
13123 & ~ ((1 << (int) f->fr_offset) - 1));
13124 else
13125 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13126 if (stretch == 0)
13127 break;
13130 if (f != NULL)
13131 val += stretch;
13134 addr = fragp->fr_address + fragp->fr_fix;
13136 /* The base address rules are complicated. The base address of
13137 a branch is the following instruction. The base address of a
13138 PC relative load or add is the instruction itself, but if it
13139 is in a delay slot (in which case it can not be extended) use
13140 the address of the instruction whose delay slot it is in. */
13141 if (type == 'p' || type == 'q')
13143 addr += 2;
13145 /* If we are currently assuming that this frag should be
13146 extended, then, the current address is two bytes
13147 higher. */
13148 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13149 addr += 2;
13151 /* Ignore the low bit in the target, since it will be set
13152 for a text label. */
13153 if ((val & 1) != 0)
13154 --val;
13156 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13157 addr -= 4;
13158 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13159 addr -= 2;
13161 val -= addr & ~ ((1 << op->shift) - 1);
13163 /* Branch offsets have an implicit 0 in the lowest bit. */
13164 if (type == 'p' || type == 'q')
13165 val /= 2;
13167 /* If any of the shifted bits are set, we must use an extended
13168 opcode. If the address depends on the size of this
13169 instruction, this can lead to a loop, so we arrange to always
13170 use an extended opcode. We only check this when we are in
13171 the main relaxation loop, when SEC is NULL. */
13172 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13174 fragp->fr_subtype =
13175 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13176 return 1;
13179 /* If we are about to mark a frag as extended because the value
13180 is precisely maxtiny + 1, then there is a chance of an
13181 infinite loop as in the following code:
13182 la $4,foo
13183 .skip 1020
13184 .align 2
13185 foo:
13186 In this case when the la is extended, foo is 0x3fc bytes
13187 away, so the la can be shrunk, but then foo is 0x400 away, so
13188 the la must be extended. To avoid this loop, we mark the
13189 frag as extended if it was small, and is about to become
13190 extended with a value of maxtiny + 1. */
13191 if (val == ((maxtiny + 1) << op->shift)
13192 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13193 && sec == NULL)
13195 fragp->fr_subtype =
13196 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13197 return 1;
13200 else if (symsec != absolute_section && sec != NULL)
13201 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13203 if ((val & ((1 << op->shift) - 1)) != 0
13204 || val < (mintiny << op->shift)
13205 || val > (maxtiny << op->shift))
13206 return 1;
13207 else
13208 return 0;
13211 /* Compute the length of a branch sequence, and adjust the
13212 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13213 worst-case length is computed, with UPDATE being used to indicate
13214 whether an unconditional (-1), branch-likely (+1) or regular (0)
13215 branch is to be computed. */
13216 static int
13217 relaxed_branch_length (fragS *fragp, asection *sec, int update)
13219 bfd_boolean toofar;
13220 int length;
13222 if (fragp
13223 && S_IS_DEFINED (fragp->fr_symbol)
13224 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13226 addressT addr;
13227 offsetT val;
13229 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13231 addr = fragp->fr_address + fragp->fr_fix + 4;
13233 val -= addr;
13235 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13237 else if (fragp)
13238 /* If the symbol is not defined or it's in a different segment,
13239 assume the user knows what's going on and emit a short
13240 branch. */
13241 toofar = FALSE;
13242 else
13243 toofar = TRUE;
13245 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13246 fragp->fr_subtype
13247 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13248 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13249 RELAX_BRANCH_LINK (fragp->fr_subtype),
13250 toofar);
13252 length = 4;
13253 if (toofar)
13255 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13256 length += 8;
13258 if (mips_pic != NO_PIC)
13260 /* Additional space for PIC loading of target address. */
13261 length += 8;
13262 if (mips_opts.isa == ISA_MIPS1)
13263 /* Additional space for $at-stabilizing nop. */
13264 length += 4;
13267 /* If branch is conditional. */
13268 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13269 length += 8;
13272 return length;
13275 /* Estimate the size of a frag before relaxing. Unless this is the
13276 mips16, we are not really relaxing here, and the final size is
13277 encoded in the subtype information. For the mips16, we have to
13278 decide whether we are using an extended opcode or not. */
13281 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
13283 int change;
13285 if (RELAX_BRANCH_P (fragp->fr_subtype))
13288 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13290 return fragp->fr_var;
13293 if (RELAX_MIPS16_P (fragp->fr_subtype))
13294 /* We don't want to modify the EXTENDED bit here; it might get us
13295 into infinite loops. We change it only in mips_relax_frag(). */
13296 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13298 if (mips_pic == NO_PIC)
13299 change = nopic_need_relax (fragp->fr_symbol, 0);
13300 else if (mips_pic == SVR4_PIC)
13301 change = pic_need_relax (fragp->fr_symbol, segtype);
13302 else
13303 abort ();
13305 if (change)
13307 /* Record the offset to the first reloc in the fr_opcode field.
13308 This lets md_convert_frag and tc_gen_reloc know that the code
13309 must be expanded. */
13310 fragp->fr_opcode = (fragp->fr_literal
13311 + fragp->fr_fix
13312 - RELAX_OLD (fragp->fr_subtype)
13313 + RELAX_RELOC1 (fragp->fr_subtype));
13314 /* FIXME: This really needs as_warn_where. */
13315 if (RELAX_WARN (fragp->fr_subtype))
13316 as_warn (_("AT used after \".set noat\" or macro used after "
13317 "\".set nomacro\""));
13319 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13322 return 0;
13325 /* This is called to see whether a reloc against a defined symbol
13326 should be converted into a reloc against a section. Don't adjust
13327 MIPS16 jump relocations, so we don't have to worry about the format
13328 of the offset in the .o file. Don't adjust relocations against
13329 mips16 symbols, so that the linker can find them if it needs to set
13330 up a stub. */
13333 mips_fix_adjustable (fixS *fixp)
13335 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13336 return 0;
13338 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13339 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13340 return 0;
13342 if (fixp->fx_addsy == NULL)
13343 return 1;
13345 #ifdef OBJ_ELF
13346 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13347 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13348 && fixp->fx_subsy == NULL)
13349 return 0;
13350 #endif
13352 return 1;
13355 /* Translate internal representation of relocation info to BFD target
13356 format. */
13358 arelent **
13359 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
13361 static arelent *retval[4];
13362 arelent *reloc;
13363 bfd_reloc_code_real_type code;
13365 memset (retval, 0, sizeof(retval));
13366 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
13367 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13368 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13369 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13371 if (mips_pic == EMBEDDED_PIC
13372 && SWITCH_TABLE (fixp))
13374 /* For a switch table entry we use a special reloc. The addend
13375 is actually the difference between the reloc address and the
13376 subtrahend. */
13377 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13378 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13379 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13380 fixp->fx_r_type = BFD_RELOC_GPREL32;
13382 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13384 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13385 reloc->addend = fixp->fx_addnumber;
13386 else
13388 /* We use a special addend for an internal RELLO reloc. */
13389 if (symbol_section_p (fixp->fx_addsy))
13390 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13391 else
13392 reloc->addend = fixp->fx_addnumber + reloc->address;
13395 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13397 assert (fixp->fx_next != NULL
13398 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13400 /* The reloc is relative to the RELLO; adjust the addend
13401 accordingly. */
13402 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13403 reloc->addend = fixp->fx_next->fx_addnumber;
13404 else
13406 /* We use a special addend for an internal RELHI reloc. */
13407 if (symbol_section_p (fixp->fx_addsy))
13408 reloc->addend = (fixp->fx_next->fx_frag->fr_address
13409 + fixp->fx_next->fx_where
13410 - S_GET_VALUE (fixp->fx_subsy));
13411 else
13412 reloc->addend = (fixp->fx_addnumber
13413 + fixp->fx_next->fx_frag->fr_address
13414 + fixp->fx_next->fx_where);
13417 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13418 reloc->addend = fixp->fx_addnumber;
13419 else
13421 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13422 /* A gruesome hack which is a result of the gruesome gas reloc
13423 handling. */
13424 reloc->addend = reloc->address;
13425 else
13426 reloc->addend = -reloc->address;
13429 /* If this is a variant frag, we may need to adjust the existing
13430 reloc and generate a new one. */
13431 if (fixp->fx_frag->fr_opcode != NULL
13432 && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13433 && ! HAVE_NEWABI)
13434 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_DISP
13435 && HAVE_NEWABI)
13436 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13437 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13438 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13439 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13440 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13441 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13444 arelent *reloc2;
13446 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13448 /* If this is not the last reloc in this frag, then we have two
13449 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13450 CALL_HI16/CALL_LO16, both of which are being replaced. Let
13451 the second one handle all of them. */
13452 if (fixp->fx_next != NULL
13453 && fixp->fx_frag == fixp->fx_next->fx_frag)
13455 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13456 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13457 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13458 && (fixp->fx_next->fx_r_type
13459 == BFD_RELOC_MIPS_GOT_LO16))
13460 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13461 && (fixp->fx_next->fx_r_type
13462 == BFD_RELOC_MIPS_CALL_LO16)));
13463 retval[0] = NULL;
13464 return retval;
13467 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13468 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13469 reloc->addend += fixp->fx_frag->tc_frag_data.tc_fr_offset;
13470 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13471 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13472 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13473 reloc2->address = (reloc->address
13474 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13475 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13476 reloc2->addend = fixp->fx_addnumber - S_GET_VALUE (fixp->fx_addsy)
13477 + fixp->fx_frag->tc_frag_data.tc_fr_offset;
13478 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13479 assert (reloc2->howto != NULL);
13481 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13483 arelent *reloc3;
13485 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13486 *reloc3 = *reloc2;
13487 reloc3->address += 4;
13490 if (mips_pic == NO_PIC)
13492 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13493 fixp->fx_r_type = BFD_RELOC_HI16_S;
13495 else if (mips_pic == SVR4_PIC)
13497 switch (fixp->fx_r_type)
13499 default:
13500 abort ();
13501 case BFD_RELOC_MIPS_GOT16:
13502 break;
13503 case BFD_RELOC_MIPS_GOT_LO16:
13504 case BFD_RELOC_MIPS_CALL_LO16:
13505 if (HAVE_NEWABI)
13507 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13508 reloc2->howto = bfd_reloc_type_lookup
13509 (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13511 else
13512 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13513 break;
13514 case BFD_RELOC_MIPS_CALL16:
13515 case BFD_RELOC_MIPS_GOT_OFST:
13516 case BFD_RELOC_MIPS_GOT_DISP:
13517 if (HAVE_NEWABI)
13519 /* It may seem nonsensical to relax GOT_DISP to
13520 GOT_DISP, but we're actually turning a GOT_DISP
13521 without offset into a GOT_DISP with an offset,
13522 getting rid of the separate addition, which we can
13523 do when the symbol is found to be local. */
13524 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13525 retval[1] = NULL;
13527 else
13528 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13529 break;
13532 else
13533 abort ();
13536 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13537 entry to be used in the relocation's section offset. */
13538 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13540 reloc->address = reloc->addend;
13541 reloc->addend = 0;
13544 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13545 fixup_segment converted a non-PC relative reloc into a PC
13546 relative reloc. In such a case, we need to convert the reloc
13547 code. */
13548 code = fixp->fx_r_type;
13549 if (fixp->fx_pcrel)
13551 switch (code)
13553 case BFD_RELOC_8:
13554 code = BFD_RELOC_8_PCREL;
13555 break;
13556 case BFD_RELOC_16:
13557 code = BFD_RELOC_16_PCREL;
13558 break;
13559 case BFD_RELOC_32:
13560 code = BFD_RELOC_32_PCREL;
13561 break;
13562 case BFD_RELOC_64:
13563 code = BFD_RELOC_64_PCREL;
13564 break;
13565 case BFD_RELOC_8_PCREL:
13566 case BFD_RELOC_16_PCREL:
13567 case BFD_RELOC_32_PCREL:
13568 case BFD_RELOC_64_PCREL:
13569 case BFD_RELOC_16_PCREL_S2:
13570 case BFD_RELOC_PCREL_HI16_S:
13571 case BFD_RELOC_PCREL_LO16:
13572 break;
13573 default:
13574 as_bad_where (fixp->fx_file, fixp->fx_line,
13575 _("Cannot make %s relocation PC relative"),
13576 bfd_get_reloc_code_name (code));
13580 /* To support a PC relative reloc when generating embedded PIC code
13581 for ECOFF, we use a Cygnus extension. We check for that here to
13582 make sure that we don't let such a reloc escape normally. */
13583 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13584 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13585 && code == BFD_RELOC_16_PCREL_S2
13586 && mips_pic != EMBEDDED_PIC)
13587 reloc->howto = NULL;
13588 else
13589 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13591 if (reloc->howto == NULL)
13593 as_bad_where (fixp->fx_file, fixp->fx_line,
13594 _("Can not represent %s relocation in this object file format"),
13595 bfd_get_reloc_code_name (code));
13596 retval[0] = NULL;
13599 return retval;
13602 /* Relax a machine dependent frag. This returns the amount by which
13603 the current size of the frag should change. */
13606 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
13608 if (RELAX_BRANCH_P (fragp->fr_subtype))
13610 offsetT old_var = fragp->fr_var;
13612 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13614 return fragp->fr_var - old_var;
13617 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13618 return 0;
13620 if (mips16_extended_frag (fragp, NULL, stretch))
13622 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13623 return 0;
13624 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13625 return 2;
13627 else
13629 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13630 return 0;
13631 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13632 return -2;
13635 return 0;
13638 /* Convert a machine dependent frag. */
13640 void
13641 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
13643 int old, new;
13644 char *fixptr;
13646 if (RELAX_BRANCH_P (fragp->fr_subtype))
13648 bfd_byte *buf;
13649 unsigned long insn;
13650 expressionS exp;
13651 fixS *fixp;
13653 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13655 if (target_big_endian)
13656 insn = bfd_getb32 (buf);
13657 else
13658 insn = bfd_getl32 (buf);
13660 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13662 /* We generate a fixup instead of applying it right now
13663 because, if there are linker relaxations, we're going to
13664 need the relocations. */
13665 exp.X_op = O_symbol;
13666 exp.X_add_symbol = fragp->fr_symbol;
13667 exp.X_add_number = fragp->fr_offset;
13669 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13670 4, &exp, 1,
13671 BFD_RELOC_16_PCREL_S2);
13672 fixp->fx_file = fragp->fr_file;
13673 fixp->fx_line = fragp->fr_line;
13675 md_number_to_chars (buf, insn, 4);
13676 buf += 4;
13678 else
13680 int i;
13682 as_warn_where (fragp->fr_file, fragp->fr_line,
13683 _("relaxed out-of-range branch into a jump"));
13685 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13686 goto uncond;
13688 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13690 /* Reverse the branch. */
13691 switch ((insn >> 28) & 0xf)
13693 case 4:
13694 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13695 have the condition reversed by tweaking a single
13696 bit, and their opcodes all have 0x4???????. */
13697 assert ((insn & 0xf1000000) == 0x41000000);
13698 insn ^= 0x00010000;
13699 break;
13701 case 0:
13702 /* bltz 0x04000000 bgez 0x04010000
13703 bltzal 0x04100000 bgezal 0x04110000 */
13704 assert ((insn & 0xfc0e0000) == 0x04000000);
13705 insn ^= 0x00010000;
13706 break;
13708 case 1:
13709 /* beq 0x10000000 bne 0x14000000
13710 blez 0x18000000 bgtz 0x1c000000 */
13711 insn ^= 0x04000000;
13712 break;
13714 default:
13715 abort ();
13719 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13721 /* Clear the and-link bit. */
13722 assert ((insn & 0xfc1c0000) == 0x04100000);
13724 /* bltzal 0x04100000 bgezal 0x04110000
13725 bltzall 0x04120000 bgezall 0x04130000 */
13726 insn &= ~0x00100000;
13729 /* Branch over the branch (if the branch was likely) or the
13730 full jump (not likely case). Compute the offset from the
13731 current instruction to branch to. */
13732 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13733 i = 16;
13734 else
13736 /* How many bytes in instructions we've already emitted? */
13737 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13738 /* How many bytes in instructions from here to the end? */
13739 i = fragp->fr_var - i;
13741 /* Convert to instruction count. */
13742 i >>= 2;
13743 /* Branch counts from the next instruction. */
13744 i--;
13745 insn |= i;
13746 /* Branch over the jump. */
13747 md_number_to_chars (buf, insn, 4);
13748 buf += 4;
13750 /* Nop */
13751 md_number_to_chars (buf, 0, 4);
13752 buf += 4;
13754 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13756 /* beql $0, $0, 2f */
13757 insn = 0x50000000;
13758 /* Compute the PC offset from the current instruction to
13759 the end of the variable frag. */
13760 /* How many bytes in instructions we've already emitted? */
13761 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13762 /* How many bytes in instructions from here to the end? */
13763 i = fragp->fr_var - i;
13764 /* Convert to instruction count. */
13765 i >>= 2;
13766 /* Don't decrement i, because we want to branch over the
13767 delay slot. */
13769 insn |= i;
13770 md_number_to_chars (buf, insn, 4);
13771 buf += 4;
13773 md_number_to_chars (buf, 0, 4);
13774 buf += 4;
13777 uncond:
13778 if (mips_pic == NO_PIC)
13780 /* j or jal. */
13781 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13782 ? 0x0c000000 : 0x08000000);
13783 exp.X_op = O_symbol;
13784 exp.X_add_symbol = fragp->fr_symbol;
13785 exp.X_add_number = fragp->fr_offset;
13787 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13788 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13789 fixp->fx_file = fragp->fr_file;
13790 fixp->fx_line = fragp->fr_line;
13792 md_number_to_chars (buf, insn, 4);
13793 buf += 4;
13795 else
13797 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13798 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13799 exp.X_op = O_symbol;
13800 exp.X_add_symbol = fragp->fr_symbol;
13801 exp.X_add_number = fragp->fr_offset;
13803 if (fragp->fr_offset)
13805 exp.X_add_symbol = make_expr_symbol (&exp);
13806 exp.X_add_number = 0;
13809 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13810 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13811 fixp->fx_file = fragp->fr_file;
13812 fixp->fx_line = fragp->fr_line;
13814 md_number_to_chars (buf, insn, 4);
13815 buf += 4;
13817 if (mips_opts.isa == ISA_MIPS1)
13819 /* nop */
13820 md_number_to_chars (buf, 0, 4);
13821 buf += 4;
13824 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13825 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13827 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13828 4, &exp, 0, BFD_RELOC_LO16);
13829 fixp->fx_file = fragp->fr_file;
13830 fixp->fx_line = fragp->fr_line;
13832 md_number_to_chars (buf, insn, 4);
13833 buf += 4;
13835 /* j(al)r $at. */
13836 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13837 insn = 0x0020f809;
13838 else
13839 insn = 0x00200008;
13841 md_number_to_chars (buf, insn, 4);
13842 buf += 4;
13846 assert (buf == (bfd_byte *)fragp->fr_literal
13847 + fragp->fr_fix + fragp->fr_var);
13849 fragp->fr_fix += fragp->fr_var;
13851 return;
13854 if (RELAX_MIPS16_P (fragp->fr_subtype))
13856 int type;
13857 register const struct mips16_immed_operand *op;
13858 bfd_boolean small, ext;
13859 offsetT val;
13860 bfd_byte *buf;
13861 unsigned long insn;
13862 bfd_boolean use_extend;
13863 unsigned short extend;
13865 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13866 op = mips16_immed_operands;
13867 while (op->type != type)
13868 ++op;
13870 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13872 small = FALSE;
13873 ext = TRUE;
13875 else
13877 small = TRUE;
13878 ext = FALSE;
13881 resolve_symbol_value (fragp->fr_symbol);
13882 val = S_GET_VALUE (fragp->fr_symbol);
13883 if (op->pcrel)
13885 addressT addr;
13887 addr = fragp->fr_address + fragp->fr_fix;
13889 /* The rules for the base address of a PC relative reloc are
13890 complicated; see mips16_extended_frag. */
13891 if (type == 'p' || type == 'q')
13893 addr += 2;
13894 if (ext)
13895 addr += 2;
13896 /* Ignore the low bit in the target, since it will be
13897 set for a text label. */
13898 if ((val & 1) != 0)
13899 --val;
13901 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13902 addr -= 4;
13903 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13904 addr -= 2;
13906 addr &= ~ (addressT) ((1 << op->shift) - 1);
13907 val -= addr;
13909 /* Make sure the section winds up with the alignment we have
13910 assumed. */
13911 if (op->shift > 0)
13912 record_alignment (asec, op->shift);
13915 if (ext
13916 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13917 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13918 as_warn_where (fragp->fr_file, fragp->fr_line,
13919 _("extended instruction in delay slot"));
13921 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13923 if (target_big_endian)
13924 insn = bfd_getb16 (buf);
13925 else
13926 insn = bfd_getl16 (buf);
13928 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13929 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13930 small, ext, &insn, &use_extend, &extend);
13932 if (use_extend)
13934 md_number_to_chars (buf, 0xf000 | extend, 2);
13935 fragp->fr_fix += 2;
13936 buf += 2;
13939 md_number_to_chars (buf, insn, 2);
13940 fragp->fr_fix += 2;
13941 buf += 2;
13943 else
13945 if (fragp->fr_opcode == NULL)
13946 return;
13948 old = RELAX_OLD (fragp->fr_subtype);
13949 new = RELAX_NEW (fragp->fr_subtype);
13950 fixptr = fragp->fr_literal + fragp->fr_fix;
13952 if (new > 0)
13953 memmove (fixptr - old, fixptr, new);
13955 fragp->fr_fix += new - old;
13959 #ifdef OBJ_ELF
13961 /* This function is called after the relocs have been generated.
13962 We've been storing mips16 text labels as odd. Here we convert them
13963 back to even for the convenience of the debugger. */
13965 void
13966 mips_frob_file_after_relocs (void)
13968 asymbol **syms;
13969 unsigned int count, i;
13971 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13972 return;
13974 syms = bfd_get_outsymbols (stdoutput);
13975 count = bfd_get_symcount (stdoutput);
13976 for (i = 0; i < count; i++, syms++)
13978 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13979 && ((*syms)->value & 1) != 0)
13981 (*syms)->value &= ~1;
13982 /* If the symbol has an odd size, it was probably computed
13983 incorrectly, so adjust that as well. */
13984 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13985 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13990 #endif
13992 /* This function is called whenever a label is defined. It is used
13993 when handling branch delays; if a branch has a label, we assume we
13994 can not move it. */
13996 void
13997 mips_define_label (symbolS *sym)
13999 struct insn_label_list *l;
14001 if (free_insn_labels == NULL)
14002 l = (struct insn_label_list *) xmalloc (sizeof *l);
14003 else
14005 l = free_insn_labels;
14006 free_insn_labels = l->next;
14009 l->label = sym;
14010 l->next = insn_labels;
14011 insn_labels = l;
14014 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14016 /* Some special processing for a MIPS ELF file. */
14018 void
14019 mips_elf_final_processing (void)
14021 /* Write out the register information. */
14022 if (mips_abi != N64_ABI)
14024 Elf32_RegInfo s;
14026 s.ri_gprmask = mips_gprmask;
14027 s.ri_cprmask[0] = mips_cprmask[0];
14028 s.ri_cprmask[1] = mips_cprmask[1];
14029 s.ri_cprmask[2] = mips_cprmask[2];
14030 s.ri_cprmask[3] = mips_cprmask[3];
14031 /* The gp_value field is set by the MIPS ELF backend. */
14033 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14034 ((Elf32_External_RegInfo *)
14035 mips_regmask_frag));
14037 else
14039 Elf64_Internal_RegInfo s;
14041 s.ri_gprmask = mips_gprmask;
14042 s.ri_pad = 0;
14043 s.ri_cprmask[0] = mips_cprmask[0];
14044 s.ri_cprmask[1] = mips_cprmask[1];
14045 s.ri_cprmask[2] = mips_cprmask[2];
14046 s.ri_cprmask[3] = mips_cprmask[3];
14047 /* The gp_value field is set by the MIPS ELF backend. */
14049 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14050 ((Elf64_External_RegInfo *)
14051 mips_regmask_frag));
14054 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
14055 sort of BFD interface for this. */
14056 if (mips_any_noreorder)
14057 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14058 if (mips_pic != NO_PIC)
14060 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
14061 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14063 if (mips_abicalls)
14064 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14066 /* Set MIPS ELF flags for ASEs. */
14067 if (file_ase_mips16)
14068 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
14069 #if 0 /* XXX FIXME */
14070 if (file_ase_mips3d)
14071 elf_elfheader (stdoutput)->e_flags |= ???;
14072 #endif
14073 if (file_ase_mdmx)
14074 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
14076 /* Set the MIPS ELF ABI flags. */
14077 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
14078 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
14079 else if (mips_abi == O64_ABI)
14080 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
14081 else if (mips_abi == EABI_ABI)
14083 if (!file_mips_gp32)
14084 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14085 else
14086 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14088 else if (mips_abi == N32_ABI)
14089 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14091 /* Nothing to do for N64_ABI. */
14093 if (mips_32bitmode)
14094 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14097 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14099 typedef struct proc {
14100 symbolS *isym;
14101 unsigned long reg_mask;
14102 unsigned long reg_offset;
14103 unsigned long fpreg_mask;
14104 unsigned long fpreg_offset;
14105 unsigned long frame_offset;
14106 unsigned long frame_reg;
14107 unsigned long pc_reg;
14108 } procS;
14110 static procS cur_proc;
14111 static procS *cur_proc_ptr;
14112 static int numprocs;
14114 /* Fill in an rs_align_code fragment. */
14116 void
14117 mips_handle_align (fragS *fragp)
14119 if (fragp->fr_type != rs_align_code)
14120 return;
14122 if (mips_opts.mips16)
14124 static const unsigned char be_nop[] = { 0x65, 0x00 };
14125 static const unsigned char le_nop[] = { 0x00, 0x65 };
14127 int bytes;
14128 char *p;
14130 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14131 p = fragp->fr_literal + fragp->fr_fix;
14133 if (bytes & 1)
14135 *p++ = 0;
14136 fragp->fr_fix++;
14139 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14140 fragp->fr_var = 2;
14143 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
14146 static void
14147 md_obj_begin (void)
14151 static void
14152 md_obj_end (void)
14154 /* check for premature end, nesting errors, etc */
14155 if (cur_proc_ptr)
14156 as_warn (_("missing .end at end of assembly"));
14159 static long
14160 get_number (void)
14162 int negative = 0;
14163 long val = 0;
14165 if (*input_line_pointer == '-')
14167 ++input_line_pointer;
14168 negative = 1;
14170 if (!ISDIGIT (*input_line_pointer))
14171 as_bad (_("expected simple number"));
14172 if (input_line_pointer[0] == '0')
14174 if (input_line_pointer[1] == 'x')
14176 input_line_pointer += 2;
14177 while (ISXDIGIT (*input_line_pointer))
14179 val <<= 4;
14180 val |= hex_value (*input_line_pointer++);
14182 return negative ? -val : val;
14184 else
14186 ++input_line_pointer;
14187 while (ISDIGIT (*input_line_pointer))
14189 val <<= 3;
14190 val |= *input_line_pointer++ - '0';
14192 return negative ? -val : val;
14195 if (!ISDIGIT (*input_line_pointer))
14197 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14198 *input_line_pointer, *input_line_pointer);
14199 as_warn (_("invalid number"));
14200 return -1;
14202 while (ISDIGIT (*input_line_pointer))
14204 val *= 10;
14205 val += *input_line_pointer++ - '0';
14207 return negative ? -val : val;
14210 /* The .file directive; just like the usual .file directive, but there
14211 is an initial number which is the ECOFF file index. In the non-ECOFF
14212 case .file implies DWARF-2. */
14214 static void
14215 s_mips_file (int x ATTRIBUTE_UNUSED)
14217 static int first_file_directive = 0;
14219 if (ECOFF_DEBUGGING)
14221 get_number ();
14222 s_app_file (0);
14224 else
14226 char *filename;
14228 filename = dwarf2_directive_file (0);
14230 /* Versions of GCC up to 3.1 start files with a ".file"
14231 directive even for stabs output. Make sure that this
14232 ".file" is handled. Note that you need a version of GCC
14233 after 3.1 in order to support DWARF-2 on MIPS. */
14234 if (filename != NULL && ! first_file_directive)
14236 (void) new_logical_line (filename, -1);
14237 s_app_file_string (filename);
14239 first_file_directive = 1;
14243 /* The .loc directive, implying DWARF-2. */
14245 static void
14246 s_mips_loc (int x ATTRIBUTE_UNUSED)
14248 if (!ECOFF_DEBUGGING)
14249 dwarf2_directive_loc (0);
14252 /* The .end directive. */
14254 static void
14255 s_mips_end (int x ATTRIBUTE_UNUSED)
14257 symbolS *p;
14259 /* Following functions need their own .frame and .cprestore directives. */
14260 mips_frame_reg_valid = 0;
14261 mips_cprestore_valid = 0;
14263 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14265 p = get_symbol ();
14266 demand_empty_rest_of_line ();
14268 else
14269 p = NULL;
14271 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14272 as_warn (_(".end not in text section"));
14274 if (!cur_proc_ptr)
14276 as_warn (_(".end directive without a preceding .ent directive."));
14277 demand_empty_rest_of_line ();
14278 return;
14281 if (p != NULL)
14283 assert (S_GET_NAME (p));
14284 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14285 as_warn (_(".end symbol does not match .ent symbol."));
14287 if (debug_type == DEBUG_STABS)
14288 stabs_generate_asm_endfunc (S_GET_NAME (p),
14289 S_GET_NAME (p));
14291 else
14292 as_warn (_(".end directive missing or unknown symbol"));
14294 #ifdef OBJ_ELF
14295 /* Generate a .pdr section. */
14296 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
14297 && mips_flag_pdr)
14299 segT saved_seg = now_seg;
14300 subsegT saved_subseg = now_subseg;
14301 valueT dot;
14302 expressionS exp;
14303 char *fragp;
14305 dot = frag_now_fix ();
14307 #ifdef md_flush_pending_output
14308 md_flush_pending_output ();
14309 #endif
14311 assert (pdr_seg);
14312 subseg_set (pdr_seg, 0);
14314 /* Write the symbol. */
14315 exp.X_op = O_symbol;
14316 exp.X_add_symbol = p;
14317 exp.X_add_number = 0;
14318 emit_expr (&exp, 4);
14320 fragp = frag_more (7 * 4);
14322 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14323 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14324 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14325 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14326 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14327 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14328 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
14330 subseg_set (saved_seg, saved_subseg);
14332 #endif /* OBJ_ELF */
14334 cur_proc_ptr = NULL;
14337 /* The .aent and .ent directives. */
14339 static void
14340 s_mips_ent (int aent)
14342 symbolS *symbolP;
14344 symbolP = get_symbol ();
14345 if (*input_line_pointer == ',')
14346 ++input_line_pointer;
14347 SKIP_WHITESPACE ();
14348 if (ISDIGIT (*input_line_pointer)
14349 || *input_line_pointer == '-')
14350 get_number ();
14352 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14353 as_warn (_(".ent or .aent not in text section."));
14355 if (!aent && cur_proc_ptr)
14356 as_warn (_("missing .end"));
14358 if (!aent)
14360 /* This function needs its own .frame and .cprestore directives. */
14361 mips_frame_reg_valid = 0;
14362 mips_cprestore_valid = 0;
14364 cur_proc_ptr = &cur_proc;
14365 memset (cur_proc_ptr, '\0', sizeof (procS));
14367 cur_proc_ptr->isym = symbolP;
14369 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14371 ++numprocs;
14373 if (debug_type == DEBUG_STABS)
14374 stabs_generate_asm_func (S_GET_NAME (symbolP),
14375 S_GET_NAME (symbolP));
14378 demand_empty_rest_of_line ();
14381 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14382 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14383 s_mips_frame is used so that we can set the PDR information correctly.
14384 We can't use the ecoff routines because they make reference to the ecoff
14385 symbol table (in the mdebug section). */
14387 static void
14388 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
14390 #ifdef OBJ_ELF
14391 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14393 long val;
14395 if (cur_proc_ptr == (procS *) NULL)
14397 as_warn (_(".frame outside of .ent"));
14398 demand_empty_rest_of_line ();
14399 return;
14402 cur_proc_ptr->frame_reg = tc_get_register (1);
14404 SKIP_WHITESPACE ();
14405 if (*input_line_pointer++ != ','
14406 || get_absolute_expression_and_terminator (&val) != ',')
14408 as_warn (_("Bad .frame directive"));
14409 --input_line_pointer;
14410 demand_empty_rest_of_line ();
14411 return;
14414 cur_proc_ptr->frame_offset = val;
14415 cur_proc_ptr->pc_reg = tc_get_register (0);
14417 demand_empty_rest_of_line ();
14419 else
14420 #endif /* OBJ_ELF */
14421 s_ignore (ignore);
14424 /* The .fmask and .mask directives. If the mdebug section is present
14425 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14426 embedded targets, s_mips_mask is used so that we can set the PDR
14427 information correctly. We can't use the ecoff routines because they
14428 make reference to the ecoff symbol table (in the mdebug section). */
14430 static void
14431 s_mips_mask (int reg_type)
14433 #ifdef OBJ_ELF
14434 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14436 long mask, off;
14438 if (cur_proc_ptr == (procS *) NULL)
14440 as_warn (_(".mask/.fmask outside of .ent"));
14441 demand_empty_rest_of_line ();
14442 return;
14445 if (get_absolute_expression_and_terminator (&mask) != ',')
14447 as_warn (_("Bad .mask/.fmask directive"));
14448 --input_line_pointer;
14449 demand_empty_rest_of_line ();
14450 return;
14453 off = get_absolute_expression ();
14455 if (reg_type == 'F')
14457 cur_proc_ptr->fpreg_mask = mask;
14458 cur_proc_ptr->fpreg_offset = off;
14460 else
14462 cur_proc_ptr->reg_mask = mask;
14463 cur_proc_ptr->reg_offset = off;
14466 demand_empty_rest_of_line ();
14468 else
14469 #endif /* OBJ_ELF */
14470 s_ignore (reg_type);
14473 /* The .loc directive. */
14475 #if 0
14476 static void
14477 s_loc (int x)
14479 symbolS *symbolP;
14480 int lineno;
14481 int addroff;
14483 assert (now_seg == text_section);
14485 lineno = get_number ();
14486 addroff = frag_now_fix ();
14488 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14489 S_SET_TYPE (symbolP, N_SLINE);
14490 S_SET_OTHER (symbolP, 0);
14491 S_SET_DESC (symbolP, lineno);
14492 symbolP->sy_segment = now_seg;
14494 #endif
14496 /* A table describing all the processors gas knows about. Names are
14497 matched in the order listed.
14499 To ease comparison, please keep this table in the same order as
14500 gcc's mips_cpu_info_table[]. */
14501 static const struct mips_cpu_info mips_cpu_info_table[] =
14503 /* Entries for generic ISAs */
14504 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14505 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14506 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14507 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14508 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14509 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
14510 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
14511 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
14512 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
14514 /* MIPS I */
14515 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14516 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14517 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14519 /* MIPS II */
14520 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14522 /* MIPS III */
14523 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14524 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14525 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14526 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
14527 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14528 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14529 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
14530 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14531 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14532 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14533 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14534 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14536 /* MIPS IV */
14537 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14538 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14539 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14540 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
14541 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14542 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
14543 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14544 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14545 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14546 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14547 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14548 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
14549 { "rm9000", 0, ISA_MIPS4, CPU_RM7000 },
14551 /* MIPS 32 */
14552 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
14553 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14554 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
14556 /* MIPS 64 */
14557 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14558 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
14560 /* Broadcom SB-1 CPU core */
14561 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
14563 /* End marker */
14564 { NULL, 0, 0, 0 }
14568 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14569 with a final "000" replaced by "k". Ignore case.
14571 Note: this function is shared between GCC and GAS. */
14573 static bfd_boolean
14574 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
14576 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14577 given++, canonical++;
14579 return ((*given == 0 && *canonical == 0)
14580 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14584 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14585 CPU name. We've traditionally allowed a lot of variation here.
14587 Note: this function is shared between GCC and GAS. */
14589 static bfd_boolean
14590 mips_matching_cpu_name_p (const char *canonical, const char *given)
14592 /* First see if the name matches exactly, or with a final "000"
14593 turned into "k". */
14594 if (mips_strict_matching_cpu_name_p (canonical, given))
14595 return TRUE;
14597 /* If not, try comparing based on numerical designation alone.
14598 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14599 if (TOLOWER (*given) == 'r')
14600 given++;
14601 if (!ISDIGIT (*given))
14602 return FALSE;
14604 /* Skip over some well-known prefixes in the canonical name,
14605 hoping to find a number there too. */
14606 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14607 canonical += 2;
14608 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14609 canonical += 2;
14610 else if (TOLOWER (canonical[0]) == 'r')
14611 canonical += 1;
14613 return mips_strict_matching_cpu_name_p (canonical, given);
14617 /* Parse an option that takes the name of a processor as its argument.
14618 OPTION is the name of the option and CPU_STRING is the argument.
14619 Return the corresponding processor enumeration if the CPU_STRING is
14620 recognized, otherwise report an error and return null.
14622 A similar function exists in GCC. */
14624 static const struct mips_cpu_info *
14625 mips_parse_cpu (const char *option, const char *cpu_string)
14627 const struct mips_cpu_info *p;
14629 /* 'from-abi' selects the most compatible architecture for the given
14630 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14631 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14632 version. Look first at the -mgp options, if given, otherwise base
14633 the choice on MIPS_DEFAULT_64BIT.
14635 Treat NO_ABI like the EABIs. One reason to do this is that the
14636 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14637 architecture. This code picks MIPS I for 'mips' and MIPS III for
14638 'mips64', just as we did in the days before 'from-abi'. */
14639 if (strcasecmp (cpu_string, "from-abi") == 0)
14641 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14642 return mips_cpu_info_from_isa (ISA_MIPS1);
14644 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14645 return mips_cpu_info_from_isa (ISA_MIPS3);
14647 if (file_mips_gp32 >= 0)
14648 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14650 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14651 ? ISA_MIPS3
14652 : ISA_MIPS1);
14655 /* 'default' has traditionally been a no-op. Probably not very useful. */
14656 if (strcasecmp (cpu_string, "default") == 0)
14657 return 0;
14659 for (p = mips_cpu_info_table; p->name != 0; p++)
14660 if (mips_matching_cpu_name_p (p->name, cpu_string))
14661 return p;
14663 as_bad ("Bad value (%s) for %s", cpu_string, option);
14664 return 0;
14667 /* Return the canonical processor information for ISA (a member of the
14668 ISA_MIPS* enumeration). */
14670 static const struct mips_cpu_info *
14671 mips_cpu_info_from_isa (int isa)
14673 int i;
14675 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14676 if (mips_cpu_info_table[i].is_isa
14677 && isa == mips_cpu_info_table[i].isa)
14678 return (&mips_cpu_info_table[i]);
14680 return NULL;
14683 static const struct mips_cpu_info *
14684 mips_cpu_info_from_arch (int arch)
14686 int i;
14688 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14689 if (arch == mips_cpu_info_table[i].cpu)
14690 return (&mips_cpu_info_table[i]);
14692 return NULL;
14695 static void
14696 show (FILE *stream, const char *string, int *col_p, int *first_p)
14698 if (*first_p)
14700 fprintf (stream, "%24s", "");
14701 *col_p = 24;
14703 else
14705 fprintf (stream, ", ");
14706 *col_p += 2;
14709 if (*col_p + strlen (string) > 72)
14711 fprintf (stream, "\n%24s", "");
14712 *col_p = 24;
14715 fprintf (stream, "%s", string);
14716 *col_p += strlen (string);
14718 *first_p = 0;
14721 void
14722 md_show_usage (FILE *stream)
14724 int column, first;
14725 size_t i;
14727 fprintf (stream, _("\
14728 MIPS options:\n\
14729 -membedded-pic generate embedded position independent code\n\
14730 -EB generate big endian output\n\
14731 -EL generate little endian output\n\
14732 -g, -g2 do not remove unneeded NOPs or swap branches\n\
14733 -G NUM allow referencing objects up to NUM bytes\n\
14734 implicitly with the gp register [default 8]\n"));
14735 fprintf (stream, _("\
14736 -mips1 generate MIPS ISA I instructions\n\
14737 -mips2 generate MIPS ISA II instructions\n\
14738 -mips3 generate MIPS ISA III instructions\n\
14739 -mips4 generate MIPS ISA IV instructions\n\
14740 -mips5 generate MIPS ISA V instructions\n\
14741 -mips32 generate MIPS32 ISA instructions\n\
14742 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
14743 -mips64 generate MIPS64 ISA instructions\n\
14744 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
14745 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14747 first = 1;
14749 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14750 show (stream, mips_cpu_info_table[i].name, &column, &first);
14751 show (stream, "from-abi", &column, &first);
14752 fputc ('\n', stream);
14754 fprintf (stream, _("\
14755 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14756 -no-mCPU don't generate code specific to CPU.\n\
14757 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14759 first = 1;
14761 show (stream, "3900", &column, &first);
14762 show (stream, "4010", &column, &first);
14763 show (stream, "4100", &column, &first);
14764 show (stream, "4650", &column, &first);
14765 fputc ('\n', stream);
14767 fprintf (stream, _("\
14768 -mips16 generate mips16 instructions\n\
14769 -no-mips16 do not generate mips16 instructions\n"));
14770 fprintf (stream, _("\
14771 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14772 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14773 -O0 remove unneeded NOPs, do not swap branches\n\
14774 -O remove unneeded NOPs and swap branches\n\
14775 -n warn about NOPs generated from macros\n\
14776 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14777 --trap, --no-break trap exception on div by 0 and mult overflow\n\
14778 --break, --no-trap break exception on div by 0 and mult overflow\n"));
14779 #ifdef OBJ_ELF
14780 fprintf (stream, _("\
14781 -KPIC, -call_shared generate SVR4 position independent code\n\
14782 -non_shared do not generate position independent code\n\
14783 -xgot assume a 32 bit GOT\n\
14784 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
14785 -mabi=ABI create ABI conformant object file for:\n"));
14787 first = 1;
14789 show (stream, "32", &column, &first);
14790 show (stream, "o64", &column, &first);
14791 show (stream, "n32", &column, &first);
14792 show (stream, "64", &column, &first);
14793 show (stream, "eabi", &column, &first);
14795 fputc ('\n', stream);
14797 fprintf (stream, _("\
14798 -32 create o32 ABI object file (default)\n\
14799 -n32 create n32 ABI object file\n\
14800 -64 create 64 ABI object file\n"));
14801 #endif
14804 enum dwarf2_format
14805 mips_dwarf2_format (void)
14807 if (mips_abi == N64_ABI)
14809 #ifdef TE_IRIX
14810 return dwarf2_format_64bit_irix;
14811 #else
14812 return dwarf2_format_64bit;
14813 #endif
14815 else
14816 return dwarf2_format_32bit;