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