2002-09-29 H.J. Lu <hjl@gnu.org>
[binutils.git] / gas / config / tc-mips.c
blob7c8be079a6075a58fcefc3e493042f4b554e69b4
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 /* Whether the processor uses hardware interlocks to protect
300 reads from the HI and LO registers, and thus does not
301 require nops to be inserted. */
303 #define hilo_interlocks (mips_arch == CPU_R4010 \
304 || mips_arch == CPU_SB1 \
307 /* Whether the processor uses hardware interlocks to protect reads
308 from the GPRs, and thus does not require nops to be inserted. */
309 #define gpr_interlocks \
310 (mips_opts.isa != ISA_MIPS1 \
311 || mips_arch == CPU_R3900)
313 /* As with other "interlocks" this is used by hardware that has FP
314 (co-processor) interlocks. */
315 /* Itbl support may require additional care here. */
316 #define cop_interlocks (mips_arch == CPU_R4300 \
317 || mips_arch == CPU_SB1 \
320 /* Is this a mfhi or mflo instruction? */
321 #define MF_HILO_INSN(PINFO) \
322 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
324 /* MIPS PIC level. */
326 enum mips_pic_level mips_pic;
328 /* Warn about all NOPS that the assembler generates. */
329 static int warn_nops = 0;
331 /* 1 if we should generate 32 bit offsets from the $gp register in
332 SVR4_PIC mode. Currently has no meaning in other modes. */
333 static int mips_big_got = 0;
335 /* 1 if trap instructions should used for overflow rather than break
336 instructions. */
337 static int mips_trap = 0;
339 /* 1 if double width floating point constants should not be constructed
340 by assembling two single width halves into two single width floating
341 point registers which just happen to alias the double width destination
342 register. On some architectures this aliasing can be disabled by a bit
343 in the status register, and the setting of this bit cannot be determined
344 automatically at assemble time. */
345 static int mips_disable_float_construction;
347 /* Non-zero if any .set noreorder directives were used. */
349 static int mips_any_noreorder;
351 /* Non-zero if nops should be inserted when the register referenced in
352 an mfhi/mflo instruction is read in the next two instructions. */
353 static int mips_7000_hilo_fix;
355 /* The size of the small data section. */
356 static unsigned int g_switch_value = 8;
357 /* Whether the -G option was used. */
358 static int g_switch_seen = 0;
360 #define N_RMASK 0xc4
361 #define N_VFP 0xd4
363 /* If we can determine in advance that GP optimization won't be
364 possible, we can skip the relaxation stuff that tries to produce
365 GP-relative references. This makes delay slot optimization work
366 better.
368 This function can only provide a guess, but it seems to work for
369 gcc output. It needs to guess right for gcc, otherwise gcc
370 will put what it thinks is a GP-relative instruction in a branch
371 delay slot.
373 I don't know if a fix is needed for the SVR4_PIC mode. I've only
374 fixed it for the non-PIC mode. KR 95/04/07 */
375 static int nopic_need_relax PARAMS ((symbolS *, int));
377 /* handle of the OPCODE hash table */
378 static struct hash_control *op_hash = NULL;
380 /* The opcode hash table we use for the mips16. */
381 static struct hash_control *mips16_op_hash = NULL;
383 /* This array holds the chars that always start a comment. If the
384 pre-processor is disabled, these aren't very useful */
385 const char comment_chars[] = "#";
387 /* This array holds the chars that only start a comment at the beginning of
388 a line. If the line seems to have the form '# 123 filename'
389 .line and .file directives will appear in the pre-processed output */
390 /* Note that input_file.c hand checks for '#' at the beginning of the
391 first line of the input file. This is because the compiler outputs
392 #NO_APP at the beginning of its output. */
393 /* Also note that C style comments are always supported. */
394 const char line_comment_chars[] = "#";
396 /* This array holds machine specific line separator characters. */
397 const char line_separator_chars[] = ";";
399 /* Chars that can be used to separate mant from exp in floating point nums */
400 const char EXP_CHARS[] = "eE";
402 /* Chars that mean this number is a floating point constant */
403 /* As in 0f12.456 */
404 /* or 0d1.2345e12 */
405 const char FLT_CHARS[] = "rRsSfFdDxXpP";
407 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
408 changed in read.c . Ideally it shouldn't have to know about it at all,
409 but nothing is ideal around here.
412 static char *insn_error;
414 static int auto_align = 1;
416 /* When outputting SVR4 PIC code, the assembler needs to know the
417 offset in the stack frame from which to restore the $gp register.
418 This is set by the .cprestore pseudo-op, and saved in this
419 variable. */
420 static offsetT mips_cprestore_offset = -1;
422 /* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
423 more optimizations, it can use a register value instead of a memory-saved
424 offset and even an other register than $gp as global pointer. */
425 static offsetT mips_cpreturn_offset = -1;
426 static int mips_cpreturn_register = -1;
427 static int mips_gp_register = GP;
428 static int mips_gprel_offset = 0;
430 /* Whether mips_cprestore_offset has been set in the current function
431 (or whether it has already been warned about, if not). */
432 static int mips_cprestore_valid = 0;
434 /* This is the register which holds the stack frame, as set by the
435 .frame pseudo-op. This is needed to implement .cprestore. */
436 static int mips_frame_reg = SP;
438 /* Whether mips_frame_reg has been set in the current function
439 (or whether it has already been warned about, if not). */
440 static int mips_frame_reg_valid = 0;
442 /* To output NOP instructions correctly, we need to keep information
443 about the previous two instructions. */
445 /* Whether we are optimizing. The default value of 2 means to remove
446 unneeded NOPs and swap branch instructions when possible. A value
447 of 1 means to not swap branches. A value of 0 means to always
448 insert NOPs. */
449 static int mips_optimize = 2;
451 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
452 equivalent to seeing no -g option at all. */
453 static int mips_debug = 0;
455 /* The previous instruction. */
456 static struct mips_cl_insn prev_insn;
458 /* The instruction before prev_insn. */
459 static struct mips_cl_insn prev_prev_insn;
461 /* If we don't want information for prev_insn or prev_prev_insn, we
462 point the insn_mo field at this dummy integer. */
463 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
465 /* Non-zero if prev_insn is valid. */
466 static int prev_insn_valid;
468 /* The frag for the previous instruction. */
469 static struct frag *prev_insn_frag;
471 /* The offset into prev_insn_frag for the previous instruction. */
472 static long prev_insn_where;
474 /* The reloc type for the previous instruction, if any. */
475 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
477 /* The reloc for the previous instruction, if any. */
478 static fixS *prev_insn_fixp[3];
480 /* Non-zero if the previous instruction was in a delay slot. */
481 static int prev_insn_is_delay_slot;
483 /* Non-zero if the previous instruction was in a .set noreorder. */
484 static int prev_insn_unreordered;
486 /* Non-zero if the previous instruction uses an extend opcode (if
487 mips16). */
488 static int prev_insn_extended;
490 /* Non-zero if the previous previous instruction was in a .set
491 noreorder. */
492 static int prev_prev_insn_unreordered;
494 /* If this is set, it points to a frag holding nop instructions which
495 were inserted before the start of a noreorder section. If those
496 nops turn out to be unnecessary, the size of the frag can be
497 decreased. */
498 static fragS *prev_nop_frag;
500 /* The number of nop instructions we created in prev_nop_frag. */
501 static int prev_nop_frag_holds;
503 /* The number of nop instructions that we know we need in
504 prev_nop_frag. */
505 static int prev_nop_frag_required;
507 /* The number of instructions we've seen since prev_nop_frag. */
508 static int prev_nop_frag_since;
510 /* For ECOFF and ELF, relocations against symbols are done in two
511 parts, with a HI relocation and a LO relocation. Each relocation
512 has only 16 bits of space to store an addend. This means that in
513 order for the linker to handle carries correctly, it must be able
514 to locate both the HI and the LO relocation. This means that the
515 relocations must appear in order in the relocation table.
517 In order to implement this, we keep track of each unmatched HI
518 relocation. We then sort them so that they immediately precede the
519 corresponding LO relocation. */
521 struct mips_hi_fixup
523 /* Next HI fixup. */
524 struct mips_hi_fixup *next;
525 /* This fixup. */
526 fixS *fixp;
527 /* The section this fixup is in. */
528 segT seg;
531 /* The list of unmatched HI relocs. */
533 static struct mips_hi_fixup *mips_hi_fixup_list;
535 /* Map normal MIPS register numbers to mips16 register numbers. */
537 #define X ILLEGAL_REG
538 static const int mips32_to_16_reg_map[] =
540 X, X, 2, 3, 4, 5, 6, 7,
541 X, X, X, X, X, X, X, X,
542 0, 1, X, X, X, X, X, X,
543 X, X, X, X, X, X, X, X
545 #undef X
547 /* Map mips16 register numbers to normal MIPS register numbers. */
549 static const unsigned int mips16_to_32_reg_map[] =
551 16, 17, 2, 3, 4, 5, 6, 7
554 /* Since the MIPS does not have multiple forms of PC relative
555 instructions, we do not have to do relaxing as is done on other
556 platforms. However, we do have to handle GP relative addressing
557 correctly, which turns out to be a similar problem.
559 Every macro that refers to a symbol can occur in (at least) two
560 forms, one with GP relative addressing and one without. For
561 example, loading a global variable into a register generally uses
562 a macro instruction like this:
563 lw $4,i
564 If i can be addressed off the GP register (this is true if it is in
565 the .sbss or .sdata section, or if it is known to be smaller than
566 the -G argument) this will generate the following instruction:
567 lw $4,i($gp)
568 This instruction will use a GPREL reloc. If i can not be addressed
569 off the GP register, the following instruction sequence will be used:
570 lui $at,i
571 lw $4,i($at)
572 In this case the first instruction will have a HI16 reloc, and the
573 second reloc will have a LO16 reloc. Both relocs will be against
574 the symbol i.
576 The issue here is that we may not know whether i is GP addressable
577 until after we see the instruction that uses it. Therefore, we
578 want to be able to choose the final instruction sequence only at
579 the end of the assembly. This is similar to the way other
580 platforms choose the size of a PC relative instruction only at the
581 end of assembly.
583 When generating position independent code we do not use GP
584 addressing in quite the same way, but the issue still arises as
585 external symbols and local symbols must be handled differently.
587 We handle these issues by actually generating both possible
588 instruction sequences. The longer one is put in a frag_var with
589 type rs_machine_dependent. We encode what to do with the frag in
590 the subtype field. We encode (1) the number of existing bytes to
591 replace, (2) the number of new bytes to use, (3) the offset from
592 the start of the existing bytes to the first reloc we must generate
593 (that is, the offset is applied from the start of the existing
594 bytes after they are replaced by the new bytes, if any), (4) the
595 offset from the start of the existing bytes to the second reloc,
596 (5) whether a third reloc is needed (the third reloc is always four
597 bytes after the second reloc), and (6) whether to warn if this
598 variant is used (this is sometimes needed if .set nomacro or .set
599 noat is in effect). All these numbers are reasonably small.
601 Generating two instruction sequences must be handled carefully to
602 ensure that delay slots are handled correctly. Fortunately, there
603 are a limited number of cases. When the second instruction
604 sequence is generated, append_insn is directed to maintain the
605 existing delay slot information, so it continues to apply to any
606 code after the second instruction sequence. This means that the
607 second instruction sequence must not impose any requirements not
608 required by the first instruction sequence.
610 These variant frags are then handled in functions called by the
611 machine independent code. md_estimate_size_before_relax returns
612 the final size of the frag. md_convert_frag sets up the final form
613 of the frag. tc_gen_reloc adjust the first reloc and adds a second
614 one if needed. */
615 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
616 ((relax_substateT) \
617 (((old) << 23) \
618 | ((new) << 16) \
619 | (((reloc1) + 64) << 9) \
620 | (((reloc2) + 64) << 2) \
621 | ((reloc3) ? (1 << 1) : 0) \
622 | ((warn) ? 1 : 0)))
623 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
624 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
625 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
626 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
627 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
628 #define RELAX_WARN(i) ((i) & 1)
630 /* For mips16 code, we use an entirely different form of relaxation.
631 mips16 supports two versions of most instructions which take
632 immediate values: a small one which takes some small value, and a
633 larger one which takes a 16 bit value. Since branches also follow
634 this pattern, relaxing these values is required.
636 We can assemble both mips16 and normal MIPS code in a single
637 object. Therefore, we need to support this type of relaxation at
638 the same time that we support the relaxation described above. We
639 use the high bit of the subtype field to distinguish these cases.
641 The information we store for this type of relaxation is the
642 argument code found in the opcode file for this relocation, whether
643 the user explicitly requested a small or extended form, and whether
644 the relocation is in a jump or jal delay slot. That tells us the
645 size of the value, and how it should be stored. We also store
646 whether the fragment is considered to be extended or not. We also
647 store whether this is known to be a branch to a different section,
648 whether we have tried to relax this frag yet, and whether we have
649 ever extended a PC relative fragment because of a shift count. */
650 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
651 (0x80000000 \
652 | ((type) & 0xff) \
653 | ((small) ? 0x100 : 0) \
654 | ((ext) ? 0x200 : 0) \
655 | ((dslot) ? 0x400 : 0) \
656 | ((jal_dslot) ? 0x800 : 0))
657 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
658 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
659 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
660 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
661 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
662 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
663 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
664 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
665 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
666 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
667 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
668 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
670 /* Is the given value a sign-extended 32-bit value? */
671 #define IS_SEXT_32BIT_NUM(x) \
672 (((x) &~ (offsetT) 0x7fffffff) == 0 \
673 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
675 /* Is the given value a sign-extended 16-bit value? */
676 #define IS_SEXT_16BIT_NUM(x) \
677 (((x) &~ (offsetT) 0x7fff) == 0 \
678 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
681 /* Prototypes for static functions. */
683 #ifdef __STDC__
684 #define internalError() \
685 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
686 #else
687 #define internalError() as_fatal (_("MIPS internal Error"));
688 #endif
690 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
692 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
693 unsigned int reg, enum mips_regclass class));
694 static int reg_needs_delay PARAMS ((unsigned int));
695 static void mips16_mark_labels PARAMS ((void));
696 static void append_insn PARAMS ((char *place,
697 struct mips_cl_insn * ip,
698 expressionS * p,
699 bfd_reloc_code_real_type *r,
700 boolean));
701 static void mips_no_prev_insn PARAMS ((int));
702 static void mips_emit_delays PARAMS ((boolean));
703 #ifdef USE_STDARG
704 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
705 const char *name, const char *fmt,
706 ...));
707 #else
708 static void macro_build ();
709 #endif
710 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
711 const char *, const char *,
712 va_list));
713 static void macro_build_jalr PARAMS ((int, expressionS *));
714 static void macro_build_lui PARAMS ((char *place, int *counter,
715 expressionS * ep, int regnum));
716 static void macro_build_ldst_constoffset PARAMS ((char *place, int *counter,
717 expressionS * ep, const char *op,
718 int valreg, int breg));
719 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
720 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
721 expressionS *));
722 static void load_register PARAMS ((int *, int, expressionS *, int));
723 static void load_address PARAMS ((int *, int, expressionS *, int *));
724 static void move_register PARAMS ((int *, int, int));
725 static void macro PARAMS ((struct mips_cl_insn * ip));
726 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
727 #ifdef LOSING_COMPILER
728 static void macro2 PARAMS ((struct mips_cl_insn * ip));
729 #endif
730 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
731 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
732 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
733 boolean, boolean, unsigned long *,
734 boolean *, unsigned short *));
735 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
736 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
737 static int my_getSmallExpression PARAMS ((expressionS *, char *));
738 static void my_getExpression PARAMS ((expressionS *, char *));
739 #ifdef OBJ_ELF
740 static int support_64bit_objects PARAMS((void));
741 #endif
742 static void mips_set_option_string PARAMS ((const char **, const char *));
743 static symbolS *get_symbol PARAMS ((void));
744 static void mips_align PARAMS ((int to, int fill, symbolS *label));
745 static void s_align PARAMS ((int));
746 static void s_change_sec PARAMS ((int));
747 static void s_change_section PARAMS ((int));
748 static void s_cons PARAMS ((int));
749 static void s_float_cons PARAMS ((int));
750 static void s_mips_globl PARAMS ((int));
751 static void s_option PARAMS ((int));
752 static void s_mipsset PARAMS ((int));
753 static void s_abicalls PARAMS ((int));
754 static void s_cpload PARAMS ((int));
755 static void s_cpsetup PARAMS ((int));
756 static void s_cplocal PARAMS ((int));
757 static void s_cprestore PARAMS ((int));
758 static void s_cpreturn PARAMS ((int));
759 static void s_gpvalue PARAMS ((int));
760 static void s_gpword PARAMS ((int));
761 static void s_cpadd PARAMS ((int));
762 static void s_insn PARAMS ((int));
763 static void md_obj_begin PARAMS ((void));
764 static void md_obj_end PARAMS ((void));
765 static long get_number PARAMS ((void));
766 static void s_mips_ent PARAMS ((int));
767 static void s_mips_end PARAMS ((int));
768 static void s_mips_frame PARAMS ((int));
769 static void s_mips_mask PARAMS ((int));
770 static void s_mips_stab PARAMS ((int));
771 static void s_mips_weakext PARAMS ((int));
772 static void s_mips_file PARAMS ((int));
773 static void s_mips_loc PARAMS ((int));
774 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
775 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
776 static void show PARAMS ((FILE *, const char *, int *, int *));
777 #ifdef OBJ_ELF
778 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
779 #endif
781 /* Return values of my_getSmallExpression(). */
783 enum small_ex_type
785 S_EX_NONE = 0,
786 S_EX_REGISTER,
788 /* Direct relocation creation by %percent_op(). */
789 S_EX_HALF,
790 S_EX_HI,
791 S_EX_LO,
792 S_EX_GP_REL,
793 S_EX_GOT,
794 S_EX_CALL16,
795 S_EX_GOT_DISP,
796 S_EX_GOT_PAGE,
797 S_EX_GOT_OFST,
798 S_EX_GOT_HI,
799 S_EX_GOT_LO,
800 S_EX_NEG,
801 S_EX_HIGHER,
802 S_EX_HIGHEST,
803 S_EX_CALL_HI,
804 S_EX_CALL_LO
807 /* Table and functions used to map between CPU/ISA names, and
808 ISA levels, and CPU numbers. */
810 struct mips_cpu_info
812 const char *name; /* CPU or ISA name. */
813 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
814 int isa; /* ISA level. */
815 int cpu; /* CPU number (default CPU if ISA). */
818 static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
819 static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
820 static boolean mips_strict_matching_cpu_name_p PARAMS ((const char *,
821 const char *));
822 static boolean mips_matching_cpu_name_p PARAMS ((const char *, const char *));
823 static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
824 const char *));
825 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
827 /* Pseudo-op table.
829 The following pseudo-ops from the Kane and Heinrich MIPS book
830 should be defined here, but are currently unsupported: .alias,
831 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
833 The following pseudo-ops from the Kane and Heinrich MIPS book are
834 specific to the type of debugging information being generated, and
835 should be defined by the object format: .aent, .begin, .bend,
836 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
837 .vreg.
839 The following pseudo-ops from the Kane and Heinrich MIPS book are
840 not MIPS CPU specific, but are also not specific to the object file
841 format. This file is probably the best place to define them, but
842 they are not currently supported: .asm0, .endr, .lab, .repeat,
843 .struct. */
845 static const pseudo_typeS mips_pseudo_table[] =
847 /* MIPS specific pseudo-ops. */
848 {"option", s_option, 0},
849 {"set", s_mipsset, 0},
850 {"rdata", s_change_sec, 'r'},
851 {"sdata", s_change_sec, 's'},
852 {"livereg", s_ignore, 0},
853 {"abicalls", s_abicalls, 0},
854 {"cpload", s_cpload, 0},
855 {"cpsetup", s_cpsetup, 0},
856 {"cplocal", s_cplocal, 0},
857 {"cprestore", s_cprestore, 0},
858 {"cpreturn", s_cpreturn, 0},
859 {"gpvalue", s_gpvalue, 0},
860 {"gpword", s_gpword, 0},
861 {"cpadd", s_cpadd, 0},
862 {"insn", s_insn, 0},
864 /* Relatively generic pseudo-ops that happen to be used on MIPS
865 chips. */
866 {"asciiz", stringer, 1},
867 {"bss", s_change_sec, 'b'},
868 {"err", s_err, 0},
869 {"half", s_cons, 1},
870 {"dword", s_cons, 3},
871 {"weakext", s_mips_weakext, 0},
873 /* These pseudo-ops are defined in read.c, but must be overridden
874 here for one reason or another. */
875 {"align", s_align, 0},
876 {"byte", s_cons, 0},
877 {"data", s_change_sec, 'd'},
878 {"double", s_float_cons, 'd'},
879 {"float", s_float_cons, 'f'},
880 {"globl", s_mips_globl, 0},
881 {"global", s_mips_globl, 0},
882 {"hword", s_cons, 1},
883 {"int", s_cons, 2},
884 {"long", s_cons, 2},
885 {"octa", s_cons, 4},
886 {"quad", s_cons, 3},
887 {"section", s_change_section, 0},
888 {"short", s_cons, 1},
889 {"single", s_float_cons, 'f'},
890 {"stabn", s_mips_stab, 'n'},
891 {"text", s_change_sec, 't'},
892 {"word", s_cons, 2},
894 { "extern", ecoff_directive_extern, 0},
896 { NULL, NULL, 0 },
899 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
901 /* These pseudo-ops should be defined by the object file format.
902 However, a.out doesn't support them, so we have versions here. */
903 {"aent", s_mips_ent, 1},
904 {"bgnb", s_ignore, 0},
905 {"end", s_mips_end, 0},
906 {"endb", s_ignore, 0},
907 {"ent", s_mips_ent, 0},
908 {"file", s_mips_file, 0},
909 {"fmask", s_mips_mask, 'F'},
910 {"frame", s_mips_frame, 0},
911 {"loc", s_mips_loc, 0},
912 {"mask", s_mips_mask, 'R'},
913 {"verstamp", s_ignore, 0},
914 { NULL, NULL, 0 },
917 extern void pop_insert PARAMS ((const pseudo_typeS *));
919 void
920 mips_pop_insert ()
922 pop_insert (mips_pseudo_table);
923 if (! ECOFF_DEBUGGING)
924 pop_insert (mips_nonecoff_pseudo_table);
927 /* Symbols labelling the current insn. */
929 struct insn_label_list
931 struct insn_label_list *next;
932 symbolS *label;
935 static struct insn_label_list *insn_labels;
936 static struct insn_label_list *free_insn_labels;
938 static void mips_clear_insn_labels PARAMS ((void));
940 static inline void
941 mips_clear_insn_labels ()
943 register struct insn_label_list **pl;
945 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
947 *pl = insn_labels;
948 insn_labels = NULL;
951 static char *expr_end;
953 /* Expressions which appear in instructions. These are set by
954 mips_ip. */
956 static expressionS imm_expr;
957 static expressionS offset_expr;
959 /* Relocs associated with imm_expr and offset_expr. */
961 static bfd_reloc_code_real_type imm_reloc[3]
962 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
963 static bfd_reloc_code_real_type offset_reloc[3]
964 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
966 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
968 static boolean imm_unmatched_hi;
970 /* These are set by mips16_ip if an explicit extension is used. */
972 static boolean mips16_small, mips16_ext;
974 #ifdef OBJ_ELF
975 /* The pdr segment for per procedure frame/regmask info. Not used for
976 ECOFF debugging. */
978 static segT pdr_seg;
979 #endif
981 /* The default target format to use. */
983 const char *
984 mips_target_format ()
986 switch (OUTPUT_FLAVOR)
988 case bfd_target_aout_flavour:
989 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
990 case bfd_target_ecoff_flavour:
991 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
992 case bfd_target_coff_flavour:
993 return "pe-mips";
994 case bfd_target_elf_flavour:
995 #ifdef TE_TMIPS
996 /* This is traditional mips. */
997 return (target_big_endian
998 ? (HAVE_64BIT_OBJECTS
999 ? "elf64-tradbigmips"
1000 : (HAVE_NEWABI
1001 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1002 : (HAVE_64BIT_OBJECTS
1003 ? "elf64-tradlittlemips"
1004 : (HAVE_NEWABI
1005 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1006 #else
1007 return (target_big_endian
1008 ? (HAVE_64BIT_OBJECTS
1009 ? "elf64-bigmips"
1010 : (HAVE_NEWABI
1011 ? "elf32-nbigmips" : "elf32-bigmips"))
1012 : (HAVE_64BIT_OBJECTS
1013 ? "elf64-littlemips"
1014 : (HAVE_NEWABI
1015 ? "elf32-nlittlemips" : "elf32-littlemips")));
1016 #endif
1017 default:
1018 abort ();
1019 return NULL;
1023 /* This function is called once, at assembler startup time. It should
1024 set up all the tables, etc. that the MD part of the assembler will need. */
1026 void
1027 md_begin ()
1029 register const char *retval = NULL;
1030 int i = 0;
1031 int broken = 0;
1033 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1034 as_warn (_("Could not set architecture and machine"));
1036 op_hash = hash_new ();
1038 for (i = 0; i < NUMOPCODES;)
1040 const char *name = mips_opcodes[i].name;
1042 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1043 if (retval != NULL)
1045 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1046 mips_opcodes[i].name, retval);
1047 /* Probably a memory allocation problem? Give up now. */
1048 as_fatal (_("Broken assembler. No assembly attempted."));
1052 if (mips_opcodes[i].pinfo != INSN_MACRO)
1054 if (!validate_mips_insn (&mips_opcodes[i]))
1055 broken = 1;
1057 ++i;
1059 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1062 mips16_op_hash = hash_new ();
1064 i = 0;
1065 while (i < bfd_mips16_num_opcodes)
1067 const char *name = mips16_opcodes[i].name;
1069 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1070 if (retval != NULL)
1071 as_fatal (_("internal: can't hash `%s': %s"),
1072 mips16_opcodes[i].name, retval);
1075 if (mips16_opcodes[i].pinfo != INSN_MACRO
1076 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1077 != mips16_opcodes[i].match))
1079 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1080 mips16_opcodes[i].name, mips16_opcodes[i].args);
1081 broken = 1;
1083 ++i;
1085 while (i < bfd_mips16_num_opcodes
1086 && strcmp (mips16_opcodes[i].name, name) == 0);
1089 if (broken)
1090 as_fatal (_("Broken assembler. No assembly attempted."));
1092 /* We add all the general register names to the symbol table. This
1093 helps us detect invalid uses of them. */
1094 for (i = 0; i < 32; i++)
1096 char buf[5];
1098 sprintf (buf, "$%d", i);
1099 symbol_table_insert (symbol_new (buf, reg_section, i,
1100 &zero_address_frag));
1102 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1103 &zero_address_frag));
1104 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1105 &zero_address_frag));
1106 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1107 &zero_address_frag));
1108 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1109 &zero_address_frag));
1110 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1111 &zero_address_frag));
1112 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1113 &zero_address_frag));
1114 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1115 &zero_address_frag));
1116 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1117 &zero_address_frag));
1118 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1119 &zero_address_frag));
1121 mips_no_prev_insn (false);
1123 mips_gprmask = 0;
1124 mips_cprmask[0] = 0;
1125 mips_cprmask[1] = 0;
1126 mips_cprmask[2] = 0;
1127 mips_cprmask[3] = 0;
1129 /* set the default alignment for the text section (2**2) */
1130 record_alignment (text_section, 2);
1132 if (USE_GLOBAL_POINTER_OPT)
1133 bfd_set_gp_size (stdoutput, g_switch_value);
1135 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1137 /* On a native system, sections must be aligned to 16 byte
1138 boundaries. When configured for an embedded ELF target, we
1139 don't bother. */
1140 if (strcmp (TARGET_OS, "elf") != 0)
1142 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1143 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1144 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1147 /* Create a .reginfo section for register masks and a .mdebug
1148 section for debugging information. */
1150 segT seg;
1151 subsegT subseg;
1152 flagword flags;
1153 segT sec;
1155 seg = now_seg;
1156 subseg = now_subseg;
1158 /* The ABI says this section should be loaded so that the
1159 running program can access it. However, we don't load it
1160 if we are configured for an embedded target */
1161 flags = SEC_READONLY | SEC_DATA;
1162 if (strcmp (TARGET_OS, "elf") != 0)
1163 flags |= SEC_ALLOC | SEC_LOAD;
1165 if (mips_abi != N64_ABI)
1167 sec = subseg_new (".reginfo", (subsegT) 0);
1169 bfd_set_section_flags (stdoutput, sec, flags);
1170 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1172 #ifdef OBJ_ELF
1173 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1174 #endif
1176 else
1178 /* The 64-bit ABI uses a .MIPS.options section rather than
1179 .reginfo section. */
1180 sec = subseg_new (".MIPS.options", (subsegT) 0);
1181 bfd_set_section_flags (stdoutput, sec, flags);
1182 bfd_set_section_alignment (stdoutput, sec, 3);
1184 #ifdef OBJ_ELF
1185 /* Set up the option header. */
1187 Elf_Internal_Options opthdr;
1188 char *f;
1190 opthdr.kind = ODK_REGINFO;
1191 opthdr.size = (sizeof (Elf_External_Options)
1192 + sizeof (Elf64_External_RegInfo));
1193 opthdr.section = 0;
1194 opthdr.info = 0;
1195 f = frag_more (sizeof (Elf_External_Options));
1196 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1197 (Elf_External_Options *) f);
1199 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1201 #endif
1204 if (ECOFF_DEBUGGING)
1206 sec = subseg_new (".mdebug", (subsegT) 0);
1207 (void) bfd_set_section_flags (stdoutput, sec,
1208 SEC_HAS_CONTENTS | SEC_READONLY);
1209 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1211 #ifdef OBJ_ELF
1212 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1214 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1215 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1216 SEC_READONLY | SEC_RELOC
1217 | SEC_DEBUGGING);
1218 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1220 #endif
1222 subseg_set (seg, subseg);
1226 if (! ECOFF_DEBUGGING)
1227 md_obj_begin ();
1230 void
1231 md_mips_end ()
1233 if (! ECOFF_DEBUGGING)
1234 md_obj_end ();
1237 void
1238 md_assemble (str)
1239 char *str;
1241 struct mips_cl_insn insn;
1242 bfd_reloc_code_real_type unused_reloc[3]
1243 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1245 imm_expr.X_op = O_absent;
1246 imm_unmatched_hi = false;
1247 offset_expr.X_op = O_absent;
1248 imm_reloc[0] = BFD_RELOC_UNUSED;
1249 imm_reloc[1] = BFD_RELOC_UNUSED;
1250 imm_reloc[2] = BFD_RELOC_UNUSED;
1251 offset_reloc[0] = BFD_RELOC_UNUSED;
1252 offset_reloc[1] = BFD_RELOC_UNUSED;
1253 offset_reloc[2] = BFD_RELOC_UNUSED;
1255 if (mips_opts.mips16)
1256 mips16_ip (str, &insn);
1257 else
1259 mips_ip (str, &insn);
1260 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1261 str, insn.insn_opcode));
1264 if (insn_error)
1266 as_bad ("%s `%s'", insn_error, str);
1267 return;
1270 if (insn.insn_mo->pinfo == INSN_MACRO)
1272 if (mips_opts.mips16)
1273 mips16_macro (&insn);
1274 else
1275 macro (&insn);
1277 else
1279 if (imm_expr.X_op != O_absent)
1280 append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1281 else if (offset_expr.X_op != O_absent)
1282 append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1283 else
1284 append_insn (NULL, &insn, NULL, unused_reloc, false);
1288 /* See whether instruction IP reads register REG. CLASS is the type
1289 of register. */
1291 static int
1292 insn_uses_reg (ip, reg, class)
1293 struct mips_cl_insn *ip;
1294 unsigned int reg;
1295 enum mips_regclass class;
1297 if (class == MIPS16_REG)
1299 assert (mips_opts.mips16);
1300 reg = mips16_to_32_reg_map[reg];
1301 class = MIPS_GR_REG;
1304 /* Don't report on general register ZERO, since it never changes. */
1305 if (class == MIPS_GR_REG && reg == ZERO)
1306 return 0;
1308 if (class == MIPS_FP_REG)
1310 assert (! mips_opts.mips16);
1311 /* If we are called with either $f0 or $f1, we must check $f0.
1312 This is not optimal, because it will introduce an unnecessary
1313 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1314 need to distinguish reading both $f0 and $f1 or just one of
1315 them. Note that we don't have to check the other way,
1316 because there is no instruction that sets both $f0 and $f1
1317 and requires a delay. */
1318 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1319 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1320 == (reg &~ (unsigned) 1)))
1321 return 1;
1322 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1323 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1324 == (reg &~ (unsigned) 1)))
1325 return 1;
1327 else if (! mips_opts.mips16)
1329 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1330 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1331 return 1;
1332 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1333 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1334 return 1;
1336 else
1338 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1339 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1340 & MIPS16OP_MASK_RX)]
1341 == reg))
1342 return 1;
1343 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1344 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1345 & MIPS16OP_MASK_RY)]
1346 == reg))
1347 return 1;
1348 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1349 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1350 & MIPS16OP_MASK_MOVE32Z)]
1351 == reg))
1352 return 1;
1353 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1354 return 1;
1355 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1356 return 1;
1357 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1358 return 1;
1359 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1360 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1361 & MIPS16OP_MASK_REGR32) == reg)
1362 return 1;
1365 return 0;
1368 /* This function returns true if modifying a register requires a
1369 delay. */
1371 static int
1372 reg_needs_delay (reg)
1373 unsigned int reg;
1375 unsigned long prev_pinfo;
1377 prev_pinfo = prev_insn.insn_mo->pinfo;
1378 if (! mips_opts.noreorder
1379 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1380 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1381 || (! gpr_interlocks
1382 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1384 /* A load from a coprocessor or from memory. All load
1385 delays delay the use of general register rt for one
1386 instruction on the r3000. The r6000 and r4000 use
1387 interlocks. */
1388 /* Itbl support may require additional care here. */
1389 know (prev_pinfo & INSN_WRITE_GPR_T);
1390 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1391 return 1;
1394 return 0;
1397 /* Mark instruction labels in mips16 mode. This permits the linker to
1398 handle them specially, such as generating jalx instructions when
1399 needed. We also make them odd for the duration of the assembly, in
1400 order to generate the right sort of code. We will make them even
1401 in the adjust_symtab routine, while leaving them marked. This is
1402 convenient for the debugger and the disassembler. The linker knows
1403 to make them odd again. */
1405 static void
1406 mips16_mark_labels ()
1408 if (mips_opts.mips16)
1410 struct insn_label_list *l;
1411 valueT val;
1413 for (l = insn_labels; l != NULL; l = l->next)
1415 #ifdef OBJ_ELF
1416 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1417 S_SET_OTHER (l->label, STO_MIPS16);
1418 #endif
1419 val = S_GET_VALUE (l->label);
1420 if ((val & 1) == 0)
1421 S_SET_VALUE (l->label, val + 1);
1426 /* Output an instruction. PLACE is where to put the instruction; if
1427 it is NULL, this uses frag_more to get room. IP is the instruction
1428 information. ADDRESS_EXPR is an operand of the instruction to be
1429 used with RELOC_TYPE. */
1431 static void
1432 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1433 char *place;
1434 struct mips_cl_insn *ip;
1435 expressionS *address_expr;
1436 bfd_reloc_code_real_type *reloc_type;
1437 boolean unmatched_hi;
1439 register unsigned long prev_pinfo, pinfo;
1440 char *f;
1441 fixS *fixp[3];
1442 int nops = 0;
1444 /* Mark instruction labels in mips16 mode. */
1445 mips16_mark_labels ();
1447 prev_pinfo = prev_insn.insn_mo->pinfo;
1448 pinfo = ip->insn_mo->pinfo;
1450 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1452 int prev_prev_nop;
1454 /* If the previous insn required any delay slots, see if we need
1455 to insert a NOP or two. There are eight kinds of possible
1456 hazards, of which an instruction can have at most one type.
1457 (1) a load from memory delay
1458 (2) a load from a coprocessor delay
1459 (3) an unconditional branch delay
1460 (4) a conditional branch delay
1461 (5) a move to coprocessor register delay
1462 (6) a load coprocessor register from memory delay
1463 (7) a coprocessor condition code delay
1464 (8) a HI/LO special register delay
1466 There are a lot of optimizations we could do that we don't.
1467 In particular, we do not, in general, reorder instructions.
1468 If you use gcc with optimization, it will reorder
1469 instructions and generally do much more optimization then we
1470 do here; repeating all that work in the assembler would only
1471 benefit hand written assembly code, and does not seem worth
1472 it. */
1474 /* This is how a NOP is emitted. */
1475 #define emit_nop() \
1476 (mips_opts.mips16 \
1477 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1478 : md_number_to_chars (frag_more (4), 0, 4))
1480 /* The previous insn might require a delay slot, depending upon
1481 the contents of the current insn. */
1482 if (! mips_opts.mips16
1483 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1484 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1485 && ! cop_interlocks)
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 (mips_optimize == 0
1496 || insn_uses_reg (ip,
1497 ((prev_insn.insn_opcode >> OP_SH_RT)
1498 & OP_MASK_RT),
1499 MIPS_GR_REG))
1500 ++nops;
1502 else if (! mips_opts.mips16
1503 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1504 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1505 && ! cop_interlocks)
1506 || (mips_opts.isa == ISA_MIPS1
1507 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1509 /* A generic coprocessor delay. The previous instruction
1510 modified a coprocessor general or control register. If
1511 it modified a control register, we need to avoid any
1512 coprocessor instruction (this is probably not always
1513 required, but it sometimes is). If it modified a general
1514 register, we avoid using that register.
1516 On the r6000 and r4000 loading a coprocessor register
1517 from memory is interlocked, and does not require a delay.
1519 This case is not handled very well. There is no special
1520 knowledge of CP0 handling, and the coprocessors other
1521 than the floating point unit are not distinguished at
1522 all. */
1523 /* Itbl support may require additional care here. FIXME!
1524 Need to modify this to include knowledge about
1525 user specified delays! */
1526 if (prev_pinfo & INSN_WRITE_FPR_T)
1528 if (mips_optimize == 0
1529 || insn_uses_reg (ip,
1530 ((prev_insn.insn_opcode >> OP_SH_FT)
1531 & OP_MASK_FT),
1532 MIPS_FP_REG))
1533 ++nops;
1535 else if (prev_pinfo & INSN_WRITE_FPR_S)
1537 if (mips_optimize == 0
1538 || insn_uses_reg (ip,
1539 ((prev_insn.insn_opcode >> OP_SH_FS)
1540 & OP_MASK_FS),
1541 MIPS_FP_REG))
1542 ++nops;
1544 else
1546 /* We don't know exactly what the previous instruction
1547 does. If the current instruction uses a coprocessor
1548 register, we must insert a NOP. If previous
1549 instruction may set the condition codes, and the
1550 current instruction uses them, we must insert two
1551 NOPS. */
1552 /* Itbl support may require additional care here. */
1553 if (mips_optimize == 0
1554 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1555 && (pinfo & INSN_READ_COND_CODE)))
1556 nops += 2;
1557 else if (pinfo & INSN_COP)
1558 ++nops;
1561 else if (! mips_opts.mips16
1562 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1563 && (prev_pinfo & INSN_WRITE_COND_CODE)
1564 && ! cop_interlocks)
1566 /* The previous instruction sets the coprocessor condition
1567 codes, but does not require a general coprocessor delay
1568 (this means it is a floating point comparison
1569 instruction). If this instruction uses the condition
1570 codes, we need to insert a single NOP. */
1571 /* Itbl support may require additional care here. */
1572 if (mips_optimize == 0
1573 || (pinfo & INSN_READ_COND_CODE))
1574 ++nops;
1577 /* If we're fixing up mfhi/mflo for the r7000 and the
1578 previous insn was an mfhi/mflo and the current insn
1579 reads the register that the mfhi/mflo wrote to, then
1580 insert two nops. */
1582 else if (mips_7000_hilo_fix
1583 && MF_HILO_INSN (prev_pinfo)
1584 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1585 & OP_MASK_RD),
1586 MIPS_GR_REG))
1588 nops += 2;
1591 /* If we're fixing up mfhi/mflo for the r7000 and the
1592 2nd previous insn was an mfhi/mflo and the current insn
1593 reads the register that the mfhi/mflo wrote to, then
1594 insert one nop. */
1596 else if (mips_7000_hilo_fix
1597 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1598 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1599 & OP_MASK_RD),
1600 MIPS_GR_REG))
1603 ++nops;
1606 else if (prev_pinfo & INSN_READ_LO)
1608 /* The previous instruction reads the LO register; if the
1609 current instruction writes to the LO register, we must
1610 insert two NOPS. Some newer processors have interlocks.
1611 Also the tx39's multiply instructions can be exectuted
1612 immediatly after a read from HI/LO (without the delay),
1613 though the tx39's divide insns still do require the
1614 delay. */
1615 if (! (hilo_interlocks
1616 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1617 && (mips_optimize == 0
1618 || (pinfo & INSN_WRITE_LO)))
1619 nops += 2;
1620 /* Most mips16 branch insns don't have a delay slot.
1621 If a read from LO is immediately followed by a branch
1622 to a write to LO we have a read followed by a write
1623 less than 2 insns away. We assume the target of
1624 a branch might be a write to LO, and insert a nop
1625 between a read and an immediately following branch. */
1626 else if (mips_opts.mips16
1627 && (mips_optimize == 0
1628 || (pinfo & MIPS16_INSN_BRANCH)))
1629 ++nops;
1631 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1633 /* The previous instruction reads the HI register; if the
1634 current instruction writes to the HI register, we must
1635 insert a NOP. Some newer processors have interlocks.
1636 Also the note tx39's multiply above. */
1637 if (! (hilo_interlocks
1638 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1639 && (mips_optimize == 0
1640 || (pinfo & INSN_WRITE_HI)))
1641 nops += 2;
1642 /* Most mips16 branch insns don't have a delay slot.
1643 If a read from HI is immediately followed by a branch
1644 to a write to HI we have a read followed by a write
1645 less than 2 insns away. We assume the target of
1646 a branch might be a write to HI, and insert a nop
1647 between a read and an immediately following branch. */
1648 else if (mips_opts.mips16
1649 && (mips_optimize == 0
1650 || (pinfo & MIPS16_INSN_BRANCH)))
1651 ++nops;
1654 /* If the previous instruction was in a noreorder section, then
1655 we don't want to insert the nop after all. */
1656 /* Itbl support may require additional care here. */
1657 if (prev_insn_unreordered)
1658 nops = 0;
1660 /* There are two cases which require two intervening
1661 instructions: 1) setting the condition codes using a move to
1662 coprocessor instruction which requires a general coprocessor
1663 delay and then reading the condition codes 2) reading the HI
1664 or LO register and then writing to it (except on processors
1665 which have interlocks). If we are not already emitting a NOP
1666 instruction, we must check for these cases compared to the
1667 instruction previous to the previous instruction. */
1668 if ((! mips_opts.mips16
1669 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1670 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1671 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1672 && (pinfo & INSN_READ_COND_CODE)
1673 && ! cop_interlocks)
1674 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1675 && (pinfo & INSN_WRITE_LO)
1676 && ! (hilo_interlocks
1677 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1678 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1679 && (pinfo & INSN_WRITE_HI)
1680 && ! (hilo_interlocks
1681 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1682 prev_prev_nop = 1;
1683 else
1684 prev_prev_nop = 0;
1686 if (prev_prev_insn_unreordered)
1687 prev_prev_nop = 0;
1689 if (prev_prev_nop && nops == 0)
1690 ++nops;
1692 /* If we are being given a nop instruction, don't bother with
1693 one of the nops we would otherwise output. This will only
1694 happen when a nop instruction is used with mips_optimize set
1695 to 0. */
1696 if (nops > 0
1697 && ! mips_opts.noreorder
1698 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1699 --nops;
1701 /* Now emit the right number of NOP instructions. */
1702 if (nops > 0 && ! mips_opts.noreorder)
1704 fragS *old_frag;
1705 unsigned long old_frag_offset;
1706 int i;
1707 struct insn_label_list *l;
1709 old_frag = frag_now;
1710 old_frag_offset = frag_now_fix ();
1712 for (i = 0; i < nops; i++)
1713 emit_nop ();
1715 if (listing)
1717 listing_prev_line ();
1718 /* We may be at the start of a variant frag. In case we
1719 are, make sure there is enough space for the frag
1720 after the frags created by listing_prev_line. The
1721 argument to frag_grow here must be at least as large
1722 as the argument to all other calls to frag_grow in
1723 this file. We don't have to worry about being in the
1724 middle of a variant frag, because the variants insert
1725 all needed nop instructions themselves. */
1726 frag_grow (40);
1729 for (l = insn_labels; l != NULL; l = l->next)
1731 valueT val;
1733 assert (S_GET_SEGMENT (l->label) == now_seg);
1734 symbol_set_frag (l->label, frag_now);
1735 val = (valueT) frag_now_fix ();
1736 /* mips16 text labels are stored as odd. */
1737 if (mips_opts.mips16)
1738 ++val;
1739 S_SET_VALUE (l->label, val);
1742 #ifndef NO_ECOFF_DEBUGGING
1743 if (ECOFF_DEBUGGING)
1744 ecoff_fix_loc (old_frag, old_frag_offset);
1745 #endif
1747 else if (prev_nop_frag != NULL)
1749 /* We have a frag holding nops we may be able to remove. If
1750 we don't need any nops, we can decrease the size of
1751 prev_nop_frag by the size of one instruction. If we do
1752 need some nops, we count them in prev_nops_required. */
1753 if (prev_nop_frag_since == 0)
1755 if (nops == 0)
1757 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1758 --prev_nop_frag_holds;
1760 else
1761 prev_nop_frag_required += nops;
1763 else
1765 if (prev_prev_nop == 0)
1767 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1768 --prev_nop_frag_holds;
1770 else
1771 ++prev_nop_frag_required;
1774 if (prev_nop_frag_holds <= prev_nop_frag_required)
1775 prev_nop_frag = NULL;
1777 ++prev_nop_frag_since;
1779 /* Sanity check: by the time we reach the second instruction
1780 after prev_nop_frag, we should have used up all the nops
1781 one way or another. */
1782 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1786 if (*reloc_type > BFD_RELOC_UNUSED)
1788 /* We need to set up a variant frag. */
1789 assert (mips_opts.mips16 && address_expr != NULL);
1790 f = frag_var (rs_machine_dependent, 4, 0,
1791 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1792 mips16_small, mips16_ext,
1793 (prev_pinfo
1794 & INSN_UNCOND_BRANCH_DELAY),
1795 (*prev_insn_reloc_type
1796 == BFD_RELOC_MIPS16_JMP)),
1797 make_expr_symbol (address_expr), 0, NULL);
1799 else if (place != NULL)
1800 f = place;
1801 else if (mips_opts.mips16
1802 && ! ip->use_extend
1803 && *reloc_type != BFD_RELOC_MIPS16_JMP)
1805 /* Make sure there is enough room to swap this instruction with
1806 a following jump instruction. */
1807 frag_grow (6);
1808 f = frag_more (2);
1810 else
1812 if (mips_opts.mips16
1813 && mips_opts.noreorder
1814 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1815 as_warn (_("extended instruction in delay slot"));
1817 f = frag_more (4);
1820 fixp[0] = fixp[1] = fixp[2] = NULL;
1821 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1823 if (address_expr->X_op == O_constant)
1825 valueT tmp;
1827 switch (*reloc_type)
1829 case BFD_RELOC_32:
1830 ip->insn_opcode |= address_expr->X_add_number;
1831 break;
1833 case BFD_RELOC_MIPS_HIGHEST:
1834 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1835 tmp >>= 16;
1836 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1837 break;
1839 case BFD_RELOC_MIPS_HIGHER:
1840 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1841 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1842 break;
1844 case BFD_RELOC_HI16_S:
1845 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1846 >> 16) & 0xffff;
1847 break;
1849 case BFD_RELOC_HI16:
1850 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1851 break;
1853 case BFD_RELOC_LO16:
1854 case BFD_RELOC_MIPS_GOT_DISP:
1855 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1856 break;
1858 case BFD_RELOC_MIPS_JMP:
1859 if ((address_expr->X_add_number & 3) != 0)
1860 as_bad (_("jump to misaligned address (0x%lx)"),
1861 (unsigned long) address_expr->X_add_number);
1862 if (address_expr->X_add_number & ~0xfffffff)
1863 as_bad (_("jump address range overflow (0x%lx)"),
1864 (unsigned long) address_expr->X_add_number);
1865 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1866 break;
1868 case BFD_RELOC_MIPS16_JMP:
1869 if ((address_expr->X_add_number & 3) != 0)
1870 as_bad (_("jump to misaligned address (0x%lx)"),
1871 (unsigned long) address_expr->X_add_number);
1872 if (address_expr->X_add_number & ~0xfffffff)
1873 as_bad (_("jump address range overflow (0x%lx)"),
1874 (unsigned long) address_expr->X_add_number);
1875 ip->insn_opcode |=
1876 (((address_expr->X_add_number & 0x7c0000) << 3)
1877 | ((address_expr->X_add_number & 0xf800000) >> 7)
1878 | ((address_expr->X_add_number & 0x3fffc) >> 2));
1879 break;
1881 case BFD_RELOC_16_PCREL:
1882 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1883 break;
1885 case BFD_RELOC_16_PCREL_S2:
1886 goto need_reloc;
1888 default:
1889 internalError ();
1892 else
1894 need_reloc:
1895 /* Don't generate a reloc if we are writing into a variant frag. */
1896 if (place == NULL)
1898 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1899 address_expr,
1900 (*reloc_type == BFD_RELOC_16_PCREL
1901 || *reloc_type == BFD_RELOC_16_PCREL_S2),
1902 reloc_type[0]);
1904 /* These relocations can have an addend that won't fit in
1905 4 octets for 64bit assembly. */
1906 if (HAVE_64BIT_GPRS &&
1907 (*reloc_type == BFD_RELOC_16
1908 || *reloc_type == BFD_RELOC_32
1909 || *reloc_type == BFD_RELOC_MIPS_JMP
1910 || *reloc_type == BFD_RELOC_HI16_S
1911 || *reloc_type == BFD_RELOC_LO16
1912 || *reloc_type == BFD_RELOC_GPREL16
1913 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1914 || *reloc_type == BFD_RELOC_GPREL32
1915 || *reloc_type == BFD_RELOC_64
1916 || *reloc_type == BFD_RELOC_CTOR
1917 || *reloc_type == BFD_RELOC_MIPS_SUB
1918 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1919 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1920 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1921 || *reloc_type == BFD_RELOC_MIPS_REL16
1922 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1923 fixp[0]->fx_no_overflow = 1;
1925 if (unmatched_hi)
1927 struct mips_hi_fixup *hi_fixup;
1929 assert (*reloc_type == BFD_RELOC_HI16_S);
1930 hi_fixup = ((struct mips_hi_fixup *)
1931 xmalloc (sizeof (struct mips_hi_fixup)));
1932 hi_fixup->fixp = fixp[0];
1933 hi_fixup->seg = now_seg;
1934 hi_fixup->next = mips_hi_fixup_list;
1935 mips_hi_fixup_list = hi_fixup;
1938 if (reloc_type[1] != BFD_RELOC_UNUSED)
1940 /* FIXME: This symbol can be one of
1941 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
1942 address_expr->X_op = O_absent;
1943 address_expr->X_add_symbol = 0;
1944 address_expr->X_add_number = 0;
1946 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
1947 4, address_expr, false,
1948 reloc_type[1]);
1950 /* These relocations can have an addend that won't fit in
1951 4 octets for 64bit assembly. */
1952 if (HAVE_64BIT_GPRS &&
1953 (*reloc_type == BFD_RELOC_16
1954 || *reloc_type == BFD_RELOC_32
1955 || *reloc_type == BFD_RELOC_MIPS_JMP
1956 || *reloc_type == BFD_RELOC_HI16_S
1957 || *reloc_type == BFD_RELOC_LO16
1958 || *reloc_type == BFD_RELOC_GPREL16
1959 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1960 || *reloc_type == BFD_RELOC_GPREL32
1961 || *reloc_type == BFD_RELOC_64
1962 || *reloc_type == BFD_RELOC_CTOR
1963 || *reloc_type == BFD_RELOC_MIPS_SUB
1964 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1965 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1966 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1967 || *reloc_type == BFD_RELOC_MIPS_REL16
1968 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1969 fixp[1]->fx_no_overflow = 1;
1971 if (reloc_type[2] != BFD_RELOC_UNUSED)
1973 address_expr->X_op = O_absent;
1974 address_expr->X_add_symbol = 0;
1975 address_expr->X_add_number = 0;
1977 fixp[2] = fix_new_exp (frag_now,
1978 f - frag_now->fr_literal, 4,
1979 address_expr, false,
1980 reloc_type[2]);
1982 /* These relocations can have an addend that won't fit in
1983 4 octets for 64bit assembly. */
1984 if (HAVE_64BIT_GPRS &&
1985 (*reloc_type == BFD_RELOC_16
1986 || *reloc_type == BFD_RELOC_32
1987 || *reloc_type == BFD_RELOC_MIPS_JMP
1988 || *reloc_type == BFD_RELOC_HI16_S
1989 || *reloc_type == BFD_RELOC_LO16
1990 || *reloc_type == BFD_RELOC_GPREL16
1991 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1992 || *reloc_type == BFD_RELOC_GPREL32
1993 || *reloc_type == BFD_RELOC_64
1994 || *reloc_type == BFD_RELOC_CTOR
1995 || *reloc_type == BFD_RELOC_MIPS_SUB
1996 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1997 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1998 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1999 || *reloc_type == BFD_RELOC_MIPS_REL16
2000 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2001 fixp[2]->fx_no_overflow = 1;
2008 if (! mips_opts.mips16)
2010 md_number_to_chars (f, ip->insn_opcode, 4);
2011 #ifdef OBJ_ELF
2012 dwarf2_emit_insn (4);
2013 #endif
2015 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2017 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2018 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2019 #ifdef OBJ_ELF
2020 dwarf2_emit_insn (4);
2021 #endif
2023 else
2025 if (ip->use_extend)
2027 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2028 f += 2;
2030 md_number_to_chars (f, ip->insn_opcode, 2);
2031 #ifdef OBJ_ELF
2032 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2033 #endif
2036 /* Update the register mask information. */
2037 if (! mips_opts.mips16)
2039 if (pinfo & INSN_WRITE_GPR_D)
2040 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2041 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2042 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2043 if (pinfo & INSN_READ_GPR_S)
2044 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2045 if (pinfo & INSN_WRITE_GPR_31)
2046 mips_gprmask |= 1 << RA;
2047 if (pinfo & INSN_WRITE_FPR_D)
2048 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2049 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2050 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2051 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2052 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2053 if ((pinfo & INSN_READ_FPR_R) != 0)
2054 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2055 if (pinfo & INSN_COP)
2057 /* We don't keep enough information to sort these cases out.
2058 The itbl support does keep this information however, although
2059 we currently don't support itbl fprmats as part of the cop
2060 instruction. May want to add this support in the future. */
2062 /* Never set the bit for $0, which is always zero. */
2063 mips_gprmask &= ~1 << 0;
2065 else
2067 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2068 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2069 & MIPS16OP_MASK_RX);
2070 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2071 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2072 & MIPS16OP_MASK_RY);
2073 if (pinfo & MIPS16_INSN_WRITE_Z)
2074 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2075 & MIPS16OP_MASK_RZ);
2076 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2077 mips_gprmask |= 1 << TREG;
2078 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2079 mips_gprmask |= 1 << SP;
2080 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2081 mips_gprmask |= 1 << RA;
2082 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2083 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2084 if (pinfo & MIPS16_INSN_READ_Z)
2085 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2086 & MIPS16OP_MASK_MOVE32Z);
2087 if (pinfo & MIPS16_INSN_READ_GPR_X)
2088 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2089 & MIPS16OP_MASK_REGR32);
2092 if (place == NULL && ! mips_opts.noreorder)
2094 /* Filling the branch delay slot is more complex. We try to
2095 switch the branch with the previous instruction, which we can
2096 do if the previous instruction does not set up a condition
2097 that the branch tests and if the branch is not itself the
2098 target of any branch. */
2099 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2100 || (pinfo & INSN_COND_BRANCH_DELAY))
2102 if (mips_optimize < 2
2103 /* If we have seen .set volatile or .set nomove, don't
2104 optimize. */
2105 || mips_opts.nomove != 0
2106 /* If we had to emit any NOP instructions, then we
2107 already know we can not swap. */
2108 || nops != 0
2109 /* If we don't even know the previous insn, we can not
2110 swap. */
2111 || ! prev_insn_valid
2112 /* If the previous insn is already in a branch delay
2113 slot, then we can not swap. */
2114 || prev_insn_is_delay_slot
2115 /* If the previous previous insn was in a .set
2116 noreorder, we can't swap. Actually, the MIPS
2117 assembler will swap in this situation. However, gcc
2118 configured -with-gnu-as will generate code like
2119 .set noreorder
2120 lw $4,XXX
2121 .set reorder
2122 INSN
2123 bne $4,$0,foo
2124 in which we can not swap the bne and INSN. If gcc is
2125 not configured -with-gnu-as, it does not output the
2126 .set pseudo-ops. We don't have to check
2127 prev_insn_unreordered, because prev_insn_valid will
2128 be 0 in that case. We don't want to use
2129 prev_prev_insn_valid, because we do want to be able
2130 to swap at the start of a function. */
2131 || prev_prev_insn_unreordered
2132 /* If the branch is itself the target of a branch, we
2133 can not swap. We cheat on this; all we check for is
2134 whether there is a label on this instruction. If
2135 there are any branches to anything other than a
2136 label, users must use .set noreorder. */
2137 || insn_labels != NULL
2138 /* If the previous instruction is in a variant frag, we
2139 can not do the swap. This does not apply to the
2140 mips16, which uses variant frags for different
2141 purposes. */
2142 || (! mips_opts.mips16
2143 && prev_insn_frag->fr_type == rs_machine_dependent)
2144 /* If the branch reads the condition codes, we don't
2145 even try to swap, because in the sequence
2146 ctc1 $X,$31
2147 INSN
2148 INSN
2149 bc1t LABEL
2150 we can not swap, and I don't feel like handling that
2151 case. */
2152 || (! mips_opts.mips16
2153 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2154 && (pinfo & INSN_READ_COND_CODE))
2155 /* We can not swap with an instruction that requires a
2156 delay slot, becase the target of the branch might
2157 interfere with that instruction. */
2158 || (! mips_opts.mips16
2159 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2160 && (prev_pinfo
2161 /* Itbl support may require additional care here. */
2162 & (INSN_LOAD_COPROC_DELAY
2163 | INSN_COPROC_MOVE_DELAY
2164 | INSN_WRITE_COND_CODE)))
2165 || (! (hilo_interlocks
2166 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2167 && (prev_pinfo
2168 & (INSN_READ_LO
2169 | INSN_READ_HI)))
2170 || (! mips_opts.mips16
2171 && ! gpr_interlocks
2172 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2173 || (! mips_opts.mips16
2174 && mips_opts.isa == ISA_MIPS1
2175 /* Itbl support may require additional care here. */
2176 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2177 /* We can not swap with a branch instruction. */
2178 || (prev_pinfo
2179 & (INSN_UNCOND_BRANCH_DELAY
2180 | INSN_COND_BRANCH_DELAY
2181 | INSN_COND_BRANCH_LIKELY))
2182 /* We do not swap with a trap instruction, since it
2183 complicates trap handlers to have the trap
2184 instruction be in a delay slot. */
2185 || (prev_pinfo & INSN_TRAP)
2186 /* If the branch reads a register that the previous
2187 instruction sets, we can not swap. */
2188 || (! mips_opts.mips16
2189 && (prev_pinfo & INSN_WRITE_GPR_T)
2190 && insn_uses_reg (ip,
2191 ((prev_insn.insn_opcode >> OP_SH_RT)
2192 & OP_MASK_RT),
2193 MIPS_GR_REG))
2194 || (! mips_opts.mips16
2195 && (prev_pinfo & INSN_WRITE_GPR_D)
2196 && insn_uses_reg (ip,
2197 ((prev_insn.insn_opcode >> OP_SH_RD)
2198 & OP_MASK_RD),
2199 MIPS_GR_REG))
2200 || (mips_opts.mips16
2201 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2202 && insn_uses_reg (ip,
2203 ((prev_insn.insn_opcode
2204 >> MIPS16OP_SH_RX)
2205 & MIPS16OP_MASK_RX),
2206 MIPS16_REG))
2207 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2208 && insn_uses_reg (ip,
2209 ((prev_insn.insn_opcode
2210 >> MIPS16OP_SH_RY)
2211 & MIPS16OP_MASK_RY),
2212 MIPS16_REG))
2213 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2214 && insn_uses_reg (ip,
2215 ((prev_insn.insn_opcode
2216 >> MIPS16OP_SH_RZ)
2217 & MIPS16OP_MASK_RZ),
2218 MIPS16_REG))
2219 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2220 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2221 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2222 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2223 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2224 && insn_uses_reg (ip,
2225 MIPS16OP_EXTRACT_REG32R (prev_insn.
2226 insn_opcode),
2227 MIPS_GR_REG))))
2228 /* If the branch writes a register that the previous
2229 instruction sets, we can not swap (we know that
2230 branches write only to RD or to $31). */
2231 || (! mips_opts.mips16
2232 && (prev_pinfo & INSN_WRITE_GPR_T)
2233 && (((pinfo & INSN_WRITE_GPR_D)
2234 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2235 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2236 || ((pinfo & INSN_WRITE_GPR_31)
2237 && (((prev_insn.insn_opcode >> OP_SH_RT)
2238 & OP_MASK_RT)
2239 == RA))))
2240 || (! mips_opts.mips16
2241 && (prev_pinfo & INSN_WRITE_GPR_D)
2242 && (((pinfo & INSN_WRITE_GPR_D)
2243 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2244 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2245 || ((pinfo & INSN_WRITE_GPR_31)
2246 && (((prev_insn.insn_opcode >> OP_SH_RD)
2247 & OP_MASK_RD)
2248 == RA))))
2249 || (mips_opts.mips16
2250 && (pinfo & MIPS16_INSN_WRITE_31)
2251 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2252 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2253 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2254 == RA))))
2255 /* If the branch writes a register that the previous
2256 instruction reads, we can not swap (we know that
2257 branches only write to RD or to $31). */
2258 || (! mips_opts.mips16
2259 && (pinfo & INSN_WRITE_GPR_D)
2260 && insn_uses_reg (&prev_insn,
2261 ((ip->insn_opcode >> OP_SH_RD)
2262 & OP_MASK_RD),
2263 MIPS_GR_REG))
2264 || (! mips_opts.mips16
2265 && (pinfo & INSN_WRITE_GPR_31)
2266 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2267 || (mips_opts.mips16
2268 && (pinfo & MIPS16_INSN_WRITE_31)
2269 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2270 /* If we are generating embedded PIC code, the branch
2271 might be expanded into a sequence which uses $at, so
2272 we can't swap with an instruction which reads it. */
2273 || (mips_pic == EMBEDDED_PIC
2274 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2275 /* If the previous previous instruction has a load
2276 delay, and sets a register that the branch reads, we
2277 can not swap. */
2278 || (! mips_opts.mips16
2279 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2280 /* Itbl support may require additional care here. */
2281 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2282 || (! gpr_interlocks
2283 && (prev_prev_insn.insn_mo->pinfo
2284 & INSN_LOAD_MEMORY_DELAY)))
2285 && insn_uses_reg (ip,
2286 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2287 & OP_MASK_RT),
2288 MIPS_GR_REG))
2289 /* If one instruction sets a condition code and the
2290 other one uses a condition code, we can not swap. */
2291 || ((pinfo & INSN_READ_COND_CODE)
2292 && (prev_pinfo & INSN_WRITE_COND_CODE))
2293 || ((pinfo & INSN_WRITE_COND_CODE)
2294 && (prev_pinfo & INSN_READ_COND_CODE))
2295 /* If the previous instruction uses the PC, we can not
2296 swap. */
2297 || (mips_opts.mips16
2298 && (prev_pinfo & MIPS16_INSN_READ_PC))
2299 /* If the previous instruction was extended, we can not
2300 swap. */
2301 || (mips_opts.mips16 && prev_insn_extended)
2302 /* If the previous instruction had a fixup in mips16
2303 mode, we can not swap. This normally means that the
2304 previous instruction was a 4 byte branch anyhow. */
2305 || (mips_opts.mips16 && prev_insn_fixp[0])
2306 /* If the previous instruction is a sync, sync.l, or
2307 sync.p, we can not swap. */
2308 || (prev_pinfo & INSN_SYNC))
2310 /* We could do even better for unconditional branches to
2311 portions of this object file; we could pick up the
2312 instruction at the destination, put it in the delay
2313 slot, and bump the destination address. */
2314 emit_nop ();
2315 /* Update the previous insn information. */
2316 prev_prev_insn = *ip;
2317 prev_insn.insn_mo = &dummy_opcode;
2319 else
2321 /* It looks like we can actually do the swap. */
2322 if (! mips_opts.mips16)
2324 char *prev_f;
2325 char temp[4];
2327 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2328 memcpy (temp, prev_f, 4);
2329 memcpy (prev_f, f, 4);
2330 memcpy (f, temp, 4);
2331 if (prev_insn_fixp[0])
2333 prev_insn_fixp[0]->fx_frag = frag_now;
2334 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2336 if (prev_insn_fixp[1])
2338 prev_insn_fixp[1]->fx_frag = frag_now;
2339 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2341 if (prev_insn_fixp[2])
2343 prev_insn_fixp[2]->fx_frag = frag_now;
2344 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2346 if (fixp[0])
2348 fixp[0]->fx_frag = prev_insn_frag;
2349 fixp[0]->fx_where = prev_insn_where;
2351 if (fixp[1])
2353 fixp[1]->fx_frag = prev_insn_frag;
2354 fixp[1]->fx_where = prev_insn_where;
2356 if (fixp[2])
2358 fixp[2]->fx_frag = prev_insn_frag;
2359 fixp[2]->fx_where = prev_insn_where;
2362 else
2364 char *prev_f;
2365 char temp[2];
2367 assert (prev_insn_fixp[0] == NULL);
2368 assert (prev_insn_fixp[1] == NULL);
2369 assert (prev_insn_fixp[2] == NULL);
2370 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2371 memcpy (temp, prev_f, 2);
2372 memcpy (prev_f, f, 2);
2373 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2375 assert (*reloc_type == BFD_RELOC_UNUSED);
2376 memcpy (f, temp, 2);
2378 else
2380 memcpy (f, f + 2, 2);
2381 memcpy (f + 2, temp, 2);
2383 if (fixp[0])
2385 fixp[0]->fx_frag = prev_insn_frag;
2386 fixp[0]->fx_where = prev_insn_where;
2388 if (fixp[1])
2390 fixp[1]->fx_frag = prev_insn_frag;
2391 fixp[1]->fx_where = prev_insn_where;
2393 if (fixp[2])
2395 fixp[2]->fx_frag = prev_insn_frag;
2396 fixp[2]->fx_where = prev_insn_where;
2400 /* Update the previous insn information; leave prev_insn
2401 unchanged. */
2402 prev_prev_insn = *ip;
2404 prev_insn_is_delay_slot = 1;
2406 /* If that was an unconditional branch, forget the previous
2407 insn information. */
2408 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2410 prev_prev_insn.insn_mo = &dummy_opcode;
2411 prev_insn.insn_mo = &dummy_opcode;
2414 prev_insn_fixp[0] = NULL;
2415 prev_insn_fixp[1] = NULL;
2416 prev_insn_fixp[2] = NULL;
2417 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2418 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2419 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2420 prev_insn_extended = 0;
2422 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2424 /* We don't yet optimize a branch likely. What we should do
2425 is look at the target, copy the instruction found there
2426 into the delay slot, and increment the branch to jump to
2427 the next instruction. */
2428 emit_nop ();
2429 /* Update the previous insn information. */
2430 prev_prev_insn = *ip;
2431 prev_insn.insn_mo = &dummy_opcode;
2432 prev_insn_fixp[0] = NULL;
2433 prev_insn_fixp[1] = NULL;
2434 prev_insn_fixp[2] = NULL;
2435 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2436 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2437 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2438 prev_insn_extended = 0;
2440 else
2442 /* Update the previous insn information. */
2443 if (nops > 0)
2444 prev_prev_insn.insn_mo = &dummy_opcode;
2445 else
2446 prev_prev_insn = prev_insn;
2447 prev_insn = *ip;
2449 /* Any time we see a branch, we always fill the delay slot
2450 immediately; since this insn is not a branch, we know it
2451 is not in a delay slot. */
2452 prev_insn_is_delay_slot = 0;
2454 prev_insn_fixp[0] = fixp[0];
2455 prev_insn_fixp[1] = fixp[1];
2456 prev_insn_fixp[2] = fixp[2];
2457 prev_insn_reloc_type[0] = reloc_type[0];
2458 prev_insn_reloc_type[1] = reloc_type[1];
2459 prev_insn_reloc_type[2] = reloc_type[2];
2460 if (mips_opts.mips16)
2461 prev_insn_extended = (ip->use_extend
2462 || *reloc_type > BFD_RELOC_UNUSED);
2465 prev_prev_insn_unreordered = prev_insn_unreordered;
2466 prev_insn_unreordered = 0;
2467 prev_insn_frag = frag_now;
2468 prev_insn_where = f - frag_now->fr_literal;
2469 prev_insn_valid = 1;
2471 else if (place == NULL)
2473 /* We need to record a bit of information even when we are not
2474 reordering, in order to determine the base address for mips16
2475 PC relative relocs. */
2476 prev_prev_insn = prev_insn;
2477 prev_insn = *ip;
2478 prev_insn_reloc_type[0] = reloc_type[0];
2479 prev_insn_reloc_type[1] = reloc_type[1];
2480 prev_insn_reloc_type[2] = reloc_type[2];
2481 prev_prev_insn_unreordered = prev_insn_unreordered;
2482 prev_insn_unreordered = 1;
2485 /* We just output an insn, so the next one doesn't have a label. */
2486 mips_clear_insn_labels ();
2488 /* We must ensure that a fixup associated with an unmatched %hi
2489 reloc does not become a variant frag. Otherwise, the
2490 rearrangement of %hi relocs in frob_file may confuse
2491 tc_gen_reloc. */
2492 if (unmatched_hi)
2494 frag_wane (frag_now);
2495 frag_new (0);
2499 /* This function forgets that there was any previous instruction or
2500 label. If PRESERVE is non-zero, it remembers enough information to
2501 know whether nops are needed before a noreorder section. */
2503 static void
2504 mips_no_prev_insn (preserve)
2505 int preserve;
2507 if (! preserve)
2509 prev_insn.insn_mo = &dummy_opcode;
2510 prev_prev_insn.insn_mo = &dummy_opcode;
2511 prev_nop_frag = NULL;
2512 prev_nop_frag_holds = 0;
2513 prev_nop_frag_required = 0;
2514 prev_nop_frag_since = 0;
2516 prev_insn_valid = 0;
2517 prev_insn_is_delay_slot = 0;
2518 prev_insn_unreordered = 0;
2519 prev_insn_extended = 0;
2520 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2521 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2522 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2523 prev_prev_insn_unreordered = 0;
2524 mips_clear_insn_labels ();
2527 /* This function must be called whenever we turn on noreorder or emit
2528 something other than instructions. It inserts any NOPS which might
2529 be needed by the previous instruction, and clears the information
2530 kept for the previous instructions. The INSNS parameter is true if
2531 instructions are to follow. */
2533 static void
2534 mips_emit_delays (insns)
2535 boolean insns;
2537 if (! mips_opts.noreorder)
2539 int nops;
2541 nops = 0;
2542 if ((! mips_opts.mips16
2543 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2544 && (! cop_interlocks
2545 && (prev_insn.insn_mo->pinfo
2546 & (INSN_LOAD_COPROC_DELAY
2547 | INSN_COPROC_MOVE_DELAY
2548 | INSN_WRITE_COND_CODE))))
2549 || (! hilo_interlocks
2550 && (prev_insn.insn_mo->pinfo
2551 & (INSN_READ_LO
2552 | INSN_READ_HI)))
2553 || (! mips_opts.mips16
2554 && ! gpr_interlocks
2555 && (prev_insn.insn_mo->pinfo
2556 & INSN_LOAD_MEMORY_DELAY))
2557 || (! mips_opts.mips16
2558 && mips_opts.isa == ISA_MIPS1
2559 && (prev_insn.insn_mo->pinfo
2560 & INSN_COPROC_MEMORY_DELAY)))
2562 /* Itbl support may require additional care here. */
2563 ++nops;
2564 if ((! mips_opts.mips16
2565 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2566 && (! cop_interlocks
2567 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2568 || (! hilo_interlocks
2569 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2570 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2571 ++nops;
2573 if (prev_insn_unreordered)
2574 nops = 0;
2576 else if ((! mips_opts.mips16
2577 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2578 && (! cop_interlocks
2579 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2580 || (! hilo_interlocks
2581 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2582 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2584 /* Itbl support may require additional care here. */
2585 if (! prev_prev_insn_unreordered)
2586 ++nops;
2589 if (nops > 0)
2591 struct insn_label_list *l;
2593 if (insns)
2595 /* Record the frag which holds the nop instructions, so
2596 that we can remove them if we don't need them. */
2597 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2598 prev_nop_frag = frag_now;
2599 prev_nop_frag_holds = nops;
2600 prev_nop_frag_required = 0;
2601 prev_nop_frag_since = 0;
2604 for (; nops > 0; --nops)
2605 emit_nop ();
2607 if (insns)
2609 /* Move on to a new frag, so that it is safe to simply
2610 decrease the size of prev_nop_frag. */
2611 frag_wane (frag_now);
2612 frag_new (0);
2615 for (l = insn_labels; l != NULL; l = l->next)
2617 valueT val;
2619 assert (S_GET_SEGMENT (l->label) == now_seg);
2620 symbol_set_frag (l->label, frag_now);
2621 val = (valueT) frag_now_fix ();
2622 /* mips16 text labels are stored as odd. */
2623 if (mips_opts.mips16)
2624 ++val;
2625 S_SET_VALUE (l->label, val);
2630 /* Mark instruction labels in mips16 mode. */
2631 if (insns)
2632 mips16_mark_labels ();
2634 mips_no_prev_insn (insns);
2637 /* Build an instruction created by a macro expansion. This is passed
2638 a pointer to the count of instructions created so far, an
2639 expression, the name of the instruction to build, an operand format
2640 string, and corresponding arguments. */
2642 #ifdef USE_STDARG
2643 static void
2644 macro_build (char *place,
2645 int *counter,
2646 expressionS * ep,
2647 const char *name,
2648 const char *fmt,
2649 ...)
2650 #else
2651 static void
2652 macro_build (place, counter, ep, name, fmt, va_alist)
2653 char *place;
2654 int *counter;
2655 expressionS *ep;
2656 const char *name;
2657 const char *fmt;
2658 va_dcl
2659 #endif
2661 struct mips_cl_insn insn;
2662 bfd_reloc_code_real_type r[3];
2663 va_list args;
2665 #ifdef USE_STDARG
2666 va_start (args, fmt);
2667 #else
2668 va_start (args);
2669 #endif
2672 * If the macro is about to expand into a second instruction,
2673 * print a warning if needed. We need to pass ip as a parameter
2674 * to generate a better warning message here...
2676 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2677 as_warn (_("Macro instruction expanded into multiple instructions"));
2680 * If the macro is about to expand into a second instruction,
2681 * and it is in a delay slot, print a warning.
2683 if (place == NULL
2684 && *counter == 1
2685 && mips_opts.noreorder
2686 && (prev_prev_insn.insn_mo->pinfo
2687 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2688 | INSN_COND_BRANCH_LIKELY)) != 0)
2689 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2691 if (place == NULL)
2692 ++*counter; /* bump instruction counter */
2694 if (mips_opts.mips16)
2696 mips16_macro_build (place, counter, ep, name, fmt, args);
2697 va_end (args);
2698 return;
2701 r[0] = BFD_RELOC_UNUSED;
2702 r[1] = BFD_RELOC_UNUSED;
2703 r[2] = BFD_RELOC_UNUSED;
2704 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2705 assert (insn.insn_mo);
2706 assert (strcmp (name, insn.insn_mo->name) == 0);
2708 /* Search until we get a match for NAME. */
2709 while (1)
2711 /* It is assumed here that macros will never generate
2712 MDMX or MIPS-3D instructions. */
2713 if (strcmp (fmt, insn.insn_mo->args) == 0
2714 && insn.insn_mo->pinfo != INSN_MACRO
2715 && OPCODE_IS_MEMBER (insn.insn_mo,
2716 (mips_opts.isa
2717 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2718 mips_arch)
2719 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2720 break;
2722 ++insn.insn_mo;
2723 assert (insn.insn_mo->name);
2724 assert (strcmp (name, insn.insn_mo->name) == 0);
2727 insn.insn_opcode = insn.insn_mo->match;
2728 for (;;)
2730 switch (*fmt++)
2732 case '\0':
2733 break;
2735 case ',':
2736 case '(':
2737 case ')':
2738 continue;
2740 case 't':
2741 case 'w':
2742 case 'E':
2743 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2744 continue;
2746 case 'c':
2747 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2748 continue;
2750 case 'T':
2751 case 'W':
2752 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2753 continue;
2755 case 'd':
2756 case 'G':
2757 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2758 continue;
2760 case 'U':
2762 int tmp = va_arg (args, int);
2764 insn.insn_opcode |= tmp << OP_SH_RT;
2765 insn.insn_opcode |= tmp << OP_SH_RD;
2766 continue;
2769 case 'V':
2770 case 'S':
2771 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2772 continue;
2774 case 'z':
2775 continue;
2777 case '<':
2778 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2779 continue;
2781 case 'D':
2782 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2783 continue;
2785 case 'B':
2786 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2787 continue;
2789 case 'J':
2790 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2791 continue;
2793 case 'q':
2794 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2795 continue;
2797 case 'b':
2798 case 's':
2799 case 'r':
2800 case 'v':
2801 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2802 continue;
2804 case 'i':
2805 case 'j':
2806 case 'o':
2807 *r = (bfd_reloc_code_real_type) va_arg (args, int);
2808 assert (*r == BFD_RELOC_GPREL16
2809 || *r == BFD_RELOC_MIPS_LITERAL
2810 || *r == BFD_RELOC_MIPS_HIGHER
2811 || *r == BFD_RELOC_HI16_S
2812 || *r == BFD_RELOC_LO16
2813 || *r == BFD_RELOC_MIPS_GOT16
2814 || *r == BFD_RELOC_MIPS_CALL16
2815 || *r == BFD_RELOC_MIPS_GOT_DISP
2816 || *r == BFD_RELOC_MIPS_GOT_PAGE
2817 || *r == BFD_RELOC_MIPS_GOT_OFST
2818 || *r == BFD_RELOC_MIPS_GOT_LO16
2819 || *r == BFD_RELOC_MIPS_CALL_LO16
2820 || (ep->X_op == O_subtract
2821 && *r == BFD_RELOC_PCREL_LO16));
2822 continue;
2824 case 'u':
2825 *r = (bfd_reloc_code_real_type) va_arg (args, int);
2826 assert (ep != NULL
2827 && (ep->X_op == O_constant
2828 || (ep->X_op == O_symbol
2829 && (*r == BFD_RELOC_MIPS_HIGHEST
2830 || *r == BFD_RELOC_HI16_S
2831 || *r == BFD_RELOC_HI16
2832 || *r == BFD_RELOC_GPREL16
2833 || *r == BFD_RELOC_MIPS_GOT_HI16
2834 || *r == BFD_RELOC_MIPS_CALL_HI16))
2835 || (ep->X_op == O_subtract
2836 && *r == BFD_RELOC_PCREL_HI16_S)));
2837 continue;
2839 case 'p':
2840 assert (ep != NULL);
2842 * This allows macro() to pass an immediate expression for
2843 * creating short branches without creating a symbol.
2844 * Note that the expression still might come from the assembly
2845 * input, in which case the value is not checked for range nor
2846 * is a relocation entry generated (yuck).
2848 if (ep->X_op == O_constant)
2850 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2851 ep = NULL;
2853 else
2854 if (mips_pic == EMBEDDED_PIC)
2855 *r = BFD_RELOC_16_PCREL_S2;
2856 else
2857 *r = BFD_RELOC_16_PCREL;
2858 continue;
2860 case 'a':
2861 assert (ep != NULL);
2862 *r = BFD_RELOC_MIPS_JMP;
2863 continue;
2865 case 'C':
2866 insn.insn_opcode |= va_arg (args, unsigned long);
2867 continue;
2869 default:
2870 internalError ();
2872 break;
2874 va_end (args);
2875 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2877 append_insn (place, &insn, ep, r, false);
2880 static void
2881 mips16_macro_build (place, counter, ep, name, fmt, args)
2882 char *place;
2883 int *counter ATTRIBUTE_UNUSED;
2884 expressionS *ep;
2885 const char *name;
2886 const char *fmt;
2887 va_list args;
2889 struct mips_cl_insn insn;
2890 bfd_reloc_code_real_type r[3]
2891 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2893 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2894 assert (insn.insn_mo);
2895 assert (strcmp (name, insn.insn_mo->name) == 0);
2897 while (strcmp (fmt, insn.insn_mo->args) != 0
2898 || insn.insn_mo->pinfo == INSN_MACRO)
2900 ++insn.insn_mo;
2901 assert (insn.insn_mo->name);
2902 assert (strcmp (name, insn.insn_mo->name) == 0);
2905 insn.insn_opcode = insn.insn_mo->match;
2906 insn.use_extend = false;
2908 for (;;)
2910 int c;
2912 c = *fmt++;
2913 switch (c)
2915 case '\0':
2916 break;
2918 case ',':
2919 case '(':
2920 case ')':
2921 continue;
2923 case 'y':
2924 case 'w':
2925 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2926 continue;
2928 case 'x':
2929 case 'v':
2930 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2931 continue;
2933 case 'z':
2934 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2935 continue;
2937 case 'Z':
2938 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2939 continue;
2941 case '0':
2942 case 'S':
2943 case 'P':
2944 case 'R':
2945 continue;
2947 case 'X':
2948 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2949 continue;
2951 case 'Y':
2953 int regno;
2955 regno = va_arg (args, int);
2956 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2957 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2959 continue;
2961 case '<':
2962 case '>':
2963 case '4':
2964 case '5':
2965 case 'H':
2966 case 'W':
2967 case 'D':
2968 case 'j':
2969 case '8':
2970 case 'V':
2971 case 'C':
2972 case 'U':
2973 case 'k':
2974 case 'K':
2975 case 'p':
2976 case 'q':
2978 assert (ep != NULL);
2980 if (ep->X_op != O_constant)
2981 *r = (int) BFD_RELOC_UNUSED + c;
2982 else
2984 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
2985 false, &insn.insn_opcode, &insn.use_extend,
2986 &insn.extend);
2987 ep = NULL;
2988 *r = BFD_RELOC_UNUSED;
2991 continue;
2993 case '6':
2994 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2995 continue;
2998 break;
3001 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3003 append_insn (place, &insn, ep, r, false);
3007 * Generate a "jalr" instruction with a relocation hint to the called
3008 * function. This occurs in NewABI PIC code.
3010 static void
3011 macro_build_jalr (icnt, ep)
3012 int icnt;
3013 expressionS *ep;
3015 char *f;
3017 if (HAVE_NEWABI)
3019 frag_grow (4);
3020 f = frag_more (0);
3022 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3023 RA, PIC_CALL_REG);
3024 if (HAVE_NEWABI)
3025 fix_new_exp (frag_now, f - frag_now->fr_literal,
3026 0, ep, false, BFD_RELOC_MIPS_JALR);
3030 * Generate a "lui" instruction.
3032 static void
3033 macro_build_lui (place, counter, ep, regnum)
3034 char *place;
3035 int *counter;
3036 expressionS *ep;
3037 int regnum;
3039 expressionS high_expr;
3040 struct mips_cl_insn insn;
3041 bfd_reloc_code_real_type r[3]
3042 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3043 const char *name = "lui";
3044 const char *fmt = "t,u";
3046 assert (! mips_opts.mips16);
3048 if (place == NULL)
3049 high_expr = *ep;
3050 else
3052 high_expr.X_op = O_constant;
3053 high_expr.X_add_number = ep->X_add_number;
3056 if (high_expr.X_op == O_constant)
3058 /* we can compute the instruction now without a relocation entry */
3059 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3060 >> 16) & 0xffff;
3061 *r = BFD_RELOC_UNUSED;
3063 else if (! HAVE_NEWABI)
3065 assert (ep->X_op == O_symbol);
3066 /* _gp_disp is a special case, used from s_cpload. */
3067 assert (mips_pic == NO_PIC
3068 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3069 *r = BFD_RELOC_HI16_S;
3073 * If the macro is about to expand into a second instruction,
3074 * print a warning if needed. We need to pass ip as a parameter
3075 * to generate a better warning message here...
3077 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3078 as_warn (_("Macro instruction expanded into multiple instructions"));
3080 if (place == NULL)
3081 ++*counter; /* bump instruction counter */
3083 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3084 assert (insn.insn_mo);
3085 assert (strcmp (name, insn.insn_mo->name) == 0);
3086 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3088 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3089 if (*r == BFD_RELOC_UNUSED)
3091 insn.insn_opcode |= high_expr.X_add_number;
3092 append_insn (place, &insn, NULL, r, false);
3094 else
3095 append_insn (place, &insn, &high_expr, r, false);
3098 /* Generate a sequence of instructions to do a load or store from a constant
3099 offset off of a base register (breg) into/from a target register (treg),
3100 using AT if necessary. */
3101 static void
3102 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3103 char *place;
3104 int *counter;
3105 expressionS *ep;
3106 const char *op;
3107 int treg, breg;
3109 assert (ep->X_op == O_constant);
3111 /* Right now, this routine can only handle signed 32-bit contants. */
3112 if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3113 as_warn (_("operand overflow"));
3115 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3117 /* Signed 16-bit offset will fit in the op. Easy! */
3118 macro_build (place, counter, ep, op, "t,o(b)", treg,
3119 (int) BFD_RELOC_LO16, breg);
3121 else
3123 /* 32-bit offset, need multiple instructions and AT, like:
3124 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3125 addu $tempreg,$tempreg,$breg
3126 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3127 to handle the complete offset. */
3128 macro_build_lui (place, counter, ep, AT);
3129 if (place != NULL)
3130 place += 4;
3131 macro_build (place, counter, (expressionS *) NULL,
3132 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3133 "d,v,t", AT, AT, breg);
3134 if (place != NULL)
3135 place += 4;
3136 macro_build (place, counter, ep, op, "t,o(b)", treg,
3137 (int) BFD_RELOC_LO16, AT);
3139 if (mips_opts.noat)
3140 as_warn (_("Macro used $at after \".set noat\""));
3144 /* set_at()
3145 * Generates code to set the $at register to true (one)
3146 * if reg is less than the immediate expression.
3148 static void
3149 set_at (counter, reg, unsignedp)
3150 int *counter;
3151 int reg;
3152 int unsignedp;
3154 if (imm_expr.X_op == O_constant
3155 && imm_expr.X_add_number >= -0x8000
3156 && imm_expr.X_add_number < 0x8000)
3157 macro_build ((char *) NULL, counter, &imm_expr,
3158 unsignedp ? "sltiu" : "slti",
3159 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3160 else
3162 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3163 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3164 unsignedp ? "sltu" : "slt",
3165 "d,v,t", AT, reg, AT);
3169 /* Warn if an expression is not a constant. */
3171 static void
3172 check_absolute_expr (ip, ex)
3173 struct mips_cl_insn *ip;
3174 expressionS *ex;
3176 if (ex->X_op == O_big)
3177 as_bad (_("unsupported large constant"));
3178 else if (ex->X_op != O_constant)
3179 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3182 /* Count the leading zeroes by performing a binary chop. This is a
3183 bulky bit of source, but performance is a LOT better for the
3184 majority of values than a simple loop to count the bits:
3185 for (lcnt = 0; (lcnt < 32); lcnt++)
3186 if ((v) & (1 << (31 - lcnt)))
3187 break;
3188 However it is not code size friendly, and the gain will drop a bit
3189 on certain cached systems.
3191 #define COUNT_TOP_ZEROES(v) \
3192 (((v) & ~0xffff) == 0 \
3193 ? ((v) & ~0xff) == 0 \
3194 ? ((v) & ~0xf) == 0 \
3195 ? ((v) & ~0x3) == 0 \
3196 ? ((v) & ~0x1) == 0 \
3197 ? !(v) \
3198 ? 32 \
3199 : 31 \
3200 : 30 \
3201 : ((v) & ~0x7) == 0 \
3202 ? 29 \
3203 : 28 \
3204 : ((v) & ~0x3f) == 0 \
3205 ? ((v) & ~0x1f) == 0 \
3206 ? 27 \
3207 : 26 \
3208 : ((v) & ~0x7f) == 0 \
3209 ? 25 \
3210 : 24 \
3211 : ((v) & ~0xfff) == 0 \
3212 ? ((v) & ~0x3ff) == 0 \
3213 ? ((v) & ~0x1ff) == 0 \
3214 ? 23 \
3215 : 22 \
3216 : ((v) & ~0x7ff) == 0 \
3217 ? 21 \
3218 : 20 \
3219 : ((v) & ~0x3fff) == 0 \
3220 ? ((v) & ~0x1fff) == 0 \
3221 ? 19 \
3222 : 18 \
3223 : ((v) & ~0x7fff) == 0 \
3224 ? 17 \
3225 : 16 \
3226 : ((v) & ~0xffffff) == 0 \
3227 ? ((v) & ~0xfffff) == 0 \
3228 ? ((v) & ~0x3ffff) == 0 \
3229 ? ((v) & ~0x1ffff) == 0 \
3230 ? 15 \
3231 : 14 \
3232 : ((v) & ~0x7ffff) == 0 \
3233 ? 13 \
3234 : 12 \
3235 : ((v) & ~0x3fffff) == 0 \
3236 ? ((v) & ~0x1fffff) == 0 \
3237 ? 11 \
3238 : 10 \
3239 : ((v) & ~0x7fffff) == 0 \
3240 ? 9 \
3241 : 8 \
3242 : ((v) & ~0xfffffff) == 0 \
3243 ? ((v) & ~0x3ffffff) == 0 \
3244 ? ((v) & ~0x1ffffff) == 0 \
3245 ? 7 \
3246 : 6 \
3247 : ((v) & ~0x7ffffff) == 0 \
3248 ? 5 \
3249 : 4 \
3250 : ((v) & ~0x3fffffff) == 0 \
3251 ? ((v) & ~0x1fffffff) == 0 \
3252 ? 3 \
3253 : 2 \
3254 : ((v) & ~0x7fffffff) == 0 \
3255 ? 1 \
3256 : 0)
3258 /* load_register()
3259 * This routine generates the least number of instructions neccessary to load
3260 * an absolute expression value into a register.
3262 static void
3263 load_register (counter, reg, ep, dbl)
3264 int *counter;
3265 int reg;
3266 expressionS *ep;
3267 int dbl;
3269 int freg;
3270 expressionS hi32, lo32;
3272 if (ep->X_op != O_big)
3274 assert (ep->X_op == O_constant);
3275 if (ep->X_add_number < 0x8000
3276 && (ep->X_add_number >= 0
3277 || (ep->X_add_number >= -0x8000
3278 && (! dbl
3279 || ! ep->X_unsigned
3280 || sizeof (ep->X_add_number) > 4))))
3282 /* We can handle 16 bit signed values with an addiu to
3283 $zero. No need to ever use daddiu here, since $zero and
3284 the result are always correct in 32 bit mode. */
3285 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3286 (int) BFD_RELOC_LO16);
3287 return;
3289 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3291 /* We can handle 16 bit unsigned values with an ori to
3292 $zero. */
3293 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3294 (int) BFD_RELOC_LO16);
3295 return;
3297 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3298 && (! dbl
3299 || ! ep->X_unsigned
3300 || sizeof (ep->X_add_number) > 4
3301 || (ep->X_add_number & 0x80000000) == 0))
3302 || ((HAVE_32BIT_GPRS || ! dbl)
3303 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3304 || (HAVE_32BIT_GPRS
3305 && ! dbl
3306 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3307 == ~ (offsetT) 0xffffffff)))
3309 /* 32 bit values require an lui. */
3310 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3311 (int) BFD_RELOC_HI16);
3312 if ((ep->X_add_number & 0xffff) != 0)
3313 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3314 (int) BFD_RELOC_LO16);
3315 return;
3319 /* The value is larger than 32 bits. */
3321 if (HAVE_32BIT_GPRS)
3323 as_bad (_("Number (0x%lx) larger than 32 bits"),
3324 (unsigned long) ep->X_add_number);
3325 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3326 (int) BFD_RELOC_LO16);
3327 return;
3330 if (ep->X_op != O_big)
3332 hi32 = *ep;
3333 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3334 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3335 hi32.X_add_number &= 0xffffffff;
3336 lo32 = *ep;
3337 lo32.X_add_number &= 0xffffffff;
3339 else
3341 assert (ep->X_add_number > 2);
3342 if (ep->X_add_number == 3)
3343 generic_bignum[3] = 0;
3344 else if (ep->X_add_number > 4)
3345 as_bad (_("Number larger than 64 bits"));
3346 lo32.X_op = O_constant;
3347 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3348 hi32.X_op = O_constant;
3349 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3352 if (hi32.X_add_number == 0)
3353 freg = 0;
3354 else
3356 int shift, bit;
3357 unsigned long hi, lo;
3359 if (hi32.X_add_number == (offsetT) 0xffffffff)
3361 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3363 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3364 reg, 0, (int) BFD_RELOC_LO16);
3365 return;
3367 if (lo32.X_add_number & 0x80000000)
3369 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3370 (int) BFD_RELOC_HI16);
3371 if (lo32.X_add_number & 0xffff)
3372 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3373 reg, reg, (int) BFD_RELOC_LO16);
3374 return;
3378 /* Check for 16bit shifted constant. We know that hi32 is
3379 non-zero, so start the mask on the first bit of the hi32
3380 value. */
3381 shift = 17;
3384 unsigned long himask, lomask;
3386 if (shift < 32)
3388 himask = 0xffff >> (32 - shift);
3389 lomask = (0xffff << shift) & 0xffffffff;
3391 else
3393 himask = 0xffff << (shift - 32);
3394 lomask = 0;
3396 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3397 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3399 expressionS tmp;
3401 tmp.X_op = O_constant;
3402 if (shift < 32)
3403 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3404 | (lo32.X_add_number >> shift));
3405 else
3406 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3407 macro_build ((char *) NULL, counter, &tmp,
3408 "ori", "t,r,i", reg, 0,
3409 (int) BFD_RELOC_LO16);
3410 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3411 (shift >= 32) ? "dsll32" : "dsll",
3412 "d,w,<", reg, reg,
3413 (shift >= 32) ? shift - 32 : shift);
3414 return;
3416 ++shift;
3418 while (shift <= (64 - 16));
3420 /* Find the bit number of the lowest one bit, and store the
3421 shifted value in hi/lo. */
3422 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3423 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3424 if (lo != 0)
3426 bit = 0;
3427 while ((lo & 1) == 0)
3429 lo >>= 1;
3430 ++bit;
3432 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3433 hi >>= bit;
3435 else
3437 bit = 32;
3438 while ((hi & 1) == 0)
3440 hi >>= 1;
3441 ++bit;
3443 lo = hi;
3444 hi = 0;
3447 /* Optimize if the shifted value is a (power of 2) - 1. */
3448 if ((hi == 0 && ((lo + 1) & lo) == 0)
3449 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3451 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3452 if (shift != 0)
3454 expressionS tmp;
3456 /* This instruction will set the register to be all
3457 ones. */
3458 tmp.X_op = O_constant;
3459 tmp.X_add_number = (offsetT) -1;
3460 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3461 reg, 0, (int) BFD_RELOC_LO16);
3462 if (bit != 0)
3464 bit += shift;
3465 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3466 (bit >= 32) ? "dsll32" : "dsll",
3467 "d,w,<", reg, reg,
3468 (bit >= 32) ? bit - 32 : bit);
3470 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3471 (shift >= 32) ? "dsrl32" : "dsrl",
3472 "d,w,<", reg, reg,
3473 (shift >= 32) ? shift - 32 : shift);
3474 return;
3478 /* Sign extend hi32 before calling load_register, because we can
3479 generally get better code when we load a sign extended value. */
3480 if ((hi32.X_add_number & 0x80000000) != 0)
3481 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3482 load_register (counter, reg, &hi32, 0);
3483 freg = reg;
3485 if ((lo32.X_add_number & 0xffff0000) == 0)
3487 if (freg != 0)
3489 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3490 "dsll32", "d,w,<", reg, freg, 0);
3491 freg = reg;
3494 else
3496 expressionS mid16;
3498 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3500 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3501 (int) BFD_RELOC_HI16);
3502 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3503 "dsrl32", "d,w,<", reg, reg, 0);
3504 return;
3507 if (freg != 0)
3509 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3510 "d,w,<", reg, freg, 16);
3511 freg = reg;
3513 mid16 = lo32;
3514 mid16.X_add_number >>= 16;
3515 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3516 freg, (int) BFD_RELOC_LO16);
3517 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3518 "d,w,<", reg, reg, 16);
3519 freg = reg;
3521 if ((lo32.X_add_number & 0xffff) != 0)
3522 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3523 (int) BFD_RELOC_LO16);
3526 /* Load an address into a register. */
3528 static void
3529 load_address (counter, reg, ep, used_at)
3530 int *counter;
3531 int reg;
3532 expressionS *ep;
3533 int *used_at;
3535 char *p = NULL;
3537 if (ep->X_op != O_constant
3538 && ep->X_op != O_symbol)
3540 as_bad (_("expression too complex"));
3541 ep->X_op = O_constant;
3544 if (ep->X_op == O_constant)
3546 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3547 return;
3550 if (mips_pic == NO_PIC)
3552 /* If this is a reference to a GP relative symbol, we want
3553 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3554 Otherwise we want
3555 lui $reg,<sym> (BFD_RELOC_HI16_S)
3556 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3557 If we have an addend, we always use the latter form.
3559 With 64bit address space and a usable $at we want
3560 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3561 lui $at,<sym> (BFD_RELOC_HI16_S)
3562 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3563 daddiu $at,<sym> (BFD_RELOC_LO16)
3564 dsll32 $reg,0
3565 daddu $reg,$reg,$at
3567 If $at is already in use, we use an path which is suboptimal
3568 on superscalar processors.
3569 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3570 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3571 dsll $reg,16
3572 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3573 dsll $reg,16
3574 daddiu $reg,<sym> (BFD_RELOC_LO16)
3576 if (HAVE_64BIT_ADDRESSES)
3578 /* We don't do GP optimization for now because RELAX_ENCODE can't
3579 hold the data for such large chunks. */
3581 if (*used_at == 0 && ! mips_opts.noat)
3583 macro_build (p, counter, ep, "lui", "t,u",
3584 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3585 macro_build (p, counter, ep, "lui", "t,u",
3586 AT, (int) BFD_RELOC_HI16_S);
3587 macro_build (p, counter, ep, "daddiu", "t,r,j",
3588 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3589 macro_build (p, counter, ep, "daddiu", "t,r,j",
3590 AT, AT, (int) BFD_RELOC_LO16);
3591 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3592 "d,w,<", reg, reg, 0);
3593 macro_build (p, counter, (expressionS *) NULL, "daddu",
3594 "d,v,t", reg, reg, AT);
3595 *used_at = 1;
3597 else
3599 macro_build (p, counter, ep, "lui", "t,u",
3600 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3601 macro_build (p, counter, ep, "daddiu", "t,r,j",
3602 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3603 macro_build (p, counter, (expressionS *) NULL, "dsll",
3604 "d,w,<", reg, reg, 16);
3605 macro_build (p, counter, ep, "daddiu", "t,r,j",
3606 reg, reg, (int) BFD_RELOC_HI16_S);
3607 macro_build (p, counter, (expressionS *) NULL, "dsll",
3608 "d,w,<", reg, reg, 16);
3609 macro_build (p, counter, ep, "daddiu", "t,r,j",
3610 reg, reg, (int) BFD_RELOC_LO16);
3613 else
3615 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3616 && ! nopic_need_relax (ep->X_add_symbol, 1))
3618 frag_grow (20);
3619 macro_build ((char *) NULL, counter, ep,
3620 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3621 reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3622 p = frag_var (rs_machine_dependent, 8, 0,
3623 RELAX_ENCODE (4, 8, 0, 4, 0,
3624 mips_opts.warn_about_macros),
3625 ep->X_add_symbol, 0, NULL);
3627 macro_build_lui (p, counter, ep, reg);
3628 if (p != NULL)
3629 p += 4;
3630 macro_build (p, counter, ep,
3631 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3632 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3635 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3637 expressionS ex;
3639 /* If this is a reference to an external symbol, we want
3640 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3641 Otherwise we want
3642 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3644 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3645 If we have NewABI, we want
3646 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3647 If there is a constant, it must be added in after. */
3648 ex.X_add_number = ep->X_add_number;
3649 ep->X_add_number = 0;
3650 frag_grow (20);
3651 if (HAVE_NEWABI)
3653 macro_build ((char *) NULL, counter, ep,
3654 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3655 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3657 else
3659 macro_build ((char *) NULL, counter, ep,
3660 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3661 reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3662 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3663 p = frag_var (rs_machine_dependent, 4, 0,
3664 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3665 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3666 macro_build (p, counter, ep,
3667 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3668 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3671 if (ex.X_add_number != 0)
3673 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3674 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3675 ex.X_op = O_constant;
3676 macro_build ((char *) NULL, counter, &ex,
3677 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3678 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3681 else if (mips_pic == SVR4_PIC)
3683 expressionS ex;
3684 int off;
3686 /* This is the large GOT case. If this is a reference to an
3687 external symbol, we want
3688 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3689 addu $reg,$reg,$gp
3690 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3691 Otherwise, for a reference to a local symbol, we want
3692 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3694 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3695 If we have NewABI, we want
3696 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3697 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
3698 If there is a constant, it must be added in after. */
3699 ex.X_add_number = ep->X_add_number;
3700 ep->X_add_number = 0;
3701 if (HAVE_NEWABI)
3703 macro_build ((char *) NULL, counter, ep,
3704 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3705 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3706 macro_build (p, counter, ep,
3707 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3708 reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3710 else
3712 if (reg_needs_delay (mips_gp_register))
3713 off = 4;
3714 else
3715 off = 0;
3716 frag_grow (32);
3717 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3718 (int) BFD_RELOC_MIPS_GOT_HI16);
3719 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3720 HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3721 reg, mips_gp_register);
3722 macro_build ((char *) NULL, counter, ep,
3723 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3724 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3725 p = frag_var (rs_machine_dependent, 12 + off, 0,
3726 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3727 mips_opts.warn_about_macros),
3728 ep->X_add_symbol, 0, NULL);
3729 if (off > 0)
3731 /* We need a nop before loading from $gp. This special
3732 check is required because the lui which starts the main
3733 instruction stream does not refer to $gp, and so will not
3734 insert the nop which may be required. */
3735 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3736 p += 4;
3738 macro_build (p, counter, ep,
3739 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3740 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3741 p += 4;
3742 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3743 p += 4;
3744 macro_build (p, counter, ep,
3745 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3746 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3749 if (ex.X_add_number != 0)
3751 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3752 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3753 ex.X_op = O_constant;
3754 macro_build ((char *) NULL, counter, &ex,
3755 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3756 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3759 else if (mips_pic == EMBEDDED_PIC)
3761 /* We always do
3762 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3764 macro_build ((char *) NULL, counter, ep,
3765 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3766 "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3768 else
3769 abort ();
3772 /* Move the contents of register SOURCE into register DEST. */
3774 static void
3775 move_register (counter, dest, source)
3776 int *counter;
3777 int dest;
3778 int source;
3780 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3781 HAVE_32BIT_GPRS ? "addu" : "daddu",
3782 "d,v,t", dest, source, 0);
3786 * Build macros
3787 * This routine implements the seemingly endless macro or synthesized
3788 * instructions and addressing modes in the mips assembly language. Many
3789 * of these macros are simple and are similar to each other. These could
3790 * probably be handled by some kind of table or grammer aproach instead of
3791 * this verbose method. Others are not simple macros but are more like
3792 * optimizing code generation.
3793 * One interesting optimization is when several store macros appear
3794 * consecutivly that would load AT with the upper half of the same address.
3795 * The ensuing load upper instructions are ommited. This implies some kind
3796 * of global optimization. We currently only optimize within a single macro.
3797 * For many of the load and store macros if the address is specified as a
3798 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3799 * first load register 'at' with zero and use it as the base register. The
3800 * mips assembler simply uses register $zero. Just one tiny optimization
3801 * we're missing.
3803 static void
3804 macro (ip)
3805 struct mips_cl_insn *ip;
3807 register int treg, sreg, dreg, breg;
3808 int tempreg;
3809 int mask;
3810 int icnt = 0;
3811 int used_at = 0;
3812 expressionS expr1;
3813 const char *s;
3814 const char *s2;
3815 const char *fmt;
3816 int likely = 0;
3817 int dbl = 0;
3818 int coproc = 0;
3819 int lr = 0;
3820 int imm = 0;
3821 offsetT maxnum;
3822 int off;
3823 bfd_reloc_code_real_type r;
3824 int hold_mips_optimize;
3826 assert (! mips_opts.mips16);
3828 treg = (ip->insn_opcode >> 16) & 0x1f;
3829 dreg = (ip->insn_opcode >> 11) & 0x1f;
3830 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3831 mask = ip->insn_mo->mask;
3833 expr1.X_op = O_constant;
3834 expr1.X_op_symbol = NULL;
3835 expr1.X_add_symbol = NULL;
3836 expr1.X_add_number = 1;
3838 switch (mask)
3840 case M_DABS:
3841 dbl = 1;
3842 case M_ABS:
3843 /* bgez $a0,.+12
3844 move v0,$a0
3845 sub v0,$zero,$a0
3848 mips_emit_delays (true);
3849 ++mips_opts.noreorder;
3850 mips_any_noreorder = 1;
3852 expr1.X_add_number = 8;
3853 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3854 if (dreg == sreg)
3855 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3857 else
3858 move_register (&icnt, dreg, sreg);
3859 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3860 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3862 --mips_opts.noreorder;
3863 return;
3865 case M_ADD_I:
3866 s = "addi";
3867 s2 = "add";
3868 goto do_addi;
3869 case M_ADDU_I:
3870 s = "addiu";
3871 s2 = "addu";
3872 goto do_addi;
3873 case M_DADD_I:
3874 dbl = 1;
3875 s = "daddi";
3876 s2 = "dadd";
3877 goto do_addi;
3878 case M_DADDU_I:
3879 dbl = 1;
3880 s = "daddiu";
3881 s2 = "daddu";
3882 do_addi:
3883 if (imm_expr.X_op == O_constant
3884 && imm_expr.X_add_number >= -0x8000
3885 && imm_expr.X_add_number < 0x8000)
3887 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3888 (int) BFD_RELOC_LO16);
3889 return;
3891 load_register (&icnt, AT, &imm_expr, dbl);
3892 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3893 treg, sreg, AT);
3894 break;
3896 case M_AND_I:
3897 s = "andi";
3898 s2 = "and";
3899 goto do_bit;
3900 case M_OR_I:
3901 s = "ori";
3902 s2 = "or";
3903 goto do_bit;
3904 case M_NOR_I:
3905 s = "";
3906 s2 = "nor";
3907 goto do_bit;
3908 case M_XOR_I:
3909 s = "xori";
3910 s2 = "xor";
3911 do_bit:
3912 if (imm_expr.X_op == O_constant
3913 && imm_expr.X_add_number >= 0
3914 && imm_expr.X_add_number < 0x10000)
3916 if (mask != M_NOR_I)
3917 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3918 sreg, (int) BFD_RELOC_LO16);
3919 else
3921 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3922 treg, sreg, (int) BFD_RELOC_LO16);
3923 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3924 "d,v,t", treg, treg, 0);
3926 return;
3929 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3930 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3931 treg, sreg, AT);
3932 break;
3934 case M_BEQ_I:
3935 s = "beq";
3936 goto beq_i;
3937 case M_BEQL_I:
3938 s = "beql";
3939 likely = 1;
3940 goto beq_i;
3941 case M_BNE_I:
3942 s = "bne";
3943 goto beq_i;
3944 case M_BNEL_I:
3945 s = "bnel";
3946 likely = 1;
3947 beq_i:
3948 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3950 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3952 return;
3954 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3955 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3956 break;
3958 case M_BGEL:
3959 likely = 1;
3960 case M_BGE:
3961 if (treg == 0)
3963 macro_build ((char *) NULL, &icnt, &offset_expr,
3964 likely ? "bgezl" : "bgez", "s,p", sreg);
3965 return;
3967 if (sreg == 0)
3969 macro_build ((char *) NULL, &icnt, &offset_expr,
3970 likely ? "blezl" : "blez", "s,p", treg);
3971 return;
3973 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
3974 AT, sreg, treg);
3975 macro_build ((char *) NULL, &icnt, &offset_expr,
3976 likely ? "beql" : "beq", "s,t,p", AT, 0);
3977 break;
3979 case M_BGTL_I:
3980 likely = 1;
3981 case M_BGT_I:
3982 /* check for > max integer */
3983 maxnum = 0x7fffffff;
3984 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3986 maxnum <<= 16;
3987 maxnum |= 0xffff;
3988 maxnum <<= 16;
3989 maxnum |= 0xffff;
3991 if (imm_expr.X_op == O_constant
3992 && imm_expr.X_add_number >= maxnum
3993 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3995 do_false:
3996 /* result is always false */
3997 if (! likely)
3999 if (warn_nops)
4000 as_warn (_("Branch %s is always false (nop)"),
4001 ip->insn_mo->name);
4002 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4003 "", 0);
4005 else
4007 if (warn_nops)
4008 as_warn (_("Branch likely %s is always false"),
4009 ip->insn_mo->name);
4010 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4011 "s,t,p", 0, 0);
4013 return;
4015 if (imm_expr.X_op != O_constant)
4016 as_bad (_("Unsupported large constant"));
4017 ++imm_expr.X_add_number;
4018 /* FALLTHROUGH */
4019 case M_BGE_I:
4020 case M_BGEL_I:
4021 if (mask == M_BGEL_I)
4022 likely = 1;
4023 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4025 macro_build ((char *) NULL, &icnt, &offset_expr,
4026 likely ? "bgezl" : "bgez", "s,p", sreg);
4027 return;
4029 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4031 macro_build ((char *) NULL, &icnt, &offset_expr,
4032 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4033 return;
4035 maxnum = 0x7fffffff;
4036 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4038 maxnum <<= 16;
4039 maxnum |= 0xffff;
4040 maxnum <<= 16;
4041 maxnum |= 0xffff;
4043 maxnum = - maxnum - 1;
4044 if (imm_expr.X_op == O_constant
4045 && imm_expr.X_add_number <= maxnum
4046 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4048 do_true:
4049 /* result is always true */
4050 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4051 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4052 return;
4054 set_at (&icnt, sreg, 0);
4055 macro_build ((char *) NULL, &icnt, &offset_expr,
4056 likely ? "beql" : "beq", "s,t,p", AT, 0);
4057 break;
4059 case M_BGEUL:
4060 likely = 1;
4061 case M_BGEU:
4062 if (treg == 0)
4063 goto do_true;
4064 if (sreg == 0)
4066 macro_build ((char *) NULL, &icnt, &offset_expr,
4067 likely ? "beql" : "beq", "s,t,p", 0, treg);
4068 return;
4070 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4071 "d,v,t", AT, sreg, treg);
4072 macro_build ((char *) NULL, &icnt, &offset_expr,
4073 likely ? "beql" : "beq", "s,t,p", AT, 0);
4074 break;
4076 case M_BGTUL_I:
4077 likely = 1;
4078 case M_BGTU_I:
4079 if (sreg == 0
4080 || (HAVE_32BIT_GPRS
4081 && imm_expr.X_op == O_constant
4082 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4083 goto do_false;
4084 if (imm_expr.X_op != O_constant)
4085 as_bad (_("Unsupported large constant"));
4086 ++imm_expr.X_add_number;
4087 /* FALLTHROUGH */
4088 case M_BGEU_I:
4089 case M_BGEUL_I:
4090 if (mask == M_BGEUL_I)
4091 likely = 1;
4092 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4093 goto do_true;
4094 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4096 macro_build ((char *) NULL, &icnt, &offset_expr,
4097 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4098 return;
4100 set_at (&icnt, sreg, 1);
4101 macro_build ((char *) NULL, &icnt, &offset_expr,
4102 likely ? "beql" : "beq", "s,t,p", AT, 0);
4103 break;
4105 case M_BGTL:
4106 likely = 1;
4107 case M_BGT:
4108 if (treg == 0)
4110 macro_build ((char *) NULL, &icnt, &offset_expr,
4111 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4112 return;
4114 if (sreg == 0)
4116 macro_build ((char *) NULL, &icnt, &offset_expr,
4117 likely ? "bltzl" : "bltz", "s,p", treg);
4118 return;
4120 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4121 AT, treg, sreg);
4122 macro_build ((char *) NULL, &icnt, &offset_expr,
4123 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4124 break;
4126 case M_BGTUL:
4127 likely = 1;
4128 case M_BGTU:
4129 if (treg == 0)
4131 macro_build ((char *) NULL, &icnt, &offset_expr,
4132 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4133 return;
4135 if (sreg == 0)
4136 goto do_false;
4137 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4138 "d,v,t", AT, treg, sreg);
4139 macro_build ((char *) NULL, &icnt, &offset_expr,
4140 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4141 break;
4143 case M_BLEL:
4144 likely = 1;
4145 case M_BLE:
4146 if (treg == 0)
4148 macro_build ((char *) NULL, &icnt, &offset_expr,
4149 likely ? "blezl" : "blez", "s,p", sreg);
4150 return;
4152 if (sreg == 0)
4154 macro_build ((char *) NULL, &icnt, &offset_expr,
4155 likely ? "bgezl" : "bgez", "s,p", treg);
4156 return;
4158 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4159 AT, treg, sreg);
4160 macro_build ((char *) NULL, &icnt, &offset_expr,
4161 likely ? "beql" : "beq", "s,t,p", AT, 0);
4162 break;
4164 case M_BLEL_I:
4165 likely = 1;
4166 case M_BLE_I:
4167 maxnum = 0x7fffffff;
4168 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4170 maxnum <<= 16;
4171 maxnum |= 0xffff;
4172 maxnum <<= 16;
4173 maxnum |= 0xffff;
4175 if (imm_expr.X_op == O_constant
4176 && imm_expr.X_add_number >= maxnum
4177 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4178 goto do_true;
4179 if (imm_expr.X_op != O_constant)
4180 as_bad (_("Unsupported large constant"));
4181 ++imm_expr.X_add_number;
4182 /* FALLTHROUGH */
4183 case M_BLT_I:
4184 case M_BLTL_I:
4185 if (mask == M_BLTL_I)
4186 likely = 1;
4187 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4189 macro_build ((char *) NULL, &icnt, &offset_expr,
4190 likely ? "bltzl" : "bltz", "s,p", sreg);
4191 return;
4193 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4195 macro_build ((char *) NULL, &icnt, &offset_expr,
4196 likely ? "blezl" : "blez", "s,p", sreg);
4197 return;
4199 set_at (&icnt, sreg, 0);
4200 macro_build ((char *) NULL, &icnt, &offset_expr,
4201 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4202 break;
4204 case M_BLEUL:
4205 likely = 1;
4206 case M_BLEU:
4207 if (treg == 0)
4209 macro_build ((char *) NULL, &icnt, &offset_expr,
4210 likely ? "beql" : "beq", "s,t,p", sreg, 0);
4211 return;
4213 if (sreg == 0)
4214 goto do_true;
4215 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4216 "d,v,t", AT, treg, sreg);
4217 macro_build ((char *) NULL, &icnt, &offset_expr,
4218 likely ? "beql" : "beq", "s,t,p", AT, 0);
4219 break;
4221 case M_BLEUL_I:
4222 likely = 1;
4223 case M_BLEU_I:
4224 if (sreg == 0
4225 || (HAVE_32BIT_GPRS
4226 && imm_expr.X_op == O_constant
4227 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4228 goto do_true;
4229 if (imm_expr.X_op != O_constant)
4230 as_bad (_("Unsupported large constant"));
4231 ++imm_expr.X_add_number;
4232 /* FALLTHROUGH */
4233 case M_BLTU_I:
4234 case M_BLTUL_I:
4235 if (mask == M_BLTUL_I)
4236 likely = 1;
4237 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4238 goto do_false;
4239 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4241 macro_build ((char *) NULL, &icnt, &offset_expr,
4242 likely ? "beql" : "beq",
4243 "s,t,p", sreg, 0);
4244 return;
4246 set_at (&icnt, sreg, 1);
4247 macro_build ((char *) NULL, &icnt, &offset_expr,
4248 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4249 break;
4251 case M_BLTL:
4252 likely = 1;
4253 case M_BLT:
4254 if (treg == 0)
4256 macro_build ((char *) NULL, &icnt, &offset_expr,
4257 likely ? "bltzl" : "bltz", "s,p", sreg);
4258 return;
4260 if (sreg == 0)
4262 macro_build ((char *) NULL, &icnt, &offset_expr,
4263 likely ? "bgtzl" : "bgtz", "s,p", treg);
4264 return;
4266 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4267 AT, sreg, treg);
4268 macro_build ((char *) NULL, &icnt, &offset_expr,
4269 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4270 break;
4272 case M_BLTUL:
4273 likely = 1;
4274 case M_BLTU:
4275 if (treg == 0)
4276 goto do_false;
4277 if (sreg == 0)
4279 macro_build ((char *) NULL, &icnt, &offset_expr,
4280 likely ? "bnel" : "bne", "s,t,p", 0, treg);
4281 return;
4283 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4284 "d,v,t", AT, sreg,
4285 treg);
4286 macro_build ((char *) NULL, &icnt, &offset_expr,
4287 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4288 break;
4290 case M_DDIV_3:
4291 dbl = 1;
4292 case M_DIV_3:
4293 s = "mflo";
4294 goto do_div3;
4295 case M_DREM_3:
4296 dbl = 1;
4297 case M_REM_3:
4298 s = "mfhi";
4299 do_div3:
4300 if (treg == 0)
4302 as_warn (_("Divide by zero."));
4303 if (mips_trap)
4304 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4305 "s,t,q", 0, 0, 7);
4306 else
4307 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4308 "c", 7);
4309 return;
4312 mips_emit_delays (true);
4313 ++mips_opts.noreorder;
4314 mips_any_noreorder = 1;
4315 if (mips_trap)
4317 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4318 "s,t,q", treg, 0, 7);
4319 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4320 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4322 else
4324 expr1.X_add_number = 8;
4325 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4326 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4327 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4328 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4329 "c", 7);
4331 expr1.X_add_number = -1;
4332 macro_build ((char *) NULL, &icnt, &expr1,
4333 dbl ? "daddiu" : "addiu",
4334 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4335 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4336 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4337 if (dbl)
4339 expr1.X_add_number = 1;
4340 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4341 (int) BFD_RELOC_LO16);
4342 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4343 "d,w,<", AT, AT, 31);
4345 else
4347 expr1.X_add_number = 0x80000000;
4348 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4349 (int) BFD_RELOC_HI16);
4351 if (mips_trap)
4353 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4354 "s,t,q", sreg, AT, 6);
4355 /* We want to close the noreorder block as soon as possible, so
4356 that later insns are available for delay slot filling. */
4357 --mips_opts.noreorder;
4359 else
4361 expr1.X_add_number = 8;
4362 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4363 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4366 /* We want to close the noreorder block as soon as possible, so
4367 that later insns are available for delay slot filling. */
4368 --mips_opts.noreorder;
4370 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4371 "c", 6);
4373 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4374 break;
4376 case M_DIV_3I:
4377 s = "div";
4378 s2 = "mflo";
4379 goto do_divi;
4380 case M_DIVU_3I:
4381 s = "divu";
4382 s2 = "mflo";
4383 goto do_divi;
4384 case M_REM_3I:
4385 s = "div";
4386 s2 = "mfhi";
4387 goto do_divi;
4388 case M_REMU_3I:
4389 s = "divu";
4390 s2 = "mfhi";
4391 goto do_divi;
4392 case M_DDIV_3I:
4393 dbl = 1;
4394 s = "ddiv";
4395 s2 = "mflo";
4396 goto do_divi;
4397 case M_DDIVU_3I:
4398 dbl = 1;
4399 s = "ddivu";
4400 s2 = "mflo";
4401 goto do_divi;
4402 case M_DREM_3I:
4403 dbl = 1;
4404 s = "ddiv";
4405 s2 = "mfhi";
4406 goto do_divi;
4407 case M_DREMU_3I:
4408 dbl = 1;
4409 s = "ddivu";
4410 s2 = "mfhi";
4411 do_divi:
4412 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4414 as_warn (_("Divide by zero."));
4415 if (mips_trap)
4416 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4417 "s,t,q", 0, 0, 7);
4418 else
4419 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4420 "c", 7);
4421 return;
4423 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4425 if (strcmp (s2, "mflo") == 0)
4426 move_register (&icnt, dreg, sreg);
4427 else
4428 move_register (&icnt, dreg, 0);
4429 return;
4431 if (imm_expr.X_op == O_constant
4432 && imm_expr.X_add_number == -1
4433 && s[strlen (s) - 1] != 'u')
4435 if (strcmp (s2, "mflo") == 0)
4437 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4438 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4440 else
4441 move_register (&icnt, dreg, 0);
4442 return;
4445 load_register (&icnt, AT, &imm_expr, dbl);
4446 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4447 sreg, AT);
4448 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4449 break;
4451 case M_DIVU_3:
4452 s = "divu";
4453 s2 = "mflo";
4454 goto do_divu3;
4455 case M_REMU_3:
4456 s = "divu";
4457 s2 = "mfhi";
4458 goto do_divu3;
4459 case M_DDIVU_3:
4460 s = "ddivu";
4461 s2 = "mflo";
4462 goto do_divu3;
4463 case M_DREMU_3:
4464 s = "ddivu";
4465 s2 = "mfhi";
4466 do_divu3:
4467 mips_emit_delays (true);
4468 ++mips_opts.noreorder;
4469 mips_any_noreorder = 1;
4470 if (mips_trap)
4472 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4473 "s,t,q", treg, 0, 7);
4474 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4475 sreg, treg);
4476 /* We want to close the noreorder block as soon as possible, so
4477 that later insns are available for delay slot filling. */
4478 --mips_opts.noreorder;
4480 else
4482 expr1.X_add_number = 8;
4483 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4484 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4485 sreg, treg);
4487 /* We want to close the noreorder block as soon as possible, so
4488 that later insns are available for delay slot filling. */
4489 --mips_opts.noreorder;
4490 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4491 "c", 7);
4493 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4494 return;
4496 case M_DLA_AB:
4497 dbl = 1;
4498 case M_LA_AB:
4499 /* Load the address of a symbol into a register. If breg is not
4500 zero, we then add a base register to it. */
4502 if (dbl && HAVE_32BIT_GPRS)
4503 as_warn (_("dla used to load 32-bit register"));
4505 if (! dbl && HAVE_64BIT_OBJECTS)
4506 as_warn (_("la used to load 64-bit address"));
4508 if (offset_expr.X_op == O_constant
4509 && offset_expr.X_add_number >= -0x8000
4510 && offset_expr.X_add_number < 0x8000)
4512 macro_build ((char *) NULL, &icnt, &offset_expr,
4513 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4514 "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4515 return;
4518 if (treg == breg)
4520 tempreg = AT;
4521 used_at = 1;
4523 else
4525 tempreg = treg;
4526 used_at = 0;
4529 /* When generating embedded PIC code, we permit expressions of
4530 the form
4531 la $treg,foo-bar
4532 la $treg,foo-bar($breg)
4533 where bar is an address in the current section. These are used
4534 when getting the addresses of functions. We don't permit
4535 X_add_number to be non-zero, because if the symbol is
4536 external the relaxing code needs to know that any addend is
4537 purely the offset to X_op_symbol. */
4538 if (mips_pic == EMBEDDED_PIC
4539 && offset_expr.X_op == O_subtract
4540 && (symbol_constant_p (offset_expr.X_op_symbol)
4541 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4542 : (symbol_equated_p (offset_expr.X_op_symbol)
4543 && (S_GET_SEGMENT
4544 (symbol_get_value_expression (offset_expr.X_op_symbol)
4545 ->X_add_symbol)
4546 == now_seg)))
4547 && (offset_expr.X_add_number == 0
4548 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4550 if (breg == 0)
4552 tempreg = treg;
4553 used_at = 0;
4554 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4555 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4557 else
4559 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4560 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4561 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4562 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4563 "d,v,t", tempreg, tempreg, breg);
4565 macro_build ((char *) NULL, &icnt, &offset_expr,
4566 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4567 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4568 if (! used_at)
4569 return;
4570 break;
4573 if (offset_expr.X_op != O_symbol
4574 && offset_expr.X_op != O_constant)
4576 as_bad (_("expression too complex"));
4577 offset_expr.X_op = O_constant;
4580 if (offset_expr.X_op == O_constant)
4581 load_register (&icnt, tempreg, &offset_expr,
4582 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4583 ? (dbl || HAVE_64BIT_ADDRESSES)
4584 : HAVE_64BIT_ADDRESSES));
4585 else if (mips_pic == NO_PIC)
4587 /* If this is a reference to a GP relative symbol, we want
4588 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4589 Otherwise we want
4590 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4591 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4592 If we have a constant, we need two instructions anyhow,
4593 so we may as well always use the latter form.
4595 With 64bit address space and a usable $at we want
4596 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4597 lui $at,<sym> (BFD_RELOC_HI16_S)
4598 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4599 daddiu $at,<sym> (BFD_RELOC_LO16)
4600 dsll32 $tempreg,0
4601 daddu $tempreg,$tempreg,$at
4603 If $at is already in use, we use an path which is suboptimal
4604 on superscalar processors.
4605 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4606 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4607 dsll $tempreg,16
4608 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4609 dsll $tempreg,16
4610 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4612 char *p = NULL;
4613 if (HAVE_64BIT_ADDRESSES)
4615 /* We don't do GP optimization for now because RELAX_ENCODE can't
4616 hold the data for such large chunks. */
4618 if (used_at == 0 && ! mips_opts.noat)
4620 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4621 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4622 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4623 AT, (int) BFD_RELOC_HI16_S);
4624 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4625 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4626 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4627 AT, AT, (int) BFD_RELOC_LO16);
4628 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4629 "d,w,<", tempreg, tempreg, 0);
4630 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4631 "d,v,t", tempreg, tempreg, AT);
4632 used_at = 1;
4634 else
4636 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4637 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4638 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4639 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4640 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4641 tempreg, tempreg, 16);
4642 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4643 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4644 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4645 tempreg, tempreg, 16);
4646 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4647 tempreg, tempreg, (int) BFD_RELOC_LO16);
4650 else
4652 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4653 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4655 frag_grow (20);
4656 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4657 "t,r,j", tempreg, mips_gp_register,
4658 (int) BFD_RELOC_GPREL16);
4659 p = frag_var (rs_machine_dependent, 8, 0,
4660 RELAX_ENCODE (4, 8, 0, 4, 0,
4661 mips_opts.warn_about_macros),
4662 offset_expr.X_add_symbol, 0, NULL);
4664 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4665 if (p != NULL)
4666 p += 4;
4667 macro_build (p, &icnt, &offset_expr, "addiu",
4668 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4671 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4673 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4675 /* If this is a reference to an external symbol, and there
4676 is no constant, we want
4677 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4678 or if tempreg is PIC_CALL_REG
4679 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4680 For a local symbol, we want
4681 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4683 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4685 If we have a small constant, and this is a reference to
4686 an external symbol, we want
4687 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4689 addiu $tempreg,$tempreg,<constant>
4690 For a local symbol, we want the same instruction
4691 sequence, but we output a BFD_RELOC_LO16 reloc on the
4692 addiu instruction.
4694 If we have a large constant, and this is a reference to
4695 an external symbol, we want
4696 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4697 lui $at,<hiconstant>
4698 addiu $at,$at,<loconstant>
4699 addu $tempreg,$tempreg,$at
4700 For a local symbol, we want the same instruction
4701 sequence, but we output a BFD_RELOC_LO16 reloc on the
4702 addiu instruction.
4704 For NewABI, we want for local or external data addresses
4705 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4706 For a local function symbol, we want
4707 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4709 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4712 expr1.X_add_number = offset_expr.X_add_number;
4713 offset_expr.X_add_number = 0;
4714 frag_grow (32);
4715 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4716 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4717 else if (HAVE_NEWABI)
4718 lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4719 macro_build ((char *) NULL, &icnt, &offset_expr,
4720 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4721 "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4722 if (expr1.X_add_number == 0)
4724 int off;
4725 char *p;
4727 if (breg == 0)
4728 off = 0;
4729 else
4731 /* We're going to put in an addu instruction using
4732 tempreg, so we may as well insert the nop right
4733 now. */
4734 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4735 "nop", "");
4736 off = 4;
4738 p = frag_var (rs_machine_dependent, 8 - off, 0,
4739 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4740 (breg == 0
4741 ? mips_opts.warn_about_macros
4742 : 0)),
4743 offset_expr.X_add_symbol, 0, NULL);
4744 if (breg == 0)
4746 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4747 p += 4;
4749 macro_build (p, &icnt, &expr1,
4750 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4751 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4752 /* FIXME: If breg == 0, and the next instruction uses
4753 $tempreg, then if this variant case is used an extra
4754 nop will be generated. */
4756 else if (expr1.X_add_number >= -0x8000
4757 && expr1.X_add_number < 0x8000)
4759 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4760 "nop", "");
4761 macro_build ((char *) NULL, &icnt, &expr1,
4762 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4763 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4764 frag_var (rs_machine_dependent, 0, 0,
4765 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4766 offset_expr.X_add_symbol, 0, NULL);
4768 else
4770 int off1;
4772 /* If we are going to add in a base register, and the
4773 target register and the base register are the same,
4774 then we are using AT as a temporary register. Since
4775 we want to load the constant into AT, we add our
4776 current AT (from the global offset table) and the
4777 register into the register now, and pretend we were
4778 not using a base register. */
4779 if (breg != treg)
4780 off1 = 0;
4781 else
4783 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4784 "nop", "");
4785 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4786 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4787 "d,v,t", treg, AT, breg);
4788 breg = 0;
4789 tempreg = treg;
4790 off1 = -8;
4793 /* Set mips_optimize around the lui instruction to avoid
4794 inserting an unnecessary nop after the lw. */
4795 hold_mips_optimize = mips_optimize;
4796 mips_optimize = 2;
4797 macro_build_lui (NULL, &icnt, &expr1, AT);
4798 mips_optimize = hold_mips_optimize;
4800 macro_build ((char *) NULL, &icnt, &expr1,
4801 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4802 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4803 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4804 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4805 "d,v,t", tempreg, tempreg, AT);
4806 frag_var (rs_machine_dependent, 0, 0,
4807 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4808 offset_expr.X_add_symbol, 0, NULL);
4809 used_at = 1;
4812 else if (mips_pic == SVR4_PIC)
4814 int gpdel;
4815 char *p;
4816 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4817 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4818 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4820 /* This is the large GOT case. If this is a reference to an
4821 external symbol, and there is no constant, we want
4822 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4823 addu $tempreg,$tempreg,$gp
4824 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4825 or if tempreg is PIC_CALL_REG
4826 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4827 addu $tempreg,$tempreg,$gp
4828 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4829 For a local symbol, we want
4830 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4832 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4834 If we have a small constant, and this is a reference to
4835 an external symbol, we want
4836 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4837 addu $tempreg,$tempreg,$gp
4838 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4840 addiu $tempreg,$tempreg,<constant>
4841 For a local symbol, we want
4842 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4844 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4846 If we have a large constant, and this is a reference to
4847 an external symbol, we want
4848 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4849 addu $tempreg,$tempreg,$gp
4850 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4851 lui $at,<hiconstant>
4852 addiu $at,$at,<loconstant>
4853 addu $tempreg,$tempreg,$at
4854 For a local symbol, we want
4855 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4856 lui $at,<hiconstant>
4857 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4858 addu $tempreg,$tempreg,$at
4860 For NewABI, we want for local data addresses
4861 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4864 expr1.X_add_number = offset_expr.X_add_number;
4865 offset_expr.X_add_number = 0;
4866 frag_grow (52);
4867 if (reg_needs_delay (mips_gp_register))
4868 gpdel = 4;
4869 else
4870 gpdel = 0;
4871 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4873 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4874 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4876 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4877 tempreg, lui_reloc_type);
4878 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4879 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4880 "d,v,t", tempreg, tempreg, mips_gp_register);
4881 macro_build ((char *) NULL, &icnt, &offset_expr,
4882 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4883 "t,o(b)", tempreg, lw_reloc_type, tempreg);
4884 if (expr1.X_add_number == 0)
4886 int off;
4888 if (breg == 0)
4889 off = 0;
4890 else
4892 /* We're going to put in an addu instruction using
4893 tempreg, so we may as well insert the nop right
4894 now. */
4895 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4896 "nop", "");
4897 off = 4;
4900 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4901 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4902 8 + gpdel, 0,
4903 (breg == 0
4904 ? mips_opts.warn_about_macros
4905 : 0)),
4906 offset_expr.X_add_symbol, 0, NULL);
4908 else if (expr1.X_add_number >= -0x8000
4909 && expr1.X_add_number < 0x8000)
4911 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4912 "nop", "");
4913 macro_build ((char *) NULL, &icnt, &expr1,
4914 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4915 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4917 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4918 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4919 (breg == 0
4920 ? mips_opts.warn_about_macros
4921 : 0)),
4922 offset_expr.X_add_symbol, 0, NULL);
4924 else
4926 int adj, dreg;
4928 /* If we are going to add in a base register, and the
4929 target register and the base register are the same,
4930 then we are using AT as a temporary register. Since
4931 we want to load the constant into AT, we add our
4932 current AT (from the global offset table) and the
4933 register into the register now, and pretend we were
4934 not using a base register. */
4935 if (breg != treg)
4937 adj = 0;
4938 dreg = tempreg;
4940 else
4942 assert (tempreg == AT);
4943 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4944 "nop", "");
4945 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4946 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4947 "d,v,t", treg, AT, breg);
4948 dreg = treg;
4949 adj = 8;
4952 /* Set mips_optimize around the lui instruction to avoid
4953 inserting an unnecessary nop after the lw. */
4954 hold_mips_optimize = mips_optimize;
4955 mips_optimize = 2;
4956 macro_build_lui (NULL, &icnt, &expr1, AT);
4957 mips_optimize = hold_mips_optimize;
4959 macro_build ((char *) NULL, &icnt, &expr1,
4960 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4961 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4962 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4963 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4964 "d,v,t", dreg, dreg, AT);
4966 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4967 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4968 8 + gpdel, 0,
4969 (breg == 0
4970 ? mips_opts.warn_about_macros
4971 : 0)),
4972 offset_expr.X_add_symbol, 0, NULL);
4974 used_at = 1;
4977 if (gpdel > 0)
4979 /* This is needed because this instruction uses $gp, but
4980 the first instruction on the main stream does not. */
4981 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4982 p += 4;
4985 if (HAVE_NEWABI)
4986 local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4987 macro_build (p, &icnt, &offset_expr,
4988 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4989 "t,o(b)", tempreg,
4990 local_reloc_type,
4991 mips_gp_register);
4992 p += 4;
4993 if (expr1.X_add_number == 0 && HAVE_NEWABI)
4995 /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
4997 else
4998 if (expr1.X_add_number >= -0x8000
4999 && expr1.X_add_number < 0x8000)
5001 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5002 p += 4;
5003 macro_build (p, &icnt, &expr1,
5004 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5005 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5006 /* FIXME: If add_number is 0, and there was no base
5007 register, the external symbol case ended with a load,
5008 so if the symbol turns out to not be external, and
5009 the next instruction uses tempreg, an unnecessary nop
5010 will be inserted. */
5012 else
5014 if (breg == treg)
5016 /* We must add in the base register now, as in the
5017 external symbol case. */
5018 assert (tempreg == AT);
5019 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5020 p += 4;
5021 macro_build (p, &icnt, (expressionS *) NULL,
5022 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5023 "d,v,t", treg, AT, breg);
5024 p += 4;
5025 tempreg = treg;
5026 /* We set breg to 0 because we have arranged to add
5027 it in in both cases. */
5028 breg = 0;
5031 macro_build_lui (p, &icnt, &expr1, AT);
5032 p += 4;
5033 macro_build (p, &icnt, &expr1,
5034 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5035 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5036 p += 4;
5037 macro_build (p, &icnt, (expressionS *) NULL,
5038 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5039 "d,v,t", tempreg, tempreg, AT);
5040 p += 4;
5043 else if (mips_pic == EMBEDDED_PIC)
5045 /* We use
5046 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
5048 macro_build ((char *) NULL, &icnt, &offset_expr,
5049 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5050 tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5052 else
5053 abort ();
5055 if (breg != 0)
5057 char *s;
5059 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5060 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5061 else
5062 s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5064 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5065 "d,v,t", treg, tempreg, breg);
5068 if (! used_at)
5069 return;
5071 break;
5073 case M_J_A:
5074 /* The j instruction may not be used in PIC code, since it
5075 requires an absolute address. We convert it to a b
5076 instruction. */
5077 if (mips_pic == NO_PIC)
5078 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5079 else
5080 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5081 return;
5083 /* The jal instructions must be handled as macros because when
5084 generating PIC code they expand to multi-instruction
5085 sequences. Normally they are simple instructions. */
5086 case M_JAL_1:
5087 dreg = RA;
5088 /* Fall through. */
5089 case M_JAL_2:
5090 if (mips_pic == NO_PIC
5091 || mips_pic == EMBEDDED_PIC)
5092 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5093 "d,s", dreg, sreg);
5094 else if (mips_pic == SVR4_PIC)
5096 if (sreg != PIC_CALL_REG)
5097 as_warn (_("MIPS PIC call to register other than $25"));
5099 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5100 "d,s", dreg, sreg);
5101 if (! HAVE_NEWABI)
5103 if (mips_cprestore_offset < 0)
5104 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5105 else
5107 if (! mips_frame_reg_valid)
5109 as_warn (_("No .frame pseudo-op used in PIC code"));
5110 /* Quiet this warning. */
5111 mips_frame_reg_valid = 1;
5113 if (! mips_cprestore_valid)
5115 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5116 /* Quiet this warning. */
5117 mips_cprestore_valid = 1;
5119 expr1.X_add_number = mips_cprestore_offset;
5120 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5121 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5122 mips_gp_register, mips_frame_reg);
5126 else
5127 abort ();
5129 return;
5131 case M_JAL_A:
5132 if (mips_pic == NO_PIC)
5133 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5134 else if (mips_pic == SVR4_PIC)
5136 char *p;
5138 /* If this is a reference to an external symbol, and we are
5139 using a small GOT, we want
5140 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5142 jalr $ra,$25
5144 lw $gp,cprestore($sp)
5145 The cprestore value is set using the .cprestore
5146 pseudo-op. If we are using a big GOT, we want
5147 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5148 addu $25,$25,$gp
5149 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5151 jalr $ra,$25
5153 lw $gp,cprestore($sp)
5154 If the symbol is not external, we want
5155 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5157 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5158 jalr $ra,$25
5160 lw $gp,cprestore($sp)
5161 For NewABI, we want
5162 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5163 jalr $ra,$25 (BFD_RELOC_MIPS_JALR)
5165 if (HAVE_NEWABI)
5167 macro_build ((char *) NULL, &icnt, &offset_expr,
5168 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5169 "t,o(b)", PIC_CALL_REG,
5170 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5171 macro_build_jalr (icnt, &offset_expr);
5173 else
5175 frag_grow (40);
5176 if (! mips_big_got)
5178 macro_build ((char *) NULL, &icnt, &offset_expr,
5179 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5180 "t,o(b)", PIC_CALL_REG,
5181 (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5182 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5183 "nop", "");
5184 p = frag_var (rs_machine_dependent, 4, 0,
5185 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5186 offset_expr.X_add_symbol, 0, NULL);
5188 else
5190 int gpdel;
5192 if (reg_needs_delay (mips_gp_register))
5193 gpdel = 4;
5194 else
5195 gpdel = 0;
5196 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5197 "t,u", PIC_CALL_REG,
5198 (int) BFD_RELOC_MIPS_CALL_HI16);
5199 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5200 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5201 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5202 mips_gp_register);
5203 macro_build ((char *) NULL, &icnt, &offset_expr,
5204 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5205 "t,o(b)", PIC_CALL_REG,
5206 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5207 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5208 "nop", "");
5209 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5210 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5211 8 + gpdel, 0, 0),
5212 offset_expr.X_add_symbol, 0, NULL);
5213 if (gpdel > 0)
5215 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5216 p += 4;
5218 macro_build (p, &icnt, &offset_expr,
5219 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5220 "t,o(b)", PIC_CALL_REG,
5221 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5222 p += 4;
5223 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5224 p += 4;
5226 macro_build (p, &icnt, &offset_expr,
5227 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5228 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5229 (int) BFD_RELOC_LO16);
5230 macro_build_jalr (icnt, &offset_expr);
5232 if (mips_cprestore_offset < 0)
5233 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5234 else
5236 if (! mips_frame_reg_valid)
5238 as_warn (_("No .frame pseudo-op used in PIC code"));
5239 /* Quiet this warning. */
5240 mips_frame_reg_valid = 1;
5242 if (! mips_cprestore_valid)
5244 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5245 /* Quiet this warning. */
5246 mips_cprestore_valid = 1;
5248 if (mips_opts.noreorder)
5249 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5250 "nop", "");
5251 expr1.X_add_number = mips_cprestore_offset;
5252 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5253 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5254 mips_gp_register, mips_frame_reg);
5258 else if (mips_pic == EMBEDDED_PIC)
5260 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5261 /* The linker may expand the call to a longer sequence which
5262 uses $at, so we must break rather than return. */
5263 break;
5265 else
5266 abort ();
5268 return;
5270 case M_LB_AB:
5271 s = "lb";
5272 goto ld;
5273 case M_LBU_AB:
5274 s = "lbu";
5275 goto ld;
5276 case M_LH_AB:
5277 s = "lh";
5278 goto ld;
5279 case M_LHU_AB:
5280 s = "lhu";
5281 goto ld;
5282 case M_LW_AB:
5283 s = "lw";
5284 goto ld;
5285 case M_LWC0_AB:
5286 s = "lwc0";
5287 /* Itbl support may require additional care here. */
5288 coproc = 1;
5289 goto ld;
5290 case M_LWC1_AB:
5291 s = "lwc1";
5292 /* Itbl support may require additional care here. */
5293 coproc = 1;
5294 goto ld;
5295 case M_LWC2_AB:
5296 s = "lwc2";
5297 /* Itbl support may require additional care here. */
5298 coproc = 1;
5299 goto ld;
5300 case M_LWC3_AB:
5301 s = "lwc3";
5302 /* Itbl support may require additional care here. */
5303 coproc = 1;
5304 goto ld;
5305 case M_LWL_AB:
5306 s = "lwl";
5307 lr = 1;
5308 goto ld;
5309 case M_LWR_AB:
5310 s = "lwr";
5311 lr = 1;
5312 goto ld;
5313 case M_LDC1_AB:
5314 if (mips_arch == CPU_R4650)
5316 as_bad (_("opcode not supported on this processor"));
5317 return;
5319 s = "ldc1";
5320 /* Itbl support may require additional care here. */
5321 coproc = 1;
5322 goto ld;
5323 case M_LDC2_AB:
5324 s = "ldc2";
5325 /* Itbl support may require additional care here. */
5326 coproc = 1;
5327 goto ld;
5328 case M_LDC3_AB:
5329 s = "ldc3";
5330 /* Itbl support may require additional care here. */
5331 coproc = 1;
5332 goto ld;
5333 case M_LDL_AB:
5334 s = "ldl";
5335 lr = 1;
5336 goto ld;
5337 case M_LDR_AB:
5338 s = "ldr";
5339 lr = 1;
5340 goto ld;
5341 case M_LL_AB:
5342 s = "ll";
5343 goto ld;
5344 case M_LLD_AB:
5345 s = "lld";
5346 goto ld;
5347 case M_LWU_AB:
5348 s = "lwu";
5350 if (breg == treg || coproc || lr)
5352 tempreg = AT;
5353 used_at = 1;
5355 else
5357 tempreg = treg;
5358 used_at = 0;
5360 goto ld_st;
5361 case M_SB_AB:
5362 s = "sb";
5363 goto st;
5364 case M_SH_AB:
5365 s = "sh";
5366 goto st;
5367 case M_SW_AB:
5368 s = "sw";
5369 goto st;
5370 case M_SWC0_AB:
5371 s = "swc0";
5372 /* Itbl support may require additional care here. */
5373 coproc = 1;
5374 goto st;
5375 case M_SWC1_AB:
5376 s = "swc1";
5377 /* Itbl support may require additional care here. */
5378 coproc = 1;
5379 goto st;
5380 case M_SWC2_AB:
5381 s = "swc2";
5382 /* Itbl support may require additional care here. */
5383 coproc = 1;
5384 goto st;
5385 case M_SWC3_AB:
5386 s = "swc3";
5387 /* Itbl support may require additional care here. */
5388 coproc = 1;
5389 goto st;
5390 case M_SWL_AB:
5391 s = "swl";
5392 goto st;
5393 case M_SWR_AB:
5394 s = "swr";
5395 goto st;
5396 case M_SC_AB:
5397 s = "sc";
5398 goto st;
5399 case M_SCD_AB:
5400 s = "scd";
5401 goto st;
5402 case M_SDC1_AB:
5403 if (mips_arch == CPU_R4650)
5405 as_bad (_("opcode not supported on this processor"));
5406 return;
5408 s = "sdc1";
5409 coproc = 1;
5410 /* Itbl support may require additional care here. */
5411 goto st;
5412 case M_SDC2_AB:
5413 s = "sdc2";
5414 /* Itbl support may require additional care here. */
5415 coproc = 1;
5416 goto st;
5417 case M_SDC3_AB:
5418 s = "sdc3";
5419 /* Itbl support may require additional care here. */
5420 coproc = 1;
5421 goto st;
5422 case M_SDL_AB:
5423 s = "sdl";
5424 goto st;
5425 case M_SDR_AB:
5426 s = "sdr";
5428 tempreg = AT;
5429 used_at = 1;
5430 ld_st:
5431 /* Itbl support may require additional care here. */
5432 if (mask == M_LWC1_AB
5433 || mask == M_SWC1_AB
5434 || mask == M_LDC1_AB
5435 || mask == M_SDC1_AB
5436 || mask == M_L_DAB
5437 || mask == M_S_DAB)
5438 fmt = "T,o(b)";
5439 else if (coproc)
5440 fmt = "E,o(b)";
5441 else
5442 fmt = "t,o(b)";
5444 /* For embedded PIC, we allow loads where the offset is calculated
5445 by subtracting a symbol in the current segment from an unknown
5446 symbol, relative to a base register, e.g.:
5447 <op> $treg, <sym>-<localsym>($breg)
5448 This is used by the compiler for switch statements. */
5449 if (mips_pic == EMBEDDED_PIC
5450 && offset_expr.X_op == O_subtract
5451 && (symbol_constant_p (offset_expr.X_op_symbol)
5452 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5453 : (symbol_equated_p (offset_expr.X_op_symbol)
5454 && (S_GET_SEGMENT
5455 (symbol_get_value_expression (offset_expr.X_op_symbol)
5456 ->X_add_symbol)
5457 == now_seg)))
5458 && breg != 0
5459 && (offset_expr.X_add_number == 0
5460 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5462 /* For this case, we output the instructions:
5463 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5464 addiu $tempreg,$tempreg,$breg
5465 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5466 If the relocation would fit entirely in 16 bits, it would be
5467 nice to emit:
5468 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5469 instead, but that seems quite difficult. */
5470 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5471 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5472 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5473 ((bfd_arch_bits_per_address (stdoutput) == 32
5474 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5475 ? "addu" : "daddu"),
5476 "d,v,t", tempreg, tempreg, breg);
5477 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5478 (int) BFD_RELOC_PCREL_LO16, tempreg);
5479 if (! used_at)
5480 return;
5481 break;
5484 if (offset_expr.X_op != O_constant
5485 && offset_expr.X_op != O_symbol)
5487 as_bad (_("expression too complex"));
5488 offset_expr.X_op = O_constant;
5491 /* A constant expression in PIC code can be handled just as it
5492 is in non PIC code. */
5493 if (mips_pic == NO_PIC
5494 || offset_expr.X_op == O_constant)
5496 char *p;
5498 /* If this is a reference to a GP relative symbol, and there
5499 is no base register, we want
5500 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5501 Otherwise, if there is no base register, we want
5502 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5503 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5504 If we have a constant, we need two instructions anyhow,
5505 so we always use the latter form.
5507 If we have a base register, and this is a reference to a
5508 GP relative symbol, we want
5509 addu $tempreg,$breg,$gp
5510 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5511 Otherwise we want
5512 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5513 addu $tempreg,$tempreg,$breg
5514 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5515 With a constant we always use the latter case.
5517 With 64bit address space and no base register and $at usable,
5518 we want
5519 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5520 lui $at,<sym> (BFD_RELOC_HI16_S)
5521 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5522 dsll32 $tempreg,0
5523 daddu $tempreg,$at
5524 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5525 If we have a base register, we want
5526 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5527 lui $at,<sym> (BFD_RELOC_HI16_S)
5528 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5529 daddu $at,$breg
5530 dsll32 $tempreg,0
5531 daddu $tempreg,$at
5532 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5534 Without $at we can't generate the optimal path for superscalar
5535 processors here since this would require two temporary registers.
5536 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5537 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5538 dsll $tempreg,16
5539 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5540 dsll $tempreg,16
5541 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5542 If we have a base register, we want
5543 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5544 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5545 dsll $tempreg,16
5546 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5547 dsll $tempreg,16
5548 daddu $tempreg,$tempreg,$breg
5549 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5551 If we have 64-bit addresses, as an optimization, for
5552 addresses which are 32-bit constants (e.g. kseg0/kseg1
5553 addresses) we fall back to the 32-bit address generation
5554 mechanism since it is more efficient. Note that due to
5555 the signed offset used by memory operations, the 32-bit
5556 range is shifted down by 32768 here. This code should
5557 probably attempt to generate 64-bit constants more
5558 efficiently in general.
5560 if (HAVE_64BIT_ADDRESSES
5561 && !(offset_expr.X_op == O_constant
5562 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5564 p = NULL;
5566 /* We don't do GP optimization for now because RELAX_ENCODE can't
5567 hold the data for such large chunks. */
5569 if (used_at == 0 && ! mips_opts.noat)
5571 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5572 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5573 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5574 AT, (int) BFD_RELOC_HI16_S);
5575 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5576 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5577 if (breg != 0)
5578 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5579 "d,v,t", AT, AT, breg);
5580 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5581 "d,w,<", tempreg, tempreg, 0);
5582 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5583 "d,v,t", tempreg, tempreg, AT);
5584 macro_build (p, &icnt, &offset_expr, s,
5585 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5586 used_at = 1;
5588 else
5590 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5591 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5592 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5593 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5594 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5595 "d,w,<", tempreg, tempreg, 16);
5596 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5597 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5598 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5599 "d,w,<", tempreg, tempreg, 16);
5600 if (breg != 0)
5601 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5602 "d,v,t", tempreg, tempreg, breg);
5603 macro_build (p, &icnt, &offset_expr, s,
5604 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5607 return;
5610 if (breg == 0)
5612 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5613 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5614 p = NULL;
5615 else
5617 frag_grow (20);
5618 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5619 treg, (int) BFD_RELOC_GPREL16,
5620 mips_gp_register);
5621 p = frag_var (rs_machine_dependent, 8, 0,
5622 RELAX_ENCODE (4, 8, 0, 4, 0,
5623 (mips_opts.warn_about_macros
5624 || (used_at
5625 && mips_opts.noat))),
5626 offset_expr.X_add_symbol, 0, NULL);
5627 used_at = 0;
5629 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5630 if (p != NULL)
5631 p += 4;
5632 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5633 (int) BFD_RELOC_LO16, tempreg);
5635 else
5637 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5638 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5639 p = NULL;
5640 else
5642 frag_grow (28);
5643 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5644 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5645 "d,v,t", tempreg, breg, mips_gp_register);
5646 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5647 treg, (int) BFD_RELOC_GPREL16, tempreg);
5648 p = frag_var (rs_machine_dependent, 12, 0,
5649 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5650 offset_expr.X_add_symbol, 0, NULL);
5652 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5653 if (p != NULL)
5654 p += 4;
5655 macro_build (p, &icnt, (expressionS *) NULL,
5656 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5657 "d,v,t", tempreg, tempreg, breg);
5658 if (p != NULL)
5659 p += 4;
5660 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5661 (int) BFD_RELOC_LO16, tempreg);
5664 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5666 char *p;
5667 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5669 /* If this is a reference to an external symbol, we want
5670 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5672 <op> $treg,0($tempreg)
5673 Otherwise we want
5674 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5676 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5677 <op> $treg,0($tempreg)
5678 If we have NewABI, we want
5679 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5680 If there is a base register, we add it to $tempreg before
5681 the <op>. If there is a constant, we stick it in the
5682 <op> instruction. We don't handle constants larger than
5683 16 bits, because we have no way to load the upper 16 bits
5684 (actually, we could handle them for the subset of cases
5685 in which we are not using $at). */
5686 assert (offset_expr.X_op == O_symbol);
5687 expr1.X_add_number = offset_expr.X_add_number;
5688 offset_expr.X_add_number = 0;
5689 if (HAVE_NEWABI)
5690 lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5691 if (expr1.X_add_number < -0x8000
5692 || expr1.X_add_number >= 0x8000)
5693 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5694 frag_grow (20);
5695 macro_build ((char *) NULL, &icnt, &offset_expr,
5696 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5697 (int) lw_reloc_type, mips_gp_register);
5698 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5699 p = frag_var (rs_machine_dependent, 4, 0,
5700 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5701 offset_expr.X_add_symbol, 0, NULL);
5702 macro_build (p, &icnt, &offset_expr,
5703 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5704 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5705 if (breg != 0)
5706 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5707 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5708 "d,v,t", tempreg, tempreg, breg);
5709 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5710 (int) BFD_RELOC_LO16, tempreg);
5712 else if (mips_pic == SVR4_PIC)
5714 int gpdel;
5715 char *p;
5717 /* If this is a reference to an external symbol, we want
5718 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5719 addu $tempreg,$tempreg,$gp
5720 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5721 <op> $treg,0($tempreg)
5722 Otherwise we want
5723 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5725 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5726 <op> $treg,0($tempreg)
5727 If there is a base register, we add it to $tempreg before
5728 the <op>. If there is a constant, we stick it in the
5729 <op> instruction. We don't handle constants larger than
5730 16 bits, because we have no way to load the upper 16 bits
5731 (actually, we could handle them for the subset of cases
5732 in which we are not using $at).
5734 For NewABI, we want
5735 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5736 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5737 <op> $treg,0($tempreg)
5739 assert (offset_expr.X_op == O_symbol);
5740 expr1.X_add_number = offset_expr.X_add_number;
5741 offset_expr.X_add_number = 0;
5742 if (expr1.X_add_number < -0x8000
5743 || expr1.X_add_number >= 0x8000)
5744 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5745 if (HAVE_NEWABI)
5747 macro_build ((char *) NULL, &icnt, &offset_expr,
5748 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5749 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5750 mips_gp_register);
5751 macro_build ((char *) NULL, &icnt, &offset_expr,
5752 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5753 "t,r,j", tempreg, tempreg,
5754 BFD_RELOC_MIPS_GOT_OFST);
5755 if (breg != 0)
5756 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5757 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5758 "d,v,t", tempreg, tempreg, breg);
5759 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5760 (int) BFD_RELOC_LO16, tempreg);
5762 if (! used_at)
5763 return;
5765 break;
5767 if (reg_needs_delay (mips_gp_register))
5768 gpdel = 4;
5769 else
5770 gpdel = 0;
5771 frag_grow (36);
5772 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5773 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5774 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5775 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5776 "d,v,t", tempreg, tempreg, mips_gp_register);
5777 macro_build ((char *) NULL, &icnt, &offset_expr,
5778 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5779 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5780 tempreg);
5781 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5782 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5783 offset_expr.X_add_symbol, 0, NULL);
5784 if (gpdel > 0)
5786 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5787 p += 4;
5789 macro_build (p, &icnt, &offset_expr,
5790 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5791 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5792 mips_gp_register);
5793 p += 4;
5794 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5795 p += 4;
5796 macro_build (p, &icnt, &offset_expr,
5797 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5798 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5799 if (breg != 0)
5800 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5801 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5802 "d,v,t", tempreg, tempreg, breg);
5803 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5804 (int) BFD_RELOC_LO16, tempreg);
5806 else if (mips_pic == EMBEDDED_PIC)
5808 /* If there is no base register, we want
5809 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5810 If there is a base register, we want
5811 addu $tempreg,$breg,$gp
5812 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5814 assert (offset_expr.X_op == O_symbol);
5815 if (breg == 0)
5817 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5818 treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
5819 used_at = 0;
5821 else
5823 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5824 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5825 "d,v,t", tempreg, breg, mips_gp_register);
5826 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5827 treg, (int) BFD_RELOC_GPREL16, tempreg);
5830 else
5831 abort ();
5833 if (! used_at)
5834 return;
5836 break;
5838 case M_LI:
5839 case M_LI_S:
5840 load_register (&icnt, treg, &imm_expr, 0);
5841 return;
5843 case M_DLI:
5844 load_register (&icnt, treg, &imm_expr, 1);
5845 return;
5847 case M_LI_SS:
5848 if (imm_expr.X_op == O_constant)
5850 load_register (&icnt, AT, &imm_expr, 0);
5851 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5852 "mtc1", "t,G", AT, treg);
5853 break;
5855 else
5857 assert (offset_expr.X_op == O_symbol
5858 && strcmp (segment_name (S_GET_SEGMENT
5859 (offset_expr.X_add_symbol)),
5860 ".lit4") == 0
5861 && offset_expr.X_add_number == 0);
5862 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5863 treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
5864 return;
5867 case M_LI_D:
5868 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5869 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5870 order 32 bits of the value and the low order 32 bits are either
5871 zero or in OFFSET_EXPR. */
5872 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5874 if (HAVE_64BIT_GPRS)
5875 load_register (&icnt, treg, &imm_expr, 1);
5876 else
5878 int hreg, lreg;
5880 if (target_big_endian)
5882 hreg = treg;
5883 lreg = treg + 1;
5885 else
5887 hreg = treg + 1;
5888 lreg = treg;
5891 if (hreg <= 31)
5892 load_register (&icnt, hreg, &imm_expr, 0);
5893 if (lreg <= 31)
5895 if (offset_expr.X_op == O_absent)
5896 move_register (&icnt, lreg, 0);
5897 else
5899 assert (offset_expr.X_op == O_constant);
5900 load_register (&icnt, lreg, &offset_expr, 0);
5904 return;
5907 /* We know that sym is in the .rdata section. First we get the
5908 upper 16 bits of the address. */
5909 if (mips_pic == NO_PIC)
5911 macro_build_lui (NULL, &icnt, &offset_expr, AT);
5913 else if (mips_pic == SVR4_PIC)
5915 macro_build ((char *) NULL, &icnt, &offset_expr,
5916 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5917 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5918 mips_gp_register);
5920 else if (mips_pic == EMBEDDED_PIC)
5922 /* For embedded PIC we pick up the entire address off $gp in
5923 a single instruction. */
5924 macro_build ((char *) NULL, &icnt, &offset_expr,
5925 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
5926 mips_gp_register, (int) BFD_RELOC_GPREL16);
5927 offset_expr.X_op = O_constant;
5928 offset_expr.X_add_number = 0;
5930 else
5931 abort ();
5933 /* Now we load the register(s). */
5934 if (HAVE_64BIT_GPRS)
5935 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5936 treg, (int) BFD_RELOC_LO16, AT);
5937 else
5939 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5940 treg, (int) BFD_RELOC_LO16, AT);
5941 if (treg != RA)
5943 /* FIXME: How in the world do we deal with the possible
5944 overflow here? */
5945 offset_expr.X_add_number += 4;
5946 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5947 treg + 1, (int) BFD_RELOC_LO16, AT);
5951 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5952 does not become a variant frag. */
5953 frag_wane (frag_now);
5954 frag_new (0);
5956 break;
5958 case M_LI_DD:
5959 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5960 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5961 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5962 the value and the low order 32 bits are either zero or in
5963 OFFSET_EXPR. */
5964 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5966 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5967 if (HAVE_64BIT_FPRS)
5969 assert (HAVE_64BIT_GPRS);
5970 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5971 "dmtc1", "t,S", AT, treg);
5973 else
5975 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5976 "mtc1", "t,G", AT, treg + 1);
5977 if (offset_expr.X_op == O_absent)
5978 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5979 "mtc1", "t,G", 0, treg);
5980 else
5982 assert (offset_expr.X_op == O_constant);
5983 load_register (&icnt, AT, &offset_expr, 0);
5984 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5985 "mtc1", "t,G", AT, treg);
5988 break;
5991 assert (offset_expr.X_op == O_symbol
5992 && offset_expr.X_add_number == 0);
5993 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5994 if (strcmp (s, ".lit8") == 0)
5996 if (mips_opts.isa != ISA_MIPS1)
5998 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5999 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6000 mips_gp_register);
6001 return;
6003 breg = mips_gp_register;
6004 r = BFD_RELOC_MIPS_LITERAL;
6005 goto dob;
6007 else
6009 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6010 if (mips_pic == SVR4_PIC)
6011 macro_build ((char *) NULL, &icnt, &offset_expr,
6012 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6013 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6014 mips_gp_register);
6015 else
6017 /* FIXME: This won't work for a 64 bit address. */
6018 macro_build_lui (NULL, &icnt, &offset_expr, AT);
6021 if (mips_opts.isa != ISA_MIPS1)
6023 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6024 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6026 /* To avoid confusion in tc_gen_reloc, we must ensure
6027 that this does not become a variant frag. */
6028 frag_wane (frag_now);
6029 frag_new (0);
6031 break;
6033 breg = AT;
6034 r = BFD_RELOC_LO16;
6035 goto dob;
6038 case M_L_DOB:
6039 if (mips_arch == CPU_R4650)
6041 as_bad (_("opcode not supported on this processor"));
6042 return;
6044 /* Even on a big endian machine $fn comes before $fn+1. We have
6045 to adjust when loading from memory. */
6046 r = BFD_RELOC_LO16;
6047 dob:
6048 assert (mips_opts.isa == ISA_MIPS1);
6049 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6050 target_big_endian ? treg + 1 : treg,
6051 (int) r, breg);
6052 /* FIXME: A possible overflow which I don't know how to deal
6053 with. */
6054 offset_expr.X_add_number += 4;
6055 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6056 target_big_endian ? treg : treg + 1,
6057 (int) r, breg);
6059 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6060 does not become a variant frag. */
6061 frag_wane (frag_now);
6062 frag_new (0);
6064 if (breg != AT)
6065 return;
6066 break;
6068 case M_L_DAB:
6070 * The MIPS assembler seems to check for X_add_number not
6071 * being double aligned and generating:
6072 * lui at,%hi(foo+1)
6073 * addu at,at,v1
6074 * addiu at,at,%lo(foo+1)
6075 * lwc1 f2,0(at)
6076 * lwc1 f3,4(at)
6077 * But, the resulting address is the same after relocation so why
6078 * generate the extra instruction?
6080 if (mips_arch == CPU_R4650)
6082 as_bad (_("opcode not supported on this processor"));
6083 return;
6085 /* Itbl support may require additional care here. */
6086 coproc = 1;
6087 if (mips_opts.isa != ISA_MIPS1)
6089 s = "ldc1";
6090 goto ld;
6093 s = "lwc1";
6094 fmt = "T,o(b)";
6095 goto ldd_std;
6097 case M_S_DAB:
6098 if (mips_arch == CPU_R4650)
6100 as_bad (_("opcode not supported on this processor"));
6101 return;
6104 if (mips_opts.isa != ISA_MIPS1)
6106 s = "sdc1";
6107 goto st;
6110 s = "swc1";
6111 fmt = "T,o(b)";
6112 /* Itbl support may require additional care here. */
6113 coproc = 1;
6114 goto ldd_std;
6116 case M_LD_AB:
6117 if (HAVE_64BIT_GPRS)
6119 s = "ld";
6120 goto ld;
6123 s = "lw";
6124 fmt = "t,o(b)";
6125 goto ldd_std;
6127 case M_SD_AB:
6128 if (HAVE_64BIT_GPRS)
6130 s = "sd";
6131 goto st;
6134 s = "sw";
6135 fmt = "t,o(b)";
6137 ldd_std:
6138 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6139 loads for the case of doing a pair of loads to simulate an 'ld'.
6140 This is not currently done by the compiler, and assembly coders
6141 writing embedded-pic code can cope. */
6143 if (offset_expr.X_op != O_symbol
6144 && offset_expr.X_op != O_constant)
6146 as_bad (_("expression too complex"));
6147 offset_expr.X_op = O_constant;
6150 /* Even on a big endian machine $fn comes before $fn+1. We have
6151 to adjust when loading from memory. We set coproc if we must
6152 load $fn+1 first. */
6153 /* Itbl support may require additional care here. */
6154 if (! target_big_endian)
6155 coproc = 0;
6157 if (mips_pic == NO_PIC
6158 || offset_expr.X_op == O_constant)
6160 char *p;
6162 /* If this is a reference to a GP relative symbol, we want
6163 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6164 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6165 If we have a base register, we use this
6166 addu $at,$breg,$gp
6167 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6168 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6169 If this is not a GP relative symbol, we want
6170 lui $at,<sym> (BFD_RELOC_HI16_S)
6171 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6172 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6173 If there is a base register, we add it to $at after the
6174 lui instruction. If there is a constant, we always use
6175 the last case. */
6176 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6177 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6179 p = NULL;
6180 used_at = 1;
6182 else
6184 int off;
6186 if (breg == 0)
6188 frag_grow (28);
6189 tempreg = mips_gp_register;
6190 off = 0;
6191 used_at = 0;
6193 else
6195 frag_grow (36);
6196 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6197 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6198 "d,v,t", AT, breg, mips_gp_register);
6199 tempreg = AT;
6200 off = 4;
6201 used_at = 1;
6204 /* Itbl support may require additional care here. */
6205 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6206 coproc ? treg + 1 : treg,
6207 (int) BFD_RELOC_GPREL16, tempreg);
6208 offset_expr.X_add_number += 4;
6210 /* Set mips_optimize to 2 to avoid inserting an
6211 undesired nop. */
6212 hold_mips_optimize = mips_optimize;
6213 mips_optimize = 2;
6214 /* Itbl support may require additional care here. */
6215 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6216 coproc ? treg : treg + 1,
6217 (int) BFD_RELOC_GPREL16, tempreg);
6218 mips_optimize = hold_mips_optimize;
6220 p = frag_var (rs_machine_dependent, 12 + off, 0,
6221 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6222 used_at && mips_opts.noat),
6223 offset_expr.X_add_symbol, 0, NULL);
6225 /* We just generated two relocs. When tc_gen_reloc
6226 handles this case, it will skip the first reloc and
6227 handle the second. The second reloc already has an
6228 extra addend of 4, which we added above. We must
6229 subtract it out, and then subtract another 4 to make
6230 the first reloc come out right. The second reloc
6231 will come out right because we are going to add 4 to
6232 offset_expr when we build its instruction below.
6234 If we have a symbol, then we don't want to include
6235 the offset, because it will wind up being included
6236 when we generate the reloc. */
6238 if (offset_expr.X_op == O_constant)
6239 offset_expr.X_add_number -= 8;
6240 else
6242 offset_expr.X_add_number = -4;
6243 offset_expr.X_op = O_constant;
6246 macro_build_lui (p, &icnt, &offset_expr, AT);
6247 if (p != NULL)
6248 p += 4;
6249 if (breg != 0)
6251 macro_build (p, &icnt, (expressionS *) NULL,
6252 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6253 "d,v,t", AT, breg, AT);
6254 if (p != NULL)
6255 p += 4;
6257 /* Itbl support may require additional care here. */
6258 macro_build (p, &icnt, &offset_expr, s, fmt,
6259 coproc ? treg + 1 : treg,
6260 (int) BFD_RELOC_LO16, AT);
6261 if (p != NULL)
6262 p += 4;
6263 /* FIXME: How do we handle overflow here? */
6264 offset_expr.X_add_number += 4;
6265 /* Itbl support may require additional care here. */
6266 macro_build (p, &icnt, &offset_expr, s, fmt,
6267 coproc ? treg : treg + 1,
6268 (int) BFD_RELOC_LO16, AT);
6270 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6272 int off;
6274 /* If this is a reference to an external symbol, we want
6275 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6277 <op> $treg,0($at)
6278 <op> $treg+1,4($at)
6279 Otherwise we want
6280 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6282 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6283 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6284 If there is a base register we add it to $at before the
6285 lwc1 instructions. If there is a constant we include it
6286 in the lwc1 instructions. */
6287 used_at = 1;
6288 expr1.X_add_number = offset_expr.X_add_number;
6289 offset_expr.X_add_number = 0;
6290 if (expr1.X_add_number < -0x8000
6291 || expr1.X_add_number >= 0x8000 - 4)
6292 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6293 if (breg == 0)
6294 off = 0;
6295 else
6296 off = 4;
6297 frag_grow (24 + off);
6298 macro_build ((char *) NULL, &icnt, &offset_expr,
6299 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6300 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6301 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6302 if (breg != 0)
6303 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6304 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6305 "d,v,t", AT, breg, AT);
6306 /* Itbl support may require additional care here. */
6307 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6308 coproc ? treg + 1 : treg,
6309 (int) BFD_RELOC_LO16, AT);
6310 expr1.X_add_number += 4;
6312 /* Set mips_optimize to 2 to avoid inserting an undesired
6313 nop. */
6314 hold_mips_optimize = mips_optimize;
6315 mips_optimize = 2;
6316 /* Itbl support may require additional care here. */
6317 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6318 coproc ? treg : treg + 1,
6319 (int) BFD_RELOC_LO16, AT);
6320 mips_optimize = hold_mips_optimize;
6322 (void) frag_var (rs_machine_dependent, 0, 0,
6323 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6324 offset_expr.X_add_symbol, 0, NULL);
6326 else if (mips_pic == SVR4_PIC)
6328 int gpdel, off;
6329 char *p;
6331 /* If this is a reference to an external symbol, we want
6332 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6333 addu $at,$at,$gp
6334 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6336 <op> $treg,0($at)
6337 <op> $treg+1,4($at)
6338 Otherwise we want
6339 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6341 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6342 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6343 If there is a base register we add it to $at before the
6344 lwc1 instructions. If there is a constant we include it
6345 in the lwc1 instructions. */
6346 used_at = 1;
6347 expr1.X_add_number = offset_expr.X_add_number;
6348 offset_expr.X_add_number = 0;
6349 if (expr1.X_add_number < -0x8000
6350 || expr1.X_add_number >= 0x8000 - 4)
6351 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6352 if (reg_needs_delay (mips_gp_register))
6353 gpdel = 4;
6354 else
6355 gpdel = 0;
6356 if (breg == 0)
6357 off = 0;
6358 else
6359 off = 4;
6360 frag_grow (56);
6361 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6362 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6363 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6364 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6365 "d,v,t", AT, AT, mips_gp_register);
6366 macro_build ((char *) NULL, &icnt, &offset_expr,
6367 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6368 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6369 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6370 if (breg != 0)
6371 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6372 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6373 "d,v,t", AT, breg, AT);
6374 /* Itbl support may require additional care here. */
6375 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6376 coproc ? treg + 1 : treg,
6377 (int) BFD_RELOC_LO16, AT);
6378 expr1.X_add_number += 4;
6380 /* Set mips_optimize to 2 to avoid inserting an undesired
6381 nop. */
6382 hold_mips_optimize = mips_optimize;
6383 mips_optimize = 2;
6384 /* Itbl support may require additional care here. */
6385 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6386 coproc ? treg : treg + 1,
6387 (int) BFD_RELOC_LO16, AT);
6388 mips_optimize = hold_mips_optimize;
6389 expr1.X_add_number -= 4;
6391 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6392 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6393 8 + gpdel + off, 1, 0),
6394 offset_expr.X_add_symbol, 0, NULL);
6395 if (gpdel > 0)
6397 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6398 p += 4;
6400 macro_build (p, &icnt, &offset_expr,
6401 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6402 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6403 mips_gp_register);
6404 p += 4;
6405 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6406 p += 4;
6407 if (breg != 0)
6409 macro_build (p, &icnt, (expressionS *) NULL,
6410 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6411 "d,v,t", AT, breg, AT);
6412 p += 4;
6414 /* Itbl support may require additional care here. */
6415 macro_build (p, &icnt, &expr1, s, fmt,
6416 coproc ? treg + 1 : treg,
6417 (int) BFD_RELOC_LO16, AT);
6418 p += 4;
6419 expr1.X_add_number += 4;
6421 /* Set mips_optimize to 2 to avoid inserting an undesired
6422 nop. */
6423 hold_mips_optimize = mips_optimize;
6424 mips_optimize = 2;
6425 /* Itbl support may require additional care here. */
6426 macro_build (p, &icnt, &expr1, s, fmt,
6427 coproc ? treg : treg + 1,
6428 (int) BFD_RELOC_LO16, AT);
6429 mips_optimize = hold_mips_optimize;
6431 else if (mips_pic == EMBEDDED_PIC)
6433 /* If there is no base register, we use
6434 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6435 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6436 If we have a base register, we use
6437 addu $at,$breg,$gp
6438 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6439 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6441 if (breg == 0)
6443 tempreg = mips_gp_register;
6444 used_at = 0;
6446 else
6448 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6449 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6450 "d,v,t", AT, breg, mips_gp_register);
6451 tempreg = AT;
6452 used_at = 1;
6455 /* Itbl support may require additional care here. */
6456 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6457 coproc ? treg + 1 : treg,
6458 (int) BFD_RELOC_GPREL16, tempreg);
6459 offset_expr.X_add_number += 4;
6460 /* Itbl support may require additional care here. */
6461 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6462 coproc ? treg : treg + 1,
6463 (int) BFD_RELOC_GPREL16, tempreg);
6465 else
6466 abort ();
6468 if (! used_at)
6469 return;
6471 break;
6473 case M_LD_OB:
6474 s = "lw";
6475 goto sd_ob;
6476 case M_SD_OB:
6477 s = "sw";
6478 sd_ob:
6479 assert (HAVE_32BIT_ADDRESSES);
6480 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6481 (int) BFD_RELOC_LO16, breg);
6482 offset_expr.X_add_number += 4;
6483 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6484 (int) BFD_RELOC_LO16, breg);
6485 return;
6487 /* New code added to support COPZ instructions.
6488 This code builds table entries out of the macros in mip_opcodes.
6489 R4000 uses interlocks to handle coproc delays.
6490 Other chips (like the R3000) require nops to be inserted for delays.
6492 FIXME: Currently, we require that the user handle delays.
6493 In order to fill delay slots for non-interlocked chips,
6494 we must have a way to specify delays based on the coprocessor.
6495 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6496 What are the side-effects of the cop instruction?
6497 What cache support might we have and what are its effects?
6498 Both coprocessor & memory require delays. how long???
6499 What registers are read/set/modified?
6501 If an itbl is provided to interpret cop instructions,
6502 this knowledge can be encoded in the itbl spec. */
6504 case M_COP0:
6505 s = "c0";
6506 goto copz;
6507 case M_COP1:
6508 s = "c1";
6509 goto copz;
6510 case M_COP2:
6511 s = "c2";
6512 goto copz;
6513 case M_COP3:
6514 s = "c3";
6515 copz:
6516 /* For now we just do C (same as Cz). The parameter will be
6517 stored in insn_opcode by mips_ip. */
6518 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6519 ip->insn_opcode);
6520 return;
6522 case M_MOVE:
6523 move_register (&icnt, dreg, sreg);
6524 return;
6526 #ifdef LOSING_COMPILER
6527 default:
6528 /* Try and see if this is a new itbl instruction.
6529 This code builds table entries out of the macros in mip_opcodes.
6530 FIXME: For now we just assemble the expression and pass it's
6531 value along as a 32-bit immediate.
6532 We may want to have the assembler assemble this value,
6533 so that we gain the assembler's knowledge of delay slots,
6534 symbols, etc.
6535 Would it be more efficient to use mask (id) here? */
6536 if (itbl_have_entries
6537 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6539 s = ip->insn_mo->name;
6540 s2 = "cop3";
6541 coproc = ITBL_DECODE_PNUM (immed_expr);;
6542 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6543 return;
6545 macro2 (ip);
6546 return;
6548 if (mips_opts.noat)
6549 as_warn (_("Macro used $at after \".set noat\""));
6552 static void
6553 macro2 (ip)
6554 struct mips_cl_insn *ip;
6556 register int treg, sreg, dreg, breg;
6557 int tempreg;
6558 int mask;
6559 int icnt = 0;
6560 int used_at;
6561 expressionS expr1;
6562 const char *s;
6563 const char *s2;
6564 const char *fmt;
6565 int likely = 0;
6566 int dbl = 0;
6567 int coproc = 0;
6568 int lr = 0;
6569 int imm = 0;
6570 int off;
6571 offsetT maxnum;
6572 bfd_reloc_code_real_type r;
6573 char *p;
6575 treg = (ip->insn_opcode >> 16) & 0x1f;
6576 dreg = (ip->insn_opcode >> 11) & 0x1f;
6577 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6578 mask = ip->insn_mo->mask;
6580 expr1.X_op = O_constant;
6581 expr1.X_op_symbol = NULL;
6582 expr1.X_add_symbol = NULL;
6583 expr1.X_add_number = 1;
6585 switch (mask)
6587 #endif /* LOSING_COMPILER */
6589 case M_DMUL:
6590 dbl = 1;
6591 case M_MUL:
6592 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6593 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6594 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6595 dreg);
6596 return;
6598 case M_DMUL_I:
6599 dbl = 1;
6600 case M_MUL_I:
6601 /* The MIPS assembler some times generates shifts and adds. I'm
6602 not trying to be that fancy. GCC should do this for us
6603 anyway. */
6604 load_register (&icnt, AT, &imm_expr, dbl);
6605 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6606 dbl ? "dmult" : "mult", "s,t", sreg, AT);
6607 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6608 dreg);
6609 break;
6611 case M_DMULO_I:
6612 dbl = 1;
6613 case M_MULO_I:
6614 imm = 1;
6615 goto do_mulo;
6617 case M_DMULO:
6618 dbl = 1;
6619 case M_MULO:
6620 do_mulo:
6621 mips_emit_delays (true);
6622 ++mips_opts.noreorder;
6623 mips_any_noreorder = 1;
6624 if (imm)
6625 load_register (&icnt, AT, &imm_expr, dbl);
6626 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6627 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6628 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6629 dreg);
6630 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6631 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6632 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6633 AT);
6634 if (mips_trap)
6635 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6636 "s,t,q", dreg, AT, 6);
6637 else
6639 expr1.X_add_number = 8;
6640 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6641 AT);
6642 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6644 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6645 "c", 6);
6647 --mips_opts.noreorder;
6648 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6649 break;
6651 case M_DMULOU_I:
6652 dbl = 1;
6653 case M_MULOU_I:
6654 imm = 1;
6655 goto do_mulou;
6657 case M_DMULOU:
6658 dbl = 1;
6659 case M_MULOU:
6660 do_mulou:
6661 mips_emit_delays (true);
6662 ++mips_opts.noreorder;
6663 mips_any_noreorder = 1;
6664 if (imm)
6665 load_register (&icnt, AT, &imm_expr, dbl);
6666 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6667 dbl ? "dmultu" : "multu",
6668 "s,t", sreg, imm ? AT : treg);
6669 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6670 AT);
6671 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6672 dreg);
6673 if (mips_trap)
6674 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6675 "s,t,q", AT, 0, 6);
6676 else
6678 expr1.X_add_number = 8;
6679 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6680 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6682 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6683 "c", 6);
6685 --mips_opts.noreorder;
6686 break;
6688 case M_DROL:
6689 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6690 "d,v,t", AT, 0, treg);
6691 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6692 "d,t,s", AT, sreg, AT);
6693 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6694 "d,t,s", dreg, sreg, treg);
6695 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6696 "d,v,t", dreg, dreg, AT);
6697 break;
6699 case M_ROL:
6700 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6701 "d,v,t", AT, 0, treg);
6702 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6703 "d,t,s", AT, sreg, AT);
6704 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6705 "d,t,s", dreg, sreg, treg);
6706 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6707 "d,v,t", dreg, dreg, AT);
6708 break;
6710 case M_DROL_I:
6712 unsigned int rot;
6714 if (imm_expr.X_op != O_constant)
6715 as_bad (_("rotate count too large"));
6716 rot = imm_expr.X_add_number & 0x3f;
6717 if (rot == 0)
6718 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6719 "d,w,<", dreg, sreg, 0);
6720 else
6722 char *l, *r;
6724 l = (rot < 0x20) ? "dsll" : "dsll32";
6725 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6726 rot &= 0x1f;
6727 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6728 "d,w,<", AT, sreg, rot);
6729 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6730 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6731 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6732 "d,v,t", dreg, dreg, AT);
6735 break;
6737 case M_ROL_I:
6739 unsigned int rot;
6741 if (imm_expr.X_op != O_constant)
6742 as_bad (_("rotate count too large"));
6743 rot = imm_expr.X_add_number & 0x1f;
6744 if (rot == 0)
6745 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6746 "d,w,<", dreg, sreg, 0);
6747 else
6749 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6750 "d,w,<", AT, sreg, rot);
6751 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6752 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6753 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6754 "d,v,t", dreg, dreg, AT);
6757 break;
6759 case M_DROR:
6760 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6761 "d,v,t", AT, 0, treg);
6762 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6763 "d,t,s", AT, sreg, AT);
6764 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6765 "d,t,s", dreg, sreg, treg);
6766 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6767 "d,v,t", dreg, dreg, AT);
6768 break;
6770 case M_ROR:
6771 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6772 "d,v,t", AT, 0, treg);
6773 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6774 "d,t,s", AT, sreg, AT);
6775 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6776 "d,t,s", dreg, sreg, treg);
6777 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6778 "d,v,t", dreg, dreg, AT);
6779 break;
6781 case M_DROR_I:
6783 unsigned int rot;
6785 if (imm_expr.X_op != O_constant)
6786 as_bad (_("rotate count too large"));
6787 rot = imm_expr.X_add_number & 0x3f;
6788 if (rot == 0)
6789 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6790 "d,w,<", dreg, sreg, 0);
6791 else
6793 char *l, *r;
6795 r = (rot < 0x20) ? "dsrl" : "dsrl32";
6796 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6797 rot &= 0x1f;
6798 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6799 "d,w,<", AT, sreg, rot);
6800 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6801 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6802 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6803 "d,v,t", dreg, dreg, AT);
6806 break;
6808 case M_ROR_I:
6810 unsigned int rot;
6812 if (imm_expr.X_op != O_constant)
6813 as_bad (_("rotate count too large"));
6814 rot = imm_expr.X_add_number & 0x1f;
6815 if (rot == 0)
6816 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6817 "d,w,<", dreg, sreg, 0);
6818 else
6820 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6821 "d,w,<", AT, sreg, rot);
6822 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6823 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6824 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6825 "d,v,t", dreg, dreg, AT);
6828 break;
6830 case M_S_DOB:
6831 if (mips_arch == CPU_R4650)
6833 as_bad (_("opcode not supported on this processor"));
6834 return;
6836 assert (mips_opts.isa == ISA_MIPS1);
6837 /* Even on a big endian machine $fn comes before $fn+1. We have
6838 to adjust when storing to memory. */
6839 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6840 target_big_endian ? treg + 1 : treg,
6841 (int) BFD_RELOC_LO16, breg);
6842 offset_expr.X_add_number += 4;
6843 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6844 target_big_endian ? treg : treg + 1,
6845 (int) BFD_RELOC_LO16, breg);
6846 return;
6848 case M_SEQ:
6849 if (sreg == 0)
6850 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6851 treg, (int) BFD_RELOC_LO16);
6852 else if (treg == 0)
6853 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6854 sreg, (int) BFD_RELOC_LO16);
6855 else
6857 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6858 "d,v,t", dreg, sreg, treg);
6859 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6860 dreg, (int) BFD_RELOC_LO16);
6862 return;
6864 case M_SEQ_I:
6865 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6867 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6868 sreg, (int) BFD_RELOC_LO16);
6869 return;
6871 if (sreg == 0)
6873 as_warn (_("Instruction %s: result is always false"),
6874 ip->insn_mo->name);
6875 move_register (&icnt, dreg, 0);
6876 return;
6878 if (imm_expr.X_op == O_constant
6879 && imm_expr.X_add_number >= 0
6880 && imm_expr.X_add_number < 0x10000)
6882 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6883 sreg, (int) BFD_RELOC_LO16);
6884 used_at = 0;
6886 else if (imm_expr.X_op == O_constant
6887 && imm_expr.X_add_number > -0x8000
6888 && imm_expr.X_add_number < 0)
6890 imm_expr.X_add_number = -imm_expr.X_add_number;
6891 macro_build ((char *) NULL, &icnt, &imm_expr,
6892 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6893 "t,r,j", dreg, sreg,
6894 (int) BFD_RELOC_LO16);
6895 used_at = 0;
6897 else
6899 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6900 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6901 "d,v,t", dreg, sreg, AT);
6902 used_at = 1;
6904 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6905 (int) BFD_RELOC_LO16);
6906 if (used_at)
6907 break;
6908 return;
6910 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6911 s = "slt";
6912 goto sge;
6913 case M_SGEU:
6914 s = "sltu";
6915 sge:
6916 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6917 dreg, sreg, treg);
6918 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6919 (int) BFD_RELOC_LO16);
6920 return;
6922 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6923 case M_SGEU_I:
6924 if (imm_expr.X_op == O_constant
6925 && imm_expr.X_add_number >= -0x8000
6926 && imm_expr.X_add_number < 0x8000)
6928 macro_build ((char *) NULL, &icnt, &imm_expr,
6929 mask == M_SGE_I ? "slti" : "sltiu",
6930 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6931 used_at = 0;
6933 else
6935 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6936 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6937 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6938 AT);
6939 used_at = 1;
6941 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6942 (int) BFD_RELOC_LO16);
6943 if (used_at)
6944 break;
6945 return;
6947 case M_SGT: /* sreg > treg <==> treg < sreg */
6948 s = "slt";
6949 goto sgt;
6950 case M_SGTU:
6951 s = "sltu";
6952 sgt:
6953 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6954 dreg, treg, sreg);
6955 return;
6957 case M_SGT_I: /* sreg > I <==> I < sreg */
6958 s = "slt";
6959 goto sgti;
6960 case M_SGTU_I:
6961 s = "sltu";
6962 sgti:
6963 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6964 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6965 dreg, AT, sreg);
6966 break;
6968 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
6969 s = "slt";
6970 goto sle;
6971 case M_SLEU:
6972 s = "sltu";
6973 sle:
6974 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6975 dreg, treg, sreg);
6976 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6977 (int) BFD_RELOC_LO16);
6978 return;
6980 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6981 s = "slt";
6982 goto slei;
6983 case M_SLEU_I:
6984 s = "sltu";
6985 slei:
6986 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6987 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6988 dreg, AT, sreg);
6989 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6990 (int) BFD_RELOC_LO16);
6991 break;
6993 case M_SLT_I:
6994 if (imm_expr.X_op == O_constant
6995 && imm_expr.X_add_number >= -0x8000
6996 && imm_expr.X_add_number < 0x8000)
6998 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6999 dreg, sreg, (int) BFD_RELOC_LO16);
7000 return;
7002 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7003 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7004 dreg, sreg, AT);
7005 break;
7007 case M_SLTU_I:
7008 if (imm_expr.X_op == O_constant
7009 && imm_expr.X_add_number >= -0x8000
7010 && imm_expr.X_add_number < 0x8000)
7012 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7013 dreg, sreg, (int) BFD_RELOC_LO16);
7014 return;
7016 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7017 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7018 "d,v,t", dreg, sreg, AT);
7019 break;
7021 case M_SNE:
7022 if (sreg == 0)
7023 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7024 "d,v,t", dreg, 0, treg);
7025 else if (treg == 0)
7026 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7027 "d,v,t", dreg, 0, sreg);
7028 else
7030 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7031 "d,v,t", dreg, sreg, treg);
7032 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7033 "d,v,t", dreg, 0, dreg);
7035 return;
7037 case M_SNE_I:
7038 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7040 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7041 "d,v,t", dreg, 0, sreg);
7042 return;
7044 if (sreg == 0)
7046 as_warn (_("Instruction %s: result is always true"),
7047 ip->insn_mo->name);
7048 macro_build ((char *) NULL, &icnt, &expr1,
7049 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7050 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7051 return;
7053 if (imm_expr.X_op == O_constant
7054 && imm_expr.X_add_number >= 0
7055 && imm_expr.X_add_number < 0x10000)
7057 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7058 dreg, sreg, (int) BFD_RELOC_LO16);
7059 used_at = 0;
7061 else if (imm_expr.X_op == O_constant
7062 && imm_expr.X_add_number > -0x8000
7063 && imm_expr.X_add_number < 0)
7065 imm_expr.X_add_number = -imm_expr.X_add_number;
7066 macro_build ((char *) NULL, &icnt, &imm_expr,
7067 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7068 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7069 used_at = 0;
7071 else
7073 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7074 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7075 "d,v,t", dreg, sreg, AT);
7076 used_at = 1;
7078 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7079 "d,v,t", dreg, 0, dreg);
7080 if (used_at)
7081 break;
7082 return;
7084 case M_DSUB_I:
7085 dbl = 1;
7086 case M_SUB_I:
7087 if (imm_expr.X_op == O_constant
7088 && imm_expr.X_add_number > -0x8000
7089 && imm_expr.X_add_number <= 0x8000)
7091 imm_expr.X_add_number = -imm_expr.X_add_number;
7092 macro_build ((char *) NULL, &icnt, &imm_expr,
7093 dbl ? "daddi" : "addi",
7094 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7095 return;
7097 load_register (&icnt, AT, &imm_expr, dbl);
7098 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7099 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7100 break;
7102 case M_DSUBU_I:
7103 dbl = 1;
7104 case M_SUBU_I:
7105 if (imm_expr.X_op == O_constant
7106 && imm_expr.X_add_number > -0x8000
7107 && imm_expr.X_add_number <= 0x8000)
7109 imm_expr.X_add_number = -imm_expr.X_add_number;
7110 macro_build ((char *) NULL, &icnt, &imm_expr,
7111 dbl ? "daddiu" : "addiu",
7112 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7113 return;
7115 load_register (&icnt, AT, &imm_expr, dbl);
7116 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7117 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7118 break;
7120 case M_TEQ_I:
7121 s = "teq";
7122 goto trap;
7123 case M_TGE_I:
7124 s = "tge";
7125 goto trap;
7126 case M_TGEU_I:
7127 s = "tgeu";
7128 goto trap;
7129 case M_TLT_I:
7130 s = "tlt";
7131 goto trap;
7132 case M_TLTU_I:
7133 s = "tltu";
7134 goto trap;
7135 case M_TNE_I:
7136 s = "tne";
7137 trap:
7138 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7139 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7140 AT);
7141 break;
7143 case M_TRUNCWS:
7144 case M_TRUNCWD:
7145 assert (mips_opts.isa == ISA_MIPS1);
7146 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7147 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7150 * Is the double cfc1 instruction a bug in the mips assembler;
7151 * or is there a reason for it?
7153 mips_emit_delays (true);
7154 ++mips_opts.noreorder;
7155 mips_any_noreorder = 1;
7156 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7157 treg, RA);
7158 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7159 treg, RA);
7160 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7161 expr1.X_add_number = 3;
7162 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7163 (int) BFD_RELOC_LO16);
7164 expr1.X_add_number = 2;
7165 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7166 (int) BFD_RELOC_LO16);
7167 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7168 AT, RA);
7169 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7170 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7171 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7172 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7173 treg, RA);
7174 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7175 --mips_opts.noreorder;
7176 break;
7178 case M_ULH:
7179 s = "lb";
7180 goto ulh;
7181 case M_ULHU:
7182 s = "lbu";
7183 ulh:
7184 if (offset_expr.X_add_number >= 0x7fff)
7185 as_bad (_("operand overflow"));
7186 /* avoid load delay */
7187 if (! target_big_endian)
7188 ++offset_expr.X_add_number;
7189 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7190 (int) BFD_RELOC_LO16, breg);
7191 if (! target_big_endian)
7192 --offset_expr.X_add_number;
7193 else
7194 ++offset_expr.X_add_number;
7195 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7196 (int) BFD_RELOC_LO16, breg);
7197 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7198 treg, treg, 8);
7199 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7200 treg, treg, AT);
7201 break;
7203 case M_ULD:
7204 s = "ldl";
7205 s2 = "ldr";
7206 off = 7;
7207 goto ulw;
7208 case M_ULW:
7209 s = "lwl";
7210 s2 = "lwr";
7211 off = 3;
7212 ulw:
7213 if (offset_expr.X_add_number >= 0x8000 - off)
7214 as_bad (_("operand overflow"));
7215 if (! target_big_endian)
7216 offset_expr.X_add_number += off;
7217 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7218 (int) BFD_RELOC_LO16, breg);
7219 if (! target_big_endian)
7220 offset_expr.X_add_number -= off;
7221 else
7222 offset_expr.X_add_number += off;
7223 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7224 (int) BFD_RELOC_LO16, breg);
7225 return;
7227 case M_ULD_A:
7228 s = "ldl";
7229 s2 = "ldr";
7230 off = 7;
7231 goto ulwa;
7232 case M_ULW_A:
7233 s = "lwl";
7234 s2 = "lwr";
7235 off = 3;
7236 ulwa:
7237 used_at = 1;
7238 load_address (&icnt, AT, &offset_expr, &used_at);
7239 if (breg != 0)
7240 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7241 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7242 "d,v,t", AT, AT, breg);
7243 if (! target_big_endian)
7244 expr1.X_add_number = off;
7245 else
7246 expr1.X_add_number = 0;
7247 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7248 (int) BFD_RELOC_LO16, AT);
7249 if (! target_big_endian)
7250 expr1.X_add_number = 0;
7251 else
7252 expr1.X_add_number = off;
7253 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7254 (int) BFD_RELOC_LO16, AT);
7255 break;
7257 case M_ULH_A:
7258 case M_ULHU_A:
7259 used_at = 1;
7260 load_address (&icnt, AT, &offset_expr, &used_at);
7261 if (breg != 0)
7262 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7263 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7264 "d,v,t", AT, AT, breg);
7265 if (target_big_endian)
7266 expr1.X_add_number = 0;
7267 macro_build ((char *) NULL, &icnt, &expr1,
7268 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7269 (int) BFD_RELOC_LO16, AT);
7270 if (target_big_endian)
7271 expr1.X_add_number = 1;
7272 else
7273 expr1.X_add_number = 0;
7274 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7275 (int) BFD_RELOC_LO16, AT);
7276 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7277 treg, treg, 8);
7278 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7279 treg, treg, AT);
7280 break;
7282 case M_USH:
7283 if (offset_expr.X_add_number >= 0x7fff)
7284 as_bad (_("operand overflow"));
7285 if (target_big_endian)
7286 ++offset_expr.X_add_number;
7287 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7288 (int) BFD_RELOC_LO16, breg);
7289 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7290 AT, treg, 8);
7291 if (target_big_endian)
7292 --offset_expr.X_add_number;
7293 else
7294 ++offset_expr.X_add_number;
7295 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7296 (int) BFD_RELOC_LO16, breg);
7297 break;
7299 case M_USD:
7300 s = "sdl";
7301 s2 = "sdr";
7302 off = 7;
7303 goto usw;
7304 case M_USW:
7305 s = "swl";
7306 s2 = "swr";
7307 off = 3;
7308 usw:
7309 if (offset_expr.X_add_number >= 0x8000 - off)
7310 as_bad (_("operand overflow"));
7311 if (! target_big_endian)
7312 offset_expr.X_add_number += off;
7313 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7314 (int) BFD_RELOC_LO16, breg);
7315 if (! target_big_endian)
7316 offset_expr.X_add_number -= off;
7317 else
7318 offset_expr.X_add_number += off;
7319 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7320 (int) BFD_RELOC_LO16, breg);
7321 return;
7323 case M_USD_A:
7324 s = "sdl";
7325 s2 = "sdr";
7326 off = 7;
7327 goto uswa;
7328 case M_USW_A:
7329 s = "swl";
7330 s2 = "swr";
7331 off = 3;
7332 uswa:
7333 used_at = 1;
7334 load_address (&icnt, AT, &offset_expr, &used_at);
7335 if (breg != 0)
7336 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7337 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7338 "d,v,t", AT, AT, breg);
7339 if (! target_big_endian)
7340 expr1.X_add_number = off;
7341 else
7342 expr1.X_add_number = 0;
7343 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7344 (int) BFD_RELOC_LO16, AT);
7345 if (! target_big_endian)
7346 expr1.X_add_number = 0;
7347 else
7348 expr1.X_add_number = off;
7349 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7350 (int) BFD_RELOC_LO16, AT);
7351 break;
7353 case M_USH_A:
7354 used_at = 1;
7355 load_address (&icnt, AT, &offset_expr, &used_at);
7356 if (breg != 0)
7357 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7358 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7359 "d,v,t", AT, AT, breg);
7360 if (! target_big_endian)
7361 expr1.X_add_number = 0;
7362 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7363 (int) BFD_RELOC_LO16, AT);
7364 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7365 treg, treg, 8);
7366 if (! target_big_endian)
7367 expr1.X_add_number = 1;
7368 else
7369 expr1.X_add_number = 0;
7370 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7371 (int) BFD_RELOC_LO16, AT);
7372 if (! target_big_endian)
7373 expr1.X_add_number = 0;
7374 else
7375 expr1.X_add_number = 1;
7376 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7377 (int) BFD_RELOC_LO16, AT);
7378 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7379 treg, treg, 8);
7380 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7381 treg, treg, AT);
7382 break;
7384 default:
7385 /* FIXME: Check if this is one of the itbl macros, since they
7386 are added dynamically. */
7387 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7388 break;
7390 if (mips_opts.noat)
7391 as_warn (_("Macro used $at after \".set noat\""));
7394 /* Implement macros in mips16 mode. */
7396 static void
7397 mips16_macro (ip)
7398 struct mips_cl_insn *ip;
7400 int mask;
7401 int xreg, yreg, zreg, tmp;
7402 int icnt;
7403 expressionS expr1;
7404 int dbl;
7405 const char *s, *s2, *s3;
7407 mask = ip->insn_mo->mask;
7409 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7410 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7411 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7413 icnt = 0;
7415 expr1.X_op = O_constant;
7416 expr1.X_op_symbol = NULL;
7417 expr1.X_add_symbol = NULL;
7418 expr1.X_add_number = 1;
7420 dbl = 0;
7422 switch (mask)
7424 default:
7425 internalError ();
7427 case M_DDIV_3:
7428 dbl = 1;
7429 case M_DIV_3:
7430 s = "mflo";
7431 goto do_div3;
7432 case M_DREM_3:
7433 dbl = 1;
7434 case M_REM_3:
7435 s = "mfhi";
7436 do_div3:
7437 mips_emit_delays (true);
7438 ++mips_opts.noreorder;
7439 mips_any_noreorder = 1;
7440 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7441 dbl ? "ddiv" : "div",
7442 "0,x,y", xreg, yreg);
7443 expr1.X_add_number = 2;
7444 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7445 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7448 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7449 since that causes an overflow. We should do that as well,
7450 but I don't see how to do the comparisons without a temporary
7451 register. */
7452 --mips_opts.noreorder;
7453 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7454 break;
7456 case M_DIVU_3:
7457 s = "divu";
7458 s2 = "mflo";
7459 goto do_divu3;
7460 case M_REMU_3:
7461 s = "divu";
7462 s2 = "mfhi";
7463 goto do_divu3;
7464 case M_DDIVU_3:
7465 s = "ddivu";
7466 s2 = "mflo";
7467 goto do_divu3;
7468 case M_DREMU_3:
7469 s = "ddivu";
7470 s2 = "mfhi";
7471 do_divu3:
7472 mips_emit_delays (true);
7473 ++mips_opts.noreorder;
7474 mips_any_noreorder = 1;
7475 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7476 xreg, yreg);
7477 expr1.X_add_number = 2;
7478 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7479 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7480 "6", 7);
7481 --mips_opts.noreorder;
7482 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7483 break;
7485 case M_DMUL:
7486 dbl = 1;
7487 case M_MUL:
7488 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7489 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7490 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7491 zreg);
7492 return;
7494 case M_DSUBU_I:
7495 dbl = 1;
7496 goto do_subu;
7497 case M_SUBU_I:
7498 do_subu:
7499 if (imm_expr.X_op != O_constant)
7500 as_bad (_("Unsupported large constant"));
7501 imm_expr.X_add_number = -imm_expr.X_add_number;
7502 macro_build ((char *) NULL, &icnt, &imm_expr,
7503 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7504 break;
7506 case M_SUBU_I_2:
7507 if (imm_expr.X_op != O_constant)
7508 as_bad (_("Unsupported large constant"));
7509 imm_expr.X_add_number = -imm_expr.X_add_number;
7510 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7511 "x,k", xreg);
7512 break;
7514 case M_DSUBU_I_2:
7515 if (imm_expr.X_op != O_constant)
7516 as_bad (_("Unsupported large constant"));
7517 imm_expr.X_add_number = -imm_expr.X_add_number;
7518 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7519 "y,j", yreg);
7520 break;
7522 case M_BEQ:
7523 s = "cmp";
7524 s2 = "bteqz";
7525 goto do_branch;
7526 case M_BNE:
7527 s = "cmp";
7528 s2 = "btnez";
7529 goto do_branch;
7530 case M_BLT:
7531 s = "slt";
7532 s2 = "btnez";
7533 goto do_branch;
7534 case M_BLTU:
7535 s = "sltu";
7536 s2 = "btnez";
7537 goto do_branch;
7538 case M_BLE:
7539 s = "slt";
7540 s2 = "bteqz";
7541 goto do_reverse_branch;
7542 case M_BLEU:
7543 s = "sltu";
7544 s2 = "bteqz";
7545 goto do_reverse_branch;
7546 case M_BGE:
7547 s = "slt";
7548 s2 = "bteqz";
7549 goto do_branch;
7550 case M_BGEU:
7551 s = "sltu";
7552 s2 = "bteqz";
7553 goto do_branch;
7554 case M_BGT:
7555 s = "slt";
7556 s2 = "btnez";
7557 goto do_reverse_branch;
7558 case M_BGTU:
7559 s = "sltu";
7560 s2 = "btnez";
7562 do_reverse_branch:
7563 tmp = xreg;
7564 xreg = yreg;
7565 yreg = tmp;
7567 do_branch:
7568 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7569 xreg, yreg);
7570 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7571 break;
7573 case M_BEQ_I:
7574 s = "cmpi";
7575 s2 = "bteqz";
7576 s3 = "x,U";
7577 goto do_branch_i;
7578 case M_BNE_I:
7579 s = "cmpi";
7580 s2 = "btnez";
7581 s3 = "x,U";
7582 goto do_branch_i;
7583 case M_BLT_I:
7584 s = "slti";
7585 s2 = "btnez";
7586 s3 = "x,8";
7587 goto do_branch_i;
7588 case M_BLTU_I:
7589 s = "sltiu";
7590 s2 = "btnez";
7591 s3 = "x,8";
7592 goto do_branch_i;
7593 case M_BLE_I:
7594 s = "slti";
7595 s2 = "btnez";
7596 s3 = "x,8";
7597 goto do_addone_branch_i;
7598 case M_BLEU_I:
7599 s = "sltiu";
7600 s2 = "btnez";
7601 s3 = "x,8";
7602 goto do_addone_branch_i;
7603 case M_BGE_I:
7604 s = "slti";
7605 s2 = "bteqz";
7606 s3 = "x,8";
7607 goto do_branch_i;
7608 case M_BGEU_I:
7609 s = "sltiu";
7610 s2 = "bteqz";
7611 s3 = "x,8";
7612 goto do_branch_i;
7613 case M_BGT_I:
7614 s = "slti";
7615 s2 = "bteqz";
7616 s3 = "x,8";
7617 goto do_addone_branch_i;
7618 case M_BGTU_I:
7619 s = "sltiu";
7620 s2 = "bteqz";
7621 s3 = "x,8";
7623 do_addone_branch_i:
7624 if (imm_expr.X_op != O_constant)
7625 as_bad (_("Unsupported large constant"));
7626 ++imm_expr.X_add_number;
7628 do_branch_i:
7629 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7630 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7631 break;
7633 case M_ABS:
7634 expr1.X_add_number = 0;
7635 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7636 if (xreg != yreg)
7637 move_register (&icnt, xreg, yreg);
7638 expr1.X_add_number = 2;
7639 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7640 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7641 "neg", "x,w", xreg, xreg);
7645 /* For consistency checking, verify that all bits are specified either
7646 by the match/mask part of the instruction definition, or by the
7647 operand list. */
7648 static int
7649 validate_mips_insn (opc)
7650 const struct mips_opcode *opc;
7652 const char *p = opc->args;
7653 char c;
7654 unsigned long used_bits = opc->mask;
7656 if ((used_bits & opc->match) != opc->match)
7658 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7659 opc->name, opc->args);
7660 return 0;
7662 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7663 while (*p)
7664 switch (c = *p++)
7666 case ',': break;
7667 case '(': break;
7668 case ')': break;
7669 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7670 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7671 case 'A': break;
7672 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7673 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7674 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7675 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7676 case 'F': break;
7677 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7678 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7679 case 'I': break;
7680 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7681 case 'L': break;
7682 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7683 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7684 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7685 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7686 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7687 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7688 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7689 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7690 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7691 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7692 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7693 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7694 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7695 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7696 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7697 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7698 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7699 case 'f': break;
7700 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7701 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7702 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7703 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7704 case 'l': break;
7705 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7706 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7707 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7708 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7709 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7710 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7711 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7712 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7713 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7714 case 'x': break;
7715 case 'z': break;
7716 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7717 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7718 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7719 default:
7720 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7721 c, opc->name, opc->args);
7722 return 0;
7724 #undef USE_BITS
7725 if (used_bits != 0xffffffff)
7727 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7728 ~used_bits & 0xffffffff, opc->name, opc->args);
7729 return 0;
7731 return 1;
7734 /* This routine assembles an instruction into its binary format. As a
7735 side effect, it sets one of the global variables imm_reloc or
7736 offset_reloc to the type of relocation to do if one of the operands
7737 is an address expression. */
7739 static void
7740 mips_ip (str, ip)
7741 char *str;
7742 struct mips_cl_insn *ip;
7744 char *s;
7745 const char *args;
7746 char c = 0;
7747 struct mips_opcode *insn;
7748 char *argsStart;
7749 unsigned int regno;
7750 unsigned int lastregno = 0;
7751 char *s_reset;
7752 char save_c = 0;
7754 insn_error = NULL;
7756 /* If the instruction contains a '.', we first try to match an instruction
7757 including the '.'. Then we try again without the '.'. */
7758 insn = NULL;
7759 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7760 continue;
7762 /* If we stopped on whitespace, then replace the whitespace with null for
7763 the call to hash_find. Save the character we replaced just in case we
7764 have to re-parse the instruction. */
7765 if (ISSPACE (*s))
7767 save_c = *s;
7768 *s++ = '\0';
7771 insn = (struct mips_opcode *) hash_find (op_hash, str);
7773 /* If we didn't find the instruction in the opcode table, try again, but
7774 this time with just the instruction up to, but not including the
7775 first '.'. */
7776 if (insn == NULL)
7778 /* Restore the character we overwrite above (if any). */
7779 if (save_c)
7780 *(--s) = save_c;
7782 /* Scan up to the first '.' or whitespace. */
7783 for (s = str;
7784 *s != '\0' && *s != '.' && !ISSPACE (*s);
7785 ++s)
7786 continue;
7788 /* If we did not find a '.', then we can quit now. */
7789 if (*s != '.')
7791 insn_error = "unrecognized opcode";
7792 return;
7795 /* Lookup the instruction in the hash table. */
7796 *s++ = '\0';
7797 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7799 insn_error = "unrecognized opcode";
7800 return;
7804 argsStart = s;
7805 for (;;)
7807 boolean ok;
7809 assert (strcmp (insn->name, str) == 0);
7811 if (OPCODE_IS_MEMBER (insn,
7812 (mips_opts.isa
7813 | (file_ase_mips16 ? INSN_MIPS16 : 0)
7814 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7815 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7816 mips_arch))
7817 ok = true;
7818 else
7819 ok = false;
7821 if (insn->pinfo != INSN_MACRO)
7823 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7824 ok = false;
7827 if (! ok)
7829 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7830 && strcmp (insn->name, insn[1].name) == 0)
7832 ++insn;
7833 continue;
7835 else
7837 if (!insn_error)
7839 static char buf[100];
7840 if (mips_arch_info->is_isa)
7841 sprintf (buf,
7842 _("opcode not supported at this ISA level (%s)"),
7843 mips_cpu_info_from_isa (mips_opts.isa)->name);
7844 else
7845 sprintf (buf,
7846 _("opcode not supported on this processor: %s (%s)"),
7847 mips_arch_info->name,
7848 mips_cpu_info_from_isa (mips_opts.isa)->name);
7849 insn_error = buf;
7851 if (save_c)
7852 *(--s) = save_c;
7853 return;
7857 ip->insn_mo = insn;
7858 ip->insn_opcode = insn->match;
7859 insn_error = NULL;
7860 for (args = insn->args;; ++args)
7862 int is_mdmx;
7864 s += strspn (s, " \t");
7865 is_mdmx = 0;
7866 switch (*args)
7868 case '\0': /* end of args */
7869 if (*s == '\0')
7870 return;
7871 break;
7873 case ',':
7874 if (*s++ == *args)
7875 continue;
7876 s--;
7877 switch (*++args)
7879 case 'r':
7880 case 'v':
7881 ip->insn_opcode |= lastregno << OP_SH_RS;
7882 continue;
7884 case 'w':
7885 ip->insn_opcode |= lastregno << OP_SH_RT;
7886 continue;
7888 case 'W':
7889 ip->insn_opcode |= lastregno << OP_SH_FT;
7890 continue;
7892 case 'V':
7893 ip->insn_opcode |= lastregno << OP_SH_FS;
7894 continue;
7896 break;
7898 case '(':
7899 /* Handle optional base register.
7900 Either the base register is omitted or
7901 we must have a left paren. */
7902 /* This is dependent on the next operand specifier
7903 is a base register specification. */
7904 assert (args[1] == 'b' || args[1] == '5'
7905 || args[1] == '-' || args[1] == '4');
7906 if (*s == '\0')
7907 return;
7909 case ')': /* these must match exactly */
7910 if (*s++ == *args)
7911 continue;
7912 break;
7914 case '<': /* must be at least one digit */
7916 * According to the manual, if the shift amount is greater
7917 * than 31 or less than 0, then the shift amount should be
7918 * mod 32. In reality the mips assembler issues an error.
7919 * We issue a warning and mask out all but the low 5 bits.
7921 my_getExpression (&imm_expr, s);
7922 check_absolute_expr (ip, &imm_expr);
7923 if ((unsigned long) imm_expr.X_add_number > 31)
7925 as_warn (_("Improper shift amount (%lu)"),
7926 (unsigned long) imm_expr.X_add_number);
7927 imm_expr.X_add_number &= OP_MASK_SHAMT;
7929 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7930 imm_expr.X_op = O_absent;
7931 s = expr_end;
7932 continue;
7934 case '>': /* shift amount minus 32 */
7935 my_getExpression (&imm_expr, s);
7936 check_absolute_expr (ip, &imm_expr);
7937 if ((unsigned long) imm_expr.X_add_number < 32
7938 || (unsigned long) imm_expr.X_add_number > 63)
7939 break;
7940 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7941 imm_expr.X_op = O_absent;
7942 s = expr_end;
7943 continue;
7945 case 'k': /* cache code */
7946 case 'h': /* prefx code */
7947 my_getExpression (&imm_expr, s);
7948 check_absolute_expr (ip, &imm_expr);
7949 if ((unsigned long) imm_expr.X_add_number > 31)
7951 as_warn (_("Invalid value for `%s' (%lu)"),
7952 ip->insn_mo->name,
7953 (unsigned long) imm_expr.X_add_number);
7954 imm_expr.X_add_number &= 0x1f;
7956 if (*args == 'k')
7957 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7958 else
7959 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7960 imm_expr.X_op = O_absent;
7961 s = expr_end;
7962 continue;
7964 case 'c': /* break code */
7965 my_getExpression (&imm_expr, s);
7966 check_absolute_expr (ip, &imm_expr);
7967 if ((unsigned long) imm_expr.X_add_number > 1023)
7969 as_warn (_("Illegal break code (%lu)"),
7970 (unsigned long) imm_expr.X_add_number);
7971 imm_expr.X_add_number &= OP_MASK_CODE;
7973 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7974 imm_expr.X_op = O_absent;
7975 s = expr_end;
7976 continue;
7978 case 'q': /* lower break code */
7979 my_getExpression (&imm_expr, s);
7980 check_absolute_expr (ip, &imm_expr);
7981 if ((unsigned long) imm_expr.X_add_number > 1023)
7983 as_warn (_("Illegal lower break code (%lu)"),
7984 (unsigned long) imm_expr.X_add_number);
7985 imm_expr.X_add_number &= OP_MASK_CODE2;
7987 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7988 imm_expr.X_op = O_absent;
7989 s = expr_end;
7990 continue;
7992 case 'B': /* 20-bit syscall/break code. */
7993 my_getExpression (&imm_expr, s);
7994 check_absolute_expr (ip, &imm_expr);
7995 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
7996 as_warn (_("Illegal 20-bit code (%lu)"),
7997 (unsigned long) imm_expr.X_add_number);
7998 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7999 imm_expr.X_op = O_absent;
8000 s = expr_end;
8001 continue;
8003 case 'C': /* Coprocessor code */
8004 my_getExpression (&imm_expr, s);
8005 check_absolute_expr (ip, &imm_expr);
8006 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8008 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8009 (unsigned long) imm_expr.X_add_number);
8010 imm_expr.X_add_number &= ((1 << 25) - 1);
8012 ip->insn_opcode |= imm_expr.X_add_number;
8013 imm_expr.X_op = O_absent;
8014 s = expr_end;
8015 continue;
8017 case 'J': /* 19-bit wait code. */
8018 my_getExpression (&imm_expr, s);
8019 check_absolute_expr (ip, &imm_expr);
8020 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8021 as_warn (_("Illegal 19-bit code (%lu)"),
8022 (unsigned long) imm_expr.X_add_number);
8023 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8024 imm_expr.X_op = O_absent;
8025 s = expr_end;
8026 continue;
8028 case 'P': /* Performance register */
8029 my_getExpression (&imm_expr, s);
8030 check_absolute_expr (ip, &imm_expr);
8031 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8033 as_warn (_("Invalid performance register (%lu)"),
8034 (unsigned long) imm_expr.X_add_number);
8035 imm_expr.X_add_number &= OP_MASK_PERFREG;
8037 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8038 imm_expr.X_op = O_absent;
8039 s = expr_end;
8040 continue;
8042 case 'b': /* base register */
8043 case 'd': /* destination register */
8044 case 's': /* source register */
8045 case 't': /* target register */
8046 case 'r': /* both target and source */
8047 case 'v': /* both dest and source */
8048 case 'w': /* both dest and target */
8049 case 'E': /* coprocessor target register */
8050 case 'G': /* coprocessor destination register */
8051 case 'x': /* ignore register name */
8052 case 'z': /* must be zero register */
8053 case 'U': /* destination register (clo/clz). */
8054 s_reset = s;
8055 if (s[0] == '$')
8058 if (ISDIGIT (s[1]))
8060 ++s;
8061 regno = 0;
8064 regno *= 10;
8065 regno += *s - '0';
8066 ++s;
8068 while (ISDIGIT (*s));
8069 if (regno > 31)
8070 as_bad (_("Invalid register number (%d)"), regno);
8072 else if (*args == 'E' || *args == 'G')
8073 goto notreg;
8074 else
8076 if (s[1] == 'r' && s[2] == 'a')
8078 s += 3;
8079 regno = RA;
8081 else if (s[1] == 'f' && s[2] == 'p')
8083 s += 3;
8084 regno = FP;
8086 else if (s[1] == 's' && s[2] == 'p')
8088 s += 3;
8089 regno = SP;
8091 else if (s[1] == 'g' && s[2] == 'p')
8093 s += 3;
8094 regno = GP;
8096 else if (s[1] == 'a' && s[2] == 't')
8098 s += 3;
8099 regno = AT;
8101 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8103 s += 4;
8104 regno = KT0;
8106 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8108 s += 4;
8109 regno = KT1;
8111 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8113 s += 5;
8114 regno = ZERO;
8116 else if (itbl_have_entries)
8118 char *p, *n;
8119 unsigned long r;
8121 p = s + 1; /* advance past '$' */
8122 n = itbl_get_field (&p); /* n is name */
8124 /* See if this is a register defined in an
8125 itbl entry. */
8126 if (itbl_get_reg_val (n, &r))
8128 /* Get_field advances to the start of
8129 the next field, so we need to back
8130 rack to the end of the last field. */
8131 if (p)
8132 s = p - 1;
8133 else
8134 s = strchr (s, '\0');
8135 regno = r;
8137 else
8138 goto notreg;
8140 else
8141 goto notreg;
8143 if (regno == AT
8144 && ! mips_opts.noat
8145 && *args != 'E'
8146 && *args != 'G')
8147 as_warn (_("Used $at without \".set noat\""));
8148 c = *args;
8149 if (*s == ' ')
8150 ++s;
8151 if (args[1] != *s)
8153 if (c == 'r' || c == 'v' || c == 'w')
8155 regno = lastregno;
8156 s = s_reset;
8157 ++args;
8160 /* 'z' only matches $0. */
8161 if (c == 'z' && regno != 0)
8162 break;
8164 /* Now that we have assembled one operand, we use the args string
8165 * to figure out where it goes in the instruction. */
8166 switch (c)
8168 case 'r':
8169 case 's':
8170 case 'v':
8171 case 'b':
8172 ip->insn_opcode |= regno << OP_SH_RS;
8173 break;
8174 case 'd':
8175 case 'G':
8176 ip->insn_opcode |= regno << OP_SH_RD;
8177 break;
8178 case 'U':
8179 ip->insn_opcode |= regno << OP_SH_RD;
8180 ip->insn_opcode |= regno << OP_SH_RT;
8181 break;
8182 case 'w':
8183 case 't':
8184 case 'E':
8185 ip->insn_opcode |= regno << OP_SH_RT;
8186 break;
8187 case 'x':
8188 /* This case exists because on the r3000 trunc
8189 expands into a macro which requires a gp
8190 register. On the r6000 or r4000 it is
8191 assembled into a single instruction which
8192 ignores the register. Thus the insn version
8193 is MIPS_ISA2 and uses 'x', and the macro
8194 version is MIPS_ISA1 and uses 't'. */
8195 break;
8196 case 'z':
8197 /* This case is for the div instruction, which
8198 acts differently if the destination argument
8199 is $0. This only matches $0, and is checked
8200 outside the switch. */
8201 break;
8202 case 'D':
8203 /* Itbl operand; not yet implemented. FIXME ?? */
8204 break;
8205 /* What about all other operands like 'i', which
8206 can be specified in the opcode table? */
8208 lastregno = regno;
8209 continue;
8211 notreg:
8212 switch (*args++)
8214 case 'r':
8215 case 'v':
8216 ip->insn_opcode |= lastregno << OP_SH_RS;
8217 continue;
8218 case 'w':
8219 ip->insn_opcode |= lastregno << OP_SH_RT;
8220 continue;
8222 break;
8224 case 'O': /* MDMX alignment immediate constant. */
8225 my_getExpression (&imm_expr, s);
8226 check_absolute_expr (ip, &imm_expr);
8227 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8229 as_warn ("Improper align amount (%ld), using low bits",
8230 (long) imm_expr.X_add_number);
8231 imm_expr.X_add_number &= OP_MASK_ALN;
8233 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8234 imm_expr.X_op = O_absent;
8235 s = expr_end;
8236 continue;
8238 case 'Q': /* MDMX vector, element sel, or const. */
8239 if (s[0] != '$')
8241 /* MDMX Immediate. */
8242 my_getExpression (&imm_expr, s);
8243 check_absolute_expr (ip, &imm_expr);
8244 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8246 as_warn (_("Invalid MDMX Immediate (%ld)"),
8247 (long) imm_expr.X_add_number);
8248 imm_expr.X_add_number &= OP_MASK_FT;
8250 imm_expr.X_add_number &= OP_MASK_FT;
8251 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8252 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8253 else
8254 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8255 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8256 imm_expr.X_op = O_absent;
8257 s = expr_end;
8258 continue;
8260 /* Not MDMX Immediate. Fall through. */
8261 case 'X': /* MDMX destination register. */
8262 case 'Y': /* MDMX source register. */
8263 case 'Z': /* MDMX target register. */
8264 is_mdmx = 1;
8265 case 'D': /* floating point destination register */
8266 case 'S': /* floating point source register */
8267 case 'T': /* floating point target register */
8268 case 'R': /* floating point source register */
8269 case 'V':
8270 case 'W':
8271 s_reset = s;
8272 /* Accept $fN for FP and MDMX register numbers, and in
8273 addition accept $vN for MDMX register numbers. */
8274 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8275 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8276 && ISDIGIT (s[2])))
8278 s += 2;
8279 regno = 0;
8282 regno *= 10;
8283 regno += *s - '0';
8284 ++s;
8286 while (ISDIGIT (*s));
8288 if (regno > 31)
8289 as_bad (_("Invalid float register number (%d)"), regno);
8291 if ((regno & 1) != 0
8292 && HAVE_32BIT_FPRS
8293 && ! (strcmp (str, "mtc1") == 0
8294 || strcmp (str, "mfc1") == 0
8295 || strcmp (str, "lwc1") == 0
8296 || strcmp (str, "swc1") == 0
8297 || strcmp (str, "l.s") == 0
8298 || strcmp (str, "s.s") == 0))
8299 as_warn (_("Float register should be even, was %d"),
8300 regno);
8302 c = *args;
8303 if (*s == ' ')
8304 ++s;
8305 if (args[1] != *s)
8307 if (c == 'V' || c == 'W')
8309 regno = lastregno;
8310 s = s_reset;
8311 ++args;
8314 switch (c)
8316 case 'D':
8317 case 'X':
8318 ip->insn_opcode |= regno << OP_SH_FD;
8319 break;
8320 case 'V':
8321 case 'S':
8322 case 'Y':
8323 ip->insn_opcode |= regno << OP_SH_FS;
8324 break;
8325 case 'Q':
8326 /* This is like 'Z', but also needs to fix the MDMX
8327 vector/scalar select bits. Note that the
8328 scalar immediate case is handled above. */
8329 if (*s == '[')
8331 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8332 int max_el = (is_qh ? 3 : 7);
8333 s++;
8334 my_getExpression(&imm_expr, s);
8335 check_absolute_expr (ip, &imm_expr);
8336 s = expr_end;
8337 if (imm_expr.X_add_number > max_el)
8338 as_bad(_("Bad element selector %ld"),
8339 (long) imm_expr.X_add_number);
8340 imm_expr.X_add_number &= max_el;
8341 ip->insn_opcode |= (imm_expr.X_add_number
8342 << (OP_SH_VSEL +
8343 (is_qh ? 2 : 1)));
8344 if (*s != ']')
8345 as_warn(_("Expecting ']' found '%s'"), s);
8346 else
8347 s++;
8349 else
8351 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8352 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8353 << OP_SH_VSEL);
8354 else
8355 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8356 OP_SH_VSEL);
8358 /* Fall through */
8359 case 'W':
8360 case 'T':
8361 case 'Z':
8362 ip->insn_opcode |= regno << OP_SH_FT;
8363 break;
8364 case 'R':
8365 ip->insn_opcode |= regno << OP_SH_FR;
8366 break;
8368 lastregno = regno;
8369 continue;
8372 switch (*args++)
8374 case 'V':
8375 ip->insn_opcode |= lastregno << OP_SH_FS;
8376 continue;
8377 case 'W':
8378 ip->insn_opcode |= lastregno << OP_SH_FT;
8379 continue;
8381 break;
8383 case 'I':
8384 my_getExpression (&imm_expr, s);
8385 if (imm_expr.X_op != O_big
8386 && imm_expr.X_op != O_constant)
8387 insn_error = _("absolute expression required");
8388 s = expr_end;
8389 continue;
8391 case 'A':
8392 my_getExpression (&offset_expr, s);
8393 *imm_reloc = BFD_RELOC_32;
8394 s = expr_end;
8395 continue;
8397 case 'F':
8398 case 'L':
8399 case 'f':
8400 case 'l':
8402 int f64;
8403 int using_gprs;
8404 char *save_in;
8405 char *err;
8406 unsigned char temp[8];
8407 int len;
8408 unsigned int length;
8409 segT seg;
8410 subsegT subseg;
8411 char *p;
8413 /* These only appear as the last operand in an
8414 instruction, and every instruction that accepts
8415 them in any variant accepts them in all variants.
8416 This means we don't have to worry about backing out
8417 any changes if the instruction does not match.
8419 The difference between them is the size of the
8420 floating point constant and where it goes. For 'F'
8421 and 'L' the constant is 64 bits; for 'f' and 'l' it
8422 is 32 bits. Where the constant is placed is based
8423 on how the MIPS assembler does things:
8424 F -- .rdata
8425 L -- .lit8
8426 f -- immediate value
8427 l -- .lit4
8429 The .lit4 and .lit8 sections are only used if
8430 permitted by the -G argument.
8432 When generating embedded PIC code, we use the
8433 .lit8 section but not the .lit4 section (we can do
8434 .lit4 inline easily; we need to put .lit8
8435 somewhere in the data segment, and using .lit8
8436 permits the linker to eventually combine identical
8437 .lit8 entries).
8439 The code below needs to know whether the target register
8440 is 32 or 64 bits wide. It relies on the fact 'f' and
8441 'F' are used with GPR-based instructions and 'l' and
8442 'L' are used with FPR-based instructions. */
8444 f64 = *args == 'F' || *args == 'L';
8445 using_gprs = *args == 'F' || *args == 'f';
8447 save_in = input_line_pointer;
8448 input_line_pointer = s;
8449 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8450 length = len;
8451 s = input_line_pointer;
8452 input_line_pointer = save_in;
8453 if (err != NULL && *err != '\0')
8455 as_bad (_("Bad floating point constant: %s"), err);
8456 memset (temp, '\0', sizeof temp);
8457 length = f64 ? 8 : 4;
8460 assert (length == (unsigned) (f64 ? 8 : 4));
8462 if (*args == 'f'
8463 || (*args == 'l'
8464 && (! USE_GLOBAL_POINTER_OPT
8465 || mips_pic == EMBEDDED_PIC
8466 || g_switch_value < 4
8467 || (temp[0] == 0 && temp[1] == 0)
8468 || (temp[2] == 0 && temp[3] == 0))))
8470 imm_expr.X_op = O_constant;
8471 if (! target_big_endian)
8472 imm_expr.X_add_number = bfd_getl32 (temp);
8473 else
8474 imm_expr.X_add_number = bfd_getb32 (temp);
8476 else if (length > 4
8477 && ! mips_disable_float_construction
8478 /* Constants can only be constructed in GPRs and
8479 copied to FPRs if the GPRs are at least as wide
8480 as the FPRs. Force the constant into memory if
8481 we are using 64-bit FPRs but the GPRs are only
8482 32 bits wide. */
8483 && (using_gprs
8484 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8485 && ((temp[0] == 0 && temp[1] == 0)
8486 || (temp[2] == 0 && temp[3] == 0))
8487 && ((temp[4] == 0 && temp[5] == 0)
8488 || (temp[6] == 0 && temp[7] == 0)))
8490 /* The value is simple enough to load with a couple of
8491 instructions. If using 32-bit registers, set
8492 imm_expr to the high order 32 bits and offset_expr to
8493 the low order 32 bits. Otherwise, set imm_expr to
8494 the entire 64 bit constant. */
8495 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8497 imm_expr.X_op = O_constant;
8498 offset_expr.X_op = O_constant;
8499 if (! target_big_endian)
8501 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8502 offset_expr.X_add_number = bfd_getl32 (temp);
8504 else
8506 imm_expr.X_add_number = bfd_getb32 (temp);
8507 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8509 if (offset_expr.X_add_number == 0)
8510 offset_expr.X_op = O_absent;
8512 else if (sizeof (imm_expr.X_add_number) > 4)
8514 imm_expr.X_op = O_constant;
8515 if (! target_big_endian)
8516 imm_expr.X_add_number = bfd_getl64 (temp);
8517 else
8518 imm_expr.X_add_number = bfd_getb64 (temp);
8520 else
8522 imm_expr.X_op = O_big;
8523 imm_expr.X_add_number = 4;
8524 if (! target_big_endian)
8526 generic_bignum[0] = bfd_getl16 (temp);
8527 generic_bignum[1] = bfd_getl16 (temp + 2);
8528 generic_bignum[2] = bfd_getl16 (temp + 4);
8529 generic_bignum[3] = bfd_getl16 (temp + 6);
8531 else
8533 generic_bignum[0] = bfd_getb16 (temp + 6);
8534 generic_bignum[1] = bfd_getb16 (temp + 4);
8535 generic_bignum[2] = bfd_getb16 (temp + 2);
8536 generic_bignum[3] = bfd_getb16 (temp);
8540 else
8542 const char *newname;
8543 segT new_seg;
8545 /* Switch to the right section. */
8546 seg = now_seg;
8547 subseg = now_subseg;
8548 switch (*args)
8550 default: /* unused default case avoids warnings. */
8551 case 'L':
8552 newname = RDATA_SECTION_NAME;
8553 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8554 || mips_pic == EMBEDDED_PIC)
8555 newname = ".lit8";
8556 break;
8557 case 'F':
8558 if (mips_pic == EMBEDDED_PIC)
8559 newname = ".lit8";
8560 else
8561 newname = RDATA_SECTION_NAME;
8562 break;
8563 case 'l':
8564 assert (!USE_GLOBAL_POINTER_OPT
8565 || g_switch_value >= 4);
8566 newname = ".lit4";
8567 break;
8569 new_seg = subseg_new (newname, (subsegT) 0);
8570 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8571 bfd_set_section_flags (stdoutput, new_seg,
8572 (SEC_ALLOC
8573 | SEC_LOAD
8574 | SEC_READONLY
8575 | SEC_DATA));
8576 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8577 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8578 && strcmp (TARGET_OS, "elf") != 0)
8579 record_alignment (new_seg, 4);
8580 else
8581 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8582 if (seg == now_seg)
8583 as_bad (_("Can't use floating point insn in this section"));
8585 /* Set the argument to the current address in the
8586 section. */
8587 offset_expr.X_op = O_symbol;
8588 offset_expr.X_add_symbol =
8589 symbol_new ("L0\001", now_seg,
8590 (valueT) frag_now_fix (), frag_now);
8591 offset_expr.X_add_number = 0;
8593 /* Put the floating point number into the section. */
8594 p = frag_more ((int) length);
8595 memcpy (p, temp, length);
8597 /* Switch back to the original section. */
8598 subseg_set (seg, subseg);
8601 continue;
8603 case 'i': /* 16 bit unsigned immediate */
8604 case 'j': /* 16 bit signed immediate */
8605 *imm_reloc = BFD_RELOC_LO16;
8606 c = my_getSmallExpression (&imm_expr, s);
8607 if (c != S_EX_NONE)
8609 if (c != S_EX_LO)
8611 if (c == S_EX_HI)
8613 *imm_reloc = BFD_RELOC_HI16_S;
8614 imm_unmatched_hi = true;
8616 #ifdef OBJ_ELF
8617 else if (c == S_EX_HIGHEST)
8618 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8619 else if (c == S_EX_HIGHER)
8620 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8621 else if (c == S_EX_GP_REL)
8623 /* This occurs in NewABI only. */
8624 c = my_getSmallExpression (&imm_expr, s);
8625 if (c != S_EX_NEG)
8626 as_bad (_("bad composition of relocations"));
8627 else
8629 c = my_getSmallExpression (&imm_expr, s);
8630 if (c != S_EX_LO)
8631 as_bad (_("bad composition of relocations"));
8632 else
8634 imm_reloc[0] = BFD_RELOC_GPREL16;
8635 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8636 imm_reloc[2] = BFD_RELOC_LO16;
8640 #endif
8641 else
8642 *imm_reloc = BFD_RELOC_HI16;
8644 else if (imm_expr.X_op == O_constant)
8645 imm_expr.X_add_number &= 0xffff;
8647 if (*args == 'i')
8649 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8650 || ((imm_expr.X_add_number < 0
8651 || imm_expr.X_add_number >= 0x10000)
8652 && imm_expr.X_op == O_constant))
8654 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8655 !strcmp (insn->name, insn[1].name))
8656 break;
8657 if (imm_expr.X_op == O_constant
8658 || imm_expr.X_op == O_big)
8659 as_bad (_("16 bit expression not in range 0..65535"));
8662 else
8664 int more;
8665 offsetT max;
8667 /* The upper bound should be 0x8000, but
8668 unfortunately the MIPS assembler accepts numbers
8669 from 0x8000 to 0xffff and sign extends them, and
8670 we want to be compatible. We only permit this
8671 extended range for an instruction which does not
8672 provide any further alternates, since those
8673 alternates may handle other cases. People should
8674 use the numbers they mean, rather than relying on
8675 a mysterious sign extension. */
8676 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8677 strcmp (insn->name, insn[1].name) == 0);
8678 if (more)
8679 max = 0x8000;
8680 else
8681 max = 0x10000;
8682 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8683 || ((imm_expr.X_add_number < -0x8000
8684 || imm_expr.X_add_number >= max)
8685 && imm_expr.X_op == O_constant)
8686 || (more
8687 && imm_expr.X_add_number < 0
8688 && HAVE_64BIT_GPRS
8689 && imm_expr.X_unsigned
8690 && sizeof (imm_expr.X_add_number) <= 4))
8692 if (more)
8693 break;
8694 if (imm_expr.X_op == O_constant
8695 || imm_expr.X_op == O_big)
8696 as_bad (_("16 bit expression not in range -32768..32767"));
8699 s = expr_end;
8700 continue;
8702 case 'o': /* 16 bit offset */
8703 c = my_getSmallExpression (&offset_expr, s);
8705 /* If this value won't fit into a 16 bit offset, then go
8706 find a macro that will generate the 32 bit offset
8707 code pattern. */
8708 if (c == S_EX_NONE
8709 && (offset_expr.X_op != O_constant
8710 || offset_expr.X_add_number >= 0x8000
8711 || offset_expr.X_add_number < -0x8000))
8712 break;
8714 if (c == S_EX_HI)
8716 if (offset_expr.X_op != O_constant)
8717 break;
8718 offset_expr.X_add_number =
8719 (offset_expr.X_add_number >> 16) & 0xffff;
8721 *offset_reloc = BFD_RELOC_LO16;
8722 s = expr_end;
8723 continue;
8725 case 'p': /* pc relative offset */
8726 if (mips_pic == EMBEDDED_PIC)
8727 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8728 else
8729 *offset_reloc = BFD_RELOC_16_PCREL;
8730 my_getExpression (&offset_expr, s);
8731 s = expr_end;
8732 continue;
8734 case 'u': /* upper 16 bits */
8735 c = my_getSmallExpression (&imm_expr, s);
8736 *imm_reloc = BFD_RELOC_LO16;
8737 if (c != S_EX_NONE)
8739 if (c != S_EX_LO)
8741 if (c == S_EX_HI)
8743 *imm_reloc = BFD_RELOC_HI16_S;
8744 imm_unmatched_hi = true;
8746 #ifdef OBJ_ELF
8747 else if (c == S_EX_HIGHEST)
8748 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8749 else if (c == S_EX_GP_REL)
8751 /* This occurs in NewABI only. */
8752 c = my_getSmallExpression (&imm_expr, s);
8753 if (c != S_EX_NEG)
8754 as_bad (_("bad composition of relocations"));
8755 else
8757 c = my_getSmallExpression (&imm_expr, s);
8758 if (c != S_EX_HI)
8759 as_bad (_("bad composition of relocations"));
8760 else
8762 imm_reloc[0] = BFD_RELOC_GPREL16;
8763 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8764 imm_reloc[2] = BFD_RELOC_HI16_S;
8768 #endif
8769 else
8770 *imm_reloc = BFD_RELOC_HI16;
8772 else if (imm_expr.X_op == O_constant)
8773 imm_expr.X_add_number &= 0xffff;
8775 else if (imm_expr.X_op == O_constant
8776 && (imm_expr.X_add_number < 0
8777 || imm_expr.X_add_number >= 0x10000))
8778 as_bad (_("lui expression not in range 0..65535"));
8779 s = expr_end;
8780 continue;
8782 case 'a': /* 26 bit address */
8783 my_getExpression (&offset_expr, s);
8784 s = expr_end;
8785 *offset_reloc = BFD_RELOC_MIPS_JMP;
8786 continue;
8788 case 'N': /* 3 bit branch condition code */
8789 case 'M': /* 3 bit compare condition code */
8790 if (strncmp (s, "$fcc", 4) != 0)
8791 break;
8792 s += 4;
8793 regno = 0;
8796 regno *= 10;
8797 regno += *s - '0';
8798 ++s;
8800 while (ISDIGIT (*s));
8801 if (regno > 7)
8802 as_bad (_("invalid condition code register $fcc%d"), regno);
8803 if (*args == 'N')
8804 ip->insn_opcode |= regno << OP_SH_BCC;
8805 else
8806 ip->insn_opcode |= regno << OP_SH_CCC;
8807 continue;
8809 case 'H':
8810 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8811 s += 2;
8812 if (ISDIGIT (*s))
8814 c = 0;
8817 c *= 10;
8818 c += *s - '0';
8819 ++s;
8821 while (ISDIGIT (*s));
8823 else
8824 c = 8; /* Invalid sel value. */
8826 if (c > 7)
8827 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8828 ip->insn_opcode |= c;
8829 continue;
8831 default:
8832 as_bad (_("bad char = '%c'\n"), *args);
8833 internalError ();
8835 break;
8837 /* Args don't match. */
8838 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8839 !strcmp (insn->name, insn[1].name))
8841 ++insn;
8842 s = argsStart;
8843 insn_error = _("illegal operands");
8844 continue;
8846 if (save_c)
8847 *(--s) = save_c;
8848 insn_error = _("illegal operands");
8849 return;
8853 /* This routine assembles an instruction into its binary format when
8854 assembling for the mips16. As a side effect, it sets one of the
8855 global variables imm_reloc or offset_reloc to the type of
8856 relocation to do if one of the operands is an address expression.
8857 It also sets mips16_small and mips16_ext if the user explicitly
8858 requested a small or extended instruction. */
8860 static void
8861 mips16_ip (str, ip)
8862 char *str;
8863 struct mips_cl_insn *ip;
8865 char *s;
8866 const char *args;
8867 struct mips_opcode *insn;
8868 char *argsstart;
8869 unsigned int regno;
8870 unsigned int lastregno = 0;
8871 char *s_reset;
8873 insn_error = NULL;
8875 mips16_small = false;
8876 mips16_ext = false;
8878 for (s = str; ISLOWER (*s); ++s)
8880 switch (*s)
8882 case '\0':
8883 break;
8885 case ' ':
8886 *s++ = '\0';
8887 break;
8889 case '.':
8890 if (s[1] == 't' && s[2] == ' ')
8892 *s = '\0';
8893 mips16_small = true;
8894 s += 3;
8895 break;
8897 else if (s[1] == 'e' && s[2] == ' ')
8899 *s = '\0';
8900 mips16_ext = true;
8901 s += 3;
8902 break;
8904 /* Fall through. */
8905 default:
8906 insn_error = _("unknown opcode");
8907 return;
8910 if (mips_opts.noautoextend && ! mips16_ext)
8911 mips16_small = true;
8913 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8915 insn_error = _("unrecognized opcode");
8916 return;
8919 argsstart = s;
8920 for (;;)
8922 assert (strcmp (insn->name, str) == 0);
8924 ip->insn_mo = insn;
8925 ip->insn_opcode = insn->match;
8926 ip->use_extend = false;
8927 imm_expr.X_op = O_absent;
8928 imm_reloc[0] = BFD_RELOC_UNUSED;
8929 imm_reloc[1] = BFD_RELOC_UNUSED;
8930 imm_reloc[2] = BFD_RELOC_UNUSED;
8931 offset_expr.X_op = O_absent;
8932 offset_reloc[0] = BFD_RELOC_UNUSED;
8933 offset_reloc[1] = BFD_RELOC_UNUSED;
8934 offset_reloc[2] = BFD_RELOC_UNUSED;
8935 for (args = insn->args; 1; ++args)
8937 int c;
8939 if (*s == ' ')
8940 ++s;
8942 /* In this switch statement we call break if we did not find
8943 a match, continue if we did find a match, or return if we
8944 are done. */
8946 c = *args;
8947 switch (c)
8949 case '\0':
8950 if (*s == '\0')
8952 /* Stuff the immediate value in now, if we can. */
8953 if (imm_expr.X_op == O_constant
8954 && *imm_reloc > BFD_RELOC_UNUSED
8955 && insn->pinfo != INSN_MACRO)
8957 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8958 imm_expr.X_add_number, true, mips16_small,
8959 mips16_ext, &ip->insn_opcode,
8960 &ip->use_extend, &ip->extend);
8961 imm_expr.X_op = O_absent;
8962 *imm_reloc = BFD_RELOC_UNUSED;
8965 return;
8967 break;
8969 case ',':
8970 if (*s++ == c)
8971 continue;
8972 s--;
8973 switch (*++args)
8975 case 'v':
8976 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8977 continue;
8978 case 'w':
8979 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8980 continue;
8982 break;
8984 case '(':
8985 case ')':
8986 if (*s++ == c)
8987 continue;
8988 break;
8990 case 'v':
8991 case 'w':
8992 if (s[0] != '$')
8994 if (c == 'v')
8995 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8996 else
8997 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8998 ++args;
8999 continue;
9001 /* Fall through. */
9002 case 'x':
9003 case 'y':
9004 case 'z':
9005 case 'Z':
9006 case '0':
9007 case 'S':
9008 case 'R':
9009 case 'X':
9010 case 'Y':
9011 if (s[0] != '$')
9012 break;
9013 s_reset = s;
9014 if (ISDIGIT (s[1]))
9016 ++s;
9017 regno = 0;
9020 regno *= 10;
9021 regno += *s - '0';
9022 ++s;
9024 while (ISDIGIT (*s));
9025 if (regno > 31)
9027 as_bad (_("invalid register number (%d)"), regno);
9028 regno = 2;
9031 else
9033 if (s[1] == 'r' && s[2] == 'a')
9035 s += 3;
9036 regno = RA;
9038 else if (s[1] == 'f' && s[2] == 'p')
9040 s += 3;
9041 regno = FP;
9043 else if (s[1] == 's' && s[2] == 'p')
9045 s += 3;
9046 regno = SP;
9048 else if (s[1] == 'g' && s[2] == 'p')
9050 s += 3;
9051 regno = GP;
9053 else if (s[1] == 'a' && s[2] == 't')
9055 s += 3;
9056 regno = AT;
9058 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9060 s += 4;
9061 regno = KT0;
9063 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9065 s += 4;
9066 regno = KT1;
9068 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9070 s += 5;
9071 regno = ZERO;
9073 else
9074 break;
9077 if (*s == ' ')
9078 ++s;
9079 if (args[1] != *s)
9081 if (c == 'v' || c == 'w')
9083 regno = mips16_to_32_reg_map[lastregno];
9084 s = s_reset;
9085 ++args;
9089 switch (c)
9091 case 'x':
9092 case 'y':
9093 case 'z':
9094 case 'v':
9095 case 'w':
9096 case 'Z':
9097 regno = mips32_to_16_reg_map[regno];
9098 break;
9100 case '0':
9101 if (regno != 0)
9102 regno = ILLEGAL_REG;
9103 break;
9105 case 'S':
9106 if (regno != SP)
9107 regno = ILLEGAL_REG;
9108 break;
9110 case 'R':
9111 if (regno != RA)
9112 regno = ILLEGAL_REG;
9113 break;
9115 case 'X':
9116 case 'Y':
9117 if (regno == AT && ! mips_opts.noat)
9118 as_warn (_("used $at without \".set noat\""));
9119 break;
9121 default:
9122 internalError ();
9125 if (regno == ILLEGAL_REG)
9126 break;
9128 switch (c)
9130 case 'x':
9131 case 'v':
9132 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9133 break;
9134 case 'y':
9135 case 'w':
9136 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9137 break;
9138 case 'z':
9139 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9140 break;
9141 case 'Z':
9142 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9143 case '0':
9144 case 'S':
9145 case 'R':
9146 break;
9147 case 'X':
9148 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9149 break;
9150 case 'Y':
9151 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9152 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9153 break;
9154 default:
9155 internalError ();
9158 lastregno = regno;
9159 continue;
9161 case 'P':
9162 if (strncmp (s, "$pc", 3) == 0)
9164 s += 3;
9165 continue;
9167 break;
9169 case '<':
9170 case '>':
9171 case '[':
9172 case ']':
9173 case '4':
9174 case '5':
9175 case 'H':
9176 case 'W':
9177 case 'D':
9178 case 'j':
9179 case '8':
9180 case 'V':
9181 case 'C':
9182 case 'U':
9183 case 'k':
9184 case 'K':
9185 if (s[0] == '%'
9186 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9188 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9189 and generate the appropriate reloc. If the text
9190 inside %gprel is not a symbol name with an
9191 optional offset, then we generate a normal reloc
9192 and will probably fail later. */
9193 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9194 if (imm_expr.X_op == O_symbol)
9196 mips16_ext = true;
9197 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9198 s = expr_end;
9199 ip->use_extend = true;
9200 ip->extend = 0;
9201 continue;
9204 else
9206 /* Just pick up a normal expression. */
9207 my_getExpression (&imm_expr, s);
9210 if (imm_expr.X_op == O_register)
9212 /* What we thought was an expression turned out to
9213 be a register. */
9215 if (s[0] == '(' && args[1] == '(')
9217 /* It looks like the expression was omitted
9218 before a register indirection, which means
9219 that the expression is implicitly zero. We
9220 still set up imm_expr, so that we handle
9221 explicit extensions correctly. */
9222 imm_expr.X_op = O_constant;
9223 imm_expr.X_add_number = 0;
9224 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9225 continue;
9228 break;
9231 /* We need to relax this instruction. */
9232 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9233 s = expr_end;
9234 continue;
9236 case 'p':
9237 case 'q':
9238 case 'A':
9239 case 'B':
9240 case 'E':
9241 /* We use offset_reloc rather than imm_reloc for the PC
9242 relative operands. This lets macros with both
9243 immediate and address operands work correctly. */
9244 my_getExpression (&offset_expr, s);
9246 if (offset_expr.X_op == O_register)
9247 break;
9249 /* We need to relax this instruction. */
9250 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9251 s = expr_end;
9252 continue;
9254 case '6': /* break code */
9255 my_getExpression (&imm_expr, s);
9256 check_absolute_expr (ip, &imm_expr);
9257 if ((unsigned long) imm_expr.X_add_number > 63)
9259 as_warn (_("Invalid value for `%s' (%lu)"),
9260 ip->insn_mo->name,
9261 (unsigned long) imm_expr.X_add_number);
9262 imm_expr.X_add_number &= 0x3f;
9264 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9265 imm_expr.X_op = O_absent;
9266 s = expr_end;
9267 continue;
9269 case 'a': /* 26 bit address */
9270 my_getExpression (&offset_expr, s);
9271 s = expr_end;
9272 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9273 ip->insn_opcode <<= 16;
9274 continue;
9276 case 'l': /* register list for entry macro */
9277 case 'L': /* register list for exit macro */
9279 int mask;
9281 if (c == 'l')
9282 mask = 0;
9283 else
9284 mask = 7 << 3;
9285 while (*s != '\0')
9287 int freg, reg1, reg2;
9289 while (*s == ' ' || *s == ',')
9290 ++s;
9291 if (*s != '$')
9293 as_bad (_("can't parse register list"));
9294 break;
9296 ++s;
9297 if (*s != 'f')
9298 freg = 0;
9299 else
9301 freg = 1;
9302 ++s;
9304 reg1 = 0;
9305 while (ISDIGIT (*s))
9307 reg1 *= 10;
9308 reg1 += *s - '0';
9309 ++s;
9311 if (*s == ' ')
9312 ++s;
9313 if (*s != '-')
9314 reg2 = reg1;
9315 else
9317 ++s;
9318 if (*s != '$')
9319 break;
9320 ++s;
9321 if (freg)
9323 if (*s == 'f')
9324 ++s;
9325 else
9327 as_bad (_("invalid register list"));
9328 break;
9331 reg2 = 0;
9332 while (ISDIGIT (*s))
9334 reg2 *= 10;
9335 reg2 += *s - '0';
9336 ++s;
9339 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9341 mask &= ~ (7 << 3);
9342 mask |= 5 << 3;
9344 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9346 mask &= ~ (7 << 3);
9347 mask |= 6 << 3;
9349 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9350 mask |= (reg2 - 3) << 3;
9351 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9352 mask |= (reg2 - 15) << 1;
9353 else if (reg1 == RA && reg2 == RA)
9354 mask |= 1;
9355 else
9357 as_bad (_("invalid register list"));
9358 break;
9361 /* The mask is filled in in the opcode table for the
9362 benefit of the disassembler. We remove it before
9363 applying the actual mask. */
9364 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9365 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9367 continue;
9369 case 'e': /* extend code */
9370 my_getExpression (&imm_expr, s);
9371 check_absolute_expr (ip, &imm_expr);
9372 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9374 as_warn (_("Invalid value for `%s' (%lu)"),
9375 ip->insn_mo->name,
9376 (unsigned long) imm_expr.X_add_number);
9377 imm_expr.X_add_number &= 0x7ff;
9379 ip->insn_opcode |= imm_expr.X_add_number;
9380 imm_expr.X_op = O_absent;
9381 s = expr_end;
9382 continue;
9384 default:
9385 internalError ();
9387 break;
9390 /* Args don't match. */
9391 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9392 strcmp (insn->name, insn[1].name) == 0)
9394 ++insn;
9395 s = argsstart;
9396 continue;
9399 insn_error = _("illegal operands");
9401 return;
9405 /* This structure holds information we know about a mips16 immediate
9406 argument type. */
9408 struct mips16_immed_operand
9410 /* The type code used in the argument string in the opcode table. */
9411 int type;
9412 /* The number of bits in the short form of the opcode. */
9413 int nbits;
9414 /* The number of bits in the extended form of the opcode. */
9415 int extbits;
9416 /* The amount by which the short form is shifted when it is used;
9417 for example, the sw instruction has a shift count of 2. */
9418 int shift;
9419 /* The amount by which the short form is shifted when it is stored
9420 into the instruction code. */
9421 int op_shift;
9422 /* Non-zero if the short form is unsigned. */
9423 int unsp;
9424 /* Non-zero if the extended form is unsigned. */
9425 int extu;
9426 /* Non-zero if the value is PC relative. */
9427 int pcrel;
9430 /* The mips16 immediate operand types. */
9432 static const struct mips16_immed_operand mips16_immed_operands[] =
9434 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9435 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9436 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9437 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9438 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9439 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9440 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9441 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9442 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9443 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9444 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9445 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9446 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9447 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9448 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9449 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9450 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9451 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9452 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9453 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9454 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9457 #define MIPS16_NUM_IMMED \
9458 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9460 /* Handle a mips16 instruction with an immediate value. This or's the
9461 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9462 whether an extended value is needed; if one is needed, it sets
9463 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9464 If SMALL is true, an unextended opcode was explicitly requested.
9465 If EXT is true, an extended opcode was explicitly requested. If
9466 WARN is true, warn if EXT does not match reality. */
9468 static void
9469 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9470 extend)
9471 char *file;
9472 unsigned int line;
9473 int type;
9474 offsetT val;
9475 boolean warn;
9476 boolean small;
9477 boolean ext;
9478 unsigned long *insn;
9479 boolean *use_extend;
9480 unsigned short *extend;
9482 register const struct mips16_immed_operand *op;
9483 int mintiny, maxtiny;
9484 boolean needext;
9486 op = mips16_immed_operands;
9487 while (op->type != type)
9489 ++op;
9490 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9493 if (op->unsp)
9495 if (type == '<' || type == '>' || type == '[' || type == ']')
9497 mintiny = 1;
9498 maxtiny = 1 << op->nbits;
9500 else
9502 mintiny = 0;
9503 maxtiny = (1 << op->nbits) - 1;
9506 else
9508 mintiny = - (1 << (op->nbits - 1));
9509 maxtiny = (1 << (op->nbits - 1)) - 1;
9512 /* Branch offsets have an implicit 0 in the lowest bit. */
9513 if (type == 'p' || type == 'q')
9514 val /= 2;
9516 if ((val & ((1 << op->shift) - 1)) != 0
9517 || val < (mintiny << op->shift)
9518 || val > (maxtiny << op->shift))
9519 needext = true;
9520 else
9521 needext = false;
9523 if (warn && ext && ! needext)
9524 as_warn_where (file, line,
9525 _("extended operand requested but not required"));
9526 if (small && needext)
9527 as_bad_where (file, line, _("invalid unextended operand value"));
9529 if (small || (! ext && ! needext))
9531 int insnval;
9533 *use_extend = false;
9534 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9535 insnval <<= op->op_shift;
9536 *insn |= insnval;
9538 else
9540 long minext, maxext;
9541 int extval;
9543 if (op->extu)
9545 minext = 0;
9546 maxext = (1 << op->extbits) - 1;
9548 else
9550 minext = - (1 << (op->extbits - 1));
9551 maxext = (1 << (op->extbits - 1)) - 1;
9553 if (val < minext || val > maxext)
9554 as_bad_where (file, line,
9555 _("operand value out of range for instruction"));
9557 *use_extend = true;
9558 if (op->extbits == 16)
9560 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9561 val &= 0x1f;
9563 else if (op->extbits == 15)
9565 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9566 val &= 0xf;
9568 else
9570 extval = ((val & 0x1f) << 6) | (val & 0x20);
9571 val = 0;
9574 *extend = (unsigned short) extval;
9575 *insn |= val;
9579 static struct percent_op_match
9581 const char *str;
9582 const enum small_ex_type type;
9583 } percent_op[] =
9585 {"%lo", S_EX_LO},
9586 #ifdef OBJ_ELF
9587 {"%call_hi", S_EX_CALL_HI},
9588 {"%call_lo", S_EX_CALL_LO},
9589 {"%call16", S_EX_CALL16},
9590 {"%got_disp", S_EX_GOT_DISP},
9591 {"%got_page", S_EX_GOT_PAGE},
9592 {"%got_ofst", S_EX_GOT_OFST},
9593 {"%got_hi", S_EX_GOT_HI},
9594 {"%got_lo", S_EX_GOT_LO},
9595 {"%got", S_EX_GOT},
9596 {"%gp_rel", S_EX_GP_REL},
9597 {"%half", S_EX_HALF},
9598 {"%highest", S_EX_HIGHEST},
9599 {"%higher", S_EX_HIGHER},
9600 {"%neg", S_EX_NEG},
9601 #endif
9602 {"%hi", S_EX_HI}
9605 /* Parse small expression input. STR gets adjusted to eat up whitespace.
9606 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9607 can be nested, this is handled by blanking the innermost, parsing the
9608 rest by subsequent calls. */
9610 static int
9611 my_getSmallParser (str, len, nestlevel)
9612 char **str;
9613 unsigned int *len;
9614 int *nestlevel;
9616 *len = 0;
9617 *str += strspn (*str, " \t");
9618 /* Check for expression in parentheses. */
9619 if (**str == '(')
9621 char *b = *str + 1 + strspn (*str + 1, " \t");
9622 char *e;
9624 /* Check for base register. */
9625 if (b[0] == '$')
9627 if (strchr (b, ')')
9628 && (e = b + strcspn (b, ") \t"))
9629 && e - b > 1 && e - b < 4)
9631 if ((e - b == 3
9632 && ((b[1] == 'f' && b[2] == 'p')
9633 || (b[1] == 's' && b[2] == 'p')
9634 || (b[1] == 'g' && b[2] == 'p')
9635 || (b[1] == 'a' && b[2] == 't')
9636 || (ISDIGIT (b[1])
9637 && ISDIGIT (b[2]))))
9638 || (ISDIGIT (b[1])))
9640 *len = strcspn (*str, ")") + 1;
9641 return S_EX_REGISTER;
9645 /* Check for percent_op (in parentheses). */
9646 else if (b[0] == '%')
9648 *str = b;
9649 return my_getPercentOp (str, len, nestlevel);
9652 /* Some other expression in the parentheses, which can contain
9653 parentheses itself. Attempt to find the matching one. */
9655 int pcnt = 1;
9656 char *s;
9658 *len = 1;
9659 for (s = *str + 1; *s && pcnt; s++, (*len)++)
9661 if (*s == '(')
9662 ++pcnt;
9663 else if (*s == ')')
9664 --pcnt;
9668 /* Check for percent_op (outside of parentheses). */
9669 else if (*str[0] == '%')
9670 return my_getPercentOp (str, len, nestlevel);
9672 /* Any other expression. */
9673 return S_EX_NONE;
9676 static int
9677 my_getPercentOp (str, len, nestlevel)
9678 char **str;
9679 unsigned int *len;
9680 int *nestlevel;
9682 char *tmp = *str + 1;
9683 unsigned int i = 0;
9685 while (ISALPHA (*tmp) || *tmp == '_')
9687 *tmp = TOLOWER (*tmp);
9688 tmp++;
9690 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9692 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9693 i++;
9694 else
9696 int type = percent_op[i].type;
9698 /* Only %hi and %lo are allowed for OldABI. */
9699 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9700 return S_EX_NONE;
9702 *len = strlen (percent_op[i].str);
9703 ++(*nestlevel);
9704 return type;
9707 return S_EX_NONE;
9710 static int
9711 my_getSmallExpression (ep, str)
9712 expressionS *ep;
9713 char *str;
9715 static char *oldstr = NULL;
9716 int c = S_EX_NONE;
9717 int oldc;
9718 int nestlevel = -1;
9719 unsigned int len;
9721 /* Don't update oldstr if the last call had nested percent_op's. We need
9722 it to parse the outer ones later. */
9723 if (! oldstr)
9724 oldstr = str;
9728 oldc = c;
9729 c = my_getSmallParser (&str, &len, &nestlevel);
9730 if (c != S_EX_NONE && c != S_EX_REGISTER)
9731 str += len;
9733 while (c != S_EX_NONE && c != S_EX_REGISTER);
9735 if (nestlevel >= 0)
9737 /* A percent_op was encountered. Don't try to get an expression if
9738 it is already blanked out. */
9739 if (*(str + strspn (str + 1, " )")) != ')')
9741 char save;
9743 /* Let my_getExpression() stop at the closing parenthesis. */
9744 save = *(str + len);
9745 *(str + len) = '\0';
9746 my_getExpression (ep, str);
9747 *(str + len) = save;
9749 if (nestlevel > 0)
9751 /* Blank out including the % sign and the proper matching
9752 parenthesis. */
9753 int pcnt = 1;
9754 char *s = strrchr (oldstr, '%');
9755 char *end;
9757 for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9759 if (*end == '(')
9760 ++pcnt;
9761 else if (*end == ')')
9762 --pcnt;
9765 memset (s, ' ', end - s);
9766 str = oldstr;
9768 else
9769 expr_end = str + len;
9771 c = oldc;
9773 else if (c == S_EX_NONE)
9775 my_getExpression (ep, str);
9777 else if (c == S_EX_REGISTER)
9779 ep->X_op = O_constant;
9780 expr_end = str;
9781 ep->X_add_symbol = NULL;
9782 ep->X_op_symbol = NULL;
9783 ep->X_add_number = 0;
9785 else
9787 as_fatal (_("internal error"));
9790 if (nestlevel <= 0)
9791 /* All percent_op's have been handled. */
9792 oldstr = NULL;
9794 return c;
9797 static void
9798 my_getExpression (ep, str)
9799 expressionS *ep;
9800 char *str;
9802 char *save_in;
9803 valueT val;
9805 save_in = input_line_pointer;
9806 input_line_pointer = str;
9807 expression (ep);
9808 expr_end = input_line_pointer;
9809 input_line_pointer = save_in;
9811 /* If we are in mips16 mode, and this is an expression based on `.',
9812 then we bump the value of the symbol by 1 since that is how other
9813 text symbols are handled. We don't bother to handle complex
9814 expressions, just `.' plus or minus a constant. */
9815 if (mips_opts.mips16
9816 && ep->X_op == O_symbol
9817 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9818 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9819 && symbol_get_frag (ep->X_add_symbol) == frag_now
9820 && symbol_constant_p (ep->X_add_symbol)
9821 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9822 S_SET_VALUE (ep->X_add_symbol, val + 1);
9825 /* Turn a string in input_line_pointer into a floating point constant
9826 of type TYPE, and store the appropriate bytes in *LITP. The number
9827 of LITTLENUMS emitted is stored in *SIZEP. An error message is
9828 returned, or NULL on OK. */
9830 char *
9831 md_atof (type, litP, sizeP)
9832 int type;
9833 char *litP;
9834 int *sizeP;
9836 int prec;
9837 LITTLENUM_TYPE words[4];
9838 char *t;
9839 int i;
9841 switch (type)
9843 case 'f':
9844 prec = 2;
9845 break;
9847 case 'd':
9848 prec = 4;
9849 break;
9851 default:
9852 *sizeP = 0;
9853 return _("bad call to md_atof");
9856 t = atof_ieee (input_line_pointer, type, words);
9857 if (t)
9858 input_line_pointer = t;
9860 *sizeP = prec * 2;
9862 if (! target_big_endian)
9864 for (i = prec - 1; i >= 0; i--)
9866 md_number_to_chars (litP, (valueT) words[i], 2);
9867 litP += 2;
9870 else
9872 for (i = 0; i < prec; i++)
9874 md_number_to_chars (litP, (valueT) words[i], 2);
9875 litP += 2;
9879 return NULL;
9882 void
9883 md_number_to_chars (buf, val, n)
9884 char *buf;
9885 valueT val;
9886 int n;
9888 if (target_big_endian)
9889 number_to_chars_bigendian (buf, val, n);
9890 else
9891 number_to_chars_littleendian (buf, val, n);
9894 #ifdef OBJ_ELF
9895 static int support_64bit_objects(void)
9897 const char **list, **l;
9899 list = bfd_target_list ();
9900 for (l = list; *l != NULL; l++)
9901 #ifdef TE_TMIPS
9902 /* This is traditional mips */
9903 if (strcmp (*l, "elf64-tradbigmips") == 0
9904 || strcmp (*l, "elf64-tradlittlemips") == 0)
9905 #else
9906 if (strcmp (*l, "elf64-bigmips") == 0
9907 || strcmp (*l, "elf64-littlemips") == 0)
9908 #endif
9909 break;
9910 free (list);
9911 return (*l != NULL);
9913 #endif /* OBJ_ELF */
9915 const char *md_shortopts = "nO::g::G:";
9917 struct option md_longopts[] =
9919 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9920 {"mips0", no_argument, NULL, OPTION_MIPS1},
9921 {"mips1", no_argument, NULL, OPTION_MIPS1},
9922 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9923 {"mips2", no_argument, NULL, OPTION_MIPS2},
9924 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9925 {"mips3", no_argument, NULL, OPTION_MIPS3},
9926 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9927 {"mips4", no_argument, NULL, OPTION_MIPS4},
9928 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9929 {"mips5", no_argument, NULL, OPTION_MIPS5},
9930 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9931 {"mips32", no_argument, NULL, OPTION_MIPS32},
9932 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9933 {"mips64", no_argument, NULL, OPTION_MIPS64},
9934 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9935 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9936 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9937 {"trap", no_argument, NULL, OPTION_TRAP},
9938 {"no-break", no_argument, NULL, OPTION_TRAP},
9939 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9940 {"break", no_argument, NULL, OPTION_BREAK},
9941 {"no-trap", no_argument, NULL, OPTION_BREAK},
9942 #define OPTION_EB (OPTION_MD_BASE + 11)
9943 {"EB", no_argument, NULL, OPTION_EB},
9944 #define OPTION_EL (OPTION_MD_BASE + 12)
9945 {"EL", no_argument, NULL, OPTION_EL},
9946 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9947 {"mips16", no_argument, NULL, OPTION_MIPS16},
9948 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9949 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9950 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9951 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9952 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
9953 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9954 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9955 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9956 {"mfp32", no_argument, NULL, OPTION_FP32},
9957 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9958 {"mgp32", no_argument, NULL, OPTION_GP32},
9959 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9960 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9961 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9962 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9963 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9964 {"march", required_argument, NULL, OPTION_MARCH},
9965 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9966 {"mtune", required_argument, NULL, OPTION_MTUNE},
9967 #define OPTION_FP64 (OPTION_MD_BASE + 23)
9968 {"mfp64", no_argument, NULL, OPTION_FP64},
9969 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9970 {"m4650", no_argument, NULL, OPTION_M4650},
9971 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9972 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9973 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9974 {"m4010", no_argument, NULL, OPTION_M4010},
9975 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9976 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9977 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9978 {"m4100", no_argument, NULL, OPTION_M4100},
9979 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9980 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9981 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9982 {"m3900", no_argument, NULL, OPTION_M3900},
9983 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9984 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9985 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9986 {"mgp64", no_argument, NULL, OPTION_GP64},
9987 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9988 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9989 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9990 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
9991 #define OPTION_MDMX (OPTION_MD_BASE + 35)
9992 {"mdmx", no_argument, NULL, OPTION_MDMX},
9993 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
9994 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
9995 #ifdef OBJ_ELF
9996 #define OPTION_ELF_BASE (OPTION_MD_BASE + 37)
9997 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9998 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
9999 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10000 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10001 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10002 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10003 {"xgot", no_argument, NULL, OPTION_XGOT},
10004 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10005 {"mabi", required_argument, NULL, OPTION_MABI},
10006 #define OPTION_32 (OPTION_ELF_BASE + 4)
10007 {"32", no_argument, NULL, OPTION_32},
10008 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10009 {"n32", no_argument, NULL, OPTION_N32},
10010 #define OPTION_64 (OPTION_ELF_BASE + 6)
10011 {"64", no_argument, NULL, OPTION_64},
10012 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10013 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10014 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10015 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10016 #endif /* OBJ_ELF */
10017 {NULL, no_argument, NULL, 0}
10019 size_t md_longopts_size = sizeof (md_longopts);
10021 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10022 NEW_VALUE. Warn if another value was already specified. Note:
10023 we have to defer parsing the -march and -mtune arguments in order
10024 to handle 'from-abi' correctly, since the ABI might be specified
10025 in a later argument. */
10027 static void
10028 mips_set_option_string (string_ptr, new_value)
10029 const char **string_ptr, *new_value;
10031 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10032 as_warn (_("A different %s was already specified, is now %s"),
10033 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10034 new_value);
10036 *string_ptr = new_value;
10040 md_parse_option (c, arg)
10041 int c;
10042 char *arg;
10044 switch (c)
10046 case OPTION_CONSTRUCT_FLOATS:
10047 mips_disable_float_construction = 0;
10048 break;
10050 case OPTION_NO_CONSTRUCT_FLOATS:
10051 mips_disable_float_construction = 1;
10052 break;
10054 case OPTION_TRAP:
10055 mips_trap = 1;
10056 break;
10058 case OPTION_BREAK:
10059 mips_trap = 0;
10060 break;
10062 case OPTION_EB:
10063 target_big_endian = 1;
10064 break;
10066 case OPTION_EL:
10067 target_big_endian = 0;
10068 break;
10070 case 'n':
10071 warn_nops = 1;
10072 break;
10074 case 'O':
10075 if (arg && arg[1] == '0')
10076 mips_optimize = 1;
10077 else
10078 mips_optimize = 2;
10079 break;
10081 case 'g':
10082 if (arg == NULL)
10083 mips_debug = 2;
10084 else
10085 mips_debug = atoi (arg);
10086 /* When the MIPS assembler sees -g or -g2, it does not do
10087 optimizations which limit full symbolic debugging. We take
10088 that to be equivalent to -O0. */
10089 if (mips_debug == 2)
10090 mips_optimize = 1;
10091 break;
10093 case OPTION_MIPS1:
10094 file_mips_isa = ISA_MIPS1;
10095 break;
10097 case OPTION_MIPS2:
10098 file_mips_isa = ISA_MIPS2;
10099 break;
10101 case OPTION_MIPS3:
10102 file_mips_isa = ISA_MIPS3;
10103 break;
10105 case OPTION_MIPS4:
10106 file_mips_isa = ISA_MIPS4;
10107 break;
10109 case OPTION_MIPS5:
10110 file_mips_isa = ISA_MIPS5;
10111 break;
10113 case OPTION_MIPS32:
10114 file_mips_isa = ISA_MIPS32;
10115 break;
10117 case OPTION_MIPS64:
10118 file_mips_isa = ISA_MIPS64;
10119 break;
10121 case OPTION_MTUNE:
10122 mips_set_option_string (&mips_tune_string, arg);
10123 break;
10125 case OPTION_MARCH:
10126 mips_set_option_string (&mips_arch_string, arg);
10127 break;
10129 case OPTION_M4650:
10130 mips_set_option_string (&mips_arch_string, "4650");
10131 mips_set_option_string (&mips_tune_string, "4650");
10132 break;
10134 case OPTION_NO_M4650:
10135 break;
10137 case OPTION_M4010:
10138 mips_set_option_string (&mips_arch_string, "4010");
10139 mips_set_option_string (&mips_tune_string, "4010");
10140 break;
10142 case OPTION_NO_M4010:
10143 break;
10145 case OPTION_M4100:
10146 mips_set_option_string (&mips_arch_string, "4100");
10147 mips_set_option_string (&mips_tune_string, "4100");
10148 break;
10150 case OPTION_NO_M4100:
10151 break;
10153 case OPTION_M3900:
10154 mips_set_option_string (&mips_arch_string, "3900");
10155 mips_set_option_string (&mips_tune_string, "3900");
10156 break;
10158 case OPTION_NO_M3900:
10159 break;
10161 case OPTION_MDMX:
10162 mips_opts.ase_mdmx = 1;
10163 break;
10165 case OPTION_NO_MDMX:
10166 mips_opts.ase_mdmx = 0;
10167 break;
10169 case OPTION_MIPS16:
10170 mips_opts.mips16 = 1;
10171 mips_no_prev_insn (false);
10172 break;
10174 case OPTION_NO_MIPS16:
10175 mips_opts.mips16 = 0;
10176 mips_no_prev_insn (false);
10177 break;
10179 case OPTION_MIPS3D:
10180 mips_opts.ase_mips3d = 1;
10181 break;
10183 case OPTION_NO_MIPS3D:
10184 mips_opts.ase_mips3d = 0;
10185 break;
10187 case OPTION_MEMBEDDED_PIC:
10188 mips_pic = EMBEDDED_PIC;
10189 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10191 as_bad (_("-G may not be used with embedded PIC code"));
10192 return 0;
10194 g_switch_value = 0x7fffffff;
10195 break;
10197 #ifdef OBJ_ELF
10198 /* When generating ELF code, we permit -KPIC and -call_shared to
10199 select SVR4_PIC, and -non_shared to select no PIC. This is
10200 intended to be compatible with Irix 5. */
10201 case OPTION_CALL_SHARED:
10202 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10204 as_bad (_("-call_shared is supported only for ELF format"));
10205 return 0;
10207 mips_pic = SVR4_PIC;
10208 if (g_switch_seen && g_switch_value != 0)
10210 as_bad (_("-G may not be used with SVR4 PIC code"));
10211 return 0;
10213 g_switch_value = 0;
10214 break;
10216 case OPTION_NON_SHARED:
10217 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10219 as_bad (_("-non_shared is supported only for ELF format"));
10220 return 0;
10222 mips_pic = NO_PIC;
10223 break;
10225 /* The -xgot option tells the assembler to use 32 offsets when
10226 accessing the got in SVR4_PIC mode. It is for Irix
10227 compatibility. */
10228 case OPTION_XGOT:
10229 mips_big_got = 1;
10230 break;
10231 #endif /* OBJ_ELF */
10233 case 'G':
10234 if (! USE_GLOBAL_POINTER_OPT)
10236 as_bad (_("-G is not supported for this configuration"));
10237 return 0;
10239 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10241 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10242 return 0;
10244 else
10245 g_switch_value = atoi (arg);
10246 g_switch_seen = 1;
10247 break;
10249 #ifdef OBJ_ELF
10250 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10251 and -mabi=64. */
10252 case OPTION_32:
10253 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10255 as_bad (_("-32 is supported for ELF format only"));
10256 return 0;
10258 mips_abi = O32_ABI;
10259 break;
10261 case OPTION_N32:
10262 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10264 as_bad (_("-n32 is supported for ELF format only"));
10265 return 0;
10267 mips_abi = N32_ABI;
10268 break;
10270 case OPTION_64:
10271 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10273 as_bad (_("-64 is supported for ELF format only"));
10274 return 0;
10276 mips_abi = N64_ABI;
10277 if (! support_64bit_objects())
10278 as_fatal (_("No compiled in support for 64 bit object file format"));
10279 break;
10280 #endif /* OBJ_ELF */
10282 case OPTION_GP32:
10283 file_mips_gp32 = 1;
10284 break;
10286 case OPTION_GP64:
10287 file_mips_gp32 = 0;
10288 break;
10290 case OPTION_FP32:
10291 file_mips_fp32 = 1;
10292 break;
10294 case OPTION_FP64:
10295 file_mips_fp32 = 0;
10296 break;
10298 #ifdef OBJ_ELF
10299 case OPTION_MABI:
10300 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10302 as_bad (_("-mabi is supported for ELF format only"));
10303 return 0;
10305 if (strcmp (arg, "32") == 0)
10306 mips_abi = O32_ABI;
10307 else if (strcmp (arg, "o64") == 0)
10308 mips_abi = O64_ABI;
10309 else if (strcmp (arg, "n32") == 0)
10310 mips_abi = N32_ABI;
10311 else if (strcmp (arg, "64") == 0)
10313 mips_abi = N64_ABI;
10314 if (! support_64bit_objects())
10315 as_fatal (_("No compiled in support for 64 bit object file "
10316 "format"));
10318 else if (strcmp (arg, "eabi") == 0)
10319 mips_abi = EABI_ABI;
10320 else
10322 as_fatal (_("invalid abi -mabi=%s"), arg);
10323 return 0;
10325 break;
10326 #endif /* OBJ_ELF */
10328 case OPTION_M7000_HILO_FIX:
10329 mips_7000_hilo_fix = true;
10330 break;
10332 case OPTION_MNO_7000_HILO_FIX:
10333 mips_7000_hilo_fix = false;
10334 break;
10336 #ifdef OBJ_ELF
10337 case OPTION_MDEBUG:
10338 mips_flag_mdebug = true;
10339 break;
10341 case OPTION_NO_MDEBUG:
10342 mips_flag_mdebug = false;
10343 break;
10344 #endif /* OBJ_ELF */
10346 default:
10347 return 0;
10350 return 1;
10353 /* Set up globals to generate code for the ISA or processor
10354 described by INFO. */
10356 static void
10357 mips_set_architecture (info)
10358 const struct mips_cpu_info *info;
10360 if (info != 0)
10362 mips_arch_info = info;
10363 mips_arch = info->cpu;
10364 mips_opts.isa = info->isa;
10369 /* Likewise for tuning. */
10371 static void
10372 mips_set_tune (info)
10373 const struct mips_cpu_info *info;
10375 if (info != 0)
10377 mips_tune_info = info;
10378 mips_tune = info->cpu;
10383 void
10384 mips_after_parse_args ()
10386 /* GP relative stuff not working for PE */
10387 if (strncmp (TARGET_OS, "pe", 2) == 0
10388 && g_switch_value != 0)
10390 if (g_switch_seen)
10391 as_bad (_("-G not supported in this configuration."));
10392 g_switch_value = 0;
10395 /* The following code determines the architecture and register size.
10396 Similar code was added to GCC 3.3 (see override_options() in
10397 config/mips/mips.c). The GAS and GCC code should be kept in sync
10398 as much as possible. */
10400 if (mips_arch_string != 0)
10401 mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10403 if (mips_tune_string != 0)
10404 mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10406 if (file_mips_isa != ISA_UNKNOWN)
10408 /* Handle -mipsN. At this point, file_mips_isa contains the
10409 ISA level specified by -mipsN, while mips_opts.isa contains
10410 the -march selection (if any). */
10411 if (mips_arch_info != 0)
10413 /* -march takes precedence over -mipsN, since it is more descriptive.
10414 There's no harm in specifying both as long as the ISA levels
10415 are the same. */
10416 if (file_mips_isa != mips_opts.isa)
10417 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10418 mips_cpu_info_from_isa (file_mips_isa)->name,
10419 mips_cpu_info_from_isa (mips_opts.isa)->name);
10421 else
10422 mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10425 if (mips_arch_info == 0)
10426 mips_set_architecture (mips_parse_cpu ("default CPU",
10427 MIPS_CPU_STRING_DEFAULT));
10429 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10430 as_bad ("-march=%s is not compatible with the selected ABI",
10431 mips_arch_info->name);
10433 /* Optimize for mips_arch, unless -mtune selects a different processor. */
10434 if (mips_tune_info == 0)
10435 mips_set_tune (mips_arch_info);
10437 if (file_mips_gp32 >= 0)
10439 /* The user specified the size of the integer registers. Make sure
10440 it agrees with the ABI and ISA. */
10441 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10442 as_bad (_("-mgp64 used with a 32-bit processor"));
10443 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10444 as_bad (_("-mgp32 used with a 64-bit ABI"));
10445 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10446 as_bad (_("-mgp64 used with a 32-bit ABI"));
10448 else
10450 /* Infer the integer register size from the ABI and processor.
10451 Restrict ourselves to 32-bit registers if that's all the
10452 processor has, or if the ABI cannot handle 64-bit registers. */
10453 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10454 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10457 /* ??? GAS treats single-float processors as though they had 64-bit
10458 float registers (although it complains when double-precision
10459 instructions are used). As things stand, saying they have 32-bit
10460 registers would lead to spurious "register must be even" messages.
10461 So here we assume float registers are always the same size as
10462 integer ones, unless the user says otherwise. */
10463 if (file_mips_fp32 < 0)
10464 file_mips_fp32 = file_mips_gp32;
10466 /* End of GCC-shared inference code. */
10468 /* ??? When do we want this flag to be set? Who uses it? */
10469 if (file_mips_gp32 == 1
10470 && mips_abi == NO_ABI
10471 && ISA_HAS_64BIT_REGS (mips_opts.isa))
10472 mips_32bitmode = 1;
10474 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10475 as_bad (_("trap exception not supported at ISA 1"));
10477 /* If the selected architecture includes support for ASEs, enable
10478 generation of code for them. */
10479 if (mips_opts.mips16 == -1)
10480 mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10481 if (mips_opts.ase_mips3d == -1)
10482 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10483 if (mips_opts.ase_mdmx == -1)
10484 mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10486 file_mips_isa = mips_opts.isa;
10487 file_ase_mips16 = mips_opts.mips16;
10488 file_ase_mips3d = mips_opts.ase_mips3d;
10489 file_ase_mdmx = mips_opts.ase_mdmx;
10490 mips_opts.gp32 = file_mips_gp32;
10491 mips_opts.fp32 = file_mips_fp32;
10493 if (mips_flag_mdebug < 0)
10495 #ifdef OBJ_MAYBE_ECOFF
10496 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10497 mips_flag_mdebug = 1;
10498 else
10499 #endif /* OBJ_MAYBE_ECOFF */
10500 mips_flag_mdebug = 0;
10504 void
10505 mips_init_after_args ()
10507 /* initialize opcodes */
10508 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10509 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10512 long
10513 md_pcrel_from (fixP)
10514 fixS *fixP;
10516 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10517 && fixP->fx_addsy != (symbolS *) NULL
10518 && ! S_IS_DEFINED (fixP->fx_addsy))
10520 /* This makes a branch to an undefined symbol be a branch to the
10521 current location. */
10522 if (mips_pic == EMBEDDED_PIC)
10523 return 4;
10524 else
10525 return 1;
10528 /* Return the address of the delay slot. */
10529 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10532 /* This is called before the symbol table is processed. In order to
10533 work with gcc when using mips-tfile, we must keep all local labels.
10534 However, in other cases, we want to discard them. If we were
10535 called with -g, but we didn't see any debugging information, it may
10536 mean that gcc is smuggling debugging information through to
10537 mips-tfile, in which case we must generate all local labels. */
10539 void
10540 mips_frob_file_before_adjust ()
10542 #ifndef NO_ECOFF_DEBUGGING
10543 if (ECOFF_DEBUGGING
10544 && mips_debug != 0
10545 && ! ecoff_debugging_seen)
10546 flag_keep_locals = 1;
10547 #endif
10550 /* Sort any unmatched HI16_S relocs so that they immediately precede
10551 the corresponding LO reloc. This is called before md_apply_fix3 and
10552 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10553 explicit use of the %hi modifier. */
10555 void
10556 mips_frob_file ()
10558 struct mips_hi_fixup *l;
10560 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10562 segment_info_type *seginfo;
10563 int pass;
10565 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10567 /* Check quickly whether the next fixup happens to be a matching
10568 %lo. */
10569 if (l->fixp->fx_next != NULL
10570 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10571 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10572 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10573 continue;
10575 /* Look through the fixups for this segment for a matching %lo.
10576 When we find one, move the %hi just in front of it. We do
10577 this in two passes. In the first pass, we try to find a
10578 unique %lo. In the second pass, we permit multiple %hi
10579 relocs for a single %lo (this is a GNU extension). */
10580 seginfo = seg_info (l->seg);
10581 for (pass = 0; pass < 2; pass++)
10583 fixS *f, *prev;
10585 prev = NULL;
10586 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10588 /* Check whether this is a %lo fixup which matches l->fixp. */
10589 if (f->fx_r_type == BFD_RELOC_LO16
10590 && f->fx_addsy == l->fixp->fx_addsy
10591 && f->fx_offset == l->fixp->fx_offset
10592 && (pass == 1
10593 || prev == NULL
10594 || prev->fx_r_type != BFD_RELOC_HI16_S
10595 || prev->fx_addsy != f->fx_addsy
10596 || prev->fx_offset != f->fx_offset))
10598 fixS **pf;
10600 /* Move l->fixp before f. */
10601 for (pf = &seginfo->fix_root;
10602 *pf != l->fixp;
10603 pf = &(*pf)->fx_next)
10604 assert (*pf != NULL);
10606 *pf = l->fixp->fx_next;
10608 l->fixp->fx_next = f;
10609 if (prev == NULL)
10610 seginfo->fix_root = l->fixp;
10611 else
10612 prev->fx_next = l->fixp;
10614 break;
10617 prev = f;
10620 if (f != NULL)
10621 break;
10623 #if 0 /* GCC code motion plus incomplete dead code elimination
10624 can leave a %hi without a %lo. */
10625 if (pass == 1)
10626 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10627 _("Unmatched %%hi reloc"));
10628 #endif
10633 /* When generating embedded PIC code we need to use a special
10634 relocation to represent the difference of two symbols in the .text
10635 section (switch tables use a difference of this sort). See
10636 include/coff/mips.h for details. This macro checks whether this
10637 fixup requires the special reloc. */
10638 #define SWITCH_TABLE(fixp) \
10639 ((fixp)->fx_r_type == BFD_RELOC_32 \
10640 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10641 && (fixp)->fx_addsy != NULL \
10642 && (fixp)->fx_subsy != NULL \
10643 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10644 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10646 /* When generating embedded PIC code we must keep all PC relative
10647 relocations, in case the linker has to relax a call. We also need
10648 to keep relocations for switch table entries.
10650 We may have combined relocations without symbols in the N32/N64 ABI.
10651 We have to prevent gas from dropping them. */
10654 mips_force_relocation (fixp)
10655 fixS *fixp;
10657 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10658 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10659 || S_FORCE_RELOC (fixp->fx_addsy))
10660 return 1;
10662 if (HAVE_NEWABI
10663 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10664 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10665 || fixp->fx_r_type == BFD_RELOC_HI16_S
10666 || fixp->fx_r_type == BFD_RELOC_LO16))
10667 return 1;
10669 return (mips_pic == EMBEDDED_PIC
10670 && (fixp->fx_pcrel
10671 || SWITCH_TABLE (fixp)
10672 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10673 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10676 #ifdef OBJ_ELF
10677 static int
10678 mips_need_elf_addend_fixup (fixP)
10679 fixS *fixP;
10681 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10682 return 1;
10683 if (mips_pic == EMBEDDED_PIC
10684 && S_IS_WEAK (fixP->fx_addsy))
10685 return 1;
10686 if (mips_pic != EMBEDDED_PIC
10687 && (S_IS_WEAK (fixP->fx_addsy)
10688 || S_IS_EXTERNAL (fixP->fx_addsy))
10689 && !S_IS_COMMON (fixP->fx_addsy))
10690 return 1;
10691 if (symbol_used_in_reloc_p (fixP->fx_addsy)
10692 && (((bfd_get_section_flags (stdoutput,
10693 S_GET_SEGMENT (fixP->fx_addsy))
10694 & SEC_LINK_ONCE) != 0)
10695 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10696 ".gnu.linkonce",
10697 sizeof (".gnu.linkonce") - 1)))
10698 return 1;
10699 return 0;
10701 #endif
10703 /* Apply a fixup to the object file. */
10705 void
10706 md_apply_fix3 (fixP, valP, seg)
10707 fixS *fixP;
10708 valueT *valP;
10709 segT seg ATTRIBUTE_UNUSED;
10711 bfd_byte *buf;
10712 long insn;
10713 valueT value;
10714 static int previous_fx_r_type = 0;
10716 /* FIXME: Maybe just return for all reloc types not listed below?
10717 Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
10718 if (fixP->fx_r_type == BFD_RELOC_8)
10719 return;
10721 assert (fixP->fx_size == 4
10722 || fixP->fx_r_type == BFD_RELOC_16
10723 || fixP->fx_r_type == BFD_RELOC_32
10724 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10725 || fixP->fx_r_type == BFD_RELOC_HI16_S
10726 || fixP->fx_r_type == BFD_RELOC_LO16
10727 || fixP->fx_r_type == BFD_RELOC_GPREL16
10728 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10729 || fixP->fx_r_type == BFD_RELOC_GPREL32
10730 || fixP->fx_r_type == BFD_RELOC_64
10731 || fixP->fx_r_type == BFD_RELOC_CTOR
10732 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10733 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10734 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10735 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10736 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10737 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10738 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10739 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10740 || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
10742 value = *valP;
10744 /* If we aren't adjusting this fixup to be against the section
10745 symbol, we need to adjust the value. */
10746 #ifdef OBJ_ELF
10747 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10749 if (mips_need_elf_addend_fixup (fixP))
10751 reloc_howto_type *howto;
10752 valueT symval = S_GET_VALUE (fixP->fx_addsy);
10754 value -= symval;
10756 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10757 if (value != 0 && howto->partial_inplace
10758 && (! fixP->fx_pcrel || howto->pcrel_offset))
10760 /* In this case, the bfd_install_relocation routine will
10761 incorrectly add the symbol value back in. We just want
10762 the addend to appear in the object file.
10764 howto->pcrel_offset is added for R_MIPS_PC16, which is
10765 generated for code like
10767 globl g1 .text
10768 .text
10769 .space 20
10772 bal g1
10774 value -= symval;
10776 /* Make sure the addend is still non-zero. If it became zero
10777 after the last operation, set it to a spurious value and
10778 subtract the same value from the object file's contents. */
10779 if (value == 0)
10781 value = 8;
10783 /* The in-place addends for LO16 relocations are signed;
10784 leave the matching HI16 in-place addends as zero. */
10785 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10787 bfd_vma contents, mask, field;
10789 contents = bfd_get_bits (fixP->fx_frag->fr_literal
10790 + fixP->fx_where,
10791 fixP->fx_size * 8,
10792 target_big_endian);
10794 /* MASK has bits set where the relocation should go.
10795 FIELD is -value, shifted into the appropriate place
10796 for this relocation. */
10797 mask = 1 << (howto->bitsize - 1);
10798 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10799 field = (-value >> howto->rightshift) << howto->bitpos;
10801 bfd_put_bits ((field & mask) | (contents & ~mask),
10802 fixP->fx_frag->fr_literal + fixP->fx_where,
10803 fixP->fx_size * 8,
10804 target_big_endian);
10810 /* This code was generated using trial and error and so is
10811 fragile and not trustworthy. If you change it, you should
10812 rerun the elf-rel, elf-rel2, and empic testcases and ensure
10813 they still pass. */
10814 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10816 value += fixP->fx_frag->fr_address + fixP->fx_where;
10818 /* BFD's REL handling, for MIPS, is _very_ weird.
10819 This gives the right results, but it can't possibly
10820 be the way things are supposed to work. */
10821 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10822 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10823 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10824 value += fixP->fx_frag->fr_address + fixP->fx_where;
10827 #endif
10829 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
10831 /* We are not done if this is a composite relocation to set up gp. */
10832 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
10833 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10834 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
10835 && (fixP->fx_r_type == BFD_RELOC_HI16_S
10836 || fixP->fx_r_type == BFD_RELOC_LO16))))
10837 fixP->fx_done = 1;
10838 previous_fx_r_type = fixP->fx_r_type;
10840 switch (fixP->fx_r_type)
10842 case BFD_RELOC_MIPS_JMP:
10843 case BFD_RELOC_MIPS_SHIFT5:
10844 case BFD_RELOC_MIPS_SHIFT6:
10845 case BFD_RELOC_MIPS_GOT_DISP:
10846 case BFD_RELOC_MIPS_GOT_PAGE:
10847 case BFD_RELOC_MIPS_GOT_OFST:
10848 case BFD_RELOC_MIPS_SUB:
10849 case BFD_RELOC_MIPS_INSERT_A:
10850 case BFD_RELOC_MIPS_INSERT_B:
10851 case BFD_RELOC_MIPS_DELETE:
10852 case BFD_RELOC_MIPS_HIGHEST:
10853 case BFD_RELOC_MIPS_HIGHER:
10854 case BFD_RELOC_MIPS_SCN_DISP:
10855 case BFD_RELOC_MIPS_REL16:
10856 case BFD_RELOC_MIPS_RELGOT:
10857 case BFD_RELOC_MIPS_JALR:
10858 case BFD_RELOC_HI16:
10859 case BFD_RELOC_HI16_S:
10860 case BFD_RELOC_GPREL16:
10861 case BFD_RELOC_MIPS_LITERAL:
10862 case BFD_RELOC_MIPS_CALL16:
10863 case BFD_RELOC_MIPS_GOT16:
10864 case BFD_RELOC_GPREL32:
10865 case BFD_RELOC_MIPS_GOT_HI16:
10866 case BFD_RELOC_MIPS_GOT_LO16:
10867 case BFD_RELOC_MIPS_CALL_HI16:
10868 case BFD_RELOC_MIPS_CALL_LO16:
10869 case BFD_RELOC_MIPS16_GPREL:
10870 if (fixP->fx_pcrel)
10871 as_bad_where (fixP->fx_file, fixP->fx_line,
10872 _("Invalid PC relative reloc"));
10873 /* Nothing needed to do. The value comes from the reloc entry */
10874 break;
10876 case BFD_RELOC_MIPS16_JMP:
10877 /* We currently always generate a reloc against a symbol, which
10878 means that we don't want an addend even if the symbol is
10879 defined. */
10880 fixP->fx_addnumber = 0;
10881 break;
10883 case BFD_RELOC_PCREL_HI16_S:
10884 /* The addend for this is tricky if it is internal, so we just
10885 do everything here rather than in bfd_install_relocation. */
10886 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10887 && !fixP->fx_done
10888 && value != 0)
10889 break;
10890 if (fixP->fx_addsy
10891 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10893 /* For an external symbol adjust by the address to make it
10894 pcrel_offset. We use the address of the RELLO reloc
10895 which follows this one. */
10896 value += (fixP->fx_next->fx_frag->fr_address
10897 + fixP->fx_next->fx_where);
10899 value = ((value + 0x8000) >> 16) & 0xffff;
10900 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10901 if (target_big_endian)
10902 buf += 2;
10903 md_number_to_chars ((char *) buf, value, 2);
10904 break;
10906 case BFD_RELOC_PCREL_LO16:
10907 /* The addend for this is tricky if it is internal, so we just
10908 do everything here rather than in bfd_install_relocation. */
10909 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10910 && !fixP->fx_done
10911 && value != 0)
10912 break;
10913 if (fixP->fx_addsy
10914 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10915 value += fixP->fx_frag->fr_address + fixP->fx_where;
10916 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10917 if (target_big_endian)
10918 buf += 2;
10919 md_number_to_chars ((char *) buf, value, 2);
10920 break;
10922 case BFD_RELOC_64:
10923 /* This is handled like BFD_RELOC_32, but we output a sign
10924 extended value if we are only 32 bits. */
10925 if (fixP->fx_done
10926 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10928 if (8 <= sizeof (valueT))
10929 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10930 value, 8);
10931 else
10933 long w1, w2;
10934 long hiv;
10936 w1 = w2 = fixP->fx_where;
10937 if (target_big_endian)
10938 w1 += 4;
10939 else
10940 w2 += 4;
10941 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10942 if ((value & 0x80000000) != 0)
10943 hiv = 0xffffffff;
10944 else
10945 hiv = 0;
10946 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10949 break;
10951 case BFD_RELOC_RVA:
10952 case BFD_RELOC_32:
10953 /* If we are deleting this reloc entry, we must fill in the
10954 value now. This can happen if we have a .word which is not
10955 resolved when it appears but is later defined. We also need
10956 to fill in the value if this is an embedded PIC switch table
10957 entry. */
10958 if (fixP->fx_done
10959 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10960 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10961 value, 4);
10962 break;
10964 case BFD_RELOC_16:
10965 /* If we are deleting this reloc entry, we must fill in the
10966 value now. */
10967 assert (fixP->fx_size == 2);
10968 if (fixP->fx_done)
10969 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10970 value, 2);
10971 break;
10973 case BFD_RELOC_LO16:
10974 /* When handling an embedded PIC switch statement, we can wind
10975 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
10976 if (fixP->fx_done)
10978 if (value + 0x8000 > 0xffff)
10979 as_bad_where (fixP->fx_file, fixP->fx_line,
10980 _("relocation overflow"));
10981 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10982 if (target_big_endian)
10983 buf += 2;
10984 md_number_to_chars ((char *) buf, value, 2);
10986 break;
10988 case BFD_RELOC_16_PCREL_S2:
10989 if ((value & 0x3) != 0)
10990 as_bad_where (fixP->fx_file, fixP->fx_line,
10991 _("Branch to odd address (%lx)"), (long) value);
10993 /* Fall through. */
10995 case BFD_RELOC_16_PCREL:
10997 * We need to save the bits in the instruction since fixup_segment()
10998 * might be deleting the relocation entry (i.e., a branch within
10999 * the current segment).
11001 if (!fixP->fx_done && value != 0)
11002 break;
11003 /* If 'value' is zero, the remaining reloc code won't actually
11004 do the store, so it must be done here. This is probably
11005 a bug somewhere. */
11006 if (!fixP->fx_done
11007 && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11008 || fixP->fx_addsy == NULL /* ??? */
11009 || ! S_IS_DEFINED (fixP->fx_addsy)))
11010 value -= fixP->fx_frag->fr_address + fixP->fx_where;
11012 value = (offsetT) value >> 2;
11014 /* update old instruction data */
11015 buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11016 if (target_big_endian)
11017 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11018 else
11019 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11021 if (value + 0x8000 <= 0xffff)
11022 insn |= value & 0xffff;
11023 else
11025 /* The branch offset is too large. If this is an
11026 unconditional branch, and we are not generating PIC code,
11027 we can convert it to an absolute jump instruction. */
11028 if (mips_pic == NO_PIC
11029 && fixP->fx_done
11030 && fixP->fx_frag->fr_address >= text_section->vma
11031 && (fixP->fx_frag->fr_address
11032 < text_section->vma + text_section->_raw_size)
11033 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11034 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11035 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11037 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11038 insn = 0x0c000000; /* jal */
11039 else
11040 insn = 0x08000000; /* j */
11041 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11042 fixP->fx_done = 0;
11043 fixP->fx_addsy = section_symbol (text_section);
11044 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11046 else
11048 /* FIXME. It would be possible in principle to handle
11049 conditional branches which overflow. They could be
11050 transformed into a branch around a jump. This would
11051 require setting up variant frags for each different
11052 branch type. The native MIPS assembler attempts to
11053 handle these cases, but it appears to do it
11054 incorrectly. */
11055 as_bad_where (fixP->fx_file, fixP->fx_line,
11056 _("Branch out of range"));
11060 md_number_to_chars ((char *) buf, (valueT) insn, 4);
11061 break;
11063 case BFD_RELOC_VTABLE_INHERIT:
11064 fixP->fx_done = 0;
11065 if (fixP->fx_addsy
11066 && !S_IS_DEFINED (fixP->fx_addsy)
11067 && !S_IS_WEAK (fixP->fx_addsy))
11068 S_SET_WEAK (fixP->fx_addsy);
11069 break;
11071 case BFD_RELOC_VTABLE_ENTRY:
11072 fixP->fx_done = 0;
11073 break;
11075 default:
11076 internalError ();
11080 #if 0
11081 void
11082 printInsn (oc)
11083 unsigned long oc;
11085 const struct mips_opcode *p;
11086 int treg, sreg, dreg, shamt;
11087 short imm;
11088 const char *args;
11089 int i;
11091 for (i = 0; i < NUMOPCODES; ++i)
11093 p = &mips_opcodes[i];
11094 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11096 printf ("%08lx %s\t", oc, p->name);
11097 treg = (oc >> 16) & 0x1f;
11098 sreg = (oc >> 21) & 0x1f;
11099 dreg = (oc >> 11) & 0x1f;
11100 shamt = (oc >> 6) & 0x1f;
11101 imm = oc;
11102 for (args = p->args;; ++args)
11104 switch (*args)
11106 case '\0':
11107 printf ("\n");
11108 break;
11110 case ',':
11111 case '(':
11112 case ')':
11113 printf ("%c", *args);
11114 continue;
11116 case 'r':
11117 assert (treg == sreg);
11118 printf ("$%d,$%d", treg, sreg);
11119 continue;
11121 case 'd':
11122 case 'G':
11123 printf ("$%d", dreg);
11124 continue;
11126 case 't':
11127 case 'E':
11128 printf ("$%d", treg);
11129 continue;
11131 case 'k':
11132 printf ("0x%x", treg);
11133 continue;
11135 case 'b':
11136 case 's':
11137 printf ("$%d", sreg);
11138 continue;
11140 case 'a':
11141 printf ("0x%08lx", oc & 0x1ffffff);
11142 continue;
11144 case 'i':
11145 case 'j':
11146 case 'o':
11147 case 'u':
11148 printf ("%d", imm);
11149 continue;
11151 case '<':
11152 case '>':
11153 printf ("$%d", shamt);
11154 continue;
11156 default:
11157 internalError ();
11159 break;
11161 return;
11164 printf (_("%08lx UNDEFINED\n"), oc);
11166 #endif
11168 static symbolS *
11169 get_symbol ()
11171 int c;
11172 char *name;
11173 symbolS *p;
11175 name = input_line_pointer;
11176 c = get_symbol_end ();
11177 p = (symbolS *) symbol_find_or_make (name);
11178 *input_line_pointer = c;
11179 return p;
11182 /* Align the current frag to a given power of two. The MIPS assembler
11183 also automatically adjusts any preceding label. */
11185 static void
11186 mips_align (to, fill, label)
11187 int to;
11188 int fill;
11189 symbolS *label;
11191 mips_emit_delays (false);
11192 frag_align (to, fill, 0);
11193 record_alignment (now_seg, to);
11194 if (label != NULL)
11196 assert (S_GET_SEGMENT (label) == now_seg);
11197 symbol_set_frag (label, frag_now);
11198 S_SET_VALUE (label, (valueT) frag_now_fix ());
11202 /* Align to a given power of two. .align 0 turns off the automatic
11203 alignment used by the data creating pseudo-ops. */
11205 static void
11206 s_align (x)
11207 int x ATTRIBUTE_UNUSED;
11209 register int temp;
11210 register long temp_fill;
11211 long max_alignment = 15;
11215 o Note that the assembler pulls down any immediately preceeding label
11216 to the aligned address.
11217 o It's not documented but auto alignment is reinstated by
11218 a .align pseudo instruction.
11219 o Note also that after auto alignment is turned off the mips assembler
11220 issues an error on attempt to assemble an improperly aligned data item.
11221 We don't.
11225 temp = get_absolute_expression ();
11226 if (temp > max_alignment)
11227 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11228 else if (temp < 0)
11230 as_warn (_("Alignment negative: 0 assumed."));
11231 temp = 0;
11233 if (*input_line_pointer == ',')
11235 ++input_line_pointer;
11236 temp_fill = get_absolute_expression ();
11238 else
11239 temp_fill = 0;
11240 if (temp)
11242 auto_align = 1;
11243 mips_align (temp, (int) temp_fill,
11244 insn_labels != NULL ? insn_labels->label : NULL);
11246 else
11248 auto_align = 0;
11251 demand_empty_rest_of_line ();
11254 void
11255 mips_flush_pending_output ()
11257 mips_emit_delays (false);
11258 mips_clear_insn_labels ();
11261 static void
11262 s_change_sec (sec)
11263 int sec;
11265 segT seg;
11267 /* When generating embedded PIC code, we only use the .text, .lit8,
11268 .sdata and .sbss sections. We change the .data and .rdata
11269 pseudo-ops to use .sdata. */
11270 if (mips_pic == EMBEDDED_PIC
11271 && (sec == 'd' || sec == 'r'))
11272 sec = 's';
11274 #ifdef OBJ_ELF
11275 /* The ELF backend needs to know that we are changing sections, so
11276 that .previous works correctly. We could do something like check
11277 for an obj_section_change_hook macro, but that might be confusing
11278 as it would not be appropriate to use it in the section changing
11279 functions in read.c, since obj-elf.c intercepts those. FIXME:
11280 This should be cleaner, somehow. */
11281 obj_elf_section_change_hook ();
11282 #endif
11284 mips_emit_delays (false);
11285 switch (sec)
11287 case 't':
11288 s_text (0);
11289 break;
11290 case 'd':
11291 s_data (0);
11292 break;
11293 case 'b':
11294 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11295 demand_empty_rest_of_line ();
11296 break;
11298 case 'r':
11299 if (USE_GLOBAL_POINTER_OPT)
11301 seg = subseg_new (RDATA_SECTION_NAME,
11302 (subsegT) get_absolute_expression ());
11303 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11305 bfd_set_section_flags (stdoutput, seg,
11306 (SEC_ALLOC
11307 | SEC_LOAD
11308 | SEC_READONLY
11309 | SEC_RELOC
11310 | SEC_DATA));
11311 if (strcmp (TARGET_OS, "elf") != 0)
11312 record_alignment (seg, 4);
11314 demand_empty_rest_of_line ();
11316 else
11318 as_bad (_("No read only data section in this object file format"));
11319 demand_empty_rest_of_line ();
11320 return;
11322 break;
11324 case 's':
11325 if (USE_GLOBAL_POINTER_OPT)
11327 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11328 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11330 bfd_set_section_flags (stdoutput, seg,
11331 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11332 | SEC_DATA);
11333 if (strcmp (TARGET_OS, "elf") != 0)
11334 record_alignment (seg, 4);
11336 demand_empty_rest_of_line ();
11337 break;
11339 else
11341 as_bad (_("Global pointers not supported; recompile -G 0"));
11342 demand_empty_rest_of_line ();
11343 return;
11347 auto_align = 1;
11350 void
11351 s_change_section (ignore)
11352 int ignore ATTRIBUTE_UNUSED;
11354 #ifdef OBJ_ELF
11355 char *section_name;
11356 char c;
11357 char next_c;
11358 int section_type;
11359 int section_flag;
11360 int section_entry_size;
11361 int section_alignment;
11363 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11364 return;
11366 section_name = input_line_pointer;
11367 c = get_symbol_end ();
11368 next_c = *(input_line_pointer + 1);
11370 /* Do we have .section Name<,"flags">? */
11371 if (c != ',' || (c == ',' && next_c == '"'))
11373 /* just after name is now '\0'. */
11374 *input_line_pointer = c;
11375 input_line_pointer = section_name;
11376 obj_elf_section (ignore);
11377 return;
11379 input_line_pointer++;
11381 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11382 if (c == ',')
11383 section_type = get_absolute_expression ();
11384 else
11385 section_type = 0;
11386 if (*input_line_pointer++ == ',')
11387 section_flag = get_absolute_expression ();
11388 else
11389 section_flag = 0;
11390 if (*input_line_pointer++ == ',')
11391 section_entry_size = get_absolute_expression ();
11392 else
11393 section_entry_size = 0;
11394 if (*input_line_pointer++ == ',')
11395 section_alignment = get_absolute_expression ();
11396 else
11397 section_alignment = 0;
11399 obj_elf_change_section (section_name, section_type, section_flag,
11400 section_entry_size, 0, 0, 0);
11401 #endif /* OBJ_ELF */
11404 void
11405 mips_enable_auto_align ()
11407 auto_align = 1;
11410 static void
11411 s_cons (log_size)
11412 int log_size;
11414 symbolS *label;
11416 label = insn_labels != NULL ? insn_labels->label : NULL;
11417 mips_emit_delays (false);
11418 if (log_size > 0 && auto_align)
11419 mips_align (log_size, 0, label);
11420 mips_clear_insn_labels ();
11421 cons (1 << log_size);
11424 static void
11425 s_float_cons (type)
11426 int type;
11428 symbolS *label;
11430 label = insn_labels != NULL ? insn_labels->label : NULL;
11432 mips_emit_delays (false);
11434 if (auto_align)
11436 if (type == 'd')
11437 mips_align (3, 0, label);
11438 else
11439 mips_align (2, 0, label);
11442 mips_clear_insn_labels ();
11444 float_cons (type);
11447 /* Handle .globl. We need to override it because on Irix 5 you are
11448 permitted to say
11449 .globl foo .text
11450 where foo is an undefined symbol, to mean that foo should be
11451 considered to be the address of a function. */
11453 static void
11454 s_mips_globl (x)
11455 int x ATTRIBUTE_UNUSED;
11457 char *name;
11458 int c;
11459 symbolS *symbolP;
11460 flagword flag;
11462 name = input_line_pointer;
11463 c = get_symbol_end ();
11464 symbolP = symbol_find_or_make (name);
11465 *input_line_pointer = c;
11466 SKIP_WHITESPACE ();
11468 /* On Irix 5, every global symbol that is not explicitly labelled as
11469 being a function is apparently labelled as being an object. */
11470 flag = BSF_OBJECT;
11472 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11474 char *secname;
11475 asection *sec;
11477 secname = input_line_pointer;
11478 c = get_symbol_end ();
11479 sec = bfd_get_section_by_name (stdoutput, secname);
11480 if (sec == NULL)
11481 as_bad (_("%s: no such section"), secname);
11482 *input_line_pointer = c;
11484 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11485 flag = BSF_FUNCTION;
11488 symbol_get_bfdsym (symbolP)->flags |= flag;
11490 S_SET_EXTERNAL (symbolP);
11491 demand_empty_rest_of_line ();
11494 static void
11495 s_option (x)
11496 int x ATTRIBUTE_UNUSED;
11498 char *opt;
11499 char c;
11501 opt = input_line_pointer;
11502 c = get_symbol_end ();
11504 if (*opt == 'O')
11506 /* FIXME: What does this mean? */
11508 else if (strncmp (opt, "pic", 3) == 0)
11510 int i;
11512 i = atoi (opt + 3);
11513 if (i == 0)
11514 mips_pic = NO_PIC;
11515 else if (i == 2)
11516 mips_pic = SVR4_PIC;
11517 else
11518 as_bad (_(".option pic%d not supported"), i);
11520 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11522 if (g_switch_seen && g_switch_value != 0)
11523 as_warn (_("-G may not be used with SVR4 PIC code"));
11524 g_switch_value = 0;
11525 bfd_set_gp_size (stdoutput, 0);
11528 else
11529 as_warn (_("Unrecognized option \"%s\""), opt);
11531 *input_line_pointer = c;
11532 demand_empty_rest_of_line ();
11535 /* This structure is used to hold a stack of .set values. */
11537 struct mips_option_stack
11539 struct mips_option_stack *next;
11540 struct mips_set_options options;
11543 static struct mips_option_stack *mips_opts_stack;
11545 /* Handle the .set pseudo-op. */
11547 static void
11548 s_mipsset (x)
11549 int x ATTRIBUTE_UNUSED;
11551 char *name = input_line_pointer, ch;
11553 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11554 ++input_line_pointer;
11555 ch = *input_line_pointer;
11556 *input_line_pointer = '\0';
11558 if (strcmp (name, "reorder") == 0)
11560 if (mips_opts.noreorder && prev_nop_frag != NULL)
11562 /* If we still have pending nops, we can discard them. The
11563 usual nop handling will insert any that are still
11564 needed. */
11565 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11566 * (mips_opts.mips16 ? 2 : 4));
11567 prev_nop_frag = NULL;
11569 mips_opts.noreorder = 0;
11571 else if (strcmp (name, "noreorder") == 0)
11573 mips_emit_delays (true);
11574 mips_opts.noreorder = 1;
11575 mips_any_noreorder = 1;
11577 else if (strcmp (name, "at") == 0)
11579 mips_opts.noat = 0;
11581 else if (strcmp (name, "noat") == 0)
11583 mips_opts.noat = 1;
11585 else if (strcmp (name, "macro") == 0)
11587 mips_opts.warn_about_macros = 0;
11589 else if (strcmp (name, "nomacro") == 0)
11591 if (mips_opts.noreorder == 0)
11592 as_bad (_("`noreorder' must be set before `nomacro'"));
11593 mips_opts.warn_about_macros = 1;
11595 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11597 mips_opts.nomove = 0;
11599 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11601 mips_opts.nomove = 1;
11603 else if (strcmp (name, "bopt") == 0)
11605 mips_opts.nobopt = 0;
11607 else if (strcmp (name, "nobopt") == 0)
11609 mips_opts.nobopt = 1;
11611 else if (strcmp (name, "mips16") == 0
11612 || strcmp (name, "MIPS-16") == 0)
11613 mips_opts.mips16 = 1;
11614 else if (strcmp (name, "nomips16") == 0
11615 || strcmp (name, "noMIPS-16") == 0)
11616 mips_opts.mips16 = 0;
11617 else if (strcmp (name, "mips3d") == 0)
11618 mips_opts.ase_mips3d = 1;
11619 else if (strcmp (name, "nomips3d") == 0)
11620 mips_opts.ase_mips3d = 0;
11621 else if (strcmp (name, "mdmx") == 0)
11622 mips_opts.ase_mdmx = 1;
11623 else if (strcmp (name, "nomdmx") == 0)
11624 mips_opts.ase_mdmx = 0;
11625 else if (strncmp (name, "mips", 4) == 0)
11627 int isa;
11629 /* Permit the user to change the ISA on the fly. Needless to
11630 say, misuse can cause serious problems. */
11631 isa = atoi (name + 4);
11632 switch (isa)
11634 case 0:
11635 mips_opts.gp32 = file_mips_gp32;
11636 mips_opts.fp32 = file_mips_fp32;
11637 break;
11638 case 1:
11639 case 2:
11640 case 32:
11641 mips_opts.gp32 = 1;
11642 mips_opts.fp32 = 1;
11643 break;
11644 case 3:
11645 case 4:
11646 case 5:
11647 case 64:
11648 mips_opts.gp32 = 0;
11649 mips_opts.fp32 = 0;
11650 break;
11651 default:
11652 as_bad (_("unknown ISA level %s"), name + 4);
11653 break;
11656 switch (isa)
11658 case 0: mips_opts.isa = file_mips_isa; break;
11659 case 1: mips_opts.isa = ISA_MIPS1; break;
11660 case 2: mips_opts.isa = ISA_MIPS2; break;
11661 case 3: mips_opts.isa = ISA_MIPS3; break;
11662 case 4: mips_opts.isa = ISA_MIPS4; break;
11663 case 5: mips_opts.isa = ISA_MIPS5; break;
11664 case 32: mips_opts.isa = ISA_MIPS32; break;
11665 case 64: mips_opts.isa = ISA_MIPS64; break;
11666 default: as_bad (_("unknown ISA level %s"), name + 4); break;
11669 else if (strcmp (name, "autoextend") == 0)
11670 mips_opts.noautoextend = 0;
11671 else if (strcmp (name, "noautoextend") == 0)
11672 mips_opts.noautoextend = 1;
11673 else if (strcmp (name, "push") == 0)
11675 struct mips_option_stack *s;
11677 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11678 s->next = mips_opts_stack;
11679 s->options = mips_opts;
11680 mips_opts_stack = s;
11682 else if (strcmp (name, "pop") == 0)
11684 struct mips_option_stack *s;
11686 s = mips_opts_stack;
11687 if (s == NULL)
11688 as_bad (_(".set pop with no .set push"));
11689 else
11691 /* If we're changing the reorder mode we need to handle
11692 delay slots correctly. */
11693 if (s->options.noreorder && ! mips_opts.noreorder)
11694 mips_emit_delays (true);
11695 else if (! s->options.noreorder && mips_opts.noreorder)
11697 if (prev_nop_frag != NULL)
11699 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11700 * (mips_opts.mips16 ? 2 : 4));
11701 prev_nop_frag = NULL;
11705 mips_opts = s->options;
11706 mips_opts_stack = s->next;
11707 free (s);
11710 else
11712 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11714 *input_line_pointer = ch;
11715 demand_empty_rest_of_line ();
11718 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11719 .option pic2. It means to generate SVR4 PIC calls. */
11721 static void
11722 s_abicalls (ignore)
11723 int ignore ATTRIBUTE_UNUSED;
11725 mips_pic = SVR4_PIC;
11726 if (USE_GLOBAL_POINTER_OPT)
11728 if (g_switch_seen && g_switch_value != 0)
11729 as_warn (_("-G may not be used with SVR4 PIC code"));
11730 g_switch_value = 0;
11732 bfd_set_gp_size (stdoutput, 0);
11733 demand_empty_rest_of_line ();
11736 /* Handle the .cpload pseudo-op. This is used when generating SVR4
11737 PIC code. It sets the $gp register for the function based on the
11738 function address, which is in the register named in the argument.
11739 This uses a relocation against _gp_disp, which is handled specially
11740 by the linker. The result is:
11741 lui $gp,%hi(_gp_disp)
11742 addiu $gp,$gp,%lo(_gp_disp)
11743 addu $gp,$gp,.cpload argument
11744 The .cpload argument is normally $25 == $t9. */
11746 static void
11747 s_cpload (ignore)
11748 int ignore ATTRIBUTE_UNUSED;
11750 expressionS ex;
11751 int icnt = 0;
11753 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11754 .cpload is ignored. */
11755 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11757 s_ignore (0);
11758 return;
11761 /* .cpload should be in a .set noreorder section. */
11762 if (mips_opts.noreorder == 0)
11763 as_warn (_(".cpload not in noreorder section"));
11765 ex.X_op = O_symbol;
11766 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11767 ex.X_op_symbol = NULL;
11768 ex.X_add_number = 0;
11770 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11771 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11773 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11774 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11775 mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
11777 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11778 mips_gp_register, mips_gp_register, tc_get_register (0));
11780 demand_empty_rest_of_line ();
11783 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11784 .cpsetup $reg1, offset|$reg2, label
11786 If offset is given, this results in:
11787 sd $gp, offset($sp)
11788 lui $gp, %hi(%neg(%gp_rel(label)))
11789 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11790 daddu $gp, $gp, $reg1
11792 If $reg2 is given, this results in:
11793 daddu $reg2, $gp, $0
11794 lui $gp, %hi(%neg(%gp_rel(label)))
11795 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11796 daddu $gp, $gp, $reg1
11797 $reg1 is normally $25 == $t9. */
11798 static void
11799 s_cpsetup (ignore)
11800 int ignore ATTRIBUTE_UNUSED;
11802 expressionS ex_off;
11803 expressionS ex_sym;
11804 int reg1;
11805 int icnt = 0;
11806 char *f;
11808 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11809 We also need NewABI support. */
11810 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11812 s_ignore (0);
11813 return;
11816 reg1 = tc_get_register (0);
11817 SKIP_WHITESPACE ();
11818 if (*input_line_pointer != ',')
11820 as_bad (_("missing argument separator ',' for .cpsetup"));
11821 return;
11823 else
11824 ++input_line_pointer;
11825 SKIP_WHITESPACE ();
11826 if (*input_line_pointer == '$')
11828 mips_cpreturn_register = tc_get_register (0);
11829 mips_cpreturn_offset = -1;
11831 else
11833 mips_cpreturn_offset = get_absolute_expression ();
11834 mips_cpreturn_register = -1;
11836 SKIP_WHITESPACE ();
11837 if (*input_line_pointer != ',')
11839 as_bad (_("missing argument separator ',' for .cpsetup"));
11840 return;
11842 else
11843 ++input_line_pointer;
11844 SKIP_WHITESPACE ();
11845 expression (&ex_sym);
11847 if (mips_cpreturn_register == -1)
11849 ex_off.X_op = O_constant;
11850 ex_off.X_add_symbol = NULL;
11851 ex_off.X_op_symbol = NULL;
11852 ex_off.X_add_number = mips_cpreturn_offset;
11854 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11855 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11857 else
11858 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11859 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11861 /* Ensure there's room for the next two instructions, so that `f'
11862 doesn't end up with an address in the wrong frag. */
11863 frag_grow (8);
11864 f = frag_more (0);
11865 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11866 (int) BFD_RELOC_GPREL16);
11867 fix_new (frag_now, f - frag_now->fr_literal,
11868 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11869 fix_new (frag_now, f - frag_now->fr_literal,
11870 0, NULL, 0, 0, BFD_RELOC_HI16_S);
11872 f = frag_more (0);
11873 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11874 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11875 fix_new (frag_now, f - frag_now->fr_literal,
11876 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11877 fix_new (frag_now, f - frag_now->fr_literal,
11878 0, NULL, 0, 0, BFD_RELOC_LO16);
11880 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11881 HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11882 mips_gp_register, mips_gp_register, reg1);
11884 demand_empty_rest_of_line ();
11887 static void
11888 s_cplocal (ignore)
11889 int ignore ATTRIBUTE_UNUSED;
11891 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11892 .cplocal is ignored. */
11893 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11895 s_ignore (0);
11896 return;
11899 mips_gp_register = tc_get_register (0);
11900 demand_empty_rest_of_line ();
11903 /* Handle the .cprestore pseudo-op. This stores $gp into a given
11904 offset from $sp. The offset is remembered, and after making a PIC
11905 call $gp is restored from that location. */
11907 static void
11908 s_cprestore (ignore)
11909 int ignore ATTRIBUTE_UNUSED;
11911 expressionS ex;
11912 int icnt = 0;
11914 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11915 .cprestore is ignored. */
11916 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11918 s_ignore (0);
11919 return;
11922 mips_cprestore_offset = get_absolute_expression ();
11923 mips_cprestore_valid = 1;
11925 ex.X_op = O_constant;
11926 ex.X_add_symbol = NULL;
11927 ex.X_op_symbol = NULL;
11928 ex.X_add_number = mips_cprestore_offset;
11930 macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
11931 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11932 mips_gp_register, SP);
11934 demand_empty_rest_of_line ();
11937 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11938 was given in the preceeding .gpsetup, it results in:
11939 ld $gp, offset($sp)
11941 If a register $reg2 was given there, it results in:
11942 daddiu $gp, $gp, $reg2
11944 static void
11945 s_cpreturn (ignore)
11946 int ignore ATTRIBUTE_UNUSED;
11948 expressionS ex;
11949 int icnt = 0;
11951 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11952 We also need NewABI support. */
11953 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11955 s_ignore (0);
11956 return;
11959 if (mips_cpreturn_register == -1)
11961 ex.X_op = O_constant;
11962 ex.X_add_symbol = NULL;
11963 ex.X_op_symbol = NULL;
11964 ex.X_add_number = mips_cpreturn_offset;
11966 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11967 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11969 else
11970 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11971 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11973 demand_empty_rest_of_line ();
11976 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
11977 code. It sets the offset to use in gp_rel relocations. */
11979 static void
11980 s_gpvalue (ignore)
11981 int ignore ATTRIBUTE_UNUSED;
11983 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11984 We also need NewABI support. */
11985 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11987 s_ignore (0);
11988 return;
11991 mips_gprel_offset = get_absolute_expression ();
11993 demand_empty_rest_of_line ();
11996 /* Handle the .gpword pseudo-op. This is used when generating PIC
11997 code. It generates a 32 bit GP relative reloc. */
11999 static void
12000 s_gpword (ignore)
12001 int ignore ATTRIBUTE_UNUSED;
12003 symbolS *label;
12004 expressionS ex;
12005 char *p;
12007 /* When not generating PIC code, this is treated as .word. */
12008 if (mips_pic != SVR4_PIC)
12010 s_cons (2);
12011 return;
12014 label = insn_labels != NULL ? insn_labels->label : NULL;
12015 mips_emit_delays (true);
12016 if (auto_align)
12017 mips_align (2, 0, label);
12018 mips_clear_insn_labels ();
12020 expression (&ex);
12022 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12024 as_bad (_("Unsupported use of .gpword"));
12025 ignore_rest_of_line ();
12028 p = frag_more (4);
12029 md_number_to_chars (p, (valueT) 0, 4);
12030 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
12031 BFD_RELOC_GPREL32);
12033 demand_empty_rest_of_line ();
12036 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12037 tables in SVR4 PIC code. */
12039 static void
12040 s_cpadd (ignore)
12041 int ignore ATTRIBUTE_UNUSED;
12043 int icnt = 0;
12044 int reg;
12046 /* This is ignored when not generating SVR4 PIC code or if this is NewABI
12047 code. */
12048 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12050 s_ignore (0);
12051 return;
12054 /* Add $gp to the register named as an argument. */
12055 reg = tc_get_register (0);
12056 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12057 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12058 "d,v,t", reg, reg, mips_gp_register);
12060 demand_empty_rest_of_line ();
12063 /* Handle the .insn pseudo-op. This marks instruction labels in
12064 mips16 mode. This permits the linker to handle them specially,
12065 such as generating jalx instructions when needed. We also make
12066 them odd for the duration of the assembly, in order to generate the
12067 right sort of code. We will make them even in the adjust_symtab
12068 routine, while leaving them marked. This is convenient for the
12069 debugger and the disassembler. The linker knows to make them odd
12070 again. */
12072 static void
12073 s_insn (ignore)
12074 int ignore ATTRIBUTE_UNUSED;
12076 mips16_mark_labels ();
12078 demand_empty_rest_of_line ();
12081 /* Handle a .stabn directive. We need these in order to mark a label
12082 as being a mips16 text label correctly. Sometimes the compiler
12083 will emit a label, followed by a .stabn, and then switch sections.
12084 If the label and .stabn are in mips16 mode, then the label is
12085 really a mips16 text label. */
12087 static void
12088 s_mips_stab (type)
12089 int type;
12091 if (type == 'n')
12092 mips16_mark_labels ();
12094 s_stab (type);
12097 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12100 static void
12101 s_mips_weakext (ignore)
12102 int ignore ATTRIBUTE_UNUSED;
12104 char *name;
12105 int c;
12106 symbolS *symbolP;
12107 expressionS exp;
12109 name = input_line_pointer;
12110 c = get_symbol_end ();
12111 symbolP = symbol_find_or_make (name);
12112 S_SET_WEAK (symbolP);
12113 *input_line_pointer = c;
12115 SKIP_WHITESPACE ();
12117 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12119 if (S_IS_DEFINED (symbolP))
12121 as_bad ("ignoring attempt to redefine symbol %s",
12122 S_GET_NAME (symbolP));
12123 ignore_rest_of_line ();
12124 return;
12127 if (*input_line_pointer == ',')
12129 ++input_line_pointer;
12130 SKIP_WHITESPACE ();
12133 expression (&exp);
12134 if (exp.X_op != O_symbol)
12136 as_bad ("bad .weakext directive");
12137 ignore_rest_of_line ();
12138 return;
12140 symbol_set_value_expression (symbolP, &exp);
12143 demand_empty_rest_of_line ();
12146 /* Parse a register string into a number. Called from the ECOFF code
12147 to parse .frame. The argument is non-zero if this is the frame
12148 register, so that we can record it in mips_frame_reg. */
12151 tc_get_register (frame)
12152 int frame;
12154 int reg;
12156 SKIP_WHITESPACE ();
12157 if (*input_line_pointer++ != '$')
12159 as_warn (_("expected `$'"));
12160 reg = ZERO;
12162 else if (ISDIGIT (*input_line_pointer))
12164 reg = get_absolute_expression ();
12165 if (reg < 0 || reg >= 32)
12167 as_warn (_("Bad register number"));
12168 reg = ZERO;
12171 else
12173 if (strncmp (input_line_pointer, "ra", 2) == 0)
12175 reg = RA;
12176 input_line_pointer += 2;
12178 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12180 reg = FP;
12181 input_line_pointer += 2;
12183 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12185 reg = SP;
12186 input_line_pointer += 2;
12188 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12190 reg = GP;
12191 input_line_pointer += 2;
12193 else if (strncmp (input_line_pointer, "at", 2) == 0)
12195 reg = AT;
12196 input_line_pointer += 2;
12198 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12200 reg = KT0;
12201 input_line_pointer += 3;
12203 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12205 reg = KT1;
12206 input_line_pointer += 3;
12208 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12210 reg = ZERO;
12211 input_line_pointer += 4;
12213 else
12215 as_warn (_("Unrecognized register name"));
12216 reg = ZERO;
12217 while (ISALNUM(*input_line_pointer))
12218 input_line_pointer++;
12221 if (frame)
12223 mips_frame_reg = reg != 0 ? reg : SP;
12224 mips_frame_reg_valid = 1;
12225 mips_cprestore_valid = 0;
12227 return reg;
12230 valueT
12231 md_section_align (seg, addr)
12232 asection *seg;
12233 valueT addr;
12235 int align = bfd_get_section_alignment (stdoutput, seg);
12237 #ifdef OBJ_ELF
12238 /* We don't need to align ELF sections to the full alignment.
12239 However, Irix 5 may prefer that we align them at least to a 16
12240 byte boundary. We don't bother to align the sections if we are
12241 targeted for an embedded system. */
12242 if (strcmp (TARGET_OS, "elf") == 0)
12243 return addr;
12244 if (align > 4)
12245 align = 4;
12246 #endif
12248 return ((addr + (1 << align) - 1) & (-1 << align));
12251 /* Utility routine, called from above as well. If called while the
12252 input file is still being read, it's only an approximation. (For
12253 example, a symbol may later become defined which appeared to be
12254 undefined earlier.) */
12256 static int
12257 nopic_need_relax (sym, before_relaxing)
12258 symbolS *sym;
12259 int before_relaxing;
12261 if (sym == 0)
12262 return 0;
12264 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12266 const char *symname;
12267 int change;
12269 /* Find out whether this symbol can be referenced off the $gp
12270 register. It can be if it is smaller than the -G size or if
12271 it is in the .sdata or .sbss section. Certain symbols can
12272 not be referenced off the $gp, although it appears as though
12273 they can. */
12274 symname = S_GET_NAME (sym);
12275 if (symname != (const char *) NULL
12276 && (strcmp (symname, "eprol") == 0
12277 || strcmp (symname, "etext") == 0
12278 || strcmp (symname, "_gp") == 0
12279 || strcmp (symname, "edata") == 0
12280 || strcmp (symname, "_fbss") == 0
12281 || strcmp (symname, "_fdata") == 0
12282 || strcmp (symname, "_ftext") == 0
12283 || strcmp (symname, "end") == 0
12284 || strcmp (symname, "_gp_disp") == 0))
12285 change = 1;
12286 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12287 && (0
12288 #ifndef NO_ECOFF_DEBUGGING
12289 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12290 && (symbol_get_obj (sym)->ecoff_extern_size
12291 <= g_switch_value))
12292 #endif
12293 /* We must defer this decision until after the whole
12294 file has been read, since there might be a .extern
12295 after the first use of this symbol. */
12296 || (before_relaxing
12297 #ifndef NO_ECOFF_DEBUGGING
12298 && symbol_get_obj (sym)->ecoff_extern_size == 0
12299 #endif
12300 && S_GET_VALUE (sym) == 0)
12301 || (S_GET_VALUE (sym) != 0
12302 && S_GET_VALUE (sym) <= g_switch_value)))
12303 change = 0;
12304 else
12306 const char *segname;
12308 segname = segment_name (S_GET_SEGMENT (sym));
12309 assert (strcmp (segname, ".lit8") != 0
12310 && strcmp (segname, ".lit4") != 0);
12311 change = (strcmp (segname, ".sdata") != 0
12312 && strcmp (segname, ".sbss") != 0
12313 && strncmp (segname, ".sdata.", 7) != 0
12314 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12316 return change;
12318 else
12319 /* We are not optimizing for the $gp register. */
12320 return 1;
12323 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12324 extended opcode. SEC is the section the frag is in. */
12326 static int
12327 mips16_extended_frag (fragp, sec, stretch)
12328 fragS *fragp;
12329 asection *sec;
12330 long stretch;
12332 int type;
12333 register const struct mips16_immed_operand *op;
12334 offsetT val;
12335 int mintiny, maxtiny;
12336 segT symsec;
12337 fragS *sym_frag;
12339 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12340 return 0;
12341 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12342 return 1;
12344 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12345 op = mips16_immed_operands;
12346 while (op->type != type)
12348 ++op;
12349 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12352 if (op->unsp)
12354 if (type == '<' || type == '>' || type == '[' || type == ']')
12356 mintiny = 1;
12357 maxtiny = 1 << op->nbits;
12359 else
12361 mintiny = 0;
12362 maxtiny = (1 << op->nbits) - 1;
12365 else
12367 mintiny = - (1 << (op->nbits - 1));
12368 maxtiny = (1 << (op->nbits - 1)) - 1;
12371 sym_frag = symbol_get_frag (fragp->fr_symbol);
12372 val = S_GET_VALUE (fragp->fr_symbol);
12373 symsec = S_GET_SEGMENT (fragp->fr_symbol);
12375 if (op->pcrel)
12377 addressT addr;
12379 /* We won't have the section when we are called from
12380 mips_relax_frag. However, we will always have been called
12381 from md_estimate_size_before_relax first. If this is a
12382 branch to a different section, we mark it as such. If SEC is
12383 NULL, and the frag is not marked, then it must be a branch to
12384 the same section. */
12385 if (sec == NULL)
12387 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12388 return 1;
12390 else
12392 /* Must have been called from md_estimate_size_before_relax. */
12393 if (symsec != sec)
12395 fragp->fr_subtype =
12396 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12398 /* FIXME: We should support this, and let the linker
12399 catch branches and loads that are out of range. */
12400 as_bad_where (fragp->fr_file, fragp->fr_line,
12401 _("unsupported PC relative reference to different section"));
12403 return 1;
12405 if (fragp != sym_frag && sym_frag->fr_address == 0)
12406 /* Assume non-extended on the first relaxation pass.
12407 The address we have calculated will be bogus if this is
12408 a forward branch to another frag, as the forward frag
12409 will have fr_address == 0. */
12410 return 0;
12413 /* In this case, we know for sure that the symbol fragment is in
12414 the same section. If the relax_marker of the symbol fragment
12415 differs from the relax_marker of this fragment, we have not
12416 yet adjusted the symbol fragment fr_address. We want to add
12417 in STRETCH in order to get a better estimate of the address.
12418 This particularly matters because of the shift bits. */
12419 if (stretch != 0
12420 && sym_frag->relax_marker != fragp->relax_marker)
12422 fragS *f;
12424 /* Adjust stretch for any alignment frag. Note that if have
12425 been expanding the earlier code, the symbol may be
12426 defined in what appears to be an earlier frag. FIXME:
12427 This doesn't handle the fr_subtype field, which specifies
12428 a maximum number of bytes to skip when doing an
12429 alignment. */
12430 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12432 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12434 if (stretch < 0)
12435 stretch = - ((- stretch)
12436 & ~ ((1 << (int) f->fr_offset) - 1));
12437 else
12438 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12439 if (stretch == 0)
12440 break;
12443 if (f != NULL)
12444 val += stretch;
12447 addr = fragp->fr_address + fragp->fr_fix;
12449 /* The base address rules are complicated. The base address of
12450 a branch is the following instruction. The base address of a
12451 PC relative load or add is the instruction itself, but if it
12452 is in a delay slot (in which case it can not be extended) use
12453 the address of the instruction whose delay slot it is in. */
12454 if (type == 'p' || type == 'q')
12456 addr += 2;
12458 /* If we are currently assuming that this frag should be
12459 extended, then, the current address is two bytes
12460 higher. */
12461 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12462 addr += 2;
12464 /* Ignore the low bit in the target, since it will be set
12465 for a text label. */
12466 if ((val & 1) != 0)
12467 --val;
12469 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12470 addr -= 4;
12471 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12472 addr -= 2;
12474 val -= addr & ~ ((1 << op->shift) - 1);
12476 /* Branch offsets have an implicit 0 in the lowest bit. */
12477 if (type == 'p' || type == 'q')
12478 val /= 2;
12480 /* If any of the shifted bits are set, we must use an extended
12481 opcode. If the address depends on the size of this
12482 instruction, this can lead to a loop, so we arrange to always
12483 use an extended opcode. We only check this when we are in
12484 the main relaxation loop, when SEC is NULL. */
12485 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12487 fragp->fr_subtype =
12488 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12489 return 1;
12492 /* If we are about to mark a frag as extended because the value
12493 is precisely maxtiny + 1, then there is a chance of an
12494 infinite loop as in the following code:
12495 la $4,foo
12496 .skip 1020
12497 .align 2
12498 foo:
12499 In this case when the la is extended, foo is 0x3fc bytes
12500 away, so the la can be shrunk, but then foo is 0x400 away, so
12501 the la must be extended. To avoid this loop, we mark the
12502 frag as extended if it was small, and is about to become
12503 extended with a value of maxtiny + 1. */
12504 if (val == ((maxtiny + 1) << op->shift)
12505 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12506 && sec == NULL)
12508 fragp->fr_subtype =
12509 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12510 return 1;
12513 else if (symsec != absolute_section && sec != NULL)
12514 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12516 if ((val & ((1 << op->shift) - 1)) != 0
12517 || val < (mintiny << op->shift)
12518 || val > (maxtiny << op->shift))
12519 return 1;
12520 else
12521 return 0;
12524 /* Estimate the size of a frag before relaxing. Unless this is the
12525 mips16, we are not really relaxing here, and the final size is
12526 encoded in the subtype information. For the mips16, we have to
12527 decide whether we are using an extended opcode or not. */
12530 md_estimate_size_before_relax (fragp, segtype)
12531 fragS *fragp;
12532 asection *segtype;
12534 int change = 0;
12535 boolean linkonce = false;
12537 if (RELAX_MIPS16_P (fragp->fr_subtype))
12538 /* We don't want to modify the EXTENDED bit here; it might get us
12539 into infinite loops. We change it only in mips_relax_frag(). */
12540 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12542 if (mips_pic == NO_PIC)
12544 change = nopic_need_relax (fragp->fr_symbol, 0);
12546 else if (mips_pic == SVR4_PIC)
12548 symbolS *sym;
12549 asection *symsec;
12551 sym = fragp->fr_symbol;
12553 /* Handle the case of a symbol equated to another symbol. */
12554 while (symbol_equated_reloc_p (sym))
12556 symbolS *n;
12558 /* It's possible to get a loop here in a badly written
12559 program. */
12560 n = symbol_get_value_expression (sym)->X_add_symbol;
12561 if (n == sym)
12562 break;
12563 sym = n;
12566 symsec = S_GET_SEGMENT (sym);
12568 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12569 if (symsec != segtype && ! S_IS_LOCAL (sym))
12571 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12572 != 0)
12573 linkonce = true;
12575 /* The GNU toolchain uses an extension for ELF: a section
12576 beginning with the magic string .gnu.linkonce is a linkonce
12577 section. */
12578 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12579 sizeof ".gnu.linkonce" - 1) == 0)
12580 linkonce = true;
12583 /* This must duplicate the test in adjust_reloc_syms. */
12584 change = (symsec != &bfd_und_section
12585 && symsec != &bfd_abs_section
12586 && ! bfd_is_com_section (symsec)
12587 && !linkonce
12588 #ifdef OBJ_ELF
12589 /* A global or weak symbol is treated as external. */
12590 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12591 || (! S_IS_WEAK (sym)
12592 && (! S_IS_EXTERNAL (sym)
12593 || mips_pic == EMBEDDED_PIC)))
12594 #endif
12597 else
12598 abort ();
12600 if (change)
12602 /* Record the offset to the first reloc in the fr_opcode field.
12603 This lets md_convert_frag and tc_gen_reloc know that the code
12604 must be expanded. */
12605 fragp->fr_opcode = (fragp->fr_literal
12606 + fragp->fr_fix
12607 - RELAX_OLD (fragp->fr_subtype)
12608 + RELAX_RELOC1 (fragp->fr_subtype));
12609 /* FIXME: This really needs as_warn_where. */
12610 if (RELAX_WARN (fragp->fr_subtype))
12611 as_warn (_("AT used after \".set noat\" or macro used after "
12612 "\".set nomacro\""));
12614 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12617 return 0;
12620 /* This is called to see whether a reloc against a defined symbol
12621 should be converted into a reloc against a section. Don't adjust
12622 MIPS16 jump relocations, so we don't have to worry about the format
12623 of the offset in the .o file. Don't adjust relocations against
12624 mips16 symbols, so that the linker can find them if it needs to set
12625 up a stub. */
12628 mips_fix_adjustable (fixp)
12629 fixS *fixp;
12631 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12632 return 0;
12634 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12635 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12636 return 0;
12638 if (fixp->fx_addsy == NULL)
12639 return 1;
12641 #ifdef OBJ_ELF
12642 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12643 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12644 && fixp->fx_subsy == NULL)
12645 return 0;
12646 #endif
12648 return 1;
12651 /* Translate internal representation of relocation info to BFD target
12652 format. */
12654 arelent **
12655 tc_gen_reloc (section, fixp)
12656 asection *section ATTRIBUTE_UNUSED;
12657 fixS *fixp;
12659 static arelent *retval[4];
12660 arelent *reloc;
12661 bfd_reloc_code_real_type code;
12663 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12664 retval[1] = NULL;
12666 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12667 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12668 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12670 if (mips_pic == EMBEDDED_PIC
12671 && SWITCH_TABLE (fixp))
12673 /* For a switch table entry we use a special reloc. The addend
12674 is actually the difference between the reloc address and the
12675 subtrahend. */
12676 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12677 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12678 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12679 fixp->fx_r_type = BFD_RELOC_GPREL32;
12681 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12683 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12684 reloc->addend = fixp->fx_addnumber;
12685 else
12687 /* We use a special addend for an internal RELLO reloc. */
12688 if (symbol_section_p (fixp->fx_addsy))
12689 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12690 else
12691 reloc->addend = fixp->fx_addnumber + reloc->address;
12694 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12696 assert (fixp->fx_next != NULL
12697 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12699 /* The reloc is relative to the RELLO; adjust the addend
12700 accordingly. */
12701 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12702 reloc->addend = fixp->fx_next->fx_addnumber;
12703 else
12705 /* We use a special addend for an internal RELHI reloc. */
12706 if (symbol_section_p (fixp->fx_addsy))
12707 reloc->addend = (fixp->fx_next->fx_frag->fr_address
12708 + fixp->fx_next->fx_where
12709 - S_GET_VALUE (fixp->fx_subsy));
12710 else
12711 reloc->addend = (fixp->fx_addnumber
12712 + fixp->fx_next->fx_frag->fr_address
12713 + fixp->fx_next->fx_where);
12716 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12717 reloc->addend = fixp->fx_addnumber;
12718 else
12720 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12721 /* A gruesome hack which is a result of the gruesome gas reloc
12722 handling. */
12723 reloc->addend = reloc->address;
12724 else
12725 reloc->addend = -reloc->address;
12728 /* If this is a variant frag, we may need to adjust the existing
12729 reloc and generate a new one. */
12730 if (fixp->fx_frag->fr_opcode != NULL
12731 && ((fixp->fx_r_type == BFD_RELOC_GPREL16
12732 && ! HAVE_NEWABI)
12733 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12734 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12735 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12736 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12737 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12738 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12741 arelent *reloc2;
12743 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12745 /* If this is not the last reloc in this frag, then we have two
12746 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12747 CALL_HI16/CALL_LO16, both of which are being replaced. Let
12748 the second one handle all of them. */
12749 if (fixp->fx_next != NULL
12750 && fixp->fx_frag == fixp->fx_next->fx_frag)
12752 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12753 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12754 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12755 && (fixp->fx_next->fx_r_type
12756 == BFD_RELOC_MIPS_GOT_LO16))
12757 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12758 && (fixp->fx_next->fx_r_type
12759 == BFD_RELOC_MIPS_CALL_LO16)));
12760 retval[0] = NULL;
12761 return retval;
12764 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12765 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12766 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12767 retval[2] = NULL;
12768 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12769 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12770 reloc2->address = (reloc->address
12771 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12772 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12773 reloc2->addend = fixp->fx_addnumber;
12774 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12775 assert (reloc2->howto != NULL);
12777 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12779 arelent *reloc3;
12781 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12782 retval[3] = NULL;
12783 *reloc3 = *reloc2;
12784 reloc3->address += 4;
12787 if (mips_pic == NO_PIC)
12789 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12790 fixp->fx_r_type = BFD_RELOC_HI16_S;
12792 else if (mips_pic == SVR4_PIC)
12794 switch (fixp->fx_r_type)
12796 default:
12797 abort ();
12798 case BFD_RELOC_MIPS_GOT16:
12799 break;
12800 case BFD_RELOC_MIPS_GOT_LO16:
12801 case BFD_RELOC_MIPS_CALL_LO16:
12802 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12803 break;
12804 case BFD_RELOC_MIPS_CALL16:
12805 if (HAVE_NEWABI)
12807 /* BFD_RELOC_MIPS_GOT16;*/
12808 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
12809 reloc2->howto = bfd_reloc_type_lookup
12810 (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
12812 else
12813 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12814 break;
12817 else
12818 abort ();
12820 /* newabi uses R_MIPS_GOT_DISP for local symbols */
12821 if (HAVE_NEWABI && BFD_RELOC_MIPS_GOT_LO16)
12823 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
12824 retval[1] = NULL;
12828 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12829 entry to be used in the relocation's section offset. */
12830 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12832 reloc->address = reloc->addend;
12833 reloc->addend = 0;
12836 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12837 fixup_segment converted a non-PC relative reloc into a PC
12838 relative reloc. In such a case, we need to convert the reloc
12839 code. */
12840 code = fixp->fx_r_type;
12841 if (fixp->fx_pcrel)
12843 switch (code)
12845 case BFD_RELOC_8:
12846 code = BFD_RELOC_8_PCREL;
12847 break;
12848 case BFD_RELOC_16:
12849 code = BFD_RELOC_16_PCREL;
12850 break;
12851 case BFD_RELOC_32:
12852 code = BFD_RELOC_32_PCREL;
12853 break;
12854 case BFD_RELOC_64:
12855 code = BFD_RELOC_64_PCREL;
12856 break;
12857 case BFD_RELOC_8_PCREL:
12858 case BFD_RELOC_16_PCREL:
12859 case BFD_RELOC_32_PCREL:
12860 case BFD_RELOC_64_PCREL:
12861 case BFD_RELOC_16_PCREL_S2:
12862 case BFD_RELOC_PCREL_HI16_S:
12863 case BFD_RELOC_PCREL_LO16:
12864 break;
12865 default:
12866 as_bad_where (fixp->fx_file, fixp->fx_line,
12867 _("Cannot make %s relocation PC relative"),
12868 bfd_get_reloc_code_name (code));
12872 #ifdef OBJ_ELF
12873 /* md_apply_fix3 has a double-subtraction hack to get
12874 bfd_install_relocation to behave nicely. GPREL relocations are
12875 handled correctly without this hack, so undo it here. We can't
12876 stop md_apply_fix3 from subtracting twice in the first place since
12877 the fake addend is required for variant frags above. */
12878 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12879 && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
12880 && reloc->addend != 0
12881 && mips_need_elf_addend_fixup (fixp))
12882 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12883 #endif
12885 /* To support a PC relative reloc when generating embedded PIC code
12886 for ECOFF, we use a Cygnus extension. We check for that here to
12887 make sure that we don't let such a reloc escape normally. */
12888 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12889 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12890 && code == BFD_RELOC_16_PCREL_S2
12891 && mips_pic != EMBEDDED_PIC)
12892 reloc->howto = NULL;
12893 else
12894 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12896 if (reloc->howto == NULL)
12898 as_bad_where (fixp->fx_file, fixp->fx_line,
12899 _("Can not represent %s relocation in this object file format"),
12900 bfd_get_reloc_code_name (code));
12901 retval[0] = NULL;
12904 return retval;
12907 /* Relax a machine dependent frag. This returns the amount by which
12908 the current size of the frag should change. */
12911 mips_relax_frag (fragp, stretch)
12912 fragS *fragp;
12913 long stretch;
12915 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12916 return 0;
12918 if (mips16_extended_frag (fragp, NULL, stretch))
12920 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12921 return 0;
12922 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12923 return 2;
12925 else
12927 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12928 return 0;
12929 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12930 return -2;
12933 return 0;
12936 /* Convert a machine dependent frag. */
12938 void
12939 md_convert_frag (abfd, asec, fragp)
12940 bfd *abfd ATTRIBUTE_UNUSED;
12941 segT asec;
12942 fragS *fragp;
12944 int old, new;
12945 char *fixptr;
12947 if (RELAX_MIPS16_P (fragp->fr_subtype))
12949 int type;
12950 register const struct mips16_immed_operand *op;
12951 boolean small, ext;
12952 offsetT val;
12953 bfd_byte *buf;
12954 unsigned long insn;
12955 boolean use_extend;
12956 unsigned short extend;
12958 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12959 op = mips16_immed_operands;
12960 while (op->type != type)
12961 ++op;
12963 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12965 small = false;
12966 ext = true;
12968 else
12970 small = true;
12971 ext = false;
12974 resolve_symbol_value (fragp->fr_symbol);
12975 val = S_GET_VALUE (fragp->fr_symbol);
12976 if (op->pcrel)
12978 addressT addr;
12980 addr = fragp->fr_address + fragp->fr_fix;
12982 /* The rules for the base address of a PC relative reloc are
12983 complicated; see mips16_extended_frag. */
12984 if (type == 'p' || type == 'q')
12986 addr += 2;
12987 if (ext)
12988 addr += 2;
12989 /* Ignore the low bit in the target, since it will be
12990 set for a text label. */
12991 if ((val & 1) != 0)
12992 --val;
12994 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12995 addr -= 4;
12996 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12997 addr -= 2;
12999 addr &= ~ (addressT) ((1 << op->shift) - 1);
13000 val -= addr;
13002 /* Make sure the section winds up with the alignment we have
13003 assumed. */
13004 if (op->shift > 0)
13005 record_alignment (asec, op->shift);
13008 if (ext
13009 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13010 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13011 as_warn_where (fragp->fr_file, fragp->fr_line,
13012 _("extended instruction in delay slot"));
13014 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13016 if (target_big_endian)
13017 insn = bfd_getb16 (buf);
13018 else
13019 insn = bfd_getl16 (buf);
13021 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13022 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13023 small, ext, &insn, &use_extend, &extend);
13025 if (use_extend)
13027 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13028 fragp->fr_fix += 2;
13029 buf += 2;
13032 md_number_to_chars ((char *) buf, insn, 2);
13033 fragp->fr_fix += 2;
13034 buf += 2;
13036 else
13038 if (fragp->fr_opcode == NULL)
13039 return;
13041 old = RELAX_OLD (fragp->fr_subtype);
13042 new = RELAX_NEW (fragp->fr_subtype);
13043 fixptr = fragp->fr_literal + fragp->fr_fix;
13045 if (new > 0)
13046 memcpy (fixptr - old, fixptr, new);
13048 fragp->fr_fix += new - old;
13052 #ifdef OBJ_ELF
13054 /* This function is called after the relocs have been generated.
13055 We've been storing mips16 text labels as odd. Here we convert them
13056 back to even for the convenience of the debugger. */
13058 void
13059 mips_frob_file_after_relocs ()
13061 asymbol **syms;
13062 unsigned int count, i;
13064 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13065 return;
13067 syms = bfd_get_outsymbols (stdoutput);
13068 count = bfd_get_symcount (stdoutput);
13069 for (i = 0; i < count; i++, syms++)
13071 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13072 && ((*syms)->value & 1) != 0)
13074 (*syms)->value &= ~1;
13075 /* If the symbol has an odd size, it was probably computed
13076 incorrectly, so adjust that as well. */
13077 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13078 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13083 #endif
13085 /* This function is called whenever a label is defined. It is used
13086 when handling branch delays; if a branch has a label, we assume we
13087 can not move it. */
13089 void
13090 mips_define_label (sym)
13091 symbolS *sym;
13093 struct insn_label_list *l;
13095 if (free_insn_labels == NULL)
13096 l = (struct insn_label_list *) xmalloc (sizeof *l);
13097 else
13099 l = free_insn_labels;
13100 free_insn_labels = l->next;
13103 l->label = sym;
13104 l->next = insn_labels;
13105 insn_labels = l;
13108 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13110 /* Some special processing for a MIPS ELF file. */
13112 void
13113 mips_elf_final_processing ()
13115 /* Write out the register information. */
13116 if (mips_abi != N64_ABI)
13118 Elf32_RegInfo s;
13120 s.ri_gprmask = mips_gprmask;
13121 s.ri_cprmask[0] = mips_cprmask[0];
13122 s.ri_cprmask[1] = mips_cprmask[1];
13123 s.ri_cprmask[2] = mips_cprmask[2];
13124 s.ri_cprmask[3] = mips_cprmask[3];
13125 /* The gp_value field is set by the MIPS ELF backend. */
13127 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13128 ((Elf32_External_RegInfo *)
13129 mips_regmask_frag));
13131 else
13133 Elf64_Internal_RegInfo s;
13135 s.ri_gprmask = mips_gprmask;
13136 s.ri_pad = 0;
13137 s.ri_cprmask[0] = mips_cprmask[0];
13138 s.ri_cprmask[1] = mips_cprmask[1];
13139 s.ri_cprmask[2] = mips_cprmask[2];
13140 s.ri_cprmask[3] = mips_cprmask[3];
13141 /* The gp_value field is set by the MIPS ELF backend. */
13143 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13144 ((Elf64_External_RegInfo *)
13145 mips_regmask_frag));
13148 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13149 sort of BFD interface for this. */
13150 if (mips_any_noreorder)
13151 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13152 if (mips_pic != NO_PIC)
13153 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13155 /* Set MIPS ELF flags for ASEs. */
13156 if (file_ase_mips16)
13157 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13158 #if 0 /* XXX FIXME */
13159 if (file_ase_mips3d)
13160 elf_elfheader (stdoutput)->e_flags |= ???;
13161 #endif
13162 if (file_ase_mdmx)
13163 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13165 /* Set the MIPS ELF ABI flags. */
13166 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13167 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13168 else if (mips_abi == O64_ABI)
13169 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13170 else if (mips_abi == EABI_ABI)
13172 if (!file_mips_gp32)
13173 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13174 else
13175 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13177 else if (mips_abi == N32_ABI)
13178 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13180 /* Nothing to do for N64_ABI. */
13182 if (mips_32bitmode)
13183 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13186 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13188 typedef struct proc {
13189 symbolS *isym;
13190 unsigned long reg_mask;
13191 unsigned long reg_offset;
13192 unsigned long fpreg_mask;
13193 unsigned long fpreg_offset;
13194 unsigned long frame_offset;
13195 unsigned long frame_reg;
13196 unsigned long pc_reg;
13197 } procS;
13199 static procS cur_proc;
13200 static procS *cur_proc_ptr;
13201 static int numprocs;
13203 /* Fill in an rs_align_code fragment. */
13205 void
13206 mips_handle_align (fragp)
13207 fragS *fragp;
13209 if (fragp->fr_type != rs_align_code)
13210 return;
13212 if (mips_opts.mips16)
13214 static const unsigned char be_nop[] = { 0x65, 0x00 };
13215 static const unsigned char le_nop[] = { 0x00, 0x65 };
13217 int bytes;
13218 char *p;
13220 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13221 p = fragp->fr_literal + fragp->fr_fix;
13223 if (bytes & 1)
13225 *p++ = 0;
13226 fragp->fr_fix++;
13229 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13230 fragp->fr_var = 2;
13233 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
13236 static void
13237 md_obj_begin ()
13241 static void
13242 md_obj_end ()
13244 /* check for premature end, nesting errors, etc */
13245 if (cur_proc_ptr)
13246 as_warn (_("missing .end at end of assembly"));
13249 static long
13250 get_number ()
13252 int negative = 0;
13253 long val = 0;
13255 if (*input_line_pointer == '-')
13257 ++input_line_pointer;
13258 negative = 1;
13260 if (!ISDIGIT (*input_line_pointer))
13261 as_bad (_("expected simple number"));
13262 if (input_line_pointer[0] == '0')
13264 if (input_line_pointer[1] == 'x')
13266 input_line_pointer += 2;
13267 while (ISXDIGIT (*input_line_pointer))
13269 val <<= 4;
13270 val |= hex_value (*input_line_pointer++);
13272 return negative ? -val : val;
13274 else
13276 ++input_line_pointer;
13277 while (ISDIGIT (*input_line_pointer))
13279 val <<= 3;
13280 val |= *input_line_pointer++ - '0';
13282 return negative ? -val : val;
13285 if (!ISDIGIT (*input_line_pointer))
13287 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13288 *input_line_pointer, *input_line_pointer);
13289 as_warn (_("invalid number"));
13290 return -1;
13292 while (ISDIGIT (*input_line_pointer))
13294 val *= 10;
13295 val += *input_line_pointer++ - '0';
13297 return negative ? -val : val;
13300 /* The .file directive; just like the usual .file directive, but there
13301 is an initial number which is the ECOFF file index. In the non-ECOFF
13302 case .file implies DWARF-2. */
13304 static void
13305 s_mips_file (x)
13306 int x ATTRIBUTE_UNUSED;
13308 static int first_file_directive = 0;
13310 if (ECOFF_DEBUGGING)
13312 get_number ();
13313 s_app_file (0);
13315 else
13317 char *filename;
13319 filename = dwarf2_directive_file (0);
13321 /* Versions of GCC up to 3.1 start files with a ".file"
13322 directive even for stabs output. Make sure that this
13323 ".file" is handled. Note that you need a version of GCC
13324 after 3.1 in order to support DWARF-2 on MIPS. */
13325 if (filename != NULL && ! first_file_directive)
13327 (void) new_logical_line (filename, -1);
13328 s_app_file_string (filename);
13330 first_file_directive = 1;
13334 /* The .loc directive, implying DWARF-2. */
13336 static void
13337 s_mips_loc (x)
13338 int x ATTRIBUTE_UNUSED;
13340 if (!ECOFF_DEBUGGING)
13341 dwarf2_directive_loc (0);
13344 /* The .end directive. */
13346 static void
13347 s_mips_end (x)
13348 int x ATTRIBUTE_UNUSED;
13350 symbolS *p;
13351 int maybe_text;
13353 /* Following functions need their own .frame and .cprestore directives. */
13354 mips_frame_reg_valid = 0;
13355 mips_cprestore_valid = 0;
13357 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13359 p = get_symbol ();
13360 demand_empty_rest_of_line ();
13362 else
13363 p = NULL;
13365 #ifdef BFD_ASSEMBLER
13366 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13367 maybe_text = 1;
13368 else
13369 maybe_text = 0;
13370 #else
13371 if (now_seg != data_section && now_seg != bss_section)
13372 maybe_text = 1;
13373 else
13374 maybe_text = 0;
13375 #endif
13377 if (!maybe_text)
13378 as_warn (_(".end not in text section"));
13380 if (!cur_proc_ptr)
13382 as_warn (_(".end directive without a preceding .ent directive."));
13383 demand_empty_rest_of_line ();
13384 return;
13387 if (p != NULL)
13389 assert (S_GET_NAME (p));
13390 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13391 as_warn (_(".end symbol does not match .ent symbol."));
13393 if (debug_type == DEBUG_STABS)
13394 stabs_generate_asm_endfunc (S_GET_NAME (p),
13395 S_GET_NAME (p));
13397 else
13398 as_warn (_(".end directive missing or unknown symbol"));
13400 #ifdef OBJ_ELF
13401 /* Generate a .pdr section. */
13402 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13404 segT saved_seg = now_seg;
13405 subsegT saved_subseg = now_subseg;
13406 valueT dot;
13407 expressionS exp;
13408 char *fragp;
13410 dot = frag_now_fix ();
13412 #ifdef md_flush_pending_output
13413 md_flush_pending_output ();
13414 #endif
13416 assert (pdr_seg);
13417 subseg_set (pdr_seg, 0);
13419 /* Write the symbol. */
13420 exp.X_op = O_symbol;
13421 exp.X_add_symbol = p;
13422 exp.X_add_number = 0;
13423 emit_expr (&exp, 4);
13425 fragp = frag_more (7 * 4);
13427 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
13428 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
13429 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13430 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13431 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13432 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13433 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
13435 subseg_set (saved_seg, saved_subseg);
13437 #endif /* OBJ_ELF */
13439 cur_proc_ptr = NULL;
13442 /* The .aent and .ent directives. */
13444 static void
13445 s_mips_ent (aent)
13446 int aent;
13448 symbolS *symbolP;
13449 int maybe_text;
13451 symbolP = get_symbol ();
13452 if (*input_line_pointer == ',')
13453 ++input_line_pointer;
13454 SKIP_WHITESPACE ();
13455 if (ISDIGIT (*input_line_pointer)
13456 || *input_line_pointer == '-')
13457 get_number ();
13459 #ifdef BFD_ASSEMBLER
13460 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13461 maybe_text = 1;
13462 else
13463 maybe_text = 0;
13464 #else
13465 if (now_seg != data_section && now_seg != bss_section)
13466 maybe_text = 1;
13467 else
13468 maybe_text = 0;
13469 #endif
13471 if (!maybe_text)
13472 as_warn (_(".ent or .aent not in text section."));
13474 if (!aent && cur_proc_ptr)
13475 as_warn (_("missing .end"));
13477 if (!aent)
13479 /* This function needs its own .frame and .cprestore directives. */
13480 mips_frame_reg_valid = 0;
13481 mips_cprestore_valid = 0;
13483 cur_proc_ptr = &cur_proc;
13484 memset (cur_proc_ptr, '\0', sizeof (procS));
13486 cur_proc_ptr->isym = symbolP;
13488 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13490 ++numprocs;
13492 if (debug_type == DEBUG_STABS)
13493 stabs_generate_asm_func (S_GET_NAME (symbolP),
13494 S_GET_NAME (symbolP));
13497 demand_empty_rest_of_line ();
13500 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13501 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13502 s_mips_frame is used so that we can set the PDR information correctly.
13503 We can't use the ecoff routines because they make reference to the ecoff
13504 symbol table (in the mdebug section). */
13506 static void
13507 s_mips_frame (ignore)
13508 int ignore ATTRIBUTE_UNUSED;
13510 #ifdef OBJ_ELF
13511 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13513 long val;
13515 if (cur_proc_ptr == (procS *) NULL)
13517 as_warn (_(".frame outside of .ent"));
13518 demand_empty_rest_of_line ();
13519 return;
13522 cur_proc_ptr->frame_reg = tc_get_register (1);
13524 SKIP_WHITESPACE ();
13525 if (*input_line_pointer++ != ','
13526 || get_absolute_expression_and_terminator (&val) != ',')
13528 as_warn (_("Bad .frame directive"));
13529 --input_line_pointer;
13530 demand_empty_rest_of_line ();
13531 return;
13534 cur_proc_ptr->frame_offset = val;
13535 cur_proc_ptr->pc_reg = tc_get_register (0);
13537 demand_empty_rest_of_line ();
13539 else
13540 #endif /* OBJ_ELF */
13541 s_ignore (ignore);
13544 /* The .fmask and .mask directives. If the mdebug section is present
13545 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13546 embedded targets, s_mips_mask is used so that we can set the PDR
13547 information correctly. We can't use the ecoff routines because they
13548 make reference to the ecoff symbol table (in the mdebug section). */
13550 static void
13551 s_mips_mask (reg_type)
13552 char reg_type;
13554 #ifdef OBJ_ELF
13555 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13557 long mask, off;
13559 if (cur_proc_ptr == (procS *) NULL)
13561 as_warn (_(".mask/.fmask outside of .ent"));
13562 demand_empty_rest_of_line ();
13563 return;
13566 if (get_absolute_expression_and_terminator (&mask) != ',')
13568 as_warn (_("Bad .mask/.fmask directive"));
13569 --input_line_pointer;
13570 demand_empty_rest_of_line ();
13571 return;
13574 off = get_absolute_expression ();
13576 if (reg_type == 'F')
13578 cur_proc_ptr->fpreg_mask = mask;
13579 cur_proc_ptr->fpreg_offset = off;
13581 else
13583 cur_proc_ptr->reg_mask = mask;
13584 cur_proc_ptr->reg_offset = off;
13587 demand_empty_rest_of_line ();
13589 else
13590 #endif /* OBJ_ELF */
13591 s_ignore (reg_type);
13594 /* The .loc directive. */
13596 #if 0
13597 static void
13598 s_loc (x)
13599 int x;
13601 symbolS *symbolP;
13602 int lineno;
13603 int addroff;
13605 assert (now_seg == text_section);
13607 lineno = get_number ();
13608 addroff = frag_now_fix ();
13610 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13611 S_SET_TYPE (symbolP, N_SLINE);
13612 S_SET_OTHER (symbolP, 0);
13613 S_SET_DESC (symbolP, lineno);
13614 symbolP->sy_segment = now_seg;
13616 #endif
13618 /* A table describing all the processors gas knows about. Names are
13619 matched in the order listed.
13621 To ease comparison, please keep this table in the same order as
13622 gcc's mips_cpu_info_table[]. */
13623 static const struct mips_cpu_info mips_cpu_info_table[] =
13625 /* Entries for generic ISAs */
13626 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
13627 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
13628 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
13629 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
13630 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
13631 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
13632 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
13634 /* MIPS I */
13635 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
13636 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
13637 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
13639 /* MIPS II */
13640 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
13642 /* MIPS III */
13643 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
13644 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
13645 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
13646 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
13647 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
13648 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
13649 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
13650 { "orion", 0, ISA_MIPS3, CPU_R4600 },
13651 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
13653 /* MIPS IV */
13654 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
13655 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
13656 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
13657 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
13658 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
13659 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
13660 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
13661 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
13662 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
13663 { "r7000", 0, ISA_MIPS4, CPU_R5000 },
13665 /* MIPS 32 */
13666 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
13667 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
13668 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
13670 /* MIPS 64 */
13671 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
13672 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
13674 /* Broadcom SB-1 CPU core */
13675 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
13677 /* End marker */
13678 { NULL, 0, 0, 0 }
13682 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13683 with a final "000" replaced by "k". Ignore case.
13685 Note: this function is shared between GCC and GAS. */
13687 static boolean
13688 mips_strict_matching_cpu_name_p (canonical, given)
13689 const char *canonical, *given;
13691 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13692 given++, canonical++;
13694 return ((*given == 0 && *canonical == 0)
13695 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13699 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13700 CPU name. We've traditionally allowed a lot of variation here.
13702 Note: this function is shared between GCC and GAS. */
13704 static boolean
13705 mips_matching_cpu_name_p (canonical, given)
13706 const char *canonical, *given;
13708 /* First see if the name matches exactly, or with a final "000"
13709 turned into "k". */
13710 if (mips_strict_matching_cpu_name_p (canonical, given))
13711 return true;
13713 /* If not, try comparing based on numerical designation alone.
13714 See if GIVEN is an unadorned number, or 'r' followed by a number. */
13715 if (TOLOWER (*given) == 'r')
13716 given++;
13717 if (!ISDIGIT (*given))
13718 return false;
13720 /* Skip over some well-known prefixes in the canonical name,
13721 hoping to find a number there too. */
13722 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13723 canonical += 2;
13724 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13725 canonical += 2;
13726 else if (TOLOWER (canonical[0]) == 'r')
13727 canonical += 1;
13729 return mips_strict_matching_cpu_name_p (canonical, given);
13733 /* Parse an option that takes the name of a processor as its argument.
13734 OPTION is the name of the option and CPU_STRING is the argument.
13735 Return the corresponding processor enumeration if the CPU_STRING is
13736 recognized, otherwise report an error and return null.
13738 A similar function exists in GCC. */
13740 static const struct mips_cpu_info *
13741 mips_parse_cpu (option, cpu_string)
13742 const char *option, *cpu_string;
13744 const struct mips_cpu_info *p;
13746 /* 'from-abi' selects the most compatible architecture for the given
13747 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
13748 EABIs, we have to decide whether we're using the 32-bit or 64-bit
13749 version. Look first at the -mgp options, if given, otherwise base
13750 the choice on MIPS_DEFAULT_64BIT.
13752 Treat NO_ABI like the EABIs. One reason to do this is that the
13753 plain 'mips' and 'mips64' configs have 'from-abi' as their default
13754 architecture. This code picks MIPS I for 'mips' and MIPS III for
13755 'mips64', just as we did in the days before 'from-abi'. */
13756 if (strcasecmp (cpu_string, "from-abi") == 0)
13758 if (ABI_NEEDS_32BIT_REGS (mips_abi))
13759 return mips_cpu_info_from_isa (ISA_MIPS1);
13761 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13762 return mips_cpu_info_from_isa (ISA_MIPS3);
13764 if (file_mips_gp32 >= 0)
13765 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13767 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13768 ? ISA_MIPS3
13769 : ISA_MIPS1);
13772 /* 'default' has traditionally been a no-op. Probably not very useful. */
13773 if (strcasecmp (cpu_string, "default") == 0)
13774 return 0;
13776 for (p = mips_cpu_info_table; p->name != 0; p++)
13777 if (mips_matching_cpu_name_p (p->name, cpu_string))
13778 return p;
13780 as_bad ("Bad value (%s) for %s", cpu_string, option);
13781 return 0;
13784 /* Return the canonical processor information for ISA (a member of the
13785 ISA_MIPS* enumeration). */
13787 static const struct mips_cpu_info *
13788 mips_cpu_info_from_isa (isa)
13789 int isa;
13791 int i;
13793 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13794 if (mips_cpu_info_table[i].is_isa
13795 && isa == mips_cpu_info_table[i].isa)
13796 return (&mips_cpu_info_table[i]);
13798 return NULL;
13801 static void
13802 show (stream, string, col_p, first_p)
13803 FILE *stream;
13804 const char *string;
13805 int *col_p;
13806 int *first_p;
13808 if (*first_p)
13810 fprintf (stream, "%24s", "");
13811 *col_p = 24;
13813 else
13815 fprintf (stream, ", ");
13816 *col_p += 2;
13819 if (*col_p + strlen (string) > 72)
13821 fprintf (stream, "\n%24s", "");
13822 *col_p = 24;
13825 fprintf (stream, "%s", string);
13826 *col_p += strlen (string);
13828 *first_p = 0;
13831 void
13832 md_show_usage (stream)
13833 FILE *stream;
13835 int column, first;
13836 size_t i;
13838 fprintf (stream, _("\
13839 MIPS options:\n\
13840 -membedded-pic generate embedded position independent code\n\
13841 -EB generate big endian output\n\
13842 -EL generate little endian output\n\
13843 -g, -g2 do not remove unneeded NOPs or swap branches\n\
13844 -G NUM allow referencing objects up to NUM bytes\n\
13845 implicitly with the gp register [default 8]\n"));
13846 fprintf (stream, _("\
13847 -mips1 generate MIPS ISA I instructions\n\
13848 -mips2 generate MIPS ISA II instructions\n\
13849 -mips3 generate MIPS ISA III instructions\n\
13850 -mips4 generate MIPS ISA IV instructions\n\
13851 -mips5 generate MIPS ISA V instructions\n\
13852 -mips32 generate MIPS32 ISA instructions\n\
13853 -mips64 generate MIPS64 ISA instructions\n\
13854 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
13856 first = 1;
13858 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13859 show (stream, mips_cpu_info_table[i].name, &column, &first);
13860 show (stream, "from-abi", &column, &first);
13861 fputc ('\n', stream);
13863 fprintf (stream, _("\
13864 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
13865 -no-mCPU don't generate code specific to CPU.\n\
13866 For -mCPU and -no-mCPU, CPU must be one of:\n"));
13868 first = 1;
13870 show (stream, "3900", &column, &first);
13871 show (stream, "4010", &column, &first);
13872 show (stream, "4100", &column, &first);
13873 show (stream, "4650", &column, &first);
13874 fputc ('\n', stream);
13876 fprintf (stream, _("\
13877 -mips16 generate mips16 instructions\n\
13878 -no-mips16 do not generate mips16 instructions\n"));
13879 fprintf (stream, _("\
13880 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
13881 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
13882 -O0 remove unneeded NOPs, do not swap branches\n\
13883 -O remove unneeded NOPs and swap branches\n\
13884 -n warn about NOPs generated from macros\n\
13885 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
13886 --trap, --no-break trap exception on div by 0 and mult overflow\n\
13887 --break, --no-trap break exception on div by 0 and mult overflow\n"));
13888 #ifdef OBJ_ELF
13889 fprintf (stream, _("\
13890 -KPIC, -call_shared generate SVR4 position independent code\n\
13891 -non_shared do not generate position independent code\n\
13892 -xgot assume a 32 bit GOT\n\
13893 -mabi=ABI create ABI conformant object file for:\n"));
13895 first = 1;
13897 show (stream, "32", &column, &first);
13898 show (stream, "o64", &column, &first);
13899 show (stream, "n32", &column, &first);
13900 show (stream, "64", &column, &first);
13901 show (stream, "eabi", &column, &first);
13903 fputc ('\n', stream);
13905 fprintf (stream, _("\
13906 -32 create o32 ABI object file (default)\n\
13907 -n32 create n32 ABI object file\n\
13908 -64 create 64 ABI object file\n"));
13909 #endif