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