*** empty log message ***
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
f7e42eb4 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
82efde3a 3 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
25
26#include "as.h"
27#include "config.h"
28#include "subsegs.h"
29
30#include <ctype.h>
31
32#ifdef USE_STDARG
33#include <stdarg.h>
34#endif
35#ifdef USE_VARARGS
36#include <varargs.h>
37#endif
38
39#include "opcode/mips.h"
40#include "itbl-ops.h"
41
42#ifdef DEBUG
43#define DBG(x) printf x
44#else
45#define DBG(x)
46#endif
47
48#ifdef OBJ_MAYBE_ELF
49/* Clean up namespace so we can include obj-elf.h too. */
50static int mips_output_flavor PARAMS ((void));
51static 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
252b5132
RH
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
64
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
70
71#if defined (OBJ_ELF)
72#include "elf/mips.h"
73#endif
74
75#ifndef ECOFF_DEBUGGING
76#define NO_ECOFF_DEBUGGING
77#define ECOFF_DEBUGGING 0
78#endif
79
80#include "ecoff.h"
81
82#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
83static char *mips_regmask_frag;
84#endif
85
86#define AT 1
87#define TREG 24
88#define PIC_CALL_REG 25
89#define KT0 26
90#define KT1 27
91#define GP 28
92#define SP 29
93#define FP 30
94#define RA 31
95
96#define ILLEGAL_REG (32)
97
98/* Allow override of standard little-endian ECOFF format. */
99
100#ifndef ECOFF_LITTLE_FORMAT
101#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
102#endif
103
104extern int target_big_endian;
105
106/* 1 is we should use the 64 bit MIPS ELF ABI, 0 if we should use the
107 32 bit ABI. This has no meaning for ECOFF.
108 Note that the default is always 32 bit, even if "configured" for
109 64 bit [e.g. --target=mips64-elf]. */
110static int mips_64;
111
112/* The default target format to use. */
beae10d5 113
252b5132
RH
114const char *
115mips_target_format ()
116{
117 switch (OUTPUT_FLAVOR)
118 {
119 case bfd_target_aout_flavour:
120 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
121 case bfd_target_ecoff_flavour:
122 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
056350c6
NC
123 case bfd_target_coff_flavour:
124 return "pe-mips";
252b5132 125 case bfd_target_elf_flavour:
8614eeee
UC
126#ifdef TE_TMIPS
127 /* This is traditional mips */
128 return (target_big_endian
af9539e4
L
129 ? (mips_64 ? "elf64-tradbigmips" : "elf32-tradbigmips")
130 : (mips_64 ? "elf64-tradlittlemips" : "elf32-tradlittlemips"));
8614eeee 131#else
252b5132
RH
132 return (target_big_endian
133 ? (mips_64 ? "elf64-bigmips" : "elf32-bigmips")
134 : (mips_64 ? "elf64-littlemips" : "elf32-littlemips"));
8614eeee 135#endif
252b5132
RH
136 default:
137 abort ();
138 return NULL;
139 }
140}
141
142/* The name of the readonly data section. */
143#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
144 ? ".data" \
145 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
146 ? ".rdata" \
056350c6
NC
147 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
148 ? ".rdata" \
252b5132
RH
149 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
150 ? ".rodata" \
151 : (abort (), ""))
152
153/* This is the set of options which may be modified by the .set
154 pseudo-op. We use a struct so that .set push and .set pop are more
155 reliable. */
156
e972090a
NC
157struct mips_set_options
158{
252b5132
RH
159 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
160 if it has not been initialized. Changed by `.set mipsN', and the
161 -mipsN command line option, and the default CPU. */
162 int isa;
163 /* Whether we are assembling for the mips16 processor. 0 if we are
164 not, 1 if we are, and -1 if the value has not been initialized.
165 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
166 -nomips16 command line options, and the default CPU. */
167 int mips16;
168 /* Non-zero if we should not reorder instructions. Changed by `.set
169 reorder' and `.set noreorder'. */
170 int noreorder;
171 /* Non-zero if we should not permit the $at ($1) register to be used
172 in instructions. Changed by `.set at' and `.set noat'. */
173 int noat;
174 /* Non-zero if we should warn when a macro instruction expands into
175 more than one machine instruction. Changed by `.set nomacro' and
176 `.set macro'. */
177 int warn_about_macros;
178 /* Non-zero if we should not move instructions. Changed by `.set
179 move', `.set volatile', `.set nomove', and `.set novolatile'. */
180 int nomove;
181 /* Non-zero if we should not optimize branches by moving the target
182 of the branch into the delay slot. Actually, we don't perform
183 this optimization anyhow. Changed by `.set bopt' and `.set
184 nobopt'. */
185 int nobopt;
186 /* Non-zero if we should not autoextend mips16 instructions.
187 Changed by `.set autoextend' and `.set noautoextend'. */
188 int noautoextend;
189};
190
191/* This is the struct we use to hold the current set of options. Note
e7af610e
NC
192 that we must set the isa field to ISA_UNKNOWN and the mips16 field to
193 -1 to indicate that they have not been initialized. */
252b5132 194
e972090a
NC
195static struct mips_set_options mips_opts =
196{
e7af610e
NC
197 ISA_UNKNOWN, -1, 0, 0, 0, 0, 0, 0
198};
252b5132
RH
199
200/* These variables are filled in with the masks of registers used.
201 The object format code reads them and puts them in the appropriate
202 place. */
203unsigned long mips_gprmask;
204unsigned long mips_cprmask[4];
205
206/* MIPS ISA we are using for this output file. */
e7af610e 207static int file_mips_isa = ISA_UNKNOWN;
252b5132 208
ec68c924 209/* The argument of the -mcpu= flag. Historical for code generation. */
e7af610e 210static int mips_cpu = CPU_UNKNOWN;
252b5132 211
ec68c924
EC
212/* The argument of the -march= flag. The architecture we are assembling. */
213static int mips_arch = CPU_UNKNOWN;
214
215/* The argument of the -mtune= flag. The architecture for which we
216 are optimizing. */
217static int mips_tune = CPU_UNKNOWN;
218
bdaaa2e1 219/* The argument of the -mabi= flag. */
9a41af64 220static char * mips_abi_string = NULL;
252b5132 221
2f4dcb11 222/* Whether we should mark the file EABI64 or EABI32. */
252b5132
RH
223static int mips_eabi64 = 0;
224
225/* If they asked for mips1 or mips2 and a cpu that is
bdaaa2e1 226 mips3 or greater, then mark the object file 32BITMODE. */
252b5132
RH
227static int mips_32bitmode = 0;
228
bdaaa2e1 229/* True if -mgp32 was passed. */
c97ef257
AH
230static int mips_gp32 = 0;
231
ca4e0257
RS
232/* True if -mfp32 was passed. */
233static int mips_fp32 = 0;
234
235/* True if the selected ABI is defined for 32-bit registers only. */
236static int mips_32bit_abi = 0;
237
9ce8a5dd
GRK
238/* Some ISA's have delay slots for instructions which read or write
239 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
bdaaa2e1 240 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
9ce8a5dd
GRK
241 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
242 delay slot in this ISA. The uses of this macro assume that any
243 ISA that has delay slots for one of these, has them for all. They
244 also assume that ISAs which don't have delays for these insns, don't
bdaaa2e1 245 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
9ce8a5dd 246#define ISA_HAS_COPROC_DELAYS(ISA) ( \
e7af610e
NC
247 (ISA) == ISA_MIPS1 \
248 || (ISA) == ISA_MIPS2 \
249 || (ISA) == ISA_MIPS3 \
9ce8a5dd
GRK
250 )
251
bdaaa2e1 252/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 253#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
254 (ISA) == ISA_MIPS3 \
255 || (ISA) == ISA_MIPS4 \
84ea6cf2 256 || (ISA) == ISA_MIPS5 \
d1cf510e 257 || (ISA) == ISA_MIPS64 \
9ce8a5dd
GRK
258 )
259
ca4e0257
RS
260#define HAVE_32BIT_GPRS \
261 (mips_gp32 \
262 || mips_32bit_abi \
263 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
264
265#define HAVE_32BIT_FPRS \
266 (mips_fp32 \
267 || mips_32bit_abi \
268 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
269
270#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
271#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
272
273#define HAVE_32BIT_ADDRESSES \
274 (HAVE_32BIT_GPRS \
275 || bfd_arch_bits_per_address (stdoutput) == 32)
276
bdaaa2e1 277/* Whether the processor uses hardware interlocks to protect
252b5132 278 reads from the HI and LO registers, and thus does not
ec68c924 279 require nops to be inserted. */
252b5132 280
ec68c924 281#define hilo_interlocks (mips_arch == CPU_R4010 \
252b5132
RH
282 )
283
284/* Whether the processor uses hardware interlocks to protect reads
285 from the GPRs, and thus does not require nops to be inserted. */
286#define gpr_interlocks \
e7af610e 287 (mips_opts.isa != ISA_MIPS1 \
ec68c924 288 || mips_arch == CPU_R3900)
252b5132
RH
289
290/* As with other "interlocks" this is used by hardware that has FP
291 (co-processor) interlocks. */
bdaaa2e1 292/* Itbl support may require additional care here. */
ec68c924 293#define cop_interlocks (mips_arch == CPU_R4300 \
252b5132
RH
294 )
295
6b76fefe
CM
296/* Is this a mfhi or mflo instruction? */
297#define MF_HILO_INSN(PINFO) \
298 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
299
252b5132
RH
300/* MIPS PIC level. */
301
e972090a
NC
302enum mips_pic_level
303{
252b5132
RH
304 /* Do not generate PIC code. */
305 NO_PIC,
306
307 /* Generate PIC code as in Irix 4. This is not implemented, and I'm
308 not sure what it is supposed to do. */
309 IRIX4_PIC,
310
311 /* Generate PIC code as in the SVR4 MIPS ABI. */
312 SVR4_PIC,
313
314 /* Generate PIC code without using a global offset table: the data
315 segment has a maximum size of 64K, all data references are off
316 the $gp register, and all text references are PC relative. This
317 is used on some embedded systems. */
318 EMBEDDED_PIC
319};
320
321static enum mips_pic_level mips_pic;
322
39c0a331
L
323/* Warn about all NOPS that the assembler generates. */
324static int warn_nops = 0;
325
252b5132
RH
326/* 1 if we should generate 32 bit offsets from the GP register in
327 SVR4_PIC mode. Currently has no meaning in other modes. */
328static int mips_big_got;
329
330/* 1 if trap instructions should used for overflow rather than break
331 instructions. */
332static int mips_trap;
333
119d663a
NC
334/* 1 if double width floating point constants should not be constructed
335 by a assembling two single width halves into two single width floating
336 point registers which just happen to alias the double width destination
337 register. On some architectures this aliasing can be disabled by a bit
d547a75e 338 in the status register, and the setting of this bit cannot be determined
119d663a
NC
339 automatically at assemble time. */
340static int mips_disable_float_construction;
341
252b5132
RH
342/* Non-zero if any .set noreorder directives were used. */
343
344static int mips_any_noreorder;
345
6b76fefe
CM
346/* Non-zero if nops should be inserted when the register referenced in
347 an mfhi/mflo instruction is read in the next two instructions. */
348static int mips_7000_hilo_fix;
349
252b5132 350/* The size of the small data section. */
156c2f8b 351static unsigned int g_switch_value = 8;
252b5132
RH
352/* Whether the -G option was used. */
353static int g_switch_seen = 0;
354
355#define N_RMASK 0xc4
356#define N_VFP 0xd4
357
358/* If we can determine in advance that GP optimization won't be
359 possible, we can skip the relaxation stuff that tries to produce
360 GP-relative references. This makes delay slot optimization work
361 better.
362
363 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
364 gcc output. It needs to guess right for gcc, otherwise gcc
365 will put what it thinks is a GP-relative instruction in a branch
366 delay slot.
252b5132
RH
367
368 I don't know if a fix is needed for the SVR4_PIC mode. I've only
369 fixed it for the non-PIC mode. KR 95/04/07 */
370static int nopic_need_relax PARAMS ((symbolS *, int));
371
372/* handle of the OPCODE hash table */
373static struct hash_control *op_hash = NULL;
374
375/* The opcode hash table we use for the mips16. */
376static struct hash_control *mips16_op_hash = NULL;
377
378/* This array holds the chars that always start a comment. If the
379 pre-processor is disabled, these aren't very useful */
380const char comment_chars[] = "#";
381
382/* This array holds the chars that only start a comment at the beginning of
383 a line. If the line seems to have the form '# 123 filename'
384 .line and .file directives will appear in the pre-processed output */
385/* Note that input_file.c hand checks for '#' at the beginning of the
386 first line of the input file. This is because the compiler outputs
bdaaa2e1 387 #NO_APP at the beginning of its output. */
252b5132
RH
388/* Also note that C style comments are always supported. */
389const char line_comment_chars[] = "#";
390
bdaaa2e1 391/* This array holds machine specific line separator characters. */
63a0b638 392const char line_separator_chars[] = ";";
252b5132
RH
393
394/* Chars that can be used to separate mant from exp in floating point nums */
395const char EXP_CHARS[] = "eE";
396
397/* Chars that mean this number is a floating point constant */
398/* As in 0f12.456 */
399/* or 0d1.2345e12 */
400const char FLT_CHARS[] = "rRsSfFdDxXpP";
401
402/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
403 changed in read.c . Ideally it shouldn't have to know about it at all,
404 but nothing is ideal around here.
405 */
406
407static char *insn_error;
408
409static int auto_align = 1;
410
411/* When outputting SVR4 PIC code, the assembler needs to know the
412 offset in the stack frame from which to restore the $gp register.
413 This is set by the .cprestore pseudo-op, and saved in this
414 variable. */
415static offsetT mips_cprestore_offset = -1;
416
417/* This is the register which holds the stack frame, as set by the
418 .frame pseudo-op. This is needed to implement .cprestore. */
419static int mips_frame_reg = SP;
420
421/* To output NOP instructions correctly, we need to keep information
422 about the previous two instructions. */
423
424/* Whether we are optimizing. The default value of 2 means to remove
425 unneeded NOPs and swap branch instructions when possible. A value
426 of 1 means to not swap branches. A value of 0 means to always
427 insert NOPs. */
428static int mips_optimize = 2;
429
430/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
431 equivalent to seeing no -g option at all. */
432static int mips_debug = 0;
433
434/* The previous instruction. */
435static struct mips_cl_insn prev_insn;
436
437/* The instruction before prev_insn. */
438static struct mips_cl_insn prev_prev_insn;
439
440/* If we don't want information for prev_insn or prev_prev_insn, we
441 point the insn_mo field at this dummy integer. */
43841e91 442static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
443
444/* Non-zero if prev_insn is valid. */
445static int prev_insn_valid;
446
447/* The frag for the previous instruction. */
448static struct frag *prev_insn_frag;
449
450/* The offset into prev_insn_frag for the previous instruction. */
451static long prev_insn_where;
452
453/* The reloc type for the previous instruction, if any. */
454static bfd_reloc_code_real_type prev_insn_reloc_type;
455
456/* The reloc for the previous instruction, if any. */
457static fixS *prev_insn_fixp;
458
459/* Non-zero if the previous instruction was in a delay slot. */
460static int prev_insn_is_delay_slot;
461
462/* Non-zero if the previous instruction was in a .set noreorder. */
463static int prev_insn_unreordered;
464
465/* Non-zero if the previous instruction uses an extend opcode (if
466 mips16). */
467static int prev_insn_extended;
468
469/* Non-zero if the previous previous instruction was in a .set
470 noreorder. */
471static int prev_prev_insn_unreordered;
472
473/* If this is set, it points to a frag holding nop instructions which
474 were inserted before the start of a noreorder section. If those
475 nops turn out to be unnecessary, the size of the frag can be
476 decreased. */
477static fragS *prev_nop_frag;
478
479/* The number of nop instructions we created in prev_nop_frag. */
480static int prev_nop_frag_holds;
481
482/* The number of nop instructions that we know we need in
bdaaa2e1 483 prev_nop_frag. */
252b5132
RH
484static int prev_nop_frag_required;
485
486/* The number of instructions we've seen since prev_nop_frag. */
487static int prev_nop_frag_since;
488
489/* For ECOFF and ELF, relocations against symbols are done in two
490 parts, with a HI relocation and a LO relocation. Each relocation
491 has only 16 bits of space to store an addend. This means that in
492 order for the linker to handle carries correctly, it must be able
493 to locate both the HI and the LO relocation. This means that the
494 relocations must appear in order in the relocation table.
495
496 In order to implement this, we keep track of each unmatched HI
497 relocation. We then sort them so that they immediately precede the
bdaaa2e1 498 corresponding LO relocation. */
252b5132 499
e972090a
NC
500struct mips_hi_fixup
501{
252b5132
RH
502 /* Next HI fixup. */
503 struct mips_hi_fixup *next;
504 /* This fixup. */
505 fixS *fixp;
506 /* The section this fixup is in. */
507 segT seg;
508};
509
510/* The list of unmatched HI relocs. */
511
512static struct mips_hi_fixup *mips_hi_fixup_list;
513
514/* Map normal MIPS register numbers to mips16 register numbers. */
515
516#define X ILLEGAL_REG
e972090a
NC
517static const int mips32_to_16_reg_map[] =
518{
252b5132
RH
519 X, X, 2, 3, 4, 5, 6, 7,
520 X, X, X, X, X, X, X, X,
521 0, 1, X, X, X, X, X, X,
522 X, X, X, X, X, X, X, X
523};
524#undef X
525
526/* Map mips16 register numbers to normal MIPS register numbers. */
527
e972090a
NC
528static const unsigned int mips16_to_32_reg_map[] =
529{
252b5132
RH
530 16, 17, 2, 3, 4, 5, 6, 7
531};
532\f
533/* Since the MIPS does not have multiple forms of PC relative
534 instructions, we do not have to do relaxing as is done on other
535 platforms. However, we do have to handle GP relative addressing
536 correctly, which turns out to be a similar problem.
537
538 Every macro that refers to a symbol can occur in (at least) two
539 forms, one with GP relative addressing and one without. For
540 example, loading a global variable into a register generally uses
541 a macro instruction like this:
542 lw $4,i
543 If i can be addressed off the GP register (this is true if it is in
544 the .sbss or .sdata section, or if it is known to be smaller than
545 the -G argument) this will generate the following instruction:
546 lw $4,i($gp)
547 This instruction will use a GPREL reloc. If i can not be addressed
548 off the GP register, the following instruction sequence will be used:
549 lui $at,i
550 lw $4,i($at)
551 In this case the first instruction will have a HI16 reloc, and the
552 second reloc will have a LO16 reloc. Both relocs will be against
553 the symbol i.
554
555 The issue here is that we may not know whether i is GP addressable
556 until after we see the instruction that uses it. Therefore, we
557 want to be able to choose the final instruction sequence only at
558 the end of the assembly. This is similar to the way other
559 platforms choose the size of a PC relative instruction only at the
560 end of assembly.
561
562 When generating position independent code we do not use GP
563 addressing in quite the same way, but the issue still arises as
564 external symbols and local symbols must be handled differently.
565
566 We handle these issues by actually generating both possible
567 instruction sequences. The longer one is put in a frag_var with
568 type rs_machine_dependent. We encode what to do with the frag in
569 the subtype field. We encode (1) the number of existing bytes to
570 replace, (2) the number of new bytes to use, (3) the offset from
571 the start of the existing bytes to the first reloc we must generate
572 (that is, the offset is applied from the start of the existing
573 bytes after they are replaced by the new bytes, if any), (4) the
574 offset from the start of the existing bytes to the second reloc,
575 (5) whether a third reloc is needed (the third reloc is always four
576 bytes after the second reloc), and (6) whether to warn if this
577 variant is used (this is sometimes needed if .set nomacro or .set
578 noat is in effect). All these numbers are reasonably small.
579
580 Generating two instruction sequences must be handled carefully to
581 ensure that delay slots are handled correctly. Fortunately, there
582 are a limited number of cases. When the second instruction
583 sequence is generated, append_insn is directed to maintain the
584 existing delay slot information, so it continues to apply to any
585 code after the second instruction sequence. This means that the
586 second instruction sequence must not impose any requirements not
587 required by the first instruction sequence.
588
589 These variant frags are then handled in functions called by the
590 machine independent code. md_estimate_size_before_relax returns
591 the final size of the frag. md_convert_frag sets up the final form
592 of the frag. tc_gen_reloc adjust the first reloc and adds a second
593 one if needed. */
594#define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
595 ((relax_substateT) \
596 (((old) << 23) \
597 | ((new) << 16) \
598 | (((reloc1) + 64) << 9) \
599 | (((reloc2) + 64) << 2) \
600 | ((reloc3) ? (1 << 1) : 0) \
601 | ((warn) ? 1 : 0)))
602#define RELAX_OLD(i) (((i) >> 23) & 0x7f)
603#define RELAX_NEW(i) (((i) >> 16) & 0x7f)
9a41af64
TS
604#define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
605#define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
252b5132
RH
606#define RELAX_RELOC3(i) (((i) >> 1) & 1)
607#define RELAX_WARN(i) ((i) & 1)
608
609/* For mips16 code, we use an entirely different form of relaxation.
610 mips16 supports two versions of most instructions which take
611 immediate values: a small one which takes some small value, and a
612 larger one which takes a 16 bit value. Since branches also follow
613 this pattern, relaxing these values is required.
614
615 We can assemble both mips16 and normal MIPS code in a single
616 object. Therefore, we need to support this type of relaxation at
617 the same time that we support the relaxation described above. We
618 use the high bit of the subtype field to distinguish these cases.
619
620 The information we store for this type of relaxation is the
621 argument code found in the opcode file for this relocation, whether
622 the user explicitly requested a small or extended form, and whether
623 the relocation is in a jump or jal delay slot. That tells us the
624 size of the value, and how it should be stored. We also store
625 whether the fragment is considered to be extended or not. We also
626 store whether this is known to be a branch to a different section,
627 whether we have tried to relax this frag yet, and whether we have
628 ever extended a PC relative fragment because of a shift count. */
629#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
630 (0x80000000 \
631 | ((type) & 0xff) \
632 | ((small) ? 0x100 : 0) \
633 | ((ext) ? 0x200 : 0) \
634 | ((dslot) ? 0x400 : 0) \
635 | ((jal_dslot) ? 0x800 : 0))
636#define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
637#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
638#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
639#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
640#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
641#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
642#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
643#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
644#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
645#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
646#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
647#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
648\f
649/* Prototypes for static functions. */
650
651#ifdef __STDC__
652#define internalError() \
653 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
654#else
655#define internalError() as_fatal (_("MIPS internal Error"));
656#endif
657
658enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
659
660static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
661 unsigned int reg, enum mips_regclass class));
156c2f8b 662static int reg_needs_delay PARAMS ((unsigned int));
252b5132
RH
663static void mips16_mark_labels PARAMS ((void));
664static void append_insn PARAMS ((char *place,
665 struct mips_cl_insn * ip,
666 expressionS * p,
667 bfd_reloc_code_real_type r,
668 boolean));
669static void mips_no_prev_insn PARAMS ((int));
670static void mips_emit_delays PARAMS ((boolean));
671#ifdef USE_STDARG
672static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
673 const char *name, const char *fmt,
674 ...));
675#else
676static void macro_build ();
677#endif
678static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
679 const char *, const char *,
680 va_list));
681static void macro_build_lui PARAMS ((char *place, int *counter,
682 expressionS * ep, int regnum));
683static void set_at PARAMS ((int *counter, int reg, int unsignedp));
684static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
685 expressionS *));
686static void load_register PARAMS ((int *, int, expressionS *, int));
687static void load_address PARAMS ((int *counter, int reg, expressionS *ep));
ea1fb5dc 688static void move_register PARAMS ((int *, int, int));
252b5132
RH
689static void macro PARAMS ((struct mips_cl_insn * ip));
690static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
691#ifdef LOSING_COMPILER
692static void macro2 PARAMS ((struct mips_cl_insn * ip));
693#endif
694static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
695static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
696static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
697 boolean, boolean, unsigned long *,
698 boolean *, unsigned short *));
699static int my_getSmallExpression PARAMS ((expressionS * ep, char *str));
700static void my_getExpression PARAMS ((expressionS * ep, char *str));
701static symbolS *get_symbol PARAMS ((void));
702static void mips_align PARAMS ((int to, int fill, symbolS *label));
703static void s_align PARAMS ((int));
704static void s_change_sec PARAMS ((int));
705static void s_cons PARAMS ((int));
706static void s_float_cons PARAMS ((int));
707static void s_mips_globl PARAMS ((int));
708static void s_option PARAMS ((int));
709static void s_mipsset PARAMS ((int));
710static void s_abicalls PARAMS ((int));
711static void s_cpload PARAMS ((int));
712static void s_cprestore PARAMS ((int));
713static void s_gpword PARAMS ((int));
714static void s_cpadd PARAMS ((int));
715static void s_insn PARAMS ((int));
716static void md_obj_begin PARAMS ((void));
717static void md_obj_end PARAMS ((void));
718static long get_number PARAMS ((void));
719static void s_mips_ent PARAMS ((int));
720static void s_mips_end PARAMS ((int));
721static void s_mips_frame PARAMS ((int));
722static void s_mips_mask PARAMS ((int));
723static void s_mips_stab PARAMS ((int));
724static void s_mips_weakext PARAMS ((int));
725static void s_file PARAMS ((int));
726static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
e7af610e
NC
727static const char *mips_isa_to_str PARAMS ((int));
728static const char *mips_cpu_to_str PARAMS ((int));
252b5132 729static int validate_mips_insn PARAMS ((const struct mips_opcode *));
e87a0284 730static void show PARAMS ((FILE *, char *, int *, int *));
e7af610e 731
fb1b3232
TS
732/* Return values of my_getSmallExpression() */
733
734enum
735{
736 S_EX_NONE = 0,
737 S_EX_LO,
738 S_EX_HI,
739 S_EX_HIGHER,
740 S_EX_HIGHEST,
741 S_EX_GPREL,
742 S_EX_NEG
743};
744
e7af610e
NC
745/* Table and functions used to map between CPU/ISA names, and
746 ISA levels, and CPU numbers. */
747
e972090a
NC
748struct mips_cpu_info
749{
e7af610e
NC
750 const char *name; /* CPU or ISA name. */
751 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
752 int isa; /* ISA level. */
753 int cpu; /* CPU number (default CPU if ISA). */
754};
755
756static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
757static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
758static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
252b5132
RH
759\f
760/* Pseudo-op table.
761
762 The following pseudo-ops from the Kane and Heinrich MIPS book
763 should be defined here, but are currently unsupported: .alias,
764 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
765
766 The following pseudo-ops from the Kane and Heinrich MIPS book are
767 specific to the type of debugging information being generated, and
768 should be defined by the object format: .aent, .begin, .bend,
769 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
770 .vreg.
771
772 The following pseudo-ops from the Kane and Heinrich MIPS book are
773 not MIPS CPU specific, but are also not specific to the object file
774 format. This file is probably the best place to define them, but
775 they are not currently supported: .asm0, .endr, .lab, .repeat,
776 .struct. */
777
e972090a
NC
778static const pseudo_typeS mips_pseudo_table[] =
779{
beae10d5 780 /* MIPS specific pseudo-ops. */
252b5132
RH
781 {"option", s_option, 0},
782 {"set", s_mipsset, 0},
783 {"rdata", s_change_sec, 'r'},
784 {"sdata", s_change_sec, 's'},
785 {"livereg", s_ignore, 0},
786 {"abicalls", s_abicalls, 0},
787 {"cpload", s_cpload, 0},
788 {"cprestore", s_cprestore, 0},
789 {"gpword", s_gpword, 0},
790 {"cpadd", s_cpadd, 0},
791 {"insn", s_insn, 0},
792
beae10d5 793 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
794 chips. */
795 {"asciiz", stringer, 1},
796 {"bss", s_change_sec, 'b'},
797 {"err", s_err, 0},
798 {"half", s_cons, 1},
799 {"dword", s_cons, 3},
800 {"weakext", s_mips_weakext, 0},
801
beae10d5 802 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
803 here for one reason or another. */
804 {"align", s_align, 0},
805 {"byte", s_cons, 0},
806 {"data", s_change_sec, 'd'},
807 {"double", s_float_cons, 'd'},
808 {"float", s_float_cons, 'f'},
809 {"globl", s_mips_globl, 0},
810 {"global", s_mips_globl, 0},
811 {"hword", s_cons, 1},
812 {"int", s_cons, 2},
813 {"long", s_cons, 2},
814 {"octa", s_cons, 4},
815 {"quad", s_cons, 3},
816 {"short", s_cons, 1},
817 {"single", s_float_cons, 'f'},
818 {"stabn", s_mips_stab, 'n'},
819 {"text", s_change_sec, 't'},
820 {"word", s_cons, 2},
add56521
L
821
822#ifdef MIPS_STABS_ELF
823 { "extern", ecoff_directive_extern, 0},
824#endif
825
43841e91 826 { NULL, NULL, 0 },
252b5132
RH
827};
828
e972090a
NC
829static const pseudo_typeS mips_nonecoff_pseudo_table[] =
830{
beae10d5
KH
831 /* These pseudo-ops should be defined by the object file format.
832 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
833 {"aent", s_mips_ent, 1},
834 {"bgnb", s_ignore, 0},
835 {"end", s_mips_end, 0},
836 {"endb", s_ignore, 0},
837 {"ent", s_mips_ent, 0},
838 {"file", s_file, 0},
839 {"fmask", s_mips_mask, 'F'},
840 {"frame", s_mips_frame, 0},
841 {"loc", s_ignore, 0},
842 {"mask", s_mips_mask, 'R'},
843 {"verstamp", s_ignore, 0},
43841e91 844 { NULL, NULL, 0 },
252b5132
RH
845};
846
847extern void pop_insert PARAMS ((const pseudo_typeS *));
848
849void
850mips_pop_insert ()
851{
852 pop_insert (mips_pseudo_table);
853 if (! ECOFF_DEBUGGING)
854 pop_insert (mips_nonecoff_pseudo_table);
855}
856\f
857/* Symbols labelling the current insn. */
858
e972090a
NC
859struct insn_label_list
860{
252b5132
RH
861 struct insn_label_list *next;
862 symbolS *label;
863};
864
865static struct insn_label_list *insn_labels;
866static struct insn_label_list *free_insn_labels;
867
868static void mips_clear_insn_labels PARAMS ((void));
869
870static inline void
871mips_clear_insn_labels ()
872{
873 register struct insn_label_list **pl;
874
875 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
876 ;
877 *pl = insn_labels;
878 insn_labels = NULL;
879}
880\f
881static char *expr_end;
882
883/* Expressions which appear in instructions. These are set by
884 mips_ip. */
885
886static expressionS imm_expr;
887static expressionS offset_expr;
888
889/* Relocs associated with imm_expr and offset_expr. */
890
891static bfd_reloc_code_real_type imm_reloc;
892static bfd_reloc_code_real_type offset_reloc;
893
894/* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
895
896static boolean imm_unmatched_hi;
897
898/* These are set by mips16_ip if an explicit extension is used. */
899
900static boolean mips16_small, mips16_ext;
901
902#ifdef MIPS_STABS_ELF
903/* The pdr segment for per procedure frame/regmask info */
904
905static segT pdr_seg;
906#endif
907
e7af610e
NC
908static const char *
909mips_isa_to_str (isa)
910 int isa;
911{
912 const struct mips_cpu_info *ci;
913 static char s[20];
914
915 ci = mips_cpu_info_from_isa (isa);
916 if (ci != NULL)
917 return (ci->name);
918
919 sprintf (s, "ISA#%d", isa);
920 return s;
921}
922
923static const char *
156c2f8b
NC
924mips_cpu_to_str (cpu)
925 int cpu;
926{
e7af610e 927 const struct mips_cpu_info *ci;
156c2f8b 928 static char s[16];
e7af610e
NC
929
930 ci = mips_cpu_info_from_cpu (cpu);
931 if (ci != NULL)
932 return (ci->name);
933
934 sprintf (s, "CPU#%d", cpu);
935 return s;
156c2f8b
NC
936}
937
938/* This function is called once, at assembler startup time. It should
939 set up all the tables, etc. that the MD part of the assembler will need. */
940
252b5132
RH
941void
942md_begin ()
943{
252b5132 944 register const char *retval = NULL;
156c2f8b 945 int i = 0;
252b5132
RH
946 const char *cpu;
947 char *a = NULL;
948 int broken = 0;
949 int mips_isa_from_cpu;
e7af610e
NC
950 int target_cpu_had_mips16 = 0;
951 const struct mips_cpu_info *ci;
252b5132 952
056350c6
NC
953 /* GP relative stuff not working for PE */
954 if (strncmp (TARGET_OS, "pe", 2) == 0
955 && g_switch_value != 0)
956 {
957 if (g_switch_seen)
958 as_bad (_("-G not supported in this configuration."));
959 g_switch_value = 0;
960 }
961
252b5132
RH
962 cpu = TARGET_CPU;
963 if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
964 {
965 a = xmalloc (sizeof TARGET_CPU);
966 strcpy (a, TARGET_CPU);
967 a[(sizeof TARGET_CPU) - 3] = '\0';
968 cpu = a;
969 }
970
e7af610e 971 if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
252b5132 972 {
e7af610e
NC
973 target_cpu_had_mips16 = 1;
974 cpu += sizeof "mips16" - 1;
252b5132
RH
975 }
976
e7af610e
NC
977 if (mips_opts.mips16 < 0)
978 mips_opts.mips16 = target_cpu_had_mips16;
252b5132 979
6dce9e24
TS
980 /* Backward compatibility for historic -mcpu= option. Check for
981 incompatible options, warn if -mcpu is used. */
982 if (mips_cpu != CPU_UNKNOWN
983 && mips_arch != CPU_UNKNOWN
984 && mips_cpu != mips_arch)
985 {
986 as_fatal (_("The -mcpu option can't be used together with -march. "
987 "Use -mtune instead of -mcpu."));
988 }
989
990 if (mips_cpu != CPU_UNKNOWN
991 && mips_tune != CPU_UNKNOWN
992 && mips_cpu != mips_tune)
993 {
994 as_fatal (_("The -mcpu option can't be used together with -mtune. "
995 "Use -march instead of -mcpu."));
996 }
997
998 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
999 {
1000 ci = mips_cpu_info_from_cpu (mips_cpu);
1001 assert (ci != NULL);
1002 mips_arch = ci->cpu;
1003 as_warn (_("The -mcpu option is deprecated. Please use -march and "
1004 "-mtune instead."));
1005 }
1006
ec68c924 1007 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
e7af610e
NC
1008 specified on the command line, or some other value if one was.
1009 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1010 the command line, or will be set otherwise if one was. */
ec68c924 1011 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
252b5132 1012 {
1ff54a84
TS
1013 /* We have to check if the isa is the default isa of arch. Otherwise
1014 we'll get invalid object file headers. */
1015 ci = mips_cpu_info_from_cpu (mips_arch);
1016 assert (ci != NULL);
1017 if (mips_opts.isa != ci->isa)
1018 {
1019 /* This really should be an error instead of a warning, but old
1020 compilers only have -mcpu which sets both arch and tune. For
1021 now, we discard arch and preserve tune. */
1022 as_warn (_("The -march option is incompatible to -mipsN and "
1023 "therefore ignored."));
1024 if (mips_tune == CPU_UNKNOWN)
1025 mips_tune = mips_arch;
1026 ci = mips_cpu_info_from_isa (mips_opts.isa);
1027 assert (ci != NULL);
1028 mips_arch = ci->cpu;
1029 }
252b5132 1030 }
ec68c924 1031 else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
252b5132 1032 {
ec68c924
EC
1033 /* We have ARCH, we need ISA. */
1034 ci = mips_cpu_info_from_cpu (mips_arch);
e7af610e
NC
1035 assert (ci != NULL);
1036 mips_opts.isa = ci->isa;
1037 }
ec68c924 1038 else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
e7af610e 1039 {
ec68c924 1040 /* We have ISA, we need default ARCH. */
e7af610e
NC
1041 ci = mips_cpu_info_from_isa (mips_opts.isa);
1042 assert (ci != NULL);
ec68c924
EC
1043 mips_arch = ci->cpu;
1044 }
e7af610e
NC
1045 else
1046 {
ec68c924 1047 /* We need to set both ISA and ARCH from target cpu. */
e7af610e
NC
1048 ci = mips_cpu_info_from_name (cpu);
1049 if (ci == NULL)
beae10d5 1050 ci = mips_cpu_info_from_cpu (CPU_R3000);
e7af610e
NC
1051 assert (ci != NULL);
1052 mips_opts.isa = ci->isa;
ec68c924 1053 mips_arch = ci->cpu;
252b5132
RH
1054 }
1055
ec68c924
EC
1056 if (mips_tune == CPU_UNKNOWN)
1057 mips_tune = mips_arch;
1058
1059 ci = mips_cpu_info_from_cpu (mips_arch);
e7af610e
NC
1060 assert (ci != NULL);
1061 mips_isa_from_cpu = ci->isa;
1062
252b5132 1063 /* End of TARGET_CPU processing, get rid of malloced memory
bdaaa2e1 1064 if necessary. */
252b5132
RH
1065 cpu = NULL;
1066 if (a != NULL)
1067 {
156c2f8b
NC
1068 free (a);
1069 a = NULL;
252b5132
RH
1070 }
1071
e7af610e 1072 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
252b5132
RH
1073 as_bad (_("trap exception not supported at ISA 1"));
1074
1075 /* Set the EABI kind based on the ISA before the user gets
1076 to change the ISA with directives. This isn't really
bdaaa2e1 1077 the best, but then neither is basing the abi on the isa. */
9ce8a5dd 1078 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
252b5132 1079 && mips_abi_string
beae10d5 1080 && 0 == strcmp (mips_abi_string, "eabi"))
252b5132
RH
1081 mips_eabi64 = 1;
1082
e7af610e
NC
1083 /* If they asked for mips1 or mips2 and a cpu that is
1084 mips3 or greater, then mark the object file 32BITMODE. */
1085 if (mips_isa_from_cpu != ISA_UNKNOWN
1086 && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
1087 && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
1088 mips_32bitmode = 1;
252b5132 1089
ec68c924 1090 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
252b5132
RH
1091 as_warn (_("Could not set architecture and machine"));
1092
1093 file_mips_isa = mips_opts.isa;
1094
1095 op_hash = hash_new ();
1096
1097 for (i = 0; i < NUMOPCODES;)
1098 {
1099 const char *name = mips_opcodes[i].name;
1100
1101 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1102 if (retval != NULL)
1103 {
1104 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1105 mips_opcodes[i].name, retval);
1106 /* Probably a memory allocation problem? Give up now. */
1107 as_fatal (_("Broken assembler. No assembly attempted."));
1108 }
1109 do
1110 {
1111 if (mips_opcodes[i].pinfo != INSN_MACRO)
1112 {
1113 if (!validate_mips_insn (&mips_opcodes[i]))
1114 broken = 1;
1115 }
1116 ++i;
1117 }
1118 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1119 }
1120
1121 mips16_op_hash = hash_new ();
1122
1123 i = 0;
1124 while (i < bfd_mips16_num_opcodes)
1125 {
1126 const char *name = mips16_opcodes[i].name;
1127
1128 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1129 if (retval != NULL)
1130 as_fatal (_("internal: can't hash `%s': %s"),
1131 mips16_opcodes[i].name, retval);
1132 do
1133 {
1134 if (mips16_opcodes[i].pinfo != INSN_MACRO
1135 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1136 != mips16_opcodes[i].match))
1137 {
1138 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1139 mips16_opcodes[i].name, mips16_opcodes[i].args);
1140 broken = 1;
1141 }
1142 ++i;
1143 }
1144 while (i < bfd_mips16_num_opcodes
1145 && strcmp (mips16_opcodes[i].name, name) == 0);
1146 }
1147
1148 if (broken)
1149 as_fatal (_("Broken assembler. No assembly attempted."));
1150
1151 /* We add all the general register names to the symbol table. This
1152 helps us detect invalid uses of them. */
1153 for (i = 0; i < 32; i++)
1154 {
1155 char buf[5];
1156
1157 sprintf (buf, "$%d", i);
1158 symbol_table_insert (symbol_new (buf, reg_section, i,
1159 &zero_address_frag));
1160 }
1161 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1162 &zero_address_frag));
1163 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1164 &zero_address_frag));
1165 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1166 &zero_address_frag));
1167 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1168 &zero_address_frag));
1169 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1170 &zero_address_frag));
1171 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1172 &zero_address_frag));
1173 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1174 &zero_address_frag));
1175
1176 mips_no_prev_insn (false);
1177
1178 mips_gprmask = 0;
1179 mips_cprmask[0] = 0;
1180 mips_cprmask[1] = 0;
1181 mips_cprmask[2] = 0;
1182 mips_cprmask[3] = 0;
1183
1184 /* set the default alignment for the text section (2**2) */
1185 record_alignment (text_section, 2);
1186
1187 if (USE_GLOBAL_POINTER_OPT)
1188 bfd_set_gp_size (stdoutput, g_switch_value);
1189
1190 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1191 {
1192 /* On a native system, sections must be aligned to 16 byte
1193 boundaries. When configured for an embedded ELF target, we
1194 don't bother. */
1195 if (strcmp (TARGET_OS, "elf") != 0)
1196 {
1197 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1198 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1199 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1200 }
1201
1202 /* Create a .reginfo section for register masks and a .mdebug
1203 section for debugging information. */
1204 {
1205 segT seg;
1206 subsegT subseg;
1207 flagword flags;
1208 segT sec;
1209
1210 seg = now_seg;
1211 subseg = now_subseg;
1212
1213 /* The ABI says this section should be loaded so that the
1214 running program can access it. However, we don't load it
1215 if we are configured for an embedded target */
1216 flags = SEC_READONLY | SEC_DATA;
1217 if (strcmp (TARGET_OS, "elf") != 0)
1218 flags |= SEC_ALLOC | SEC_LOAD;
1219
1220 if (! mips_64)
1221 {
1222 sec = subseg_new (".reginfo", (subsegT) 0);
1223
252b5132
RH
1224 (void) bfd_set_section_flags (stdoutput, sec, flags);
1225 (void) bfd_set_section_alignment (stdoutput, sec, 2);
bdaaa2e1 1226
252b5132
RH
1227#ifdef OBJ_ELF
1228 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1229#endif
1230 }
1231 else
1232 {
1233 /* The 64-bit ABI uses a .MIPS.options section rather than
1234 .reginfo section. */
1235 sec = subseg_new (".MIPS.options", (subsegT) 0);
1236 (void) bfd_set_section_flags (stdoutput, sec, flags);
1237 (void) bfd_set_section_alignment (stdoutput, sec, 3);
1238
1239#ifdef OBJ_ELF
1240 /* Set up the option header. */
1241 {
1242 Elf_Internal_Options opthdr;
1243 char *f;
1244
1245 opthdr.kind = ODK_REGINFO;
1246 opthdr.size = (sizeof (Elf_External_Options)
1247 + sizeof (Elf64_External_RegInfo));
1248 opthdr.section = 0;
1249 opthdr.info = 0;
1250 f = frag_more (sizeof (Elf_External_Options));
1251 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1252 (Elf_External_Options *) f);
1253
1254 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1255 }
1256#endif
1257 }
1258
1259 if (ECOFF_DEBUGGING)
1260 {
1261 sec = subseg_new (".mdebug", (subsegT) 0);
1262 (void) bfd_set_section_flags (stdoutput, sec,
1263 SEC_HAS_CONTENTS | SEC_READONLY);
1264 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1265 }
1266
1267#ifdef MIPS_STABS_ELF
1268 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1269 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1270 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1271 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1272#endif
1273
1274 subseg_set (seg, subseg);
1275 }
1276 }
1277
1278 if (! ECOFF_DEBUGGING)
1279 md_obj_begin ();
1280}
1281
1282void
1283md_mips_end ()
1284{
1285 if (! ECOFF_DEBUGGING)
1286 md_obj_end ();
1287}
1288
1289void
1290md_assemble (str)
1291 char *str;
1292{
1293 struct mips_cl_insn insn;
1294
1295 imm_expr.X_op = O_absent;
1296 imm_reloc = BFD_RELOC_UNUSED;
1297 imm_unmatched_hi = false;
1298 offset_expr.X_op = O_absent;
1299 offset_reloc = BFD_RELOC_UNUSED;
1300
1301 if (mips_opts.mips16)
1302 mips16_ip (str, &insn);
1303 else
1304 {
1305 mips_ip (str, &insn);
beae10d5
KH
1306 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1307 str, insn.insn_opcode));
252b5132
RH
1308 }
1309
1310 if (insn_error)
1311 {
1312 as_bad ("%s `%s'", insn_error, str);
1313 return;
1314 }
1315
1316 if (insn.insn_mo->pinfo == INSN_MACRO)
1317 {
1318 if (mips_opts.mips16)
1319 mips16_macro (&insn);
1320 else
1321 macro (&insn);
1322 }
1323 else
1324 {
1325 if (imm_expr.X_op != O_absent)
1326 append_insn ((char *) NULL, &insn, &imm_expr, imm_reloc,
1327 imm_unmatched_hi);
1328 else if (offset_expr.X_op != O_absent)
1329 append_insn ((char *) NULL, &insn, &offset_expr, offset_reloc, false);
1330 else
1331 append_insn ((char *) NULL, &insn, NULL, BFD_RELOC_UNUSED, false);
1332 }
1333}
1334
1335/* See whether instruction IP reads register REG. CLASS is the type
1336 of register. */
1337
1338static int
1339insn_uses_reg (ip, reg, class)
1340 struct mips_cl_insn *ip;
1341 unsigned int reg;
1342 enum mips_regclass class;
1343{
1344 if (class == MIPS16_REG)
1345 {
1346 assert (mips_opts.mips16);
1347 reg = mips16_to_32_reg_map[reg];
1348 class = MIPS_GR_REG;
1349 }
1350
1351 /* Don't report on general register 0, since it never changes. */
1352 if (class == MIPS_GR_REG && reg == 0)
1353 return 0;
1354
1355 if (class == MIPS_FP_REG)
1356 {
1357 assert (! mips_opts.mips16);
1358 /* If we are called with either $f0 or $f1, we must check $f0.
1359 This is not optimal, because it will introduce an unnecessary
1360 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1361 need to distinguish reading both $f0 and $f1 or just one of
1362 them. Note that we don't have to check the other way,
1363 because there is no instruction that sets both $f0 and $f1
1364 and requires a delay. */
1365 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1366 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1367 == (reg &~ (unsigned) 1)))
1368 return 1;
1369 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1370 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1371 == (reg &~ (unsigned) 1)))
1372 return 1;
1373 }
1374 else if (! mips_opts.mips16)
1375 {
1376 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1377 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1378 return 1;
1379 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1380 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1381 return 1;
1382 }
1383 else
1384 {
1385 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1386 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1387 & MIPS16OP_MASK_RX)]
1388 == reg))
1389 return 1;
1390 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1391 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1392 & MIPS16OP_MASK_RY)]
1393 == reg))
1394 return 1;
1395 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1396 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1397 & MIPS16OP_MASK_MOVE32Z)]
1398 == reg))
1399 return 1;
1400 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1401 return 1;
1402 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1403 return 1;
1404 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1405 return 1;
1406 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1407 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1408 & MIPS16OP_MASK_REGR32) == reg)
1409 return 1;
1410 }
1411
1412 return 0;
1413}
1414
1415/* This function returns true if modifying a register requires a
1416 delay. */
1417
1418static int
1419reg_needs_delay (reg)
156c2f8b 1420 unsigned int reg;
252b5132
RH
1421{
1422 unsigned long prev_pinfo;
1423
1424 prev_pinfo = prev_insn.insn_mo->pinfo;
1425 if (! mips_opts.noreorder
9ce8a5dd 1426 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1427 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1428 || (! gpr_interlocks
1429 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1430 {
1431 /* A load from a coprocessor or from memory. All load
1432 delays delay the use of general register rt for one
1433 instruction on the r3000. The r6000 and r4000 use
1434 interlocks. */
bdaaa2e1 1435 /* Itbl support may require additional care here. */
252b5132
RH
1436 know (prev_pinfo & INSN_WRITE_GPR_T);
1437 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1438 return 1;
1439 }
1440
1441 return 0;
1442}
1443
1444/* Mark instruction labels in mips16 mode. This permits the linker to
1445 handle them specially, such as generating jalx instructions when
1446 needed. We also make them odd for the duration of the assembly, in
1447 order to generate the right sort of code. We will make them even
1448 in the adjust_symtab routine, while leaving them marked. This is
1449 convenient for the debugger and the disassembler. The linker knows
1450 to make them odd again. */
1451
1452static void
1453mips16_mark_labels ()
1454{
1455 if (mips_opts.mips16)
1456 {
1457 struct insn_label_list *l;
98aa84af 1458 valueT val;
252b5132
RH
1459
1460 for (l = insn_labels; l != NULL; l = l->next)
1461 {
1462#ifdef OBJ_ELF
1463 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1464 S_SET_OTHER (l->label, STO_MIPS16);
1465#endif
98aa84af
AM
1466 val = S_GET_VALUE (l->label);
1467 if ((val & 1) == 0)
1468 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1469 }
1470 }
1471}
1472
1473/* Output an instruction. PLACE is where to put the instruction; if
1474 it is NULL, this uses frag_more to get room. IP is the instruction
1475 information. ADDRESS_EXPR is an operand of the instruction to be
1476 used with RELOC_TYPE. */
1477
1478static void
1479append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1480 char *place;
1481 struct mips_cl_insn *ip;
1482 expressionS *address_expr;
1483 bfd_reloc_code_real_type reloc_type;
1484 boolean unmatched_hi;
1485{
1486 register unsigned long prev_pinfo, pinfo;
1487 char *f;
1488 fixS *fixp;
1489 int nops = 0;
1490
1491 /* Mark instruction labels in mips16 mode. */
1492 if (mips_opts.mips16)
1493 mips16_mark_labels ();
1494
1495 prev_pinfo = prev_insn.insn_mo->pinfo;
1496 pinfo = ip->insn_mo->pinfo;
1497
1498 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1499 {
1500 int prev_prev_nop;
1501
1502 /* If the previous insn required any delay slots, see if we need
1503 to insert a NOP or two. There are eight kinds of possible
1504 hazards, of which an instruction can have at most one type.
1505 (1) a load from memory delay
1506 (2) a load from a coprocessor delay
1507 (3) an unconditional branch delay
1508 (4) a conditional branch delay
1509 (5) a move to coprocessor register delay
1510 (6) a load coprocessor register from memory delay
1511 (7) a coprocessor condition code delay
1512 (8) a HI/LO special register delay
1513
1514 There are a lot of optimizations we could do that we don't.
1515 In particular, we do not, in general, reorder instructions.
1516 If you use gcc with optimization, it will reorder
1517 instructions and generally do much more optimization then we
1518 do here; repeating all that work in the assembler would only
1519 benefit hand written assembly code, and does not seem worth
1520 it. */
1521
1522 /* This is how a NOP is emitted. */
1523#define emit_nop() \
1524 (mips_opts.mips16 \
1525 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1526 : md_number_to_chars (frag_more (4), 0, 4))
1527
1528 /* The previous insn might require a delay slot, depending upon
1529 the contents of the current insn. */
1530 if (! mips_opts.mips16
9ce8a5dd 1531 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1532 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1533 && ! cop_interlocks)
1534 || (! gpr_interlocks
1535 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1536 {
1537 /* A load from a coprocessor or from memory. All load
1538 delays delay the use of general register rt for one
1539 instruction on the r3000. The r6000 and r4000 use
1540 interlocks. */
beae10d5 1541 /* Itbl support may require additional care here. */
252b5132
RH
1542 know (prev_pinfo & INSN_WRITE_GPR_T);
1543 if (mips_optimize == 0
1544 || insn_uses_reg (ip,
1545 ((prev_insn.insn_opcode >> OP_SH_RT)
1546 & OP_MASK_RT),
1547 MIPS_GR_REG))
1548 ++nops;
1549 }
1550 else if (! mips_opts.mips16
9ce8a5dd 1551 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 1552 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1553 && ! cop_interlocks)
e7af610e 1554 || (mips_opts.isa == ISA_MIPS1
252b5132
RH
1555 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1556 {
1557 /* A generic coprocessor delay. The previous instruction
1558 modified a coprocessor general or control register. If
1559 it modified a control register, we need to avoid any
1560 coprocessor instruction (this is probably not always
1561 required, but it sometimes is). If it modified a general
1562 register, we avoid using that register.
1563
1564 On the r6000 and r4000 loading a coprocessor register
1565 from memory is interlocked, and does not require a delay.
1566
1567 This case is not handled very well. There is no special
1568 knowledge of CP0 handling, and the coprocessors other
1569 than the floating point unit are not distinguished at
1570 all. */
1571 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1572 Need to modify this to include knowledge about
252b5132
RH
1573 user specified delays! */
1574 if (prev_pinfo & INSN_WRITE_FPR_T)
1575 {
1576 if (mips_optimize == 0
1577 || insn_uses_reg (ip,
1578 ((prev_insn.insn_opcode >> OP_SH_FT)
1579 & OP_MASK_FT),
1580 MIPS_FP_REG))
1581 ++nops;
1582 }
1583 else if (prev_pinfo & INSN_WRITE_FPR_S)
1584 {
1585 if (mips_optimize == 0
1586 || insn_uses_reg (ip,
1587 ((prev_insn.insn_opcode >> OP_SH_FS)
1588 & OP_MASK_FS),
1589 MIPS_FP_REG))
1590 ++nops;
1591 }
1592 else
1593 {
1594 /* We don't know exactly what the previous instruction
1595 does. If the current instruction uses a coprocessor
1596 register, we must insert a NOP. If previous
1597 instruction may set the condition codes, and the
1598 current instruction uses them, we must insert two
1599 NOPS. */
bdaaa2e1 1600 /* Itbl support may require additional care here. */
252b5132
RH
1601 if (mips_optimize == 0
1602 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1603 && (pinfo & INSN_READ_COND_CODE)))
1604 nops += 2;
1605 else if (pinfo & INSN_COP)
1606 ++nops;
1607 }
1608 }
1609 else if (! mips_opts.mips16
9ce8a5dd 1610 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1611 && (prev_pinfo & INSN_WRITE_COND_CODE)
1612 && ! cop_interlocks)
1613 {
1614 /* The previous instruction sets the coprocessor condition
1615 codes, but does not require a general coprocessor delay
1616 (this means it is a floating point comparison
1617 instruction). If this instruction uses the condition
1618 codes, we need to insert a single NOP. */
beae10d5 1619 /* Itbl support may require additional care here. */
252b5132
RH
1620 if (mips_optimize == 0
1621 || (pinfo & INSN_READ_COND_CODE))
1622 ++nops;
1623 }
6b76fefe
CM
1624
1625 /* If we're fixing up mfhi/mflo for the r7000 and the
1626 previous insn was an mfhi/mflo and the current insn
1627 reads the register that the mfhi/mflo wrote to, then
1628 insert two nops. */
1629
1630 else if (mips_7000_hilo_fix
1631 && MF_HILO_INSN (prev_pinfo)
1632 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1633 & OP_MASK_RD),
1634 MIPS_GR_REG))
6b76fefe
CM
1635 {
1636 nops += 2;
1637 }
1638
1639 /* If we're fixing up mfhi/mflo for the r7000 and the
1640 2nd previous insn was an mfhi/mflo and the current insn
1641 reads the register that the mfhi/mflo wrote to, then
1642 insert one nop. */
1643
1644 else if (mips_7000_hilo_fix
1645 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1646 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1647 & OP_MASK_RD),
1648 MIPS_GR_REG))
bdaaa2e1 1649
6b76fefe
CM
1650 {
1651 nops += 1;
1652 }
bdaaa2e1 1653
252b5132
RH
1654 else if (prev_pinfo & INSN_READ_LO)
1655 {
1656 /* The previous instruction reads the LO register; if the
1657 current instruction writes to the LO register, we must
bdaaa2e1
KH
1658 insert two NOPS. Some newer processors have interlocks.
1659 Also the tx39's multiply instructions can be exectuted
252b5132 1660 immediatly after a read from HI/LO (without the delay),
bdaaa2e1
KH
1661 though the tx39's divide insns still do require the
1662 delay. */
252b5132 1663 if (! (hilo_interlocks
ec68c924 1664 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1665 && (mips_optimize == 0
1666 || (pinfo & INSN_WRITE_LO)))
1667 nops += 2;
1668 /* Most mips16 branch insns don't have a delay slot.
1669 If a read from LO is immediately followed by a branch
1670 to a write to LO we have a read followed by a write
1671 less than 2 insns away. We assume the target of
1672 a branch might be a write to LO, and insert a nop
bdaaa2e1 1673 between a read and an immediately following branch. */
252b5132
RH
1674 else if (mips_opts.mips16
1675 && (mips_optimize == 0
1676 || (pinfo & MIPS16_INSN_BRANCH)))
1677 nops += 1;
1678 }
1679 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1680 {
1681 /* The previous instruction reads the HI register; if the
1682 current instruction writes to the HI register, we must
1683 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1684 Also the note tx39's multiply above. */
252b5132 1685 if (! (hilo_interlocks
ec68c924 1686 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1687 && (mips_optimize == 0
1688 || (pinfo & INSN_WRITE_HI)))
1689 nops += 2;
1690 /* Most mips16 branch insns don't have a delay slot.
1691 If a read from HI is immediately followed by a branch
1692 to a write to HI we have a read followed by a write
1693 less than 2 insns away. We assume the target of
1694 a branch might be a write to HI, and insert a nop
bdaaa2e1 1695 between a read and an immediately following branch. */
252b5132
RH
1696 else if (mips_opts.mips16
1697 && (mips_optimize == 0
1698 || (pinfo & MIPS16_INSN_BRANCH)))
1699 nops += 1;
1700 }
1701
1702 /* If the previous instruction was in a noreorder section, then
1703 we don't want to insert the nop after all. */
bdaaa2e1 1704 /* Itbl support may require additional care here. */
252b5132
RH
1705 if (prev_insn_unreordered)
1706 nops = 0;
1707
1708 /* There are two cases which require two intervening
1709 instructions: 1) setting the condition codes using a move to
1710 coprocessor instruction which requires a general coprocessor
1711 delay and then reading the condition codes 2) reading the HI
1712 or LO register and then writing to it (except on processors
1713 which have interlocks). If we are not already emitting a NOP
1714 instruction, we must check for these cases compared to the
1715 instruction previous to the previous instruction. */
1716 if ((! mips_opts.mips16
9ce8a5dd 1717 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1718 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1719 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1720 && (pinfo & INSN_READ_COND_CODE)
1721 && ! cop_interlocks)
1722 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1723 && (pinfo & INSN_WRITE_LO)
1724 && ! (hilo_interlocks
ec68c924 1725 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1726 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1727 && (pinfo & INSN_WRITE_HI)
1728 && ! (hilo_interlocks
ec68c924 1729 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1730 prev_prev_nop = 1;
1731 else
1732 prev_prev_nop = 0;
1733
1734 if (prev_prev_insn_unreordered)
1735 prev_prev_nop = 0;
1736
1737 if (prev_prev_nop && nops == 0)
1738 ++nops;
1739
1740 /* If we are being given a nop instruction, don't bother with
1741 one of the nops we would otherwise output. This will only
1742 happen when a nop instruction is used with mips_optimize set
1743 to 0. */
1744 if (nops > 0
1745 && ! mips_opts.noreorder
156c2f8b 1746 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1747 --nops;
1748
1749 /* Now emit the right number of NOP instructions. */
1750 if (nops > 0 && ! mips_opts.noreorder)
1751 {
1752 fragS *old_frag;
1753 unsigned long old_frag_offset;
1754 int i;
1755 struct insn_label_list *l;
1756
1757 old_frag = frag_now;
1758 old_frag_offset = frag_now_fix ();
1759
1760 for (i = 0; i < nops; i++)
1761 emit_nop ();
1762
1763 if (listing)
1764 {
1765 listing_prev_line ();
1766 /* We may be at the start of a variant frag. In case we
1767 are, make sure there is enough space for the frag
1768 after the frags created by listing_prev_line. The
1769 argument to frag_grow here must be at least as large
1770 as the argument to all other calls to frag_grow in
1771 this file. We don't have to worry about being in the
1772 middle of a variant frag, because the variants insert
1773 all needed nop instructions themselves. */
1774 frag_grow (40);
1775 }
1776
1777 for (l = insn_labels; l != NULL; l = l->next)
1778 {
98aa84af
AM
1779 valueT val;
1780
252b5132 1781 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1782 symbol_set_frag (l->label, frag_now);
98aa84af 1783 val = (valueT) frag_now_fix ();
252b5132
RH
1784 /* mips16 text labels are stored as odd. */
1785 if (mips_opts.mips16)
98aa84af
AM
1786 val += 1;
1787 S_SET_VALUE (l->label, val);
252b5132
RH
1788 }
1789
1790#ifndef NO_ECOFF_DEBUGGING
1791 if (ECOFF_DEBUGGING)
1792 ecoff_fix_loc (old_frag, old_frag_offset);
1793#endif
1794 }
1795 else if (prev_nop_frag != NULL)
1796 {
1797 /* We have a frag holding nops we may be able to remove. If
1798 we don't need any nops, we can decrease the size of
1799 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1800 need some nops, we count them in prev_nops_required. */
252b5132
RH
1801 if (prev_nop_frag_since == 0)
1802 {
1803 if (nops == 0)
1804 {
1805 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1806 --prev_nop_frag_holds;
1807 }
1808 else
1809 prev_nop_frag_required += nops;
1810 }
1811 else
1812 {
1813 if (prev_prev_nop == 0)
1814 {
1815 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1816 --prev_nop_frag_holds;
1817 }
1818 else
1819 ++prev_nop_frag_required;
1820 }
1821
1822 if (prev_nop_frag_holds <= prev_nop_frag_required)
1823 prev_nop_frag = NULL;
1824
1825 ++prev_nop_frag_since;
1826
1827 /* Sanity check: by the time we reach the second instruction
1828 after prev_nop_frag, we should have used up all the nops
1829 one way or another. */
1830 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1831 }
1832 }
1833
1834 if (reloc_type > BFD_RELOC_UNUSED)
1835 {
1836 /* We need to set up a variant frag. */
1837 assert (mips_opts.mips16 && address_expr != NULL);
1838 f = frag_var (rs_machine_dependent, 4, 0,
1839 RELAX_MIPS16_ENCODE (reloc_type - BFD_RELOC_UNUSED,
1840 mips16_small, mips16_ext,
1841 (prev_pinfo
1842 & INSN_UNCOND_BRANCH_DELAY),
1843 (prev_insn_reloc_type
1844 == BFD_RELOC_MIPS16_JMP)),
1845 make_expr_symbol (address_expr), (offsetT) 0,
1846 (char *) NULL);
1847 }
1848 else if (place != NULL)
1849 f = place;
1850 else if (mips_opts.mips16
1851 && ! ip->use_extend
1852 && reloc_type != BFD_RELOC_MIPS16_JMP)
1853 {
1854 /* Make sure there is enough room to swap this instruction with
1855 a following jump instruction. */
1856 frag_grow (6);
1857 f = frag_more (2);
1858 }
1859 else
1860 {
1861 if (mips_opts.mips16
1862 && mips_opts.noreorder
1863 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1864 as_warn (_("extended instruction in delay slot"));
1865
1866 f = frag_more (4);
1867 }
1868
1869 fixp = NULL;
1870 if (address_expr != NULL && reloc_type < BFD_RELOC_UNUSED)
1871 {
1872 if (address_expr->X_op == O_constant)
1873 {
1874 switch (reloc_type)
1875 {
1876 case BFD_RELOC_32:
1877 ip->insn_opcode |= address_expr->X_add_number;
1878 break;
1879
1880 case BFD_RELOC_LO16:
1881 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1882 break;
1883
1884 case BFD_RELOC_MIPS_JMP:
1885 if ((address_expr->X_add_number & 3) != 0)
1886 as_bad (_("jump to misaligned address (0x%lx)"),
1887 (unsigned long) address_expr->X_add_number);
1888 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1889 break;
1890
1891 case BFD_RELOC_MIPS16_JMP:
1892 if ((address_expr->X_add_number & 3) != 0)
1893 as_bad (_("jump to misaligned address (0x%lx)"),
1894 (unsigned long) address_expr->X_add_number);
1895 ip->insn_opcode |=
1896 (((address_expr->X_add_number & 0x7c0000) << 3)
1897 | ((address_expr->X_add_number & 0xf800000) >> 7)
1898 | ((address_expr->X_add_number & 0x3fffc) >> 2));
1899 break;
1900
252b5132
RH
1901 case BFD_RELOC_16_PCREL_S2:
1902 goto need_reloc;
1903
1904 default:
1905 internalError ();
1906 }
1907 }
1908 else
1909 {
1910 need_reloc:
1911 /* Don't generate a reloc if we are writing into a variant
1912 frag. */
1913 if (place == NULL)
1914 {
1915 fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1916 address_expr,
1917 reloc_type == BFD_RELOC_16_PCREL_S2,
1918 reloc_type);
1919 if (unmatched_hi)
1920 {
1921 struct mips_hi_fixup *hi_fixup;
1922
1923 assert (reloc_type == BFD_RELOC_HI16_S);
1924 hi_fixup = ((struct mips_hi_fixup *)
1925 xmalloc (sizeof (struct mips_hi_fixup)));
1926 hi_fixup->fixp = fixp;
1927 hi_fixup->seg = now_seg;
1928 hi_fixup->next = mips_hi_fixup_list;
1929 mips_hi_fixup_list = hi_fixup;
1930 }
1931 }
1932 }
1933 }
1934
1935 if (! mips_opts.mips16)
1936 md_number_to_chars (f, ip->insn_opcode, 4);
1937 else if (reloc_type == BFD_RELOC_MIPS16_JMP)
1938 {
1939 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
1940 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
1941 }
1942 else
1943 {
1944 if (ip->use_extend)
1945 {
1946 md_number_to_chars (f, 0xf000 | ip->extend, 2);
1947 f += 2;
1948 }
1949 md_number_to_chars (f, ip->insn_opcode, 2);
1950 }
1951
1952 /* Update the register mask information. */
1953 if (! mips_opts.mips16)
1954 {
1955 if (pinfo & INSN_WRITE_GPR_D)
1956 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
1957 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
1958 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
1959 if (pinfo & INSN_READ_GPR_S)
1960 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
1961 if (pinfo & INSN_WRITE_GPR_31)
1962 mips_gprmask |= 1 << 31;
1963 if (pinfo & INSN_WRITE_FPR_D)
1964 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
1965 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
1966 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
1967 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
1968 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
1969 if ((pinfo & INSN_READ_FPR_R) != 0)
1970 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
1971 if (pinfo & INSN_COP)
1972 {
bdaaa2e1
KH
1973 /* We don't keep enough information to sort these cases out.
1974 The itbl support does keep this information however, although
1975 we currently don't support itbl fprmats as part of the cop
1976 instruction. May want to add this support in the future. */
252b5132
RH
1977 }
1978 /* Never set the bit for $0, which is always zero. */
beae10d5 1979 mips_gprmask &= ~1 << 0;
252b5132
RH
1980 }
1981 else
1982 {
1983 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
1984 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
1985 & MIPS16OP_MASK_RX);
1986 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
1987 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
1988 & MIPS16OP_MASK_RY);
1989 if (pinfo & MIPS16_INSN_WRITE_Z)
1990 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
1991 & MIPS16OP_MASK_RZ);
1992 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
1993 mips_gprmask |= 1 << TREG;
1994 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
1995 mips_gprmask |= 1 << SP;
1996 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
1997 mips_gprmask |= 1 << RA;
1998 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
1999 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2000 if (pinfo & MIPS16_INSN_READ_Z)
2001 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2002 & MIPS16OP_MASK_MOVE32Z);
2003 if (pinfo & MIPS16_INSN_READ_GPR_X)
2004 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2005 & MIPS16OP_MASK_REGR32);
2006 }
2007
2008 if (place == NULL && ! mips_opts.noreorder)
2009 {
2010 /* Filling the branch delay slot is more complex. We try to
2011 switch the branch with the previous instruction, which we can
2012 do if the previous instruction does not set up a condition
2013 that the branch tests and if the branch is not itself the
2014 target of any branch. */
2015 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2016 || (pinfo & INSN_COND_BRANCH_DELAY))
2017 {
2018 if (mips_optimize < 2
2019 /* If we have seen .set volatile or .set nomove, don't
2020 optimize. */
2021 || mips_opts.nomove != 0
2022 /* If we had to emit any NOP instructions, then we
2023 already know we can not swap. */
2024 || nops != 0
2025 /* If we don't even know the previous insn, we can not
bdaaa2e1 2026 swap. */
252b5132
RH
2027 || ! prev_insn_valid
2028 /* If the previous insn is already in a branch delay
2029 slot, then we can not swap. */
2030 || prev_insn_is_delay_slot
2031 /* If the previous previous insn was in a .set
2032 noreorder, we can't swap. Actually, the MIPS
2033 assembler will swap in this situation. However, gcc
2034 configured -with-gnu-as will generate code like
2035 .set noreorder
2036 lw $4,XXX
2037 .set reorder
2038 INSN
2039 bne $4,$0,foo
2040 in which we can not swap the bne and INSN. If gcc is
2041 not configured -with-gnu-as, it does not output the
2042 .set pseudo-ops. We don't have to check
2043 prev_insn_unreordered, because prev_insn_valid will
2044 be 0 in that case. We don't want to use
2045 prev_prev_insn_valid, because we do want to be able
2046 to swap at the start of a function. */
2047 || prev_prev_insn_unreordered
2048 /* If the branch is itself the target of a branch, we
2049 can not swap. We cheat on this; all we check for is
2050 whether there is a label on this instruction. If
2051 there are any branches to anything other than a
2052 label, users must use .set noreorder. */
2053 || insn_labels != NULL
2054 /* If the previous instruction is in a variant frag, we
2055 can not do the swap. This does not apply to the
2056 mips16, which uses variant frags for different
2057 purposes. */
2058 || (! mips_opts.mips16
2059 && prev_insn_frag->fr_type == rs_machine_dependent)
2060 /* If the branch reads the condition codes, we don't
2061 even try to swap, because in the sequence
2062 ctc1 $X,$31
2063 INSN
2064 INSN
2065 bc1t LABEL
2066 we can not swap, and I don't feel like handling that
2067 case. */
2068 || (! mips_opts.mips16
9ce8a5dd 2069 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2070 && (pinfo & INSN_READ_COND_CODE))
2071 /* We can not swap with an instruction that requires a
2072 delay slot, becase the target of the branch might
2073 interfere with that instruction. */
2074 || (! mips_opts.mips16
9ce8a5dd 2075 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 2076 && (prev_pinfo
bdaaa2e1 2077 /* Itbl support may require additional care here. */
252b5132
RH
2078 & (INSN_LOAD_COPROC_DELAY
2079 | INSN_COPROC_MOVE_DELAY
2080 | INSN_WRITE_COND_CODE)))
2081 || (! (hilo_interlocks
ec68c924 2082 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2083 && (prev_pinfo
2084 & (INSN_READ_LO
2085 | INSN_READ_HI)))
2086 || (! mips_opts.mips16
2087 && ! gpr_interlocks
2088 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2089 || (! mips_opts.mips16
e7af610e 2090 && mips_opts.isa == ISA_MIPS1
bdaaa2e1 2091 /* Itbl support may require additional care here. */
252b5132
RH
2092 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2093 /* We can not swap with a branch instruction. */
2094 || (prev_pinfo
2095 & (INSN_UNCOND_BRANCH_DELAY
2096 | INSN_COND_BRANCH_DELAY
2097 | INSN_COND_BRANCH_LIKELY))
2098 /* We do not swap with a trap instruction, since it
2099 complicates trap handlers to have the trap
2100 instruction be in a delay slot. */
2101 || (prev_pinfo & INSN_TRAP)
2102 /* If the branch reads a register that the previous
2103 instruction sets, we can not swap. */
2104 || (! mips_opts.mips16
2105 && (prev_pinfo & INSN_WRITE_GPR_T)
2106 && insn_uses_reg (ip,
2107 ((prev_insn.insn_opcode >> OP_SH_RT)
2108 & OP_MASK_RT),
2109 MIPS_GR_REG))
2110 || (! mips_opts.mips16
2111 && (prev_pinfo & INSN_WRITE_GPR_D)
2112 && insn_uses_reg (ip,
2113 ((prev_insn.insn_opcode >> OP_SH_RD)
2114 & OP_MASK_RD),
2115 MIPS_GR_REG))
2116 || (mips_opts.mips16
2117 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2118 && insn_uses_reg (ip,
2119 ((prev_insn.insn_opcode
2120 >> MIPS16OP_SH_RX)
2121 & MIPS16OP_MASK_RX),
2122 MIPS16_REG))
2123 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2124 && insn_uses_reg (ip,
2125 ((prev_insn.insn_opcode
2126 >> MIPS16OP_SH_RY)
2127 & MIPS16OP_MASK_RY),
2128 MIPS16_REG))
2129 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2130 && insn_uses_reg (ip,
2131 ((prev_insn.insn_opcode
2132 >> MIPS16OP_SH_RZ)
2133 & MIPS16OP_MASK_RZ),
2134 MIPS16_REG))
2135 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2136 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2137 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2138 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2139 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2140 && insn_uses_reg (ip,
2141 MIPS16OP_EXTRACT_REG32R (prev_insn.
2142 insn_opcode),
2143 MIPS_GR_REG))))
2144 /* If the branch writes a register that the previous
2145 instruction sets, we can not swap (we know that
2146 branches write only to RD or to $31). */
2147 || (! mips_opts.mips16
2148 && (prev_pinfo & INSN_WRITE_GPR_T)
2149 && (((pinfo & INSN_WRITE_GPR_D)
2150 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2151 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2152 || ((pinfo & INSN_WRITE_GPR_31)
2153 && (((prev_insn.insn_opcode >> OP_SH_RT)
2154 & OP_MASK_RT)
2155 == 31))))
2156 || (! mips_opts.mips16
2157 && (prev_pinfo & INSN_WRITE_GPR_D)
2158 && (((pinfo & INSN_WRITE_GPR_D)
2159 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2160 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2161 || ((pinfo & INSN_WRITE_GPR_31)
2162 && (((prev_insn.insn_opcode >> OP_SH_RD)
2163 & OP_MASK_RD)
2164 == 31))))
2165 || (mips_opts.mips16
2166 && (pinfo & MIPS16_INSN_WRITE_31)
2167 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2168 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2169 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2170 == RA))))
2171 /* If the branch writes a register that the previous
2172 instruction reads, we can not swap (we know that
2173 branches only write to RD or to $31). */
2174 || (! mips_opts.mips16
2175 && (pinfo & INSN_WRITE_GPR_D)
2176 && insn_uses_reg (&prev_insn,
2177 ((ip->insn_opcode >> OP_SH_RD)
2178 & OP_MASK_RD),
2179 MIPS_GR_REG))
2180 || (! mips_opts.mips16
2181 && (pinfo & INSN_WRITE_GPR_31)
2182 && insn_uses_reg (&prev_insn, 31, MIPS_GR_REG))
2183 || (mips_opts.mips16
2184 && (pinfo & MIPS16_INSN_WRITE_31)
2185 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2186 /* If we are generating embedded PIC code, the branch
2187 might be expanded into a sequence which uses $at, so
2188 we can't swap with an instruction which reads it. */
2189 || (mips_pic == EMBEDDED_PIC
2190 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2191 /* If the previous previous instruction has a load
2192 delay, and sets a register that the branch reads, we
2193 can not swap. */
2194 || (! mips_opts.mips16
9ce8a5dd 2195 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
bdaaa2e1 2196 /* Itbl support may require additional care here. */
252b5132
RH
2197 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2198 || (! gpr_interlocks
2199 && (prev_prev_insn.insn_mo->pinfo
2200 & INSN_LOAD_MEMORY_DELAY)))
2201 && insn_uses_reg (ip,
2202 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2203 & OP_MASK_RT),
2204 MIPS_GR_REG))
2205 /* If one instruction sets a condition code and the
2206 other one uses a condition code, we can not swap. */
2207 || ((pinfo & INSN_READ_COND_CODE)
2208 && (prev_pinfo & INSN_WRITE_COND_CODE))
2209 || ((pinfo & INSN_WRITE_COND_CODE)
2210 && (prev_pinfo & INSN_READ_COND_CODE))
2211 /* If the previous instruction uses the PC, we can not
2212 swap. */
2213 || (mips_opts.mips16
2214 && (prev_pinfo & MIPS16_INSN_READ_PC))
2215 /* If the previous instruction was extended, we can not
2216 swap. */
2217 || (mips_opts.mips16 && prev_insn_extended)
2218 /* If the previous instruction had a fixup in mips16
2219 mode, we can not swap. This normally means that the
2220 previous instruction was a 4 byte branch anyhow. */
2221 || (mips_opts.mips16 && prev_insn_fixp)
bdaaa2e1
KH
2222 /* If the previous instruction is a sync, sync.l, or
2223 sync.p, we can not swap. */
f173e82e 2224 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2225 {
2226 /* We could do even better for unconditional branches to
2227 portions of this object file; we could pick up the
2228 instruction at the destination, put it in the delay
2229 slot, and bump the destination address. */
2230 emit_nop ();
2231 /* Update the previous insn information. */
2232 prev_prev_insn = *ip;
2233 prev_insn.insn_mo = &dummy_opcode;
2234 }
2235 else
2236 {
2237 /* It looks like we can actually do the swap. */
2238 if (! mips_opts.mips16)
2239 {
2240 char *prev_f;
2241 char temp[4];
2242
2243 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2244 memcpy (temp, prev_f, 4);
2245 memcpy (prev_f, f, 4);
2246 memcpy (f, temp, 4);
2247 if (prev_insn_fixp)
2248 {
2249 prev_insn_fixp->fx_frag = frag_now;
2250 prev_insn_fixp->fx_where = f - frag_now->fr_literal;
2251 }
2252 if (fixp)
2253 {
2254 fixp->fx_frag = prev_insn_frag;
2255 fixp->fx_where = prev_insn_where;
2256 }
2257 }
2258 else
2259 {
2260 char *prev_f;
2261 char temp[2];
2262
2263 assert (prev_insn_fixp == NULL);
2264 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2265 memcpy (temp, prev_f, 2);
2266 memcpy (prev_f, f, 2);
2267 if (reloc_type != BFD_RELOC_MIPS16_JMP)
2268 {
2269 assert (reloc_type == BFD_RELOC_UNUSED);
2270 memcpy (f, temp, 2);
2271 }
2272 else
2273 {
2274 memcpy (f, f + 2, 2);
2275 memcpy (f + 2, temp, 2);
2276 }
2277 if (fixp)
2278 {
2279 fixp->fx_frag = prev_insn_frag;
2280 fixp->fx_where = prev_insn_where;
2281 }
2282 }
2283
2284 /* Update the previous insn information; leave prev_insn
2285 unchanged. */
2286 prev_prev_insn = *ip;
2287 }
2288 prev_insn_is_delay_slot = 1;
2289
2290 /* If that was an unconditional branch, forget the previous
2291 insn information. */
2292 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2293 {
2294 prev_prev_insn.insn_mo = &dummy_opcode;
2295 prev_insn.insn_mo = &dummy_opcode;
2296 }
2297
2298 prev_insn_fixp = NULL;
2299 prev_insn_reloc_type = BFD_RELOC_UNUSED;
2300 prev_insn_extended = 0;
2301 }
2302 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2303 {
2304 /* We don't yet optimize a branch likely. What we should do
2305 is look at the target, copy the instruction found there
2306 into the delay slot, and increment the branch to jump to
2307 the next instruction. */
2308 emit_nop ();
2309 /* Update the previous insn information. */
2310 prev_prev_insn = *ip;
2311 prev_insn.insn_mo = &dummy_opcode;
2312 prev_insn_fixp = NULL;
2313 prev_insn_reloc_type = BFD_RELOC_UNUSED;
2314 prev_insn_extended = 0;
2315 }
2316 else
2317 {
2318 /* Update the previous insn information. */
2319 if (nops > 0)
2320 prev_prev_insn.insn_mo = &dummy_opcode;
2321 else
2322 prev_prev_insn = prev_insn;
2323 prev_insn = *ip;
2324
2325 /* Any time we see a branch, we always fill the delay slot
2326 immediately; since this insn is not a branch, we know it
2327 is not in a delay slot. */
2328 prev_insn_is_delay_slot = 0;
2329
2330 prev_insn_fixp = fixp;
2331 prev_insn_reloc_type = reloc_type;
2332 if (mips_opts.mips16)
2333 prev_insn_extended = (ip->use_extend
2334 || reloc_type > BFD_RELOC_UNUSED);
2335 }
2336
2337 prev_prev_insn_unreordered = prev_insn_unreordered;
2338 prev_insn_unreordered = 0;
2339 prev_insn_frag = frag_now;
2340 prev_insn_where = f - frag_now->fr_literal;
2341 prev_insn_valid = 1;
2342 }
2343 else if (place == NULL)
2344 {
2345 /* We need to record a bit of information even when we are not
2346 reordering, in order to determine the base address for mips16
2347 PC relative relocs. */
2348 prev_prev_insn = prev_insn;
2349 prev_insn = *ip;
2350 prev_insn_reloc_type = reloc_type;
2351 prev_prev_insn_unreordered = prev_insn_unreordered;
2352 prev_insn_unreordered = 1;
2353 }
2354
2355 /* We just output an insn, so the next one doesn't have a label. */
2356 mips_clear_insn_labels ();
2357
2358 /* We must ensure that a fixup associated with an unmatched %hi
2359 reloc does not become a variant frag. Otherwise, the
2360 rearrangement of %hi relocs in frob_file may confuse
2361 tc_gen_reloc. */
2362 if (unmatched_hi)
2363 {
2364 frag_wane (frag_now);
2365 frag_new (0);
2366 }
2367}
2368
2369/* This function forgets that there was any previous instruction or
2370 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2371 know whether nops are needed before a noreorder section. */
252b5132
RH
2372
2373static void
2374mips_no_prev_insn (preserve)
2375 int preserve;
2376{
2377 if (! preserve)
2378 {
2379 prev_insn.insn_mo = &dummy_opcode;
2380 prev_prev_insn.insn_mo = &dummy_opcode;
2381 prev_nop_frag = NULL;
2382 prev_nop_frag_holds = 0;
2383 prev_nop_frag_required = 0;
2384 prev_nop_frag_since = 0;
2385 }
2386 prev_insn_valid = 0;
2387 prev_insn_is_delay_slot = 0;
2388 prev_insn_unreordered = 0;
2389 prev_insn_extended = 0;
2390 prev_insn_reloc_type = BFD_RELOC_UNUSED;
2391 prev_prev_insn_unreordered = 0;
2392 mips_clear_insn_labels ();
2393}
2394
2395/* This function must be called whenever we turn on noreorder or emit
2396 something other than instructions. It inserts any NOPS which might
2397 be needed by the previous instruction, and clears the information
2398 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2399 instructions are to follow. */
252b5132
RH
2400
2401static void
2402mips_emit_delays (insns)
2403 boolean insns;
2404{
2405 if (! mips_opts.noreorder)
2406 {
2407 int nops;
2408
2409 nops = 0;
2410 if ((! mips_opts.mips16
9ce8a5dd 2411 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2412 && (! cop_interlocks
2413 && (prev_insn.insn_mo->pinfo
2414 & (INSN_LOAD_COPROC_DELAY
2415 | INSN_COPROC_MOVE_DELAY
2416 | INSN_WRITE_COND_CODE))))
2417 || (! hilo_interlocks
2418 && (prev_insn.insn_mo->pinfo
2419 & (INSN_READ_LO
2420 | INSN_READ_HI)))
2421 || (! mips_opts.mips16
2422 && ! gpr_interlocks
bdaaa2e1 2423 && (prev_insn.insn_mo->pinfo
252b5132
RH
2424 & INSN_LOAD_MEMORY_DELAY))
2425 || (! mips_opts.mips16
e7af610e 2426 && mips_opts.isa == ISA_MIPS1
252b5132
RH
2427 && (prev_insn.insn_mo->pinfo
2428 & INSN_COPROC_MEMORY_DELAY)))
2429 {
beae10d5 2430 /* Itbl support may require additional care here. */
252b5132
RH
2431 ++nops;
2432 if ((! mips_opts.mips16
9ce8a5dd 2433 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2434 && (! cop_interlocks
2435 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2436 || (! hilo_interlocks
2437 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2438 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2439 ++nops;
2440
2441 if (prev_insn_unreordered)
2442 nops = 0;
2443 }
2444 else if ((! mips_opts.mips16
9ce8a5dd 2445 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2446 && (! cop_interlocks
2447 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2448 || (! hilo_interlocks
2449 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2450 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2451 {
beae10d5 2452 /* Itbl support may require additional care here. */
252b5132
RH
2453 if (! prev_prev_insn_unreordered)
2454 ++nops;
2455 }
2456
2457 if (nops > 0)
2458 {
2459 struct insn_label_list *l;
2460
2461 if (insns)
2462 {
2463 /* Record the frag which holds the nop instructions, so
2464 that we can remove them if we don't need them. */
2465 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2466 prev_nop_frag = frag_now;
2467 prev_nop_frag_holds = nops;
2468 prev_nop_frag_required = 0;
2469 prev_nop_frag_since = 0;
2470 }
2471
2472 for (; nops > 0; --nops)
2473 emit_nop ();
2474
2475 if (insns)
2476 {
2477 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2478 decrease the size of prev_nop_frag. */
252b5132
RH
2479 frag_wane (frag_now);
2480 frag_new (0);
2481 }
2482
2483 for (l = insn_labels; l != NULL; l = l->next)
2484 {
98aa84af
AM
2485 valueT val;
2486
252b5132 2487 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2488 symbol_set_frag (l->label, frag_now);
98aa84af 2489 val = (valueT) frag_now_fix ();
252b5132
RH
2490 /* mips16 text labels are stored as odd. */
2491 if (mips_opts.mips16)
98aa84af
AM
2492 val += 1;
2493 S_SET_VALUE (l->label, val);
252b5132
RH
2494 }
2495 }
2496 }
2497
2498 /* Mark instruction labels in mips16 mode. */
2499 if (mips_opts.mips16 && insns)
2500 mips16_mark_labels ();
2501
2502 mips_no_prev_insn (insns);
2503}
2504
2505/* Build an instruction created by a macro expansion. This is passed
2506 a pointer to the count of instructions created so far, an
2507 expression, the name of the instruction to build, an operand format
2508 string, and corresponding arguments. */
2509
2510#ifdef USE_STDARG
2511static void
2512macro_build (char *place,
2513 int *counter,
2514 expressionS * ep,
2515 const char *name,
2516 const char *fmt,
2517 ...)
2518#else
2519static void
2520macro_build (place, counter, ep, name, fmt, va_alist)
2521 char *place;
2522 int *counter;
2523 expressionS *ep;
2524 const char *name;
2525 const char *fmt;
2526 va_dcl
2527#endif
2528{
2529 struct mips_cl_insn insn;
2530 bfd_reloc_code_real_type r;
2531 va_list args;
252b5132
RH
2532
2533#ifdef USE_STDARG
2534 va_start (args, fmt);
2535#else
2536 va_start (args);
2537#endif
2538
2539 /*
2540 * If the macro is about to expand into a second instruction,
2541 * print a warning if needed. We need to pass ip as a parameter
2542 * to generate a better warning message here...
2543 */
2544 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2545 as_warn (_("Macro instruction expanded into multiple instructions"));
2546
2547 if (place == NULL)
2548 *counter += 1; /* bump instruction counter */
2549
2550 if (mips_opts.mips16)
2551 {
2552 mips16_macro_build (place, counter, ep, name, fmt, args);
2553 va_end (args);
2554 return;
2555 }
2556
2557 r = BFD_RELOC_UNUSED;
2558 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2559 assert (insn.insn_mo);
2560 assert (strcmp (name, insn.insn_mo->name) == 0);
2561
2562 /* Search until we get a match for NAME. */
2563 while (1)
2564 {
252b5132
RH
2565 if (strcmp (fmt, insn.insn_mo->args) == 0
2566 && insn.insn_mo->pinfo != INSN_MACRO
ea1fb5dc 2567 && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
ec68c924 2568 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
2569 break;
2570
2571 ++insn.insn_mo;
2572 assert (insn.insn_mo->name);
2573 assert (strcmp (name, insn.insn_mo->name) == 0);
2574 }
2575
2576 insn.insn_opcode = insn.insn_mo->match;
2577 for (;;)
2578 {
2579 switch (*fmt++)
2580 {
2581 case '\0':
2582 break;
2583
2584 case ',':
2585 case '(':
2586 case ')':
2587 continue;
2588
2589 case 't':
2590 case 'w':
2591 case 'E':
38487616 2592 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
2593 continue;
2594
2595 case 'c':
38487616
TS
2596 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2597 continue;
2598
252b5132
RH
2599 case 'T':
2600 case 'W':
38487616 2601 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
2602 continue;
2603
2604 case 'd':
2605 case 'G':
38487616 2606 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
2607 continue;
2608
4372b673
NC
2609 case 'U':
2610 {
2611 int tmp = va_arg (args, int);
2612
38487616
TS
2613 insn.insn_opcode |= tmp << OP_SH_RT;
2614 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 2615 continue;
4372b673
NC
2616 }
2617
252b5132
RH
2618 case 'V':
2619 case 'S':
38487616 2620 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
2621 continue;
2622
2623 case 'z':
2624 continue;
2625
2626 case '<':
38487616 2627 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
2628 continue;
2629
2630 case 'D':
38487616 2631 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
2632 continue;
2633
2634 case 'B':
38487616 2635 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
2636 continue;
2637
4372b673 2638 case 'J':
38487616 2639 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
2640 continue;
2641
252b5132 2642 case 'q':
38487616 2643 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
2644 continue;
2645
2646 case 'b':
2647 case 's':
2648 case 'r':
2649 case 'v':
38487616 2650 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
2651 continue;
2652
2653 case 'i':
2654 case 'j':
2655 case 'o':
2656 r = (bfd_reloc_code_real_type) va_arg (args, int);
2657 assert (r == BFD_RELOC_MIPS_GPREL
2658 || r == BFD_RELOC_MIPS_LITERAL
2659 || r == BFD_RELOC_LO16
2660 || r == BFD_RELOC_MIPS_GOT16
2661 || r == BFD_RELOC_MIPS_CALL16
2662 || r == BFD_RELOC_MIPS_GOT_LO16
2663 || r == BFD_RELOC_MIPS_CALL_LO16
2664 || (ep->X_op == O_subtract
252b5132
RH
2665 && r == BFD_RELOC_PCREL_LO16));
2666 continue;
2667
2668 case 'u':
2669 r = (bfd_reloc_code_real_type) va_arg (args, int);
2670 assert (ep != NULL
2671 && (ep->X_op == O_constant
2672 || (ep->X_op == O_symbol
2673 && (r == BFD_RELOC_HI16_S
2674 || r == BFD_RELOC_HI16
2675 || r == BFD_RELOC_MIPS_GOT_HI16
2676 || r == BFD_RELOC_MIPS_CALL_HI16))
2677 || (ep->X_op == O_subtract
252b5132
RH
2678 && r == BFD_RELOC_PCREL_HI16_S)));
2679 if (ep->X_op == O_constant)
2680 {
2681 insn.insn_opcode |= (ep->X_add_number >> 16) & 0xffff;
2682 ep = NULL;
2683 r = BFD_RELOC_UNUSED;
2684 }
2685 continue;
2686
2687 case 'p':
2688 assert (ep != NULL);
2689 /*
2690 * This allows macro() to pass an immediate expression for
2691 * creating short branches without creating a symbol.
2692 * Note that the expression still might come from the assembly
2693 * input, in which case the value is not checked for range nor
2694 * is a relocation entry generated (yuck).
2695 */
2696 if (ep->X_op == O_constant)
2697 {
2698 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2699 ep = NULL;
2700 }
2701 else
2702 r = BFD_RELOC_16_PCREL_S2;
2703 continue;
2704
2705 case 'a':
2706 assert (ep != NULL);
2707 r = BFD_RELOC_MIPS_JMP;
2708 continue;
2709
2710 case 'C':
2711 insn.insn_opcode |= va_arg (args, unsigned long);
2712 continue;
2713
2714 default:
2715 internalError ();
2716 }
2717 break;
2718 }
2719 va_end (args);
2720 assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2721
2722 append_insn (place, &insn, ep, r, false);
2723}
2724
2725static void
2726mips16_macro_build (place, counter, ep, name, fmt, args)
2727 char *place;
43841e91 2728 int *counter ATTRIBUTE_UNUSED;
252b5132
RH
2729 expressionS *ep;
2730 const char *name;
2731 const char *fmt;
2732 va_list args;
2733{
2734 struct mips_cl_insn insn;
2735 bfd_reloc_code_real_type r;
2736
2737 r = BFD_RELOC_UNUSED;
2738 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2739 assert (insn.insn_mo);
2740 assert (strcmp (name, insn.insn_mo->name) == 0);
2741
2742 while (strcmp (fmt, insn.insn_mo->args) != 0
2743 || insn.insn_mo->pinfo == INSN_MACRO)
2744 {
2745 ++insn.insn_mo;
2746 assert (insn.insn_mo->name);
2747 assert (strcmp (name, insn.insn_mo->name) == 0);
2748 }
2749
2750 insn.insn_opcode = insn.insn_mo->match;
2751 insn.use_extend = false;
2752
2753 for (;;)
2754 {
2755 int c;
2756
2757 c = *fmt++;
2758 switch (c)
2759 {
2760 case '\0':
2761 break;
2762
2763 case ',':
2764 case '(':
2765 case ')':
2766 continue;
2767
2768 case 'y':
2769 case 'w':
2770 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2771 continue;
2772
2773 case 'x':
2774 case 'v':
2775 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2776 continue;
2777
2778 case 'z':
2779 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2780 continue;
2781
2782 case 'Z':
2783 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2784 continue;
2785
2786 case '0':
2787 case 'S':
2788 case 'P':
2789 case 'R':
2790 continue;
2791
2792 case 'X':
2793 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2794 continue;
2795
2796 case 'Y':
2797 {
2798 int regno;
2799
2800 regno = va_arg (args, int);
2801 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2802 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2803 }
2804 continue;
2805
2806 case '<':
2807 case '>':
2808 case '4':
2809 case '5':
2810 case 'H':
2811 case 'W':
2812 case 'D':
2813 case 'j':
2814 case '8':
2815 case 'V':
2816 case 'C':
2817 case 'U':
2818 case 'k':
2819 case 'K':
2820 case 'p':
2821 case 'q':
2822 {
2823 assert (ep != NULL);
2824
2825 if (ep->X_op != O_constant)
2826 r = BFD_RELOC_UNUSED + c;
2827 else
2828 {
2829 mips16_immed ((char *) NULL, 0, c, ep->X_add_number, false,
2830 false, false, &insn.insn_opcode,
2831 &insn.use_extend, &insn.extend);
2832 ep = NULL;
2833 r = BFD_RELOC_UNUSED;
2834 }
2835 }
2836 continue;
2837
2838 case '6':
2839 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2840 continue;
2841 }
2842
2843 break;
2844 }
2845
2846 assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2847
2848 append_insn (place, &insn, ep, r, false);
2849}
2850
2851/*
2852 * Generate a "lui" instruction.
2853 */
2854static void
2855macro_build_lui (place, counter, ep, regnum)
2856 char *place;
2857 int *counter;
2858 expressionS *ep;
2859 int regnum;
2860{
2861 expressionS high_expr;
2862 struct mips_cl_insn insn;
2863 bfd_reloc_code_real_type r;
2864 CONST char *name = "lui";
2865 CONST char *fmt = "t,u";
2866
2867 assert (! mips_opts.mips16);
2868
2869 if (place == NULL)
2870 high_expr = *ep;
2871 else
2872 {
2873 high_expr.X_op = O_constant;
2874 high_expr.X_add_number = ep->X_add_number;
2875 }
2876
2877 if (high_expr.X_op == O_constant)
2878 {
2879 /* we can compute the instruction now without a relocation entry */
2880 if (high_expr.X_add_number & 0x8000)
2881 high_expr.X_add_number += 0x10000;
2882 high_expr.X_add_number =
2883 ((unsigned long) high_expr.X_add_number >> 16) & 0xffff;
2884 r = BFD_RELOC_UNUSED;
2885 }
2886 else
2887 {
2888 assert (ep->X_op == O_symbol);
2889 /* _gp_disp is a special case, used from s_cpload. */
2890 assert (mips_pic == NO_PIC
2891 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
2892 r = BFD_RELOC_HI16_S;
2893 }
2894
2895 /*
2896 * If the macro is about to expand into a second instruction,
2897 * print a warning if needed. We need to pass ip as a parameter
2898 * to generate a better warning message here...
2899 */
2900 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2901 as_warn (_("Macro instruction expanded into multiple instructions"));
2902
2903 if (place == NULL)
2904 *counter += 1; /* bump instruction counter */
2905
2906 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2907 assert (insn.insn_mo);
2908 assert (strcmp (name, insn.insn_mo->name) == 0);
2909 assert (strcmp (fmt, insn.insn_mo->args) == 0);
2910
2911 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
2912 if (r == BFD_RELOC_UNUSED)
2913 {
2914 insn.insn_opcode |= high_expr.X_add_number;
2915 append_insn (place, &insn, NULL, r, false);
2916 }
2917 else
2918 append_insn (place, &insn, &high_expr, r, false);
2919}
2920
2921/* set_at()
2922 * Generates code to set the $at register to true (one)
2923 * if reg is less than the immediate expression.
2924 */
2925static void
2926set_at (counter, reg, unsignedp)
2927 int *counter;
2928 int reg;
2929 int unsignedp;
2930{
2931 if (imm_expr.X_op == O_constant
2932 && imm_expr.X_add_number >= -0x8000
2933 && imm_expr.X_add_number < 0x8000)
2934 macro_build ((char *) NULL, counter, &imm_expr,
2935 unsignedp ? "sltiu" : "slti",
2936 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
2937 else
2938 {
2939 load_register (counter, AT, &imm_expr, 0);
2940 macro_build ((char *) NULL, counter, NULL,
2941 unsignedp ? "sltu" : "slt",
2942 "d,v,t", AT, reg, AT);
2943 }
2944}
2945
2946/* Warn if an expression is not a constant. */
2947
2948static void
2949check_absolute_expr (ip, ex)
2950 struct mips_cl_insn *ip;
2951 expressionS *ex;
2952{
2953 if (ex->X_op == O_big)
2954 as_bad (_("unsupported large constant"));
2955 else if (ex->X_op != O_constant)
2956 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
2957}
2958
2959/* Count the leading zeroes by performing a binary chop. This is a
2960 bulky bit of source, but performance is a LOT better for the
2961 majority of values than a simple loop to count the bits:
2962 for (lcnt = 0; (lcnt < 32); lcnt++)
2963 if ((v) & (1 << (31 - lcnt)))
2964 break;
2965 However it is not code size friendly, and the gain will drop a bit
2966 on certain cached systems.
2967*/
2968#define COUNT_TOP_ZEROES(v) \
2969 (((v) & ~0xffff) == 0 \
2970 ? ((v) & ~0xff) == 0 \
2971 ? ((v) & ~0xf) == 0 \
2972 ? ((v) & ~0x3) == 0 \
2973 ? ((v) & ~0x1) == 0 \
2974 ? !(v) \
2975 ? 32 \
2976 : 31 \
2977 : 30 \
2978 : ((v) & ~0x7) == 0 \
2979 ? 29 \
2980 : 28 \
2981 : ((v) & ~0x3f) == 0 \
2982 ? ((v) & ~0x1f) == 0 \
2983 ? 27 \
2984 : 26 \
2985 : ((v) & ~0x7f) == 0 \
2986 ? 25 \
2987 : 24 \
2988 : ((v) & ~0xfff) == 0 \
2989 ? ((v) & ~0x3ff) == 0 \
2990 ? ((v) & ~0x1ff) == 0 \
2991 ? 23 \
2992 : 22 \
2993 : ((v) & ~0x7ff) == 0 \
2994 ? 21 \
2995 : 20 \
2996 : ((v) & ~0x3fff) == 0 \
2997 ? ((v) & ~0x1fff) == 0 \
2998 ? 19 \
2999 : 18 \
3000 : ((v) & ~0x7fff) == 0 \
3001 ? 17 \
3002 : 16 \
3003 : ((v) & ~0xffffff) == 0 \
3004 ? ((v) & ~0xfffff) == 0 \
3005 ? ((v) & ~0x3ffff) == 0 \
3006 ? ((v) & ~0x1ffff) == 0 \
3007 ? 15 \
3008 : 14 \
3009 : ((v) & ~0x7ffff) == 0 \
3010 ? 13 \
3011 : 12 \
3012 : ((v) & ~0x3fffff) == 0 \
3013 ? ((v) & ~0x1fffff) == 0 \
3014 ? 11 \
3015 : 10 \
3016 : ((v) & ~0x7fffff) == 0 \
3017 ? 9 \
3018 : 8 \
3019 : ((v) & ~0xfffffff) == 0 \
3020 ? ((v) & ~0x3ffffff) == 0 \
3021 ? ((v) & ~0x1ffffff) == 0 \
3022 ? 7 \
3023 : 6 \
3024 : ((v) & ~0x7ffffff) == 0 \
3025 ? 5 \
3026 : 4 \
3027 : ((v) & ~0x3fffffff) == 0 \
3028 ? ((v) & ~0x1fffffff) == 0 \
3029 ? 3 \
3030 : 2 \
3031 : ((v) & ~0x7fffffff) == 0 \
3032 ? 1 \
3033 : 0)
3034
3035/* load_register()
3036 * This routine generates the least number of instructions neccessary to load
3037 * an absolute expression value into a register.
3038 */
3039static void
3040load_register (counter, reg, ep, dbl)
3041 int *counter;
3042 int reg;
3043 expressionS *ep;
3044 int dbl;
3045{
3046 int freg;
3047 expressionS hi32, lo32;
3048
3049 if (ep->X_op != O_big)
3050 {
3051 assert (ep->X_op == O_constant);
3052 if (ep->X_add_number < 0x8000
3053 && (ep->X_add_number >= 0
3054 || (ep->X_add_number >= -0x8000
3055 && (! dbl
3056 || ! ep->X_unsigned
3057 || sizeof (ep->X_add_number) > 4))))
3058 {
3059 /* We can handle 16 bit signed values with an addiu to
3060 $zero. No need to ever use daddiu here, since $zero and
3061 the result are always correct in 32 bit mode. */
3062 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3063 (int) BFD_RELOC_LO16);
3064 return;
3065 }
3066 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3067 {
3068 /* We can handle 16 bit unsigned values with an ori to
3069 $zero. */
3070 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3071 (int) BFD_RELOC_LO16);
3072 return;
3073 }
3074 else if ((((ep->X_add_number &~ (offsetT) 0x7fffffff) == 0
3075 || ((ep->X_add_number &~ (offsetT) 0x7fffffff)
3076 == ~ (offsetT) 0x7fffffff))
3077 && (! dbl
3078 || ! ep->X_unsigned
3079 || sizeof (ep->X_add_number) > 4
3080 || (ep->X_add_number & 0x80000000) == 0))
ca4e0257 3081 || ((HAVE_32BIT_GPRS || ! dbl)
252b5132 3082 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
ca4e0257 3083 || (HAVE_32BIT_GPRS
252b5132
RH
3084 && ! dbl
3085 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3086 == ~ (offsetT) 0xffffffff)))
3087 {
3088 /* 32 bit values require an lui. */
3089 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3090 (int) BFD_RELOC_HI16);
3091 if ((ep->X_add_number & 0xffff) != 0)
3092 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3093 (int) BFD_RELOC_LO16);
3094 return;
3095 }
3096 }
3097
3098 /* The value is larger than 32 bits. */
3099
ca4e0257 3100 if (HAVE_32BIT_GPRS)
252b5132
RH
3101 {
3102 as_bad (_("Number larger than 32 bits"));
3103 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3104 (int) BFD_RELOC_LO16);
3105 return;
3106 }
3107
3108 if (ep->X_op != O_big)
3109 {
3110 hi32 = *ep;
3111 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3112 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3113 hi32.X_add_number &= 0xffffffff;
3114 lo32 = *ep;
3115 lo32.X_add_number &= 0xffffffff;
3116 }
3117 else
3118 {
3119 assert (ep->X_add_number > 2);
3120 if (ep->X_add_number == 3)
3121 generic_bignum[3] = 0;
3122 else if (ep->X_add_number > 4)
3123 as_bad (_("Number larger than 64 bits"));
3124 lo32.X_op = O_constant;
3125 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3126 hi32.X_op = O_constant;
3127 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3128 }
3129
3130 if (hi32.X_add_number == 0)
3131 freg = 0;
3132 else
3133 {
3134 int shift, bit;
3135 unsigned long hi, lo;
3136
3137 if (hi32.X_add_number == 0xffffffff)
beae10d5
KH
3138 {
3139 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3140 {
3141 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
252b5132 3142 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3143 return;
3144 }
3145 if (lo32.X_add_number & 0x80000000)
3146 {
3147 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3148 (int) BFD_RELOC_HI16);
252b5132
RH
3149 if (lo32.X_add_number & 0xffff)
3150 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3151 reg, reg, (int) BFD_RELOC_LO16);
beae10d5
KH
3152 return;
3153 }
3154 }
252b5132
RH
3155
3156 /* Check for 16bit shifted constant. We know that hi32 is
3157 non-zero, so start the mask on the first bit of the hi32
3158 value. */
3159 shift = 17;
3160 do
beae10d5
KH
3161 {
3162 unsigned long himask, lomask;
3163
3164 if (shift < 32)
3165 {
3166 himask = 0xffff >> (32 - shift);
3167 lomask = (0xffff << shift) & 0xffffffff;
3168 }
3169 else
3170 {
3171 himask = 0xffff << (shift - 32);
3172 lomask = 0;
3173 }
3174 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3175 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3176 {
3177 expressionS tmp;
3178
3179 tmp.X_op = O_constant;
3180 if (shift < 32)
3181 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3182 | (lo32.X_add_number >> shift));
3183 else
3184 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3185 macro_build ((char *) NULL, counter, &tmp,
3186 "ori", "t,r,i", reg, 0,
3187 (int) BFD_RELOC_LO16);
3188 macro_build ((char *) NULL, counter, NULL,
3189 (shift >= 32) ? "dsll32" : "dsll",
3190 "d,w,<", reg, reg,
3191 (shift >= 32) ? shift - 32 : shift);
3192 return;
3193 }
3194 shift++;
3195 }
3196 while (shift <= (64 - 16));
252b5132
RH
3197
3198 /* Find the bit number of the lowest one bit, and store the
3199 shifted value in hi/lo. */
3200 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3201 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3202 if (lo != 0)
3203 {
3204 bit = 0;
3205 while ((lo & 1) == 0)
3206 {
3207 lo >>= 1;
3208 ++bit;
3209 }
3210 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3211 hi >>= bit;
3212 }
3213 else
3214 {
3215 bit = 32;
3216 while ((hi & 1) == 0)
3217 {
3218 hi >>= 1;
3219 ++bit;
3220 }
3221 lo = hi;
3222 hi = 0;
3223 }
3224
3225 /* Optimize if the shifted value is a (power of 2) - 1. */
3226 if ((hi == 0 && ((lo + 1) & lo) == 0)
3227 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3228 {
3229 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3230 if (shift != 0)
beae10d5 3231 {
252b5132
RH
3232 expressionS tmp;
3233
3234 /* This instruction will set the register to be all
3235 ones. */
beae10d5
KH
3236 tmp.X_op = O_constant;
3237 tmp.X_add_number = (offsetT) -1;
3238 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
252b5132 3239 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3240 if (bit != 0)
3241 {
3242 bit += shift;
3243 macro_build ((char *) NULL, counter, NULL,
3244 (bit >= 32) ? "dsll32" : "dsll",
3245 "d,w,<", reg, reg,
3246 (bit >= 32) ? bit - 32 : bit);
3247 }
3248 macro_build ((char *) NULL, counter, NULL,
252b5132 3249 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3250 "d,w,<", reg, reg,
252b5132 3251 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3252 return;
3253 }
3254 }
252b5132
RH
3255
3256 /* Sign extend hi32 before calling load_register, because we can
3257 generally get better code when we load a sign extended value. */
3258 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3259 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3260 load_register (counter, reg, &hi32, 0);
3261 freg = reg;
3262 }
3263 if ((lo32.X_add_number & 0xffff0000) == 0)
3264 {
3265 if (freg != 0)
3266 {
3267 macro_build ((char *) NULL, counter, NULL, "dsll32", "d,w,<", reg,
3268 freg, 0);
3269 freg = reg;
3270 }
3271 }
3272 else
3273 {
3274 expressionS mid16;
3275
3276 if ((freg == 0) && (lo32.X_add_number == 0xffffffff))
beae10d5 3277 {
252b5132
RH
3278 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3279 (int) BFD_RELOC_HI16);
beae10d5
KH
3280 macro_build ((char *) NULL, counter, NULL, "dsrl32", "d,w,<", reg,
3281 reg, 0);
3282 return;
3283 }
252b5132
RH
3284
3285 if (freg != 0)
3286 {
3287 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
3288 freg, 16);
3289 freg = reg;
3290 }
3291 mid16 = lo32;
3292 mid16.X_add_number >>= 16;
3293 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3294 freg, (int) BFD_RELOC_LO16);
3295 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
3296 reg, 16);
3297 freg = reg;
3298 }
3299 if ((lo32.X_add_number & 0xffff) != 0)
3300 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3301 (int) BFD_RELOC_LO16);
3302}
3303
3304/* Load an address into a register. */
3305
3306static void
3307load_address (counter, reg, ep)
3308 int *counter;
3309 int reg;
3310 expressionS *ep;
3311{
3312 char *p;
3313
3314 if (ep->X_op != O_constant
3315 && ep->X_op != O_symbol)
3316 {
3317 as_bad (_("expression too complex"));
3318 ep->X_op = O_constant;
3319 }
3320
3321 if (ep->X_op == O_constant)
3322 {
3323 load_register (counter, reg, ep, 0);
3324 return;
3325 }
3326
3327 if (mips_pic == NO_PIC)
3328 {
3329 /* If this is a reference to a GP relative symbol, we want
3330 addiu $reg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
3331 Otherwise we want
3332 lui $reg,<sym> (BFD_RELOC_HI16_S)
3333 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3334 If we have an addend, we always use the latter form. */
3335 if ((valueT) ep->X_add_number >= MAX_GPREL_OFFSET
beae10d5 3336 || nopic_need_relax (ep->X_add_symbol, 1))
252b5132
RH
3337 p = NULL;
3338 else
3339 {
3340 frag_grow (20);
3341 macro_build ((char *) NULL, counter, ep,
ca4e0257 3342 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3343 "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
3344 p = frag_var (rs_machine_dependent, 8, 0,
3345 RELAX_ENCODE (4, 8, 0, 4, 0,
3346 mips_opts.warn_about_macros),
3347 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3348 }
3349 macro_build_lui (p, counter, ep, reg);
3350 if (p != NULL)
3351 p += 4;
3352 macro_build (p, counter, ep,
ca4e0257 3353 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3354 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3355 }
3356 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3357 {
3358 expressionS ex;
3359
3360 /* If this is a reference to an external symbol, we want
3361 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3362 Otherwise we want
3363 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3364 nop
3365 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3366 If there is a constant, it must be added in after. */
3367 ex.X_add_number = ep->X_add_number;
3368 ep->X_add_number = 0;
3369 frag_grow (20);
3370 macro_build ((char *) NULL, counter, ep,
ca4e0257 3371 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
3372 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3373 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3374 p = frag_var (rs_machine_dependent, 4, 0,
3375 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3376 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3377 macro_build (p, counter, ep,
ca4e0257 3378 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3379 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3380 if (ex.X_add_number != 0)
3381 {
3382 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3383 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3384 ex.X_op = O_constant;
3385 macro_build ((char *) NULL, counter, &ex,
ca4e0257 3386 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3387 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3388 }
3389 }
3390 else if (mips_pic == SVR4_PIC)
3391 {
3392 expressionS ex;
3393 int off;
3394
3395 /* This is the large GOT case. If this is a reference to an
3396 external symbol, we want
3397 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3398 addu $reg,$reg,$gp
3399 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3400 Otherwise, for a reference to a local symbol, we want
3401 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3402 nop
3403 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3404 If there is a constant, it must be added in after. */
3405 ex.X_add_number = ep->X_add_number;
3406 ep->X_add_number = 0;
3407 if (reg_needs_delay (GP))
3408 off = 4;
3409 else
3410 off = 0;
3411 frag_grow (32);
3412 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3413 (int) BFD_RELOC_MIPS_GOT_HI16);
3414 macro_build ((char *) NULL, counter, (expressionS *) NULL,
ca4e0257 3415 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
3416 "d,v,t", reg, reg, GP);
3417 macro_build ((char *) NULL, counter, ep,
ca4e0257 3418 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
3419 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3420 p = frag_var (rs_machine_dependent, 12 + off, 0,
3421 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3422 mips_opts.warn_about_macros),
3423 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3424 if (off > 0)
3425 {
3426 /* We need a nop before loading from $gp. This special
3427 check is required because the lui which starts the main
3428 instruction stream does not refer to $gp, and so will not
3429 insert the nop which may be required. */
3430 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3431 p += 4;
3432 }
9a41af64 3433 macro_build (p, counter, ep, HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
3434 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3435 p += 4;
3436 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3437 p += 4;
9a41af64 3438 macro_build (p, counter, ep, HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3439 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3440 if (ex.X_add_number != 0)
3441 {
3442 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3443 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3444 ex.X_op = O_constant;
3445 macro_build ((char *) NULL, counter, &ex,
ca4e0257 3446 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3447 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3448 }
3449 }
3450 else if (mips_pic == EMBEDDED_PIC)
3451 {
3452 /* We always do
3453 addiu $reg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
3454 */
3455 macro_build ((char *) NULL, counter, ep,
ca4e0257 3456 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3457 "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
3458 }
3459 else
3460 abort ();
3461}
3462
ea1fb5dc
RS
3463/* Move the contents of register SOURCE into register DEST. */
3464
3465static void
3466move_register (counter, dest, source)
3467 int *counter;
3468 int dest;
3469 int source;
3470{
3471 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3472 HAVE_32BIT_GPRS ? "addu" : "daddu",
3473 "d,v,t", dest, source, 0);
3474}
3475
252b5132
RH
3476/*
3477 * Build macros
3478 * This routine implements the seemingly endless macro or synthesized
3479 * instructions and addressing modes in the mips assembly language. Many
3480 * of these macros are simple and are similar to each other. These could
3481 * probably be handled by some kind of table or grammer aproach instead of
3482 * this verbose method. Others are not simple macros but are more like
3483 * optimizing code generation.
3484 * One interesting optimization is when several store macros appear
3485 * consecutivly that would load AT with the upper half of the same address.
3486 * The ensuing load upper instructions are ommited. This implies some kind
3487 * of global optimization. We currently only optimize within a single macro.
3488 * For many of the load and store macros if the address is specified as a
3489 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3490 * first load register 'at' with zero and use it as the base register. The
3491 * mips assembler simply uses register $zero. Just one tiny optimization
3492 * we're missing.
3493 */
3494static void
3495macro (ip)
3496 struct mips_cl_insn *ip;
3497{
3498 register int treg, sreg, dreg, breg;
3499 int tempreg;
3500 int mask;
3501 int icnt = 0;
43841e91 3502 int used_at = 0;
252b5132
RH
3503 expressionS expr1;
3504 const char *s;
3505 const char *s2;
3506 const char *fmt;
3507 int likely = 0;
3508 int dbl = 0;
3509 int coproc = 0;
3510 int lr = 0;
3511 int imm = 0;
3512 offsetT maxnum;
3513 int off;
3514 bfd_reloc_code_real_type r;
3515 char *p;
3516 int hold_mips_optimize;
3517
3518 assert (! mips_opts.mips16);
3519
3520 treg = (ip->insn_opcode >> 16) & 0x1f;
3521 dreg = (ip->insn_opcode >> 11) & 0x1f;
3522 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3523 mask = ip->insn_mo->mask;
3524
3525 expr1.X_op = O_constant;
3526 expr1.X_op_symbol = NULL;
3527 expr1.X_add_symbol = NULL;
3528 expr1.X_add_number = 1;
3529
3530 switch (mask)
3531 {
3532 case M_DABS:
3533 dbl = 1;
3534 case M_ABS:
3535 /* bgez $a0,.+12
3536 move v0,$a0
3537 sub v0,$zero,$a0
3538 */
3539
3540 mips_emit_delays (true);
3541 ++mips_opts.noreorder;
3542 mips_any_noreorder = 1;
3543
3544 expr1.X_add_number = 8;
3545 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3546 if (dreg == sreg)
3547 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3548 else
ea1fb5dc 3549 move_register (&icnt, dreg, sreg);
252b5132 3550 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 3551 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
3552
3553 --mips_opts.noreorder;
3554 return;
3555
3556 case M_ADD_I:
3557 s = "addi";
3558 s2 = "add";
3559 goto do_addi;
3560 case M_ADDU_I:
3561 s = "addiu";
3562 s2 = "addu";
3563 goto do_addi;
3564 case M_DADD_I:
3565 dbl = 1;
3566 s = "daddi";
3567 s2 = "dadd";
3568 goto do_addi;
3569 case M_DADDU_I:
3570 dbl = 1;
3571 s = "daddiu";
3572 s2 = "daddu";
3573 do_addi:
3574 if (imm_expr.X_op == O_constant
3575 && imm_expr.X_add_number >= -0x8000
3576 && imm_expr.X_add_number < 0x8000)
3577 {
3578 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3579 (int) BFD_RELOC_LO16);
3580 return;
3581 }
3582 load_register (&icnt, AT, &imm_expr, dbl);
3583 macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
3584 break;
3585
3586 case M_AND_I:
3587 s = "andi";
3588 s2 = "and";
3589 goto do_bit;
3590 case M_OR_I:
3591 s = "ori";
3592 s2 = "or";
3593 goto do_bit;
3594 case M_NOR_I:
3595 s = "";
3596 s2 = "nor";
3597 goto do_bit;
3598 case M_XOR_I:
3599 s = "xori";
3600 s2 = "xor";
3601 do_bit:
3602 if (imm_expr.X_op == O_constant
3603 && imm_expr.X_add_number >= 0
3604 && imm_expr.X_add_number < 0x10000)
3605 {
3606 if (mask != M_NOR_I)
3607 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3608 sreg, (int) BFD_RELOC_LO16);
3609 else
3610 {
3611 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3612 treg, sreg, (int) BFD_RELOC_LO16);
3613 macro_build ((char *) NULL, &icnt, NULL, "nor", "d,v,t",
3614 treg, treg, 0);
3615 }
3616 return;
3617 }
3618
3619 load_register (&icnt, AT, &imm_expr, 0);
3620 macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
3621 break;
3622
3623 case M_BEQ_I:
3624 s = "beq";
3625 goto beq_i;
3626 case M_BEQL_I:
3627 s = "beql";
3628 likely = 1;
3629 goto beq_i;
3630 case M_BNE_I:
3631 s = "bne";
3632 goto beq_i;
3633 case M_BNEL_I:
3634 s = "bnel";
3635 likely = 1;
3636 beq_i:
3637 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3638 {
3639 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3640 0);
3641 return;
3642 }
3643 load_register (&icnt, AT, &imm_expr, 0);
3644 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3645 break;
3646
3647 case M_BGEL:
3648 likely = 1;
3649 case M_BGE:
3650 if (treg == 0)
3651 {
3652 macro_build ((char *) NULL, &icnt, &offset_expr,
3653 likely ? "bgezl" : "bgez",
3654 "s,p", sreg);
3655 return;
3656 }
3657 if (sreg == 0)
3658 {
3659 macro_build ((char *) NULL, &icnt, &offset_expr,
3660 likely ? "blezl" : "blez",
3661 "s,p", treg);
3662 return;
3663 }
3664 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
3665 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3666 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3667 break;
3668
3669 case M_BGTL_I:
3670 likely = 1;
3671 case M_BGT_I:
3672 /* check for > max integer */
3673 maxnum = 0x7fffffff;
ca4e0257 3674 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
3675 {
3676 maxnum <<= 16;
3677 maxnum |= 0xffff;
3678 maxnum <<= 16;
3679 maxnum |= 0xffff;
3680 }
3681 if (imm_expr.X_op == O_constant
3682 && imm_expr.X_add_number >= maxnum
ca4e0257 3683 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
3684 {
3685 do_false:
3686 /* result is always false */
3687 if (! likely)
3688 {
39c0a331
L
3689 if (warn_nops)
3690 as_warn (_("Branch %s is always false (nop)"),
3691 ip->insn_mo->name);
252b5132
RH
3692 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3693 }
3694 else
3695 {
39c0a331
L
3696 if (warn_nops)
3697 as_warn (_("Branch likely %s is always false"),
3698 ip->insn_mo->name);
252b5132
RH
3699 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3700 "s,t,p", 0, 0);
3701 }
3702 return;
3703 }
3704 if (imm_expr.X_op != O_constant)
3705 as_bad (_("Unsupported large constant"));
3706 imm_expr.X_add_number++;
3707 /* FALLTHROUGH */
3708 case M_BGE_I:
3709 case M_BGEL_I:
3710 if (mask == M_BGEL_I)
3711 likely = 1;
3712 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3713 {
3714 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3715 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
3716 return;
3717 }
3718 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3719 {
3720 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3721 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
3722 return;
3723 }
3724 maxnum = 0x7fffffff;
ca4e0257 3725 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
3726 {
3727 maxnum <<= 16;
3728 maxnum |= 0xffff;
3729 maxnum <<= 16;
3730 maxnum |= 0xffff;
3731 }
3732 maxnum = - maxnum - 1;
3733 if (imm_expr.X_op == O_constant
3734 && imm_expr.X_add_number <= maxnum
ca4e0257 3735 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
3736 {
3737 do_true:
3738 /* result is always true */
3739 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
3740 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
3741 return;
3742 }
3743 set_at (&icnt, sreg, 0);
3744 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3745 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3746 break;
3747
3748 case M_BGEUL:
3749 likely = 1;
3750 case M_BGEU:
3751 if (treg == 0)
3752 goto do_true;
3753 if (sreg == 0)
3754 {
3755 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3756 likely ? "beql" : "beq", "s,t,p", 0, treg);
252b5132
RH
3757 return;
3758 }
3759 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
3760 treg);
3761 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3762 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3763 break;
3764
3765 case M_BGTUL_I:
3766 likely = 1;
3767 case M_BGTU_I:
3768 if (sreg == 0
ca4e0257 3769 || (HAVE_32BIT_GPRS
252b5132
RH
3770 && imm_expr.X_op == O_constant
3771 && imm_expr.X_add_number == 0xffffffff))
3772 goto do_false;
3773 if (imm_expr.X_op != O_constant)
3774 as_bad (_("Unsupported large constant"));
3775 imm_expr.X_add_number++;
3776 /* FALLTHROUGH */
3777 case M_BGEU_I:
3778 case M_BGEUL_I:
3779 if (mask == M_BGEUL_I)
3780 likely = 1;
3781 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3782 goto do_true;
3783 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3784 {
3785 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3786 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
3787 return;
3788 }
3789 set_at (&icnt, sreg, 1);
3790 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3791 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3792 break;
3793
3794 case M_BGTL:
3795 likely = 1;
3796 case M_BGT:
3797 if (treg == 0)
3798 {
3799 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3800 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
3801 return;
3802 }
3803 if (sreg == 0)
3804 {
3805 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3806 likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
3807 return;
3808 }
3809 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
3810 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3811 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
3812 break;
3813
3814 case M_BGTUL:
3815 likely = 1;
3816 case M_BGTU:
3817 if (treg == 0)
3818 {
3819 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3820 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
3821 return;
3822 }
3823 if (sreg == 0)
3824 goto do_false;
3825 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
3826 sreg);
3827 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3828 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
3829 break;
3830
3831 case M_BLEL:
3832 likely = 1;
3833 case M_BLE:
3834 if (treg == 0)
3835 {
3836 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3837 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
3838 return;
3839 }
3840 if (sreg == 0)
3841 {
3842 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3843 likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
3844 return;
3845 }
3846 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
3847 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3848 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3849 break;
3850
3851 case M_BLEL_I:
3852 likely = 1;
3853 case M_BLE_I:
3854 maxnum = 0x7fffffff;
ca4e0257 3855 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
3856 {
3857 maxnum <<= 16;
3858 maxnum |= 0xffff;
3859 maxnum <<= 16;
3860 maxnum |= 0xffff;
3861 }
3862 if (imm_expr.X_op == O_constant
3863 && imm_expr.X_add_number >= maxnum
ca4e0257 3864 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
3865 goto do_true;
3866 if (imm_expr.X_op != O_constant)
3867 as_bad (_("Unsupported large constant"));
3868 imm_expr.X_add_number++;
3869 /* FALLTHROUGH */
3870 case M_BLT_I:
3871 case M_BLTL_I:
3872 if (mask == M_BLTL_I)
3873 likely = 1;
3874 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3875 {
3876 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3877 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
3878 return;
3879 }
3880 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3881 {
3882 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3883 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
3884 return;
3885 }
3886 set_at (&icnt, sreg, 0);
3887 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3888 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
3889 break;
3890
3891 case M_BLEUL:
3892 likely = 1;
3893 case M_BLEU:
3894 if (treg == 0)
3895 {
3896 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3897 likely ? "beql" : "beq", "s,t,p", sreg, 0);
252b5132
RH
3898 return;
3899 }
3900 if (sreg == 0)
3901 goto do_true;
3902 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
3903 sreg);
3904 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3905 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3906 break;
3907
3908 case M_BLEUL_I:
3909 likely = 1;
3910 case M_BLEU_I:
3911 if (sreg == 0
ca4e0257 3912 || (HAVE_32BIT_GPRS
252b5132
RH
3913 && imm_expr.X_op == O_constant
3914 && imm_expr.X_add_number == 0xffffffff))
3915 goto do_true;
3916 if (imm_expr.X_op != O_constant)
3917 as_bad (_("Unsupported large constant"));
3918 imm_expr.X_add_number++;
3919 /* FALLTHROUGH */
3920 case M_BLTU_I:
3921 case M_BLTUL_I:
3922 if (mask == M_BLTUL_I)
3923 likely = 1;
3924 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3925 goto do_false;
3926 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3927 {
3928 macro_build ((char *) NULL, &icnt, &offset_expr,
3929 likely ? "beql" : "beq",
3930 "s,t,p", sreg, 0);
3931 return;
3932 }
3933 set_at (&icnt, sreg, 1);
3934 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3935 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
3936 break;
3937
3938 case M_BLTL:
3939 likely = 1;
3940 case M_BLT:
3941 if (treg == 0)
3942 {
3943 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3944 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
3945 return;
3946 }
3947 if (sreg == 0)
3948 {
3949 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3950 likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
3951 return;
3952 }
3953 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
3954 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3955 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
3956 break;
3957
3958 case M_BLTUL:
3959 likely = 1;
3960 case M_BLTU:
3961 if (treg == 0)
3962 goto do_false;
3963 if (sreg == 0)
3964 {
3965 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3966 likely ? "bnel" : "bne", "s,t,p", 0, treg);
252b5132
RH
3967 return;
3968 }
3969 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
3970 treg);
3971 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3972 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
3973 break;
3974
3975 case M_DDIV_3:
3976 dbl = 1;
3977 case M_DIV_3:
3978 s = "mflo";
3979 goto do_div3;
3980 case M_DREM_3:
3981 dbl = 1;
3982 case M_REM_3:
3983 s = "mfhi";
3984 do_div3:
3985 if (treg == 0)
3986 {
3987 as_warn (_("Divide by zero."));
3988 if (mips_trap)
3989 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
3990 else
9117d219 3991 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
3992 return;
3993 }
3994
3995 mips_emit_delays (true);
3996 ++mips_opts.noreorder;
3997 mips_any_noreorder = 1;
3998 if (mips_trap)
3999 {
4000 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
4001 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 4002 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4003 }
4004 else
4005 {
4006 expr1.X_add_number = 8;
4007 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4008 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 4009 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
9117d219 4010 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4011 }
4012 expr1.X_add_number = -1;
4013 macro_build ((char *) NULL, &icnt, &expr1,
4014 dbl ? "daddiu" : "addiu",
4015 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4016 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4017 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4018 if (dbl)
4019 {
4020 expr1.X_add_number = 1;
4021 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4022 (int) BFD_RELOC_LO16);
4023 macro_build ((char *) NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT,
4024 31);
4025 }
4026 else
4027 {
4028 expr1.X_add_number = 0x80000000;
4029 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4030 (int) BFD_RELOC_HI16);
4031 }
4032 if (mips_trap)
4033 {
4034 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", sreg, AT);
4035 /* We want to close the noreorder block as soon as possible, so
4036 that later insns are available for delay slot filling. */
4037 --mips_opts.noreorder;
4038 }
4039 else
4040 {
4041 expr1.X_add_number = 8;
4042 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4043 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
4044
4045 /* We want to close the noreorder block as soon as possible, so
4046 that later insns are available for delay slot filling. */
4047 --mips_opts.noreorder;
4048
9117d219 4049 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
252b5132
RH
4050 }
4051 macro_build ((char *) NULL, &icnt, NULL, s, "d", dreg);
4052 break;
4053
4054 case M_DIV_3I:
4055 s = "div";
4056 s2 = "mflo";
4057 goto do_divi;
4058 case M_DIVU_3I:
4059 s = "divu";
4060 s2 = "mflo";
4061 goto do_divi;
4062 case M_REM_3I:
4063 s = "div";
4064 s2 = "mfhi";
4065 goto do_divi;
4066 case M_REMU_3I:
4067 s = "divu";
4068 s2 = "mfhi";
4069 goto do_divi;
4070 case M_DDIV_3I:
4071 dbl = 1;
4072 s = "ddiv";
4073 s2 = "mflo";
4074 goto do_divi;
4075 case M_DDIVU_3I:
4076 dbl = 1;
4077 s = "ddivu";
4078 s2 = "mflo";
4079 goto do_divi;
4080 case M_DREM_3I:
4081 dbl = 1;
4082 s = "ddiv";
4083 s2 = "mfhi";
4084 goto do_divi;
4085 case M_DREMU_3I:
4086 dbl = 1;
4087 s = "ddivu";
4088 s2 = "mfhi";
4089 do_divi:
4090 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4091 {
4092 as_warn (_("Divide by zero."));
4093 if (mips_trap)
4094 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
4095 else
beae10d5 4096 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4097 return;
4098 }
4099 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4100 {
4101 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4102 move_register (&icnt, dreg, sreg);
252b5132 4103 else
ea1fb5dc 4104 move_register (&icnt, dreg, 0);
252b5132
RH
4105 return;
4106 }
4107 if (imm_expr.X_op == O_constant
4108 && imm_expr.X_add_number == -1
4109 && s[strlen (s) - 1] != 'u')
4110 {
4111 if (strcmp (s2, "mflo") == 0)
4112 {
9a41af64
TS
4113 macro_build ((char *) NULL, &icnt, NULL, dbl ? "dneg" : "neg",
4114 "d,w", dreg, sreg);
252b5132
RH
4115 }
4116 else
ea1fb5dc 4117 move_register (&icnt, dreg, 0);
252b5132
RH
4118 return;
4119 }
4120
4121 load_register (&icnt, AT, &imm_expr, dbl);
4122 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
4123 macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
4124 break;
4125
4126 case M_DIVU_3:
4127 s = "divu";
4128 s2 = "mflo";
4129 goto do_divu3;
4130 case M_REMU_3:
4131 s = "divu";
4132 s2 = "mfhi";
4133 goto do_divu3;
4134 case M_DDIVU_3:
4135 s = "ddivu";
4136 s2 = "mflo";
4137 goto do_divu3;
4138 case M_DREMU_3:
4139 s = "ddivu";
4140 s2 = "mfhi";
4141 do_divu3:
4142 mips_emit_delays (true);
4143 ++mips_opts.noreorder;
4144 mips_any_noreorder = 1;
4145 if (mips_trap)
4146 {
4147 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
4148 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4149 /* We want to close the noreorder block as soon as possible, so
4150 that later insns are available for delay slot filling. */
4151 --mips_opts.noreorder;
4152 }
4153 else
4154 {
4155 expr1.X_add_number = 8;
4156 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4157 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4158
4159 /* We want to close the noreorder block as soon as possible, so
4160 that later insns are available for delay slot filling. */
4161 --mips_opts.noreorder;
beae10d5 4162 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4163 }
4164 macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
4165 return;
4166
4167 case M_DLA_AB:
4168 dbl = 1;
4169 case M_LA_AB:
4170 /* Load the address of a symbol into a register. If breg is not
4171 zero, we then add a base register to it. */
4172
4173 /* When generating embedded PIC code, we permit expressions of
4174 the form
4175 la $4,foo-bar
bb2d6cd7 4176 where bar is an address in the current section. These are used
252b5132
RH
4177 when getting the addresses of functions. We don't permit
4178 X_add_number to be non-zero, because if the symbol is
4179 external the relaxing code needs to know that any addend is
4180 purely the offset to X_op_symbol. */
4181 if (mips_pic == EMBEDDED_PIC
4182 && offset_expr.X_op == O_subtract
49309057 4183 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4184 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4185 : (symbol_equated_p (offset_expr.X_op_symbol)
4186 && (S_GET_SEGMENT
4187 (symbol_get_value_expression (offset_expr.X_op_symbol)
4188 ->X_add_symbol)
bb2d6cd7 4189 == now_seg)))
252b5132 4190 && breg == 0
bb2d6cd7
GK
4191 && (offset_expr.X_add_number == 0
4192 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132
RH
4193 {
4194 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4195 treg, (int) BFD_RELOC_PCREL_HI16_S);
4196 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 4197 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4198 "t,r,j", treg, treg, (int) BFD_RELOC_PCREL_LO16);
4199 return;
4200 }
4201
4202 if (offset_expr.X_op != O_symbol
4203 && offset_expr.X_op != O_constant)
4204 {
4205 as_bad (_("expression too complex"));
4206 offset_expr.X_op = O_constant;
4207 }
4208
4209 if (treg == breg)
4210 {
4211 tempreg = AT;
4212 used_at = 1;
4213 }
4214 else
4215 {
4216 tempreg = treg;
4217 used_at = 0;
4218 }
4219
4220 if (offset_expr.X_op == O_constant)
4221 load_register (&icnt, tempreg, &offset_expr, dbl);
4222 else if (mips_pic == NO_PIC)
4223 {
4224 /* If this is a reference to an GP relative symbol, we want
4225 addiu $tempreg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
4226 Otherwise we want
4227 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4228 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4229 If we have a constant, we need two instructions anyhow,
4230 so we may as well always use the latter form. */
4231 if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
4232 || nopic_need_relax (offset_expr.X_add_symbol, 1))
4233 p = NULL;
4234 else
4235 {
4236 frag_grow (20);
4237 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 4238 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4239 "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
4240 p = frag_var (rs_machine_dependent, 8, 0,
4241 RELAX_ENCODE (4, 8, 0, 4, 0,
4242 mips_opts.warn_about_macros),
4243 offset_expr.X_add_symbol, (offsetT) 0,
4244 (char *) NULL);
4245 }
4246 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4247 if (p != NULL)
4248 p += 4;
4249 macro_build (p, &icnt, &offset_expr,
ca4e0257 4250 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4251 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4252 }
4253 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4254 {
9117d219
NC
4255 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4256
252b5132
RH
4257 /* If this is a reference to an external symbol, and there
4258 is no constant, we want
4259 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9117d219
NC
4260 or if tempreg is PIC_CALL_REG
4261 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
4262 For a local symbol, we want
4263 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4264 nop
4265 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4266
4267 If we have a small constant, and this is a reference to
4268 an external symbol, we want
4269 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4270 nop
4271 addiu $tempreg,$tempreg,<constant>
4272 For a local symbol, we want the same instruction
4273 sequence, but we output a BFD_RELOC_LO16 reloc on the
4274 addiu instruction.
4275
4276 If we have a large constant, and this is a reference to
4277 an external symbol, we want
4278 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4279 lui $at,<hiconstant>
4280 addiu $at,$at,<loconstant>
4281 addu $tempreg,$tempreg,$at
4282 For a local symbol, we want the same instruction
4283 sequence, but we output a BFD_RELOC_LO16 reloc on the
4284 addiu instruction. */
4285 expr1.X_add_number = offset_expr.X_add_number;
4286 offset_expr.X_add_number = 0;
4287 frag_grow (32);
9117d219
NC
4288 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4289 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
252b5132
RH
4290 macro_build ((char *) NULL, &icnt, &offset_expr,
4291 dbl ? "ld" : "lw",
9117d219 4292 "t,o(b)", tempreg, lw_reloc_type, GP);
252b5132
RH
4293 if (expr1.X_add_number == 0)
4294 {
4295 int off;
4296
4297 if (breg == 0)
4298 off = 0;
4299 else
4300 {
4301 /* We're going to put in an addu instruction using
4302 tempreg, so we may as well insert the nop right
4303 now. */
4304 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4305 "nop", "");
4306 off = 4;
4307 }
4308 p = frag_var (rs_machine_dependent, 8 - off, 0,
4309 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4310 (breg == 0
4311 ? mips_opts.warn_about_macros
4312 : 0)),
4313 offset_expr.X_add_symbol, (offsetT) 0,
4314 (char *) NULL);
4315 if (breg == 0)
4316 {
4317 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4318 p += 4;
4319 }
4320 macro_build (p, &icnt, &expr1,
ca4e0257 4321 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4322 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4323 /* FIXME: If breg == 0, and the next instruction uses
4324 $tempreg, then if this variant case is used an extra
4325 nop will be generated. */
4326 }
4327 else if (expr1.X_add_number >= -0x8000
4328 && expr1.X_add_number < 0x8000)
4329 {
4330 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4331 "nop", "");
4332 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4333 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4334 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4335 (void) frag_var (rs_machine_dependent, 0, 0,
4336 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4337 offset_expr.X_add_symbol, (offsetT) 0,
4338 (char *) NULL);
4339 }
4340 else
4341 {
4342 int off1;
4343
4344 /* If we are going to add in a base register, and the
4345 target register and the base register are the same,
4346 then we are using AT as a temporary register. Since
4347 we want to load the constant into AT, we add our
4348 current AT (from the global offset table) and the
4349 register into the register now, and pretend we were
4350 not using a base register. */
4351 if (breg != treg)
4352 off1 = 0;
4353 else
4354 {
4355 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4356 "nop", "");
4357 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4358 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4359 "d,v,t", treg, AT, breg);
4360 breg = 0;
4361 tempreg = treg;
4362 off1 = -8;
4363 }
4364
4365 /* Set mips_optimize around the lui instruction to avoid
4366 inserting an unnecessary nop after the lw. */
4367 hold_mips_optimize = mips_optimize;
4368 mips_optimize = 2;
4369 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
4370 mips_optimize = hold_mips_optimize;
4371
4372 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4373 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4374 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4375 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4376 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4377 "d,v,t", tempreg, tempreg, AT);
4378 (void) frag_var (rs_machine_dependent, 0, 0,
4379 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4380 offset_expr.X_add_symbol, (offsetT) 0,
4381 (char *) NULL);
4382 used_at = 1;
4383 }
4384 }
4385 else if (mips_pic == SVR4_PIC)
4386 {
4387 int gpdel;
9117d219
NC
4388 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4389 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
252b5132
RH
4390
4391 /* This is the large GOT case. If this is a reference to an
4392 external symbol, and there is no constant, we want
4393 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4394 addu $tempreg,$tempreg,$gp
4395 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9117d219
NC
4396 or if tempreg is PIC_CALL_REG
4397 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4398 addu $tempreg,$tempreg,$gp
4399 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
4400 For a local symbol, we want
4401 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4402 nop
4403 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4404
4405 If we have a small constant, and this is a reference to
4406 an external symbol, we want
4407 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4408 addu $tempreg,$tempreg,$gp
4409 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4410 nop
4411 addiu $tempreg,$tempreg,<constant>
4412 For a local symbol, we want
4413 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4414 nop
4415 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4416
4417 If we have a large constant, and this is a reference to
4418 an external symbol, we want
4419 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4420 addu $tempreg,$tempreg,$gp
4421 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4422 lui $at,<hiconstant>
4423 addiu $at,$at,<loconstant>
4424 addu $tempreg,$tempreg,$at
4425 For a local symbol, we want
4426 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4427 lui $at,<hiconstant>
4428 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4429 addu $tempreg,$tempreg,$at
4430 */
4431 expr1.X_add_number = offset_expr.X_add_number;
4432 offset_expr.X_add_number = 0;
4433 frag_grow (52);
4434 if (reg_needs_delay (GP))
4435 gpdel = 4;
4436 else
4437 gpdel = 0;
9117d219
NC
4438 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4439 {
4440 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4441 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4442 }
252b5132 4443 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 4444 tempreg, lui_reloc_type);
252b5132 4445 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4446 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4447 "d,v,t", tempreg, tempreg, GP);
4448 macro_build ((char *) NULL, &icnt, &offset_expr,
4449 dbl ? "ld" : "lw",
9117d219 4450 "t,o(b)", tempreg, lw_reloc_type, tempreg);
252b5132
RH
4451 if (expr1.X_add_number == 0)
4452 {
4453 int off;
4454
4455 if (breg == 0)
4456 off = 0;
4457 else
4458 {
4459 /* We're going to put in an addu instruction using
4460 tempreg, so we may as well insert the nop right
4461 now. */
4462 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4463 "nop", "");
4464 off = 4;
4465 }
4466
4467 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4468 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4469 8 + gpdel, 0,
4470 (breg == 0
4471 ? mips_opts.warn_about_macros
4472 : 0)),
4473 offset_expr.X_add_symbol, (offsetT) 0,
4474 (char *) NULL);
4475 }
4476 else if (expr1.X_add_number >= -0x8000
4477 && expr1.X_add_number < 0x8000)
4478 {
4479 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4480 "nop", "");
4481 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4482 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4483 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4484
4485 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4486 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4487 (breg == 0
4488 ? mips_opts.warn_about_macros
4489 : 0)),
4490 offset_expr.X_add_symbol, (offsetT) 0,
4491 (char *) NULL);
4492 }
4493 else
4494 {
4495 int adj, dreg;
4496
4497 /* If we are going to add in a base register, and the
4498 target register and the base register are the same,
4499 then we are using AT as a temporary register. Since
4500 we want to load the constant into AT, we add our
4501 current AT (from the global offset table) and the
4502 register into the register now, and pretend we were
4503 not using a base register. */
4504 if (breg != treg)
4505 {
4506 adj = 0;
4507 dreg = tempreg;
4508 }
4509 else
4510 {
4511 assert (tempreg == AT);
4512 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4513 "nop", "");
4514 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4515 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4516 "d,v,t", treg, AT, breg);
4517 dreg = treg;
4518 adj = 8;
4519 }
4520
4521 /* Set mips_optimize around the lui instruction to avoid
4522 inserting an unnecessary nop after the lw. */
4523 hold_mips_optimize = mips_optimize;
4524 mips_optimize = 2;
4525 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
4526 mips_optimize = hold_mips_optimize;
4527
4528 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4529 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4530 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4531 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4532 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4533 "d,v,t", dreg, dreg, AT);
4534
4535 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4536 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4537 8 + gpdel, 0,
4538 (breg == 0
4539 ? mips_opts.warn_about_macros
4540 : 0)),
4541 offset_expr.X_add_symbol, (offsetT) 0,
4542 (char *) NULL);
4543
4544 used_at = 1;
4545 }
4546
4547 if (gpdel > 0)
4548 {
4549 /* This is needed because this instruction uses $gp, but
4550 the first instruction on the main stream does not. */
4551 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4552 p += 4;
4553 }
4554 macro_build (p, &icnt, &offset_expr,
4555 dbl ? "ld" : "lw",
4556 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
4557 p += 4;
4558 if (expr1.X_add_number >= -0x8000
4559 && expr1.X_add_number < 0x8000)
4560 {
4561 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4562 p += 4;
4563 macro_build (p, &icnt, &expr1,
ca4e0257 4564 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4565 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4566 /* FIXME: If add_number is 0, and there was no base
4567 register, the external symbol case ended with a load,
4568 so if the symbol turns out to not be external, and
4569 the next instruction uses tempreg, an unnecessary nop
4570 will be inserted. */
4571 }
4572 else
4573 {
4574 if (breg == treg)
4575 {
4576 /* We must add in the base register now, as in the
4577 external symbol case. */
4578 assert (tempreg == AT);
4579 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4580 p += 4;
4581 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 4582 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4583 "d,v,t", treg, AT, breg);
4584 p += 4;
4585 tempreg = treg;
4586 /* We set breg to 0 because we have arranged to add
4587 it in in both cases. */
4588 breg = 0;
4589 }
4590
4591 macro_build_lui (p, &icnt, &expr1, AT);
4592 p += 4;
4593 macro_build (p, &icnt, &expr1,
ca4e0257 4594 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4595 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4596 p += 4;
4597 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 4598 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4599 "d,v,t", tempreg, tempreg, AT);
4600 p += 4;
4601 }
4602 }
4603 else if (mips_pic == EMBEDDED_PIC)
4604 {
4605 /* We use
4606 addiu $tempreg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
4607 */
4608 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 4609 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4610 "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
4611 }
4612 else
4613 abort ();
4614
4615 if (breg != 0)
4616 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4617 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4618 "d,v,t", treg, tempreg, breg);
4619
4620 if (! used_at)
4621 return;
4622
4623 break;
4624
4625 case M_J_A:
4626 /* The j instruction may not be used in PIC code, since it
4627 requires an absolute address. We convert it to a b
4628 instruction. */
4629 if (mips_pic == NO_PIC)
4630 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
4631 else
4632 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4633 return;
4634
4635 /* The jal instructions must be handled as macros because when
4636 generating PIC code they expand to multi-instruction
4637 sequences. Normally they are simple instructions. */
4638 case M_JAL_1:
4639 dreg = RA;
4640 /* Fall through. */
4641 case M_JAL_2:
4642 if (mips_pic == NO_PIC
4643 || mips_pic == EMBEDDED_PIC)
4644 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
4645 "d,s", dreg, sreg);
4646 else if (mips_pic == SVR4_PIC)
4647 {
4648 if (sreg != PIC_CALL_REG)
4649 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 4650
252b5132
RH
4651 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
4652 "d,s", dreg, sreg);
4653 if (mips_cprestore_offset < 0)
4654 as_warn (_("No .cprestore pseudo-op used in PIC code"));
4655 else
4656 {
4657 expr1.X_add_number = mips_cprestore_offset;
4658 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4659 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
4660 "t,o(b)", GP, (int) BFD_RELOC_LO16, mips_frame_reg);
4661 }
4662 }
4663 else
4664 abort ();
4665
4666 return;
4667
4668 case M_JAL_A:
4669 if (mips_pic == NO_PIC)
4670 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
4671 else if (mips_pic == SVR4_PIC)
4672 {
4673 /* If this is a reference to an external symbol, and we are
4674 using a small GOT, we want
4675 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4676 nop
4677 jalr $25
4678 nop
4679 lw $gp,cprestore($sp)
4680 The cprestore value is set using the .cprestore
4681 pseudo-op. If we are using a big GOT, we want
4682 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4683 addu $25,$25,$gp
4684 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
4685 nop
4686 jalr $25
4687 nop
4688 lw $gp,cprestore($sp)
4689 If the symbol is not external, we want
4690 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4691 nop
4692 addiu $25,$25,<sym> (BFD_RELOC_LO16)
4693 jalr $25
4694 nop
4695 lw $gp,cprestore($sp) */
4696 frag_grow (40);
4697 if (! mips_big_got)
4698 {
4699 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 4700 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
4701 "t,o(b)", PIC_CALL_REG,
4702 (int) BFD_RELOC_MIPS_CALL16, GP);
4703 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4704 "nop", "");
4705 p = frag_var (rs_machine_dependent, 4, 0,
4706 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
4707 offset_expr.X_add_symbol, (offsetT) 0,
4708 (char *) NULL);
4709 }
4710 else
4711 {
4712 int gpdel;
4713
4714 if (reg_needs_delay (GP))
4715 gpdel = 4;
4716 else
4717 gpdel = 0;
4718 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4719 PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
4720 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4721 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4722 "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
4723 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 4724 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
4725 "t,o(b)", PIC_CALL_REG,
4726 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
4727 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4728 "nop", "");
4729 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4730 RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
4731 0, 0),
4732 offset_expr.X_add_symbol, (offsetT) 0,
4733 (char *) NULL);
4734 if (gpdel > 0)
4735 {
4736 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4737 p += 4;
4738 }
4739 macro_build (p, &icnt, &offset_expr,
ca4e0257 4740 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
4741 "t,o(b)", PIC_CALL_REG,
4742 (int) BFD_RELOC_MIPS_GOT16, GP);
4743 p += 4;
4744 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4745 p += 4;
bdaaa2e1 4746 }
252b5132 4747 macro_build (p, &icnt, &offset_expr,
ca4e0257 4748 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4749 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
4750 (int) BFD_RELOC_LO16);
4751 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4752 "jalr", "s", PIC_CALL_REG);
4753 if (mips_cprestore_offset < 0)
4754 as_warn (_("No .cprestore pseudo-op used in PIC code"));
4755 else
4756 {
4757 if (mips_opts.noreorder)
4758 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4759 "nop", "");
4760 expr1.X_add_number = mips_cprestore_offset;
4761 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4762 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
4763 "t,o(b)", GP, (int) BFD_RELOC_LO16,
4764 mips_frame_reg);
4765 }
4766 }
4767 else if (mips_pic == EMBEDDED_PIC)
4768 {
4769 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
4770 /* The linker may expand the call to a longer sequence which
4771 uses $at, so we must break rather than return. */
4772 break;
4773 }
4774 else
4775 abort ();
4776
4777 return;
4778
4779 case M_LB_AB:
4780 s = "lb";
4781 goto ld;
4782 case M_LBU_AB:
4783 s = "lbu";
4784 goto ld;
4785 case M_LH_AB:
4786 s = "lh";
4787 goto ld;
4788 case M_LHU_AB:
4789 s = "lhu";
4790 goto ld;
4791 case M_LW_AB:
4792 s = "lw";
4793 goto ld;
4794 case M_LWC0_AB:
4795 s = "lwc0";
bdaaa2e1 4796 /* Itbl support may require additional care here. */
252b5132
RH
4797 coproc = 1;
4798 goto ld;
4799 case M_LWC1_AB:
4800 s = "lwc1";
bdaaa2e1 4801 /* Itbl support may require additional care here. */
252b5132
RH
4802 coproc = 1;
4803 goto ld;
4804 case M_LWC2_AB:
4805 s = "lwc2";
bdaaa2e1 4806 /* Itbl support may require additional care here. */
252b5132
RH
4807 coproc = 1;
4808 goto ld;
4809 case M_LWC3_AB:
4810 s = "lwc3";
bdaaa2e1 4811 /* Itbl support may require additional care here. */
252b5132
RH
4812 coproc = 1;
4813 goto ld;
4814 case M_LWL_AB:
4815 s = "lwl";
4816 lr = 1;
4817 goto ld;
4818 case M_LWR_AB:
4819 s = "lwr";
4820 lr = 1;
4821 goto ld;
4822 case M_LDC1_AB:
ec68c924 4823 if (mips_arch == CPU_R4650)
252b5132
RH
4824 {
4825 as_bad (_("opcode not supported on this processor"));
4826 return;
4827 }
4828 s = "ldc1";
bdaaa2e1 4829 /* Itbl support may require additional care here. */
252b5132
RH
4830 coproc = 1;
4831 goto ld;
4832 case M_LDC2_AB:
4833 s = "ldc2";
bdaaa2e1 4834 /* Itbl support may require additional care here. */
252b5132
RH
4835 coproc = 1;
4836 goto ld;
4837 case M_LDC3_AB:
4838 s = "ldc3";
bdaaa2e1 4839 /* Itbl support may require additional care here. */
252b5132
RH
4840 coproc = 1;
4841 goto ld;
4842 case M_LDL_AB:
4843 s = "ldl";
4844 lr = 1;
4845 goto ld;
4846 case M_LDR_AB:
4847 s = "ldr";
4848 lr = 1;
4849 goto ld;
4850 case M_LL_AB:
4851 s = "ll";
4852 goto ld;
4853 case M_LLD_AB:
4854 s = "lld";
4855 goto ld;
4856 case M_LWU_AB:
4857 s = "lwu";
4858 ld:
4859 if (breg == treg || coproc || lr)
4860 {
4861 tempreg = AT;
4862 used_at = 1;
4863 }
4864 else
4865 {
4866 tempreg = treg;
4867 used_at = 0;
4868 }
4869 goto ld_st;
4870 case M_SB_AB:
4871 s = "sb";
4872 goto st;
4873 case M_SH_AB:
4874 s = "sh";
4875 goto st;
4876 case M_SW_AB:
4877 s = "sw";
4878 goto st;
4879 case M_SWC0_AB:
4880 s = "swc0";
bdaaa2e1 4881 /* Itbl support may require additional care here. */
252b5132
RH
4882 coproc = 1;
4883 goto st;
4884 case M_SWC1_AB:
4885 s = "swc1";
bdaaa2e1 4886 /* Itbl support may require additional care here. */
252b5132
RH
4887 coproc = 1;
4888 goto st;
4889 case M_SWC2_AB:
4890 s = "swc2";
bdaaa2e1 4891 /* Itbl support may require additional care here. */
252b5132
RH
4892 coproc = 1;
4893 goto st;
4894 case M_SWC3_AB:
4895 s = "swc3";
bdaaa2e1 4896 /* Itbl support may require additional care here. */
252b5132
RH
4897 coproc = 1;
4898 goto st;
4899 case M_SWL_AB:
4900 s = "swl";
4901 goto st;
4902 case M_SWR_AB:
4903 s = "swr";
4904 goto st;
4905 case M_SC_AB:
4906 s = "sc";
4907 goto st;
4908 case M_SCD_AB:
4909 s = "scd";
4910 goto st;
4911 case M_SDC1_AB:
ec68c924 4912 if (mips_arch == CPU_R4650)
252b5132
RH
4913 {
4914 as_bad (_("opcode not supported on this processor"));
4915 return;
4916 }
4917 s = "sdc1";
4918 coproc = 1;
bdaaa2e1 4919 /* Itbl support may require additional care here. */
252b5132
RH
4920 goto st;
4921 case M_SDC2_AB:
4922 s = "sdc2";
bdaaa2e1 4923 /* Itbl support may require additional care here. */
252b5132
RH
4924 coproc = 1;
4925 goto st;
4926 case M_SDC3_AB:
4927 s = "sdc3";
bdaaa2e1 4928 /* Itbl support may require additional care here. */
252b5132
RH
4929 coproc = 1;
4930 goto st;
4931 case M_SDL_AB:
4932 s = "sdl";
4933 goto st;
4934 case M_SDR_AB:
4935 s = "sdr";
4936 st:
4937 tempreg = AT;
4938 used_at = 1;
4939 ld_st:
bdaaa2e1 4940 /* Itbl support may require additional care here. */
252b5132
RH
4941 if (mask == M_LWC1_AB
4942 || mask == M_SWC1_AB
4943 || mask == M_LDC1_AB
4944 || mask == M_SDC1_AB
4945 || mask == M_L_DAB
4946 || mask == M_S_DAB)
4947 fmt = "T,o(b)";
4948 else if (coproc)
4949 fmt = "E,o(b)";
4950 else
4951 fmt = "t,o(b)";
4952
4953 if (offset_expr.X_op != O_constant
4954 && offset_expr.X_op != O_symbol)
4955 {
4956 as_bad (_("expression too complex"));
4957 offset_expr.X_op = O_constant;
4958 }
4959
4960 /* A constant expression in PIC code can be handled just as it
4961 is in non PIC code. */
4962 if (mips_pic == NO_PIC
4963 || offset_expr.X_op == O_constant)
4964 {
4965 /* If this is a reference to a GP relative symbol, and there
4966 is no base register, we want
4967 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
4968 Otherwise, if there is no base register, we want
4969 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4970 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
4971 If we have a constant, we need two instructions anyhow,
4972 so we always use the latter form.
4973
4974 If we have a base register, and this is a reference to a
4975 GP relative symbol, we want
4976 addu $tempreg,$breg,$gp
4977 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GPREL)
4978 Otherwise we want
4979 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4980 addu $tempreg,$tempreg,$breg
4981 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
4982 With a constant we always use the latter case. */
4983 if (breg == 0)
4984 {
4985 if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
4986 || nopic_need_relax (offset_expr.X_add_symbol, 1))
4987 p = NULL;
4988 else
4989 {
4990 frag_grow (20);
4991 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4992 treg, (int) BFD_RELOC_MIPS_GPREL, GP);
4993 p = frag_var (rs_machine_dependent, 8, 0,
4994 RELAX_ENCODE (4, 8, 0, 4, 0,
4995 (mips_opts.warn_about_macros
4996 || (used_at
4997 && mips_opts.noat))),
4998 offset_expr.X_add_symbol, (offsetT) 0,
4999 (char *) NULL);
5000 used_at = 0;
5001 }
5002 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5003 if (p != NULL)
5004 p += 4;
5005 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5006 (int) BFD_RELOC_LO16, tempreg);
5007 }
5008 else
5009 {
5010 if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
5011 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5012 p = NULL;
5013 else
5014 {
5015 frag_grow (28);
5016 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5017 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5018 "d,v,t", tempreg, breg, GP);
5019 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5020 treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
5021 p = frag_var (rs_machine_dependent, 12, 0,
5022 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5023 offset_expr.X_add_symbol, (offsetT) 0,
5024 (char *) NULL);
5025 }
5026 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5027 if (p != NULL)
5028 p += 4;
5029 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5030 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5031 "d,v,t", tempreg, tempreg, breg);
5032 if (p != NULL)
5033 p += 4;
5034 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5035 (int) BFD_RELOC_LO16, tempreg);
5036 }
5037 }
5038 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5039 {
5040 /* If this is a reference to an external symbol, we want
5041 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5042 nop
5043 <op> $treg,0($tempreg)
5044 Otherwise we want
5045 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5046 nop
5047 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5048 <op> $treg,0($tempreg)
5049 If there is a base register, we add it to $tempreg before
5050 the <op>. If there is a constant, we stick it in the
5051 <op> instruction. We don't handle constants larger than
5052 16 bits, because we have no way to load the upper 16 bits
5053 (actually, we could handle them for the subset of cases
5054 in which we are not using $at). */
5055 assert (offset_expr.X_op == O_symbol);
5056 expr1.X_add_number = offset_expr.X_add_number;
5057 offset_expr.X_add_number = 0;
5058 if (expr1.X_add_number < -0x8000
5059 || expr1.X_add_number >= 0x8000)
5060 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5061 frag_grow (20);
5062 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5063 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5064 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5065 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
bdaaa2e1 5066 p = frag_var (rs_machine_dependent, 4, 0,
252b5132
RH
5067 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5068 offset_expr.X_add_symbol, (offsetT) 0,
5069 (char *) NULL);
5070 macro_build (p, &icnt, &offset_expr,
ca4e0257 5071 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5072 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5073 if (breg != 0)
5074 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5075 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5076 "d,v,t", tempreg, tempreg, breg);
5077 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5078 (int) BFD_RELOC_LO16, tempreg);
5079 }
5080 else if (mips_pic == SVR4_PIC)
5081 {
5082 int gpdel;
5083
5084 /* If this is a reference to an external symbol, we want
5085 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5086 addu $tempreg,$tempreg,$gp
5087 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5088 <op> $treg,0($tempreg)
5089 Otherwise we want
5090 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5091 nop
5092 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5093 <op> $treg,0($tempreg)
5094 If there is a base register, we add it to $tempreg before
5095 the <op>. If there is a constant, we stick it in the
5096 <op> instruction. We don't handle constants larger than
5097 16 bits, because we have no way to load the upper 16 bits
5098 (actually, we could handle them for the subset of cases
5099 in which we are not using $at). */
5100 assert (offset_expr.X_op == O_symbol);
5101 expr1.X_add_number = offset_expr.X_add_number;
5102 offset_expr.X_add_number = 0;
5103 if (expr1.X_add_number < -0x8000
5104 || expr1.X_add_number >= 0x8000)
5105 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5106 if (reg_needs_delay (GP))
5107 gpdel = 4;
5108 else
5109 gpdel = 0;
5110 frag_grow (36);
5111 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5112 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5113 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5114 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5115 "d,v,t", tempreg, tempreg, GP);
5116 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5117 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5118 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5119 tempreg);
5120 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5121 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5122 offset_expr.X_add_symbol, (offsetT) 0, (char *) NULL);
5123 if (gpdel > 0)
5124 {
5125 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5126 p += 4;
5127 }
5128 macro_build (p, &icnt, &offset_expr,
ca4e0257 5129 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5130 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5131 p += 4;
5132 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5133 p += 4;
5134 macro_build (p, &icnt, &offset_expr,
ca4e0257 5135 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5136 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5137 if (breg != 0)
5138 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5139 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5140 "d,v,t", tempreg, tempreg, breg);
5141 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5142 (int) BFD_RELOC_LO16, tempreg);
5143 }
5144 else if (mips_pic == EMBEDDED_PIC)
5145 {
5146 /* If there is no base register, we want
5147 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5148 If there is a base register, we want
5149 addu $tempreg,$breg,$gp
5150 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GPREL)
5151 */
5152 assert (offset_expr.X_op == O_symbol);
5153 if (breg == 0)
5154 {
5155 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5156 treg, (int) BFD_RELOC_MIPS_GPREL, GP);
5157 used_at = 0;
5158 }
5159 else
5160 {
5161 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5162 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5163 "d,v,t", tempreg, breg, GP);
5164 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5165 treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
5166 }
5167 }
5168 else
5169 abort ();
5170
5171 if (! used_at)
5172 return;
5173
5174 break;
5175
5176 case M_LI:
5177 case M_LI_S:
5178 load_register (&icnt, treg, &imm_expr, 0);
5179 return;
5180
5181 case M_DLI:
5182 load_register (&icnt, treg, &imm_expr, 1);
5183 return;
5184
5185 case M_LI_SS:
5186 if (imm_expr.X_op == O_constant)
5187 {
5188 load_register (&icnt, AT, &imm_expr, 0);
5189 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5190 "mtc1", "t,G", AT, treg);
5191 break;
5192 }
5193 else
5194 {
5195 assert (offset_expr.X_op == O_symbol
5196 && strcmp (segment_name (S_GET_SEGMENT
5197 (offset_expr.X_add_symbol)),
5198 ".lit4") == 0
5199 && offset_expr.X_add_number == 0);
5200 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5201 treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5202 return;
5203 }
5204
5205 case M_LI_D:
ca4e0257
RS
5206 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5207 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5208 order 32 bits of the value and the low order 32 bits are either
5209 zero or in OFFSET_EXPR. */
252b5132
RH
5210 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5211 {
ca4e0257 5212 if (HAVE_64BIT_GPRS)
252b5132
RH
5213 load_register (&icnt, treg, &imm_expr, 1);
5214 else
5215 {
5216 int hreg, lreg;
5217
5218 if (target_big_endian)
5219 {
5220 hreg = treg;
5221 lreg = treg + 1;
5222 }
5223 else
5224 {
5225 hreg = treg + 1;
5226 lreg = treg;
5227 }
5228
5229 if (hreg <= 31)
5230 load_register (&icnt, hreg, &imm_expr, 0);
5231 if (lreg <= 31)
5232 {
5233 if (offset_expr.X_op == O_absent)
ea1fb5dc 5234 move_register (&icnt, lreg, 0);
252b5132
RH
5235 else
5236 {
5237 assert (offset_expr.X_op == O_constant);
5238 load_register (&icnt, lreg, &offset_expr, 0);
5239 }
5240 }
5241 }
5242 return;
5243 }
5244
5245 /* We know that sym is in the .rdata section. First we get the
5246 upper 16 bits of the address. */
5247 if (mips_pic == NO_PIC)
5248 {
5249 /* FIXME: This won't work for a 64 bit address. */
5250 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
5251 }
5252 else if (mips_pic == SVR4_PIC)
5253 {
5254 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5255 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5256 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5257 }
5258 else if (mips_pic == EMBEDDED_PIC)
5259 {
5260 /* For embedded PIC we pick up the entire address off $gp in
5261 a single instruction. */
5262 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5263 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5264 "t,r,j", AT, GP, (int) BFD_RELOC_MIPS_GPREL);
5265 offset_expr.X_op = O_constant;
5266 offset_expr.X_add_number = 0;
5267 }
5268 else
5269 abort ();
bdaaa2e1 5270
252b5132 5271 /* Now we load the register(s). */
ca4e0257 5272 if (HAVE_64BIT_GPRS)
252b5132
RH
5273 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5274 treg, (int) BFD_RELOC_LO16, AT);
5275 else
5276 {
5277 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5278 treg, (int) BFD_RELOC_LO16, AT);
5279 if (treg != 31)
5280 {
5281 /* FIXME: How in the world do we deal with the possible
5282 overflow here? */
5283 offset_expr.X_add_number += 4;
5284 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5285 treg + 1, (int) BFD_RELOC_LO16, AT);
5286 }
5287 }
5288
5289 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5290 does not become a variant frag. */
5291 frag_wane (frag_now);
5292 frag_new (0);
5293
5294 break;
5295
5296 case M_LI_DD:
ca4e0257
RS
5297 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5298 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5299 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5300 the value and the low order 32 bits are either zero or in
5301 OFFSET_EXPR. */
252b5132
RH
5302 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5303 {
ca4e0257
RS
5304 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5305 if (HAVE_64BIT_FPRS)
5306 {
5307 assert (HAVE_64BIT_GPRS);
5308 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5309 "dmtc1", "t,S", AT, treg);
5310 }
252b5132
RH
5311 else
5312 {
5313 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5314 "mtc1", "t,G", AT, treg + 1);
5315 if (offset_expr.X_op == O_absent)
5316 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5317 "mtc1", "t,G", 0, treg);
5318 else
5319 {
5320 assert (offset_expr.X_op == O_constant);
5321 load_register (&icnt, AT, &offset_expr, 0);
5322 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5323 "mtc1", "t,G", AT, treg);
5324 }
5325 }
5326 break;
5327 }
5328
5329 assert (offset_expr.X_op == O_symbol
5330 && offset_expr.X_add_number == 0);
5331 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5332 if (strcmp (s, ".lit8") == 0)
5333 {
e7af610e 5334 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5335 {
5336 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5337 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5338 return;
5339 }
5340 breg = GP;
5341 r = BFD_RELOC_MIPS_LITERAL;
5342 goto dob;
5343 }
5344 else
5345 {
5346 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5347 if (mips_pic == SVR4_PIC)
5348 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5349 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5350 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5351 else
5352 {
5353 /* FIXME: This won't work for a 64 bit address. */
5354 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
5355 }
bdaaa2e1 5356
e7af610e 5357 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5358 {
5359 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5360 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5361
5362 /* To avoid confusion in tc_gen_reloc, we must ensure
5363 that this does not become a variant frag. */
5364 frag_wane (frag_now);
5365 frag_new (0);
5366
5367 break;
5368 }
5369 breg = AT;
5370 r = BFD_RELOC_LO16;
5371 goto dob;
5372 }
5373
5374 case M_L_DOB:
ec68c924 5375 if (mips_arch == CPU_R4650)
252b5132
RH
5376 {
5377 as_bad (_("opcode not supported on this processor"));
5378 return;
5379 }
5380 /* Even on a big endian machine $fn comes before $fn+1. We have
5381 to adjust when loading from memory. */
5382 r = BFD_RELOC_LO16;
5383 dob:
e7af610e 5384 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
5385 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5386 target_big_endian ? treg + 1 : treg,
5387 (int) r, breg);
5388 /* FIXME: A possible overflow which I don't know how to deal
5389 with. */
5390 offset_expr.X_add_number += 4;
5391 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5392 target_big_endian ? treg : treg + 1,
5393 (int) r, breg);
5394
5395 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5396 does not become a variant frag. */
5397 frag_wane (frag_now);
5398 frag_new (0);
5399
5400 if (breg != AT)
5401 return;
5402 break;
5403
5404 case M_L_DAB:
5405 /*
5406 * The MIPS assembler seems to check for X_add_number not
5407 * being double aligned and generating:
5408 * lui at,%hi(foo+1)
5409 * addu at,at,v1
5410 * addiu at,at,%lo(foo+1)
5411 * lwc1 f2,0(at)
5412 * lwc1 f3,4(at)
5413 * But, the resulting address is the same after relocation so why
5414 * generate the extra instruction?
5415 */
ec68c924 5416 if (mips_arch == CPU_R4650)
252b5132
RH
5417 {
5418 as_bad (_("opcode not supported on this processor"));
5419 return;
5420 }
bdaaa2e1 5421 /* Itbl support may require additional care here. */
252b5132 5422 coproc = 1;
e7af610e 5423 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5424 {
5425 s = "ldc1";
5426 goto ld;
5427 }
5428
5429 s = "lwc1";
5430 fmt = "T,o(b)";
5431 goto ldd_std;
5432
5433 case M_S_DAB:
ec68c924 5434 if (mips_arch == CPU_R4650)
252b5132
RH
5435 {
5436 as_bad (_("opcode not supported on this processor"));
5437 return;
5438 }
5439
e7af610e 5440 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5441 {
5442 s = "sdc1";
5443 goto st;
5444 }
5445
5446 s = "swc1";
5447 fmt = "T,o(b)";
bdaaa2e1 5448 /* Itbl support may require additional care here. */
252b5132
RH
5449 coproc = 1;
5450 goto ldd_std;
5451
5452 case M_LD_AB:
ca4e0257 5453 if (HAVE_64BIT_GPRS)
252b5132
RH
5454 {
5455 s = "ld";
5456 goto ld;
5457 }
5458
5459 s = "lw";
5460 fmt = "t,o(b)";
5461 goto ldd_std;
5462
5463 case M_SD_AB:
ca4e0257 5464 if (HAVE_64BIT_GPRS)
252b5132
RH
5465 {
5466 s = "sd";
5467 goto st;
5468 }
5469
5470 s = "sw";
5471 fmt = "t,o(b)";
5472
5473 ldd_std:
5474 if (offset_expr.X_op != O_symbol
5475 && offset_expr.X_op != O_constant)
5476 {
5477 as_bad (_("expression too complex"));
5478 offset_expr.X_op = O_constant;
5479 }
5480
5481 /* Even on a big endian machine $fn comes before $fn+1. We have
5482 to adjust when loading from memory. We set coproc if we must
5483 load $fn+1 first. */
bdaaa2e1 5484 /* Itbl support may require additional care here. */
252b5132
RH
5485 if (! target_big_endian)
5486 coproc = 0;
5487
5488 if (mips_pic == NO_PIC
5489 || offset_expr.X_op == O_constant)
5490 {
5491 /* If this is a reference to a GP relative symbol, we want
5492 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5493 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
5494 If we have a base register, we use this
5495 addu $at,$breg,$gp
5496 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
5497 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
5498 If this is not a GP relative symbol, we want
5499 lui $at,<sym> (BFD_RELOC_HI16_S)
5500 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5501 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5502 If there is a base register, we add it to $at after the
5503 lui instruction. If there is a constant, we always use
5504 the last case. */
5505 if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
5506 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5507 {
5508 p = NULL;
5509 used_at = 1;
5510 }
5511 else
5512 {
5513 int off;
5514
5515 if (breg == 0)
5516 {
5517 frag_grow (28);
5518 tempreg = GP;
5519 off = 0;
5520 used_at = 0;
5521 }
5522 else
5523 {
5524 frag_grow (36);
5525 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5526 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5527 "d,v,t", AT, breg, GP);
5528 tempreg = AT;
5529 off = 4;
5530 used_at = 1;
5531 }
5532
beae10d5 5533 /* Itbl support may require additional care here. */
252b5132
RH
5534 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5535 coproc ? treg + 1 : treg,
5536 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5537 offset_expr.X_add_number += 4;
5538
5539 /* Set mips_optimize to 2 to avoid inserting an
5540 undesired nop. */
5541 hold_mips_optimize = mips_optimize;
5542 mips_optimize = 2;
beae10d5 5543 /* Itbl support may require additional care here. */
252b5132
RH
5544 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5545 coproc ? treg : treg + 1,
5546 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5547 mips_optimize = hold_mips_optimize;
5548
5549 p = frag_var (rs_machine_dependent, 12 + off, 0,
5550 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
5551 used_at && mips_opts.noat),
5552 offset_expr.X_add_symbol, (offsetT) 0,
5553 (char *) NULL);
5554
5555 /* We just generated two relocs. When tc_gen_reloc
5556 handles this case, it will skip the first reloc and
5557 handle the second. The second reloc already has an
5558 extra addend of 4, which we added above. We must
5559 subtract it out, and then subtract another 4 to make
5560 the first reloc come out right. The second reloc
5561 will come out right because we are going to add 4 to
5562 offset_expr when we build its instruction below.
5563
5564 If we have a symbol, then we don't want to include
5565 the offset, because it will wind up being included
5566 when we generate the reloc. */
5567
5568 if (offset_expr.X_op == O_constant)
5569 offset_expr.X_add_number -= 8;
5570 else
5571 {
5572 offset_expr.X_add_number = -4;
5573 offset_expr.X_op = O_constant;
5574 }
5575 }
5576 macro_build_lui (p, &icnt, &offset_expr, AT);
5577 if (p != NULL)
5578 p += 4;
5579 if (breg != 0)
5580 {
5581 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5582 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5583 "d,v,t", AT, breg, AT);
5584 if (p != NULL)
5585 p += 4;
5586 }
beae10d5 5587 /* Itbl support may require additional care here. */
252b5132
RH
5588 macro_build (p, &icnt, &offset_expr, s, fmt,
5589 coproc ? treg + 1 : treg,
5590 (int) BFD_RELOC_LO16, AT);
5591 if (p != NULL)
5592 p += 4;
5593 /* FIXME: How do we handle overflow here? */
5594 offset_expr.X_add_number += 4;
beae10d5 5595 /* Itbl support may require additional care here. */
252b5132
RH
5596 macro_build (p, &icnt, &offset_expr, s, fmt,
5597 coproc ? treg : treg + 1,
5598 (int) BFD_RELOC_LO16, AT);
bdaaa2e1 5599 }
252b5132
RH
5600 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5601 {
5602 int off;
5603
5604 /* If this is a reference to an external symbol, we want
5605 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5606 nop
5607 <op> $treg,0($at)
5608 <op> $treg+1,4($at)
5609 Otherwise we want
5610 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5611 nop
5612 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5613 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5614 If there is a base register we add it to $at before the
5615 lwc1 instructions. If there is a constant we include it
5616 in the lwc1 instructions. */
5617 used_at = 1;
5618 expr1.X_add_number = offset_expr.X_add_number;
5619 offset_expr.X_add_number = 0;
5620 if (expr1.X_add_number < -0x8000
5621 || expr1.X_add_number >= 0x8000 - 4)
5622 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5623 if (breg == 0)
5624 off = 0;
5625 else
5626 off = 4;
5627 frag_grow (24 + off);
5628 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5629 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5630 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5631 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5632 if (breg != 0)
5633 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5634 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 5635 "d,v,t", AT, breg, AT);
beae10d5 5636 /* Itbl support may require additional care here. */
252b5132
RH
5637 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5638 coproc ? treg + 1 : treg,
5639 (int) BFD_RELOC_LO16, AT);
5640 expr1.X_add_number += 4;
5641
5642 /* Set mips_optimize to 2 to avoid inserting an undesired
5643 nop. */
5644 hold_mips_optimize = mips_optimize;
5645 mips_optimize = 2;
beae10d5 5646 /* Itbl support may require additional care here. */
252b5132
RH
5647 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5648 coproc ? treg : treg + 1,
5649 (int) BFD_RELOC_LO16, AT);
5650 mips_optimize = hold_mips_optimize;
5651
5652 (void) frag_var (rs_machine_dependent, 0, 0,
5653 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
5654 offset_expr.X_add_symbol, (offsetT) 0,
5655 (char *) NULL);
5656 }
5657 else if (mips_pic == SVR4_PIC)
5658 {
5659 int gpdel, off;
5660
5661 /* If this is a reference to an external symbol, we want
5662 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5663 addu $at,$at,$gp
5664 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
5665 nop
5666 <op> $treg,0($at)
5667 <op> $treg+1,4($at)
5668 Otherwise we want
5669 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5670 nop
5671 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5672 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5673 If there is a base register we add it to $at before the
5674 lwc1 instructions. If there is a constant we include it
5675 in the lwc1 instructions. */
5676 used_at = 1;
5677 expr1.X_add_number = offset_expr.X_add_number;
5678 offset_expr.X_add_number = 0;
5679 if (expr1.X_add_number < -0x8000
5680 || expr1.X_add_number >= 0x8000 - 4)
5681 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5682 if (reg_needs_delay (GP))
5683 gpdel = 4;
5684 else
5685 gpdel = 0;
5686 if (breg == 0)
5687 off = 0;
5688 else
5689 off = 4;
5690 frag_grow (56);
5691 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5692 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
5693 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5694 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5695 "d,v,t", AT, AT, GP);
5696 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5697 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5698 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
5699 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5700 if (breg != 0)
5701 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5702 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 5703 "d,v,t", AT, breg, AT);
beae10d5 5704 /* Itbl support may require additional care here. */
252b5132
RH
5705 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5706 coproc ? treg + 1 : treg,
5707 (int) BFD_RELOC_LO16, AT);
5708 expr1.X_add_number += 4;
5709
5710 /* Set mips_optimize to 2 to avoid inserting an undesired
5711 nop. */
5712 hold_mips_optimize = mips_optimize;
5713 mips_optimize = 2;
beae10d5 5714 /* Itbl support may require additional care here. */
252b5132
RH
5715 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5716 coproc ? treg : treg + 1,
5717 (int) BFD_RELOC_LO16, AT);
5718 mips_optimize = hold_mips_optimize;
5719 expr1.X_add_number -= 4;
5720
5721 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
5722 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
5723 8 + gpdel + off, 1, 0),
5724 offset_expr.X_add_symbol, (offsetT) 0,
5725 (char *) NULL);
5726 if (gpdel > 0)
5727 {
5728 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5729 p += 4;
5730 }
5731 macro_build (p, &icnt, &offset_expr,
ca4e0257 5732 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5733 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5734 p += 4;
5735 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5736 p += 4;
5737 if (breg != 0)
5738 {
5739 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5740 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5741 "d,v,t", AT, breg, AT);
5742 p += 4;
5743 }
beae10d5 5744 /* Itbl support may require additional care here. */
252b5132
RH
5745 macro_build (p, &icnt, &expr1, s, fmt,
5746 coproc ? treg + 1 : treg,
5747 (int) BFD_RELOC_LO16, AT);
5748 p += 4;
5749 expr1.X_add_number += 4;
5750
5751 /* Set mips_optimize to 2 to avoid inserting an undesired
5752 nop. */
5753 hold_mips_optimize = mips_optimize;
5754 mips_optimize = 2;
beae10d5 5755 /* Itbl support may require additional care here. */
252b5132
RH
5756 macro_build (p, &icnt, &expr1, s, fmt,
5757 coproc ? treg : treg + 1,
5758 (int) BFD_RELOC_LO16, AT);
5759 mips_optimize = hold_mips_optimize;
5760 }
5761 else if (mips_pic == EMBEDDED_PIC)
5762 {
5763 /* If there is no base register, we use
5764 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5765 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
5766 If we have a base register, we use
5767 addu $at,$breg,$gp
5768 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
5769 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
5770 */
5771 if (breg == 0)
5772 {
5773 tempreg = GP;
5774 used_at = 0;
5775 }
5776 else
5777 {
5778 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5779 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5780 "d,v,t", AT, breg, GP);
5781 tempreg = AT;
5782 used_at = 1;
5783 }
5784
beae10d5 5785 /* Itbl support may require additional care here. */
252b5132
RH
5786 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5787 coproc ? treg + 1 : treg,
5788 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5789 offset_expr.X_add_number += 4;
beae10d5 5790 /* Itbl support may require additional care here. */
252b5132
RH
5791 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5792 coproc ? treg : treg + 1,
5793 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5794 }
5795 else
5796 abort ();
5797
5798 if (! used_at)
5799 return;
5800
5801 break;
5802
5803 case M_LD_OB:
5804 s = "lw";
5805 goto sd_ob;
5806 case M_SD_OB:
5807 s = "sw";
5808 sd_ob:
ca4e0257 5809 assert (HAVE_32BIT_ADDRESSES);
252b5132
RH
5810 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
5811 (int) BFD_RELOC_LO16, breg);
5812 offset_expr.X_add_number += 4;
5813 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
5814 (int) BFD_RELOC_LO16, breg);
5815 return;
5816
5817 /* New code added to support COPZ instructions.
5818 This code builds table entries out of the macros in mip_opcodes.
5819 R4000 uses interlocks to handle coproc delays.
5820 Other chips (like the R3000) require nops to be inserted for delays.
5821
f72c8c98 5822 FIXME: Currently, we require that the user handle delays.
252b5132
RH
5823 In order to fill delay slots for non-interlocked chips,
5824 we must have a way to specify delays based on the coprocessor.
5825 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
5826 What are the side-effects of the cop instruction?
5827 What cache support might we have and what are its effects?
5828 Both coprocessor & memory require delays. how long???
bdaaa2e1 5829 What registers are read/set/modified?
252b5132
RH
5830
5831 If an itbl is provided to interpret cop instructions,
bdaaa2e1 5832 this knowledge can be encoded in the itbl spec. */
252b5132
RH
5833
5834 case M_COP0:
5835 s = "c0";
5836 goto copz;
5837 case M_COP1:
5838 s = "c1";
5839 goto copz;
5840 case M_COP2:
5841 s = "c2";
5842 goto copz;
5843 case M_COP3:
5844 s = "c3";
5845 copz:
5846 /* For now we just do C (same as Cz). The parameter will be
5847 stored in insn_opcode by mips_ip. */
5848 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
5849 ip->insn_opcode);
5850 return;
5851
ea1fb5dc
RS
5852 case M_MOVE:
5853 move_register (&icnt, dreg, sreg);
5854 return;
5855
252b5132
RH
5856#ifdef LOSING_COMPILER
5857 default:
5858 /* Try and see if this is a new itbl instruction.
5859 This code builds table entries out of the macros in mip_opcodes.
5860 FIXME: For now we just assemble the expression and pass it's
5861 value along as a 32-bit immediate.
bdaaa2e1 5862 We may want to have the assembler assemble this value,
252b5132
RH
5863 so that we gain the assembler's knowledge of delay slots,
5864 symbols, etc.
5865 Would it be more efficient to use mask (id) here? */
bdaaa2e1 5866 if (itbl_have_entries
252b5132 5867 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 5868 {
252b5132
RH
5869 s = ip->insn_mo->name;
5870 s2 = "cop3";
5871 coproc = ITBL_DECODE_PNUM (immed_expr);;
5872 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
5873 return;
beae10d5 5874 }
252b5132
RH
5875 macro2 (ip);
5876 return;
5877 }
5878 if (mips_opts.noat)
5879 as_warn (_("Macro used $at after \".set noat\""));
5880}
bdaaa2e1 5881
252b5132
RH
5882static void
5883macro2 (ip)
5884 struct mips_cl_insn *ip;
5885{
5886 register int treg, sreg, dreg, breg;
5887 int tempreg;
5888 int mask;
5889 int icnt = 0;
5890 int used_at;
5891 expressionS expr1;
5892 const char *s;
5893 const char *s2;
5894 const char *fmt;
5895 int likely = 0;
5896 int dbl = 0;
5897 int coproc = 0;
5898 int lr = 0;
5899 int imm = 0;
5900 int off;
5901 offsetT maxnum;
5902 bfd_reloc_code_real_type r;
5903 char *p;
bdaaa2e1 5904
252b5132
RH
5905 treg = (ip->insn_opcode >> 16) & 0x1f;
5906 dreg = (ip->insn_opcode >> 11) & 0x1f;
5907 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
5908 mask = ip->insn_mo->mask;
bdaaa2e1 5909
252b5132
RH
5910 expr1.X_op = O_constant;
5911 expr1.X_op_symbol = NULL;
5912 expr1.X_add_symbol = NULL;
5913 expr1.X_add_number = 1;
bdaaa2e1 5914
252b5132
RH
5915 switch (mask)
5916 {
5917#endif /* LOSING_COMPILER */
5918
5919 case M_DMUL:
5920 dbl = 1;
5921 case M_MUL:
5922 macro_build ((char *) NULL, &icnt, NULL,
5923 dbl ? "dmultu" : "multu",
5924 "s,t", sreg, treg);
5925 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5926 return;
5927
5928 case M_DMUL_I:
5929 dbl = 1;
5930 case M_MUL_I:
5931 /* The MIPS assembler some times generates shifts and adds. I'm
5932 not trying to be that fancy. GCC should do this for us
5933 anyway. */
5934 load_register (&icnt, AT, &imm_expr, dbl);
5935 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 5936 dbl ? "dmult" : "mult", "s,t", sreg, AT);
252b5132
RH
5937 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5938 break;
5939
5940 case M_DMULO_I:
5941 dbl = 1;
5942 case M_MULO_I:
5943 imm = 1;
5944 goto do_mulo;
5945
5946 case M_DMULO:
5947 dbl = 1;
5948 case M_MULO:
5949 do_mulo:
5950 mips_emit_delays (true);
5951 ++mips_opts.noreorder;
5952 mips_any_noreorder = 1;
5953 if (imm)
5954 load_register (&icnt, AT, &imm_expr, dbl);
5955 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 5956 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
252b5132
RH
5957 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5958 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 5959 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, 31);
252b5132
RH
5960 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
5961 if (mips_trap)
5962 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", dreg, AT);
5963 else
5964 {
5965 expr1.X_add_number = 8;
5966 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
5967 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
beae10d5 5968 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
252b5132
RH
5969 }
5970 --mips_opts.noreorder;
5971 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5972 break;
5973
5974 case M_DMULOU_I:
5975 dbl = 1;
5976 case M_MULOU_I:
5977 imm = 1;
5978 goto do_mulou;
5979
5980 case M_DMULOU:
5981 dbl = 1;
5982 case M_MULOU:
5983 do_mulou:
5984 mips_emit_delays (true);
5985 ++mips_opts.noreorder;
5986 mips_any_noreorder = 1;
5987 if (imm)
5988 load_register (&icnt, AT, &imm_expr, dbl);
5989 macro_build ((char *) NULL, &icnt, NULL,
5990 dbl ? "dmultu" : "multu",
5991 "s,t", sreg, imm ? AT : treg);
5992 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
5993 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5994 if (mips_trap)
5995 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", AT, 0);
5996 else
5997 {
5998 expr1.X_add_number = 8;
5999 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6000 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
beae10d5 6001 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
252b5132
RH
6002 }
6003 --mips_opts.noreorder;
6004 break;
6005
6006 case M_ROL:
6007 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
6008 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
6009 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg,
6010 treg);
6011 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6012 break;
6013
6014 case M_ROL_I:
6015 if (imm_expr.X_op != O_constant)
6016 as_bad (_("rotate count too large"));
6017 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg,
6018 (int) (imm_expr.X_add_number & 0x1f));
6019 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
6020 (int) ((0 - imm_expr.X_add_number) & 0x1f));
6021 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6022 break;
6023
6024 case M_ROR:
6025 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
6026 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
6027 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg,
6028 treg);
6029 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6030 break;
6031
6032 case M_ROR_I:
6033 if (imm_expr.X_op != O_constant)
6034 as_bad (_("rotate count too large"));
6035 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg,
6036 (int) (imm_expr.X_add_number & 0x1f));
6037 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
6038 (int) ((0 - imm_expr.X_add_number) & 0x1f));
6039 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6040 break;
6041
6042 case M_S_DOB:
ec68c924 6043 if (mips_arch == CPU_R4650)
252b5132
RH
6044 {
6045 as_bad (_("opcode not supported on this processor"));
6046 return;
6047 }
e7af610e 6048 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6049 /* Even on a big endian machine $fn comes before $fn+1. We have
6050 to adjust when storing to memory. */
6051 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6052 target_big_endian ? treg + 1 : treg,
6053 (int) BFD_RELOC_LO16, breg);
6054 offset_expr.X_add_number += 4;
6055 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6056 target_big_endian ? treg : treg + 1,
6057 (int) BFD_RELOC_LO16, breg);
6058 return;
6059
6060 case M_SEQ:
6061 if (sreg == 0)
6062 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6063 treg, (int) BFD_RELOC_LO16);
6064 else if (treg == 0)
6065 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6066 sreg, (int) BFD_RELOC_LO16);
6067 else
6068 {
6069 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6070 sreg, treg);
6071 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6072 dreg, (int) BFD_RELOC_LO16);
6073 }
6074 return;
6075
6076 case M_SEQ_I:
6077 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6078 {
6079 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6080 sreg, (int) BFD_RELOC_LO16);
6081 return;
6082 }
6083 if (sreg == 0)
6084 {
6085 as_warn (_("Instruction %s: result is always false"),
6086 ip->insn_mo->name);
ea1fb5dc 6087 move_register (&icnt, dreg, 0);
252b5132
RH
6088 return;
6089 }
6090 if (imm_expr.X_op == O_constant
6091 && imm_expr.X_add_number >= 0
6092 && imm_expr.X_add_number < 0x10000)
6093 {
6094 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6095 sreg, (int) BFD_RELOC_LO16);
6096 used_at = 0;
6097 }
6098 else if (imm_expr.X_op == O_constant
6099 && imm_expr.X_add_number > -0x8000
6100 && imm_expr.X_add_number < 0)
6101 {
6102 imm_expr.X_add_number = -imm_expr.X_add_number;
6103 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6104 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6105 "t,r,j", dreg, sreg,
6106 (int) BFD_RELOC_LO16);
6107 used_at = 0;
6108 }
6109 else
6110 {
6111 load_register (&icnt, AT, &imm_expr, 0);
6112 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6113 sreg, AT);
6114 used_at = 1;
6115 }
6116 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6117 (int) BFD_RELOC_LO16);
6118 if (used_at)
6119 break;
6120 return;
6121
6122 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6123 s = "slt";
6124 goto sge;
6125 case M_SGEU:
6126 s = "sltu";
6127 sge:
6128 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
6129 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6130 (int) BFD_RELOC_LO16);
6131 return;
6132
6133 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6134 case M_SGEU_I:
6135 if (imm_expr.X_op == O_constant
6136 && imm_expr.X_add_number >= -0x8000
6137 && imm_expr.X_add_number < 0x8000)
6138 {
6139 macro_build ((char *) NULL, &icnt, &imm_expr,
6140 mask == M_SGE_I ? "slti" : "sltiu",
6141 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6142 used_at = 0;
6143 }
6144 else
6145 {
6146 load_register (&icnt, AT, &imm_expr, 0);
6147 macro_build ((char *) NULL, &icnt, NULL,
6148 mask == M_SGE_I ? "slt" : "sltu",
6149 "d,v,t", dreg, sreg, AT);
6150 used_at = 1;
6151 }
6152 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6153 (int) BFD_RELOC_LO16);
6154 if (used_at)
6155 break;
6156 return;
6157
6158 case M_SGT: /* sreg > treg <==> treg < sreg */
6159 s = "slt";
6160 goto sgt;
6161 case M_SGTU:
6162 s = "sltu";
6163 sgt:
6164 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
6165 return;
6166
6167 case M_SGT_I: /* sreg > I <==> I < sreg */
6168 s = "slt";
6169 goto sgti;
6170 case M_SGTU_I:
6171 s = "sltu";
6172 sgti:
6173 load_register (&icnt, AT, &imm_expr, 0);
6174 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
6175 break;
6176
6177 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
6178 s = "slt";
6179 goto sle;
6180 case M_SLEU:
6181 s = "sltu";
6182 sle:
6183 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
6184 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6185 (int) BFD_RELOC_LO16);
6186 return;
6187
6188 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6189 s = "slt";
6190 goto slei;
6191 case M_SLEU_I:
6192 s = "sltu";
6193 slei:
6194 load_register (&icnt, AT, &imm_expr, 0);
6195 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
6196 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6197 (int) BFD_RELOC_LO16);
6198 break;
6199
6200 case M_SLT_I:
6201 if (imm_expr.X_op == O_constant
6202 && imm_expr.X_add_number >= -0x8000
6203 && imm_expr.X_add_number < 0x8000)
6204 {
6205 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6206 dreg, sreg, (int) BFD_RELOC_LO16);
6207 return;
6208 }
6209 load_register (&icnt, AT, &imm_expr, 0);
6210 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
6211 break;
6212
6213 case M_SLTU_I:
6214 if (imm_expr.X_op == O_constant
6215 && imm_expr.X_add_number >= -0x8000
6216 && imm_expr.X_add_number < 0x8000)
6217 {
6218 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6219 dreg, sreg, (int) BFD_RELOC_LO16);
6220 return;
6221 }
6222 load_register (&icnt, AT, &imm_expr, 0);
6223 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg,
6224 AT);
6225 break;
6226
6227 case M_SNE:
6228 if (sreg == 0)
6229 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6230 treg);
6231 else if (treg == 0)
6232 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6233 sreg);
6234 else
6235 {
6236 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6237 sreg, treg);
6238 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6239 dreg);
6240 }
6241 return;
6242
6243 case M_SNE_I:
6244 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6245 {
6246 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6247 sreg);
6248 return;
6249 }
6250 if (sreg == 0)
6251 {
6252 as_warn (_("Instruction %s: result is always true"),
6253 ip->insn_mo->name);
6254 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 6255 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6256 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6257 return;
6258 }
6259 if (imm_expr.X_op == O_constant
6260 && imm_expr.X_add_number >= 0
6261 && imm_expr.X_add_number < 0x10000)
6262 {
6263 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6264 dreg, sreg, (int) BFD_RELOC_LO16);
6265 used_at = 0;
6266 }
6267 else if (imm_expr.X_op == O_constant
6268 && imm_expr.X_add_number > -0x8000
6269 && imm_expr.X_add_number < 0)
6270 {
6271 imm_expr.X_add_number = -imm_expr.X_add_number;
6272 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6273 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6274 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6275 used_at = 0;
6276 }
6277 else
6278 {
6279 load_register (&icnt, AT, &imm_expr, 0);
6280 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6281 sreg, AT);
6282 used_at = 1;
6283 }
6284 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
6285 if (used_at)
6286 break;
6287 return;
6288
6289 case M_DSUB_I:
6290 dbl = 1;
6291 case M_SUB_I:
6292 if (imm_expr.X_op == O_constant
6293 && imm_expr.X_add_number > -0x8000
6294 && imm_expr.X_add_number <= 0x8000)
6295 {
6296 imm_expr.X_add_number = -imm_expr.X_add_number;
6297 macro_build ((char *) NULL, &icnt, &imm_expr,
6298 dbl ? "daddi" : "addi",
6299 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6300 return;
6301 }
6302 load_register (&icnt, AT, &imm_expr, dbl);
6303 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 6304 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
6305 break;
6306
6307 case M_DSUBU_I:
6308 dbl = 1;
6309 case M_SUBU_I:
6310 if (imm_expr.X_op == O_constant
6311 && imm_expr.X_add_number > -0x8000
6312 && imm_expr.X_add_number <= 0x8000)
6313 {
6314 imm_expr.X_add_number = -imm_expr.X_add_number;
6315 macro_build ((char *) NULL, &icnt, &imm_expr,
6316 dbl ? "daddiu" : "addiu",
6317 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6318 return;
6319 }
6320 load_register (&icnt, AT, &imm_expr, dbl);
6321 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 6322 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
6323 break;
6324
6325 case M_TEQ_I:
6326 s = "teq";
6327 goto trap;
6328 case M_TGE_I:
6329 s = "tge";
6330 goto trap;
6331 case M_TGEU_I:
6332 s = "tgeu";
6333 goto trap;
6334 case M_TLT_I:
6335 s = "tlt";
6336 goto trap;
6337 case M_TLTU_I:
6338 s = "tltu";
6339 goto trap;
6340 case M_TNE_I:
6341 s = "tne";
6342 trap:
6343 load_register (&icnt, AT, &imm_expr, 0);
6344 macro_build ((char *) NULL, &icnt, NULL, s, "s,t", sreg, AT);
6345 break;
6346
252b5132 6347 case M_TRUNCWS:
43841e91 6348 case M_TRUNCWD:
e7af610e 6349 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6350 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
6351 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
6352
6353 /*
6354 * Is the double cfc1 instruction a bug in the mips assembler;
6355 * or is there a reason for it?
6356 */
6357 mips_emit_delays (true);
6358 ++mips_opts.noreorder;
6359 mips_any_noreorder = 1;
6360 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
6361 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
6362 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6363 expr1.X_add_number = 3;
6364 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
6365 (int) BFD_RELOC_LO16);
6366 expr1.X_add_number = 2;
6367 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
6368 (int) BFD_RELOC_LO16);
6369 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", AT, 31);
6370 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6371 macro_build ((char *) NULL, &icnt, NULL,
6372 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
6373 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", treg, 31);
6374 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6375 --mips_opts.noreorder;
6376 break;
6377
6378 case M_ULH:
6379 s = "lb";
6380 goto ulh;
6381 case M_ULHU:
6382 s = "lbu";
6383 ulh:
6384 if (offset_expr.X_add_number >= 0x7fff)
6385 as_bad (_("operand overflow"));
6386 /* avoid load delay */
6387 if (! target_big_endian)
6388 offset_expr.X_add_number += 1;
6389 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6390 (int) BFD_RELOC_LO16, breg);
6391 if (! target_big_endian)
6392 offset_expr.X_add_number -= 1;
6393 else
6394 offset_expr.X_add_number += 1;
6395 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
6396 (int) BFD_RELOC_LO16, breg);
6397 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
6398 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
6399 break;
6400
6401 case M_ULD:
6402 s = "ldl";
6403 s2 = "ldr";
6404 off = 7;
6405 goto ulw;
6406 case M_ULW:
6407 s = "lwl";
6408 s2 = "lwr";
6409 off = 3;
6410 ulw:
6411 if (offset_expr.X_add_number >= 0x8000 - off)
6412 as_bad (_("operand overflow"));
6413 if (! target_big_endian)
6414 offset_expr.X_add_number += off;
6415 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6416 (int) BFD_RELOC_LO16, breg);
6417 if (! target_big_endian)
6418 offset_expr.X_add_number -= off;
6419 else
6420 offset_expr.X_add_number += off;
6421 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
6422 (int) BFD_RELOC_LO16, breg);
6423 return;
6424
6425 case M_ULD_A:
6426 s = "ldl";
6427 s2 = "ldr";
6428 off = 7;
6429 goto ulwa;
6430 case M_ULW_A:
6431 s = "lwl";
6432 s2 = "lwr";
6433 off = 3;
6434 ulwa:
6435 load_address (&icnt, AT, &offset_expr);
6436 if (breg != 0)
6437 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6438 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6439 "d,v,t", AT, AT, breg);
6440 if (! target_big_endian)
6441 expr1.X_add_number = off;
6442 else
6443 expr1.X_add_number = 0;
6444 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
6445 (int) BFD_RELOC_LO16, AT);
6446 if (! target_big_endian)
6447 expr1.X_add_number = 0;
6448 else
6449 expr1.X_add_number = off;
6450 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
6451 (int) BFD_RELOC_LO16, AT);
6452 break;
6453
6454 case M_ULH_A:
6455 case M_ULHU_A:
6456 load_address (&icnt, AT, &offset_expr);
6457 if (breg != 0)
6458 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6459 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6460 "d,v,t", AT, AT, breg);
6461 if (target_big_endian)
6462 expr1.X_add_number = 0;
6463 macro_build ((char *) NULL, &icnt, &expr1,
6464 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
6465 (int) BFD_RELOC_LO16, AT);
6466 if (target_big_endian)
6467 expr1.X_add_number = 1;
6468 else
6469 expr1.X_add_number = 0;
6470 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
6471 (int) BFD_RELOC_LO16, AT);
6472 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
6473 treg, 8);
6474 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
6475 treg, AT);
6476 break;
6477
6478 case M_USH:
6479 if (offset_expr.X_add_number >= 0x7fff)
6480 as_bad (_("operand overflow"));
6481 if (target_big_endian)
6482 offset_expr.X_add_number += 1;
6483 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
6484 (int) BFD_RELOC_LO16, breg);
6485 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
6486 if (target_big_endian)
6487 offset_expr.X_add_number -= 1;
6488 else
6489 offset_expr.X_add_number += 1;
6490 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
6491 (int) BFD_RELOC_LO16, breg);
6492 break;
6493
6494 case M_USD:
6495 s = "sdl";
6496 s2 = "sdr";
6497 off = 7;
6498 goto usw;
6499 case M_USW:
6500 s = "swl";
6501 s2 = "swr";
6502 off = 3;
6503 usw:
6504 if (offset_expr.X_add_number >= 0x8000 - off)
6505 as_bad (_("operand overflow"));
6506 if (! target_big_endian)
6507 offset_expr.X_add_number += off;
6508 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6509 (int) BFD_RELOC_LO16, breg);
6510 if (! target_big_endian)
6511 offset_expr.X_add_number -= off;
6512 else
6513 offset_expr.X_add_number += off;
6514 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
6515 (int) BFD_RELOC_LO16, breg);
6516 return;
6517
6518 case M_USD_A:
6519 s = "sdl";
6520 s2 = "sdr";
6521 off = 7;
6522 goto uswa;
6523 case M_USW_A:
6524 s = "swl";
6525 s2 = "swr";
6526 off = 3;
6527 uswa:
6528 load_address (&icnt, AT, &offset_expr);
6529 if (breg != 0)
6530 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6531 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6532 "d,v,t", AT, AT, breg);
6533 if (! target_big_endian)
6534 expr1.X_add_number = off;
6535 else
6536 expr1.X_add_number = 0;
6537 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
6538 (int) BFD_RELOC_LO16, AT);
6539 if (! target_big_endian)
6540 expr1.X_add_number = 0;
6541 else
6542 expr1.X_add_number = off;
6543 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
6544 (int) BFD_RELOC_LO16, AT);
6545 break;
6546
6547 case M_USH_A:
6548 load_address (&icnt, AT, &offset_expr);
6549 if (breg != 0)
6550 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6551 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6552 "d,v,t", AT, AT, breg);
6553 if (! target_big_endian)
6554 expr1.X_add_number = 0;
6555 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
6556 (int) BFD_RELOC_LO16, AT);
6557 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", treg,
6558 treg, 8);
6559 if (! target_big_endian)
6560 expr1.X_add_number = 1;
6561 else
6562 expr1.X_add_number = 0;
6563 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
6564 (int) BFD_RELOC_LO16, AT);
6565 if (! target_big_endian)
6566 expr1.X_add_number = 0;
6567 else
6568 expr1.X_add_number = 1;
6569 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
6570 (int) BFD_RELOC_LO16, AT);
6571 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
6572 treg, 8);
6573 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
6574 treg, AT);
6575 break;
6576
6577 default:
6578 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 6579 are added dynamically. */
252b5132
RH
6580 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
6581 break;
6582 }
6583 if (mips_opts.noat)
6584 as_warn (_("Macro used $at after \".set noat\""));
6585}
6586
6587/* Implement macros in mips16 mode. */
6588
6589static void
6590mips16_macro (ip)
6591 struct mips_cl_insn *ip;
6592{
6593 int mask;
6594 int xreg, yreg, zreg, tmp;
6595 int icnt;
6596 expressionS expr1;
6597 int dbl;
6598 const char *s, *s2, *s3;
6599
6600 mask = ip->insn_mo->mask;
6601
6602 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
6603 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
6604 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
6605
6606 icnt = 0;
6607
6608 expr1.X_op = O_constant;
6609 expr1.X_op_symbol = NULL;
6610 expr1.X_add_symbol = NULL;
6611 expr1.X_add_number = 1;
6612
6613 dbl = 0;
6614
6615 switch (mask)
6616 {
6617 default:
6618 internalError ();
6619
6620 case M_DDIV_3:
6621 dbl = 1;
6622 case M_DIV_3:
6623 s = "mflo";
6624 goto do_div3;
6625 case M_DREM_3:
6626 dbl = 1;
6627 case M_REM_3:
6628 s = "mfhi";
6629 do_div3:
6630 mips_emit_delays (true);
6631 ++mips_opts.noreorder;
6632 mips_any_noreorder = 1;
6633 macro_build ((char *) NULL, &icnt, NULL,
6634 dbl ? "ddiv" : "div",
6635 "0,x,y", xreg, yreg);
6636 expr1.X_add_number = 2;
6637 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
beae10d5 6638 macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
bdaaa2e1 6639
252b5132
RH
6640 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
6641 since that causes an overflow. We should do that as well,
6642 but I don't see how to do the comparisons without a temporary
6643 register. */
6644 --mips_opts.noreorder;
6645 macro_build ((char *) NULL, &icnt, NULL, s, "x", zreg);
6646 break;
6647
6648 case M_DIVU_3:
6649 s = "divu";
6650 s2 = "mflo";
6651 goto do_divu3;
6652 case M_REMU_3:
6653 s = "divu";
6654 s2 = "mfhi";
6655 goto do_divu3;
6656 case M_DDIVU_3:
6657 s = "ddivu";
6658 s2 = "mflo";
6659 goto do_divu3;
6660 case M_DREMU_3:
6661 s = "ddivu";
6662 s2 = "mfhi";
6663 do_divu3:
6664 mips_emit_delays (true);
6665 ++mips_opts.noreorder;
6666 mips_any_noreorder = 1;
6667 macro_build ((char *) NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
6668 expr1.X_add_number = 2;
6669 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
6670 macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
6671 --mips_opts.noreorder;
6672 macro_build ((char *) NULL, &icnt, NULL, s2, "x", zreg);
6673 break;
6674
6675 case M_DMUL:
6676 dbl = 1;
6677 case M_MUL:
6678 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 6679 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
252b5132
RH
6680 macro_build ((char *) NULL, &icnt, NULL, "mflo", "x", zreg);
6681 return;
6682
6683 case M_DSUBU_I:
6684 dbl = 1;
6685 goto do_subu;
6686 case M_SUBU_I:
6687 do_subu:
6688 if (imm_expr.X_op != O_constant)
6689 as_bad (_("Unsupported large constant"));
6690 imm_expr.X_add_number = -imm_expr.X_add_number;
6691 macro_build ((char *) NULL, &icnt, &imm_expr,
9a41af64 6692 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
6693 break;
6694
6695 case M_SUBU_I_2:
6696 if (imm_expr.X_op != O_constant)
6697 as_bad (_("Unsupported large constant"));
6698 imm_expr.X_add_number = -imm_expr.X_add_number;
6699 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
6700 "x,k", xreg);
6701 break;
6702
6703 case M_DSUBU_I_2:
6704 if (imm_expr.X_op != O_constant)
6705 as_bad (_("Unsupported large constant"));
6706 imm_expr.X_add_number = -imm_expr.X_add_number;
6707 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
6708 "y,j", yreg);
6709 break;
6710
6711 case M_BEQ:
6712 s = "cmp";
6713 s2 = "bteqz";
6714 goto do_branch;
6715 case M_BNE:
6716 s = "cmp";
6717 s2 = "btnez";
6718 goto do_branch;
6719 case M_BLT:
6720 s = "slt";
6721 s2 = "btnez";
6722 goto do_branch;
6723 case M_BLTU:
6724 s = "sltu";
6725 s2 = "btnez";
6726 goto do_branch;
6727 case M_BLE:
6728 s = "slt";
6729 s2 = "bteqz";
6730 goto do_reverse_branch;
6731 case M_BLEU:
6732 s = "sltu";
6733 s2 = "bteqz";
6734 goto do_reverse_branch;
6735 case M_BGE:
6736 s = "slt";
6737 s2 = "bteqz";
6738 goto do_branch;
6739 case M_BGEU:
6740 s = "sltu";
6741 s2 = "bteqz";
6742 goto do_branch;
6743 case M_BGT:
6744 s = "slt";
6745 s2 = "btnez";
6746 goto do_reverse_branch;
6747 case M_BGTU:
6748 s = "sltu";
6749 s2 = "btnez";
6750
6751 do_reverse_branch:
6752 tmp = xreg;
6753 xreg = yreg;
6754 yreg = tmp;
6755
6756 do_branch:
6757 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
6758 xreg, yreg);
6759 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
6760 break;
6761
6762 case M_BEQ_I:
6763 s = "cmpi";
6764 s2 = "bteqz";
6765 s3 = "x,U";
6766 goto do_branch_i;
6767 case M_BNE_I:
6768 s = "cmpi";
6769 s2 = "btnez";
6770 s3 = "x,U";
6771 goto do_branch_i;
6772 case M_BLT_I:
6773 s = "slti";
6774 s2 = "btnez";
6775 s3 = "x,8";
6776 goto do_branch_i;
6777 case M_BLTU_I:
6778 s = "sltiu";
6779 s2 = "btnez";
6780 s3 = "x,8";
6781 goto do_branch_i;
6782 case M_BLE_I:
6783 s = "slti";
6784 s2 = "btnez";
6785 s3 = "x,8";
6786 goto do_addone_branch_i;
6787 case M_BLEU_I:
6788 s = "sltiu";
6789 s2 = "btnez";
6790 s3 = "x,8";
6791 goto do_addone_branch_i;
6792 case M_BGE_I:
6793 s = "slti";
6794 s2 = "bteqz";
6795 s3 = "x,8";
6796 goto do_branch_i;
6797 case M_BGEU_I:
6798 s = "sltiu";
6799 s2 = "bteqz";
6800 s3 = "x,8";
6801 goto do_branch_i;
6802 case M_BGT_I:
6803 s = "slti";
6804 s2 = "bteqz";
6805 s3 = "x,8";
6806 goto do_addone_branch_i;
6807 case M_BGTU_I:
6808 s = "sltiu";
6809 s2 = "bteqz";
6810 s3 = "x,8";
6811
6812 do_addone_branch_i:
6813 if (imm_expr.X_op != O_constant)
6814 as_bad (_("Unsupported large constant"));
6815 ++imm_expr.X_add_number;
6816
6817 do_branch_i:
6818 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
6819 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
6820 break;
6821
6822 case M_ABS:
6823 expr1.X_add_number = 0;
6824 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
6825 if (xreg != yreg)
ea1fb5dc 6826 move_register (&icnt, xreg, yreg);
252b5132
RH
6827 expr1.X_add_number = 2;
6828 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
6829 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6830 "neg", "x,w", xreg, xreg);
6831 }
6832}
6833
6834/* For consistency checking, verify that all bits are specified either
6835 by the match/mask part of the instruction definition, or by the
6836 operand list. */
6837static int
6838validate_mips_insn (opc)
6839 const struct mips_opcode *opc;
6840{
6841 const char *p = opc->args;
6842 char c;
6843 unsigned long used_bits = opc->mask;
6844
6845 if ((used_bits & opc->match) != opc->match)
6846 {
6847 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
6848 opc->name, opc->args);
6849 return 0;
6850 }
6851#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
6852 while (*p)
6853 switch (c = *p++)
6854 {
6855 case ',': break;
6856 case '(': break;
6857 case ')': break;
6858 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
6859 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
6860 case 'A': break;
4372b673 6861 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
6862 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
6863 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
6864 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
6865 case 'F': break;
6866 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 6867 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 6868 case 'I': break;
e972090a 6869 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
252b5132
RH
6870 case 'L': break;
6871 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
6872 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
6873 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
6874 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
6875 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
6876 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
6877 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
6878 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
6879 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
6880 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
6881 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
6882 case 'f': break;
6883 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
6884 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
6885 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
6886 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
6887 case 'l': break;
6888 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
6889 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
6890 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
6891 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
6892 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
6893 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
6894 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
6895 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
6896 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
6897 case 'x': break;
6898 case 'z': break;
6899 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
6900 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
6901 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
252b5132
RH
6902 default:
6903 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
6904 c, opc->name, opc->args);
6905 return 0;
6906 }
6907#undef USE_BITS
6908 if (used_bits != 0xffffffff)
6909 {
6910 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
6911 ~used_bits & 0xffffffff, opc->name, opc->args);
6912 return 0;
6913 }
6914 return 1;
6915}
6916
6917/* This routine assembles an instruction into its binary format. As a
6918 side effect, it sets one of the global variables imm_reloc or
6919 offset_reloc to the type of relocation to do if one of the operands
6920 is an address expression. */
6921
6922static void
6923mips_ip (str, ip)
6924 char *str;
6925 struct mips_cl_insn *ip;
6926{
6927 char *s;
6928 const char *args;
43841e91 6929 char c = 0;
252b5132
RH
6930 struct mips_opcode *insn;
6931 char *argsStart;
6932 unsigned int regno;
6933 unsigned int lastregno = 0;
6934 char *s_reset;
6935 char save_c = 0;
6936 int full_opcode_match = 1;
6937
6938 insn_error = NULL;
6939
6940 /* If the instruction contains a '.', we first try to match an instruction
6941 including the '.'. Then we try again without the '.'. */
6942 insn = NULL;
d9a62219 6943 for (s = str; *s != '\0' && !isspace ((unsigned char) *s); ++s)
252b5132
RH
6944 continue;
6945
6946 /* If we stopped on whitespace, then replace the whitespace with null for
6947 the call to hash_find. Save the character we replaced just in case we
6948 have to re-parse the instruction. */
d9a62219 6949 if (isspace ((unsigned char) *s))
252b5132
RH
6950 {
6951 save_c = *s;
6952 *s++ = '\0';
6953 }
bdaaa2e1 6954
252b5132
RH
6955 insn = (struct mips_opcode *) hash_find (op_hash, str);
6956
6957 /* If we didn't find the instruction in the opcode table, try again, but
6958 this time with just the instruction up to, but not including the
6959 first '.'. */
6960 if (insn == NULL)
6961 {
bdaaa2e1 6962 /* Restore the character we overwrite above (if any). */
252b5132
RH
6963 if (save_c)
6964 *(--s) = save_c;
6965
6966 /* Scan up to the first '.' or whitespace. */
d9a62219 6967 for (s = str; *s != '\0' && *s != '.' && !isspace ((unsigned char) *s); ++s)
252b5132
RH
6968 continue;
6969
6970 /* If we did not find a '.', then we can quit now. */
6971 if (*s != '.')
6972 {
6973 insn_error = "unrecognized opcode";
6974 return;
6975 }
6976
6977 /* Lookup the instruction in the hash table. */
6978 *s++ = '\0';
6979 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
6980 {
6981 insn_error = "unrecognized opcode";
6982 return;
6983 }
6984
6985 full_opcode_match = 0;
6986 }
6987
6988 argsStart = s;
6989 for (;;)
6990 {
252b5132
RH
6991 boolean ok;
6992
6993 assert (strcmp (insn->name, str) == 0);
6994
ea1fb5dc 6995 if (OPCODE_IS_MEMBER (insn, mips_opts.isa, mips_arch))
252b5132 6996 ok = true;
bdaaa2e1 6997 else
252b5132 6998 ok = false;
bdaaa2e1 6999
252b5132
RH
7000 if (insn->pinfo != INSN_MACRO)
7001 {
ec68c924 7002 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
252b5132
RH
7003 ok = false;
7004 }
7005
7006 if (! ok)
7007 {
7008 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7009 && strcmp (insn->name, insn[1].name) == 0)
7010 {
7011 ++insn;
7012 continue;
7013 }
252b5132 7014 else
beae10d5 7015 {
268f6bed
L
7016 if (!insn_error)
7017 {
7018 static char buf[100];
7019 sprintf (buf,
7020 _("opcode not supported on this processor: %s (%s)"),
ec68c924 7021 mips_cpu_to_str (mips_arch),
268f6bed 7022 mips_isa_to_str (mips_opts.isa));
bdaaa2e1 7023
268f6bed
L
7024 insn_error = buf;
7025 }
7026 if (save_c)
7027 *(--s) = save_c;
2bd7f1f3 7028 return;
252b5132 7029 }
252b5132
RH
7030 }
7031
7032 ip->insn_mo = insn;
7033 ip->insn_opcode = insn->match;
268f6bed 7034 insn_error = NULL;
252b5132
RH
7035 for (args = insn->args;; ++args)
7036 {
7037 if (*s == ' ')
7038 ++s;
7039 switch (*args)
7040 {
7041 case '\0': /* end of args */
7042 if (*s == '\0')
7043 return;
7044 break;
7045
7046 case ',':
7047 if (*s++ == *args)
7048 continue;
7049 s--;
7050 switch (*++args)
7051 {
7052 case 'r':
7053 case 'v':
38487616 7054 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
7055 continue;
7056
7057 case 'w':
38487616
TS
7058 ip->insn_opcode |= lastregno << OP_SH_RT;
7059 continue;
7060
252b5132 7061 case 'W':
38487616 7062 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
7063 continue;
7064
7065 case 'V':
38487616 7066 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
7067 continue;
7068 }
7069 break;
7070
7071 case '(':
7072 /* Handle optional base register.
7073 Either the base register is omitted or
bdaaa2e1 7074 we must have a left paren. */
252b5132
RH
7075 /* This is dependent on the next operand specifier
7076 is a base register specification. */
7077 assert (args[1] == 'b' || args[1] == '5'
7078 || args[1] == '-' || args[1] == '4');
7079 if (*s == '\0')
7080 return;
7081
7082 case ')': /* these must match exactly */
7083 if (*s++ == *args)
7084 continue;
7085 break;
7086
7087 case '<': /* must be at least one digit */
7088 /*
7089 * According to the manual, if the shift amount is greater
7090 * than 31 or less than 0 the the shift amount should be
7091 * mod 32. In reality the mips assembler issues an error.
7092 * We issue a warning and mask out all but the low 5 bits.
7093 */
7094 my_getExpression (&imm_expr, s);
7095 check_absolute_expr (ip, &imm_expr);
7096 if ((unsigned long) imm_expr.X_add_number > 31)
7097 {
7098 as_warn (_("Improper shift amount (%ld)"),
7099 (long) imm_expr.X_add_number);
38487616 7100 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 7101 }
38487616 7102 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
7103 imm_expr.X_op = O_absent;
7104 s = expr_end;
7105 continue;
7106
7107 case '>': /* shift amount minus 32 */
7108 my_getExpression (&imm_expr, s);
7109 check_absolute_expr (ip, &imm_expr);
7110 if ((unsigned long) imm_expr.X_add_number < 32
7111 || (unsigned long) imm_expr.X_add_number > 63)
7112 break;
38487616 7113 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
7114 imm_expr.X_op = O_absent;
7115 s = expr_end;
7116 continue;
7117
252b5132
RH
7118 case 'k': /* cache code */
7119 case 'h': /* prefx code */
7120 my_getExpression (&imm_expr, s);
7121 check_absolute_expr (ip, &imm_expr);
7122 if ((unsigned long) imm_expr.X_add_number > 31)
7123 {
7124 as_warn (_("Invalid value for `%s' (%lu)"),
7125 ip->insn_mo->name,
7126 (unsigned long) imm_expr.X_add_number);
7127 imm_expr.X_add_number &= 0x1f;
7128 }
7129 if (*args == 'k')
7130 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7131 else
7132 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7133 imm_expr.X_op = O_absent;
7134 s = expr_end;
7135 continue;
7136
7137 case 'c': /* break code */
7138 my_getExpression (&imm_expr, s);
7139 check_absolute_expr (ip, &imm_expr);
7140 if ((unsigned) imm_expr.X_add_number > 1023)
7141 {
7142 as_warn (_("Illegal break code (%ld)"),
7143 (long) imm_expr.X_add_number);
38487616 7144 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 7145 }
38487616 7146 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
7147 imm_expr.X_op = O_absent;
7148 s = expr_end;
7149 continue;
7150
7151 case 'q': /* lower break code */
7152 my_getExpression (&imm_expr, s);
7153 check_absolute_expr (ip, &imm_expr);
7154 if ((unsigned) imm_expr.X_add_number > 1023)
7155 {
7156 as_warn (_("Illegal lower break code (%ld)"),
7157 (long) imm_expr.X_add_number);
38487616 7158 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 7159 }
38487616 7160 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
7161 imm_expr.X_op = O_absent;
7162 s = expr_end;
7163 continue;
7164
4372b673 7165 case 'B': /* 20-bit syscall/break code. */
156c2f8b 7166 my_getExpression (&imm_expr, s);
156c2f8b 7167 check_absolute_expr (ip, &imm_expr);
38487616 7168 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE20)
4372b673 7169 as_warn (_("Illegal 20-bit code (%ld)"),
252b5132 7170 (long) imm_expr.X_add_number);
38487616 7171 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
7172 imm_expr.X_op = O_absent;
7173 s = expr_end;
7174 continue;
7175
7176 case 'C': /* Coprocessor code */
beae10d5 7177 my_getExpression (&imm_expr, s);
252b5132 7178 check_absolute_expr (ip, &imm_expr);
beae10d5 7179 if ((unsigned long) imm_expr.X_add_number >= (1<<25))
252b5132 7180 {
beae10d5 7181 as_warn (_("Coproccesor code > 25 bits (%ld)"),
252b5132 7182 (long) imm_expr.X_add_number);
beae10d5 7183 imm_expr.X_add_number &= ((1<<25) - 1);
252b5132 7184 }
beae10d5
KH
7185 ip->insn_opcode |= imm_expr.X_add_number;
7186 imm_expr.X_op = O_absent;
7187 s = expr_end;
7188 continue;
252b5132 7189
4372b673
NC
7190 case 'J': /* 19-bit wait code. */
7191 my_getExpression (&imm_expr, s);
7192 check_absolute_expr (ip, &imm_expr);
38487616 7193 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE19)
4372b673
NC
7194 as_warn (_("Illegal 19-bit code (%ld)"),
7195 (long) imm_expr.X_add_number);
38487616 7196 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
7197 imm_expr.X_op = O_absent;
7198 s = expr_end;
7199 continue;
7200
252b5132 7201 case 'P': /* Performance register */
beae10d5 7202 my_getExpression (&imm_expr, s);
252b5132 7203 check_absolute_expr (ip, &imm_expr);
beae10d5 7204 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 7205 {
38487616 7206 as_warn (_("Invalid performance register (%ld)"),
252b5132 7207 (long) imm_expr.X_add_number);
38487616 7208 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 7209 }
38487616 7210 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
7211 imm_expr.X_op = O_absent;
7212 s = expr_end;
7213 continue;
252b5132
RH
7214
7215 case 'b': /* base register */
7216 case 'd': /* destination register */
7217 case 's': /* source register */
7218 case 't': /* target register */
7219 case 'r': /* both target and source */
7220 case 'v': /* both dest and source */
7221 case 'w': /* both dest and target */
7222 case 'E': /* coprocessor target register */
7223 case 'G': /* coprocessor destination register */
7224 case 'x': /* ignore register name */
7225 case 'z': /* must be zero register */
4372b673 7226 case 'U': /* destination register (clo/clz). */
252b5132
RH
7227 s_reset = s;
7228 if (s[0] == '$')
7229 {
7230
d9a62219 7231 if (isdigit ((unsigned char) s[1]))
252b5132
RH
7232 {
7233 ++s;
7234 regno = 0;
7235 do
7236 {
7237 regno *= 10;
7238 regno += *s - '0';
7239 ++s;
7240 }
d9a62219 7241 while (isdigit ((unsigned char) *s));
252b5132
RH
7242 if (regno > 31)
7243 as_bad (_("Invalid register number (%d)"), regno);
7244 }
7245 else if (*args == 'E' || *args == 'G')
7246 goto notreg;
7247 else
7248 {
7249 if (s[1] == 'f' && s[2] == 'p')
7250 {
7251 s += 3;
7252 regno = FP;
7253 }
7254 else if (s[1] == 's' && s[2] == 'p')
7255 {
7256 s += 3;
7257 regno = SP;
7258 }
7259 else if (s[1] == 'g' && s[2] == 'p')
7260 {
7261 s += 3;
7262 regno = GP;
7263 }
7264 else if (s[1] == 'a' && s[2] == 't')
7265 {
7266 s += 3;
7267 regno = AT;
7268 }
7269 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7270 {
7271 s += 4;
7272 regno = KT0;
7273 }
7274 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7275 {
7276 s += 4;
7277 regno = KT1;
7278 }
7279 else if (itbl_have_entries)
7280 {
7281 char *p, *n;
d7ba4a77 7282 unsigned long r;
252b5132 7283
d7ba4a77 7284 p = s + 1; /* advance past '$' */
252b5132
RH
7285 n = itbl_get_field (&p); /* n is name */
7286
d7ba4a77
ILT
7287 /* See if this is a register defined in an
7288 itbl entry. */
7289 if (itbl_get_reg_val (n, &r))
252b5132
RH
7290 {
7291 /* Get_field advances to the start of
7292 the next field, so we need to back
d7ba4a77 7293 rack to the end of the last field. */
bdaaa2e1 7294 if (p)
252b5132 7295 s = p - 1;
bdaaa2e1 7296 else
d7ba4a77 7297 s = strchr (s, '\0');
252b5132
RH
7298 regno = r;
7299 }
7300 else
7301 goto notreg;
beae10d5 7302 }
252b5132
RH
7303 else
7304 goto notreg;
7305 }
7306 if (regno == AT
7307 && ! mips_opts.noat
7308 && *args != 'E'
7309 && *args != 'G')
7310 as_warn (_("Used $at without \".set noat\""));
7311 c = *args;
7312 if (*s == ' ')
7313 s++;
7314 if (args[1] != *s)
7315 {
7316 if (c == 'r' || c == 'v' || c == 'w')
7317 {
7318 regno = lastregno;
7319 s = s_reset;
7320 args++;
7321 }
7322 }
7323 /* 'z' only matches $0. */
7324 if (c == 'z' && regno != 0)
7325 break;
7326
bdaaa2e1
KH
7327 /* Now that we have assembled one operand, we use the args string
7328 * to figure out where it goes in the instruction. */
252b5132
RH
7329 switch (c)
7330 {
7331 case 'r':
7332 case 's':
7333 case 'v':
7334 case 'b':
38487616 7335 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
7336 break;
7337 case 'd':
7338 case 'G':
38487616 7339 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 7340 break;
4372b673 7341 case 'U':
38487616
TS
7342 ip->insn_opcode |= regno << OP_SH_RD;
7343 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 7344 break;
252b5132
RH
7345 case 'w':
7346 case 't':
7347 case 'E':
38487616 7348 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
7349 break;
7350 case 'x':
7351 /* This case exists because on the r3000 trunc
7352 expands into a macro which requires a gp
7353 register. On the r6000 or r4000 it is
7354 assembled into a single instruction which
7355 ignores the register. Thus the insn version
7356 is MIPS_ISA2 and uses 'x', and the macro
7357 version is MIPS_ISA1 and uses 't'. */
7358 break;
7359 case 'z':
7360 /* This case is for the div instruction, which
7361 acts differently if the destination argument
7362 is $0. This only matches $0, and is checked
7363 outside the switch. */
7364 break;
7365 case 'D':
7366 /* Itbl operand; not yet implemented. FIXME ?? */
7367 break;
7368 /* What about all other operands like 'i', which
7369 can be specified in the opcode table? */
7370 }
7371 lastregno = regno;
7372 continue;
7373 }
7374 notreg:
7375 switch (*args++)
7376 {
7377 case 'r':
7378 case 'v':
38487616 7379 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
7380 continue;
7381 case 'w':
38487616 7382 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
7383 continue;
7384 }
7385 break;
7386
7387 case 'D': /* floating point destination register */
7388 case 'S': /* floating point source register */
7389 case 'T': /* floating point target register */
7390 case 'R': /* floating point source register */
7391 case 'V':
7392 case 'W':
7393 s_reset = s;
d9a62219 7394 if (s[0] == '$' && s[1] == 'f' && isdigit ((unsigned char) s[2]))
252b5132
RH
7395 {
7396 s += 2;
7397 regno = 0;
7398 do
7399 {
7400 regno *= 10;
7401 regno += *s - '0';
7402 ++s;
7403 }
d9a62219 7404 while (isdigit ((unsigned char) *s));
252b5132
RH
7405
7406 if (regno > 31)
7407 as_bad (_("Invalid float register number (%d)"), regno);
7408
7409 if ((regno & 1) != 0
ca4e0257 7410 && HAVE_32BIT_FPRS
252b5132
RH
7411 && ! (strcmp (str, "mtc1") == 0
7412 || strcmp (str, "mfc1") == 0
7413 || strcmp (str, "lwc1") == 0
7414 || strcmp (str, "swc1") == 0
7415 || strcmp (str, "l.s") == 0
7416 || strcmp (str, "s.s") == 0))
7417 as_warn (_("Float register should be even, was %d"),
7418 regno);
7419
7420 c = *args;
7421 if (*s == ' ')
7422 s++;
7423 if (args[1] != *s)
7424 {
7425 if (c == 'V' || c == 'W')
7426 {
7427 regno = lastregno;
7428 s = s_reset;
7429 args++;
7430 }
7431 }
7432 switch (c)
7433 {
7434 case 'D':
38487616 7435 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
7436 break;
7437 case 'V':
7438 case 'S':
38487616 7439 ip->insn_opcode |= regno << OP_SH_FS;
252b5132
RH
7440 break;
7441 case 'W':
7442 case 'T':
38487616 7443 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
7444 break;
7445 case 'R':
38487616 7446 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
7447 break;
7448 }
7449 lastregno = regno;
7450 continue;
7451 }
7452
252b5132
RH
7453 switch (*args++)
7454 {
7455 case 'V':
38487616 7456 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
7457 continue;
7458 case 'W':
38487616 7459 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
7460 continue;
7461 }
7462 break;
7463
7464 case 'I':
7465 my_getExpression (&imm_expr, s);
7466 if (imm_expr.X_op != O_big
7467 && imm_expr.X_op != O_constant)
7468 insn_error = _("absolute expression required");
7469 s = expr_end;
7470 continue;
7471
7472 case 'A':
7473 my_getExpression (&offset_expr, s);
7474 imm_reloc = BFD_RELOC_32;
7475 s = expr_end;
7476 continue;
7477
7478 case 'F':
7479 case 'L':
7480 case 'f':
7481 case 'l':
7482 {
7483 int f64;
ca4e0257 7484 int using_gprs;
252b5132
RH
7485 char *save_in;
7486 char *err;
7487 unsigned char temp[8];
7488 int len;
7489 unsigned int length;
7490 segT seg;
7491 subsegT subseg;
7492 char *p;
7493
7494 /* These only appear as the last operand in an
7495 instruction, and every instruction that accepts
7496 them in any variant accepts them in all variants.
7497 This means we don't have to worry about backing out
7498 any changes if the instruction does not match.
7499
7500 The difference between them is the size of the
7501 floating point constant and where it goes. For 'F'
7502 and 'L' the constant is 64 bits; for 'f' and 'l' it
7503 is 32 bits. Where the constant is placed is based
7504 on how the MIPS assembler does things:
7505 F -- .rdata
7506 L -- .lit8
7507 f -- immediate value
7508 l -- .lit4
7509
7510 The .lit4 and .lit8 sections are only used if
7511 permitted by the -G argument.
7512
7513 When generating embedded PIC code, we use the
7514 .lit8 section but not the .lit4 section (we can do
7515 .lit4 inline easily; we need to put .lit8
7516 somewhere in the data segment, and using .lit8
7517 permits the linker to eventually combine identical
ca4e0257
RS
7518 .lit8 entries).
7519
7520 The code below needs to know whether the target register
7521 is 32 or 64 bits wide. It relies on the fact 'f' and
7522 'F' are used with GPR-based instructions and 'l' and
7523 'L' are used with FPR-based instructions. */
252b5132
RH
7524
7525 f64 = *args == 'F' || *args == 'L';
ca4e0257 7526 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
7527
7528 save_in = input_line_pointer;
7529 input_line_pointer = s;
7530 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
7531 length = len;
7532 s = input_line_pointer;
7533 input_line_pointer = save_in;
7534 if (err != NULL && *err != '\0')
7535 {
7536 as_bad (_("Bad floating point constant: %s"), err);
7537 memset (temp, '\0', sizeof temp);
7538 length = f64 ? 8 : 4;
7539 }
7540
156c2f8b 7541 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
7542
7543 if (*args == 'f'
7544 || (*args == 'l'
7545 && (! USE_GLOBAL_POINTER_OPT
7546 || mips_pic == EMBEDDED_PIC
7547 || g_switch_value < 4
7548 || (temp[0] == 0 && temp[1] == 0)
7549 || (temp[2] == 0 && temp[3] == 0))))
7550 {
7551 imm_expr.X_op = O_constant;
7552 if (! target_big_endian)
7553 imm_expr.X_add_number = bfd_getl32 (temp);
7554 else
7555 imm_expr.X_add_number = bfd_getb32 (temp);
7556 }
7557 else if (length > 4
119d663a 7558 && ! mips_disable_float_construction
ca4e0257
RS
7559 /* Constants can only be constructed in GPRs and
7560 copied to FPRs if the GPRs are at least as wide
7561 as the FPRs. Force the constant into memory if
7562 we are using 64-bit FPRs but the GPRs are only
7563 32 bits wide. */
7564 && (using_gprs
7565 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
7566 && ((temp[0] == 0 && temp[1] == 0)
7567 || (temp[2] == 0 && temp[3] == 0))
7568 && ((temp[4] == 0 && temp[5] == 0)
7569 || (temp[6] == 0 && temp[7] == 0)))
7570 {
ca4e0257
RS
7571 /* The value is simple enough to load with a couple of
7572 instructions. If using 32-bit registers, set
7573 imm_expr to the high order 32 bits and offset_expr to
7574 the low order 32 bits. Otherwise, set imm_expr to
7575 the entire 64 bit constant. */
7576 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
7577 {
7578 imm_expr.X_op = O_constant;
7579 offset_expr.X_op = O_constant;
7580 if (! target_big_endian)
7581 {
7582 imm_expr.X_add_number = bfd_getl32 (temp + 4);
7583 offset_expr.X_add_number = bfd_getl32 (temp);
7584 }
7585 else
7586 {
7587 imm_expr.X_add_number = bfd_getb32 (temp);
7588 offset_expr.X_add_number = bfd_getb32 (temp + 4);
7589 }
7590 if (offset_expr.X_add_number == 0)
7591 offset_expr.X_op = O_absent;
7592 }
7593 else if (sizeof (imm_expr.X_add_number) > 4)
7594 {
7595 imm_expr.X_op = O_constant;
7596 if (! target_big_endian)
7597 imm_expr.X_add_number = bfd_getl64 (temp);
7598 else
7599 imm_expr.X_add_number = bfd_getb64 (temp);
7600 }
7601 else
7602 {
7603 imm_expr.X_op = O_big;
7604 imm_expr.X_add_number = 4;
7605 if (! target_big_endian)
7606 {
7607 generic_bignum[0] = bfd_getl16 (temp);
7608 generic_bignum[1] = bfd_getl16 (temp + 2);
7609 generic_bignum[2] = bfd_getl16 (temp + 4);
7610 generic_bignum[3] = bfd_getl16 (temp + 6);
7611 }
7612 else
7613 {
7614 generic_bignum[0] = bfd_getb16 (temp + 6);
7615 generic_bignum[1] = bfd_getb16 (temp + 4);
7616 generic_bignum[2] = bfd_getb16 (temp + 2);
7617 generic_bignum[3] = bfd_getb16 (temp);
7618 }
7619 }
7620 }
7621 else
7622 {
7623 const char *newname;
7624 segT new_seg;
7625
7626 /* Switch to the right section. */
7627 seg = now_seg;
7628 subseg = now_subseg;
7629 switch (*args)
7630 {
7631 default: /* unused default case avoids warnings. */
7632 case 'L':
7633 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
7634 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
7635 || mips_pic == EMBEDDED_PIC)
252b5132
RH
7636 newname = ".lit8";
7637 break;
7638 case 'F':
bb2d6cd7
GK
7639 if (mips_pic == EMBEDDED_PIC)
7640 newname = ".lit8";
7641 else
7642 newname = RDATA_SECTION_NAME;
252b5132
RH
7643 break;
7644 case 'l':
7645 assert (!USE_GLOBAL_POINTER_OPT
7646 || g_switch_value >= 4);
7647 newname = ".lit4";
7648 break;
7649 }
7650 new_seg = subseg_new (newname, (subsegT) 0);
7651 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
7652 bfd_set_section_flags (stdoutput, new_seg,
7653 (SEC_ALLOC
7654 | SEC_LOAD
7655 | SEC_READONLY
7656 | SEC_DATA));
7657 frag_align (*args == 'l' ? 2 : 3, 0, 0);
7658 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
7659 && strcmp (TARGET_OS, "elf") != 0)
7660 record_alignment (new_seg, 4);
7661 else
7662 record_alignment (new_seg, *args == 'l' ? 2 : 3);
7663 if (seg == now_seg)
7664 as_bad (_("Can't use floating point insn in this section"));
7665
7666 /* Set the argument to the current address in the
7667 section. */
7668 offset_expr.X_op = O_symbol;
7669 offset_expr.X_add_symbol =
7670 symbol_new ("L0\001", now_seg,
7671 (valueT) frag_now_fix (), frag_now);
7672 offset_expr.X_add_number = 0;
7673
7674 /* Put the floating point number into the section. */
7675 p = frag_more ((int) length);
7676 memcpy (p, temp, length);
7677
7678 /* Switch back to the original section. */
7679 subseg_set (seg, subseg);
7680 }
7681 }
7682 continue;
7683
7684 case 'i': /* 16 bit unsigned immediate */
7685 case 'j': /* 16 bit signed immediate */
7686 imm_reloc = BFD_RELOC_LO16;
7687 c = my_getSmallExpression (&imm_expr, s);
fb1b3232 7688 if (c != S_EX_NONE)
252b5132 7689 {
fb1b3232 7690 if (c != S_EX_LO)
252b5132
RH
7691 {
7692 if (imm_expr.X_op == O_constant)
7693 imm_expr.X_add_number =
7694 (imm_expr.X_add_number >> 16) & 0xffff;
fb1b3232
TS
7695 else if (c == S_EX_HIGHEST)
7696 imm_reloc = BFD_RELOC_MIPS_HIGHEST;
7697 else if (c == S_EX_HIGHER)
7698 imm_reloc = BFD_RELOC_MIPS_HIGHER;
7699 else if (c == S_EX_HI)
252b5132
RH
7700 {
7701 imm_reloc = BFD_RELOC_HI16_S;
7702 imm_unmatched_hi = true;
7703 }
7704 else
7705 imm_reloc = BFD_RELOC_HI16;
7706 }
7707 else if (imm_expr.X_op == O_constant)
7708 imm_expr.X_add_number &= 0xffff;
7709 }
7710 if (*args == 'i')
7711 {
fb1b3232 7712 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 7713 || ((imm_expr.X_add_number < 0
beae10d5
KH
7714 || imm_expr.X_add_number >= 0x10000)
7715 && imm_expr.X_op == O_constant))
252b5132
RH
7716 {
7717 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7718 !strcmp (insn->name, insn[1].name))
7719 break;
2ae7e77b
AH
7720 if (imm_expr.X_op == O_constant
7721 || imm_expr.X_op == O_big)
252b5132
RH
7722 as_bad (_("16 bit expression not in range 0..65535"));
7723 }
7724 }
7725 else
7726 {
7727 int more;
7728 offsetT max;
7729
7730 /* The upper bound should be 0x8000, but
7731 unfortunately the MIPS assembler accepts numbers
7732 from 0x8000 to 0xffff and sign extends them, and
7733 we want to be compatible. We only permit this
7734 extended range for an instruction which does not
7735 provide any further alternates, since those
7736 alternates may handle other cases. People should
7737 use the numbers they mean, rather than relying on
7738 a mysterious sign extension. */
7739 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7740 strcmp (insn->name, insn[1].name) == 0);
7741 if (more)
7742 max = 0x8000;
7743 else
7744 max = 0x10000;
fb1b3232 7745 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 7746 || ((imm_expr.X_add_number < -0x8000
beae10d5
KH
7747 || imm_expr.X_add_number >= max)
7748 && imm_expr.X_op == O_constant)
252b5132
RH
7749 || (more
7750 && imm_expr.X_add_number < 0
ca4e0257 7751 && HAVE_64BIT_GPRS
252b5132
RH
7752 && imm_expr.X_unsigned
7753 && sizeof (imm_expr.X_add_number) <= 4))
7754 {
7755 if (more)
7756 break;
2ae7e77b
AH
7757 if (imm_expr.X_op == O_constant
7758 || imm_expr.X_op == O_big)
252b5132
RH
7759 as_bad (_("16 bit expression not in range -32768..32767"));
7760 }
7761 }
7762 s = expr_end;
7763 continue;
7764
7765 case 'o': /* 16 bit offset */
7766 c = my_getSmallExpression (&offset_expr, s);
7767
7768 /* If this value won't fit into a 16 bit offset, then go
7769 find a macro that will generate the 32 bit offset
7770 code pattern. As a special hack, we accept the
7771 difference of two local symbols as a constant. This
7772 is required to suppose embedded PIC switches, which
7773 use an instruction which looks like
7774 lw $4,$L12-$LS12($4)
7775 The problem with handling this in a more general
7776 fashion is that the macro function doesn't expect to
7777 see anything which can be handled in a single
7778 constant instruction. */
fb1b3232 7779 if (c == S_EX_NONE
252b5132
RH
7780 && (offset_expr.X_op != O_constant
7781 || offset_expr.X_add_number >= 0x8000
7782 || offset_expr.X_add_number < -0x8000)
7783 && (mips_pic != EMBEDDED_PIC
7784 || offset_expr.X_op != O_subtract
3dacdf2e
NC
7785 || (S_GET_SEGMENT (offset_expr.X_add_symbol)
7786 != S_GET_SEGMENT (offset_expr.X_op_symbol))))
252b5132
RH
7787 break;
7788
fb1b3232 7789 if (c == S_EX_HI)
252b5132
RH
7790 {
7791 if (offset_expr.X_op != O_constant)
7792 break;
7793 offset_expr.X_add_number =
7794 (offset_expr.X_add_number >> 16) & 0xffff;
7795 }
7796 offset_reloc = BFD_RELOC_LO16;
7797 s = expr_end;
7798 continue;
7799
7800 case 'p': /* pc relative offset */
7801 offset_reloc = BFD_RELOC_16_PCREL_S2;
7802 my_getExpression (&offset_expr, s);
7803 s = expr_end;
7804 continue;
7805
7806 case 'u': /* upper 16 bits */
7807 c = my_getSmallExpression (&imm_expr, s);
7808 imm_reloc = BFD_RELOC_LO16;
7809 if (c)
7810 {
fb1b3232 7811 if (c != S_EX_LO)
252b5132
RH
7812 {
7813 if (imm_expr.X_op == O_constant)
7814 imm_expr.X_add_number =
7815 (imm_expr.X_add_number >> 16) & 0xffff;
fb1b3232
TS
7816 else if (c == S_EX_HIGHEST)
7817 imm_reloc = BFD_RELOC_MIPS_HIGHEST;
7818 else if (c == S_EX_HI)
252b5132
RH
7819 {
7820 imm_reloc = BFD_RELOC_HI16_S;
7821 imm_unmatched_hi = true;
7822 }
7823 else
7824 imm_reloc = BFD_RELOC_HI16;
7825 }
7826 else if (imm_expr.X_op == O_constant)
7827 imm_expr.X_add_number &= 0xffff;
7828 }
7829 if (imm_expr.X_op == O_constant
7830 && (imm_expr.X_add_number < 0
7831 || imm_expr.X_add_number >= 0x10000))
7832 as_bad (_("lui expression not in range 0..65535"));
7833 s = expr_end;
7834 continue;
7835
7836 case 'a': /* 26 bit address */
7837 my_getExpression (&offset_expr, s);
7838 s = expr_end;
7839 offset_reloc = BFD_RELOC_MIPS_JMP;
7840 continue;
7841
7842 case 'N': /* 3 bit branch condition code */
7843 case 'M': /* 3 bit compare condition code */
7844 if (strncmp (s, "$fcc", 4) != 0)
7845 break;
7846 s += 4;
7847 regno = 0;
7848 do
7849 {
7850 regno *= 10;
7851 regno += *s - '0';
7852 ++s;
7853 }
d9a62219 7854 while (isdigit ((unsigned char) *s));
252b5132
RH
7855 if (regno > 7)
7856 as_bad (_("invalid condition code register $fcc%d"), regno);
7857 if (*args == 'N')
7858 ip->insn_opcode |= regno << OP_SH_BCC;
7859 else
7860 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 7861 continue;
252b5132 7862
156c2f8b
NC
7863 case 'H':
7864 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
7865 s += 2;
7866 if (isdigit ((unsigned char) *s))
7867 {
7868 c = 0;
7869 do
7870 {
7871 c *= 10;
7872 c += *s - '0';
7873 ++s;
7874 }
7875 while (isdigit ((unsigned char) *s));
7876 }
7877 else
7878 c = 8; /* Invalid sel value. */
7879
7880 if (c > 7)
7881 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
7882 ip->insn_opcode |= c;
7883 continue;
7884
252b5132
RH
7885 default:
7886 as_bad (_("bad char = '%c'\n"), *args);
7887 internalError ();
7888 }
7889 break;
7890 }
7891 /* Args don't match. */
7892 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7893 !strcmp (insn->name, insn[1].name))
7894 {
7895 ++insn;
7896 s = argsStart;
268f6bed 7897 insn_error = _("illegal operands");
252b5132
RH
7898 continue;
7899 }
268f6bed
L
7900 if (save_c)
7901 *(--s) = save_c;
252b5132
RH
7902 insn_error = _("illegal operands");
7903 return;
7904 }
7905}
7906
7907/* This routine assembles an instruction into its binary format when
7908 assembling for the mips16. As a side effect, it sets one of the
7909 global variables imm_reloc or offset_reloc to the type of
7910 relocation to do if one of the operands is an address expression.
7911 It also sets mips16_small and mips16_ext if the user explicitly
7912 requested a small or extended instruction. */
7913
7914static void
7915mips16_ip (str, ip)
7916 char *str;
7917 struct mips_cl_insn *ip;
7918{
7919 char *s;
7920 const char *args;
7921 struct mips_opcode *insn;
7922 char *argsstart;
7923 unsigned int regno;
7924 unsigned int lastregno = 0;
7925 char *s_reset;
7926
7927 insn_error = NULL;
7928
7929 mips16_small = false;
7930 mips16_ext = false;
7931
d9a62219 7932 for (s = str; islower ((unsigned char) *s); ++s)
252b5132
RH
7933 ;
7934 switch (*s)
7935 {
7936 case '\0':
7937 break;
7938
7939 case ' ':
7940 *s++ = '\0';
7941 break;
7942
7943 case '.':
7944 if (s[1] == 't' && s[2] == ' ')
7945 {
7946 *s = '\0';
7947 mips16_small = true;
7948 s += 3;
7949 break;
7950 }
7951 else if (s[1] == 'e' && s[2] == ' ')
7952 {
7953 *s = '\0';
7954 mips16_ext = true;
7955 s += 3;
7956 break;
7957 }
7958 /* Fall through. */
7959 default:
7960 insn_error = _("unknown opcode");
7961 return;
7962 }
7963
7964 if (mips_opts.noautoextend && ! mips16_ext)
7965 mips16_small = true;
7966
7967 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
7968 {
7969 insn_error = _("unrecognized opcode");
7970 return;
7971 }
7972
7973 argsstart = s;
7974 for (;;)
7975 {
7976 assert (strcmp (insn->name, str) == 0);
7977
7978 ip->insn_mo = insn;
7979 ip->insn_opcode = insn->match;
7980 ip->use_extend = false;
7981 imm_expr.X_op = O_absent;
7982 imm_reloc = BFD_RELOC_UNUSED;
7983 offset_expr.X_op = O_absent;
7984 offset_reloc = BFD_RELOC_UNUSED;
7985 for (args = insn->args; 1; ++args)
7986 {
7987 int c;
7988
7989 if (*s == ' ')
7990 ++s;
7991
7992 /* In this switch statement we call break if we did not find
7993 a match, continue if we did find a match, or return if we
7994 are done. */
7995
7996 c = *args;
7997 switch (c)
7998 {
7999 case '\0':
8000 if (*s == '\0')
8001 {
8002 /* Stuff the immediate value in now, if we can. */
8003 if (imm_expr.X_op == O_constant
8004 && imm_reloc > BFD_RELOC_UNUSED
8005 && insn->pinfo != INSN_MACRO)
8006 {
8007 mips16_immed ((char *) NULL, 0,
8008 imm_reloc - BFD_RELOC_UNUSED,
8009 imm_expr.X_add_number, true, mips16_small,
8010 mips16_ext, &ip->insn_opcode,
8011 &ip->use_extend, &ip->extend);
8012 imm_expr.X_op = O_absent;
8013 imm_reloc = BFD_RELOC_UNUSED;
8014 }
8015
8016 return;
8017 }
8018 break;
8019
8020 case ',':
8021 if (*s++ == c)
8022 continue;
8023 s--;
8024 switch (*++args)
8025 {
8026 case 'v':
8027 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8028 continue;
8029 case 'w':
8030 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8031 continue;
8032 }
8033 break;
8034
8035 case '(':
8036 case ')':
8037 if (*s++ == c)
8038 continue;
8039 break;
8040
8041 case 'v':
8042 case 'w':
8043 if (s[0] != '$')
8044 {
8045 if (c == 'v')
8046 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8047 else
8048 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8049 ++args;
8050 continue;
8051 }
8052 /* Fall through. */
8053 case 'x':
8054 case 'y':
8055 case 'z':
8056 case 'Z':
8057 case '0':
8058 case 'S':
8059 case 'R':
8060 case 'X':
8061 case 'Y':
8062 if (s[0] != '$')
8063 break;
8064 s_reset = s;
d9a62219 8065 if (isdigit ((unsigned char) s[1]))
252b5132
RH
8066 {
8067 ++s;
8068 regno = 0;
8069 do
8070 {
8071 regno *= 10;
8072 regno += *s - '0';
8073 ++s;
8074 }
d9a62219 8075 while (isdigit ((unsigned char) *s));
252b5132
RH
8076 if (regno > 31)
8077 {
8078 as_bad (_("invalid register number (%d)"), regno);
8079 regno = 2;
8080 }
8081 }
8082 else
8083 {
8084 if (s[1] == 'f' && s[2] == 'p')
8085 {
8086 s += 3;
8087 regno = FP;
8088 }
8089 else if (s[1] == 's' && s[2] == 'p')
8090 {
8091 s += 3;
8092 regno = SP;
8093 }
8094 else if (s[1] == 'g' && s[2] == 'p')
8095 {
8096 s += 3;
8097 regno = GP;
8098 }
8099 else if (s[1] == 'a' && s[2] == 't')
8100 {
8101 s += 3;
8102 regno = AT;
8103 }
8104 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8105 {
8106 s += 4;
8107 regno = KT0;
8108 }
8109 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8110 {
8111 s += 4;
8112 regno = KT1;
8113 }
8114 else
8115 break;
8116 }
8117
8118 if (*s == ' ')
8119 ++s;
8120 if (args[1] != *s)
8121 {
8122 if (c == 'v' || c == 'w')
8123 {
8124 regno = mips16_to_32_reg_map[lastregno];
8125 s = s_reset;
8126 args++;
8127 }
8128 }
8129
8130 switch (c)
8131 {
8132 case 'x':
8133 case 'y':
8134 case 'z':
8135 case 'v':
8136 case 'w':
8137 case 'Z':
8138 regno = mips32_to_16_reg_map[regno];
8139 break;
8140
8141 case '0':
8142 if (regno != 0)
8143 regno = ILLEGAL_REG;
8144 break;
8145
8146 case 'S':
8147 if (regno != SP)
8148 regno = ILLEGAL_REG;
8149 break;
8150
8151 case 'R':
8152 if (regno != RA)
8153 regno = ILLEGAL_REG;
8154 break;
8155
8156 case 'X':
8157 case 'Y':
8158 if (regno == AT && ! mips_opts.noat)
8159 as_warn (_("used $at without \".set noat\""));
8160 break;
8161
8162 default:
8163 internalError ();
8164 }
8165
8166 if (regno == ILLEGAL_REG)
8167 break;
8168
8169 switch (c)
8170 {
8171 case 'x':
8172 case 'v':
8173 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
8174 break;
8175 case 'y':
8176 case 'w':
8177 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
8178 break;
8179 case 'z':
8180 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
8181 break;
8182 case 'Z':
8183 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
8184 case '0':
8185 case 'S':
8186 case 'R':
8187 break;
8188 case 'X':
8189 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
8190 break;
8191 case 'Y':
8192 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
8193 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
8194 break;
8195 default:
8196 internalError ();
8197 }
8198
8199 lastregno = regno;
8200 continue;
8201
8202 case 'P':
8203 if (strncmp (s, "$pc", 3) == 0)
8204 {
8205 s += 3;
8206 continue;
8207 }
8208 break;
8209
8210 case '<':
8211 case '>':
8212 case '[':
8213 case ']':
8214 case '4':
8215 case '5':
8216 case 'H':
8217 case 'W':
8218 case 'D':
8219 case 'j':
8220 case '8':
8221 case 'V':
8222 case 'C':
8223 case 'U':
8224 case 'k':
8225 case 'K':
8226 if (s[0] == '%'
8227 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
8228 {
8229 /* This is %gprel(SYMBOL). We need to read SYMBOL,
8230 and generate the appropriate reloc. If the text
8231 inside %gprel is not a symbol name with an
8232 optional offset, then we generate a normal reloc
8233 and will probably fail later. */
8234 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
8235 if (imm_expr.X_op == O_symbol)
8236 {
8237 mips16_ext = true;
8238 imm_reloc = BFD_RELOC_MIPS16_GPREL;
8239 s = expr_end;
8240 ip->use_extend = true;
8241 ip->extend = 0;
8242 continue;
8243 }
8244 }
8245 else
8246 {
8247 /* Just pick up a normal expression. */
8248 my_getExpression (&imm_expr, s);
8249 }
8250
8251 if (imm_expr.X_op == O_register)
8252 {
8253 /* What we thought was an expression turned out to
8254 be a register. */
8255
8256 if (s[0] == '(' && args[1] == '(')
8257 {
8258 /* It looks like the expression was omitted
8259 before a register indirection, which means
8260 that the expression is implicitly zero. We
8261 still set up imm_expr, so that we handle
8262 explicit extensions correctly. */
8263 imm_expr.X_op = O_constant;
8264 imm_expr.X_add_number = 0;
8265 imm_reloc = (int) BFD_RELOC_UNUSED + c;
8266 continue;
8267 }
8268
8269 break;
8270 }
8271
8272 /* We need to relax this instruction. */
8273 imm_reloc = (int) BFD_RELOC_UNUSED + c;
8274 s = expr_end;
8275 continue;
8276
8277 case 'p':
8278 case 'q':
8279 case 'A':
8280 case 'B':
8281 case 'E':
8282 /* We use offset_reloc rather than imm_reloc for the PC
8283 relative operands. This lets macros with both
8284 immediate and address operands work correctly. */
8285 my_getExpression (&offset_expr, s);
8286
8287 if (offset_expr.X_op == O_register)
8288 break;
8289
8290 /* We need to relax this instruction. */
8291 offset_reloc = (int) BFD_RELOC_UNUSED + c;
8292 s = expr_end;
8293 continue;
8294
8295 case '6': /* break code */
8296 my_getExpression (&imm_expr, s);
8297 check_absolute_expr (ip, &imm_expr);
8298 if ((unsigned long) imm_expr.X_add_number > 63)
8299 {
8300 as_warn (_("Invalid value for `%s' (%lu)"),
8301 ip->insn_mo->name,
8302 (unsigned long) imm_expr.X_add_number);
8303 imm_expr.X_add_number &= 0x3f;
8304 }
8305 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
8306 imm_expr.X_op = O_absent;
8307 s = expr_end;
8308 continue;
8309
8310 case 'a': /* 26 bit address */
8311 my_getExpression (&offset_expr, s);
8312 s = expr_end;
8313 offset_reloc = BFD_RELOC_MIPS16_JMP;
8314 ip->insn_opcode <<= 16;
8315 continue;
8316
8317 case 'l': /* register list for entry macro */
8318 case 'L': /* register list for exit macro */
8319 {
8320 int mask;
8321
8322 if (c == 'l')
8323 mask = 0;
8324 else
8325 mask = 7 << 3;
8326 while (*s != '\0')
8327 {
8328 int freg, reg1, reg2;
8329
8330 while (*s == ' ' || *s == ',')
8331 ++s;
8332 if (*s != '$')
8333 {
8334 as_bad (_("can't parse register list"));
8335 break;
8336 }
8337 ++s;
8338 if (*s != 'f')
8339 freg = 0;
8340 else
8341 {
8342 freg = 1;
8343 ++s;
8344 }
8345 reg1 = 0;
d9a62219 8346 while (isdigit ((unsigned char) *s))
252b5132
RH
8347 {
8348 reg1 *= 10;
8349 reg1 += *s - '0';
8350 ++s;
8351 }
8352 if (*s == ' ')
8353 ++s;
8354 if (*s != '-')
8355 reg2 = reg1;
8356 else
8357 {
8358 ++s;
8359 if (*s != '$')
8360 break;
8361 ++s;
8362 if (freg)
8363 {
8364 if (*s == 'f')
8365 ++s;
8366 else
8367 {
8368 as_bad (_("invalid register list"));
8369 break;
8370 }
8371 }
8372 reg2 = 0;
d9a62219 8373 while (isdigit ((unsigned char) *s))
252b5132
RH
8374 {
8375 reg2 *= 10;
8376 reg2 += *s - '0';
8377 ++s;
8378 }
8379 }
8380 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
8381 {
8382 mask &= ~ (7 << 3);
8383 mask |= 5 << 3;
8384 }
8385 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
8386 {
8387 mask &= ~ (7 << 3);
8388 mask |= 6 << 3;
8389 }
8390 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
8391 mask |= (reg2 - 3) << 3;
8392 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
8393 mask |= (reg2 - 15) << 1;
8394 else if (reg1 == 31 && reg2 == 31)
8395 mask |= 1;
8396 else
8397 {
8398 as_bad (_("invalid register list"));
8399 break;
8400 }
8401 }
8402 /* The mask is filled in in the opcode table for the
8403 benefit of the disassembler. We remove it before
8404 applying the actual mask. */
8405 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
8406 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
8407 }
8408 continue;
8409
8410 case 'e': /* extend code */
8411 my_getExpression (&imm_expr, s);
8412 check_absolute_expr (ip, &imm_expr);
8413 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
8414 {
8415 as_warn (_("Invalid value for `%s' (%lu)"),
8416 ip->insn_mo->name,
8417 (unsigned long) imm_expr.X_add_number);
8418 imm_expr.X_add_number &= 0x7ff;
8419 }
8420 ip->insn_opcode |= imm_expr.X_add_number;
8421 imm_expr.X_op = O_absent;
8422 s = expr_end;
8423 continue;
8424
8425 default:
8426 internalError ();
8427 }
8428 break;
8429 }
8430
8431 /* Args don't match. */
8432 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
8433 strcmp (insn->name, insn[1].name) == 0)
8434 {
8435 ++insn;
8436 s = argsstart;
8437 continue;
8438 }
8439
8440 insn_error = _("illegal operands");
8441
8442 return;
8443 }
8444}
8445
8446/* This structure holds information we know about a mips16 immediate
8447 argument type. */
8448
e972090a
NC
8449struct mips16_immed_operand
8450{
252b5132
RH
8451 /* The type code used in the argument string in the opcode table. */
8452 int type;
8453 /* The number of bits in the short form of the opcode. */
8454 int nbits;
8455 /* The number of bits in the extended form of the opcode. */
8456 int extbits;
8457 /* The amount by which the short form is shifted when it is used;
8458 for example, the sw instruction has a shift count of 2. */
8459 int shift;
8460 /* The amount by which the short form is shifted when it is stored
8461 into the instruction code. */
8462 int op_shift;
8463 /* Non-zero if the short form is unsigned. */
8464 int unsp;
8465 /* Non-zero if the extended form is unsigned. */
8466 int extu;
8467 /* Non-zero if the value is PC relative. */
8468 int pcrel;
8469};
8470
8471/* The mips16 immediate operand types. */
8472
8473static const struct mips16_immed_operand mips16_immed_operands[] =
8474{
8475 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
8476 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
8477 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
8478 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
8479 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
8480 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
8481 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
8482 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
8483 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
8484 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
8485 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
8486 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
8487 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
8488 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
8489 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
8490 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
8491 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
8492 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
8493 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
8494 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
8495 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
8496};
8497
8498#define MIPS16_NUM_IMMED \
8499 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
8500
8501/* Handle a mips16 instruction with an immediate value. This or's the
8502 small immediate value into *INSN. It sets *USE_EXTEND to indicate
8503 whether an extended value is needed; if one is needed, it sets
8504 *EXTEND to the value. The argument type is TYPE. The value is VAL.
8505 If SMALL is true, an unextended opcode was explicitly requested.
8506 If EXT is true, an extended opcode was explicitly requested. If
8507 WARN is true, warn if EXT does not match reality. */
8508
8509static void
8510mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
8511 extend)
8512 char *file;
8513 unsigned int line;
8514 int type;
8515 offsetT val;
8516 boolean warn;
8517 boolean small;
8518 boolean ext;
8519 unsigned long *insn;
8520 boolean *use_extend;
8521 unsigned short *extend;
8522{
8523 register const struct mips16_immed_operand *op;
8524 int mintiny, maxtiny;
8525 boolean needext;
8526
8527 op = mips16_immed_operands;
8528 while (op->type != type)
8529 {
8530 ++op;
8531 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
8532 }
8533
8534 if (op->unsp)
8535 {
8536 if (type == '<' || type == '>' || type == '[' || type == ']')
8537 {
8538 mintiny = 1;
8539 maxtiny = 1 << op->nbits;
8540 }
8541 else
8542 {
8543 mintiny = 0;
8544 maxtiny = (1 << op->nbits) - 1;
8545 }
8546 }
8547 else
8548 {
8549 mintiny = - (1 << (op->nbits - 1));
8550 maxtiny = (1 << (op->nbits - 1)) - 1;
8551 }
8552
8553 /* Branch offsets have an implicit 0 in the lowest bit. */
8554 if (type == 'p' || type == 'q')
8555 val /= 2;
8556
8557 if ((val & ((1 << op->shift) - 1)) != 0
8558 || val < (mintiny << op->shift)
8559 || val > (maxtiny << op->shift))
8560 needext = true;
8561 else
8562 needext = false;
8563
8564 if (warn && ext && ! needext)
beae10d5
KH
8565 as_warn_where (file, line,
8566 _("extended operand requested but not required"));
252b5132
RH
8567 if (small && needext)
8568 as_bad_where (file, line, _("invalid unextended operand value"));
8569
8570 if (small || (! ext && ! needext))
8571 {
8572 int insnval;
8573
8574 *use_extend = false;
8575 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
8576 insnval <<= op->op_shift;
8577 *insn |= insnval;
8578 }
8579 else
8580 {
8581 long minext, maxext;
8582 int extval;
8583
8584 if (op->extu)
8585 {
8586 minext = 0;
8587 maxext = (1 << op->extbits) - 1;
8588 }
8589 else
8590 {
8591 minext = - (1 << (op->extbits - 1));
8592 maxext = (1 << (op->extbits - 1)) - 1;
8593 }
8594 if (val < minext || val > maxext)
8595 as_bad_where (file, line,
8596 _("operand value out of range for instruction"));
8597
8598 *use_extend = true;
8599 if (op->extbits == 16)
8600 {
8601 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
8602 val &= 0x1f;
8603 }
8604 else if (op->extbits == 15)
8605 {
8606 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
8607 val &= 0xf;
8608 }
8609 else
8610 {
8611 extval = ((val & 0x1f) << 6) | (val & 0x20);
8612 val = 0;
8613 }
8614
8615 *extend = (unsigned short) extval;
8616 *insn |= val;
8617 }
8618}
8619\f
252b5132
RH
8620
8621static int
8622my_getSmallExpression (ep, str)
8623 expressionS *ep;
8624 char *str;
8625{
8626 char *sp;
fb1b3232
TS
8627 char *oldstr = str;
8628 int c = S_EX_NONE;
252b5132
RH
8629
8630 if (*str == ' ')
8631 str++;
fb1b3232
TS
8632 if (*str == '(')
8633 c = S_EX_NONE;
8634 else if (str[0] == '%'
8635 && tolower(str[1]) == 'l'
8636 && tolower(str[2]) == 'o'
8637 && str[3] == '(')
252b5132 8638 {
fb1b3232
TS
8639 c = S_EX_LO;
8640 str += sizeof ("%lo(") - 2;
8641 }
8642 else if (str[0] == '%'
8643 && tolower(str[1]) == 'h'
8644 && tolower(str[2]) == 'i'
8645 && str[3] == '(')
8646 {
8647 c = S_EX_HI;
8648 str += sizeof ("%hi(") - 2;
8649 }
8650 else if (str[0] == '%'
8651 && tolower(str[1]) == 'h'
8652 && tolower(str[2]) == 'i'
8653 && tolower(str[3]) == 'g'
8654 && tolower(str[4]) == 'h'
8655 && tolower(str[5]) == 'e'
8656 && tolower(str[6]) == 'r'
8657 && str[7] == '(')
8658 {
8659 c = S_EX_HIGHER;
8660 str += sizeof ("%higher(") - 2;
8661 }
8662 else if (str[0] == '%'
8663 && tolower(str[1]) == 'h'
8664 && tolower(str[2]) == 'i'
8665 && tolower(str[3]) == 'g'
8666 && tolower(str[4]) == 'h'
8667 && tolower(str[5]) == 'e'
8668 && tolower(str[6]) == 's'
8669 && tolower(str[7]) == 't'
8670 && str[8] == '(')
8671 {
8672 c = S_EX_HIGHEST;
8673 str += sizeof ("%highest(") - 2;
8674 }
8675/* currently unsupported */
8676#if 0
8677 else if (str[0] == '%'
8678 && tolower(str[1]) == 'g'
8679 && tolower(str[2]) == 'p'
8680 && tolower(str[3]) == 'r'
8681 && tolower(str[4]) == 'e'
8682 && tolower(str[5]) == 'l'
8683 && str[6] == '(')
8684 {
8685 c = S_EX_GPREL;
8686 str += sizeof ("%gprel(") - 2;
8687 }
8688 else if (str[0] == '%'
8689 && tolower(str[1]) == 'n'
8690 && tolower(str[2]) == 'e'
8691 && tolower(str[3]) == 'g'
8692 && str[4] == '(')
8693 {
8694 c = S_EX_NEG;
8695 str += sizeof ("%neg(") - 2;
8696 }
8697#endif
8698 else
8699 {
8700 my_getExpression (ep, str);
8701 return c;
8702 }
252b5132 8703
fb1b3232
TS
8704 /*
8705 * A small expression may be followed by a base register.
8706 * Scan to the end of this operand, and then back over a possible
8707 * base register. Then scan the small expression up to that
8708 * point. (Based on code in sparc.c...)
8709 */
8710 for (sp = str; *sp && *sp != ','; sp++)
8711 ;
8712 if (sp - 4 >= str && sp[-1] == ')')
8713 {
8714 if (isdigit ((unsigned char) sp[-2]))
252b5132 8715 {
fb1b3232
TS
8716 for (sp -= 3; sp >= str && isdigit ((unsigned char) *sp); sp--)
8717 ;
8718 if (*sp == '$' && sp > str && sp[-1] == '(')
252b5132 8719 {
fb1b3232
TS
8720 sp--;
8721 goto do_it;
252b5132 8722 }
fb1b3232
TS
8723 }
8724 else if (sp - 5 >= str
8725 && sp[-5] == '('
8726 && sp[-4] == '$'
8727 && ((sp[-3] == 'f' && sp[-2] == 'p')
8728 || (sp[-3] == 's' && sp[-2] == 'p')
8729 || (sp[-3] == 'g' && sp[-2] == 'p')
8730 || (sp[-3] == 'a' && sp[-2] == 't')))
8731 {
8732 sp -= 5;
8733 do_it:
8734 if (sp == str)
252b5132 8735 {
fb1b3232
TS
8736 /* no expression means zero offset */
8737 if (c != S_EX_NONE)
252b5132 8738 {
fb1b3232
TS
8739 /* %xx(reg) is an error */
8740 ep->X_op = O_absent;
8741 expr_end = oldstr;
252b5132
RH
8742 }
8743 else
8744 {
fb1b3232
TS
8745 ep->X_op = O_constant;
8746 expr_end = sp;
252b5132 8747 }
fb1b3232
TS
8748 ep->X_add_symbol = NULL;
8749 ep->X_op_symbol = NULL;
8750 ep->X_add_number = 0;
252b5132 8751 }
fb1b3232
TS
8752 else
8753 {
8754 *sp = '\0';
8755 my_getExpression (ep, str);
8756 *sp = '(';
8757 }
8758 return c;
252b5132
RH
8759 }
8760 }
8761 my_getExpression (ep, str);
fb1b3232
TS
8762
8763 /* => %highest, %higher, %hi, %lo, %gprel, %neg encountered */
8764 return c;
252b5132
RH
8765}
8766
8767static void
8768my_getExpression (ep, str)
8769 expressionS *ep;
8770 char *str;
8771{
8772 char *save_in;
98aa84af 8773 valueT val;
252b5132
RH
8774
8775 save_in = input_line_pointer;
8776 input_line_pointer = str;
8777 expression (ep);
8778 expr_end = input_line_pointer;
8779 input_line_pointer = save_in;
8780
8781 /* If we are in mips16 mode, and this is an expression based on `.',
8782 then we bump the value of the symbol by 1 since that is how other
8783 text symbols are handled. We don't bother to handle complex
8784 expressions, just `.' plus or minus a constant. */
8785 if (mips_opts.mips16
8786 && ep->X_op == O_symbol
8787 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
8788 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
8789 && symbol_get_frag (ep->X_add_symbol) == frag_now
8790 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
8791 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
8792 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
8793}
8794
8795/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
8796 of type TYPE, and store the appropriate bytes in *LITP. The number
8797 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
8798 returned, or NULL on OK. */
8799
8800char *
8801md_atof (type, litP, sizeP)
8802 int type;
8803 char *litP;
8804 int *sizeP;
8805{
8806 int prec;
8807 LITTLENUM_TYPE words[4];
8808 char *t;
8809 int i;
8810
8811 switch (type)
8812 {
8813 case 'f':
8814 prec = 2;
8815 break;
8816
8817 case 'd':
8818 prec = 4;
8819 break;
8820
8821 default:
8822 *sizeP = 0;
8823 return _("bad call to md_atof");
8824 }
8825
8826 t = atof_ieee (input_line_pointer, type, words);
8827 if (t)
8828 input_line_pointer = t;
8829
8830 *sizeP = prec * 2;
8831
8832 if (! target_big_endian)
8833 {
8834 for (i = prec - 1; i >= 0; i--)
8835 {
8836 md_number_to_chars (litP, (valueT) words[i], 2);
8837 litP += 2;
8838 }
8839 }
8840 else
8841 {
8842 for (i = 0; i < prec; i++)
8843 {
8844 md_number_to_chars (litP, (valueT) words[i], 2);
8845 litP += 2;
8846 }
8847 }
bdaaa2e1 8848
252b5132
RH
8849 return NULL;
8850}
8851
8852void
8853md_number_to_chars (buf, val, n)
8854 char *buf;
8855 valueT val;
8856 int n;
8857{
8858 if (target_big_endian)
8859 number_to_chars_bigendian (buf, val, n);
8860 else
8861 number_to_chars_littleendian (buf, val, n);
8862}
8863\f
39c0a331 8864CONST char *md_shortopts = "nO::g::G:";
252b5132 8865
e972090a
NC
8866struct option md_longopts[] =
8867{
252b5132
RH
8868#define OPTION_MIPS1 (OPTION_MD_BASE + 1)
8869 {"mips0", no_argument, NULL, OPTION_MIPS1},
8870 {"mips1", no_argument, NULL, OPTION_MIPS1},
8871#define OPTION_MIPS2 (OPTION_MD_BASE + 2)
8872 {"mips2", no_argument, NULL, OPTION_MIPS2},
8873#define OPTION_MIPS3 (OPTION_MD_BASE + 3)
8874 {"mips3", no_argument, NULL, OPTION_MIPS3},
8875#define OPTION_MIPS4 (OPTION_MD_BASE + 4)
8876 {"mips4", no_argument, NULL, OPTION_MIPS4},
8877#define OPTION_MCPU (OPTION_MD_BASE + 5)
8878 {"mcpu", required_argument, NULL, OPTION_MCPU},
8879#define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 6)
8880 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
156c2f8b 8881#define OPTION_TRAP (OPTION_MD_BASE + 7)
252b5132
RH
8882 {"trap", no_argument, NULL, OPTION_TRAP},
8883 {"no-break", no_argument, NULL, OPTION_TRAP},
156c2f8b 8884#define OPTION_BREAK (OPTION_MD_BASE + 8)
252b5132
RH
8885 {"break", no_argument, NULL, OPTION_BREAK},
8886 {"no-trap", no_argument, NULL, OPTION_BREAK},
156c2f8b 8887#define OPTION_EB (OPTION_MD_BASE + 9)
252b5132 8888 {"EB", no_argument, NULL, OPTION_EB},
156c2f8b 8889#define OPTION_EL (OPTION_MD_BASE + 10)
252b5132 8890 {"EL", no_argument, NULL, OPTION_EL},
156c2f8b 8891#define OPTION_M4650 (OPTION_MD_BASE + 11)
252b5132 8892 {"m4650", no_argument, NULL, OPTION_M4650},
156c2f8b 8893#define OPTION_NO_M4650 (OPTION_MD_BASE + 12)
252b5132 8894 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
156c2f8b 8895#define OPTION_M4010 (OPTION_MD_BASE + 13)
252b5132 8896 {"m4010", no_argument, NULL, OPTION_M4010},
156c2f8b 8897#define OPTION_NO_M4010 (OPTION_MD_BASE + 14)
252b5132 8898 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
156c2f8b 8899#define OPTION_M4100 (OPTION_MD_BASE + 15)
252b5132 8900 {"m4100", no_argument, NULL, OPTION_M4100},
156c2f8b 8901#define OPTION_NO_M4100 (OPTION_MD_BASE + 16)
252b5132 8902 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
156c2f8b 8903#define OPTION_MIPS16 (OPTION_MD_BASE + 17)
252b5132 8904 {"mips16", no_argument, NULL, OPTION_MIPS16},
156c2f8b 8905#define OPTION_NO_MIPS16 (OPTION_MD_BASE + 18)
252b5132 8906 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
156c2f8b 8907#define OPTION_M3900 (OPTION_MD_BASE + 19)
252b5132 8908 {"m3900", no_argument, NULL, OPTION_M3900},
156c2f8b 8909#define OPTION_NO_M3900 (OPTION_MD_BASE + 20)
252b5132 8910 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
156c2f8b 8911#define OPTION_MABI (OPTION_MD_BASE + 21)
252b5132 8912 {"mabi", required_argument, NULL, OPTION_MABI},
156c2f8b 8913#define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 22)
6b76fefe 8914 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
156c2f8b 8915#define OPTION_NO_M7000_HILO_FIX (OPTION_MD_BASE + 23)
6b76fefe 8916 {"no-fix-7000", no_argument, NULL, OPTION_NO_M7000_HILO_FIX},
156c2f8b 8917#define OPTION_GP32 (OPTION_MD_BASE + 24)
c97ef257 8918 {"mgp32", no_argument, NULL, OPTION_GP32},
156c2f8b 8919#define OPTION_GP64 (OPTION_MD_BASE + 25)
c97ef257 8920 {"mgp64", no_argument, NULL, OPTION_GP64},
156c2f8b 8921#define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 26)
119d663a 8922 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
156c2f8b 8923#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 27)
119d663a 8924 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
156c2f8b
NC
8925#define OPTION_MIPS32 (OPTION_MD_BASE + 28)
8926 {"mips32", no_argument, NULL, OPTION_MIPS32},
84ea6cf2
NC
8927#define OPTION_MIPS5 (OPTION_MD_BASE + 29)
8928 {"mips5", no_argument, NULL, OPTION_MIPS5},
8929#define OPTION_MIPS64 (OPTION_MD_BASE + 30)
8930 {"mips64", no_argument, NULL, OPTION_MIPS64},
ec68c924
EC
8931#define OPTION_MARCH (OPTION_MD_BASE + 31)
8932 {"march", required_argument, NULL, OPTION_MARCH},
8933#define OPTION_MTUNE (OPTION_MD_BASE + 32)
8934 {"mtune", required_argument, NULL, OPTION_MTUNE},
ca4e0257
RS
8935#define OPTION_FP32 (OPTION_MD_BASE + 33)
8936 {"mfp32", no_argument, NULL, OPTION_FP32},
156c2f8b
NC
8937#ifdef OBJ_ELF
8938#define OPTION_ELF_BASE (OPTION_MD_BASE + 35)
8939#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
8940#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
8941#define OPTION_XGOT (OPTION_ELF_BASE + 2)
8942#define OPTION_32 (OPTION_ELF_BASE + 3)
8943#define OPTION_64 (OPTION_ELF_BASE + 4)
8944 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
8945 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
8946 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
8947 {"xgot", no_argument, NULL, OPTION_XGOT},
8948 {"32", no_argument, NULL, OPTION_32},
8949 {"64", no_argument, NULL, OPTION_64},
8950#endif
119d663a 8951
252b5132
RH
8952 {NULL, no_argument, NULL, 0}
8953};
156c2f8b 8954size_t md_longopts_size = sizeof (md_longopts);
252b5132
RH
8955
8956int
8957md_parse_option (c, arg)
8958 int c;
8959 char *arg;
8960{
8961 switch (c)
8962 {
119d663a
NC
8963 case OPTION_CONSTRUCT_FLOATS:
8964 mips_disable_float_construction = 0;
8965 break;
bdaaa2e1 8966
119d663a
NC
8967 case OPTION_NO_CONSTRUCT_FLOATS:
8968 mips_disable_float_construction = 1;
8969 break;
bdaaa2e1 8970
252b5132
RH
8971 case OPTION_TRAP:
8972 mips_trap = 1;
8973 break;
8974
8975 case OPTION_BREAK:
8976 mips_trap = 0;
8977 break;
8978
8979 case OPTION_EB:
8980 target_big_endian = 1;
8981 break;
8982
8983 case OPTION_EL:
8984 target_big_endian = 0;
8985 break;
8986
39c0a331
L
8987 case 'n':
8988 warn_nops = 1;
8989 break;
8990
252b5132
RH
8991 case 'O':
8992 if (arg && arg[1] == '0')
8993 mips_optimize = 1;
8994 else
8995 mips_optimize = 2;
8996 break;
8997
8998 case 'g':
8999 if (arg == NULL)
9000 mips_debug = 2;
9001 else
9002 mips_debug = atoi (arg);
9003 /* When the MIPS assembler sees -g or -g2, it does not do
9004 optimizations which limit full symbolic debugging. We take
9005 that to be equivalent to -O0. */
9006 if (mips_debug == 2)
9007 mips_optimize = 1;
9008 break;
9009
9010 case OPTION_MIPS1:
e7af610e 9011 mips_opts.isa = ISA_MIPS1;
252b5132
RH
9012 break;
9013
9014 case OPTION_MIPS2:
e7af610e 9015 mips_opts.isa = ISA_MIPS2;
252b5132
RH
9016 break;
9017
9018 case OPTION_MIPS3:
e7af610e 9019 mips_opts.isa = ISA_MIPS3;
252b5132
RH
9020 break;
9021
9022 case OPTION_MIPS4:
e7af610e
NC
9023 mips_opts.isa = ISA_MIPS4;
9024 break;
9025
84ea6cf2
NC
9026 case OPTION_MIPS5:
9027 mips_opts.isa = ISA_MIPS5;
9028 break;
9029
e7af610e
NC
9030 case OPTION_MIPS32:
9031 mips_opts.isa = ISA_MIPS32;
252b5132
RH
9032 break;
9033
84ea6cf2
NC
9034 case OPTION_MIPS64:
9035 mips_opts.isa = ISA_MIPS64;
9036 break;
9037
ec68c924
EC
9038 case OPTION_MTUNE:
9039 case OPTION_MARCH:
252b5132
RH
9040 case OPTION_MCPU:
9041 {
ec68c924
EC
9042 int cpu = CPU_UNKNOWN;
9043
e7af610e 9044 /* Identify the processor type. */
ec68c924 9045 if (strcasecmp (arg, "default") != 0)
252b5132 9046 {
e7af610e 9047 const struct mips_cpu_info *ci;
252b5132 9048
e7af610e
NC
9049 ci = mips_cpu_info_from_name (arg);
9050 if (ci == NULL || ci->is_isa)
ec68c924
EC
9051 {
9052 switch (c)
9053 {
9054 case OPTION_MTUNE:
9055 as_fatal (_("invalid architecture -mtune=%s"), arg);
9056 break;
9057 case OPTION_MARCH:
9058 as_fatal (_("invalid architecture -march=%s"), arg);
9059 break;
9060 case OPTION_MCPU:
9061 as_fatal (_("invalid architecture -mcpu=%s"), arg);
9062 break;
9063 }
9064 }
e7af610e 9065 else
ec68c924
EC
9066 cpu = ci->cpu;
9067 }
9068
9069 switch (c)
9070 {
9071 case OPTION_MTUNE:
6dce9e24
TS
9072 if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
9073 as_warn(_("A different -mtune= was already specified, is now "
9074 "-mtune=%s"), arg);
ec68c924
EC
9075 mips_tune = cpu;
9076 break;
9077 case OPTION_MARCH:
6dce9e24
TS
9078 if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
9079 as_warn(_("A different -march= was already specified, is now "
9080 "-march=%s"), arg);
ec68c924
EC
9081 mips_arch = cpu;
9082 break;
9083 case OPTION_MCPU:
6dce9e24
TS
9084 if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
9085 as_warn(_("A different -mcpu= was already specified, is now "
9086 "-mcpu=%s"), arg);
ec68c924 9087 mips_cpu = cpu;
252b5132
RH
9088 }
9089 }
9090 break;
9091
9092 case OPTION_M4650:
6dce9e24
TS
9093 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
9094 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
9095 as_warn(_("A different -march= or -mtune= was already specified, "
9096 "is now -m4650"));
ec68c924
EC
9097 mips_arch = CPU_R4650;
9098 mips_tune = CPU_R4650;
252b5132
RH
9099 break;
9100
9101 case OPTION_NO_M4650:
9102 break;
9103
9104 case OPTION_M4010:
6dce9e24
TS
9105 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
9106 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
9107 as_warn(_("A different -march= or -mtune= was already specified, "
9108 "is now -m4010"));
ec68c924
EC
9109 mips_arch = CPU_R4010;
9110 mips_tune = CPU_R4010;
252b5132
RH
9111 break;
9112
9113 case OPTION_NO_M4010:
9114 break;
9115
9116 case OPTION_M4100:
6dce9e24
TS
9117 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
9118 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
9119 as_warn(_("A different -march= or -mtune= was already specified, "
9120 "is now -m4100"));
ec68c924
EC
9121 mips_arch = CPU_VR4100;
9122 mips_tune = CPU_VR4100;
252b5132
RH
9123 break;
9124
9125 case OPTION_NO_M4100:
9126 break;
9127
252b5132 9128 case OPTION_M3900:
6dce9e24
TS
9129 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
9130 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
9131 as_warn(_("A different -march= or -mtune= was already specified, "
9132 "is now -m3900"));
ec68c924
EC
9133 mips_arch = CPU_R3900;
9134 mips_tune = CPU_R3900;
252b5132 9135 break;
bdaaa2e1 9136
252b5132
RH
9137 case OPTION_NO_M3900:
9138 break;
9139
9140 case OPTION_MIPS16:
9141 mips_opts.mips16 = 1;
9142 mips_no_prev_insn (false);
9143 break;
9144
9145 case OPTION_NO_MIPS16:
9146 mips_opts.mips16 = 0;
9147 mips_no_prev_insn (false);
9148 break;
9149
9150 case OPTION_MEMBEDDED_PIC:
9151 mips_pic = EMBEDDED_PIC;
9152 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
9153 {
9154 as_bad (_("-G may not be used with embedded PIC code"));
9155 return 0;
9156 }
9157 g_switch_value = 0x7fffffff;
9158 break;
9159
0f074f60 9160#ifdef OBJ_ELF
252b5132
RH
9161 /* When generating ELF code, we permit -KPIC and -call_shared to
9162 select SVR4_PIC, and -non_shared to select no PIC. This is
9163 intended to be compatible with Irix 5. */
9164 case OPTION_CALL_SHARED:
9165 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9166 {
9167 as_bad (_("-call_shared is supported only for ELF format"));
9168 return 0;
9169 }
9170 mips_pic = SVR4_PIC;
9171 if (g_switch_seen && g_switch_value != 0)
9172 {
9173 as_bad (_("-G may not be used with SVR4 PIC code"));
9174 return 0;
9175 }
9176 g_switch_value = 0;
9177 break;
9178
9179 case OPTION_NON_SHARED:
9180 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9181 {
9182 as_bad (_("-non_shared is supported only for ELF format"));
9183 return 0;
9184 }
9185 mips_pic = NO_PIC;
9186 break;
9187
9188 /* The -xgot option tells the assembler to use 32 offsets when
9189 accessing the got in SVR4_PIC mode. It is for Irix
9190 compatibility. */
9191 case OPTION_XGOT:
9192 mips_big_got = 1;
9193 break;
0f074f60 9194#endif /* OBJ_ELF */
252b5132
RH
9195
9196 case 'G':
9197 if (! USE_GLOBAL_POINTER_OPT)
9198 {
9199 as_bad (_("-G is not supported for this configuration"));
9200 return 0;
9201 }
9202 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
9203 {
9204 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
9205 return 0;
9206 }
9207 else
9208 g_switch_value = atoi (arg);
9209 g_switch_seen = 1;
9210 break;
9211
0f074f60 9212#ifdef OBJ_ELF
252b5132
RH
9213 /* The -32 and -64 options tell the assembler to output the 32
9214 bit or the 64 bit MIPS ELF format. */
9215 case OPTION_32:
9216 mips_64 = 0;
9217 break;
9218
9219 case OPTION_64:
9220 {
9221 const char **list, **l;
9222
9223 list = bfd_target_list ();
9224 for (l = list; *l != NULL; l++)
a6bacc80
TS
9225#ifdef TE_TMIPS
9226 /* This is traditional mips */
9227 if (strcmp (*l, "elf64-tradbigmips") == 0
f4ddc5e6 9228 || strcmp (*l, "elf64-tradlittlemips") == 0)
a6bacc80
TS
9229#else
9230 if (strcmp (*l, "elf64-bigmips") == 0
9231 || strcmp (*l, "elf64-littlemips") == 0)
9232#endif
252b5132
RH
9233 break;
9234 if (*l == NULL)
9235 as_fatal (_("No compiled in support for 64 bit object file format"));
9236 free (list);
9237 mips_64 = 1;
9238 }
9239 break;
0f074f60 9240#endif /* OBJ_ELF */
252b5132 9241
c97ef257
AH
9242 case OPTION_GP32:
9243 mips_gp32 = 1;
9244 mips_64 = 0;
9245
9246 /* We deliberately don't allow "-gp32" to set the MIPS_32BITMODE
9247 flag in object files because to do so would make it
9248 impossible to link with libraries compiled without "-gp32".
bdaaa2e1 9249 This is unnecessarily restrictive.
c97ef257
AH
9250
9251 We could solve this problem by adding "-gp32" multilibs to
9252 gcc, but to set this flag before gcc is built with such
bdaaa2e1 9253 multilibs will break too many systems. */
c97ef257 9254
beae10d5
KH
9255#if 0
9256 mips_32bitmode = 1;
9257#endif
c97ef257
AH
9258 break;
9259
9260 case OPTION_GP64:
9261 mips_gp32 = 0;
9262 mips_64 = 1;
beae10d5
KH
9263#if 0
9264 mips_32bitmode = 0;
9265#endif
c97ef257 9266 break;
252b5132 9267
ca4e0257
RS
9268 case OPTION_FP32:
9269 mips_fp32 = 1;
9270 break;
9271
252b5132 9272 case OPTION_MABI:
beae10d5
KH
9273 if (strcmp (arg, "32") == 0
9274 || strcmp (arg, "n32") == 0
9275 || strcmp (arg, "64") == 0
9276 || strcmp (arg, "o64") == 0
9277 || strcmp (arg, "eabi") == 0)
ca4e0257
RS
9278 {
9279 mips_abi_string = arg;
9280 mips_32bit_abi = (strcmp (arg, "32") == 0);
9281 }
252b5132
RH
9282 break;
9283
6b76fefe
CM
9284 case OPTION_M7000_HILO_FIX:
9285 mips_7000_hilo_fix = true;
9286 break;
9287
9288 case OPTION_NO_M7000_HILO_FIX:
9289 mips_7000_hilo_fix = false;
9290 break;
9291
252b5132
RH
9292 default:
9293 return 0;
9294 }
9295
9296 return 1;
9297}
9298
252b5132
RH
9299static void
9300show (stream, string, col_p, first_p)
9301 FILE *stream;
9302 char *string;
9303 int *col_p;
9304 int *first_p;
9305{
9306 if (*first_p)
9307 {
9308 fprintf (stream, "%24s", "");
9309 *col_p = 24;
9310 }
9311 else
9312 {
9313 fprintf (stream, ", ");
9314 *col_p += 2;
9315 }
9316
9317 if (*col_p + strlen (string) > 72)
9318 {
9319 fprintf (stream, "\n%24s", "");
9320 *col_p = 24;
9321 }
9322
9323 fprintf (stream, "%s", string);
9324 *col_p += strlen (string);
9325
9326 *first_p = 0;
9327}
9328
252b5132
RH
9329void
9330md_show_usage (stream)
9331 FILE *stream;
9332{
9333 int column, first;
9334
beae10d5 9335 fprintf (stream, _("\
252b5132
RH
9336MIPS options:\n\
9337-membedded-pic generate embedded position independent code\n\
9338-EB generate big endian output\n\
9339-EL generate little endian output\n\
9a41af64 9340-g, -g2 do not remove unneeded NOPs or swap branches\n\
252b5132
RH
9341-G NUM allow referencing objects up to NUM bytes\n\
9342 implicitly with the gp register [default 8]\n"));
beae10d5 9343 fprintf (stream, _("\
252b5132
RH
9344-mips1 generate MIPS ISA I instructions\n\
9345-mips2 generate MIPS ISA II instructions\n\
9346-mips3 generate MIPS ISA III instructions\n\
9347-mips4 generate MIPS ISA IV instructions\n\
84ea6cf2 9348-mips5 generate MIPS ISA V instructions\n\
e7af610e 9349-mips32 generate MIPS32 ISA instructions\n\
84ea6cf2 9350-mips64 generate MIPS64 ISA instructions\n\
ec68c924 9351-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
252b5132
RH
9352
9353 first = 1;
9354
9355 show (stream, "2000", &column, &first);
9356 show (stream, "3000", &column, &first);
9357 show (stream, "3900", &column, &first);
9358 show (stream, "4000", &column, &first);
9359 show (stream, "4010", &column, &first);
9360 show (stream, "4100", &column, &first);
9361 show (stream, "4111", &column, &first);
9362 show (stream, "4300", &column, &first);
9363 show (stream, "4400", &column, &first);
9364 show (stream, "4600", &column, &first);
9365 show (stream, "4650", &column, &first);
9366 show (stream, "5000", &column, &first);
18ae5d72
EC
9367 show (stream, "5200", &column, &first);
9368 show (stream, "5230", &column, &first);
9369 show (stream, "5231", &column, &first);
9370 show (stream, "5261", &column, &first);
9371 show (stream, "5721", &column, &first);
252b5132
RH
9372 show (stream, "6000", &column, &first);
9373 show (stream, "8000", &column, &first);
9374 show (stream, "10000", &column, &first);
d1cf510e 9375 show (stream, "12000", &column, &first);
e7af610e 9376 show (stream, "mips32-4k", &column, &first);
c6c98b38 9377 show (stream, "sb-1", &column, &first);
252b5132
RH
9378 fputc ('\n', stream);
9379
9380 fprintf (stream, _("\
ec68c924 9381-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
252b5132
RH
9382-no-mCPU don't generate code specific to CPU.\n\
9383 For -mCPU and -no-mCPU, CPU must be one of:\n"));
9384
9385 first = 1;
9386
9387 show (stream, "3900", &column, &first);
9388 show (stream, "4010", &column, &first);
9389 show (stream, "4100", &column, &first);
9390 show (stream, "4650", &column, &first);
9391 fputc ('\n', stream);
9392
beae10d5 9393 fprintf (stream, _("\
252b5132
RH
9394-mips16 generate mips16 instructions\n\
9395-no-mips16 do not generate mips16 instructions\n"));
beae10d5 9396 fprintf (stream, _("\
ca4e0257
RS
9397-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
9398-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
252b5132
RH
9399-O0 remove unneeded NOPs, do not swap branches\n\
9400-O remove unneeded NOPs and swap branches\n\
63486801 9401-n warn about NOPs generated from macros\n\
119d663a 9402--[no-]construct-floats [dis]allow floating point values to be constructed\n\
252b5132
RH
9403--trap, --no-break trap exception on div by 0 and mult overflow\n\
9404--break, --no-trap break exception on div by 0 and mult overflow\n"));
9405#ifdef OBJ_ELF
beae10d5 9406 fprintf (stream, _("\
252b5132
RH
9407-KPIC, -call_shared generate SVR4 position independent code\n\
9408-non_shared do not generate position independent code\n\
9409-xgot assume a 32 bit GOT\n\
9410-32 create 32 bit object file (default)\n\
9411-64 create 64 bit object file\n"));
9412#endif
9413}
9414\f
9415void
9416mips_init_after_args ()
9417{
9418 /* initialize opcodes */
9419 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 9420 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
9421}
9422
9423long
9424md_pcrel_from (fixP)
9425 fixS *fixP;
9426{
9427 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
9428 && fixP->fx_addsy != (symbolS *) NULL
9429 && ! S_IS_DEFINED (fixP->fx_addsy))
9430 {
9431 /* This makes a branch to an undefined symbol be a branch to the
9432 current location. */
9433 return 4;
9434 }
9435
9436 /* return the address of the delay slot */
9437 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
9438}
9439
252b5132
RH
9440/* This is called before the symbol table is processed. In order to
9441 work with gcc when using mips-tfile, we must keep all local labels.
9442 However, in other cases, we want to discard them. If we were
9443 called with -g, but we didn't see any debugging information, it may
9444 mean that gcc is smuggling debugging information through to
9445 mips-tfile, in which case we must generate all local labels. */
9446
9447void
9448mips_frob_file_before_adjust ()
9449{
9450#ifndef NO_ECOFF_DEBUGGING
9451 if (ECOFF_DEBUGGING
9452 && mips_debug != 0
9453 && ! ecoff_debugging_seen)
9454 flag_keep_locals = 1;
9455#endif
9456}
9457
9458/* Sort any unmatched HI16_S relocs so that they immediately precede
9459 the corresponding LO reloc. This is called before md_apply_fix and
9460 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
9461 explicit use of the %hi modifier. */
9462
9463void
9464mips_frob_file ()
9465{
9466 struct mips_hi_fixup *l;
9467
9468 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
9469 {
9470 segment_info_type *seginfo;
9471 int pass;
9472
9473 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
9474
9475 /* Check quickly whether the next fixup happens to be a matching
9476 %lo. */
9477 if (l->fixp->fx_next != NULL
9478 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
9479 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
9480 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
9481 continue;
9482
9483 /* Look through the fixups for this segment for a matching %lo.
9484 When we find one, move the %hi just in front of it. We do
9485 this in two passes. In the first pass, we try to find a
9486 unique %lo. In the second pass, we permit multiple %hi
9487 relocs for a single %lo (this is a GNU extension). */
9488 seginfo = seg_info (l->seg);
9489 for (pass = 0; pass < 2; pass++)
9490 {
9491 fixS *f, *prev;
9492
9493 prev = NULL;
9494 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
9495 {
9496 /* Check whether this is a %lo fixup which matches l->fixp. */
9497 if (f->fx_r_type == BFD_RELOC_LO16
9498 && f->fx_addsy == l->fixp->fx_addsy
9499 && f->fx_offset == l->fixp->fx_offset
9500 && (pass == 1
9501 || prev == NULL
9502 || prev->fx_r_type != BFD_RELOC_HI16_S
9503 || prev->fx_addsy != f->fx_addsy
9504 || prev->fx_offset != f->fx_offset))
9505 {
9506 fixS **pf;
9507
9508 /* Move l->fixp before f. */
9509 for (pf = &seginfo->fix_root;
9510 *pf != l->fixp;
9511 pf = &(*pf)->fx_next)
9512 assert (*pf != NULL);
9513
9514 *pf = l->fixp->fx_next;
9515
9516 l->fixp->fx_next = f;
9517 if (prev == NULL)
9518 seginfo->fix_root = l->fixp;
9519 else
9520 prev->fx_next = l->fixp;
9521
9522 break;
9523 }
9524
9525 prev = f;
9526 }
9527
9528 if (f != NULL)
9529 break;
9530
9531#if 0 /* GCC code motion plus incomplete dead code elimination
9532 can leave a %hi without a %lo. */
9533 if (pass == 1)
9534 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
9535 _("Unmatched %%hi reloc"));
9536#endif
9537 }
9538 }
9539}
9540
9541/* When generating embedded PIC code we need to use a special
9542 relocation to represent the difference of two symbols in the .text
9543 section (switch tables use a difference of this sort). See
9544 include/coff/mips.h for details. This macro checks whether this
9545 fixup requires the special reloc. */
9546#define SWITCH_TABLE(fixp) \
9547 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 9548 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
9549 && (fixp)->fx_addsy != NULL \
9550 && (fixp)->fx_subsy != NULL \
9551 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
9552 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
9553
9554/* When generating embedded PIC code we must keep all PC relative
9555 relocations, in case the linker has to relax a call. We also need
9556 to keep relocations for switch table entries. */
9557
252b5132
RH
9558int
9559mips_force_relocation (fixp)
9560 fixS *fixp;
9561{
9562 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
9563 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
9564 return 1;
9565
9566 return (mips_pic == EMBEDDED_PIC
9567 && (fixp->fx_pcrel
9568 || SWITCH_TABLE (fixp)
9569 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
9570 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
9571}
9572
9573/* Apply a fixup to the object file. */
9574
9575int
9576md_apply_fix (fixP, valueP)
9577 fixS *fixP;
9578 valueT *valueP;
9579{
9580 unsigned char *buf;
98aa84af
AM
9581 long insn;
9582 valueT value;
252b5132
RH
9583
9584 assert (fixP->fx_size == 4
9585 || fixP->fx_r_type == BFD_RELOC_16
9586 || fixP->fx_r_type == BFD_RELOC_64
9587 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
9588 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
9589
9590 value = *valueP;
9591
9592 /* If we aren't adjusting this fixup to be against the section
9593 symbol, we need to adjust the value. */
9594#ifdef OBJ_ELF
9595 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 9596 {
98aa84af 9597 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16
ea4ff978
L
9598 || ((S_IS_WEAK (fixP->fx_addsy)
9599 || S_IS_EXTERN (fixP->fx_addsy))
9600 && !S_IS_COMMON (fixP->fx_addsy))
98aa84af
AM
9601 || (symbol_used_in_reloc_p (fixP->fx_addsy)
9602 && (((bfd_get_section_flags (stdoutput,
9603 S_GET_SEGMENT (fixP->fx_addsy))
9604 & SEC_LINK_ONCE) != 0)
9605 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
9606 ".gnu.linkonce",
9607 sizeof (".gnu.linkonce") - 1))))
252b5132 9608
98aa84af
AM
9609 {
9610 valueT symval = S_GET_VALUE (fixP->fx_addsy);
9611 value -= symval;
236bd878
RS
9612 if (value != 0
9613 && ! fixP->fx_pcrel
9614 && fixP->fx_r_type != BFD_RELOC_MIPS_GPREL)
98aa84af
AM
9615 {
9616 /* In this case, the bfd_install_relocation routine will
9617 incorrectly add the symbol value back in. We just want
7461da6e 9618 the addend to appear in the object file. */
98aa84af 9619 value -= symval;
7461da6e
RS
9620
9621 /* Make sure the addend is still non-zero. If it became zero
9622 after the last operation, set it to a spurious value and
9623 subtract the same value from the object file's contents. */
9624 if (value == 0)
9625 {
9626 value = 8;
9627
9628 /* The in-place addends for LO16 relocations are signed;
9629 leave the matching HI16 in-place addends as zero. */
9630 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
9631 {
9632 reloc_howto_type *howto;
9633 bfd_vma contents, mask, field;
9634
9635 howto = bfd_reloc_type_lookup (stdoutput,
9636 fixP->fx_r_type);
9637
9638 contents = bfd_get_bits (fixP->fx_frag->fr_literal
9639 + fixP->fx_where,
9640 fixP->fx_size * 8,
9641 target_big_endian);
9642
9643 /* MASK has bits set where the relocation should go.
9644 FIELD is -value, shifted into the appropriate place
9645 for this relocation. */
9646 mask = 1 << (howto->bitsize - 1);
9647 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
9648 field = (-value >> howto->rightshift) << howto->bitpos;
9649
9650 bfd_put_bits ((field & mask) | (contents & ~mask),
9651 fixP->fx_frag->fr_literal + fixP->fx_where,
9652 fixP->fx_size * 8,
9653 target_big_endian);
9654 }
9655 }
98aa84af
AM
9656 }
9657 }
252b5132 9658
bb2d6cd7
GK
9659 /* This code was generated using trial and error and so is
9660 fragile and not trustworthy. If you change it, you should
9661 rerun the elf-rel, elf-rel2, and empic testcases and ensure
9662 they still pass. */
9663 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
9664 {
9665 value += fixP->fx_frag->fr_address + fixP->fx_where;
9666
9667 /* BFD's REL handling, for MIPS, is _very_ weird.
9668 This gives the right results, but it can't possibly
9669 be the way things are supposed to work. */
9670 if (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
9671 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
9672 value += fixP->fx_frag->fr_address + fixP->fx_where;
9673 }
9674 }
9675#endif
252b5132
RH
9676
9677 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc */
9678
9679 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
9680 fixP->fx_done = 1;
9681
9682 switch (fixP->fx_r_type)
9683 {
9684 case BFD_RELOC_MIPS_JMP:
9685 case BFD_RELOC_HI16:
9686 case BFD_RELOC_HI16_S:
9687 case BFD_RELOC_MIPS_GPREL:
9688 case BFD_RELOC_MIPS_LITERAL:
9689 case BFD_RELOC_MIPS_CALL16:
9690 case BFD_RELOC_MIPS_GOT16:
9691 case BFD_RELOC_MIPS_GPREL32:
9692 case BFD_RELOC_MIPS_GOT_HI16:
9693 case BFD_RELOC_MIPS_GOT_LO16:
9694 case BFD_RELOC_MIPS_CALL_HI16:
9695 case BFD_RELOC_MIPS_CALL_LO16:
9696 case BFD_RELOC_MIPS16_GPREL:
9697 if (fixP->fx_pcrel)
9698 as_bad_where (fixP->fx_file, fixP->fx_line,
9699 _("Invalid PC relative reloc"));
9700 /* Nothing needed to do. The value comes from the reloc entry */
9701 break;
9702
9703 case BFD_RELOC_MIPS16_JMP:
9704 /* We currently always generate a reloc against a symbol, which
9705 means that we don't want an addend even if the symbol is
9706 defined. */
9707 fixP->fx_addnumber = 0;
9708 break;
9709
9710 case BFD_RELOC_PCREL_HI16_S:
9711 /* The addend for this is tricky if it is internal, so we just
9712 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 9713 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
9714 && !fixP->fx_done
9715 && value != 0)
9716 break;
9717 if (fixP->fx_addsy
9718 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
9719 {
9720 /* For an external symbol adjust by the address to make it
9721 pcrel_offset. We use the address of the RELLO reloc
9722 which follows this one. */
9723 value += (fixP->fx_next->fx_frag->fr_address
9724 + fixP->fx_next->fx_where);
9725 }
9726 if (value & 0x8000)
9727 value += 0x10000;
9728 value >>= 16;
9729 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9730 if (target_big_endian)
9731 buf += 2;
9732 md_number_to_chars (buf, value, 2);
9733 break;
9734
9735 case BFD_RELOC_PCREL_LO16:
9736 /* The addend for this is tricky if it is internal, so we just
9737 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 9738 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
9739 && !fixP->fx_done
9740 && value != 0)
9741 break;
9742 if (fixP->fx_addsy
9743 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
9744 value += fixP->fx_frag->fr_address + fixP->fx_where;
9745 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9746 if (target_big_endian)
9747 buf += 2;
9748 md_number_to_chars (buf, value, 2);
9749 break;
9750
9751 case BFD_RELOC_64:
9752 /* This is handled like BFD_RELOC_32, but we output a sign
9753 extended value if we are only 32 bits. */
9754 if (fixP->fx_done
9755 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
9756 {
9757 if (8 <= sizeof (valueT))
9758 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9759 value, 8);
9760 else
9761 {
9762 long w1, w2;
9763 long hiv;
9764
9765 w1 = w2 = fixP->fx_where;
9766 if (target_big_endian)
9767 w1 += 4;
9768 else
9769 w2 += 4;
9770 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
9771 if ((value & 0x80000000) != 0)
9772 hiv = 0xffffffff;
9773 else
9774 hiv = 0;
9775 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
9776 }
9777 }
9778 break;
9779
056350c6 9780 case BFD_RELOC_RVA:
252b5132
RH
9781 case BFD_RELOC_32:
9782 /* If we are deleting this reloc entry, we must fill in the
9783 value now. This can happen if we have a .word which is not
9784 resolved when it appears but is later defined. We also need
9785 to fill in the value if this is an embedded PIC switch table
9786 entry. */
9787 if (fixP->fx_done
9788 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
9789 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9790 value, 4);
9791 break;
9792
9793 case BFD_RELOC_16:
9794 /* If we are deleting this reloc entry, we must fill in the
9795 value now. */
9796 assert (fixP->fx_size == 2);
9797 if (fixP->fx_done)
9798 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9799 value, 2);
9800 break;
9801
9802 case BFD_RELOC_LO16:
9803 /* When handling an embedded PIC switch statement, we can wind
9804 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
9805 if (fixP->fx_done)
9806 {
98aa84af 9807 if (value + 0x8000 > 0xffff)
252b5132
RH
9808 as_bad_where (fixP->fx_file, fixP->fx_line,
9809 _("relocation overflow"));
9810 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9811 if (target_big_endian)
9812 buf += 2;
9813 md_number_to_chars (buf, value, 2);
9814 }
9815 break;
9816
9817 case BFD_RELOC_16_PCREL_S2:
9818 /*
9819 * We need to save the bits in the instruction since fixup_segment()
9820 * might be deleting the relocation entry (i.e., a branch within
9821 * the current segment).
9822 */
9823 if ((value & 0x3) != 0)
9824 as_bad_where (fixP->fx_file, fixP->fx_line,
98aa84af 9825 _("Branch to odd address (%lx)"), (long) value);
bb2d6cd7
GK
9826
9827 if (!fixP->fx_done && value != 0)
9828 break;
9829 /* If 'value' is zero, the remaining reloc code won't actually
9830 do the store, so it must be done here. This is probably
9831 a bug somewhere. */
9832 if (!fixP->fx_done)
9833 value -= fixP->fx_frag->fr_address + fixP->fx_where;
bdaaa2e1 9834
98aa84af 9835 value = (offsetT) value >> 2;
252b5132
RH
9836
9837 /* update old instruction data */
9838 buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
9839 if (target_big_endian)
9840 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
9841 else
9842 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
9843
98aa84af 9844 if (value + 0x8000 <= 0xffff)
252b5132
RH
9845 insn |= value & 0xffff;
9846 else
9847 {
9848 /* The branch offset is too large. If this is an
9849 unconditional branch, and we are not generating PIC code,
9850 we can convert it to an absolute jump instruction. */
9851 if (mips_pic == NO_PIC
9852 && fixP->fx_done
9853 && fixP->fx_frag->fr_address >= text_section->vma
9854 && (fixP->fx_frag->fr_address
9855 < text_section->vma + text_section->_raw_size)
9856 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
9857 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
9858 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
9859 {
9860 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
9861 insn = 0x0c000000; /* jal */
9862 else
9863 insn = 0x08000000; /* j */
9864 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
9865 fixP->fx_done = 0;
9866 fixP->fx_addsy = section_symbol (text_section);
9867 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
9868 }
9869 else
9870 {
9871 /* FIXME. It would be possible in principle to handle
9872 conditional branches which overflow. They could be
9873 transformed into a branch around a jump. This would
9874 require setting up variant frags for each different
9875 branch type. The native MIPS assembler attempts to
9876 handle these cases, but it appears to do it
9877 incorrectly. */
9878 as_bad_where (fixP->fx_file, fixP->fx_line,
9879 _("Branch out of range"));
9880 }
9881 }
9882
9883 md_number_to_chars ((char *) buf, (valueT) insn, 4);
9884 break;
9885
9886 case BFD_RELOC_VTABLE_INHERIT:
9887 fixP->fx_done = 0;
9888 if (fixP->fx_addsy
9889 && !S_IS_DEFINED (fixP->fx_addsy)
9890 && !S_IS_WEAK (fixP->fx_addsy))
9891 S_SET_WEAK (fixP->fx_addsy);
9892 break;
9893
9894 case BFD_RELOC_VTABLE_ENTRY:
9895 fixP->fx_done = 0;
9896 break;
9897
9898 default:
9899 internalError ();
9900 }
9901
9902 return 1;
9903}
9904
9905#if 0
9906void
9907printInsn (oc)
9908 unsigned long oc;
9909{
9910 const struct mips_opcode *p;
9911 int treg, sreg, dreg, shamt;
9912 short imm;
9913 const char *args;
9914 int i;
9915
9916 for (i = 0; i < NUMOPCODES; ++i)
9917 {
9918 p = &mips_opcodes[i];
9919 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
9920 {
9921 printf ("%08lx %s\t", oc, p->name);
9922 treg = (oc >> 16) & 0x1f;
9923 sreg = (oc >> 21) & 0x1f;
9924 dreg = (oc >> 11) & 0x1f;
9925 shamt = (oc >> 6) & 0x1f;
9926 imm = oc;
9927 for (args = p->args;; ++args)
9928 {
9929 switch (*args)
9930 {
9931 case '\0':
9932 printf ("\n");
9933 break;
9934
9935 case ',':
9936 case '(':
9937 case ')':
9938 printf ("%c", *args);
9939 continue;
9940
9941 case 'r':
9942 assert (treg == sreg);
9943 printf ("$%d,$%d", treg, sreg);
9944 continue;
9945
9946 case 'd':
9947 case 'G':
9948 printf ("$%d", dreg);
9949 continue;
9950
9951 case 't':
9952 case 'E':
9953 printf ("$%d", treg);
9954 continue;
9955
9956 case 'k':
9957 printf ("0x%x", treg);
9958 continue;
9959
9960 case 'b':
9961 case 's':
9962 printf ("$%d", sreg);
9963 continue;
9964
9965 case 'a':
9966 printf ("0x%08lx", oc & 0x1ffffff);
9967 continue;
9968
9969 case 'i':
9970 case 'j':
9971 case 'o':
9972 case 'u':
9973 printf ("%d", imm);
9974 continue;
9975
9976 case '<':
9977 case '>':
9978 printf ("$%d", shamt);
9979 continue;
9980
9981 default:
9982 internalError ();
9983 }
9984 break;
9985 }
9986 return;
9987 }
9988 }
9989 printf (_("%08lx UNDEFINED\n"), oc);
9990}
9991#endif
9992
9993static symbolS *
9994get_symbol ()
9995{
9996 int c;
9997 char *name;
9998 symbolS *p;
9999
10000 name = input_line_pointer;
10001 c = get_symbol_end ();
10002 p = (symbolS *) symbol_find_or_make (name);
10003 *input_line_pointer = c;
10004 return p;
10005}
10006
10007/* Align the current frag to a given power of two. The MIPS assembler
10008 also automatically adjusts any preceding label. */
10009
10010static void
10011mips_align (to, fill, label)
10012 int to;
10013 int fill;
10014 symbolS *label;
10015{
10016 mips_emit_delays (false);
10017 frag_align (to, fill, 0);
10018 record_alignment (now_seg, to);
10019 if (label != NULL)
10020 {
10021 assert (S_GET_SEGMENT (label) == now_seg);
49309057 10022 symbol_set_frag (label, frag_now);
252b5132
RH
10023 S_SET_VALUE (label, (valueT) frag_now_fix ());
10024 }
10025}
10026
10027/* Align to a given power of two. .align 0 turns off the automatic
10028 alignment used by the data creating pseudo-ops. */
10029
10030static void
10031s_align (x)
43841e91 10032 int x ATTRIBUTE_UNUSED;
252b5132
RH
10033{
10034 register int temp;
10035 register long temp_fill;
10036 long max_alignment = 15;
10037
10038 /*
10039
10040 o Note that the assembler pulls down any immediately preceeding label
10041 to the aligned address.
10042 o It's not documented but auto alignment is reinstated by
10043 a .align pseudo instruction.
10044 o Note also that after auto alignment is turned off the mips assembler
10045 issues an error on attempt to assemble an improperly aligned data item.
10046 We don't.
10047
10048 */
10049
10050 temp = get_absolute_expression ();
10051 if (temp > max_alignment)
10052 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
10053 else if (temp < 0)
10054 {
10055 as_warn (_("Alignment negative: 0 assumed."));
10056 temp = 0;
10057 }
10058 if (*input_line_pointer == ',')
10059 {
10060 input_line_pointer++;
10061 temp_fill = get_absolute_expression ();
10062 }
10063 else
10064 temp_fill = 0;
10065 if (temp)
10066 {
10067 auto_align = 1;
10068 mips_align (temp, (int) temp_fill,
10069 insn_labels != NULL ? insn_labels->label : NULL);
10070 }
10071 else
10072 {
10073 auto_align = 0;
10074 }
10075
10076 demand_empty_rest_of_line ();
10077}
10078
10079void
10080mips_flush_pending_output ()
10081{
10082 mips_emit_delays (false);
10083 mips_clear_insn_labels ();
10084}
10085
10086static void
10087s_change_sec (sec)
10088 int sec;
10089{
10090 segT seg;
10091
10092 /* When generating embedded PIC code, we only use the .text, .lit8,
10093 .sdata and .sbss sections. We change the .data and .rdata
10094 pseudo-ops to use .sdata. */
10095 if (mips_pic == EMBEDDED_PIC
10096 && (sec == 'd' || sec == 'r'))
10097 sec = 's';
10098
10099#ifdef OBJ_ELF
10100 /* The ELF backend needs to know that we are changing sections, so
10101 that .previous works correctly. We could do something like check
10102 for a obj_section_change_hook macro, but that might be confusing
10103 as it would not be appropriate to use it in the section changing
10104 functions in read.c, since obj-elf.c intercepts those. FIXME:
10105 This should be cleaner, somehow. */
10106 obj_elf_section_change_hook ();
10107#endif
10108
10109 mips_emit_delays (false);
10110 switch (sec)
10111 {
10112 case 't':
10113 s_text (0);
10114 break;
10115 case 'd':
10116 s_data (0);
10117 break;
10118 case 'b':
10119 subseg_set (bss_section, (subsegT) get_absolute_expression ());
10120 demand_empty_rest_of_line ();
10121 break;
10122
10123 case 'r':
10124 if (USE_GLOBAL_POINTER_OPT)
10125 {
10126 seg = subseg_new (RDATA_SECTION_NAME,
10127 (subsegT) get_absolute_expression ());
10128 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
10129 {
10130 bfd_set_section_flags (stdoutput, seg,
10131 (SEC_ALLOC
10132 | SEC_LOAD
10133 | SEC_READONLY
10134 | SEC_RELOC
10135 | SEC_DATA));
10136 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 10137 record_alignment (seg, 4);
252b5132
RH
10138 }
10139 demand_empty_rest_of_line ();
10140 }
10141 else
10142 {
10143 as_bad (_("No read only data section in this object file format"));
10144 demand_empty_rest_of_line ();
10145 return;
10146 }
10147 break;
10148
10149 case 's':
10150 if (USE_GLOBAL_POINTER_OPT)
10151 {
10152 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
10153 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
10154 {
10155 bfd_set_section_flags (stdoutput, seg,
10156 SEC_ALLOC | SEC_LOAD | SEC_RELOC
10157 | SEC_DATA);
10158 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 10159 record_alignment (seg, 4);
252b5132
RH
10160 }
10161 demand_empty_rest_of_line ();
10162 break;
10163 }
10164 else
10165 {
10166 as_bad (_("Global pointers not supported; recompile -G 0"));
10167 demand_empty_rest_of_line ();
10168 return;
10169 }
10170 }
10171
10172 auto_align = 1;
10173}
10174
10175void
10176mips_enable_auto_align ()
10177{
10178 auto_align = 1;
10179}
10180
10181static void
10182s_cons (log_size)
10183 int log_size;
10184{
10185 symbolS *label;
10186
10187 label = insn_labels != NULL ? insn_labels->label : NULL;
10188 mips_emit_delays (false);
10189 if (log_size > 0 && auto_align)
10190 mips_align (log_size, 0, label);
10191 mips_clear_insn_labels ();
10192 cons (1 << log_size);
10193}
10194
10195static void
10196s_float_cons (type)
10197 int type;
10198{
10199 symbolS *label;
10200
10201 label = insn_labels != NULL ? insn_labels->label : NULL;
10202
10203 mips_emit_delays (false);
10204
10205 if (auto_align)
49309057
ILT
10206 {
10207 if (type == 'd')
10208 mips_align (3, 0, label);
10209 else
10210 mips_align (2, 0, label);
10211 }
252b5132
RH
10212
10213 mips_clear_insn_labels ();
10214
10215 float_cons (type);
10216}
10217
10218/* Handle .globl. We need to override it because on Irix 5 you are
10219 permitted to say
10220 .globl foo .text
10221 where foo is an undefined symbol, to mean that foo should be
10222 considered to be the address of a function. */
10223
10224static void
10225s_mips_globl (x)
43841e91 10226 int x ATTRIBUTE_UNUSED;
252b5132
RH
10227{
10228 char *name;
10229 int c;
10230 symbolS *symbolP;
10231 flagword flag;
10232
10233 name = input_line_pointer;
10234 c = get_symbol_end ();
10235 symbolP = symbol_find_or_make (name);
10236 *input_line_pointer = c;
10237 SKIP_WHITESPACE ();
10238
10239 /* On Irix 5, every global symbol that is not explicitly labelled as
10240 being a function is apparently labelled as being an object. */
10241 flag = BSF_OBJECT;
10242
10243 if (! is_end_of_line[(unsigned char) *input_line_pointer])
10244 {
10245 char *secname;
10246 asection *sec;
10247
10248 secname = input_line_pointer;
10249 c = get_symbol_end ();
10250 sec = bfd_get_section_by_name (stdoutput, secname);
10251 if (sec == NULL)
10252 as_bad (_("%s: no such section"), secname);
10253 *input_line_pointer = c;
10254
10255 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
10256 flag = BSF_FUNCTION;
10257 }
10258
49309057 10259 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
10260
10261 S_SET_EXTERNAL (symbolP);
10262 demand_empty_rest_of_line ();
10263}
10264
10265static void
10266s_option (x)
43841e91 10267 int x ATTRIBUTE_UNUSED;
252b5132
RH
10268{
10269 char *opt;
10270 char c;
10271
10272 opt = input_line_pointer;
10273 c = get_symbol_end ();
10274
10275 if (*opt == 'O')
10276 {
10277 /* FIXME: What does this mean? */
10278 }
10279 else if (strncmp (opt, "pic", 3) == 0)
10280 {
10281 int i;
10282
10283 i = atoi (opt + 3);
10284 if (i == 0)
10285 mips_pic = NO_PIC;
10286 else if (i == 2)
10287 mips_pic = SVR4_PIC;
10288 else
10289 as_bad (_(".option pic%d not supported"), i);
10290
10291 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
10292 {
10293 if (g_switch_seen && g_switch_value != 0)
10294 as_warn (_("-G may not be used with SVR4 PIC code"));
10295 g_switch_value = 0;
10296 bfd_set_gp_size (stdoutput, 0);
10297 }
10298 }
10299 else
10300 as_warn (_("Unrecognized option \"%s\""), opt);
10301
10302 *input_line_pointer = c;
10303 demand_empty_rest_of_line ();
10304}
10305
10306/* This structure is used to hold a stack of .set values. */
10307
e972090a
NC
10308struct mips_option_stack
10309{
252b5132
RH
10310 struct mips_option_stack *next;
10311 struct mips_set_options options;
10312};
10313
10314static struct mips_option_stack *mips_opts_stack;
10315
10316/* Handle the .set pseudo-op. */
10317
10318static void
10319s_mipsset (x)
43841e91 10320 int x ATTRIBUTE_UNUSED;
252b5132
RH
10321{
10322 char *name = input_line_pointer, ch;
10323
10324 while (!is_end_of_line[(unsigned char) *input_line_pointer])
10325 input_line_pointer++;
10326 ch = *input_line_pointer;
10327 *input_line_pointer = '\0';
10328
10329 if (strcmp (name, "reorder") == 0)
10330 {
10331 if (mips_opts.noreorder && prev_nop_frag != NULL)
10332 {
10333 /* If we still have pending nops, we can discard them. The
10334 usual nop handling will insert any that are still
bdaaa2e1 10335 needed. */
252b5132
RH
10336 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
10337 * (mips_opts.mips16 ? 2 : 4));
10338 prev_nop_frag = NULL;
10339 }
10340 mips_opts.noreorder = 0;
10341 }
10342 else if (strcmp (name, "noreorder") == 0)
10343 {
10344 mips_emit_delays (true);
10345 mips_opts.noreorder = 1;
10346 mips_any_noreorder = 1;
10347 }
10348 else if (strcmp (name, "at") == 0)
10349 {
10350 mips_opts.noat = 0;
10351 }
10352 else if (strcmp (name, "noat") == 0)
10353 {
10354 mips_opts.noat = 1;
10355 }
10356 else if (strcmp (name, "macro") == 0)
10357 {
10358 mips_opts.warn_about_macros = 0;
10359 }
10360 else if (strcmp (name, "nomacro") == 0)
10361 {
10362 if (mips_opts.noreorder == 0)
10363 as_bad (_("`noreorder' must be set before `nomacro'"));
10364 mips_opts.warn_about_macros = 1;
10365 }
10366 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
10367 {
10368 mips_opts.nomove = 0;
10369 }
10370 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
10371 {
10372 mips_opts.nomove = 1;
10373 }
10374 else if (strcmp (name, "bopt") == 0)
10375 {
10376 mips_opts.nobopt = 0;
10377 }
10378 else if (strcmp (name, "nobopt") == 0)
10379 {
10380 mips_opts.nobopt = 1;
10381 }
10382 else if (strcmp (name, "mips16") == 0
10383 || strcmp (name, "MIPS-16") == 0)
10384 mips_opts.mips16 = 1;
10385 else if (strcmp (name, "nomips16") == 0
10386 || strcmp (name, "noMIPS-16") == 0)
10387 mips_opts.mips16 = 0;
10388 else if (strncmp (name, "mips", 4) == 0)
10389 {
10390 int isa;
10391
10392 /* Permit the user to change the ISA on the fly. Needless to
10393 say, misuse can cause serious problems. */
10394 isa = atoi (name + 4);
e7af610e
NC
10395 switch (isa)
10396 {
84ea6cf2
NC
10397 case 0: mips_opts.isa = file_mips_isa; break;
10398 case 1: mips_opts.isa = ISA_MIPS1; break;
10399 case 2: mips_opts.isa = ISA_MIPS2; break;
10400 case 3: mips_opts.isa = ISA_MIPS3; break;
84ea6cf2 10401 case 4: mips_opts.isa = ISA_MIPS4; break;
9a41af64 10402 case 5: mips_opts.isa = ISA_MIPS5; break;
84ea6cf2
NC
10403 case 32: mips_opts.isa = ISA_MIPS32; break;
10404 case 64: mips_opts.isa = ISA_MIPS64; break;
10405 default: as_bad (_("unknown ISA level")); break;
e7af610e 10406 }
252b5132
RH
10407 }
10408 else if (strcmp (name, "autoextend") == 0)
10409 mips_opts.noautoextend = 0;
10410 else if (strcmp (name, "noautoextend") == 0)
10411 mips_opts.noautoextend = 1;
10412 else if (strcmp (name, "push") == 0)
10413 {
10414 struct mips_option_stack *s;
10415
10416 s = (struct mips_option_stack *) xmalloc (sizeof *s);
10417 s->next = mips_opts_stack;
10418 s->options = mips_opts;
10419 mips_opts_stack = s;
10420 }
10421 else if (strcmp (name, "pop") == 0)
10422 {
10423 struct mips_option_stack *s;
10424
10425 s = mips_opts_stack;
10426 if (s == NULL)
10427 as_bad (_(".set pop with no .set push"));
10428 else
10429 {
10430 /* If we're changing the reorder mode we need to handle
10431 delay slots correctly. */
10432 if (s->options.noreorder && ! mips_opts.noreorder)
10433 mips_emit_delays (true);
10434 else if (! s->options.noreorder && mips_opts.noreorder)
10435 {
10436 if (prev_nop_frag != NULL)
10437 {
10438 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
10439 * (mips_opts.mips16 ? 2 : 4));
10440 prev_nop_frag = NULL;
10441 }
10442 }
10443
10444 mips_opts = s->options;
10445 mips_opts_stack = s->next;
10446 free (s);
10447 }
10448 }
10449 else
10450 {
10451 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
10452 }
10453 *input_line_pointer = ch;
10454 demand_empty_rest_of_line ();
10455}
10456
10457/* Handle the .abicalls pseudo-op. I believe this is equivalent to
10458 .option pic2. It means to generate SVR4 PIC calls. */
10459
10460static void
10461s_abicalls (ignore)
43841e91 10462 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10463{
10464 mips_pic = SVR4_PIC;
10465 if (USE_GLOBAL_POINTER_OPT)
10466 {
10467 if (g_switch_seen && g_switch_value != 0)
10468 as_warn (_("-G may not be used with SVR4 PIC code"));
10469 g_switch_value = 0;
10470 }
10471 bfd_set_gp_size (stdoutput, 0);
10472 demand_empty_rest_of_line ();
10473}
10474
10475/* Handle the .cpload pseudo-op. This is used when generating SVR4
10476 PIC code. It sets the $gp register for the function based on the
10477 function address, which is in the register named in the argument.
10478 This uses a relocation against _gp_disp, which is handled specially
10479 by the linker. The result is:
10480 lui $gp,%hi(_gp_disp)
10481 addiu $gp,$gp,%lo(_gp_disp)
10482 addu $gp,$gp,.cpload argument
10483 The .cpload argument is normally $25 == $t9. */
10484
10485static void
10486s_cpload (ignore)
43841e91 10487 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10488{
10489 expressionS ex;
10490 int icnt = 0;
10491
10492 /* If we are not generating SVR4 PIC code, .cpload is ignored. */
10493 if (mips_pic != SVR4_PIC)
10494 {
10495 s_ignore (0);
10496 return;
10497 }
10498
10499 /* .cpload should be a in .set noreorder section. */
10500 if (mips_opts.noreorder == 0)
10501 as_warn (_(".cpload not in noreorder section"));
10502
10503 ex.X_op = O_symbol;
10504 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
10505 ex.X_op_symbol = NULL;
10506 ex.X_add_number = 0;
10507
10508 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 10509 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132
RH
10510
10511 macro_build_lui ((char *) NULL, &icnt, &ex, GP);
10512 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
10513 (int) BFD_RELOC_LO16);
10514
10515 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
10516 GP, GP, tc_get_register (0));
10517
10518 demand_empty_rest_of_line ();
10519}
10520
10521/* Handle the .cprestore pseudo-op. This stores $gp into a given
10522 offset from $sp. The offset is remembered, and after making a PIC
10523 call $gp is restored from that location. */
10524
10525static void
10526s_cprestore (ignore)
43841e91 10527 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10528{
10529 expressionS ex;
10530 int icnt = 0;
10531
10532 /* If we are not generating SVR4 PIC code, .cprestore is ignored. */
10533 if (mips_pic != SVR4_PIC)
10534 {
10535 s_ignore (0);
10536 return;
10537 }
10538
10539 mips_cprestore_offset = get_absolute_expression ();
10540
10541 ex.X_op = O_constant;
10542 ex.X_add_symbol = NULL;
10543 ex.X_op_symbol = NULL;
10544 ex.X_add_number = mips_cprestore_offset;
10545
10546 macro_build ((char *) NULL, &icnt, &ex,
ca4e0257 10547 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
252b5132
RH
10548 "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
10549
10550 demand_empty_rest_of_line ();
10551}
10552
10553/* Handle the .gpword pseudo-op. This is used when generating PIC
10554 code. It generates a 32 bit GP relative reloc. */
10555
10556static void
10557s_gpword (ignore)
43841e91 10558 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10559{
10560 symbolS *label;
10561 expressionS ex;
10562 char *p;
10563
10564 /* When not generating PIC code, this is treated as .word. */
10565 if (mips_pic != SVR4_PIC)
10566 {
10567 s_cons (2);
10568 return;
10569 }
10570
10571 label = insn_labels != NULL ? insn_labels->label : NULL;
10572 mips_emit_delays (true);
10573 if (auto_align)
10574 mips_align (2, 0, label);
10575 mips_clear_insn_labels ();
10576
10577 expression (&ex);
10578
10579 if (ex.X_op != O_symbol || ex.X_add_number != 0)
10580 {
10581 as_bad (_("Unsupported use of .gpword"));
10582 ignore_rest_of_line ();
10583 }
10584
10585 p = frag_more (4);
10586 md_number_to_chars (p, (valueT) 0, 4);
10587 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
10588 BFD_RELOC_MIPS_GPREL32);
10589
10590 demand_empty_rest_of_line ();
10591}
10592
10593/* Handle the .cpadd pseudo-op. This is used when dealing with switch
10594 tables in SVR4 PIC code. */
10595
10596static void
10597s_cpadd (ignore)
43841e91 10598 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10599{
10600 int icnt = 0;
10601 int reg;
10602
10603 /* This is ignored when not generating SVR4 PIC code. */
10604 if (mips_pic != SVR4_PIC)
10605 {
10606 s_ignore (0);
10607 return;
10608 }
10609
10610 /* Add $gp to the register named as an argument. */
10611 reg = tc_get_register (0);
10612 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 10613 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
10614 "d,v,t", reg, reg, GP);
10615
bdaaa2e1 10616 demand_empty_rest_of_line ();
252b5132
RH
10617}
10618
10619/* Handle the .insn pseudo-op. This marks instruction labels in
10620 mips16 mode. This permits the linker to handle them specially,
10621 such as generating jalx instructions when needed. We also make
10622 them odd for the duration of the assembly, in order to generate the
10623 right sort of code. We will make them even in the adjust_symtab
10624 routine, while leaving them marked. This is convenient for the
10625 debugger and the disassembler. The linker knows to make them odd
10626 again. */
10627
10628static void
10629s_insn (ignore)
43841e91 10630 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10631{
10632 if (mips_opts.mips16)
10633 mips16_mark_labels ();
10634
10635 demand_empty_rest_of_line ();
10636}
10637
10638/* Handle a .stabn directive. We need these in order to mark a label
10639 as being a mips16 text label correctly. Sometimes the compiler
10640 will emit a label, followed by a .stabn, and then switch sections.
10641 If the label and .stabn are in mips16 mode, then the label is
10642 really a mips16 text label. */
10643
10644static void
10645s_mips_stab (type)
10646 int type;
10647{
10648 if (type == 'n' && mips_opts.mips16)
10649 mips16_mark_labels ();
10650
10651 s_stab (type);
10652}
10653
10654/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
10655 */
10656
10657static void
10658s_mips_weakext (ignore)
43841e91 10659 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10660{
10661 char *name;
10662 int c;
10663 symbolS *symbolP;
10664 expressionS exp;
10665
10666 name = input_line_pointer;
10667 c = get_symbol_end ();
10668 symbolP = symbol_find_or_make (name);
10669 S_SET_WEAK (symbolP);
10670 *input_line_pointer = c;
10671
10672 SKIP_WHITESPACE ();
10673
10674 if (! is_end_of_line[(unsigned char) *input_line_pointer])
10675 {
10676 if (S_IS_DEFINED (symbolP))
10677 {
10678 as_bad ("Ignoring attempt to redefine symbol `%s'.",
10679 S_GET_NAME (symbolP));
10680 ignore_rest_of_line ();
10681 return;
10682 }
bdaaa2e1 10683
252b5132
RH
10684 if (*input_line_pointer == ',')
10685 {
10686 ++input_line_pointer;
10687 SKIP_WHITESPACE ();
10688 }
bdaaa2e1 10689
252b5132
RH
10690 expression (&exp);
10691 if (exp.X_op != O_symbol)
10692 {
10693 as_bad ("bad .weakext directive");
10694 ignore_rest_of_line();
10695 return;
10696 }
49309057 10697 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
10698 }
10699
10700 demand_empty_rest_of_line ();
10701}
10702
10703/* Parse a register string into a number. Called from the ECOFF code
10704 to parse .frame. The argument is non-zero if this is the frame
10705 register, so that we can record it in mips_frame_reg. */
10706
10707int
10708tc_get_register (frame)
10709 int frame;
10710{
10711 int reg;
10712
10713 SKIP_WHITESPACE ();
10714 if (*input_line_pointer++ != '$')
10715 {
10716 as_warn (_("expected `$'"));
10717 reg = 0;
10718 }
10719 else if (isdigit ((unsigned char) *input_line_pointer))
10720 {
10721 reg = get_absolute_expression ();
10722 if (reg < 0 || reg >= 32)
10723 {
10724 as_warn (_("Bad register number"));
10725 reg = 0;
10726 }
10727 }
10728 else
10729 {
10730 if (strncmp (input_line_pointer, "fp", 2) == 0)
10731 reg = FP;
10732 else if (strncmp (input_line_pointer, "sp", 2) == 0)
10733 reg = SP;
10734 else if (strncmp (input_line_pointer, "gp", 2) == 0)
10735 reg = GP;
10736 else if (strncmp (input_line_pointer, "at", 2) == 0)
10737 reg = AT;
10738 else
10739 {
10740 as_warn (_("Unrecognized register name"));
10741 reg = 0;
10742 }
10743 input_line_pointer += 2;
10744 }
10745 if (frame)
10746 mips_frame_reg = reg != 0 ? reg : SP;
10747 return reg;
10748}
10749
10750valueT
10751md_section_align (seg, addr)
10752 asection *seg;
10753 valueT addr;
10754{
10755 int align = bfd_get_section_alignment (stdoutput, seg);
10756
10757#ifdef OBJ_ELF
10758 /* We don't need to align ELF sections to the full alignment.
10759 However, Irix 5 may prefer that we align them at least to a 16
10760 byte boundary. We don't bother to align the sections if we are
10761 targeted for an embedded system. */
10762 if (strcmp (TARGET_OS, "elf") == 0)
10763 return addr;
10764 if (align > 4)
10765 align = 4;
10766#endif
10767
10768 return ((addr + (1 << align) - 1) & (-1 << align));
10769}
10770
10771/* Utility routine, called from above as well. If called while the
10772 input file is still being read, it's only an approximation. (For
10773 example, a symbol may later become defined which appeared to be
10774 undefined earlier.) */
10775
10776static int
10777nopic_need_relax (sym, before_relaxing)
10778 symbolS *sym;
10779 int before_relaxing;
10780{
10781 if (sym == 0)
10782 return 0;
10783
10784 if (USE_GLOBAL_POINTER_OPT)
10785 {
10786 const char *symname;
10787 int change;
10788
10789 /* Find out whether this symbol can be referenced off the GP
10790 register. It can be if it is smaller than the -G size or if
10791 it is in the .sdata or .sbss section. Certain symbols can
10792 not be referenced off the GP, although it appears as though
10793 they can. */
10794 symname = S_GET_NAME (sym);
10795 if (symname != (const char *) NULL
10796 && (strcmp (symname, "eprol") == 0
10797 || strcmp (symname, "etext") == 0
10798 || strcmp (symname, "_gp") == 0
10799 || strcmp (symname, "edata") == 0
10800 || strcmp (symname, "_fbss") == 0
10801 || strcmp (symname, "_fdata") == 0
10802 || strcmp (symname, "_ftext") == 0
10803 || strcmp (symname, "end") == 0
10804 || strcmp (symname, "_gp_disp") == 0))
10805 change = 1;
10806 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
10807 && (0
10808#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
10809 || (symbol_get_obj (sym)->ecoff_extern_size != 0
10810 && (symbol_get_obj (sym)->ecoff_extern_size
10811 <= g_switch_value))
252b5132
RH
10812#endif
10813 /* We must defer this decision until after the whole
10814 file has been read, since there might be a .extern
10815 after the first use of this symbol. */
10816 || (before_relaxing
10817#ifndef NO_ECOFF_DEBUGGING
49309057 10818 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
10819#endif
10820 && S_GET_VALUE (sym) == 0)
10821 || (S_GET_VALUE (sym) != 0
10822 && S_GET_VALUE (sym) <= g_switch_value)))
10823 change = 0;
10824 else
10825 {
10826 const char *segname;
10827
10828 segname = segment_name (S_GET_SEGMENT (sym));
10829 assert (strcmp (segname, ".lit8") != 0
10830 && strcmp (segname, ".lit4") != 0);
10831 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
10832 && strcmp (segname, ".sbss") != 0
10833 && strncmp (segname, ".sdata.", 7) != 0
10834 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
10835 }
10836 return change;
10837 }
10838 else
10839 /* We are not optimizing for the GP register. */
10840 return 1;
10841}
10842
10843/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
10844 extended opcode. SEC is the section the frag is in. */
10845
10846static int
10847mips16_extended_frag (fragp, sec, stretch)
10848 fragS *fragp;
10849 asection *sec;
10850 long stretch;
10851{
10852 int type;
10853 register const struct mips16_immed_operand *op;
10854 offsetT val;
10855 int mintiny, maxtiny;
10856 segT symsec;
98aa84af 10857 fragS *sym_frag;
252b5132
RH
10858
10859 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
10860 return 0;
10861 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
10862 return 1;
10863
10864 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
10865 op = mips16_immed_operands;
10866 while (op->type != type)
10867 {
10868 ++op;
10869 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10870 }
10871
10872 if (op->unsp)
10873 {
10874 if (type == '<' || type == '>' || type == '[' || type == ']')
10875 {
10876 mintiny = 1;
10877 maxtiny = 1 << op->nbits;
10878 }
10879 else
10880 {
10881 mintiny = 0;
10882 maxtiny = (1 << op->nbits) - 1;
10883 }
10884 }
10885 else
10886 {
10887 mintiny = - (1 << (op->nbits - 1));
10888 maxtiny = (1 << (op->nbits - 1)) - 1;
10889 }
10890
98aa84af 10891 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 10892 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 10893 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
10894
10895 if (op->pcrel)
10896 {
10897 addressT addr;
10898
10899 /* We won't have the section when we are called from
10900 mips_relax_frag. However, we will always have been called
10901 from md_estimate_size_before_relax first. If this is a
10902 branch to a different section, we mark it as such. If SEC is
10903 NULL, and the frag is not marked, then it must be a branch to
10904 the same section. */
10905 if (sec == NULL)
10906 {
10907 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
10908 return 1;
10909 }
10910 else
10911 {
98aa84af 10912 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
10913 if (symsec != sec)
10914 {
10915 fragp->fr_subtype =
10916 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
10917
10918 /* FIXME: We should support this, and let the linker
10919 catch branches and loads that are out of range. */
10920 as_bad_where (fragp->fr_file, fragp->fr_line,
10921 _("unsupported PC relative reference to different section"));
10922
10923 return 1;
10924 }
98aa84af
AM
10925 if (fragp != sym_frag && sym_frag->fr_address == 0)
10926 /* Assume non-extended on the first relaxation pass.
10927 The address we have calculated will be bogus if this is
10928 a forward branch to another frag, as the forward frag
10929 will have fr_address == 0. */
10930 return 0;
252b5132
RH
10931 }
10932
10933 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
10934 the same section. If the relax_marker of the symbol fragment
10935 differs from the relax_marker of this fragment, we have not
10936 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
10937 in STRETCH in order to get a better estimate of the address.
10938 This particularly matters because of the shift bits. */
10939 if (stretch != 0
98aa84af 10940 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
10941 {
10942 fragS *f;
10943
10944 /* Adjust stretch for any alignment frag. Note that if have
10945 been expanding the earlier code, the symbol may be
10946 defined in what appears to be an earlier frag. FIXME:
10947 This doesn't handle the fr_subtype field, which specifies
10948 a maximum number of bytes to skip when doing an
10949 alignment. */
98aa84af 10950 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
10951 {
10952 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
10953 {
10954 if (stretch < 0)
10955 stretch = - ((- stretch)
10956 & ~ ((1 << (int) f->fr_offset) - 1));
10957 else
10958 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
10959 if (stretch == 0)
10960 break;
10961 }
10962 }
10963 if (f != NULL)
10964 val += stretch;
10965 }
10966
10967 addr = fragp->fr_address + fragp->fr_fix;
10968
10969 /* The base address rules are complicated. The base address of
10970 a branch is the following instruction. The base address of a
10971 PC relative load or add is the instruction itself, but if it
10972 is in a delay slot (in which case it can not be extended) use
10973 the address of the instruction whose delay slot it is in. */
10974 if (type == 'p' || type == 'q')
10975 {
10976 addr += 2;
10977
10978 /* If we are currently assuming that this frag should be
10979 extended, then, the current address is two bytes
bdaaa2e1 10980 higher. */
252b5132
RH
10981 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
10982 addr += 2;
10983
10984 /* Ignore the low bit in the target, since it will be set
10985 for a text label. */
10986 if ((val & 1) != 0)
10987 --val;
10988 }
10989 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
10990 addr -= 4;
10991 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
10992 addr -= 2;
10993
10994 val -= addr & ~ ((1 << op->shift) - 1);
10995
10996 /* Branch offsets have an implicit 0 in the lowest bit. */
10997 if (type == 'p' || type == 'q')
10998 val /= 2;
10999
11000 /* If any of the shifted bits are set, we must use an extended
11001 opcode. If the address depends on the size of this
11002 instruction, this can lead to a loop, so we arrange to always
11003 use an extended opcode. We only check this when we are in
11004 the main relaxation loop, when SEC is NULL. */
11005 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
11006 {
11007 fragp->fr_subtype =
11008 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11009 return 1;
11010 }
11011
11012 /* If we are about to mark a frag as extended because the value
11013 is precisely maxtiny + 1, then there is a chance of an
11014 infinite loop as in the following code:
11015 la $4,foo
11016 .skip 1020
11017 .align 2
11018 foo:
11019 In this case when the la is extended, foo is 0x3fc bytes
11020 away, so the la can be shrunk, but then foo is 0x400 away, so
11021 the la must be extended. To avoid this loop, we mark the
11022 frag as extended if it was small, and is about to become
11023 extended with a value of maxtiny + 1. */
11024 if (val == ((maxtiny + 1) << op->shift)
11025 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
11026 && sec == NULL)
11027 {
11028 fragp->fr_subtype =
11029 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11030 return 1;
11031 }
11032 }
11033 else if (symsec != absolute_section && sec != NULL)
11034 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
11035
11036 if ((val & ((1 << op->shift) - 1)) != 0
11037 || val < (mintiny << op->shift)
11038 || val > (maxtiny << op->shift))
11039 return 1;
11040 else
11041 return 0;
11042}
11043
11044/* Estimate the size of a frag before relaxing. Unless this is the
11045 mips16, we are not really relaxing here, and the final size is
11046 encoded in the subtype information. For the mips16, we have to
11047 decide whether we are using an extended opcode or not. */
11048
252b5132
RH
11049int
11050md_estimate_size_before_relax (fragp, segtype)
11051 fragS *fragp;
11052 asection *segtype;
11053{
43841e91 11054 int change = 0;
8614eeee 11055 boolean linkonce = false;
252b5132
RH
11056
11057 if (RELAX_MIPS16_P (fragp->fr_subtype))
11058 {
11059 if (mips16_extended_frag (fragp, segtype, 0))
11060 {
11061 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
11062 return 4;
11063 }
11064 else
11065 {
11066 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
11067 return 2;
11068 }
11069 }
11070
11071 if (mips_pic == NO_PIC)
11072 {
11073 change = nopic_need_relax (fragp->fr_symbol, 0);
11074 }
11075 else if (mips_pic == SVR4_PIC)
11076 {
11077 symbolS *sym;
11078 asection *symsec;
11079
11080 sym = fragp->fr_symbol;
11081
11082 /* Handle the case of a symbol equated to another symbol. */
49309057 11083 while (symbol_equated_p (sym)
252b5132
RH
11084 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
11085 {
11086 symbolS *n;
11087
11088 /* It's possible to get a loop here in a badly written
11089 program. */
49309057 11090 n = symbol_get_value_expression (sym)->X_add_symbol;
252b5132
RH
11091 if (n == sym)
11092 break;
11093 sym = n;
11094 }
11095
11096 symsec = S_GET_SEGMENT (sym);
11097
8614eeee
UC
11098 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
11099 if (symsec != segtype && ! S_IS_LOCAL (sym))
beae10d5
KH
11100 {
11101 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
11102 != 0)
11103 linkonce = true;
11104
11105 /* The GNU toolchain uses an extension for ELF: a section
11106 beginning with the magic string .gnu.linkonce is a linkonce
11107 section. */
11108 if (strncmp (segment_name (symsec), ".gnu.linkonce",
11109 sizeof ".gnu.linkonce" - 1) == 0)
11110 linkonce = true;
11111 }
8614eeee 11112
252b5132
RH
11113 /* This must duplicate the test in adjust_reloc_syms. */
11114 change = (symsec != &bfd_und_section
11115 && symsec != &bfd_abs_section
426b0403 11116 && ! bfd_is_com_section (symsec)
8614eeee 11117 && !linkonce
426b0403 11118#ifdef OBJ_ELF
ea4ff978 11119 /* A global or weak symbol is treated as external. */
46bac6de
L
11120 && (OUTPUT_FLAVOR == bfd_target_elf_flavour
11121 && ! (S_IS_EXTERN (sym) || S_IS_WEAK (sym)))
426b0403
AM
11122#endif
11123 );
252b5132
RH
11124 }
11125 else
11126 abort ();
11127
11128 if (change)
11129 {
11130 /* Record the offset to the first reloc in the fr_opcode field.
11131 This lets md_convert_frag and tc_gen_reloc know that the code
11132 must be expanded. */
11133 fragp->fr_opcode = (fragp->fr_literal
11134 + fragp->fr_fix
11135 - RELAX_OLD (fragp->fr_subtype)
11136 + RELAX_RELOC1 (fragp->fr_subtype));
11137 /* FIXME: This really needs as_warn_where. */
11138 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
11139 as_warn (_("AT used after \".set noat\" or macro used after "
11140 "\".set nomacro\""));
11141
11142 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
11143 }
11144
9a41af64 11145 return 0;
252b5132
RH
11146}
11147
11148/* This is called to see whether a reloc against a defined symbol
11149 should be converted into a reloc against a section. Don't adjust
11150 MIPS16 jump relocations, so we don't have to worry about the format
11151 of the offset in the .o file. Don't adjust relocations against
11152 mips16 symbols, so that the linker can find them if it needs to set
11153 up a stub. */
11154
11155int
11156mips_fix_adjustable (fixp)
11157 fixS *fixp;
11158{
ea4ff978
L
11159#ifdef OBJ_ELF
11160 /* Prevent all adjustments to global symbols. */
46bac6de
L
11161 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11162 && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
ea4ff978
L
11163 return 0;
11164#endif
252b5132
RH
11165 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
11166 return 0;
11167 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11168 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
11169 return 0;
11170 if (fixp->fx_addsy == NULL)
11171 return 1;
11172#ifdef OBJ_ELF
11173 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11174 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
11175 && fixp->fx_subsy == NULL)
11176 return 0;
11177#endif
11178 return 1;
11179}
11180
11181/* Translate internal representation of relocation info to BFD target
11182 format. */
11183
11184arelent **
11185tc_gen_reloc (section, fixp)
43841e91 11186 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
11187 fixS *fixp;
11188{
11189 static arelent *retval[4];
11190 arelent *reloc;
11191 bfd_reloc_code_real_type code;
11192
11193 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
11194 retval[1] = NULL;
11195
49309057
ILT
11196 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
11197 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
11198 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
11199
11200 if (mips_pic == EMBEDDED_PIC
11201 && SWITCH_TABLE (fixp))
11202 {
11203 /* For a switch table entry we use a special reloc. The addend
11204 is actually the difference between the reloc address and the
11205 subtrahend. */
11206 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
11207 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
11208 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
11209 fixp->fx_r_type = BFD_RELOC_GPREL32;
11210 }
bb2d6cd7
GK
11211 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11212 reloc->addend = fixp->fx_addnumber;
252b5132
RH
11213 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
11214 {
11215 /* We use a special addend for an internal RELLO reloc. */
49309057 11216 if (symbol_section_p (fixp->fx_addsy))
252b5132
RH
11217 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
11218 else
11219 reloc->addend = fixp->fx_addnumber + reloc->address;
11220 }
11221 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
11222 {
11223 assert (fixp->fx_next != NULL
11224 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
11225 /* We use a special addend for an internal RELHI reloc. The
11226 reloc is relative to the RELLO; adjust the addend
11227 accordingly. */
49309057 11228 if (symbol_section_p (fixp->fx_addsy))
252b5132
RH
11229 reloc->addend = (fixp->fx_next->fx_frag->fr_address
11230 + fixp->fx_next->fx_where
11231 - S_GET_VALUE (fixp->fx_subsy));
11232 else
11233 reloc->addend = (fixp->fx_addnumber
11234 + fixp->fx_next->fx_frag->fr_address
11235 + fixp->fx_next->fx_where);
11236 }
252b5132
RH
11237 else
11238 {
11239 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
11240 /* A gruesome hack which is a result of the gruesome gas reloc
11241 handling. */
11242 reloc->addend = reloc->address;
11243 else
11244 reloc->addend = -reloc->address;
11245 }
11246
11247 /* If this is a variant frag, we may need to adjust the existing
11248 reloc and generate a new one. */
11249 if (fixp->fx_frag->fr_opcode != NULL
11250 && (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
11251 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11252 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
11253 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
11254 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
11255 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
11256 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16))
11257 {
11258 arelent *reloc2;
11259
11260 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
11261
11262 /* If this is not the last reloc in this frag, then we have two
11263 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
11264 CALL_HI16/CALL_LO16, both of which are being replaced. Let
11265 the second one handle all of them. */
11266 if (fixp->fx_next != NULL
11267 && fixp->fx_frag == fixp->fx_next->fx_frag)
11268 {
11269 assert ((fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
11270 && fixp->fx_next->fx_r_type == BFD_RELOC_MIPS_GPREL)
11271 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
11272 && (fixp->fx_next->fx_r_type
11273 == BFD_RELOC_MIPS_GOT_LO16))
11274 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
11275 && (fixp->fx_next->fx_r_type
11276 == BFD_RELOC_MIPS_CALL_LO16)));
11277 retval[0] = NULL;
11278 return retval;
11279 }
11280
11281 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
11282 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
11283 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
11284 retval[2] = NULL;
49309057
ILT
11285 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
11286 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
11287 reloc2->address = (reloc->address
11288 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
11289 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
11290 reloc2->addend = fixp->fx_addnumber;
11291 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
11292 assert (reloc2->howto != NULL);
11293
11294 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
11295 {
11296 arelent *reloc3;
11297
11298 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
11299 retval[3] = NULL;
11300 *reloc3 = *reloc2;
11301 reloc3->address += 4;
11302 }
11303
11304 if (mips_pic == NO_PIC)
11305 {
11306 assert (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL);
11307 fixp->fx_r_type = BFD_RELOC_HI16_S;
11308 }
11309 else if (mips_pic == SVR4_PIC)
11310 {
11311 switch (fixp->fx_r_type)
11312 {
11313 default:
11314 abort ();
11315 case BFD_RELOC_MIPS_GOT16:
11316 break;
11317 case BFD_RELOC_MIPS_CALL16:
11318 case BFD_RELOC_MIPS_GOT_LO16:
11319 case BFD_RELOC_MIPS_CALL_LO16:
11320 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
11321 break;
11322 }
11323 }
11324 else
11325 abort ();
11326 }
11327
11328 /* Since MIPS ELF uses Rel instead of Rela, encode the vtable entry
11329 to be used in the relocation's section offset. */
11330 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
11331 {
11332 reloc->address = reloc->addend;
11333 reloc->addend = 0;
11334 }
11335
11336 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
11337 fixup_segment converted a non-PC relative reloc into a PC
11338 relative reloc. In such a case, we need to convert the reloc
11339 code. */
11340 code = fixp->fx_r_type;
11341 if (fixp->fx_pcrel)
11342 {
11343 switch (code)
11344 {
11345 case BFD_RELOC_8:
11346 code = BFD_RELOC_8_PCREL;
11347 break;
11348 case BFD_RELOC_16:
11349 code = BFD_RELOC_16_PCREL;
11350 break;
11351 case BFD_RELOC_32:
11352 code = BFD_RELOC_32_PCREL;
11353 break;
11354 case BFD_RELOC_64:
11355 code = BFD_RELOC_64_PCREL;
11356 break;
11357 case BFD_RELOC_8_PCREL:
11358 case BFD_RELOC_16_PCREL:
11359 case BFD_RELOC_32_PCREL:
11360 case BFD_RELOC_64_PCREL:
11361 case BFD_RELOC_16_PCREL_S2:
11362 case BFD_RELOC_PCREL_HI16_S:
11363 case BFD_RELOC_PCREL_LO16:
11364 break;
11365 default:
11366 as_bad_where (fixp->fx_file, fixp->fx_line,
11367 _("Cannot make %s relocation PC relative"),
11368 bfd_get_reloc_code_name (code));
11369 }
11370 }
11371
11372 /* To support a PC relative reloc when generating embedded PIC code
11373 for ECOFF, we use a Cygnus extension. We check for that here to
11374 make sure that we don't let such a reloc escape normally. */
bb2d6cd7
GK
11375 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11376 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132
RH
11377 && code == BFD_RELOC_16_PCREL_S2
11378 && mips_pic != EMBEDDED_PIC)
11379 reloc->howto = NULL;
11380 else
11381 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
11382
11383 if (reloc->howto == NULL)
11384 {
11385 as_bad_where (fixp->fx_file, fixp->fx_line,
11386 _("Can not represent %s relocation in this object file format"),
11387 bfd_get_reloc_code_name (code));
11388 retval[0] = NULL;
11389 }
11390
11391 return retval;
11392}
11393
11394/* Relax a machine dependent frag. This returns the amount by which
11395 the current size of the frag should change. */
11396
11397int
11398mips_relax_frag (fragp, stretch)
11399 fragS *fragp;
11400 long stretch;
11401{
11402 if (! RELAX_MIPS16_P (fragp->fr_subtype))
11403 return 0;
11404
11405 if (mips16_extended_frag (fragp, (asection *) NULL, stretch))
11406 {
11407 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11408 return 0;
11409 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
11410 return 2;
11411 }
11412 else
11413 {
11414 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11415 return 0;
11416 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
11417 return -2;
11418 }
11419
11420 return 0;
11421}
11422
11423/* Convert a machine dependent frag. */
11424
11425void
11426md_convert_frag (abfd, asec, fragp)
43841e91 11427 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
11428 segT asec;
11429 fragS *fragp;
11430{
11431 int old, new;
11432 char *fixptr;
11433
11434 if (RELAX_MIPS16_P (fragp->fr_subtype))
11435 {
11436 int type;
11437 register const struct mips16_immed_operand *op;
11438 boolean small, ext;
11439 offsetT val;
11440 bfd_byte *buf;
11441 unsigned long insn;
11442 boolean use_extend;
11443 unsigned short extend;
11444
11445 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
11446 op = mips16_immed_operands;
11447 while (op->type != type)
11448 ++op;
11449
11450 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11451 {
11452 small = false;
11453 ext = true;
11454 }
11455 else
11456 {
11457 small = true;
11458 ext = false;
11459 }
11460
6386f3a7 11461 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
11462 val = S_GET_VALUE (fragp->fr_symbol);
11463 if (op->pcrel)
11464 {
11465 addressT addr;
11466
11467 addr = fragp->fr_address + fragp->fr_fix;
11468
11469 /* The rules for the base address of a PC relative reloc are
11470 complicated; see mips16_extended_frag. */
11471 if (type == 'p' || type == 'q')
11472 {
11473 addr += 2;
11474 if (ext)
11475 addr += 2;
11476 /* Ignore the low bit in the target, since it will be
11477 set for a text label. */
11478 if ((val & 1) != 0)
11479 --val;
11480 }
11481 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
11482 addr -= 4;
11483 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
11484 addr -= 2;
11485
11486 addr &= ~ (addressT) ((1 << op->shift) - 1);
11487 val -= addr;
11488
11489 /* Make sure the section winds up with the alignment we have
11490 assumed. */
11491 if (op->shift > 0)
11492 record_alignment (asec, op->shift);
11493 }
11494
11495 if (ext
11496 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
11497 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
11498 as_warn_where (fragp->fr_file, fragp->fr_line,
11499 _("extended instruction in delay slot"));
11500
11501 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
11502
11503 if (target_big_endian)
11504 insn = bfd_getb16 (buf);
11505 else
11506 insn = bfd_getl16 (buf);
11507
11508 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
11509 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
11510 small, ext, &insn, &use_extend, &extend);
11511
11512 if (use_extend)
11513 {
11514 md_number_to_chars (buf, 0xf000 | extend, 2);
11515 fragp->fr_fix += 2;
11516 buf += 2;
11517 }
11518
11519 md_number_to_chars (buf, insn, 2);
11520 fragp->fr_fix += 2;
11521 buf += 2;
11522 }
11523 else
11524 {
11525 if (fragp->fr_opcode == NULL)
11526 return;
11527
11528 old = RELAX_OLD (fragp->fr_subtype);
11529 new = RELAX_NEW (fragp->fr_subtype);
11530 fixptr = fragp->fr_literal + fragp->fr_fix;
11531
11532 if (new > 0)
11533 memcpy (fixptr - old, fixptr, new);
11534
11535 fragp->fr_fix += new - old;
11536 }
11537}
11538
11539#ifdef OBJ_ELF
11540
11541/* This function is called after the relocs have been generated.
11542 We've been storing mips16 text labels as odd. Here we convert them
11543 back to even for the convenience of the debugger. */
11544
11545void
11546mips_frob_file_after_relocs ()
11547{
11548 asymbol **syms;
11549 unsigned int count, i;
11550
11551 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11552 return;
11553
11554 syms = bfd_get_outsymbols (stdoutput);
11555 count = bfd_get_symcount (stdoutput);
11556 for (i = 0; i < count; i++, syms++)
11557 {
11558 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
11559 && ((*syms)->value & 1) != 0)
11560 {
11561 (*syms)->value &= ~1;
11562 /* If the symbol has an odd size, it was probably computed
11563 incorrectly, so adjust that as well. */
11564 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
11565 ++elf_symbol (*syms)->internal_elf_sym.st_size;
11566 }
11567 }
11568}
11569
11570#endif
11571
11572/* This function is called whenever a label is defined. It is used
11573 when handling branch delays; if a branch has a label, we assume we
11574 can not move it. */
11575
11576void
11577mips_define_label (sym)
11578 symbolS *sym;
11579{
11580 struct insn_label_list *l;
11581
11582 if (free_insn_labels == NULL)
11583 l = (struct insn_label_list *) xmalloc (sizeof *l);
11584 else
11585 {
11586 l = free_insn_labels;
11587 free_insn_labels = l->next;
11588 }
11589
11590 l->label = sym;
11591 l->next = insn_labels;
11592 insn_labels = l;
11593}
11594\f
11595#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11596
11597/* Some special processing for a MIPS ELF file. */
11598
11599void
11600mips_elf_final_processing ()
11601{
11602 /* Write out the register information. */
11603 if (! mips_64)
11604 {
11605 Elf32_RegInfo s;
11606
11607 s.ri_gprmask = mips_gprmask;
11608 s.ri_cprmask[0] = mips_cprmask[0];
11609 s.ri_cprmask[1] = mips_cprmask[1];
11610 s.ri_cprmask[2] = mips_cprmask[2];
11611 s.ri_cprmask[3] = mips_cprmask[3];
11612 /* The gp_value field is set by the MIPS ELF backend. */
11613
11614 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
11615 ((Elf32_External_RegInfo *)
11616 mips_regmask_frag));
11617 }
11618 else
11619 {
11620 Elf64_Internal_RegInfo s;
11621
11622 s.ri_gprmask = mips_gprmask;
11623 s.ri_pad = 0;
11624 s.ri_cprmask[0] = mips_cprmask[0];
11625 s.ri_cprmask[1] = mips_cprmask[1];
11626 s.ri_cprmask[2] = mips_cprmask[2];
11627 s.ri_cprmask[3] = mips_cprmask[3];
11628 /* The gp_value field is set by the MIPS ELF backend. */
11629
11630 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
11631 ((Elf64_External_RegInfo *)
11632 mips_regmask_frag));
11633 }
11634
11635 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
11636 sort of BFD interface for this. */
11637 if (mips_any_noreorder)
11638 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
11639 if (mips_pic != NO_PIC)
11640 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
11641
bdaaa2e1 11642 /* Set the MIPS ELF ABI flags. */
9a41af64 11643 if (mips_abi_string == NULL)
252b5132 11644 ;
beae10d5 11645 else if (strcmp (mips_abi_string, "32") == 0)
252b5132 11646 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
beae10d5 11647 else if (strcmp (mips_abi_string, "o64") == 0)
252b5132 11648 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
beae10d5 11649 else if (strcmp (mips_abi_string, "eabi") == 0)
252b5132
RH
11650 {
11651 if (mips_eabi64)
11652 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
11653 else
11654 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
11655 }
be00bddd
TS
11656 else if (strcmp (mips_abi_string, "n32") == 0)
11657 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
11658
11659 /* Nothing to do for "64". */
252b5132
RH
11660
11661 if (mips_32bitmode)
11662 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
11663}
11664
11665#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
11666\f
beae10d5
KH
11667typedef struct proc {
11668 symbolS *isym;
11669 unsigned long reg_mask;
11670 unsigned long reg_offset;
11671 unsigned long fpreg_mask;
11672 unsigned long fpreg_offset;
11673 unsigned long frame_offset;
11674 unsigned long frame_reg;
11675 unsigned long pc_reg;
11676} procS;
252b5132
RH
11677
11678static procS cur_proc;
11679static procS *cur_proc_ptr;
11680static int numprocs;
11681
0a9ef439 11682/* Fill in an rs_align_code fragment. */
a19d8eb0 11683
0a9ef439
RH
11684void
11685mips_handle_align (fragp)
11686 fragS *fragp;
a19d8eb0 11687{
0a9ef439
RH
11688 if (fragp->fr_type != rs_align_code)
11689 return;
11690
11691 if (mips_opts.mips16)
a19d8eb0
CP
11692 {
11693 static const unsigned char be_nop[] = { 0x65, 0x00 };
11694 static const unsigned char le_nop[] = { 0x00, 0x65 };
11695
0a9ef439
RH
11696 int bytes;
11697 char *p;
a19d8eb0 11698
0a9ef439
RH
11699 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
11700 p = fragp->fr_literal + fragp->fr_fix;
11701
11702 if (bytes & 1)
11703 {
11704 *p++ = 0;
11705 fragp->fr_fix += 1;
11706 }
11707
11708 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
11709 fragp->fr_var = 2;
a19d8eb0
CP
11710 }
11711
0a9ef439 11712 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
11713}
11714
252b5132
RH
11715static void
11716md_obj_begin ()
11717{
11718}
11719
11720static void
11721md_obj_end ()
11722{
11723 /* check for premature end, nesting errors, etc */
11724 if (cur_proc_ptr)
9a41af64 11725 as_warn (_("missing .end at end of assembly"));
252b5132
RH
11726}
11727
11728static long
11729get_number ()
11730{
11731 int negative = 0;
11732 long val = 0;
11733
11734 if (*input_line_pointer == '-')
11735 {
11736 ++input_line_pointer;
11737 negative = 1;
11738 }
d9a62219 11739 if (!isdigit ((unsigned char) *input_line_pointer))
252b5132
RH
11740 as_bad (_("Expected simple number."));
11741 if (input_line_pointer[0] == '0')
11742 {
11743 if (input_line_pointer[1] == 'x')
11744 {
11745 input_line_pointer += 2;
d9a62219 11746 while (isxdigit ((unsigned char) *input_line_pointer))
252b5132
RH
11747 {
11748 val <<= 4;
11749 val |= hex_value (*input_line_pointer++);
11750 }
11751 return negative ? -val : val;
11752 }
11753 else
11754 {
11755 ++input_line_pointer;
d9a62219 11756 while (isdigit ((unsigned char) *input_line_pointer))
252b5132
RH
11757 {
11758 val <<= 3;
11759 val |= *input_line_pointer++ - '0';
11760 }
11761 return negative ? -val : val;
11762 }
11763 }
d9a62219 11764 if (!isdigit ((unsigned char) *input_line_pointer))
252b5132
RH
11765 {
11766 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
11767 *input_line_pointer, *input_line_pointer);
11768 as_warn (_("Invalid number"));
11769 return -1;
11770 }
d9a62219 11771 while (isdigit ((unsigned char) *input_line_pointer))
252b5132
RH
11772 {
11773 val *= 10;
11774 val += *input_line_pointer++ - '0';
11775 }
11776 return negative ? -val : val;
11777}
11778
11779/* The .file directive; just like the usual .file directive, but there
11780 is an initial number which is the ECOFF file index. */
11781
11782static void
11783s_file (x)
43841e91 11784 int x ATTRIBUTE_UNUSED;
252b5132
RH
11785{
11786 int line;
11787
11788 line = get_number ();
11789 s_app_file (0);
11790}
11791
252b5132
RH
11792/* The .end directive. */
11793
11794static void
11795s_mips_end (x)
43841e91 11796 int x ATTRIBUTE_UNUSED;
252b5132
RH
11797{
11798 symbolS *p;
11799 int maybe_text;
11800
11801 if (!is_end_of_line[(unsigned char) *input_line_pointer])
11802 {
11803 p = get_symbol ();
11804 demand_empty_rest_of_line ();
11805 }
11806 else
11807 p = NULL;
11808
11809#ifdef BFD_ASSEMBLER
11810 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
11811 maybe_text = 1;
11812 else
11813 maybe_text = 0;
11814#else
11815 if (now_seg != data_section && now_seg != bss_section)
11816 maybe_text = 1;
11817 else
11818 maybe_text = 0;
11819#endif
11820
11821 if (!maybe_text)
11822 as_warn (_(".end not in text section"));
11823
11824 if (!cur_proc_ptr)
11825 {
11826 as_warn (_(".end directive without a preceding .ent directive."));
11827 demand_empty_rest_of_line ();
11828 return;
11829 }
11830
11831 if (p != NULL)
11832 {
11833 assert (S_GET_NAME (p));
11834 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
11835 as_warn (_(".end symbol does not match .ent symbol."));
11836 }
11837 else
11838 as_warn (_(".end directive missing or unknown symbol"));
11839
11840#ifdef MIPS_STABS_ELF
11841 {
11842 segT saved_seg = now_seg;
11843 subsegT saved_subseg = now_subseg;
252b5132 11844 valueT dot;
252b5132
RH
11845 expressionS exp;
11846 char *fragp;
11847
11848 dot = frag_now_fix ();
11849
11850#ifdef md_flush_pending_output
11851 md_flush_pending_output ();
11852#endif
11853
11854 assert (pdr_seg);
11855 subseg_set (pdr_seg, 0);
11856
beae10d5 11857 /* Write the symbol. */
252b5132
RH
11858 exp.X_op = O_symbol;
11859 exp.X_add_symbol = p;
11860 exp.X_add_number = 0;
11861 emit_expr (&exp, 4);
11862
beae10d5 11863 fragp = frag_more (7 * 4);
252b5132 11864
beae10d5
KH
11865 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
11866 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
11867 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
11868 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
11869 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
11870 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
11871 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
252b5132
RH
11872
11873 subseg_set (saved_seg, saved_subseg);
11874 }
11875#endif
11876
11877 cur_proc_ptr = NULL;
11878}
11879
11880/* The .aent and .ent directives. */
11881
11882static void
11883s_mips_ent (aent)
11884 int aent;
11885{
11886 int number = 0;
11887 symbolS *symbolP;
11888 int maybe_text;
11889
11890 symbolP = get_symbol ();
11891 if (*input_line_pointer == ',')
11892 input_line_pointer++;
11893 SKIP_WHITESPACE ();
d9a62219
DE
11894 if (isdigit ((unsigned char) *input_line_pointer)
11895 || *input_line_pointer == '-')
252b5132
RH
11896 number = get_number ();
11897
11898#ifdef BFD_ASSEMBLER
11899 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
11900 maybe_text = 1;
11901 else
11902 maybe_text = 0;
11903#else
11904 if (now_seg != data_section && now_seg != bss_section)
11905 maybe_text = 1;
11906 else
11907 maybe_text = 0;
11908#endif
11909
11910 if (!maybe_text)
11911 as_warn (_(".ent or .aent not in text section."));
11912
11913 if (!aent && cur_proc_ptr)
9a41af64 11914 as_warn (_("missing .end"));
252b5132
RH
11915
11916 if (!aent)
11917 {
11918 cur_proc_ptr = &cur_proc;
11919 memset (cur_proc_ptr, '\0', sizeof (procS));
11920
11921 cur_proc_ptr->isym = symbolP;
11922
49309057 11923 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132
RH
11924
11925 numprocs++;
11926 }
11927
11928 demand_empty_rest_of_line ();
11929}
11930
11931/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 11932 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 11933 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 11934 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
11935 symbol table (in the mdebug section). */
11936
11937static void
11938s_mips_frame (ignore)
2b3c5a5d 11939 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11940{
11941#ifdef MIPS_STABS_ELF
11942
11943 long val;
11944
beae10d5 11945 if (cur_proc_ptr == (procS *) NULL)
252b5132
RH
11946 {
11947 as_warn (_(".frame outside of .ent"));
11948 demand_empty_rest_of_line ();
11949 return;
11950 }
11951
11952 cur_proc_ptr->frame_reg = tc_get_register (1);
11953
11954 SKIP_WHITESPACE ();
11955 if (*input_line_pointer++ != ','
11956 || get_absolute_expression_and_terminator (&val) != ',')
11957 {
11958 as_warn (_("Bad .frame directive"));
11959 --input_line_pointer;
11960 demand_empty_rest_of_line ();
11961 return;
11962 }
11963
11964 cur_proc_ptr->frame_offset = val;
11965 cur_proc_ptr->pc_reg = tc_get_register (0);
11966
11967 demand_empty_rest_of_line ();
11968#else
11969 s_ignore (ignore);
11970#endif /* MIPS_STABS_ELF */
11971}
11972
bdaaa2e1
KH
11973/* The .fmask and .mask directives. If the mdebug section is present
11974 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 11975 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 11976 information correctly. We can't use the ecoff routines because they
252b5132
RH
11977 make reference to the ecoff symbol table (in the mdebug section). */
11978
11979static void
11980s_mips_mask (reg_type)
11981 char reg_type;
11982{
11983#ifdef MIPS_STABS_ELF
11984 long mask, off;
bdaaa2e1 11985
252b5132
RH
11986 if (cur_proc_ptr == (procS *) NULL)
11987 {
11988 as_warn (_(".mask/.fmask outside of .ent"));
11989 demand_empty_rest_of_line ();
11990 return;
11991 }
11992
11993 if (get_absolute_expression_and_terminator (&mask) != ',')
11994 {
11995 as_warn (_("Bad .mask/.fmask directive"));
11996 --input_line_pointer;
11997 demand_empty_rest_of_line ();
11998 return;
11999 }
12000
12001 off = get_absolute_expression ();
12002
12003 if (reg_type == 'F')
12004 {
12005 cur_proc_ptr->fpreg_mask = mask;
12006 cur_proc_ptr->fpreg_offset = off;
12007 }
12008 else
12009 {
12010 cur_proc_ptr->reg_mask = mask;
12011 cur_proc_ptr->reg_offset = off;
12012 }
12013
12014 demand_empty_rest_of_line ();
12015#else
12016 s_ignore (reg_type);
12017#endif /* MIPS_STABS_ELF */
12018}
12019
12020/* The .loc directive. */
12021
12022#if 0
12023static void
12024s_loc (x)
12025 int x;
12026{
12027 symbolS *symbolP;
12028 int lineno;
12029 int addroff;
12030
12031 assert (now_seg == text_section);
12032
12033 lineno = get_number ();
12034 addroff = frag_now_fix ();
12035
12036 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
12037 S_SET_TYPE (symbolP, N_SLINE);
12038 S_SET_OTHER (symbolP, 0);
12039 S_SET_DESC (symbolP, lineno);
12040 symbolP->sy_segment = now_seg;
12041}
12042#endif
e7af610e
NC
12043
12044/* CPU name/ISA/number mapping table.
12045
12046 Entries are grouped by type. The first matching CPU or ISA entry
12047 gets chosen by CPU or ISA, so it should be the 'canonical' name
12048 for that type. Entries after that within the type are sorted
12049 alphabetically.
12050
12051 Case is ignored in comparison, so put the canonical entry in the
12052 appropriate case but everything else in lower case to ease eye pain. */
e972090a
NC
12053static const struct mips_cpu_info mips_cpu_info_table[] =
12054{
e7af610e
NC
12055 /* MIPS1 ISA */
12056 { "MIPS1", 1, ISA_MIPS1, CPU_R3000, },
12057 { "mips", 1, ISA_MIPS1, CPU_R3000, },
12058
12059 /* MIPS2 ISA */
12060 { "MIPS2", 1, ISA_MIPS2, CPU_R6000, },
12061
12062 /* MIPS3 ISA */
12063 { "MIPS3", 1, ISA_MIPS3, CPU_R4000, },
12064
12065 /* MIPS4 ISA */
12066 { "MIPS4", 1, ISA_MIPS4, CPU_R8000, },
12067
84ea6cf2
NC
12068 /* MIPS5 ISA */
12069 { "MIPS5", 1, ISA_MIPS5, CPU_MIPS5, },
12070 { "Generic-MIPS5", 0, ISA_MIPS5, CPU_MIPS5, },
12071
e7af610e
NC
12072 /* MIPS32 ISA */
12073 { "MIPS32", 1, ISA_MIPS32, CPU_MIPS32, },
12074 { "Generic-MIPS32", 0, ISA_MIPS32, CPU_MIPS32, },
12075
84ea6cf2 12076#if 1
e7af610e
NC
12077 /* XXX for now, MIPS64 -> MIPS3 because of history */
12078 { "MIPS64", 1, ISA_MIPS3, CPU_R4000 }, /* XXX! */
84ea6cf2
NC
12079#else
12080 /* MIPS64 ISA */
12081 { "MIPS64", 1, ISA_MIPS64, CPU_MIPS64 },
12082#endif
12083 { "mips64isa", 1, ISA_MIPS64, CPU_MIPS64 },
12084 { "Generic-MIPS64", 0, ISA_MIPS64, CPU_MIPS64, },
e7af610e
NC
12085
12086 /* R2000 CPU */
12087 { "R2000", 0, ISA_MIPS1, CPU_R2000, },
12088 { "2000", 0, ISA_MIPS1, CPU_R2000, },
12089 { "2k", 0, ISA_MIPS1, CPU_R2000, },
12090 { "r2k", 0, ISA_MIPS1, CPU_R2000, },
12091
12092 /* R3000 CPU */
12093 { "R3000", 0, ISA_MIPS1, CPU_R3000, },
12094 { "3000", 0, ISA_MIPS1, CPU_R3000, },
12095 { "3k", 0, ISA_MIPS1, CPU_R3000, },
12096 { "r3k", 0, ISA_MIPS1, CPU_R3000, },
12097
12098 /* TX3900 CPU */
12099 { "R3900", 0, ISA_MIPS1, CPU_R3900, },
12100 { "3900", 0, ISA_MIPS1, CPU_R3900, },
e972090a 12101 { "mipstx39", 0, ISA_MIPS1, CPU_R3900, },
e7af610e
NC
12102
12103 /* R4000 CPU */
12104 { "R4000", 0, ISA_MIPS3, CPU_R4000, },
12105 { "4000", 0, ISA_MIPS3, CPU_R4000, },
12106 { "4k", 0, ISA_MIPS3, CPU_R4000, }, /* beware */
12107 { "r4k", 0, ISA_MIPS3, CPU_R4000, },
12108
12109 /* R4010 CPU */
12110 { "R4010", 0, ISA_MIPS2, CPU_R4010, },
12111 { "4010", 0, ISA_MIPS2, CPU_R4010, },
12112
12113 /* R4400 CPU */
12114 { "R4400", 0, ISA_MIPS3, CPU_R4400, },
12115 { "4400", 0, ISA_MIPS3, CPU_R4400, },
12116
12117 /* R4600 CPU */
12118 { "R4600", 0, ISA_MIPS3, CPU_R4600, },
12119 { "4600", 0, ISA_MIPS3, CPU_R4600, },
12120 { "mips64orion", 0, ISA_MIPS3, CPU_R4600, },
12121 { "orion", 0, ISA_MIPS3, CPU_R4600, },
12122
12123 /* R4650 CPU */
12124 { "R4650", 0, ISA_MIPS3, CPU_R4650, },
12125 { "4650", 0, ISA_MIPS3, CPU_R4650, },
12126
12127 /* R6000 CPU */
12128 { "R6000", 0, ISA_MIPS2, CPU_R6000, },
12129 { "6000", 0, ISA_MIPS2, CPU_R6000, },
12130 { "6k", 0, ISA_MIPS2, CPU_R6000, },
12131 { "r6k", 0, ISA_MIPS2, CPU_R6000, },
12132
12133 /* R8000 CPU */
12134 { "R8000", 0, ISA_MIPS4, CPU_R8000, },
12135 { "8000", 0, ISA_MIPS4, CPU_R8000, },
12136 { "8k", 0, ISA_MIPS4, CPU_R8000, },
12137 { "r8k", 0, ISA_MIPS4, CPU_R8000, },
12138
12139 /* R10000 CPU */
12140 { "R10000", 0, ISA_MIPS4, CPU_R10000, },
12141 { "10000", 0, ISA_MIPS4, CPU_R10000, },
12142 { "10k", 0, ISA_MIPS4, CPU_R10000, },
12143 { "r10k", 0, ISA_MIPS4, CPU_R10000, },
12144
d1cf510e
NC
12145 /* R12000 CPU */
12146 { "R12000", 0, ISA_MIPS4, CPU_R12000, },
12147 { "12000", 0, ISA_MIPS4, CPU_R12000, },
12148 { "12k", 0, ISA_MIPS4, CPU_R12000, },
12149 { "r12k", 0, ISA_MIPS4, CPU_R12000, },
12150
e7af610e
NC
12151 /* VR4100 CPU */
12152 { "VR4100", 0, ISA_MIPS3, CPU_VR4100, },
12153 { "4100", 0, ISA_MIPS3, CPU_VR4100, },
12154 { "mips64vr4100", 0, ISA_MIPS3, CPU_VR4100, },
12155 { "r4100", 0, ISA_MIPS3, CPU_VR4100, },
12156
12157 /* VR4111 CPU */
12158 { "VR4111", 0, ISA_MIPS3, CPU_R4111, },
12159 { "4111", 0, ISA_MIPS3, CPU_R4111, },
12160 { "mips64vr4111", 0, ISA_MIPS3, CPU_R4111, },
12161 { "r4111", 0, ISA_MIPS3, CPU_R4111, },
12162
12163 /* VR4300 CPU */
12164 { "VR4300", 0, ISA_MIPS3, CPU_R4300, },
12165 { "4300", 0, ISA_MIPS3, CPU_R4300, },
12166 { "mips64vr4300", 0, ISA_MIPS3, CPU_R4300, },
12167 { "r4300", 0, ISA_MIPS3, CPU_R4300, },
12168
12169 /* VR5000 CPU */
12170 { "VR5000", 0, ISA_MIPS4, CPU_R5000, },
12171 { "5000", 0, ISA_MIPS4, CPU_R5000, },
12172 { "5k", 0, ISA_MIPS4, CPU_R5000, },
12173 { "mips64vr5000", 0, ISA_MIPS4, CPU_R5000, },
12174 { "r5000", 0, ISA_MIPS4, CPU_R5000, },
12175 { "r5200", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 12176 { "rm5200", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 12177 { "r5230", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 12178 { "rm5230", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 12179 { "r5231", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 12180 { "rm5231", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 12181 { "r5261", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 12182 { "rm5261", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 12183 { "r5721", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 12184 { "rm5721", 0, ISA_MIPS4, CPU_R5000, },
e7af610e
NC
12185 { "r5k", 0, ISA_MIPS4, CPU_R5000, },
12186 { "r7000", 0, ISA_MIPS4, CPU_R5000, },
12187
12188 /* MIPS32 4K CPU */
12189 { "MIPS32-4K", 0, ISA_MIPS32, CPU_MIPS32_4K, },
12190 { "4kc", 0, ISA_MIPS32, CPU_MIPS32_4K, },
12191 { "4km", 0, ISA_MIPS32, CPU_MIPS32_4K, },
12192 { "4kp", 0, ISA_MIPS32, CPU_MIPS32_4K, },
12193 { "mips32-4kc", 0, ISA_MIPS32, CPU_MIPS32_4K, },
12194 { "mips32-4km", 0, ISA_MIPS32, CPU_MIPS32_4K, },
12195 { "mips32-4kp", 0, ISA_MIPS32, CPU_MIPS32_4K, },
12196
c6c98b38
NC
12197 /* SiByte SB-1 CPU */
12198 { "SB-1", 0, ISA_MIPS64, CPU_SB1, },
e972090a 12199 { "sb-1250", 0, ISA_MIPS64, CPU_SB1, },
c6c98b38
NC
12200 { "sb1", 0, ISA_MIPS64, CPU_SB1, },
12201 { "sb1250", 0, ISA_MIPS64, CPU_SB1, },
12202
beae10d5 12203 /* End marker. */
e7af610e
NC
12204 { NULL, 0, 0, 0, },
12205};
12206
12207static const struct mips_cpu_info *
12208mips_cpu_info_from_name (name)
12209 const char *name;
12210{
12211 int i;
12212
12213 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
beae10d5 12214 if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
e7af610e
NC
12215 return (&mips_cpu_info_table[i]);
12216
e972090a 12217 return NULL;
e7af610e
NC
12218}
12219
12220static const struct mips_cpu_info *
12221mips_cpu_info_from_isa (isa)
12222 int isa;
12223{
12224 int i;
12225
12226 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
12227 if (mips_cpu_info_table[i].is_isa
12228 && isa == mips_cpu_info_table[i].isa)
12229 return (&mips_cpu_info_table[i]);
12230
e972090a 12231 return NULL;
e7af610e
NC
12232}
12233
12234static const struct mips_cpu_info *
12235mips_cpu_info_from_cpu (cpu)
12236 int cpu;
12237{
12238 int i;
12239
12240 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
12241 if (!mips_cpu_info_table[i].is_isa
12242 && cpu == mips_cpu_info_table[i].cpu)
12243 return (&mips_cpu_info_table[i]);
12244
e972090a 12245 return NULL;
e7af610e 12246}
This page took 1.008921 seconds and 4 git commands to generate.