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