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