* config/tc-mips.c (s_gpdword): New function.
[binutils.git] / gas / config / tc-mips.c
blob5a30e51ab1afbcc7ebdda57589be9eef3af12f4b
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
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 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
38 #include "opcode/mips.h"
39 #include "itbl-ops.h"
40 #include "dwarf2dbg.h"
42 #ifdef DEBUG
43 #define DBG(x) printf x
44 #else
45 #define DBG(x)
46 #endif
48 #ifdef OBJ_MAYBE_ELF
49 /* Clean up namespace so we can include obj-elf.h too. */
50 static int mips_output_flavor PARAMS ((void));
51 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52 #undef OBJ_PROCESS_STAB
53 #undef OUTPUT_FLAVOR
54 #undef S_GET_ALIGN
55 #undef S_GET_SIZE
56 #undef S_SET_ALIGN
57 #undef S_SET_SIZE
58 #undef obj_frob_file
59 #undef obj_frob_file_after_relocs
60 #undef obj_frob_symbol
61 #undef obj_pop_insert
62 #undef obj_sec_sym_ok_for_reloc
63 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
65 #include "obj-elf.h"
66 /* Fix any of them that we actually care about. */
67 #undef OUTPUT_FLAVOR
68 #define OUTPUT_FLAVOR mips_output_flavor()
69 #endif
71 #if defined (OBJ_ELF)
72 #include "elf/mips.h"
73 #endif
75 #ifndef ECOFF_DEBUGGING
76 #define NO_ECOFF_DEBUGGING
77 #define ECOFF_DEBUGGING 0
78 #endif
80 int mips_flag_mdebug = -1;
82 #include "ecoff.h"
84 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
85 static char *mips_regmask_frag;
86 #endif
88 #define ZERO 0
89 #define AT 1
90 #define TREG 24
91 #define PIC_CALL_REG 25
92 #define KT0 26
93 #define KT1 27
94 #define GP 28
95 #define SP 29
96 #define FP 30
97 #define RA 31
99 #define ILLEGAL_REG (32)
101 /* Allow override of standard little-endian ECOFF format. */
103 #ifndef ECOFF_LITTLE_FORMAT
104 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
105 #endif
107 extern int target_big_endian;
109 /* The name of the readonly data section. */
110 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
111 ? ".data" \
112 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
113 ? ".rdata" \
114 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
115 ? ".rdata" \
116 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117 ? ".rodata" \
118 : (abort (), ""))
120 /* The ABI to use. */
121 enum mips_abi_level
123 NO_ABI = 0,
124 O32_ABI,
125 O64_ABI,
126 N32_ABI,
127 N64_ABI,
128 EABI_ABI
131 /* MIPS ABI we are using for this output file. */
132 static enum mips_abi_level mips_abi = NO_ABI;
134 /* This is the set of options which may be modified by the .set
135 pseudo-op. We use a struct so that .set push and .set pop are more
136 reliable. */
138 struct mips_set_options
140 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
141 if it has not been initialized. Changed by `.set mipsN', and the
142 -mipsN command line option, and the default CPU. */
143 int isa;
144 /* Enabled Application Specific Extensions (ASEs). These are set to -1
145 if they have not been initialized. Changed by `.set <asename>', by
146 command line options, and based on the default architecture. */
147 int ase_mips3d;
148 int ase_mdmx;
149 /* Whether we are assembling for the mips16 processor. 0 if we are
150 not, 1 if we are, and -1 if the value has not been initialized.
151 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
152 -nomips16 command line options, and the default CPU. */
153 int mips16;
154 /* Non-zero if we should not reorder instructions. Changed by `.set
155 reorder' and `.set noreorder'. */
156 int noreorder;
157 /* Non-zero if we should not permit the $at ($1) register to be used
158 in instructions. Changed by `.set at' and `.set noat'. */
159 int noat;
160 /* Non-zero if we should warn when a macro instruction expands into
161 more than one machine instruction. Changed by `.set nomacro' and
162 `.set macro'. */
163 int warn_about_macros;
164 /* Non-zero if we should not move instructions. Changed by `.set
165 move', `.set volatile', `.set nomove', and `.set novolatile'. */
166 int nomove;
167 /* Non-zero if we should not optimize branches by moving the target
168 of the branch into the delay slot. Actually, we don't perform
169 this optimization anyhow. Changed by `.set bopt' and `.set
170 nobopt'. */
171 int nobopt;
172 /* Non-zero if we should not autoextend mips16 instructions.
173 Changed by `.set autoextend' and `.set noautoextend'. */
174 int noautoextend;
175 /* Restrict general purpose registers and floating point registers
176 to 32 bit. This is initially determined when -mgp32 or -mfp32
177 is passed but can changed if the assembler code uses .set mipsN. */
178 int gp32;
179 int fp32;
182 /* True if -mgp32 was passed. */
183 static int file_mips_gp32 = -1;
185 /* True if -mfp32 was passed. */
186 static int file_mips_fp32 = -1;
188 /* This is the struct we use to hold the current set of options. Note
189 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
190 -1 to indicate that they have not been initialized. */
192 static struct mips_set_options mips_opts =
194 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
197 /* These variables are filled in with the masks of registers used.
198 The object format code reads them and puts them in the appropriate
199 place. */
200 unsigned long mips_gprmask;
201 unsigned long mips_cprmask[4];
203 /* MIPS ISA we are using for this output file. */
204 static int file_mips_isa = ISA_UNKNOWN;
206 /* True if -mips16 was passed or implied by arguments passed on the
207 command line (e.g., by -march). */
208 static int file_ase_mips16;
210 /* True if -mips3d was passed or implied by arguments passed on the
211 command line (e.g., by -march). */
212 static int file_ase_mips3d;
214 /* True if -mdmx was passed or implied by arguments passed on the
215 command line (e.g., by -march). */
216 static int file_ase_mdmx;
218 /* The argument of the -march= flag. The architecture we are assembling. */
219 static int mips_arch = CPU_UNKNOWN;
220 static const char *mips_arch_string;
221 static const struct mips_cpu_info *mips_arch_info;
223 /* The argument of the -mtune= flag. The architecture for which we
224 are optimizing. */
225 static int mips_tune = CPU_UNKNOWN;
226 static const char *mips_tune_string;
227 static const struct mips_cpu_info *mips_tune_info;
229 /* True when generating 32-bit code for a 64-bit processor. */
230 static int mips_32bitmode = 0;
232 /* Some ISA's have delay slots for instructions which read or write
233 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
234 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
235 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
236 delay slot in this ISA. The uses of this macro assume that any
237 ISA that has delay slots for one of these, has them for all. They
238 also assume that ISAs which don't have delays for these insns, don't
239 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
240 #define ISA_HAS_COPROC_DELAYS(ISA) ( \
241 (ISA) == ISA_MIPS1 \
242 || (ISA) == ISA_MIPS2 \
243 || (ISA) == ISA_MIPS3 \
246 /* True if the given ABI requires 32-bit registers. */
247 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
249 /* Likewise 64-bit registers. */
250 #define ABI_NEEDS_64BIT_REGS(ABI) \
251 ((ABI) == N32_ABI \
252 || (ABI) == N64_ABI \
253 || (ABI) == O64_ABI)
255 /* Return true if ISA supports 64 bit gp register instructions. */
256 #define ISA_HAS_64BIT_REGS(ISA) ( \
257 (ISA) == ISA_MIPS3 \
258 || (ISA) == ISA_MIPS4 \
259 || (ISA) == ISA_MIPS5 \
260 || (ISA) == ISA_MIPS64 \
263 #define HAVE_32BIT_GPRS \
264 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
266 #define HAVE_32BIT_FPRS \
267 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
269 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
270 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
272 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
274 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
276 /* We can only have 64bit addresses if the object file format
277 supports it. */
278 #define HAVE_32BIT_ADDRESSES \
279 (HAVE_32BIT_GPRS \
280 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
281 || ! HAVE_64BIT_OBJECTS) \
282 && mips_pic != EMBEDDED_PIC))
284 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
286 /* Return true if the given CPU supports the MIPS16 ASE. */
287 #define CPU_HAS_MIPS16(cpu) \
288 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
289 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
291 /* Return true if the given CPU supports the MIPS3D ASE. */
292 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
295 /* Return true if the given CPU supports the MDMX ASE. */
296 #define CPU_HAS_MDMX(cpu) (false \
299 /* True if CPU has a dror instruction. */
300 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
302 /* True if CPU has a ror instruction. */
303 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
305 /* Whether the processor uses hardware interlocks to protect
306 reads from the HI and LO registers, and thus does not
307 require nops to be inserted. */
309 #define hilo_interlocks (mips_arch == CPU_R4010 \
310 || mips_arch == CPU_VR5500 \
311 || mips_arch == CPU_SB1 \
314 /* Whether the processor uses hardware interlocks to protect reads
315 from the GPRs, and thus does not require nops to be inserted. */
316 #define gpr_interlocks \
317 (mips_opts.isa != ISA_MIPS1 \
318 || mips_arch == CPU_VR5400 \
319 || mips_arch == CPU_VR5500 \
320 || mips_arch == CPU_R3900)
322 /* As with other "interlocks" this is used by hardware that has FP
323 (co-processor) interlocks. */
324 /* Itbl support may require additional care here. */
325 #define cop_interlocks (mips_arch == CPU_R4300 \
326 || mips_arch == CPU_VR5400 \
327 || mips_arch == CPU_VR5500 \
328 || mips_arch == CPU_SB1 \
331 /* Is this a mfhi or mflo instruction? */
332 #define MF_HILO_INSN(PINFO) \
333 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
335 /* MIPS PIC level. */
337 enum mips_pic_level mips_pic;
339 /* Warn about all NOPS that the assembler generates. */
340 static int warn_nops = 0;
342 /* 1 if we should generate 32 bit offsets from the $gp register in
343 SVR4_PIC mode. Currently has no meaning in other modes. */
344 static int mips_big_got = 0;
346 /* 1 if trap instructions should used for overflow rather than break
347 instructions. */
348 static int mips_trap = 0;
350 /* 1 if double width floating point constants should not be constructed
351 by assembling two single width halves into two single width floating
352 point registers which just happen to alias the double width destination
353 register. On some architectures this aliasing can be disabled by a bit
354 in the status register, and the setting of this bit cannot be determined
355 automatically at assemble time. */
356 static int mips_disable_float_construction;
358 /* Non-zero if any .set noreorder directives were used. */
360 static int mips_any_noreorder;
362 /* Non-zero if nops should be inserted when the register referenced in
363 an mfhi/mflo instruction is read in the next two instructions. */
364 static int mips_7000_hilo_fix;
366 /* The size of the small data section. */
367 static unsigned int g_switch_value = 8;
368 /* Whether the -G option was used. */
369 static int g_switch_seen = 0;
371 #define N_RMASK 0xc4
372 #define N_VFP 0xd4
374 /* If we can determine in advance that GP optimization won't be
375 possible, we can skip the relaxation stuff that tries to produce
376 GP-relative references. This makes delay slot optimization work
377 better.
379 This function can only provide a guess, but it seems to work for
380 gcc output. It needs to guess right for gcc, otherwise gcc
381 will put what it thinks is a GP-relative instruction in a branch
382 delay slot.
384 I don't know if a fix is needed for the SVR4_PIC mode. I've only
385 fixed it for the non-PIC mode. KR 95/04/07 */
386 static int nopic_need_relax PARAMS ((symbolS *, int));
388 /* handle of the OPCODE hash table */
389 static struct hash_control *op_hash = NULL;
391 /* The opcode hash table we use for the mips16. */
392 static struct hash_control *mips16_op_hash = NULL;
394 /* This array holds the chars that always start a comment. If the
395 pre-processor is disabled, these aren't very useful */
396 const char comment_chars[] = "#";
398 /* This array holds the chars that only start a comment at the beginning of
399 a line. If the line seems to have the form '# 123 filename'
400 .line and .file directives will appear in the pre-processed output */
401 /* Note that input_file.c hand checks for '#' at the beginning of the
402 first line of the input file. This is because the compiler outputs
403 #NO_APP at the beginning of its output. */
404 /* Also note that C style comments are always supported. */
405 const char line_comment_chars[] = "#";
407 /* This array holds machine specific line separator characters. */
408 const char line_separator_chars[] = ";";
410 /* Chars that can be used to separate mant from exp in floating point nums */
411 const char EXP_CHARS[] = "eE";
413 /* Chars that mean this number is a floating point constant */
414 /* As in 0f12.456 */
415 /* or 0d1.2345e12 */
416 const char FLT_CHARS[] = "rRsSfFdDxXpP";
418 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
419 changed in read.c . Ideally it shouldn't have to know about it at all,
420 but nothing is ideal around here.
423 static char *insn_error;
425 static int auto_align = 1;
427 /* When outputting SVR4 PIC code, the assembler needs to know the
428 offset in the stack frame from which to restore the $gp register.
429 This is set by the .cprestore pseudo-op, and saved in this
430 variable. */
431 static offsetT mips_cprestore_offset = -1;
433 /* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
434 more optimizations, it can use a register value instead of a memory-saved
435 offset and even an other register than $gp as global pointer. */
436 static offsetT mips_cpreturn_offset = -1;
437 static int mips_cpreturn_register = -1;
438 static int mips_gp_register = GP;
439 static int mips_gprel_offset = 0;
441 /* Whether mips_cprestore_offset has been set in the current function
442 (or whether it has already been warned about, if not). */
443 static int mips_cprestore_valid = 0;
445 /* This is the register which holds the stack frame, as set by the
446 .frame pseudo-op. This is needed to implement .cprestore. */
447 static int mips_frame_reg = SP;
449 /* Whether mips_frame_reg has been set in the current function
450 (or whether it has already been warned about, if not). */
451 static int mips_frame_reg_valid = 0;
453 /* To output NOP instructions correctly, we need to keep information
454 about the previous two instructions. */
456 /* Whether we are optimizing. The default value of 2 means to remove
457 unneeded NOPs and swap branch instructions when possible. A value
458 of 1 means to not swap branches. A value of 0 means to always
459 insert NOPs. */
460 static int mips_optimize = 2;
462 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
463 equivalent to seeing no -g option at all. */
464 static int mips_debug = 0;
466 /* The previous instruction. */
467 static struct mips_cl_insn prev_insn;
469 /* The instruction before prev_insn. */
470 static struct mips_cl_insn prev_prev_insn;
472 /* If we don't want information for prev_insn or prev_prev_insn, we
473 point the insn_mo field at this dummy integer. */
474 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
476 /* Non-zero if prev_insn is valid. */
477 static int prev_insn_valid;
479 /* The frag for the previous instruction. */
480 static struct frag *prev_insn_frag;
482 /* The offset into prev_insn_frag for the previous instruction. */
483 static long prev_insn_where;
485 /* The reloc type for the previous instruction, if any. */
486 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
488 /* The reloc for the previous instruction, if any. */
489 static fixS *prev_insn_fixp[3];
491 /* Non-zero if the previous instruction was in a delay slot. */
492 static int prev_insn_is_delay_slot;
494 /* Non-zero if the previous instruction was in a .set noreorder. */
495 static int prev_insn_unreordered;
497 /* Non-zero if the previous instruction uses an extend opcode (if
498 mips16). */
499 static int prev_insn_extended;
501 /* Non-zero if the previous previous instruction was in a .set
502 noreorder. */
503 static int prev_prev_insn_unreordered;
505 /* If this is set, it points to a frag holding nop instructions which
506 were inserted before the start of a noreorder section. If those
507 nops turn out to be unnecessary, the size of the frag can be
508 decreased. */
509 static fragS *prev_nop_frag;
511 /* The number of nop instructions we created in prev_nop_frag. */
512 static int prev_nop_frag_holds;
514 /* The number of nop instructions that we know we need in
515 prev_nop_frag. */
516 static int prev_nop_frag_required;
518 /* The number of instructions we've seen since prev_nop_frag. */
519 static int prev_nop_frag_since;
521 /* For ECOFF and ELF, relocations against symbols are done in two
522 parts, with a HI relocation and a LO relocation. Each relocation
523 has only 16 bits of space to store an addend. This means that in
524 order for the linker to handle carries correctly, it must be able
525 to locate both the HI and the LO relocation. This means that the
526 relocations must appear in order in the relocation table.
528 In order to implement this, we keep track of each unmatched HI
529 relocation. We then sort them so that they immediately precede the
530 corresponding LO relocation. */
532 struct mips_hi_fixup
534 /* Next HI fixup. */
535 struct mips_hi_fixup *next;
536 /* This fixup. */
537 fixS *fixp;
538 /* The section this fixup is in. */
539 segT seg;
542 /* The list of unmatched HI relocs. */
544 static struct mips_hi_fixup *mips_hi_fixup_list;
546 /* Map normal MIPS register numbers to mips16 register numbers. */
548 #define X ILLEGAL_REG
549 static const int mips32_to_16_reg_map[] =
551 X, X, 2, 3, 4, 5, 6, 7,
552 X, X, X, X, X, X, X, X,
553 0, 1, X, X, X, X, X, X,
554 X, X, X, X, X, X, X, X
556 #undef X
558 /* Map mips16 register numbers to normal MIPS register numbers. */
560 static const unsigned int mips16_to_32_reg_map[] =
562 16, 17, 2, 3, 4, 5, 6, 7
565 static int mips_fix_4122_bugs;
567 /* We don't relax branches by default, since this causes us to expand
568 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
569 fail to compute the offset before expanding the macro to the most
570 efficient expansion. */
572 static int mips_relax_branch;
574 /* Since the MIPS does not have multiple forms of PC relative
575 instructions, we do not have to do relaxing as is done on other
576 platforms. However, we do have to handle GP relative addressing
577 correctly, which turns out to be a similar problem.
579 Every macro that refers to a symbol can occur in (at least) two
580 forms, one with GP relative addressing and one without. For
581 example, loading a global variable into a register generally uses
582 a macro instruction like this:
583 lw $4,i
584 If i can be addressed off the GP register (this is true if it is in
585 the .sbss or .sdata section, or if it is known to be smaller than
586 the -G argument) this will generate the following instruction:
587 lw $4,i($gp)
588 This instruction will use a GPREL reloc. If i can not be addressed
589 off the GP register, the following instruction sequence will be used:
590 lui $at,i
591 lw $4,i($at)
592 In this case the first instruction will have a HI16 reloc, and the
593 second reloc will have a LO16 reloc. Both relocs will be against
594 the symbol i.
596 The issue here is that we may not know whether i is GP addressable
597 until after we see the instruction that uses it. Therefore, we
598 want to be able to choose the final instruction sequence only at
599 the end of the assembly. This is similar to the way other
600 platforms choose the size of a PC relative instruction only at the
601 end of assembly.
603 When generating position independent code we do not use GP
604 addressing in quite the same way, but the issue still arises as
605 external symbols and local symbols must be handled differently.
607 We handle these issues by actually generating both possible
608 instruction sequences. The longer one is put in a frag_var with
609 type rs_machine_dependent. We encode what to do with the frag in
610 the subtype field. We encode (1) the number of existing bytes to
611 replace, (2) the number of new bytes to use, (3) the offset from
612 the start of the existing bytes to the first reloc we must generate
613 (that is, the offset is applied from the start of the existing
614 bytes after they are replaced by the new bytes, if any), (4) the
615 offset from the start of the existing bytes to the second reloc,
616 (5) whether a third reloc is needed (the third reloc is always four
617 bytes after the second reloc), and (6) whether to warn if this
618 variant is used (this is sometimes needed if .set nomacro or .set
619 noat is in effect). All these numbers are reasonably small.
621 Generating two instruction sequences must be handled carefully to
622 ensure that delay slots are handled correctly. Fortunately, there
623 are a limited number of cases. When the second instruction
624 sequence is generated, append_insn is directed to maintain the
625 existing delay slot information, so it continues to apply to any
626 code after the second instruction sequence. This means that the
627 second instruction sequence must not impose any requirements not
628 required by the first instruction sequence.
630 These variant frags are then handled in functions called by the
631 machine independent code. md_estimate_size_before_relax returns
632 the final size of the frag. md_convert_frag sets up the final form
633 of the frag. tc_gen_reloc adjust the first reloc and adds a second
634 one if needed. */
635 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
636 ((relax_substateT) \
637 (((old) << 23) \
638 | ((new) << 16) \
639 | (((reloc1) + 64) << 9) \
640 | (((reloc2) + 64) << 2) \
641 | ((reloc3) ? (1 << 1) : 0) \
642 | ((warn) ? 1 : 0)))
643 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
644 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
645 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
646 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
647 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
648 #define RELAX_WARN(i) ((i) & 1)
650 /* Branch without likely bit. If label is out of range, we turn:
652 beq reg1, reg2, label
653 delay slot
655 into
657 bne reg1, reg2, 0f
659 j label
660 0: delay slot
662 with the following opcode replacements:
664 beq <-> bne
665 blez <-> bgtz
666 bltz <-> bgez
667 bc1f <-> bc1t
669 bltzal <-> bgezal (with jal label instead of j label)
671 Even though keeping the delay slot instruction in the delay slot of
672 the branch would be more efficient, it would be very tricky to do
673 correctly, because we'd have to introduce a variable frag *after*
674 the delay slot instruction, and expand that instead. Let's do it
675 the easy way for now, even if the branch-not-taken case now costs
676 one additional instruction. Out-of-range branches are not supposed
677 to be common, anyway.
679 Branch likely. If label is out of range, we turn:
681 beql reg1, reg2, label
682 delay slot (annulled if branch not taken)
684 into
686 beql reg1, reg2, 1f
688 beql $0, $0, 2f
690 1: j[al] label
691 delay slot (executed only if branch taken)
694 It would be possible to generate a shorter sequence by losing the
695 likely bit, generating something like:
697 bne reg1, reg2, 0f
699 j[al] label
700 delay slot (executed only if branch taken)
703 beql -> bne
704 bnel -> beq
705 blezl -> bgtz
706 bgtzl -> blez
707 bltzl -> bgez
708 bgezl -> bltz
709 bc1fl -> bc1t
710 bc1tl -> bc1f
712 bltzall -> bgezal (with jal label instead of j label)
713 bgezall -> bltzal (ditto)
716 but it's not clear that it would actually improve performance. */
717 #define RELAX_BRANCH_ENCODE(reloc_s2, uncond, likely, link, toofar) \
718 ((relax_substateT) \
719 (0xc0000000 \
720 | ((toofar) ? 1 : 0) \
721 | ((link) ? 2 : 0) \
722 | ((likely) ? 4 : 0) \
723 | ((uncond) ? 8 : 0) \
724 | ((reloc_s2) ? 16 : 0)))
725 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
726 #define RELAX_BRANCH_RELOC_S2(i) (((i) & 16) != 0)
727 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
728 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
729 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
730 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1))
732 /* For mips16 code, we use an entirely different form of relaxation.
733 mips16 supports two versions of most instructions which take
734 immediate values: a small one which takes some small value, and a
735 larger one which takes a 16 bit value. Since branches also follow
736 this pattern, relaxing these values is required.
738 We can assemble both mips16 and normal MIPS code in a single
739 object. Therefore, we need to support this type of relaxation at
740 the same time that we support the relaxation described above. We
741 use the high bit of the subtype field to distinguish these cases.
743 The information we store for this type of relaxation is the
744 argument code found in the opcode file for this relocation, whether
745 the user explicitly requested a small or extended form, and whether
746 the relocation is in a jump or jal delay slot. That tells us the
747 size of the value, and how it should be stored. We also store
748 whether the fragment is considered to be extended or not. We also
749 store whether this is known to be a branch to a different section,
750 whether we have tried to relax this frag yet, and whether we have
751 ever extended a PC relative fragment because of a shift count. */
752 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
753 (0x80000000 \
754 | ((type) & 0xff) \
755 | ((small) ? 0x100 : 0) \
756 | ((ext) ? 0x200 : 0) \
757 | ((dslot) ? 0x400 : 0) \
758 | ((jal_dslot) ? 0x800 : 0))
759 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
760 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
761 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
762 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
763 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
764 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
765 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
766 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
767 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
768 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
769 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
770 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
772 /* Is the given value a sign-extended 32-bit value? */
773 #define IS_SEXT_32BIT_NUM(x) \
774 (((x) &~ (offsetT) 0x7fffffff) == 0 \
775 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
777 /* Is the given value a sign-extended 16-bit value? */
778 #define IS_SEXT_16BIT_NUM(x) \
779 (((x) &~ (offsetT) 0x7fff) == 0 \
780 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
783 /* Prototypes for static functions. */
785 #ifdef __STDC__
786 #define internalError() \
787 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
788 #else
789 #define internalError() as_fatal (_("MIPS internal Error"));
790 #endif
792 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
794 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
795 unsigned int reg, enum mips_regclass class));
796 static int reg_needs_delay PARAMS ((unsigned int));
797 static void mips16_mark_labels PARAMS ((void));
798 static void append_insn PARAMS ((char *place,
799 struct mips_cl_insn * ip,
800 expressionS * p,
801 bfd_reloc_code_real_type *r,
802 boolean));
803 static void mips_no_prev_insn PARAMS ((int));
804 static void mips_emit_delays PARAMS ((boolean));
805 #ifdef USE_STDARG
806 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
807 const char *name, const char *fmt,
808 ...));
809 #else
810 static void macro_build ();
811 #endif
812 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
813 const char *, const char *,
814 va_list));
815 static void macro_build_jalr PARAMS ((int, expressionS *));
816 static void macro_build_lui PARAMS ((char *place, int *counter,
817 expressionS * ep, int regnum));
818 static void macro_build_ldst_constoffset PARAMS ((char *place, int *counter,
819 expressionS * ep, const char *op,
820 int valreg, int breg));
821 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
822 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
823 expressionS *));
824 static void load_register PARAMS ((int *, int, expressionS *, int));
825 static void load_address PARAMS ((int *, int, expressionS *, int *));
826 static void move_register PARAMS ((int *, int, int));
827 static void macro PARAMS ((struct mips_cl_insn * ip));
828 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
829 #ifdef LOSING_COMPILER
830 static void macro2 PARAMS ((struct mips_cl_insn * ip));
831 #endif
832 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
833 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
834 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
835 boolean, boolean, unsigned long *,
836 boolean *, unsigned short *));
837 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
838 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
839 static int my_getSmallExpression PARAMS ((expressionS *, char *));
840 static void my_getExpression PARAMS ((expressionS *, char *));
841 #ifdef OBJ_ELF
842 static int support_64bit_objects PARAMS((void));
843 #endif
844 static void mips_set_option_string PARAMS ((const char **, const char *));
845 static symbolS *get_symbol PARAMS ((void));
846 static void mips_align PARAMS ((int to, int fill, symbolS *label));
847 static void s_align PARAMS ((int));
848 static void s_change_sec PARAMS ((int));
849 static void s_change_section PARAMS ((int));
850 static void s_cons PARAMS ((int));
851 static void s_float_cons PARAMS ((int));
852 static void s_mips_globl PARAMS ((int));
853 static void s_option PARAMS ((int));
854 static void s_mipsset PARAMS ((int));
855 static void s_abicalls PARAMS ((int));
856 static void s_cpload PARAMS ((int));
857 static void s_cpsetup PARAMS ((int));
858 static void s_cplocal PARAMS ((int));
859 static void s_cprestore PARAMS ((int));
860 static void s_cpreturn PARAMS ((int));
861 static void s_gpvalue PARAMS ((int));
862 static void s_gpword PARAMS ((int));
863 static void s_gpdword PARAMS ((int));
864 static void s_cpadd PARAMS ((int));
865 static void s_insn PARAMS ((int));
866 static void md_obj_begin PARAMS ((void));
867 static void md_obj_end PARAMS ((void));
868 static long get_number PARAMS ((void));
869 static void s_mips_ent PARAMS ((int));
870 static void s_mips_end PARAMS ((int));
871 static void s_mips_frame PARAMS ((int));
872 static void s_mips_mask PARAMS ((int));
873 static void s_mips_stab PARAMS ((int));
874 static void s_mips_weakext PARAMS ((int));
875 static void s_mips_file PARAMS ((int));
876 static void s_mips_loc PARAMS ((int));
877 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
878 static int relaxed_branch_length (fragS *, asection *, int);
879 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
880 static void show PARAMS ((FILE *, const char *, int *, int *));
881 #ifdef OBJ_ELF
882 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
883 #endif
885 /* Return values of my_getSmallExpression(). */
887 enum small_ex_type
889 S_EX_NONE = 0,
890 S_EX_REGISTER,
892 /* Direct relocation creation by %percent_op(). */
893 S_EX_HALF,
894 S_EX_HI,
895 S_EX_LO,
896 S_EX_GP_REL,
897 S_EX_GOT,
898 S_EX_CALL16,
899 S_EX_GOT_DISP,
900 S_EX_GOT_PAGE,
901 S_EX_GOT_OFST,
902 S_EX_GOT_HI,
903 S_EX_GOT_LO,
904 S_EX_NEG,
905 S_EX_HIGHER,
906 S_EX_HIGHEST,
907 S_EX_CALL_HI,
908 S_EX_CALL_LO
911 /* Table and functions used to map between CPU/ISA names, and
912 ISA levels, and CPU numbers. */
914 struct mips_cpu_info
916 const char *name; /* CPU or ISA name. */
917 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
918 int isa; /* ISA level. */
919 int cpu; /* CPU number (default CPU if ISA). */
922 static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
923 static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
924 static boolean mips_strict_matching_cpu_name_p PARAMS ((const char *,
925 const char *));
926 static boolean mips_matching_cpu_name_p PARAMS ((const char *, const char *));
927 static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
928 const char *));
929 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
931 /* Pseudo-op table.
933 The following pseudo-ops from the Kane and Heinrich MIPS book
934 should be defined here, but are currently unsupported: .alias,
935 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
937 The following pseudo-ops from the Kane and Heinrich MIPS book are
938 specific to the type of debugging information being generated, and
939 should be defined by the object format: .aent, .begin, .bend,
940 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
941 .vreg.
943 The following pseudo-ops from the Kane and Heinrich MIPS book are
944 not MIPS CPU specific, but are also not specific to the object file
945 format. This file is probably the best place to define them, but
946 they are not currently supported: .asm0, .endr, .lab, .repeat,
947 .struct. */
949 static const pseudo_typeS mips_pseudo_table[] =
951 /* MIPS specific pseudo-ops. */
952 {"option", s_option, 0},
953 {"set", s_mipsset, 0},
954 {"rdata", s_change_sec, 'r'},
955 {"sdata", s_change_sec, 's'},
956 {"livereg", s_ignore, 0},
957 {"abicalls", s_abicalls, 0},
958 {"cpload", s_cpload, 0},
959 {"cpsetup", s_cpsetup, 0},
960 {"cplocal", s_cplocal, 0},
961 {"cprestore", s_cprestore, 0},
962 {"cpreturn", s_cpreturn, 0},
963 {"gpvalue", s_gpvalue, 0},
964 {"gpword", s_gpword, 0},
965 {"gpdword", s_gpdword, 0},
966 {"cpadd", s_cpadd, 0},
967 {"insn", s_insn, 0},
969 /* Relatively generic pseudo-ops that happen to be used on MIPS
970 chips. */
971 {"asciiz", stringer, 1},
972 {"bss", s_change_sec, 'b'},
973 {"err", s_err, 0},
974 {"half", s_cons, 1},
975 {"dword", s_cons, 3},
976 {"weakext", s_mips_weakext, 0},
978 /* These pseudo-ops are defined in read.c, but must be overridden
979 here for one reason or another. */
980 {"align", s_align, 0},
981 {"byte", s_cons, 0},
982 {"data", s_change_sec, 'd'},
983 {"double", s_float_cons, 'd'},
984 {"float", s_float_cons, 'f'},
985 {"globl", s_mips_globl, 0},
986 {"global", s_mips_globl, 0},
987 {"hword", s_cons, 1},
988 {"int", s_cons, 2},
989 {"long", s_cons, 2},
990 {"octa", s_cons, 4},
991 {"quad", s_cons, 3},
992 {"section", s_change_section, 0},
993 {"short", s_cons, 1},
994 {"single", s_float_cons, 'f'},
995 {"stabn", s_mips_stab, 'n'},
996 {"text", s_change_sec, 't'},
997 {"word", s_cons, 2},
999 { "extern", ecoff_directive_extern, 0},
1001 { NULL, NULL, 0 },
1004 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1006 /* These pseudo-ops should be defined by the object file format.
1007 However, a.out doesn't support them, so we have versions here. */
1008 {"aent", s_mips_ent, 1},
1009 {"bgnb", s_ignore, 0},
1010 {"end", s_mips_end, 0},
1011 {"endb", s_ignore, 0},
1012 {"ent", s_mips_ent, 0},
1013 {"file", s_mips_file, 0},
1014 {"fmask", s_mips_mask, 'F'},
1015 {"frame", s_mips_frame, 0},
1016 {"loc", s_mips_loc, 0},
1017 {"mask", s_mips_mask, 'R'},
1018 {"verstamp", s_ignore, 0},
1019 { NULL, NULL, 0 },
1022 extern void pop_insert PARAMS ((const pseudo_typeS *));
1024 void
1025 mips_pop_insert ()
1027 pop_insert (mips_pseudo_table);
1028 if (! ECOFF_DEBUGGING)
1029 pop_insert (mips_nonecoff_pseudo_table);
1032 /* Symbols labelling the current insn. */
1034 struct insn_label_list
1036 struct insn_label_list *next;
1037 symbolS *label;
1040 static struct insn_label_list *insn_labels;
1041 static struct insn_label_list *free_insn_labels;
1043 static void mips_clear_insn_labels PARAMS ((void));
1045 static inline void
1046 mips_clear_insn_labels ()
1048 register struct insn_label_list **pl;
1050 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1052 *pl = insn_labels;
1053 insn_labels = NULL;
1056 static char *expr_end;
1058 /* Expressions which appear in instructions. These are set by
1059 mips_ip. */
1061 static expressionS imm_expr;
1062 static expressionS offset_expr;
1064 /* Relocs associated with imm_expr and offset_expr. */
1066 static bfd_reloc_code_real_type imm_reloc[3]
1067 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1068 static bfd_reloc_code_real_type offset_reloc[3]
1069 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1071 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
1073 static boolean imm_unmatched_hi;
1075 /* These are set by mips16_ip if an explicit extension is used. */
1077 static boolean mips16_small, mips16_ext;
1079 #ifdef OBJ_ELF
1080 /* The pdr segment for per procedure frame/regmask info. Not used for
1081 ECOFF debugging. */
1083 static segT pdr_seg;
1084 #endif
1086 /* The default target format to use. */
1088 const char *
1089 mips_target_format ()
1091 switch (OUTPUT_FLAVOR)
1093 case bfd_target_aout_flavour:
1094 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1095 case bfd_target_ecoff_flavour:
1096 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1097 case bfd_target_coff_flavour:
1098 return "pe-mips";
1099 case bfd_target_elf_flavour:
1100 #ifdef TE_TMIPS
1101 /* This is traditional mips. */
1102 return (target_big_endian
1103 ? (HAVE_64BIT_OBJECTS
1104 ? "elf64-tradbigmips"
1105 : (HAVE_NEWABI
1106 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1107 : (HAVE_64BIT_OBJECTS
1108 ? "elf64-tradlittlemips"
1109 : (HAVE_NEWABI
1110 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1111 #else
1112 return (target_big_endian
1113 ? (HAVE_64BIT_OBJECTS
1114 ? "elf64-bigmips"
1115 : (HAVE_NEWABI
1116 ? "elf32-nbigmips" : "elf32-bigmips"))
1117 : (HAVE_64BIT_OBJECTS
1118 ? "elf64-littlemips"
1119 : (HAVE_NEWABI
1120 ? "elf32-nlittlemips" : "elf32-littlemips")));
1121 #endif
1122 default:
1123 abort ();
1124 return NULL;
1128 /* This function is called once, at assembler startup time. It should
1129 set up all the tables, etc. that the MD part of the assembler will need. */
1131 void
1132 md_begin ()
1134 register const char *retval = NULL;
1135 int i = 0;
1136 int broken = 0;
1138 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1139 as_warn (_("Could not set architecture and machine"));
1141 op_hash = hash_new ();
1143 for (i = 0; i < NUMOPCODES;)
1145 const char *name = mips_opcodes[i].name;
1147 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1148 if (retval != NULL)
1150 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1151 mips_opcodes[i].name, retval);
1152 /* Probably a memory allocation problem? Give up now. */
1153 as_fatal (_("Broken assembler. No assembly attempted."));
1157 if (mips_opcodes[i].pinfo != INSN_MACRO)
1159 if (!validate_mips_insn (&mips_opcodes[i]))
1160 broken = 1;
1162 ++i;
1164 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1167 mips16_op_hash = hash_new ();
1169 i = 0;
1170 while (i < bfd_mips16_num_opcodes)
1172 const char *name = mips16_opcodes[i].name;
1174 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1175 if (retval != NULL)
1176 as_fatal (_("internal: can't hash `%s': %s"),
1177 mips16_opcodes[i].name, retval);
1180 if (mips16_opcodes[i].pinfo != INSN_MACRO
1181 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1182 != mips16_opcodes[i].match))
1184 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1185 mips16_opcodes[i].name, mips16_opcodes[i].args);
1186 broken = 1;
1188 ++i;
1190 while (i < bfd_mips16_num_opcodes
1191 && strcmp (mips16_opcodes[i].name, name) == 0);
1194 if (broken)
1195 as_fatal (_("Broken assembler. No assembly attempted."));
1197 /* We add all the general register names to the symbol table. This
1198 helps us detect invalid uses of them. */
1199 for (i = 0; i < 32; i++)
1201 char buf[5];
1203 sprintf (buf, "$%d", i);
1204 symbol_table_insert (symbol_new (buf, reg_section, i,
1205 &zero_address_frag));
1207 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1208 &zero_address_frag));
1209 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1210 &zero_address_frag));
1211 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1212 &zero_address_frag));
1213 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1214 &zero_address_frag));
1215 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1216 &zero_address_frag));
1217 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1218 &zero_address_frag));
1219 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1220 &zero_address_frag));
1221 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1222 &zero_address_frag));
1223 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1224 &zero_address_frag));
1226 mips_no_prev_insn (false);
1228 mips_gprmask = 0;
1229 mips_cprmask[0] = 0;
1230 mips_cprmask[1] = 0;
1231 mips_cprmask[2] = 0;
1232 mips_cprmask[3] = 0;
1234 /* set the default alignment for the text section (2**2) */
1235 record_alignment (text_section, 2);
1237 if (USE_GLOBAL_POINTER_OPT)
1238 bfd_set_gp_size (stdoutput, g_switch_value);
1240 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1242 /* On a native system, sections must be aligned to 16 byte
1243 boundaries. When configured for an embedded ELF target, we
1244 don't bother. */
1245 if (strcmp (TARGET_OS, "elf") != 0)
1247 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1248 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1249 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1252 /* Create a .reginfo section for register masks and a .mdebug
1253 section for debugging information. */
1255 segT seg;
1256 subsegT subseg;
1257 flagword flags;
1258 segT sec;
1260 seg = now_seg;
1261 subseg = now_subseg;
1263 /* The ABI says this section should be loaded so that the
1264 running program can access it. However, we don't load it
1265 if we are configured for an embedded target */
1266 flags = SEC_READONLY | SEC_DATA;
1267 if (strcmp (TARGET_OS, "elf") != 0)
1268 flags |= SEC_ALLOC | SEC_LOAD;
1270 if (mips_abi != N64_ABI)
1272 sec = subseg_new (".reginfo", (subsegT) 0);
1274 bfd_set_section_flags (stdoutput, sec, flags);
1275 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1277 #ifdef OBJ_ELF
1278 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1279 #endif
1281 else
1283 /* The 64-bit ABI uses a .MIPS.options section rather than
1284 .reginfo section. */
1285 sec = subseg_new (".MIPS.options", (subsegT) 0);
1286 bfd_set_section_flags (stdoutput, sec, flags);
1287 bfd_set_section_alignment (stdoutput, sec, 3);
1289 #ifdef OBJ_ELF
1290 /* Set up the option header. */
1292 Elf_Internal_Options opthdr;
1293 char *f;
1295 opthdr.kind = ODK_REGINFO;
1296 opthdr.size = (sizeof (Elf_External_Options)
1297 + sizeof (Elf64_External_RegInfo));
1298 opthdr.section = 0;
1299 opthdr.info = 0;
1300 f = frag_more (sizeof (Elf_External_Options));
1301 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1302 (Elf_External_Options *) f);
1304 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1306 #endif
1309 if (ECOFF_DEBUGGING)
1311 sec = subseg_new (".mdebug", (subsegT) 0);
1312 (void) bfd_set_section_flags (stdoutput, sec,
1313 SEC_HAS_CONTENTS | SEC_READONLY);
1314 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1316 #ifdef OBJ_ELF
1317 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1319 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1320 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1321 SEC_READONLY | SEC_RELOC
1322 | SEC_DEBUGGING);
1323 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1325 #endif
1327 subseg_set (seg, subseg);
1331 if (! ECOFF_DEBUGGING)
1332 md_obj_begin ();
1335 void
1336 md_mips_end ()
1338 if (! ECOFF_DEBUGGING)
1339 md_obj_end ();
1342 void
1343 md_assemble (str)
1344 char *str;
1346 struct mips_cl_insn insn;
1347 bfd_reloc_code_real_type unused_reloc[3]
1348 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1350 imm_expr.X_op = O_absent;
1351 imm_unmatched_hi = false;
1352 offset_expr.X_op = O_absent;
1353 imm_reloc[0] = BFD_RELOC_UNUSED;
1354 imm_reloc[1] = BFD_RELOC_UNUSED;
1355 imm_reloc[2] = BFD_RELOC_UNUSED;
1356 offset_reloc[0] = BFD_RELOC_UNUSED;
1357 offset_reloc[1] = BFD_RELOC_UNUSED;
1358 offset_reloc[2] = BFD_RELOC_UNUSED;
1360 if (mips_opts.mips16)
1361 mips16_ip (str, &insn);
1362 else
1364 mips_ip (str, &insn);
1365 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1366 str, insn.insn_opcode));
1369 if (insn_error)
1371 as_bad ("%s `%s'", insn_error, str);
1372 return;
1375 if (insn.insn_mo->pinfo == INSN_MACRO)
1377 if (mips_opts.mips16)
1378 mips16_macro (&insn);
1379 else
1380 macro (&insn);
1382 else
1384 if (imm_expr.X_op != O_absent)
1385 append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1386 else if (offset_expr.X_op != O_absent)
1387 append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1388 else
1389 append_insn (NULL, &insn, NULL, unused_reloc, false);
1393 /* See whether instruction IP reads register REG. CLASS is the type
1394 of register. */
1396 static int
1397 insn_uses_reg (ip, reg, class)
1398 struct mips_cl_insn *ip;
1399 unsigned int reg;
1400 enum mips_regclass class;
1402 if (class == MIPS16_REG)
1404 assert (mips_opts.mips16);
1405 reg = mips16_to_32_reg_map[reg];
1406 class = MIPS_GR_REG;
1409 /* Don't report on general register ZERO, since it never changes. */
1410 if (class == MIPS_GR_REG && reg == ZERO)
1411 return 0;
1413 if (class == MIPS_FP_REG)
1415 assert (! mips_opts.mips16);
1416 /* If we are called with either $f0 or $f1, we must check $f0.
1417 This is not optimal, because it will introduce an unnecessary
1418 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1419 need to distinguish reading both $f0 and $f1 or just one of
1420 them. Note that we don't have to check the other way,
1421 because there is no instruction that sets both $f0 and $f1
1422 and requires a delay. */
1423 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1424 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1425 == (reg &~ (unsigned) 1)))
1426 return 1;
1427 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1428 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1429 == (reg &~ (unsigned) 1)))
1430 return 1;
1432 else if (! mips_opts.mips16)
1434 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1435 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1436 return 1;
1437 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1438 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1439 return 1;
1441 else
1443 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1444 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1445 & MIPS16OP_MASK_RX)]
1446 == reg))
1447 return 1;
1448 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1449 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1450 & MIPS16OP_MASK_RY)]
1451 == reg))
1452 return 1;
1453 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1454 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1455 & MIPS16OP_MASK_MOVE32Z)]
1456 == reg))
1457 return 1;
1458 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1459 return 1;
1460 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1461 return 1;
1462 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1463 return 1;
1464 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1465 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1466 & MIPS16OP_MASK_REGR32) == reg)
1467 return 1;
1470 return 0;
1473 /* This function returns true if modifying a register requires a
1474 delay. */
1476 static int
1477 reg_needs_delay (reg)
1478 unsigned int reg;
1480 unsigned long prev_pinfo;
1482 prev_pinfo = prev_insn.insn_mo->pinfo;
1483 if (! mips_opts.noreorder
1484 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1485 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1486 || (! gpr_interlocks
1487 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1489 /* A load from a coprocessor or from memory. All load
1490 delays delay the use of general register rt for one
1491 instruction on the r3000. The r6000 and r4000 use
1492 interlocks. */
1493 /* Itbl support may require additional care here. */
1494 know (prev_pinfo & INSN_WRITE_GPR_T);
1495 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1496 return 1;
1499 return 0;
1502 /* Mark instruction labels in mips16 mode. This permits the linker to
1503 handle them specially, such as generating jalx instructions when
1504 needed. We also make them odd for the duration of the assembly, in
1505 order to generate the right sort of code. We will make them even
1506 in the adjust_symtab routine, while leaving them marked. This is
1507 convenient for the debugger and the disassembler. The linker knows
1508 to make them odd again. */
1510 static void
1511 mips16_mark_labels ()
1513 if (mips_opts.mips16)
1515 struct insn_label_list *l;
1516 valueT val;
1518 for (l = insn_labels; l != NULL; l = l->next)
1520 #ifdef OBJ_ELF
1521 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1522 S_SET_OTHER (l->label, STO_MIPS16);
1523 #endif
1524 val = S_GET_VALUE (l->label);
1525 if ((val & 1) == 0)
1526 S_SET_VALUE (l->label, val + 1);
1531 /* Output an instruction. PLACE is where to put the instruction; if
1532 it is NULL, this uses frag_more to get room. IP is the instruction
1533 information. ADDRESS_EXPR is an operand of the instruction to be
1534 used with RELOC_TYPE. */
1536 static void
1537 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1538 char *place;
1539 struct mips_cl_insn *ip;
1540 expressionS *address_expr;
1541 bfd_reloc_code_real_type *reloc_type;
1542 boolean unmatched_hi;
1544 register unsigned long prev_pinfo, pinfo;
1545 char *f;
1546 fixS *fixp[3];
1547 int nops = 0;
1549 /* Mark instruction labels in mips16 mode. */
1550 mips16_mark_labels ();
1552 prev_pinfo = prev_insn.insn_mo->pinfo;
1553 pinfo = ip->insn_mo->pinfo;
1555 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1557 int prev_prev_nop;
1559 /* If the previous insn required any delay slots, see if we need
1560 to insert a NOP or two. There are eight kinds of possible
1561 hazards, of which an instruction can have at most one type.
1562 (1) a load from memory delay
1563 (2) a load from a coprocessor delay
1564 (3) an unconditional branch delay
1565 (4) a conditional branch delay
1566 (5) a move to coprocessor register delay
1567 (6) a load coprocessor register from memory delay
1568 (7) a coprocessor condition code delay
1569 (8) a HI/LO special register delay
1571 There are a lot of optimizations we could do that we don't.
1572 In particular, we do not, in general, reorder instructions.
1573 If you use gcc with optimization, it will reorder
1574 instructions and generally do much more optimization then we
1575 do here; repeating all that work in the assembler would only
1576 benefit hand written assembly code, and does not seem worth
1577 it. */
1579 /* This is how a NOP is emitted. */
1580 #define emit_nop() \
1581 (mips_opts.mips16 \
1582 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1583 : md_number_to_chars (frag_more (4), 0, 4))
1585 /* The previous insn might require a delay slot, depending upon
1586 the contents of the current insn. */
1587 if (! mips_opts.mips16
1588 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1589 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1590 && ! cop_interlocks)
1591 || (! gpr_interlocks
1592 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1594 /* A load from a coprocessor or from memory. All load
1595 delays delay the use of general register rt for one
1596 instruction on the r3000. The r6000 and r4000 use
1597 interlocks. */
1598 /* Itbl support may require additional care here. */
1599 know (prev_pinfo & INSN_WRITE_GPR_T);
1600 if (mips_optimize == 0
1601 || insn_uses_reg (ip,
1602 ((prev_insn.insn_opcode >> OP_SH_RT)
1603 & OP_MASK_RT),
1604 MIPS_GR_REG))
1605 ++nops;
1607 else if (! mips_opts.mips16
1608 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1609 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1610 && ! cop_interlocks)
1611 || (mips_opts.isa == ISA_MIPS1
1612 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1614 /* A generic coprocessor delay. The previous instruction
1615 modified a coprocessor general or control register. If
1616 it modified a control register, we need to avoid any
1617 coprocessor instruction (this is probably not always
1618 required, but it sometimes is). If it modified a general
1619 register, we avoid using that register.
1621 On the r6000 and r4000 loading a coprocessor register
1622 from memory is interlocked, and does not require a delay.
1624 This case is not handled very well. There is no special
1625 knowledge of CP0 handling, and the coprocessors other
1626 than the floating point unit are not distinguished at
1627 all. */
1628 /* Itbl support may require additional care here. FIXME!
1629 Need to modify this to include knowledge about
1630 user specified delays! */
1631 if (prev_pinfo & INSN_WRITE_FPR_T)
1633 if (mips_optimize == 0
1634 || insn_uses_reg (ip,
1635 ((prev_insn.insn_opcode >> OP_SH_FT)
1636 & OP_MASK_FT),
1637 MIPS_FP_REG))
1638 ++nops;
1640 else if (prev_pinfo & INSN_WRITE_FPR_S)
1642 if (mips_optimize == 0
1643 || insn_uses_reg (ip,
1644 ((prev_insn.insn_opcode >> OP_SH_FS)
1645 & OP_MASK_FS),
1646 MIPS_FP_REG))
1647 ++nops;
1649 else
1651 /* We don't know exactly what the previous instruction
1652 does. If the current instruction uses a coprocessor
1653 register, we must insert a NOP. If previous
1654 instruction may set the condition codes, and the
1655 current instruction uses them, we must insert two
1656 NOPS. */
1657 /* Itbl support may require additional care here. */
1658 if (mips_optimize == 0
1659 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1660 && (pinfo & INSN_READ_COND_CODE)))
1661 nops += 2;
1662 else if (pinfo & INSN_COP)
1663 ++nops;
1666 else if (! mips_opts.mips16
1667 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1668 && (prev_pinfo & INSN_WRITE_COND_CODE)
1669 && ! cop_interlocks)
1671 /* The previous instruction sets the coprocessor condition
1672 codes, but does not require a general coprocessor delay
1673 (this means it is a floating point comparison
1674 instruction). If this instruction uses the condition
1675 codes, we need to insert a single NOP. */
1676 /* Itbl support may require additional care here. */
1677 if (mips_optimize == 0
1678 || (pinfo & INSN_READ_COND_CODE))
1679 ++nops;
1682 /* If we're fixing up mfhi/mflo for the r7000 and the
1683 previous insn was an mfhi/mflo and the current insn
1684 reads the register that the mfhi/mflo wrote to, then
1685 insert two nops. */
1687 else if (mips_7000_hilo_fix
1688 && MF_HILO_INSN (prev_pinfo)
1689 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1690 & OP_MASK_RD),
1691 MIPS_GR_REG))
1693 nops += 2;
1696 /* If we're fixing up mfhi/mflo for the r7000 and the
1697 2nd previous insn was an mfhi/mflo and the current insn
1698 reads the register that the mfhi/mflo wrote to, then
1699 insert one nop. */
1701 else if (mips_7000_hilo_fix
1702 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1703 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1704 & OP_MASK_RD),
1705 MIPS_GR_REG))
1708 ++nops;
1711 else if (prev_pinfo & INSN_READ_LO)
1713 /* The previous instruction reads the LO register; if the
1714 current instruction writes to the LO register, we must
1715 insert two NOPS. Some newer processors have interlocks.
1716 Also the tx39's multiply instructions can be exectuted
1717 immediatly after a read from HI/LO (without the delay),
1718 though the tx39's divide insns still do require the
1719 delay. */
1720 if (! (hilo_interlocks
1721 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1722 && (mips_optimize == 0
1723 || (pinfo & INSN_WRITE_LO)))
1724 nops += 2;
1725 /* Most mips16 branch insns don't have a delay slot.
1726 If a read from LO is immediately followed by a branch
1727 to a write to LO we have a read followed by a write
1728 less than 2 insns away. We assume the target of
1729 a branch might be a write to LO, and insert a nop
1730 between a read and an immediately following branch. */
1731 else if (mips_opts.mips16
1732 && (mips_optimize == 0
1733 || (pinfo & MIPS16_INSN_BRANCH)))
1734 ++nops;
1736 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1738 /* The previous instruction reads the HI register; if the
1739 current instruction writes to the HI register, we must
1740 insert a NOP. Some newer processors have interlocks.
1741 Also the note tx39's multiply above. */
1742 if (! (hilo_interlocks
1743 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1744 && (mips_optimize == 0
1745 || (pinfo & INSN_WRITE_HI)))
1746 nops += 2;
1747 /* Most mips16 branch insns don't have a delay slot.
1748 If a read from HI is immediately followed by a branch
1749 to a write to HI we have a read followed by a write
1750 less than 2 insns away. We assume the target of
1751 a branch might be a write to HI, and insert a nop
1752 between a read and an immediately following branch. */
1753 else if (mips_opts.mips16
1754 && (mips_optimize == 0
1755 || (pinfo & MIPS16_INSN_BRANCH)))
1756 ++nops;
1759 /* If the previous instruction was in a noreorder section, then
1760 we don't want to insert the nop after all. */
1761 /* Itbl support may require additional care here. */
1762 if (prev_insn_unreordered)
1763 nops = 0;
1765 /* There are two cases which require two intervening
1766 instructions: 1) setting the condition codes using a move to
1767 coprocessor instruction which requires a general coprocessor
1768 delay and then reading the condition codes 2) reading the HI
1769 or LO register and then writing to it (except on processors
1770 which have interlocks). If we are not already emitting a NOP
1771 instruction, we must check for these cases compared to the
1772 instruction previous to the previous instruction. */
1773 if ((! mips_opts.mips16
1774 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1775 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1776 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1777 && (pinfo & INSN_READ_COND_CODE)
1778 && ! cop_interlocks)
1779 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1780 && (pinfo & INSN_WRITE_LO)
1781 && ! (hilo_interlocks
1782 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1783 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1784 && (pinfo & INSN_WRITE_HI)
1785 && ! (hilo_interlocks
1786 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1787 prev_prev_nop = 1;
1788 else
1789 prev_prev_nop = 0;
1791 if (prev_prev_insn_unreordered)
1792 prev_prev_nop = 0;
1794 if (prev_prev_nop && nops == 0)
1795 ++nops;
1797 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1799 /* We're out of bits in pinfo, so we must resort to string
1800 ops here. Shortcuts are selected based on opcodes being
1801 limited to the VR4122 instruction set. */
1802 int min_nops = 0;
1803 const char *pn = prev_insn.insn_mo->name;
1804 const char *tn = ip->insn_mo->name;
1805 if (strncmp(pn, "macc", 4) == 0
1806 || strncmp(pn, "dmacc", 5) == 0)
1808 /* Errata 21 - [D]DIV[U] after [D]MACC */
1809 if (strstr (tn, "div"))
1811 min_nops = 1;
1814 /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1815 if (pn[0] == 'd' /* dmacc */
1816 && (strncmp(tn, "dmult", 5) == 0
1817 || strncmp(tn, "dmacc", 5) == 0))
1819 min_nops = 1;
1822 /* Errata 24 - MT{LO,HI} after [D]MACC */
1823 if (strcmp (tn, "mtlo") == 0
1824 || strcmp (tn, "mthi") == 0)
1826 min_nops = 1;
1830 else if (strncmp(pn, "dmult", 5) == 0
1831 && (strncmp(tn, "dmult", 5) == 0
1832 || strncmp(tn, "dmacc", 5) == 0))
1834 /* Here is the rest of errata 23. */
1835 min_nops = 1;
1837 if (nops < min_nops)
1838 nops = min_nops;
1841 /* If we are being given a nop instruction, don't bother with
1842 one of the nops we would otherwise output. This will only
1843 happen when a nop instruction is used with mips_optimize set
1844 to 0. */
1845 if (nops > 0
1846 && ! mips_opts.noreorder
1847 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1848 --nops;
1850 /* Now emit the right number of NOP instructions. */
1851 if (nops > 0 && ! mips_opts.noreorder)
1853 fragS *old_frag;
1854 unsigned long old_frag_offset;
1855 int i;
1856 struct insn_label_list *l;
1858 old_frag = frag_now;
1859 old_frag_offset = frag_now_fix ();
1861 for (i = 0; i < nops; i++)
1862 emit_nop ();
1864 if (listing)
1866 listing_prev_line ();
1867 /* We may be at the start of a variant frag. In case we
1868 are, make sure there is enough space for the frag
1869 after the frags created by listing_prev_line. The
1870 argument to frag_grow here must be at least as large
1871 as the argument to all other calls to frag_grow in
1872 this file. We don't have to worry about being in the
1873 middle of a variant frag, because the variants insert
1874 all needed nop instructions themselves. */
1875 frag_grow (40);
1878 for (l = insn_labels; l != NULL; l = l->next)
1880 valueT val;
1882 assert (S_GET_SEGMENT (l->label) == now_seg);
1883 symbol_set_frag (l->label, frag_now);
1884 val = (valueT) frag_now_fix ();
1885 /* mips16 text labels are stored as odd. */
1886 if (mips_opts.mips16)
1887 ++val;
1888 S_SET_VALUE (l->label, val);
1891 #ifndef NO_ECOFF_DEBUGGING
1892 if (ECOFF_DEBUGGING)
1893 ecoff_fix_loc (old_frag, old_frag_offset);
1894 #endif
1896 else if (prev_nop_frag != NULL)
1898 /* We have a frag holding nops we may be able to remove. If
1899 we don't need any nops, we can decrease the size of
1900 prev_nop_frag by the size of one instruction. If we do
1901 need some nops, we count them in prev_nops_required. */
1902 if (prev_nop_frag_since == 0)
1904 if (nops == 0)
1906 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1907 --prev_nop_frag_holds;
1909 else
1910 prev_nop_frag_required += nops;
1912 else
1914 if (prev_prev_nop == 0)
1916 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1917 --prev_nop_frag_holds;
1919 else
1920 ++prev_nop_frag_required;
1923 if (prev_nop_frag_holds <= prev_nop_frag_required)
1924 prev_nop_frag = NULL;
1926 ++prev_nop_frag_since;
1928 /* Sanity check: by the time we reach the second instruction
1929 after prev_nop_frag, we should have used up all the nops
1930 one way or another. */
1931 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1935 if (place == NULL
1936 && address_expr
1937 && ((*reloc_type == BFD_RELOC_16_PCREL
1938 && address_expr->X_op != O_constant)
1939 || *reloc_type == BFD_RELOC_16_PCREL_S2)
1940 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
1941 || pinfo & INSN_COND_BRANCH_LIKELY)
1942 && mips_relax_branch
1943 /* Don't try branch relaxation within .set nomacro, or within
1944 .set noat if we use $at for PIC computations. If it turns
1945 out that the branch was out-of-range, we'll get an error. */
1946 && !mips_opts.warn_about_macros
1947 && !(mips_opts.noat && mips_pic != NO_PIC)
1948 && !mips_opts.mips16)
1950 f = frag_var (rs_machine_dependent,
1951 relaxed_branch_length
1952 (NULL, NULL,
1953 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
1954 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
1955 RELAX_BRANCH_ENCODE
1956 (*reloc_type == BFD_RELOC_16_PCREL_S2,
1957 pinfo & INSN_UNCOND_BRANCH_DELAY,
1958 pinfo & INSN_COND_BRANCH_LIKELY,
1959 pinfo & INSN_WRITE_GPR_31,
1961 address_expr->X_add_symbol,
1962 address_expr->X_add_number,
1964 *reloc_type = BFD_RELOC_UNUSED;
1966 else if (*reloc_type > BFD_RELOC_UNUSED)
1968 /* We need to set up a variant frag. */
1969 assert (mips_opts.mips16 && address_expr != NULL);
1970 f = frag_var (rs_machine_dependent, 4, 0,
1971 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1972 mips16_small, mips16_ext,
1973 (prev_pinfo
1974 & INSN_UNCOND_BRANCH_DELAY),
1975 (*prev_insn_reloc_type
1976 == BFD_RELOC_MIPS16_JMP)),
1977 make_expr_symbol (address_expr), 0, NULL);
1979 else if (place != NULL)
1980 f = place;
1981 else if (mips_opts.mips16
1982 && ! ip->use_extend
1983 && *reloc_type != BFD_RELOC_MIPS16_JMP)
1985 /* Make sure there is enough room to swap this instruction with
1986 a following jump instruction. */
1987 frag_grow (6);
1988 f = frag_more (2);
1990 else
1992 if (mips_opts.mips16
1993 && mips_opts.noreorder
1994 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1995 as_warn (_("extended instruction in delay slot"));
1997 f = frag_more (4);
2000 fixp[0] = fixp[1] = fixp[2] = NULL;
2001 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2003 if (address_expr->X_op == O_constant)
2005 valueT tmp;
2007 switch (*reloc_type)
2009 case BFD_RELOC_32:
2010 ip->insn_opcode |= address_expr->X_add_number;
2011 break;
2013 case BFD_RELOC_MIPS_HIGHEST:
2014 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2015 tmp >>= 16;
2016 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2017 break;
2019 case BFD_RELOC_MIPS_HIGHER:
2020 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2021 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2022 break;
2024 case BFD_RELOC_HI16_S:
2025 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2026 >> 16) & 0xffff;
2027 break;
2029 case BFD_RELOC_HI16:
2030 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2031 break;
2033 case BFD_RELOC_LO16:
2034 case BFD_RELOC_MIPS_GOT_DISP:
2035 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2036 break;
2038 case BFD_RELOC_MIPS_JMP:
2039 if ((address_expr->X_add_number & 3) != 0)
2040 as_bad (_("jump to misaligned address (0x%lx)"),
2041 (unsigned long) address_expr->X_add_number);
2042 if (address_expr->X_add_number & ~0xfffffff)
2043 as_bad (_("jump address range overflow (0x%lx)"),
2044 (unsigned long) address_expr->X_add_number);
2045 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2046 break;
2048 case BFD_RELOC_MIPS16_JMP:
2049 if ((address_expr->X_add_number & 3) != 0)
2050 as_bad (_("jump to misaligned address (0x%lx)"),
2051 (unsigned long) address_expr->X_add_number);
2052 if (address_expr->X_add_number & ~0xfffffff)
2053 as_bad (_("jump address range overflow (0x%lx)"),
2054 (unsigned long) address_expr->X_add_number);
2055 ip->insn_opcode |=
2056 (((address_expr->X_add_number & 0x7c0000) << 3)
2057 | ((address_expr->X_add_number & 0xf800000) >> 7)
2058 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2059 break;
2061 case BFD_RELOC_16_PCREL:
2062 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2063 break;
2065 case BFD_RELOC_16_PCREL_S2:
2066 goto need_reloc;
2068 default:
2069 internalError ();
2072 else
2074 need_reloc:
2075 /* Don't generate a reloc if we are writing into a variant frag. */
2076 if (place == NULL)
2078 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2079 address_expr,
2080 (*reloc_type == BFD_RELOC_16_PCREL
2081 || *reloc_type == BFD_RELOC_16_PCREL_S2),
2082 reloc_type[0]);
2084 /* These relocations can have an addend that won't fit in
2085 4 octets for 64bit assembly. */
2086 if (HAVE_64BIT_GPRS &&
2087 (*reloc_type == BFD_RELOC_16
2088 || *reloc_type == BFD_RELOC_32
2089 || *reloc_type == BFD_RELOC_MIPS_JMP
2090 || *reloc_type == BFD_RELOC_HI16_S
2091 || *reloc_type == BFD_RELOC_LO16
2092 || *reloc_type == BFD_RELOC_GPREL16
2093 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2094 || *reloc_type == BFD_RELOC_GPREL32
2095 || *reloc_type == BFD_RELOC_64
2096 || *reloc_type == BFD_RELOC_CTOR
2097 || *reloc_type == BFD_RELOC_MIPS_SUB
2098 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2099 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2100 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2101 || *reloc_type == BFD_RELOC_MIPS_REL16
2102 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2103 fixp[0]->fx_no_overflow = 1;
2105 if (unmatched_hi)
2107 struct mips_hi_fixup *hi_fixup;
2109 assert (*reloc_type == BFD_RELOC_HI16_S);
2110 hi_fixup = ((struct mips_hi_fixup *)
2111 xmalloc (sizeof (struct mips_hi_fixup)));
2112 hi_fixup->fixp = fixp[0];
2113 hi_fixup->seg = now_seg;
2114 hi_fixup->next = mips_hi_fixup_list;
2115 mips_hi_fixup_list = hi_fixup;
2118 if (reloc_type[1] != BFD_RELOC_UNUSED)
2120 /* FIXME: This symbol can be one of
2121 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
2122 address_expr->X_op = O_absent;
2123 address_expr->X_add_symbol = 0;
2124 address_expr->X_add_number = 0;
2126 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2127 4, address_expr, false,
2128 reloc_type[1]);
2130 /* These relocations can have an addend that won't fit in
2131 4 octets for 64bit assembly. */
2132 if (HAVE_64BIT_GPRS &&
2133 (*reloc_type == BFD_RELOC_16
2134 || *reloc_type == BFD_RELOC_32
2135 || *reloc_type == BFD_RELOC_MIPS_JMP
2136 || *reloc_type == BFD_RELOC_HI16_S
2137 || *reloc_type == BFD_RELOC_LO16
2138 || *reloc_type == BFD_RELOC_GPREL16
2139 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2140 || *reloc_type == BFD_RELOC_GPREL32
2141 || *reloc_type == BFD_RELOC_64
2142 || *reloc_type == BFD_RELOC_CTOR
2143 || *reloc_type == BFD_RELOC_MIPS_SUB
2144 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2145 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2146 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2147 || *reloc_type == BFD_RELOC_MIPS_REL16
2148 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2149 fixp[1]->fx_no_overflow = 1;
2151 if (reloc_type[2] != BFD_RELOC_UNUSED)
2153 address_expr->X_op = O_absent;
2154 address_expr->X_add_symbol = 0;
2155 address_expr->X_add_number = 0;
2157 fixp[2] = fix_new_exp (frag_now,
2158 f - frag_now->fr_literal, 4,
2159 address_expr, false,
2160 reloc_type[2]);
2162 /* These relocations can have an addend that won't fit in
2163 4 octets for 64bit assembly. */
2164 if (HAVE_64BIT_GPRS &&
2165 (*reloc_type == BFD_RELOC_16
2166 || *reloc_type == BFD_RELOC_32
2167 || *reloc_type == BFD_RELOC_MIPS_JMP
2168 || *reloc_type == BFD_RELOC_HI16_S
2169 || *reloc_type == BFD_RELOC_LO16
2170 || *reloc_type == BFD_RELOC_GPREL16
2171 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2172 || *reloc_type == BFD_RELOC_GPREL32
2173 || *reloc_type == BFD_RELOC_64
2174 || *reloc_type == BFD_RELOC_CTOR
2175 || *reloc_type == BFD_RELOC_MIPS_SUB
2176 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2177 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2178 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2179 || *reloc_type == BFD_RELOC_MIPS_REL16
2180 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2181 fixp[2]->fx_no_overflow = 1;
2188 if (! mips_opts.mips16)
2190 md_number_to_chars (f, ip->insn_opcode, 4);
2191 #ifdef OBJ_ELF
2192 dwarf2_emit_insn (4);
2193 #endif
2195 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2197 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2198 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2199 #ifdef OBJ_ELF
2200 dwarf2_emit_insn (4);
2201 #endif
2203 else
2205 if (ip->use_extend)
2207 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2208 f += 2;
2210 md_number_to_chars (f, ip->insn_opcode, 2);
2211 #ifdef OBJ_ELF
2212 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2213 #endif
2216 /* Update the register mask information. */
2217 if (! mips_opts.mips16)
2219 if (pinfo & INSN_WRITE_GPR_D)
2220 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2221 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2222 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2223 if (pinfo & INSN_READ_GPR_S)
2224 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2225 if (pinfo & INSN_WRITE_GPR_31)
2226 mips_gprmask |= 1 << RA;
2227 if (pinfo & INSN_WRITE_FPR_D)
2228 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2229 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2230 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2231 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2232 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2233 if ((pinfo & INSN_READ_FPR_R) != 0)
2234 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2235 if (pinfo & INSN_COP)
2237 /* We don't keep enough information to sort these cases out.
2238 The itbl support does keep this information however, although
2239 we currently don't support itbl fprmats as part of the cop
2240 instruction. May want to add this support in the future. */
2242 /* Never set the bit for $0, which is always zero. */
2243 mips_gprmask &= ~1 << 0;
2245 else
2247 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2248 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2249 & MIPS16OP_MASK_RX);
2250 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2251 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2252 & MIPS16OP_MASK_RY);
2253 if (pinfo & MIPS16_INSN_WRITE_Z)
2254 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2255 & MIPS16OP_MASK_RZ);
2256 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2257 mips_gprmask |= 1 << TREG;
2258 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2259 mips_gprmask |= 1 << SP;
2260 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2261 mips_gprmask |= 1 << RA;
2262 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2263 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2264 if (pinfo & MIPS16_INSN_READ_Z)
2265 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2266 & MIPS16OP_MASK_MOVE32Z);
2267 if (pinfo & MIPS16_INSN_READ_GPR_X)
2268 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2269 & MIPS16OP_MASK_REGR32);
2272 if (place == NULL && ! mips_opts.noreorder)
2274 /* Filling the branch delay slot is more complex. We try to
2275 switch the branch with the previous instruction, which we can
2276 do if the previous instruction does not set up a condition
2277 that the branch tests and if the branch is not itself the
2278 target of any branch. */
2279 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2280 || (pinfo & INSN_COND_BRANCH_DELAY))
2282 if (mips_optimize < 2
2283 /* If we have seen .set volatile or .set nomove, don't
2284 optimize. */
2285 || mips_opts.nomove != 0
2286 /* If we had to emit any NOP instructions, then we
2287 already know we can not swap. */
2288 || nops != 0
2289 /* If we don't even know the previous insn, we can not
2290 swap. */
2291 || ! prev_insn_valid
2292 /* If the previous insn is already in a branch delay
2293 slot, then we can not swap. */
2294 || prev_insn_is_delay_slot
2295 /* If the previous previous insn was in a .set
2296 noreorder, we can't swap. Actually, the MIPS
2297 assembler will swap in this situation. However, gcc
2298 configured -with-gnu-as will generate code like
2299 .set noreorder
2300 lw $4,XXX
2301 .set reorder
2302 INSN
2303 bne $4,$0,foo
2304 in which we can not swap the bne and INSN. If gcc is
2305 not configured -with-gnu-as, it does not output the
2306 .set pseudo-ops. We don't have to check
2307 prev_insn_unreordered, because prev_insn_valid will
2308 be 0 in that case. We don't want to use
2309 prev_prev_insn_valid, because we do want to be able
2310 to swap at the start of a function. */
2311 || prev_prev_insn_unreordered
2312 /* If the branch is itself the target of a branch, we
2313 can not swap. We cheat on this; all we check for is
2314 whether there is a label on this instruction. If
2315 there are any branches to anything other than a
2316 label, users must use .set noreorder. */
2317 || insn_labels != NULL
2318 /* If the previous instruction is in a variant frag, we
2319 can not do the swap. This does not apply to the
2320 mips16, which uses variant frags for different
2321 purposes. */
2322 || (! mips_opts.mips16
2323 && prev_insn_frag->fr_type == rs_machine_dependent)
2324 /* If the branch reads the condition codes, we don't
2325 even try to swap, because in the sequence
2326 ctc1 $X,$31
2327 INSN
2328 INSN
2329 bc1t LABEL
2330 we can not swap, and I don't feel like handling that
2331 case. */
2332 || (! mips_opts.mips16
2333 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2334 && (pinfo & INSN_READ_COND_CODE))
2335 /* We can not swap with an instruction that requires a
2336 delay slot, becase the target of the branch might
2337 interfere with that instruction. */
2338 || (! mips_opts.mips16
2339 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2340 && (prev_pinfo
2341 /* Itbl support may require additional care here. */
2342 & (INSN_LOAD_COPROC_DELAY
2343 | INSN_COPROC_MOVE_DELAY
2344 | INSN_WRITE_COND_CODE)))
2345 || (! (hilo_interlocks
2346 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2347 && (prev_pinfo
2348 & (INSN_READ_LO
2349 | INSN_READ_HI)))
2350 || (! mips_opts.mips16
2351 && ! gpr_interlocks
2352 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2353 || (! mips_opts.mips16
2354 && mips_opts.isa == ISA_MIPS1
2355 /* Itbl support may require additional care here. */
2356 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2357 /* We can not swap with a branch instruction. */
2358 || (prev_pinfo
2359 & (INSN_UNCOND_BRANCH_DELAY
2360 | INSN_COND_BRANCH_DELAY
2361 | INSN_COND_BRANCH_LIKELY))
2362 /* We do not swap with a trap instruction, since it
2363 complicates trap handlers to have the trap
2364 instruction be in a delay slot. */
2365 || (prev_pinfo & INSN_TRAP)
2366 /* If the branch reads a register that the previous
2367 instruction sets, we can not swap. */
2368 || (! mips_opts.mips16
2369 && (prev_pinfo & INSN_WRITE_GPR_T)
2370 && insn_uses_reg (ip,
2371 ((prev_insn.insn_opcode >> OP_SH_RT)
2372 & OP_MASK_RT),
2373 MIPS_GR_REG))
2374 || (! mips_opts.mips16
2375 && (prev_pinfo & INSN_WRITE_GPR_D)
2376 && insn_uses_reg (ip,
2377 ((prev_insn.insn_opcode >> OP_SH_RD)
2378 & OP_MASK_RD),
2379 MIPS_GR_REG))
2380 || (mips_opts.mips16
2381 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2382 && insn_uses_reg (ip,
2383 ((prev_insn.insn_opcode
2384 >> MIPS16OP_SH_RX)
2385 & MIPS16OP_MASK_RX),
2386 MIPS16_REG))
2387 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2388 && insn_uses_reg (ip,
2389 ((prev_insn.insn_opcode
2390 >> MIPS16OP_SH_RY)
2391 & MIPS16OP_MASK_RY),
2392 MIPS16_REG))
2393 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2394 && insn_uses_reg (ip,
2395 ((prev_insn.insn_opcode
2396 >> MIPS16OP_SH_RZ)
2397 & MIPS16OP_MASK_RZ),
2398 MIPS16_REG))
2399 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2400 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2401 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2402 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2403 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2404 && insn_uses_reg (ip,
2405 MIPS16OP_EXTRACT_REG32R (prev_insn.
2406 insn_opcode),
2407 MIPS_GR_REG))))
2408 /* If the branch writes a register that the previous
2409 instruction sets, we can not swap (we know that
2410 branches write only to RD or to $31). */
2411 || (! mips_opts.mips16
2412 && (prev_pinfo & INSN_WRITE_GPR_T)
2413 && (((pinfo & INSN_WRITE_GPR_D)
2414 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2415 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2416 || ((pinfo & INSN_WRITE_GPR_31)
2417 && (((prev_insn.insn_opcode >> OP_SH_RT)
2418 & OP_MASK_RT)
2419 == RA))))
2420 || (! mips_opts.mips16
2421 && (prev_pinfo & INSN_WRITE_GPR_D)
2422 && (((pinfo & INSN_WRITE_GPR_D)
2423 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2424 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2425 || ((pinfo & INSN_WRITE_GPR_31)
2426 && (((prev_insn.insn_opcode >> OP_SH_RD)
2427 & OP_MASK_RD)
2428 == RA))))
2429 || (mips_opts.mips16
2430 && (pinfo & MIPS16_INSN_WRITE_31)
2431 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2432 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2433 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2434 == RA))))
2435 /* If the branch writes a register that the previous
2436 instruction reads, we can not swap (we know that
2437 branches only write to RD or to $31). */
2438 || (! mips_opts.mips16
2439 && (pinfo & INSN_WRITE_GPR_D)
2440 && insn_uses_reg (&prev_insn,
2441 ((ip->insn_opcode >> OP_SH_RD)
2442 & OP_MASK_RD),
2443 MIPS_GR_REG))
2444 || (! mips_opts.mips16
2445 && (pinfo & INSN_WRITE_GPR_31)
2446 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2447 || (mips_opts.mips16
2448 && (pinfo & MIPS16_INSN_WRITE_31)
2449 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2450 /* If we are generating embedded PIC code, the branch
2451 might be expanded into a sequence which uses $at, so
2452 we can't swap with an instruction which reads it. */
2453 || (mips_pic == EMBEDDED_PIC
2454 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2455 /* If the previous previous instruction has a load
2456 delay, and sets a register that the branch reads, we
2457 can not swap. */
2458 || (! mips_opts.mips16
2459 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2460 /* Itbl support may require additional care here. */
2461 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2462 || (! gpr_interlocks
2463 && (prev_prev_insn.insn_mo->pinfo
2464 & INSN_LOAD_MEMORY_DELAY)))
2465 && insn_uses_reg (ip,
2466 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2467 & OP_MASK_RT),
2468 MIPS_GR_REG))
2469 /* If one instruction sets a condition code and the
2470 other one uses a condition code, we can not swap. */
2471 || ((pinfo & INSN_READ_COND_CODE)
2472 && (prev_pinfo & INSN_WRITE_COND_CODE))
2473 || ((pinfo & INSN_WRITE_COND_CODE)
2474 && (prev_pinfo & INSN_READ_COND_CODE))
2475 /* If the previous instruction uses the PC, we can not
2476 swap. */
2477 || (mips_opts.mips16
2478 && (prev_pinfo & MIPS16_INSN_READ_PC))
2479 /* If the previous instruction was extended, we can not
2480 swap. */
2481 || (mips_opts.mips16 && prev_insn_extended)
2482 /* If the previous instruction had a fixup in mips16
2483 mode, we can not swap. This normally means that the
2484 previous instruction was a 4 byte branch anyhow. */
2485 || (mips_opts.mips16 && prev_insn_fixp[0])
2486 /* If the previous instruction is a sync, sync.l, or
2487 sync.p, we can not swap. */
2488 || (prev_pinfo & INSN_SYNC))
2490 /* We could do even better for unconditional branches to
2491 portions of this object file; we could pick up the
2492 instruction at the destination, put it in the delay
2493 slot, and bump the destination address. */
2494 emit_nop ();
2495 /* Update the previous insn information. */
2496 prev_prev_insn = *ip;
2497 prev_insn.insn_mo = &dummy_opcode;
2499 else
2501 /* It looks like we can actually do the swap. */
2502 if (! mips_opts.mips16)
2504 char *prev_f;
2505 char temp[4];
2507 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2508 memcpy (temp, prev_f, 4);
2509 memcpy (prev_f, f, 4);
2510 memcpy (f, temp, 4);
2511 if (prev_insn_fixp[0])
2513 prev_insn_fixp[0]->fx_frag = frag_now;
2514 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2516 if (prev_insn_fixp[1])
2518 prev_insn_fixp[1]->fx_frag = frag_now;
2519 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2521 if (prev_insn_fixp[2])
2523 prev_insn_fixp[2]->fx_frag = frag_now;
2524 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2526 if (fixp[0])
2528 fixp[0]->fx_frag = prev_insn_frag;
2529 fixp[0]->fx_where = prev_insn_where;
2531 if (fixp[1])
2533 fixp[1]->fx_frag = prev_insn_frag;
2534 fixp[1]->fx_where = prev_insn_where;
2536 if (fixp[2])
2538 fixp[2]->fx_frag = prev_insn_frag;
2539 fixp[2]->fx_where = prev_insn_where;
2542 else
2544 char *prev_f;
2545 char temp[2];
2547 assert (prev_insn_fixp[0] == NULL);
2548 assert (prev_insn_fixp[1] == NULL);
2549 assert (prev_insn_fixp[2] == NULL);
2550 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2551 memcpy (temp, prev_f, 2);
2552 memcpy (prev_f, f, 2);
2553 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2555 assert (*reloc_type == BFD_RELOC_UNUSED);
2556 memcpy (f, temp, 2);
2558 else
2560 memcpy (f, f + 2, 2);
2561 memcpy (f + 2, temp, 2);
2563 if (fixp[0])
2565 fixp[0]->fx_frag = prev_insn_frag;
2566 fixp[0]->fx_where = prev_insn_where;
2568 if (fixp[1])
2570 fixp[1]->fx_frag = prev_insn_frag;
2571 fixp[1]->fx_where = prev_insn_where;
2573 if (fixp[2])
2575 fixp[2]->fx_frag = prev_insn_frag;
2576 fixp[2]->fx_where = prev_insn_where;
2580 /* Update the previous insn information; leave prev_insn
2581 unchanged. */
2582 prev_prev_insn = *ip;
2584 prev_insn_is_delay_slot = 1;
2586 /* If that was an unconditional branch, forget the previous
2587 insn information. */
2588 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2590 prev_prev_insn.insn_mo = &dummy_opcode;
2591 prev_insn.insn_mo = &dummy_opcode;
2594 prev_insn_fixp[0] = NULL;
2595 prev_insn_fixp[1] = NULL;
2596 prev_insn_fixp[2] = NULL;
2597 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2598 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2599 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2600 prev_insn_extended = 0;
2602 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2604 /* We don't yet optimize a branch likely. What we should do
2605 is look at the target, copy the instruction found there
2606 into the delay slot, and increment the branch to jump to
2607 the next instruction. */
2608 emit_nop ();
2609 /* Update the previous insn information. */
2610 prev_prev_insn = *ip;
2611 prev_insn.insn_mo = &dummy_opcode;
2612 prev_insn_fixp[0] = NULL;
2613 prev_insn_fixp[1] = NULL;
2614 prev_insn_fixp[2] = NULL;
2615 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2616 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2617 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2618 prev_insn_extended = 0;
2620 else
2622 /* Update the previous insn information. */
2623 if (nops > 0)
2624 prev_prev_insn.insn_mo = &dummy_opcode;
2625 else
2626 prev_prev_insn = prev_insn;
2627 prev_insn = *ip;
2629 /* Any time we see a branch, we always fill the delay slot
2630 immediately; since this insn is not a branch, we know it
2631 is not in a delay slot. */
2632 prev_insn_is_delay_slot = 0;
2634 prev_insn_fixp[0] = fixp[0];
2635 prev_insn_fixp[1] = fixp[1];
2636 prev_insn_fixp[2] = fixp[2];
2637 prev_insn_reloc_type[0] = reloc_type[0];
2638 prev_insn_reloc_type[1] = reloc_type[1];
2639 prev_insn_reloc_type[2] = reloc_type[2];
2640 if (mips_opts.mips16)
2641 prev_insn_extended = (ip->use_extend
2642 || *reloc_type > BFD_RELOC_UNUSED);
2645 prev_prev_insn_unreordered = prev_insn_unreordered;
2646 prev_insn_unreordered = 0;
2647 prev_insn_frag = frag_now;
2648 prev_insn_where = f - frag_now->fr_literal;
2649 prev_insn_valid = 1;
2651 else if (place == NULL)
2653 /* We need to record a bit of information even when we are not
2654 reordering, in order to determine the base address for mips16
2655 PC relative relocs. */
2656 prev_prev_insn = prev_insn;
2657 prev_insn = *ip;
2658 prev_insn_reloc_type[0] = reloc_type[0];
2659 prev_insn_reloc_type[1] = reloc_type[1];
2660 prev_insn_reloc_type[2] = reloc_type[2];
2661 prev_prev_insn_unreordered = prev_insn_unreordered;
2662 prev_insn_unreordered = 1;
2665 /* We just output an insn, so the next one doesn't have a label. */
2666 mips_clear_insn_labels ();
2668 /* We must ensure that a fixup associated with an unmatched %hi
2669 reloc does not become a variant frag. Otherwise, the
2670 rearrangement of %hi relocs in frob_file may confuse
2671 tc_gen_reloc. */
2672 if (unmatched_hi)
2674 frag_wane (frag_now);
2675 frag_new (0);
2679 /* This function forgets that there was any previous instruction or
2680 label. If PRESERVE is non-zero, it remembers enough information to
2681 know whether nops are needed before a noreorder section. */
2683 static void
2684 mips_no_prev_insn (preserve)
2685 int preserve;
2687 if (! preserve)
2689 prev_insn.insn_mo = &dummy_opcode;
2690 prev_prev_insn.insn_mo = &dummy_opcode;
2691 prev_nop_frag = NULL;
2692 prev_nop_frag_holds = 0;
2693 prev_nop_frag_required = 0;
2694 prev_nop_frag_since = 0;
2696 prev_insn_valid = 0;
2697 prev_insn_is_delay_slot = 0;
2698 prev_insn_unreordered = 0;
2699 prev_insn_extended = 0;
2700 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2701 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2702 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2703 prev_prev_insn_unreordered = 0;
2704 mips_clear_insn_labels ();
2707 /* This function must be called whenever we turn on noreorder or emit
2708 something other than instructions. It inserts any NOPS which might
2709 be needed by the previous instruction, and clears the information
2710 kept for the previous instructions. The INSNS parameter is true if
2711 instructions are to follow. */
2713 static void
2714 mips_emit_delays (insns)
2715 boolean insns;
2717 if (! mips_opts.noreorder)
2719 int nops;
2721 nops = 0;
2722 if ((! mips_opts.mips16
2723 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2724 && (! cop_interlocks
2725 && (prev_insn.insn_mo->pinfo
2726 & (INSN_LOAD_COPROC_DELAY
2727 | INSN_COPROC_MOVE_DELAY
2728 | INSN_WRITE_COND_CODE))))
2729 || (! hilo_interlocks
2730 && (prev_insn.insn_mo->pinfo
2731 & (INSN_READ_LO
2732 | INSN_READ_HI)))
2733 || (! mips_opts.mips16
2734 && ! gpr_interlocks
2735 && (prev_insn.insn_mo->pinfo
2736 & INSN_LOAD_MEMORY_DELAY))
2737 || (! mips_opts.mips16
2738 && mips_opts.isa == ISA_MIPS1
2739 && (prev_insn.insn_mo->pinfo
2740 & INSN_COPROC_MEMORY_DELAY)))
2742 /* Itbl support may require additional care here. */
2743 ++nops;
2744 if ((! mips_opts.mips16
2745 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2746 && (! cop_interlocks
2747 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2748 || (! hilo_interlocks
2749 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2750 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2751 ++nops;
2753 if (prev_insn_unreordered)
2754 nops = 0;
2756 else if ((! mips_opts.mips16
2757 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2758 && (! cop_interlocks
2759 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2760 || (! hilo_interlocks
2761 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2762 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2764 /* Itbl support may require additional care here. */
2765 if (! prev_prev_insn_unreordered)
2766 ++nops;
2769 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2771 int min_nops = 0;
2772 const char *pn = prev_insn.insn_mo->name;
2773 if (strncmp(pn, "macc", 4) == 0
2774 || strncmp(pn, "dmacc", 5) == 0
2775 || strncmp(pn, "dmult", 5) == 0)
2777 min_nops = 1;
2779 if (nops < min_nops)
2780 nops = min_nops;
2783 if (nops > 0)
2785 struct insn_label_list *l;
2787 if (insns)
2789 /* Record the frag which holds the nop instructions, so
2790 that we can remove them if we don't need them. */
2791 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2792 prev_nop_frag = frag_now;
2793 prev_nop_frag_holds = nops;
2794 prev_nop_frag_required = 0;
2795 prev_nop_frag_since = 0;
2798 for (; nops > 0; --nops)
2799 emit_nop ();
2801 if (insns)
2803 /* Move on to a new frag, so that it is safe to simply
2804 decrease the size of prev_nop_frag. */
2805 frag_wane (frag_now);
2806 frag_new (0);
2809 for (l = insn_labels; l != NULL; l = l->next)
2811 valueT val;
2813 assert (S_GET_SEGMENT (l->label) == now_seg);
2814 symbol_set_frag (l->label, frag_now);
2815 val = (valueT) frag_now_fix ();
2816 /* mips16 text labels are stored as odd. */
2817 if (mips_opts.mips16)
2818 ++val;
2819 S_SET_VALUE (l->label, val);
2824 /* Mark instruction labels in mips16 mode. */
2825 if (insns)
2826 mips16_mark_labels ();
2828 mips_no_prev_insn (insns);
2831 /* Build an instruction created by a macro expansion. This is passed
2832 a pointer to the count of instructions created so far, an
2833 expression, the name of the instruction to build, an operand format
2834 string, and corresponding arguments. */
2836 #ifdef USE_STDARG
2837 static void
2838 macro_build (char *place,
2839 int *counter,
2840 expressionS * ep,
2841 const char *name,
2842 const char *fmt,
2843 ...)
2844 #else
2845 static void
2846 macro_build (place, counter, ep, name, fmt, va_alist)
2847 char *place;
2848 int *counter;
2849 expressionS *ep;
2850 const char *name;
2851 const char *fmt;
2852 va_dcl
2853 #endif
2855 struct mips_cl_insn insn;
2856 bfd_reloc_code_real_type r[3];
2857 va_list args;
2859 #ifdef USE_STDARG
2860 va_start (args, fmt);
2861 #else
2862 va_start (args);
2863 #endif
2866 * If the macro is about to expand into a second instruction,
2867 * print a warning if needed. We need to pass ip as a parameter
2868 * to generate a better warning message here...
2870 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2871 as_warn (_("Macro instruction expanded into multiple instructions"));
2874 * If the macro is about to expand into a second instruction,
2875 * and it is in a delay slot, print a warning.
2877 if (place == NULL
2878 && *counter == 1
2879 && mips_opts.noreorder
2880 && (prev_prev_insn.insn_mo->pinfo
2881 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2882 | INSN_COND_BRANCH_LIKELY)) != 0)
2883 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2885 if (place == NULL)
2886 ++*counter; /* bump instruction counter */
2888 if (mips_opts.mips16)
2890 mips16_macro_build (place, counter, ep, name, fmt, args);
2891 va_end (args);
2892 return;
2895 r[0] = BFD_RELOC_UNUSED;
2896 r[1] = BFD_RELOC_UNUSED;
2897 r[2] = BFD_RELOC_UNUSED;
2898 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2899 assert (insn.insn_mo);
2900 assert (strcmp (name, insn.insn_mo->name) == 0);
2902 /* Search until we get a match for NAME. */
2903 while (1)
2905 /* It is assumed here that macros will never generate
2906 MDMX or MIPS-3D instructions. */
2907 if (strcmp (fmt, insn.insn_mo->args) == 0
2908 && insn.insn_mo->pinfo != INSN_MACRO
2909 && OPCODE_IS_MEMBER (insn.insn_mo,
2910 (mips_opts.isa
2911 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2912 mips_arch)
2913 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2914 break;
2916 ++insn.insn_mo;
2917 assert (insn.insn_mo->name);
2918 assert (strcmp (name, insn.insn_mo->name) == 0);
2921 insn.insn_opcode = insn.insn_mo->match;
2922 for (;;)
2924 switch (*fmt++)
2926 case '\0':
2927 break;
2929 case ',':
2930 case '(':
2931 case ')':
2932 continue;
2934 case 't':
2935 case 'w':
2936 case 'E':
2937 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2938 continue;
2940 case 'c':
2941 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2942 continue;
2944 case 'T':
2945 case 'W':
2946 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2947 continue;
2949 case 'd':
2950 case 'G':
2951 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2952 continue;
2954 case 'U':
2956 int tmp = va_arg (args, int);
2958 insn.insn_opcode |= tmp << OP_SH_RT;
2959 insn.insn_opcode |= tmp << OP_SH_RD;
2960 continue;
2963 case 'V':
2964 case 'S':
2965 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2966 continue;
2968 case 'z':
2969 continue;
2971 case '<':
2972 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2973 continue;
2975 case 'D':
2976 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2977 continue;
2979 case 'B':
2980 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2981 continue;
2983 case 'J':
2984 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2985 continue;
2987 case 'q':
2988 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2989 continue;
2991 case 'b':
2992 case 's':
2993 case 'r':
2994 case 'v':
2995 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2996 continue;
2998 case 'i':
2999 case 'j':
3000 case 'o':
3001 *r = (bfd_reloc_code_real_type) va_arg (args, int);
3002 assert (*r == BFD_RELOC_GPREL16
3003 || *r == BFD_RELOC_MIPS_LITERAL
3004 || *r == BFD_RELOC_MIPS_HIGHER
3005 || *r == BFD_RELOC_HI16_S
3006 || *r == BFD_RELOC_LO16
3007 || *r == BFD_RELOC_MIPS_GOT16
3008 || *r == BFD_RELOC_MIPS_CALL16
3009 || *r == BFD_RELOC_MIPS_GOT_DISP
3010 || *r == BFD_RELOC_MIPS_GOT_PAGE
3011 || *r == BFD_RELOC_MIPS_GOT_OFST
3012 || *r == BFD_RELOC_MIPS_GOT_LO16
3013 || *r == BFD_RELOC_MIPS_CALL_LO16
3014 || (ep->X_op == O_subtract
3015 && *r == BFD_RELOC_PCREL_LO16));
3016 continue;
3018 case 'u':
3019 *r = (bfd_reloc_code_real_type) va_arg (args, int);
3020 assert (ep != NULL
3021 && (ep->X_op == O_constant
3022 || (ep->X_op == O_symbol
3023 && (*r == BFD_RELOC_MIPS_HIGHEST
3024 || *r == BFD_RELOC_HI16_S
3025 || *r == BFD_RELOC_HI16
3026 || *r == BFD_RELOC_GPREL16
3027 || *r == BFD_RELOC_MIPS_GOT_HI16
3028 || *r == BFD_RELOC_MIPS_CALL_HI16))
3029 || (ep->X_op == O_subtract
3030 && *r == BFD_RELOC_PCREL_HI16_S)));
3031 continue;
3033 case 'p':
3034 assert (ep != NULL);
3036 * This allows macro() to pass an immediate expression for
3037 * creating short branches without creating a symbol.
3038 * Note that the expression still might come from the assembly
3039 * input, in which case the value is not checked for range nor
3040 * is a relocation entry generated (yuck).
3042 if (ep->X_op == O_constant)
3044 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3045 ep = NULL;
3047 else
3048 if (mips_pic == EMBEDDED_PIC)
3049 *r = BFD_RELOC_16_PCREL_S2;
3050 else
3051 *r = BFD_RELOC_16_PCREL;
3052 continue;
3054 case 'a':
3055 assert (ep != NULL);
3056 *r = BFD_RELOC_MIPS_JMP;
3057 continue;
3059 case 'C':
3060 insn.insn_opcode |= va_arg (args, unsigned long);
3061 continue;
3063 default:
3064 internalError ();
3066 break;
3068 va_end (args);
3069 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3071 append_insn (place, &insn, ep, r, false);
3074 static void
3075 mips16_macro_build (place, counter, ep, name, fmt, args)
3076 char *place;
3077 int *counter ATTRIBUTE_UNUSED;
3078 expressionS *ep;
3079 const char *name;
3080 const char *fmt;
3081 va_list args;
3083 struct mips_cl_insn insn;
3084 bfd_reloc_code_real_type r[3]
3085 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3087 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3088 assert (insn.insn_mo);
3089 assert (strcmp (name, insn.insn_mo->name) == 0);
3091 while (strcmp (fmt, insn.insn_mo->args) != 0
3092 || insn.insn_mo->pinfo == INSN_MACRO)
3094 ++insn.insn_mo;
3095 assert (insn.insn_mo->name);
3096 assert (strcmp (name, insn.insn_mo->name) == 0);
3099 insn.insn_opcode = insn.insn_mo->match;
3100 insn.use_extend = false;
3102 for (;;)
3104 int c;
3106 c = *fmt++;
3107 switch (c)
3109 case '\0':
3110 break;
3112 case ',':
3113 case '(':
3114 case ')':
3115 continue;
3117 case 'y':
3118 case 'w':
3119 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3120 continue;
3122 case 'x':
3123 case 'v':
3124 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3125 continue;
3127 case 'z':
3128 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3129 continue;
3131 case 'Z':
3132 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3133 continue;
3135 case '0':
3136 case 'S':
3137 case 'P':
3138 case 'R':
3139 continue;
3141 case 'X':
3142 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3143 continue;
3145 case 'Y':
3147 int regno;
3149 regno = va_arg (args, int);
3150 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3151 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3153 continue;
3155 case '<':
3156 case '>':
3157 case '4':
3158 case '5':
3159 case 'H':
3160 case 'W':
3161 case 'D':
3162 case 'j':
3163 case '8':
3164 case 'V':
3165 case 'C':
3166 case 'U':
3167 case 'k':
3168 case 'K':
3169 case 'p':
3170 case 'q':
3172 assert (ep != NULL);
3174 if (ep->X_op != O_constant)
3175 *r = (int) BFD_RELOC_UNUSED + c;
3176 else
3178 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
3179 false, &insn.insn_opcode, &insn.use_extend,
3180 &insn.extend);
3181 ep = NULL;
3182 *r = BFD_RELOC_UNUSED;
3185 continue;
3187 case '6':
3188 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3189 continue;
3192 break;
3195 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3197 append_insn (place, &insn, ep, r, false);
3201 * Generate a "jalr" instruction with a relocation hint to the called
3202 * function. This occurs in NewABI PIC code.
3204 static void
3205 macro_build_jalr (icnt, ep)
3206 int icnt;
3207 expressionS *ep;
3209 char *f;
3211 if (HAVE_NEWABI)
3213 frag_grow (4);
3214 f = frag_more (0);
3216 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3217 RA, PIC_CALL_REG);
3218 if (HAVE_NEWABI)
3219 fix_new_exp (frag_now, f - frag_now->fr_literal,
3220 0, ep, false, BFD_RELOC_MIPS_JALR);
3224 * Generate a "lui" instruction.
3226 static void
3227 macro_build_lui (place, counter, ep, regnum)
3228 char *place;
3229 int *counter;
3230 expressionS *ep;
3231 int regnum;
3233 expressionS high_expr;
3234 struct mips_cl_insn insn;
3235 bfd_reloc_code_real_type r[3]
3236 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3237 const char *name = "lui";
3238 const char *fmt = "t,u";
3240 assert (! mips_opts.mips16);
3242 if (place == NULL)
3243 high_expr = *ep;
3244 else
3246 high_expr.X_op = O_constant;
3247 high_expr.X_add_number = ep->X_add_number;
3250 if (high_expr.X_op == O_constant)
3252 /* we can compute the instruction now without a relocation entry */
3253 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3254 >> 16) & 0xffff;
3255 *r = BFD_RELOC_UNUSED;
3257 else if (! HAVE_NEWABI)
3259 assert (ep->X_op == O_symbol);
3260 /* _gp_disp is a special case, used from s_cpload. */
3261 assert (mips_pic == NO_PIC
3262 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3263 *r = BFD_RELOC_HI16_S;
3267 * If the macro is about to expand into a second instruction,
3268 * print a warning if needed. We need to pass ip as a parameter
3269 * to generate a better warning message here...
3271 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3272 as_warn (_("Macro instruction expanded into multiple instructions"));
3274 if (place == NULL)
3275 ++*counter; /* bump instruction counter */
3277 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3278 assert (insn.insn_mo);
3279 assert (strcmp (name, insn.insn_mo->name) == 0);
3280 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3282 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3283 if (*r == BFD_RELOC_UNUSED)
3285 insn.insn_opcode |= high_expr.X_add_number;
3286 append_insn (place, &insn, NULL, r, false);
3288 else
3289 append_insn (place, &insn, &high_expr, r, false);
3292 /* Generate a sequence of instructions to do a load or store from a constant
3293 offset off of a base register (breg) into/from a target register (treg),
3294 using AT if necessary. */
3295 static void
3296 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3297 char *place;
3298 int *counter;
3299 expressionS *ep;
3300 const char *op;
3301 int treg, breg;
3303 assert (ep->X_op == O_constant);
3305 /* Right now, this routine can only handle signed 32-bit contants. */
3306 if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3307 as_warn (_("operand overflow"));
3309 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3311 /* Signed 16-bit offset will fit in the op. Easy! */
3312 macro_build (place, counter, ep, op, "t,o(b)", treg,
3313 (int) BFD_RELOC_LO16, breg);
3315 else
3317 /* 32-bit offset, need multiple instructions and AT, like:
3318 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3319 addu $tempreg,$tempreg,$breg
3320 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3321 to handle the complete offset. */
3322 macro_build_lui (place, counter, ep, AT);
3323 if (place != NULL)
3324 place += 4;
3325 macro_build (place, counter, (expressionS *) NULL,
3326 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3327 "d,v,t", AT, AT, breg);
3328 if (place != NULL)
3329 place += 4;
3330 macro_build (place, counter, ep, op, "t,o(b)", treg,
3331 (int) BFD_RELOC_LO16, AT);
3333 if (mips_opts.noat)
3334 as_warn (_("Macro used $at after \".set noat\""));
3338 /* set_at()
3339 * Generates code to set the $at register to true (one)
3340 * if reg is less than the immediate expression.
3342 static void
3343 set_at (counter, reg, unsignedp)
3344 int *counter;
3345 int reg;
3346 int unsignedp;
3348 if (imm_expr.X_op == O_constant
3349 && imm_expr.X_add_number >= -0x8000
3350 && imm_expr.X_add_number < 0x8000)
3351 macro_build ((char *) NULL, counter, &imm_expr,
3352 unsignedp ? "sltiu" : "slti",
3353 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3354 else
3356 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3357 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3358 unsignedp ? "sltu" : "slt",
3359 "d,v,t", AT, reg, AT);
3363 /* Warn if an expression is not a constant. */
3365 static void
3366 check_absolute_expr (ip, ex)
3367 struct mips_cl_insn *ip;
3368 expressionS *ex;
3370 if (ex->X_op == O_big)
3371 as_bad (_("unsupported large constant"));
3372 else if (ex->X_op != O_constant)
3373 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3376 /* Count the leading zeroes by performing a binary chop. This is a
3377 bulky bit of source, but performance is a LOT better for the
3378 majority of values than a simple loop to count the bits:
3379 for (lcnt = 0; (lcnt < 32); lcnt++)
3380 if ((v) & (1 << (31 - lcnt)))
3381 break;
3382 However it is not code size friendly, and the gain will drop a bit
3383 on certain cached systems.
3385 #define COUNT_TOP_ZEROES(v) \
3386 (((v) & ~0xffff) == 0 \
3387 ? ((v) & ~0xff) == 0 \
3388 ? ((v) & ~0xf) == 0 \
3389 ? ((v) & ~0x3) == 0 \
3390 ? ((v) & ~0x1) == 0 \
3391 ? !(v) \
3392 ? 32 \
3393 : 31 \
3394 : 30 \
3395 : ((v) & ~0x7) == 0 \
3396 ? 29 \
3397 : 28 \
3398 : ((v) & ~0x3f) == 0 \
3399 ? ((v) & ~0x1f) == 0 \
3400 ? 27 \
3401 : 26 \
3402 : ((v) & ~0x7f) == 0 \
3403 ? 25 \
3404 : 24 \
3405 : ((v) & ~0xfff) == 0 \
3406 ? ((v) & ~0x3ff) == 0 \
3407 ? ((v) & ~0x1ff) == 0 \
3408 ? 23 \
3409 : 22 \
3410 : ((v) & ~0x7ff) == 0 \
3411 ? 21 \
3412 : 20 \
3413 : ((v) & ~0x3fff) == 0 \
3414 ? ((v) & ~0x1fff) == 0 \
3415 ? 19 \
3416 : 18 \
3417 : ((v) & ~0x7fff) == 0 \
3418 ? 17 \
3419 : 16 \
3420 : ((v) & ~0xffffff) == 0 \
3421 ? ((v) & ~0xfffff) == 0 \
3422 ? ((v) & ~0x3ffff) == 0 \
3423 ? ((v) & ~0x1ffff) == 0 \
3424 ? 15 \
3425 : 14 \
3426 : ((v) & ~0x7ffff) == 0 \
3427 ? 13 \
3428 : 12 \
3429 : ((v) & ~0x3fffff) == 0 \
3430 ? ((v) & ~0x1fffff) == 0 \
3431 ? 11 \
3432 : 10 \
3433 : ((v) & ~0x7fffff) == 0 \
3434 ? 9 \
3435 : 8 \
3436 : ((v) & ~0xfffffff) == 0 \
3437 ? ((v) & ~0x3ffffff) == 0 \
3438 ? ((v) & ~0x1ffffff) == 0 \
3439 ? 7 \
3440 : 6 \
3441 : ((v) & ~0x7ffffff) == 0 \
3442 ? 5 \
3443 : 4 \
3444 : ((v) & ~0x3fffffff) == 0 \
3445 ? ((v) & ~0x1fffffff) == 0 \
3446 ? 3 \
3447 : 2 \
3448 : ((v) & ~0x7fffffff) == 0 \
3449 ? 1 \
3450 : 0)
3452 /* load_register()
3453 * This routine generates the least number of instructions neccessary to load
3454 * an absolute expression value into a register.
3456 static void
3457 load_register (counter, reg, ep, dbl)
3458 int *counter;
3459 int reg;
3460 expressionS *ep;
3461 int dbl;
3463 int freg;
3464 expressionS hi32, lo32;
3466 if (ep->X_op != O_big)
3468 assert (ep->X_op == O_constant);
3469 if (ep->X_add_number < 0x8000
3470 && (ep->X_add_number >= 0
3471 || (ep->X_add_number >= -0x8000
3472 && (! dbl
3473 || ! ep->X_unsigned
3474 || sizeof (ep->X_add_number) > 4))))
3476 /* We can handle 16 bit signed values with an addiu to
3477 $zero. No need to ever use daddiu here, since $zero and
3478 the result are always correct in 32 bit mode. */
3479 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3480 (int) BFD_RELOC_LO16);
3481 return;
3483 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3485 /* We can handle 16 bit unsigned values with an ori to
3486 $zero. */
3487 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3488 (int) BFD_RELOC_LO16);
3489 return;
3491 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3492 && (! dbl
3493 || ! ep->X_unsigned
3494 || sizeof (ep->X_add_number) > 4
3495 || (ep->X_add_number & 0x80000000) == 0))
3496 || ((HAVE_32BIT_GPRS || ! dbl)
3497 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3498 || (HAVE_32BIT_GPRS
3499 && ! dbl
3500 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3501 == ~ (offsetT) 0xffffffff)))
3503 /* 32 bit values require an lui. */
3504 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3505 (int) BFD_RELOC_HI16);
3506 if ((ep->X_add_number & 0xffff) != 0)
3507 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3508 (int) BFD_RELOC_LO16);
3509 return;
3513 /* The value is larger than 32 bits. */
3515 if (HAVE_32BIT_GPRS)
3517 as_bad (_("Number (0x%lx) larger than 32 bits"),
3518 (unsigned long) ep->X_add_number);
3519 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3520 (int) BFD_RELOC_LO16);
3521 return;
3524 if (ep->X_op != O_big)
3526 hi32 = *ep;
3527 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3528 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3529 hi32.X_add_number &= 0xffffffff;
3530 lo32 = *ep;
3531 lo32.X_add_number &= 0xffffffff;
3533 else
3535 assert (ep->X_add_number > 2);
3536 if (ep->X_add_number == 3)
3537 generic_bignum[3] = 0;
3538 else if (ep->X_add_number > 4)
3539 as_bad (_("Number larger than 64 bits"));
3540 lo32.X_op = O_constant;
3541 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3542 hi32.X_op = O_constant;
3543 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3546 if (hi32.X_add_number == 0)
3547 freg = 0;
3548 else
3550 int shift, bit;
3551 unsigned long hi, lo;
3553 if (hi32.X_add_number == (offsetT) 0xffffffff)
3555 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3557 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3558 reg, 0, (int) BFD_RELOC_LO16);
3559 return;
3561 if (lo32.X_add_number & 0x80000000)
3563 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3564 (int) BFD_RELOC_HI16);
3565 if (lo32.X_add_number & 0xffff)
3566 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3567 reg, reg, (int) BFD_RELOC_LO16);
3568 return;
3572 /* Check for 16bit shifted constant. We know that hi32 is
3573 non-zero, so start the mask on the first bit of the hi32
3574 value. */
3575 shift = 17;
3578 unsigned long himask, lomask;
3580 if (shift < 32)
3582 himask = 0xffff >> (32 - shift);
3583 lomask = (0xffff << shift) & 0xffffffff;
3585 else
3587 himask = 0xffff << (shift - 32);
3588 lomask = 0;
3590 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3591 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3593 expressionS tmp;
3595 tmp.X_op = O_constant;
3596 if (shift < 32)
3597 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3598 | (lo32.X_add_number >> shift));
3599 else
3600 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3601 macro_build ((char *) NULL, counter, &tmp,
3602 "ori", "t,r,i", reg, 0,
3603 (int) BFD_RELOC_LO16);
3604 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3605 (shift >= 32) ? "dsll32" : "dsll",
3606 "d,w,<", reg, reg,
3607 (shift >= 32) ? shift - 32 : shift);
3608 return;
3610 ++shift;
3612 while (shift <= (64 - 16));
3614 /* Find the bit number of the lowest one bit, and store the
3615 shifted value in hi/lo. */
3616 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3617 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3618 if (lo != 0)
3620 bit = 0;
3621 while ((lo & 1) == 0)
3623 lo >>= 1;
3624 ++bit;
3626 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3627 hi >>= bit;
3629 else
3631 bit = 32;
3632 while ((hi & 1) == 0)
3634 hi >>= 1;
3635 ++bit;
3637 lo = hi;
3638 hi = 0;
3641 /* Optimize if the shifted value is a (power of 2) - 1. */
3642 if ((hi == 0 && ((lo + 1) & lo) == 0)
3643 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3645 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3646 if (shift != 0)
3648 expressionS tmp;
3650 /* This instruction will set the register to be all
3651 ones. */
3652 tmp.X_op = O_constant;
3653 tmp.X_add_number = (offsetT) -1;
3654 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3655 reg, 0, (int) BFD_RELOC_LO16);
3656 if (bit != 0)
3658 bit += shift;
3659 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3660 (bit >= 32) ? "dsll32" : "dsll",
3661 "d,w,<", reg, reg,
3662 (bit >= 32) ? bit - 32 : bit);
3664 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3665 (shift >= 32) ? "dsrl32" : "dsrl",
3666 "d,w,<", reg, reg,
3667 (shift >= 32) ? shift - 32 : shift);
3668 return;
3672 /* Sign extend hi32 before calling load_register, because we can
3673 generally get better code when we load a sign extended value. */
3674 if ((hi32.X_add_number & 0x80000000) != 0)
3675 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3676 load_register (counter, reg, &hi32, 0);
3677 freg = reg;
3679 if ((lo32.X_add_number & 0xffff0000) == 0)
3681 if (freg != 0)
3683 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3684 "dsll32", "d,w,<", reg, freg, 0);
3685 freg = reg;
3688 else
3690 expressionS mid16;
3692 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3694 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3695 (int) BFD_RELOC_HI16);
3696 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3697 "dsrl32", "d,w,<", reg, reg, 0);
3698 return;
3701 if (freg != 0)
3703 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3704 "d,w,<", reg, freg, 16);
3705 freg = reg;
3707 mid16 = lo32;
3708 mid16.X_add_number >>= 16;
3709 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3710 freg, (int) BFD_RELOC_LO16);
3711 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3712 "d,w,<", reg, reg, 16);
3713 freg = reg;
3715 if ((lo32.X_add_number & 0xffff) != 0)
3716 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3717 (int) BFD_RELOC_LO16);
3720 /* Load an address into a register. */
3722 static void
3723 load_address (counter, reg, ep, used_at)
3724 int *counter;
3725 int reg;
3726 expressionS *ep;
3727 int *used_at;
3729 char *p = NULL;
3731 if (ep->X_op != O_constant
3732 && ep->X_op != O_symbol)
3734 as_bad (_("expression too complex"));
3735 ep->X_op = O_constant;
3738 if (ep->X_op == O_constant)
3740 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3741 return;
3744 if (mips_pic == NO_PIC)
3746 /* If this is a reference to a GP relative symbol, we want
3747 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3748 Otherwise we want
3749 lui $reg,<sym> (BFD_RELOC_HI16_S)
3750 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3751 If we have an addend, we always use the latter form.
3753 With 64bit address space and a usable $at we want
3754 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3755 lui $at,<sym> (BFD_RELOC_HI16_S)
3756 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3757 daddiu $at,<sym> (BFD_RELOC_LO16)
3758 dsll32 $reg,0
3759 daddu $reg,$reg,$at
3761 If $at is already in use, we use an path which is suboptimal
3762 on superscalar processors.
3763 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3764 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3765 dsll $reg,16
3766 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3767 dsll $reg,16
3768 daddiu $reg,<sym> (BFD_RELOC_LO16)
3770 if (HAVE_64BIT_ADDRESSES)
3772 /* We don't do GP optimization for now because RELAX_ENCODE can't
3773 hold the data for such large chunks. */
3775 if (*used_at == 0 && ! mips_opts.noat)
3777 macro_build (p, counter, ep, "lui", "t,u",
3778 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3779 macro_build (p, counter, ep, "lui", "t,u",
3780 AT, (int) BFD_RELOC_HI16_S);
3781 macro_build (p, counter, ep, "daddiu", "t,r,j",
3782 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3783 macro_build (p, counter, ep, "daddiu", "t,r,j",
3784 AT, AT, (int) BFD_RELOC_LO16);
3785 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3786 "d,w,<", reg, reg, 0);
3787 macro_build (p, counter, (expressionS *) NULL, "daddu",
3788 "d,v,t", reg, reg, AT);
3789 *used_at = 1;
3791 else
3793 macro_build (p, counter, ep, "lui", "t,u",
3794 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3795 macro_build (p, counter, ep, "daddiu", "t,r,j",
3796 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3797 macro_build (p, counter, (expressionS *) NULL, "dsll",
3798 "d,w,<", reg, reg, 16);
3799 macro_build (p, counter, ep, "daddiu", "t,r,j",
3800 reg, reg, (int) BFD_RELOC_HI16_S);
3801 macro_build (p, counter, (expressionS *) NULL, "dsll",
3802 "d,w,<", reg, reg, 16);
3803 macro_build (p, counter, ep, "daddiu", "t,r,j",
3804 reg, reg, (int) BFD_RELOC_LO16);
3807 else
3809 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3810 && ! nopic_need_relax (ep->X_add_symbol, 1))
3812 frag_grow (20);
3813 macro_build ((char *) NULL, counter, ep,
3814 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3815 reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3816 p = frag_var (rs_machine_dependent, 8, 0,
3817 RELAX_ENCODE (4, 8, 0, 4, 0,
3818 mips_opts.warn_about_macros),
3819 ep->X_add_symbol, 0, NULL);
3821 macro_build_lui (p, counter, ep, reg);
3822 if (p != NULL)
3823 p += 4;
3824 macro_build (p, counter, ep,
3825 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3826 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3829 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3831 expressionS ex;
3833 /* If this is a reference to an external symbol, we want
3834 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3835 Otherwise we want
3836 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3838 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3839 If we have NewABI, we want
3840 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3841 If there is a constant, it must be added in after. */
3842 ex.X_add_number = ep->X_add_number;
3843 ep->X_add_number = 0;
3844 frag_grow (20);
3845 if (HAVE_NEWABI)
3847 macro_build ((char *) NULL, counter, ep,
3848 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3849 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3851 else
3853 macro_build ((char *) NULL, counter, ep,
3854 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3855 reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3856 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3857 p = frag_var (rs_machine_dependent, 4, 0,
3858 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3859 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3860 macro_build (p, counter, ep,
3861 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3862 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3865 if (ex.X_add_number != 0)
3867 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3868 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3869 ex.X_op = O_constant;
3870 macro_build ((char *) NULL, counter, &ex,
3871 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3872 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3875 else if (mips_pic == SVR4_PIC)
3877 expressionS ex;
3878 int off;
3880 /* This is the large GOT case. If this is a reference to an
3881 external symbol, we want
3882 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3883 addu $reg,$reg,$gp
3884 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3885 Otherwise, for a reference to a local symbol, we want
3886 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3888 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3889 If we have NewABI, we want
3890 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3891 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
3892 If there is a constant, it must be added in after. */
3893 ex.X_add_number = ep->X_add_number;
3894 ep->X_add_number = 0;
3895 if (HAVE_NEWABI)
3897 macro_build ((char *) NULL, counter, ep,
3898 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3899 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3900 macro_build (p, counter, ep,
3901 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3902 reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3904 else
3906 if (reg_needs_delay (mips_gp_register))
3907 off = 4;
3908 else
3909 off = 0;
3910 frag_grow (32);
3911 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3912 (int) BFD_RELOC_MIPS_GOT_HI16);
3913 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3914 HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3915 reg, mips_gp_register);
3916 macro_build ((char *) NULL, counter, ep,
3917 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3918 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3919 p = frag_var (rs_machine_dependent, 12 + off, 0,
3920 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3921 mips_opts.warn_about_macros),
3922 ep->X_add_symbol, 0, NULL);
3923 if (off > 0)
3925 /* We need a nop before loading from $gp. This special
3926 check is required because the lui which starts the main
3927 instruction stream does not refer to $gp, and so will not
3928 insert the nop which may be required. */
3929 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3930 p += 4;
3932 macro_build (p, counter, ep,
3933 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3934 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3935 p += 4;
3936 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3937 p += 4;
3938 macro_build (p, counter, ep,
3939 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3940 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3943 if (ex.X_add_number != 0)
3945 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3946 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3947 ex.X_op = O_constant;
3948 macro_build ((char *) NULL, counter, &ex,
3949 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3950 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3953 else if (mips_pic == EMBEDDED_PIC)
3955 /* We always do
3956 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3958 macro_build ((char *) NULL, counter, ep,
3959 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3960 "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3962 else
3963 abort ();
3966 /* Move the contents of register SOURCE into register DEST. */
3968 static void
3969 move_register (counter, dest, source)
3970 int *counter;
3971 int dest;
3972 int source;
3974 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3975 HAVE_32BIT_GPRS ? "addu" : "daddu",
3976 "d,v,t", dest, source, 0);
3980 * Build macros
3981 * This routine implements the seemingly endless macro or synthesized
3982 * instructions and addressing modes in the mips assembly language. Many
3983 * of these macros are simple and are similar to each other. These could
3984 * probably be handled by some kind of table or grammer aproach instead of
3985 * this verbose method. Others are not simple macros but are more like
3986 * optimizing code generation.
3987 * One interesting optimization is when several store macros appear
3988 * consecutivly that would load AT with the upper half of the same address.
3989 * The ensuing load upper instructions are ommited. This implies some kind
3990 * of global optimization. We currently only optimize within a single macro.
3991 * For many of the load and store macros if the address is specified as a
3992 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3993 * first load register 'at' with zero and use it as the base register. The
3994 * mips assembler simply uses register $zero. Just one tiny optimization
3995 * we're missing.
3997 static void
3998 macro (ip)
3999 struct mips_cl_insn *ip;
4001 register int treg, sreg, dreg, breg;
4002 int tempreg;
4003 int mask;
4004 int icnt = 0;
4005 int used_at = 0;
4006 expressionS expr1;
4007 const char *s;
4008 const char *s2;
4009 const char *fmt;
4010 int likely = 0;
4011 int dbl = 0;
4012 int coproc = 0;
4013 int lr = 0;
4014 int imm = 0;
4015 offsetT maxnum;
4016 int off;
4017 bfd_reloc_code_real_type r;
4018 int hold_mips_optimize;
4020 assert (! mips_opts.mips16);
4022 treg = (ip->insn_opcode >> 16) & 0x1f;
4023 dreg = (ip->insn_opcode >> 11) & 0x1f;
4024 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4025 mask = ip->insn_mo->mask;
4027 expr1.X_op = O_constant;
4028 expr1.X_op_symbol = NULL;
4029 expr1.X_add_symbol = NULL;
4030 expr1.X_add_number = 1;
4032 switch (mask)
4034 case M_DABS:
4035 dbl = 1;
4036 case M_ABS:
4037 /* bgez $a0,.+12
4038 move v0,$a0
4039 sub v0,$zero,$a0
4042 mips_emit_delays (true);
4043 ++mips_opts.noreorder;
4044 mips_any_noreorder = 1;
4046 expr1.X_add_number = 8;
4047 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4048 if (dreg == sreg)
4049 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4051 else
4052 move_register (&icnt, dreg, sreg);
4053 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4054 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4056 --mips_opts.noreorder;
4057 return;
4059 case M_ADD_I:
4060 s = "addi";
4061 s2 = "add";
4062 goto do_addi;
4063 case M_ADDU_I:
4064 s = "addiu";
4065 s2 = "addu";
4066 goto do_addi;
4067 case M_DADD_I:
4068 dbl = 1;
4069 s = "daddi";
4070 s2 = "dadd";
4071 goto do_addi;
4072 case M_DADDU_I:
4073 dbl = 1;
4074 s = "daddiu";
4075 s2 = "daddu";
4076 do_addi:
4077 if (imm_expr.X_op == O_constant
4078 && imm_expr.X_add_number >= -0x8000
4079 && imm_expr.X_add_number < 0x8000)
4081 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4082 (int) BFD_RELOC_LO16);
4083 return;
4085 load_register (&icnt, AT, &imm_expr, dbl);
4086 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4087 treg, sreg, AT);
4088 break;
4090 case M_AND_I:
4091 s = "andi";
4092 s2 = "and";
4093 goto do_bit;
4094 case M_OR_I:
4095 s = "ori";
4096 s2 = "or";
4097 goto do_bit;
4098 case M_NOR_I:
4099 s = "";
4100 s2 = "nor";
4101 goto do_bit;
4102 case M_XOR_I:
4103 s = "xori";
4104 s2 = "xor";
4105 do_bit:
4106 if (imm_expr.X_op == O_constant
4107 && imm_expr.X_add_number >= 0
4108 && imm_expr.X_add_number < 0x10000)
4110 if (mask != M_NOR_I)
4111 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4112 sreg, (int) BFD_RELOC_LO16);
4113 else
4115 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4116 treg, sreg, (int) BFD_RELOC_LO16);
4117 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4118 "d,v,t", treg, treg, 0);
4120 return;
4123 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4124 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4125 treg, sreg, AT);
4126 break;
4128 case M_BEQ_I:
4129 s = "beq";
4130 goto beq_i;
4131 case M_BEQL_I:
4132 s = "beql";
4133 likely = 1;
4134 goto beq_i;
4135 case M_BNE_I:
4136 s = "bne";
4137 goto beq_i;
4138 case M_BNEL_I:
4139 s = "bnel";
4140 likely = 1;
4141 beq_i:
4142 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4144 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4146 return;
4148 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4149 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4150 break;
4152 case M_BGEL:
4153 likely = 1;
4154 case M_BGE:
4155 if (treg == 0)
4157 macro_build ((char *) NULL, &icnt, &offset_expr,
4158 likely ? "bgezl" : "bgez", "s,p", sreg);
4159 return;
4161 if (sreg == 0)
4163 macro_build ((char *) NULL, &icnt, &offset_expr,
4164 likely ? "blezl" : "blez", "s,p", treg);
4165 return;
4167 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4168 AT, sreg, treg);
4169 macro_build ((char *) NULL, &icnt, &offset_expr,
4170 likely ? "beql" : "beq", "s,t,p", AT, 0);
4171 break;
4173 case M_BGTL_I:
4174 likely = 1;
4175 case M_BGT_I:
4176 /* check for > max integer */
4177 maxnum = 0x7fffffff;
4178 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4180 maxnum <<= 16;
4181 maxnum |= 0xffff;
4182 maxnum <<= 16;
4183 maxnum |= 0xffff;
4185 if (imm_expr.X_op == O_constant
4186 && imm_expr.X_add_number >= maxnum
4187 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4189 do_false:
4190 /* result is always false */
4191 if (! likely)
4193 if (warn_nops)
4194 as_warn (_("Branch %s is always false (nop)"),
4195 ip->insn_mo->name);
4196 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4197 "", 0);
4199 else
4201 if (warn_nops)
4202 as_warn (_("Branch likely %s is always false"),
4203 ip->insn_mo->name);
4204 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4205 "s,t,p", 0, 0);
4207 return;
4209 if (imm_expr.X_op != O_constant)
4210 as_bad (_("Unsupported large constant"));
4211 ++imm_expr.X_add_number;
4212 /* FALLTHROUGH */
4213 case M_BGE_I:
4214 case M_BGEL_I:
4215 if (mask == M_BGEL_I)
4216 likely = 1;
4217 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4219 macro_build ((char *) NULL, &icnt, &offset_expr,
4220 likely ? "bgezl" : "bgez", "s,p", sreg);
4221 return;
4223 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4225 macro_build ((char *) NULL, &icnt, &offset_expr,
4226 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4227 return;
4229 maxnum = 0x7fffffff;
4230 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4232 maxnum <<= 16;
4233 maxnum |= 0xffff;
4234 maxnum <<= 16;
4235 maxnum |= 0xffff;
4237 maxnum = - maxnum - 1;
4238 if (imm_expr.X_op == O_constant
4239 && imm_expr.X_add_number <= maxnum
4240 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4242 do_true:
4243 /* result is always true */
4244 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4245 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4246 return;
4248 set_at (&icnt, sreg, 0);
4249 macro_build ((char *) NULL, &icnt, &offset_expr,
4250 likely ? "beql" : "beq", "s,t,p", AT, 0);
4251 break;
4253 case M_BGEUL:
4254 likely = 1;
4255 case M_BGEU:
4256 if (treg == 0)
4257 goto do_true;
4258 if (sreg == 0)
4260 macro_build ((char *) NULL, &icnt, &offset_expr,
4261 likely ? "beql" : "beq", "s,t,p", 0, treg);
4262 return;
4264 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4265 "d,v,t", AT, sreg, treg);
4266 macro_build ((char *) NULL, &icnt, &offset_expr,
4267 likely ? "beql" : "beq", "s,t,p", AT, 0);
4268 break;
4270 case M_BGTUL_I:
4271 likely = 1;
4272 case M_BGTU_I:
4273 if (sreg == 0
4274 || (HAVE_32BIT_GPRS
4275 && imm_expr.X_op == O_constant
4276 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4277 goto do_false;
4278 if (imm_expr.X_op != O_constant)
4279 as_bad (_("Unsupported large constant"));
4280 ++imm_expr.X_add_number;
4281 /* FALLTHROUGH */
4282 case M_BGEU_I:
4283 case M_BGEUL_I:
4284 if (mask == M_BGEUL_I)
4285 likely = 1;
4286 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4287 goto do_true;
4288 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4290 macro_build ((char *) NULL, &icnt, &offset_expr,
4291 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4292 return;
4294 set_at (&icnt, sreg, 1);
4295 macro_build ((char *) NULL, &icnt, &offset_expr,
4296 likely ? "beql" : "beq", "s,t,p", AT, 0);
4297 break;
4299 case M_BGTL:
4300 likely = 1;
4301 case M_BGT:
4302 if (treg == 0)
4304 macro_build ((char *) NULL, &icnt, &offset_expr,
4305 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4306 return;
4308 if (sreg == 0)
4310 macro_build ((char *) NULL, &icnt, &offset_expr,
4311 likely ? "bltzl" : "bltz", "s,p", treg);
4312 return;
4314 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4315 AT, treg, sreg);
4316 macro_build ((char *) NULL, &icnt, &offset_expr,
4317 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4318 break;
4320 case M_BGTUL:
4321 likely = 1;
4322 case M_BGTU:
4323 if (treg == 0)
4325 macro_build ((char *) NULL, &icnt, &offset_expr,
4326 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4327 return;
4329 if (sreg == 0)
4330 goto do_false;
4331 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4332 "d,v,t", AT, treg, sreg);
4333 macro_build ((char *) NULL, &icnt, &offset_expr,
4334 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4335 break;
4337 case M_BLEL:
4338 likely = 1;
4339 case M_BLE:
4340 if (treg == 0)
4342 macro_build ((char *) NULL, &icnt, &offset_expr,
4343 likely ? "blezl" : "blez", "s,p", sreg);
4344 return;
4346 if (sreg == 0)
4348 macro_build ((char *) NULL, &icnt, &offset_expr,
4349 likely ? "bgezl" : "bgez", "s,p", treg);
4350 return;
4352 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4353 AT, treg, sreg);
4354 macro_build ((char *) NULL, &icnt, &offset_expr,
4355 likely ? "beql" : "beq", "s,t,p", AT, 0);
4356 break;
4358 case M_BLEL_I:
4359 likely = 1;
4360 case M_BLE_I:
4361 maxnum = 0x7fffffff;
4362 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4364 maxnum <<= 16;
4365 maxnum |= 0xffff;
4366 maxnum <<= 16;
4367 maxnum |= 0xffff;
4369 if (imm_expr.X_op == O_constant
4370 && imm_expr.X_add_number >= maxnum
4371 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4372 goto do_true;
4373 if (imm_expr.X_op != O_constant)
4374 as_bad (_("Unsupported large constant"));
4375 ++imm_expr.X_add_number;
4376 /* FALLTHROUGH */
4377 case M_BLT_I:
4378 case M_BLTL_I:
4379 if (mask == M_BLTL_I)
4380 likely = 1;
4381 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4383 macro_build ((char *) NULL, &icnt, &offset_expr,
4384 likely ? "bltzl" : "bltz", "s,p", sreg);
4385 return;
4387 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4389 macro_build ((char *) NULL, &icnt, &offset_expr,
4390 likely ? "blezl" : "blez", "s,p", sreg);
4391 return;
4393 set_at (&icnt, sreg, 0);
4394 macro_build ((char *) NULL, &icnt, &offset_expr,
4395 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4396 break;
4398 case M_BLEUL:
4399 likely = 1;
4400 case M_BLEU:
4401 if (treg == 0)
4403 macro_build ((char *) NULL, &icnt, &offset_expr,
4404 likely ? "beql" : "beq", "s,t,p", sreg, 0);
4405 return;
4407 if (sreg == 0)
4408 goto do_true;
4409 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4410 "d,v,t", AT, treg, sreg);
4411 macro_build ((char *) NULL, &icnt, &offset_expr,
4412 likely ? "beql" : "beq", "s,t,p", AT, 0);
4413 break;
4415 case M_BLEUL_I:
4416 likely = 1;
4417 case M_BLEU_I:
4418 if (sreg == 0
4419 || (HAVE_32BIT_GPRS
4420 && imm_expr.X_op == O_constant
4421 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4422 goto do_true;
4423 if (imm_expr.X_op != O_constant)
4424 as_bad (_("Unsupported large constant"));
4425 ++imm_expr.X_add_number;
4426 /* FALLTHROUGH */
4427 case M_BLTU_I:
4428 case M_BLTUL_I:
4429 if (mask == M_BLTUL_I)
4430 likely = 1;
4431 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4432 goto do_false;
4433 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4435 macro_build ((char *) NULL, &icnt, &offset_expr,
4436 likely ? "beql" : "beq",
4437 "s,t,p", sreg, 0);
4438 return;
4440 set_at (&icnt, sreg, 1);
4441 macro_build ((char *) NULL, &icnt, &offset_expr,
4442 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4443 break;
4445 case M_BLTL:
4446 likely = 1;
4447 case M_BLT:
4448 if (treg == 0)
4450 macro_build ((char *) NULL, &icnt, &offset_expr,
4451 likely ? "bltzl" : "bltz", "s,p", sreg);
4452 return;
4454 if (sreg == 0)
4456 macro_build ((char *) NULL, &icnt, &offset_expr,
4457 likely ? "bgtzl" : "bgtz", "s,p", treg);
4458 return;
4460 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4461 AT, sreg, treg);
4462 macro_build ((char *) NULL, &icnt, &offset_expr,
4463 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4464 break;
4466 case M_BLTUL:
4467 likely = 1;
4468 case M_BLTU:
4469 if (treg == 0)
4470 goto do_false;
4471 if (sreg == 0)
4473 macro_build ((char *) NULL, &icnt, &offset_expr,
4474 likely ? "bnel" : "bne", "s,t,p", 0, treg);
4475 return;
4477 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4478 "d,v,t", AT, sreg,
4479 treg);
4480 macro_build ((char *) NULL, &icnt, &offset_expr,
4481 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4482 break;
4484 case M_DDIV_3:
4485 dbl = 1;
4486 case M_DIV_3:
4487 s = "mflo";
4488 goto do_div3;
4489 case M_DREM_3:
4490 dbl = 1;
4491 case M_REM_3:
4492 s = "mfhi";
4493 do_div3:
4494 if (treg == 0)
4496 as_warn (_("Divide by zero."));
4497 if (mips_trap)
4498 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4499 "s,t,q", 0, 0, 7);
4500 else
4501 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4502 "c", 7);
4503 return;
4506 mips_emit_delays (true);
4507 ++mips_opts.noreorder;
4508 mips_any_noreorder = 1;
4509 if (mips_trap)
4511 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4512 "s,t,q", treg, 0, 7);
4513 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4514 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4516 else
4518 expr1.X_add_number = 8;
4519 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4520 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4521 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4522 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4523 "c", 7);
4525 expr1.X_add_number = -1;
4526 macro_build ((char *) NULL, &icnt, &expr1,
4527 dbl ? "daddiu" : "addiu",
4528 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4529 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4530 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4531 if (dbl)
4533 expr1.X_add_number = 1;
4534 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4535 (int) BFD_RELOC_LO16);
4536 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4537 "d,w,<", AT, AT, 31);
4539 else
4541 expr1.X_add_number = 0x80000000;
4542 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4543 (int) BFD_RELOC_HI16);
4545 if (mips_trap)
4547 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4548 "s,t,q", sreg, AT, 6);
4549 /* We want to close the noreorder block as soon as possible, so
4550 that later insns are available for delay slot filling. */
4551 --mips_opts.noreorder;
4553 else
4555 expr1.X_add_number = 8;
4556 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4557 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4560 /* We want to close the noreorder block as soon as possible, so
4561 that later insns are available for delay slot filling. */
4562 --mips_opts.noreorder;
4564 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4565 "c", 6);
4567 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4568 break;
4570 case M_DIV_3I:
4571 s = "div";
4572 s2 = "mflo";
4573 goto do_divi;
4574 case M_DIVU_3I:
4575 s = "divu";
4576 s2 = "mflo";
4577 goto do_divi;
4578 case M_REM_3I:
4579 s = "div";
4580 s2 = "mfhi";
4581 goto do_divi;
4582 case M_REMU_3I:
4583 s = "divu";
4584 s2 = "mfhi";
4585 goto do_divi;
4586 case M_DDIV_3I:
4587 dbl = 1;
4588 s = "ddiv";
4589 s2 = "mflo";
4590 goto do_divi;
4591 case M_DDIVU_3I:
4592 dbl = 1;
4593 s = "ddivu";
4594 s2 = "mflo";
4595 goto do_divi;
4596 case M_DREM_3I:
4597 dbl = 1;
4598 s = "ddiv";
4599 s2 = "mfhi";
4600 goto do_divi;
4601 case M_DREMU_3I:
4602 dbl = 1;
4603 s = "ddivu";
4604 s2 = "mfhi";
4605 do_divi:
4606 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4608 as_warn (_("Divide by zero."));
4609 if (mips_trap)
4610 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4611 "s,t,q", 0, 0, 7);
4612 else
4613 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4614 "c", 7);
4615 return;
4617 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4619 if (strcmp (s2, "mflo") == 0)
4620 move_register (&icnt, dreg, sreg);
4621 else
4622 move_register (&icnt, dreg, 0);
4623 return;
4625 if (imm_expr.X_op == O_constant
4626 && imm_expr.X_add_number == -1
4627 && s[strlen (s) - 1] != 'u')
4629 if (strcmp (s2, "mflo") == 0)
4631 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4632 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4634 else
4635 move_register (&icnt, dreg, 0);
4636 return;
4639 load_register (&icnt, AT, &imm_expr, dbl);
4640 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4641 sreg, AT);
4642 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4643 break;
4645 case M_DIVU_3:
4646 s = "divu";
4647 s2 = "mflo";
4648 goto do_divu3;
4649 case M_REMU_3:
4650 s = "divu";
4651 s2 = "mfhi";
4652 goto do_divu3;
4653 case M_DDIVU_3:
4654 s = "ddivu";
4655 s2 = "mflo";
4656 goto do_divu3;
4657 case M_DREMU_3:
4658 s = "ddivu";
4659 s2 = "mfhi";
4660 do_divu3:
4661 mips_emit_delays (true);
4662 ++mips_opts.noreorder;
4663 mips_any_noreorder = 1;
4664 if (mips_trap)
4666 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4667 "s,t,q", treg, 0, 7);
4668 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4669 sreg, treg);
4670 /* We want to close the noreorder block as soon as possible, so
4671 that later insns are available for delay slot filling. */
4672 --mips_opts.noreorder;
4674 else
4676 expr1.X_add_number = 8;
4677 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4678 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4679 sreg, treg);
4681 /* We want to close the noreorder block as soon as possible, so
4682 that later insns are available for delay slot filling. */
4683 --mips_opts.noreorder;
4684 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4685 "c", 7);
4687 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4688 return;
4690 case M_DLA_AB:
4691 dbl = 1;
4692 case M_LA_AB:
4693 /* Load the address of a symbol into a register. If breg is not
4694 zero, we then add a base register to it. */
4696 if (dbl && HAVE_32BIT_GPRS)
4697 as_warn (_("dla used to load 32-bit register"));
4699 if (! dbl && HAVE_64BIT_OBJECTS)
4700 as_warn (_("la used to load 64-bit address"));
4702 if (offset_expr.X_op == O_constant
4703 && offset_expr.X_add_number >= -0x8000
4704 && offset_expr.X_add_number < 0x8000)
4706 macro_build ((char *) NULL, &icnt, &offset_expr,
4707 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4708 "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4709 return;
4712 if (treg == breg)
4714 tempreg = AT;
4715 used_at = 1;
4717 else
4719 tempreg = treg;
4720 used_at = 0;
4723 /* When generating embedded PIC code, we permit expressions of
4724 the form
4725 la $treg,foo-bar
4726 la $treg,foo-bar($breg)
4727 where bar is an address in the current section. These are used
4728 when getting the addresses of functions. We don't permit
4729 X_add_number to be non-zero, because if the symbol is
4730 external the relaxing code needs to know that any addend is
4731 purely the offset to X_op_symbol. */
4732 if (mips_pic == EMBEDDED_PIC
4733 && offset_expr.X_op == O_subtract
4734 && (symbol_constant_p (offset_expr.X_op_symbol)
4735 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4736 : (symbol_equated_p (offset_expr.X_op_symbol)
4737 && (S_GET_SEGMENT
4738 (symbol_get_value_expression (offset_expr.X_op_symbol)
4739 ->X_add_symbol)
4740 == now_seg)))
4741 && (offset_expr.X_add_number == 0
4742 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4744 if (breg == 0)
4746 tempreg = treg;
4747 used_at = 0;
4748 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4749 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4751 else
4753 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4754 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4755 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4756 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4757 "d,v,t", tempreg, tempreg, breg);
4759 macro_build ((char *) NULL, &icnt, &offset_expr,
4760 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4761 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4762 if (! used_at)
4763 return;
4764 break;
4767 if (offset_expr.X_op != O_symbol
4768 && offset_expr.X_op != O_constant)
4770 as_bad (_("expression too complex"));
4771 offset_expr.X_op = O_constant;
4774 if (offset_expr.X_op == O_constant)
4775 load_register (&icnt, tempreg, &offset_expr,
4776 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4777 ? (dbl || HAVE_64BIT_ADDRESSES)
4778 : HAVE_64BIT_ADDRESSES));
4779 else if (mips_pic == NO_PIC)
4781 /* If this is a reference to a GP relative symbol, we want
4782 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4783 Otherwise we want
4784 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4785 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4786 If we have a constant, we need two instructions anyhow,
4787 so we may as well always use the latter form.
4789 With 64bit address space and a usable $at we want
4790 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4791 lui $at,<sym> (BFD_RELOC_HI16_S)
4792 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4793 daddiu $at,<sym> (BFD_RELOC_LO16)
4794 dsll32 $tempreg,0
4795 daddu $tempreg,$tempreg,$at
4797 If $at is already in use, we use an path which is suboptimal
4798 on superscalar processors.
4799 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4800 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4801 dsll $tempreg,16
4802 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4803 dsll $tempreg,16
4804 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4806 char *p = NULL;
4807 if (HAVE_64BIT_ADDRESSES)
4809 /* We don't do GP optimization for now because RELAX_ENCODE can't
4810 hold the data for such large chunks. */
4812 if (used_at == 0 && ! mips_opts.noat)
4814 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4815 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4816 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4817 AT, (int) BFD_RELOC_HI16_S);
4818 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4819 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4820 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4821 AT, AT, (int) BFD_RELOC_LO16);
4822 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4823 "d,w,<", tempreg, tempreg, 0);
4824 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4825 "d,v,t", tempreg, tempreg, AT);
4826 used_at = 1;
4828 else
4830 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4831 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4832 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4833 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4834 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4835 tempreg, tempreg, 16);
4836 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4837 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4838 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4839 tempreg, tempreg, 16);
4840 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4841 tempreg, tempreg, (int) BFD_RELOC_LO16);
4844 else
4846 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4847 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4849 frag_grow (20);
4850 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4851 "t,r,j", tempreg, mips_gp_register,
4852 (int) BFD_RELOC_GPREL16);
4853 p = frag_var (rs_machine_dependent, 8, 0,
4854 RELAX_ENCODE (4, 8, 0, 4, 0,
4855 mips_opts.warn_about_macros),
4856 offset_expr.X_add_symbol, 0, NULL);
4858 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4859 if (p != NULL)
4860 p += 4;
4861 macro_build (p, &icnt, &offset_expr, "addiu",
4862 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4865 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4867 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4869 /* If this is a reference to an external symbol, and there
4870 is no constant, we want
4871 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4872 or if tempreg is PIC_CALL_REG
4873 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4874 For a local symbol, we want
4875 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4877 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4879 If we have a small constant, and this is a reference to
4880 an external symbol, we want
4881 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4883 addiu $tempreg,$tempreg,<constant>
4884 For a local symbol, we want the same instruction
4885 sequence, but we output a BFD_RELOC_LO16 reloc on the
4886 addiu instruction.
4888 If we have a large constant, and this is a reference to
4889 an external symbol, we want
4890 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4891 lui $at,<hiconstant>
4892 addiu $at,$at,<loconstant>
4893 addu $tempreg,$tempreg,$at
4894 For a local symbol, we want the same instruction
4895 sequence, but we output a BFD_RELOC_LO16 reloc on the
4896 addiu instruction.
4898 For NewABI, we want for local or external data addresses
4899 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4900 For a local function symbol, we want
4901 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4903 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4906 expr1.X_add_number = offset_expr.X_add_number;
4907 offset_expr.X_add_number = 0;
4908 frag_grow (32);
4909 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4910 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4911 else if (HAVE_NEWABI)
4912 lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4913 macro_build ((char *) NULL, &icnt, &offset_expr,
4914 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4915 "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4916 if (expr1.X_add_number == 0)
4918 int off;
4919 char *p;
4921 if (breg == 0)
4922 off = 0;
4923 else
4925 /* We're going to put in an addu instruction using
4926 tempreg, so we may as well insert the nop right
4927 now. */
4928 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4929 "nop", "");
4930 off = 4;
4932 p = frag_var (rs_machine_dependent, 8 - off, 0,
4933 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4934 (breg == 0
4935 ? mips_opts.warn_about_macros
4936 : 0)),
4937 offset_expr.X_add_symbol, 0, NULL);
4938 if (breg == 0)
4940 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4941 p += 4;
4943 macro_build (p, &icnt, &expr1,
4944 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4945 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4946 /* FIXME: If breg == 0, and the next instruction uses
4947 $tempreg, then if this variant case is used an extra
4948 nop will be generated. */
4950 else if (expr1.X_add_number >= -0x8000
4951 && expr1.X_add_number < 0x8000)
4953 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4954 "nop", "");
4955 macro_build ((char *) NULL, &icnt, &expr1,
4956 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4957 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4958 frag_var (rs_machine_dependent, 0, 0,
4959 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4960 offset_expr.X_add_symbol, 0, NULL);
4962 else
4964 int off1;
4966 /* If we are going to add in a base register, and the
4967 target register and the base register are the same,
4968 then we are using AT as a temporary register. Since
4969 we want to load the constant into AT, we add our
4970 current AT (from the global offset table) and the
4971 register into the register now, and pretend we were
4972 not using a base register. */
4973 if (breg != treg)
4974 off1 = 0;
4975 else
4977 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4978 "nop", "");
4979 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4980 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4981 "d,v,t", treg, AT, breg);
4982 breg = 0;
4983 tempreg = treg;
4984 off1 = -8;
4987 /* Set mips_optimize around the lui instruction to avoid
4988 inserting an unnecessary nop after the lw. */
4989 hold_mips_optimize = mips_optimize;
4990 mips_optimize = 2;
4991 macro_build_lui (NULL, &icnt, &expr1, AT);
4992 mips_optimize = hold_mips_optimize;
4994 macro_build ((char *) NULL, &icnt, &expr1,
4995 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4996 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4997 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4998 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4999 "d,v,t", tempreg, tempreg, AT);
5000 frag_var (rs_machine_dependent, 0, 0,
5001 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5002 offset_expr.X_add_symbol, 0, NULL);
5003 used_at = 1;
5006 else if (mips_pic == SVR4_PIC)
5008 int gpdel;
5009 char *p;
5010 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5011 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5012 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5014 /* This is the large GOT case. If this is a reference to an
5015 external symbol, and there is no constant, we want
5016 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5017 addu $tempreg,$tempreg,$gp
5018 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5019 or if tempreg is PIC_CALL_REG
5020 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5021 addu $tempreg,$tempreg,$gp
5022 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5023 For a local symbol, we want
5024 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5026 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5028 If we have a small constant, and this is a reference to
5029 an external symbol, we want
5030 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5031 addu $tempreg,$tempreg,$gp
5032 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5034 addiu $tempreg,$tempreg,<constant>
5035 For a local symbol, we want
5036 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5038 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5040 If we have a large constant, and this is a reference to
5041 an external symbol, we want
5042 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5043 addu $tempreg,$tempreg,$gp
5044 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5045 lui $at,<hiconstant>
5046 addiu $at,$at,<loconstant>
5047 addu $tempreg,$tempreg,$at
5048 For a local symbol, we want
5049 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5050 lui $at,<hiconstant>
5051 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5052 addu $tempreg,$tempreg,$at
5054 For NewABI, we want for local data addresses
5055 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5058 expr1.X_add_number = offset_expr.X_add_number;
5059 offset_expr.X_add_number = 0;
5060 frag_grow (52);
5061 if (reg_needs_delay (mips_gp_register))
5062 gpdel = 4;
5063 else
5064 gpdel = 0;
5065 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5067 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5068 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5070 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5071 tempreg, lui_reloc_type);
5072 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5073 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5074 "d,v,t", tempreg, tempreg, mips_gp_register);
5075 macro_build ((char *) NULL, &icnt, &offset_expr,
5076 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5077 "t,o(b)", tempreg, lw_reloc_type, tempreg);
5078 if (expr1.X_add_number == 0)
5080 int off;
5082 if (breg == 0)
5083 off = 0;
5084 else
5086 /* We're going to put in an addu instruction using
5087 tempreg, so we may as well insert the nop right
5088 now. */
5089 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5090 "nop", "");
5091 off = 4;
5094 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5095 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5096 8 + gpdel, 0,
5097 (breg == 0
5098 ? mips_opts.warn_about_macros
5099 : 0)),
5100 offset_expr.X_add_symbol, 0, NULL);
5102 else if (expr1.X_add_number >= -0x8000
5103 && expr1.X_add_number < 0x8000)
5105 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5106 "nop", "");
5107 macro_build ((char *) NULL, &icnt, &expr1,
5108 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5109 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5111 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5112 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5113 (breg == 0
5114 ? mips_opts.warn_about_macros
5115 : 0)),
5116 offset_expr.X_add_symbol, 0, NULL);
5118 else
5120 int adj, dreg;
5122 /* If we are going to add in a base register, and the
5123 target register and the base register are the same,
5124 then we are using AT as a temporary register. Since
5125 we want to load the constant into AT, we add our
5126 current AT (from the global offset table) and the
5127 register into the register now, and pretend we were
5128 not using a base register. */
5129 if (breg != treg)
5131 adj = 0;
5132 dreg = tempreg;
5134 else
5136 assert (tempreg == AT);
5137 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5138 "nop", "");
5139 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5140 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5141 "d,v,t", treg, AT, breg);
5142 dreg = treg;
5143 adj = 8;
5146 /* Set mips_optimize around the lui instruction to avoid
5147 inserting an unnecessary nop after the lw. */
5148 hold_mips_optimize = mips_optimize;
5149 mips_optimize = 2;
5150 macro_build_lui (NULL, &icnt, &expr1, AT);
5151 mips_optimize = hold_mips_optimize;
5153 macro_build ((char *) NULL, &icnt, &expr1,
5154 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5155 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5156 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5157 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5158 "d,v,t", dreg, dreg, AT);
5160 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5161 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5162 8 + gpdel, 0,
5163 (breg == 0
5164 ? mips_opts.warn_about_macros
5165 : 0)),
5166 offset_expr.X_add_symbol, 0, NULL);
5168 used_at = 1;
5171 if (gpdel > 0)
5173 /* This is needed because this instruction uses $gp, but
5174 the first instruction on the main stream does not. */
5175 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5176 p += 4;
5179 if (HAVE_NEWABI)
5180 local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5181 macro_build (p, &icnt, &offset_expr,
5182 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5183 "t,o(b)", tempreg,
5184 local_reloc_type,
5185 mips_gp_register);
5186 p += 4;
5187 if (expr1.X_add_number == 0 && HAVE_NEWABI)
5189 /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
5191 else
5192 if (expr1.X_add_number >= -0x8000
5193 && expr1.X_add_number < 0x8000)
5195 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5196 p += 4;
5197 macro_build (p, &icnt, &expr1,
5198 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5199 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5200 /* FIXME: If add_number is 0, and there was no base
5201 register, the external symbol case ended with a load,
5202 so if the symbol turns out to not be external, and
5203 the next instruction uses tempreg, an unnecessary nop
5204 will be inserted. */
5206 else
5208 if (breg == treg)
5210 /* We must add in the base register now, as in the
5211 external symbol case. */
5212 assert (tempreg == AT);
5213 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5214 p += 4;
5215 macro_build (p, &icnt, (expressionS *) NULL,
5216 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5217 "d,v,t", treg, AT, breg);
5218 p += 4;
5219 tempreg = treg;
5220 /* We set breg to 0 because we have arranged to add
5221 it in in both cases. */
5222 breg = 0;
5225 macro_build_lui (p, &icnt, &expr1, AT);
5226 p += 4;
5227 macro_build (p, &icnt, &expr1,
5228 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5229 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5230 p += 4;
5231 macro_build (p, &icnt, (expressionS *) NULL,
5232 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5233 "d,v,t", tempreg, tempreg, AT);
5234 p += 4;
5237 else if (mips_pic == EMBEDDED_PIC)
5239 /* We use
5240 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
5242 macro_build ((char *) NULL, &icnt, &offset_expr,
5243 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5244 tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5246 else
5247 abort ();
5249 if (breg != 0)
5251 char *s;
5253 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5254 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5255 else
5256 s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5258 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5259 "d,v,t", treg, tempreg, breg);
5262 if (! used_at)
5263 return;
5265 break;
5267 case M_J_A:
5268 /* The j instruction may not be used in PIC code, since it
5269 requires an absolute address. We convert it to a b
5270 instruction. */
5271 if (mips_pic == NO_PIC)
5272 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5273 else
5274 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5275 return;
5277 /* The jal instructions must be handled as macros because when
5278 generating PIC code they expand to multi-instruction
5279 sequences. Normally they are simple instructions. */
5280 case M_JAL_1:
5281 dreg = RA;
5282 /* Fall through. */
5283 case M_JAL_2:
5284 if (mips_pic == NO_PIC
5285 || mips_pic == EMBEDDED_PIC)
5286 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5287 "d,s", dreg, sreg);
5288 else if (mips_pic == SVR4_PIC)
5290 if (sreg != PIC_CALL_REG)
5291 as_warn (_("MIPS PIC call to register other than $25"));
5293 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5294 "d,s", dreg, sreg);
5295 if (! HAVE_NEWABI)
5297 if (mips_cprestore_offset < 0)
5298 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5299 else
5301 if (! mips_frame_reg_valid)
5303 as_warn (_("No .frame pseudo-op used in PIC code"));
5304 /* Quiet this warning. */
5305 mips_frame_reg_valid = 1;
5307 if (! mips_cprestore_valid)
5309 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5310 /* Quiet this warning. */
5311 mips_cprestore_valid = 1;
5313 expr1.X_add_number = mips_cprestore_offset;
5314 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5315 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5316 mips_gp_register, mips_frame_reg);
5320 else
5321 abort ();
5323 return;
5325 case M_JAL_A:
5326 if (mips_pic == NO_PIC)
5327 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5328 else if (mips_pic == SVR4_PIC)
5330 char *p;
5332 /* If this is a reference to an external symbol, and we are
5333 using a small GOT, we want
5334 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5336 jalr $ra,$25
5338 lw $gp,cprestore($sp)
5339 The cprestore value is set using the .cprestore
5340 pseudo-op. If we are using a big GOT, we want
5341 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5342 addu $25,$25,$gp
5343 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5345 jalr $ra,$25
5347 lw $gp,cprestore($sp)
5348 If the symbol is not external, we want
5349 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5351 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5352 jalr $ra,$25
5354 lw $gp,cprestore($sp)
5355 For NewABI, we want
5356 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5357 jalr $ra,$25 (BFD_RELOC_MIPS_JALR)
5359 if (HAVE_NEWABI)
5361 macro_build ((char *) NULL, &icnt, &offset_expr,
5362 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5363 "t,o(b)", PIC_CALL_REG,
5364 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5365 macro_build_jalr (icnt, &offset_expr);
5367 else
5369 frag_grow (40);
5370 if (! mips_big_got)
5372 macro_build ((char *) NULL, &icnt, &offset_expr,
5373 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5374 "t,o(b)", PIC_CALL_REG,
5375 (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5376 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5377 "nop", "");
5378 p = frag_var (rs_machine_dependent, 4, 0,
5379 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5380 offset_expr.X_add_symbol, 0, NULL);
5382 else
5384 int gpdel;
5386 if (reg_needs_delay (mips_gp_register))
5387 gpdel = 4;
5388 else
5389 gpdel = 0;
5390 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5391 "t,u", PIC_CALL_REG,
5392 (int) BFD_RELOC_MIPS_CALL_HI16);
5393 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5394 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5395 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5396 mips_gp_register);
5397 macro_build ((char *) NULL, &icnt, &offset_expr,
5398 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5399 "t,o(b)", PIC_CALL_REG,
5400 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5401 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5402 "nop", "");
5403 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5404 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5405 8 + gpdel, 0, 0),
5406 offset_expr.X_add_symbol, 0, NULL);
5407 if (gpdel > 0)
5409 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5410 p += 4;
5412 macro_build (p, &icnt, &offset_expr,
5413 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5414 "t,o(b)", PIC_CALL_REG,
5415 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5416 p += 4;
5417 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5418 p += 4;
5420 macro_build (p, &icnt, &offset_expr,
5421 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5422 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5423 (int) BFD_RELOC_LO16);
5424 macro_build_jalr (icnt, &offset_expr);
5426 if (mips_cprestore_offset < 0)
5427 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5428 else
5430 if (! mips_frame_reg_valid)
5432 as_warn (_("No .frame pseudo-op used in PIC code"));
5433 /* Quiet this warning. */
5434 mips_frame_reg_valid = 1;
5436 if (! mips_cprestore_valid)
5438 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5439 /* Quiet this warning. */
5440 mips_cprestore_valid = 1;
5442 if (mips_opts.noreorder)
5443 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5444 "nop", "");
5445 expr1.X_add_number = mips_cprestore_offset;
5446 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5447 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5448 mips_gp_register, mips_frame_reg);
5452 else if (mips_pic == EMBEDDED_PIC)
5454 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5455 /* The linker may expand the call to a longer sequence which
5456 uses $at, so we must break rather than return. */
5457 break;
5459 else
5460 abort ();
5462 return;
5464 case M_LB_AB:
5465 s = "lb";
5466 goto ld;
5467 case M_LBU_AB:
5468 s = "lbu";
5469 goto ld;
5470 case M_LH_AB:
5471 s = "lh";
5472 goto ld;
5473 case M_LHU_AB:
5474 s = "lhu";
5475 goto ld;
5476 case M_LW_AB:
5477 s = "lw";
5478 goto ld;
5479 case M_LWC0_AB:
5480 s = "lwc0";
5481 /* Itbl support may require additional care here. */
5482 coproc = 1;
5483 goto ld;
5484 case M_LWC1_AB:
5485 s = "lwc1";
5486 /* Itbl support may require additional care here. */
5487 coproc = 1;
5488 goto ld;
5489 case M_LWC2_AB:
5490 s = "lwc2";
5491 /* Itbl support may require additional care here. */
5492 coproc = 1;
5493 goto ld;
5494 case M_LWC3_AB:
5495 s = "lwc3";
5496 /* Itbl support may require additional care here. */
5497 coproc = 1;
5498 goto ld;
5499 case M_LWL_AB:
5500 s = "lwl";
5501 lr = 1;
5502 goto ld;
5503 case M_LWR_AB:
5504 s = "lwr";
5505 lr = 1;
5506 goto ld;
5507 case M_LDC1_AB:
5508 if (mips_arch == CPU_R4650)
5510 as_bad (_("opcode not supported on this processor"));
5511 return;
5513 s = "ldc1";
5514 /* Itbl support may require additional care here. */
5515 coproc = 1;
5516 goto ld;
5517 case M_LDC2_AB:
5518 s = "ldc2";
5519 /* Itbl support may require additional care here. */
5520 coproc = 1;
5521 goto ld;
5522 case M_LDC3_AB:
5523 s = "ldc3";
5524 /* Itbl support may require additional care here. */
5525 coproc = 1;
5526 goto ld;
5527 case M_LDL_AB:
5528 s = "ldl";
5529 lr = 1;
5530 goto ld;
5531 case M_LDR_AB:
5532 s = "ldr";
5533 lr = 1;
5534 goto ld;
5535 case M_LL_AB:
5536 s = "ll";
5537 goto ld;
5538 case M_LLD_AB:
5539 s = "lld";
5540 goto ld;
5541 case M_LWU_AB:
5542 s = "lwu";
5544 if (breg == treg || coproc || lr)
5546 tempreg = AT;
5547 used_at = 1;
5549 else
5551 tempreg = treg;
5552 used_at = 0;
5554 goto ld_st;
5555 case M_SB_AB:
5556 s = "sb";
5557 goto st;
5558 case M_SH_AB:
5559 s = "sh";
5560 goto st;
5561 case M_SW_AB:
5562 s = "sw";
5563 goto st;
5564 case M_SWC0_AB:
5565 s = "swc0";
5566 /* Itbl support may require additional care here. */
5567 coproc = 1;
5568 goto st;
5569 case M_SWC1_AB:
5570 s = "swc1";
5571 /* Itbl support may require additional care here. */
5572 coproc = 1;
5573 goto st;
5574 case M_SWC2_AB:
5575 s = "swc2";
5576 /* Itbl support may require additional care here. */
5577 coproc = 1;
5578 goto st;
5579 case M_SWC3_AB:
5580 s = "swc3";
5581 /* Itbl support may require additional care here. */
5582 coproc = 1;
5583 goto st;
5584 case M_SWL_AB:
5585 s = "swl";
5586 goto st;
5587 case M_SWR_AB:
5588 s = "swr";
5589 goto st;
5590 case M_SC_AB:
5591 s = "sc";
5592 goto st;
5593 case M_SCD_AB:
5594 s = "scd";
5595 goto st;
5596 case M_SDC1_AB:
5597 if (mips_arch == CPU_R4650)
5599 as_bad (_("opcode not supported on this processor"));
5600 return;
5602 s = "sdc1";
5603 coproc = 1;
5604 /* Itbl support may require additional care here. */
5605 goto st;
5606 case M_SDC2_AB:
5607 s = "sdc2";
5608 /* Itbl support may require additional care here. */
5609 coproc = 1;
5610 goto st;
5611 case M_SDC3_AB:
5612 s = "sdc3";
5613 /* Itbl support may require additional care here. */
5614 coproc = 1;
5615 goto st;
5616 case M_SDL_AB:
5617 s = "sdl";
5618 goto st;
5619 case M_SDR_AB:
5620 s = "sdr";
5622 tempreg = AT;
5623 used_at = 1;
5624 ld_st:
5625 /* Itbl support may require additional care here. */
5626 if (mask == M_LWC1_AB
5627 || mask == M_SWC1_AB
5628 || mask == M_LDC1_AB
5629 || mask == M_SDC1_AB
5630 || mask == M_L_DAB
5631 || mask == M_S_DAB)
5632 fmt = "T,o(b)";
5633 else if (coproc)
5634 fmt = "E,o(b)";
5635 else
5636 fmt = "t,o(b)";
5638 /* For embedded PIC, we allow loads where the offset is calculated
5639 by subtracting a symbol in the current segment from an unknown
5640 symbol, relative to a base register, e.g.:
5641 <op> $treg, <sym>-<localsym>($breg)
5642 This is used by the compiler for switch statements. */
5643 if (mips_pic == EMBEDDED_PIC
5644 && offset_expr.X_op == O_subtract
5645 && (symbol_constant_p (offset_expr.X_op_symbol)
5646 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5647 : (symbol_equated_p (offset_expr.X_op_symbol)
5648 && (S_GET_SEGMENT
5649 (symbol_get_value_expression (offset_expr.X_op_symbol)
5650 ->X_add_symbol)
5651 == now_seg)))
5652 && breg != 0
5653 && (offset_expr.X_add_number == 0
5654 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5656 /* For this case, we output the instructions:
5657 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5658 addiu $tempreg,$tempreg,$breg
5659 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5660 If the relocation would fit entirely in 16 bits, it would be
5661 nice to emit:
5662 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5663 instead, but that seems quite difficult. */
5664 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5665 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5666 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5667 ((bfd_arch_bits_per_address (stdoutput) == 32
5668 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5669 ? "addu" : "daddu"),
5670 "d,v,t", tempreg, tempreg, breg);
5671 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5672 (int) BFD_RELOC_PCREL_LO16, tempreg);
5673 if (! used_at)
5674 return;
5675 break;
5678 if (offset_expr.X_op != O_constant
5679 && offset_expr.X_op != O_symbol)
5681 as_bad (_("expression too complex"));
5682 offset_expr.X_op = O_constant;
5685 /* A constant expression in PIC code can be handled just as it
5686 is in non PIC code. */
5687 if (mips_pic == NO_PIC
5688 || offset_expr.X_op == O_constant)
5690 char *p;
5692 /* If this is a reference to a GP relative symbol, and there
5693 is no base register, we want
5694 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5695 Otherwise, if there is no base register, we want
5696 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5697 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5698 If we have a constant, we need two instructions anyhow,
5699 so we always use the latter form.
5701 If we have a base register, and this is a reference to a
5702 GP relative symbol, we want
5703 addu $tempreg,$breg,$gp
5704 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5705 Otherwise we want
5706 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5707 addu $tempreg,$tempreg,$breg
5708 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5709 With a constant we always use the latter case.
5711 With 64bit address space and no base register and $at usable,
5712 we want
5713 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5714 lui $at,<sym> (BFD_RELOC_HI16_S)
5715 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5716 dsll32 $tempreg,0
5717 daddu $tempreg,$at
5718 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5719 If we have a base register, we want
5720 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5721 lui $at,<sym> (BFD_RELOC_HI16_S)
5722 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5723 daddu $at,$breg
5724 dsll32 $tempreg,0
5725 daddu $tempreg,$at
5726 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5728 Without $at we can't generate the optimal path for superscalar
5729 processors here since this would require two temporary registers.
5730 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5731 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5732 dsll $tempreg,16
5733 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5734 dsll $tempreg,16
5735 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5736 If we have a base register, we want
5737 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5738 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5739 dsll $tempreg,16
5740 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5741 dsll $tempreg,16
5742 daddu $tempreg,$tempreg,$breg
5743 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5745 If we have 64-bit addresses, as an optimization, for
5746 addresses which are 32-bit constants (e.g. kseg0/kseg1
5747 addresses) we fall back to the 32-bit address generation
5748 mechanism since it is more efficient. Note that due to
5749 the signed offset used by memory operations, the 32-bit
5750 range is shifted down by 32768 here. This code should
5751 probably attempt to generate 64-bit constants more
5752 efficiently in general.
5754 if (HAVE_64BIT_ADDRESSES
5755 && !(offset_expr.X_op == O_constant
5756 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5758 p = NULL;
5760 /* We don't do GP optimization for now because RELAX_ENCODE can't
5761 hold the data for such large chunks. */
5763 if (used_at == 0 && ! mips_opts.noat)
5765 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5766 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5767 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5768 AT, (int) BFD_RELOC_HI16_S);
5769 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5770 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5771 if (breg != 0)
5772 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5773 "d,v,t", AT, AT, breg);
5774 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5775 "d,w,<", tempreg, tempreg, 0);
5776 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5777 "d,v,t", tempreg, tempreg, AT);
5778 macro_build (p, &icnt, &offset_expr, s,
5779 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5780 used_at = 1;
5782 else
5784 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5785 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5786 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5787 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5788 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5789 "d,w,<", tempreg, tempreg, 16);
5790 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5791 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5792 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5793 "d,w,<", tempreg, tempreg, 16);
5794 if (breg != 0)
5795 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5796 "d,v,t", tempreg, tempreg, breg);
5797 macro_build (p, &icnt, &offset_expr, s,
5798 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5801 return;
5804 if (breg == 0)
5806 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5807 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5808 p = NULL;
5809 else
5811 frag_grow (20);
5812 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5813 treg, (int) BFD_RELOC_GPREL16,
5814 mips_gp_register);
5815 p = frag_var (rs_machine_dependent, 8, 0,
5816 RELAX_ENCODE (4, 8, 0, 4, 0,
5817 (mips_opts.warn_about_macros
5818 || (used_at
5819 && mips_opts.noat))),
5820 offset_expr.X_add_symbol, 0, NULL);
5821 used_at = 0;
5823 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5824 if (p != NULL)
5825 p += 4;
5826 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5827 (int) BFD_RELOC_LO16, tempreg);
5829 else
5831 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5832 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5833 p = NULL;
5834 else
5836 frag_grow (28);
5837 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5838 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5839 "d,v,t", tempreg, breg, mips_gp_register);
5840 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5841 treg, (int) BFD_RELOC_GPREL16, tempreg);
5842 p = frag_var (rs_machine_dependent, 12, 0,
5843 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5844 offset_expr.X_add_symbol, 0, NULL);
5846 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5847 if (p != NULL)
5848 p += 4;
5849 macro_build (p, &icnt, (expressionS *) NULL,
5850 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5851 "d,v,t", tempreg, tempreg, breg);
5852 if (p != NULL)
5853 p += 4;
5854 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5855 (int) BFD_RELOC_LO16, tempreg);
5858 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5860 char *p;
5861 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5863 /* If this is a reference to an external symbol, we want
5864 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5866 <op> $treg,0($tempreg)
5867 Otherwise we want
5868 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5870 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5871 <op> $treg,0($tempreg)
5872 If we have NewABI, we want
5873 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5874 If there is a base register, we add it to $tempreg before
5875 the <op>. If there is a constant, we stick it in the
5876 <op> instruction. We don't handle constants larger than
5877 16 bits, because we have no way to load the upper 16 bits
5878 (actually, we could handle them for the subset of cases
5879 in which we are not using $at). */
5880 assert (offset_expr.X_op == O_symbol);
5881 expr1.X_add_number = offset_expr.X_add_number;
5882 offset_expr.X_add_number = 0;
5883 if (HAVE_NEWABI)
5884 lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5885 if (expr1.X_add_number < -0x8000
5886 || expr1.X_add_number >= 0x8000)
5887 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5888 frag_grow (20);
5889 macro_build ((char *) NULL, &icnt, &offset_expr,
5890 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5891 (int) lw_reloc_type, mips_gp_register);
5892 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5893 p = frag_var (rs_machine_dependent, 4, 0,
5894 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5895 offset_expr.X_add_symbol, 0, NULL);
5896 macro_build (p, &icnt, &offset_expr,
5897 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5898 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5899 if (breg != 0)
5900 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5901 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5902 "d,v,t", tempreg, tempreg, breg);
5903 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5904 (int) BFD_RELOC_LO16, tempreg);
5906 else if (mips_pic == SVR4_PIC)
5908 int gpdel;
5909 char *p;
5911 /* If this is a reference to an external symbol, we want
5912 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5913 addu $tempreg,$tempreg,$gp
5914 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5915 <op> $treg,0($tempreg)
5916 Otherwise we want
5917 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5919 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5920 <op> $treg,0($tempreg)
5921 If there is a base register, we add it to $tempreg before
5922 the <op>. If there is a constant, we stick it in the
5923 <op> instruction. We don't handle constants larger than
5924 16 bits, because we have no way to load the upper 16 bits
5925 (actually, we could handle them for the subset of cases
5926 in which we are not using $at).
5928 For NewABI, we want
5929 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5930 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5931 <op> $treg,0($tempreg)
5933 assert (offset_expr.X_op == O_symbol);
5934 expr1.X_add_number = offset_expr.X_add_number;
5935 offset_expr.X_add_number = 0;
5936 if (expr1.X_add_number < -0x8000
5937 || expr1.X_add_number >= 0x8000)
5938 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5939 if (HAVE_NEWABI)
5941 macro_build ((char *) NULL, &icnt, &offset_expr,
5942 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5943 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5944 mips_gp_register);
5945 macro_build ((char *) NULL, &icnt, &offset_expr,
5946 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5947 "t,r,j", tempreg, tempreg,
5948 BFD_RELOC_MIPS_GOT_OFST);
5949 if (breg != 0)
5950 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5951 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5952 "d,v,t", tempreg, tempreg, breg);
5953 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5954 (int) BFD_RELOC_LO16, tempreg);
5956 if (! used_at)
5957 return;
5959 break;
5961 if (reg_needs_delay (mips_gp_register))
5962 gpdel = 4;
5963 else
5964 gpdel = 0;
5965 frag_grow (36);
5966 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5967 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5968 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5969 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5970 "d,v,t", tempreg, tempreg, mips_gp_register);
5971 macro_build ((char *) NULL, &icnt, &offset_expr,
5972 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5973 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5974 tempreg);
5975 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5976 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5977 offset_expr.X_add_symbol, 0, NULL);
5978 if (gpdel > 0)
5980 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5981 p += 4;
5983 macro_build (p, &icnt, &offset_expr,
5984 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5985 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5986 mips_gp_register);
5987 p += 4;
5988 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5989 p += 4;
5990 macro_build (p, &icnt, &offset_expr,
5991 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5992 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5993 if (breg != 0)
5994 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5995 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5996 "d,v,t", tempreg, tempreg, breg);
5997 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5998 (int) BFD_RELOC_LO16, tempreg);
6000 else if (mips_pic == EMBEDDED_PIC)
6002 /* If there is no base register, we want
6003 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6004 If there is a base register, we want
6005 addu $tempreg,$breg,$gp
6006 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
6008 assert (offset_expr.X_op == O_symbol);
6009 if (breg == 0)
6011 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6012 treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6013 used_at = 0;
6015 else
6017 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6018 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6019 "d,v,t", tempreg, breg, mips_gp_register);
6020 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6021 treg, (int) BFD_RELOC_GPREL16, tempreg);
6024 else
6025 abort ();
6027 if (! used_at)
6028 return;
6030 break;
6032 case M_LI:
6033 case M_LI_S:
6034 load_register (&icnt, treg, &imm_expr, 0);
6035 return;
6037 case M_DLI:
6038 load_register (&icnt, treg, &imm_expr, 1);
6039 return;
6041 case M_LI_SS:
6042 if (imm_expr.X_op == O_constant)
6044 load_register (&icnt, AT, &imm_expr, 0);
6045 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6046 "mtc1", "t,G", AT, treg);
6047 break;
6049 else
6051 assert (offset_expr.X_op == O_symbol
6052 && strcmp (segment_name (S_GET_SEGMENT
6053 (offset_expr.X_add_symbol)),
6054 ".lit4") == 0
6055 && offset_expr.X_add_number == 0);
6056 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6057 treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6058 return;
6061 case M_LI_D:
6062 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6063 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6064 order 32 bits of the value and the low order 32 bits are either
6065 zero or in OFFSET_EXPR. */
6066 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6068 if (HAVE_64BIT_GPRS)
6069 load_register (&icnt, treg, &imm_expr, 1);
6070 else
6072 int hreg, lreg;
6074 if (target_big_endian)
6076 hreg = treg;
6077 lreg = treg + 1;
6079 else
6081 hreg = treg + 1;
6082 lreg = treg;
6085 if (hreg <= 31)
6086 load_register (&icnt, hreg, &imm_expr, 0);
6087 if (lreg <= 31)
6089 if (offset_expr.X_op == O_absent)
6090 move_register (&icnt, lreg, 0);
6091 else
6093 assert (offset_expr.X_op == O_constant);
6094 load_register (&icnt, lreg, &offset_expr, 0);
6098 return;
6101 /* We know that sym is in the .rdata section. First we get the
6102 upper 16 bits of the address. */
6103 if (mips_pic == NO_PIC)
6105 macro_build_lui (NULL, &icnt, &offset_expr, AT);
6107 else if (mips_pic == SVR4_PIC)
6109 macro_build ((char *) NULL, &icnt, &offset_expr,
6110 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6111 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6112 mips_gp_register);
6114 else if (mips_pic == EMBEDDED_PIC)
6116 /* For embedded PIC we pick up the entire address off $gp in
6117 a single instruction. */
6118 macro_build ((char *) NULL, &icnt, &offset_expr,
6119 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6120 mips_gp_register, (int) BFD_RELOC_GPREL16);
6121 offset_expr.X_op = O_constant;
6122 offset_expr.X_add_number = 0;
6124 else
6125 abort ();
6127 /* Now we load the register(s). */
6128 if (HAVE_64BIT_GPRS)
6129 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6130 treg, (int) BFD_RELOC_LO16, AT);
6131 else
6133 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6134 treg, (int) BFD_RELOC_LO16, AT);
6135 if (treg != RA)
6137 /* FIXME: How in the world do we deal with the possible
6138 overflow here? */
6139 offset_expr.X_add_number += 4;
6140 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6141 treg + 1, (int) BFD_RELOC_LO16, AT);
6145 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6146 does not become a variant frag. */
6147 frag_wane (frag_now);
6148 frag_new (0);
6150 break;
6152 case M_LI_DD:
6153 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6154 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6155 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6156 the value and the low order 32 bits are either zero or in
6157 OFFSET_EXPR. */
6158 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6160 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6161 if (HAVE_64BIT_FPRS)
6163 assert (HAVE_64BIT_GPRS);
6164 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6165 "dmtc1", "t,S", AT, treg);
6167 else
6169 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6170 "mtc1", "t,G", AT, treg + 1);
6171 if (offset_expr.X_op == O_absent)
6172 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6173 "mtc1", "t,G", 0, treg);
6174 else
6176 assert (offset_expr.X_op == O_constant);
6177 load_register (&icnt, AT, &offset_expr, 0);
6178 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6179 "mtc1", "t,G", AT, treg);
6182 break;
6185 assert (offset_expr.X_op == O_symbol
6186 && offset_expr.X_add_number == 0);
6187 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6188 if (strcmp (s, ".lit8") == 0)
6190 if (mips_opts.isa != ISA_MIPS1)
6192 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6193 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6194 mips_gp_register);
6195 return;
6197 breg = mips_gp_register;
6198 r = BFD_RELOC_MIPS_LITERAL;
6199 goto dob;
6201 else
6203 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6204 if (mips_pic == SVR4_PIC)
6205 macro_build ((char *) NULL, &icnt, &offset_expr,
6206 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6207 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6208 mips_gp_register);
6209 else
6211 /* FIXME: This won't work for a 64 bit address. */
6212 macro_build_lui (NULL, &icnt, &offset_expr, AT);
6215 if (mips_opts.isa != ISA_MIPS1)
6217 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6218 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6220 /* To avoid confusion in tc_gen_reloc, we must ensure
6221 that this does not become a variant frag. */
6222 frag_wane (frag_now);
6223 frag_new (0);
6225 break;
6227 breg = AT;
6228 r = BFD_RELOC_LO16;
6229 goto dob;
6232 case M_L_DOB:
6233 if (mips_arch == CPU_R4650)
6235 as_bad (_("opcode not supported on this processor"));
6236 return;
6238 /* Even on a big endian machine $fn comes before $fn+1. We have
6239 to adjust when loading from memory. */
6240 r = BFD_RELOC_LO16;
6241 dob:
6242 assert (mips_opts.isa == ISA_MIPS1);
6243 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6244 target_big_endian ? treg + 1 : treg,
6245 (int) r, breg);
6246 /* FIXME: A possible overflow which I don't know how to deal
6247 with. */
6248 offset_expr.X_add_number += 4;
6249 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6250 target_big_endian ? treg : treg + 1,
6251 (int) r, breg);
6253 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6254 does not become a variant frag. */
6255 frag_wane (frag_now);
6256 frag_new (0);
6258 if (breg != AT)
6259 return;
6260 break;
6262 case M_L_DAB:
6264 * The MIPS assembler seems to check for X_add_number not
6265 * being double aligned and generating:
6266 * lui at,%hi(foo+1)
6267 * addu at,at,v1
6268 * addiu at,at,%lo(foo+1)
6269 * lwc1 f2,0(at)
6270 * lwc1 f3,4(at)
6271 * But, the resulting address is the same after relocation so why
6272 * generate the extra instruction?
6274 if (mips_arch == CPU_R4650)
6276 as_bad (_("opcode not supported on this processor"));
6277 return;
6279 /* Itbl support may require additional care here. */
6280 coproc = 1;
6281 if (mips_opts.isa != ISA_MIPS1)
6283 s = "ldc1";
6284 goto ld;
6287 s = "lwc1";
6288 fmt = "T,o(b)";
6289 goto ldd_std;
6291 case M_S_DAB:
6292 if (mips_arch == CPU_R4650)
6294 as_bad (_("opcode not supported on this processor"));
6295 return;
6298 if (mips_opts.isa != ISA_MIPS1)
6300 s = "sdc1";
6301 goto st;
6304 s = "swc1";
6305 fmt = "T,o(b)";
6306 /* Itbl support may require additional care here. */
6307 coproc = 1;
6308 goto ldd_std;
6310 case M_LD_AB:
6311 if (HAVE_64BIT_GPRS)
6313 s = "ld";
6314 goto ld;
6317 s = "lw";
6318 fmt = "t,o(b)";
6319 goto ldd_std;
6321 case M_SD_AB:
6322 if (HAVE_64BIT_GPRS)
6324 s = "sd";
6325 goto st;
6328 s = "sw";
6329 fmt = "t,o(b)";
6331 ldd_std:
6332 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6333 loads for the case of doing a pair of loads to simulate an 'ld'.
6334 This is not currently done by the compiler, and assembly coders
6335 writing embedded-pic code can cope. */
6337 if (offset_expr.X_op != O_symbol
6338 && offset_expr.X_op != O_constant)
6340 as_bad (_("expression too complex"));
6341 offset_expr.X_op = O_constant;
6344 /* Even on a big endian machine $fn comes before $fn+1. We have
6345 to adjust when loading from memory. We set coproc if we must
6346 load $fn+1 first. */
6347 /* Itbl support may require additional care here. */
6348 if (! target_big_endian)
6349 coproc = 0;
6351 if (mips_pic == NO_PIC
6352 || offset_expr.X_op == O_constant)
6354 char *p;
6356 /* If this is a reference to a GP relative symbol, we want
6357 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6358 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6359 If we have a base register, we use this
6360 addu $at,$breg,$gp
6361 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6362 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6363 If this is not a GP relative symbol, we want
6364 lui $at,<sym> (BFD_RELOC_HI16_S)
6365 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6366 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6367 If there is a base register, we add it to $at after the
6368 lui instruction. If there is a constant, we always use
6369 the last case. */
6370 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6371 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6373 p = NULL;
6374 used_at = 1;
6376 else
6378 int off;
6380 if (breg == 0)
6382 frag_grow (28);
6383 tempreg = mips_gp_register;
6384 off = 0;
6385 used_at = 0;
6387 else
6389 frag_grow (36);
6390 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6391 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6392 "d,v,t", AT, breg, mips_gp_register);
6393 tempreg = AT;
6394 off = 4;
6395 used_at = 1;
6398 /* Itbl support may require additional care here. */
6399 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6400 coproc ? treg + 1 : treg,
6401 (int) BFD_RELOC_GPREL16, tempreg);
6402 offset_expr.X_add_number += 4;
6404 /* Set mips_optimize to 2 to avoid inserting an
6405 undesired nop. */
6406 hold_mips_optimize = mips_optimize;
6407 mips_optimize = 2;
6408 /* Itbl support may require additional care here. */
6409 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6410 coproc ? treg : treg + 1,
6411 (int) BFD_RELOC_GPREL16, tempreg);
6412 mips_optimize = hold_mips_optimize;
6414 p = frag_var (rs_machine_dependent, 12 + off, 0,
6415 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6416 used_at && mips_opts.noat),
6417 offset_expr.X_add_symbol, 0, NULL);
6419 /* We just generated two relocs. When tc_gen_reloc
6420 handles this case, it will skip the first reloc and
6421 handle the second. The second reloc already has an
6422 extra addend of 4, which we added above. We must
6423 subtract it out, and then subtract another 4 to make
6424 the first reloc come out right. The second reloc
6425 will come out right because we are going to add 4 to
6426 offset_expr when we build its instruction below.
6428 If we have a symbol, then we don't want to include
6429 the offset, because it will wind up being included
6430 when we generate the reloc. */
6432 if (offset_expr.X_op == O_constant)
6433 offset_expr.X_add_number -= 8;
6434 else
6436 offset_expr.X_add_number = -4;
6437 offset_expr.X_op = O_constant;
6440 macro_build_lui (p, &icnt, &offset_expr, AT);
6441 if (p != NULL)
6442 p += 4;
6443 if (breg != 0)
6445 macro_build (p, &icnt, (expressionS *) NULL,
6446 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6447 "d,v,t", AT, breg, AT);
6448 if (p != NULL)
6449 p += 4;
6451 /* Itbl support may require additional care here. */
6452 macro_build (p, &icnt, &offset_expr, s, fmt,
6453 coproc ? treg + 1 : treg,
6454 (int) BFD_RELOC_LO16, AT);
6455 if (p != NULL)
6456 p += 4;
6457 /* FIXME: How do we handle overflow here? */
6458 offset_expr.X_add_number += 4;
6459 /* Itbl support may require additional care here. */
6460 macro_build (p, &icnt, &offset_expr, s, fmt,
6461 coproc ? treg : treg + 1,
6462 (int) BFD_RELOC_LO16, AT);
6464 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6466 int off;
6468 /* If this is a reference to an external symbol, we want
6469 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6471 <op> $treg,0($at)
6472 <op> $treg+1,4($at)
6473 Otherwise we want
6474 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6476 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6477 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6478 If there is a base register we add it to $at before the
6479 lwc1 instructions. If there is a constant we include it
6480 in the lwc1 instructions. */
6481 used_at = 1;
6482 expr1.X_add_number = offset_expr.X_add_number;
6483 offset_expr.X_add_number = 0;
6484 if (expr1.X_add_number < -0x8000
6485 || expr1.X_add_number >= 0x8000 - 4)
6486 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6487 if (breg == 0)
6488 off = 0;
6489 else
6490 off = 4;
6491 frag_grow (24 + off);
6492 macro_build ((char *) NULL, &icnt, &offset_expr,
6493 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6494 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6495 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6496 if (breg != 0)
6497 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6498 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6499 "d,v,t", AT, breg, AT);
6500 /* Itbl support may require additional care here. */
6501 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6502 coproc ? treg + 1 : treg,
6503 (int) BFD_RELOC_LO16, AT);
6504 expr1.X_add_number += 4;
6506 /* Set mips_optimize to 2 to avoid inserting an undesired
6507 nop. */
6508 hold_mips_optimize = mips_optimize;
6509 mips_optimize = 2;
6510 /* Itbl support may require additional care here. */
6511 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6512 coproc ? treg : treg + 1,
6513 (int) BFD_RELOC_LO16, AT);
6514 mips_optimize = hold_mips_optimize;
6516 (void) frag_var (rs_machine_dependent, 0, 0,
6517 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6518 offset_expr.X_add_symbol, 0, NULL);
6520 else if (mips_pic == SVR4_PIC)
6522 int gpdel, off;
6523 char *p;
6525 /* If this is a reference to an external symbol, we want
6526 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6527 addu $at,$at,$gp
6528 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6530 <op> $treg,0($at)
6531 <op> $treg+1,4($at)
6532 Otherwise we want
6533 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6535 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6536 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6537 If there is a base register we add it to $at before the
6538 lwc1 instructions. If there is a constant we include it
6539 in the lwc1 instructions. */
6540 used_at = 1;
6541 expr1.X_add_number = offset_expr.X_add_number;
6542 offset_expr.X_add_number = 0;
6543 if (expr1.X_add_number < -0x8000
6544 || expr1.X_add_number >= 0x8000 - 4)
6545 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6546 if (reg_needs_delay (mips_gp_register))
6547 gpdel = 4;
6548 else
6549 gpdel = 0;
6550 if (breg == 0)
6551 off = 0;
6552 else
6553 off = 4;
6554 frag_grow (56);
6555 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6556 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6557 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6558 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6559 "d,v,t", AT, AT, mips_gp_register);
6560 macro_build ((char *) NULL, &icnt, &offset_expr,
6561 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6562 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6563 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6564 if (breg != 0)
6565 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6566 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6567 "d,v,t", AT, breg, AT);
6568 /* Itbl support may require additional care here. */
6569 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6570 coproc ? treg + 1 : treg,
6571 (int) BFD_RELOC_LO16, AT);
6572 expr1.X_add_number += 4;
6574 /* Set mips_optimize to 2 to avoid inserting an undesired
6575 nop. */
6576 hold_mips_optimize = mips_optimize;
6577 mips_optimize = 2;
6578 /* Itbl support may require additional care here. */
6579 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6580 coproc ? treg : treg + 1,
6581 (int) BFD_RELOC_LO16, AT);
6582 mips_optimize = hold_mips_optimize;
6583 expr1.X_add_number -= 4;
6585 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6586 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6587 8 + gpdel + off, 1, 0),
6588 offset_expr.X_add_symbol, 0, NULL);
6589 if (gpdel > 0)
6591 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6592 p += 4;
6594 macro_build (p, &icnt, &offset_expr,
6595 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6596 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6597 mips_gp_register);
6598 p += 4;
6599 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6600 p += 4;
6601 if (breg != 0)
6603 macro_build (p, &icnt, (expressionS *) NULL,
6604 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6605 "d,v,t", AT, breg, AT);
6606 p += 4;
6608 /* Itbl support may require additional care here. */
6609 macro_build (p, &icnt, &expr1, s, fmt,
6610 coproc ? treg + 1 : treg,
6611 (int) BFD_RELOC_LO16, AT);
6612 p += 4;
6613 expr1.X_add_number += 4;
6615 /* Set mips_optimize to 2 to avoid inserting an undesired
6616 nop. */
6617 hold_mips_optimize = mips_optimize;
6618 mips_optimize = 2;
6619 /* Itbl support may require additional care here. */
6620 macro_build (p, &icnt, &expr1, s, fmt,
6621 coproc ? treg : treg + 1,
6622 (int) BFD_RELOC_LO16, AT);
6623 mips_optimize = hold_mips_optimize;
6625 else if (mips_pic == EMBEDDED_PIC)
6627 /* If there is no base register, we use
6628 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6629 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6630 If we have a base register, we use
6631 addu $at,$breg,$gp
6632 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6633 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6635 if (breg == 0)
6637 tempreg = mips_gp_register;
6638 used_at = 0;
6640 else
6642 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6643 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6644 "d,v,t", AT, breg, mips_gp_register);
6645 tempreg = AT;
6646 used_at = 1;
6649 /* Itbl support may require additional care here. */
6650 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6651 coproc ? treg + 1 : treg,
6652 (int) BFD_RELOC_GPREL16, tempreg);
6653 offset_expr.X_add_number += 4;
6654 /* Itbl support may require additional care here. */
6655 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6656 coproc ? treg : treg + 1,
6657 (int) BFD_RELOC_GPREL16, tempreg);
6659 else
6660 abort ();
6662 if (! used_at)
6663 return;
6665 break;
6667 case M_LD_OB:
6668 s = "lw";
6669 goto sd_ob;
6670 case M_SD_OB:
6671 s = "sw";
6672 sd_ob:
6673 assert (HAVE_32BIT_ADDRESSES);
6674 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6675 (int) BFD_RELOC_LO16, breg);
6676 offset_expr.X_add_number += 4;
6677 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6678 (int) BFD_RELOC_LO16, breg);
6679 return;
6681 /* New code added to support COPZ instructions.
6682 This code builds table entries out of the macros in mip_opcodes.
6683 R4000 uses interlocks to handle coproc delays.
6684 Other chips (like the R3000) require nops to be inserted for delays.
6686 FIXME: Currently, we require that the user handle delays.
6687 In order to fill delay slots for non-interlocked chips,
6688 we must have a way to specify delays based on the coprocessor.
6689 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6690 What are the side-effects of the cop instruction?
6691 What cache support might we have and what are its effects?
6692 Both coprocessor & memory require delays. how long???
6693 What registers are read/set/modified?
6695 If an itbl is provided to interpret cop instructions,
6696 this knowledge can be encoded in the itbl spec. */
6698 case M_COP0:
6699 s = "c0";
6700 goto copz;
6701 case M_COP1:
6702 s = "c1";
6703 goto copz;
6704 case M_COP2:
6705 s = "c2";
6706 goto copz;
6707 case M_COP3:
6708 s = "c3";
6709 copz:
6710 /* For now we just do C (same as Cz). The parameter will be
6711 stored in insn_opcode by mips_ip. */
6712 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6713 ip->insn_opcode);
6714 return;
6716 case M_MOVE:
6717 move_register (&icnt, dreg, sreg);
6718 return;
6720 #ifdef LOSING_COMPILER
6721 default:
6722 /* Try and see if this is a new itbl instruction.
6723 This code builds table entries out of the macros in mip_opcodes.
6724 FIXME: For now we just assemble the expression and pass it's
6725 value along as a 32-bit immediate.
6726 We may want to have the assembler assemble this value,
6727 so that we gain the assembler's knowledge of delay slots,
6728 symbols, etc.
6729 Would it be more efficient to use mask (id) here? */
6730 if (itbl_have_entries
6731 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6733 s = ip->insn_mo->name;
6734 s2 = "cop3";
6735 coproc = ITBL_DECODE_PNUM (immed_expr);;
6736 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6737 return;
6739 macro2 (ip);
6740 return;
6742 if (mips_opts.noat)
6743 as_warn (_("Macro used $at after \".set noat\""));
6746 static void
6747 macro2 (ip)
6748 struct mips_cl_insn *ip;
6750 register int treg, sreg, dreg, breg;
6751 int tempreg;
6752 int mask;
6753 int icnt = 0;
6754 int used_at;
6755 expressionS expr1;
6756 const char *s;
6757 const char *s2;
6758 const char *fmt;
6759 int likely = 0;
6760 int dbl = 0;
6761 int coproc = 0;
6762 int lr = 0;
6763 int imm = 0;
6764 int off;
6765 offsetT maxnum;
6766 bfd_reloc_code_real_type r;
6767 char *p;
6769 treg = (ip->insn_opcode >> 16) & 0x1f;
6770 dreg = (ip->insn_opcode >> 11) & 0x1f;
6771 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6772 mask = ip->insn_mo->mask;
6774 expr1.X_op = O_constant;
6775 expr1.X_op_symbol = NULL;
6776 expr1.X_add_symbol = NULL;
6777 expr1.X_add_number = 1;
6779 switch (mask)
6781 #endif /* LOSING_COMPILER */
6783 case M_DMUL:
6784 dbl = 1;
6785 case M_MUL:
6786 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6787 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6788 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6789 dreg);
6790 return;
6792 case M_DMUL_I:
6793 dbl = 1;
6794 case M_MUL_I:
6795 /* The MIPS assembler some times generates shifts and adds. I'm
6796 not trying to be that fancy. GCC should do this for us
6797 anyway. */
6798 load_register (&icnt, AT, &imm_expr, dbl);
6799 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6800 dbl ? "dmult" : "mult", "s,t", sreg, AT);
6801 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6802 dreg);
6803 break;
6805 case M_DMULO_I:
6806 dbl = 1;
6807 case M_MULO_I:
6808 imm = 1;
6809 goto do_mulo;
6811 case M_DMULO:
6812 dbl = 1;
6813 case M_MULO:
6814 do_mulo:
6815 mips_emit_delays (true);
6816 ++mips_opts.noreorder;
6817 mips_any_noreorder = 1;
6818 if (imm)
6819 load_register (&icnt, AT, &imm_expr, dbl);
6820 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6821 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6822 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6823 dreg);
6824 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6825 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6826 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6827 AT);
6828 if (mips_trap)
6829 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6830 "s,t,q", dreg, AT, 6);
6831 else
6833 expr1.X_add_number = 8;
6834 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6835 AT);
6836 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6838 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6839 "c", 6);
6841 --mips_opts.noreorder;
6842 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6843 break;
6845 case M_DMULOU_I:
6846 dbl = 1;
6847 case M_MULOU_I:
6848 imm = 1;
6849 goto do_mulou;
6851 case M_DMULOU:
6852 dbl = 1;
6853 case M_MULOU:
6854 do_mulou:
6855 mips_emit_delays (true);
6856 ++mips_opts.noreorder;
6857 mips_any_noreorder = 1;
6858 if (imm)
6859 load_register (&icnt, AT, &imm_expr, dbl);
6860 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6861 dbl ? "dmultu" : "multu",
6862 "s,t", sreg, imm ? AT : treg);
6863 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6864 AT);
6865 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6866 dreg);
6867 if (mips_trap)
6868 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6869 "s,t,q", AT, 0, 6);
6870 else
6872 expr1.X_add_number = 8;
6873 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6874 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6876 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6877 "c", 6);
6879 --mips_opts.noreorder;
6880 break;
6882 case M_DROL:
6883 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6884 "d,v,t", AT, 0, treg);
6885 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6886 "d,t,s", AT, sreg, AT);
6887 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6888 "d,t,s", dreg, sreg, treg);
6889 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6890 "d,v,t", dreg, dreg, AT);
6891 break;
6893 case M_ROL:
6894 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6895 "d,v,t", AT, 0, treg);
6896 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6897 "d,t,s", AT, sreg, AT);
6898 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6899 "d,t,s", dreg, sreg, treg);
6900 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6901 "d,v,t", dreg, dreg, AT);
6902 break;
6904 case M_DROL_I:
6906 unsigned int rot;
6908 if (imm_expr.X_op != O_constant)
6909 as_bad (_("rotate count too large"));
6910 rot = imm_expr.X_add_number & 0x3f;
6911 if (CPU_HAS_DROR (mips_arch))
6913 rot = (64 - rot) & 0x3f;
6914 if (rot >= 32)
6915 macro_build ((char *) NULL, &icnt, NULL, "dror32",
6916 "d,w,<", dreg, sreg, rot - 32);
6917 else
6918 macro_build ((char *) NULL, &icnt, NULL, "dror",
6919 "d,w,<", dreg, sreg, rot);
6920 break;
6922 if (rot == 0)
6923 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6924 "d,w,<", dreg, sreg, 0);
6925 else
6927 char *l, *r;
6929 l = (rot < 0x20) ? "dsll" : "dsll32";
6930 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6931 rot &= 0x1f;
6932 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6933 "d,w,<", AT, sreg, rot);
6934 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6935 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6936 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6937 "d,v,t", dreg, dreg, AT);
6940 break;
6942 case M_ROL_I:
6944 unsigned int rot;
6946 if (imm_expr.X_op != O_constant)
6947 as_bad (_("rotate count too large"));
6948 rot = imm_expr.X_add_number & 0x1f;
6949 if (CPU_HAS_ROR (mips_arch))
6951 macro_build ((char *) NULL, &icnt, NULL, "ror",
6952 "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
6953 break;
6955 if (rot == 0)
6956 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6957 "d,w,<", dreg, sreg, 0);
6958 else
6960 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6961 "d,w,<", AT, sreg, rot);
6962 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6963 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6964 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6965 "d,v,t", dreg, dreg, AT);
6968 break;
6970 case M_DROR:
6971 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6972 "d,v,t", AT, 0, treg);
6973 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6974 "d,t,s", AT, sreg, AT);
6975 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6976 "d,t,s", dreg, sreg, treg);
6977 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6978 "d,v,t", dreg, dreg, AT);
6979 break;
6981 case M_ROR:
6982 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6983 "d,v,t", AT, 0, treg);
6984 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6985 "d,t,s", AT, sreg, AT);
6986 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6987 "d,t,s", dreg, sreg, treg);
6988 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6989 "d,v,t", dreg, dreg, AT);
6990 break;
6992 case M_DROR_I:
6994 unsigned int rot;
6996 if (imm_expr.X_op != O_constant)
6997 as_bad (_("rotate count too large"));
6998 rot = imm_expr.X_add_number & 0x3f;
6999 if (rot == 0)
7000 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7001 "d,w,<", dreg, sreg, 0);
7002 else
7004 char *l, *r;
7006 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7007 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7008 rot &= 0x1f;
7009 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7010 "d,w,<", AT, sreg, rot);
7011 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7012 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7013 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7014 "d,v,t", dreg, dreg, AT);
7017 break;
7019 case M_ROR_I:
7021 unsigned int rot;
7023 if (imm_expr.X_op != O_constant)
7024 as_bad (_("rotate count too large"));
7025 rot = imm_expr.X_add_number & 0x1f;
7026 if (rot == 0)
7027 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7028 "d,w,<", dreg, sreg, 0);
7029 else
7031 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7032 "d,w,<", AT, sreg, rot);
7033 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7034 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7035 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7036 "d,v,t", dreg, dreg, AT);
7039 break;
7041 case M_S_DOB:
7042 if (mips_arch == CPU_R4650)
7044 as_bad (_("opcode not supported on this processor"));
7045 return;
7047 assert (mips_opts.isa == ISA_MIPS1);
7048 /* Even on a big endian machine $fn comes before $fn+1. We have
7049 to adjust when storing to memory. */
7050 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7051 target_big_endian ? treg + 1 : treg,
7052 (int) BFD_RELOC_LO16, breg);
7053 offset_expr.X_add_number += 4;
7054 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7055 target_big_endian ? treg : treg + 1,
7056 (int) BFD_RELOC_LO16, breg);
7057 return;
7059 case M_SEQ:
7060 if (sreg == 0)
7061 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7062 treg, (int) BFD_RELOC_LO16);
7063 else if (treg == 0)
7064 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7065 sreg, (int) BFD_RELOC_LO16);
7066 else
7068 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7069 "d,v,t", dreg, sreg, treg);
7070 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7071 dreg, (int) BFD_RELOC_LO16);
7073 return;
7075 case M_SEQ_I:
7076 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7078 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7079 sreg, (int) BFD_RELOC_LO16);
7080 return;
7082 if (sreg == 0)
7084 as_warn (_("Instruction %s: result is always false"),
7085 ip->insn_mo->name);
7086 move_register (&icnt, dreg, 0);
7087 return;
7089 if (imm_expr.X_op == O_constant
7090 && imm_expr.X_add_number >= 0
7091 && imm_expr.X_add_number < 0x10000)
7093 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7094 sreg, (int) BFD_RELOC_LO16);
7095 used_at = 0;
7097 else if (imm_expr.X_op == O_constant
7098 && imm_expr.X_add_number > -0x8000
7099 && imm_expr.X_add_number < 0)
7101 imm_expr.X_add_number = -imm_expr.X_add_number;
7102 macro_build ((char *) NULL, &icnt, &imm_expr,
7103 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7104 "t,r,j", dreg, sreg,
7105 (int) BFD_RELOC_LO16);
7106 used_at = 0;
7108 else
7110 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7111 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7112 "d,v,t", dreg, sreg, AT);
7113 used_at = 1;
7115 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7116 (int) BFD_RELOC_LO16);
7117 if (used_at)
7118 break;
7119 return;
7121 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7122 s = "slt";
7123 goto sge;
7124 case M_SGEU:
7125 s = "sltu";
7126 sge:
7127 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7128 dreg, sreg, treg);
7129 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7130 (int) BFD_RELOC_LO16);
7131 return;
7133 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7134 case M_SGEU_I:
7135 if (imm_expr.X_op == O_constant
7136 && imm_expr.X_add_number >= -0x8000
7137 && imm_expr.X_add_number < 0x8000)
7139 macro_build ((char *) NULL, &icnt, &imm_expr,
7140 mask == M_SGE_I ? "slti" : "sltiu",
7141 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7142 used_at = 0;
7144 else
7146 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7147 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7148 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7149 AT);
7150 used_at = 1;
7152 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7153 (int) BFD_RELOC_LO16);
7154 if (used_at)
7155 break;
7156 return;
7158 case M_SGT: /* sreg > treg <==> treg < sreg */
7159 s = "slt";
7160 goto sgt;
7161 case M_SGTU:
7162 s = "sltu";
7163 sgt:
7164 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7165 dreg, treg, sreg);
7166 return;
7168 case M_SGT_I: /* sreg > I <==> I < sreg */
7169 s = "slt";
7170 goto sgti;
7171 case M_SGTU_I:
7172 s = "sltu";
7173 sgti:
7174 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7175 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7176 dreg, AT, sreg);
7177 break;
7179 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7180 s = "slt";
7181 goto sle;
7182 case M_SLEU:
7183 s = "sltu";
7184 sle:
7185 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7186 dreg, treg, sreg);
7187 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7188 (int) BFD_RELOC_LO16);
7189 return;
7191 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7192 s = "slt";
7193 goto slei;
7194 case M_SLEU_I:
7195 s = "sltu";
7196 slei:
7197 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7198 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7199 dreg, AT, sreg);
7200 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7201 (int) BFD_RELOC_LO16);
7202 break;
7204 case M_SLT_I:
7205 if (imm_expr.X_op == O_constant
7206 && imm_expr.X_add_number >= -0x8000
7207 && imm_expr.X_add_number < 0x8000)
7209 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7210 dreg, sreg, (int) BFD_RELOC_LO16);
7211 return;
7213 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7214 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7215 dreg, sreg, AT);
7216 break;
7218 case M_SLTU_I:
7219 if (imm_expr.X_op == O_constant
7220 && imm_expr.X_add_number >= -0x8000
7221 && imm_expr.X_add_number < 0x8000)
7223 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7224 dreg, sreg, (int) BFD_RELOC_LO16);
7225 return;
7227 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7228 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7229 "d,v,t", dreg, sreg, AT);
7230 break;
7232 case M_SNE:
7233 if (sreg == 0)
7234 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7235 "d,v,t", dreg, 0, treg);
7236 else if (treg == 0)
7237 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7238 "d,v,t", dreg, 0, sreg);
7239 else
7241 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7242 "d,v,t", dreg, sreg, treg);
7243 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7244 "d,v,t", dreg, 0, dreg);
7246 return;
7248 case M_SNE_I:
7249 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7251 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7252 "d,v,t", dreg, 0, sreg);
7253 return;
7255 if (sreg == 0)
7257 as_warn (_("Instruction %s: result is always true"),
7258 ip->insn_mo->name);
7259 macro_build ((char *) NULL, &icnt, &expr1,
7260 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7261 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7262 return;
7264 if (imm_expr.X_op == O_constant
7265 && imm_expr.X_add_number >= 0
7266 && imm_expr.X_add_number < 0x10000)
7268 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7269 dreg, sreg, (int) BFD_RELOC_LO16);
7270 used_at = 0;
7272 else if (imm_expr.X_op == O_constant
7273 && imm_expr.X_add_number > -0x8000
7274 && imm_expr.X_add_number < 0)
7276 imm_expr.X_add_number = -imm_expr.X_add_number;
7277 macro_build ((char *) NULL, &icnt, &imm_expr,
7278 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7279 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7280 used_at = 0;
7282 else
7284 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7285 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7286 "d,v,t", dreg, sreg, AT);
7287 used_at = 1;
7289 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7290 "d,v,t", dreg, 0, dreg);
7291 if (used_at)
7292 break;
7293 return;
7295 case M_DSUB_I:
7296 dbl = 1;
7297 case M_SUB_I:
7298 if (imm_expr.X_op == O_constant
7299 && imm_expr.X_add_number > -0x8000
7300 && imm_expr.X_add_number <= 0x8000)
7302 imm_expr.X_add_number = -imm_expr.X_add_number;
7303 macro_build ((char *) NULL, &icnt, &imm_expr,
7304 dbl ? "daddi" : "addi",
7305 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7306 return;
7308 load_register (&icnt, AT, &imm_expr, dbl);
7309 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7310 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7311 break;
7313 case M_DSUBU_I:
7314 dbl = 1;
7315 case M_SUBU_I:
7316 if (imm_expr.X_op == O_constant
7317 && imm_expr.X_add_number > -0x8000
7318 && imm_expr.X_add_number <= 0x8000)
7320 imm_expr.X_add_number = -imm_expr.X_add_number;
7321 macro_build ((char *) NULL, &icnt, &imm_expr,
7322 dbl ? "daddiu" : "addiu",
7323 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7324 return;
7326 load_register (&icnt, AT, &imm_expr, dbl);
7327 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7328 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7329 break;
7331 case M_TEQ_I:
7332 s = "teq";
7333 goto trap;
7334 case M_TGE_I:
7335 s = "tge";
7336 goto trap;
7337 case M_TGEU_I:
7338 s = "tgeu";
7339 goto trap;
7340 case M_TLT_I:
7341 s = "tlt";
7342 goto trap;
7343 case M_TLTU_I:
7344 s = "tltu";
7345 goto trap;
7346 case M_TNE_I:
7347 s = "tne";
7348 trap:
7349 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7350 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7351 AT);
7352 break;
7354 case M_TRUNCWS:
7355 case M_TRUNCWD:
7356 assert (mips_opts.isa == ISA_MIPS1);
7357 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7358 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7361 * Is the double cfc1 instruction a bug in the mips assembler;
7362 * or is there a reason for it?
7364 mips_emit_delays (true);
7365 ++mips_opts.noreorder;
7366 mips_any_noreorder = 1;
7367 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7368 treg, RA);
7369 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7370 treg, RA);
7371 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7372 expr1.X_add_number = 3;
7373 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7374 (int) BFD_RELOC_LO16);
7375 expr1.X_add_number = 2;
7376 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7377 (int) BFD_RELOC_LO16);
7378 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7379 AT, RA);
7380 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7381 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7382 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7383 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7384 treg, RA);
7385 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7386 --mips_opts.noreorder;
7387 break;
7389 case M_ULH:
7390 s = "lb";
7391 goto ulh;
7392 case M_ULHU:
7393 s = "lbu";
7394 ulh:
7395 if (offset_expr.X_add_number >= 0x7fff)
7396 as_bad (_("operand overflow"));
7397 /* avoid load delay */
7398 if (! target_big_endian)
7399 ++offset_expr.X_add_number;
7400 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7401 (int) BFD_RELOC_LO16, breg);
7402 if (! target_big_endian)
7403 --offset_expr.X_add_number;
7404 else
7405 ++offset_expr.X_add_number;
7406 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7407 (int) BFD_RELOC_LO16, breg);
7408 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7409 treg, treg, 8);
7410 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7411 treg, treg, AT);
7412 break;
7414 case M_ULD:
7415 s = "ldl";
7416 s2 = "ldr";
7417 off = 7;
7418 goto ulw;
7419 case M_ULW:
7420 s = "lwl";
7421 s2 = "lwr";
7422 off = 3;
7423 ulw:
7424 if (offset_expr.X_add_number >= 0x8000 - off)
7425 as_bad (_("operand overflow"));
7426 if (! target_big_endian)
7427 offset_expr.X_add_number += off;
7428 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7429 (int) BFD_RELOC_LO16, breg);
7430 if (! target_big_endian)
7431 offset_expr.X_add_number -= off;
7432 else
7433 offset_expr.X_add_number += off;
7434 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7435 (int) BFD_RELOC_LO16, breg);
7436 return;
7438 case M_ULD_A:
7439 s = "ldl";
7440 s2 = "ldr";
7441 off = 7;
7442 goto ulwa;
7443 case M_ULW_A:
7444 s = "lwl";
7445 s2 = "lwr";
7446 off = 3;
7447 ulwa:
7448 used_at = 1;
7449 load_address (&icnt, AT, &offset_expr, &used_at);
7450 if (breg != 0)
7451 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7452 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7453 "d,v,t", AT, AT, breg);
7454 if (! target_big_endian)
7455 expr1.X_add_number = off;
7456 else
7457 expr1.X_add_number = 0;
7458 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7459 (int) BFD_RELOC_LO16, AT);
7460 if (! target_big_endian)
7461 expr1.X_add_number = 0;
7462 else
7463 expr1.X_add_number = off;
7464 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7465 (int) BFD_RELOC_LO16, AT);
7466 break;
7468 case M_ULH_A:
7469 case M_ULHU_A:
7470 used_at = 1;
7471 load_address (&icnt, AT, &offset_expr, &used_at);
7472 if (breg != 0)
7473 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7474 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7475 "d,v,t", AT, AT, breg);
7476 if (target_big_endian)
7477 expr1.X_add_number = 0;
7478 macro_build ((char *) NULL, &icnt, &expr1,
7479 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7480 (int) BFD_RELOC_LO16, AT);
7481 if (target_big_endian)
7482 expr1.X_add_number = 1;
7483 else
7484 expr1.X_add_number = 0;
7485 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7486 (int) BFD_RELOC_LO16, AT);
7487 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7488 treg, treg, 8);
7489 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7490 treg, treg, AT);
7491 break;
7493 case M_USH:
7494 if (offset_expr.X_add_number >= 0x7fff)
7495 as_bad (_("operand overflow"));
7496 if (target_big_endian)
7497 ++offset_expr.X_add_number;
7498 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7499 (int) BFD_RELOC_LO16, breg);
7500 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7501 AT, treg, 8);
7502 if (target_big_endian)
7503 --offset_expr.X_add_number;
7504 else
7505 ++offset_expr.X_add_number;
7506 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7507 (int) BFD_RELOC_LO16, breg);
7508 break;
7510 case M_USD:
7511 s = "sdl";
7512 s2 = "sdr";
7513 off = 7;
7514 goto usw;
7515 case M_USW:
7516 s = "swl";
7517 s2 = "swr";
7518 off = 3;
7519 usw:
7520 if (offset_expr.X_add_number >= 0x8000 - off)
7521 as_bad (_("operand overflow"));
7522 if (! target_big_endian)
7523 offset_expr.X_add_number += off;
7524 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7525 (int) BFD_RELOC_LO16, breg);
7526 if (! target_big_endian)
7527 offset_expr.X_add_number -= off;
7528 else
7529 offset_expr.X_add_number += off;
7530 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7531 (int) BFD_RELOC_LO16, breg);
7532 return;
7534 case M_USD_A:
7535 s = "sdl";
7536 s2 = "sdr";
7537 off = 7;
7538 goto uswa;
7539 case M_USW_A:
7540 s = "swl";
7541 s2 = "swr";
7542 off = 3;
7543 uswa:
7544 used_at = 1;
7545 load_address (&icnt, AT, &offset_expr, &used_at);
7546 if (breg != 0)
7547 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7548 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7549 "d,v,t", AT, AT, breg);
7550 if (! target_big_endian)
7551 expr1.X_add_number = off;
7552 else
7553 expr1.X_add_number = 0;
7554 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7555 (int) BFD_RELOC_LO16, AT);
7556 if (! target_big_endian)
7557 expr1.X_add_number = 0;
7558 else
7559 expr1.X_add_number = off;
7560 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7561 (int) BFD_RELOC_LO16, AT);
7562 break;
7564 case M_USH_A:
7565 used_at = 1;
7566 load_address (&icnt, AT, &offset_expr, &used_at);
7567 if (breg != 0)
7568 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7569 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7570 "d,v,t", AT, AT, breg);
7571 if (! target_big_endian)
7572 expr1.X_add_number = 0;
7573 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7574 (int) BFD_RELOC_LO16, AT);
7575 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7576 treg, treg, 8);
7577 if (! target_big_endian)
7578 expr1.X_add_number = 1;
7579 else
7580 expr1.X_add_number = 0;
7581 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7582 (int) BFD_RELOC_LO16, AT);
7583 if (! target_big_endian)
7584 expr1.X_add_number = 0;
7585 else
7586 expr1.X_add_number = 1;
7587 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7588 (int) BFD_RELOC_LO16, AT);
7589 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7590 treg, treg, 8);
7591 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7592 treg, treg, AT);
7593 break;
7595 default:
7596 /* FIXME: Check if this is one of the itbl macros, since they
7597 are added dynamically. */
7598 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7599 break;
7601 if (mips_opts.noat)
7602 as_warn (_("Macro used $at after \".set noat\""));
7605 /* Implement macros in mips16 mode. */
7607 static void
7608 mips16_macro (ip)
7609 struct mips_cl_insn *ip;
7611 int mask;
7612 int xreg, yreg, zreg, tmp;
7613 int icnt;
7614 expressionS expr1;
7615 int dbl;
7616 const char *s, *s2, *s3;
7618 mask = ip->insn_mo->mask;
7620 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7621 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7622 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7624 icnt = 0;
7626 expr1.X_op = O_constant;
7627 expr1.X_op_symbol = NULL;
7628 expr1.X_add_symbol = NULL;
7629 expr1.X_add_number = 1;
7631 dbl = 0;
7633 switch (mask)
7635 default:
7636 internalError ();
7638 case M_DDIV_3:
7639 dbl = 1;
7640 case M_DIV_3:
7641 s = "mflo";
7642 goto do_div3;
7643 case M_DREM_3:
7644 dbl = 1;
7645 case M_REM_3:
7646 s = "mfhi";
7647 do_div3:
7648 mips_emit_delays (true);
7649 ++mips_opts.noreorder;
7650 mips_any_noreorder = 1;
7651 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7652 dbl ? "ddiv" : "div",
7653 "0,x,y", xreg, yreg);
7654 expr1.X_add_number = 2;
7655 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7656 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7659 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7660 since that causes an overflow. We should do that as well,
7661 but I don't see how to do the comparisons without a temporary
7662 register. */
7663 --mips_opts.noreorder;
7664 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7665 break;
7667 case M_DIVU_3:
7668 s = "divu";
7669 s2 = "mflo";
7670 goto do_divu3;
7671 case M_REMU_3:
7672 s = "divu";
7673 s2 = "mfhi";
7674 goto do_divu3;
7675 case M_DDIVU_3:
7676 s = "ddivu";
7677 s2 = "mflo";
7678 goto do_divu3;
7679 case M_DREMU_3:
7680 s = "ddivu";
7681 s2 = "mfhi";
7682 do_divu3:
7683 mips_emit_delays (true);
7684 ++mips_opts.noreorder;
7685 mips_any_noreorder = 1;
7686 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7687 xreg, yreg);
7688 expr1.X_add_number = 2;
7689 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7690 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7691 "6", 7);
7692 --mips_opts.noreorder;
7693 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7694 break;
7696 case M_DMUL:
7697 dbl = 1;
7698 case M_MUL:
7699 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7700 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7701 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7702 zreg);
7703 return;
7705 case M_DSUBU_I:
7706 dbl = 1;
7707 goto do_subu;
7708 case M_SUBU_I:
7709 do_subu:
7710 if (imm_expr.X_op != O_constant)
7711 as_bad (_("Unsupported large constant"));
7712 imm_expr.X_add_number = -imm_expr.X_add_number;
7713 macro_build ((char *) NULL, &icnt, &imm_expr,
7714 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7715 break;
7717 case M_SUBU_I_2:
7718 if (imm_expr.X_op != O_constant)
7719 as_bad (_("Unsupported large constant"));
7720 imm_expr.X_add_number = -imm_expr.X_add_number;
7721 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7722 "x,k", xreg);
7723 break;
7725 case M_DSUBU_I_2:
7726 if (imm_expr.X_op != O_constant)
7727 as_bad (_("Unsupported large constant"));
7728 imm_expr.X_add_number = -imm_expr.X_add_number;
7729 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7730 "y,j", yreg);
7731 break;
7733 case M_BEQ:
7734 s = "cmp";
7735 s2 = "bteqz";
7736 goto do_branch;
7737 case M_BNE:
7738 s = "cmp";
7739 s2 = "btnez";
7740 goto do_branch;
7741 case M_BLT:
7742 s = "slt";
7743 s2 = "btnez";
7744 goto do_branch;
7745 case M_BLTU:
7746 s = "sltu";
7747 s2 = "btnez";
7748 goto do_branch;
7749 case M_BLE:
7750 s = "slt";
7751 s2 = "bteqz";
7752 goto do_reverse_branch;
7753 case M_BLEU:
7754 s = "sltu";
7755 s2 = "bteqz";
7756 goto do_reverse_branch;
7757 case M_BGE:
7758 s = "slt";
7759 s2 = "bteqz";
7760 goto do_branch;
7761 case M_BGEU:
7762 s = "sltu";
7763 s2 = "bteqz";
7764 goto do_branch;
7765 case M_BGT:
7766 s = "slt";
7767 s2 = "btnez";
7768 goto do_reverse_branch;
7769 case M_BGTU:
7770 s = "sltu";
7771 s2 = "btnez";
7773 do_reverse_branch:
7774 tmp = xreg;
7775 xreg = yreg;
7776 yreg = tmp;
7778 do_branch:
7779 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7780 xreg, yreg);
7781 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7782 break;
7784 case M_BEQ_I:
7785 s = "cmpi";
7786 s2 = "bteqz";
7787 s3 = "x,U";
7788 goto do_branch_i;
7789 case M_BNE_I:
7790 s = "cmpi";
7791 s2 = "btnez";
7792 s3 = "x,U";
7793 goto do_branch_i;
7794 case M_BLT_I:
7795 s = "slti";
7796 s2 = "btnez";
7797 s3 = "x,8";
7798 goto do_branch_i;
7799 case M_BLTU_I:
7800 s = "sltiu";
7801 s2 = "btnez";
7802 s3 = "x,8";
7803 goto do_branch_i;
7804 case M_BLE_I:
7805 s = "slti";
7806 s2 = "btnez";
7807 s3 = "x,8";
7808 goto do_addone_branch_i;
7809 case M_BLEU_I:
7810 s = "sltiu";
7811 s2 = "btnez";
7812 s3 = "x,8";
7813 goto do_addone_branch_i;
7814 case M_BGE_I:
7815 s = "slti";
7816 s2 = "bteqz";
7817 s3 = "x,8";
7818 goto do_branch_i;
7819 case M_BGEU_I:
7820 s = "sltiu";
7821 s2 = "bteqz";
7822 s3 = "x,8";
7823 goto do_branch_i;
7824 case M_BGT_I:
7825 s = "slti";
7826 s2 = "bteqz";
7827 s3 = "x,8";
7828 goto do_addone_branch_i;
7829 case M_BGTU_I:
7830 s = "sltiu";
7831 s2 = "bteqz";
7832 s3 = "x,8";
7834 do_addone_branch_i:
7835 if (imm_expr.X_op != O_constant)
7836 as_bad (_("Unsupported large constant"));
7837 ++imm_expr.X_add_number;
7839 do_branch_i:
7840 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7841 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7842 break;
7844 case M_ABS:
7845 expr1.X_add_number = 0;
7846 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7847 if (xreg != yreg)
7848 move_register (&icnt, xreg, yreg);
7849 expr1.X_add_number = 2;
7850 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7851 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7852 "neg", "x,w", xreg, xreg);
7856 /* For consistency checking, verify that all bits are specified either
7857 by the match/mask part of the instruction definition, or by the
7858 operand list. */
7859 static int
7860 validate_mips_insn (opc)
7861 const struct mips_opcode *opc;
7863 const char *p = opc->args;
7864 char c;
7865 unsigned long used_bits = opc->mask;
7867 if ((used_bits & opc->match) != opc->match)
7869 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7870 opc->name, opc->args);
7871 return 0;
7873 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7874 while (*p)
7875 switch (c = *p++)
7877 case ',': break;
7878 case '(': break;
7879 case ')': break;
7880 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7881 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7882 case 'A': break;
7883 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7884 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7885 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7886 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7887 case 'F': break;
7888 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7889 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7890 case 'I': break;
7891 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7892 case 'L': break;
7893 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7894 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7895 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7896 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7897 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7898 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7899 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7900 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7901 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7902 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7903 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7904 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7905 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7906 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7907 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7908 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7909 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7910 case 'f': break;
7911 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7912 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7913 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7914 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7915 case 'l': break;
7916 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7917 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7918 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7919 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7920 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7921 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7922 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7923 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7924 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7925 case 'x': break;
7926 case 'z': break;
7927 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7928 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7929 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7930 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
7931 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
7932 case '[': break;
7933 case ']': break;
7934 default:
7935 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7936 c, opc->name, opc->args);
7937 return 0;
7939 #undef USE_BITS
7940 if (used_bits != 0xffffffff)
7942 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7943 ~used_bits & 0xffffffff, opc->name, opc->args);
7944 return 0;
7946 return 1;
7949 /* This routine assembles an instruction into its binary format. As a
7950 side effect, it sets one of the global variables imm_reloc or
7951 offset_reloc to the type of relocation to do if one of the operands
7952 is an address expression. */
7954 static void
7955 mips_ip (str, ip)
7956 char *str;
7957 struct mips_cl_insn *ip;
7959 char *s;
7960 const char *args;
7961 char c = 0;
7962 struct mips_opcode *insn;
7963 char *argsStart;
7964 unsigned int regno;
7965 unsigned int lastregno = 0;
7966 char *s_reset;
7967 char save_c = 0;
7969 insn_error = NULL;
7971 /* If the instruction contains a '.', we first try to match an instruction
7972 including the '.'. Then we try again without the '.'. */
7973 insn = NULL;
7974 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7975 continue;
7977 /* If we stopped on whitespace, then replace the whitespace with null for
7978 the call to hash_find. Save the character we replaced just in case we
7979 have to re-parse the instruction. */
7980 if (ISSPACE (*s))
7982 save_c = *s;
7983 *s++ = '\0';
7986 insn = (struct mips_opcode *) hash_find (op_hash, str);
7988 /* If we didn't find the instruction in the opcode table, try again, but
7989 this time with just the instruction up to, but not including the
7990 first '.'. */
7991 if (insn == NULL)
7993 /* Restore the character we overwrite above (if any). */
7994 if (save_c)
7995 *(--s) = save_c;
7997 /* Scan up to the first '.' or whitespace. */
7998 for (s = str;
7999 *s != '\0' && *s != '.' && !ISSPACE (*s);
8000 ++s)
8001 continue;
8003 /* If we did not find a '.', then we can quit now. */
8004 if (*s != '.')
8006 insn_error = "unrecognized opcode";
8007 return;
8010 /* Lookup the instruction in the hash table. */
8011 *s++ = '\0';
8012 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8014 insn_error = "unrecognized opcode";
8015 return;
8019 argsStart = s;
8020 for (;;)
8022 boolean ok;
8024 assert (strcmp (insn->name, str) == 0);
8026 if (OPCODE_IS_MEMBER (insn,
8027 (mips_opts.isa
8028 | (file_ase_mips16 ? INSN_MIPS16 : 0)
8029 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8030 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8031 mips_arch))
8032 ok = true;
8033 else
8034 ok = false;
8036 if (insn->pinfo != INSN_MACRO)
8038 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8039 ok = false;
8042 if (! ok)
8044 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8045 && strcmp (insn->name, insn[1].name) == 0)
8047 ++insn;
8048 continue;
8050 else
8052 if (!insn_error)
8054 static char buf[100];
8055 if (mips_arch_info->is_isa)
8056 sprintf (buf,
8057 _("opcode not supported at this ISA level (%s)"),
8058 mips_cpu_info_from_isa (mips_opts.isa)->name);
8059 else
8060 sprintf (buf,
8061 _("opcode not supported on this processor: %s (%s)"),
8062 mips_arch_info->name,
8063 mips_cpu_info_from_isa (mips_opts.isa)->name);
8064 insn_error = buf;
8066 if (save_c)
8067 *(--s) = save_c;
8068 return;
8072 ip->insn_mo = insn;
8073 ip->insn_opcode = insn->match;
8074 insn_error = NULL;
8075 for (args = insn->args;; ++args)
8077 int is_mdmx;
8079 s += strspn (s, " \t");
8080 is_mdmx = 0;
8081 switch (*args)
8083 case '\0': /* end of args */
8084 if (*s == '\0')
8085 return;
8086 break;
8088 case ',':
8089 if (*s++ == *args)
8090 continue;
8091 s--;
8092 switch (*++args)
8094 case 'r':
8095 case 'v':
8096 ip->insn_opcode |= lastregno << OP_SH_RS;
8097 continue;
8099 case 'w':
8100 ip->insn_opcode |= lastregno << OP_SH_RT;
8101 continue;
8103 case 'W':
8104 ip->insn_opcode |= lastregno << OP_SH_FT;
8105 continue;
8107 case 'V':
8108 ip->insn_opcode |= lastregno << OP_SH_FS;
8109 continue;
8111 break;
8113 case '(':
8114 /* Handle optional base register.
8115 Either the base register is omitted or
8116 we must have a left paren. */
8117 /* This is dependent on the next operand specifier
8118 is a base register specification. */
8119 assert (args[1] == 'b' || args[1] == '5'
8120 || args[1] == '-' || args[1] == '4');
8121 if (*s == '\0')
8122 return;
8124 case ')': /* these must match exactly */
8125 case '[':
8126 case ']':
8127 if (*s++ == *args)
8128 continue;
8129 break;
8131 case '<': /* must be at least one digit */
8133 * According to the manual, if the shift amount is greater
8134 * than 31 or less than 0, then the shift amount should be
8135 * mod 32. In reality the mips assembler issues an error.
8136 * We issue a warning and mask out all but the low 5 bits.
8138 my_getExpression (&imm_expr, s);
8139 check_absolute_expr (ip, &imm_expr);
8140 if ((unsigned long) imm_expr.X_add_number > 31)
8142 as_warn (_("Improper shift amount (%lu)"),
8143 (unsigned long) imm_expr.X_add_number);
8144 imm_expr.X_add_number &= OP_MASK_SHAMT;
8146 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8147 imm_expr.X_op = O_absent;
8148 s = expr_end;
8149 continue;
8151 case '>': /* shift amount minus 32 */
8152 my_getExpression (&imm_expr, s);
8153 check_absolute_expr (ip, &imm_expr);
8154 if ((unsigned long) imm_expr.X_add_number < 32
8155 || (unsigned long) imm_expr.X_add_number > 63)
8156 break;
8157 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8158 imm_expr.X_op = O_absent;
8159 s = expr_end;
8160 continue;
8162 case 'k': /* cache code */
8163 case 'h': /* prefx code */
8164 my_getExpression (&imm_expr, s);
8165 check_absolute_expr (ip, &imm_expr);
8166 if ((unsigned long) imm_expr.X_add_number > 31)
8168 as_warn (_("Invalid value for `%s' (%lu)"),
8169 ip->insn_mo->name,
8170 (unsigned long) imm_expr.X_add_number);
8171 imm_expr.X_add_number &= 0x1f;
8173 if (*args == 'k')
8174 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8175 else
8176 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8177 imm_expr.X_op = O_absent;
8178 s = expr_end;
8179 continue;
8181 case 'c': /* break code */
8182 my_getExpression (&imm_expr, s);
8183 check_absolute_expr (ip, &imm_expr);
8184 if ((unsigned long) imm_expr.X_add_number > 1023)
8186 as_warn (_("Illegal break code (%lu)"),
8187 (unsigned long) imm_expr.X_add_number);
8188 imm_expr.X_add_number &= OP_MASK_CODE;
8190 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8191 imm_expr.X_op = O_absent;
8192 s = expr_end;
8193 continue;
8195 case 'q': /* lower break code */
8196 my_getExpression (&imm_expr, s);
8197 check_absolute_expr (ip, &imm_expr);
8198 if ((unsigned long) imm_expr.X_add_number > 1023)
8200 as_warn (_("Illegal lower break code (%lu)"),
8201 (unsigned long) imm_expr.X_add_number);
8202 imm_expr.X_add_number &= OP_MASK_CODE2;
8204 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8205 imm_expr.X_op = O_absent;
8206 s = expr_end;
8207 continue;
8209 case 'B': /* 20-bit syscall/break code. */
8210 my_getExpression (&imm_expr, s);
8211 check_absolute_expr (ip, &imm_expr);
8212 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8213 as_warn (_("Illegal 20-bit code (%lu)"),
8214 (unsigned long) imm_expr.X_add_number);
8215 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8216 imm_expr.X_op = O_absent;
8217 s = expr_end;
8218 continue;
8220 case 'C': /* Coprocessor code */
8221 my_getExpression (&imm_expr, s);
8222 check_absolute_expr (ip, &imm_expr);
8223 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8225 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8226 (unsigned long) imm_expr.X_add_number);
8227 imm_expr.X_add_number &= ((1 << 25) - 1);
8229 ip->insn_opcode |= imm_expr.X_add_number;
8230 imm_expr.X_op = O_absent;
8231 s = expr_end;
8232 continue;
8234 case 'J': /* 19-bit wait code. */
8235 my_getExpression (&imm_expr, s);
8236 check_absolute_expr (ip, &imm_expr);
8237 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8238 as_warn (_("Illegal 19-bit code (%lu)"),
8239 (unsigned long) imm_expr.X_add_number);
8240 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8241 imm_expr.X_op = O_absent;
8242 s = expr_end;
8243 continue;
8245 case 'P': /* Performance register */
8246 my_getExpression (&imm_expr, s);
8247 check_absolute_expr (ip, &imm_expr);
8248 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8250 as_warn (_("Invalid performance register (%lu)"),
8251 (unsigned long) imm_expr.X_add_number);
8252 imm_expr.X_add_number &= OP_MASK_PERFREG;
8254 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8255 imm_expr.X_op = O_absent;
8256 s = expr_end;
8257 continue;
8259 case 'b': /* base register */
8260 case 'd': /* destination register */
8261 case 's': /* source register */
8262 case 't': /* target register */
8263 case 'r': /* both target and source */
8264 case 'v': /* both dest and source */
8265 case 'w': /* both dest and target */
8266 case 'E': /* coprocessor target register */
8267 case 'G': /* coprocessor destination register */
8268 case 'x': /* ignore register name */
8269 case 'z': /* must be zero register */
8270 case 'U': /* destination register (clo/clz). */
8271 s_reset = s;
8272 if (s[0] == '$')
8275 if (ISDIGIT (s[1]))
8277 ++s;
8278 regno = 0;
8281 regno *= 10;
8282 regno += *s - '0';
8283 ++s;
8285 while (ISDIGIT (*s));
8286 if (regno > 31)
8287 as_bad (_("Invalid register number (%d)"), regno);
8289 else if (*args == 'E' || *args == 'G')
8290 goto notreg;
8291 else
8293 if (s[1] == 'r' && s[2] == 'a')
8295 s += 3;
8296 regno = RA;
8298 else if (s[1] == 'f' && s[2] == 'p')
8300 s += 3;
8301 regno = FP;
8303 else if (s[1] == 's' && s[2] == 'p')
8305 s += 3;
8306 regno = SP;
8308 else if (s[1] == 'g' && s[2] == 'p')
8310 s += 3;
8311 regno = GP;
8313 else if (s[1] == 'a' && s[2] == 't')
8315 s += 3;
8316 regno = AT;
8318 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8320 s += 4;
8321 regno = KT0;
8323 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8325 s += 4;
8326 regno = KT1;
8328 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8330 s += 5;
8331 regno = ZERO;
8333 else if (itbl_have_entries)
8335 char *p, *n;
8336 unsigned long r;
8338 p = s + 1; /* advance past '$' */
8339 n = itbl_get_field (&p); /* n is name */
8341 /* See if this is a register defined in an
8342 itbl entry. */
8343 if (itbl_get_reg_val (n, &r))
8345 /* Get_field advances to the start of
8346 the next field, so we need to back
8347 rack to the end of the last field. */
8348 if (p)
8349 s = p - 1;
8350 else
8351 s = strchr (s, '\0');
8352 regno = r;
8354 else
8355 goto notreg;
8357 else
8358 goto notreg;
8360 if (regno == AT
8361 && ! mips_opts.noat
8362 && *args != 'E'
8363 && *args != 'G')
8364 as_warn (_("Used $at without \".set noat\""));
8365 c = *args;
8366 if (*s == ' ')
8367 ++s;
8368 if (args[1] != *s)
8370 if (c == 'r' || c == 'v' || c == 'w')
8372 regno = lastregno;
8373 s = s_reset;
8374 ++args;
8377 /* 'z' only matches $0. */
8378 if (c == 'z' && regno != 0)
8379 break;
8381 /* Now that we have assembled one operand, we use the args string
8382 * to figure out where it goes in the instruction. */
8383 switch (c)
8385 case 'r':
8386 case 's':
8387 case 'v':
8388 case 'b':
8389 ip->insn_opcode |= regno << OP_SH_RS;
8390 break;
8391 case 'd':
8392 case 'G':
8393 ip->insn_opcode |= regno << OP_SH_RD;
8394 break;
8395 case 'U':
8396 ip->insn_opcode |= regno << OP_SH_RD;
8397 ip->insn_opcode |= regno << OP_SH_RT;
8398 break;
8399 case 'w':
8400 case 't':
8401 case 'E':
8402 ip->insn_opcode |= regno << OP_SH_RT;
8403 break;
8404 case 'x':
8405 /* This case exists because on the r3000 trunc
8406 expands into a macro which requires a gp
8407 register. On the r6000 or r4000 it is
8408 assembled into a single instruction which
8409 ignores the register. Thus the insn version
8410 is MIPS_ISA2 and uses 'x', and the macro
8411 version is MIPS_ISA1 and uses 't'. */
8412 break;
8413 case 'z':
8414 /* This case is for the div instruction, which
8415 acts differently if the destination argument
8416 is $0. This only matches $0, and is checked
8417 outside the switch. */
8418 break;
8419 case 'D':
8420 /* Itbl operand; not yet implemented. FIXME ?? */
8421 break;
8422 /* What about all other operands like 'i', which
8423 can be specified in the opcode table? */
8425 lastregno = regno;
8426 continue;
8428 notreg:
8429 switch (*args++)
8431 case 'r':
8432 case 'v':
8433 ip->insn_opcode |= lastregno << OP_SH_RS;
8434 continue;
8435 case 'w':
8436 ip->insn_opcode |= lastregno << OP_SH_RT;
8437 continue;
8439 break;
8441 case 'O': /* MDMX alignment immediate constant. */
8442 my_getExpression (&imm_expr, s);
8443 check_absolute_expr (ip, &imm_expr);
8444 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8446 as_warn ("Improper align amount (%ld), using low bits",
8447 (long) imm_expr.X_add_number);
8448 imm_expr.X_add_number &= OP_MASK_ALN;
8450 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8451 imm_expr.X_op = O_absent;
8452 s = expr_end;
8453 continue;
8455 case 'Q': /* MDMX vector, element sel, or const. */
8456 if (s[0] != '$')
8458 /* MDMX Immediate. */
8459 my_getExpression (&imm_expr, s);
8460 check_absolute_expr (ip, &imm_expr);
8461 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8463 as_warn (_("Invalid MDMX Immediate (%ld)"),
8464 (long) imm_expr.X_add_number);
8465 imm_expr.X_add_number &= OP_MASK_FT;
8467 imm_expr.X_add_number &= OP_MASK_FT;
8468 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8469 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8470 else
8471 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8472 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8473 imm_expr.X_op = O_absent;
8474 s = expr_end;
8475 continue;
8477 /* Not MDMX Immediate. Fall through. */
8478 case 'X': /* MDMX destination register. */
8479 case 'Y': /* MDMX source register. */
8480 case 'Z': /* MDMX target register. */
8481 is_mdmx = 1;
8482 case 'D': /* floating point destination register */
8483 case 'S': /* floating point source register */
8484 case 'T': /* floating point target register */
8485 case 'R': /* floating point source register */
8486 case 'V':
8487 case 'W':
8488 s_reset = s;
8489 /* Accept $fN for FP and MDMX register numbers, and in
8490 addition accept $vN for MDMX register numbers. */
8491 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8492 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8493 && ISDIGIT (s[2])))
8495 s += 2;
8496 regno = 0;
8499 regno *= 10;
8500 regno += *s - '0';
8501 ++s;
8503 while (ISDIGIT (*s));
8505 if (regno > 31)
8506 as_bad (_("Invalid float register number (%d)"), regno);
8508 if ((regno & 1) != 0
8509 && HAVE_32BIT_FPRS
8510 && ! (strcmp (str, "mtc1") == 0
8511 || strcmp (str, "mfc1") == 0
8512 || strcmp (str, "lwc1") == 0
8513 || strcmp (str, "swc1") == 0
8514 || strcmp (str, "l.s") == 0
8515 || strcmp (str, "s.s") == 0))
8516 as_warn (_("Float register should be even, was %d"),
8517 regno);
8519 c = *args;
8520 if (*s == ' ')
8521 ++s;
8522 if (args[1] != *s)
8524 if (c == 'V' || c == 'W')
8526 regno = lastregno;
8527 s = s_reset;
8528 ++args;
8531 switch (c)
8533 case 'D':
8534 case 'X':
8535 ip->insn_opcode |= regno << OP_SH_FD;
8536 break;
8537 case 'V':
8538 case 'S':
8539 case 'Y':
8540 ip->insn_opcode |= regno << OP_SH_FS;
8541 break;
8542 case 'Q':
8543 /* This is like 'Z', but also needs to fix the MDMX
8544 vector/scalar select bits. Note that the
8545 scalar immediate case is handled above. */
8546 if (*s == '[')
8548 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8549 int max_el = (is_qh ? 3 : 7);
8550 s++;
8551 my_getExpression(&imm_expr, s);
8552 check_absolute_expr (ip, &imm_expr);
8553 s = expr_end;
8554 if (imm_expr.X_add_number > max_el)
8555 as_bad(_("Bad element selector %ld"),
8556 (long) imm_expr.X_add_number);
8557 imm_expr.X_add_number &= max_el;
8558 ip->insn_opcode |= (imm_expr.X_add_number
8559 << (OP_SH_VSEL +
8560 (is_qh ? 2 : 1)));
8561 if (*s != ']')
8562 as_warn(_("Expecting ']' found '%s'"), s);
8563 else
8564 s++;
8566 else
8568 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8569 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8570 << OP_SH_VSEL);
8571 else
8572 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8573 OP_SH_VSEL);
8575 /* Fall through */
8576 case 'W':
8577 case 'T':
8578 case 'Z':
8579 ip->insn_opcode |= regno << OP_SH_FT;
8580 break;
8581 case 'R':
8582 ip->insn_opcode |= regno << OP_SH_FR;
8583 break;
8585 lastregno = regno;
8586 continue;
8589 switch (*args++)
8591 case 'V':
8592 ip->insn_opcode |= lastregno << OP_SH_FS;
8593 continue;
8594 case 'W':
8595 ip->insn_opcode |= lastregno << OP_SH_FT;
8596 continue;
8598 break;
8600 case 'I':
8601 my_getExpression (&imm_expr, s);
8602 if (imm_expr.X_op != O_big
8603 && imm_expr.X_op != O_constant)
8604 insn_error = _("absolute expression required");
8605 s = expr_end;
8606 continue;
8608 case 'A':
8609 my_getExpression (&offset_expr, s);
8610 *imm_reloc = BFD_RELOC_32;
8611 s = expr_end;
8612 continue;
8614 case 'F':
8615 case 'L':
8616 case 'f':
8617 case 'l':
8619 int f64;
8620 int using_gprs;
8621 char *save_in;
8622 char *err;
8623 unsigned char temp[8];
8624 int len;
8625 unsigned int length;
8626 segT seg;
8627 subsegT subseg;
8628 char *p;
8630 /* These only appear as the last operand in an
8631 instruction, and every instruction that accepts
8632 them in any variant accepts them in all variants.
8633 This means we don't have to worry about backing out
8634 any changes if the instruction does not match.
8636 The difference between them is the size of the
8637 floating point constant and where it goes. For 'F'
8638 and 'L' the constant is 64 bits; for 'f' and 'l' it
8639 is 32 bits. Where the constant is placed is based
8640 on how the MIPS assembler does things:
8641 F -- .rdata
8642 L -- .lit8
8643 f -- immediate value
8644 l -- .lit4
8646 The .lit4 and .lit8 sections are only used if
8647 permitted by the -G argument.
8649 When generating embedded PIC code, we use the
8650 .lit8 section but not the .lit4 section (we can do
8651 .lit4 inline easily; we need to put .lit8
8652 somewhere in the data segment, and using .lit8
8653 permits the linker to eventually combine identical
8654 .lit8 entries).
8656 The code below needs to know whether the target register
8657 is 32 or 64 bits wide. It relies on the fact 'f' and
8658 'F' are used with GPR-based instructions and 'l' and
8659 'L' are used with FPR-based instructions. */
8661 f64 = *args == 'F' || *args == 'L';
8662 using_gprs = *args == 'F' || *args == 'f';
8664 save_in = input_line_pointer;
8665 input_line_pointer = s;
8666 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8667 length = len;
8668 s = input_line_pointer;
8669 input_line_pointer = save_in;
8670 if (err != NULL && *err != '\0')
8672 as_bad (_("Bad floating point constant: %s"), err);
8673 memset (temp, '\0', sizeof temp);
8674 length = f64 ? 8 : 4;
8677 assert (length == (unsigned) (f64 ? 8 : 4));
8679 if (*args == 'f'
8680 || (*args == 'l'
8681 && (! USE_GLOBAL_POINTER_OPT
8682 || mips_pic == EMBEDDED_PIC
8683 || g_switch_value < 4
8684 || (temp[0] == 0 && temp[1] == 0)
8685 || (temp[2] == 0 && temp[3] == 0))))
8687 imm_expr.X_op = O_constant;
8688 if (! target_big_endian)
8689 imm_expr.X_add_number = bfd_getl32 (temp);
8690 else
8691 imm_expr.X_add_number = bfd_getb32 (temp);
8693 else if (length > 4
8694 && ! mips_disable_float_construction
8695 /* Constants can only be constructed in GPRs and
8696 copied to FPRs if the GPRs are at least as wide
8697 as the FPRs. Force the constant into memory if
8698 we are using 64-bit FPRs but the GPRs are only
8699 32 bits wide. */
8700 && (using_gprs
8701 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8702 && ((temp[0] == 0 && temp[1] == 0)
8703 || (temp[2] == 0 && temp[3] == 0))
8704 && ((temp[4] == 0 && temp[5] == 0)
8705 || (temp[6] == 0 && temp[7] == 0)))
8707 /* The value is simple enough to load with a couple of
8708 instructions. If using 32-bit registers, set
8709 imm_expr to the high order 32 bits and offset_expr to
8710 the low order 32 bits. Otherwise, set imm_expr to
8711 the entire 64 bit constant. */
8712 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8714 imm_expr.X_op = O_constant;
8715 offset_expr.X_op = O_constant;
8716 if (! target_big_endian)
8718 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8719 offset_expr.X_add_number = bfd_getl32 (temp);
8721 else
8723 imm_expr.X_add_number = bfd_getb32 (temp);
8724 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8726 if (offset_expr.X_add_number == 0)
8727 offset_expr.X_op = O_absent;
8729 else if (sizeof (imm_expr.X_add_number) > 4)
8731 imm_expr.X_op = O_constant;
8732 if (! target_big_endian)
8733 imm_expr.X_add_number = bfd_getl64 (temp);
8734 else
8735 imm_expr.X_add_number = bfd_getb64 (temp);
8737 else
8739 imm_expr.X_op = O_big;
8740 imm_expr.X_add_number = 4;
8741 if (! target_big_endian)
8743 generic_bignum[0] = bfd_getl16 (temp);
8744 generic_bignum[1] = bfd_getl16 (temp + 2);
8745 generic_bignum[2] = bfd_getl16 (temp + 4);
8746 generic_bignum[3] = bfd_getl16 (temp + 6);
8748 else
8750 generic_bignum[0] = bfd_getb16 (temp + 6);
8751 generic_bignum[1] = bfd_getb16 (temp + 4);
8752 generic_bignum[2] = bfd_getb16 (temp + 2);
8753 generic_bignum[3] = bfd_getb16 (temp);
8757 else
8759 const char *newname;
8760 segT new_seg;
8762 /* Switch to the right section. */
8763 seg = now_seg;
8764 subseg = now_subseg;
8765 switch (*args)
8767 default: /* unused default case avoids warnings. */
8768 case 'L':
8769 newname = RDATA_SECTION_NAME;
8770 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8771 || mips_pic == EMBEDDED_PIC)
8772 newname = ".lit8";
8773 break;
8774 case 'F':
8775 if (mips_pic == EMBEDDED_PIC)
8776 newname = ".lit8";
8777 else
8778 newname = RDATA_SECTION_NAME;
8779 break;
8780 case 'l':
8781 assert (!USE_GLOBAL_POINTER_OPT
8782 || g_switch_value >= 4);
8783 newname = ".lit4";
8784 break;
8786 new_seg = subseg_new (newname, (subsegT) 0);
8787 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8788 bfd_set_section_flags (stdoutput, new_seg,
8789 (SEC_ALLOC
8790 | SEC_LOAD
8791 | SEC_READONLY
8792 | SEC_DATA));
8793 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8794 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8795 && strcmp (TARGET_OS, "elf") != 0)
8796 record_alignment (new_seg, 4);
8797 else
8798 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8799 if (seg == now_seg)
8800 as_bad (_("Can't use floating point insn in this section"));
8802 /* Set the argument to the current address in the
8803 section. */
8804 offset_expr.X_op = O_symbol;
8805 offset_expr.X_add_symbol =
8806 symbol_new ("L0\001", now_seg,
8807 (valueT) frag_now_fix (), frag_now);
8808 offset_expr.X_add_number = 0;
8810 /* Put the floating point number into the section. */
8811 p = frag_more ((int) length);
8812 memcpy (p, temp, length);
8814 /* Switch back to the original section. */
8815 subseg_set (seg, subseg);
8818 continue;
8820 case 'i': /* 16 bit unsigned immediate */
8821 case 'j': /* 16 bit signed immediate */
8822 *imm_reloc = BFD_RELOC_LO16;
8823 c = my_getSmallExpression (&imm_expr, s);
8824 if (c != S_EX_NONE)
8826 if (c != S_EX_LO)
8828 if (c == S_EX_HI)
8830 *imm_reloc = BFD_RELOC_HI16_S;
8831 imm_unmatched_hi = true;
8833 #ifdef OBJ_ELF
8834 else if (c == S_EX_HIGHEST)
8835 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8836 else if (c == S_EX_HIGHER)
8837 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8838 else if (c == S_EX_GP_REL)
8840 /* This occurs in NewABI only. */
8841 c = my_getSmallExpression (&imm_expr, s);
8842 if (c != S_EX_NEG)
8843 as_bad (_("bad composition of relocations"));
8844 else
8846 c = my_getSmallExpression (&imm_expr, s);
8847 if (c != S_EX_LO)
8848 as_bad (_("bad composition of relocations"));
8849 else
8851 imm_reloc[0] = BFD_RELOC_GPREL16;
8852 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8853 imm_reloc[2] = BFD_RELOC_LO16;
8857 #endif
8858 else
8859 *imm_reloc = BFD_RELOC_HI16;
8861 else if (imm_expr.X_op == O_constant)
8862 imm_expr.X_add_number &= 0xffff;
8864 if (*args == 'i')
8866 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8867 || ((imm_expr.X_add_number < 0
8868 || imm_expr.X_add_number >= 0x10000)
8869 && imm_expr.X_op == O_constant))
8871 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8872 !strcmp (insn->name, insn[1].name))
8873 break;
8874 if (imm_expr.X_op == O_constant
8875 || imm_expr.X_op == O_big)
8876 as_bad (_("16 bit expression not in range 0..65535"));
8879 else
8881 int more;
8882 offsetT max;
8884 /* The upper bound should be 0x8000, but
8885 unfortunately the MIPS assembler accepts numbers
8886 from 0x8000 to 0xffff and sign extends them, and
8887 we want to be compatible. We only permit this
8888 extended range for an instruction which does not
8889 provide any further alternates, since those
8890 alternates may handle other cases. People should
8891 use the numbers they mean, rather than relying on
8892 a mysterious sign extension. */
8893 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8894 strcmp (insn->name, insn[1].name) == 0);
8895 if (more)
8896 max = 0x8000;
8897 else
8898 max = 0x10000;
8899 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8900 || ((imm_expr.X_add_number < -0x8000
8901 || imm_expr.X_add_number >= max)
8902 && imm_expr.X_op == O_constant)
8903 || (more
8904 && imm_expr.X_add_number < 0
8905 && HAVE_64BIT_GPRS
8906 && imm_expr.X_unsigned
8907 && sizeof (imm_expr.X_add_number) <= 4))
8909 if (more)
8910 break;
8911 if (imm_expr.X_op == O_constant
8912 || imm_expr.X_op == O_big)
8913 as_bad (_("16 bit expression not in range -32768..32767"));
8916 s = expr_end;
8917 continue;
8919 case 'o': /* 16 bit offset */
8920 c = my_getSmallExpression (&offset_expr, s);
8922 /* If this value won't fit into a 16 bit offset, then go
8923 find a macro that will generate the 32 bit offset
8924 code pattern. */
8925 if (c == S_EX_NONE
8926 && (offset_expr.X_op != O_constant
8927 || offset_expr.X_add_number >= 0x8000
8928 || offset_expr.X_add_number < -0x8000))
8929 break;
8931 if (c == S_EX_HI)
8933 if (offset_expr.X_op != O_constant)
8934 break;
8935 offset_expr.X_add_number =
8936 (offset_expr.X_add_number >> 16) & 0xffff;
8938 *offset_reloc = BFD_RELOC_LO16;
8939 s = expr_end;
8940 continue;
8942 case 'p': /* pc relative offset */
8943 if (mips_pic == EMBEDDED_PIC)
8944 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8945 else
8946 *offset_reloc = BFD_RELOC_16_PCREL;
8947 my_getExpression (&offset_expr, s);
8948 s = expr_end;
8949 continue;
8951 case 'u': /* upper 16 bits */
8952 c = my_getSmallExpression (&imm_expr, s);
8953 *imm_reloc = BFD_RELOC_LO16;
8954 if (c != S_EX_NONE)
8956 if (c != S_EX_LO)
8958 if (c == S_EX_HI)
8960 *imm_reloc = BFD_RELOC_HI16_S;
8961 imm_unmatched_hi = true;
8963 #ifdef OBJ_ELF
8964 else if (c == S_EX_HIGHEST)
8965 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8966 else if (c == S_EX_GP_REL)
8968 /* This occurs in NewABI only. */
8969 c = my_getSmallExpression (&imm_expr, s);
8970 if (c != S_EX_NEG)
8971 as_bad (_("bad composition of relocations"));
8972 else
8974 c = my_getSmallExpression (&imm_expr, s);
8975 if (c != S_EX_HI)
8976 as_bad (_("bad composition of relocations"));
8977 else
8979 imm_reloc[0] = BFD_RELOC_GPREL16;
8980 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8981 imm_reloc[2] = BFD_RELOC_HI16_S;
8985 #endif
8986 else
8987 *imm_reloc = BFD_RELOC_HI16;
8989 else if (imm_expr.X_op == O_constant)
8990 imm_expr.X_add_number &= 0xffff;
8992 else if (imm_expr.X_op == O_constant
8993 && (imm_expr.X_add_number < 0
8994 || imm_expr.X_add_number >= 0x10000))
8995 as_bad (_("lui expression not in range 0..65535"));
8996 s = expr_end;
8997 continue;
8999 case 'a': /* 26 bit address */
9000 my_getExpression (&offset_expr, s);
9001 s = expr_end;
9002 *offset_reloc = BFD_RELOC_MIPS_JMP;
9003 continue;
9005 case 'N': /* 3 bit branch condition code */
9006 case 'M': /* 3 bit compare condition code */
9007 if (strncmp (s, "$fcc", 4) != 0)
9008 break;
9009 s += 4;
9010 regno = 0;
9013 regno *= 10;
9014 regno += *s - '0';
9015 ++s;
9017 while (ISDIGIT (*s));
9018 if (regno > 7)
9019 as_bad (_("invalid condition code register $fcc%d"), regno);
9020 if (*args == 'N')
9021 ip->insn_opcode |= regno << OP_SH_BCC;
9022 else
9023 ip->insn_opcode |= regno << OP_SH_CCC;
9024 continue;
9026 case 'H':
9027 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9028 s += 2;
9029 if (ISDIGIT (*s))
9031 c = 0;
9034 c *= 10;
9035 c += *s - '0';
9036 ++s;
9038 while (ISDIGIT (*s));
9040 else
9041 c = 8; /* Invalid sel value. */
9043 if (c > 7)
9044 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9045 ip->insn_opcode |= c;
9046 continue;
9048 case 'e':
9049 /* Must be at least one digit. */
9050 my_getExpression (&imm_expr, s);
9051 check_absolute_expr (ip, &imm_expr);
9053 if ((unsigned long) imm_expr.X_add_number
9054 > (unsigned long) OP_MASK_VECBYTE)
9056 as_bad (_("bad byte vector index (%ld)"),
9057 (long) imm_expr.X_add_number);
9058 imm_expr.X_add_number = 0;
9061 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9062 imm_expr.X_op = O_absent;
9063 s = expr_end;
9064 continue;
9066 case '%':
9067 my_getExpression (&imm_expr, s);
9068 check_absolute_expr (ip, &imm_expr);
9070 if ((unsigned long) imm_expr.X_add_number
9071 > (unsigned long) OP_MASK_VECALIGN)
9073 as_bad (_("bad byte vector index (%ld)"),
9074 (long) imm_expr.X_add_number);
9075 imm_expr.X_add_number = 0;
9078 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9079 imm_expr.X_op = O_absent;
9080 s = expr_end;
9081 continue;
9083 default:
9084 as_bad (_("bad char = '%c'\n"), *args);
9085 internalError ();
9087 break;
9089 /* Args don't match. */
9090 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9091 !strcmp (insn->name, insn[1].name))
9093 ++insn;
9094 s = argsStart;
9095 insn_error = _("illegal operands");
9096 continue;
9098 if (save_c)
9099 *(--s) = save_c;
9100 insn_error = _("illegal operands");
9101 return;
9105 /* This routine assembles an instruction into its binary format when
9106 assembling for the mips16. As a side effect, it sets one of the
9107 global variables imm_reloc or offset_reloc to the type of
9108 relocation to do if one of the operands is an address expression.
9109 It also sets mips16_small and mips16_ext if the user explicitly
9110 requested a small or extended instruction. */
9112 static void
9113 mips16_ip (str, ip)
9114 char *str;
9115 struct mips_cl_insn *ip;
9117 char *s;
9118 const char *args;
9119 struct mips_opcode *insn;
9120 char *argsstart;
9121 unsigned int regno;
9122 unsigned int lastregno = 0;
9123 char *s_reset;
9125 insn_error = NULL;
9127 mips16_small = false;
9128 mips16_ext = false;
9130 for (s = str; ISLOWER (*s); ++s)
9132 switch (*s)
9134 case '\0':
9135 break;
9137 case ' ':
9138 *s++ = '\0';
9139 break;
9141 case '.':
9142 if (s[1] == 't' && s[2] == ' ')
9144 *s = '\0';
9145 mips16_small = true;
9146 s += 3;
9147 break;
9149 else if (s[1] == 'e' && s[2] == ' ')
9151 *s = '\0';
9152 mips16_ext = true;
9153 s += 3;
9154 break;
9156 /* Fall through. */
9157 default:
9158 insn_error = _("unknown opcode");
9159 return;
9162 if (mips_opts.noautoextend && ! mips16_ext)
9163 mips16_small = true;
9165 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9167 insn_error = _("unrecognized opcode");
9168 return;
9171 argsstart = s;
9172 for (;;)
9174 assert (strcmp (insn->name, str) == 0);
9176 ip->insn_mo = insn;
9177 ip->insn_opcode = insn->match;
9178 ip->use_extend = false;
9179 imm_expr.X_op = O_absent;
9180 imm_reloc[0] = BFD_RELOC_UNUSED;
9181 imm_reloc[1] = BFD_RELOC_UNUSED;
9182 imm_reloc[2] = BFD_RELOC_UNUSED;
9183 offset_expr.X_op = O_absent;
9184 offset_reloc[0] = BFD_RELOC_UNUSED;
9185 offset_reloc[1] = BFD_RELOC_UNUSED;
9186 offset_reloc[2] = BFD_RELOC_UNUSED;
9187 for (args = insn->args; 1; ++args)
9189 int c;
9191 if (*s == ' ')
9192 ++s;
9194 /* In this switch statement we call break if we did not find
9195 a match, continue if we did find a match, or return if we
9196 are done. */
9198 c = *args;
9199 switch (c)
9201 case '\0':
9202 if (*s == '\0')
9204 /* Stuff the immediate value in now, if we can. */
9205 if (imm_expr.X_op == O_constant
9206 && *imm_reloc > BFD_RELOC_UNUSED
9207 && insn->pinfo != INSN_MACRO)
9209 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9210 imm_expr.X_add_number, true, mips16_small,
9211 mips16_ext, &ip->insn_opcode,
9212 &ip->use_extend, &ip->extend);
9213 imm_expr.X_op = O_absent;
9214 *imm_reloc = BFD_RELOC_UNUSED;
9217 return;
9219 break;
9221 case ',':
9222 if (*s++ == c)
9223 continue;
9224 s--;
9225 switch (*++args)
9227 case 'v':
9228 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9229 continue;
9230 case 'w':
9231 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9232 continue;
9234 break;
9236 case '(':
9237 case ')':
9238 if (*s++ == c)
9239 continue;
9240 break;
9242 case 'v':
9243 case 'w':
9244 if (s[0] != '$')
9246 if (c == 'v')
9247 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9248 else
9249 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9250 ++args;
9251 continue;
9253 /* Fall through. */
9254 case 'x':
9255 case 'y':
9256 case 'z':
9257 case 'Z':
9258 case '0':
9259 case 'S':
9260 case 'R':
9261 case 'X':
9262 case 'Y':
9263 if (s[0] != '$')
9264 break;
9265 s_reset = s;
9266 if (ISDIGIT (s[1]))
9268 ++s;
9269 regno = 0;
9272 regno *= 10;
9273 regno += *s - '0';
9274 ++s;
9276 while (ISDIGIT (*s));
9277 if (regno > 31)
9279 as_bad (_("invalid register number (%d)"), regno);
9280 regno = 2;
9283 else
9285 if (s[1] == 'r' && s[2] == 'a')
9287 s += 3;
9288 regno = RA;
9290 else if (s[1] == 'f' && s[2] == 'p')
9292 s += 3;
9293 regno = FP;
9295 else if (s[1] == 's' && s[2] == 'p')
9297 s += 3;
9298 regno = SP;
9300 else if (s[1] == 'g' && s[2] == 'p')
9302 s += 3;
9303 regno = GP;
9305 else if (s[1] == 'a' && s[2] == 't')
9307 s += 3;
9308 regno = AT;
9310 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9312 s += 4;
9313 regno = KT0;
9315 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9317 s += 4;
9318 regno = KT1;
9320 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9322 s += 5;
9323 regno = ZERO;
9325 else
9326 break;
9329 if (*s == ' ')
9330 ++s;
9331 if (args[1] != *s)
9333 if (c == 'v' || c == 'w')
9335 regno = mips16_to_32_reg_map[lastregno];
9336 s = s_reset;
9337 ++args;
9341 switch (c)
9343 case 'x':
9344 case 'y':
9345 case 'z':
9346 case 'v':
9347 case 'w':
9348 case 'Z':
9349 regno = mips32_to_16_reg_map[regno];
9350 break;
9352 case '0':
9353 if (regno != 0)
9354 regno = ILLEGAL_REG;
9355 break;
9357 case 'S':
9358 if (regno != SP)
9359 regno = ILLEGAL_REG;
9360 break;
9362 case 'R':
9363 if (regno != RA)
9364 regno = ILLEGAL_REG;
9365 break;
9367 case 'X':
9368 case 'Y':
9369 if (regno == AT && ! mips_opts.noat)
9370 as_warn (_("used $at without \".set noat\""));
9371 break;
9373 default:
9374 internalError ();
9377 if (regno == ILLEGAL_REG)
9378 break;
9380 switch (c)
9382 case 'x':
9383 case 'v':
9384 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9385 break;
9386 case 'y':
9387 case 'w':
9388 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9389 break;
9390 case 'z':
9391 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9392 break;
9393 case 'Z':
9394 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9395 case '0':
9396 case 'S':
9397 case 'R':
9398 break;
9399 case 'X':
9400 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9401 break;
9402 case 'Y':
9403 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9404 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9405 break;
9406 default:
9407 internalError ();
9410 lastregno = regno;
9411 continue;
9413 case 'P':
9414 if (strncmp (s, "$pc", 3) == 0)
9416 s += 3;
9417 continue;
9419 break;
9421 case '<':
9422 case '>':
9423 case '[':
9424 case ']':
9425 case '4':
9426 case '5':
9427 case 'H':
9428 case 'W':
9429 case 'D':
9430 case 'j':
9431 case '8':
9432 case 'V':
9433 case 'C':
9434 case 'U':
9435 case 'k':
9436 case 'K':
9437 if (s[0] == '%'
9438 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9440 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9441 and generate the appropriate reloc. If the text
9442 inside %gprel is not a symbol name with an
9443 optional offset, then we generate a normal reloc
9444 and will probably fail later. */
9445 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9446 if (imm_expr.X_op == O_symbol)
9448 mips16_ext = true;
9449 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9450 s = expr_end;
9451 ip->use_extend = true;
9452 ip->extend = 0;
9453 continue;
9456 else
9458 /* Just pick up a normal expression. */
9459 my_getExpression (&imm_expr, s);
9462 if (imm_expr.X_op == O_register)
9464 /* What we thought was an expression turned out to
9465 be a register. */
9467 if (s[0] == '(' && args[1] == '(')
9469 /* It looks like the expression was omitted
9470 before a register indirection, which means
9471 that the expression is implicitly zero. We
9472 still set up imm_expr, so that we handle
9473 explicit extensions correctly. */
9474 imm_expr.X_op = O_constant;
9475 imm_expr.X_add_number = 0;
9476 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9477 continue;
9480 break;
9483 /* We need to relax this instruction. */
9484 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9485 s = expr_end;
9486 continue;
9488 case 'p':
9489 case 'q':
9490 case 'A':
9491 case 'B':
9492 case 'E':
9493 /* We use offset_reloc rather than imm_reloc for the PC
9494 relative operands. This lets macros with both
9495 immediate and address operands work correctly. */
9496 my_getExpression (&offset_expr, s);
9498 if (offset_expr.X_op == O_register)
9499 break;
9501 /* We need to relax this instruction. */
9502 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9503 s = expr_end;
9504 continue;
9506 case '6': /* break code */
9507 my_getExpression (&imm_expr, s);
9508 check_absolute_expr (ip, &imm_expr);
9509 if ((unsigned long) imm_expr.X_add_number > 63)
9511 as_warn (_("Invalid value for `%s' (%lu)"),
9512 ip->insn_mo->name,
9513 (unsigned long) imm_expr.X_add_number);
9514 imm_expr.X_add_number &= 0x3f;
9516 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9517 imm_expr.X_op = O_absent;
9518 s = expr_end;
9519 continue;
9521 case 'a': /* 26 bit address */
9522 my_getExpression (&offset_expr, s);
9523 s = expr_end;
9524 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9525 ip->insn_opcode <<= 16;
9526 continue;
9528 case 'l': /* register list for entry macro */
9529 case 'L': /* register list for exit macro */
9531 int mask;
9533 if (c == 'l')
9534 mask = 0;
9535 else
9536 mask = 7 << 3;
9537 while (*s != '\0')
9539 int freg, reg1, reg2;
9541 while (*s == ' ' || *s == ',')
9542 ++s;
9543 if (*s != '$')
9545 as_bad (_("can't parse register list"));
9546 break;
9548 ++s;
9549 if (*s != 'f')
9550 freg = 0;
9551 else
9553 freg = 1;
9554 ++s;
9556 reg1 = 0;
9557 while (ISDIGIT (*s))
9559 reg1 *= 10;
9560 reg1 += *s - '0';
9561 ++s;
9563 if (*s == ' ')
9564 ++s;
9565 if (*s != '-')
9566 reg2 = reg1;
9567 else
9569 ++s;
9570 if (*s != '$')
9571 break;
9572 ++s;
9573 if (freg)
9575 if (*s == 'f')
9576 ++s;
9577 else
9579 as_bad (_("invalid register list"));
9580 break;
9583 reg2 = 0;
9584 while (ISDIGIT (*s))
9586 reg2 *= 10;
9587 reg2 += *s - '0';
9588 ++s;
9591 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9593 mask &= ~ (7 << 3);
9594 mask |= 5 << 3;
9596 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9598 mask &= ~ (7 << 3);
9599 mask |= 6 << 3;
9601 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9602 mask |= (reg2 - 3) << 3;
9603 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9604 mask |= (reg2 - 15) << 1;
9605 else if (reg1 == RA && reg2 == RA)
9606 mask |= 1;
9607 else
9609 as_bad (_("invalid register list"));
9610 break;
9613 /* The mask is filled in in the opcode table for the
9614 benefit of the disassembler. We remove it before
9615 applying the actual mask. */
9616 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9617 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9619 continue;
9621 case 'e': /* extend code */
9622 my_getExpression (&imm_expr, s);
9623 check_absolute_expr (ip, &imm_expr);
9624 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9626 as_warn (_("Invalid value for `%s' (%lu)"),
9627 ip->insn_mo->name,
9628 (unsigned long) imm_expr.X_add_number);
9629 imm_expr.X_add_number &= 0x7ff;
9631 ip->insn_opcode |= imm_expr.X_add_number;
9632 imm_expr.X_op = O_absent;
9633 s = expr_end;
9634 continue;
9636 default:
9637 internalError ();
9639 break;
9642 /* Args don't match. */
9643 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9644 strcmp (insn->name, insn[1].name) == 0)
9646 ++insn;
9647 s = argsstart;
9648 continue;
9651 insn_error = _("illegal operands");
9653 return;
9657 /* This structure holds information we know about a mips16 immediate
9658 argument type. */
9660 struct mips16_immed_operand
9662 /* The type code used in the argument string in the opcode table. */
9663 int type;
9664 /* The number of bits in the short form of the opcode. */
9665 int nbits;
9666 /* The number of bits in the extended form of the opcode. */
9667 int extbits;
9668 /* The amount by which the short form is shifted when it is used;
9669 for example, the sw instruction has a shift count of 2. */
9670 int shift;
9671 /* The amount by which the short form is shifted when it is stored
9672 into the instruction code. */
9673 int op_shift;
9674 /* Non-zero if the short form is unsigned. */
9675 int unsp;
9676 /* Non-zero if the extended form is unsigned. */
9677 int extu;
9678 /* Non-zero if the value is PC relative. */
9679 int pcrel;
9682 /* The mips16 immediate operand types. */
9684 static const struct mips16_immed_operand mips16_immed_operands[] =
9686 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9687 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9688 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9689 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9690 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9691 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9692 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9693 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9694 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9695 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9696 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9697 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9698 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9699 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9700 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9701 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9702 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9703 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9704 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9705 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9706 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9709 #define MIPS16_NUM_IMMED \
9710 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9712 /* Handle a mips16 instruction with an immediate value. This or's the
9713 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9714 whether an extended value is needed; if one is needed, it sets
9715 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9716 If SMALL is true, an unextended opcode was explicitly requested.
9717 If EXT is true, an extended opcode was explicitly requested. If
9718 WARN is true, warn if EXT does not match reality. */
9720 static void
9721 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9722 extend)
9723 char *file;
9724 unsigned int line;
9725 int type;
9726 offsetT val;
9727 boolean warn;
9728 boolean small;
9729 boolean ext;
9730 unsigned long *insn;
9731 boolean *use_extend;
9732 unsigned short *extend;
9734 register const struct mips16_immed_operand *op;
9735 int mintiny, maxtiny;
9736 boolean needext;
9738 op = mips16_immed_operands;
9739 while (op->type != type)
9741 ++op;
9742 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9745 if (op->unsp)
9747 if (type == '<' || type == '>' || type == '[' || type == ']')
9749 mintiny = 1;
9750 maxtiny = 1 << op->nbits;
9752 else
9754 mintiny = 0;
9755 maxtiny = (1 << op->nbits) - 1;
9758 else
9760 mintiny = - (1 << (op->nbits - 1));
9761 maxtiny = (1 << (op->nbits - 1)) - 1;
9764 /* Branch offsets have an implicit 0 in the lowest bit. */
9765 if (type == 'p' || type == 'q')
9766 val /= 2;
9768 if ((val & ((1 << op->shift) - 1)) != 0
9769 || val < (mintiny << op->shift)
9770 || val > (maxtiny << op->shift))
9771 needext = true;
9772 else
9773 needext = false;
9775 if (warn && ext && ! needext)
9776 as_warn_where (file, line,
9777 _("extended operand requested but not required"));
9778 if (small && needext)
9779 as_bad_where (file, line, _("invalid unextended operand value"));
9781 if (small || (! ext && ! needext))
9783 int insnval;
9785 *use_extend = false;
9786 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9787 insnval <<= op->op_shift;
9788 *insn |= insnval;
9790 else
9792 long minext, maxext;
9793 int extval;
9795 if (op->extu)
9797 minext = 0;
9798 maxext = (1 << op->extbits) - 1;
9800 else
9802 minext = - (1 << (op->extbits - 1));
9803 maxext = (1 << (op->extbits - 1)) - 1;
9805 if (val < minext || val > maxext)
9806 as_bad_where (file, line,
9807 _("operand value out of range for instruction"));
9809 *use_extend = true;
9810 if (op->extbits == 16)
9812 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9813 val &= 0x1f;
9815 else if (op->extbits == 15)
9817 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9818 val &= 0xf;
9820 else
9822 extval = ((val & 0x1f) << 6) | (val & 0x20);
9823 val = 0;
9826 *extend = (unsigned short) extval;
9827 *insn |= val;
9831 static struct percent_op_match
9833 const char *str;
9834 const enum small_ex_type type;
9835 } percent_op[] =
9837 {"%lo", S_EX_LO},
9838 #ifdef OBJ_ELF
9839 {"%call_hi", S_EX_CALL_HI},
9840 {"%call_lo", S_EX_CALL_LO},
9841 {"%call16", S_EX_CALL16},
9842 {"%got_disp", S_EX_GOT_DISP},
9843 {"%got_page", S_EX_GOT_PAGE},
9844 {"%got_ofst", S_EX_GOT_OFST},
9845 {"%got_hi", S_EX_GOT_HI},
9846 {"%got_lo", S_EX_GOT_LO},
9847 {"%got", S_EX_GOT},
9848 {"%gp_rel", S_EX_GP_REL},
9849 {"%half", S_EX_HALF},
9850 {"%highest", S_EX_HIGHEST},
9851 {"%higher", S_EX_HIGHER},
9852 {"%neg", S_EX_NEG},
9853 #endif
9854 {"%hi", S_EX_HI}
9857 /* Parse small expression input. STR gets adjusted to eat up whitespace.
9858 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9859 can be nested, this is handled by blanking the innermost, parsing the
9860 rest by subsequent calls. */
9862 static int
9863 my_getSmallParser (str, len, nestlevel)
9864 char **str;
9865 unsigned int *len;
9866 int *nestlevel;
9868 *len = 0;
9869 *str += strspn (*str, " \t");
9870 /* Check for expression in parentheses. */
9871 if (**str == '(')
9873 char *b = *str + 1 + strspn (*str + 1, " \t");
9874 char *e;
9876 /* Check for base register. */
9877 if (b[0] == '$')
9879 if (strchr (b, ')')
9880 && (e = b + strcspn (b, ") \t"))
9881 && e - b > 1 && e - b < 4)
9883 if ((e - b == 3
9884 && ((b[1] == 'f' && b[2] == 'p')
9885 || (b[1] == 's' && b[2] == 'p')
9886 || (b[1] == 'g' && b[2] == 'p')
9887 || (b[1] == 'a' && b[2] == 't')
9888 || (ISDIGIT (b[1])
9889 && ISDIGIT (b[2]))))
9890 || (ISDIGIT (b[1])))
9892 *len = strcspn (*str, ")") + 1;
9893 return S_EX_REGISTER;
9897 /* Check for percent_op (in parentheses). */
9898 else if (b[0] == '%')
9900 *str = b;
9901 return my_getPercentOp (str, len, nestlevel);
9904 /* Some other expression in the parentheses, which can contain
9905 parentheses itself. Attempt to find the matching one. */
9907 int pcnt = 1;
9908 char *s;
9910 *len = 1;
9911 for (s = *str + 1; *s && pcnt; s++, (*len)++)
9913 if (*s == '(')
9914 ++pcnt;
9915 else if (*s == ')')
9916 --pcnt;
9920 /* Check for percent_op (outside of parentheses). */
9921 else if (*str[0] == '%')
9922 return my_getPercentOp (str, len, nestlevel);
9924 /* Any other expression. */
9925 return S_EX_NONE;
9928 static int
9929 my_getPercentOp (str, len, nestlevel)
9930 char **str;
9931 unsigned int *len;
9932 int *nestlevel;
9934 char *tmp = *str + 1;
9935 unsigned int i = 0;
9937 while (ISALPHA (*tmp) || *tmp == '_')
9939 *tmp = TOLOWER (*tmp);
9940 tmp++;
9942 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9944 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9945 i++;
9946 else
9948 int type = percent_op[i].type;
9950 /* Only %hi and %lo are allowed for OldABI. */
9951 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9952 return S_EX_NONE;
9954 *len = strlen (percent_op[i].str);
9955 ++(*nestlevel);
9956 return type;
9959 return S_EX_NONE;
9962 static int
9963 my_getSmallExpression (ep, str)
9964 expressionS *ep;
9965 char *str;
9967 static char *oldstr = NULL;
9968 int c = S_EX_NONE;
9969 int oldc;
9970 int nestlevel = -1;
9971 unsigned int len;
9973 /* Don't update oldstr if the last call had nested percent_op's. We need
9974 it to parse the outer ones later. */
9975 if (! oldstr)
9976 oldstr = str;
9980 oldc = c;
9981 c = my_getSmallParser (&str, &len, &nestlevel);
9982 if (c != S_EX_NONE && c != S_EX_REGISTER)
9983 str += len;
9985 while (c != S_EX_NONE && c != S_EX_REGISTER);
9987 if (nestlevel >= 0)
9989 /* A percent_op was encountered. Don't try to get an expression if
9990 it is already blanked out. */
9991 if (*(str + strspn (str + 1, " )")) != ')')
9993 char save;
9995 /* Let my_getExpression() stop at the closing parenthesis. */
9996 save = *(str + len);
9997 *(str + len) = '\0';
9998 my_getExpression (ep, str);
9999 *(str + len) = save;
10001 if (nestlevel > 0)
10003 /* Blank out including the % sign and the proper matching
10004 parenthesis. */
10005 int pcnt = 1;
10006 char *s = strrchr (oldstr, '%');
10007 char *end;
10009 for (end = strchr (s, '(') + 1; *end && pcnt; end++)
10011 if (*end == '(')
10012 ++pcnt;
10013 else if (*end == ')')
10014 --pcnt;
10017 memset (s, ' ', end - s);
10018 str = oldstr;
10020 else
10021 expr_end = str + len;
10023 c = oldc;
10025 else if (c == S_EX_NONE)
10027 my_getExpression (ep, str);
10029 else if (c == S_EX_REGISTER)
10031 ep->X_op = O_constant;
10032 expr_end = str;
10033 ep->X_add_symbol = NULL;
10034 ep->X_op_symbol = NULL;
10035 ep->X_add_number = 0;
10037 else
10039 as_fatal (_("internal error"));
10042 if (nestlevel <= 0)
10043 /* All percent_op's have been handled. */
10044 oldstr = NULL;
10046 return c;
10049 static void
10050 my_getExpression (ep, str)
10051 expressionS *ep;
10052 char *str;
10054 char *save_in;
10055 valueT val;
10057 save_in = input_line_pointer;
10058 input_line_pointer = str;
10059 expression (ep);
10060 expr_end = input_line_pointer;
10061 input_line_pointer = save_in;
10063 /* If we are in mips16 mode, and this is an expression based on `.',
10064 then we bump the value of the symbol by 1 since that is how other
10065 text symbols are handled. We don't bother to handle complex
10066 expressions, just `.' plus or minus a constant. */
10067 if (mips_opts.mips16
10068 && ep->X_op == O_symbol
10069 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10070 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10071 && symbol_get_frag (ep->X_add_symbol) == frag_now
10072 && symbol_constant_p (ep->X_add_symbol)
10073 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10074 S_SET_VALUE (ep->X_add_symbol, val + 1);
10077 /* Turn a string in input_line_pointer into a floating point constant
10078 of type TYPE, and store the appropriate bytes in *LITP. The number
10079 of LITTLENUMS emitted is stored in *SIZEP. An error message is
10080 returned, or NULL on OK. */
10082 char *
10083 md_atof (type, litP, sizeP)
10084 int type;
10085 char *litP;
10086 int *sizeP;
10088 int prec;
10089 LITTLENUM_TYPE words[4];
10090 char *t;
10091 int i;
10093 switch (type)
10095 case 'f':
10096 prec = 2;
10097 break;
10099 case 'd':
10100 prec = 4;
10101 break;
10103 default:
10104 *sizeP = 0;
10105 return _("bad call to md_atof");
10108 t = atof_ieee (input_line_pointer, type, words);
10109 if (t)
10110 input_line_pointer = t;
10112 *sizeP = prec * 2;
10114 if (! target_big_endian)
10116 for (i = prec - 1; i >= 0; i--)
10118 md_number_to_chars (litP, (valueT) words[i], 2);
10119 litP += 2;
10122 else
10124 for (i = 0; i < prec; i++)
10126 md_number_to_chars (litP, (valueT) words[i], 2);
10127 litP += 2;
10131 return NULL;
10134 void
10135 md_number_to_chars (buf, val, n)
10136 char *buf;
10137 valueT val;
10138 int n;
10140 if (target_big_endian)
10141 number_to_chars_bigendian (buf, val, n);
10142 else
10143 number_to_chars_littleendian (buf, val, n);
10146 #ifdef OBJ_ELF
10147 static int support_64bit_objects(void)
10149 const char **list, **l;
10151 list = bfd_target_list ();
10152 for (l = list; *l != NULL; l++)
10153 #ifdef TE_TMIPS
10154 /* This is traditional mips */
10155 if (strcmp (*l, "elf64-tradbigmips") == 0
10156 || strcmp (*l, "elf64-tradlittlemips") == 0)
10157 #else
10158 if (strcmp (*l, "elf64-bigmips") == 0
10159 || strcmp (*l, "elf64-littlemips") == 0)
10160 #endif
10161 break;
10162 free (list);
10163 return (*l != NULL);
10165 #endif /* OBJ_ELF */
10167 const char *md_shortopts = "nO::g::G:";
10169 struct option md_longopts[] =
10171 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10172 {"mips0", no_argument, NULL, OPTION_MIPS1},
10173 {"mips1", no_argument, NULL, OPTION_MIPS1},
10174 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10175 {"mips2", no_argument, NULL, OPTION_MIPS2},
10176 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10177 {"mips3", no_argument, NULL, OPTION_MIPS3},
10178 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10179 {"mips4", no_argument, NULL, OPTION_MIPS4},
10180 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10181 {"mips5", no_argument, NULL, OPTION_MIPS5},
10182 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10183 {"mips32", no_argument, NULL, OPTION_MIPS32},
10184 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10185 {"mips64", no_argument, NULL, OPTION_MIPS64},
10186 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
10187 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10188 #define OPTION_TRAP (OPTION_MD_BASE + 9)
10189 {"trap", no_argument, NULL, OPTION_TRAP},
10190 {"no-break", no_argument, NULL, OPTION_TRAP},
10191 #define OPTION_BREAK (OPTION_MD_BASE + 10)
10192 {"break", no_argument, NULL, OPTION_BREAK},
10193 {"no-trap", no_argument, NULL, OPTION_BREAK},
10194 #define OPTION_EB (OPTION_MD_BASE + 11)
10195 {"EB", no_argument, NULL, OPTION_EB},
10196 #define OPTION_EL (OPTION_MD_BASE + 12)
10197 {"EL", no_argument, NULL, OPTION_EL},
10198 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
10199 {"mips16", no_argument, NULL, OPTION_MIPS16},
10200 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
10201 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10202 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
10203 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10204 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10205 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10206 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10207 #define OPTION_FP32 (OPTION_MD_BASE + 17)
10208 {"mfp32", no_argument, NULL, OPTION_FP32},
10209 #define OPTION_GP32 (OPTION_MD_BASE + 18)
10210 {"mgp32", no_argument, NULL, OPTION_GP32},
10211 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
10212 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10213 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
10214 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10215 #define OPTION_MARCH (OPTION_MD_BASE + 21)
10216 {"march", required_argument, NULL, OPTION_MARCH},
10217 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
10218 {"mtune", required_argument, NULL, OPTION_MTUNE},
10219 #define OPTION_FP64 (OPTION_MD_BASE + 23)
10220 {"mfp64", no_argument, NULL, OPTION_FP64},
10221 #define OPTION_M4650 (OPTION_MD_BASE + 24)
10222 {"m4650", no_argument, NULL, OPTION_M4650},
10223 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10224 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10225 #define OPTION_M4010 (OPTION_MD_BASE + 26)
10226 {"m4010", no_argument, NULL, OPTION_M4010},
10227 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10228 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10229 #define OPTION_M4100 (OPTION_MD_BASE + 28)
10230 {"m4100", no_argument, NULL, OPTION_M4100},
10231 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10232 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10233 #define OPTION_M3900 (OPTION_MD_BASE + 30)
10234 {"m3900", no_argument, NULL, OPTION_M3900},
10235 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10236 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10237 #define OPTION_GP64 (OPTION_MD_BASE + 32)
10238 {"mgp64", no_argument, NULL, OPTION_GP64},
10239 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10240 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10241 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10242 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10243 #define OPTION_MDMX (OPTION_MD_BASE + 35)
10244 {"mdmx", no_argument, NULL, OPTION_MDMX},
10245 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10246 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10247 #define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10248 #define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10249 {"mfix-vr4122-bugs", no_argument, NULL, OPTION_FIX_VR4122},
10250 {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10251 #define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10252 #define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10253 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10254 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10255 #ifdef OBJ_ELF
10256 #define OPTION_ELF_BASE (OPTION_MD_BASE + 41)
10257 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10258 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10259 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10260 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10261 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10262 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10263 {"xgot", no_argument, NULL, OPTION_XGOT},
10264 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10265 {"mabi", required_argument, NULL, OPTION_MABI},
10266 #define OPTION_32 (OPTION_ELF_BASE + 4)
10267 {"32", no_argument, NULL, OPTION_32},
10268 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10269 {"n32", no_argument, NULL, OPTION_N32},
10270 #define OPTION_64 (OPTION_ELF_BASE + 6)
10271 {"64", no_argument, NULL, OPTION_64},
10272 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10273 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10274 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10275 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10276 #endif /* OBJ_ELF */
10277 {NULL, no_argument, NULL, 0}
10279 size_t md_longopts_size = sizeof (md_longopts);
10281 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10282 NEW_VALUE. Warn if another value was already specified. Note:
10283 we have to defer parsing the -march and -mtune arguments in order
10284 to handle 'from-abi' correctly, since the ABI might be specified
10285 in a later argument. */
10287 static void
10288 mips_set_option_string (string_ptr, new_value)
10289 const char **string_ptr, *new_value;
10291 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10292 as_warn (_("A different %s was already specified, is now %s"),
10293 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10294 new_value);
10296 *string_ptr = new_value;
10300 md_parse_option (c, arg)
10301 int c;
10302 char *arg;
10304 switch (c)
10306 case OPTION_CONSTRUCT_FLOATS:
10307 mips_disable_float_construction = 0;
10308 break;
10310 case OPTION_NO_CONSTRUCT_FLOATS:
10311 mips_disable_float_construction = 1;
10312 break;
10314 case OPTION_TRAP:
10315 mips_trap = 1;
10316 break;
10318 case OPTION_BREAK:
10319 mips_trap = 0;
10320 break;
10322 case OPTION_EB:
10323 target_big_endian = 1;
10324 break;
10326 case OPTION_EL:
10327 target_big_endian = 0;
10328 break;
10330 case 'n':
10331 warn_nops = 1;
10332 break;
10334 case 'O':
10335 if (arg && arg[1] == '0')
10336 mips_optimize = 1;
10337 else
10338 mips_optimize = 2;
10339 break;
10341 case 'g':
10342 if (arg == NULL)
10343 mips_debug = 2;
10344 else
10345 mips_debug = atoi (arg);
10346 /* When the MIPS assembler sees -g or -g2, it does not do
10347 optimizations which limit full symbolic debugging. We take
10348 that to be equivalent to -O0. */
10349 if (mips_debug == 2)
10350 mips_optimize = 1;
10351 break;
10353 case OPTION_MIPS1:
10354 file_mips_isa = ISA_MIPS1;
10355 break;
10357 case OPTION_MIPS2:
10358 file_mips_isa = ISA_MIPS2;
10359 break;
10361 case OPTION_MIPS3:
10362 file_mips_isa = ISA_MIPS3;
10363 break;
10365 case OPTION_MIPS4:
10366 file_mips_isa = ISA_MIPS4;
10367 break;
10369 case OPTION_MIPS5:
10370 file_mips_isa = ISA_MIPS5;
10371 break;
10373 case OPTION_MIPS32:
10374 file_mips_isa = ISA_MIPS32;
10375 break;
10377 case OPTION_MIPS64:
10378 file_mips_isa = ISA_MIPS64;
10379 break;
10381 case OPTION_MTUNE:
10382 mips_set_option_string (&mips_tune_string, arg);
10383 break;
10385 case OPTION_MARCH:
10386 mips_set_option_string (&mips_arch_string, arg);
10387 break;
10389 case OPTION_M4650:
10390 mips_set_option_string (&mips_arch_string, "4650");
10391 mips_set_option_string (&mips_tune_string, "4650");
10392 break;
10394 case OPTION_NO_M4650:
10395 break;
10397 case OPTION_M4010:
10398 mips_set_option_string (&mips_arch_string, "4010");
10399 mips_set_option_string (&mips_tune_string, "4010");
10400 break;
10402 case OPTION_NO_M4010:
10403 break;
10405 case OPTION_M4100:
10406 mips_set_option_string (&mips_arch_string, "4100");
10407 mips_set_option_string (&mips_tune_string, "4100");
10408 break;
10410 case OPTION_NO_M4100:
10411 break;
10413 case OPTION_M3900:
10414 mips_set_option_string (&mips_arch_string, "3900");
10415 mips_set_option_string (&mips_tune_string, "3900");
10416 break;
10418 case OPTION_NO_M3900:
10419 break;
10421 case OPTION_MDMX:
10422 mips_opts.ase_mdmx = 1;
10423 break;
10425 case OPTION_NO_MDMX:
10426 mips_opts.ase_mdmx = 0;
10427 break;
10429 case OPTION_MIPS16:
10430 mips_opts.mips16 = 1;
10431 mips_no_prev_insn (false);
10432 break;
10434 case OPTION_NO_MIPS16:
10435 mips_opts.mips16 = 0;
10436 mips_no_prev_insn (false);
10437 break;
10439 case OPTION_MIPS3D:
10440 mips_opts.ase_mips3d = 1;
10441 break;
10443 case OPTION_NO_MIPS3D:
10444 mips_opts.ase_mips3d = 0;
10445 break;
10447 case OPTION_MEMBEDDED_PIC:
10448 mips_pic = EMBEDDED_PIC;
10449 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10451 as_bad (_("-G may not be used with embedded PIC code"));
10452 return 0;
10454 g_switch_value = 0x7fffffff;
10455 break;
10457 case OPTION_FIX_VR4122:
10458 mips_fix_4122_bugs = 1;
10459 break;
10461 case OPTION_NO_FIX_VR4122:
10462 mips_fix_4122_bugs = 0;
10463 break;
10465 case OPTION_RELAX_BRANCH:
10466 mips_relax_branch = 1;
10467 break;
10469 case OPTION_NO_RELAX_BRANCH:
10470 mips_relax_branch = 0;
10471 break;
10473 #ifdef OBJ_ELF
10474 /* When generating ELF code, we permit -KPIC and -call_shared to
10475 select SVR4_PIC, and -non_shared to select no PIC. This is
10476 intended to be compatible with Irix 5. */
10477 case OPTION_CALL_SHARED:
10478 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10480 as_bad (_("-call_shared is supported only for ELF format"));
10481 return 0;
10483 mips_pic = SVR4_PIC;
10484 if (g_switch_seen && g_switch_value != 0)
10486 as_bad (_("-G may not be used with SVR4 PIC code"));
10487 return 0;
10489 g_switch_value = 0;
10490 break;
10492 case OPTION_NON_SHARED:
10493 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10495 as_bad (_("-non_shared is supported only for ELF format"));
10496 return 0;
10498 mips_pic = NO_PIC;
10499 break;
10501 /* The -xgot option tells the assembler to use 32 offsets when
10502 accessing the got in SVR4_PIC mode. It is for Irix
10503 compatibility. */
10504 case OPTION_XGOT:
10505 mips_big_got = 1;
10506 break;
10507 #endif /* OBJ_ELF */
10509 case 'G':
10510 if (! USE_GLOBAL_POINTER_OPT)
10512 as_bad (_("-G is not supported for this configuration"));
10513 return 0;
10515 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10517 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10518 return 0;
10520 else
10521 g_switch_value = atoi (arg);
10522 g_switch_seen = 1;
10523 break;
10525 #ifdef OBJ_ELF
10526 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10527 and -mabi=64. */
10528 case OPTION_32:
10529 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10531 as_bad (_("-32 is supported for ELF format only"));
10532 return 0;
10534 mips_abi = O32_ABI;
10535 break;
10537 case OPTION_N32:
10538 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10540 as_bad (_("-n32 is supported for ELF format only"));
10541 return 0;
10543 mips_abi = N32_ABI;
10544 break;
10546 case OPTION_64:
10547 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10549 as_bad (_("-64 is supported for ELF format only"));
10550 return 0;
10552 mips_abi = N64_ABI;
10553 if (! support_64bit_objects())
10554 as_fatal (_("No compiled in support for 64 bit object file format"));
10555 break;
10556 #endif /* OBJ_ELF */
10558 case OPTION_GP32:
10559 file_mips_gp32 = 1;
10560 break;
10562 case OPTION_GP64:
10563 file_mips_gp32 = 0;
10564 break;
10566 case OPTION_FP32:
10567 file_mips_fp32 = 1;
10568 break;
10570 case OPTION_FP64:
10571 file_mips_fp32 = 0;
10572 break;
10574 #ifdef OBJ_ELF
10575 case OPTION_MABI:
10576 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10578 as_bad (_("-mabi is supported for ELF format only"));
10579 return 0;
10581 if (strcmp (arg, "32") == 0)
10582 mips_abi = O32_ABI;
10583 else if (strcmp (arg, "o64") == 0)
10584 mips_abi = O64_ABI;
10585 else if (strcmp (arg, "n32") == 0)
10586 mips_abi = N32_ABI;
10587 else if (strcmp (arg, "64") == 0)
10589 mips_abi = N64_ABI;
10590 if (! support_64bit_objects())
10591 as_fatal (_("No compiled in support for 64 bit object file "
10592 "format"));
10594 else if (strcmp (arg, "eabi") == 0)
10595 mips_abi = EABI_ABI;
10596 else
10598 as_fatal (_("invalid abi -mabi=%s"), arg);
10599 return 0;
10601 break;
10602 #endif /* OBJ_ELF */
10604 case OPTION_M7000_HILO_FIX:
10605 mips_7000_hilo_fix = true;
10606 break;
10608 case OPTION_MNO_7000_HILO_FIX:
10609 mips_7000_hilo_fix = false;
10610 break;
10612 #ifdef OBJ_ELF
10613 case OPTION_MDEBUG:
10614 mips_flag_mdebug = true;
10615 break;
10617 case OPTION_NO_MDEBUG:
10618 mips_flag_mdebug = false;
10619 break;
10620 #endif /* OBJ_ELF */
10622 default:
10623 return 0;
10626 return 1;
10629 /* Set up globals to generate code for the ISA or processor
10630 described by INFO. */
10632 static void
10633 mips_set_architecture (info)
10634 const struct mips_cpu_info *info;
10636 if (info != 0)
10638 mips_arch_info = info;
10639 mips_arch = info->cpu;
10640 mips_opts.isa = info->isa;
10645 /* Likewise for tuning. */
10647 static void
10648 mips_set_tune (info)
10649 const struct mips_cpu_info *info;
10651 if (info != 0)
10653 mips_tune_info = info;
10654 mips_tune = info->cpu;
10659 void
10660 mips_after_parse_args ()
10662 /* GP relative stuff not working for PE */
10663 if (strncmp (TARGET_OS, "pe", 2) == 0
10664 && g_switch_value != 0)
10666 if (g_switch_seen)
10667 as_bad (_("-G not supported in this configuration."));
10668 g_switch_value = 0;
10671 /* The following code determines the architecture and register size.
10672 Similar code was added to GCC 3.3 (see override_options() in
10673 config/mips/mips.c). The GAS and GCC code should be kept in sync
10674 as much as possible. */
10676 if (mips_arch_string != 0)
10677 mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10679 if (mips_tune_string != 0)
10680 mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10682 if (file_mips_isa != ISA_UNKNOWN)
10684 /* Handle -mipsN. At this point, file_mips_isa contains the
10685 ISA level specified by -mipsN, while mips_opts.isa contains
10686 the -march selection (if any). */
10687 if (mips_arch_info != 0)
10689 /* -march takes precedence over -mipsN, since it is more descriptive.
10690 There's no harm in specifying both as long as the ISA levels
10691 are the same. */
10692 if (file_mips_isa != mips_opts.isa)
10693 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10694 mips_cpu_info_from_isa (file_mips_isa)->name,
10695 mips_cpu_info_from_isa (mips_opts.isa)->name);
10697 else
10698 mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10701 if (mips_arch_info == 0)
10702 mips_set_architecture (mips_parse_cpu ("default CPU",
10703 MIPS_CPU_STRING_DEFAULT));
10705 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10706 as_bad ("-march=%s is not compatible with the selected ABI",
10707 mips_arch_info->name);
10709 /* Optimize for mips_arch, unless -mtune selects a different processor. */
10710 if (mips_tune_info == 0)
10711 mips_set_tune (mips_arch_info);
10713 if (file_mips_gp32 >= 0)
10715 /* The user specified the size of the integer registers. Make sure
10716 it agrees with the ABI and ISA. */
10717 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10718 as_bad (_("-mgp64 used with a 32-bit processor"));
10719 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10720 as_bad (_("-mgp32 used with a 64-bit ABI"));
10721 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10722 as_bad (_("-mgp64 used with a 32-bit ABI"));
10724 else
10726 /* Infer the integer register size from the ABI and processor.
10727 Restrict ourselves to 32-bit registers if that's all the
10728 processor has, or if the ABI cannot handle 64-bit registers. */
10729 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10730 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10733 /* ??? GAS treats single-float processors as though they had 64-bit
10734 float registers (although it complains when double-precision
10735 instructions are used). As things stand, saying they have 32-bit
10736 registers would lead to spurious "register must be even" messages.
10737 So here we assume float registers are always the same size as
10738 integer ones, unless the user says otherwise. */
10739 if (file_mips_fp32 < 0)
10740 file_mips_fp32 = file_mips_gp32;
10742 /* End of GCC-shared inference code. */
10744 /* ??? When do we want this flag to be set? Who uses it? */
10745 if (file_mips_gp32 == 1
10746 && mips_abi == NO_ABI
10747 && ISA_HAS_64BIT_REGS (mips_opts.isa))
10748 mips_32bitmode = 1;
10750 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10751 as_bad (_("trap exception not supported at ISA 1"));
10753 /* If the selected architecture includes support for ASEs, enable
10754 generation of code for them. */
10755 if (mips_opts.mips16 == -1)
10756 mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10757 if (mips_opts.ase_mips3d == -1)
10758 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10759 if (mips_opts.ase_mdmx == -1)
10760 mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10762 file_mips_isa = mips_opts.isa;
10763 file_ase_mips16 = mips_opts.mips16;
10764 file_ase_mips3d = mips_opts.ase_mips3d;
10765 file_ase_mdmx = mips_opts.ase_mdmx;
10766 mips_opts.gp32 = file_mips_gp32;
10767 mips_opts.fp32 = file_mips_fp32;
10769 if (mips_flag_mdebug < 0)
10771 #ifdef OBJ_MAYBE_ECOFF
10772 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10773 mips_flag_mdebug = 1;
10774 else
10775 #endif /* OBJ_MAYBE_ECOFF */
10776 mips_flag_mdebug = 0;
10780 void
10781 mips_init_after_args ()
10783 /* initialize opcodes */
10784 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10785 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10788 long
10789 md_pcrel_from (fixP)
10790 fixS *fixP;
10792 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10793 && fixP->fx_addsy != (symbolS *) NULL
10794 && ! S_IS_DEFINED (fixP->fx_addsy))
10796 /* This makes a branch to an undefined symbol be a branch to the
10797 current location. */
10798 if (mips_pic == EMBEDDED_PIC)
10799 return 4;
10800 else
10801 return 1;
10804 /* Return the address of the delay slot. */
10805 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10808 /* This is called before the symbol table is processed. In order to
10809 work with gcc when using mips-tfile, we must keep all local labels.
10810 However, in other cases, we want to discard them. If we were
10811 called with -g, but we didn't see any debugging information, it may
10812 mean that gcc is smuggling debugging information through to
10813 mips-tfile, in which case we must generate all local labels. */
10815 void
10816 mips_frob_file_before_adjust ()
10818 #ifndef NO_ECOFF_DEBUGGING
10819 if (ECOFF_DEBUGGING
10820 && mips_debug != 0
10821 && ! ecoff_debugging_seen)
10822 flag_keep_locals = 1;
10823 #endif
10826 /* Sort any unmatched HI16_S relocs so that they immediately precede
10827 the corresponding LO reloc. This is called before md_apply_fix3 and
10828 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10829 explicit use of the %hi modifier. */
10831 void
10832 mips_frob_file ()
10834 struct mips_hi_fixup *l;
10836 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10838 segment_info_type *seginfo;
10839 int pass;
10841 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10843 /* Check quickly whether the next fixup happens to be a matching
10844 %lo. */
10845 if (l->fixp->fx_next != NULL
10846 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10847 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10848 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10849 continue;
10851 /* Look through the fixups for this segment for a matching %lo.
10852 When we find one, move the %hi just in front of it. We do
10853 this in two passes. In the first pass, we try to find a
10854 unique %lo. In the second pass, we permit multiple %hi
10855 relocs for a single %lo (this is a GNU extension). */
10856 seginfo = seg_info (l->seg);
10857 for (pass = 0; pass < 2; pass++)
10859 fixS *f, *prev;
10861 prev = NULL;
10862 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10864 /* Check whether this is a %lo fixup which matches l->fixp. */
10865 if (f->fx_r_type == BFD_RELOC_LO16
10866 && f->fx_addsy == l->fixp->fx_addsy
10867 && f->fx_offset == l->fixp->fx_offset
10868 && (pass == 1
10869 || prev == NULL
10870 || prev->fx_r_type != BFD_RELOC_HI16_S
10871 || prev->fx_addsy != f->fx_addsy
10872 || prev->fx_offset != f->fx_offset))
10874 fixS **pf;
10876 /* Move l->fixp before f. */
10877 for (pf = &seginfo->fix_root;
10878 *pf != l->fixp;
10879 pf = &(*pf)->fx_next)
10880 assert (*pf != NULL);
10882 *pf = l->fixp->fx_next;
10884 l->fixp->fx_next = f;
10885 if (prev == NULL)
10886 seginfo->fix_root = l->fixp;
10887 else
10888 prev->fx_next = l->fixp;
10890 break;
10893 prev = f;
10896 if (f != NULL)
10897 break;
10899 #if 0 /* GCC code motion plus incomplete dead code elimination
10900 can leave a %hi without a %lo. */
10901 if (pass == 1)
10902 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10903 _("Unmatched %%hi reloc"));
10904 #endif
10909 /* When generating embedded PIC code we need to use a special
10910 relocation to represent the difference of two symbols in the .text
10911 section (switch tables use a difference of this sort). See
10912 include/coff/mips.h for details. This macro checks whether this
10913 fixup requires the special reloc. */
10914 #define SWITCH_TABLE(fixp) \
10915 ((fixp)->fx_r_type == BFD_RELOC_32 \
10916 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10917 && (fixp)->fx_addsy != NULL \
10918 && (fixp)->fx_subsy != NULL \
10919 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10920 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10922 /* When generating embedded PIC code we must keep all PC relative
10923 relocations, in case the linker has to relax a call. We also need
10924 to keep relocations for switch table entries.
10926 We may have combined relocations without symbols in the N32/N64 ABI.
10927 We have to prevent gas from dropping them. */
10930 mips_force_relocation (fixp)
10931 fixS *fixp;
10933 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10934 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10935 || S_FORCE_RELOC (fixp->fx_addsy))
10936 return 1;
10938 if (HAVE_NEWABI
10939 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10940 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10941 || fixp->fx_r_type == BFD_RELOC_HI16_S
10942 || fixp->fx_r_type == BFD_RELOC_LO16))
10943 return 1;
10945 return (mips_pic == EMBEDDED_PIC
10946 && (fixp->fx_pcrel
10947 || SWITCH_TABLE (fixp)
10948 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10949 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10952 #ifdef OBJ_ELF
10953 static int
10954 mips_need_elf_addend_fixup (fixP)
10955 fixS *fixP;
10957 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10958 return 1;
10959 if (mips_pic == EMBEDDED_PIC
10960 && S_IS_WEAK (fixP->fx_addsy))
10961 return 1;
10962 if (mips_pic != EMBEDDED_PIC
10963 && (S_IS_WEAK (fixP->fx_addsy)
10964 || S_IS_EXTERNAL (fixP->fx_addsy))
10965 && !S_IS_COMMON (fixP->fx_addsy))
10966 return 1;
10967 if (symbol_used_in_reloc_p (fixP->fx_addsy)
10968 && (((bfd_get_section_flags (stdoutput,
10969 S_GET_SEGMENT (fixP->fx_addsy))
10970 & SEC_LINK_ONCE) != 0)
10971 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10972 ".gnu.linkonce",
10973 sizeof (".gnu.linkonce") - 1)))
10974 return 1;
10975 return 0;
10977 #endif
10979 /* Apply a fixup to the object file. */
10981 void
10982 md_apply_fix3 (fixP, valP, seg)
10983 fixS *fixP;
10984 valueT *valP;
10985 segT seg ATTRIBUTE_UNUSED;
10987 bfd_byte *buf;
10988 long insn;
10989 valueT value;
10990 static int previous_fx_r_type = 0;
10992 /* FIXME: Maybe just return for all reloc types not listed below?
10993 Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
10994 if (fixP->fx_r_type == BFD_RELOC_8)
10995 return;
10997 assert (fixP->fx_size == 4
10998 || fixP->fx_r_type == BFD_RELOC_16
10999 || fixP->fx_r_type == BFD_RELOC_32
11000 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11001 || fixP->fx_r_type == BFD_RELOC_HI16_S
11002 || fixP->fx_r_type == BFD_RELOC_LO16
11003 || fixP->fx_r_type == BFD_RELOC_GPREL16
11004 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
11005 || fixP->fx_r_type == BFD_RELOC_GPREL32
11006 || fixP->fx_r_type == BFD_RELOC_64
11007 || fixP->fx_r_type == BFD_RELOC_CTOR
11008 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11009 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11010 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11011 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11012 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11013 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
11014 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11015 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11016 || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
11018 value = *valP;
11020 /* If we aren't adjusting this fixup to be against the section
11021 symbol, we need to adjust the value. */
11022 #ifdef OBJ_ELF
11023 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11025 if (mips_need_elf_addend_fixup (fixP))
11027 reloc_howto_type *howto;
11028 valueT symval = S_GET_VALUE (fixP->fx_addsy);
11030 value -= symval;
11032 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11033 if (value != 0 && howto->partial_inplace
11034 && (! fixP->fx_pcrel || howto->pcrel_offset))
11036 /* In this case, the bfd_install_relocation routine will
11037 incorrectly add the symbol value back in. We just want
11038 the addend to appear in the object file.
11040 howto->pcrel_offset is added for R_MIPS_PC16, which is
11041 generated for code like
11043 globl g1 .text
11044 .text
11045 .space 20
11048 bal g1
11050 value -= symval;
11052 /* Make sure the addend is still non-zero. If it became zero
11053 after the last operation, set it to a spurious value and
11054 subtract the same value from the object file's contents. */
11055 if (value == 0)
11057 value = 8;
11059 /* The in-place addends for LO16 relocations are signed;
11060 leave the matching HI16 in-place addends as zero. */
11061 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11063 bfd_vma contents, mask, field;
11065 contents = bfd_get_bits (fixP->fx_frag->fr_literal
11066 + fixP->fx_where,
11067 fixP->fx_size * 8,
11068 target_big_endian);
11070 /* MASK has bits set where the relocation should go.
11071 FIELD is -value, shifted into the appropriate place
11072 for this relocation. */
11073 mask = 1 << (howto->bitsize - 1);
11074 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11075 field = (-value >> howto->rightshift) << howto->bitpos;
11077 bfd_put_bits ((field & mask) | (contents & ~mask),
11078 fixP->fx_frag->fr_literal + fixP->fx_where,
11079 fixP->fx_size * 8,
11080 target_big_endian);
11086 /* This code was generated using trial and error and so is
11087 fragile and not trustworthy. If you change it, you should
11088 rerun the elf-rel, elf-rel2, and empic testcases and ensure
11089 they still pass. */
11090 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11092 value += fixP->fx_frag->fr_address + fixP->fx_where;
11094 /* BFD's REL handling, for MIPS, is _very_ weird.
11095 This gives the right results, but it can't possibly
11096 be the way things are supposed to work. */
11097 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
11098 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
11099 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11100 value += fixP->fx_frag->fr_address + fixP->fx_where;
11103 #endif
11105 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
11107 /* We are not done if this is a composite relocation to set up gp. */
11108 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11109 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11110 || (fixP->fx_r_type == BFD_RELOC_64
11111 && (previous_fx_r_type == BFD_RELOC_GPREL32
11112 || previous_fx_r_type == BFD_RELOC_GPREL16))
11113 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11114 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11115 || fixP->fx_r_type == BFD_RELOC_LO16))))
11116 fixP->fx_done = 1;
11117 previous_fx_r_type = fixP->fx_r_type;
11119 switch (fixP->fx_r_type)
11121 case BFD_RELOC_MIPS_JMP:
11122 case BFD_RELOC_MIPS_SHIFT5:
11123 case BFD_RELOC_MIPS_SHIFT6:
11124 case BFD_RELOC_MIPS_GOT_DISP:
11125 case BFD_RELOC_MIPS_GOT_PAGE:
11126 case BFD_RELOC_MIPS_GOT_OFST:
11127 case BFD_RELOC_MIPS_SUB:
11128 case BFD_RELOC_MIPS_INSERT_A:
11129 case BFD_RELOC_MIPS_INSERT_B:
11130 case BFD_RELOC_MIPS_DELETE:
11131 case BFD_RELOC_MIPS_HIGHEST:
11132 case BFD_RELOC_MIPS_HIGHER:
11133 case BFD_RELOC_MIPS_SCN_DISP:
11134 case BFD_RELOC_MIPS_REL16:
11135 case BFD_RELOC_MIPS_RELGOT:
11136 case BFD_RELOC_MIPS_JALR:
11137 case BFD_RELOC_HI16:
11138 case BFD_RELOC_HI16_S:
11139 case BFD_RELOC_GPREL16:
11140 case BFD_RELOC_MIPS_LITERAL:
11141 case BFD_RELOC_MIPS_CALL16:
11142 case BFD_RELOC_MIPS_GOT16:
11143 case BFD_RELOC_GPREL32:
11144 case BFD_RELOC_MIPS_GOT_HI16:
11145 case BFD_RELOC_MIPS_GOT_LO16:
11146 case BFD_RELOC_MIPS_CALL_HI16:
11147 case BFD_RELOC_MIPS_CALL_LO16:
11148 case BFD_RELOC_MIPS16_GPREL:
11149 if (fixP->fx_pcrel)
11150 as_bad_where (fixP->fx_file, fixP->fx_line,
11151 _("Invalid PC relative reloc"));
11152 /* Nothing needed to do. The value comes from the reloc entry */
11153 break;
11155 case BFD_RELOC_MIPS16_JMP:
11156 /* We currently always generate a reloc against a symbol, which
11157 means that we don't want an addend even if the symbol is
11158 defined. */
11159 fixP->fx_addnumber = 0;
11160 break;
11162 case BFD_RELOC_PCREL_HI16_S:
11163 /* The addend for this is tricky if it is internal, so we just
11164 do everything here rather than in bfd_install_relocation. */
11165 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11166 && !fixP->fx_done
11167 && value != 0)
11168 break;
11169 if (fixP->fx_addsy
11170 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11172 /* For an external symbol adjust by the address to make it
11173 pcrel_offset. We use the address of the RELLO reloc
11174 which follows this one. */
11175 value += (fixP->fx_next->fx_frag->fr_address
11176 + fixP->fx_next->fx_where);
11178 value = ((value + 0x8000) >> 16) & 0xffff;
11179 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11180 if (target_big_endian)
11181 buf += 2;
11182 md_number_to_chars ((char *) buf, value, 2);
11183 break;
11185 case BFD_RELOC_PCREL_LO16:
11186 /* The addend for this is tricky if it is internal, so we just
11187 do everything here rather than in bfd_install_relocation. */
11188 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11189 && !fixP->fx_done
11190 && value != 0)
11191 break;
11192 if (fixP->fx_addsy
11193 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11194 value += fixP->fx_frag->fr_address + fixP->fx_where;
11195 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11196 if (target_big_endian)
11197 buf += 2;
11198 md_number_to_chars ((char *) buf, value, 2);
11199 break;
11201 case BFD_RELOC_64:
11202 /* This is handled like BFD_RELOC_32, but we output a sign
11203 extended value if we are only 32 bits. */
11204 if (fixP->fx_done
11205 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11207 if (8 <= sizeof (valueT))
11208 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11209 value, 8);
11210 else
11212 long w1, w2;
11213 long hiv;
11215 w1 = w2 = fixP->fx_where;
11216 if (target_big_endian)
11217 w1 += 4;
11218 else
11219 w2 += 4;
11220 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11221 if ((value & 0x80000000) != 0)
11222 hiv = 0xffffffff;
11223 else
11224 hiv = 0;
11225 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11228 break;
11230 case BFD_RELOC_RVA:
11231 case BFD_RELOC_32:
11232 /* If we are deleting this reloc entry, we must fill in the
11233 value now. This can happen if we have a .word which is not
11234 resolved when it appears but is later defined. We also need
11235 to fill in the value if this is an embedded PIC switch table
11236 entry. */
11237 if (fixP->fx_done
11238 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11239 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11240 value, 4);
11241 break;
11243 case BFD_RELOC_16:
11244 /* If we are deleting this reloc entry, we must fill in the
11245 value now. */
11246 assert (fixP->fx_size == 2);
11247 if (fixP->fx_done)
11248 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11249 value, 2);
11250 break;
11252 case BFD_RELOC_LO16:
11253 /* When handling an embedded PIC switch statement, we can wind
11254 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11255 if (fixP->fx_done)
11257 if (value + 0x8000 > 0xffff)
11258 as_bad_where (fixP->fx_file, fixP->fx_line,
11259 _("relocation overflow"));
11260 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11261 if (target_big_endian)
11262 buf += 2;
11263 md_number_to_chars ((char *) buf, value, 2);
11265 break;
11267 case BFD_RELOC_16_PCREL_S2:
11268 if ((value & 0x3) != 0)
11269 as_bad_where (fixP->fx_file, fixP->fx_line,
11270 _("Branch to odd address (%lx)"), (long) value);
11272 /* Fall through. */
11274 case BFD_RELOC_16_PCREL:
11276 * We need to save the bits in the instruction since fixup_segment()
11277 * might be deleting the relocation entry (i.e., a branch within
11278 * the current segment).
11280 if (!fixP->fx_done && value != 0)
11281 break;
11282 /* If 'value' is zero, the remaining reloc code won't actually
11283 do the store, so it must be done here. This is probably
11284 a bug somewhere. */
11285 if (!fixP->fx_done
11286 && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11287 || fixP->fx_addsy == NULL /* ??? */
11288 || ! S_IS_DEFINED (fixP->fx_addsy)))
11289 value -= fixP->fx_frag->fr_address + fixP->fx_where;
11291 value = (offsetT) value >> 2;
11293 /* update old instruction data */
11294 buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11295 if (target_big_endian)
11296 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11297 else
11298 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11300 if (value + 0x8000 <= 0xffff)
11301 insn |= value & 0xffff;
11302 else
11304 /* The branch offset is too large. If this is an
11305 unconditional branch, and we are not generating PIC code,
11306 we can convert it to an absolute jump instruction. */
11307 if (mips_pic == NO_PIC
11308 && fixP->fx_done
11309 && fixP->fx_frag->fr_address >= text_section->vma
11310 && (fixP->fx_frag->fr_address
11311 < text_section->vma + text_section->_raw_size)
11312 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11313 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11314 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11316 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11317 insn = 0x0c000000; /* jal */
11318 else
11319 insn = 0x08000000; /* j */
11320 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11321 fixP->fx_done = 0;
11322 fixP->fx_addsy = section_symbol (text_section);
11323 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11325 else
11327 /* If we got here, we have branch-relaxation disabled,
11328 and there's nothing we can do to fix this instruction
11329 without turning it into a longer sequence. */
11330 as_bad_where (fixP->fx_file, fixP->fx_line,
11331 _("Branch out of range"));
11335 md_number_to_chars ((char *) buf, (valueT) insn, 4);
11336 break;
11338 case BFD_RELOC_VTABLE_INHERIT:
11339 fixP->fx_done = 0;
11340 if (fixP->fx_addsy
11341 && !S_IS_DEFINED (fixP->fx_addsy)
11342 && !S_IS_WEAK (fixP->fx_addsy))
11343 S_SET_WEAK (fixP->fx_addsy);
11344 break;
11346 case BFD_RELOC_VTABLE_ENTRY:
11347 fixP->fx_done = 0;
11348 break;
11350 default:
11351 internalError ();
11355 #if 0
11356 void
11357 printInsn (oc)
11358 unsigned long oc;
11360 const struct mips_opcode *p;
11361 int treg, sreg, dreg, shamt;
11362 short imm;
11363 const char *args;
11364 int i;
11366 for (i = 0; i < NUMOPCODES; ++i)
11368 p = &mips_opcodes[i];
11369 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11371 printf ("%08lx %s\t", oc, p->name);
11372 treg = (oc >> 16) & 0x1f;
11373 sreg = (oc >> 21) & 0x1f;
11374 dreg = (oc >> 11) & 0x1f;
11375 shamt = (oc >> 6) & 0x1f;
11376 imm = oc;
11377 for (args = p->args;; ++args)
11379 switch (*args)
11381 case '\0':
11382 printf ("\n");
11383 break;
11385 case ',':
11386 case '(':
11387 case ')':
11388 printf ("%c", *args);
11389 continue;
11391 case 'r':
11392 assert (treg == sreg);
11393 printf ("$%d,$%d", treg, sreg);
11394 continue;
11396 case 'd':
11397 case 'G':
11398 printf ("$%d", dreg);
11399 continue;
11401 case 't':
11402 case 'E':
11403 printf ("$%d", treg);
11404 continue;
11406 case 'k':
11407 printf ("0x%x", treg);
11408 continue;
11410 case 'b':
11411 case 's':
11412 printf ("$%d", sreg);
11413 continue;
11415 case 'a':
11416 printf ("0x%08lx", oc & 0x1ffffff);
11417 continue;
11419 case 'i':
11420 case 'j':
11421 case 'o':
11422 case 'u':
11423 printf ("%d", imm);
11424 continue;
11426 case '<':
11427 case '>':
11428 printf ("$%d", shamt);
11429 continue;
11431 default:
11432 internalError ();
11434 break;
11436 return;
11439 printf (_("%08lx UNDEFINED\n"), oc);
11441 #endif
11443 static symbolS *
11444 get_symbol ()
11446 int c;
11447 char *name;
11448 symbolS *p;
11450 name = input_line_pointer;
11451 c = get_symbol_end ();
11452 p = (symbolS *) symbol_find_or_make (name);
11453 *input_line_pointer = c;
11454 return p;
11457 /* Align the current frag to a given power of two. The MIPS assembler
11458 also automatically adjusts any preceding label. */
11460 static void
11461 mips_align (to, fill, label)
11462 int to;
11463 int fill;
11464 symbolS *label;
11466 mips_emit_delays (false);
11467 frag_align (to, fill, 0);
11468 record_alignment (now_seg, to);
11469 if (label != NULL)
11471 assert (S_GET_SEGMENT (label) == now_seg);
11472 symbol_set_frag (label, frag_now);
11473 S_SET_VALUE (label, (valueT) frag_now_fix ());
11477 /* Align to a given power of two. .align 0 turns off the automatic
11478 alignment used by the data creating pseudo-ops. */
11480 static void
11481 s_align (x)
11482 int x ATTRIBUTE_UNUSED;
11484 register int temp;
11485 register long temp_fill;
11486 long max_alignment = 15;
11490 o Note that the assembler pulls down any immediately preceeding label
11491 to the aligned address.
11492 o It's not documented but auto alignment is reinstated by
11493 a .align pseudo instruction.
11494 o Note also that after auto alignment is turned off the mips assembler
11495 issues an error on attempt to assemble an improperly aligned data item.
11496 We don't.
11500 temp = get_absolute_expression ();
11501 if (temp > max_alignment)
11502 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11503 else if (temp < 0)
11505 as_warn (_("Alignment negative: 0 assumed."));
11506 temp = 0;
11508 if (*input_line_pointer == ',')
11510 ++input_line_pointer;
11511 temp_fill = get_absolute_expression ();
11513 else
11514 temp_fill = 0;
11515 if (temp)
11517 auto_align = 1;
11518 mips_align (temp, (int) temp_fill,
11519 insn_labels != NULL ? insn_labels->label : NULL);
11521 else
11523 auto_align = 0;
11526 demand_empty_rest_of_line ();
11529 void
11530 mips_flush_pending_output ()
11532 mips_emit_delays (false);
11533 mips_clear_insn_labels ();
11536 static void
11537 s_change_sec (sec)
11538 int sec;
11540 segT seg;
11542 /* When generating embedded PIC code, we only use the .text, .lit8,
11543 .sdata and .sbss sections. We change the .data and .rdata
11544 pseudo-ops to use .sdata. */
11545 if (mips_pic == EMBEDDED_PIC
11546 && (sec == 'd' || sec == 'r'))
11547 sec = 's';
11549 #ifdef OBJ_ELF
11550 /* The ELF backend needs to know that we are changing sections, so
11551 that .previous works correctly. We could do something like check
11552 for an obj_section_change_hook macro, but that might be confusing
11553 as it would not be appropriate to use it in the section changing
11554 functions in read.c, since obj-elf.c intercepts those. FIXME:
11555 This should be cleaner, somehow. */
11556 obj_elf_section_change_hook ();
11557 #endif
11559 mips_emit_delays (false);
11560 switch (sec)
11562 case 't':
11563 s_text (0);
11564 break;
11565 case 'd':
11566 s_data (0);
11567 break;
11568 case 'b':
11569 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11570 demand_empty_rest_of_line ();
11571 break;
11573 case 'r':
11574 if (USE_GLOBAL_POINTER_OPT)
11576 seg = subseg_new (RDATA_SECTION_NAME,
11577 (subsegT) get_absolute_expression ());
11578 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11580 bfd_set_section_flags (stdoutput, seg,
11581 (SEC_ALLOC
11582 | SEC_LOAD
11583 | SEC_READONLY
11584 | SEC_RELOC
11585 | SEC_DATA));
11586 if (strcmp (TARGET_OS, "elf") != 0)
11587 record_alignment (seg, 4);
11589 demand_empty_rest_of_line ();
11591 else
11593 as_bad (_("No read only data section in this object file format"));
11594 demand_empty_rest_of_line ();
11595 return;
11597 break;
11599 case 's':
11600 if (USE_GLOBAL_POINTER_OPT)
11602 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11603 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11605 bfd_set_section_flags (stdoutput, seg,
11606 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11607 | SEC_DATA);
11608 if (strcmp (TARGET_OS, "elf") != 0)
11609 record_alignment (seg, 4);
11611 demand_empty_rest_of_line ();
11612 break;
11614 else
11616 as_bad (_("Global pointers not supported; recompile -G 0"));
11617 demand_empty_rest_of_line ();
11618 return;
11622 auto_align = 1;
11625 void
11626 s_change_section (ignore)
11627 int ignore ATTRIBUTE_UNUSED;
11629 #ifdef OBJ_ELF
11630 char *section_name;
11631 char c;
11632 char next_c;
11633 int section_type;
11634 int section_flag;
11635 int section_entry_size;
11636 int section_alignment;
11638 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11639 return;
11641 section_name = input_line_pointer;
11642 c = get_symbol_end ();
11643 next_c = *(input_line_pointer + 1);
11645 /* Do we have .section Name<,"flags">? */
11646 if (c != ',' || (c == ',' && next_c == '"'))
11648 /* just after name is now '\0'. */
11649 *input_line_pointer = c;
11650 input_line_pointer = section_name;
11651 obj_elf_section (ignore);
11652 return;
11654 input_line_pointer++;
11656 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11657 if (c == ',')
11658 section_type = get_absolute_expression ();
11659 else
11660 section_type = 0;
11661 if (*input_line_pointer++ == ',')
11662 section_flag = get_absolute_expression ();
11663 else
11664 section_flag = 0;
11665 if (*input_line_pointer++ == ',')
11666 section_entry_size = get_absolute_expression ();
11667 else
11668 section_entry_size = 0;
11669 if (*input_line_pointer++ == ',')
11670 section_alignment = get_absolute_expression ();
11671 else
11672 section_alignment = 0;
11674 obj_elf_change_section (section_name, section_type, section_flag,
11675 section_entry_size, 0, 0, 0);
11676 #endif /* OBJ_ELF */
11679 void
11680 mips_enable_auto_align ()
11682 auto_align = 1;
11685 static void
11686 s_cons (log_size)
11687 int log_size;
11689 symbolS *label;
11691 label = insn_labels != NULL ? insn_labels->label : NULL;
11692 mips_emit_delays (false);
11693 if (log_size > 0 && auto_align)
11694 mips_align (log_size, 0, label);
11695 mips_clear_insn_labels ();
11696 cons (1 << log_size);
11699 static void
11700 s_float_cons (type)
11701 int type;
11703 symbolS *label;
11705 label = insn_labels != NULL ? insn_labels->label : NULL;
11707 mips_emit_delays (false);
11709 if (auto_align)
11711 if (type == 'd')
11712 mips_align (3, 0, label);
11713 else
11714 mips_align (2, 0, label);
11717 mips_clear_insn_labels ();
11719 float_cons (type);
11722 /* Handle .globl. We need to override it because on Irix 5 you are
11723 permitted to say
11724 .globl foo .text
11725 where foo is an undefined symbol, to mean that foo should be
11726 considered to be the address of a function. */
11728 static void
11729 s_mips_globl (x)
11730 int x ATTRIBUTE_UNUSED;
11732 char *name;
11733 int c;
11734 symbolS *symbolP;
11735 flagword flag;
11737 name = input_line_pointer;
11738 c = get_symbol_end ();
11739 symbolP = symbol_find_or_make (name);
11740 *input_line_pointer = c;
11741 SKIP_WHITESPACE ();
11743 /* On Irix 5, every global symbol that is not explicitly labelled as
11744 being a function is apparently labelled as being an object. */
11745 flag = BSF_OBJECT;
11747 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11749 char *secname;
11750 asection *sec;
11752 secname = input_line_pointer;
11753 c = get_symbol_end ();
11754 sec = bfd_get_section_by_name (stdoutput, secname);
11755 if (sec == NULL)
11756 as_bad (_("%s: no such section"), secname);
11757 *input_line_pointer = c;
11759 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11760 flag = BSF_FUNCTION;
11763 symbol_get_bfdsym (symbolP)->flags |= flag;
11765 S_SET_EXTERNAL (symbolP);
11766 demand_empty_rest_of_line ();
11769 static void
11770 s_option (x)
11771 int x ATTRIBUTE_UNUSED;
11773 char *opt;
11774 char c;
11776 opt = input_line_pointer;
11777 c = get_symbol_end ();
11779 if (*opt == 'O')
11781 /* FIXME: What does this mean? */
11783 else if (strncmp (opt, "pic", 3) == 0)
11785 int i;
11787 i = atoi (opt + 3);
11788 if (i == 0)
11789 mips_pic = NO_PIC;
11790 else if (i == 2)
11791 mips_pic = SVR4_PIC;
11792 else
11793 as_bad (_(".option pic%d not supported"), i);
11795 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11797 if (g_switch_seen && g_switch_value != 0)
11798 as_warn (_("-G may not be used with SVR4 PIC code"));
11799 g_switch_value = 0;
11800 bfd_set_gp_size (stdoutput, 0);
11803 else
11804 as_warn (_("Unrecognized option \"%s\""), opt);
11806 *input_line_pointer = c;
11807 demand_empty_rest_of_line ();
11810 /* This structure is used to hold a stack of .set values. */
11812 struct mips_option_stack
11814 struct mips_option_stack *next;
11815 struct mips_set_options options;
11818 static struct mips_option_stack *mips_opts_stack;
11820 /* Handle the .set pseudo-op. */
11822 static void
11823 s_mipsset (x)
11824 int x ATTRIBUTE_UNUSED;
11826 char *name = input_line_pointer, ch;
11828 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11829 ++input_line_pointer;
11830 ch = *input_line_pointer;
11831 *input_line_pointer = '\0';
11833 if (strcmp (name, "reorder") == 0)
11835 if (mips_opts.noreorder && prev_nop_frag != NULL)
11837 /* If we still have pending nops, we can discard them. The
11838 usual nop handling will insert any that are still
11839 needed. */
11840 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11841 * (mips_opts.mips16 ? 2 : 4));
11842 prev_nop_frag = NULL;
11844 mips_opts.noreorder = 0;
11846 else if (strcmp (name, "noreorder") == 0)
11848 mips_emit_delays (true);
11849 mips_opts.noreorder = 1;
11850 mips_any_noreorder = 1;
11852 else if (strcmp (name, "at") == 0)
11854 mips_opts.noat = 0;
11856 else if (strcmp (name, "noat") == 0)
11858 mips_opts.noat = 1;
11860 else if (strcmp (name, "macro") == 0)
11862 mips_opts.warn_about_macros = 0;
11864 else if (strcmp (name, "nomacro") == 0)
11866 if (mips_opts.noreorder == 0)
11867 as_bad (_("`noreorder' must be set before `nomacro'"));
11868 mips_opts.warn_about_macros = 1;
11870 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11872 mips_opts.nomove = 0;
11874 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11876 mips_opts.nomove = 1;
11878 else if (strcmp (name, "bopt") == 0)
11880 mips_opts.nobopt = 0;
11882 else if (strcmp (name, "nobopt") == 0)
11884 mips_opts.nobopt = 1;
11886 else if (strcmp (name, "mips16") == 0
11887 || strcmp (name, "MIPS-16") == 0)
11888 mips_opts.mips16 = 1;
11889 else if (strcmp (name, "nomips16") == 0
11890 || strcmp (name, "noMIPS-16") == 0)
11891 mips_opts.mips16 = 0;
11892 else if (strcmp (name, "mips3d") == 0)
11893 mips_opts.ase_mips3d = 1;
11894 else if (strcmp (name, "nomips3d") == 0)
11895 mips_opts.ase_mips3d = 0;
11896 else if (strcmp (name, "mdmx") == 0)
11897 mips_opts.ase_mdmx = 1;
11898 else if (strcmp (name, "nomdmx") == 0)
11899 mips_opts.ase_mdmx = 0;
11900 else if (strncmp (name, "mips", 4) == 0)
11902 int isa;
11904 /* Permit the user to change the ISA on the fly. Needless to
11905 say, misuse can cause serious problems. */
11906 isa = atoi (name + 4);
11907 switch (isa)
11909 case 0:
11910 mips_opts.gp32 = file_mips_gp32;
11911 mips_opts.fp32 = file_mips_fp32;
11912 break;
11913 case 1:
11914 case 2:
11915 case 32:
11916 mips_opts.gp32 = 1;
11917 mips_opts.fp32 = 1;
11918 break;
11919 case 3:
11920 case 4:
11921 case 5:
11922 case 64:
11923 mips_opts.gp32 = 0;
11924 mips_opts.fp32 = 0;
11925 break;
11926 default:
11927 as_bad (_("unknown ISA level %s"), name + 4);
11928 break;
11931 switch (isa)
11933 case 0: mips_opts.isa = file_mips_isa; break;
11934 case 1: mips_opts.isa = ISA_MIPS1; break;
11935 case 2: mips_opts.isa = ISA_MIPS2; break;
11936 case 3: mips_opts.isa = ISA_MIPS3; break;
11937 case 4: mips_opts.isa = ISA_MIPS4; break;
11938 case 5: mips_opts.isa = ISA_MIPS5; break;
11939 case 32: mips_opts.isa = ISA_MIPS32; break;
11940 case 64: mips_opts.isa = ISA_MIPS64; break;
11941 default: as_bad (_("unknown ISA level %s"), name + 4); break;
11944 else if (strcmp (name, "autoextend") == 0)
11945 mips_opts.noautoextend = 0;
11946 else if (strcmp (name, "noautoextend") == 0)
11947 mips_opts.noautoextend = 1;
11948 else if (strcmp (name, "push") == 0)
11950 struct mips_option_stack *s;
11952 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11953 s->next = mips_opts_stack;
11954 s->options = mips_opts;
11955 mips_opts_stack = s;
11957 else if (strcmp (name, "pop") == 0)
11959 struct mips_option_stack *s;
11961 s = mips_opts_stack;
11962 if (s == NULL)
11963 as_bad (_(".set pop with no .set push"));
11964 else
11966 /* If we're changing the reorder mode we need to handle
11967 delay slots correctly. */
11968 if (s->options.noreorder && ! mips_opts.noreorder)
11969 mips_emit_delays (true);
11970 else if (! s->options.noreorder && mips_opts.noreorder)
11972 if (prev_nop_frag != NULL)
11974 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11975 * (mips_opts.mips16 ? 2 : 4));
11976 prev_nop_frag = NULL;
11980 mips_opts = s->options;
11981 mips_opts_stack = s->next;
11982 free (s);
11985 else
11987 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11989 *input_line_pointer = ch;
11990 demand_empty_rest_of_line ();
11993 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11994 .option pic2. It means to generate SVR4 PIC calls. */
11996 static void
11997 s_abicalls (ignore)
11998 int ignore ATTRIBUTE_UNUSED;
12000 mips_pic = SVR4_PIC;
12001 if (USE_GLOBAL_POINTER_OPT)
12003 if (g_switch_seen && g_switch_value != 0)
12004 as_warn (_("-G may not be used with SVR4 PIC code"));
12005 g_switch_value = 0;
12007 bfd_set_gp_size (stdoutput, 0);
12008 demand_empty_rest_of_line ();
12011 /* Handle the .cpload pseudo-op. This is used when generating SVR4
12012 PIC code. It sets the $gp register for the function based on the
12013 function address, which is in the register named in the argument.
12014 This uses a relocation against _gp_disp, which is handled specially
12015 by the linker. The result is:
12016 lui $gp,%hi(_gp_disp)
12017 addiu $gp,$gp,%lo(_gp_disp)
12018 addu $gp,$gp,.cpload argument
12019 The .cpload argument is normally $25 == $t9. */
12021 static void
12022 s_cpload (ignore)
12023 int ignore ATTRIBUTE_UNUSED;
12025 expressionS ex;
12026 int icnt = 0;
12028 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12029 .cpload is ignored. */
12030 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12032 s_ignore (0);
12033 return;
12036 /* .cpload should be in a .set noreorder section. */
12037 if (mips_opts.noreorder == 0)
12038 as_warn (_(".cpload not in noreorder section"));
12040 ex.X_op = O_symbol;
12041 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12042 ex.X_op_symbol = NULL;
12043 ex.X_add_number = 0;
12045 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12046 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12048 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12049 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12050 mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12052 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12053 mips_gp_register, mips_gp_register, tc_get_register (0));
12055 demand_empty_rest_of_line ();
12058 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12059 .cpsetup $reg1, offset|$reg2, label
12061 If offset is given, this results in:
12062 sd $gp, offset($sp)
12063 lui $gp, %hi(%neg(%gp_rel(label)))
12064 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12065 daddu $gp, $gp, $reg1
12067 If $reg2 is given, this results in:
12068 daddu $reg2, $gp, $0
12069 lui $gp, %hi(%neg(%gp_rel(label)))
12070 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12071 daddu $gp, $gp, $reg1
12072 $reg1 is normally $25 == $t9. */
12073 static void
12074 s_cpsetup (ignore)
12075 int ignore ATTRIBUTE_UNUSED;
12077 expressionS ex_off;
12078 expressionS ex_sym;
12079 int reg1;
12080 int icnt = 0;
12081 char *f;
12083 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12084 We also need NewABI support. */
12085 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12087 s_ignore (0);
12088 return;
12091 reg1 = tc_get_register (0);
12092 SKIP_WHITESPACE ();
12093 if (*input_line_pointer != ',')
12095 as_bad (_("missing argument separator ',' for .cpsetup"));
12096 return;
12098 else
12099 ++input_line_pointer;
12100 SKIP_WHITESPACE ();
12101 if (*input_line_pointer == '$')
12103 mips_cpreturn_register = tc_get_register (0);
12104 mips_cpreturn_offset = -1;
12106 else
12108 mips_cpreturn_offset = get_absolute_expression ();
12109 mips_cpreturn_register = -1;
12111 SKIP_WHITESPACE ();
12112 if (*input_line_pointer != ',')
12114 as_bad (_("missing argument separator ',' for .cpsetup"));
12115 return;
12117 else
12118 ++input_line_pointer;
12119 SKIP_WHITESPACE ();
12120 expression (&ex_sym);
12122 if (mips_cpreturn_register == -1)
12124 ex_off.X_op = O_constant;
12125 ex_off.X_add_symbol = NULL;
12126 ex_off.X_op_symbol = NULL;
12127 ex_off.X_add_number = mips_cpreturn_offset;
12129 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12130 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12132 else
12133 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12134 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12136 /* Ensure there's room for the next two instructions, so that `f'
12137 doesn't end up with an address in the wrong frag. */
12138 frag_grow (8);
12139 f = frag_more (0);
12140 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12141 (int) BFD_RELOC_GPREL16);
12142 fix_new (frag_now, f - frag_now->fr_literal,
12143 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12144 fix_new (frag_now, f - frag_now->fr_literal,
12145 0, NULL, 0, 0, BFD_RELOC_HI16_S);
12147 f = frag_more (0);
12148 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12149 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12150 fix_new (frag_now, f - frag_now->fr_literal,
12151 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12152 fix_new (frag_now, f - frag_now->fr_literal,
12153 0, NULL, 0, 0, BFD_RELOC_LO16);
12155 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12156 HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
12157 mips_gp_register, mips_gp_register, reg1);
12159 demand_empty_rest_of_line ();
12162 static void
12163 s_cplocal (ignore)
12164 int ignore ATTRIBUTE_UNUSED;
12166 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12167 .cplocal is ignored. */
12168 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12170 s_ignore (0);
12171 return;
12174 mips_gp_register = tc_get_register (0);
12175 demand_empty_rest_of_line ();
12178 /* Handle the .cprestore pseudo-op. This stores $gp into a given
12179 offset from $sp. The offset is remembered, and after making a PIC
12180 call $gp is restored from that location. */
12182 static void
12183 s_cprestore (ignore)
12184 int ignore ATTRIBUTE_UNUSED;
12186 expressionS ex;
12187 int icnt = 0;
12189 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12190 .cprestore is ignored. */
12191 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12193 s_ignore (0);
12194 return;
12197 mips_cprestore_offset = get_absolute_expression ();
12198 mips_cprestore_valid = 1;
12200 ex.X_op = O_constant;
12201 ex.X_add_symbol = NULL;
12202 ex.X_op_symbol = NULL;
12203 ex.X_add_number = mips_cprestore_offset;
12205 macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12206 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12207 mips_gp_register, SP);
12209 demand_empty_rest_of_line ();
12212 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12213 was given in the preceeding .gpsetup, it results in:
12214 ld $gp, offset($sp)
12216 If a register $reg2 was given there, it results in:
12217 daddiu $gp, $gp, $reg2
12219 static void
12220 s_cpreturn (ignore)
12221 int ignore ATTRIBUTE_UNUSED;
12223 expressionS ex;
12224 int icnt = 0;
12226 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12227 We also need NewABI support. */
12228 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12230 s_ignore (0);
12231 return;
12234 if (mips_cpreturn_register == -1)
12236 ex.X_op = O_constant;
12237 ex.X_add_symbol = NULL;
12238 ex.X_op_symbol = NULL;
12239 ex.X_add_number = mips_cpreturn_offset;
12241 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12242 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12244 else
12245 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12246 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12248 demand_empty_rest_of_line ();
12251 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12252 code. It sets the offset to use in gp_rel relocations. */
12254 static void
12255 s_gpvalue (ignore)
12256 int ignore ATTRIBUTE_UNUSED;
12258 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12259 We also need NewABI support. */
12260 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12262 s_ignore (0);
12263 return;
12266 mips_gprel_offset = get_absolute_expression ();
12268 demand_empty_rest_of_line ();
12271 /* Handle the .gpword pseudo-op. This is used when generating PIC
12272 code. It generates a 32 bit GP relative reloc. */
12274 static void
12275 s_gpword (ignore)
12276 int ignore ATTRIBUTE_UNUSED;
12278 symbolS *label;
12279 expressionS ex;
12280 char *p;
12282 /* When not generating PIC code, this is treated as .word. */
12283 if (mips_pic != SVR4_PIC)
12285 s_cons (2);
12286 return;
12289 label = insn_labels != NULL ? insn_labels->label : NULL;
12290 mips_emit_delays (true);
12291 if (auto_align)
12292 mips_align (2, 0, label);
12293 mips_clear_insn_labels ();
12295 expression (&ex);
12297 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12299 as_bad (_("Unsupported use of .gpword"));
12300 ignore_rest_of_line ();
12303 p = frag_more (4);
12304 md_number_to_chars (p, (valueT) 0, 4);
12305 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
12306 BFD_RELOC_GPREL32);
12308 demand_empty_rest_of_line ();
12311 static void
12312 s_gpdword (ignore)
12313 int ignore ATTRIBUTE_UNUSED;
12315 symbolS *label;
12316 expressionS ex;
12317 char *p;
12319 /* When not generating PIC code, this is treated as .dword. */
12320 if (mips_pic != SVR4_PIC)
12322 s_cons (3);
12323 return;
12326 label = insn_labels != NULL ? insn_labels->label : NULL;
12327 mips_emit_delays (true);
12328 if (auto_align)
12329 mips_align (3, 0, label);
12330 mips_clear_insn_labels ();
12332 expression (&ex);
12334 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12336 as_bad (_("Unsupported use of .gpdword"));
12337 ignore_rest_of_line ();
12340 p = frag_more (8);
12341 md_number_to_chars (p, (valueT) 0, 8);
12342 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, false,
12343 BFD_RELOC_GPREL32);
12345 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12346 ex.X_op = O_absent;
12347 ex.X_add_symbol = 0;
12348 ex.X_add_number = 0;
12349 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, false,
12350 BFD_RELOC_64);
12352 demand_empty_rest_of_line ();
12355 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12356 tables in SVR4 PIC code. */
12358 static void
12359 s_cpadd (ignore)
12360 int ignore ATTRIBUTE_UNUSED;
12362 int icnt = 0;
12363 int reg;
12365 /* This is ignored when not generating SVR4 PIC code. */
12366 if (mips_pic != SVR4_PIC)
12368 s_ignore (0);
12369 return;
12372 /* Add $gp to the register named as an argument. */
12373 reg = tc_get_register (0);
12374 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12375 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12376 "d,v,t", reg, reg, mips_gp_register);
12378 demand_empty_rest_of_line ();
12381 /* Handle the .insn pseudo-op. This marks instruction labels in
12382 mips16 mode. This permits the linker to handle them specially,
12383 such as generating jalx instructions when needed. We also make
12384 them odd for the duration of the assembly, in order to generate the
12385 right sort of code. We will make them even in the adjust_symtab
12386 routine, while leaving them marked. This is convenient for the
12387 debugger and the disassembler. The linker knows to make them odd
12388 again. */
12390 static void
12391 s_insn (ignore)
12392 int ignore ATTRIBUTE_UNUSED;
12394 mips16_mark_labels ();
12396 demand_empty_rest_of_line ();
12399 /* Handle a .stabn directive. We need these in order to mark a label
12400 as being a mips16 text label correctly. Sometimes the compiler
12401 will emit a label, followed by a .stabn, and then switch sections.
12402 If the label and .stabn are in mips16 mode, then the label is
12403 really a mips16 text label. */
12405 static void
12406 s_mips_stab (type)
12407 int type;
12409 if (type == 'n')
12410 mips16_mark_labels ();
12412 s_stab (type);
12415 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12418 static void
12419 s_mips_weakext (ignore)
12420 int ignore ATTRIBUTE_UNUSED;
12422 char *name;
12423 int c;
12424 symbolS *symbolP;
12425 expressionS exp;
12427 name = input_line_pointer;
12428 c = get_symbol_end ();
12429 symbolP = symbol_find_or_make (name);
12430 S_SET_WEAK (symbolP);
12431 *input_line_pointer = c;
12433 SKIP_WHITESPACE ();
12435 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12437 if (S_IS_DEFINED (symbolP))
12439 as_bad ("ignoring attempt to redefine symbol %s",
12440 S_GET_NAME (symbolP));
12441 ignore_rest_of_line ();
12442 return;
12445 if (*input_line_pointer == ',')
12447 ++input_line_pointer;
12448 SKIP_WHITESPACE ();
12451 expression (&exp);
12452 if (exp.X_op != O_symbol)
12454 as_bad ("bad .weakext directive");
12455 ignore_rest_of_line ();
12456 return;
12458 symbol_set_value_expression (symbolP, &exp);
12461 demand_empty_rest_of_line ();
12464 /* Parse a register string into a number. Called from the ECOFF code
12465 to parse .frame. The argument is non-zero if this is the frame
12466 register, so that we can record it in mips_frame_reg. */
12469 tc_get_register (frame)
12470 int frame;
12472 int reg;
12474 SKIP_WHITESPACE ();
12475 if (*input_line_pointer++ != '$')
12477 as_warn (_("expected `$'"));
12478 reg = ZERO;
12480 else if (ISDIGIT (*input_line_pointer))
12482 reg = get_absolute_expression ();
12483 if (reg < 0 || reg >= 32)
12485 as_warn (_("Bad register number"));
12486 reg = ZERO;
12489 else
12491 if (strncmp (input_line_pointer, "ra", 2) == 0)
12493 reg = RA;
12494 input_line_pointer += 2;
12496 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12498 reg = FP;
12499 input_line_pointer += 2;
12501 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12503 reg = SP;
12504 input_line_pointer += 2;
12506 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12508 reg = GP;
12509 input_line_pointer += 2;
12511 else if (strncmp (input_line_pointer, "at", 2) == 0)
12513 reg = AT;
12514 input_line_pointer += 2;
12516 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12518 reg = KT0;
12519 input_line_pointer += 3;
12521 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12523 reg = KT1;
12524 input_line_pointer += 3;
12526 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12528 reg = ZERO;
12529 input_line_pointer += 4;
12531 else
12533 as_warn (_("Unrecognized register name"));
12534 reg = ZERO;
12535 while (ISALNUM(*input_line_pointer))
12536 input_line_pointer++;
12539 if (frame)
12541 mips_frame_reg = reg != 0 ? reg : SP;
12542 mips_frame_reg_valid = 1;
12543 mips_cprestore_valid = 0;
12545 return reg;
12548 valueT
12549 md_section_align (seg, addr)
12550 asection *seg;
12551 valueT addr;
12553 int align = bfd_get_section_alignment (stdoutput, seg);
12555 #ifdef OBJ_ELF
12556 /* We don't need to align ELF sections to the full alignment.
12557 However, Irix 5 may prefer that we align them at least to a 16
12558 byte boundary. We don't bother to align the sections if we are
12559 targeted for an embedded system. */
12560 if (strcmp (TARGET_OS, "elf") == 0)
12561 return addr;
12562 if (align > 4)
12563 align = 4;
12564 #endif
12566 return ((addr + (1 << align) - 1) & (-1 << align));
12569 /* Utility routine, called from above as well. If called while the
12570 input file is still being read, it's only an approximation. (For
12571 example, a symbol may later become defined which appeared to be
12572 undefined earlier.) */
12574 static int
12575 nopic_need_relax (sym, before_relaxing)
12576 symbolS *sym;
12577 int before_relaxing;
12579 if (sym == 0)
12580 return 0;
12582 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12584 const char *symname;
12585 int change;
12587 /* Find out whether this symbol can be referenced off the $gp
12588 register. It can be if it is smaller than the -G size or if
12589 it is in the .sdata or .sbss section. Certain symbols can
12590 not be referenced off the $gp, although it appears as though
12591 they can. */
12592 symname = S_GET_NAME (sym);
12593 if (symname != (const char *) NULL
12594 && (strcmp (symname, "eprol") == 0
12595 || strcmp (symname, "etext") == 0
12596 || strcmp (symname, "_gp") == 0
12597 || strcmp (symname, "edata") == 0
12598 || strcmp (symname, "_fbss") == 0
12599 || strcmp (symname, "_fdata") == 0
12600 || strcmp (symname, "_ftext") == 0
12601 || strcmp (symname, "end") == 0
12602 || strcmp (symname, "_gp_disp") == 0))
12603 change = 1;
12604 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12605 && (0
12606 #ifndef NO_ECOFF_DEBUGGING
12607 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12608 && (symbol_get_obj (sym)->ecoff_extern_size
12609 <= g_switch_value))
12610 #endif
12611 /* We must defer this decision until after the whole
12612 file has been read, since there might be a .extern
12613 after the first use of this symbol. */
12614 || (before_relaxing
12615 #ifndef NO_ECOFF_DEBUGGING
12616 && symbol_get_obj (sym)->ecoff_extern_size == 0
12617 #endif
12618 && S_GET_VALUE (sym) == 0)
12619 || (S_GET_VALUE (sym) != 0
12620 && S_GET_VALUE (sym) <= g_switch_value)))
12621 change = 0;
12622 else
12624 const char *segname;
12626 segname = segment_name (S_GET_SEGMENT (sym));
12627 assert (strcmp (segname, ".lit8") != 0
12628 && strcmp (segname, ".lit4") != 0);
12629 change = (strcmp (segname, ".sdata") != 0
12630 && strcmp (segname, ".sbss") != 0
12631 && strncmp (segname, ".sdata.", 7) != 0
12632 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12634 return change;
12636 else
12637 /* We are not optimizing for the $gp register. */
12638 return 1;
12641 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12642 extended opcode. SEC is the section the frag is in. */
12644 static int
12645 mips16_extended_frag (fragp, sec, stretch)
12646 fragS *fragp;
12647 asection *sec;
12648 long stretch;
12650 int type;
12651 register const struct mips16_immed_operand *op;
12652 offsetT val;
12653 int mintiny, maxtiny;
12654 segT symsec;
12655 fragS *sym_frag;
12657 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12658 return 0;
12659 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12660 return 1;
12662 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12663 op = mips16_immed_operands;
12664 while (op->type != type)
12666 ++op;
12667 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12670 if (op->unsp)
12672 if (type == '<' || type == '>' || type == '[' || type == ']')
12674 mintiny = 1;
12675 maxtiny = 1 << op->nbits;
12677 else
12679 mintiny = 0;
12680 maxtiny = (1 << op->nbits) - 1;
12683 else
12685 mintiny = - (1 << (op->nbits - 1));
12686 maxtiny = (1 << (op->nbits - 1)) - 1;
12689 sym_frag = symbol_get_frag (fragp->fr_symbol);
12690 val = S_GET_VALUE (fragp->fr_symbol);
12691 symsec = S_GET_SEGMENT (fragp->fr_symbol);
12693 if (op->pcrel)
12695 addressT addr;
12697 /* We won't have the section when we are called from
12698 mips_relax_frag. However, we will always have been called
12699 from md_estimate_size_before_relax first. If this is a
12700 branch to a different section, we mark it as such. If SEC is
12701 NULL, and the frag is not marked, then it must be a branch to
12702 the same section. */
12703 if (sec == NULL)
12705 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12706 return 1;
12708 else
12710 /* Must have been called from md_estimate_size_before_relax. */
12711 if (symsec != sec)
12713 fragp->fr_subtype =
12714 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12716 /* FIXME: We should support this, and let the linker
12717 catch branches and loads that are out of range. */
12718 as_bad_where (fragp->fr_file, fragp->fr_line,
12719 _("unsupported PC relative reference to different section"));
12721 return 1;
12723 if (fragp != sym_frag && sym_frag->fr_address == 0)
12724 /* Assume non-extended on the first relaxation pass.
12725 The address we have calculated will be bogus if this is
12726 a forward branch to another frag, as the forward frag
12727 will have fr_address == 0. */
12728 return 0;
12731 /* In this case, we know for sure that the symbol fragment is in
12732 the same section. If the relax_marker of the symbol fragment
12733 differs from the relax_marker of this fragment, we have not
12734 yet adjusted the symbol fragment fr_address. We want to add
12735 in STRETCH in order to get a better estimate of the address.
12736 This particularly matters because of the shift bits. */
12737 if (stretch != 0
12738 && sym_frag->relax_marker != fragp->relax_marker)
12740 fragS *f;
12742 /* Adjust stretch for any alignment frag. Note that if have
12743 been expanding the earlier code, the symbol may be
12744 defined in what appears to be an earlier frag. FIXME:
12745 This doesn't handle the fr_subtype field, which specifies
12746 a maximum number of bytes to skip when doing an
12747 alignment. */
12748 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12750 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12752 if (stretch < 0)
12753 stretch = - ((- stretch)
12754 & ~ ((1 << (int) f->fr_offset) - 1));
12755 else
12756 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12757 if (stretch == 0)
12758 break;
12761 if (f != NULL)
12762 val += stretch;
12765 addr = fragp->fr_address + fragp->fr_fix;
12767 /* The base address rules are complicated. The base address of
12768 a branch is the following instruction. The base address of a
12769 PC relative load or add is the instruction itself, but if it
12770 is in a delay slot (in which case it can not be extended) use
12771 the address of the instruction whose delay slot it is in. */
12772 if (type == 'p' || type == 'q')
12774 addr += 2;
12776 /* If we are currently assuming that this frag should be
12777 extended, then, the current address is two bytes
12778 higher. */
12779 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12780 addr += 2;
12782 /* Ignore the low bit in the target, since it will be set
12783 for a text label. */
12784 if ((val & 1) != 0)
12785 --val;
12787 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12788 addr -= 4;
12789 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12790 addr -= 2;
12792 val -= addr & ~ ((1 << op->shift) - 1);
12794 /* Branch offsets have an implicit 0 in the lowest bit. */
12795 if (type == 'p' || type == 'q')
12796 val /= 2;
12798 /* If any of the shifted bits are set, we must use an extended
12799 opcode. If the address depends on the size of this
12800 instruction, this can lead to a loop, so we arrange to always
12801 use an extended opcode. We only check this when we are in
12802 the main relaxation loop, when SEC is NULL. */
12803 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12805 fragp->fr_subtype =
12806 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12807 return 1;
12810 /* If we are about to mark a frag as extended because the value
12811 is precisely maxtiny + 1, then there is a chance of an
12812 infinite loop as in the following code:
12813 la $4,foo
12814 .skip 1020
12815 .align 2
12816 foo:
12817 In this case when the la is extended, foo is 0x3fc bytes
12818 away, so the la can be shrunk, but then foo is 0x400 away, so
12819 the la must be extended. To avoid this loop, we mark the
12820 frag as extended if it was small, and is about to become
12821 extended with a value of maxtiny + 1. */
12822 if (val == ((maxtiny + 1) << op->shift)
12823 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12824 && sec == NULL)
12826 fragp->fr_subtype =
12827 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12828 return 1;
12831 else if (symsec != absolute_section && sec != NULL)
12832 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12834 if ((val & ((1 << op->shift) - 1)) != 0
12835 || val < (mintiny << op->shift)
12836 || val > (maxtiny << op->shift))
12837 return 1;
12838 else
12839 return 0;
12842 /* Compute the length of a branch sequence, and adjust the
12843 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12844 worst-case length is computed, with UPDATE being used to indicate
12845 whether an unconditional (-1), branch-likely (+1) or regular (0)
12846 branch is to be computed. */
12847 static int
12848 relaxed_branch_length (fragp, sec, update)
12849 fragS *fragp;
12850 asection *sec;
12851 int update;
12853 boolean toofar;
12854 int length;
12856 if (fragp
12857 && S_IS_DEFINED (fragp->fr_symbol)
12858 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12860 addressT addr;
12861 offsetT val;
12863 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12865 addr = fragp->fr_address + fragp->fr_fix + 4;
12867 val -= addr;
12869 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12871 else if (fragp)
12872 /* If the symbol is not defined or it's in a different segment,
12873 assume the user knows what's going on and emit a short
12874 branch. */
12875 toofar = false;
12876 else
12877 toofar = true;
12879 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12880 fragp->fr_subtype
12881 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_RELOC_S2 (fragp->fr_subtype),
12882 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12883 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12884 RELAX_BRANCH_LINK (fragp->fr_subtype),
12885 toofar);
12887 length = 4;
12888 if (toofar)
12890 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12891 length += 8;
12893 if (mips_pic != NO_PIC)
12895 /* Additional space for PIC loading of target address. */
12896 length += 8;
12897 if (mips_opts.isa == ISA_MIPS1)
12898 /* Additional space for $at-stabilizing nop. */
12899 length += 4;
12902 /* If branch is conditional. */
12903 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12904 length += 8;
12907 return length;
12910 /* Estimate the size of a frag before relaxing. Unless this is the
12911 mips16, we are not really relaxing here, and the final size is
12912 encoded in the subtype information. For the mips16, we have to
12913 decide whether we are using an extended opcode or not. */
12916 md_estimate_size_before_relax (fragp, segtype)
12917 fragS *fragp;
12918 asection *segtype;
12920 int change = 0;
12921 boolean linkonce = false;
12923 if (RELAX_BRANCH_P (fragp->fr_subtype))
12926 fragp->fr_var = relaxed_branch_length (fragp, segtype, false);
12928 return fragp->fr_var;
12931 if (RELAX_MIPS16_P (fragp->fr_subtype))
12932 /* We don't want to modify the EXTENDED bit here; it might get us
12933 into infinite loops. We change it only in mips_relax_frag(). */
12934 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12936 if (mips_pic == NO_PIC)
12938 change = nopic_need_relax (fragp->fr_symbol, 0);
12940 else if (mips_pic == SVR4_PIC)
12942 symbolS *sym;
12943 asection *symsec;
12945 sym = fragp->fr_symbol;
12947 /* Handle the case of a symbol equated to another symbol. */
12948 while (symbol_equated_reloc_p (sym))
12950 symbolS *n;
12952 /* It's possible to get a loop here in a badly written
12953 program. */
12954 n = symbol_get_value_expression (sym)->X_add_symbol;
12955 if (n == sym)
12956 break;
12957 sym = n;
12960 symsec = S_GET_SEGMENT (sym);
12962 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12963 if (symsec != segtype && ! S_IS_LOCAL (sym))
12965 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12966 != 0)
12967 linkonce = true;
12969 /* The GNU toolchain uses an extension for ELF: a section
12970 beginning with the magic string .gnu.linkonce is a linkonce
12971 section. */
12972 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12973 sizeof ".gnu.linkonce" - 1) == 0)
12974 linkonce = true;
12977 /* This must duplicate the test in adjust_reloc_syms. */
12978 change = (symsec != &bfd_und_section
12979 && symsec != &bfd_abs_section
12980 && ! bfd_is_com_section (symsec)
12981 && !linkonce
12982 #ifdef OBJ_ELF
12983 /* A global or weak symbol is treated as external. */
12984 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12985 || (! S_IS_WEAK (sym)
12986 && (! S_IS_EXTERNAL (sym)
12987 || mips_pic == EMBEDDED_PIC)))
12988 #endif
12991 else
12992 abort ();
12994 if (change)
12996 /* Record the offset to the first reloc in the fr_opcode field.
12997 This lets md_convert_frag and tc_gen_reloc know that the code
12998 must be expanded. */
12999 fragp->fr_opcode = (fragp->fr_literal
13000 + fragp->fr_fix
13001 - RELAX_OLD (fragp->fr_subtype)
13002 + RELAX_RELOC1 (fragp->fr_subtype));
13003 /* FIXME: This really needs as_warn_where. */
13004 if (RELAX_WARN (fragp->fr_subtype))
13005 as_warn (_("AT used after \".set noat\" or macro used after "
13006 "\".set nomacro\""));
13008 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13011 return 0;
13014 /* This is called to see whether a reloc against a defined symbol
13015 should be converted into a reloc against a section. Don't adjust
13016 MIPS16 jump relocations, so we don't have to worry about the format
13017 of the offset in the .o file. Don't adjust relocations against
13018 mips16 symbols, so that the linker can find them if it needs to set
13019 up a stub. */
13022 mips_fix_adjustable (fixp)
13023 fixS *fixp;
13025 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13026 return 0;
13028 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13029 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13030 return 0;
13032 if (fixp->fx_addsy == NULL)
13033 return 1;
13035 #ifdef OBJ_ELF
13036 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13037 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13038 && fixp->fx_subsy == NULL)
13039 return 0;
13040 #endif
13042 return 1;
13045 /* Translate internal representation of relocation info to BFD target
13046 format. */
13048 arelent **
13049 tc_gen_reloc (section, fixp)
13050 asection *section ATTRIBUTE_UNUSED;
13051 fixS *fixp;
13053 static arelent *retval[4];
13054 arelent *reloc;
13055 bfd_reloc_code_real_type code;
13057 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13058 retval[1] = NULL;
13060 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13061 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13062 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13064 if (mips_pic == EMBEDDED_PIC
13065 && SWITCH_TABLE (fixp))
13067 /* For a switch table entry we use a special reloc. The addend
13068 is actually the difference between the reloc address and the
13069 subtrahend. */
13070 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13071 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13072 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13073 fixp->fx_r_type = BFD_RELOC_GPREL32;
13075 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13077 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13078 reloc->addend = fixp->fx_addnumber;
13079 else
13081 /* We use a special addend for an internal RELLO reloc. */
13082 if (symbol_section_p (fixp->fx_addsy))
13083 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13084 else
13085 reloc->addend = fixp->fx_addnumber + reloc->address;
13088 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13090 assert (fixp->fx_next != NULL
13091 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13093 /* The reloc is relative to the RELLO; adjust the addend
13094 accordingly. */
13095 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13096 reloc->addend = fixp->fx_next->fx_addnumber;
13097 else
13099 /* We use a special addend for an internal RELHI reloc. */
13100 if (symbol_section_p (fixp->fx_addsy))
13101 reloc->addend = (fixp->fx_next->fx_frag->fr_address
13102 + fixp->fx_next->fx_where
13103 - S_GET_VALUE (fixp->fx_subsy));
13104 else
13105 reloc->addend = (fixp->fx_addnumber
13106 + fixp->fx_next->fx_frag->fr_address
13107 + fixp->fx_next->fx_where);
13110 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13111 reloc->addend = fixp->fx_addnumber;
13112 else
13114 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13115 /* A gruesome hack which is a result of the gruesome gas reloc
13116 handling. */
13117 reloc->addend = reloc->address;
13118 else
13119 reloc->addend = -reloc->address;
13122 /* If this is a variant frag, we may need to adjust the existing
13123 reloc and generate a new one. */
13124 if (fixp->fx_frag->fr_opcode != NULL
13125 && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13126 && ! HAVE_NEWABI)
13127 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13128 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13129 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13130 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13131 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13132 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13135 arelent *reloc2;
13137 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13139 /* If this is not the last reloc in this frag, then we have two
13140 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13141 CALL_HI16/CALL_LO16, both of which are being replaced. Let
13142 the second one handle all of them. */
13143 if (fixp->fx_next != NULL
13144 && fixp->fx_frag == fixp->fx_next->fx_frag)
13146 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13147 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13148 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13149 && (fixp->fx_next->fx_r_type
13150 == BFD_RELOC_MIPS_GOT_LO16))
13151 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13152 && (fixp->fx_next->fx_r_type
13153 == BFD_RELOC_MIPS_CALL_LO16)));
13154 retval[0] = NULL;
13155 return retval;
13158 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13159 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13160 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13161 retval[2] = NULL;
13162 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13163 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13164 reloc2->address = (reloc->address
13165 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13166 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13167 reloc2->addend = fixp->fx_addnumber;
13168 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13169 assert (reloc2->howto != NULL);
13171 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13173 arelent *reloc3;
13175 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13176 retval[3] = NULL;
13177 *reloc3 = *reloc2;
13178 reloc3->address += 4;
13181 if (mips_pic == NO_PIC)
13183 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13184 fixp->fx_r_type = BFD_RELOC_HI16_S;
13186 else if (mips_pic == SVR4_PIC)
13188 switch (fixp->fx_r_type)
13190 default:
13191 abort ();
13192 case BFD_RELOC_MIPS_GOT16:
13193 break;
13194 case BFD_RELOC_MIPS_GOT_LO16:
13195 case BFD_RELOC_MIPS_CALL_LO16:
13196 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13197 break;
13198 case BFD_RELOC_MIPS_CALL16:
13199 if (HAVE_NEWABI)
13201 /* BFD_RELOC_MIPS_GOT16;*/
13202 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13203 reloc2->howto = bfd_reloc_type_lookup
13204 (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13206 else
13207 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13208 break;
13211 else
13212 abort ();
13214 /* newabi uses R_MIPS_GOT_DISP for local symbols */
13215 if (HAVE_NEWABI && BFD_RELOC_MIPS_GOT_LO16)
13217 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13218 retval[1] = NULL;
13222 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13223 entry to be used in the relocation's section offset. */
13224 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13226 reloc->address = reloc->addend;
13227 reloc->addend = 0;
13230 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13231 fixup_segment converted a non-PC relative reloc into a PC
13232 relative reloc. In such a case, we need to convert the reloc
13233 code. */
13234 code = fixp->fx_r_type;
13235 if (fixp->fx_pcrel)
13237 switch (code)
13239 case BFD_RELOC_8:
13240 code = BFD_RELOC_8_PCREL;
13241 break;
13242 case BFD_RELOC_16:
13243 code = BFD_RELOC_16_PCREL;
13244 break;
13245 case BFD_RELOC_32:
13246 code = BFD_RELOC_32_PCREL;
13247 break;
13248 case BFD_RELOC_64:
13249 code = BFD_RELOC_64_PCREL;
13250 break;
13251 case BFD_RELOC_8_PCREL:
13252 case BFD_RELOC_16_PCREL:
13253 case BFD_RELOC_32_PCREL:
13254 case BFD_RELOC_64_PCREL:
13255 case BFD_RELOC_16_PCREL_S2:
13256 case BFD_RELOC_PCREL_HI16_S:
13257 case BFD_RELOC_PCREL_LO16:
13258 break;
13259 default:
13260 as_bad_where (fixp->fx_file, fixp->fx_line,
13261 _("Cannot make %s relocation PC relative"),
13262 bfd_get_reloc_code_name (code));
13266 #ifdef OBJ_ELF
13267 /* md_apply_fix3 has a double-subtraction hack to get
13268 bfd_install_relocation to behave nicely. GPREL relocations are
13269 handled correctly without this hack, so undo it here. We can't
13270 stop md_apply_fix3 from subtracting twice in the first place since
13271 the fake addend is required for variant frags above. */
13272 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
13273 && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
13274 && reloc->addend != 0
13275 && mips_need_elf_addend_fixup (fixp))
13276 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13277 #endif
13279 /* To support a PC relative reloc when generating embedded PIC code
13280 for ECOFF, we use a Cygnus extension. We check for that here to
13281 make sure that we don't let such a reloc escape normally. */
13282 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13283 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13284 && code == BFD_RELOC_16_PCREL_S2
13285 && mips_pic != EMBEDDED_PIC)
13286 reloc->howto = NULL;
13287 else
13288 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13290 if (reloc->howto == NULL)
13292 as_bad_where (fixp->fx_file, fixp->fx_line,
13293 _("Can not represent %s relocation in this object file format"),
13294 bfd_get_reloc_code_name (code));
13295 retval[0] = NULL;
13298 return retval;
13301 /* Relax a machine dependent frag. This returns the amount by which
13302 the current size of the frag should change. */
13305 mips_relax_frag (sec, fragp, stretch)
13306 asection *sec;
13307 fragS *fragp;
13308 long stretch;
13310 if (RELAX_BRANCH_P (fragp->fr_subtype))
13312 offsetT old_var = fragp->fr_var;
13314 fragp->fr_var = relaxed_branch_length (fragp, sec, true);
13316 return fragp->fr_var - old_var;
13319 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13320 return 0;
13322 if (mips16_extended_frag (fragp, NULL, stretch))
13324 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13325 return 0;
13326 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13327 return 2;
13329 else
13331 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13332 return 0;
13333 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13334 return -2;
13337 return 0;
13340 /* Convert a machine dependent frag. */
13342 void
13343 md_convert_frag (abfd, asec, fragp)
13344 bfd *abfd ATTRIBUTE_UNUSED;
13345 segT asec;
13346 fragS *fragp;
13348 int old, new;
13349 char *fixptr;
13351 if (RELAX_BRANCH_P (fragp->fr_subtype))
13353 bfd_byte *buf;
13354 unsigned long insn;
13355 expressionS exp;
13356 fixS *fixp;
13358 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13360 if (target_big_endian)
13361 insn = bfd_getb32 (buf);
13362 else
13363 insn = bfd_getl32 (buf);
13365 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13367 /* We generate a fixup instead of applying it right now
13368 because, if there are linker relaxations, we're going to
13369 need the relocations. */
13370 exp.X_op = O_symbol;
13371 exp.X_add_symbol = fragp->fr_symbol;
13372 exp.X_add_number = fragp->fr_offset;
13374 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13375 4, &exp, 1,
13376 RELAX_BRANCH_RELOC_S2 (fragp->fr_subtype)
13377 ? BFD_RELOC_16_PCREL_S2
13378 : BFD_RELOC_16_PCREL);
13379 fixp->fx_file = fragp->fr_file;
13380 fixp->fx_line = fragp->fr_line;
13382 md_number_to_chars ((char *)buf, insn, 4);
13383 buf += 4;
13385 else
13387 int i;
13389 as_warn_where (fragp->fr_file, fragp->fr_line,
13390 _("relaxed out-of-range branch into a jump"));
13392 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13393 goto uncond;
13395 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13397 /* Reverse the branch. */
13398 switch ((insn >> 28) & 0xf)
13400 case 4:
13401 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13402 have the condition reversed by tweaking a single
13403 bit, and their opcodes all have 0x4???????. */
13404 assert ((insn & 0xf1000000) == 0x41000000);
13405 insn ^= 0x00010000;
13406 break;
13408 case 0:
13409 /* bltz 0x04000000 bgez 0x04010000
13410 bltzal 0x04100000 bgezal 0x04110000 */
13411 assert ((insn & 0xfc0e0000) == 0x04000000);
13412 insn ^= 0x00010000;
13413 break;
13415 case 1:
13416 /* beq 0x10000000 bne 0x14000000
13417 blez 0x18000000 bgtz 0x1c000000 */
13418 insn ^= 0x04000000;
13419 break;
13421 default:
13422 abort ();
13426 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13428 /* Clear the and-link bit. */
13429 assert ((insn & 0xfc1c0000) == 0x04100000);
13431 /* bltzal 0x04100000 bgezal 0x04110000
13432 bltzall 0x04120000 bgezall 0x04130000 */
13433 insn &= ~0x00100000;
13436 /* Branch over the branch (if the branch was likely) or the
13437 full jump (not likely case). Compute the offset from the
13438 current instruction to branch to. */
13439 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13440 i = 16;
13441 else
13443 /* How many bytes in instructions we've already emitted? */
13444 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13445 /* How many bytes in instructions from here to the end? */
13446 i = fragp->fr_var - i;
13448 /* Convert to instruction count. */
13449 i >>= 2;
13450 /* Branch counts from the next instruction. */
13451 i--;
13452 insn |= i;
13453 /* Branch over the jump. */
13454 md_number_to_chars ((char *)buf, insn, 4);
13455 buf += 4;
13457 /* Nop */
13458 md_number_to_chars ((char*)buf, 0, 4);
13459 buf += 4;
13461 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13463 /* beql $0, $0, 2f */
13464 insn = 0x50000000;
13465 /* Compute the PC offset from the current instruction to
13466 the end of the variable frag. */
13467 /* How many bytes in instructions we've already emitted? */
13468 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13469 /* How many bytes in instructions from here to the end? */
13470 i = fragp->fr_var - i;
13471 /* Convert to instruction count. */
13472 i >>= 2;
13473 /* Don't decrement i, because we want to branch over the
13474 delay slot. */
13476 insn |= i;
13477 md_number_to_chars ((char *)buf, insn, 4);
13478 buf += 4;
13480 md_number_to_chars ((char *)buf, 0, 4);
13481 buf += 4;
13484 uncond:
13485 if (mips_pic == NO_PIC)
13487 /* j or jal. */
13488 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13489 ? 0x0c000000 : 0x08000000);
13490 exp.X_op = O_symbol;
13491 exp.X_add_symbol = fragp->fr_symbol;
13492 exp.X_add_number = fragp->fr_offset;
13494 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13495 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13496 fixp->fx_file = fragp->fr_file;
13497 fixp->fx_line = fragp->fr_line;
13499 md_number_to_chars ((char*)buf, insn, 4);
13500 buf += 4;
13502 else
13504 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13505 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13506 exp.X_op = O_symbol;
13507 exp.X_add_symbol = fragp->fr_symbol;
13508 exp.X_add_number = fragp->fr_offset;
13510 if (fragp->fr_offset)
13512 exp.X_add_symbol = make_expr_symbol (&exp);
13513 exp.X_add_number = 0;
13516 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13517 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13518 fixp->fx_file = fragp->fr_file;
13519 fixp->fx_line = fragp->fr_line;
13521 md_number_to_chars ((char*)buf, insn, 4);
13522 buf += 4;
13524 if (mips_opts.isa == ISA_MIPS1)
13526 /* nop */
13527 md_number_to_chars ((char*)buf, 0, 4);
13528 buf += 4;
13531 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13532 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13534 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13535 4, &exp, 0, BFD_RELOC_LO16);
13536 fixp->fx_file = fragp->fr_file;
13537 fixp->fx_line = fragp->fr_line;
13539 md_number_to_chars ((char*)buf, insn, 4);
13540 buf += 4;
13542 /* j(al)r $at. */
13543 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13544 insn = 0x0020f809;
13545 else
13546 insn = 0x00200008;
13548 md_number_to_chars ((char*)buf, insn, 4);
13549 buf += 4;
13553 assert (buf == (bfd_byte *)fragp->fr_literal
13554 + fragp->fr_fix + fragp->fr_var);
13556 fragp->fr_fix += fragp->fr_var;
13558 return;
13561 if (RELAX_MIPS16_P (fragp->fr_subtype))
13563 int type;
13564 register const struct mips16_immed_operand *op;
13565 boolean small, ext;
13566 offsetT val;
13567 bfd_byte *buf;
13568 unsigned long insn;
13569 boolean use_extend;
13570 unsigned short extend;
13572 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13573 op = mips16_immed_operands;
13574 while (op->type != type)
13575 ++op;
13577 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13579 small = false;
13580 ext = true;
13582 else
13584 small = true;
13585 ext = false;
13588 resolve_symbol_value (fragp->fr_symbol);
13589 val = S_GET_VALUE (fragp->fr_symbol);
13590 if (op->pcrel)
13592 addressT addr;
13594 addr = fragp->fr_address + fragp->fr_fix;
13596 /* The rules for the base address of a PC relative reloc are
13597 complicated; see mips16_extended_frag. */
13598 if (type == 'p' || type == 'q')
13600 addr += 2;
13601 if (ext)
13602 addr += 2;
13603 /* Ignore the low bit in the target, since it will be
13604 set for a text label. */
13605 if ((val & 1) != 0)
13606 --val;
13608 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13609 addr -= 4;
13610 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13611 addr -= 2;
13613 addr &= ~ (addressT) ((1 << op->shift) - 1);
13614 val -= addr;
13616 /* Make sure the section winds up with the alignment we have
13617 assumed. */
13618 if (op->shift > 0)
13619 record_alignment (asec, op->shift);
13622 if (ext
13623 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13624 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13625 as_warn_where (fragp->fr_file, fragp->fr_line,
13626 _("extended instruction in delay slot"));
13628 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13630 if (target_big_endian)
13631 insn = bfd_getb16 (buf);
13632 else
13633 insn = bfd_getl16 (buf);
13635 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13636 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13637 small, ext, &insn, &use_extend, &extend);
13639 if (use_extend)
13641 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13642 fragp->fr_fix += 2;
13643 buf += 2;
13646 md_number_to_chars ((char *) buf, insn, 2);
13647 fragp->fr_fix += 2;
13648 buf += 2;
13650 else
13652 if (fragp->fr_opcode == NULL)
13653 return;
13655 old = RELAX_OLD (fragp->fr_subtype);
13656 new = RELAX_NEW (fragp->fr_subtype);
13657 fixptr = fragp->fr_literal + fragp->fr_fix;
13659 if (new > 0)
13660 memcpy (fixptr - old, fixptr, new);
13662 fragp->fr_fix += new - old;
13666 #ifdef OBJ_ELF
13668 /* This function is called after the relocs have been generated.
13669 We've been storing mips16 text labels as odd. Here we convert them
13670 back to even for the convenience of the debugger. */
13672 void
13673 mips_frob_file_after_relocs ()
13675 asymbol **syms;
13676 unsigned int count, i;
13678 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13679 return;
13681 syms = bfd_get_outsymbols (stdoutput);
13682 count = bfd_get_symcount (stdoutput);
13683 for (i = 0; i < count; i++, syms++)
13685 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13686 && ((*syms)->value & 1) != 0)
13688 (*syms)->value &= ~1;
13689 /* If the symbol has an odd size, it was probably computed
13690 incorrectly, so adjust that as well. */
13691 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13692 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13697 #endif
13699 /* This function is called whenever a label is defined. It is used
13700 when handling branch delays; if a branch has a label, we assume we
13701 can not move it. */
13703 void
13704 mips_define_label (sym)
13705 symbolS *sym;
13707 struct insn_label_list *l;
13709 if (free_insn_labels == NULL)
13710 l = (struct insn_label_list *) xmalloc (sizeof *l);
13711 else
13713 l = free_insn_labels;
13714 free_insn_labels = l->next;
13717 l->label = sym;
13718 l->next = insn_labels;
13719 insn_labels = l;
13722 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13724 /* Some special processing for a MIPS ELF file. */
13726 void
13727 mips_elf_final_processing ()
13729 /* Write out the register information. */
13730 if (mips_abi != N64_ABI)
13732 Elf32_RegInfo s;
13734 s.ri_gprmask = mips_gprmask;
13735 s.ri_cprmask[0] = mips_cprmask[0];
13736 s.ri_cprmask[1] = mips_cprmask[1];
13737 s.ri_cprmask[2] = mips_cprmask[2];
13738 s.ri_cprmask[3] = mips_cprmask[3];
13739 /* The gp_value field is set by the MIPS ELF backend. */
13741 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13742 ((Elf32_External_RegInfo *)
13743 mips_regmask_frag));
13745 else
13747 Elf64_Internal_RegInfo s;
13749 s.ri_gprmask = mips_gprmask;
13750 s.ri_pad = 0;
13751 s.ri_cprmask[0] = mips_cprmask[0];
13752 s.ri_cprmask[1] = mips_cprmask[1];
13753 s.ri_cprmask[2] = mips_cprmask[2];
13754 s.ri_cprmask[3] = mips_cprmask[3];
13755 /* The gp_value field is set by the MIPS ELF backend. */
13757 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13758 ((Elf64_External_RegInfo *)
13759 mips_regmask_frag));
13762 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13763 sort of BFD interface for this. */
13764 if (mips_any_noreorder)
13765 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13766 if (mips_pic != NO_PIC)
13767 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13769 /* Set MIPS ELF flags for ASEs. */
13770 if (file_ase_mips16)
13771 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13772 #if 0 /* XXX FIXME */
13773 if (file_ase_mips3d)
13774 elf_elfheader (stdoutput)->e_flags |= ???;
13775 #endif
13776 if (file_ase_mdmx)
13777 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13779 /* Set the MIPS ELF ABI flags. */
13780 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13781 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13782 else if (mips_abi == O64_ABI)
13783 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13784 else if (mips_abi == EABI_ABI)
13786 if (!file_mips_gp32)
13787 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13788 else
13789 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13791 else if (mips_abi == N32_ABI)
13792 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13794 /* Nothing to do for N64_ABI. */
13796 if (mips_32bitmode)
13797 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13800 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13802 typedef struct proc {
13803 symbolS *isym;
13804 unsigned long reg_mask;
13805 unsigned long reg_offset;
13806 unsigned long fpreg_mask;
13807 unsigned long fpreg_offset;
13808 unsigned long frame_offset;
13809 unsigned long frame_reg;
13810 unsigned long pc_reg;
13811 } procS;
13813 static procS cur_proc;
13814 static procS *cur_proc_ptr;
13815 static int numprocs;
13817 /* Fill in an rs_align_code fragment. */
13819 void
13820 mips_handle_align (fragp)
13821 fragS *fragp;
13823 if (fragp->fr_type != rs_align_code)
13824 return;
13826 if (mips_opts.mips16)
13828 static const unsigned char be_nop[] = { 0x65, 0x00 };
13829 static const unsigned char le_nop[] = { 0x00, 0x65 };
13831 int bytes;
13832 char *p;
13834 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13835 p = fragp->fr_literal + fragp->fr_fix;
13837 if (bytes & 1)
13839 *p++ = 0;
13840 fragp->fr_fix++;
13843 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13844 fragp->fr_var = 2;
13847 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
13850 static void
13851 md_obj_begin ()
13855 static void
13856 md_obj_end ()
13858 /* check for premature end, nesting errors, etc */
13859 if (cur_proc_ptr)
13860 as_warn (_("missing .end at end of assembly"));
13863 static long
13864 get_number ()
13866 int negative = 0;
13867 long val = 0;
13869 if (*input_line_pointer == '-')
13871 ++input_line_pointer;
13872 negative = 1;
13874 if (!ISDIGIT (*input_line_pointer))
13875 as_bad (_("expected simple number"));
13876 if (input_line_pointer[0] == '0')
13878 if (input_line_pointer[1] == 'x')
13880 input_line_pointer += 2;
13881 while (ISXDIGIT (*input_line_pointer))
13883 val <<= 4;
13884 val |= hex_value (*input_line_pointer++);
13886 return negative ? -val : val;
13888 else
13890 ++input_line_pointer;
13891 while (ISDIGIT (*input_line_pointer))
13893 val <<= 3;
13894 val |= *input_line_pointer++ - '0';
13896 return negative ? -val : val;
13899 if (!ISDIGIT (*input_line_pointer))
13901 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13902 *input_line_pointer, *input_line_pointer);
13903 as_warn (_("invalid number"));
13904 return -1;
13906 while (ISDIGIT (*input_line_pointer))
13908 val *= 10;
13909 val += *input_line_pointer++ - '0';
13911 return negative ? -val : val;
13914 /* The .file directive; just like the usual .file directive, but there
13915 is an initial number which is the ECOFF file index. In the non-ECOFF
13916 case .file implies DWARF-2. */
13918 static void
13919 s_mips_file (x)
13920 int x ATTRIBUTE_UNUSED;
13922 static int first_file_directive = 0;
13924 if (ECOFF_DEBUGGING)
13926 get_number ();
13927 s_app_file (0);
13929 else
13931 char *filename;
13933 filename = dwarf2_directive_file (0);
13935 /* Versions of GCC up to 3.1 start files with a ".file"
13936 directive even for stabs output. Make sure that this
13937 ".file" is handled. Note that you need a version of GCC
13938 after 3.1 in order to support DWARF-2 on MIPS. */
13939 if (filename != NULL && ! first_file_directive)
13941 (void) new_logical_line (filename, -1);
13942 s_app_file_string (filename);
13944 first_file_directive = 1;
13948 /* The .loc directive, implying DWARF-2. */
13950 static void
13951 s_mips_loc (x)
13952 int x ATTRIBUTE_UNUSED;
13954 if (!ECOFF_DEBUGGING)
13955 dwarf2_directive_loc (0);
13958 /* The .end directive. */
13960 static void
13961 s_mips_end (x)
13962 int x ATTRIBUTE_UNUSED;
13964 symbolS *p;
13965 int maybe_text;
13967 /* Following functions need their own .frame and .cprestore directives. */
13968 mips_frame_reg_valid = 0;
13969 mips_cprestore_valid = 0;
13971 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13973 p = get_symbol ();
13974 demand_empty_rest_of_line ();
13976 else
13977 p = NULL;
13979 #ifdef BFD_ASSEMBLER
13980 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13981 maybe_text = 1;
13982 else
13983 maybe_text = 0;
13984 #else
13985 if (now_seg != data_section && now_seg != bss_section)
13986 maybe_text = 1;
13987 else
13988 maybe_text = 0;
13989 #endif
13991 if (!maybe_text)
13992 as_warn (_(".end not in text section"));
13994 if (!cur_proc_ptr)
13996 as_warn (_(".end directive without a preceding .ent directive."));
13997 demand_empty_rest_of_line ();
13998 return;
14001 if (p != NULL)
14003 assert (S_GET_NAME (p));
14004 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14005 as_warn (_(".end symbol does not match .ent symbol."));
14007 if (debug_type == DEBUG_STABS)
14008 stabs_generate_asm_endfunc (S_GET_NAME (p),
14009 S_GET_NAME (p));
14011 else
14012 as_warn (_(".end directive missing or unknown symbol"));
14014 #ifdef OBJ_ELF
14015 /* Generate a .pdr section. */
14016 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14018 segT saved_seg = now_seg;
14019 subsegT saved_subseg = now_subseg;
14020 valueT dot;
14021 expressionS exp;
14022 char *fragp;
14024 dot = frag_now_fix ();
14026 #ifdef md_flush_pending_output
14027 md_flush_pending_output ();
14028 #endif
14030 assert (pdr_seg);
14031 subseg_set (pdr_seg, 0);
14033 /* Write the symbol. */
14034 exp.X_op = O_symbol;
14035 exp.X_add_symbol = p;
14036 exp.X_add_number = 0;
14037 emit_expr (&exp, 4);
14039 fragp = frag_more (7 * 4);
14041 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
14042 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
14043 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14044 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14045 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14046 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14047 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14049 subseg_set (saved_seg, saved_subseg);
14051 #endif /* OBJ_ELF */
14053 cur_proc_ptr = NULL;
14056 /* The .aent and .ent directives. */
14058 static void
14059 s_mips_ent (aent)
14060 int aent;
14062 symbolS *symbolP;
14063 int maybe_text;
14065 symbolP = get_symbol ();
14066 if (*input_line_pointer == ',')
14067 ++input_line_pointer;
14068 SKIP_WHITESPACE ();
14069 if (ISDIGIT (*input_line_pointer)
14070 || *input_line_pointer == '-')
14071 get_number ();
14073 #ifdef BFD_ASSEMBLER
14074 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14075 maybe_text = 1;
14076 else
14077 maybe_text = 0;
14078 #else
14079 if (now_seg != data_section && now_seg != bss_section)
14080 maybe_text = 1;
14081 else
14082 maybe_text = 0;
14083 #endif
14085 if (!maybe_text)
14086 as_warn (_(".ent or .aent not in text section."));
14088 if (!aent && cur_proc_ptr)
14089 as_warn (_("missing .end"));
14091 if (!aent)
14093 /* This function needs its own .frame and .cprestore directives. */
14094 mips_frame_reg_valid = 0;
14095 mips_cprestore_valid = 0;
14097 cur_proc_ptr = &cur_proc;
14098 memset (cur_proc_ptr, '\0', sizeof (procS));
14100 cur_proc_ptr->isym = symbolP;
14102 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14104 ++numprocs;
14106 if (debug_type == DEBUG_STABS)
14107 stabs_generate_asm_func (S_GET_NAME (symbolP),
14108 S_GET_NAME (symbolP));
14111 demand_empty_rest_of_line ();
14114 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14115 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14116 s_mips_frame is used so that we can set the PDR information correctly.
14117 We can't use the ecoff routines because they make reference to the ecoff
14118 symbol table (in the mdebug section). */
14120 static void
14121 s_mips_frame (ignore)
14122 int ignore ATTRIBUTE_UNUSED;
14124 #ifdef OBJ_ELF
14125 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14127 long val;
14129 if (cur_proc_ptr == (procS *) NULL)
14131 as_warn (_(".frame outside of .ent"));
14132 demand_empty_rest_of_line ();
14133 return;
14136 cur_proc_ptr->frame_reg = tc_get_register (1);
14138 SKIP_WHITESPACE ();
14139 if (*input_line_pointer++ != ','
14140 || get_absolute_expression_and_terminator (&val) != ',')
14142 as_warn (_("Bad .frame directive"));
14143 --input_line_pointer;
14144 demand_empty_rest_of_line ();
14145 return;
14148 cur_proc_ptr->frame_offset = val;
14149 cur_proc_ptr->pc_reg = tc_get_register (0);
14151 demand_empty_rest_of_line ();
14153 else
14154 #endif /* OBJ_ELF */
14155 s_ignore (ignore);
14158 /* The .fmask and .mask directives. If the mdebug section is present
14159 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14160 embedded targets, s_mips_mask is used so that we can set the PDR
14161 information correctly. We can't use the ecoff routines because they
14162 make reference to the ecoff symbol table (in the mdebug section). */
14164 static void
14165 s_mips_mask (reg_type)
14166 char reg_type;
14168 #ifdef OBJ_ELF
14169 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14171 long mask, off;
14173 if (cur_proc_ptr == (procS *) NULL)
14175 as_warn (_(".mask/.fmask outside of .ent"));
14176 demand_empty_rest_of_line ();
14177 return;
14180 if (get_absolute_expression_and_terminator (&mask) != ',')
14182 as_warn (_("Bad .mask/.fmask directive"));
14183 --input_line_pointer;
14184 demand_empty_rest_of_line ();
14185 return;
14188 off = get_absolute_expression ();
14190 if (reg_type == 'F')
14192 cur_proc_ptr->fpreg_mask = mask;
14193 cur_proc_ptr->fpreg_offset = off;
14195 else
14197 cur_proc_ptr->reg_mask = mask;
14198 cur_proc_ptr->reg_offset = off;
14201 demand_empty_rest_of_line ();
14203 else
14204 #endif /* OBJ_ELF */
14205 s_ignore (reg_type);
14208 /* The .loc directive. */
14210 #if 0
14211 static void
14212 s_loc (x)
14213 int x;
14215 symbolS *symbolP;
14216 int lineno;
14217 int addroff;
14219 assert (now_seg == text_section);
14221 lineno = get_number ();
14222 addroff = frag_now_fix ();
14224 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14225 S_SET_TYPE (symbolP, N_SLINE);
14226 S_SET_OTHER (symbolP, 0);
14227 S_SET_DESC (symbolP, lineno);
14228 symbolP->sy_segment = now_seg;
14230 #endif
14232 /* A table describing all the processors gas knows about. Names are
14233 matched in the order listed.
14235 To ease comparison, please keep this table in the same order as
14236 gcc's mips_cpu_info_table[]. */
14237 static const struct mips_cpu_info mips_cpu_info_table[] =
14239 /* Entries for generic ISAs */
14240 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14241 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14242 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14243 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14244 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14245 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
14246 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
14248 /* MIPS I */
14249 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14250 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14251 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14253 /* MIPS II */
14254 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14256 /* MIPS III */
14257 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14258 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14259 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14260 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
14261 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14262 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14263 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
14264 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14265 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14266 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14267 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14268 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14270 /* MIPS IV */
14271 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14272 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14273 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14274 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
14275 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14276 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
14277 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14278 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14279 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14280 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14281 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14282 { "r7000", 0, ISA_MIPS4, CPU_R5000 },
14284 /* MIPS 32 */
14285 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
14286 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14287 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
14289 /* MIPS 64 */
14290 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14291 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
14293 /* Broadcom SB-1 CPU core */
14294 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
14296 /* End marker */
14297 { NULL, 0, 0, 0 }
14301 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14302 with a final "000" replaced by "k". Ignore case.
14304 Note: this function is shared between GCC and GAS. */
14306 static boolean
14307 mips_strict_matching_cpu_name_p (canonical, given)
14308 const char *canonical, *given;
14310 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14311 given++, canonical++;
14313 return ((*given == 0 && *canonical == 0)
14314 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14318 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14319 CPU name. We've traditionally allowed a lot of variation here.
14321 Note: this function is shared between GCC and GAS. */
14323 static boolean
14324 mips_matching_cpu_name_p (canonical, given)
14325 const char *canonical, *given;
14327 /* First see if the name matches exactly, or with a final "000"
14328 turned into "k". */
14329 if (mips_strict_matching_cpu_name_p (canonical, given))
14330 return true;
14332 /* If not, try comparing based on numerical designation alone.
14333 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14334 if (TOLOWER (*given) == 'r')
14335 given++;
14336 if (!ISDIGIT (*given))
14337 return false;
14339 /* Skip over some well-known prefixes in the canonical name,
14340 hoping to find a number there too. */
14341 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14342 canonical += 2;
14343 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14344 canonical += 2;
14345 else if (TOLOWER (canonical[0]) == 'r')
14346 canonical += 1;
14348 return mips_strict_matching_cpu_name_p (canonical, given);
14352 /* Parse an option that takes the name of a processor as its argument.
14353 OPTION is the name of the option and CPU_STRING is the argument.
14354 Return the corresponding processor enumeration if the CPU_STRING is
14355 recognized, otherwise report an error and return null.
14357 A similar function exists in GCC. */
14359 static const struct mips_cpu_info *
14360 mips_parse_cpu (option, cpu_string)
14361 const char *option, *cpu_string;
14363 const struct mips_cpu_info *p;
14365 /* 'from-abi' selects the most compatible architecture for the given
14366 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14367 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14368 version. Look first at the -mgp options, if given, otherwise base
14369 the choice on MIPS_DEFAULT_64BIT.
14371 Treat NO_ABI like the EABIs. One reason to do this is that the
14372 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14373 architecture. This code picks MIPS I for 'mips' and MIPS III for
14374 'mips64', just as we did in the days before 'from-abi'. */
14375 if (strcasecmp (cpu_string, "from-abi") == 0)
14377 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14378 return mips_cpu_info_from_isa (ISA_MIPS1);
14380 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14381 return mips_cpu_info_from_isa (ISA_MIPS3);
14383 if (file_mips_gp32 >= 0)
14384 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14386 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14387 ? ISA_MIPS3
14388 : ISA_MIPS1);
14391 /* 'default' has traditionally been a no-op. Probably not very useful. */
14392 if (strcasecmp (cpu_string, "default") == 0)
14393 return 0;
14395 for (p = mips_cpu_info_table; p->name != 0; p++)
14396 if (mips_matching_cpu_name_p (p->name, cpu_string))
14397 return p;
14399 as_bad ("Bad value (%s) for %s", cpu_string, option);
14400 return 0;
14403 /* Return the canonical processor information for ISA (a member of the
14404 ISA_MIPS* enumeration). */
14406 static const struct mips_cpu_info *
14407 mips_cpu_info_from_isa (isa)
14408 int isa;
14410 int i;
14412 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14413 if (mips_cpu_info_table[i].is_isa
14414 && isa == mips_cpu_info_table[i].isa)
14415 return (&mips_cpu_info_table[i]);
14417 return NULL;
14420 static void
14421 show (stream, string, col_p, first_p)
14422 FILE *stream;
14423 const char *string;
14424 int *col_p;
14425 int *first_p;
14427 if (*first_p)
14429 fprintf (stream, "%24s", "");
14430 *col_p = 24;
14432 else
14434 fprintf (stream, ", ");
14435 *col_p += 2;
14438 if (*col_p + strlen (string) > 72)
14440 fprintf (stream, "\n%24s", "");
14441 *col_p = 24;
14444 fprintf (stream, "%s", string);
14445 *col_p += strlen (string);
14447 *first_p = 0;
14450 void
14451 md_show_usage (stream)
14452 FILE *stream;
14454 int column, first;
14455 size_t i;
14457 fprintf (stream, _("\
14458 MIPS options:\n\
14459 -membedded-pic generate embedded position independent code\n\
14460 -EB generate big endian output\n\
14461 -EL generate little endian output\n\
14462 -g, -g2 do not remove unneeded NOPs or swap branches\n\
14463 -G NUM allow referencing objects up to NUM bytes\n\
14464 implicitly with the gp register [default 8]\n"));
14465 fprintf (stream, _("\
14466 -mips1 generate MIPS ISA I instructions\n\
14467 -mips2 generate MIPS ISA II instructions\n\
14468 -mips3 generate MIPS ISA III instructions\n\
14469 -mips4 generate MIPS ISA IV instructions\n\
14470 -mips5 generate MIPS ISA V instructions\n\
14471 -mips32 generate MIPS32 ISA instructions\n\
14472 -mips64 generate MIPS64 ISA instructions\n\
14473 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14475 first = 1;
14477 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14478 show (stream, mips_cpu_info_table[i].name, &column, &first);
14479 show (stream, "from-abi", &column, &first);
14480 fputc ('\n', stream);
14482 fprintf (stream, _("\
14483 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14484 -no-mCPU don't generate code specific to CPU.\n\
14485 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14487 first = 1;
14489 show (stream, "3900", &column, &first);
14490 show (stream, "4010", &column, &first);
14491 show (stream, "4100", &column, &first);
14492 show (stream, "4650", &column, &first);
14493 fputc ('\n', stream);
14495 fprintf (stream, _("\
14496 -mips16 generate mips16 instructions\n\
14497 -no-mips16 do not generate mips16 instructions\n"));
14498 fprintf (stream, _("\
14499 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14500 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14501 -O0 remove unneeded NOPs, do not swap branches\n\
14502 -O remove unneeded NOPs and swap branches\n\
14503 -n warn about NOPs generated from macros\n\
14504 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14505 --trap, --no-break trap exception on div by 0 and mult overflow\n\
14506 --break, --no-trap break exception on div by 0 and mult overflow\n"));
14507 #ifdef OBJ_ELF
14508 fprintf (stream, _("\
14509 -KPIC, -call_shared generate SVR4 position independent code\n\
14510 -non_shared do not generate position independent code\n\
14511 -xgot assume a 32 bit GOT\n\
14512 -mabi=ABI create ABI conformant object file for:\n"));
14514 first = 1;
14516 show (stream, "32", &column, &first);
14517 show (stream, "o64", &column, &first);
14518 show (stream, "n32", &column, &first);
14519 show (stream, "64", &column, &first);
14520 show (stream, "eabi", &column, &first);
14522 fputc ('\n', stream);
14524 fprintf (stream, _("\
14525 -32 create o32 ABI object file (default)\n\
14526 -n32 create n32 ABI object file\n\
14527 -64 create 64 ABI object file\n"));
14528 #endif