]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-mips.c
* readelf.c (struct unw_aux_info): Remove const from the info member.
[thirdparty/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
82efde3a
KH
2 Copyright (C) 1993, 94, 95, 96, 97, 98, 1999, 2000
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
beae10d5 129 ? "elf32-tradbigmips" : "elf32-tradlittlemips");
8614eeee 130#else
252b5132
RH
131 return (target_big_endian
132 ? (mips_64 ? "elf64-bigmips" : "elf32-bigmips")
133 : (mips_64 ? "elf64-littlemips" : "elf32-littlemips"));
8614eeee 134#endif
252b5132
RH
135 default:
136 abort ();
137 return NULL;
138 }
139}
140
141/* The name of the readonly data section. */
142#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
143 ? ".data" \
144 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
145 ? ".rdata" \
056350c6
NC
146 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
147 ? ".rdata" \
252b5132
RH
148 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
149 ? ".rodata" \
150 : (abort (), ""))
151
152/* This is the set of options which may be modified by the .set
153 pseudo-op. We use a struct so that .set push and .set pop are more
154 reliable. */
155
e972090a
NC
156struct mips_set_options
157{
252b5132
RH
158 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
159 if it has not been initialized. Changed by `.set mipsN', and the
160 -mipsN command line option, and the default CPU. */
161 int isa;
162 /* Whether we are assembling for the mips16 processor. 0 if we are
163 not, 1 if we are, and -1 if the value has not been initialized.
164 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
165 -nomips16 command line options, and the default CPU. */
166 int mips16;
167 /* Non-zero if we should not reorder instructions. Changed by `.set
168 reorder' and `.set noreorder'. */
169 int noreorder;
170 /* Non-zero if we should not permit the $at ($1) register to be used
171 in instructions. Changed by `.set at' and `.set noat'. */
172 int noat;
173 /* Non-zero if we should warn when a macro instruction expands into
174 more than one machine instruction. Changed by `.set nomacro' and
175 `.set macro'. */
176 int warn_about_macros;
177 /* Non-zero if we should not move instructions. Changed by `.set
178 move', `.set volatile', `.set nomove', and `.set novolatile'. */
179 int nomove;
180 /* Non-zero if we should not optimize branches by moving the target
181 of the branch into the delay slot. Actually, we don't perform
182 this optimization anyhow. Changed by `.set bopt' and `.set
183 nobopt'. */
184 int nobopt;
185 /* Non-zero if we should not autoextend mips16 instructions.
186 Changed by `.set autoextend' and `.set noautoextend'. */
187 int noautoextend;
188};
189
190/* This is the struct we use to hold the current set of options. Note
e7af610e
NC
191 that we must set the isa field to ISA_UNKNOWN and the mips16 field to
192 -1 to indicate that they have not been initialized. */
252b5132 193
e972090a
NC
194static struct mips_set_options mips_opts =
195{
e7af610e
NC
196 ISA_UNKNOWN, -1, 0, 0, 0, 0, 0, 0
197};
252b5132
RH
198
199/* These variables are filled in with the masks of registers used.
200 The object format code reads them and puts them in the appropriate
201 place. */
202unsigned long mips_gprmask;
203unsigned long mips_cprmask[4];
204
205/* MIPS ISA we are using for this output file. */
e7af610e 206static int file_mips_isa = ISA_UNKNOWN;
252b5132 207
e7af610e
NC
208/* The CPU type we are using for this output file. */
209static int mips_cpu = CPU_UNKNOWN;
252b5132 210
bdaaa2e1 211/* The argument of the -mabi= flag. */
e972090a 212static char * mips_abi_string = 0;
252b5132 213
bdaaa2e1 214/* Wether we should mark the file EABI64 or EABI32. */
252b5132
RH
215static int mips_eabi64 = 0;
216
217/* If they asked for mips1 or mips2 and a cpu that is
bdaaa2e1 218 mips3 or greater, then mark the object file 32BITMODE. */
252b5132
RH
219static int mips_32bitmode = 0;
220
bdaaa2e1 221/* True if -mgp32 was passed. */
c97ef257
AH
222static int mips_gp32 = 0;
223
9ce8a5dd
GRK
224/* Some ISA's have delay slots for instructions which read or write
225 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
bdaaa2e1 226 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
9ce8a5dd
GRK
227 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
228 delay slot in this ISA. The uses of this macro assume that any
229 ISA that has delay slots for one of these, has them for all. They
230 also assume that ISAs which don't have delays for these insns, don't
bdaaa2e1 231 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
9ce8a5dd 232#define ISA_HAS_COPROC_DELAYS(ISA) ( \
e7af610e
NC
233 (ISA) == ISA_MIPS1 \
234 || (ISA) == ISA_MIPS2 \
235 || (ISA) == ISA_MIPS3 \
9ce8a5dd
GRK
236 )
237
bdaaa2e1 238/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 239#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
240 (ISA) == ISA_MIPS3 \
241 || (ISA) == ISA_MIPS4 \
84ea6cf2
NC
242 || (ISA) == ISA_MIPS5 \
243 || (ISA) == ISA_MIPS32 \
9ce8a5dd
GRK
244 )
245
bdaaa2e1 246/* Whether the processor uses hardware interlocks to protect
252b5132
RH
247 reads from the HI and LO registers, and thus does not
248 require nops to be inserted.
249
250 FIXME: GCC makes a distinction between -mcpu=FOO and -mFOO:
251 -mcpu=FOO schedules for FOO, but still produces code that meets the
252 requirements of MIPS ISA I. For example, it won't generate any
253 FOO-specific instructions, and it will still assume that any
254 scheduling hazards described in MIPS ISA I are there, even if FOO
255 has interlocks. -mFOO gives GCC permission to generate code that
256 will only run on a FOO; it will generate FOO-specific instructions,
257 and assume interlocks provided by a FOO.
258
259 However, GAS currently doesn't make this distinction; before Jan 28
260 1999, GAS's -mcpu=FOO implied -mFOO, which violates GCC's
261 assumptions. The GCC driver passes these flags through to GAS, so
262 if GAS actually does anything that doesn't meet MIPS ISA I with
263 -mFOO, then GCC's -mcpu=FOO flag isn't going to work.
264
265 And furthermore, it did not assume that -mFOO implied -mcpu=FOO,
266 which seems senseless --- why generate code which will only run on
267 a FOO, but schedule for something else?
268
269 So now, at least, -mcpu=FOO and -mFOO are exactly equivalent.
270
271 -- Jim Blandy <jimb@cygnus.com> */
272
156c2f8b 273#define hilo_interlocks (mips_cpu == CPU_R4010 \
252b5132
RH
274 )
275
276/* Whether the processor uses hardware interlocks to protect reads
277 from the GPRs, and thus does not require nops to be inserted. */
278#define gpr_interlocks \
e7af610e 279 (mips_opts.isa != ISA_MIPS1 \
156c2f8b 280 || mips_cpu == CPU_R3900)
252b5132
RH
281
282/* As with other "interlocks" this is used by hardware that has FP
283 (co-processor) interlocks. */
bdaaa2e1 284/* Itbl support may require additional care here. */
156c2f8b 285#define cop_interlocks (mips_cpu == CPU_R4300 \
252b5132
RH
286 )
287
6b76fefe
CM
288/* Is this a mfhi or mflo instruction? */
289#define MF_HILO_INSN(PINFO) \
290 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
291
252b5132
RH
292/* MIPS PIC level. */
293
e972090a
NC
294enum mips_pic_level
295{
252b5132
RH
296 /* Do not generate PIC code. */
297 NO_PIC,
298
299 /* Generate PIC code as in Irix 4. This is not implemented, and I'm
300 not sure what it is supposed to do. */
301 IRIX4_PIC,
302
303 /* Generate PIC code as in the SVR4 MIPS ABI. */
304 SVR4_PIC,
305
306 /* Generate PIC code without using a global offset table: the data
307 segment has a maximum size of 64K, all data references are off
308 the $gp register, and all text references are PC relative. This
309 is used on some embedded systems. */
310 EMBEDDED_PIC
311};
312
313static enum mips_pic_level mips_pic;
314
315/* 1 if we should generate 32 bit offsets from the GP register in
316 SVR4_PIC mode. Currently has no meaning in other modes. */
317static int mips_big_got;
318
319/* 1 if trap instructions should used for overflow rather than break
320 instructions. */
321static int mips_trap;
322
119d663a
NC
323/* 1 if double width floating point constants should not be constructed
324 by a assembling two single width halves into two single width floating
325 point registers which just happen to alias the double width destination
326 register. On some architectures this aliasing can be disabled by a bit
d547a75e 327 in the status register, and the setting of this bit cannot be determined
119d663a
NC
328 automatically at assemble time. */
329static int mips_disable_float_construction;
330
252b5132
RH
331/* Non-zero if any .set noreorder directives were used. */
332
333static int mips_any_noreorder;
334
6b76fefe
CM
335/* Non-zero if nops should be inserted when the register referenced in
336 an mfhi/mflo instruction is read in the next two instructions. */
337static int mips_7000_hilo_fix;
338
252b5132 339/* The size of the small data section. */
156c2f8b 340static unsigned int g_switch_value = 8;
252b5132
RH
341/* Whether the -G option was used. */
342static int g_switch_seen = 0;
343
344#define N_RMASK 0xc4
345#define N_VFP 0xd4
346
347/* If we can determine in advance that GP optimization won't be
348 possible, we can skip the relaxation stuff that tries to produce
349 GP-relative references. This makes delay slot optimization work
350 better.
351
352 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
353 gcc output. It needs to guess right for gcc, otherwise gcc
354 will put what it thinks is a GP-relative instruction in a branch
355 delay slot.
252b5132
RH
356
357 I don't know if a fix is needed for the SVR4_PIC mode. I've only
358 fixed it for the non-PIC mode. KR 95/04/07 */
359static int nopic_need_relax PARAMS ((symbolS *, int));
360
361/* handle of the OPCODE hash table */
362static struct hash_control *op_hash = NULL;
363
364/* The opcode hash table we use for the mips16. */
365static struct hash_control *mips16_op_hash = NULL;
366
367/* This array holds the chars that always start a comment. If the
368 pre-processor is disabled, these aren't very useful */
369const char comment_chars[] = "#";
370
371/* This array holds the chars that only start a comment at the beginning of
372 a line. If the line seems to have the form '# 123 filename'
373 .line and .file directives will appear in the pre-processed output */
374/* Note that input_file.c hand checks for '#' at the beginning of the
375 first line of the input file. This is because the compiler outputs
bdaaa2e1 376 #NO_APP at the beginning of its output. */
252b5132
RH
377/* Also note that C style comments are always supported. */
378const char line_comment_chars[] = "#";
379
bdaaa2e1 380/* This array holds machine specific line separator characters. */
63a0b638 381const char line_separator_chars[] = ";";
252b5132
RH
382
383/* Chars that can be used to separate mant from exp in floating point nums */
384const char EXP_CHARS[] = "eE";
385
386/* Chars that mean this number is a floating point constant */
387/* As in 0f12.456 */
388/* or 0d1.2345e12 */
389const char FLT_CHARS[] = "rRsSfFdDxXpP";
390
391/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
392 changed in read.c . Ideally it shouldn't have to know about it at all,
393 but nothing is ideal around here.
394 */
395
396static char *insn_error;
397
398static int auto_align = 1;
399
400/* When outputting SVR4 PIC code, the assembler needs to know the
401 offset in the stack frame from which to restore the $gp register.
402 This is set by the .cprestore pseudo-op, and saved in this
403 variable. */
404static offsetT mips_cprestore_offset = -1;
405
406/* This is the register which holds the stack frame, as set by the
407 .frame pseudo-op. This is needed to implement .cprestore. */
408static int mips_frame_reg = SP;
409
410/* To output NOP instructions correctly, we need to keep information
411 about the previous two instructions. */
412
413/* Whether we are optimizing. The default value of 2 means to remove
414 unneeded NOPs and swap branch instructions when possible. A value
415 of 1 means to not swap branches. A value of 0 means to always
416 insert NOPs. */
417static int mips_optimize = 2;
418
419/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
420 equivalent to seeing no -g option at all. */
421static int mips_debug = 0;
422
423/* The previous instruction. */
424static struct mips_cl_insn prev_insn;
425
426/* The instruction before prev_insn. */
427static struct mips_cl_insn prev_prev_insn;
428
429/* If we don't want information for prev_insn or prev_prev_insn, we
430 point the insn_mo field at this dummy integer. */
43841e91 431static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
432
433/* Non-zero if prev_insn is valid. */
434static int prev_insn_valid;
435
436/* The frag for the previous instruction. */
437static struct frag *prev_insn_frag;
438
439/* The offset into prev_insn_frag for the previous instruction. */
440static long prev_insn_where;
441
442/* The reloc type for the previous instruction, if any. */
443static bfd_reloc_code_real_type prev_insn_reloc_type;
444
445/* The reloc for the previous instruction, if any. */
446static fixS *prev_insn_fixp;
447
448/* Non-zero if the previous instruction was in a delay slot. */
449static int prev_insn_is_delay_slot;
450
451/* Non-zero if the previous instruction was in a .set noreorder. */
452static int prev_insn_unreordered;
453
454/* Non-zero if the previous instruction uses an extend opcode (if
455 mips16). */
456static int prev_insn_extended;
457
458/* Non-zero if the previous previous instruction was in a .set
459 noreorder. */
460static int prev_prev_insn_unreordered;
461
462/* If this is set, it points to a frag holding nop instructions which
463 were inserted before the start of a noreorder section. If those
464 nops turn out to be unnecessary, the size of the frag can be
465 decreased. */
466static fragS *prev_nop_frag;
467
468/* The number of nop instructions we created in prev_nop_frag. */
469static int prev_nop_frag_holds;
470
471/* The number of nop instructions that we know we need in
bdaaa2e1 472 prev_nop_frag. */
252b5132
RH
473static int prev_nop_frag_required;
474
475/* The number of instructions we've seen since prev_nop_frag. */
476static int prev_nop_frag_since;
477
478/* For ECOFF and ELF, relocations against symbols are done in two
479 parts, with a HI relocation and a LO relocation. Each relocation
480 has only 16 bits of space to store an addend. This means that in
481 order for the linker to handle carries correctly, it must be able
482 to locate both the HI and the LO relocation. This means that the
483 relocations must appear in order in the relocation table.
484
485 In order to implement this, we keep track of each unmatched HI
486 relocation. We then sort them so that they immediately precede the
bdaaa2e1 487 corresponding LO relocation. */
252b5132 488
e972090a
NC
489struct mips_hi_fixup
490{
252b5132
RH
491 /* Next HI fixup. */
492 struct mips_hi_fixup *next;
493 /* This fixup. */
494 fixS *fixp;
495 /* The section this fixup is in. */
496 segT seg;
497};
498
499/* The list of unmatched HI relocs. */
500
501static struct mips_hi_fixup *mips_hi_fixup_list;
502
503/* Map normal MIPS register numbers to mips16 register numbers. */
504
505#define X ILLEGAL_REG
e972090a
NC
506static const int mips32_to_16_reg_map[] =
507{
252b5132
RH
508 X, X, 2, 3, 4, 5, 6, 7,
509 X, X, X, X, X, X, X, X,
510 0, 1, X, X, X, X, X, X,
511 X, X, X, X, X, X, X, X
512};
513#undef X
514
515/* Map mips16 register numbers to normal MIPS register numbers. */
516
e972090a
NC
517static const unsigned int mips16_to_32_reg_map[] =
518{
252b5132
RH
519 16, 17, 2, 3, 4, 5, 6, 7
520};
521\f
522/* Since the MIPS does not have multiple forms of PC relative
523 instructions, we do not have to do relaxing as is done on other
524 platforms. However, we do have to handle GP relative addressing
525 correctly, which turns out to be a similar problem.
526
527 Every macro that refers to a symbol can occur in (at least) two
528 forms, one with GP relative addressing and one without. For
529 example, loading a global variable into a register generally uses
530 a macro instruction like this:
531 lw $4,i
532 If i can be addressed off the GP register (this is true if it is in
533 the .sbss or .sdata section, or if it is known to be smaller than
534 the -G argument) this will generate the following instruction:
535 lw $4,i($gp)
536 This instruction will use a GPREL reloc. If i can not be addressed
537 off the GP register, the following instruction sequence will be used:
538 lui $at,i
539 lw $4,i($at)
540 In this case the first instruction will have a HI16 reloc, and the
541 second reloc will have a LO16 reloc. Both relocs will be against
542 the symbol i.
543
544 The issue here is that we may not know whether i is GP addressable
545 until after we see the instruction that uses it. Therefore, we
546 want to be able to choose the final instruction sequence only at
547 the end of the assembly. This is similar to the way other
548 platforms choose the size of a PC relative instruction only at the
549 end of assembly.
550
551 When generating position independent code we do not use GP
552 addressing in quite the same way, but the issue still arises as
553 external symbols and local symbols must be handled differently.
554
555 We handle these issues by actually generating both possible
556 instruction sequences. The longer one is put in a frag_var with
557 type rs_machine_dependent. We encode what to do with the frag in
558 the subtype field. We encode (1) the number of existing bytes to
559 replace, (2) the number of new bytes to use, (3) the offset from
560 the start of the existing bytes to the first reloc we must generate
561 (that is, the offset is applied from the start of the existing
562 bytes after they are replaced by the new bytes, if any), (4) the
563 offset from the start of the existing bytes to the second reloc,
564 (5) whether a third reloc is needed (the third reloc is always four
565 bytes after the second reloc), and (6) whether to warn if this
566 variant is used (this is sometimes needed if .set nomacro or .set
567 noat is in effect). All these numbers are reasonably small.
568
569 Generating two instruction sequences must be handled carefully to
570 ensure that delay slots are handled correctly. Fortunately, there
571 are a limited number of cases. When the second instruction
572 sequence is generated, append_insn is directed to maintain the
573 existing delay slot information, so it continues to apply to any
574 code after the second instruction sequence. This means that the
575 second instruction sequence must not impose any requirements not
576 required by the first instruction sequence.
577
578 These variant frags are then handled in functions called by the
579 machine independent code. md_estimate_size_before_relax returns
580 the final size of the frag. md_convert_frag sets up the final form
581 of the frag. tc_gen_reloc adjust the first reloc and adds a second
582 one if needed. */
583#define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
584 ((relax_substateT) \
585 (((old) << 23) \
586 | ((new) << 16) \
587 | (((reloc1) + 64) << 9) \
588 | (((reloc2) + 64) << 2) \
589 | ((reloc3) ? (1 << 1) : 0) \
590 | ((warn) ? 1 : 0)))
591#define RELAX_OLD(i) (((i) >> 23) & 0x7f)
592#define RELAX_NEW(i) (((i) >> 16) & 0x7f)
beae10d5
KH
593#define RELAX_RELOC1(i) ((bfd_vma) (((i) >> 9) & 0x7f) - 64)
594#define RELAX_RELOC2(i) ((bfd_vma) (((i) >> 2) & 0x7f) - 64)
252b5132
RH
595#define RELAX_RELOC3(i) (((i) >> 1) & 1)
596#define RELAX_WARN(i) ((i) & 1)
597
598/* For mips16 code, we use an entirely different form of relaxation.
599 mips16 supports two versions of most instructions which take
600 immediate values: a small one which takes some small value, and a
601 larger one which takes a 16 bit value. Since branches also follow
602 this pattern, relaxing these values is required.
603
604 We can assemble both mips16 and normal MIPS code in a single
605 object. Therefore, we need to support this type of relaxation at
606 the same time that we support the relaxation described above. We
607 use the high bit of the subtype field to distinguish these cases.
608
609 The information we store for this type of relaxation is the
610 argument code found in the opcode file for this relocation, whether
611 the user explicitly requested a small or extended form, and whether
612 the relocation is in a jump or jal delay slot. That tells us the
613 size of the value, and how it should be stored. We also store
614 whether the fragment is considered to be extended or not. We also
615 store whether this is known to be a branch to a different section,
616 whether we have tried to relax this frag yet, and whether we have
617 ever extended a PC relative fragment because of a shift count. */
618#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
619 (0x80000000 \
620 | ((type) & 0xff) \
621 | ((small) ? 0x100 : 0) \
622 | ((ext) ? 0x200 : 0) \
623 | ((dslot) ? 0x400 : 0) \
624 | ((jal_dslot) ? 0x800 : 0))
625#define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
626#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
627#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
628#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
629#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
630#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
631#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
632#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
633#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
634#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
635#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
636#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
637\f
638/* Prototypes for static functions. */
639
640#ifdef __STDC__
641#define internalError() \
642 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
643#else
644#define internalError() as_fatal (_("MIPS internal Error"));
645#endif
646
647enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
648
649static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
650 unsigned int reg, enum mips_regclass class));
156c2f8b 651static int reg_needs_delay PARAMS ((unsigned int));
252b5132
RH
652static void mips16_mark_labels PARAMS ((void));
653static void append_insn PARAMS ((char *place,
654 struct mips_cl_insn * ip,
655 expressionS * p,
656 bfd_reloc_code_real_type r,
657 boolean));
658static void mips_no_prev_insn PARAMS ((int));
659static void mips_emit_delays PARAMS ((boolean));
660#ifdef USE_STDARG
661static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
662 const char *name, const char *fmt,
663 ...));
664#else
665static void macro_build ();
666#endif
667static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
668 const char *, const char *,
669 va_list));
670static void macro_build_lui PARAMS ((char *place, int *counter,
671 expressionS * ep, int regnum));
672static void set_at PARAMS ((int *counter, int reg, int unsignedp));
673static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
674 expressionS *));
675static void load_register PARAMS ((int *, int, expressionS *, int));
676static void load_address PARAMS ((int *counter, int reg, expressionS *ep));
677static void macro PARAMS ((struct mips_cl_insn * ip));
678static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
679#ifdef LOSING_COMPILER
680static void macro2 PARAMS ((struct mips_cl_insn * ip));
681#endif
682static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
683static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
684static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
685 boolean, boolean, unsigned long *,
686 boolean *, unsigned short *));
687static int my_getSmallExpression PARAMS ((expressionS * ep, char *str));
688static void my_getExpression PARAMS ((expressionS * ep, char *str));
689static symbolS *get_symbol PARAMS ((void));
690static void mips_align PARAMS ((int to, int fill, symbolS *label));
691static void s_align PARAMS ((int));
692static void s_change_sec PARAMS ((int));
693static void s_cons PARAMS ((int));
694static void s_float_cons PARAMS ((int));
695static void s_mips_globl PARAMS ((int));
696static void s_option PARAMS ((int));
697static void s_mipsset PARAMS ((int));
698static void s_abicalls PARAMS ((int));
699static void s_cpload PARAMS ((int));
700static void s_cprestore PARAMS ((int));
701static void s_gpword PARAMS ((int));
702static void s_cpadd PARAMS ((int));
703static void s_insn PARAMS ((int));
704static void md_obj_begin PARAMS ((void));
705static void md_obj_end PARAMS ((void));
706static long get_number PARAMS ((void));
707static void s_mips_ent PARAMS ((int));
708static void s_mips_end PARAMS ((int));
709static void s_mips_frame PARAMS ((int));
710static void s_mips_mask PARAMS ((int));
711static void s_mips_stab PARAMS ((int));
712static void s_mips_weakext PARAMS ((int));
713static void s_file PARAMS ((int));
714static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
e7af610e
NC
715static const char *mips_isa_to_str PARAMS ((int));
716static const char *mips_cpu_to_str PARAMS ((int));
252b5132 717static int validate_mips_insn PARAMS ((const struct mips_opcode *));
e7af610e
NC
718
719/* Table and functions used to map between CPU/ISA names, and
720 ISA levels, and CPU numbers. */
721
e972090a
NC
722struct mips_cpu_info
723{
e7af610e
NC
724 const char *name; /* CPU or ISA name. */
725 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
726 int isa; /* ISA level. */
727 int cpu; /* CPU number (default CPU if ISA). */
728};
729
730static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
731static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
732static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
252b5132
RH
733\f
734/* Pseudo-op table.
735
736 The following pseudo-ops from the Kane and Heinrich MIPS book
737 should be defined here, but are currently unsupported: .alias,
738 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
739
740 The following pseudo-ops from the Kane and Heinrich MIPS book are
741 specific to the type of debugging information being generated, and
742 should be defined by the object format: .aent, .begin, .bend,
743 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
744 .vreg.
745
746 The following pseudo-ops from the Kane and Heinrich MIPS book are
747 not MIPS CPU specific, but are also not specific to the object file
748 format. This file is probably the best place to define them, but
749 they are not currently supported: .asm0, .endr, .lab, .repeat,
750 .struct. */
751
e972090a
NC
752static const pseudo_typeS mips_pseudo_table[] =
753{
beae10d5 754 /* MIPS specific pseudo-ops. */
252b5132
RH
755 {"option", s_option, 0},
756 {"set", s_mipsset, 0},
757 {"rdata", s_change_sec, 'r'},
758 {"sdata", s_change_sec, 's'},
759 {"livereg", s_ignore, 0},
760 {"abicalls", s_abicalls, 0},
761 {"cpload", s_cpload, 0},
762 {"cprestore", s_cprestore, 0},
763 {"gpword", s_gpword, 0},
764 {"cpadd", s_cpadd, 0},
765 {"insn", s_insn, 0},
766
beae10d5 767 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
768 chips. */
769 {"asciiz", stringer, 1},
770 {"bss", s_change_sec, 'b'},
771 {"err", s_err, 0},
772 {"half", s_cons, 1},
773 {"dword", s_cons, 3},
774 {"weakext", s_mips_weakext, 0},
775
beae10d5 776 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
777 here for one reason or another. */
778 {"align", s_align, 0},
779 {"byte", s_cons, 0},
780 {"data", s_change_sec, 'd'},
781 {"double", s_float_cons, 'd'},
782 {"float", s_float_cons, 'f'},
783 {"globl", s_mips_globl, 0},
784 {"global", s_mips_globl, 0},
785 {"hword", s_cons, 1},
786 {"int", s_cons, 2},
787 {"long", s_cons, 2},
788 {"octa", s_cons, 4},
789 {"quad", s_cons, 3},
790 {"short", s_cons, 1},
791 {"single", s_float_cons, 'f'},
792 {"stabn", s_mips_stab, 'n'},
793 {"text", s_change_sec, 't'},
794 {"word", s_cons, 2},
43841e91 795 { NULL, NULL, 0 },
252b5132
RH
796};
797
e972090a
NC
798static const pseudo_typeS mips_nonecoff_pseudo_table[] =
799{
beae10d5
KH
800 /* These pseudo-ops should be defined by the object file format.
801 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
802 {"aent", s_mips_ent, 1},
803 {"bgnb", s_ignore, 0},
804 {"end", s_mips_end, 0},
805 {"endb", s_ignore, 0},
806 {"ent", s_mips_ent, 0},
807 {"file", s_file, 0},
808 {"fmask", s_mips_mask, 'F'},
809 {"frame", s_mips_frame, 0},
810 {"loc", s_ignore, 0},
811 {"mask", s_mips_mask, 'R'},
812 {"verstamp", s_ignore, 0},
43841e91 813 { NULL, NULL, 0 },
252b5132
RH
814};
815
816extern void pop_insert PARAMS ((const pseudo_typeS *));
817
818void
819mips_pop_insert ()
820{
821 pop_insert (mips_pseudo_table);
822 if (! ECOFF_DEBUGGING)
823 pop_insert (mips_nonecoff_pseudo_table);
824}
825\f
826/* Symbols labelling the current insn. */
827
e972090a
NC
828struct insn_label_list
829{
252b5132
RH
830 struct insn_label_list *next;
831 symbolS *label;
832};
833
834static struct insn_label_list *insn_labels;
835static struct insn_label_list *free_insn_labels;
836
837static void mips_clear_insn_labels PARAMS ((void));
838
839static inline void
840mips_clear_insn_labels ()
841{
842 register struct insn_label_list **pl;
843
844 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
845 ;
846 *pl = insn_labels;
847 insn_labels = NULL;
848}
849\f
850static char *expr_end;
851
852/* Expressions which appear in instructions. These are set by
853 mips_ip. */
854
855static expressionS imm_expr;
856static expressionS offset_expr;
857
858/* Relocs associated with imm_expr and offset_expr. */
859
860static bfd_reloc_code_real_type imm_reloc;
861static bfd_reloc_code_real_type offset_reloc;
862
863/* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
864
865static boolean imm_unmatched_hi;
866
867/* These are set by mips16_ip if an explicit extension is used. */
868
869static boolean mips16_small, mips16_ext;
870
871#ifdef MIPS_STABS_ELF
872/* The pdr segment for per procedure frame/regmask info */
873
874static segT pdr_seg;
875#endif
876
e7af610e
NC
877static const char *
878mips_isa_to_str (isa)
879 int isa;
880{
881 const struct mips_cpu_info *ci;
882 static char s[20];
883
884 ci = mips_cpu_info_from_isa (isa);
885 if (ci != NULL)
886 return (ci->name);
887
888 sprintf (s, "ISA#%d", isa);
889 return s;
890}
891
892static const char *
156c2f8b
NC
893mips_cpu_to_str (cpu)
894 int cpu;
895{
e7af610e 896 const struct mips_cpu_info *ci;
156c2f8b 897 static char s[16];
e7af610e
NC
898
899 ci = mips_cpu_info_from_cpu (cpu);
900 if (ci != NULL)
901 return (ci->name);
902
903 sprintf (s, "CPU#%d", cpu);
904 return s;
156c2f8b
NC
905}
906
907/* This function is called once, at assembler startup time. It should
908 set up all the tables, etc. that the MD part of the assembler will need. */
909
252b5132
RH
910void
911md_begin ()
912{
252b5132 913 register const char *retval = NULL;
156c2f8b 914 int i = 0;
252b5132
RH
915 const char *cpu;
916 char *a = NULL;
917 int broken = 0;
918 int mips_isa_from_cpu;
e7af610e
NC
919 int target_cpu_had_mips16 = 0;
920 const struct mips_cpu_info *ci;
252b5132 921
056350c6
NC
922 /* GP relative stuff not working for PE */
923 if (strncmp (TARGET_OS, "pe", 2) == 0
924 && g_switch_value != 0)
925 {
926 if (g_switch_seen)
927 as_bad (_("-G not supported in this configuration."));
928 g_switch_value = 0;
929 }
930
252b5132
RH
931 cpu = TARGET_CPU;
932 if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
933 {
934 a = xmalloc (sizeof TARGET_CPU);
935 strcpy (a, TARGET_CPU);
936 a[(sizeof TARGET_CPU) - 3] = '\0';
937 cpu = a;
938 }
939
e7af610e 940 if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
252b5132 941 {
e7af610e
NC
942 target_cpu_had_mips16 = 1;
943 cpu += sizeof "mips16" - 1;
252b5132
RH
944 }
945
e7af610e
NC
946 if (mips_opts.mips16 < 0)
947 mips_opts.mips16 = target_cpu_had_mips16;
252b5132 948
e7af610e
NC
949 /* At this point, mips_cpu will either be CPU_UNKNOWN if no CPU was
950 specified on the command line, or some other value if one was.
951 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
952 the command line, or will be set otherwise if one was. */
953 if (mips_cpu != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
252b5132 954 {
e7af610e 955 /* We have it all. There's nothing to do. */
252b5132 956 }
e7af610e 957 else if (mips_cpu != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
252b5132 958 {
e7af610e
NC
959 /* We have CPU, we need ISA. */
960 ci = mips_cpu_info_from_cpu (mips_cpu);
961 assert (ci != NULL);
962 mips_opts.isa = ci->isa;
963 }
964 else if (mips_cpu == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
965 {
966 /* We have ISA, we need default CPU. */
967 ci = mips_cpu_info_from_isa (mips_opts.isa);
968 assert (ci != NULL);
969 mips_cpu = ci->cpu;
970 }
971 else
972 {
973 /* We need to set both ISA and CPU from target cpu. */
974 ci = mips_cpu_info_from_name (cpu);
975 if (ci == NULL)
beae10d5 976 ci = mips_cpu_info_from_cpu (CPU_R3000);
e7af610e
NC
977 assert (ci != NULL);
978 mips_opts.isa = ci->isa;
979 mips_cpu = ci->cpu;
252b5132
RH
980 }
981
e7af610e
NC
982 ci = mips_cpu_info_from_cpu (mips_cpu);
983 assert (ci != NULL);
984 mips_isa_from_cpu = ci->isa;
985
252b5132 986 /* End of TARGET_CPU processing, get rid of malloced memory
bdaaa2e1 987 if necessary. */
252b5132
RH
988 cpu = NULL;
989 if (a != NULL)
990 {
156c2f8b
NC
991 free (a);
992 a = NULL;
252b5132
RH
993 }
994
e7af610e 995 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
252b5132
RH
996 as_bad (_("trap exception not supported at ISA 1"));
997
998 /* Set the EABI kind based on the ISA before the user gets
999 to change the ISA with directives. This isn't really
bdaaa2e1 1000 the best, but then neither is basing the abi on the isa. */
9ce8a5dd 1001 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
252b5132 1002 && mips_abi_string
beae10d5 1003 && 0 == strcmp (mips_abi_string, "eabi"))
252b5132
RH
1004 mips_eabi64 = 1;
1005
e7af610e
NC
1006 /* If they asked for mips1 or mips2 and a cpu that is
1007 mips3 or greater, then mark the object file 32BITMODE. */
1008 if (mips_isa_from_cpu != ISA_UNKNOWN
1009 && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
1010 && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
1011 mips_32bitmode = 1;
252b5132 1012
e7af610e 1013 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_cpu))
252b5132
RH
1014 as_warn (_("Could not set architecture and machine"));
1015
1016 file_mips_isa = mips_opts.isa;
1017
1018 op_hash = hash_new ();
1019
1020 for (i = 0; i < NUMOPCODES;)
1021 {
1022 const char *name = mips_opcodes[i].name;
1023
1024 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1025 if (retval != NULL)
1026 {
1027 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1028 mips_opcodes[i].name, retval);
1029 /* Probably a memory allocation problem? Give up now. */
1030 as_fatal (_("Broken assembler. No assembly attempted."));
1031 }
1032 do
1033 {
1034 if (mips_opcodes[i].pinfo != INSN_MACRO)
1035 {
1036 if (!validate_mips_insn (&mips_opcodes[i]))
1037 broken = 1;
1038 }
1039 ++i;
1040 }
1041 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1042 }
1043
1044 mips16_op_hash = hash_new ();
1045
1046 i = 0;
1047 while (i < bfd_mips16_num_opcodes)
1048 {
1049 const char *name = mips16_opcodes[i].name;
1050
1051 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1052 if (retval != NULL)
1053 as_fatal (_("internal: can't hash `%s': %s"),
1054 mips16_opcodes[i].name, retval);
1055 do
1056 {
1057 if (mips16_opcodes[i].pinfo != INSN_MACRO
1058 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1059 != mips16_opcodes[i].match))
1060 {
1061 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1062 mips16_opcodes[i].name, mips16_opcodes[i].args);
1063 broken = 1;
1064 }
1065 ++i;
1066 }
1067 while (i < bfd_mips16_num_opcodes
1068 && strcmp (mips16_opcodes[i].name, name) == 0);
1069 }
1070
1071 if (broken)
1072 as_fatal (_("Broken assembler. No assembly attempted."));
1073
1074 /* We add all the general register names to the symbol table. This
1075 helps us detect invalid uses of them. */
1076 for (i = 0; i < 32; i++)
1077 {
1078 char buf[5];
1079
1080 sprintf (buf, "$%d", i);
1081 symbol_table_insert (symbol_new (buf, reg_section, i,
1082 &zero_address_frag));
1083 }
1084 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1085 &zero_address_frag));
1086 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1087 &zero_address_frag));
1088 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1089 &zero_address_frag));
1090 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1091 &zero_address_frag));
1092 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1093 &zero_address_frag));
1094 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1095 &zero_address_frag));
1096 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1097 &zero_address_frag));
1098
1099 mips_no_prev_insn (false);
1100
1101 mips_gprmask = 0;
1102 mips_cprmask[0] = 0;
1103 mips_cprmask[1] = 0;
1104 mips_cprmask[2] = 0;
1105 mips_cprmask[3] = 0;
1106
1107 /* set the default alignment for the text section (2**2) */
1108 record_alignment (text_section, 2);
1109
1110 if (USE_GLOBAL_POINTER_OPT)
1111 bfd_set_gp_size (stdoutput, g_switch_value);
1112
1113 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1114 {
1115 /* On a native system, sections must be aligned to 16 byte
1116 boundaries. When configured for an embedded ELF target, we
1117 don't bother. */
1118 if (strcmp (TARGET_OS, "elf") != 0)
1119 {
1120 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1121 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1122 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1123 }
1124
1125 /* Create a .reginfo section for register masks and a .mdebug
1126 section for debugging information. */
1127 {
1128 segT seg;
1129 subsegT subseg;
1130 flagword flags;
1131 segT sec;
1132
1133 seg = now_seg;
1134 subseg = now_subseg;
1135
1136 /* The ABI says this section should be loaded so that the
1137 running program can access it. However, we don't load it
1138 if we are configured for an embedded target */
1139 flags = SEC_READONLY | SEC_DATA;
1140 if (strcmp (TARGET_OS, "elf") != 0)
1141 flags |= SEC_ALLOC | SEC_LOAD;
1142
1143 if (! mips_64)
1144 {
1145 sec = subseg_new (".reginfo", (subsegT) 0);
1146
252b5132
RH
1147 (void) bfd_set_section_flags (stdoutput, sec, flags);
1148 (void) bfd_set_section_alignment (stdoutput, sec, 2);
bdaaa2e1 1149
252b5132
RH
1150#ifdef OBJ_ELF
1151 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1152#endif
1153 }
1154 else
1155 {
1156 /* The 64-bit ABI uses a .MIPS.options section rather than
1157 .reginfo section. */
1158 sec = subseg_new (".MIPS.options", (subsegT) 0);
1159 (void) bfd_set_section_flags (stdoutput, sec, flags);
1160 (void) bfd_set_section_alignment (stdoutput, sec, 3);
1161
1162#ifdef OBJ_ELF
1163 /* Set up the option header. */
1164 {
1165 Elf_Internal_Options opthdr;
1166 char *f;
1167
1168 opthdr.kind = ODK_REGINFO;
1169 opthdr.size = (sizeof (Elf_External_Options)
1170 + sizeof (Elf64_External_RegInfo));
1171 opthdr.section = 0;
1172 opthdr.info = 0;
1173 f = frag_more (sizeof (Elf_External_Options));
1174 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1175 (Elf_External_Options *) f);
1176
1177 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1178 }
1179#endif
1180 }
1181
1182 if (ECOFF_DEBUGGING)
1183 {
1184 sec = subseg_new (".mdebug", (subsegT) 0);
1185 (void) bfd_set_section_flags (stdoutput, sec,
1186 SEC_HAS_CONTENTS | SEC_READONLY);
1187 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1188 }
1189
1190#ifdef MIPS_STABS_ELF
1191 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1192 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1193 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1194 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1195#endif
1196
1197 subseg_set (seg, subseg);
1198 }
1199 }
1200
1201 if (! ECOFF_DEBUGGING)
1202 md_obj_begin ();
1203}
1204
1205void
1206md_mips_end ()
1207{
1208 if (! ECOFF_DEBUGGING)
1209 md_obj_end ();
1210}
1211
1212void
1213md_assemble (str)
1214 char *str;
1215{
1216 struct mips_cl_insn insn;
1217
1218 imm_expr.X_op = O_absent;
1219 imm_reloc = BFD_RELOC_UNUSED;
1220 imm_unmatched_hi = false;
1221 offset_expr.X_op = O_absent;
1222 offset_reloc = BFD_RELOC_UNUSED;
1223
1224 if (mips_opts.mips16)
1225 mips16_ip (str, &insn);
1226 else
1227 {
1228 mips_ip (str, &insn);
beae10d5
KH
1229 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1230 str, insn.insn_opcode));
252b5132
RH
1231 }
1232
1233 if (insn_error)
1234 {
1235 as_bad ("%s `%s'", insn_error, str);
1236 return;
1237 }
1238
1239 if (insn.insn_mo->pinfo == INSN_MACRO)
1240 {
1241 if (mips_opts.mips16)
1242 mips16_macro (&insn);
1243 else
1244 macro (&insn);
1245 }
1246 else
1247 {
1248 if (imm_expr.X_op != O_absent)
1249 append_insn ((char *) NULL, &insn, &imm_expr, imm_reloc,
1250 imm_unmatched_hi);
1251 else if (offset_expr.X_op != O_absent)
1252 append_insn ((char *) NULL, &insn, &offset_expr, offset_reloc, false);
1253 else
1254 append_insn ((char *) NULL, &insn, NULL, BFD_RELOC_UNUSED, false);
1255 }
1256}
1257
1258/* See whether instruction IP reads register REG. CLASS is the type
1259 of register. */
1260
1261static int
1262insn_uses_reg (ip, reg, class)
1263 struct mips_cl_insn *ip;
1264 unsigned int reg;
1265 enum mips_regclass class;
1266{
1267 if (class == MIPS16_REG)
1268 {
1269 assert (mips_opts.mips16);
1270 reg = mips16_to_32_reg_map[reg];
1271 class = MIPS_GR_REG;
1272 }
1273
1274 /* Don't report on general register 0, since it never changes. */
1275 if (class == MIPS_GR_REG && reg == 0)
1276 return 0;
1277
1278 if (class == MIPS_FP_REG)
1279 {
1280 assert (! mips_opts.mips16);
1281 /* If we are called with either $f0 or $f1, we must check $f0.
1282 This is not optimal, because it will introduce an unnecessary
1283 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1284 need to distinguish reading both $f0 and $f1 or just one of
1285 them. Note that we don't have to check the other way,
1286 because there is no instruction that sets both $f0 and $f1
1287 and requires a delay. */
1288 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1289 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1290 == (reg &~ (unsigned) 1)))
1291 return 1;
1292 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1293 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1294 == (reg &~ (unsigned) 1)))
1295 return 1;
1296 }
1297 else if (! mips_opts.mips16)
1298 {
1299 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1300 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1301 return 1;
1302 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1303 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1304 return 1;
1305 }
1306 else
1307 {
1308 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1309 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1310 & MIPS16OP_MASK_RX)]
1311 == reg))
1312 return 1;
1313 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1314 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1315 & MIPS16OP_MASK_RY)]
1316 == reg))
1317 return 1;
1318 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1319 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1320 & MIPS16OP_MASK_MOVE32Z)]
1321 == reg))
1322 return 1;
1323 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1324 return 1;
1325 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1326 return 1;
1327 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1328 return 1;
1329 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1330 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1331 & MIPS16OP_MASK_REGR32) == reg)
1332 return 1;
1333 }
1334
1335 return 0;
1336}
1337
1338/* This function returns true if modifying a register requires a
1339 delay. */
1340
1341static int
1342reg_needs_delay (reg)
156c2f8b 1343 unsigned int reg;
252b5132
RH
1344{
1345 unsigned long prev_pinfo;
1346
1347 prev_pinfo = prev_insn.insn_mo->pinfo;
1348 if (! mips_opts.noreorder
9ce8a5dd 1349 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1350 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1351 || (! gpr_interlocks
1352 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1353 {
1354 /* A load from a coprocessor or from memory. All load
1355 delays delay the use of general register rt for one
1356 instruction on the r3000. The r6000 and r4000 use
1357 interlocks. */
bdaaa2e1 1358 /* Itbl support may require additional care here. */
252b5132
RH
1359 know (prev_pinfo & INSN_WRITE_GPR_T);
1360 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1361 return 1;
1362 }
1363
1364 return 0;
1365}
1366
1367/* Mark instruction labels in mips16 mode. This permits the linker to
1368 handle them specially, such as generating jalx instructions when
1369 needed. We also make them odd for the duration of the assembly, in
1370 order to generate the right sort of code. We will make them even
1371 in the adjust_symtab routine, while leaving them marked. This is
1372 convenient for the debugger and the disassembler. The linker knows
1373 to make them odd again. */
1374
1375static void
1376mips16_mark_labels ()
1377{
1378 if (mips_opts.mips16)
1379 {
1380 struct insn_label_list *l;
1381
1382 for (l = insn_labels; l != NULL; l = l->next)
1383 {
1384#ifdef OBJ_ELF
1385 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1386 S_SET_OTHER (l->label, STO_MIPS16);
1387#endif
49309057
ILT
1388 if ((S_GET_VALUE (l->label) & 1) == 0)
1389 S_SET_VALUE (l->label, S_GET_VALUE (l->label) + 1);
252b5132
RH
1390 }
1391 }
1392}
1393
1394/* Output an instruction. PLACE is where to put the instruction; if
1395 it is NULL, this uses frag_more to get room. IP is the instruction
1396 information. ADDRESS_EXPR is an operand of the instruction to be
1397 used with RELOC_TYPE. */
1398
1399static void
1400append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1401 char *place;
1402 struct mips_cl_insn *ip;
1403 expressionS *address_expr;
1404 bfd_reloc_code_real_type reloc_type;
1405 boolean unmatched_hi;
1406{
1407 register unsigned long prev_pinfo, pinfo;
1408 char *f;
1409 fixS *fixp;
1410 int nops = 0;
1411
1412 /* Mark instruction labels in mips16 mode. */
1413 if (mips_opts.mips16)
1414 mips16_mark_labels ();
1415
1416 prev_pinfo = prev_insn.insn_mo->pinfo;
1417 pinfo = ip->insn_mo->pinfo;
1418
1419 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1420 {
1421 int prev_prev_nop;
1422
1423 /* If the previous insn required any delay slots, see if we need
1424 to insert a NOP or two. There are eight kinds of possible
1425 hazards, of which an instruction can have at most one type.
1426 (1) a load from memory delay
1427 (2) a load from a coprocessor delay
1428 (3) an unconditional branch delay
1429 (4) a conditional branch delay
1430 (5) a move to coprocessor register delay
1431 (6) a load coprocessor register from memory delay
1432 (7) a coprocessor condition code delay
1433 (8) a HI/LO special register delay
1434
1435 There are a lot of optimizations we could do that we don't.
1436 In particular, we do not, in general, reorder instructions.
1437 If you use gcc with optimization, it will reorder
1438 instructions and generally do much more optimization then we
1439 do here; repeating all that work in the assembler would only
1440 benefit hand written assembly code, and does not seem worth
1441 it. */
1442
1443 /* This is how a NOP is emitted. */
1444#define emit_nop() \
1445 (mips_opts.mips16 \
1446 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1447 : md_number_to_chars (frag_more (4), 0, 4))
1448
1449 /* The previous insn might require a delay slot, depending upon
1450 the contents of the current insn. */
1451 if (! mips_opts.mips16
9ce8a5dd 1452 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1453 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1454 && ! cop_interlocks)
1455 || (! gpr_interlocks
1456 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1457 {
1458 /* A load from a coprocessor or from memory. All load
1459 delays delay the use of general register rt for one
1460 instruction on the r3000. The r6000 and r4000 use
1461 interlocks. */
beae10d5 1462 /* Itbl support may require additional care here. */
252b5132
RH
1463 know (prev_pinfo & INSN_WRITE_GPR_T);
1464 if (mips_optimize == 0
1465 || insn_uses_reg (ip,
1466 ((prev_insn.insn_opcode >> OP_SH_RT)
1467 & OP_MASK_RT),
1468 MIPS_GR_REG))
1469 ++nops;
1470 }
1471 else if (! mips_opts.mips16
9ce8a5dd 1472 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 1473 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1474 && ! cop_interlocks)
e7af610e 1475 || (mips_opts.isa == ISA_MIPS1
252b5132
RH
1476 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1477 {
1478 /* A generic coprocessor delay. The previous instruction
1479 modified a coprocessor general or control register. If
1480 it modified a control register, we need to avoid any
1481 coprocessor instruction (this is probably not always
1482 required, but it sometimes is). If it modified a general
1483 register, we avoid using that register.
1484
1485 On the r6000 and r4000 loading a coprocessor register
1486 from memory is interlocked, and does not require a delay.
1487
1488 This case is not handled very well. There is no special
1489 knowledge of CP0 handling, and the coprocessors other
1490 than the floating point unit are not distinguished at
1491 all. */
1492 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1493 Need to modify this to include knowledge about
252b5132
RH
1494 user specified delays! */
1495 if (prev_pinfo & INSN_WRITE_FPR_T)
1496 {
1497 if (mips_optimize == 0
1498 || insn_uses_reg (ip,
1499 ((prev_insn.insn_opcode >> OP_SH_FT)
1500 & OP_MASK_FT),
1501 MIPS_FP_REG))
1502 ++nops;
1503 }
1504 else if (prev_pinfo & INSN_WRITE_FPR_S)
1505 {
1506 if (mips_optimize == 0
1507 || insn_uses_reg (ip,
1508 ((prev_insn.insn_opcode >> OP_SH_FS)
1509 & OP_MASK_FS),
1510 MIPS_FP_REG))
1511 ++nops;
1512 }
1513 else
1514 {
1515 /* We don't know exactly what the previous instruction
1516 does. If the current instruction uses a coprocessor
1517 register, we must insert a NOP. If previous
1518 instruction may set the condition codes, and the
1519 current instruction uses them, we must insert two
1520 NOPS. */
bdaaa2e1 1521 /* Itbl support may require additional care here. */
252b5132
RH
1522 if (mips_optimize == 0
1523 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1524 && (pinfo & INSN_READ_COND_CODE)))
1525 nops += 2;
1526 else if (pinfo & INSN_COP)
1527 ++nops;
1528 }
1529 }
1530 else if (! mips_opts.mips16
9ce8a5dd 1531 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1532 && (prev_pinfo & INSN_WRITE_COND_CODE)
1533 && ! cop_interlocks)
1534 {
1535 /* The previous instruction sets the coprocessor condition
1536 codes, but does not require a general coprocessor delay
1537 (this means it is a floating point comparison
1538 instruction). If this instruction uses the condition
1539 codes, we need to insert a single NOP. */
beae10d5 1540 /* Itbl support may require additional care here. */
252b5132
RH
1541 if (mips_optimize == 0
1542 || (pinfo & INSN_READ_COND_CODE))
1543 ++nops;
1544 }
6b76fefe
CM
1545
1546 /* If we're fixing up mfhi/mflo for the r7000 and the
1547 previous insn was an mfhi/mflo and the current insn
1548 reads the register that the mfhi/mflo wrote to, then
1549 insert two nops. */
1550
1551 else if (mips_7000_hilo_fix
1552 && MF_HILO_INSN (prev_pinfo)
1553 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1554 & OP_MASK_RD),
1555 MIPS_GR_REG))
6b76fefe
CM
1556 {
1557 nops += 2;
1558 }
1559
1560 /* If we're fixing up mfhi/mflo for the r7000 and the
1561 2nd previous insn was an mfhi/mflo and the current insn
1562 reads the register that the mfhi/mflo wrote to, then
1563 insert one nop. */
1564
1565 else if (mips_7000_hilo_fix
1566 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1567 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1568 & OP_MASK_RD),
1569 MIPS_GR_REG))
bdaaa2e1 1570
6b76fefe
CM
1571 {
1572 nops += 1;
1573 }
bdaaa2e1 1574
252b5132
RH
1575 else if (prev_pinfo & INSN_READ_LO)
1576 {
1577 /* The previous instruction reads the LO register; if the
1578 current instruction writes to the LO register, we must
bdaaa2e1
KH
1579 insert two NOPS. Some newer processors have interlocks.
1580 Also the tx39's multiply instructions can be exectuted
252b5132 1581 immediatly after a read from HI/LO (without the delay),
bdaaa2e1
KH
1582 though the tx39's divide insns still do require the
1583 delay. */
252b5132 1584 if (! (hilo_interlocks
156c2f8b 1585 || (mips_cpu == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1586 && (mips_optimize == 0
1587 || (pinfo & INSN_WRITE_LO)))
1588 nops += 2;
1589 /* Most mips16 branch insns don't have a delay slot.
1590 If a read from LO is immediately followed by a branch
1591 to a write to LO we have a read followed by a write
1592 less than 2 insns away. We assume the target of
1593 a branch might be a write to LO, and insert a nop
bdaaa2e1 1594 between a read and an immediately following branch. */
252b5132
RH
1595 else if (mips_opts.mips16
1596 && (mips_optimize == 0
1597 || (pinfo & MIPS16_INSN_BRANCH)))
1598 nops += 1;
1599 }
1600 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1601 {
1602 /* The previous instruction reads the HI register; if the
1603 current instruction writes to the HI register, we must
1604 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1605 Also the note tx39's multiply above. */
252b5132 1606 if (! (hilo_interlocks
156c2f8b 1607 || (mips_cpu == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1608 && (mips_optimize == 0
1609 || (pinfo & INSN_WRITE_HI)))
1610 nops += 2;
1611 /* Most mips16 branch insns don't have a delay slot.
1612 If a read from HI is immediately followed by a branch
1613 to a write to HI we have a read followed by a write
1614 less than 2 insns away. We assume the target of
1615 a branch might be a write to HI, and insert a nop
bdaaa2e1 1616 between a read and an immediately following branch. */
252b5132
RH
1617 else if (mips_opts.mips16
1618 && (mips_optimize == 0
1619 || (pinfo & MIPS16_INSN_BRANCH)))
1620 nops += 1;
1621 }
1622
1623 /* If the previous instruction was in a noreorder section, then
1624 we don't want to insert the nop after all. */
bdaaa2e1 1625 /* Itbl support may require additional care here. */
252b5132
RH
1626 if (prev_insn_unreordered)
1627 nops = 0;
1628
1629 /* There are two cases which require two intervening
1630 instructions: 1) setting the condition codes using a move to
1631 coprocessor instruction which requires a general coprocessor
1632 delay and then reading the condition codes 2) reading the HI
1633 or LO register and then writing to it (except on processors
1634 which have interlocks). If we are not already emitting a NOP
1635 instruction, we must check for these cases compared to the
1636 instruction previous to the previous instruction. */
1637 if ((! mips_opts.mips16
9ce8a5dd 1638 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1639 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1640 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1641 && (pinfo & INSN_READ_COND_CODE)
1642 && ! cop_interlocks)
1643 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1644 && (pinfo & INSN_WRITE_LO)
1645 && ! (hilo_interlocks
156c2f8b 1646 || (mips_cpu == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1647 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1648 && (pinfo & INSN_WRITE_HI)
1649 && ! (hilo_interlocks
156c2f8b 1650 || (mips_cpu == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1651 prev_prev_nop = 1;
1652 else
1653 prev_prev_nop = 0;
1654
1655 if (prev_prev_insn_unreordered)
1656 prev_prev_nop = 0;
1657
1658 if (prev_prev_nop && nops == 0)
1659 ++nops;
1660
1661 /* If we are being given a nop instruction, don't bother with
1662 one of the nops we would otherwise output. This will only
1663 happen when a nop instruction is used with mips_optimize set
1664 to 0. */
1665 if (nops > 0
1666 && ! mips_opts.noreorder
156c2f8b 1667 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1668 --nops;
1669
1670 /* Now emit the right number of NOP instructions. */
1671 if (nops > 0 && ! mips_opts.noreorder)
1672 {
1673 fragS *old_frag;
1674 unsigned long old_frag_offset;
1675 int i;
1676 struct insn_label_list *l;
1677
1678 old_frag = frag_now;
1679 old_frag_offset = frag_now_fix ();
1680
1681 for (i = 0; i < nops; i++)
1682 emit_nop ();
1683
1684 if (listing)
1685 {
1686 listing_prev_line ();
1687 /* We may be at the start of a variant frag. In case we
1688 are, make sure there is enough space for the frag
1689 after the frags created by listing_prev_line. The
1690 argument to frag_grow here must be at least as large
1691 as the argument to all other calls to frag_grow in
1692 this file. We don't have to worry about being in the
1693 middle of a variant frag, because the variants insert
1694 all needed nop instructions themselves. */
1695 frag_grow (40);
1696 }
1697
1698 for (l = insn_labels; l != NULL; l = l->next)
1699 {
1700 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1701 symbol_set_frag (l->label, frag_now);
252b5132
RH
1702 S_SET_VALUE (l->label, (valueT) frag_now_fix ());
1703 /* mips16 text labels are stored as odd. */
1704 if (mips_opts.mips16)
49309057 1705 S_SET_VALUE (l->label, S_GET_VALUE (l->label) + 1);
252b5132
RH
1706 }
1707
1708#ifndef NO_ECOFF_DEBUGGING
1709 if (ECOFF_DEBUGGING)
1710 ecoff_fix_loc (old_frag, old_frag_offset);
1711#endif
1712 }
1713 else if (prev_nop_frag != NULL)
1714 {
1715 /* We have a frag holding nops we may be able to remove. If
1716 we don't need any nops, we can decrease the size of
1717 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1718 need some nops, we count them in prev_nops_required. */
252b5132
RH
1719 if (prev_nop_frag_since == 0)
1720 {
1721 if (nops == 0)
1722 {
1723 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1724 --prev_nop_frag_holds;
1725 }
1726 else
1727 prev_nop_frag_required += nops;
1728 }
1729 else
1730 {
1731 if (prev_prev_nop == 0)
1732 {
1733 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1734 --prev_nop_frag_holds;
1735 }
1736 else
1737 ++prev_nop_frag_required;
1738 }
1739
1740 if (prev_nop_frag_holds <= prev_nop_frag_required)
1741 prev_nop_frag = NULL;
1742
1743 ++prev_nop_frag_since;
1744
1745 /* Sanity check: by the time we reach the second instruction
1746 after prev_nop_frag, we should have used up all the nops
1747 one way or another. */
1748 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1749 }
1750 }
1751
1752 if (reloc_type > BFD_RELOC_UNUSED)
1753 {
1754 /* We need to set up a variant frag. */
1755 assert (mips_opts.mips16 && address_expr != NULL);
1756 f = frag_var (rs_machine_dependent, 4, 0,
1757 RELAX_MIPS16_ENCODE (reloc_type - BFD_RELOC_UNUSED,
1758 mips16_small, mips16_ext,
1759 (prev_pinfo
1760 & INSN_UNCOND_BRANCH_DELAY),
1761 (prev_insn_reloc_type
1762 == BFD_RELOC_MIPS16_JMP)),
1763 make_expr_symbol (address_expr), (offsetT) 0,
1764 (char *) NULL);
1765 }
1766 else if (place != NULL)
1767 f = place;
1768 else if (mips_opts.mips16
1769 && ! ip->use_extend
1770 && reloc_type != BFD_RELOC_MIPS16_JMP)
1771 {
1772 /* Make sure there is enough room to swap this instruction with
1773 a following jump instruction. */
1774 frag_grow (6);
1775 f = frag_more (2);
1776 }
1777 else
1778 {
1779 if (mips_opts.mips16
1780 && mips_opts.noreorder
1781 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1782 as_warn (_("extended instruction in delay slot"));
1783
1784 f = frag_more (4);
1785 }
1786
1787 fixp = NULL;
1788 if (address_expr != NULL && reloc_type < BFD_RELOC_UNUSED)
1789 {
1790 if (address_expr->X_op == O_constant)
1791 {
1792 switch (reloc_type)
1793 {
1794 case BFD_RELOC_32:
1795 ip->insn_opcode |= address_expr->X_add_number;
1796 break;
1797
1798 case BFD_RELOC_LO16:
1799 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1800 break;
1801
1802 case BFD_RELOC_MIPS_JMP:
1803 if ((address_expr->X_add_number & 3) != 0)
1804 as_bad (_("jump to misaligned address (0x%lx)"),
1805 (unsigned long) address_expr->X_add_number);
1806 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1807 break;
1808
1809 case BFD_RELOC_MIPS16_JMP:
1810 if ((address_expr->X_add_number & 3) != 0)
1811 as_bad (_("jump to misaligned address (0x%lx)"),
1812 (unsigned long) address_expr->X_add_number);
1813 ip->insn_opcode |=
1814 (((address_expr->X_add_number & 0x7c0000) << 3)
1815 | ((address_expr->X_add_number & 0xf800000) >> 7)
1816 | ((address_expr->X_add_number & 0x3fffc) >> 2));
1817 break;
1818
252b5132
RH
1819 case BFD_RELOC_16_PCREL_S2:
1820 goto need_reloc;
1821
1822 default:
1823 internalError ();
1824 }
1825 }
1826 else
1827 {
1828 need_reloc:
1829 /* Don't generate a reloc if we are writing into a variant
1830 frag. */
1831 if (place == NULL)
1832 {
1833 fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1834 address_expr,
1835 reloc_type == BFD_RELOC_16_PCREL_S2,
1836 reloc_type);
1837 if (unmatched_hi)
1838 {
1839 struct mips_hi_fixup *hi_fixup;
1840
1841 assert (reloc_type == BFD_RELOC_HI16_S);
1842 hi_fixup = ((struct mips_hi_fixup *)
1843 xmalloc (sizeof (struct mips_hi_fixup)));
1844 hi_fixup->fixp = fixp;
1845 hi_fixup->seg = now_seg;
1846 hi_fixup->next = mips_hi_fixup_list;
1847 mips_hi_fixup_list = hi_fixup;
1848 }
1849 }
1850 }
1851 }
1852
1853 if (! mips_opts.mips16)
1854 md_number_to_chars (f, ip->insn_opcode, 4);
1855 else if (reloc_type == BFD_RELOC_MIPS16_JMP)
1856 {
1857 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
1858 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
1859 }
1860 else
1861 {
1862 if (ip->use_extend)
1863 {
1864 md_number_to_chars (f, 0xf000 | ip->extend, 2);
1865 f += 2;
1866 }
1867 md_number_to_chars (f, ip->insn_opcode, 2);
1868 }
1869
1870 /* Update the register mask information. */
1871 if (! mips_opts.mips16)
1872 {
1873 if (pinfo & INSN_WRITE_GPR_D)
1874 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
1875 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
1876 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
1877 if (pinfo & INSN_READ_GPR_S)
1878 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
1879 if (pinfo & INSN_WRITE_GPR_31)
1880 mips_gprmask |= 1 << 31;
1881 if (pinfo & INSN_WRITE_FPR_D)
1882 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
1883 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
1884 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
1885 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
1886 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
1887 if ((pinfo & INSN_READ_FPR_R) != 0)
1888 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
1889 if (pinfo & INSN_COP)
1890 {
bdaaa2e1
KH
1891 /* We don't keep enough information to sort these cases out.
1892 The itbl support does keep this information however, although
1893 we currently don't support itbl fprmats as part of the cop
1894 instruction. May want to add this support in the future. */
252b5132
RH
1895 }
1896 /* Never set the bit for $0, which is always zero. */
beae10d5 1897 mips_gprmask &= ~1 << 0;
252b5132
RH
1898 }
1899 else
1900 {
1901 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
1902 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
1903 & MIPS16OP_MASK_RX);
1904 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
1905 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
1906 & MIPS16OP_MASK_RY);
1907 if (pinfo & MIPS16_INSN_WRITE_Z)
1908 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
1909 & MIPS16OP_MASK_RZ);
1910 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
1911 mips_gprmask |= 1 << TREG;
1912 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
1913 mips_gprmask |= 1 << SP;
1914 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
1915 mips_gprmask |= 1 << RA;
1916 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
1917 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
1918 if (pinfo & MIPS16_INSN_READ_Z)
1919 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1920 & MIPS16OP_MASK_MOVE32Z);
1921 if (pinfo & MIPS16_INSN_READ_GPR_X)
1922 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1923 & MIPS16OP_MASK_REGR32);
1924 }
1925
1926 if (place == NULL && ! mips_opts.noreorder)
1927 {
1928 /* Filling the branch delay slot is more complex. We try to
1929 switch the branch with the previous instruction, which we can
1930 do if the previous instruction does not set up a condition
1931 that the branch tests and if the branch is not itself the
1932 target of any branch. */
1933 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
1934 || (pinfo & INSN_COND_BRANCH_DELAY))
1935 {
1936 if (mips_optimize < 2
1937 /* If we have seen .set volatile or .set nomove, don't
1938 optimize. */
1939 || mips_opts.nomove != 0
1940 /* If we had to emit any NOP instructions, then we
1941 already know we can not swap. */
1942 || nops != 0
1943 /* If we don't even know the previous insn, we can not
bdaaa2e1 1944 swap. */
252b5132
RH
1945 || ! prev_insn_valid
1946 /* If the previous insn is already in a branch delay
1947 slot, then we can not swap. */
1948 || prev_insn_is_delay_slot
1949 /* If the previous previous insn was in a .set
1950 noreorder, we can't swap. Actually, the MIPS
1951 assembler will swap in this situation. However, gcc
1952 configured -with-gnu-as will generate code like
1953 .set noreorder
1954 lw $4,XXX
1955 .set reorder
1956 INSN
1957 bne $4,$0,foo
1958 in which we can not swap the bne and INSN. If gcc is
1959 not configured -with-gnu-as, it does not output the
1960 .set pseudo-ops. We don't have to check
1961 prev_insn_unreordered, because prev_insn_valid will
1962 be 0 in that case. We don't want to use
1963 prev_prev_insn_valid, because we do want to be able
1964 to swap at the start of a function. */
1965 || prev_prev_insn_unreordered
1966 /* If the branch is itself the target of a branch, we
1967 can not swap. We cheat on this; all we check for is
1968 whether there is a label on this instruction. If
1969 there are any branches to anything other than a
1970 label, users must use .set noreorder. */
1971 || insn_labels != NULL
1972 /* If the previous instruction is in a variant frag, we
1973 can not do the swap. This does not apply to the
1974 mips16, which uses variant frags for different
1975 purposes. */
1976 || (! mips_opts.mips16
1977 && prev_insn_frag->fr_type == rs_machine_dependent)
1978 /* If the branch reads the condition codes, we don't
1979 even try to swap, because in the sequence
1980 ctc1 $X,$31
1981 INSN
1982 INSN
1983 bc1t LABEL
1984 we can not swap, and I don't feel like handling that
1985 case. */
1986 || (! mips_opts.mips16
9ce8a5dd 1987 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1988 && (pinfo & INSN_READ_COND_CODE))
1989 /* We can not swap with an instruction that requires a
1990 delay slot, becase the target of the branch might
1991 interfere with that instruction. */
1992 || (! mips_opts.mips16
9ce8a5dd 1993 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 1994 && (prev_pinfo
bdaaa2e1 1995 /* Itbl support may require additional care here. */
252b5132
RH
1996 & (INSN_LOAD_COPROC_DELAY
1997 | INSN_COPROC_MOVE_DELAY
1998 | INSN_WRITE_COND_CODE)))
1999 || (! (hilo_interlocks
156c2f8b 2000 || (mips_cpu == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2001 && (prev_pinfo
2002 & (INSN_READ_LO
2003 | INSN_READ_HI)))
2004 || (! mips_opts.mips16
2005 && ! gpr_interlocks
2006 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2007 || (! mips_opts.mips16
e7af610e 2008 && mips_opts.isa == ISA_MIPS1
bdaaa2e1 2009 /* Itbl support may require additional care here. */
252b5132
RH
2010 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2011 /* We can not swap with a branch instruction. */
2012 || (prev_pinfo
2013 & (INSN_UNCOND_BRANCH_DELAY
2014 | INSN_COND_BRANCH_DELAY
2015 | INSN_COND_BRANCH_LIKELY))
2016 /* We do not swap with a trap instruction, since it
2017 complicates trap handlers to have the trap
2018 instruction be in a delay slot. */
2019 || (prev_pinfo & INSN_TRAP)
2020 /* If the branch reads a register that the previous
2021 instruction sets, we can not swap. */
2022 || (! mips_opts.mips16
2023 && (prev_pinfo & INSN_WRITE_GPR_T)
2024 && insn_uses_reg (ip,
2025 ((prev_insn.insn_opcode >> OP_SH_RT)
2026 & OP_MASK_RT),
2027 MIPS_GR_REG))
2028 || (! mips_opts.mips16
2029 && (prev_pinfo & INSN_WRITE_GPR_D)
2030 && insn_uses_reg (ip,
2031 ((prev_insn.insn_opcode >> OP_SH_RD)
2032 & OP_MASK_RD),
2033 MIPS_GR_REG))
2034 || (mips_opts.mips16
2035 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2036 && insn_uses_reg (ip,
2037 ((prev_insn.insn_opcode
2038 >> MIPS16OP_SH_RX)
2039 & MIPS16OP_MASK_RX),
2040 MIPS16_REG))
2041 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2042 && insn_uses_reg (ip,
2043 ((prev_insn.insn_opcode
2044 >> MIPS16OP_SH_RY)
2045 & MIPS16OP_MASK_RY),
2046 MIPS16_REG))
2047 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2048 && insn_uses_reg (ip,
2049 ((prev_insn.insn_opcode
2050 >> MIPS16OP_SH_RZ)
2051 & MIPS16OP_MASK_RZ),
2052 MIPS16_REG))
2053 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2054 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2055 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2056 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2057 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2058 && insn_uses_reg (ip,
2059 MIPS16OP_EXTRACT_REG32R (prev_insn.
2060 insn_opcode),
2061 MIPS_GR_REG))))
2062 /* If the branch writes a register that the previous
2063 instruction sets, we can not swap (we know that
2064 branches write only to RD or to $31). */
2065 || (! mips_opts.mips16
2066 && (prev_pinfo & INSN_WRITE_GPR_T)
2067 && (((pinfo & INSN_WRITE_GPR_D)
2068 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2069 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2070 || ((pinfo & INSN_WRITE_GPR_31)
2071 && (((prev_insn.insn_opcode >> OP_SH_RT)
2072 & OP_MASK_RT)
2073 == 31))))
2074 || (! mips_opts.mips16
2075 && (prev_pinfo & INSN_WRITE_GPR_D)
2076 && (((pinfo & INSN_WRITE_GPR_D)
2077 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2078 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2079 || ((pinfo & INSN_WRITE_GPR_31)
2080 && (((prev_insn.insn_opcode >> OP_SH_RD)
2081 & OP_MASK_RD)
2082 == 31))))
2083 || (mips_opts.mips16
2084 && (pinfo & MIPS16_INSN_WRITE_31)
2085 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2086 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2087 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2088 == RA))))
2089 /* If the branch writes a register that the previous
2090 instruction reads, we can not swap (we know that
2091 branches only write to RD or to $31). */
2092 || (! mips_opts.mips16
2093 && (pinfo & INSN_WRITE_GPR_D)
2094 && insn_uses_reg (&prev_insn,
2095 ((ip->insn_opcode >> OP_SH_RD)
2096 & OP_MASK_RD),
2097 MIPS_GR_REG))
2098 || (! mips_opts.mips16
2099 && (pinfo & INSN_WRITE_GPR_31)
2100 && insn_uses_reg (&prev_insn, 31, MIPS_GR_REG))
2101 || (mips_opts.mips16
2102 && (pinfo & MIPS16_INSN_WRITE_31)
2103 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2104 /* If we are generating embedded PIC code, the branch
2105 might be expanded into a sequence which uses $at, so
2106 we can't swap with an instruction which reads it. */
2107 || (mips_pic == EMBEDDED_PIC
2108 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2109 /* If the previous previous instruction has a load
2110 delay, and sets a register that the branch reads, we
2111 can not swap. */
2112 || (! mips_opts.mips16
9ce8a5dd 2113 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
bdaaa2e1 2114 /* Itbl support may require additional care here. */
252b5132
RH
2115 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2116 || (! gpr_interlocks
2117 && (prev_prev_insn.insn_mo->pinfo
2118 & INSN_LOAD_MEMORY_DELAY)))
2119 && insn_uses_reg (ip,
2120 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2121 & OP_MASK_RT),
2122 MIPS_GR_REG))
2123 /* If one instruction sets a condition code and the
2124 other one uses a condition code, we can not swap. */
2125 || ((pinfo & INSN_READ_COND_CODE)
2126 && (prev_pinfo & INSN_WRITE_COND_CODE))
2127 || ((pinfo & INSN_WRITE_COND_CODE)
2128 && (prev_pinfo & INSN_READ_COND_CODE))
2129 /* If the previous instruction uses the PC, we can not
2130 swap. */
2131 || (mips_opts.mips16
2132 && (prev_pinfo & MIPS16_INSN_READ_PC))
2133 /* If the previous instruction was extended, we can not
2134 swap. */
2135 || (mips_opts.mips16 && prev_insn_extended)
2136 /* If the previous instruction had a fixup in mips16
2137 mode, we can not swap. This normally means that the
2138 previous instruction was a 4 byte branch anyhow. */
2139 || (mips_opts.mips16 && prev_insn_fixp)
bdaaa2e1
KH
2140 /* If the previous instruction is a sync, sync.l, or
2141 sync.p, we can not swap. */
f173e82e 2142 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2143 {
2144 /* We could do even better for unconditional branches to
2145 portions of this object file; we could pick up the
2146 instruction at the destination, put it in the delay
2147 slot, and bump the destination address. */
2148 emit_nop ();
2149 /* Update the previous insn information. */
2150 prev_prev_insn = *ip;
2151 prev_insn.insn_mo = &dummy_opcode;
2152 }
2153 else
2154 {
2155 /* It looks like we can actually do the swap. */
2156 if (! mips_opts.mips16)
2157 {
2158 char *prev_f;
2159 char temp[4];
2160
2161 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2162 memcpy (temp, prev_f, 4);
2163 memcpy (prev_f, f, 4);
2164 memcpy (f, temp, 4);
2165 if (prev_insn_fixp)
2166 {
2167 prev_insn_fixp->fx_frag = frag_now;
2168 prev_insn_fixp->fx_where = f - frag_now->fr_literal;
2169 }
2170 if (fixp)
2171 {
2172 fixp->fx_frag = prev_insn_frag;
2173 fixp->fx_where = prev_insn_where;
2174 }
2175 }
2176 else
2177 {
2178 char *prev_f;
2179 char temp[2];
2180
2181 assert (prev_insn_fixp == NULL);
2182 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2183 memcpy (temp, prev_f, 2);
2184 memcpy (prev_f, f, 2);
2185 if (reloc_type != BFD_RELOC_MIPS16_JMP)
2186 {
2187 assert (reloc_type == BFD_RELOC_UNUSED);
2188 memcpy (f, temp, 2);
2189 }
2190 else
2191 {
2192 memcpy (f, f + 2, 2);
2193 memcpy (f + 2, temp, 2);
2194 }
2195 if (fixp)
2196 {
2197 fixp->fx_frag = prev_insn_frag;
2198 fixp->fx_where = prev_insn_where;
2199 }
2200 }
2201
2202 /* Update the previous insn information; leave prev_insn
2203 unchanged. */
2204 prev_prev_insn = *ip;
2205 }
2206 prev_insn_is_delay_slot = 1;
2207
2208 /* If that was an unconditional branch, forget the previous
2209 insn information. */
2210 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2211 {
2212 prev_prev_insn.insn_mo = &dummy_opcode;
2213 prev_insn.insn_mo = &dummy_opcode;
2214 }
2215
2216 prev_insn_fixp = NULL;
2217 prev_insn_reloc_type = BFD_RELOC_UNUSED;
2218 prev_insn_extended = 0;
2219 }
2220 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2221 {
2222 /* We don't yet optimize a branch likely. What we should do
2223 is look at the target, copy the instruction found there
2224 into the delay slot, and increment the branch to jump to
2225 the next instruction. */
2226 emit_nop ();
2227 /* Update the previous insn information. */
2228 prev_prev_insn = *ip;
2229 prev_insn.insn_mo = &dummy_opcode;
2230 prev_insn_fixp = NULL;
2231 prev_insn_reloc_type = BFD_RELOC_UNUSED;
2232 prev_insn_extended = 0;
2233 }
2234 else
2235 {
2236 /* Update the previous insn information. */
2237 if (nops > 0)
2238 prev_prev_insn.insn_mo = &dummy_opcode;
2239 else
2240 prev_prev_insn = prev_insn;
2241 prev_insn = *ip;
2242
2243 /* Any time we see a branch, we always fill the delay slot
2244 immediately; since this insn is not a branch, we know it
2245 is not in a delay slot. */
2246 prev_insn_is_delay_slot = 0;
2247
2248 prev_insn_fixp = fixp;
2249 prev_insn_reloc_type = reloc_type;
2250 if (mips_opts.mips16)
2251 prev_insn_extended = (ip->use_extend
2252 || reloc_type > BFD_RELOC_UNUSED);
2253 }
2254
2255 prev_prev_insn_unreordered = prev_insn_unreordered;
2256 prev_insn_unreordered = 0;
2257 prev_insn_frag = frag_now;
2258 prev_insn_where = f - frag_now->fr_literal;
2259 prev_insn_valid = 1;
2260 }
2261 else if (place == NULL)
2262 {
2263 /* We need to record a bit of information even when we are not
2264 reordering, in order to determine the base address for mips16
2265 PC relative relocs. */
2266 prev_prev_insn = prev_insn;
2267 prev_insn = *ip;
2268 prev_insn_reloc_type = reloc_type;
2269 prev_prev_insn_unreordered = prev_insn_unreordered;
2270 prev_insn_unreordered = 1;
2271 }
2272
2273 /* We just output an insn, so the next one doesn't have a label. */
2274 mips_clear_insn_labels ();
2275
2276 /* We must ensure that a fixup associated with an unmatched %hi
2277 reloc does not become a variant frag. Otherwise, the
2278 rearrangement of %hi relocs in frob_file may confuse
2279 tc_gen_reloc. */
2280 if (unmatched_hi)
2281 {
2282 frag_wane (frag_now);
2283 frag_new (0);
2284 }
2285}
2286
2287/* This function forgets that there was any previous instruction or
2288 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2289 know whether nops are needed before a noreorder section. */
252b5132
RH
2290
2291static void
2292mips_no_prev_insn (preserve)
2293 int preserve;
2294{
2295 if (! preserve)
2296 {
2297 prev_insn.insn_mo = &dummy_opcode;
2298 prev_prev_insn.insn_mo = &dummy_opcode;
2299 prev_nop_frag = NULL;
2300 prev_nop_frag_holds = 0;
2301 prev_nop_frag_required = 0;
2302 prev_nop_frag_since = 0;
2303 }
2304 prev_insn_valid = 0;
2305 prev_insn_is_delay_slot = 0;
2306 prev_insn_unreordered = 0;
2307 prev_insn_extended = 0;
2308 prev_insn_reloc_type = BFD_RELOC_UNUSED;
2309 prev_prev_insn_unreordered = 0;
2310 mips_clear_insn_labels ();
2311}
2312
2313/* This function must be called whenever we turn on noreorder or emit
2314 something other than instructions. It inserts any NOPS which might
2315 be needed by the previous instruction, and clears the information
2316 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2317 instructions are to follow. */
252b5132
RH
2318
2319static void
2320mips_emit_delays (insns)
2321 boolean insns;
2322{
2323 if (! mips_opts.noreorder)
2324 {
2325 int nops;
2326
2327 nops = 0;
2328 if ((! mips_opts.mips16
9ce8a5dd 2329 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2330 && (! cop_interlocks
2331 && (prev_insn.insn_mo->pinfo
2332 & (INSN_LOAD_COPROC_DELAY
2333 | INSN_COPROC_MOVE_DELAY
2334 | INSN_WRITE_COND_CODE))))
2335 || (! hilo_interlocks
2336 && (prev_insn.insn_mo->pinfo
2337 & (INSN_READ_LO
2338 | INSN_READ_HI)))
2339 || (! mips_opts.mips16
2340 && ! gpr_interlocks
bdaaa2e1 2341 && (prev_insn.insn_mo->pinfo
252b5132
RH
2342 & INSN_LOAD_MEMORY_DELAY))
2343 || (! mips_opts.mips16
e7af610e 2344 && mips_opts.isa == ISA_MIPS1
252b5132
RH
2345 && (prev_insn.insn_mo->pinfo
2346 & INSN_COPROC_MEMORY_DELAY)))
2347 {
beae10d5 2348 /* Itbl support may require additional care here. */
252b5132
RH
2349 ++nops;
2350 if ((! mips_opts.mips16
9ce8a5dd 2351 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2352 && (! cop_interlocks
2353 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2354 || (! hilo_interlocks
2355 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2356 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2357 ++nops;
2358
2359 if (prev_insn_unreordered)
2360 nops = 0;
2361 }
2362 else if ((! mips_opts.mips16
9ce8a5dd 2363 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2364 && (! cop_interlocks
2365 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2366 || (! hilo_interlocks
2367 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2368 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2369 {
beae10d5 2370 /* Itbl support may require additional care here. */
252b5132
RH
2371 if (! prev_prev_insn_unreordered)
2372 ++nops;
2373 }
2374
2375 if (nops > 0)
2376 {
2377 struct insn_label_list *l;
2378
2379 if (insns)
2380 {
2381 /* Record the frag which holds the nop instructions, so
2382 that we can remove them if we don't need them. */
2383 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2384 prev_nop_frag = frag_now;
2385 prev_nop_frag_holds = nops;
2386 prev_nop_frag_required = 0;
2387 prev_nop_frag_since = 0;
2388 }
2389
2390 for (; nops > 0; --nops)
2391 emit_nop ();
2392
2393 if (insns)
2394 {
2395 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2396 decrease the size of prev_nop_frag. */
252b5132
RH
2397 frag_wane (frag_now);
2398 frag_new (0);
2399 }
2400
2401 for (l = insn_labels; l != NULL; l = l->next)
2402 {
2403 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2404 symbol_set_frag (l->label, frag_now);
252b5132
RH
2405 S_SET_VALUE (l->label, (valueT) frag_now_fix ());
2406 /* mips16 text labels are stored as odd. */
2407 if (mips_opts.mips16)
49309057 2408 S_SET_VALUE (l->label, S_GET_VALUE (l->label) + 1);
252b5132
RH
2409 }
2410 }
2411 }
2412
2413 /* Mark instruction labels in mips16 mode. */
2414 if (mips_opts.mips16 && insns)
2415 mips16_mark_labels ();
2416
2417 mips_no_prev_insn (insns);
2418}
2419
2420/* Build an instruction created by a macro expansion. This is passed
2421 a pointer to the count of instructions created so far, an
2422 expression, the name of the instruction to build, an operand format
2423 string, and corresponding arguments. */
2424
2425#ifdef USE_STDARG
2426static void
2427macro_build (char *place,
2428 int *counter,
2429 expressionS * ep,
2430 const char *name,
2431 const char *fmt,
2432 ...)
2433#else
2434static void
2435macro_build (place, counter, ep, name, fmt, va_alist)
2436 char *place;
2437 int *counter;
2438 expressionS *ep;
2439 const char *name;
2440 const char *fmt;
2441 va_dcl
2442#endif
2443{
2444 struct mips_cl_insn insn;
2445 bfd_reloc_code_real_type r;
2446 va_list args;
252b5132
RH
2447
2448#ifdef USE_STDARG
2449 va_start (args, fmt);
2450#else
2451 va_start (args);
2452#endif
2453
2454 /*
2455 * If the macro is about to expand into a second instruction,
2456 * print a warning if needed. We need to pass ip as a parameter
2457 * to generate a better warning message here...
2458 */
2459 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2460 as_warn (_("Macro instruction expanded into multiple instructions"));
2461
2462 if (place == NULL)
2463 *counter += 1; /* bump instruction counter */
2464
2465 if (mips_opts.mips16)
2466 {
2467 mips16_macro_build (place, counter, ep, name, fmt, args);
2468 va_end (args);
2469 return;
2470 }
2471
2472 r = BFD_RELOC_UNUSED;
2473 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2474 assert (insn.insn_mo);
2475 assert (strcmp (name, insn.insn_mo->name) == 0);
2476
2477 /* Search until we get a match for NAME. */
2478 while (1)
2479 {
252b5132
RH
2480 if (strcmp (fmt, insn.insn_mo->args) == 0
2481 && insn.insn_mo->pinfo != INSN_MACRO
bdaaa2e1 2482 && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_cpu,
c97ef257 2483 mips_gp32)
156c2f8b 2484 && (mips_cpu != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
2485 break;
2486
2487 ++insn.insn_mo;
2488 assert (insn.insn_mo->name);
2489 assert (strcmp (name, insn.insn_mo->name) == 0);
2490 }
2491
2492 insn.insn_opcode = insn.insn_mo->match;
2493 for (;;)
2494 {
2495 switch (*fmt++)
2496 {
2497 case '\0':
2498 break;
2499
2500 case ',':
2501 case '(':
2502 case ')':
2503 continue;
2504
2505 case 't':
2506 case 'w':
2507 case 'E':
2508 insn.insn_opcode |= va_arg (args, int) << 16;
2509 continue;
2510
2511 case 'c':
2512 case 'T':
2513 case 'W':
2514 insn.insn_opcode |= va_arg (args, int) << 16;
2515 continue;
2516
2517 case 'd':
2518 case 'G':
2519 insn.insn_opcode |= va_arg (args, int) << 11;
2520 continue;
2521
4372b673
NC
2522 case 'U':
2523 {
2524 int tmp = va_arg (args, int);
2525
2526 insn.insn_opcode |= tmp << 16;
2527 insn.insn_opcode |= tmp << 11;
beae10d5 2528 continue;
4372b673
NC
2529 }
2530
252b5132
RH
2531 case 'V':
2532 case 'S':
2533 insn.insn_opcode |= va_arg (args, int) << 11;
2534 continue;
2535
2536 case 'z':
2537 continue;
2538
2539 case '<':
2540 insn.insn_opcode |= va_arg (args, int) << 6;
2541 continue;
2542
2543 case 'D':
2544 insn.insn_opcode |= va_arg (args, int) << 6;
2545 continue;
2546
2547 case 'B':
2548 insn.insn_opcode |= va_arg (args, int) << 6;
2549 continue;
2550
4372b673
NC
2551 case 'J':
2552 insn.insn_opcode |= va_arg (args, int) << 6;
2553 continue;
2554
252b5132
RH
2555 case 'q':
2556 insn.insn_opcode |= va_arg (args, int) << 6;
2557 continue;
2558
2559 case 'b':
2560 case 's':
2561 case 'r':
2562 case 'v':
2563 insn.insn_opcode |= va_arg (args, int) << 21;
2564 continue;
2565
2566 case 'i':
2567 case 'j':
2568 case 'o':
2569 r = (bfd_reloc_code_real_type) va_arg (args, int);
2570 assert (r == BFD_RELOC_MIPS_GPREL
2571 || r == BFD_RELOC_MIPS_LITERAL
2572 || r == BFD_RELOC_LO16
2573 || r == BFD_RELOC_MIPS_GOT16
2574 || r == BFD_RELOC_MIPS_CALL16
2575 || r == BFD_RELOC_MIPS_GOT_LO16
2576 || r == BFD_RELOC_MIPS_CALL_LO16
2577 || (ep->X_op == O_subtract
252b5132
RH
2578 && r == BFD_RELOC_PCREL_LO16));
2579 continue;
2580
2581 case 'u':
2582 r = (bfd_reloc_code_real_type) va_arg (args, int);
2583 assert (ep != NULL
2584 && (ep->X_op == O_constant
2585 || (ep->X_op == O_symbol
2586 && (r == BFD_RELOC_HI16_S
2587 || r == BFD_RELOC_HI16
2588 || r == BFD_RELOC_MIPS_GOT_HI16
2589 || r == BFD_RELOC_MIPS_CALL_HI16))
2590 || (ep->X_op == O_subtract
252b5132
RH
2591 && r == BFD_RELOC_PCREL_HI16_S)));
2592 if (ep->X_op == O_constant)
2593 {
2594 insn.insn_opcode |= (ep->X_add_number >> 16) & 0xffff;
2595 ep = NULL;
2596 r = BFD_RELOC_UNUSED;
2597 }
2598 continue;
2599
2600 case 'p':
2601 assert (ep != NULL);
2602 /*
2603 * This allows macro() to pass an immediate expression for
2604 * creating short branches without creating a symbol.
2605 * Note that the expression still might come from the assembly
2606 * input, in which case the value is not checked for range nor
2607 * is a relocation entry generated (yuck).
2608 */
2609 if (ep->X_op == O_constant)
2610 {
2611 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2612 ep = NULL;
2613 }
2614 else
2615 r = BFD_RELOC_16_PCREL_S2;
2616 continue;
2617
2618 case 'a':
2619 assert (ep != NULL);
2620 r = BFD_RELOC_MIPS_JMP;
2621 continue;
2622
2623 case 'C':
2624 insn.insn_opcode |= va_arg (args, unsigned long);
2625 continue;
2626
2627 default:
2628 internalError ();
2629 }
2630 break;
2631 }
2632 va_end (args);
2633 assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2634
2635 append_insn (place, &insn, ep, r, false);
2636}
2637
2638static void
2639mips16_macro_build (place, counter, ep, name, fmt, args)
2640 char *place;
43841e91 2641 int *counter ATTRIBUTE_UNUSED;
252b5132
RH
2642 expressionS *ep;
2643 const char *name;
2644 const char *fmt;
2645 va_list args;
2646{
2647 struct mips_cl_insn insn;
2648 bfd_reloc_code_real_type r;
2649
2650 r = BFD_RELOC_UNUSED;
2651 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2652 assert (insn.insn_mo);
2653 assert (strcmp (name, insn.insn_mo->name) == 0);
2654
2655 while (strcmp (fmt, insn.insn_mo->args) != 0
2656 || insn.insn_mo->pinfo == INSN_MACRO)
2657 {
2658 ++insn.insn_mo;
2659 assert (insn.insn_mo->name);
2660 assert (strcmp (name, insn.insn_mo->name) == 0);
2661 }
2662
2663 insn.insn_opcode = insn.insn_mo->match;
2664 insn.use_extend = false;
2665
2666 for (;;)
2667 {
2668 int c;
2669
2670 c = *fmt++;
2671 switch (c)
2672 {
2673 case '\0':
2674 break;
2675
2676 case ',':
2677 case '(':
2678 case ')':
2679 continue;
2680
2681 case 'y':
2682 case 'w':
2683 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2684 continue;
2685
2686 case 'x':
2687 case 'v':
2688 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2689 continue;
2690
2691 case 'z':
2692 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2693 continue;
2694
2695 case 'Z':
2696 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2697 continue;
2698
2699 case '0':
2700 case 'S':
2701 case 'P':
2702 case 'R':
2703 continue;
2704
2705 case 'X':
2706 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2707 continue;
2708
2709 case 'Y':
2710 {
2711 int regno;
2712
2713 regno = va_arg (args, int);
2714 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2715 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2716 }
2717 continue;
2718
2719 case '<':
2720 case '>':
2721 case '4':
2722 case '5':
2723 case 'H':
2724 case 'W':
2725 case 'D':
2726 case 'j':
2727 case '8':
2728 case 'V':
2729 case 'C':
2730 case 'U':
2731 case 'k':
2732 case 'K':
2733 case 'p':
2734 case 'q':
2735 {
2736 assert (ep != NULL);
2737
2738 if (ep->X_op != O_constant)
2739 r = BFD_RELOC_UNUSED + c;
2740 else
2741 {
2742 mips16_immed ((char *) NULL, 0, c, ep->X_add_number, false,
2743 false, false, &insn.insn_opcode,
2744 &insn.use_extend, &insn.extend);
2745 ep = NULL;
2746 r = BFD_RELOC_UNUSED;
2747 }
2748 }
2749 continue;
2750
2751 case '6':
2752 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2753 continue;
2754 }
2755
2756 break;
2757 }
2758
2759 assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2760
2761 append_insn (place, &insn, ep, r, false);
2762}
2763
2764/*
2765 * Generate a "lui" instruction.
2766 */
2767static void
2768macro_build_lui (place, counter, ep, regnum)
2769 char *place;
2770 int *counter;
2771 expressionS *ep;
2772 int regnum;
2773{
2774 expressionS high_expr;
2775 struct mips_cl_insn insn;
2776 bfd_reloc_code_real_type r;
2777 CONST char *name = "lui";
2778 CONST char *fmt = "t,u";
2779
2780 assert (! mips_opts.mips16);
2781
2782 if (place == NULL)
2783 high_expr = *ep;
2784 else
2785 {
2786 high_expr.X_op = O_constant;
2787 high_expr.X_add_number = ep->X_add_number;
2788 }
2789
2790 if (high_expr.X_op == O_constant)
2791 {
2792 /* we can compute the instruction now without a relocation entry */
2793 if (high_expr.X_add_number & 0x8000)
2794 high_expr.X_add_number += 0x10000;
2795 high_expr.X_add_number =
2796 ((unsigned long) high_expr.X_add_number >> 16) & 0xffff;
2797 r = BFD_RELOC_UNUSED;
2798 }
2799 else
2800 {
2801 assert (ep->X_op == O_symbol);
2802 /* _gp_disp is a special case, used from s_cpload. */
2803 assert (mips_pic == NO_PIC
2804 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
2805 r = BFD_RELOC_HI16_S;
2806 }
2807
2808 /*
2809 * If the macro is about to expand into a second instruction,
2810 * print a warning if needed. We need to pass ip as a parameter
2811 * to generate a better warning message here...
2812 */
2813 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2814 as_warn (_("Macro instruction expanded into multiple instructions"));
2815
2816 if (place == NULL)
2817 *counter += 1; /* bump instruction counter */
2818
2819 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2820 assert (insn.insn_mo);
2821 assert (strcmp (name, insn.insn_mo->name) == 0);
2822 assert (strcmp (fmt, insn.insn_mo->args) == 0);
2823
2824 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
2825 if (r == BFD_RELOC_UNUSED)
2826 {
2827 insn.insn_opcode |= high_expr.X_add_number;
2828 append_insn (place, &insn, NULL, r, false);
2829 }
2830 else
2831 append_insn (place, &insn, &high_expr, r, false);
2832}
2833
2834/* set_at()
2835 * Generates code to set the $at register to true (one)
2836 * if reg is less than the immediate expression.
2837 */
2838static void
2839set_at (counter, reg, unsignedp)
2840 int *counter;
2841 int reg;
2842 int unsignedp;
2843{
2844 if (imm_expr.X_op == O_constant
2845 && imm_expr.X_add_number >= -0x8000
2846 && imm_expr.X_add_number < 0x8000)
2847 macro_build ((char *) NULL, counter, &imm_expr,
2848 unsignedp ? "sltiu" : "slti",
2849 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
2850 else
2851 {
2852 load_register (counter, AT, &imm_expr, 0);
2853 macro_build ((char *) NULL, counter, NULL,
2854 unsignedp ? "sltu" : "slt",
2855 "d,v,t", AT, reg, AT);
2856 }
2857}
2858
2859/* Warn if an expression is not a constant. */
2860
2861static void
2862check_absolute_expr (ip, ex)
2863 struct mips_cl_insn *ip;
2864 expressionS *ex;
2865{
2866 if (ex->X_op == O_big)
2867 as_bad (_("unsupported large constant"));
2868 else if (ex->X_op != O_constant)
2869 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
2870}
2871
2872/* Count the leading zeroes by performing a binary chop. This is a
2873 bulky bit of source, but performance is a LOT better for the
2874 majority of values than a simple loop to count the bits:
2875 for (lcnt = 0; (lcnt < 32); lcnt++)
2876 if ((v) & (1 << (31 - lcnt)))
2877 break;
2878 However it is not code size friendly, and the gain will drop a bit
2879 on certain cached systems.
2880*/
2881#define COUNT_TOP_ZEROES(v) \
2882 (((v) & ~0xffff) == 0 \
2883 ? ((v) & ~0xff) == 0 \
2884 ? ((v) & ~0xf) == 0 \
2885 ? ((v) & ~0x3) == 0 \
2886 ? ((v) & ~0x1) == 0 \
2887 ? !(v) \
2888 ? 32 \
2889 : 31 \
2890 : 30 \
2891 : ((v) & ~0x7) == 0 \
2892 ? 29 \
2893 : 28 \
2894 : ((v) & ~0x3f) == 0 \
2895 ? ((v) & ~0x1f) == 0 \
2896 ? 27 \
2897 : 26 \
2898 : ((v) & ~0x7f) == 0 \
2899 ? 25 \
2900 : 24 \
2901 : ((v) & ~0xfff) == 0 \
2902 ? ((v) & ~0x3ff) == 0 \
2903 ? ((v) & ~0x1ff) == 0 \
2904 ? 23 \
2905 : 22 \
2906 : ((v) & ~0x7ff) == 0 \
2907 ? 21 \
2908 : 20 \
2909 : ((v) & ~0x3fff) == 0 \
2910 ? ((v) & ~0x1fff) == 0 \
2911 ? 19 \
2912 : 18 \
2913 : ((v) & ~0x7fff) == 0 \
2914 ? 17 \
2915 : 16 \
2916 : ((v) & ~0xffffff) == 0 \
2917 ? ((v) & ~0xfffff) == 0 \
2918 ? ((v) & ~0x3ffff) == 0 \
2919 ? ((v) & ~0x1ffff) == 0 \
2920 ? 15 \
2921 : 14 \
2922 : ((v) & ~0x7ffff) == 0 \
2923 ? 13 \
2924 : 12 \
2925 : ((v) & ~0x3fffff) == 0 \
2926 ? ((v) & ~0x1fffff) == 0 \
2927 ? 11 \
2928 : 10 \
2929 : ((v) & ~0x7fffff) == 0 \
2930 ? 9 \
2931 : 8 \
2932 : ((v) & ~0xfffffff) == 0 \
2933 ? ((v) & ~0x3ffffff) == 0 \
2934 ? ((v) & ~0x1ffffff) == 0 \
2935 ? 7 \
2936 : 6 \
2937 : ((v) & ~0x7ffffff) == 0 \
2938 ? 5 \
2939 : 4 \
2940 : ((v) & ~0x3fffffff) == 0 \
2941 ? ((v) & ~0x1fffffff) == 0 \
2942 ? 3 \
2943 : 2 \
2944 : ((v) & ~0x7fffffff) == 0 \
2945 ? 1 \
2946 : 0)
2947
2948/* load_register()
2949 * This routine generates the least number of instructions neccessary to load
2950 * an absolute expression value into a register.
2951 */
2952static void
2953load_register (counter, reg, ep, dbl)
2954 int *counter;
2955 int reg;
2956 expressionS *ep;
2957 int dbl;
2958{
2959 int freg;
2960 expressionS hi32, lo32;
2961
2962 if (ep->X_op != O_big)
2963 {
2964 assert (ep->X_op == O_constant);
2965 if (ep->X_add_number < 0x8000
2966 && (ep->X_add_number >= 0
2967 || (ep->X_add_number >= -0x8000
2968 && (! dbl
2969 || ! ep->X_unsigned
2970 || sizeof (ep->X_add_number) > 4))))
2971 {
2972 /* We can handle 16 bit signed values with an addiu to
2973 $zero. No need to ever use daddiu here, since $zero and
2974 the result are always correct in 32 bit mode. */
2975 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
2976 (int) BFD_RELOC_LO16);
2977 return;
2978 }
2979 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
2980 {
2981 /* We can handle 16 bit unsigned values with an ori to
2982 $zero. */
2983 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
2984 (int) BFD_RELOC_LO16);
2985 return;
2986 }
2987 else if ((((ep->X_add_number &~ (offsetT) 0x7fffffff) == 0
2988 || ((ep->X_add_number &~ (offsetT) 0x7fffffff)
2989 == ~ (offsetT) 0x7fffffff))
2990 && (! dbl
2991 || ! ep->X_unsigned
2992 || sizeof (ep->X_add_number) > 4
2993 || (ep->X_add_number & 0x80000000) == 0))
9ce8a5dd 2994 || ((! ISA_HAS_64BIT_REGS (mips_opts.isa) || ! dbl)
252b5132 2995 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
9ce8a5dd 2996 || (! ISA_HAS_64BIT_REGS (mips_opts.isa)
252b5132
RH
2997 && ! dbl
2998 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
2999 == ~ (offsetT) 0xffffffff)))
3000 {
3001 /* 32 bit values require an lui. */
3002 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3003 (int) BFD_RELOC_HI16);
3004 if ((ep->X_add_number & 0xffff) != 0)
3005 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3006 (int) BFD_RELOC_LO16);
3007 return;
3008 }
3009 }
3010
3011 /* The value is larger than 32 bits. */
3012
9ce8a5dd 3013 if (! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
3014 {
3015 as_bad (_("Number larger than 32 bits"));
3016 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3017 (int) BFD_RELOC_LO16);
3018 return;
3019 }
3020
3021 if (ep->X_op != O_big)
3022 {
3023 hi32 = *ep;
3024 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3025 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3026 hi32.X_add_number &= 0xffffffff;
3027 lo32 = *ep;
3028 lo32.X_add_number &= 0xffffffff;
3029 }
3030 else
3031 {
3032 assert (ep->X_add_number > 2);
3033 if (ep->X_add_number == 3)
3034 generic_bignum[3] = 0;
3035 else if (ep->X_add_number > 4)
3036 as_bad (_("Number larger than 64 bits"));
3037 lo32.X_op = O_constant;
3038 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3039 hi32.X_op = O_constant;
3040 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3041 }
3042
3043 if (hi32.X_add_number == 0)
3044 freg = 0;
3045 else
3046 {
3047 int shift, bit;
3048 unsigned long hi, lo;
3049
3050 if (hi32.X_add_number == 0xffffffff)
beae10d5
KH
3051 {
3052 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3053 {
3054 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
252b5132 3055 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3056 return;
3057 }
3058 if (lo32.X_add_number & 0x80000000)
3059 {
3060 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3061 (int) BFD_RELOC_HI16);
252b5132
RH
3062 if (lo32.X_add_number & 0xffff)
3063 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3064 reg, reg, (int) BFD_RELOC_LO16);
beae10d5
KH
3065 return;
3066 }
3067 }
252b5132
RH
3068
3069 /* Check for 16bit shifted constant. We know that hi32 is
3070 non-zero, so start the mask on the first bit of the hi32
3071 value. */
3072 shift = 17;
3073 do
beae10d5
KH
3074 {
3075 unsigned long himask, lomask;
3076
3077 if (shift < 32)
3078 {
3079 himask = 0xffff >> (32 - shift);
3080 lomask = (0xffff << shift) & 0xffffffff;
3081 }
3082 else
3083 {
3084 himask = 0xffff << (shift - 32);
3085 lomask = 0;
3086 }
3087 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3088 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3089 {
3090 expressionS tmp;
3091
3092 tmp.X_op = O_constant;
3093 if (shift < 32)
3094 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3095 | (lo32.X_add_number >> shift));
3096 else
3097 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3098 macro_build ((char *) NULL, counter, &tmp,
3099 "ori", "t,r,i", reg, 0,
3100 (int) BFD_RELOC_LO16);
3101 macro_build ((char *) NULL, counter, NULL,
3102 (shift >= 32) ? "dsll32" : "dsll",
3103 "d,w,<", reg, reg,
3104 (shift >= 32) ? shift - 32 : shift);
3105 return;
3106 }
3107 shift++;
3108 }
3109 while (shift <= (64 - 16));
252b5132
RH
3110
3111 /* Find the bit number of the lowest one bit, and store the
3112 shifted value in hi/lo. */
3113 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3114 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3115 if (lo != 0)
3116 {
3117 bit = 0;
3118 while ((lo & 1) == 0)
3119 {
3120 lo >>= 1;
3121 ++bit;
3122 }
3123 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3124 hi >>= bit;
3125 }
3126 else
3127 {
3128 bit = 32;
3129 while ((hi & 1) == 0)
3130 {
3131 hi >>= 1;
3132 ++bit;
3133 }
3134 lo = hi;
3135 hi = 0;
3136 }
3137
3138 /* Optimize if the shifted value is a (power of 2) - 1. */
3139 if ((hi == 0 && ((lo + 1) & lo) == 0)
3140 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3141 {
3142 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3143 if (shift != 0)
beae10d5 3144 {
252b5132
RH
3145 expressionS tmp;
3146
3147 /* This instruction will set the register to be all
3148 ones. */
beae10d5
KH
3149 tmp.X_op = O_constant;
3150 tmp.X_add_number = (offsetT) -1;
3151 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
252b5132 3152 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3153 if (bit != 0)
3154 {
3155 bit += shift;
3156 macro_build ((char *) NULL, counter, NULL,
3157 (bit >= 32) ? "dsll32" : "dsll",
3158 "d,w,<", reg, reg,
3159 (bit >= 32) ? bit - 32 : bit);
3160 }
3161 macro_build ((char *) NULL, counter, NULL,
252b5132 3162 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3163 "d,w,<", reg, reg,
252b5132 3164 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3165 return;
3166 }
3167 }
252b5132
RH
3168
3169 /* Sign extend hi32 before calling load_register, because we can
3170 generally get better code when we load a sign extended value. */
3171 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3172 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3173 load_register (counter, reg, &hi32, 0);
3174 freg = reg;
3175 }
3176 if ((lo32.X_add_number & 0xffff0000) == 0)
3177 {
3178 if (freg != 0)
3179 {
3180 macro_build ((char *) NULL, counter, NULL, "dsll32", "d,w,<", reg,
3181 freg, 0);
3182 freg = reg;
3183 }
3184 }
3185 else
3186 {
3187 expressionS mid16;
3188
3189 if ((freg == 0) && (lo32.X_add_number == 0xffffffff))
beae10d5 3190 {
252b5132
RH
3191 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3192 (int) BFD_RELOC_HI16);
beae10d5
KH
3193 macro_build ((char *) NULL, counter, NULL, "dsrl32", "d,w,<", reg,
3194 reg, 0);
3195 return;
3196 }
252b5132
RH
3197
3198 if (freg != 0)
3199 {
3200 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
3201 freg, 16);
3202 freg = reg;
3203 }
3204 mid16 = lo32;
3205 mid16.X_add_number >>= 16;
3206 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3207 freg, (int) BFD_RELOC_LO16);
3208 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
3209 reg, 16);
3210 freg = reg;
3211 }
3212 if ((lo32.X_add_number & 0xffff) != 0)
3213 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3214 (int) BFD_RELOC_LO16);
3215}
3216
3217/* Load an address into a register. */
3218
3219static void
3220load_address (counter, reg, ep)
3221 int *counter;
3222 int reg;
3223 expressionS *ep;
3224{
3225 char *p;
3226
3227 if (ep->X_op != O_constant
3228 && ep->X_op != O_symbol)
3229 {
3230 as_bad (_("expression too complex"));
3231 ep->X_op = O_constant;
3232 }
3233
3234 if (ep->X_op == O_constant)
3235 {
3236 load_register (counter, reg, ep, 0);
3237 return;
3238 }
3239
3240 if (mips_pic == NO_PIC)
3241 {
3242 /* If this is a reference to a GP relative symbol, we want
3243 addiu $reg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
3244 Otherwise we want
3245 lui $reg,<sym> (BFD_RELOC_HI16_S)
3246 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3247 If we have an addend, we always use the latter form. */
3248 if ((valueT) ep->X_add_number >= MAX_GPREL_OFFSET
beae10d5 3249 || nopic_need_relax (ep->X_add_symbol, 1))
252b5132
RH
3250 p = NULL;
3251 else
3252 {
3253 frag_grow (20);
3254 macro_build ((char *) NULL, counter, ep,
3255 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 3256 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
3257 ? "addiu" : "daddiu"),
3258 "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
3259 p = frag_var (rs_machine_dependent, 8, 0,
3260 RELAX_ENCODE (4, 8, 0, 4, 0,
3261 mips_opts.warn_about_macros),
3262 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3263 }
3264 macro_build_lui (p, counter, ep, reg);
3265 if (p != NULL)
3266 p += 4;
3267 macro_build (p, counter, ep,
3268 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 3269 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
3270 ? "addiu" : "daddiu"),
3271 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3272 }
3273 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3274 {
3275 expressionS ex;
3276
3277 /* If this is a reference to an external symbol, we want
3278 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3279 Otherwise we want
3280 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3281 nop
3282 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3283 If there is a constant, it must be added in after. */
3284 ex.X_add_number = ep->X_add_number;
3285 ep->X_add_number = 0;
3286 frag_grow (20);
3287 macro_build ((char *) NULL, counter, ep,
3288 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 3289 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
3290 ? "lw" : "ld"),
3291 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3292 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3293 p = frag_var (rs_machine_dependent, 4, 0,
3294 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3295 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3296 macro_build (p, counter, ep,
3297 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 3298 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
3299 ? "addiu" : "daddiu"),
3300 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3301 if (ex.X_add_number != 0)
3302 {
3303 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3304 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3305 ex.X_op = O_constant;
3306 macro_build ((char *) NULL, counter, &ex,
3307 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 3308 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
3309 ? "addiu" : "daddiu"),
3310 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3311 }
3312 }
3313 else if (mips_pic == SVR4_PIC)
3314 {
3315 expressionS ex;
3316 int off;
3317
3318 /* This is the large GOT case. If this is a reference to an
3319 external symbol, we want
3320 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3321 addu $reg,$reg,$gp
3322 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3323 Otherwise, for a reference to a local symbol, we want
3324 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3325 nop
3326 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3327 If there is a constant, it must be added in after. */
3328 ex.X_add_number = ep->X_add_number;
3329 ep->X_add_number = 0;
3330 if (reg_needs_delay (GP))
3331 off = 4;
3332 else
3333 off = 0;
3334 frag_grow (32);
3335 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3336 (int) BFD_RELOC_MIPS_GOT_HI16);
3337 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3338 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 3339 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
3340 ? "addu" : "daddu"),
3341 "d,v,t", reg, reg, GP);
3342 macro_build ((char *) NULL, counter, ep,
3343 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 3344 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
3345 ? "lw" : "ld"),
3346 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3347 p = frag_var (rs_machine_dependent, 12 + off, 0,
3348 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3349 mips_opts.warn_about_macros),
3350 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3351 if (off > 0)
3352 {
3353 /* We need a nop before loading from $gp. This special
3354 check is required because the lui which starts the main
3355 instruction stream does not refer to $gp, and so will not
3356 insert the nop which may be required. */
3357 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3358 p += 4;
3359 }
3360 macro_build (p, counter, ep,
3361 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 3362 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
3363 ? "lw" : "ld"),
3364 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3365 p += 4;
3366 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3367 p += 4;
3368 macro_build (p, counter, ep,
3369 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 3370 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
3371 ? "addiu" : "daddiu"),
3372 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3373 if (ex.X_add_number != 0)
3374 {
3375 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3376 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3377 ex.X_op = O_constant;
3378 macro_build ((char *) NULL, counter, &ex,
3379 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 3380 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
3381 ? "addiu" : "daddiu"),
3382 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3383 }
3384 }
3385 else if (mips_pic == EMBEDDED_PIC)
3386 {
3387 /* We always do
3388 addiu $reg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
3389 */
3390 macro_build ((char *) NULL, counter, ep,
3391 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 3392 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
3393 ? "addiu" : "daddiu"),
3394 "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
3395 }
3396 else
3397 abort ();
3398}
3399
3400/*
3401 * Build macros
3402 * This routine implements the seemingly endless macro or synthesized
3403 * instructions and addressing modes in the mips assembly language. Many
3404 * of these macros are simple and are similar to each other. These could
3405 * probably be handled by some kind of table or grammer aproach instead of
3406 * this verbose method. Others are not simple macros but are more like
3407 * optimizing code generation.
3408 * One interesting optimization is when several store macros appear
3409 * consecutivly that would load AT with the upper half of the same address.
3410 * The ensuing load upper instructions are ommited. This implies some kind
3411 * of global optimization. We currently only optimize within a single macro.
3412 * For many of the load and store macros if the address is specified as a
3413 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3414 * first load register 'at' with zero and use it as the base register. The
3415 * mips assembler simply uses register $zero. Just one tiny optimization
3416 * we're missing.
3417 */
3418static void
3419macro (ip)
3420 struct mips_cl_insn *ip;
3421{
3422 register int treg, sreg, dreg, breg;
3423 int tempreg;
3424 int mask;
3425 int icnt = 0;
43841e91 3426 int used_at = 0;
252b5132
RH
3427 expressionS expr1;
3428 const char *s;
3429 const char *s2;
3430 const char *fmt;
3431 int likely = 0;
3432 int dbl = 0;
3433 int coproc = 0;
3434 int lr = 0;
3435 int imm = 0;
3436 offsetT maxnum;
3437 int off;
3438 bfd_reloc_code_real_type r;
3439 char *p;
3440 int hold_mips_optimize;
3441
3442 assert (! mips_opts.mips16);
3443
3444 treg = (ip->insn_opcode >> 16) & 0x1f;
3445 dreg = (ip->insn_opcode >> 11) & 0x1f;
3446 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3447 mask = ip->insn_mo->mask;
3448
3449 expr1.X_op = O_constant;
3450 expr1.X_op_symbol = NULL;
3451 expr1.X_add_symbol = NULL;
3452 expr1.X_add_number = 1;
3453
3454 switch (mask)
3455 {
3456 case M_DABS:
3457 dbl = 1;
3458 case M_ABS:
3459 /* bgez $a0,.+12
3460 move v0,$a0
3461 sub v0,$zero,$a0
3462 */
3463
3464 mips_emit_delays (true);
3465 ++mips_opts.noreorder;
3466 mips_any_noreorder = 1;
3467
3468 expr1.X_add_number = 8;
3469 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3470 if (dreg == sreg)
3471 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3472 else
3473 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, sreg, 0);
3474 macro_build ((char *) NULL, &icnt, NULL,
3475 dbl ? "dsub" : "sub",
3476 "d,v,t", dreg, 0, sreg);
3477
3478 --mips_opts.noreorder;
3479 return;
3480
3481 case M_ADD_I:
3482 s = "addi";
3483 s2 = "add";
3484 goto do_addi;
3485 case M_ADDU_I:
3486 s = "addiu";
3487 s2 = "addu";
3488 goto do_addi;
3489 case M_DADD_I:
3490 dbl = 1;
3491 s = "daddi";
3492 s2 = "dadd";
3493 goto do_addi;
3494 case M_DADDU_I:
3495 dbl = 1;
3496 s = "daddiu";
3497 s2 = "daddu";
3498 do_addi:
3499 if (imm_expr.X_op == O_constant
3500 && imm_expr.X_add_number >= -0x8000
3501 && imm_expr.X_add_number < 0x8000)
3502 {
3503 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3504 (int) BFD_RELOC_LO16);
3505 return;
3506 }
3507 load_register (&icnt, AT, &imm_expr, dbl);
3508 macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
3509 break;
3510
3511 case M_AND_I:
3512 s = "andi";
3513 s2 = "and";
3514 goto do_bit;
3515 case M_OR_I:
3516 s = "ori";
3517 s2 = "or";
3518 goto do_bit;
3519 case M_NOR_I:
3520 s = "";
3521 s2 = "nor";
3522 goto do_bit;
3523 case M_XOR_I:
3524 s = "xori";
3525 s2 = "xor";
3526 do_bit:
3527 if (imm_expr.X_op == O_constant
3528 && imm_expr.X_add_number >= 0
3529 && imm_expr.X_add_number < 0x10000)
3530 {
3531 if (mask != M_NOR_I)
3532 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3533 sreg, (int) BFD_RELOC_LO16);
3534 else
3535 {
3536 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3537 treg, sreg, (int) BFD_RELOC_LO16);
3538 macro_build ((char *) NULL, &icnt, NULL, "nor", "d,v,t",
3539 treg, treg, 0);
3540 }
3541 return;
3542 }
3543
3544 load_register (&icnt, AT, &imm_expr, 0);
3545 macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
3546 break;
3547
3548 case M_BEQ_I:
3549 s = "beq";
3550 goto beq_i;
3551 case M_BEQL_I:
3552 s = "beql";
3553 likely = 1;
3554 goto beq_i;
3555 case M_BNE_I:
3556 s = "bne";
3557 goto beq_i;
3558 case M_BNEL_I:
3559 s = "bnel";
3560 likely = 1;
3561 beq_i:
3562 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3563 {
3564 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3565 0);
3566 return;
3567 }
3568 load_register (&icnt, AT, &imm_expr, 0);
3569 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3570 break;
3571
3572 case M_BGEL:
3573 likely = 1;
3574 case M_BGE:
3575 if (treg == 0)
3576 {
3577 macro_build ((char *) NULL, &icnt, &offset_expr,
3578 likely ? "bgezl" : "bgez",
3579 "s,p", sreg);
3580 return;
3581 }
3582 if (sreg == 0)
3583 {
3584 macro_build ((char *) NULL, &icnt, &offset_expr,
3585 likely ? "blezl" : "blez",
3586 "s,p", treg);
3587 return;
3588 }
3589 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
3590 macro_build ((char *) NULL, &icnt, &offset_expr,
3591 likely ? "beql" : "beq",
3592 "s,t,p", AT, 0);
3593 break;
3594
3595 case M_BGTL_I:
3596 likely = 1;
3597 case M_BGT_I:
3598 /* check for > max integer */
3599 maxnum = 0x7fffffff;
9ce8a5dd 3600 if (ISA_HAS_64BIT_REGS (mips_opts.isa) && sizeof (maxnum) > 4)
252b5132
RH
3601 {
3602 maxnum <<= 16;
3603 maxnum |= 0xffff;
3604 maxnum <<= 16;
3605 maxnum |= 0xffff;
3606 }
3607 if (imm_expr.X_op == O_constant
3608 && imm_expr.X_add_number >= maxnum
9ce8a5dd 3609 && (! ISA_HAS_64BIT_REGS (mips_opts.isa) || sizeof (maxnum) > 4))
252b5132
RH
3610 {
3611 do_false:
3612 /* result is always false */
3613 if (! likely)
3614 {
3615 as_warn (_("Branch %s is always false (nop)"), ip->insn_mo->name);
3616 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3617 }
3618 else
3619 {
3620 as_warn (_("Branch likely %s is always false"), ip->insn_mo->name);
3621 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3622 "s,t,p", 0, 0);
3623 }
3624 return;
3625 }
3626 if (imm_expr.X_op != O_constant)
3627 as_bad (_("Unsupported large constant"));
3628 imm_expr.X_add_number++;
3629 /* FALLTHROUGH */
3630 case M_BGE_I:
3631 case M_BGEL_I:
3632 if (mask == M_BGEL_I)
3633 likely = 1;
3634 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3635 {
3636 macro_build ((char *) NULL, &icnt, &offset_expr,
3637 likely ? "bgezl" : "bgez",
3638 "s,p", sreg);
3639 return;
3640 }
3641 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3642 {
3643 macro_build ((char *) NULL, &icnt, &offset_expr,
3644 likely ? "bgtzl" : "bgtz",
3645 "s,p", sreg);
3646 return;
3647 }
3648 maxnum = 0x7fffffff;
9ce8a5dd 3649 if (ISA_HAS_64BIT_REGS (mips_opts.isa) && sizeof (maxnum) > 4)
252b5132
RH
3650 {
3651 maxnum <<= 16;
3652 maxnum |= 0xffff;
3653 maxnum <<= 16;
3654 maxnum |= 0xffff;
3655 }
3656 maxnum = - maxnum - 1;
3657 if (imm_expr.X_op == O_constant
3658 && imm_expr.X_add_number <= maxnum
9ce8a5dd 3659 && (! ISA_HAS_64BIT_REGS (mips_opts.isa) || sizeof (maxnum) > 4))
252b5132
RH
3660 {
3661 do_true:
3662 /* result is always true */
3663 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
3664 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
3665 return;
3666 }
3667 set_at (&icnt, sreg, 0);
3668 macro_build ((char *) NULL, &icnt, &offset_expr,
3669 likely ? "beql" : "beq",
3670 "s,t,p", AT, 0);
3671 break;
3672
3673 case M_BGEUL:
3674 likely = 1;
3675 case M_BGEU:
3676 if (treg == 0)
3677 goto do_true;
3678 if (sreg == 0)
3679 {
3680 macro_build ((char *) NULL, &icnt, &offset_expr,
3681 likely ? "beql" : "beq",
3682 "s,t,p", 0, treg);
3683 return;
3684 }
3685 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
3686 treg);
3687 macro_build ((char *) NULL, &icnt, &offset_expr,
3688 likely ? "beql" : "beq",
3689 "s,t,p", AT, 0);
3690 break;
3691
3692 case M_BGTUL_I:
3693 likely = 1;
3694 case M_BGTU_I:
3695 if (sreg == 0
9ce8a5dd 3696 || (! ISA_HAS_64BIT_REGS (mips_opts.isa)
252b5132
RH
3697 && imm_expr.X_op == O_constant
3698 && imm_expr.X_add_number == 0xffffffff))
3699 goto do_false;
3700 if (imm_expr.X_op != O_constant)
3701 as_bad (_("Unsupported large constant"));
3702 imm_expr.X_add_number++;
3703 /* FALLTHROUGH */
3704 case M_BGEU_I:
3705 case M_BGEUL_I:
3706 if (mask == M_BGEUL_I)
3707 likely = 1;
3708 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3709 goto do_true;
3710 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3711 {
3712 macro_build ((char *) NULL, &icnt, &offset_expr,
3713 likely ? "bnel" : "bne",
3714 "s,t,p", sreg, 0);
3715 return;
3716 }
3717 set_at (&icnt, sreg, 1);
3718 macro_build ((char *) NULL, &icnt, &offset_expr,
3719 likely ? "beql" : "beq",
3720 "s,t,p", AT, 0);
3721 break;
3722
3723 case M_BGTL:
3724 likely = 1;
3725 case M_BGT:
3726 if (treg == 0)
3727 {
3728 macro_build ((char *) NULL, &icnt, &offset_expr,
3729 likely ? "bgtzl" : "bgtz",
3730 "s,p", sreg);
3731 return;
3732 }
3733 if (sreg == 0)
3734 {
3735 macro_build ((char *) NULL, &icnt, &offset_expr,
3736 likely ? "bltzl" : "bltz",
3737 "s,p", treg);
3738 return;
3739 }
3740 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
3741 macro_build ((char *) NULL, &icnt, &offset_expr,
3742 likely ? "bnel" : "bne",
3743 "s,t,p", AT, 0);
3744 break;
3745
3746 case M_BGTUL:
3747 likely = 1;
3748 case M_BGTU:
3749 if (treg == 0)
3750 {
3751 macro_build ((char *) NULL, &icnt, &offset_expr,
3752 likely ? "bnel" : "bne",
3753 "s,t,p", sreg, 0);
3754 return;
3755 }
3756 if (sreg == 0)
3757 goto do_false;
3758 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
3759 sreg);
3760 macro_build ((char *) NULL, &icnt, &offset_expr,
3761 likely ? "bnel" : "bne",
3762 "s,t,p", AT, 0);
3763 break;
3764
3765 case M_BLEL:
3766 likely = 1;
3767 case M_BLE:
3768 if (treg == 0)
3769 {
3770 macro_build ((char *) NULL, &icnt, &offset_expr,
3771 likely ? "blezl" : "blez",
3772 "s,p", sreg);
3773 return;
3774 }
3775 if (sreg == 0)
3776 {
3777 macro_build ((char *) NULL, &icnt, &offset_expr,
3778 likely ? "bgezl" : "bgez",
3779 "s,p", treg);
3780 return;
3781 }
3782 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
3783 macro_build ((char *) NULL, &icnt, &offset_expr,
3784 likely ? "beql" : "beq",
3785 "s,t,p", AT, 0);
3786 break;
3787
3788 case M_BLEL_I:
3789 likely = 1;
3790 case M_BLE_I:
3791 maxnum = 0x7fffffff;
9ce8a5dd 3792 if (ISA_HAS_64BIT_REGS (mips_opts.isa) && sizeof (maxnum) > 4)
252b5132
RH
3793 {
3794 maxnum <<= 16;
3795 maxnum |= 0xffff;
3796 maxnum <<= 16;
3797 maxnum |= 0xffff;
3798 }
3799 if (imm_expr.X_op == O_constant
3800 && imm_expr.X_add_number >= maxnum
9ce8a5dd 3801 && (! ISA_HAS_64BIT_REGS (mips_opts.isa) || sizeof (maxnum) > 4))
252b5132
RH
3802 goto do_true;
3803 if (imm_expr.X_op != O_constant)
3804 as_bad (_("Unsupported large constant"));
3805 imm_expr.X_add_number++;
3806 /* FALLTHROUGH */
3807 case M_BLT_I:
3808 case M_BLTL_I:
3809 if (mask == M_BLTL_I)
3810 likely = 1;
3811 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3812 {
3813 macro_build ((char *) NULL, &icnt, &offset_expr,
3814 likely ? "bltzl" : "bltz",
3815 "s,p", sreg);
3816 return;
3817 }
3818 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3819 {
3820 macro_build ((char *) NULL, &icnt, &offset_expr,
3821 likely ? "blezl" : "blez",
3822 "s,p", sreg);
3823 return;
3824 }
3825 set_at (&icnt, sreg, 0);
3826 macro_build ((char *) NULL, &icnt, &offset_expr,
3827 likely ? "bnel" : "bne",
3828 "s,t,p", AT, 0);
3829 break;
3830
3831 case M_BLEUL:
3832 likely = 1;
3833 case M_BLEU:
3834 if (treg == 0)
3835 {
3836 macro_build ((char *) NULL, &icnt, &offset_expr,
3837 likely ? "beql" : "beq",
3838 "s,t,p", sreg, 0);
3839 return;
3840 }
3841 if (sreg == 0)
3842 goto do_true;
3843 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
3844 sreg);
3845 macro_build ((char *) NULL, &icnt, &offset_expr,
3846 likely ? "beql" : "beq",
3847 "s,t,p", AT, 0);
3848 break;
3849
3850 case M_BLEUL_I:
3851 likely = 1;
3852 case M_BLEU_I:
3853 if (sreg == 0
9ce8a5dd 3854 || (! ISA_HAS_64BIT_REGS (mips_opts.isa)
252b5132
RH
3855 && imm_expr.X_op == O_constant
3856 && imm_expr.X_add_number == 0xffffffff))
3857 goto do_true;
3858 if (imm_expr.X_op != O_constant)
3859 as_bad (_("Unsupported large constant"));
3860 imm_expr.X_add_number++;
3861 /* FALLTHROUGH */
3862 case M_BLTU_I:
3863 case M_BLTUL_I:
3864 if (mask == M_BLTUL_I)
3865 likely = 1;
3866 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3867 goto do_false;
3868 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3869 {
3870 macro_build ((char *) NULL, &icnt, &offset_expr,
3871 likely ? "beql" : "beq",
3872 "s,t,p", sreg, 0);
3873 return;
3874 }
3875 set_at (&icnt, sreg, 1);
3876 macro_build ((char *) NULL, &icnt, &offset_expr,
3877 likely ? "bnel" : "bne",
3878 "s,t,p", AT, 0);
3879 break;
3880
3881 case M_BLTL:
3882 likely = 1;
3883 case M_BLT:
3884 if (treg == 0)
3885 {
3886 macro_build ((char *) NULL, &icnt, &offset_expr,
3887 likely ? "bltzl" : "bltz",
3888 "s,p", sreg);
3889 return;
3890 }
3891 if (sreg == 0)
3892 {
3893 macro_build ((char *) NULL, &icnt, &offset_expr,
3894 likely ? "bgtzl" : "bgtz",
3895 "s,p", treg);
3896 return;
3897 }
3898 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
3899 macro_build ((char *) NULL, &icnt, &offset_expr,
3900 likely ? "bnel" : "bne",
3901 "s,t,p", AT, 0);
3902 break;
3903
3904 case M_BLTUL:
3905 likely = 1;
3906 case M_BLTU:
3907 if (treg == 0)
3908 goto do_false;
3909 if (sreg == 0)
3910 {
3911 macro_build ((char *) NULL, &icnt, &offset_expr,
3912 likely ? "bnel" : "bne",
3913 "s,t,p", 0, treg);
3914 return;
3915 }
3916 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
3917 treg);
3918 macro_build ((char *) NULL, &icnt, &offset_expr,
3919 likely ? "bnel" : "bne",
3920 "s,t,p", AT, 0);
3921 break;
3922
3923 case M_DDIV_3:
3924 dbl = 1;
3925 case M_DIV_3:
3926 s = "mflo";
3927 goto do_div3;
3928 case M_DREM_3:
3929 dbl = 1;
3930 case M_REM_3:
3931 s = "mfhi";
3932 do_div3:
3933 if (treg == 0)
3934 {
3935 as_warn (_("Divide by zero."));
3936 if (mips_trap)
3937 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
3938 else
3939 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
3940 return;
3941 }
3942
3943 mips_emit_delays (true);
3944 ++mips_opts.noreorder;
3945 mips_any_noreorder = 1;
3946 if (mips_trap)
3947 {
3948 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
3949 macro_build ((char *) NULL, &icnt, NULL,
3950 dbl ? "ddiv" : "div",
3951 "z,s,t", sreg, treg);
3952 }
3953 else
3954 {
3955 expr1.X_add_number = 8;
3956 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
3957 macro_build ((char *) NULL, &icnt, NULL,
3958 dbl ? "ddiv" : "div",
3959 "z,s,t", sreg, treg);
3960 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
3961 }
3962 expr1.X_add_number = -1;
3963 macro_build ((char *) NULL, &icnt, &expr1,
3964 dbl ? "daddiu" : "addiu",
3965 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
3966 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
3967 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
3968 if (dbl)
3969 {
3970 expr1.X_add_number = 1;
3971 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
3972 (int) BFD_RELOC_LO16);
3973 macro_build ((char *) NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT,
3974 31);
3975 }
3976 else
3977 {
3978 expr1.X_add_number = 0x80000000;
3979 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
3980 (int) BFD_RELOC_HI16);
3981 }
3982 if (mips_trap)
3983 {
3984 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", sreg, AT);
3985 /* We want to close the noreorder block as soon as possible, so
3986 that later insns are available for delay slot filling. */
3987 --mips_opts.noreorder;
3988 }
3989 else
3990 {
3991 expr1.X_add_number = 8;
3992 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
3993 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3994
3995 /* We want to close the noreorder block as soon as possible, so
3996 that later insns are available for delay slot filling. */
3997 --mips_opts.noreorder;
3998
3999 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
4000 }
4001 macro_build ((char *) NULL, &icnt, NULL, s, "d", dreg);
4002 break;
4003
4004 case M_DIV_3I:
4005 s = "div";
4006 s2 = "mflo";
4007 goto do_divi;
4008 case M_DIVU_3I:
4009 s = "divu";
4010 s2 = "mflo";
4011 goto do_divi;
4012 case M_REM_3I:
4013 s = "div";
4014 s2 = "mfhi";
4015 goto do_divi;
4016 case M_REMU_3I:
4017 s = "divu";
4018 s2 = "mfhi";
4019 goto do_divi;
4020 case M_DDIV_3I:
4021 dbl = 1;
4022 s = "ddiv";
4023 s2 = "mflo";
4024 goto do_divi;
4025 case M_DDIVU_3I:
4026 dbl = 1;
4027 s = "ddivu";
4028 s2 = "mflo";
4029 goto do_divi;
4030 case M_DREM_3I:
4031 dbl = 1;
4032 s = "ddiv";
4033 s2 = "mfhi";
4034 goto do_divi;
4035 case M_DREMU_3I:
4036 dbl = 1;
4037 s = "ddivu";
4038 s2 = "mfhi";
4039 do_divi:
4040 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4041 {
4042 as_warn (_("Divide by zero."));
4043 if (mips_trap)
4044 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
4045 else
beae10d5 4046 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4047 return;
4048 }
4049 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4050 {
4051 if (strcmp (s2, "mflo") == 0)
4052 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg,
4053 sreg);
4054 else
4055 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
4056 return;
4057 }
4058 if (imm_expr.X_op == O_constant
4059 && imm_expr.X_add_number == -1
4060 && s[strlen (s) - 1] != 'u')
4061 {
4062 if (strcmp (s2, "mflo") == 0)
4063 {
4064 if (dbl)
4065 macro_build ((char *) NULL, &icnt, NULL, "dneg", "d,w", dreg,
4066 sreg);
4067 else
4068 macro_build ((char *) NULL, &icnt, NULL, "neg", "d,w", dreg,
4069 sreg);
4070 }
4071 else
4072 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
4073 return;
4074 }
4075
4076 load_register (&icnt, AT, &imm_expr, dbl);
4077 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
4078 macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
4079 break;
4080
4081 case M_DIVU_3:
4082 s = "divu";
4083 s2 = "mflo";
4084 goto do_divu3;
4085 case M_REMU_3:
4086 s = "divu";
4087 s2 = "mfhi";
4088 goto do_divu3;
4089 case M_DDIVU_3:
4090 s = "ddivu";
4091 s2 = "mflo";
4092 goto do_divu3;
4093 case M_DREMU_3:
4094 s = "ddivu";
4095 s2 = "mfhi";
4096 do_divu3:
4097 mips_emit_delays (true);
4098 ++mips_opts.noreorder;
4099 mips_any_noreorder = 1;
4100 if (mips_trap)
4101 {
4102 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
4103 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4104 /* We want to close the noreorder block as soon as possible, so
4105 that later insns are available for delay slot filling. */
4106 --mips_opts.noreorder;
4107 }
4108 else
4109 {
4110 expr1.X_add_number = 8;
4111 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4112 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4113
4114 /* We want to close the noreorder block as soon as possible, so
4115 that later insns are available for delay slot filling. */
4116 --mips_opts.noreorder;
beae10d5 4117 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4118 }
4119 macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
4120 return;
4121
4122 case M_DLA_AB:
4123 dbl = 1;
4124 case M_LA_AB:
4125 /* Load the address of a symbol into a register. If breg is not
4126 zero, we then add a base register to it. */
4127
4128 /* When generating embedded PIC code, we permit expressions of
4129 the form
4130 la $4,foo-bar
bb2d6cd7 4131 where bar is an address in the current section. These are used
252b5132
RH
4132 when getting the addresses of functions. We don't permit
4133 X_add_number to be non-zero, because if the symbol is
4134 external the relaxing code needs to know that any addend is
4135 purely the offset to X_op_symbol. */
4136 if (mips_pic == EMBEDDED_PIC
4137 && offset_expr.X_op == O_subtract
49309057 4138 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4139 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4140 : (symbol_equated_p (offset_expr.X_op_symbol)
4141 && (S_GET_SEGMENT
4142 (symbol_get_value_expression (offset_expr.X_op_symbol)
4143 ->X_add_symbol)
bb2d6cd7 4144 == now_seg)))
252b5132 4145 && breg == 0
bb2d6cd7
GK
4146 && (offset_expr.X_add_number == 0
4147 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132
RH
4148 {
4149 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4150 treg, (int) BFD_RELOC_PCREL_HI16_S);
4151 macro_build ((char *) NULL, &icnt, &offset_expr,
4152 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4153 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4154 ? "addiu" : "daddiu"),
4155 "t,r,j", treg, treg, (int) BFD_RELOC_PCREL_LO16);
4156 return;
4157 }
4158
4159 if (offset_expr.X_op != O_symbol
4160 && offset_expr.X_op != O_constant)
4161 {
4162 as_bad (_("expression too complex"));
4163 offset_expr.X_op = O_constant;
4164 }
4165
4166 if (treg == breg)
4167 {
4168 tempreg = AT;
4169 used_at = 1;
4170 }
4171 else
4172 {
4173 tempreg = treg;
4174 used_at = 0;
4175 }
4176
4177 if (offset_expr.X_op == O_constant)
4178 load_register (&icnt, tempreg, &offset_expr, dbl);
4179 else if (mips_pic == NO_PIC)
4180 {
4181 /* If this is a reference to an GP relative symbol, we want
4182 addiu $tempreg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
4183 Otherwise we want
4184 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4185 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4186 If we have a constant, we need two instructions anyhow,
4187 so we may as well always use the latter form. */
4188 if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
4189 || nopic_need_relax (offset_expr.X_add_symbol, 1))
4190 p = NULL;
4191 else
4192 {
4193 frag_grow (20);
4194 macro_build ((char *) NULL, &icnt, &offset_expr,
4195 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4196 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4197 ? "addiu" : "daddiu"),
4198 "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
4199 p = frag_var (rs_machine_dependent, 8, 0,
4200 RELAX_ENCODE (4, 8, 0, 4, 0,
4201 mips_opts.warn_about_macros),
4202 offset_expr.X_add_symbol, (offsetT) 0,
4203 (char *) NULL);
4204 }
4205 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4206 if (p != NULL)
4207 p += 4;
4208 macro_build (p, &icnt, &offset_expr,
4209 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4210 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4211 ? "addiu" : "daddiu"),
4212 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4213 }
4214 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4215 {
4216 /* If this is a reference to an external symbol, and there
4217 is no constant, we want
4218 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4219 For a local symbol, we want
4220 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4221 nop
4222 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4223
4224 If we have a small constant, and this is a reference to
4225 an external symbol, we want
4226 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4227 nop
4228 addiu $tempreg,$tempreg,<constant>
4229 For a local symbol, we want the same instruction
4230 sequence, but we output a BFD_RELOC_LO16 reloc on the
4231 addiu instruction.
4232
4233 If we have a large constant, and this is a reference to
4234 an external symbol, we want
4235 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4236 lui $at,<hiconstant>
4237 addiu $at,$at,<loconstant>
4238 addu $tempreg,$tempreg,$at
4239 For a local symbol, we want the same instruction
4240 sequence, but we output a BFD_RELOC_LO16 reloc on the
4241 addiu instruction. */
4242 expr1.X_add_number = offset_expr.X_add_number;
4243 offset_expr.X_add_number = 0;
4244 frag_grow (32);
4245 macro_build ((char *) NULL, &icnt, &offset_expr,
4246 dbl ? "ld" : "lw",
4247 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
4248 if (expr1.X_add_number == 0)
4249 {
4250 int off;
4251
4252 if (breg == 0)
4253 off = 0;
4254 else
4255 {
4256 /* We're going to put in an addu instruction using
4257 tempreg, so we may as well insert the nop right
4258 now. */
4259 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4260 "nop", "");
4261 off = 4;
4262 }
4263 p = frag_var (rs_machine_dependent, 8 - off, 0,
4264 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4265 (breg == 0
4266 ? mips_opts.warn_about_macros
4267 : 0)),
4268 offset_expr.X_add_symbol, (offsetT) 0,
4269 (char *) NULL);
4270 if (breg == 0)
4271 {
4272 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4273 p += 4;
4274 }
4275 macro_build (p, &icnt, &expr1,
4276 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4277 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4278 ? "addiu" : "daddiu"),
4279 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4280 /* FIXME: If breg == 0, and the next instruction uses
4281 $tempreg, then if this variant case is used an extra
4282 nop will be generated. */
4283 }
4284 else if (expr1.X_add_number >= -0x8000
4285 && expr1.X_add_number < 0x8000)
4286 {
4287 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4288 "nop", "");
4289 macro_build ((char *) NULL, &icnt, &expr1,
4290 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4291 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4292 ? "addiu" : "daddiu"),
4293 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4294 (void) frag_var (rs_machine_dependent, 0, 0,
4295 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4296 offset_expr.X_add_symbol, (offsetT) 0,
4297 (char *) NULL);
4298 }
4299 else
4300 {
4301 int off1;
4302
4303 /* If we are going to add in a base register, and the
4304 target register and the base register are the same,
4305 then we are using AT as a temporary register. Since
4306 we want to load the constant into AT, we add our
4307 current AT (from the global offset table) and the
4308 register into the register now, and pretend we were
4309 not using a base register. */
4310 if (breg != treg)
4311 off1 = 0;
4312 else
4313 {
4314 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4315 "nop", "");
4316 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4317 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4318 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4319 ? "addu" : "daddu"),
4320 "d,v,t", treg, AT, breg);
4321 breg = 0;
4322 tempreg = treg;
4323 off1 = -8;
4324 }
4325
4326 /* Set mips_optimize around the lui instruction to avoid
4327 inserting an unnecessary nop after the lw. */
4328 hold_mips_optimize = mips_optimize;
4329 mips_optimize = 2;
4330 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
4331 mips_optimize = hold_mips_optimize;
4332
4333 macro_build ((char *) NULL, &icnt, &expr1,
4334 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4335 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4336 ? "addiu" : "daddiu"),
4337 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4338 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4339 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4340 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4341 ? "addu" : "daddu"),
4342 "d,v,t", tempreg, tempreg, AT);
4343 (void) frag_var (rs_machine_dependent, 0, 0,
4344 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4345 offset_expr.X_add_symbol, (offsetT) 0,
4346 (char *) NULL);
4347 used_at = 1;
4348 }
4349 }
4350 else if (mips_pic == SVR4_PIC)
4351 {
4352 int gpdel;
4353
4354 /* This is the large GOT case. If this is a reference to an
4355 external symbol, and there is no constant, we want
4356 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4357 addu $tempreg,$tempreg,$gp
4358 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4359 For a local symbol, we want
4360 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4361 nop
4362 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4363
4364 If we have a small constant, and this is a reference to
4365 an external symbol, we want
4366 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4367 addu $tempreg,$tempreg,$gp
4368 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4369 nop
4370 addiu $tempreg,$tempreg,<constant>
4371 For a local symbol, we want
4372 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4373 nop
4374 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4375
4376 If we have a large constant, and this is a reference to
4377 an external symbol, we want
4378 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4379 addu $tempreg,$tempreg,$gp
4380 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4381 lui $at,<hiconstant>
4382 addiu $at,$at,<loconstant>
4383 addu $tempreg,$tempreg,$at
4384 For a local symbol, we want
4385 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4386 lui $at,<hiconstant>
4387 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4388 addu $tempreg,$tempreg,$at
4389 */
4390 expr1.X_add_number = offset_expr.X_add_number;
4391 offset_expr.X_add_number = 0;
4392 frag_grow (52);
4393 if (reg_needs_delay (GP))
4394 gpdel = 4;
4395 else
4396 gpdel = 0;
4397 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4398 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
4399 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4400 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4401 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4402 ? "addu" : "daddu"),
4403 "d,v,t", tempreg, tempreg, GP);
4404 macro_build ((char *) NULL, &icnt, &offset_expr,
4405 dbl ? "ld" : "lw",
4406 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
4407 tempreg);
4408 if (expr1.X_add_number == 0)
4409 {
4410 int off;
4411
4412 if (breg == 0)
4413 off = 0;
4414 else
4415 {
4416 /* We're going to put in an addu instruction using
4417 tempreg, so we may as well insert the nop right
4418 now. */
4419 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4420 "nop", "");
4421 off = 4;
4422 }
4423
4424 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4425 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4426 8 + gpdel, 0,
4427 (breg == 0
4428 ? mips_opts.warn_about_macros
4429 : 0)),
4430 offset_expr.X_add_symbol, (offsetT) 0,
4431 (char *) NULL);
4432 }
4433 else if (expr1.X_add_number >= -0x8000
4434 && expr1.X_add_number < 0x8000)
4435 {
4436 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4437 "nop", "");
4438 macro_build ((char *) NULL, &icnt, &expr1,
4439 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4440 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4441 ? "addiu" : "daddiu"),
4442 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4443
4444 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4445 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4446 (breg == 0
4447 ? mips_opts.warn_about_macros
4448 : 0)),
4449 offset_expr.X_add_symbol, (offsetT) 0,
4450 (char *) NULL);
4451 }
4452 else
4453 {
4454 int adj, dreg;
4455
4456 /* If we are going to add in a base register, and the
4457 target register and the base register are the same,
4458 then we are using AT as a temporary register. Since
4459 we want to load the constant into AT, we add our
4460 current AT (from the global offset table) and the
4461 register into the register now, and pretend we were
4462 not using a base register. */
4463 if (breg != treg)
4464 {
4465 adj = 0;
4466 dreg = tempreg;
4467 }
4468 else
4469 {
4470 assert (tempreg == AT);
4471 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4472 "nop", "");
4473 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4474 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4475 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4476 ? "addu" : "daddu"),
4477 "d,v,t", treg, AT, breg);
4478 dreg = treg;
4479 adj = 8;
4480 }
4481
4482 /* Set mips_optimize around the lui instruction to avoid
4483 inserting an unnecessary nop after the lw. */
4484 hold_mips_optimize = mips_optimize;
4485 mips_optimize = 2;
4486 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
4487 mips_optimize = hold_mips_optimize;
4488
4489 macro_build ((char *) NULL, &icnt, &expr1,
4490 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4491 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4492 ? "addiu" : "daddiu"),
4493 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4494 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4495 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4496 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4497 ? "addu" : "daddu"),
4498 "d,v,t", dreg, dreg, AT);
4499
4500 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4501 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4502 8 + gpdel, 0,
4503 (breg == 0
4504 ? mips_opts.warn_about_macros
4505 : 0)),
4506 offset_expr.X_add_symbol, (offsetT) 0,
4507 (char *) NULL);
4508
4509 used_at = 1;
4510 }
4511
4512 if (gpdel > 0)
4513 {
4514 /* This is needed because this instruction uses $gp, but
4515 the first instruction on the main stream does not. */
4516 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4517 p += 4;
4518 }
4519 macro_build (p, &icnt, &offset_expr,
4520 dbl ? "ld" : "lw",
4521 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
4522 p += 4;
4523 if (expr1.X_add_number >= -0x8000
4524 && expr1.X_add_number < 0x8000)
4525 {
4526 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4527 p += 4;
4528 macro_build (p, &icnt, &expr1,
4529 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4530 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4531 ? "addiu" : "daddiu"),
4532 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4533 /* FIXME: If add_number is 0, and there was no base
4534 register, the external symbol case ended with a load,
4535 so if the symbol turns out to not be external, and
4536 the next instruction uses tempreg, an unnecessary nop
4537 will be inserted. */
4538 }
4539 else
4540 {
4541 if (breg == treg)
4542 {
4543 /* We must add in the base register now, as in the
4544 external symbol case. */
4545 assert (tempreg == AT);
4546 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4547 p += 4;
4548 macro_build (p, &icnt, (expressionS *) NULL,
4549 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4550 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4551 ? "addu" : "daddu"),
4552 "d,v,t", treg, AT, breg);
4553 p += 4;
4554 tempreg = treg;
4555 /* We set breg to 0 because we have arranged to add
4556 it in in both cases. */
4557 breg = 0;
4558 }
4559
4560 macro_build_lui (p, &icnt, &expr1, AT);
4561 p += 4;
4562 macro_build (p, &icnt, &expr1,
4563 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4564 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4565 ? "addiu" : "daddiu"),
4566 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4567 p += 4;
4568 macro_build (p, &icnt, (expressionS *) NULL,
4569 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4570 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4571 ? "addu" : "daddu"),
4572 "d,v,t", tempreg, tempreg, AT);
4573 p += 4;
4574 }
4575 }
4576 else if (mips_pic == EMBEDDED_PIC)
4577 {
4578 /* We use
4579 addiu $tempreg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
4580 */
4581 macro_build ((char *) NULL, &icnt, &offset_expr,
4582 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4583 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4584 ? "addiu" : "daddiu"),
4585 "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
4586 }
4587 else
4588 abort ();
4589
4590 if (breg != 0)
4591 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4592 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4593 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4594 ? "addu" : "daddu"),
4595 "d,v,t", treg, tempreg, breg);
4596
4597 if (! used_at)
4598 return;
4599
4600 break;
4601
4602 case M_J_A:
4603 /* The j instruction may not be used in PIC code, since it
4604 requires an absolute address. We convert it to a b
4605 instruction. */
4606 if (mips_pic == NO_PIC)
4607 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
4608 else
4609 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4610 return;
4611
4612 /* The jal instructions must be handled as macros because when
4613 generating PIC code they expand to multi-instruction
4614 sequences. Normally they are simple instructions. */
4615 case M_JAL_1:
4616 dreg = RA;
4617 /* Fall through. */
4618 case M_JAL_2:
4619 if (mips_pic == NO_PIC
4620 || mips_pic == EMBEDDED_PIC)
4621 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
4622 "d,s", dreg, sreg);
4623 else if (mips_pic == SVR4_PIC)
4624 {
4625 if (sreg != PIC_CALL_REG)
4626 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 4627
252b5132
RH
4628 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
4629 "d,s", dreg, sreg);
4630 if (mips_cprestore_offset < 0)
4631 as_warn (_("No .cprestore pseudo-op used in PIC code"));
4632 else
4633 {
4634 expr1.X_add_number = mips_cprestore_offset;
4635 macro_build ((char *) NULL, &icnt, &expr1,
4636 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4637 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4638 ? "lw" : "ld"),
4639 "t,o(b)", GP, (int) BFD_RELOC_LO16, mips_frame_reg);
4640 }
4641 }
4642 else
4643 abort ();
4644
4645 return;
4646
4647 case M_JAL_A:
4648 if (mips_pic == NO_PIC)
4649 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
4650 else if (mips_pic == SVR4_PIC)
4651 {
4652 /* If this is a reference to an external symbol, and we are
4653 using a small GOT, we want
4654 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4655 nop
4656 jalr $25
4657 nop
4658 lw $gp,cprestore($sp)
4659 The cprestore value is set using the .cprestore
4660 pseudo-op. If we are using a big GOT, we want
4661 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4662 addu $25,$25,$gp
4663 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
4664 nop
4665 jalr $25
4666 nop
4667 lw $gp,cprestore($sp)
4668 If the symbol is not external, we want
4669 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4670 nop
4671 addiu $25,$25,<sym> (BFD_RELOC_LO16)
4672 jalr $25
4673 nop
4674 lw $gp,cprestore($sp) */
4675 frag_grow (40);
4676 if (! mips_big_got)
4677 {
4678 macro_build ((char *) NULL, &icnt, &offset_expr,
4679 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4680 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4681 ? "lw" : "ld"),
4682 "t,o(b)", PIC_CALL_REG,
4683 (int) BFD_RELOC_MIPS_CALL16, GP);
4684 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4685 "nop", "");
4686 p = frag_var (rs_machine_dependent, 4, 0,
4687 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
4688 offset_expr.X_add_symbol, (offsetT) 0,
4689 (char *) NULL);
4690 }
4691 else
4692 {
4693 int gpdel;
4694
4695 if (reg_needs_delay (GP))
4696 gpdel = 4;
4697 else
4698 gpdel = 0;
4699 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4700 PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
4701 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4702 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4703 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4704 ? "addu" : "daddu"),
4705 "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
4706 macro_build ((char *) NULL, &icnt, &offset_expr,
4707 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4708 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4709 ? "lw" : "ld"),
4710 "t,o(b)", PIC_CALL_REG,
4711 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
4712 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4713 "nop", "");
4714 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4715 RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
4716 0, 0),
4717 offset_expr.X_add_symbol, (offsetT) 0,
4718 (char *) NULL);
4719 if (gpdel > 0)
4720 {
4721 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4722 p += 4;
4723 }
4724 macro_build (p, &icnt, &offset_expr,
4725 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4726 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4727 ? "lw" : "ld"),
4728 "t,o(b)", PIC_CALL_REG,
4729 (int) BFD_RELOC_MIPS_GOT16, GP);
4730 p += 4;
4731 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4732 p += 4;
bdaaa2e1 4733 }
252b5132
RH
4734 macro_build (p, &icnt, &offset_expr,
4735 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4736 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4737 ? "addiu" : "daddiu"),
4738 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
4739 (int) BFD_RELOC_LO16);
4740 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4741 "jalr", "s", PIC_CALL_REG);
4742 if (mips_cprestore_offset < 0)
4743 as_warn (_("No .cprestore pseudo-op used in PIC code"));
4744 else
4745 {
4746 if (mips_opts.noreorder)
4747 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4748 "nop", "");
4749 expr1.X_add_number = mips_cprestore_offset;
4750 macro_build ((char *) NULL, &icnt, &expr1,
4751 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 4752 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
4753 ? "lw" : "ld"),
4754 "t,o(b)", GP, (int) BFD_RELOC_LO16,
4755 mips_frame_reg);
4756 }
4757 }
4758 else if (mips_pic == EMBEDDED_PIC)
4759 {
4760 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
4761 /* The linker may expand the call to a longer sequence which
4762 uses $at, so we must break rather than return. */
4763 break;
4764 }
4765 else
4766 abort ();
4767
4768 return;
4769
4770 case M_LB_AB:
4771 s = "lb";
4772 goto ld;
4773 case M_LBU_AB:
4774 s = "lbu";
4775 goto ld;
4776 case M_LH_AB:
4777 s = "lh";
4778 goto ld;
4779 case M_LHU_AB:
4780 s = "lhu";
4781 goto ld;
4782 case M_LW_AB:
4783 s = "lw";
4784 goto ld;
4785 case M_LWC0_AB:
4786 s = "lwc0";
bdaaa2e1 4787 /* Itbl support may require additional care here. */
252b5132
RH
4788 coproc = 1;
4789 goto ld;
4790 case M_LWC1_AB:
4791 s = "lwc1";
bdaaa2e1 4792 /* Itbl support may require additional care here. */
252b5132
RH
4793 coproc = 1;
4794 goto ld;
4795 case M_LWC2_AB:
4796 s = "lwc2";
bdaaa2e1 4797 /* Itbl support may require additional care here. */
252b5132
RH
4798 coproc = 1;
4799 goto ld;
4800 case M_LWC3_AB:
4801 s = "lwc3";
bdaaa2e1 4802 /* Itbl support may require additional care here. */
252b5132
RH
4803 coproc = 1;
4804 goto ld;
4805 case M_LWL_AB:
4806 s = "lwl";
4807 lr = 1;
4808 goto ld;
4809 case M_LWR_AB:
4810 s = "lwr";
4811 lr = 1;
4812 goto ld;
4813 case M_LDC1_AB:
156c2f8b 4814 if (mips_cpu == CPU_R4650)
252b5132
RH
4815 {
4816 as_bad (_("opcode not supported on this processor"));
4817 return;
4818 }
4819 s = "ldc1";
bdaaa2e1 4820 /* Itbl support may require additional care here. */
252b5132
RH
4821 coproc = 1;
4822 goto ld;
4823 case M_LDC2_AB:
4824 s = "ldc2";
bdaaa2e1 4825 /* Itbl support may require additional care here. */
252b5132
RH
4826 coproc = 1;
4827 goto ld;
4828 case M_LDC3_AB:
4829 s = "ldc3";
bdaaa2e1 4830 /* Itbl support may require additional care here. */
252b5132
RH
4831 coproc = 1;
4832 goto ld;
4833 case M_LDL_AB:
4834 s = "ldl";
4835 lr = 1;
4836 goto ld;
4837 case M_LDR_AB:
4838 s = "ldr";
4839 lr = 1;
4840 goto ld;
4841 case M_LL_AB:
4842 s = "ll";
4843 goto ld;
4844 case M_LLD_AB:
4845 s = "lld";
4846 goto ld;
4847 case M_LWU_AB:
4848 s = "lwu";
4849 ld:
4850 if (breg == treg || coproc || lr)
4851 {
4852 tempreg = AT;
4853 used_at = 1;
4854 }
4855 else
4856 {
4857 tempreg = treg;
4858 used_at = 0;
4859 }
4860 goto ld_st;
4861 case M_SB_AB:
4862 s = "sb";
4863 goto st;
4864 case M_SH_AB:
4865 s = "sh";
4866 goto st;
4867 case M_SW_AB:
4868 s = "sw";
4869 goto st;
4870 case M_SWC0_AB:
4871 s = "swc0";
bdaaa2e1 4872 /* Itbl support may require additional care here. */
252b5132
RH
4873 coproc = 1;
4874 goto st;
4875 case M_SWC1_AB:
4876 s = "swc1";
bdaaa2e1 4877 /* Itbl support may require additional care here. */
252b5132
RH
4878 coproc = 1;
4879 goto st;
4880 case M_SWC2_AB:
4881 s = "swc2";
bdaaa2e1 4882 /* Itbl support may require additional care here. */
252b5132
RH
4883 coproc = 1;
4884 goto st;
4885 case M_SWC3_AB:
4886 s = "swc3";
bdaaa2e1 4887 /* Itbl support may require additional care here. */
252b5132
RH
4888 coproc = 1;
4889 goto st;
4890 case M_SWL_AB:
4891 s = "swl";
4892 goto st;
4893 case M_SWR_AB:
4894 s = "swr";
4895 goto st;
4896 case M_SC_AB:
4897 s = "sc";
4898 goto st;
4899 case M_SCD_AB:
4900 s = "scd";
4901 goto st;
4902 case M_SDC1_AB:
156c2f8b 4903 if (mips_cpu == CPU_R4650)
252b5132
RH
4904 {
4905 as_bad (_("opcode not supported on this processor"));
4906 return;
4907 }
4908 s = "sdc1";
4909 coproc = 1;
bdaaa2e1 4910 /* Itbl support may require additional care here. */
252b5132
RH
4911 goto st;
4912 case M_SDC2_AB:
4913 s = "sdc2";
bdaaa2e1 4914 /* Itbl support may require additional care here. */
252b5132
RH
4915 coproc = 1;
4916 goto st;
4917 case M_SDC3_AB:
4918 s = "sdc3";
bdaaa2e1 4919 /* Itbl support may require additional care here. */
252b5132
RH
4920 coproc = 1;
4921 goto st;
4922 case M_SDL_AB:
4923 s = "sdl";
4924 goto st;
4925 case M_SDR_AB:
4926 s = "sdr";
4927 st:
4928 tempreg = AT;
4929 used_at = 1;
4930 ld_st:
bdaaa2e1 4931 /* Itbl support may require additional care here. */
252b5132
RH
4932 if (mask == M_LWC1_AB
4933 || mask == M_SWC1_AB
4934 || mask == M_LDC1_AB
4935 || mask == M_SDC1_AB
4936 || mask == M_L_DAB
4937 || mask == M_S_DAB)
4938 fmt = "T,o(b)";
4939 else if (coproc)
4940 fmt = "E,o(b)";
4941 else
4942 fmt = "t,o(b)";
4943
4944 if (offset_expr.X_op != O_constant
4945 && offset_expr.X_op != O_symbol)
4946 {
4947 as_bad (_("expression too complex"));
4948 offset_expr.X_op = O_constant;
4949 }
4950
4951 /* A constant expression in PIC code can be handled just as it
4952 is in non PIC code. */
4953 if (mips_pic == NO_PIC
4954 || offset_expr.X_op == O_constant)
4955 {
4956 /* If this is a reference to a GP relative symbol, and there
4957 is no base register, we want
4958 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
4959 Otherwise, if there is no base register, we want
4960 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4961 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
4962 If we have a constant, we need two instructions anyhow,
4963 so we always use the latter form.
4964
4965 If we have a base register, and this is a reference to a
4966 GP relative symbol, we want
4967 addu $tempreg,$breg,$gp
4968 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GPREL)
4969 Otherwise we want
4970 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4971 addu $tempreg,$tempreg,$breg
4972 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
4973 With a constant we always use the latter case. */
4974 if (breg == 0)
4975 {
4976 if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
4977 || nopic_need_relax (offset_expr.X_add_symbol, 1))
4978 p = NULL;
4979 else
4980 {
4981 frag_grow (20);
4982 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4983 treg, (int) BFD_RELOC_MIPS_GPREL, GP);
4984 p = frag_var (rs_machine_dependent, 8, 0,
4985 RELAX_ENCODE (4, 8, 0, 4, 0,
4986 (mips_opts.warn_about_macros
4987 || (used_at
4988 && mips_opts.noat))),
4989 offset_expr.X_add_symbol, (offsetT) 0,
4990 (char *) NULL);
4991 used_at = 0;
4992 }
4993 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4994 if (p != NULL)
4995 p += 4;
4996 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
4997 (int) BFD_RELOC_LO16, tempreg);
4998 }
4999 else
5000 {
5001 if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
5002 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5003 p = NULL;
5004 else
5005 {
5006 frag_grow (28);
5007 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5008 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5009 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5010 ? "addu" : "daddu"),
5011 "d,v,t", tempreg, breg, GP);
5012 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5013 treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
5014 p = frag_var (rs_machine_dependent, 12, 0,
5015 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5016 offset_expr.X_add_symbol, (offsetT) 0,
5017 (char *) NULL);
5018 }
5019 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5020 if (p != NULL)
5021 p += 4;
5022 macro_build (p, &icnt, (expressionS *) NULL,
5023 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5024 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5025 ? "addu" : "daddu"),
5026 "d,v,t", tempreg, tempreg, breg);
5027 if (p != NULL)
5028 p += 4;
5029 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5030 (int) BFD_RELOC_LO16, tempreg);
5031 }
5032 }
5033 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5034 {
5035 /* If this is a reference to an external symbol, we want
5036 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5037 nop
5038 <op> $treg,0($tempreg)
5039 Otherwise we want
5040 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5041 nop
5042 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5043 <op> $treg,0($tempreg)
5044 If there is a base register, we add it to $tempreg before
5045 the <op>. If there is a constant, we stick it in the
5046 <op> instruction. We don't handle constants larger than
5047 16 bits, because we have no way to load the upper 16 bits
5048 (actually, we could handle them for the subset of cases
5049 in which we are not using $at). */
5050 assert (offset_expr.X_op == O_symbol);
5051 expr1.X_add_number = offset_expr.X_add_number;
5052 offset_expr.X_add_number = 0;
5053 if (expr1.X_add_number < -0x8000
5054 || expr1.X_add_number >= 0x8000)
5055 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5056 frag_grow (20);
5057 macro_build ((char *) NULL, &icnt, &offset_expr,
5058 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5059 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5060 ? "lw" : "ld"),
5061 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5062 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
bdaaa2e1 5063 p = frag_var (rs_machine_dependent, 4, 0,
252b5132
RH
5064 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5065 offset_expr.X_add_symbol, (offsetT) 0,
5066 (char *) NULL);
5067 macro_build (p, &icnt, &offset_expr,
5068 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5069 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5070 ? "addiu" : "daddiu"),
5071 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5072 if (breg != 0)
5073 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5074 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5075 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5076 ? "addu" : "daddu"),
5077 "d,v,t", tempreg, tempreg, breg);
5078 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5079 (int) BFD_RELOC_LO16, tempreg);
5080 }
5081 else if (mips_pic == SVR4_PIC)
5082 {
5083 int gpdel;
5084
5085 /* If this is a reference to an external symbol, we want
5086 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5087 addu $tempreg,$tempreg,$gp
5088 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5089 <op> $treg,0($tempreg)
5090 Otherwise we want
5091 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5092 nop
5093 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5094 <op> $treg,0($tempreg)
5095 If there is a base register, we add it to $tempreg before
5096 the <op>. If there is a constant, we stick it in the
5097 <op> instruction. We don't handle constants larger than
5098 16 bits, because we have no way to load the upper 16 bits
5099 (actually, we could handle them for the subset of cases
5100 in which we are not using $at). */
5101 assert (offset_expr.X_op == O_symbol);
5102 expr1.X_add_number = offset_expr.X_add_number;
5103 offset_expr.X_add_number = 0;
5104 if (expr1.X_add_number < -0x8000
5105 || expr1.X_add_number >= 0x8000)
5106 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5107 if (reg_needs_delay (GP))
5108 gpdel = 4;
5109 else
5110 gpdel = 0;
5111 frag_grow (36);
5112 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5113 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5114 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5115 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5116 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5117 ? "addu" : "daddu"),
5118 "d,v,t", tempreg, tempreg, GP);
5119 macro_build ((char *) NULL, &icnt, &offset_expr,
5120 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5121 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5122 ? "lw" : "ld"),
5123 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5124 tempreg);
5125 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5126 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5127 offset_expr.X_add_symbol, (offsetT) 0, (char *) NULL);
5128 if (gpdel > 0)
5129 {
5130 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5131 p += 4;
5132 }
5133 macro_build (p, &icnt, &offset_expr,
5134 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5135 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5136 ? "lw" : "ld"),
5137 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5138 p += 4;
5139 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5140 p += 4;
5141 macro_build (p, &icnt, &offset_expr,
5142 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5143 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5144 ? "addiu" : "daddiu"),
5145 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5146 if (breg != 0)
5147 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5148 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5149 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5150 ? "addu" : "daddu"),
5151 "d,v,t", tempreg, tempreg, breg);
5152 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5153 (int) BFD_RELOC_LO16, tempreg);
5154 }
5155 else if (mips_pic == EMBEDDED_PIC)
5156 {
5157 /* If there is no base register, we want
5158 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5159 If there is a base register, we want
5160 addu $tempreg,$breg,$gp
5161 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GPREL)
5162 */
5163 assert (offset_expr.X_op == O_symbol);
5164 if (breg == 0)
5165 {
5166 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5167 treg, (int) BFD_RELOC_MIPS_GPREL, GP);
5168 used_at = 0;
5169 }
5170 else
5171 {
5172 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5173 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5174 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5175 ? "addu" : "daddu"),
5176 "d,v,t", tempreg, breg, GP);
5177 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5178 treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
5179 }
5180 }
5181 else
5182 abort ();
5183
5184 if (! used_at)
5185 return;
5186
5187 break;
5188
5189 case M_LI:
5190 case M_LI_S:
5191 load_register (&icnt, treg, &imm_expr, 0);
5192 return;
5193
5194 case M_DLI:
5195 load_register (&icnt, treg, &imm_expr, 1);
5196 return;
5197
5198 case M_LI_SS:
5199 if (imm_expr.X_op == O_constant)
5200 {
5201 load_register (&icnt, AT, &imm_expr, 0);
5202 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5203 "mtc1", "t,G", AT, treg);
5204 break;
5205 }
5206 else
5207 {
5208 assert (offset_expr.X_op == O_symbol
5209 && strcmp (segment_name (S_GET_SEGMENT
5210 (offset_expr.X_add_symbol)),
5211 ".lit4") == 0
5212 && offset_expr.X_add_number == 0);
5213 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5214 treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5215 return;
5216 }
5217
5218 case M_LI_D:
5219 /* If we have a constant in IMM_EXPR, then in mips3 mode it is
5220 the entire value, and in mips1 mode it is the high order 32
5221 bits of the value and the low order 32 bits are either zero
5222 or in offset_expr. */
5223 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5224 {
9ce8a5dd 5225 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5226 load_register (&icnt, treg, &imm_expr, 1);
5227 else
5228 {
5229 int hreg, lreg;
5230
5231 if (target_big_endian)
5232 {
5233 hreg = treg;
5234 lreg = treg + 1;
5235 }
5236 else
5237 {
5238 hreg = treg + 1;
5239 lreg = treg;
5240 }
5241
5242 if (hreg <= 31)
5243 load_register (&icnt, hreg, &imm_expr, 0);
5244 if (lreg <= 31)
5245 {
5246 if (offset_expr.X_op == O_absent)
5247 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s",
5248 lreg, 0);
5249 else
5250 {
5251 assert (offset_expr.X_op == O_constant);
5252 load_register (&icnt, lreg, &offset_expr, 0);
5253 }
5254 }
5255 }
5256 return;
5257 }
5258
5259 /* We know that sym is in the .rdata section. First we get the
5260 upper 16 bits of the address. */
5261 if (mips_pic == NO_PIC)
5262 {
5263 /* FIXME: This won't work for a 64 bit address. */
5264 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
5265 }
5266 else if (mips_pic == SVR4_PIC)
5267 {
5268 macro_build ((char *) NULL, &icnt, &offset_expr,
5269 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5270 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5271 ? "lw" : "ld"),
5272 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5273 }
5274 else if (mips_pic == EMBEDDED_PIC)
5275 {
5276 /* For embedded PIC we pick up the entire address off $gp in
5277 a single instruction. */
5278 macro_build ((char *) NULL, &icnt, &offset_expr,
5279 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5280 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5281 ? "addiu" : "daddiu"),
5282 "t,r,j", AT, GP, (int) BFD_RELOC_MIPS_GPREL);
5283 offset_expr.X_op = O_constant;
5284 offset_expr.X_add_number = 0;
5285 }
5286 else
5287 abort ();
bdaaa2e1 5288
252b5132 5289 /* Now we load the register(s). */
9ce8a5dd 5290 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5291 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5292 treg, (int) BFD_RELOC_LO16, AT);
5293 else
5294 {
5295 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5296 treg, (int) BFD_RELOC_LO16, AT);
5297 if (treg != 31)
5298 {
5299 /* FIXME: How in the world do we deal with the possible
5300 overflow here? */
5301 offset_expr.X_add_number += 4;
5302 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5303 treg + 1, (int) BFD_RELOC_LO16, AT);
5304 }
5305 }
5306
5307 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5308 does not become a variant frag. */
5309 frag_wane (frag_now);
5310 frag_new (0);
5311
5312 break;
5313
5314 case M_LI_DD:
5315 /* If we have a constant in IMM_EXPR, then in mips3 mode it is
5316 the entire value, and in mips1 mode it is the high order 32
5317 bits of the value and the low order 32 bits are either zero
5318 or in offset_expr. */
5319 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5320 {
9ce8a5dd
GRK
5321 load_register (&icnt, AT, &imm_expr, ISA_HAS_64BIT_REGS (mips_opts.isa));
5322 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5323 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5324 "dmtc1", "t,S", AT, treg);
5325 else
5326 {
5327 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5328 "mtc1", "t,G", AT, treg + 1);
5329 if (offset_expr.X_op == O_absent)
5330 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5331 "mtc1", "t,G", 0, treg);
5332 else
5333 {
5334 assert (offset_expr.X_op == O_constant);
5335 load_register (&icnt, AT, &offset_expr, 0);
5336 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5337 "mtc1", "t,G", AT, treg);
5338 }
5339 }
5340 break;
5341 }
5342
5343 assert (offset_expr.X_op == O_symbol
5344 && offset_expr.X_add_number == 0);
5345 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5346 if (strcmp (s, ".lit8") == 0)
5347 {
e7af610e 5348 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5349 {
5350 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5351 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5352 return;
5353 }
5354 breg = GP;
5355 r = BFD_RELOC_MIPS_LITERAL;
5356 goto dob;
5357 }
5358 else
5359 {
5360 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5361 if (mips_pic == SVR4_PIC)
5362 macro_build ((char *) NULL, &icnt, &offset_expr,
5363 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5364 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5365 ? "lw" : "ld"),
5366 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5367 else
5368 {
5369 /* FIXME: This won't work for a 64 bit address. */
5370 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
5371 }
bdaaa2e1 5372
e7af610e 5373 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5374 {
5375 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5376 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5377
5378 /* To avoid confusion in tc_gen_reloc, we must ensure
5379 that this does not become a variant frag. */
5380 frag_wane (frag_now);
5381 frag_new (0);
5382
5383 break;
5384 }
5385 breg = AT;
5386 r = BFD_RELOC_LO16;
5387 goto dob;
5388 }
5389
5390 case M_L_DOB:
156c2f8b 5391 if (mips_cpu == CPU_R4650)
252b5132
RH
5392 {
5393 as_bad (_("opcode not supported on this processor"));
5394 return;
5395 }
5396 /* Even on a big endian machine $fn comes before $fn+1. We have
5397 to adjust when loading from memory. */
5398 r = BFD_RELOC_LO16;
5399 dob:
e7af610e 5400 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
5401 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5402 target_big_endian ? treg + 1 : treg,
5403 (int) r, breg);
5404 /* FIXME: A possible overflow which I don't know how to deal
5405 with. */
5406 offset_expr.X_add_number += 4;
5407 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5408 target_big_endian ? treg : treg + 1,
5409 (int) r, breg);
5410
5411 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5412 does not become a variant frag. */
5413 frag_wane (frag_now);
5414 frag_new (0);
5415
5416 if (breg != AT)
5417 return;
5418 break;
5419
5420 case M_L_DAB:
5421 /*
5422 * The MIPS assembler seems to check for X_add_number not
5423 * being double aligned and generating:
5424 * lui at,%hi(foo+1)
5425 * addu at,at,v1
5426 * addiu at,at,%lo(foo+1)
5427 * lwc1 f2,0(at)
5428 * lwc1 f3,4(at)
5429 * But, the resulting address is the same after relocation so why
5430 * generate the extra instruction?
5431 */
156c2f8b 5432 if (mips_cpu == CPU_R4650)
252b5132
RH
5433 {
5434 as_bad (_("opcode not supported on this processor"));
5435 return;
5436 }
bdaaa2e1 5437 /* Itbl support may require additional care here. */
252b5132 5438 coproc = 1;
e7af610e 5439 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5440 {
5441 s = "ldc1";
5442 goto ld;
5443 }
5444
5445 s = "lwc1";
5446 fmt = "T,o(b)";
5447 goto ldd_std;
5448
5449 case M_S_DAB:
156c2f8b 5450 if (mips_cpu == CPU_R4650)
252b5132
RH
5451 {
5452 as_bad (_("opcode not supported on this processor"));
5453 return;
5454 }
5455
e7af610e 5456 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5457 {
5458 s = "sdc1";
5459 goto st;
5460 }
5461
5462 s = "swc1";
5463 fmt = "T,o(b)";
bdaaa2e1 5464 /* Itbl support may require additional care here. */
252b5132
RH
5465 coproc = 1;
5466 goto ldd_std;
5467
5468 case M_LD_AB:
9ce8a5dd 5469 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5470 {
5471 s = "ld";
5472 goto ld;
5473 }
5474
5475 s = "lw";
5476 fmt = "t,o(b)";
5477 goto ldd_std;
5478
5479 case M_SD_AB:
9ce8a5dd 5480 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5481 {
5482 s = "sd";
5483 goto st;
5484 }
5485
5486 s = "sw";
5487 fmt = "t,o(b)";
5488
5489 ldd_std:
5490 if (offset_expr.X_op != O_symbol
5491 && offset_expr.X_op != O_constant)
5492 {
5493 as_bad (_("expression too complex"));
5494 offset_expr.X_op = O_constant;
5495 }
5496
5497 /* Even on a big endian machine $fn comes before $fn+1. We have
5498 to adjust when loading from memory. We set coproc if we must
5499 load $fn+1 first. */
bdaaa2e1 5500 /* Itbl support may require additional care here. */
252b5132
RH
5501 if (! target_big_endian)
5502 coproc = 0;
5503
5504 if (mips_pic == NO_PIC
5505 || offset_expr.X_op == O_constant)
5506 {
5507 /* If this is a reference to a GP relative symbol, we want
5508 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5509 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
5510 If we have a base register, we use this
5511 addu $at,$breg,$gp
5512 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
5513 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
5514 If this is not a GP relative symbol, we want
5515 lui $at,<sym> (BFD_RELOC_HI16_S)
5516 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5517 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5518 If there is a base register, we add it to $at after the
5519 lui instruction. If there is a constant, we always use
5520 the last case. */
5521 if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
5522 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5523 {
5524 p = NULL;
5525 used_at = 1;
5526 }
5527 else
5528 {
5529 int off;
5530
5531 if (breg == 0)
5532 {
5533 frag_grow (28);
5534 tempreg = GP;
5535 off = 0;
5536 used_at = 0;
5537 }
5538 else
5539 {
5540 frag_grow (36);
5541 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5542 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5543 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5544 ? "addu" : "daddu"),
5545 "d,v,t", AT, breg, GP);
5546 tempreg = AT;
5547 off = 4;
5548 used_at = 1;
5549 }
5550
beae10d5 5551 /* Itbl support may require additional care here. */
252b5132
RH
5552 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5553 coproc ? treg + 1 : treg,
5554 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5555 offset_expr.X_add_number += 4;
5556
5557 /* Set mips_optimize to 2 to avoid inserting an
5558 undesired nop. */
5559 hold_mips_optimize = mips_optimize;
5560 mips_optimize = 2;
beae10d5 5561 /* Itbl support may require additional care here. */
252b5132
RH
5562 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5563 coproc ? treg : treg + 1,
5564 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5565 mips_optimize = hold_mips_optimize;
5566
5567 p = frag_var (rs_machine_dependent, 12 + off, 0,
5568 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
5569 used_at && mips_opts.noat),
5570 offset_expr.X_add_symbol, (offsetT) 0,
5571 (char *) NULL);
5572
5573 /* We just generated two relocs. When tc_gen_reloc
5574 handles this case, it will skip the first reloc and
5575 handle the second. The second reloc already has an
5576 extra addend of 4, which we added above. We must
5577 subtract it out, and then subtract another 4 to make
5578 the first reloc come out right. The second reloc
5579 will come out right because we are going to add 4 to
5580 offset_expr when we build its instruction below.
5581
5582 If we have a symbol, then we don't want to include
5583 the offset, because it will wind up being included
5584 when we generate the reloc. */
5585
5586 if (offset_expr.X_op == O_constant)
5587 offset_expr.X_add_number -= 8;
5588 else
5589 {
5590 offset_expr.X_add_number = -4;
5591 offset_expr.X_op = O_constant;
5592 }
5593 }
5594 macro_build_lui (p, &icnt, &offset_expr, AT);
5595 if (p != NULL)
5596 p += 4;
5597 if (breg != 0)
5598 {
5599 macro_build (p, &icnt, (expressionS *) NULL,
5600 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5601 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5602 ? "addu" : "daddu"),
5603 "d,v,t", AT, breg, AT);
5604 if (p != NULL)
5605 p += 4;
5606 }
beae10d5 5607 /* Itbl support may require additional care here. */
252b5132
RH
5608 macro_build (p, &icnt, &offset_expr, s, fmt,
5609 coproc ? treg + 1 : treg,
5610 (int) BFD_RELOC_LO16, AT);
5611 if (p != NULL)
5612 p += 4;
5613 /* FIXME: How do we handle overflow here? */
5614 offset_expr.X_add_number += 4;
beae10d5 5615 /* Itbl support may require additional care here. */
252b5132
RH
5616 macro_build (p, &icnt, &offset_expr, s, fmt,
5617 coproc ? treg : treg + 1,
5618 (int) BFD_RELOC_LO16, AT);
bdaaa2e1 5619 }
252b5132
RH
5620 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5621 {
5622 int off;
5623
5624 /* If this is a reference to an external symbol, we want
5625 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5626 nop
5627 <op> $treg,0($at)
5628 <op> $treg+1,4($at)
5629 Otherwise we want
5630 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5631 nop
5632 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5633 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5634 If there is a base register we add it to $at before the
5635 lwc1 instructions. If there is a constant we include it
5636 in the lwc1 instructions. */
5637 used_at = 1;
5638 expr1.X_add_number = offset_expr.X_add_number;
5639 offset_expr.X_add_number = 0;
5640 if (expr1.X_add_number < -0x8000
5641 || expr1.X_add_number >= 0x8000 - 4)
5642 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5643 if (breg == 0)
5644 off = 0;
5645 else
5646 off = 4;
5647 frag_grow (24 + off);
5648 macro_build ((char *) NULL, &icnt, &offset_expr,
5649 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5650 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5651 ? "lw" : "ld"),
5652 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5653 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5654 if (breg != 0)
5655 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5656 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5657 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5658 ? "addu" : "daddu"),
5659 "d,v,t", AT, breg, AT);
beae10d5 5660 /* Itbl support may require additional care here. */
252b5132
RH
5661 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5662 coproc ? treg + 1 : treg,
5663 (int) BFD_RELOC_LO16, AT);
5664 expr1.X_add_number += 4;
5665
5666 /* Set mips_optimize to 2 to avoid inserting an undesired
5667 nop. */
5668 hold_mips_optimize = mips_optimize;
5669 mips_optimize = 2;
beae10d5 5670 /* Itbl support may require additional care here. */
252b5132
RH
5671 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5672 coproc ? treg : treg + 1,
5673 (int) BFD_RELOC_LO16, AT);
5674 mips_optimize = hold_mips_optimize;
5675
5676 (void) frag_var (rs_machine_dependent, 0, 0,
5677 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
5678 offset_expr.X_add_symbol, (offsetT) 0,
5679 (char *) NULL);
5680 }
5681 else if (mips_pic == SVR4_PIC)
5682 {
5683 int gpdel, off;
5684
5685 /* If this is a reference to an external symbol, we want
5686 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5687 addu $at,$at,$gp
5688 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
5689 nop
5690 <op> $treg,0($at)
5691 <op> $treg+1,4($at)
5692 Otherwise we want
5693 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5694 nop
5695 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5696 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5697 If there is a base register we add it to $at before the
5698 lwc1 instructions. If there is a constant we include it
5699 in the lwc1 instructions. */
5700 used_at = 1;
5701 expr1.X_add_number = offset_expr.X_add_number;
5702 offset_expr.X_add_number = 0;
5703 if (expr1.X_add_number < -0x8000
5704 || expr1.X_add_number >= 0x8000 - 4)
5705 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5706 if (reg_needs_delay (GP))
5707 gpdel = 4;
5708 else
5709 gpdel = 0;
5710 if (breg == 0)
5711 off = 0;
5712 else
5713 off = 4;
5714 frag_grow (56);
5715 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5716 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
5717 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5718 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5719 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5720 ? "addu" : "daddu"),
5721 "d,v,t", AT, AT, GP);
5722 macro_build ((char *) NULL, &icnt, &offset_expr,
5723 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5724 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5725 ? "lw" : "ld"),
5726 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
5727 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5728 if (breg != 0)
5729 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5730 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5731 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5732 ? "addu" : "daddu"),
5733 "d,v,t", AT, breg, AT);
beae10d5 5734 /* Itbl support may require additional care here. */
252b5132
RH
5735 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5736 coproc ? treg + 1 : treg,
5737 (int) BFD_RELOC_LO16, AT);
5738 expr1.X_add_number += 4;
5739
5740 /* Set mips_optimize to 2 to avoid inserting an undesired
5741 nop. */
5742 hold_mips_optimize = mips_optimize;
5743 mips_optimize = 2;
beae10d5 5744 /* Itbl support may require additional care here. */
252b5132
RH
5745 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5746 coproc ? treg : treg + 1,
5747 (int) BFD_RELOC_LO16, AT);
5748 mips_optimize = hold_mips_optimize;
5749 expr1.X_add_number -= 4;
5750
5751 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
5752 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
5753 8 + gpdel + off, 1, 0),
5754 offset_expr.X_add_symbol, (offsetT) 0,
5755 (char *) NULL);
5756 if (gpdel > 0)
5757 {
5758 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5759 p += 4;
5760 }
5761 macro_build (p, &icnt, &offset_expr,
5762 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5763 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5764 ? "lw" : "ld"),
5765 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5766 p += 4;
5767 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5768 p += 4;
5769 if (breg != 0)
5770 {
5771 macro_build (p, &icnt, (expressionS *) NULL,
5772 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5773 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5774 ? "addu" : "daddu"),
5775 "d,v,t", AT, breg, AT);
5776 p += 4;
5777 }
beae10d5 5778 /* Itbl support may require additional care here. */
252b5132
RH
5779 macro_build (p, &icnt, &expr1, s, fmt,
5780 coproc ? treg + 1 : treg,
5781 (int) BFD_RELOC_LO16, AT);
5782 p += 4;
5783 expr1.X_add_number += 4;
5784
5785 /* Set mips_optimize to 2 to avoid inserting an undesired
5786 nop. */
5787 hold_mips_optimize = mips_optimize;
5788 mips_optimize = 2;
beae10d5 5789 /* Itbl support may require additional care here. */
252b5132
RH
5790 macro_build (p, &icnt, &expr1, s, fmt,
5791 coproc ? treg : treg + 1,
5792 (int) BFD_RELOC_LO16, AT);
5793 mips_optimize = hold_mips_optimize;
5794 }
5795 else if (mips_pic == EMBEDDED_PIC)
5796 {
5797 /* If there is no base register, we use
5798 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5799 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
5800 If we have a base register, we use
5801 addu $at,$breg,$gp
5802 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
5803 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
5804 */
5805 if (breg == 0)
5806 {
5807 tempreg = GP;
5808 used_at = 0;
5809 }
5810 else
5811 {
5812 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5813 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5814 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
5815 ? "addu" : "daddu"),
5816 "d,v,t", AT, breg, GP);
5817 tempreg = AT;
5818 used_at = 1;
5819 }
5820
beae10d5 5821 /* Itbl support may require additional care here. */
252b5132
RH
5822 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5823 coproc ? treg + 1 : treg,
5824 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5825 offset_expr.X_add_number += 4;
beae10d5 5826 /* Itbl support may require additional care here. */
252b5132
RH
5827 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5828 coproc ? treg : treg + 1,
5829 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5830 }
5831 else
5832 abort ();
5833
5834 if (! used_at)
5835 return;
5836
5837 break;
5838
5839 case M_LD_OB:
5840 s = "lw";
5841 goto sd_ob;
5842 case M_SD_OB:
5843 s = "sw";
5844 sd_ob:
bdaaa2e1 5845 assert (bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 5846 || ! ISA_HAS_64BIT_REGS (mips_opts.isa));
252b5132
RH
5847 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
5848 (int) BFD_RELOC_LO16, breg);
5849 offset_expr.X_add_number += 4;
5850 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
5851 (int) BFD_RELOC_LO16, breg);
5852 return;
5853
5854 /* New code added to support COPZ instructions.
5855 This code builds table entries out of the macros in mip_opcodes.
5856 R4000 uses interlocks to handle coproc delays.
5857 Other chips (like the R3000) require nops to be inserted for delays.
5858
5859 FIXME: Currently, we require that the user handle delays.
5860 In order to fill delay slots for non-interlocked chips,
5861 we must have a way to specify delays based on the coprocessor.
5862 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
5863 What are the side-effects of the cop instruction?
5864 What cache support might we have and what are its effects?
5865 Both coprocessor & memory require delays. how long???
bdaaa2e1 5866 What registers are read/set/modified?
252b5132
RH
5867
5868 If an itbl is provided to interpret cop instructions,
bdaaa2e1 5869 this knowledge can be encoded in the itbl spec. */
252b5132
RH
5870
5871 case M_COP0:
5872 s = "c0";
5873 goto copz;
5874 case M_COP1:
5875 s = "c1";
5876 goto copz;
5877 case M_COP2:
5878 s = "c2";
5879 goto copz;
5880 case M_COP3:
5881 s = "c3";
5882 copz:
5883 /* For now we just do C (same as Cz). The parameter will be
5884 stored in insn_opcode by mips_ip. */
5885 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
5886 ip->insn_opcode);
5887 return;
5888
5889#ifdef LOSING_COMPILER
5890 default:
5891 /* Try and see if this is a new itbl instruction.
5892 This code builds table entries out of the macros in mip_opcodes.
5893 FIXME: For now we just assemble the expression and pass it's
5894 value along as a 32-bit immediate.
bdaaa2e1 5895 We may want to have the assembler assemble this value,
252b5132
RH
5896 so that we gain the assembler's knowledge of delay slots,
5897 symbols, etc.
5898 Would it be more efficient to use mask (id) here? */
bdaaa2e1 5899 if (itbl_have_entries
252b5132 5900 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 5901 {
252b5132
RH
5902 s = ip->insn_mo->name;
5903 s2 = "cop3";
5904 coproc = ITBL_DECODE_PNUM (immed_expr);;
5905 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
5906 return;
beae10d5 5907 }
252b5132
RH
5908 macro2 (ip);
5909 return;
5910 }
5911 if (mips_opts.noat)
5912 as_warn (_("Macro used $at after \".set noat\""));
5913}
bdaaa2e1 5914
252b5132
RH
5915static void
5916macro2 (ip)
5917 struct mips_cl_insn *ip;
5918{
5919 register int treg, sreg, dreg, breg;
5920 int tempreg;
5921 int mask;
5922 int icnt = 0;
5923 int used_at;
5924 expressionS expr1;
5925 const char *s;
5926 const char *s2;
5927 const char *fmt;
5928 int likely = 0;
5929 int dbl = 0;
5930 int coproc = 0;
5931 int lr = 0;
5932 int imm = 0;
5933 int off;
5934 offsetT maxnum;
5935 bfd_reloc_code_real_type r;
5936 char *p;
bdaaa2e1 5937
252b5132
RH
5938 treg = (ip->insn_opcode >> 16) & 0x1f;
5939 dreg = (ip->insn_opcode >> 11) & 0x1f;
5940 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
5941 mask = ip->insn_mo->mask;
bdaaa2e1 5942
252b5132
RH
5943 expr1.X_op = O_constant;
5944 expr1.X_op_symbol = NULL;
5945 expr1.X_add_symbol = NULL;
5946 expr1.X_add_number = 1;
bdaaa2e1 5947
252b5132
RH
5948 switch (mask)
5949 {
5950#endif /* LOSING_COMPILER */
5951
5952 case M_DMUL:
5953 dbl = 1;
5954 case M_MUL:
5955 macro_build ((char *) NULL, &icnt, NULL,
5956 dbl ? "dmultu" : "multu",
5957 "s,t", sreg, treg);
5958 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5959 return;
5960
5961 case M_DMUL_I:
5962 dbl = 1;
5963 case M_MUL_I:
5964 /* The MIPS assembler some times generates shifts and adds. I'm
5965 not trying to be that fancy. GCC should do this for us
5966 anyway. */
5967 load_register (&icnt, AT, &imm_expr, dbl);
5968 macro_build ((char *) NULL, &icnt, NULL,
5969 dbl ? "dmult" : "mult",
5970 "s,t", sreg, AT);
5971 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5972 break;
5973
5974 case M_DMULO_I:
5975 dbl = 1;
5976 case M_MULO_I:
5977 imm = 1;
5978 goto do_mulo;
5979
5980 case M_DMULO:
5981 dbl = 1;
5982 case M_MULO:
5983 do_mulo:
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 ? "dmult" : "mult",
5991 "s,t", sreg, imm ? AT : treg);
5992 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5993 macro_build ((char *) NULL, &icnt, NULL,
5994 dbl ? "dsra32" : "sra",
5995 "d,w,<", dreg, dreg, 31);
5996 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
5997 if (mips_trap)
5998 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", dreg, AT);
5999 else
6000 {
6001 expr1.X_add_number = 8;
6002 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
6003 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
beae10d5 6004 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
252b5132
RH
6005 }
6006 --mips_opts.noreorder;
6007 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
6008 break;
6009
6010 case M_DMULOU_I:
6011 dbl = 1;
6012 case M_MULOU_I:
6013 imm = 1;
6014 goto do_mulou;
6015
6016 case M_DMULOU:
6017 dbl = 1;
6018 case M_MULOU:
6019 do_mulou:
6020 mips_emit_delays (true);
6021 ++mips_opts.noreorder;
6022 mips_any_noreorder = 1;
6023 if (imm)
6024 load_register (&icnt, AT, &imm_expr, dbl);
6025 macro_build ((char *) NULL, &icnt, NULL,
6026 dbl ? "dmultu" : "multu",
6027 "s,t", sreg, imm ? AT : treg);
6028 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
6029 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
6030 if (mips_trap)
6031 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", AT, 0);
6032 else
6033 {
6034 expr1.X_add_number = 8;
6035 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6036 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
beae10d5 6037 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
252b5132
RH
6038 }
6039 --mips_opts.noreorder;
6040 break;
6041
6042 case M_ROL:
6043 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
6044 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
6045 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg,
6046 treg);
6047 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6048 break;
6049
6050 case M_ROL_I:
6051 if (imm_expr.X_op != O_constant)
6052 as_bad (_("rotate count too large"));
6053 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg,
6054 (int) (imm_expr.X_add_number & 0x1f));
6055 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
6056 (int) ((0 - imm_expr.X_add_number) & 0x1f));
6057 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6058 break;
6059
6060 case M_ROR:
6061 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
6062 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
6063 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg,
6064 treg);
6065 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6066 break;
6067
6068 case M_ROR_I:
6069 if (imm_expr.X_op != O_constant)
6070 as_bad (_("rotate count too large"));
6071 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg,
6072 (int) (imm_expr.X_add_number & 0x1f));
6073 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
6074 (int) ((0 - imm_expr.X_add_number) & 0x1f));
6075 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6076 break;
6077
6078 case M_S_DOB:
156c2f8b 6079 if (mips_cpu == CPU_R4650)
252b5132
RH
6080 {
6081 as_bad (_("opcode not supported on this processor"));
6082 return;
6083 }
e7af610e 6084 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6085 /* Even on a big endian machine $fn comes before $fn+1. We have
6086 to adjust when storing to memory. */
6087 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6088 target_big_endian ? treg + 1 : treg,
6089 (int) BFD_RELOC_LO16, breg);
6090 offset_expr.X_add_number += 4;
6091 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6092 target_big_endian ? treg : treg + 1,
6093 (int) BFD_RELOC_LO16, breg);
6094 return;
6095
6096 case M_SEQ:
6097 if (sreg == 0)
6098 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6099 treg, (int) BFD_RELOC_LO16);
6100 else if (treg == 0)
6101 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6102 sreg, (int) BFD_RELOC_LO16);
6103 else
6104 {
6105 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6106 sreg, treg);
6107 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6108 dreg, (int) BFD_RELOC_LO16);
6109 }
6110 return;
6111
6112 case M_SEQ_I:
6113 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6114 {
6115 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6116 sreg, (int) BFD_RELOC_LO16);
6117 return;
6118 }
6119 if (sreg == 0)
6120 {
6121 as_warn (_("Instruction %s: result is always false"),
6122 ip->insn_mo->name);
6123 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
6124 return;
6125 }
6126 if (imm_expr.X_op == O_constant
6127 && imm_expr.X_add_number >= 0
6128 && imm_expr.X_add_number < 0x10000)
6129 {
6130 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6131 sreg, (int) BFD_RELOC_LO16);
6132 used_at = 0;
6133 }
6134 else if (imm_expr.X_op == O_constant
6135 && imm_expr.X_add_number > -0x8000
6136 && imm_expr.X_add_number < 0)
6137 {
6138 imm_expr.X_add_number = -imm_expr.X_add_number;
6139 macro_build ((char *) NULL, &icnt, &imm_expr,
6140 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 6141 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
6142 ? "addiu" : "daddiu"),
6143 "t,r,j", dreg, sreg,
6144 (int) BFD_RELOC_LO16);
6145 used_at = 0;
6146 }
6147 else
6148 {
6149 load_register (&icnt, AT, &imm_expr, 0);
6150 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6151 sreg, AT);
6152 used_at = 1;
6153 }
6154 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6155 (int) BFD_RELOC_LO16);
6156 if (used_at)
6157 break;
6158 return;
6159
6160 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6161 s = "slt";
6162 goto sge;
6163 case M_SGEU:
6164 s = "sltu";
6165 sge:
6166 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
6167 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6168 (int) BFD_RELOC_LO16);
6169 return;
6170
6171 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6172 case M_SGEU_I:
6173 if (imm_expr.X_op == O_constant
6174 && imm_expr.X_add_number >= -0x8000
6175 && imm_expr.X_add_number < 0x8000)
6176 {
6177 macro_build ((char *) NULL, &icnt, &imm_expr,
6178 mask == M_SGE_I ? "slti" : "sltiu",
6179 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6180 used_at = 0;
6181 }
6182 else
6183 {
6184 load_register (&icnt, AT, &imm_expr, 0);
6185 macro_build ((char *) NULL, &icnt, NULL,
6186 mask == M_SGE_I ? "slt" : "sltu",
6187 "d,v,t", dreg, sreg, AT);
6188 used_at = 1;
6189 }
6190 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6191 (int) BFD_RELOC_LO16);
6192 if (used_at)
6193 break;
6194 return;
6195
6196 case M_SGT: /* sreg > treg <==> treg < sreg */
6197 s = "slt";
6198 goto sgt;
6199 case M_SGTU:
6200 s = "sltu";
6201 sgt:
6202 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
6203 return;
6204
6205 case M_SGT_I: /* sreg > I <==> I < sreg */
6206 s = "slt";
6207 goto sgti;
6208 case M_SGTU_I:
6209 s = "sltu";
6210 sgti:
6211 load_register (&icnt, AT, &imm_expr, 0);
6212 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
6213 break;
6214
6215 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
6216 s = "slt";
6217 goto sle;
6218 case M_SLEU:
6219 s = "sltu";
6220 sle:
6221 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
6222 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6223 (int) BFD_RELOC_LO16);
6224 return;
6225
6226 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6227 s = "slt";
6228 goto slei;
6229 case M_SLEU_I:
6230 s = "sltu";
6231 slei:
6232 load_register (&icnt, AT, &imm_expr, 0);
6233 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
6234 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6235 (int) BFD_RELOC_LO16);
6236 break;
6237
6238 case M_SLT_I:
6239 if (imm_expr.X_op == O_constant
6240 && imm_expr.X_add_number >= -0x8000
6241 && imm_expr.X_add_number < 0x8000)
6242 {
6243 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6244 dreg, sreg, (int) BFD_RELOC_LO16);
6245 return;
6246 }
6247 load_register (&icnt, AT, &imm_expr, 0);
6248 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
6249 break;
6250
6251 case M_SLTU_I:
6252 if (imm_expr.X_op == O_constant
6253 && imm_expr.X_add_number >= -0x8000
6254 && imm_expr.X_add_number < 0x8000)
6255 {
6256 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6257 dreg, sreg, (int) BFD_RELOC_LO16);
6258 return;
6259 }
6260 load_register (&icnt, AT, &imm_expr, 0);
6261 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg,
6262 AT);
6263 break;
6264
6265 case M_SNE:
6266 if (sreg == 0)
6267 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6268 treg);
6269 else if (treg == 0)
6270 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6271 sreg);
6272 else
6273 {
6274 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6275 sreg, treg);
6276 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6277 dreg);
6278 }
6279 return;
6280
6281 case M_SNE_I:
6282 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6283 {
6284 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6285 sreg);
6286 return;
6287 }
6288 if (sreg == 0)
6289 {
6290 as_warn (_("Instruction %s: result is always true"),
6291 ip->insn_mo->name);
6292 macro_build ((char *) NULL, &icnt, &expr1,
6293 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 6294 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
6295 ? "addiu" : "daddiu"),
6296 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6297 return;
6298 }
6299 if (imm_expr.X_op == O_constant
6300 && imm_expr.X_add_number >= 0
6301 && imm_expr.X_add_number < 0x10000)
6302 {
6303 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6304 dreg, sreg, (int) BFD_RELOC_LO16);
6305 used_at = 0;
6306 }
6307 else if (imm_expr.X_op == O_constant
6308 && imm_expr.X_add_number > -0x8000
6309 && imm_expr.X_add_number < 0)
6310 {
6311 imm_expr.X_add_number = -imm_expr.X_add_number;
6312 macro_build ((char *) NULL, &icnt, &imm_expr,
6313 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 6314 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
6315 ? "addiu" : "daddiu"),
6316 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6317 used_at = 0;
6318 }
6319 else
6320 {
6321 load_register (&icnt, AT, &imm_expr, 0);
6322 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6323 sreg, AT);
6324 used_at = 1;
6325 }
6326 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
6327 if (used_at)
6328 break;
6329 return;
6330
6331 case M_DSUB_I:
6332 dbl = 1;
6333 case M_SUB_I:
6334 if (imm_expr.X_op == O_constant
6335 && imm_expr.X_add_number > -0x8000
6336 && imm_expr.X_add_number <= 0x8000)
6337 {
6338 imm_expr.X_add_number = -imm_expr.X_add_number;
6339 macro_build ((char *) NULL, &icnt, &imm_expr,
6340 dbl ? "daddi" : "addi",
6341 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6342 return;
6343 }
6344 load_register (&icnt, AT, &imm_expr, dbl);
6345 macro_build ((char *) NULL, &icnt, NULL,
6346 dbl ? "dsub" : "sub",
6347 "d,v,t", dreg, sreg, AT);
6348 break;
6349
6350 case M_DSUBU_I:
6351 dbl = 1;
6352 case M_SUBU_I:
6353 if (imm_expr.X_op == O_constant
6354 && imm_expr.X_add_number > -0x8000
6355 && imm_expr.X_add_number <= 0x8000)
6356 {
6357 imm_expr.X_add_number = -imm_expr.X_add_number;
6358 macro_build ((char *) NULL, &icnt, &imm_expr,
6359 dbl ? "daddiu" : "addiu",
6360 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6361 return;
6362 }
6363 load_register (&icnt, AT, &imm_expr, dbl);
6364 macro_build ((char *) NULL, &icnt, NULL,
6365 dbl ? "dsubu" : "subu",
6366 "d,v,t", dreg, sreg, AT);
6367 break;
6368
6369 case M_TEQ_I:
6370 s = "teq";
6371 goto trap;
6372 case M_TGE_I:
6373 s = "tge";
6374 goto trap;
6375 case M_TGEU_I:
6376 s = "tgeu";
6377 goto trap;
6378 case M_TLT_I:
6379 s = "tlt";
6380 goto trap;
6381 case M_TLTU_I:
6382 s = "tltu";
6383 goto trap;
6384 case M_TNE_I:
6385 s = "tne";
6386 trap:
6387 load_register (&icnt, AT, &imm_expr, 0);
6388 macro_build ((char *) NULL, &icnt, NULL, s, "s,t", sreg, AT);
6389 break;
6390
252b5132 6391 case M_TRUNCWS:
43841e91 6392 case M_TRUNCWD:
e7af610e 6393 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6394 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
6395 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
6396
6397 /*
6398 * Is the double cfc1 instruction a bug in the mips assembler;
6399 * or is there a reason for it?
6400 */
6401 mips_emit_delays (true);
6402 ++mips_opts.noreorder;
6403 mips_any_noreorder = 1;
6404 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
6405 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
6406 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6407 expr1.X_add_number = 3;
6408 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
6409 (int) BFD_RELOC_LO16);
6410 expr1.X_add_number = 2;
6411 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
6412 (int) BFD_RELOC_LO16);
6413 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", AT, 31);
6414 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6415 macro_build ((char *) NULL, &icnt, NULL,
6416 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
6417 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", treg, 31);
6418 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6419 --mips_opts.noreorder;
6420 break;
6421
6422 case M_ULH:
6423 s = "lb";
6424 goto ulh;
6425 case M_ULHU:
6426 s = "lbu";
6427 ulh:
6428 if (offset_expr.X_add_number >= 0x7fff)
6429 as_bad (_("operand overflow"));
6430 /* avoid load delay */
6431 if (! target_big_endian)
6432 offset_expr.X_add_number += 1;
6433 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6434 (int) BFD_RELOC_LO16, breg);
6435 if (! target_big_endian)
6436 offset_expr.X_add_number -= 1;
6437 else
6438 offset_expr.X_add_number += 1;
6439 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
6440 (int) BFD_RELOC_LO16, breg);
6441 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
6442 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
6443 break;
6444
6445 case M_ULD:
6446 s = "ldl";
6447 s2 = "ldr";
6448 off = 7;
6449 goto ulw;
6450 case M_ULW:
6451 s = "lwl";
6452 s2 = "lwr";
6453 off = 3;
6454 ulw:
6455 if (offset_expr.X_add_number >= 0x8000 - off)
6456 as_bad (_("operand overflow"));
6457 if (! target_big_endian)
6458 offset_expr.X_add_number += off;
6459 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6460 (int) BFD_RELOC_LO16, breg);
6461 if (! target_big_endian)
6462 offset_expr.X_add_number -= off;
6463 else
6464 offset_expr.X_add_number += off;
6465 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
6466 (int) BFD_RELOC_LO16, breg);
6467 return;
6468
6469 case M_ULD_A:
6470 s = "ldl";
6471 s2 = "ldr";
6472 off = 7;
6473 goto ulwa;
6474 case M_ULW_A:
6475 s = "lwl";
6476 s2 = "lwr";
6477 off = 3;
6478 ulwa:
6479 load_address (&icnt, AT, &offset_expr);
6480 if (breg != 0)
6481 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6482 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 6483 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
6484 ? "addu" : "daddu"),
6485 "d,v,t", AT, AT, breg);
6486 if (! target_big_endian)
6487 expr1.X_add_number = off;
6488 else
6489 expr1.X_add_number = 0;
6490 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
6491 (int) BFD_RELOC_LO16, AT);
6492 if (! target_big_endian)
6493 expr1.X_add_number = 0;
6494 else
6495 expr1.X_add_number = off;
6496 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
6497 (int) BFD_RELOC_LO16, AT);
6498 break;
6499
6500 case M_ULH_A:
6501 case M_ULHU_A:
6502 load_address (&icnt, AT, &offset_expr);
6503 if (breg != 0)
6504 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6505 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 6506 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
6507 ? "addu" : "daddu"),
6508 "d,v,t", AT, AT, breg);
6509 if (target_big_endian)
6510 expr1.X_add_number = 0;
6511 macro_build ((char *) NULL, &icnt, &expr1,
6512 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
6513 (int) BFD_RELOC_LO16, AT);
6514 if (target_big_endian)
6515 expr1.X_add_number = 1;
6516 else
6517 expr1.X_add_number = 0;
6518 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
6519 (int) BFD_RELOC_LO16, AT);
6520 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
6521 treg, 8);
6522 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
6523 treg, AT);
6524 break;
6525
6526 case M_USH:
6527 if (offset_expr.X_add_number >= 0x7fff)
6528 as_bad (_("operand overflow"));
6529 if (target_big_endian)
6530 offset_expr.X_add_number += 1;
6531 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
6532 (int) BFD_RELOC_LO16, breg);
6533 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
6534 if (target_big_endian)
6535 offset_expr.X_add_number -= 1;
6536 else
6537 offset_expr.X_add_number += 1;
6538 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
6539 (int) BFD_RELOC_LO16, breg);
6540 break;
6541
6542 case M_USD:
6543 s = "sdl";
6544 s2 = "sdr";
6545 off = 7;
6546 goto usw;
6547 case M_USW:
6548 s = "swl";
6549 s2 = "swr";
6550 off = 3;
6551 usw:
6552 if (offset_expr.X_add_number >= 0x8000 - off)
6553 as_bad (_("operand overflow"));
6554 if (! target_big_endian)
6555 offset_expr.X_add_number += off;
6556 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6557 (int) BFD_RELOC_LO16, breg);
6558 if (! target_big_endian)
6559 offset_expr.X_add_number -= off;
6560 else
6561 offset_expr.X_add_number += off;
6562 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
6563 (int) BFD_RELOC_LO16, breg);
6564 return;
6565
6566 case M_USD_A:
6567 s = "sdl";
6568 s2 = "sdr";
6569 off = 7;
6570 goto uswa;
6571 case M_USW_A:
6572 s = "swl";
6573 s2 = "swr";
6574 off = 3;
6575 uswa:
6576 load_address (&icnt, AT, &offset_expr);
6577 if (breg != 0)
6578 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6579 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 6580 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
6581 ? "addu" : "daddu"),
6582 "d,v,t", AT, AT, breg);
6583 if (! target_big_endian)
6584 expr1.X_add_number = off;
6585 else
6586 expr1.X_add_number = 0;
6587 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
6588 (int) BFD_RELOC_LO16, AT);
6589 if (! target_big_endian)
6590 expr1.X_add_number = 0;
6591 else
6592 expr1.X_add_number = off;
6593 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
6594 (int) BFD_RELOC_LO16, AT);
6595 break;
6596
6597 case M_USH_A:
6598 load_address (&icnt, AT, &offset_expr);
6599 if (breg != 0)
6600 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6601 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 6602 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
6603 ? "addu" : "daddu"),
6604 "d,v,t", AT, AT, breg);
6605 if (! target_big_endian)
6606 expr1.X_add_number = 0;
6607 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
6608 (int) BFD_RELOC_LO16, AT);
6609 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", treg,
6610 treg, 8);
6611 if (! target_big_endian)
6612 expr1.X_add_number = 1;
6613 else
6614 expr1.X_add_number = 0;
6615 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
6616 (int) BFD_RELOC_LO16, AT);
6617 if (! target_big_endian)
6618 expr1.X_add_number = 0;
6619 else
6620 expr1.X_add_number = 1;
6621 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
6622 (int) BFD_RELOC_LO16, AT);
6623 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
6624 treg, 8);
6625 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
6626 treg, AT);
6627 break;
6628
6629 default:
6630 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 6631 are added dynamically. */
252b5132
RH
6632 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
6633 break;
6634 }
6635 if (mips_opts.noat)
6636 as_warn (_("Macro used $at after \".set noat\""));
6637}
6638
6639/* Implement macros in mips16 mode. */
6640
6641static void
6642mips16_macro (ip)
6643 struct mips_cl_insn *ip;
6644{
6645 int mask;
6646 int xreg, yreg, zreg, tmp;
6647 int icnt;
6648 expressionS expr1;
6649 int dbl;
6650 const char *s, *s2, *s3;
6651
6652 mask = ip->insn_mo->mask;
6653
6654 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
6655 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
6656 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
6657
6658 icnt = 0;
6659
6660 expr1.X_op = O_constant;
6661 expr1.X_op_symbol = NULL;
6662 expr1.X_add_symbol = NULL;
6663 expr1.X_add_number = 1;
6664
6665 dbl = 0;
6666
6667 switch (mask)
6668 {
6669 default:
6670 internalError ();
6671
6672 case M_DDIV_3:
6673 dbl = 1;
6674 case M_DIV_3:
6675 s = "mflo";
6676 goto do_div3;
6677 case M_DREM_3:
6678 dbl = 1;
6679 case M_REM_3:
6680 s = "mfhi";
6681 do_div3:
6682 mips_emit_delays (true);
6683 ++mips_opts.noreorder;
6684 mips_any_noreorder = 1;
6685 macro_build ((char *) NULL, &icnt, NULL,
6686 dbl ? "ddiv" : "div",
6687 "0,x,y", xreg, yreg);
6688 expr1.X_add_number = 2;
6689 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
beae10d5 6690 macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
bdaaa2e1 6691
252b5132
RH
6692 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
6693 since that causes an overflow. We should do that as well,
6694 but I don't see how to do the comparisons without a temporary
6695 register. */
6696 --mips_opts.noreorder;
6697 macro_build ((char *) NULL, &icnt, NULL, s, "x", zreg);
6698 break;
6699
6700 case M_DIVU_3:
6701 s = "divu";
6702 s2 = "mflo";
6703 goto do_divu3;
6704 case M_REMU_3:
6705 s = "divu";
6706 s2 = "mfhi";
6707 goto do_divu3;
6708 case M_DDIVU_3:
6709 s = "ddivu";
6710 s2 = "mflo";
6711 goto do_divu3;
6712 case M_DREMU_3:
6713 s = "ddivu";
6714 s2 = "mfhi";
6715 do_divu3:
6716 mips_emit_delays (true);
6717 ++mips_opts.noreorder;
6718 mips_any_noreorder = 1;
6719 macro_build ((char *) NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
6720 expr1.X_add_number = 2;
6721 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
6722 macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
6723 --mips_opts.noreorder;
6724 macro_build ((char *) NULL, &icnt, NULL, s2, "x", zreg);
6725 break;
6726
6727 case M_DMUL:
6728 dbl = 1;
6729 case M_MUL:
6730 macro_build ((char *) NULL, &icnt, NULL,
6731 dbl ? "dmultu" : "multu",
6732 "x,y", xreg, yreg);
6733 macro_build ((char *) NULL, &icnt, NULL, "mflo", "x", zreg);
6734 return;
6735
6736 case M_DSUBU_I:
6737 dbl = 1;
6738 goto do_subu;
6739 case M_SUBU_I:
6740 do_subu:
6741 if (imm_expr.X_op != O_constant)
6742 as_bad (_("Unsupported large constant"));
6743 imm_expr.X_add_number = -imm_expr.X_add_number;
6744 macro_build ((char *) NULL, &icnt, &imm_expr,
6745 dbl ? "daddiu" : "addiu",
6746 "y,x,4", yreg, xreg);
6747 break;
6748
6749 case M_SUBU_I_2:
6750 if (imm_expr.X_op != O_constant)
6751 as_bad (_("Unsupported large constant"));
6752 imm_expr.X_add_number = -imm_expr.X_add_number;
6753 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
6754 "x,k", xreg);
6755 break;
6756
6757 case M_DSUBU_I_2:
6758 if (imm_expr.X_op != O_constant)
6759 as_bad (_("Unsupported large constant"));
6760 imm_expr.X_add_number = -imm_expr.X_add_number;
6761 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
6762 "y,j", yreg);
6763 break;
6764
6765 case M_BEQ:
6766 s = "cmp";
6767 s2 = "bteqz";
6768 goto do_branch;
6769 case M_BNE:
6770 s = "cmp";
6771 s2 = "btnez";
6772 goto do_branch;
6773 case M_BLT:
6774 s = "slt";
6775 s2 = "btnez";
6776 goto do_branch;
6777 case M_BLTU:
6778 s = "sltu";
6779 s2 = "btnez";
6780 goto do_branch;
6781 case M_BLE:
6782 s = "slt";
6783 s2 = "bteqz";
6784 goto do_reverse_branch;
6785 case M_BLEU:
6786 s = "sltu";
6787 s2 = "bteqz";
6788 goto do_reverse_branch;
6789 case M_BGE:
6790 s = "slt";
6791 s2 = "bteqz";
6792 goto do_branch;
6793 case M_BGEU:
6794 s = "sltu";
6795 s2 = "bteqz";
6796 goto do_branch;
6797 case M_BGT:
6798 s = "slt";
6799 s2 = "btnez";
6800 goto do_reverse_branch;
6801 case M_BGTU:
6802 s = "sltu";
6803 s2 = "btnez";
6804
6805 do_reverse_branch:
6806 tmp = xreg;
6807 xreg = yreg;
6808 yreg = tmp;
6809
6810 do_branch:
6811 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
6812 xreg, yreg);
6813 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
6814 break;
6815
6816 case M_BEQ_I:
6817 s = "cmpi";
6818 s2 = "bteqz";
6819 s3 = "x,U";
6820 goto do_branch_i;
6821 case M_BNE_I:
6822 s = "cmpi";
6823 s2 = "btnez";
6824 s3 = "x,U";
6825 goto do_branch_i;
6826 case M_BLT_I:
6827 s = "slti";
6828 s2 = "btnez";
6829 s3 = "x,8";
6830 goto do_branch_i;
6831 case M_BLTU_I:
6832 s = "sltiu";
6833 s2 = "btnez";
6834 s3 = "x,8";
6835 goto do_branch_i;
6836 case M_BLE_I:
6837 s = "slti";
6838 s2 = "btnez";
6839 s3 = "x,8";
6840 goto do_addone_branch_i;
6841 case M_BLEU_I:
6842 s = "sltiu";
6843 s2 = "btnez";
6844 s3 = "x,8";
6845 goto do_addone_branch_i;
6846 case M_BGE_I:
6847 s = "slti";
6848 s2 = "bteqz";
6849 s3 = "x,8";
6850 goto do_branch_i;
6851 case M_BGEU_I:
6852 s = "sltiu";
6853 s2 = "bteqz";
6854 s3 = "x,8";
6855 goto do_branch_i;
6856 case M_BGT_I:
6857 s = "slti";
6858 s2 = "bteqz";
6859 s3 = "x,8";
6860 goto do_addone_branch_i;
6861 case M_BGTU_I:
6862 s = "sltiu";
6863 s2 = "bteqz";
6864 s3 = "x,8";
6865
6866 do_addone_branch_i:
6867 if (imm_expr.X_op != O_constant)
6868 as_bad (_("Unsupported large constant"));
6869 ++imm_expr.X_add_number;
6870
6871 do_branch_i:
6872 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
6873 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
6874 break;
6875
6876 case M_ABS:
6877 expr1.X_add_number = 0;
6878 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
6879 if (xreg != yreg)
6880 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6881 "move", "y,X", xreg, yreg);
6882 expr1.X_add_number = 2;
6883 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
6884 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6885 "neg", "x,w", xreg, xreg);
6886 }
6887}
6888
6889/* For consistency checking, verify that all bits are specified either
6890 by the match/mask part of the instruction definition, or by the
6891 operand list. */
6892static int
6893validate_mips_insn (opc)
6894 const struct mips_opcode *opc;
6895{
6896 const char *p = opc->args;
6897 char c;
6898 unsigned long used_bits = opc->mask;
6899
6900 if ((used_bits & opc->match) != opc->match)
6901 {
6902 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
6903 opc->name, opc->args);
6904 return 0;
6905 }
6906#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
6907 while (*p)
6908 switch (c = *p++)
6909 {
6910 case ',': break;
6911 case '(': break;
6912 case ')': break;
6913 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
6914 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
6915 case 'A': break;
4372b673 6916 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
6917 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
6918 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
6919 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
6920 case 'F': break;
6921 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 6922 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 6923 case 'I': break;
e972090a 6924 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
252b5132
RH
6925 case 'L': break;
6926 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
6927 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
6928 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
6929 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
6930 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
6931 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
6932 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
6933 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
6934 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
6935 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
6936 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
6937 case 'f': break;
6938 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
6939 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
6940 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
6941 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
6942 case 'l': break;
6943 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
6944 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
6945 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
6946 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
6947 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
6948 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
6949 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
6950 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
6951 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
6952 case 'x': break;
6953 case 'z': break;
6954 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
6955 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
6956 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
252b5132
RH
6957 default:
6958 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
6959 c, opc->name, opc->args);
6960 return 0;
6961 }
6962#undef USE_BITS
6963 if (used_bits != 0xffffffff)
6964 {
6965 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
6966 ~used_bits & 0xffffffff, opc->name, opc->args);
6967 return 0;
6968 }
6969 return 1;
6970}
6971
6972/* This routine assembles an instruction into its binary format. As a
6973 side effect, it sets one of the global variables imm_reloc or
6974 offset_reloc to the type of relocation to do if one of the operands
6975 is an address expression. */
6976
6977static void
6978mips_ip (str, ip)
6979 char *str;
6980 struct mips_cl_insn *ip;
6981{
6982 char *s;
6983 const char *args;
43841e91 6984 char c = 0;
252b5132
RH
6985 struct mips_opcode *insn;
6986 char *argsStart;
6987 unsigned int regno;
6988 unsigned int lastregno = 0;
6989 char *s_reset;
6990 char save_c = 0;
6991 int full_opcode_match = 1;
6992
6993 insn_error = NULL;
6994
6995 /* If the instruction contains a '.', we first try to match an instruction
6996 including the '.'. Then we try again without the '.'. */
6997 insn = NULL;
d9a62219 6998 for (s = str; *s != '\0' && !isspace ((unsigned char) *s); ++s)
252b5132
RH
6999 continue;
7000
7001 /* If we stopped on whitespace, then replace the whitespace with null for
7002 the call to hash_find. Save the character we replaced just in case we
7003 have to re-parse the instruction. */
d9a62219 7004 if (isspace ((unsigned char) *s))
252b5132
RH
7005 {
7006 save_c = *s;
7007 *s++ = '\0';
7008 }
bdaaa2e1 7009
252b5132
RH
7010 insn = (struct mips_opcode *) hash_find (op_hash, str);
7011
7012 /* If we didn't find the instruction in the opcode table, try again, but
7013 this time with just the instruction up to, but not including the
7014 first '.'. */
7015 if (insn == NULL)
7016 {
bdaaa2e1 7017 /* Restore the character we overwrite above (if any). */
252b5132
RH
7018 if (save_c)
7019 *(--s) = save_c;
7020
7021 /* Scan up to the first '.' or whitespace. */
d9a62219 7022 for (s = str; *s != '\0' && *s != '.' && !isspace ((unsigned char) *s); ++s)
252b5132
RH
7023 continue;
7024
7025 /* If we did not find a '.', then we can quit now. */
7026 if (*s != '.')
7027 {
7028 insn_error = "unrecognized opcode";
7029 return;
7030 }
7031
7032 /* Lookup the instruction in the hash table. */
7033 *s++ = '\0';
7034 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7035 {
7036 insn_error = "unrecognized opcode";
7037 return;
7038 }
7039
7040 full_opcode_match = 0;
7041 }
7042
7043 argsStart = s;
7044 for (;;)
7045 {
252b5132
RH
7046 boolean ok;
7047
7048 assert (strcmp (insn->name, str) == 0);
7049
c97ef257 7050 if (OPCODE_IS_MEMBER (insn, mips_opts.isa, mips_cpu, mips_gp32))
252b5132 7051 ok = true;
bdaaa2e1 7052 else
252b5132 7053 ok = false;
bdaaa2e1 7054
252b5132
RH
7055 if (insn->pinfo != INSN_MACRO)
7056 {
156c2f8b 7057 if (mips_cpu == CPU_R4650 && (insn->pinfo & FP_D) != 0)
252b5132
RH
7058 ok = false;
7059 }
7060
7061 if (! ok)
7062 {
7063 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7064 && strcmp (insn->name, insn[1].name) == 0)
7065 {
7066 ++insn;
7067 continue;
7068 }
252b5132 7069 else
beae10d5 7070 {
252b5132 7071 static char buf[100];
bdaaa2e1 7072 sprintf (buf,
e7af610e
NC
7073 _("opcode not supported on this processor: %s (%s)"),
7074 mips_cpu_to_str (mips_cpu),
7075 mips_isa_to_str (mips_opts.isa));
bdaaa2e1 7076
252b5132 7077 insn_error = buf;
2bd7f1f3 7078 return;
252b5132 7079 }
252b5132
RH
7080 }
7081
7082 ip->insn_mo = insn;
7083 ip->insn_opcode = insn->match;
7084 for (args = insn->args;; ++args)
7085 {
7086 if (*s == ' ')
7087 ++s;
7088 switch (*args)
7089 {
7090 case '\0': /* end of args */
7091 if (*s == '\0')
7092 return;
7093 break;
7094
7095 case ',':
7096 if (*s++ == *args)
7097 continue;
7098 s--;
7099 switch (*++args)
7100 {
7101 case 'r':
7102 case 'v':
7103 ip->insn_opcode |= lastregno << 21;
7104 continue;
7105
7106 case 'w':
7107 case 'W':
7108 ip->insn_opcode |= lastregno << 16;
7109 continue;
7110
7111 case 'V':
7112 ip->insn_opcode |= lastregno << 11;
7113 continue;
7114 }
7115 break;
7116
7117 case '(':
7118 /* Handle optional base register.
7119 Either the base register is omitted or
bdaaa2e1 7120 we must have a left paren. */
252b5132
RH
7121 /* This is dependent on the next operand specifier
7122 is a base register specification. */
7123 assert (args[1] == 'b' || args[1] == '5'
7124 || args[1] == '-' || args[1] == '4');
7125 if (*s == '\0')
7126 return;
7127
7128 case ')': /* these must match exactly */
7129 if (*s++ == *args)
7130 continue;
7131 break;
7132
7133 case '<': /* must be at least one digit */
7134 /*
7135 * According to the manual, if the shift amount is greater
7136 * than 31 or less than 0 the the shift amount should be
7137 * mod 32. In reality the mips assembler issues an error.
7138 * We issue a warning and mask out all but the low 5 bits.
7139 */
7140 my_getExpression (&imm_expr, s);
7141 check_absolute_expr (ip, &imm_expr);
7142 if ((unsigned long) imm_expr.X_add_number > 31)
7143 {
7144 as_warn (_("Improper shift amount (%ld)"),
7145 (long) imm_expr.X_add_number);
7146 imm_expr.X_add_number = imm_expr.X_add_number & 0x1f;
7147 }
7148 ip->insn_opcode |= imm_expr.X_add_number << 6;
7149 imm_expr.X_op = O_absent;
7150 s = expr_end;
7151 continue;
7152
7153 case '>': /* shift amount minus 32 */
7154 my_getExpression (&imm_expr, s);
7155 check_absolute_expr (ip, &imm_expr);
7156 if ((unsigned long) imm_expr.X_add_number < 32
7157 || (unsigned long) imm_expr.X_add_number > 63)
7158 break;
7159 ip->insn_opcode |= (imm_expr.X_add_number - 32) << 6;
7160 imm_expr.X_op = O_absent;
7161 s = expr_end;
7162 continue;
7163
252b5132
RH
7164 case 'k': /* cache code */
7165 case 'h': /* prefx code */
7166 my_getExpression (&imm_expr, s);
7167 check_absolute_expr (ip, &imm_expr);
7168 if ((unsigned long) imm_expr.X_add_number > 31)
7169 {
7170 as_warn (_("Invalid value for `%s' (%lu)"),
7171 ip->insn_mo->name,
7172 (unsigned long) imm_expr.X_add_number);
7173 imm_expr.X_add_number &= 0x1f;
7174 }
7175 if (*args == 'k')
7176 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7177 else
7178 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7179 imm_expr.X_op = O_absent;
7180 s = expr_end;
7181 continue;
7182
7183 case 'c': /* break code */
7184 my_getExpression (&imm_expr, s);
7185 check_absolute_expr (ip, &imm_expr);
7186 if ((unsigned) imm_expr.X_add_number > 1023)
7187 {
7188 as_warn (_("Illegal break code (%ld)"),
7189 (long) imm_expr.X_add_number);
7190 imm_expr.X_add_number &= 0x3ff;
7191 }
7192 ip->insn_opcode |= imm_expr.X_add_number << 16;
7193 imm_expr.X_op = O_absent;
7194 s = expr_end;
7195 continue;
7196
7197 case 'q': /* lower break code */
7198 my_getExpression (&imm_expr, s);
7199 check_absolute_expr (ip, &imm_expr);
7200 if ((unsigned) imm_expr.X_add_number > 1023)
7201 {
7202 as_warn (_("Illegal lower break code (%ld)"),
7203 (long) imm_expr.X_add_number);
7204 imm_expr.X_add_number &= 0x3ff;
7205 }
7206 ip->insn_opcode |= imm_expr.X_add_number << 6;
7207 imm_expr.X_op = O_absent;
7208 s = expr_end;
7209 continue;
7210
4372b673 7211 case 'B': /* 20-bit syscall/break code. */
156c2f8b 7212 my_getExpression (&imm_expr, s);
156c2f8b 7213 check_absolute_expr (ip, &imm_expr);
156c2f8b 7214 if ((unsigned) imm_expr.X_add_number > 0xfffff)
4372b673 7215 as_warn (_("Illegal 20-bit code (%ld)"),
252b5132
RH
7216 (long) imm_expr.X_add_number);
7217 ip->insn_opcode |= imm_expr.X_add_number << 6;
7218 imm_expr.X_op = O_absent;
7219 s = expr_end;
7220 continue;
7221
7222 case 'C': /* Coprocessor code */
beae10d5 7223 my_getExpression (&imm_expr, s);
252b5132 7224 check_absolute_expr (ip, &imm_expr);
beae10d5 7225 if ((unsigned long) imm_expr.X_add_number >= (1<<25))
252b5132 7226 {
beae10d5 7227 as_warn (_("Coproccesor code > 25 bits (%ld)"),
252b5132 7228 (long) imm_expr.X_add_number);
beae10d5 7229 imm_expr.X_add_number &= ((1<<25) - 1);
252b5132 7230 }
beae10d5
KH
7231 ip->insn_opcode |= imm_expr.X_add_number;
7232 imm_expr.X_op = O_absent;
7233 s = expr_end;
7234 continue;
252b5132 7235
4372b673
NC
7236 case 'J': /* 19-bit wait code. */
7237 my_getExpression (&imm_expr, s);
7238 check_absolute_expr (ip, &imm_expr);
7239 if ((unsigned) imm_expr.X_add_number > 0x7ffff)
7240 as_warn (_("Illegal 19-bit code (%ld)"),
7241 (long) imm_expr.X_add_number);
7242 ip->insn_opcode |= imm_expr.X_add_number << 6;
7243 imm_expr.X_op = O_absent;
7244 s = expr_end;
7245 continue;
7246
252b5132 7247 case 'P': /* Performance register */
beae10d5 7248 my_getExpression (&imm_expr, s);
252b5132 7249 check_absolute_expr (ip, &imm_expr);
beae10d5 7250 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 7251 {
beae10d5 7252 as_warn (_("Invalidate performance regster (%ld)"),
252b5132 7253 (long) imm_expr.X_add_number);
beae10d5 7254 imm_expr.X_add_number &= 1;
252b5132 7255 }
beae10d5
KH
7256 ip->insn_opcode |= (imm_expr.X_add_number << 1);
7257 imm_expr.X_op = O_absent;
7258 s = expr_end;
7259 continue;
252b5132
RH
7260
7261 case 'b': /* base register */
7262 case 'd': /* destination register */
7263 case 's': /* source register */
7264 case 't': /* target register */
7265 case 'r': /* both target and source */
7266 case 'v': /* both dest and source */
7267 case 'w': /* both dest and target */
7268 case 'E': /* coprocessor target register */
7269 case 'G': /* coprocessor destination register */
7270 case 'x': /* ignore register name */
7271 case 'z': /* must be zero register */
4372b673 7272 case 'U': /* destination register (clo/clz). */
252b5132
RH
7273 s_reset = s;
7274 if (s[0] == '$')
7275 {
7276
d9a62219 7277 if (isdigit ((unsigned char) s[1]))
252b5132
RH
7278 {
7279 ++s;
7280 regno = 0;
7281 do
7282 {
7283 regno *= 10;
7284 regno += *s - '0';
7285 ++s;
7286 }
d9a62219 7287 while (isdigit ((unsigned char) *s));
252b5132
RH
7288 if (regno > 31)
7289 as_bad (_("Invalid register number (%d)"), regno);
7290 }
7291 else if (*args == 'E' || *args == 'G')
7292 goto notreg;
7293 else
7294 {
7295 if (s[1] == 'f' && s[2] == 'p')
7296 {
7297 s += 3;
7298 regno = FP;
7299 }
7300 else if (s[1] == 's' && s[2] == 'p')
7301 {
7302 s += 3;
7303 regno = SP;
7304 }
7305 else if (s[1] == 'g' && s[2] == 'p')
7306 {
7307 s += 3;
7308 regno = GP;
7309 }
7310 else if (s[1] == 'a' && s[2] == 't')
7311 {
7312 s += 3;
7313 regno = AT;
7314 }
7315 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7316 {
7317 s += 4;
7318 regno = KT0;
7319 }
7320 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7321 {
7322 s += 4;
7323 regno = KT1;
7324 }
7325 else if (itbl_have_entries)
7326 {
7327 char *p, *n;
d7ba4a77 7328 unsigned long r;
252b5132 7329
d7ba4a77 7330 p = s + 1; /* advance past '$' */
252b5132
RH
7331 n = itbl_get_field (&p); /* n is name */
7332
d7ba4a77
ILT
7333 /* See if this is a register defined in an
7334 itbl entry. */
7335 if (itbl_get_reg_val (n, &r))
252b5132
RH
7336 {
7337 /* Get_field advances to the start of
7338 the next field, so we need to back
d7ba4a77 7339 rack to the end of the last field. */
bdaaa2e1 7340 if (p)
252b5132 7341 s = p - 1;
bdaaa2e1 7342 else
d7ba4a77 7343 s = strchr (s, '\0');
252b5132
RH
7344 regno = r;
7345 }
7346 else
7347 goto notreg;
beae10d5 7348 }
252b5132
RH
7349 else
7350 goto notreg;
7351 }
7352 if (regno == AT
7353 && ! mips_opts.noat
7354 && *args != 'E'
7355 && *args != 'G')
7356 as_warn (_("Used $at without \".set noat\""));
7357 c = *args;
7358 if (*s == ' ')
7359 s++;
7360 if (args[1] != *s)
7361 {
7362 if (c == 'r' || c == 'v' || c == 'w')
7363 {
7364 regno = lastregno;
7365 s = s_reset;
7366 args++;
7367 }
7368 }
7369 /* 'z' only matches $0. */
7370 if (c == 'z' && regno != 0)
7371 break;
7372
bdaaa2e1
KH
7373 /* Now that we have assembled one operand, we use the args string
7374 * to figure out where it goes in the instruction. */
252b5132
RH
7375 switch (c)
7376 {
7377 case 'r':
7378 case 's':
7379 case 'v':
7380 case 'b':
7381 ip->insn_opcode |= regno << 21;
7382 break;
7383 case 'd':
7384 case 'G':
7385 ip->insn_opcode |= regno << 11;
7386 break;
4372b673
NC
7387 case 'U':
7388 ip->insn_opcode |= regno << 11;
7389 ip->insn_opcode |= regno << 16;
7390 break;
252b5132
RH
7391 case 'w':
7392 case 't':
7393 case 'E':
7394 ip->insn_opcode |= regno << 16;
7395 break;
7396 case 'x':
7397 /* This case exists because on the r3000 trunc
7398 expands into a macro which requires a gp
7399 register. On the r6000 or r4000 it is
7400 assembled into a single instruction which
7401 ignores the register. Thus the insn version
7402 is MIPS_ISA2 and uses 'x', and the macro
7403 version is MIPS_ISA1 and uses 't'. */
7404 break;
7405 case 'z':
7406 /* This case is for the div instruction, which
7407 acts differently if the destination argument
7408 is $0. This only matches $0, and is checked
7409 outside the switch. */
7410 break;
7411 case 'D':
7412 /* Itbl operand; not yet implemented. FIXME ?? */
7413 break;
7414 /* What about all other operands like 'i', which
7415 can be specified in the opcode table? */
7416 }
7417 lastregno = regno;
7418 continue;
7419 }
7420 notreg:
7421 switch (*args++)
7422 {
7423 case 'r':
7424 case 'v':
7425 ip->insn_opcode |= lastregno << 21;
7426 continue;
7427 case 'w':
7428 ip->insn_opcode |= lastregno << 16;
7429 continue;
7430 }
7431 break;
7432
7433 case 'D': /* floating point destination register */
7434 case 'S': /* floating point source register */
7435 case 'T': /* floating point target register */
7436 case 'R': /* floating point source register */
7437 case 'V':
7438 case 'W':
7439 s_reset = s;
d9a62219 7440 if (s[0] == '$' && s[1] == 'f' && isdigit ((unsigned char) s[2]))
252b5132
RH
7441 {
7442 s += 2;
7443 regno = 0;
7444 do
7445 {
7446 regno *= 10;
7447 regno += *s - '0';
7448 ++s;
7449 }
d9a62219 7450 while (isdigit ((unsigned char) *s));
252b5132
RH
7451
7452 if (regno > 31)
7453 as_bad (_("Invalid float register number (%d)"), regno);
7454
7455 if ((regno & 1) != 0
9ce8a5dd 7456 && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
252b5132
RH
7457 && ! (strcmp (str, "mtc1") == 0
7458 || strcmp (str, "mfc1") == 0
7459 || strcmp (str, "lwc1") == 0
7460 || strcmp (str, "swc1") == 0
7461 || strcmp (str, "l.s") == 0
7462 || strcmp (str, "s.s") == 0))
7463 as_warn (_("Float register should be even, was %d"),
7464 regno);
7465
7466 c = *args;
7467 if (*s == ' ')
7468 s++;
7469 if (args[1] != *s)
7470 {
7471 if (c == 'V' || c == 'W')
7472 {
7473 regno = lastregno;
7474 s = s_reset;
7475 args++;
7476 }
7477 }
7478 switch (c)
7479 {
7480 case 'D':
7481 ip->insn_opcode |= regno << 6;
7482 break;
7483 case 'V':
7484 case 'S':
7485 ip->insn_opcode |= regno << 11;
7486 break;
7487 case 'W':
7488 case 'T':
7489 ip->insn_opcode |= regno << 16;
7490 break;
7491 case 'R':
7492 ip->insn_opcode |= regno << 21;
7493 break;
7494 }
7495 lastregno = regno;
7496 continue;
7497 }
7498
252b5132
RH
7499 switch (*args++)
7500 {
7501 case 'V':
7502 ip->insn_opcode |= lastregno << 11;
7503 continue;
7504 case 'W':
7505 ip->insn_opcode |= lastregno << 16;
7506 continue;
7507 }
7508 break;
7509
7510 case 'I':
7511 my_getExpression (&imm_expr, s);
7512 if (imm_expr.X_op != O_big
7513 && imm_expr.X_op != O_constant)
7514 insn_error = _("absolute expression required");
7515 s = expr_end;
7516 continue;
7517
7518 case 'A':
7519 my_getExpression (&offset_expr, s);
7520 imm_reloc = BFD_RELOC_32;
7521 s = expr_end;
7522 continue;
7523
7524 case 'F':
7525 case 'L':
7526 case 'f':
7527 case 'l':
7528 {
7529 int f64;
7530 char *save_in;
7531 char *err;
7532 unsigned char temp[8];
7533 int len;
7534 unsigned int length;
7535 segT seg;
7536 subsegT subseg;
7537 char *p;
7538
7539 /* These only appear as the last operand in an
7540 instruction, and every instruction that accepts
7541 them in any variant accepts them in all variants.
7542 This means we don't have to worry about backing out
7543 any changes if the instruction does not match.
7544
7545 The difference between them is the size of the
7546 floating point constant and where it goes. For 'F'
7547 and 'L' the constant is 64 bits; for 'f' and 'l' it
7548 is 32 bits. Where the constant is placed is based
7549 on how the MIPS assembler does things:
7550 F -- .rdata
7551 L -- .lit8
7552 f -- immediate value
7553 l -- .lit4
7554
7555 The .lit4 and .lit8 sections are only used if
7556 permitted by the -G argument.
7557
7558 When generating embedded PIC code, we use the
7559 .lit8 section but not the .lit4 section (we can do
7560 .lit4 inline easily; we need to put .lit8
7561 somewhere in the data segment, and using .lit8
7562 permits the linker to eventually combine identical
7563 .lit8 entries). */
7564
7565 f64 = *args == 'F' || *args == 'L';
7566
7567 save_in = input_line_pointer;
7568 input_line_pointer = s;
7569 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
7570 length = len;
7571 s = input_line_pointer;
7572 input_line_pointer = save_in;
7573 if (err != NULL && *err != '\0')
7574 {
7575 as_bad (_("Bad floating point constant: %s"), err);
7576 memset (temp, '\0', sizeof temp);
7577 length = f64 ? 8 : 4;
7578 }
7579
156c2f8b 7580 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
7581
7582 if (*args == 'f'
7583 || (*args == 'l'
7584 && (! USE_GLOBAL_POINTER_OPT
7585 || mips_pic == EMBEDDED_PIC
7586 || g_switch_value < 4
7587 || (temp[0] == 0 && temp[1] == 0)
7588 || (temp[2] == 0 && temp[3] == 0))))
7589 {
7590 imm_expr.X_op = O_constant;
7591 if (! target_big_endian)
7592 imm_expr.X_add_number = bfd_getl32 (temp);
7593 else
7594 imm_expr.X_add_number = bfd_getb32 (temp);
7595 }
7596 else if (length > 4
119d663a 7597 && ! mips_disable_float_construction
252b5132
RH
7598 && ((temp[0] == 0 && temp[1] == 0)
7599 || (temp[2] == 0 && temp[3] == 0))
7600 && ((temp[4] == 0 && temp[5] == 0)
7601 || (temp[6] == 0 && temp[7] == 0)))
7602 {
7603 /* The value is simple enough to load with a
7604 couple of instructions. In mips1 mode, set
7605 imm_expr to the high order 32 bits and
7606 offset_expr to the low order 32 bits.
7607 Otherwise, set imm_expr to the entire 64 bit
7608 constant. */
9ce8a5dd 7609 if (! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
7610 {
7611 imm_expr.X_op = O_constant;
7612 offset_expr.X_op = O_constant;
7613 if (! target_big_endian)
7614 {
7615 imm_expr.X_add_number = bfd_getl32 (temp + 4);
7616 offset_expr.X_add_number = bfd_getl32 (temp);
7617 }
7618 else
7619 {
7620 imm_expr.X_add_number = bfd_getb32 (temp);
7621 offset_expr.X_add_number = bfd_getb32 (temp + 4);
7622 }
7623 if (offset_expr.X_add_number == 0)
7624 offset_expr.X_op = O_absent;
7625 }
7626 else if (sizeof (imm_expr.X_add_number) > 4)
7627 {
7628 imm_expr.X_op = O_constant;
7629 if (! target_big_endian)
7630 imm_expr.X_add_number = bfd_getl64 (temp);
7631 else
7632 imm_expr.X_add_number = bfd_getb64 (temp);
7633 }
7634 else
7635 {
7636 imm_expr.X_op = O_big;
7637 imm_expr.X_add_number = 4;
7638 if (! target_big_endian)
7639 {
7640 generic_bignum[0] = bfd_getl16 (temp);
7641 generic_bignum[1] = bfd_getl16 (temp + 2);
7642 generic_bignum[2] = bfd_getl16 (temp + 4);
7643 generic_bignum[3] = bfd_getl16 (temp + 6);
7644 }
7645 else
7646 {
7647 generic_bignum[0] = bfd_getb16 (temp + 6);
7648 generic_bignum[1] = bfd_getb16 (temp + 4);
7649 generic_bignum[2] = bfd_getb16 (temp + 2);
7650 generic_bignum[3] = bfd_getb16 (temp);
7651 }
7652 }
7653 }
7654 else
7655 {
7656 const char *newname;
7657 segT new_seg;
7658
7659 /* Switch to the right section. */
7660 seg = now_seg;
7661 subseg = now_subseg;
7662 switch (*args)
7663 {
7664 default: /* unused default case avoids warnings. */
7665 case 'L':
7666 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
7667 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
7668 || mips_pic == EMBEDDED_PIC)
252b5132
RH
7669 newname = ".lit8";
7670 break;
7671 case 'F':
bb2d6cd7
GK
7672 if (mips_pic == EMBEDDED_PIC)
7673 newname = ".lit8";
7674 else
7675 newname = RDATA_SECTION_NAME;
252b5132
RH
7676 break;
7677 case 'l':
7678 assert (!USE_GLOBAL_POINTER_OPT
7679 || g_switch_value >= 4);
7680 newname = ".lit4";
7681 break;
7682 }
7683 new_seg = subseg_new (newname, (subsegT) 0);
7684 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
7685 bfd_set_section_flags (stdoutput, new_seg,
7686 (SEC_ALLOC
7687 | SEC_LOAD
7688 | SEC_READONLY
7689 | SEC_DATA));
7690 frag_align (*args == 'l' ? 2 : 3, 0, 0);
7691 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
7692 && strcmp (TARGET_OS, "elf") != 0)
7693 record_alignment (new_seg, 4);
7694 else
7695 record_alignment (new_seg, *args == 'l' ? 2 : 3);
7696 if (seg == now_seg)
7697 as_bad (_("Can't use floating point insn in this section"));
7698
7699 /* Set the argument to the current address in the
7700 section. */
7701 offset_expr.X_op = O_symbol;
7702 offset_expr.X_add_symbol =
7703 symbol_new ("L0\001", now_seg,
7704 (valueT) frag_now_fix (), frag_now);
7705 offset_expr.X_add_number = 0;
7706
7707 /* Put the floating point number into the section. */
7708 p = frag_more ((int) length);
7709 memcpy (p, temp, length);
7710
7711 /* Switch back to the original section. */
7712 subseg_set (seg, subseg);
7713 }
7714 }
7715 continue;
7716
7717 case 'i': /* 16 bit unsigned immediate */
7718 case 'j': /* 16 bit signed immediate */
7719 imm_reloc = BFD_RELOC_LO16;
7720 c = my_getSmallExpression (&imm_expr, s);
7721 if (c != '\0')
7722 {
7723 if (c != 'l')
7724 {
7725 if (imm_expr.X_op == O_constant)
7726 imm_expr.X_add_number =
7727 (imm_expr.X_add_number >> 16) & 0xffff;
7728 else if (c == 'h')
7729 {
7730 imm_reloc = BFD_RELOC_HI16_S;
7731 imm_unmatched_hi = true;
7732 }
7733 else
7734 imm_reloc = BFD_RELOC_HI16;
7735 }
7736 else if (imm_expr.X_op == O_constant)
7737 imm_expr.X_add_number &= 0xffff;
7738 }
7739 if (*args == 'i')
7740 {
7741 if ((c == '\0' && imm_expr.X_op != O_constant)
7742 || ((imm_expr.X_add_number < 0
beae10d5
KH
7743 || imm_expr.X_add_number >= 0x10000)
7744 && imm_expr.X_op == O_constant))
252b5132
RH
7745 {
7746 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7747 !strcmp (insn->name, insn[1].name))
7748 break;
2ae7e77b
AH
7749 if (imm_expr.X_op == O_constant
7750 || imm_expr.X_op == O_big)
252b5132
RH
7751 as_bad (_("16 bit expression not in range 0..65535"));
7752 }
7753 }
7754 else
7755 {
7756 int more;
7757 offsetT max;
7758
7759 /* The upper bound should be 0x8000, but
7760 unfortunately the MIPS assembler accepts numbers
7761 from 0x8000 to 0xffff and sign extends them, and
7762 we want to be compatible. We only permit this
7763 extended range for an instruction which does not
7764 provide any further alternates, since those
7765 alternates may handle other cases. People should
7766 use the numbers they mean, rather than relying on
7767 a mysterious sign extension. */
7768 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7769 strcmp (insn->name, insn[1].name) == 0);
7770 if (more)
7771 max = 0x8000;
7772 else
7773 max = 0x10000;
7774 if ((c == '\0' && imm_expr.X_op != O_constant)
7775 || ((imm_expr.X_add_number < -0x8000
beae10d5
KH
7776 || imm_expr.X_add_number >= max)
7777 && imm_expr.X_op == O_constant)
252b5132
RH
7778 || (more
7779 && imm_expr.X_add_number < 0
9ce8a5dd 7780 && ISA_HAS_64BIT_REGS (mips_opts.isa)
252b5132
RH
7781 && imm_expr.X_unsigned
7782 && sizeof (imm_expr.X_add_number) <= 4))
7783 {
7784 if (more)
7785 break;
2ae7e77b
AH
7786 if (imm_expr.X_op == O_constant
7787 || imm_expr.X_op == O_big)
252b5132
RH
7788 as_bad (_("16 bit expression not in range -32768..32767"));
7789 }
7790 }
7791 s = expr_end;
7792 continue;
7793
7794 case 'o': /* 16 bit offset */
7795 c = my_getSmallExpression (&offset_expr, s);
7796
7797 /* If this value won't fit into a 16 bit offset, then go
7798 find a macro that will generate the 32 bit offset
7799 code pattern. As a special hack, we accept the
7800 difference of two local symbols as a constant. This
7801 is required to suppose embedded PIC switches, which
7802 use an instruction which looks like
7803 lw $4,$L12-$LS12($4)
7804 The problem with handling this in a more general
7805 fashion is that the macro function doesn't expect to
7806 see anything which can be handled in a single
7807 constant instruction. */
7808 if (c == 0
7809 && (offset_expr.X_op != O_constant
7810 || offset_expr.X_add_number >= 0x8000
7811 || offset_expr.X_add_number < -0x8000)
7812 && (mips_pic != EMBEDDED_PIC
7813 || offset_expr.X_op != O_subtract
3dacdf2e
NC
7814 || (S_GET_SEGMENT (offset_expr.X_add_symbol)
7815 != S_GET_SEGMENT (offset_expr.X_op_symbol))))
252b5132
RH
7816 break;
7817
7818 if (c == 'h' || c == 'H')
7819 {
7820 if (offset_expr.X_op != O_constant)
7821 break;
7822 offset_expr.X_add_number =
7823 (offset_expr.X_add_number >> 16) & 0xffff;
7824 }
7825 offset_reloc = BFD_RELOC_LO16;
7826 s = expr_end;
7827 continue;
7828
7829 case 'p': /* pc relative offset */
7830 offset_reloc = BFD_RELOC_16_PCREL_S2;
7831 my_getExpression (&offset_expr, s);
7832 s = expr_end;
7833 continue;
7834
7835 case 'u': /* upper 16 bits */
7836 c = my_getSmallExpression (&imm_expr, s);
7837 imm_reloc = BFD_RELOC_LO16;
7838 if (c)
7839 {
7840 if (c != 'l')
7841 {
7842 if (imm_expr.X_op == O_constant)
7843 imm_expr.X_add_number =
7844 (imm_expr.X_add_number >> 16) & 0xffff;
7845 else if (c == 'h')
7846 {
7847 imm_reloc = BFD_RELOC_HI16_S;
7848 imm_unmatched_hi = true;
7849 }
7850 else
7851 imm_reloc = BFD_RELOC_HI16;
7852 }
7853 else if (imm_expr.X_op == O_constant)
7854 imm_expr.X_add_number &= 0xffff;
7855 }
7856 if (imm_expr.X_op == O_constant
7857 && (imm_expr.X_add_number < 0
7858 || imm_expr.X_add_number >= 0x10000))
7859 as_bad (_("lui expression not in range 0..65535"));
7860 s = expr_end;
7861 continue;
7862
7863 case 'a': /* 26 bit address */
7864 my_getExpression (&offset_expr, s);
7865 s = expr_end;
7866 offset_reloc = BFD_RELOC_MIPS_JMP;
7867 continue;
7868
7869 case 'N': /* 3 bit branch condition code */
7870 case 'M': /* 3 bit compare condition code */
7871 if (strncmp (s, "$fcc", 4) != 0)
7872 break;
7873 s += 4;
7874 regno = 0;
7875 do
7876 {
7877 regno *= 10;
7878 regno += *s - '0';
7879 ++s;
7880 }
d9a62219 7881 while (isdigit ((unsigned char) *s));
252b5132
RH
7882 if (regno > 7)
7883 as_bad (_("invalid condition code register $fcc%d"), regno);
7884 if (*args == 'N')
7885 ip->insn_opcode |= regno << OP_SH_BCC;
7886 else
7887 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 7888 continue;
252b5132 7889
156c2f8b
NC
7890 case 'H':
7891 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
7892 s += 2;
7893 if (isdigit ((unsigned char) *s))
7894 {
7895 c = 0;
7896 do
7897 {
7898 c *= 10;
7899 c += *s - '0';
7900 ++s;
7901 }
7902 while (isdigit ((unsigned char) *s));
7903 }
7904 else
7905 c = 8; /* Invalid sel value. */
7906
7907 if (c > 7)
7908 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
7909 ip->insn_opcode |= c;
7910 continue;
7911
252b5132
RH
7912 default:
7913 as_bad (_("bad char = '%c'\n"), *args);
7914 internalError ();
7915 }
7916 break;
7917 }
7918 /* Args don't match. */
7919 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7920 !strcmp (insn->name, insn[1].name))
7921 {
7922 ++insn;
7923 s = argsStart;
7924 continue;
7925 }
7926 insn_error = _("illegal operands");
7927 return;
7928 }
7929}
7930
7931/* This routine assembles an instruction into its binary format when
7932 assembling for the mips16. As a side effect, it sets one of the
7933 global variables imm_reloc or offset_reloc to the type of
7934 relocation to do if one of the operands is an address expression.
7935 It also sets mips16_small and mips16_ext if the user explicitly
7936 requested a small or extended instruction. */
7937
7938static void
7939mips16_ip (str, ip)
7940 char *str;
7941 struct mips_cl_insn *ip;
7942{
7943 char *s;
7944 const char *args;
7945 struct mips_opcode *insn;
7946 char *argsstart;
7947 unsigned int regno;
7948 unsigned int lastregno = 0;
7949 char *s_reset;
7950
7951 insn_error = NULL;
7952
7953 mips16_small = false;
7954 mips16_ext = false;
7955
d9a62219 7956 for (s = str; islower ((unsigned char) *s); ++s)
252b5132
RH
7957 ;
7958 switch (*s)
7959 {
7960 case '\0':
7961 break;
7962
7963 case ' ':
7964 *s++ = '\0';
7965 break;
7966
7967 case '.':
7968 if (s[1] == 't' && s[2] == ' ')
7969 {
7970 *s = '\0';
7971 mips16_small = true;
7972 s += 3;
7973 break;
7974 }
7975 else if (s[1] == 'e' && s[2] == ' ')
7976 {
7977 *s = '\0';
7978 mips16_ext = true;
7979 s += 3;
7980 break;
7981 }
7982 /* Fall through. */
7983 default:
7984 insn_error = _("unknown opcode");
7985 return;
7986 }
7987
7988 if (mips_opts.noautoextend && ! mips16_ext)
7989 mips16_small = true;
7990
7991 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
7992 {
7993 insn_error = _("unrecognized opcode");
7994 return;
7995 }
7996
7997 argsstart = s;
7998 for (;;)
7999 {
8000 assert (strcmp (insn->name, str) == 0);
8001
8002 ip->insn_mo = insn;
8003 ip->insn_opcode = insn->match;
8004 ip->use_extend = false;
8005 imm_expr.X_op = O_absent;
8006 imm_reloc = BFD_RELOC_UNUSED;
8007 offset_expr.X_op = O_absent;
8008 offset_reloc = BFD_RELOC_UNUSED;
8009 for (args = insn->args; 1; ++args)
8010 {
8011 int c;
8012
8013 if (*s == ' ')
8014 ++s;
8015
8016 /* In this switch statement we call break if we did not find
8017 a match, continue if we did find a match, or return if we
8018 are done. */
8019
8020 c = *args;
8021 switch (c)
8022 {
8023 case '\0':
8024 if (*s == '\0')
8025 {
8026 /* Stuff the immediate value in now, if we can. */
8027 if (imm_expr.X_op == O_constant
8028 && imm_reloc > BFD_RELOC_UNUSED
8029 && insn->pinfo != INSN_MACRO)
8030 {
8031 mips16_immed ((char *) NULL, 0,
8032 imm_reloc - BFD_RELOC_UNUSED,
8033 imm_expr.X_add_number, true, mips16_small,
8034 mips16_ext, &ip->insn_opcode,
8035 &ip->use_extend, &ip->extend);
8036 imm_expr.X_op = O_absent;
8037 imm_reloc = BFD_RELOC_UNUSED;
8038 }
8039
8040 return;
8041 }
8042 break;
8043
8044 case ',':
8045 if (*s++ == c)
8046 continue;
8047 s--;
8048 switch (*++args)
8049 {
8050 case 'v':
8051 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8052 continue;
8053 case 'w':
8054 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8055 continue;
8056 }
8057 break;
8058
8059 case '(':
8060 case ')':
8061 if (*s++ == c)
8062 continue;
8063 break;
8064
8065 case 'v':
8066 case 'w':
8067 if (s[0] != '$')
8068 {
8069 if (c == 'v')
8070 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8071 else
8072 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8073 ++args;
8074 continue;
8075 }
8076 /* Fall through. */
8077 case 'x':
8078 case 'y':
8079 case 'z':
8080 case 'Z':
8081 case '0':
8082 case 'S':
8083 case 'R':
8084 case 'X':
8085 case 'Y':
8086 if (s[0] != '$')
8087 break;
8088 s_reset = s;
d9a62219 8089 if (isdigit ((unsigned char) s[1]))
252b5132
RH
8090 {
8091 ++s;
8092 regno = 0;
8093 do
8094 {
8095 regno *= 10;
8096 regno += *s - '0';
8097 ++s;
8098 }
d9a62219 8099 while (isdigit ((unsigned char) *s));
252b5132
RH
8100 if (regno > 31)
8101 {
8102 as_bad (_("invalid register number (%d)"), regno);
8103 regno = 2;
8104 }
8105 }
8106 else
8107 {
8108 if (s[1] == 'f' && s[2] == 'p')
8109 {
8110 s += 3;
8111 regno = FP;
8112 }
8113 else if (s[1] == 's' && s[2] == 'p')
8114 {
8115 s += 3;
8116 regno = SP;
8117 }
8118 else if (s[1] == 'g' && s[2] == 'p')
8119 {
8120 s += 3;
8121 regno = GP;
8122 }
8123 else if (s[1] == 'a' && s[2] == 't')
8124 {
8125 s += 3;
8126 regno = AT;
8127 }
8128 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8129 {
8130 s += 4;
8131 regno = KT0;
8132 }
8133 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8134 {
8135 s += 4;
8136 regno = KT1;
8137 }
8138 else
8139 break;
8140 }
8141
8142 if (*s == ' ')
8143 ++s;
8144 if (args[1] != *s)
8145 {
8146 if (c == 'v' || c == 'w')
8147 {
8148 regno = mips16_to_32_reg_map[lastregno];
8149 s = s_reset;
8150 args++;
8151 }
8152 }
8153
8154 switch (c)
8155 {
8156 case 'x':
8157 case 'y':
8158 case 'z':
8159 case 'v':
8160 case 'w':
8161 case 'Z':
8162 regno = mips32_to_16_reg_map[regno];
8163 break;
8164
8165 case '0':
8166 if (regno != 0)
8167 regno = ILLEGAL_REG;
8168 break;
8169
8170 case 'S':
8171 if (regno != SP)
8172 regno = ILLEGAL_REG;
8173 break;
8174
8175 case 'R':
8176 if (regno != RA)
8177 regno = ILLEGAL_REG;
8178 break;
8179
8180 case 'X':
8181 case 'Y':
8182 if (regno == AT && ! mips_opts.noat)
8183 as_warn (_("used $at without \".set noat\""));
8184 break;
8185
8186 default:
8187 internalError ();
8188 }
8189
8190 if (regno == ILLEGAL_REG)
8191 break;
8192
8193 switch (c)
8194 {
8195 case 'x':
8196 case 'v':
8197 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
8198 break;
8199 case 'y':
8200 case 'w':
8201 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
8202 break;
8203 case 'z':
8204 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
8205 break;
8206 case 'Z':
8207 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
8208 case '0':
8209 case 'S':
8210 case 'R':
8211 break;
8212 case 'X':
8213 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
8214 break;
8215 case 'Y':
8216 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
8217 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
8218 break;
8219 default:
8220 internalError ();
8221 }
8222
8223 lastregno = regno;
8224 continue;
8225
8226 case 'P':
8227 if (strncmp (s, "$pc", 3) == 0)
8228 {
8229 s += 3;
8230 continue;
8231 }
8232 break;
8233
8234 case '<':
8235 case '>':
8236 case '[':
8237 case ']':
8238 case '4':
8239 case '5':
8240 case 'H':
8241 case 'W':
8242 case 'D':
8243 case 'j':
8244 case '8':
8245 case 'V':
8246 case 'C':
8247 case 'U':
8248 case 'k':
8249 case 'K':
8250 if (s[0] == '%'
8251 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
8252 {
8253 /* This is %gprel(SYMBOL). We need to read SYMBOL,
8254 and generate the appropriate reloc. If the text
8255 inside %gprel is not a symbol name with an
8256 optional offset, then we generate a normal reloc
8257 and will probably fail later. */
8258 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
8259 if (imm_expr.X_op == O_symbol)
8260 {
8261 mips16_ext = true;
8262 imm_reloc = BFD_RELOC_MIPS16_GPREL;
8263 s = expr_end;
8264 ip->use_extend = true;
8265 ip->extend = 0;
8266 continue;
8267 }
8268 }
8269 else
8270 {
8271 /* Just pick up a normal expression. */
8272 my_getExpression (&imm_expr, s);
8273 }
8274
8275 if (imm_expr.X_op == O_register)
8276 {
8277 /* What we thought was an expression turned out to
8278 be a register. */
8279
8280 if (s[0] == '(' && args[1] == '(')
8281 {
8282 /* It looks like the expression was omitted
8283 before a register indirection, which means
8284 that the expression is implicitly zero. We
8285 still set up imm_expr, so that we handle
8286 explicit extensions correctly. */
8287 imm_expr.X_op = O_constant;
8288 imm_expr.X_add_number = 0;
8289 imm_reloc = (int) BFD_RELOC_UNUSED + c;
8290 continue;
8291 }
8292
8293 break;
8294 }
8295
8296 /* We need to relax this instruction. */
8297 imm_reloc = (int) BFD_RELOC_UNUSED + c;
8298 s = expr_end;
8299 continue;
8300
8301 case 'p':
8302 case 'q':
8303 case 'A':
8304 case 'B':
8305 case 'E':
8306 /* We use offset_reloc rather than imm_reloc for the PC
8307 relative operands. This lets macros with both
8308 immediate and address operands work correctly. */
8309 my_getExpression (&offset_expr, s);
8310
8311 if (offset_expr.X_op == O_register)
8312 break;
8313
8314 /* We need to relax this instruction. */
8315 offset_reloc = (int) BFD_RELOC_UNUSED + c;
8316 s = expr_end;
8317 continue;
8318
8319 case '6': /* break code */
8320 my_getExpression (&imm_expr, s);
8321 check_absolute_expr (ip, &imm_expr);
8322 if ((unsigned long) imm_expr.X_add_number > 63)
8323 {
8324 as_warn (_("Invalid value for `%s' (%lu)"),
8325 ip->insn_mo->name,
8326 (unsigned long) imm_expr.X_add_number);
8327 imm_expr.X_add_number &= 0x3f;
8328 }
8329 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
8330 imm_expr.X_op = O_absent;
8331 s = expr_end;
8332 continue;
8333
8334 case 'a': /* 26 bit address */
8335 my_getExpression (&offset_expr, s);
8336 s = expr_end;
8337 offset_reloc = BFD_RELOC_MIPS16_JMP;
8338 ip->insn_opcode <<= 16;
8339 continue;
8340
8341 case 'l': /* register list for entry macro */
8342 case 'L': /* register list for exit macro */
8343 {
8344 int mask;
8345
8346 if (c == 'l')
8347 mask = 0;
8348 else
8349 mask = 7 << 3;
8350 while (*s != '\0')
8351 {
8352 int freg, reg1, reg2;
8353
8354 while (*s == ' ' || *s == ',')
8355 ++s;
8356 if (*s != '$')
8357 {
8358 as_bad (_("can't parse register list"));
8359 break;
8360 }
8361 ++s;
8362 if (*s != 'f')
8363 freg = 0;
8364 else
8365 {
8366 freg = 1;
8367 ++s;
8368 }
8369 reg1 = 0;
d9a62219 8370 while (isdigit ((unsigned char) *s))
252b5132
RH
8371 {
8372 reg1 *= 10;
8373 reg1 += *s - '0';
8374 ++s;
8375 }
8376 if (*s == ' ')
8377 ++s;
8378 if (*s != '-')
8379 reg2 = reg1;
8380 else
8381 {
8382 ++s;
8383 if (*s != '$')
8384 break;
8385 ++s;
8386 if (freg)
8387 {
8388 if (*s == 'f')
8389 ++s;
8390 else
8391 {
8392 as_bad (_("invalid register list"));
8393 break;
8394 }
8395 }
8396 reg2 = 0;
d9a62219 8397 while (isdigit ((unsigned char) *s))
252b5132
RH
8398 {
8399 reg2 *= 10;
8400 reg2 += *s - '0';
8401 ++s;
8402 }
8403 }
8404 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
8405 {
8406 mask &= ~ (7 << 3);
8407 mask |= 5 << 3;
8408 }
8409 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
8410 {
8411 mask &= ~ (7 << 3);
8412 mask |= 6 << 3;
8413 }
8414 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
8415 mask |= (reg2 - 3) << 3;
8416 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
8417 mask |= (reg2 - 15) << 1;
8418 else if (reg1 == 31 && reg2 == 31)
8419 mask |= 1;
8420 else
8421 {
8422 as_bad (_("invalid register list"));
8423 break;
8424 }
8425 }
8426 /* The mask is filled in in the opcode table for the
8427 benefit of the disassembler. We remove it before
8428 applying the actual mask. */
8429 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
8430 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
8431 }
8432 continue;
8433
8434 case 'e': /* extend code */
8435 my_getExpression (&imm_expr, s);
8436 check_absolute_expr (ip, &imm_expr);
8437 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
8438 {
8439 as_warn (_("Invalid value for `%s' (%lu)"),
8440 ip->insn_mo->name,
8441 (unsigned long) imm_expr.X_add_number);
8442 imm_expr.X_add_number &= 0x7ff;
8443 }
8444 ip->insn_opcode |= imm_expr.X_add_number;
8445 imm_expr.X_op = O_absent;
8446 s = expr_end;
8447 continue;
8448
8449 default:
8450 internalError ();
8451 }
8452 break;
8453 }
8454
8455 /* Args don't match. */
8456 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
8457 strcmp (insn->name, insn[1].name) == 0)
8458 {
8459 ++insn;
8460 s = argsstart;
8461 continue;
8462 }
8463
8464 insn_error = _("illegal operands");
8465
8466 return;
8467 }
8468}
8469
8470/* This structure holds information we know about a mips16 immediate
8471 argument type. */
8472
e972090a
NC
8473struct mips16_immed_operand
8474{
252b5132
RH
8475 /* The type code used in the argument string in the opcode table. */
8476 int type;
8477 /* The number of bits in the short form of the opcode. */
8478 int nbits;
8479 /* The number of bits in the extended form of the opcode. */
8480 int extbits;
8481 /* The amount by which the short form is shifted when it is used;
8482 for example, the sw instruction has a shift count of 2. */
8483 int shift;
8484 /* The amount by which the short form is shifted when it is stored
8485 into the instruction code. */
8486 int op_shift;
8487 /* Non-zero if the short form is unsigned. */
8488 int unsp;
8489 /* Non-zero if the extended form is unsigned. */
8490 int extu;
8491 /* Non-zero if the value is PC relative. */
8492 int pcrel;
8493};
8494
8495/* The mips16 immediate operand types. */
8496
8497static const struct mips16_immed_operand mips16_immed_operands[] =
8498{
8499 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
8500 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
8501 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
8502 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
8503 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
8504 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
8505 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
8506 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
8507 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
8508 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
8509 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
8510 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
8511 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
8512 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
8513 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
8514 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
8515 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
8516 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
8517 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
8518 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
8519 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
8520};
8521
8522#define MIPS16_NUM_IMMED \
8523 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
8524
8525/* Handle a mips16 instruction with an immediate value. This or's the
8526 small immediate value into *INSN. It sets *USE_EXTEND to indicate
8527 whether an extended value is needed; if one is needed, it sets
8528 *EXTEND to the value. The argument type is TYPE. The value is VAL.
8529 If SMALL is true, an unextended opcode was explicitly requested.
8530 If EXT is true, an extended opcode was explicitly requested. If
8531 WARN is true, warn if EXT does not match reality. */
8532
8533static void
8534mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
8535 extend)
8536 char *file;
8537 unsigned int line;
8538 int type;
8539 offsetT val;
8540 boolean warn;
8541 boolean small;
8542 boolean ext;
8543 unsigned long *insn;
8544 boolean *use_extend;
8545 unsigned short *extend;
8546{
8547 register const struct mips16_immed_operand *op;
8548 int mintiny, maxtiny;
8549 boolean needext;
8550
8551 op = mips16_immed_operands;
8552 while (op->type != type)
8553 {
8554 ++op;
8555 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
8556 }
8557
8558 if (op->unsp)
8559 {
8560 if (type == '<' || type == '>' || type == '[' || type == ']')
8561 {
8562 mintiny = 1;
8563 maxtiny = 1 << op->nbits;
8564 }
8565 else
8566 {
8567 mintiny = 0;
8568 maxtiny = (1 << op->nbits) - 1;
8569 }
8570 }
8571 else
8572 {
8573 mintiny = - (1 << (op->nbits - 1));
8574 maxtiny = (1 << (op->nbits - 1)) - 1;
8575 }
8576
8577 /* Branch offsets have an implicit 0 in the lowest bit. */
8578 if (type == 'p' || type == 'q')
8579 val /= 2;
8580
8581 if ((val & ((1 << op->shift) - 1)) != 0
8582 || val < (mintiny << op->shift)
8583 || val > (maxtiny << op->shift))
8584 needext = true;
8585 else
8586 needext = false;
8587
8588 if (warn && ext && ! needext)
beae10d5
KH
8589 as_warn_where (file, line,
8590 _("extended operand requested but not required"));
252b5132
RH
8591 if (small && needext)
8592 as_bad_where (file, line, _("invalid unextended operand value"));
8593
8594 if (small || (! ext && ! needext))
8595 {
8596 int insnval;
8597
8598 *use_extend = false;
8599 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
8600 insnval <<= op->op_shift;
8601 *insn |= insnval;
8602 }
8603 else
8604 {
8605 long minext, maxext;
8606 int extval;
8607
8608 if (op->extu)
8609 {
8610 minext = 0;
8611 maxext = (1 << op->extbits) - 1;
8612 }
8613 else
8614 {
8615 minext = - (1 << (op->extbits - 1));
8616 maxext = (1 << (op->extbits - 1)) - 1;
8617 }
8618 if (val < minext || val > maxext)
8619 as_bad_where (file, line,
8620 _("operand value out of range for instruction"));
8621
8622 *use_extend = true;
8623 if (op->extbits == 16)
8624 {
8625 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
8626 val &= 0x1f;
8627 }
8628 else if (op->extbits == 15)
8629 {
8630 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
8631 val &= 0xf;
8632 }
8633 else
8634 {
8635 extval = ((val & 0x1f) << 6) | (val & 0x20);
8636 val = 0;
8637 }
8638
8639 *extend = (unsigned short) extval;
8640 *insn |= val;
8641 }
8642}
8643\f
8644#define LP '('
8645#define RP ')'
8646
8647static int
8648my_getSmallExpression (ep, str)
8649 expressionS *ep;
8650 char *str;
8651{
8652 char *sp;
8653 int c = 0;
8654
8655 if (*str == ' ')
8656 str++;
8657 if (*str == LP
8658 || (*str == '%' &&
8659 ((str[1] == 'h' && str[2] == 'i')
8660 || (str[1] == 'H' && str[2] == 'I')
8661 || (str[1] == 'l' && str[2] == 'o'))
8662 && str[3] == LP))
8663 {
8664 if (*str == LP)
8665 c = 0;
8666 else
8667 {
8668 c = str[1];
8669 str += 3;
8670 }
8671
8672 /*
8673 * A small expression may be followed by a base register.
8674 * Scan to the end of this operand, and then back over a possible
8675 * base register. Then scan the small expression up to that
8676 * point. (Based on code in sparc.c...)
8677 */
8678 for (sp = str; *sp && *sp != ','; sp++)
8679 ;
8680 if (sp - 4 >= str && sp[-1] == RP)
8681 {
d9a62219 8682 if (isdigit ((unsigned char) sp[-2]))
252b5132 8683 {
d9a62219 8684 for (sp -= 3; sp >= str && isdigit ((unsigned char) *sp); sp--)
252b5132
RH
8685 ;
8686 if (*sp == '$' && sp > str && sp[-1] == LP)
8687 {
8688 sp--;
8689 goto do_it;
8690 }
8691 }
8692 else if (sp - 5 >= str
8693 && sp[-5] == LP
8694 && sp[-4] == '$'
8695 && ((sp[-3] == 'f' && sp[-2] == 'p')
8696 || (sp[-3] == 's' && sp[-2] == 'p')
8697 || (sp[-3] == 'g' && sp[-2] == 'p')
8698 || (sp[-3] == 'a' && sp[-2] == 't')))
8699 {
8700 sp -= 5;
8701 do_it:
8702 if (sp == str)
8703 {
8704 /* no expression means zero offset */
8705 if (c)
8706 {
8707 /* %xx(reg) is an error */
8708 ep->X_op = O_absent;
8709 expr_end = str - 3;
8710 }
8711 else
8712 {
8713 ep->X_op = O_constant;
8714 expr_end = sp;
8715 }
8716 ep->X_add_symbol = NULL;
8717 ep->X_op_symbol = NULL;
8718 ep->X_add_number = 0;
8719 }
8720 else
8721 {
8722 *sp = '\0';
8723 my_getExpression (ep, str);
8724 *sp = LP;
8725 }
8726 return c;
8727 }
8728 }
8729 }
8730 my_getExpression (ep, str);
8731 return c; /* => %hi or %lo encountered */
8732}
8733
8734static void
8735my_getExpression (ep, str)
8736 expressionS *ep;
8737 char *str;
8738{
8739 char *save_in;
8740
8741 save_in = input_line_pointer;
8742 input_line_pointer = str;
8743 expression (ep);
8744 expr_end = input_line_pointer;
8745 input_line_pointer = save_in;
8746
8747 /* If we are in mips16 mode, and this is an expression based on `.',
8748 then we bump the value of the symbol by 1 since that is how other
8749 text symbols are handled. We don't bother to handle complex
8750 expressions, just `.' plus or minus a constant. */
8751 if (mips_opts.mips16
8752 && ep->X_op == O_symbol
8753 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
8754 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
8755 && symbol_get_frag (ep->X_add_symbol) == frag_now
8756 && symbol_constant_p (ep->X_add_symbol)
8757 && S_GET_VALUE (ep->X_add_symbol) == frag_now_fix ())
8758 S_SET_VALUE (ep->X_add_symbol, S_GET_VALUE (ep->X_add_symbol) + 1);
252b5132
RH
8759}
8760
8761/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
8762 of type TYPE, and store the appropriate bytes in *LITP. The number
8763 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
8764 returned, or NULL on OK. */
8765
8766char *
8767md_atof (type, litP, sizeP)
8768 int type;
8769 char *litP;
8770 int *sizeP;
8771{
8772 int prec;
8773 LITTLENUM_TYPE words[4];
8774 char *t;
8775 int i;
8776
8777 switch (type)
8778 {
8779 case 'f':
8780 prec = 2;
8781 break;
8782
8783 case 'd':
8784 prec = 4;
8785 break;
8786
8787 default:
8788 *sizeP = 0;
8789 return _("bad call to md_atof");
8790 }
8791
8792 t = atof_ieee (input_line_pointer, type, words);
8793 if (t)
8794 input_line_pointer = t;
8795
8796 *sizeP = prec * 2;
8797
8798 if (! target_big_endian)
8799 {
8800 for (i = prec - 1; i >= 0; i--)
8801 {
8802 md_number_to_chars (litP, (valueT) words[i], 2);
8803 litP += 2;
8804 }
8805 }
8806 else
8807 {
8808 for (i = 0; i < prec; i++)
8809 {
8810 md_number_to_chars (litP, (valueT) words[i], 2);
8811 litP += 2;
8812 }
8813 }
bdaaa2e1 8814
252b5132
RH
8815 return NULL;
8816}
8817
8818void
8819md_number_to_chars (buf, val, n)
8820 char *buf;
8821 valueT val;
8822 int n;
8823{
8824 if (target_big_endian)
8825 number_to_chars_bigendian (buf, val, n);
8826 else
8827 number_to_chars_littleendian (buf, val, n);
8828}
8829\f
8830CONST char *md_shortopts = "O::g::G:";
8831
e972090a
NC
8832struct option md_longopts[] =
8833{
252b5132
RH
8834#define OPTION_MIPS1 (OPTION_MD_BASE + 1)
8835 {"mips0", no_argument, NULL, OPTION_MIPS1},
8836 {"mips1", no_argument, NULL, OPTION_MIPS1},
8837#define OPTION_MIPS2 (OPTION_MD_BASE + 2)
8838 {"mips2", no_argument, NULL, OPTION_MIPS2},
8839#define OPTION_MIPS3 (OPTION_MD_BASE + 3)
8840 {"mips3", no_argument, NULL, OPTION_MIPS3},
8841#define OPTION_MIPS4 (OPTION_MD_BASE + 4)
8842 {"mips4", no_argument, NULL, OPTION_MIPS4},
8843#define OPTION_MCPU (OPTION_MD_BASE + 5)
8844 {"mcpu", required_argument, NULL, OPTION_MCPU},
8845#define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 6)
8846 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
156c2f8b 8847#define OPTION_TRAP (OPTION_MD_BASE + 7)
252b5132
RH
8848 {"trap", no_argument, NULL, OPTION_TRAP},
8849 {"no-break", no_argument, NULL, OPTION_TRAP},
156c2f8b 8850#define OPTION_BREAK (OPTION_MD_BASE + 8)
252b5132
RH
8851 {"break", no_argument, NULL, OPTION_BREAK},
8852 {"no-trap", no_argument, NULL, OPTION_BREAK},
156c2f8b 8853#define OPTION_EB (OPTION_MD_BASE + 9)
252b5132 8854 {"EB", no_argument, NULL, OPTION_EB},
156c2f8b 8855#define OPTION_EL (OPTION_MD_BASE + 10)
252b5132 8856 {"EL", no_argument, NULL, OPTION_EL},
156c2f8b 8857#define OPTION_M4650 (OPTION_MD_BASE + 11)
252b5132 8858 {"m4650", no_argument, NULL, OPTION_M4650},
156c2f8b 8859#define OPTION_NO_M4650 (OPTION_MD_BASE + 12)
252b5132 8860 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
156c2f8b 8861#define OPTION_M4010 (OPTION_MD_BASE + 13)
252b5132 8862 {"m4010", no_argument, NULL, OPTION_M4010},
156c2f8b 8863#define OPTION_NO_M4010 (OPTION_MD_BASE + 14)
252b5132 8864 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
156c2f8b 8865#define OPTION_M4100 (OPTION_MD_BASE + 15)
252b5132 8866 {"m4100", no_argument, NULL, OPTION_M4100},
156c2f8b 8867#define OPTION_NO_M4100 (OPTION_MD_BASE + 16)
252b5132 8868 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
156c2f8b 8869#define OPTION_MIPS16 (OPTION_MD_BASE + 17)
252b5132 8870 {"mips16", no_argument, NULL, OPTION_MIPS16},
156c2f8b 8871#define OPTION_NO_MIPS16 (OPTION_MD_BASE + 18)
252b5132 8872 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
156c2f8b 8873#define OPTION_M3900 (OPTION_MD_BASE + 19)
252b5132 8874 {"m3900", no_argument, NULL, OPTION_M3900},
156c2f8b 8875#define OPTION_NO_M3900 (OPTION_MD_BASE + 20)
252b5132 8876 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
156c2f8b 8877#define OPTION_MABI (OPTION_MD_BASE + 21)
252b5132 8878 {"mabi", required_argument, NULL, OPTION_MABI},
156c2f8b 8879#define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 22)
6b76fefe 8880 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
156c2f8b 8881#define OPTION_NO_M7000_HILO_FIX (OPTION_MD_BASE + 23)
6b76fefe 8882 {"no-fix-7000", no_argument, NULL, OPTION_NO_M7000_HILO_FIX},
156c2f8b 8883#define OPTION_GP32 (OPTION_MD_BASE + 24)
c97ef257 8884 {"mgp32", no_argument, NULL, OPTION_GP32},
156c2f8b 8885#define OPTION_GP64 (OPTION_MD_BASE + 25)
c97ef257 8886 {"mgp64", no_argument, NULL, OPTION_GP64},
156c2f8b 8887#define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 26)
119d663a 8888 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
156c2f8b 8889#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 27)
119d663a 8890 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
156c2f8b
NC
8891#define OPTION_MIPS32 (OPTION_MD_BASE + 28)
8892 {"mips32", no_argument, NULL, OPTION_MIPS32},
84ea6cf2
NC
8893#define OPTION_MIPS5 (OPTION_MD_BASE + 29)
8894 {"mips5", no_argument, NULL, OPTION_MIPS5},
8895#define OPTION_MIPS64 (OPTION_MD_BASE + 30)
8896 {"mips64", no_argument, NULL, OPTION_MIPS64},
156c2f8b
NC
8897#ifdef OBJ_ELF
8898#define OPTION_ELF_BASE (OPTION_MD_BASE + 35)
8899#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
8900#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
8901#define OPTION_XGOT (OPTION_ELF_BASE + 2)
8902#define OPTION_32 (OPTION_ELF_BASE + 3)
8903#define OPTION_64 (OPTION_ELF_BASE + 4)
8904 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
8905 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
8906 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
8907 {"xgot", no_argument, NULL, OPTION_XGOT},
8908 {"32", no_argument, NULL, OPTION_32},
8909 {"64", no_argument, NULL, OPTION_64},
8910#endif
119d663a 8911
252b5132
RH
8912 {NULL, no_argument, NULL, 0}
8913};
156c2f8b 8914size_t md_longopts_size = sizeof (md_longopts);
252b5132
RH
8915
8916int
8917md_parse_option (c, arg)
8918 int c;
8919 char *arg;
8920{
8921 switch (c)
8922 {
119d663a
NC
8923 case OPTION_CONSTRUCT_FLOATS:
8924 mips_disable_float_construction = 0;
8925 break;
bdaaa2e1 8926
119d663a
NC
8927 case OPTION_NO_CONSTRUCT_FLOATS:
8928 mips_disable_float_construction = 1;
8929 break;
bdaaa2e1 8930
252b5132
RH
8931 case OPTION_TRAP:
8932 mips_trap = 1;
8933 break;
8934
8935 case OPTION_BREAK:
8936 mips_trap = 0;
8937 break;
8938
8939 case OPTION_EB:
8940 target_big_endian = 1;
8941 break;
8942
8943 case OPTION_EL:
8944 target_big_endian = 0;
8945 break;
8946
8947 case 'O':
8948 if (arg && arg[1] == '0')
8949 mips_optimize = 1;
8950 else
8951 mips_optimize = 2;
8952 break;
8953
8954 case 'g':
8955 if (arg == NULL)
8956 mips_debug = 2;
8957 else
8958 mips_debug = atoi (arg);
8959 /* When the MIPS assembler sees -g or -g2, it does not do
8960 optimizations which limit full symbolic debugging. We take
8961 that to be equivalent to -O0. */
8962 if (mips_debug == 2)
8963 mips_optimize = 1;
8964 break;
8965
8966 case OPTION_MIPS1:
e7af610e 8967 mips_opts.isa = ISA_MIPS1;
252b5132
RH
8968 break;
8969
8970 case OPTION_MIPS2:
e7af610e 8971 mips_opts.isa = ISA_MIPS2;
252b5132
RH
8972 break;
8973
8974 case OPTION_MIPS3:
e7af610e 8975 mips_opts.isa = ISA_MIPS3;
252b5132
RH
8976 break;
8977
8978 case OPTION_MIPS4:
e7af610e
NC
8979 mips_opts.isa = ISA_MIPS4;
8980 break;
8981
84ea6cf2
NC
8982 case OPTION_MIPS5:
8983 mips_opts.isa = ISA_MIPS5;
8984 break;
8985
e7af610e
NC
8986 case OPTION_MIPS32:
8987 mips_opts.isa = ISA_MIPS32;
252b5132
RH
8988 break;
8989
84ea6cf2
NC
8990 case OPTION_MIPS64:
8991 mips_opts.isa = ISA_MIPS64;
8992 break;
8993
252b5132
RH
8994 case OPTION_MCPU:
8995 {
e7af610e
NC
8996 /* Identify the processor type. */
8997 if (strcasecmp (arg, "default") == 0)
8998 mips_cpu = CPU_UNKNOWN;
252b5132
RH
8999 else
9000 {
e7af610e 9001 const struct mips_cpu_info *ci;
252b5132 9002
e7af610e
NC
9003 ci = mips_cpu_info_from_name (arg);
9004 if (ci == NULL || ci->is_isa)
beae10d5 9005 as_bad (_("invalid architecture -mcpu=%s"), arg);
e7af610e
NC
9006 else
9007 mips_cpu = ci->cpu;
252b5132
RH
9008 }
9009 }
9010 break;
9011
9012 case OPTION_M4650:
156c2f8b 9013 mips_cpu = CPU_R4650;
252b5132
RH
9014 break;
9015
9016 case OPTION_NO_M4650:
9017 break;
9018
9019 case OPTION_M4010:
156c2f8b 9020 mips_cpu = CPU_R4010;
252b5132
RH
9021 break;
9022
9023 case OPTION_NO_M4010:
9024 break;
9025
9026 case OPTION_M4100:
156c2f8b 9027 mips_cpu = CPU_VR4100;
252b5132
RH
9028 break;
9029
9030 case OPTION_NO_M4100:
9031 break;
9032
252b5132 9033 case OPTION_M3900:
156c2f8b 9034 mips_cpu = CPU_R3900;
252b5132 9035 break;
bdaaa2e1 9036
252b5132
RH
9037 case OPTION_NO_M3900:
9038 break;
9039
9040 case OPTION_MIPS16:
9041 mips_opts.mips16 = 1;
9042 mips_no_prev_insn (false);
9043 break;
9044
9045 case OPTION_NO_MIPS16:
9046 mips_opts.mips16 = 0;
9047 mips_no_prev_insn (false);
9048 break;
9049
9050 case OPTION_MEMBEDDED_PIC:
9051 mips_pic = EMBEDDED_PIC;
9052 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
9053 {
9054 as_bad (_("-G may not be used with embedded PIC code"));
9055 return 0;
9056 }
9057 g_switch_value = 0x7fffffff;
9058 break;
9059
0f074f60 9060#ifdef OBJ_ELF
252b5132
RH
9061 /* When generating ELF code, we permit -KPIC and -call_shared to
9062 select SVR4_PIC, and -non_shared to select no PIC. This is
9063 intended to be compatible with Irix 5. */
9064 case OPTION_CALL_SHARED:
9065 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9066 {
9067 as_bad (_("-call_shared is supported only for ELF format"));
9068 return 0;
9069 }
9070 mips_pic = SVR4_PIC;
9071 if (g_switch_seen && g_switch_value != 0)
9072 {
9073 as_bad (_("-G may not be used with SVR4 PIC code"));
9074 return 0;
9075 }
9076 g_switch_value = 0;
9077 break;
9078
9079 case OPTION_NON_SHARED:
9080 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9081 {
9082 as_bad (_("-non_shared is supported only for ELF format"));
9083 return 0;
9084 }
9085 mips_pic = NO_PIC;
9086 break;
9087
9088 /* The -xgot option tells the assembler to use 32 offsets when
9089 accessing the got in SVR4_PIC mode. It is for Irix
9090 compatibility. */
9091 case OPTION_XGOT:
9092 mips_big_got = 1;
9093 break;
0f074f60 9094#endif /* OBJ_ELF */
252b5132
RH
9095
9096 case 'G':
9097 if (! USE_GLOBAL_POINTER_OPT)
9098 {
9099 as_bad (_("-G is not supported for this configuration"));
9100 return 0;
9101 }
9102 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
9103 {
9104 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
9105 return 0;
9106 }
9107 else
9108 g_switch_value = atoi (arg);
9109 g_switch_seen = 1;
9110 break;
9111
0f074f60 9112#ifdef OBJ_ELF
252b5132
RH
9113 /* The -32 and -64 options tell the assembler to output the 32
9114 bit or the 64 bit MIPS ELF format. */
9115 case OPTION_32:
9116 mips_64 = 0;
9117 break;
9118
9119 case OPTION_64:
9120 {
9121 const char **list, **l;
9122
9123 list = bfd_target_list ();
9124 for (l = list; *l != NULL; l++)
9125 if (strcmp (*l, "elf64-bigmips") == 0
9126 || strcmp (*l, "elf64-littlemips") == 0)
9127 break;
9128 if (*l == NULL)
9129 as_fatal (_("No compiled in support for 64 bit object file format"));
9130 free (list);
9131 mips_64 = 1;
9132 }
9133 break;
0f074f60 9134#endif /* OBJ_ELF */
252b5132 9135
c97ef257
AH
9136 case OPTION_GP32:
9137 mips_gp32 = 1;
9138 mips_64 = 0;
9139
9140 /* We deliberately don't allow "-gp32" to set the MIPS_32BITMODE
9141 flag in object files because to do so would make it
9142 impossible to link with libraries compiled without "-gp32".
bdaaa2e1 9143 This is unnecessarily restrictive.
c97ef257
AH
9144
9145 We could solve this problem by adding "-gp32" multilibs to
9146 gcc, but to set this flag before gcc is built with such
bdaaa2e1 9147 multilibs will break too many systems. */
c97ef257 9148
beae10d5
KH
9149#if 0
9150 mips_32bitmode = 1;
9151#endif
c97ef257
AH
9152 break;
9153
9154 case OPTION_GP64:
9155 mips_gp32 = 0;
9156 mips_64 = 1;
beae10d5
KH
9157#if 0
9158 mips_32bitmode = 0;
9159#endif
c97ef257 9160 break;
252b5132
RH
9161
9162 case OPTION_MABI:
beae10d5
KH
9163 if (strcmp (arg, "32") == 0
9164 || strcmp (arg, "n32") == 0
9165 || strcmp (arg, "64") == 0
9166 || strcmp (arg, "o64") == 0
9167 || strcmp (arg, "eabi") == 0)
252b5132
RH
9168 mips_abi_string = arg;
9169 break;
9170
6b76fefe
CM
9171 case OPTION_M7000_HILO_FIX:
9172 mips_7000_hilo_fix = true;
9173 break;
9174
9175 case OPTION_NO_M7000_HILO_FIX:
9176 mips_7000_hilo_fix = false;
9177 break;
9178
252b5132
RH
9179 default:
9180 return 0;
9181 }
9182
9183 return 1;
9184}
9185
252b5132
RH
9186static void
9187show (stream, string, col_p, first_p)
9188 FILE *stream;
9189 char *string;
9190 int *col_p;
9191 int *first_p;
9192{
9193 if (*first_p)
9194 {
9195 fprintf (stream, "%24s", "");
9196 *col_p = 24;
9197 }
9198 else
9199 {
9200 fprintf (stream, ", ");
9201 *col_p += 2;
9202 }
9203
9204 if (*col_p + strlen (string) > 72)
9205 {
9206 fprintf (stream, "\n%24s", "");
9207 *col_p = 24;
9208 }
9209
9210 fprintf (stream, "%s", string);
9211 *col_p += strlen (string);
9212
9213 *first_p = 0;
9214}
9215
252b5132
RH
9216void
9217md_show_usage (stream)
9218 FILE *stream;
9219{
9220 int column, first;
9221
beae10d5 9222 fprintf (stream, _("\
252b5132
RH
9223MIPS options:\n\
9224-membedded-pic generate embedded position independent code\n\
9225-EB generate big endian output\n\
9226-EL generate little endian output\n\
9227-g, -g2 do not remove uneeded NOPs or swap branches\n\
9228-G NUM allow referencing objects up to NUM bytes\n\
9229 implicitly with the gp register [default 8]\n"));
beae10d5 9230 fprintf (stream, _("\
252b5132
RH
9231-mips1 generate MIPS ISA I instructions\n\
9232-mips2 generate MIPS ISA II instructions\n\
9233-mips3 generate MIPS ISA III instructions\n\
9234-mips4 generate MIPS ISA IV instructions\n\
84ea6cf2 9235-mips5 generate MIPS ISA V instructions\n\
e7af610e 9236-mips32 generate MIPS32 ISA instructions\n\
84ea6cf2 9237-mips64 generate MIPS64 ISA instructions\n\
252b5132
RH
9238-mcpu=CPU generate code for CPU, where CPU is one of:\n"));
9239
9240 first = 1;
9241
9242 show (stream, "2000", &column, &first);
9243 show (stream, "3000", &column, &first);
9244 show (stream, "3900", &column, &first);
9245 show (stream, "4000", &column, &first);
9246 show (stream, "4010", &column, &first);
9247 show (stream, "4100", &column, &first);
9248 show (stream, "4111", &column, &first);
9249 show (stream, "4300", &column, &first);
9250 show (stream, "4400", &column, &first);
9251 show (stream, "4600", &column, &first);
9252 show (stream, "4650", &column, &first);
9253 show (stream, "5000", &column, &first);
9254 show (stream, "6000", &column, &first);
9255 show (stream, "8000", &column, &first);
9256 show (stream, "10000", &column, &first);
e7af610e 9257 show (stream, "mips32-4k", &column, &first);
c6c98b38 9258 show (stream, "sb-1", &column, &first);
252b5132
RH
9259 fputc ('\n', stream);
9260
9261 fprintf (stream, _("\
9262-mCPU equivalent to -mcpu=CPU.\n\
9263-no-mCPU don't generate code specific to CPU.\n\
9264 For -mCPU and -no-mCPU, CPU must be one of:\n"));
9265
9266 first = 1;
9267
9268 show (stream, "3900", &column, &first);
9269 show (stream, "4010", &column, &first);
9270 show (stream, "4100", &column, &first);
9271 show (stream, "4650", &column, &first);
9272 fputc ('\n', stream);
9273
beae10d5 9274 fprintf (stream, _("\
252b5132
RH
9275-mips16 generate mips16 instructions\n\
9276-no-mips16 do not generate mips16 instructions\n"));
beae10d5 9277 fprintf (stream, _("\
252b5132
RH
9278-O0 remove unneeded NOPs, do not swap branches\n\
9279-O remove unneeded NOPs and swap branches\n\
119d663a 9280--[no-]construct-floats [dis]allow floating point values to be constructed\n\
252b5132
RH
9281--trap, --no-break trap exception on div by 0 and mult overflow\n\
9282--break, --no-trap break exception on div by 0 and mult overflow\n"));
9283#ifdef OBJ_ELF
beae10d5 9284 fprintf (stream, _("\
252b5132
RH
9285-KPIC, -call_shared generate SVR4 position independent code\n\
9286-non_shared do not generate position independent code\n\
9287-xgot assume a 32 bit GOT\n\
9288-32 create 32 bit object file (default)\n\
9289-64 create 64 bit object file\n"));
9290#endif
9291}
9292\f
9293void
9294mips_init_after_args ()
9295{
9296 /* initialize opcodes */
9297 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 9298 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
9299}
9300
9301long
9302md_pcrel_from (fixP)
9303 fixS *fixP;
9304{
9305 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
9306 && fixP->fx_addsy != (symbolS *) NULL
9307 && ! S_IS_DEFINED (fixP->fx_addsy))
9308 {
9309 /* This makes a branch to an undefined symbol be a branch to the
9310 current location. */
9311 return 4;
9312 }
9313
9314 /* return the address of the delay slot */
9315 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
9316}
9317
9318/* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
9319 reloc for a cons. We could use the definition there, except that
9320 we want to handle 64 bit relocs specially. */
9321
9322void
9323cons_fix_new_mips (frag, where, nbytes, exp)
43841e91 9324 fragS *frag ATTRIBUTE_UNUSED;
252b5132
RH
9325 int where;
9326 unsigned int nbytes;
9327 expressionS *exp;
9328{
9329#ifndef OBJ_ELF
9330 /* If we are assembling in 32 bit mode, turn an 8 byte reloc into a
9331 4 byte reloc. */
9332 if (nbytes == 8 && ! mips_64)
9333 {
9334 if (target_big_endian)
9335 where += 4;
9336 nbytes = 4;
9337 }
9338#endif
9339
9340 if (nbytes != 2 && nbytes != 4 && nbytes != 8)
9341 as_bad (_("Unsupported reloc size %d"), nbytes);
9342
9343 fix_new_exp (frag_now, where, (int) nbytes, exp, 0,
9344 (nbytes == 2
9345 ? BFD_RELOC_16
9346 : (nbytes == 4 ? BFD_RELOC_32 : BFD_RELOC_64)));
9347}
9348
9349/* This is called before the symbol table is processed. In order to
9350 work with gcc when using mips-tfile, we must keep all local labels.
9351 However, in other cases, we want to discard them. If we were
9352 called with -g, but we didn't see any debugging information, it may
9353 mean that gcc is smuggling debugging information through to
9354 mips-tfile, in which case we must generate all local labels. */
9355
9356void
9357mips_frob_file_before_adjust ()
9358{
9359#ifndef NO_ECOFF_DEBUGGING
9360 if (ECOFF_DEBUGGING
9361 && mips_debug != 0
9362 && ! ecoff_debugging_seen)
9363 flag_keep_locals = 1;
9364#endif
9365}
9366
9367/* Sort any unmatched HI16_S relocs so that they immediately precede
9368 the corresponding LO reloc. This is called before md_apply_fix and
9369 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
9370 explicit use of the %hi modifier. */
9371
9372void
9373mips_frob_file ()
9374{
9375 struct mips_hi_fixup *l;
9376
9377 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
9378 {
9379 segment_info_type *seginfo;
9380 int pass;
9381
9382 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
9383
9384 /* Check quickly whether the next fixup happens to be a matching
9385 %lo. */
9386 if (l->fixp->fx_next != NULL
9387 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
9388 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
9389 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
9390 continue;
9391
9392 /* Look through the fixups for this segment for a matching %lo.
9393 When we find one, move the %hi just in front of it. We do
9394 this in two passes. In the first pass, we try to find a
9395 unique %lo. In the second pass, we permit multiple %hi
9396 relocs for a single %lo (this is a GNU extension). */
9397 seginfo = seg_info (l->seg);
9398 for (pass = 0; pass < 2; pass++)
9399 {
9400 fixS *f, *prev;
9401
9402 prev = NULL;
9403 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
9404 {
9405 /* Check whether this is a %lo fixup which matches l->fixp. */
9406 if (f->fx_r_type == BFD_RELOC_LO16
9407 && f->fx_addsy == l->fixp->fx_addsy
9408 && f->fx_offset == l->fixp->fx_offset
9409 && (pass == 1
9410 || prev == NULL
9411 || prev->fx_r_type != BFD_RELOC_HI16_S
9412 || prev->fx_addsy != f->fx_addsy
9413 || prev->fx_offset != f->fx_offset))
9414 {
9415 fixS **pf;
9416
9417 /* Move l->fixp before f. */
9418 for (pf = &seginfo->fix_root;
9419 *pf != l->fixp;
9420 pf = &(*pf)->fx_next)
9421 assert (*pf != NULL);
9422
9423 *pf = l->fixp->fx_next;
9424
9425 l->fixp->fx_next = f;
9426 if (prev == NULL)
9427 seginfo->fix_root = l->fixp;
9428 else
9429 prev->fx_next = l->fixp;
9430
9431 break;
9432 }
9433
9434 prev = f;
9435 }
9436
9437 if (f != NULL)
9438 break;
9439
9440#if 0 /* GCC code motion plus incomplete dead code elimination
9441 can leave a %hi without a %lo. */
9442 if (pass == 1)
9443 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
9444 _("Unmatched %%hi reloc"));
9445#endif
9446 }
9447 }
9448}
9449
9450/* When generating embedded PIC code we need to use a special
9451 relocation to represent the difference of two symbols in the .text
9452 section (switch tables use a difference of this sort). See
9453 include/coff/mips.h for details. This macro checks whether this
9454 fixup requires the special reloc. */
9455#define SWITCH_TABLE(fixp) \
9456 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 9457 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
9458 && (fixp)->fx_addsy != NULL \
9459 && (fixp)->fx_subsy != NULL \
9460 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
9461 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
9462
9463/* When generating embedded PIC code we must keep all PC relative
9464 relocations, in case the linker has to relax a call. We also need
9465 to keep relocations for switch table entries. */
9466
252b5132
RH
9467int
9468mips_force_relocation (fixp)
9469 fixS *fixp;
9470{
9471 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
9472 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
9473 return 1;
9474
9475 return (mips_pic == EMBEDDED_PIC
9476 && (fixp->fx_pcrel
9477 || SWITCH_TABLE (fixp)
9478 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
9479 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
9480}
9481
9482/* Apply a fixup to the object file. */
9483
9484int
9485md_apply_fix (fixP, valueP)
9486 fixS *fixP;
9487 valueT *valueP;
9488{
9489 unsigned char *buf;
9490 long insn, value;
9491
9492 assert (fixP->fx_size == 4
9493 || fixP->fx_r_type == BFD_RELOC_16
9494 || fixP->fx_r_type == BFD_RELOC_64
9495 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
9496 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
9497
9498 value = *valueP;
9499
9500 /* If we aren't adjusting this fixup to be against the section
9501 symbol, we need to adjust the value. */
9502#ifdef OBJ_ELF
9503 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 9504 {
bdaaa2e1 9505 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16
252b5132 9506 || S_IS_WEAK (fixP->fx_addsy)
49309057
ILT
9507 || (symbol_used_in_reloc_p (fixP->fx_addsy)
9508 && (((bfd_get_section_flags (stdoutput,
bb2d6cd7
GK
9509 S_GET_SEGMENT (fixP->fx_addsy))
9510 & SEC_LINK_ONCE) != 0)
9511 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
9512 ".gnu.linkonce",
9513 sizeof (".gnu.linkonce") - 1))))
252b5132
RH
9514
9515 {
9516 value -= S_GET_VALUE (fixP->fx_addsy);
9517 if (value != 0 && ! fixP->fx_pcrel)
9518 {
9519 /* In this case, the bfd_install_relocation routine will
9520 incorrectly add the symbol value back in. We just want
bdaaa2e1 9521 the addend to appear in the object file.
bb2d6cd7 9522 FIXME: If this makes VALUE zero, we're toast. */
252b5132
RH
9523 value -= S_GET_VALUE (fixP->fx_addsy);
9524 }
9525 }
252b5132 9526
bb2d6cd7
GK
9527 /* This code was generated using trial and error and so is
9528 fragile and not trustworthy. If you change it, you should
9529 rerun the elf-rel, elf-rel2, and empic testcases and ensure
9530 they still pass. */
9531 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
9532 {
9533 value += fixP->fx_frag->fr_address + fixP->fx_where;
9534
9535 /* BFD's REL handling, for MIPS, is _very_ weird.
9536 This gives the right results, but it can't possibly
9537 be the way things are supposed to work. */
9538 if (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
9539 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
9540 value += fixP->fx_frag->fr_address + fixP->fx_where;
9541 }
9542 }
9543#endif
252b5132
RH
9544
9545 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc */
9546
9547 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
9548 fixP->fx_done = 1;
9549
9550 switch (fixP->fx_r_type)
9551 {
9552 case BFD_RELOC_MIPS_JMP:
9553 case BFD_RELOC_HI16:
9554 case BFD_RELOC_HI16_S:
9555 case BFD_RELOC_MIPS_GPREL:
9556 case BFD_RELOC_MIPS_LITERAL:
9557 case BFD_RELOC_MIPS_CALL16:
9558 case BFD_RELOC_MIPS_GOT16:
9559 case BFD_RELOC_MIPS_GPREL32:
9560 case BFD_RELOC_MIPS_GOT_HI16:
9561 case BFD_RELOC_MIPS_GOT_LO16:
9562 case BFD_RELOC_MIPS_CALL_HI16:
9563 case BFD_RELOC_MIPS_CALL_LO16:
9564 case BFD_RELOC_MIPS16_GPREL:
9565 if (fixP->fx_pcrel)
9566 as_bad_where (fixP->fx_file, fixP->fx_line,
9567 _("Invalid PC relative reloc"));
9568 /* Nothing needed to do. The value comes from the reloc entry */
9569 break;
9570
9571 case BFD_RELOC_MIPS16_JMP:
9572 /* We currently always generate a reloc against a symbol, which
9573 means that we don't want an addend even if the symbol is
9574 defined. */
9575 fixP->fx_addnumber = 0;
9576 break;
9577
9578 case BFD_RELOC_PCREL_HI16_S:
9579 /* The addend for this is tricky if it is internal, so we just
9580 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 9581 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
9582 && !fixP->fx_done
9583 && value != 0)
9584 break;
9585 if (fixP->fx_addsy
9586 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
9587 {
9588 /* For an external symbol adjust by the address to make it
9589 pcrel_offset. We use the address of the RELLO reloc
9590 which follows this one. */
9591 value += (fixP->fx_next->fx_frag->fr_address
9592 + fixP->fx_next->fx_where);
9593 }
9594 if (value & 0x8000)
9595 value += 0x10000;
9596 value >>= 16;
9597 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9598 if (target_big_endian)
9599 buf += 2;
9600 md_number_to_chars (buf, value, 2);
9601 break;
9602
9603 case BFD_RELOC_PCREL_LO16:
9604 /* The addend for this is tricky if it is internal, so we just
9605 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 9606 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
9607 && !fixP->fx_done
9608 && value != 0)
9609 break;
9610 if (fixP->fx_addsy
9611 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
9612 value += fixP->fx_frag->fr_address + fixP->fx_where;
9613 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9614 if (target_big_endian)
9615 buf += 2;
9616 md_number_to_chars (buf, value, 2);
9617 break;
9618
9619 case BFD_RELOC_64:
9620 /* This is handled like BFD_RELOC_32, but we output a sign
9621 extended value if we are only 32 bits. */
9622 if (fixP->fx_done
9623 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
9624 {
9625 if (8 <= sizeof (valueT))
9626 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9627 value, 8);
9628 else
9629 {
9630 long w1, w2;
9631 long hiv;
9632
9633 w1 = w2 = fixP->fx_where;
9634 if (target_big_endian)
9635 w1 += 4;
9636 else
9637 w2 += 4;
9638 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
9639 if ((value & 0x80000000) != 0)
9640 hiv = 0xffffffff;
9641 else
9642 hiv = 0;
9643 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
9644 }
9645 }
9646 break;
9647
056350c6 9648 case BFD_RELOC_RVA:
252b5132
RH
9649 case BFD_RELOC_32:
9650 /* If we are deleting this reloc entry, we must fill in the
9651 value now. This can happen if we have a .word which is not
9652 resolved when it appears but is later defined. We also need
9653 to fill in the value if this is an embedded PIC switch table
9654 entry. */
9655 if (fixP->fx_done
9656 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
9657 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9658 value, 4);
9659 break;
9660
9661 case BFD_RELOC_16:
9662 /* If we are deleting this reloc entry, we must fill in the
9663 value now. */
9664 assert (fixP->fx_size == 2);
9665 if (fixP->fx_done)
9666 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9667 value, 2);
9668 break;
9669
9670 case BFD_RELOC_LO16:
9671 /* When handling an embedded PIC switch statement, we can wind
9672 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
9673 if (fixP->fx_done)
9674 {
9675 if (value < -0x8000 || value > 0x7fff)
9676 as_bad_where (fixP->fx_file, fixP->fx_line,
9677 _("relocation overflow"));
9678 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9679 if (target_big_endian)
9680 buf += 2;
9681 md_number_to_chars (buf, value, 2);
9682 }
9683 break;
9684
9685 case BFD_RELOC_16_PCREL_S2:
9686 /*
9687 * We need to save the bits in the instruction since fixup_segment()
9688 * might be deleting the relocation entry (i.e., a branch within
9689 * the current segment).
9690 */
9691 if ((value & 0x3) != 0)
9692 as_bad_where (fixP->fx_file, fixP->fx_line,
9693 _("Branch to odd address (%lx)"), value);
bb2d6cd7
GK
9694
9695 if (!fixP->fx_done && value != 0)
9696 break;
9697 /* If 'value' is zero, the remaining reloc code won't actually
9698 do the store, so it must be done here. This is probably
9699 a bug somewhere. */
9700 if (!fixP->fx_done)
9701 value -= fixP->fx_frag->fr_address + fixP->fx_where;
bdaaa2e1 9702
252b5132
RH
9703 value >>= 2;
9704
9705 /* update old instruction data */
9706 buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
9707 if (target_big_endian)
9708 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
9709 else
9710 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
9711
9712 if (value >= -0x8000 && value < 0x8000)
9713 insn |= value & 0xffff;
9714 else
9715 {
9716 /* The branch offset is too large. If this is an
9717 unconditional branch, and we are not generating PIC code,
9718 we can convert it to an absolute jump instruction. */
9719 if (mips_pic == NO_PIC
9720 && fixP->fx_done
9721 && fixP->fx_frag->fr_address >= text_section->vma
9722 && (fixP->fx_frag->fr_address
9723 < text_section->vma + text_section->_raw_size)
9724 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
9725 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
9726 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
9727 {
9728 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
9729 insn = 0x0c000000; /* jal */
9730 else
9731 insn = 0x08000000; /* j */
9732 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
9733 fixP->fx_done = 0;
9734 fixP->fx_addsy = section_symbol (text_section);
9735 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
9736 }
9737 else
9738 {
9739 /* FIXME. It would be possible in principle to handle
9740 conditional branches which overflow. They could be
9741 transformed into a branch around a jump. This would
9742 require setting up variant frags for each different
9743 branch type. The native MIPS assembler attempts to
9744 handle these cases, but it appears to do it
9745 incorrectly. */
9746 as_bad_where (fixP->fx_file, fixP->fx_line,
9747 _("Branch out of range"));
9748 }
9749 }
9750
9751 md_number_to_chars ((char *) buf, (valueT) insn, 4);
9752 break;
9753
9754 case BFD_RELOC_VTABLE_INHERIT:
9755 fixP->fx_done = 0;
9756 if (fixP->fx_addsy
9757 && !S_IS_DEFINED (fixP->fx_addsy)
9758 && !S_IS_WEAK (fixP->fx_addsy))
9759 S_SET_WEAK (fixP->fx_addsy);
9760 break;
9761
9762 case BFD_RELOC_VTABLE_ENTRY:
9763 fixP->fx_done = 0;
9764 break;
9765
9766 default:
9767 internalError ();
9768 }
9769
9770 return 1;
9771}
9772
9773#if 0
9774void
9775printInsn (oc)
9776 unsigned long oc;
9777{
9778 const struct mips_opcode *p;
9779 int treg, sreg, dreg, shamt;
9780 short imm;
9781 const char *args;
9782 int i;
9783
9784 for (i = 0; i < NUMOPCODES; ++i)
9785 {
9786 p = &mips_opcodes[i];
9787 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
9788 {
9789 printf ("%08lx %s\t", oc, p->name);
9790 treg = (oc >> 16) & 0x1f;
9791 sreg = (oc >> 21) & 0x1f;
9792 dreg = (oc >> 11) & 0x1f;
9793 shamt = (oc >> 6) & 0x1f;
9794 imm = oc;
9795 for (args = p->args;; ++args)
9796 {
9797 switch (*args)
9798 {
9799 case '\0':
9800 printf ("\n");
9801 break;
9802
9803 case ',':
9804 case '(':
9805 case ')':
9806 printf ("%c", *args);
9807 continue;
9808
9809 case 'r':
9810 assert (treg == sreg);
9811 printf ("$%d,$%d", treg, sreg);
9812 continue;
9813
9814 case 'd':
9815 case 'G':
9816 printf ("$%d", dreg);
9817 continue;
9818
9819 case 't':
9820 case 'E':
9821 printf ("$%d", treg);
9822 continue;
9823
9824 case 'k':
9825 printf ("0x%x", treg);
9826 continue;
9827
9828 case 'b':
9829 case 's':
9830 printf ("$%d", sreg);
9831 continue;
9832
9833 case 'a':
9834 printf ("0x%08lx", oc & 0x1ffffff);
9835 continue;
9836
9837 case 'i':
9838 case 'j':
9839 case 'o':
9840 case 'u':
9841 printf ("%d", imm);
9842 continue;
9843
9844 case '<':
9845 case '>':
9846 printf ("$%d", shamt);
9847 continue;
9848
9849 default:
9850 internalError ();
9851 }
9852 break;
9853 }
9854 return;
9855 }
9856 }
9857 printf (_("%08lx UNDEFINED\n"), oc);
9858}
9859#endif
9860
9861static symbolS *
9862get_symbol ()
9863{
9864 int c;
9865 char *name;
9866 symbolS *p;
9867
9868 name = input_line_pointer;
9869 c = get_symbol_end ();
9870 p = (symbolS *) symbol_find_or_make (name);
9871 *input_line_pointer = c;
9872 return p;
9873}
9874
9875/* Align the current frag to a given power of two. The MIPS assembler
9876 also automatically adjusts any preceding label. */
9877
9878static void
9879mips_align (to, fill, label)
9880 int to;
9881 int fill;
9882 symbolS *label;
9883{
9884 mips_emit_delays (false);
9885 frag_align (to, fill, 0);
9886 record_alignment (now_seg, to);
9887 if (label != NULL)
9888 {
9889 assert (S_GET_SEGMENT (label) == now_seg);
49309057 9890 symbol_set_frag (label, frag_now);
252b5132
RH
9891 S_SET_VALUE (label, (valueT) frag_now_fix ());
9892 }
9893}
9894
9895/* Align to a given power of two. .align 0 turns off the automatic
9896 alignment used by the data creating pseudo-ops. */
9897
9898static void
9899s_align (x)
43841e91 9900 int x ATTRIBUTE_UNUSED;
252b5132
RH
9901{
9902 register int temp;
9903 register long temp_fill;
9904 long max_alignment = 15;
9905
9906 /*
9907
9908 o Note that the assembler pulls down any immediately preceeding label
9909 to the aligned address.
9910 o It's not documented but auto alignment is reinstated by
9911 a .align pseudo instruction.
9912 o Note also that after auto alignment is turned off the mips assembler
9913 issues an error on attempt to assemble an improperly aligned data item.
9914 We don't.
9915
9916 */
9917
9918 temp = get_absolute_expression ();
9919 if (temp > max_alignment)
9920 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
9921 else if (temp < 0)
9922 {
9923 as_warn (_("Alignment negative: 0 assumed."));
9924 temp = 0;
9925 }
9926 if (*input_line_pointer == ',')
9927 {
9928 input_line_pointer++;
9929 temp_fill = get_absolute_expression ();
9930 }
9931 else
9932 temp_fill = 0;
9933 if (temp)
9934 {
9935 auto_align = 1;
9936 mips_align (temp, (int) temp_fill,
9937 insn_labels != NULL ? insn_labels->label : NULL);
9938 }
9939 else
9940 {
9941 auto_align = 0;
9942 }
9943
9944 demand_empty_rest_of_line ();
9945}
9946
9947void
9948mips_flush_pending_output ()
9949{
9950 mips_emit_delays (false);
9951 mips_clear_insn_labels ();
9952}
9953
9954static void
9955s_change_sec (sec)
9956 int sec;
9957{
9958 segT seg;
9959
9960 /* When generating embedded PIC code, we only use the .text, .lit8,
9961 .sdata and .sbss sections. We change the .data and .rdata
9962 pseudo-ops to use .sdata. */
9963 if (mips_pic == EMBEDDED_PIC
9964 && (sec == 'd' || sec == 'r'))
9965 sec = 's';
9966
9967#ifdef OBJ_ELF
9968 /* The ELF backend needs to know that we are changing sections, so
9969 that .previous works correctly. We could do something like check
9970 for a obj_section_change_hook macro, but that might be confusing
9971 as it would not be appropriate to use it in the section changing
9972 functions in read.c, since obj-elf.c intercepts those. FIXME:
9973 This should be cleaner, somehow. */
9974 obj_elf_section_change_hook ();
9975#endif
9976
9977 mips_emit_delays (false);
9978 switch (sec)
9979 {
9980 case 't':
9981 s_text (0);
9982 break;
9983 case 'd':
9984 s_data (0);
9985 break;
9986 case 'b':
9987 subseg_set (bss_section, (subsegT) get_absolute_expression ());
9988 demand_empty_rest_of_line ();
9989 break;
9990
9991 case 'r':
9992 if (USE_GLOBAL_POINTER_OPT)
9993 {
9994 seg = subseg_new (RDATA_SECTION_NAME,
9995 (subsegT) get_absolute_expression ());
9996 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9997 {
9998 bfd_set_section_flags (stdoutput, seg,
9999 (SEC_ALLOC
10000 | SEC_LOAD
10001 | SEC_READONLY
10002 | SEC_RELOC
10003 | SEC_DATA));
10004 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 10005 record_alignment (seg, 4);
252b5132
RH
10006 }
10007 demand_empty_rest_of_line ();
10008 }
10009 else
10010 {
10011 as_bad (_("No read only data section in this object file format"));
10012 demand_empty_rest_of_line ();
10013 return;
10014 }
10015 break;
10016
10017 case 's':
10018 if (USE_GLOBAL_POINTER_OPT)
10019 {
10020 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
10021 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
10022 {
10023 bfd_set_section_flags (stdoutput, seg,
10024 SEC_ALLOC | SEC_LOAD | SEC_RELOC
10025 | SEC_DATA);
10026 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 10027 record_alignment (seg, 4);
252b5132
RH
10028 }
10029 demand_empty_rest_of_line ();
10030 break;
10031 }
10032 else
10033 {
10034 as_bad (_("Global pointers not supported; recompile -G 0"));
10035 demand_empty_rest_of_line ();
10036 return;
10037 }
10038 }
10039
10040 auto_align = 1;
10041}
10042
10043void
10044mips_enable_auto_align ()
10045{
10046 auto_align = 1;
10047}
10048
10049static void
10050s_cons (log_size)
10051 int log_size;
10052{
10053 symbolS *label;
10054
10055 label = insn_labels != NULL ? insn_labels->label : NULL;
10056 mips_emit_delays (false);
10057 if (log_size > 0 && auto_align)
10058 mips_align (log_size, 0, label);
10059 mips_clear_insn_labels ();
10060 cons (1 << log_size);
10061}
10062
10063static void
10064s_float_cons (type)
10065 int type;
10066{
10067 symbolS *label;
10068
10069 label = insn_labels != NULL ? insn_labels->label : NULL;
10070
10071 mips_emit_delays (false);
10072
10073 if (auto_align)
49309057
ILT
10074 {
10075 if (type == 'd')
10076 mips_align (3, 0, label);
10077 else
10078 mips_align (2, 0, label);
10079 }
252b5132
RH
10080
10081 mips_clear_insn_labels ();
10082
10083 float_cons (type);
10084}
10085
10086/* Handle .globl. We need to override it because on Irix 5 you are
10087 permitted to say
10088 .globl foo .text
10089 where foo is an undefined symbol, to mean that foo should be
10090 considered to be the address of a function. */
10091
10092static void
10093s_mips_globl (x)
43841e91 10094 int x ATTRIBUTE_UNUSED;
252b5132
RH
10095{
10096 char *name;
10097 int c;
10098 symbolS *symbolP;
10099 flagword flag;
10100
10101 name = input_line_pointer;
10102 c = get_symbol_end ();
10103 symbolP = symbol_find_or_make (name);
10104 *input_line_pointer = c;
10105 SKIP_WHITESPACE ();
10106
10107 /* On Irix 5, every global symbol that is not explicitly labelled as
10108 being a function is apparently labelled as being an object. */
10109 flag = BSF_OBJECT;
10110
10111 if (! is_end_of_line[(unsigned char) *input_line_pointer])
10112 {
10113 char *secname;
10114 asection *sec;
10115
10116 secname = input_line_pointer;
10117 c = get_symbol_end ();
10118 sec = bfd_get_section_by_name (stdoutput, secname);
10119 if (sec == NULL)
10120 as_bad (_("%s: no such section"), secname);
10121 *input_line_pointer = c;
10122
10123 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
10124 flag = BSF_FUNCTION;
10125 }
10126
49309057 10127 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
10128
10129 S_SET_EXTERNAL (symbolP);
10130 demand_empty_rest_of_line ();
10131}
10132
10133static void
10134s_option (x)
43841e91 10135 int x ATTRIBUTE_UNUSED;
252b5132
RH
10136{
10137 char *opt;
10138 char c;
10139
10140 opt = input_line_pointer;
10141 c = get_symbol_end ();
10142
10143 if (*opt == 'O')
10144 {
10145 /* FIXME: What does this mean? */
10146 }
10147 else if (strncmp (opt, "pic", 3) == 0)
10148 {
10149 int i;
10150
10151 i = atoi (opt + 3);
10152 if (i == 0)
10153 mips_pic = NO_PIC;
10154 else if (i == 2)
10155 mips_pic = SVR4_PIC;
10156 else
10157 as_bad (_(".option pic%d not supported"), i);
10158
10159 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
10160 {
10161 if (g_switch_seen && g_switch_value != 0)
10162 as_warn (_("-G may not be used with SVR4 PIC code"));
10163 g_switch_value = 0;
10164 bfd_set_gp_size (stdoutput, 0);
10165 }
10166 }
10167 else
10168 as_warn (_("Unrecognized option \"%s\""), opt);
10169
10170 *input_line_pointer = c;
10171 demand_empty_rest_of_line ();
10172}
10173
10174/* This structure is used to hold a stack of .set values. */
10175
e972090a
NC
10176struct mips_option_stack
10177{
252b5132
RH
10178 struct mips_option_stack *next;
10179 struct mips_set_options options;
10180};
10181
10182static struct mips_option_stack *mips_opts_stack;
10183
10184/* Handle the .set pseudo-op. */
10185
10186static void
10187s_mipsset (x)
43841e91 10188 int x ATTRIBUTE_UNUSED;
252b5132
RH
10189{
10190 char *name = input_line_pointer, ch;
10191
10192 while (!is_end_of_line[(unsigned char) *input_line_pointer])
10193 input_line_pointer++;
10194 ch = *input_line_pointer;
10195 *input_line_pointer = '\0';
10196
10197 if (strcmp (name, "reorder") == 0)
10198 {
10199 if (mips_opts.noreorder && prev_nop_frag != NULL)
10200 {
10201 /* If we still have pending nops, we can discard them. The
10202 usual nop handling will insert any that are still
bdaaa2e1 10203 needed. */
252b5132
RH
10204 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
10205 * (mips_opts.mips16 ? 2 : 4));
10206 prev_nop_frag = NULL;
10207 }
10208 mips_opts.noreorder = 0;
10209 }
10210 else if (strcmp (name, "noreorder") == 0)
10211 {
10212 mips_emit_delays (true);
10213 mips_opts.noreorder = 1;
10214 mips_any_noreorder = 1;
10215 }
10216 else if (strcmp (name, "at") == 0)
10217 {
10218 mips_opts.noat = 0;
10219 }
10220 else if (strcmp (name, "noat") == 0)
10221 {
10222 mips_opts.noat = 1;
10223 }
10224 else if (strcmp (name, "macro") == 0)
10225 {
10226 mips_opts.warn_about_macros = 0;
10227 }
10228 else if (strcmp (name, "nomacro") == 0)
10229 {
10230 if (mips_opts.noreorder == 0)
10231 as_bad (_("`noreorder' must be set before `nomacro'"));
10232 mips_opts.warn_about_macros = 1;
10233 }
10234 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
10235 {
10236 mips_opts.nomove = 0;
10237 }
10238 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
10239 {
10240 mips_opts.nomove = 1;
10241 }
10242 else if (strcmp (name, "bopt") == 0)
10243 {
10244 mips_opts.nobopt = 0;
10245 }
10246 else if (strcmp (name, "nobopt") == 0)
10247 {
10248 mips_opts.nobopt = 1;
10249 }
10250 else if (strcmp (name, "mips16") == 0
10251 || strcmp (name, "MIPS-16") == 0)
10252 mips_opts.mips16 = 1;
10253 else if (strcmp (name, "nomips16") == 0
10254 || strcmp (name, "noMIPS-16") == 0)
10255 mips_opts.mips16 = 0;
10256 else if (strncmp (name, "mips", 4) == 0)
10257 {
10258 int isa;
10259
10260 /* Permit the user to change the ISA on the fly. Needless to
10261 say, misuse can cause serious problems. */
10262 isa = atoi (name + 4);
e7af610e
NC
10263 switch (isa)
10264 {
84ea6cf2
NC
10265 case 0: mips_opts.isa = file_mips_isa; break;
10266 case 1: mips_opts.isa = ISA_MIPS1; break;
10267 case 2: mips_opts.isa = ISA_MIPS2; break;
10268 case 3: mips_opts.isa = ISA_MIPS3; break;
10269 case 5: mips_opts.isa = ISA_MIPS5; break;
10270 case 4: mips_opts.isa = ISA_MIPS4; break;
10271 case 32: mips_opts.isa = ISA_MIPS32; break;
10272 case 64: mips_opts.isa = ISA_MIPS64; break;
10273 default: as_bad (_("unknown ISA level")); break;
e7af610e 10274 }
252b5132
RH
10275 }
10276 else if (strcmp (name, "autoextend") == 0)
10277 mips_opts.noautoextend = 0;
10278 else if (strcmp (name, "noautoextend") == 0)
10279 mips_opts.noautoextend = 1;
10280 else if (strcmp (name, "push") == 0)
10281 {
10282 struct mips_option_stack *s;
10283
10284 s = (struct mips_option_stack *) xmalloc (sizeof *s);
10285 s->next = mips_opts_stack;
10286 s->options = mips_opts;
10287 mips_opts_stack = s;
10288 }
10289 else if (strcmp (name, "pop") == 0)
10290 {
10291 struct mips_option_stack *s;
10292
10293 s = mips_opts_stack;
10294 if (s == NULL)
10295 as_bad (_(".set pop with no .set push"));
10296 else
10297 {
10298 /* If we're changing the reorder mode we need to handle
10299 delay slots correctly. */
10300 if (s->options.noreorder && ! mips_opts.noreorder)
10301 mips_emit_delays (true);
10302 else if (! s->options.noreorder && mips_opts.noreorder)
10303 {
10304 if (prev_nop_frag != NULL)
10305 {
10306 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
10307 * (mips_opts.mips16 ? 2 : 4));
10308 prev_nop_frag = NULL;
10309 }
10310 }
10311
10312 mips_opts = s->options;
10313 mips_opts_stack = s->next;
10314 free (s);
10315 }
10316 }
10317 else
10318 {
10319 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
10320 }
10321 *input_line_pointer = ch;
10322 demand_empty_rest_of_line ();
10323}
10324
10325/* Handle the .abicalls pseudo-op. I believe this is equivalent to
10326 .option pic2. It means to generate SVR4 PIC calls. */
10327
10328static void
10329s_abicalls (ignore)
43841e91 10330 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10331{
10332 mips_pic = SVR4_PIC;
10333 if (USE_GLOBAL_POINTER_OPT)
10334 {
10335 if (g_switch_seen && g_switch_value != 0)
10336 as_warn (_("-G may not be used with SVR4 PIC code"));
10337 g_switch_value = 0;
10338 }
10339 bfd_set_gp_size (stdoutput, 0);
10340 demand_empty_rest_of_line ();
10341}
10342
10343/* Handle the .cpload pseudo-op. This is used when generating SVR4
10344 PIC code. It sets the $gp register for the function based on the
10345 function address, which is in the register named in the argument.
10346 This uses a relocation against _gp_disp, which is handled specially
10347 by the linker. The result is:
10348 lui $gp,%hi(_gp_disp)
10349 addiu $gp,$gp,%lo(_gp_disp)
10350 addu $gp,$gp,.cpload argument
10351 The .cpload argument is normally $25 == $t9. */
10352
10353static void
10354s_cpload (ignore)
43841e91 10355 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10356{
10357 expressionS ex;
10358 int icnt = 0;
10359
10360 /* If we are not generating SVR4 PIC code, .cpload is ignored. */
10361 if (mips_pic != SVR4_PIC)
10362 {
10363 s_ignore (0);
10364 return;
10365 }
10366
10367 /* .cpload should be a in .set noreorder section. */
10368 if (mips_opts.noreorder == 0)
10369 as_warn (_(".cpload not in noreorder section"));
10370
10371 ex.X_op = O_symbol;
10372 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
10373 ex.X_op_symbol = NULL;
10374 ex.X_add_number = 0;
10375
10376 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 10377 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132
RH
10378
10379 macro_build_lui ((char *) NULL, &icnt, &ex, GP);
10380 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
10381 (int) BFD_RELOC_LO16);
10382
10383 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
10384 GP, GP, tc_get_register (0));
10385
10386 demand_empty_rest_of_line ();
10387}
10388
10389/* Handle the .cprestore pseudo-op. This stores $gp into a given
10390 offset from $sp. The offset is remembered, and after making a PIC
10391 call $gp is restored from that location. */
10392
10393static void
10394s_cprestore (ignore)
43841e91 10395 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10396{
10397 expressionS ex;
10398 int icnt = 0;
10399
10400 /* If we are not generating SVR4 PIC code, .cprestore is ignored. */
10401 if (mips_pic != SVR4_PIC)
10402 {
10403 s_ignore (0);
10404 return;
10405 }
10406
10407 mips_cprestore_offset = get_absolute_expression ();
10408
10409 ex.X_op = O_constant;
10410 ex.X_add_symbol = NULL;
10411 ex.X_op_symbol = NULL;
10412 ex.X_add_number = mips_cprestore_offset;
10413
10414 macro_build ((char *) NULL, &icnt, &ex,
10415 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 10416 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
10417 ? "sw" : "sd"),
10418 "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
10419
10420 demand_empty_rest_of_line ();
10421}
10422
10423/* Handle the .gpword pseudo-op. This is used when generating PIC
10424 code. It generates a 32 bit GP relative reloc. */
10425
10426static void
10427s_gpword (ignore)
43841e91 10428 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10429{
10430 symbolS *label;
10431 expressionS ex;
10432 char *p;
10433
10434 /* When not generating PIC code, this is treated as .word. */
10435 if (mips_pic != SVR4_PIC)
10436 {
10437 s_cons (2);
10438 return;
10439 }
10440
10441 label = insn_labels != NULL ? insn_labels->label : NULL;
10442 mips_emit_delays (true);
10443 if (auto_align)
10444 mips_align (2, 0, label);
10445 mips_clear_insn_labels ();
10446
10447 expression (&ex);
10448
10449 if (ex.X_op != O_symbol || ex.X_add_number != 0)
10450 {
10451 as_bad (_("Unsupported use of .gpword"));
10452 ignore_rest_of_line ();
10453 }
10454
10455 p = frag_more (4);
10456 md_number_to_chars (p, (valueT) 0, 4);
10457 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
10458 BFD_RELOC_MIPS_GPREL32);
10459
10460 demand_empty_rest_of_line ();
10461}
10462
10463/* Handle the .cpadd pseudo-op. This is used when dealing with switch
10464 tables in SVR4 PIC code. */
10465
10466static void
10467s_cpadd (ignore)
43841e91 10468 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10469{
10470 int icnt = 0;
10471 int reg;
10472
10473 /* This is ignored when not generating SVR4 PIC code. */
10474 if (mips_pic != SVR4_PIC)
10475 {
10476 s_ignore (0);
10477 return;
10478 }
10479
10480 /* Add $gp to the register named as an argument. */
10481 reg = tc_get_register (0);
10482 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
10483 ((bfd_arch_bits_per_address (stdoutput) == 32
9ce8a5dd 10484 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
252b5132
RH
10485 ? "addu" : "daddu"),
10486 "d,v,t", reg, reg, GP);
10487
bdaaa2e1 10488 demand_empty_rest_of_line ();
252b5132
RH
10489}
10490
10491/* Handle the .insn pseudo-op. This marks instruction labels in
10492 mips16 mode. This permits the linker to handle them specially,
10493 such as generating jalx instructions when needed. We also make
10494 them odd for the duration of the assembly, in order to generate the
10495 right sort of code. We will make them even in the adjust_symtab
10496 routine, while leaving them marked. This is convenient for the
10497 debugger and the disassembler. The linker knows to make them odd
10498 again. */
10499
10500static void
10501s_insn (ignore)
43841e91 10502 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10503{
10504 if (mips_opts.mips16)
10505 mips16_mark_labels ();
10506
10507 demand_empty_rest_of_line ();
10508}
10509
10510/* Handle a .stabn directive. We need these in order to mark a label
10511 as being a mips16 text label correctly. Sometimes the compiler
10512 will emit a label, followed by a .stabn, and then switch sections.
10513 If the label and .stabn are in mips16 mode, then the label is
10514 really a mips16 text label. */
10515
10516static void
10517s_mips_stab (type)
10518 int type;
10519{
10520 if (type == 'n' && mips_opts.mips16)
10521 mips16_mark_labels ();
10522
10523 s_stab (type);
10524}
10525
10526/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
10527 */
10528
10529static void
10530s_mips_weakext (ignore)
43841e91 10531 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10532{
10533 char *name;
10534 int c;
10535 symbolS *symbolP;
10536 expressionS exp;
10537
10538 name = input_line_pointer;
10539 c = get_symbol_end ();
10540 symbolP = symbol_find_or_make (name);
10541 S_SET_WEAK (symbolP);
10542 *input_line_pointer = c;
10543
10544 SKIP_WHITESPACE ();
10545
10546 if (! is_end_of_line[(unsigned char) *input_line_pointer])
10547 {
10548 if (S_IS_DEFINED (symbolP))
10549 {
10550 as_bad ("Ignoring attempt to redefine symbol `%s'.",
10551 S_GET_NAME (symbolP));
10552 ignore_rest_of_line ();
10553 return;
10554 }
bdaaa2e1 10555
252b5132
RH
10556 if (*input_line_pointer == ',')
10557 {
10558 ++input_line_pointer;
10559 SKIP_WHITESPACE ();
10560 }
bdaaa2e1 10561
252b5132
RH
10562 expression (&exp);
10563 if (exp.X_op != O_symbol)
10564 {
10565 as_bad ("bad .weakext directive");
10566 ignore_rest_of_line();
10567 return;
10568 }
49309057 10569 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
10570 }
10571
10572 demand_empty_rest_of_line ();
10573}
10574
10575/* Parse a register string into a number. Called from the ECOFF code
10576 to parse .frame. The argument is non-zero if this is the frame
10577 register, so that we can record it in mips_frame_reg. */
10578
10579int
10580tc_get_register (frame)
10581 int frame;
10582{
10583 int reg;
10584
10585 SKIP_WHITESPACE ();
10586 if (*input_line_pointer++ != '$')
10587 {
10588 as_warn (_("expected `$'"));
10589 reg = 0;
10590 }
10591 else if (isdigit ((unsigned char) *input_line_pointer))
10592 {
10593 reg = get_absolute_expression ();
10594 if (reg < 0 || reg >= 32)
10595 {
10596 as_warn (_("Bad register number"));
10597 reg = 0;
10598 }
10599 }
10600 else
10601 {
10602 if (strncmp (input_line_pointer, "fp", 2) == 0)
10603 reg = FP;
10604 else if (strncmp (input_line_pointer, "sp", 2) == 0)
10605 reg = SP;
10606 else if (strncmp (input_line_pointer, "gp", 2) == 0)
10607 reg = GP;
10608 else if (strncmp (input_line_pointer, "at", 2) == 0)
10609 reg = AT;
10610 else
10611 {
10612 as_warn (_("Unrecognized register name"));
10613 reg = 0;
10614 }
10615 input_line_pointer += 2;
10616 }
10617 if (frame)
10618 mips_frame_reg = reg != 0 ? reg : SP;
10619 return reg;
10620}
10621
10622valueT
10623md_section_align (seg, addr)
10624 asection *seg;
10625 valueT addr;
10626{
10627 int align = bfd_get_section_alignment (stdoutput, seg);
10628
10629#ifdef OBJ_ELF
10630 /* We don't need to align ELF sections to the full alignment.
10631 However, Irix 5 may prefer that we align them at least to a 16
10632 byte boundary. We don't bother to align the sections if we are
10633 targeted for an embedded system. */
10634 if (strcmp (TARGET_OS, "elf") == 0)
10635 return addr;
10636 if (align > 4)
10637 align = 4;
10638#endif
10639
10640 return ((addr + (1 << align) - 1) & (-1 << align));
10641}
10642
10643/* Utility routine, called from above as well. If called while the
10644 input file is still being read, it's only an approximation. (For
10645 example, a symbol may later become defined which appeared to be
10646 undefined earlier.) */
10647
10648static int
10649nopic_need_relax (sym, before_relaxing)
10650 symbolS *sym;
10651 int before_relaxing;
10652{
10653 if (sym == 0)
10654 return 0;
10655
10656 if (USE_GLOBAL_POINTER_OPT)
10657 {
10658 const char *symname;
10659 int change;
10660
10661 /* Find out whether this symbol can be referenced off the GP
10662 register. It can be if it is smaller than the -G size or if
10663 it is in the .sdata or .sbss section. Certain symbols can
10664 not be referenced off the GP, although it appears as though
10665 they can. */
10666 symname = S_GET_NAME (sym);
10667 if (symname != (const char *) NULL
10668 && (strcmp (symname, "eprol") == 0
10669 || strcmp (symname, "etext") == 0
10670 || strcmp (symname, "_gp") == 0
10671 || strcmp (symname, "edata") == 0
10672 || strcmp (symname, "_fbss") == 0
10673 || strcmp (symname, "_fdata") == 0
10674 || strcmp (symname, "_ftext") == 0
10675 || strcmp (symname, "end") == 0
10676 || strcmp (symname, "_gp_disp") == 0))
10677 change = 1;
10678 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
10679 && (0
10680#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
10681 || (symbol_get_obj (sym)->ecoff_extern_size != 0
10682 && (symbol_get_obj (sym)->ecoff_extern_size
10683 <= g_switch_value))
252b5132
RH
10684#endif
10685 /* We must defer this decision until after the whole
10686 file has been read, since there might be a .extern
10687 after the first use of this symbol. */
10688 || (before_relaxing
10689#ifndef NO_ECOFF_DEBUGGING
49309057 10690 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
10691#endif
10692 && S_GET_VALUE (sym) == 0)
10693 || (S_GET_VALUE (sym) != 0
10694 && S_GET_VALUE (sym) <= g_switch_value)))
10695 change = 0;
10696 else
10697 {
10698 const char *segname;
10699
10700 segname = segment_name (S_GET_SEGMENT (sym));
10701 assert (strcmp (segname, ".lit8") != 0
10702 && strcmp (segname, ".lit4") != 0);
10703 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
10704 && strcmp (segname, ".sbss") != 0
10705 && strncmp (segname, ".sdata.", 7) != 0
10706 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
10707 }
10708 return change;
10709 }
10710 else
10711 /* We are not optimizing for the GP register. */
10712 return 1;
10713}
10714
10715/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
10716 extended opcode. SEC is the section the frag is in. */
10717
10718static int
10719mips16_extended_frag (fragp, sec, stretch)
10720 fragS *fragp;
10721 asection *sec;
10722 long stretch;
10723{
10724 int type;
10725 register const struct mips16_immed_operand *op;
10726 offsetT val;
10727 int mintiny, maxtiny;
10728 segT symsec;
10729
10730 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
10731 return 0;
10732 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
10733 return 1;
10734
10735 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
10736 op = mips16_immed_operands;
10737 while (op->type != type)
10738 {
10739 ++op;
10740 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10741 }
10742
10743 if (op->unsp)
10744 {
10745 if (type == '<' || type == '>' || type == '[' || type == ']')
10746 {
10747 mintiny = 1;
10748 maxtiny = 1 << op->nbits;
10749 }
10750 else
10751 {
10752 mintiny = 0;
10753 maxtiny = (1 << op->nbits) - 1;
10754 }
10755 }
10756 else
10757 {
10758 mintiny = - (1 << (op->nbits - 1));
10759 maxtiny = (1 << (op->nbits - 1)) - 1;
10760 }
10761
49309057
ILT
10762 /* We can't always call S_GET_VALUE here, because we don't want to
10763 lock in a particular frag address. */
10764 if (symbol_constant_p (fragp->fr_symbol))
252b5132 10765 {
49309057
ILT
10766 val = (S_GET_VALUE (fragp->fr_symbol)
10767 + symbol_get_frag (fragp->fr_symbol)->fr_address);
252b5132
RH
10768 symsec = S_GET_SEGMENT (fragp->fr_symbol);
10769 }
49309057
ILT
10770 else if (symbol_equated_p (fragp->fr_symbol)
10771 && (symbol_constant_p
10772 (symbol_get_value_expression (fragp->fr_symbol)->X_add_symbol)))
252b5132 10773 {
49309057
ILT
10774 symbolS *eqsym;
10775
10776 eqsym = symbol_get_value_expression (fragp->fr_symbol)->X_add_symbol;
10777 val = (S_GET_VALUE (eqsym)
10778 + symbol_get_frag (eqsym)->fr_address
10779 + symbol_get_value_expression (fragp->fr_symbol)->X_add_number
10780 + symbol_get_frag (fragp->fr_symbol)->fr_address);
10781 symsec = S_GET_SEGMENT (eqsym);
252b5132
RH
10782 }
10783 else
10784 return 1;
10785
10786 if (op->pcrel)
10787 {
10788 addressT addr;
10789
10790 /* We won't have the section when we are called from
10791 mips_relax_frag. However, we will always have been called
10792 from md_estimate_size_before_relax first. If this is a
10793 branch to a different section, we mark it as such. If SEC is
10794 NULL, and the frag is not marked, then it must be a branch to
10795 the same section. */
10796 if (sec == NULL)
10797 {
10798 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
10799 return 1;
10800 }
10801 else
10802 {
10803 if (symsec != sec)
10804 {
10805 fragp->fr_subtype =
10806 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
10807
10808 /* FIXME: We should support this, and let the linker
10809 catch branches and loads that are out of range. */
10810 as_bad_where (fragp->fr_file, fragp->fr_line,
10811 _("unsupported PC relative reference to different section"));
10812
10813 return 1;
10814 }
10815 }
10816
10817 /* In this case, we know for sure that the symbol fragment is in
10818 the same section. If the fr_address of the symbol fragment
10819 is greater then the address of this fragment we want to add
10820 in STRETCH in order to get a better estimate of the address.
10821 This particularly matters because of the shift bits. */
10822 if (stretch != 0
49309057
ILT
10823 && (symbol_get_frag (fragp->fr_symbol)->fr_address
10824 >= fragp->fr_address))
252b5132
RH
10825 {
10826 fragS *f;
10827
10828 /* Adjust stretch for any alignment frag. Note that if have
10829 been expanding the earlier code, the symbol may be
10830 defined in what appears to be an earlier frag. FIXME:
10831 This doesn't handle the fr_subtype field, which specifies
10832 a maximum number of bytes to skip when doing an
10833 alignment. */
10834 for (f = fragp;
49309057 10835 f != NULL && f != symbol_get_frag (fragp->fr_symbol);
252b5132
RH
10836 f = f->fr_next)
10837 {
10838 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
10839 {
10840 if (stretch < 0)
10841 stretch = - ((- stretch)
10842 & ~ ((1 << (int) f->fr_offset) - 1));
10843 else
10844 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
10845 if (stretch == 0)
10846 break;
10847 }
10848 }
10849 if (f != NULL)
10850 val += stretch;
10851 }
10852
10853 addr = fragp->fr_address + fragp->fr_fix;
10854
10855 /* The base address rules are complicated. The base address of
10856 a branch is the following instruction. The base address of a
10857 PC relative load or add is the instruction itself, but if it
10858 is in a delay slot (in which case it can not be extended) use
10859 the address of the instruction whose delay slot it is in. */
10860 if (type == 'p' || type == 'q')
10861 {
10862 addr += 2;
10863
10864 /* If we are currently assuming that this frag should be
10865 extended, then, the current address is two bytes
bdaaa2e1 10866 higher. */
252b5132
RH
10867 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
10868 addr += 2;
10869
10870 /* Ignore the low bit in the target, since it will be set
10871 for a text label. */
10872 if ((val & 1) != 0)
10873 --val;
10874 }
10875 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
10876 addr -= 4;
10877 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
10878 addr -= 2;
10879
10880 val -= addr & ~ ((1 << op->shift) - 1);
10881
10882 /* Branch offsets have an implicit 0 in the lowest bit. */
10883 if (type == 'p' || type == 'q')
10884 val /= 2;
10885
10886 /* If any of the shifted bits are set, we must use an extended
10887 opcode. If the address depends on the size of this
10888 instruction, this can lead to a loop, so we arrange to always
10889 use an extended opcode. We only check this when we are in
10890 the main relaxation loop, when SEC is NULL. */
10891 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
10892 {
10893 fragp->fr_subtype =
10894 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
10895 return 1;
10896 }
10897
10898 /* If we are about to mark a frag as extended because the value
10899 is precisely maxtiny + 1, then there is a chance of an
10900 infinite loop as in the following code:
10901 la $4,foo
10902 .skip 1020
10903 .align 2
10904 foo:
10905 In this case when the la is extended, foo is 0x3fc bytes
10906 away, so the la can be shrunk, but then foo is 0x400 away, so
10907 the la must be extended. To avoid this loop, we mark the
10908 frag as extended if it was small, and is about to become
10909 extended with a value of maxtiny + 1. */
10910 if (val == ((maxtiny + 1) << op->shift)
10911 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
10912 && sec == NULL)
10913 {
10914 fragp->fr_subtype =
10915 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
10916 return 1;
10917 }
10918 }
10919 else if (symsec != absolute_section && sec != NULL)
10920 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
10921
10922 if ((val & ((1 << op->shift) - 1)) != 0
10923 || val < (mintiny << op->shift)
10924 || val > (maxtiny << op->shift))
10925 return 1;
10926 else
10927 return 0;
10928}
10929
10930/* Estimate the size of a frag before relaxing. Unless this is the
10931 mips16, we are not really relaxing here, and the final size is
10932 encoded in the subtype information. For the mips16, we have to
10933 decide whether we are using an extended opcode or not. */
10934
252b5132
RH
10935int
10936md_estimate_size_before_relax (fragp, segtype)
10937 fragS *fragp;
10938 asection *segtype;
10939{
43841e91 10940 int change = 0;
8614eeee 10941 boolean linkonce = false;
252b5132
RH
10942
10943 if (RELAX_MIPS16_P (fragp->fr_subtype))
10944 {
10945 if (mips16_extended_frag (fragp, segtype, 0))
10946 {
10947 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
10948 return 4;
10949 }
10950 else
10951 {
10952 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
10953 return 2;
10954 }
10955 }
10956
10957 if (mips_pic == NO_PIC)
10958 {
10959 change = nopic_need_relax (fragp->fr_symbol, 0);
10960 }
10961 else if (mips_pic == SVR4_PIC)
10962 {
10963 symbolS *sym;
10964 asection *symsec;
10965
10966 sym = fragp->fr_symbol;
10967
10968 /* Handle the case of a symbol equated to another symbol. */
49309057 10969 while (symbol_equated_p (sym)
252b5132
RH
10970 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
10971 {
10972 symbolS *n;
10973
10974 /* It's possible to get a loop here in a badly written
10975 program. */
49309057 10976 n = symbol_get_value_expression (sym)->X_add_symbol;
252b5132
RH
10977 if (n == sym)
10978 break;
10979 sym = n;
10980 }
10981
10982 symsec = S_GET_SEGMENT (sym);
10983
8614eeee
UC
10984 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
10985 if (symsec != segtype && ! S_IS_LOCAL (sym))
beae10d5
KH
10986 {
10987 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
10988 != 0)
10989 linkonce = true;
10990
10991 /* The GNU toolchain uses an extension for ELF: a section
10992 beginning with the magic string .gnu.linkonce is a linkonce
10993 section. */
10994 if (strncmp (segment_name (symsec), ".gnu.linkonce",
10995 sizeof ".gnu.linkonce" - 1) == 0)
10996 linkonce = true;
10997 }
8614eeee 10998
252b5132
RH
10999 /* This must duplicate the test in adjust_reloc_syms. */
11000 change = (symsec != &bfd_und_section
11001 && symsec != &bfd_abs_section
426b0403 11002 && ! bfd_is_com_section (symsec)
8614eeee 11003 && !linkonce
426b0403
AM
11004#ifdef OBJ_ELF
11005 /* A weak symbol is treated as external. */
11006 && ! S_IS_WEAK (sym)
11007#endif
11008 );
252b5132
RH
11009 }
11010 else
11011 abort ();
11012
11013 if (change)
11014 {
11015 /* Record the offset to the first reloc in the fr_opcode field.
11016 This lets md_convert_frag and tc_gen_reloc know that the code
11017 must be expanded. */
11018 fragp->fr_opcode = (fragp->fr_literal
11019 + fragp->fr_fix
11020 - RELAX_OLD (fragp->fr_subtype)
11021 + RELAX_RELOC1 (fragp->fr_subtype));
11022 /* FIXME: This really needs as_warn_where. */
11023 if (RELAX_WARN (fragp->fr_subtype))
11024 as_warn (_("AT used after \".set noat\" or macro used after \".set nomacro\""));
11025 }
11026
11027 if (! change)
11028 return 0;
11029 else
11030 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
11031}
11032
11033/* This is called to see whether a reloc against a defined symbol
11034 should be converted into a reloc against a section. Don't adjust
11035 MIPS16 jump relocations, so we don't have to worry about the format
11036 of the offset in the .o file. Don't adjust relocations against
11037 mips16 symbols, so that the linker can find them if it needs to set
11038 up a stub. */
11039
11040int
11041mips_fix_adjustable (fixp)
11042 fixS *fixp;
11043{
11044 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
11045 return 0;
11046 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11047 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
11048 return 0;
11049 if (fixp->fx_addsy == NULL)
11050 return 1;
11051#ifdef OBJ_ELF
11052 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11053 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
11054 && fixp->fx_subsy == NULL)
11055 return 0;
11056#endif
11057 return 1;
11058}
11059
11060/* Translate internal representation of relocation info to BFD target
11061 format. */
11062
11063arelent **
11064tc_gen_reloc (section, fixp)
43841e91 11065 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
11066 fixS *fixp;
11067{
11068 static arelent *retval[4];
11069 arelent *reloc;
11070 bfd_reloc_code_real_type code;
11071
11072 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
11073 retval[1] = NULL;
11074
49309057
ILT
11075 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
11076 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
11077 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
11078
11079 if (mips_pic == EMBEDDED_PIC
11080 && SWITCH_TABLE (fixp))
11081 {
11082 /* For a switch table entry we use a special reloc. The addend
11083 is actually the difference between the reloc address and the
11084 subtrahend. */
11085 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
11086 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
11087 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
11088 fixp->fx_r_type = BFD_RELOC_GPREL32;
11089 }
bb2d6cd7
GK
11090 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11091 reloc->addend = fixp->fx_addnumber;
252b5132
RH
11092 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
11093 {
11094 /* We use a special addend for an internal RELLO reloc. */
49309057 11095 if (symbol_section_p (fixp->fx_addsy))
252b5132
RH
11096 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
11097 else
11098 reloc->addend = fixp->fx_addnumber + reloc->address;
11099 }
11100 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
11101 {
11102 assert (fixp->fx_next != NULL
11103 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
11104 /* We use a special addend for an internal RELHI reloc. The
11105 reloc is relative to the RELLO; adjust the addend
11106 accordingly. */
49309057 11107 if (symbol_section_p (fixp->fx_addsy))
252b5132
RH
11108 reloc->addend = (fixp->fx_next->fx_frag->fr_address
11109 + fixp->fx_next->fx_where
11110 - S_GET_VALUE (fixp->fx_subsy));
11111 else
11112 reloc->addend = (fixp->fx_addnumber
11113 + fixp->fx_next->fx_frag->fr_address
11114 + fixp->fx_next->fx_where);
11115 }
252b5132
RH
11116 else
11117 {
11118 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
11119 /* A gruesome hack which is a result of the gruesome gas reloc
11120 handling. */
11121 reloc->addend = reloc->address;
11122 else
11123 reloc->addend = -reloc->address;
11124 }
11125
11126 /* If this is a variant frag, we may need to adjust the existing
11127 reloc and generate a new one. */
11128 if (fixp->fx_frag->fr_opcode != NULL
11129 && (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
11130 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11131 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
11132 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
11133 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
11134 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
11135 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16))
11136 {
11137 arelent *reloc2;
11138
11139 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
11140
11141 /* If this is not the last reloc in this frag, then we have two
11142 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
11143 CALL_HI16/CALL_LO16, both of which are being replaced. Let
11144 the second one handle all of them. */
11145 if (fixp->fx_next != NULL
11146 && fixp->fx_frag == fixp->fx_next->fx_frag)
11147 {
11148 assert ((fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
11149 && fixp->fx_next->fx_r_type == BFD_RELOC_MIPS_GPREL)
11150 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
11151 && (fixp->fx_next->fx_r_type
11152 == BFD_RELOC_MIPS_GOT_LO16))
11153 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
11154 && (fixp->fx_next->fx_r_type
11155 == BFD_RELOC_MIPS_CALL_LO16)));
11156 retval[0] = NULL;
11157 return retval;
11158 }
11159
11160 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
11161 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
11162 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
11163 retval[2] = NULL;
49309057
ILT
11164 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
11165 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
11166 reloc2->address = (reloc->address
11167 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
11168 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
11169 reloc2->addend = fixp->fx_addnumber;
11170 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
11171 assert (reloc2->howto != NULL);
11172
11173 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
11174 {
11175 arelent *reloc3;
11176
11177 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
11178 retval[3] = NULL;
11179 *reloc3 = *reloc2;
11180 reloc3->address += 4;
11181 }
11182
11183 if (mips_pic == NO_PIC)
11184 {
11185 assert (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL);
11186 fixp->fx_r_type = BFD_RELOC_HI16_S;
11187 }
11188 else if (mips_pic == SVR4_PIC)
11189 {
11190 switch (fixp->fx_r_type)
11191 {
11192 default:
11193 abort ();
11194 case BFD_RELOC_MIPS_GOT16:
11195 break;
11196 case BFD_RELOC_MIPS_CALL16:
11197 case BFD_RELOC_MIPS_GOT_LO16:
11198 case BFD_RELOC_MIPS_CALL_LO16:
11199 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
11200 break;
11201 }
11202 }
11203 else
11204 abort ();
11205 }
11206
11207 /* Since MIPS ELF uses Rel instead of Rela, encode the vtable entry
11208 to be used in the relocation's section offset. */
11209 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
11210 {
11211 reloc->address = reloc->addend;
11212 reloc->addend = 0;
11213 }
11214
11215 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
11216 fixup_segment converted a non-PC relative reloc into a PC
11217 relative reloc. In such a case, we need to convert the reloc
11218 code. */
11219 code = fixp->fx_r_type;
11220 if (fixp->fx_pcrel)
11221 {
11222 switch (code)
11223 {
11224 case BFD_RELOC_8:
11225 code = BFD_RELOC_8_PCREL;
11226 break;
11227 case BFD_RELOC_16:
11228 code = BFD_RELOC_16_PCREL;
11229 break;
11230 case BFD_RELOC_32:
11231 code = BFD_RELOC_32_PCREL;
11232 break;
11233 case BFD_RELOC_64:
11234 code = BFD_RELOC_64_PCREL;
11235 break;
11236 case BFD_RELOC_8_PCREL:
11237 case BFD_RELOC_16_PCREL:
11238 case BFD_RELOC_32_PCREL:
11239 case BFD_RELOC_64_PCREL:
11240 case BFD_RELOC_16_PCREL_S2:
11241 case BFD_RELOC_PCREL_HI16_S:
11242 case BFD_RELOC_PCREL_LO16:
11243 break;
11244 default:
11245 as_bad_where (fixp->fx_file, fixp->fx_line,
11246 _("Cannot make %s relocation PC relative"),
11247 bfd_get_reloc_code_name (code));
11248 }
11249 }
11250
11251 /* To support a PC relative reloc when generating embedded PIC code
11252 for ECOFF, we use a Cygnus extension. We check for that here to
11253 make sure that we don't let such a reloc escape normally. */
bb2d6cd7
GK
11254 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11255 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132
RH
11256 && code == BFD_RELOC_16_PCREL_S2
11257 && mips_pic != EMBEDDED_PIC)
11258 reloc->howto = NULL;
11259 else
11260 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
11261
11262 if (reloc->howto == NULL)
11263 {
11264 as_bad_where (fixp->fx_file, fixp->fx_line,
11265 _("Can not represent %s relocation in this object file format"),
11266 bfd_get_reloc_code_name (code));
11267 retval[0] = NULL;
11268 }
11269
11270 return retval;
11271}
11272
11273/* Relax a machine dependent frag. This returns the amount by which
11274 the current size of the frag should change. */
11275
11276int
11277mips_relax_frag (fragp, stretch)
11278 fragS *fragp;
11279 long stretch;
11280{
11281 if (! RELAX_MIPS16_P (fragp->fr_subtype))
11282 return 0;
11283
11284 if (mips16_extended_frag (fragp, (asection *) NULL, stretch))
11285 {
11286 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11287 return 0;
11288 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
11289 return 2;
11290 }
11291 else
11292 {
11293 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11294 return 0;
11295 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
11296 return -2;
11297 }
11298
11299 return 0;
11300}
11301
11302/* Convert a machine dependent frag. */
11303
11304void
11305md_convert_frag (abfd, asec, fragp)
43841e91 11306 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
11307 segT asec;
11308 fragS *fragp;
11309{
11310 int old, new;
11311 char *fixptr;
11312
11313 if (RELAX_MIPS16_P (fragp->fr_subtype))
11314 {
11315 int type;
11316 register const struct mips16_immed_operand *op;
11317 boolean small, ext;
11318 offsetT val;
11319 bfd_byte *buf;
11320 unsigned long insn;
11321 boolean use_extend;
11322 unsigned short extend;
11323
11324 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
11325 op = mips16_immed_operands;
11326 while (op->type != type)
11327 ++op;
11328
11329 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11330 {
11331 small = false;
11332 ext = true;
11333 }
11334 else
11335 {
11336 small = true;
11337 ext = false;
11338 }
11339
11340 resolve_symbol_value (fragp->fr_symbol, 1);
11341 val = S_GET_VALUE (fragp->fr_symbol);
11342 if (op->pcrel)
11343 {
11344 addressT addr;
11345
11346 addr = fragp->fr_address + fragp->fr_fix;
11347
11348 /* The rules for the base address of a PC relative reloc are
11349 complicated; see mips16_extended_frag. */
11350 if (type == 'p' || type == 'q')
11351 {
11352 addr += 2;
11353 if (ext)
11354 addr += 2;
11355 /* Ignore the low bit in the target, since it will be
11356 set for a text label. */
11357 if ((val & 1) != 0)
11358 --val;
11359 }
11360 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
11361 addr -= 4;
11362 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
11363 addr -= 2;
11364
11365 addr &= ~ (addressT) ((1 << op->shift) - 1);
11366 val -= addr;
11367
11368 /* Make sure the section winds up with the alignment we have
11369 assumed. */
11370 if (op->shift > 0)
11371 record_alignment (asec, op->shift);
11372 }
11373
11374 if (ext
11375 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
11376 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
11377 as_warn_where (fragp->fr_file, fragp->fr_line,
11378 _("extended instruction in delay slot"));
11379
11380 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
11381
11382 if (target_big_endian)
11383 insn = bfd_getb16 (buf);
11384 else
11385 insn = bfd_getl16 (buf);
11386
11387 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
11388 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
11389 small, ext, &insn, &use_extend, &extend);
11390
11391 if (use_extend)
11392 {
11393 md_number_to_chars (buf, 0xf000 | extend, 2);
11394 fragp->fr_fix += 2;
11395 buf += 2;
11396 }
11397
11398 md_number_to_chars (buf, insn, 2);
11399 fragp->fr_fix += 2;
11400 buf += 2;
11401 }
11402 else
11403 {
11404 if (fragp->fr_opcode == NULL)
11405 return;
11406
11407 old = RELAX_OLD (fragp->fr_subtype);
11408 new = RELAX_NEW (fragp->fr_subtype);
11409 fixptr = fragp->fr_literal + fragp->fr_fix;
11410
11411 if (new > 0)
11412 memcpy (fixptr - old, fixptr, new);
11413
11414 fragp->fr_fix += new - old;
11415 }
11416}
11417
11418#ifdef OBJ_ELF
11419
11420/* This function is called after the relocs have been generated.
11421 We've been storing mips16 text labels as odd. Here we convert them
11422 back to even for the convenience of the debugger. */
11423
11424void
11425mips_frob_file_after_relocs ()
11426{
11427 asymbol **syms;
11428 unsigned int count, i;
11429
11430 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11431 return;
11432
11433 syms = bfd_get_outsymbols (stdoutput);
11434 count = bfd_get_symcount (stdoutput);
11435 for (i = 0; i < count; i++, syms++)
11436 {
11437 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
11438 && ((*syms)->value & 1) != 0)
11439 {
11440 (*syms)->value &= ~1;
11441 /* If the symbol has an odd size, it was probably computed
11442 incorrectly, so adjust that as well. */
11443 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
11444 ++elf_symbol (*syms)->internal_elf_sym.st_size;
11445 }
11446 }
11447}
11448
11449#endif
11450
11451/* This function is called whenever a label is defined. It is used
11452 when handling branch delays; if a branch has a label, we assume we
11453 can not move it. */
11454
11455void
11456mips_define_label (sym)
11457 symbolS *sym;
11458{
11459 struct insn_label_list *l;
11460
11461 if (free_insn_labels == NULL)
11462 l = (struct insn_label_list *) xmalloc (sizeof *l);
11463 else
11464 {
11465 l = free_insn_labels;
11466 free_insn_labels = l->next;
11467 }
11468
11469 l->label = sym;
11470 l->next = insn_labels;
11471 insn_labels = l;
11472}
11473\f
11474#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11475
11476/* Some special processing for a MIPS ELF file. */
11477
11478void
11479mips_elf_final_processing ()
11480{
11481 /* Write out the register information. */
11482 if (! mips_64)
11483 {
11484 Elf32_RegInfo s;
11485
11486 s.ri_gprmask = mips_gprmask;
11487 s.ri_cprmask[0] = mips_cprmask[0];
11488 s.ri_cprmask[1] = mips_cprmask[1];
11489 s.ri_cprmask[2] = mips_cprmask[2];
11490 s.ri_cprmask[3] = mips_cprmask[3];
11491 /* The gp_value field is set by the MIPS ELF backend. */
11492
11493 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
11494 ((Elf32_External_RegInfo *)
11495 mips_regmask_frag));
11496 }
11497 else
11498 {
11499 Elf64_Internal_RegInfo s;
11500
11501 s.ri_gprmask = mips_gprmask;
11502 s.ri_pad = 0;
11503 s.ri_cprmask[0] = mips_cprmask[0];
11504 s.ri_cprmask[1] = mips_cprmask[1];
11505 s.ri_cprmask[2] = mips_cprmask[2];
11506 s.ri_cprmask[3] = mips_cprmask[3];
11507 /* The gp_value field is set by the MIPS ELF backend. */
11508
11509 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
11510 ((Elf64_External_RegInfo *)
11511 mips_regmask_frag));
11512 }
11513
11514 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
11515 sort of BFD interface for this. */
11516 if (mips_any_noreorder)
11517 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
11518 if (mips_pic != NO_PIC)
11519 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
11520
bdaaa2e1 11521 /* Set the MIPS ELF ABI flags. */
252b5132
RH
11522 if (mips_abi_string == 0)
11523 ;
beae10d5 11524 else if (strcmp (mips_abi_string, "32") == 0)
252b5132 11525 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
beae10d5 11526 else if (strcmp (mips_abi_string, "o64") == 0)
252b5132 11527 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
beae10d5 11528 else if (strcmp (mips_abi_string, "eabi") == 0)
252b5132
RH
11529 {
11530 if (mips_eabi64)
11531 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
11532 else
11533 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
11534 }
11535
11536 if (mips_32bitmode)
11537 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
11538}
11539
11540#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
11541\f
beae10d5
KH
11542typedef struct proc {
11543 symbolS *isym;
11544 unsigned long reg_mask;
11545 unsigned long reg_offset;
11546 unsigned long fpreg_mask;
11547 unsigned long fpreg_offset;
11548 unsigned long frame_offset;
11549 unsigned long frame_reg;
11550 unsigned long pc_reg;
11551} procS;
252b5132
RH
11552
11553static procS cur_proc;
11554static procS *cur_proc_ptr;
11555static int numprocs;
11556
0a9ef439 11557/* Fill in an rs_align_code fragment. */
a19d8eb0 11558
0a9ef439
RH
11559void
11560mips_handle_align (fragp)
11561 fragS *fragp;
a19d8eb0 11562{
0a9ef439
RH
11563 if (fragp->fr_type != rs_align_code)
11564 return;
11565
11566 if (mips_opts.mips16)
a19d8eb0
CP
11567 {
11568 static const unsigned char be_nop[] = { 0x65, 0x00 };
11569 static const unsigned char le_nop[] = { 0x00, 0x65 };
11570
0a9ef439
RH
11571 int bytes;
11572 char *p;
a19d8eb0 11573
0a9ef439
RH
11574 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
11575 p = fragp->fr_literal + fragp->fr_fix;
11576
11577 if (bytes & 1)
11578 {
11579 *p++ = 0;
11580 fragp->fr_fix += 1;
11581 }
11582
11583 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
11584 fragp->fr_var = 2;
a19d8eb0
CP
11585 }
11586
0a9ef439 11587 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
11588}
11589
252b5132
RH
11590static void
11591md_obj_begin ()
11592{
11593}
11594
11595static void
11596md_obj_end ()
11597{
11598 /* check for premature end, nesting errors, etc */
11599 if (cur_proc_ptr)
11600 as_warn (_("missing `.end' at end of assembly"));
11601}
11602
11603static long
11604get_number ()
11605{
11606 int negative = 0;
11607 long val = 0;
11608
11609 if (*input_line_pointer == '-')
11610 {
11611 ++input_line_pointer;
11612 negative = 1;
11613 }
d9a62219 11614 if (!isdigit ((unsigned char) *input_line_pointer))
252b5132
RH
11615 as_bad (_("Expected simple number."));
11616 if (input_line_pointer[0] == '0')
11617 {
11618 if (input_line_pointer[1] == 'x')
11619 {
11620 input_line_pointer += 2;
d9a62219 11621 while (isxdigit ((unsigned char) *input_line_pointer))
252b5132
RH
11622 {
11623 val <<= 4;
11624 val |= hex_value (*input_line_pointer++);
11625 }
11626 return negative ? -val : val;
11627 }
11628 else
11629 {
11630 ++input_line_pointer;
d9a62219 11631 while (isdigit ((unsigned char) *input_line_pointer))
252b5132
RH
11632 {
11633 val <<= 3;
11634 val |= *input_line_pointer++ - '0';
11635 }
11636 return negative ? -val : val;
11637 }
11638 }
d9a62219 11639 if (!isdigit ((unsigned char) *input_line_pointer))
252b5132
RH
11640 {
11641 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
11642 *input_line_pointer, *input_line_pointer);
11643 as_warn (_("Invalid number"));
11644 return -1;
11645 }
d9a62219 11646 while (isdigit ((unsigned char) *input_line_pointer))
252b5132
RH
11647 {
11648 val *= 10;
11649 val += *input_line_pointer++ - '0';
11650 }
11651 return negative ? -val : val;
11652}
11653
11654/* The .file directive; just like the usual .file directive, but there
11655 is an initial number which is the ECOFF file index. */
11656
11657static void
11658s_file (x)
43841e91 11659 int x ATTRIBUTE_UNUSED;
252b5132
RH
11660{
11661 int line;
11662
11663 line = get_number ();
11664 s_app_file (0);
11665}
11666
252b5132
RH
11667/* The .end directive. */
11668
11669static void
11670s_mips_end (x)
43841e91 11671 int x ATTRIBUTE_UNUSED;
252b5132
RH
11672{
11673 symbolS *p;
11674 int maybe_text;
11675
11676 if (!is_end_of_line[(unsigned char) *input_line_pointer])
11677 {
11678 p = get_symbol ();
11679 demand_empty_rest_of_line ();
11680 }
11681 else
11682 p = NULL;
11683
11684#ifdef BFD_ASSEMBLER
11685 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
11686 maybe_text = 1;
11687 else
11688 maybe_text = 0;
11689#else
11690 if (now_seg != data_section && now_seg != bss_section)
11691 maybe_text = 1;
11692 else
11693 maybe_text = 0;
11694#endif
11695
11696 if (!maybe_text)
11697 as_warn (_(".end not in text section"));
11698
11699 if (!cur_proc_ptr)
11700 {
11701 as_warn (_(".end directive without a preceding .ent directive."));
11702 demand_empty_rest_of_line ();
11703 return;
11704 }
11705
11706 if (p != NULL)
11707 {
11708 assert (S_GET_NAME (p));
11709 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
11710 as_warn (_(".end symbol does not match .ent symbol."));
11711 }
11712 else
11713 as_warn (_(".end directive missing or unknown symbol"));
11714
11715#ifdef MIPS_STABS_ELF
11716 {
11717 segT saved_seg = now_seg;
11718 subsegT saved_subseg = now_subseg;
11719 fragS *saved_frag = frag_now;
11720 valueT dot;
11721 segT seg;
11722 expressionS exp;
11723 char *fragp;
11724
11725 dot = frag_now_fix ();
11726
11727#ifdef md_flush_pending_output
11728 md_flush_pending_output ();
11729#endif
11730
11731 assert (pdr_seg);
11732 subseg_set (pdr_seg, 0);
11733
beae10d5 11734 /* Write the symbol. */
252b5132
RH
11735 exp.X_op = O_symbol;
11736 exp.X_add_symbol = p;
11737 exp.X_add_number = 0;
11738 emit_expr (&exp, 4);
11739
beae10d5 11740 fragp = frag_more (7 * 4);
252b5132 11741
beae10d5
KH
11742 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
11743 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
11744 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
11745 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
11746 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
11747 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
11748 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
252b5132
RH
11749
11750 subseg_set (saved_seg, saved_subseg);
11751 }
11752#endif
11753
11754 cur_proc_ptr = NULL;
11755}
11756
11757/* The .aent and .ent directives. */
11758
11759static void
11760s_mips_ent (aent)
11761 int aent;
11762{
11763 int number = 0;
11764 symbolS *symbolP;
11765 int maybe_text;
11766
11767 symbolP = get_symbol ();
11768 if (*input_line_pointer == ',')
11769 input_line_pointer++;
11770 SKIP_WHITESPACE ();
d9a62219
DE
11771 if (isdigit ((unsigned char) *input_line_pointer)
11772 || *input_line_pointer == '-')
252b5132
RH
11773 number = get_number ();
11774
11775#ifdef BFD_ASSEMBLER
11776 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
11777 maybe_text = 1;
11778 else
11779 maybe_text = 0;
11780#else
11781 if (now_seg != data_section && now_seg != bss_section)
11782 maybe_text = 1;
11783 else
11784 maybe_text = 0;
11785#endif
11786
11787 if (!maybe_text)
11788 as_warn (_(".ent or .aent not in text section."));
11789
11790 if (!aent && cur_proc_ptr)
11791 as_warn (_("missing `.end'"));
11792
11793 if (!aent)
11794 {
11795 cur_proc_ptr = &cur_proc;
11796 memset (cur_proc_ptr, '\0', sizeof (procS));
11797
11798 cur_proc_ptr->isym = symbolP;
11799
49309057 11800 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132
RH
11801
11802 numprocs++;
11803 }
11804
11805 demand_empty_rest_of_line ();
11806}
11807
11808/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 11809 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 11810 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 11811 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
11812 symbol table (in the mdebug section). */
11813
11814static void
11815s_mips_frame (ignore)
11816 int ignore;
11817{
11818#ifdef MIPS_STABS_ELF
11819
11820 long val;
11821
beae10d5 11822 if (cur_proc_ptr == (procS *) NULL)
252b5132
RH
11823 {
11824 as_warn (_(".frame outside of .ent"));
11825 demand_empty_rest_of_line ();
11826 return;
11827 }
11828
11829 cur_proc_ptr->frame_reg = tc_get_register (1);
11830
11831 SKIP_WHITESPACE ();
11832 if (*input_line_pointer++ != ','
11833 || get_absolute_expression_and_terminator (&val) != ',')
11834 {
11835 as_warn (_("Bad .frame directive"));
11836 --input_line_pointer;
11837 demand_empty_rest_of_line ();
11838 return;
11839 }
11840
11841 cur_proc_ptr->frame_offset = val;
11842 cur_proc_ptr->pc_reg = tc_get_register (0);
11843
11844 demand_empty_rest_of_line ();
11845#else
11846 s_ignore (ignore);
11847#endif /* MIPS_STABS_ELF */
11848}
11849
bdaaa2e1
KH
11850/* The .fmask and .mask directives. If the mdebug section is present
11851 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 11852 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 11853 information correctly. We can't use the ecoff routines because they
252b5132
RH
11854 make reference to the ecoff symbol table (in the mdebug section). */
11855
11856static void
11857s_mips_mask (reg_type)
11858 char reg_type;
11859{
11860#ifdef MIPS_STABS_ELF
11861 long mask, off;
bdaaa2e1 11862
252b5132
RH
11863 if (cur_proc_ptr == (procS *) NULL)
11864 {
11865 as_warn (_(".mask/.fmask outside of .ent"));
11866 demand_empty_rest_of_line ();
11867 return;
11868 }
11869
11870 if (get_absolute_expression_and_terminator (&mask) != ',')
11871 {
11872 as_warn (_("Bad .mask/.fmask directive"));
11873 --input_line_pointer;
11874 demand_empty_rest_of_line ();
11875 return;
11876 }
11877
11878 off = get_absolute_expression ();
11879
11880 if (reg_type == 'F')
11881 {
11882 cur_proc_ptr->fpreg_mask = mask;
11883 cur_proc_ptr->fpreg_offset = off;
11884 }
11885 else
11886 {
11887 cur_proc_ptr->reg_mask = mask;
11888 cur_proc_ptr->reg_offset = off;
11889 }
11890
11891 demand_empty_rest_of_line ();
11892#else
11893 s_ignore (reg_type);
11894#endif /* MIPS_STABS_ELF */
11895}
11896
11897/* The .loc directive. */
11898
11899#if 0
11900static void
11901s_loc (x)
11902 int x;
11903{
11904 symbolS *symbolP;
11905 int lineno;
11906 int addroff;
11907
11908 assert (now_seg == text_section);
11909
11910 lineno = get_number ();
11911 addroff = frag_now_fix ();
11912
11913 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
11914 S_SET_TYPE (symbolP, N_SLINE);
11915 S_SET_OTHER (symbolP, 0);
11916 S_SET_DESC (symbolP, lineno);
11917 symbolP->sy_segment = now_seg;
11918}
11919#endif
e7af610e
NC
11920
11921/* CPU name/ISA/number mapping table.
11922
11923 Entries are grouped by type. The first matching CPU or ISA entry
11924 gets chosen by CPU or ISA, so it should be the 'canonical' name
11925 for that type. Entries after that within the type are sorted
11926 alphabetically.
11927
11928 Case is ignored in comparison, so put the canonical entry in the
11929 appropriate case but everything else in lower case to ease eye pain. */
e972090a
NC
11930static const struct mips_cpu_info mips_cpu_info_table[] =
11931{
e7af610e
NC
11932 /* MIPS1 ISA */
11933 { "MIPS1", 1, ISA_MIPS1, CPU_R3000, },
11934 { "mips", 1, ISA_MIPS1, CPU_R3000, },
11935
11936 /* MIPS2 ISA */
11937 { "MIPS2", 1, ISA_MIPS2, CPU_R6000, },
11938
11939 /* MIPS3 ISA */
11940 { "MIPS3", 1, ISA_MIPS3, CPU_R4000, },
11941
11942 /* MIPS4 ISA */
11943 { "MIPS4", 1, ISA_MIPS4, CPU_R8000, },
11944
84ea6cf2
NC
11945 /* MIPS5 ISA */
11946 { "MIPS5", 1, ISA_MIPS5, CPU_MIPS5, },
11947 { "Generic-MIPS5", 0, ISA_MIPS5, CPU_MIPS5, },
11948
e7af610e
NC
11949 /* MIPS32 ISA */
11950 { "MIPS32", 1, ISA_MIPS32, CPU_MIPS32, },
11951 { "Generic-MIPS32", 0, ISA_MIPS32, CPU_MIPS32, },
11952
84ea6cf2 11953#if 1
e7af610e
NC
11954 /* XXX for now, MIPS64 -> MIPS3 because of history */
11955 { "MIPS64", 1, ISA_MIPS3, CPU_R4000 }, /* XXX! */
84ea6cf2
NC
11956#else
11957 /* MIPS64 ISA */
11958 { "MIPS64", 1, ISA_MIPS64, CPU_MIPS64 },
11959#endif
11960 { "mips64isa", 1, ISA_MIPS64, CPU_MIPS64 },
11961 { "Generic-MIPS64", 0, ISA_MIPS64, CPU_MIPS64, },
e7af610e
NC
11962
11963 /* R2000 CPU */
11964 { "R2000", 0, ISA_MIPS1, CPU_R2000, },
11965 { "2000", 0, ISA_MIPS1, CPU_R2000, },
11966 { "2k", 0, ISA_MIPS1, CPU_R2000, },
11967 { "r2k", 0, ISA_MIPS1, CPU_R2000, },
11968
11969 /* R3000 CPU */
11970 { "R3000", 0, ISA_MIPS1, CPU_R3000, },
11971 { "3000", 0, ISA_MIPS1, CPU_R3000, },
11972 { "3k", 0, ISA_MIPS1, CPU_R3000, },
11973 { "r3k", 0, ISA_MIPS1, CPU_R3000, },
11974
11975 /* TX3900 CPU */
11976 { "R3900", 0, ISA_MIPS1, CPU_R3900, },
11977 { "3900", 0, ISA_MIPS1, CPU_R3900, },
e972090a 11978 { "mipstx39", 0, ISA_MIPS1, CPU_R3900, },
e7af610e
NC
11979
11980 /* R4000 CPU */
11981 { "R4000", 0, ISA_MIPS3, CPU_R4000, },
11982 { "4000", 0, ISA_MIPS3, CPU_R4000, },
11983 { "4k", 0, ISA_MIPS3, CPU_R4000, }, /* beware */
11984 { "r4k", 0, ISA_MIPS3, CPU_R4000, },
11985
11986 /* R4010 CPU */
11987 { "R4010", 0, ISA_MIPS2, CPU_R4010, },
11988 { "4010", 0, ISA_MIPS2, CPU_R4010, },
11989
11990 /* R4400 CPU */
11991 { "R4400", 0, ISA_MIPS3, CPU_R4400, },
11992 { "4400", 0, ISA_MIPS3, CPU_R4400, },
11993
11994 /* R4600 CPU */
11995 { "R4600", 0, ISA_MIPS3, CPU_R4600, },
11996 { "4600", 0, ISA_MIPS3, CPU_R4600, },
11997 { "mips64orion", 0, ISA_MIPS3, CPU_R4600, },
11998 { "orion", 0, ISA_MIPS3, CPU_R4600, },
11999
12000 /* R4650 CPU */
12001 { "R4650", 0, ISA_MIPS3, CPU_R4650, },
12002 { "4650", 0, ISA_MIPS3, CPU_R4650, },
12003
12004 /* R6000 CPU */
12005 { "R6000", 0, ISA_MIPS2, CPU_R6000, },
12006 { "6000", 0, ISA_MIPS2, CPU_R6000, },
12007 { "6k", 0, ISA_MIPS2, CPU_R6000, },
12008 { "r6k", 0, ISA_MIPS2, CPU_R6000, },
12009
12010 /* R8000 CPU */
12011 { "R8000", 0, ISA_MIPS4, CPU_R8000, },
12012 { "8000", 0, ISA_MIPS4, CPU_R8000, },
12013 { "8k", 0, ISA_MIPS4, CPU_R8000, },
12014 { "r8k", 0, ISA_MIPS4, CPU_R8000, },
12015
12016 /* R10000 CPU */
12017 { "R10000", 0, ISA_MIPS4, CPU_R10000, },
12018 { "10000", 0, ISA_MIPS4, CPU_R10000, },
12019 { "10k", 0, ISA_MIPS4, CPU_R10000, },
12020 { "r10k", 0, ISA_MIPS4, CPU_R10000, },
12021
12022 /* VR4100 CPU */
12023 { "VR4100", 0, ISA_MIPS3, CPU_VR4100, },
12024 { "4100", 0, ISA_MIPS3, CPU_VR4100, },
12025 { "mips64vr4100", 0, ISA_MIPS3, CPU_VR4100, },
12026 { "r4100", 0, ISA_MIPS3, CPU_VR4100, },
12027
12028 /* VR4111 CPU */
12029 { "VR4111", 0, ISA_MIPS3, CPU_R4111, },
12030 { "4111", 0, ISA_MIPS3, CPU_R4111, },
12031 { "mips64vr4111", 0, ISA_MIPS3, CPU_R4111, },
12032 { "r4111", 0, ISA_MIPS3, CPU_R4111, },
12033
12034 /* VR4300 CPU */
12035 { "VR4300", 0, ISA_MIPS3, CPU_R4300, },
12036 { "4300", 0, ISA_MIPS3, CPU_R4300, },
12037 { "mips64vr4300", 0, ISA_MIPS3, CPU_R4300, },
12038 { "r4300", 0, ISA_MIPS3, CPU_R4300, },
12039
12040 /* VR5000 CPU */
12041 { "VR5000", 0, ISA_MIPS4, CPU_R5000, },
12042 { "5000", 0, ISA_MIPS4, CPU_R5000, },
12043 { "5k", 0, ISA_MIPS4, CPU_R5000, },
12044 { "mips64vr5000", 0, ISA_MIPS4, CPU_R5000, },
12045 { "r5000", 0, ISA_MIPS4, CPU_R5000, },
12046 { "r5200", 0, ISA_MIPS4, CPU_R5000, },
12047 { "r5230", 0, ISA_MIPS4, CPU_R5000, },
12048 { "r5231", 0, ISA_MIPS4, CPU_R5000, },
12049 { "r5261", 0, ISA_MIPS4, CPU_R5000, },
12050 { "r5721", 0, ISA_MIPS4, CPU_R5000, },
12051 { "r5k", 0, ISA_MIPS4, CPU_R5000, },
12052 { "r7000", 0, ISA_MIPS4, CPU_R5000, },
12053
12054 /* MIPS32 4K CPU */
12055 { "MIPS32-4K", 0, ISA_MIPS32, CPU_MIPS32_4K, },
12056 { "4kc", 0, ISA_MIPS32, CPU_MIPS32_4K, },
12057 { "4km", 0, ISA_MIPS32, CPU_MIPS32_4K, },
12058 { "4kp", 0, ISA_MIPS32, CPU_MIPS32_4K, },
12059 { "mips32-4kc", 0, ISA_MIPS32, CPU_MIPS32_4K, },
12060 { "mips32-4km", 0, ISA_MIPS32, CPU_MIPS32_4K, },
12061 { "mips32-4kp", 0, ISA_MIPS32, CPU_MIPS32_4K, },
12062
c6c98b38
NC
12063 /* SiByte SB-1 CPU */
12064 { "SB-1", 0, ISA_MIPS64, CPU_SB1, },
e972090a 12065 { "sb-1250", 0, ISA_MIPS64, CPU_SB1, },
c6c98b38
NC
12066 { "sb1", 0, ISA_MIPS64, CPU_SB1, },
12067 { "sb1250", 0, ISA_MIPS64, CPU_SB1, },
12068
beae10d5 12069 /* End marker. */
e7af610e
NC
12070 { NULL, 0, 0, 0, },
12071};
12072
12073static const struct mips_cpu_info *
12074mips_cpu_info_from_name (name)
12075 const char *name;
12076{
12077 int i;
12078
12079 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
beae10d5 12080 if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
e7af610e
NC
12081 return (&mips_cpu_info_table[i]);
12082
e972090a 12083 return NULL;
e7af610e
NC
12084}
12085
12086static const struct mips_cpu_info *
12087mips_cpu_info_from_isa (isa)
12088 int isa;
12089{
12090 int i;
12091
12092 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
12093 if (mips_cpu_info_table[i].is_isa
12094 && isa == mips_cpu_info_table[i].isa)
12095 return (&mips_cpu_info_table[i]);
12096
e972090a 12097 return NULL;
e7af610e
NC
12098}
12099
12100static const struct mips_cpu_info *
12101mips_cpu_info_from_cpu (cpu)
12102 int cpu;
12103{
12104 int i;
12105
12106 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
12107 if (!mips_cpu_info_table[i].is_isa
12108 && cpu == mips_cpu_info_table[i].cpu)
12109 return (&mips_cpu_info_table[i]);
12110
e972090a 12111 return NULL;
e7af610e 12112}