merge from gcc
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
f7e42eb4 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
82efde3a 3 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
25
26#include "as.h"
27#include "config.h"
28#include "subsegs.h"
3882b010 29#include "safe-ctype.h"
252b5132
RH
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
252b5132
RH
105/* The name of the readonly data section. */
106#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
107 ? ".data" \
108 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
109 ? ".rdata" \
056350c6
NC
110 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
111 ? ".rdata" \
252b5132
RH
112 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
113 ? ".rodata" \
114 : (abort (), ""))
115
116/* This is the set of options which may be modified by the .set
117 pseudo-op. We use a struct so that .set push and .set pop are more
118 reliable. */
119
e972090a
NC
120struct mips_set_options
121{
252b5132
RH
122 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
123 if it has not been initialized. Changed by `.set mipsN', and the
124 -mipsN command line option, and the default CPU. */
125 int isa;
126 /* Whether we are assembling for the mips16 processor. 0 if we are
127 not, 1 if we are, and -1 if the value has not been initialized.
128 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
129 -nomips16 command line options, and the default CPU. */
130 int mips16;
131 /* Non-zero if we should not reorder instructions. Changed by `.set
132 reorder' and `.set noreorder'. */
133 int noreorder;
134 /* Non-zero if we should not permit the $at ($1) register to be used
135 in instructions. Changed by `.set at' and `.set noat'. */
136 int noat;
137 /* Non-zero if we should warn when a macro instruction expands into
138 more than one machine instruction. Changed by `.set nomacro' and
139 `.set macro'. */
140 int warn_about_macros;
141 /* Non-zero if we should not move instructions. Changed by `.set
142 move', `.set volatile', `.set nomove', and `.set novolatile'. */
143 int nomove;
144 /* Non-zero if we should not optimize branches by moving the target
145 of the branch into the delay slot. Actually, we don't perform
146 this optimization anyhow. Changed by `.set bopt' and `.set
147 nobopt'. */
148 int nobopt;
149 /* Non-zero if we should not autoextend mips16 instructions.
150 Changed by `.set autoextend' and `.set noautoextend'. */
151 int noautoextend;
152};
153
154/* This is the struct we use to hold the current set of options. Note
e7af610e
NC
155 that we must set the isa field to ISA_UNKNOWN and the mips16 field to
156 -1 to indicate that they have not been initialized. */
252b5132 157
e972090a
NC
158static struct mips_set_options mips_opts =
159{
e7af610e
NC
160 ISA_UNKNOWN, -1, 0, 0, 0, 0, 0, 0
161};
252b5132
RH
162
163/* These variables are filled in with the masks of registers used.
164 The object format code reads them and puts them in the appropriate
165 place. */
166unsigned long mips_gprmask;
167unsigned long mips_cprmask[4];
168
169/* MIPS ISA we are using for this output file. */
e7af610e 170static int file_mips_isa = ISA_UNKNOWN;
252b5132 171
ec68c924 172/* The argument of the -mcpu= flag. Historical for code generation. */
e7af610e 173static int mips_cpu = CPU_UNKNOWN;
252b5132 174
ec68c924
EC
175/* The argument of the -march= flag. The architecture we are assembling. */
176static int mips_arch = CPU_UNKNOWN;
177
178/* The argument of the -mtune= flag. The architecture for which we
179 are optimizing. */
180static int mips_tune = CPU_UNKNOWN;
181
e013f690
TS
182/* The ABI to use. */
183enum mips_abi_level
184{
185 NO_ABI = 0,
186 O32_ABI,
187 O64_ABI,
188 N32_ABI,
189 N64_ABI,
190 EABI_ABI
191};
192
193static enum mips_abi_level mips_abi = NO_ABI;
252b5132 194
2f4dcb11 195/* Whether we should mark the file EABI64 or EABI32. */
252b5132
RH
196static int mips_eabi64 = 0;
197
198/* If they asked for mips1 or mips2 and a cpu that is
bdaaa2e1 199 mips3 or greater, then mark the object file 32BITMODE. */
252b5132
RH
200static int mips_32bitmode = 0;
201
bdaaa2e1 202/* True if -mgp32 was passed. */
c97ef257
AH
203static int mips_gp32 = 0;
204
ca4e0257
RS
205/* True if -mfp32 was passed. */
206static int mips_fp32 = 0;
207
9ce8a5dd
GRK
208/* Some ISA's have delay slots for instructions which read or write
209 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
bdaaa2e1 210 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
9ce8a5dd
GRK
211 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
212 delay slot in this ISA. The uses of this macro assume that any
213 ISA that has delay slots for one of these, has them for all. They
214 also assume that ISAs which don't have delays for these insns, don't
bdaaa2e1 215 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
9ce8a5dd 216#define ISA_HAS_COPROC_DELAYS(ISA) ( \
e7af610e
NC
217 (ISA) == ISA_MIPS1 \
218 || (ISA) == ISA_MIPS2 \
219 || (ISA) == ISA_MIPS3 \
9ce8a5dd
GRK
220 )
221
bdaaa2e1 222/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 223#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
224 (ISA) == ISA_MIPS3 \
225 || (ISA) == ISA_MIPS4 \
84ea6cf2 226 || (ISA) == ISA_MIPS5 \
d1cf510e 227 || (ISA) == ISA_MIPS64 \
9ce8a5dd
GRK
228 )
229
e013f690
TS
230#define HAVE_32BIT_GPRS \
231 (mips_gp32 \
232 || mips_abi == O32_ABI \
233 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 234
e013f690
TS
235#define HAVE_32BIT_FPRS \
236 (mips_fp32 \
237 || mips_abi == O32_ABI \
238 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257
RS
239
240#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
241#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
242
e013f690
TS
243#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
244
245#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
246
247/* We can only have 64bit addresses if the object file format
248 supports it. */
249#define HAVE_32BIT_ADDRESSES \
250 (HAVE_32BIT_GPRS \
251 || bfd_arch_bits_per_address (stdoutput) == 32 \
252 || ! HAVE_64BIT_OBJECTS)
253
254#define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
ca4e0257 255
bdaaa2e1 256/* Whether the processor uses hardware interlocks to protect
252b5132 257 reads from the HI and LO registers, and thus does not
ec68c924 258 require nops to be inserted. */
252b5132 259
ec68c924 260#define hilo_interlocks (mips_arch == CPU_R4010 \
252b5132
RH
261 )
262
263/* Whether the processor uses hardware interlocks to protect reads
264 from the GPRs, and thus does not require nops to be inserted. */
265#define gpr_interlocks \
e7af610e 266 (mips_opts.isa != ISA_MIPS1 \
ec68c924 267 || mips_arch == CPU_R3900)
252b5132
RH
268
269/* As with other "interlocks" this is used by hardware that has FP
270 (co-processor) interlocks. */
bdaaa2e1 271/* Itbl support may require additional care here. */
ec68c924 272#define cop_interlocks (mips_arch == CPU_R4300 \
252b5132
RH
273 )
274
6b76fefe
CM
275/* Is this a mfhi or mflo instruction? */
276#define MF_HILO_INSN(PINFO) \
277 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
278
252b5132
RH
279/* MIPS PIC level. */
280
e972090a
NC
281enum mips_pic_level
282{
252b5132
RH
283 /* Do not generate PIC code. */
284 NO_PIC,
285
286 /* Generate PIC code as in Irix 4. This is not implemented, and I'm
287 not sure what it is supposed to do. */
288 IRIX4_PIC,
289
290 /* Generate PIC code as in the SVR4 MIPS ABI. */
291 SVR4_PIC,
292
293 /* Generate PIC code without using a global offset table: the data
294 segment has a maximum size of 64K, all data references are off
295 the $gp register, and all text references are PC relative. This
296 is used on some embedded systems. */
297 EMBEDDED_PIC
298};
299
300static enum mips_pic_level mips_pic;
301
39c0a331
L
302/* Warn about all NOPS that the assembler generates. */
303static int warn_nops = 0;
304
252b5132
RH
305/* 1 if we should generate 32 bit offsets from the GP register in
306 SVR4_PIC mode. Currently has no meaning in other modes. */
307static int mips_big_got;
308
309/* 1 if trap instructions should used for overflow rather than break
310 instructions. */
311static int mips_trap;
312
119d663a
NC
313/* 1 if double width floating point constants should not be constructed
314 by a assembling two single width halves into two single width floating
315 point registers which just happen to alias the double width destination
316 register. On some architectures this aliasing can be disabled by a bit
d547a75e 317 in the status register, and the setting of this bit cannot be determined
119d663a
NC
318 automatically at assemble time. */
319static int mips_disable_float_construction;
320
252b5132
RH
321/* Non-zero if any .set noreorder directives were used. */
322
323static int mips_any_noreorder;
324
6b76fefe
CM
325/* Non-zero if nops should be inserted when the register referenced in
326 an mfhi/mflo instruction is read in the next two instructions. */
327static int mips_7000_hilo_fix;
328
252b5132 329/* The size of the small data section. */
156c2f8b 330static unsigned int g_switch_value = 8;
252b5132
RH
331/* Whether the -G option was used. */
332static int g_switch_seen = 0;
333
334#define N_RMASK 0xc4
335#define N_VFP 0xd4
336
337/* If we can determine in advance that GP optimization won't be
338 possible, we can skip the relaxation stuff that tries to produce
339 GP-relative references. This makes delay slot optimization work
340 better.
341
342 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
343 gcc output. It needs to guess right for gcc, otherwise gcc
344 will put what it thinks is a GP-relative instruction in a branch
345 delay slot.
252b5132
RH
346
347 I don't know if a fix is needed for the SVR4_PIC mode. I've only
348 fixed it for the non-PIC mode. KR 95/04/07 */
349static int nopic_need_relax PARAMS ((symbolS *, int));
350
351/* handle of the OPCODE hash table */
352static struct hash_control *op_hash = NULL;
353
354/* The opcode hash table we use for the mips16. */
355static struct hash_control *mips16_op_hash = NULL;
356
357/* This array holds the chars that always start a comment. If the
358 pre-processor is disabled, these aren't very useful */
359const char comment_chars[] = "#";
360
361/* This array holds the chars that only start a comment at the beginning of
362 a line. If the line seems to have the form '# 123 filename'
363 .line and .file directives will appear in the pre-processed output */
364/* Note that input_file.c hand checks for '#' at the beginning of the
365 first line of the input file. This is because the compiler outputs
bdaaa2e1 366 #NO_APP at the beginning of its output. */
252b5132
RH
367/* Also note that C style comments are always supported. */
368const char line_comment_chars[] = "#";
369
bdaaa2e1 370/* This array holds machine specific line separator characters. */
63a0b638 371const char line_separator_chars[] = ";";
252b5132
RH
372
373/* Chars that can be used to separate mant from exp in floating point nums */
374const char EXP_CHARS[] = "eE";
375
376/* Chars that mean this number is a floating point constant */
377/* As in 0f12.456 */
378/* or 0d1.2345e12 */
379const char FLT_CHARS[] = "rRsSfFdDxXpP";
380
381/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
382 changed in read.c . Ideally it shouldn't have to know about it at all,
383 but nothing is ideal around here.
384 */
385
386static char *insn_error;
387
388static int auto_align = 1;
389
390/* When outputting SVR4 PIC code, the assembler needs to know the
391 offset in the stack frame from which to restore the $gp register.
392 This is set by the .cprestore pseudo-op, and saved in this
393 variable. */
394static offsetT mips_cprestore_offset = -1;
395
396/* This is the register which holds the stack frame, as set by the
397 .frame pseudo-op. This is needed to implement .cprestore. */
398static int mips_frame_reg = SP;
399
400/* To output NOP instructions correctly, we need to keep information
401 about the previous two instructions. */
402
403/* Whether we are optimizing. The default value of 2 means to remove
404 unneeded NOPs and swap branch instructions when possible. A value
405 of 1 means to not swap branches. A value of 0 means to always
406 insert NOPs. */
407static int mips_optimize = 2;
408
409/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
410 equivalent to seeing no -g option at all. */
411static int mips_debug = 0;
412
413/* The previous instruction. */
414static struct mips_cl_insn prev_insn;
415
416/* The instruction before prev_insn. */
417static struct mips_cl_insn prev_prev_insn;
418
419/* If we don't want information for prev_insn or prev_prev_insn, we
420 point the insn_mo field at this dummy integer. */
43841e91 421static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
422
423/* Non-zero if prev_insn is valid. */
424static int prev_insn_valid;
425
426/* The frag for the previous instruction. */
427static struct frag *prev_insn_frag;
428
429/* The offset into prev_insn_frag for the previous instruction. */
430static long prev_insn_where;
431
432/* The reloc type for the previous instruction, if any. */
f6688943 433static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
434
435/* The reloc for the previous instruction, if any. */
f6688943 436static fixS *prev_insn_fixp[3];
252b5132
RH
437
438/* Non-zero if the previous instruction was in a delay slot. */
439static int prev_insn_is_delay_slot;
440
441/* Non-zero if the previous instruction was in a .set noreorder. */
442static int prev_insn_unreordered;
443
444/* Non-zero if the previous instruction uses an extend opcode (if
445 mips16). */
446static int prev_insn_extended;
447
448/* Non-zero if the previous previous instruction was in a .set
449 noreorder. */
450static int prev_prev_insn_unreordered;
451
452/* If this is set, it points to a frag holding nop instructions which
453 were inserted before the start of a noreorder section. If those
454 nops turn out to be unnecessary, the size of the frag can be
455 decreased. */
456static fragS *prev_nop_frag;
457
458/* The number of nop instructions we created in prev_nop_frag. */
459static int prev_nop_frag_holds;
460
461/* The number of nop instructions that we know we need in
bdaaa2e1 462 prev_nop_frag. */
252b5132
RH
463static int prev_nop_frag_required;
464
465/* The number of instructions we've seen since prev_nop_frag. */
466static int prev_nop_frag_since;
467
468/* For ECOFF and ELF, relocations against symbols are done in two
469 parts, with a HI relocation and a LO relocation. Each relocation
470 has only 16 bits of space to store an addend. This means that in
471 order for the linker to handle carries correctly, it must be able
472 to locate both the HI and the LO relocation. This means that the
473 relocations must appear in order in the relocation table.
474
475 In order to implement this, we keep track of each unmatched HI
476 relocation. We then sort them so that they immediately precede the
bdaaa2e1 477 corresponding LO relocation. */
252b5132 478
e972090a
NC
479struct mips_hi_fixup
480{
252b5132
RH
481 /* Next HI fixup. */
482 struct mips_hi_fixup *next;
483 /* This fixup. */
484 fixS *fixp;
485 /* The section this fixup is in. */
486 segT seg;
487};
488
489/* The list of unmatched HI relocs. */
490
491static struct mips_hi_fixup *mips_hi_fixup_list;
492
493/* Map normal MIPS register numbers to mips16 register numbers. */
494
495#define X ILLEGAL_REG
e972090a
NC
496static const int mips32_to_16_reg_map[] =
497{
252b5132
RH
498 X, X, 2, 3, 4, 5, 6, 7,
499 X, X, X, X, X, X, X, X,
500 0, 1, X, X, X, X, X, X,
501 X, X, X, X, X, X, X, X
502};
503#undef X
504
505/* Map mips16 register numbers to normal MIPS register numbers. */
506
e972090a
NC
507static const unsigned int mips16_to_32_reg_map[] =
508{
252b5132
RH
509 16, 17, 2, 3, 4, 5, 6, 7
510};
511\f
512/* Since the MIPS does not have multiple forms of PC relative
513 instructions, we do not have to do relaxing as is done on other
514 platforms. However, we do have to handle GP relative addressing
515 correctly, which turns out to be a similar problem.
516
517 Every macro that refers to a symbol can occur in (at least) two
518 forms, one with GP relative addressing and one without. For
519 example, loading a global variable into a register generally uses
520 a macro instruction like this:
521 lw $4,i
522 If i can be addressed off the GP register (this is true if it is in
523 the .sbss or .sdata section, or if it is known to be smaller than
524 the -G argument) this will generate the following instruction:
525 lw $4,i($gp)
526 This instruction will use a GPREL reloc. If i can not be addressed
527 off the GP register, the following instruction sequence will be used:
528 lui $at,i
529 lw $4,i($at)
530 In this case the first instruction will have a HI16 reloc, and the
531 second reloc will have a LO16 reloc. Both relocs will be against
532 the symbol i.
533
534 The issue here is that we may not know whether i is GP addressable
535 until after we see the instruction that uses it. Therefore, we
536 want to be able to choose the final instruction sequence only at
537 the end of the assembly. This is similar to the way other
538 platforms choose the size of a PC relative instruction only at the
539 end of assembly.
540
541 When generating position independent code we do not use GP
542 addressing in quite the same way, but the issue still arises as
543 external symbols and local symbols must be handled differently.
544
545 We handle these issues by actually generating both possible
546 instruction sequences. The longer one is put in a frag_var with
547 type rs_machine_dependent. We encode what to do with the frag in
548 the subtype field. We encode (1) the number of existing bytes to
549 replace, (2) the number of new bytes to use, (3) the offset from
550 the start of the existing bytes to the first reloc we must generate
551 (that is, the offset is applied from the start of the existing
552 bytes after they are replaced by the new bytes, if any), (4) the
553 offset from the start of the existing bytes to the second reloc,
554 (5) whether a third reloc is needed (the third reloc is always four
555 bytes after the second reloc), and (6) whether to warn if this
556 variant is used (this is sometimes needed if .set nomacro or .set
557 noat is in effect). All these numbers are reasonably small.
558
559 Generating two instruction sequences must be handled carefully to
560 ensure that delay slots are handled correctly. Fortunately, there
561 are a limited number of cases. When the second instruction
562 sequence is generated, append_insn is directed to maintain the
563 existing delay slot information, so it continues to apply to any
564 code after the second instruction sequence. This means that the
565 second instruction sequence must not impose any requirements not
566 required by the first instruction sequence.
567
568 These variant frags are then handled in functions called by the
569 machine independent code. md_estimate_size_before_relax returns
570 the final size of the frag. md_convert_frag sets up the final form
571 of the frag. tc_gen_reloc adjust the first reloc and adds a second
572 one if needed. */
573#define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
574 ((relax_substateT) \
575 (((old) << 23) \
576 | ((new) << 16) \
577 | (((reloc1) + 64) << 9) \
578 | (((reloc2) + 64) << 2) \
579 | ((reloc3) ? (1 << 1) : 0) \
580 | ((warn) ? 1 : 0)))
581#define RELAX_OLD(i) (((i) >> 23) & 0x7f)
582#define RELAX_NEW(i) (((i) >> 16) & 0x7f)
9a41af64
TS
583#define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
584#define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
252b5132
RH
585#define RELAX_RELOC3(i) (((i) >> 1) & 1)
586#define RELAX_WARN(i) ((i) & 1)
587
588/* For mips16 code, we use an entirely different form of relaxation.
589 mips16 supports two versions of most instructions which take
590 immediate values: a small one which takes some small value, and a
591 larger one which takes a 16 bit value. Since branches also follow
592 this pattern, relaxing these values is required.
593
594 We can assemble both mips16 and normal MIPS code in a single
595 object. Therefore, we need to support this type of relaxation at
596 the same time that we support the relaxation described above. We
597 use the high bit of the subtype field to distinguish these cases.
598
599 The information we store for this type of relaxation is the
600 argument code found in the opcode file for this relocation, whether
601 the user explicitly requested a small or extended form, and whether
602 the relocation is in a jump or jal delay slot. That tells us the
603 size of the value, and how it should be stored. We also store
604 whether the fragment is considered to be extended or not. We also
605 store whether this is known to be a branch to a different section,
606 whether we have tried to relax this frag yet, and whether we have
607 ever extended a PC relative fragment because of a shift count. */
608#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
609 (0x80000000 \
610 | ((type) & 0xff) \
611 | ((small) ? 0x100 : 0) \
612 | ((ext) ? 0x200 : 0) \
613 | ((dslot) ? 0x400 : 0) \
614 | ((jal_dslot) ? 0x800 : 0))
615#define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
616#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
617#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
618#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
619#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
620#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
621#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
622#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
623#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
624#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
625#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
626#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
627\f
628/* Prototypes for static functions. */
629
630#ifdef __STDC__
631#define internalError() \
632 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
633#else
634#define internalError() as_fatal (_("MIPS internal Error"));
635#endif
636
637enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
638
639static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
640 unsigned int reg, enum mips_regclass class));
156c2f8b 641static int reg_needs_delay PARAMS ((unsigned int));
252b5132
RH
642static void mips16_mark_labels PARAMS ((void));
643static void append_insn PARAMS ((char *place,
644 struct mips_cl_insn * ip,
645 expressionS * p,
f6688943 646 bfd_reloc_code_real_type *r,
252b5132
RH
647 boolean));
648static void mips_no_prev_insn PARAMS ((int));
649static void mips_emit_delays PARAMS ((boolean));
650#ifdef USE_STDARG
651static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
652 const char *name, const char *fmt,
653 ...));
654#else
655static void macro_build ();
656#endif
657static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
658 const char *, const char *,
659 va_list));
660static void macro_build_lui PARAMS ((char *place, int *counter,
661 expressionS * ep, int regnum));
662static void set_at PARAMS ((int *counter, int reg, int unsignedp));
663static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
664 expressionS *));
665static void load_register PARAMS ((int *, int, expressionS *, int));
666static void load_address PARAMS ((int *counter, int reg, expressionS *ep));
ea1fb5dc 667static void move_register PARAMS ((int *, int, int));
252b5132
RH
668static void macro PARAMS ((struct mips_cl_insn * ip));
669static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
670#ifdef LOSING_COMPILER
671static void macro2 PARAMS ((struct mips_cl_insn * ip));
672#endif
673static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
674static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
675static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
676 boolean, boolean, unsigned long *,
677 boolean *, unsigned short *));
678static int my_getSmallExpression PARAMS ((expressionS * ep, char *str));
679static void my_getExpression PARAMS ((expressionS * ep, char *str));
e013f690 680static int support_64bit_objects PARAMS((void));
252b5132
RH
681static symbolS *get_symbol PARAMS ((void));
682static void mips_align PARAMS ((int to, int fill, symbolS *label));
683static void s_align PARAMS ((int));
684static void s_change_sec PARAMS ((int));
685static void s_cons PARAMS ((int));
686static void s_float_cons PARAMS ((int));
687static void s_mips_globl PARAMS ((int));
688static void s_option PARAMS ((int));
689static void s_mipsset PARAMS ((int));
690static void s_abicalls PARAMS ((int));
691static void s_cpload PARAMS ((int));
692static void s_cprestore PARAMS ((int));
693static void s_gpword PARAMS ((int));
694static void s_cpadd PARAMS ((int));
695static void s_insn PARAMS ((int));
696static void md_obj_begin PARAMS ((void));
697static void md_obj_end PARAMS ((void));
698static long get_number PARAMS ((void));
699static void s_mips_ent PARAMS ((int));
700static void s_mips_end PARAMS ((int));
701static void s_mips_frame PARAMS ((int));
702static void s_mips_mask PARAMS ((int));
703static void s_mips_stab PARAMS ((int));
704static void s_mips_weakext PARAMS ((int));
705static void s_file PARAMS ((int));
706static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
e7af610e
NC
707static const char *mips_isa_to_str PARAMS ((int));
708static const char *mips_cpu_to_str PARAMS ((int));
252b5132 709static int validate_mips_insn PARAMS ((const struct mips_opcode *));
e87a0284 710static void show PARAMS ((FILE *, char *, int *, int *));
e7af610e 711
fb1b3232
TS
712/* Return values of my_getSmallExpression() */
713
714enum
715{
716 S_EX_NONE = 0,
717 S_EX_LO,
718 S_EX_HI,
719 S_EX_HIGHER,
720 S_EX_HIGHEST,
721 S_EX_GPREL,
722 S_EX_NEG
723};
724
e7af610e
NC
725/* Table and functions used to map between CPU/ISA names, and
726 ISA levels, and CPU numbers. */
727
e972090a
NC
728struct mips_cpu_info
729{
e7af610e
NC
730 const char *name; /* CPU or ISA name. */
731 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
732 int isa; /* ISA level. */
733 int cpu; /* CPU number (default CPU if ISA). */
734};
735
736static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
737static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
738static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
252b5132
RH
739\f
740/* Pseudo-op table.
741
742 The following pseudo-ops from the Kane and Heinrich MIPS book
743 should be defined here, but are currently unsupported: .alias,
744 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
745
746 The following pseudo-ops from the Kane and Heinrich MIPS book are
747 specific to the type of debugging information being generated, and
748 should be defined by the object format: .aent, .begin, .bend,
749 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
750 .vreg.
751
752 The following pseudo-ops from the Kane and Heinrich MIPS book are
753 not MIPS CPU specific, but are also not specific to the object file
754 format. This file is probably the best place to define them, but
755 they are not currently supported: .asm0, .endr, .lab, .repeat,
756 .struct. */
757
e972090a
NC
758static const pseudo_typeS mips_pseudo_table[] =
759{
beae10d5 760 /* MIPS specific pseudo-ops. */
252b5132
RH
761 {"option", s_option, 0},
762 {"set", s_mipsset, 0},
763 {"rdata", s_change_sec, 'r'},
764 {"sdata", s_change_sec, 's'},
765 {"livereg", s_ignore, 0},
766 {"abicalls", s_abicalls, 0},
767 {"cpload", s_cpload, 0},
768 {"cprestore", s_cprestore, 0},
769 {"gpword", s_gpword, 0},
770 {"cpadd", s_cpadd, 0},
771 {"insn", s_insn, 0},
772
beae10d5 773 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
774 chips. */
775 {"asciiz", stringer, 1},
776 {"bss", s_change_sec, 'b'},
777 {"err", s_err, 0},
778 {"half", s_cons, 1},
779 {"dword", s_cons, 3},
780 {"weakext", s_mips_weakext, 0},
781
beae10d5 782 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
783 here for one reason or another. */
784 {"align", s_align, 0},
785 {"byte", s_cons, 0},
786 {"data", s_change_sec, 'd'},
787 {"double", s_float_cons, 'd'},
788 {"float", s_float_cons, 'f'},
789 {"globl", s_mips_globl, 0},
790 {"global", s_mips_globl, 0},
791 {"hword", s_cons, 1},
792 {"int", s_cons, 2},
793 {"long", s_cons, 2},
794 {"octa", s_cons, 4},
795 {"quad", s_cons, 3},
796 {"short", s_cons, 1},
797 {"single", s_float_cons, 'f'},
798 {"stabn", s_mips_stab, 'n'},
799 {"text", s_change_sec, 't'},
800 {"word", s_cons, 2},
add56521
L
801
802#ifdef MIPS_STABS_ELF
803 { "extern", ecoff_directive_extern, 0},
804#endif
805
43841e91 806 { NULL, NULL, 0 },
252b5132
RH
807};
808
e972090a
NC
809static const pseudo_typeS mips_nonecoff_pseudo_table[] =
810{
beae10d5
KH
811 /* These pseudo-ops should be defined by the object file format.
812 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
813 {"aent", s_mips_ent, 1},
814 {"bgnb", s_ignore, 0},
815 {"end", s_mips_end, 0},
816 {"endb", s_ignore, 0},
817 {"ent", s_mips_ent, 0},
818 {"file", s_file, 0},
819 {"fmask", s_mips_mask, 'F'},
820 {"frame", s_mips_frame, 0},
821 {"loc", s_ignore, 0},
822 {"mask", s_mips_mask, 'R'},
823 {"verstamp", s_ignore, 0},
43841e91 824 { NULL, NULL, 0 },
252b5132
RH
825};
826
827extern void pop_insert PARAMS ((const pseudo_typeS *));
828
829void
830mips_pop_insert ()
831{
832 pop_insert (mips_pseudo_table);
833 if (! ECOFF_DEBUGGING)
834 pop_insert (mips_nonecoff_pseudo_table);
835}
836\f
837/* Symbols labelling the current insn. */
838
e972090a
NC
839struct insn_label_list
840{
252b5132
RH
841 struct insn_label_list *next;
842 symbolS *label;
843};
844
845static struct insn_label_list *insn_labels;
846static struct insn_label_list *free_insn_labels;
847
848static void mips_clear_insn_labels PARAMS ((void));
849
850static inline void
851mips_clear_insn_labels ()
852{
853 register struct insn_label_list **pl;
854
855 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
856 ;
857 *pl = insn_labels;
858 insn_labels = NULL;
859}
860\f
861static char *expr_end;
862
863/* Expressions which appear in instructions. These are set by
864 mips_ip. */
865
866static expressionS imm_expr;
867static expressionS offset_expr;
868
869/* Relocs associated with imm_expr and offset_expr. */
870
f6688943
TS
871static bfd_reloc_code_real_type imm_reloc[3]
872 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
873static bfd_reloc_code_real_type offset_reloc[3]
874 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
875
876/* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
877
878static boolean imm_unmatched_hi;
879
880/* These are set by mips16_ip if an explicit extension is used. */
881
882static boolean mips16_small, mips16_ext;
883
884#ifdef MIPS_STABS_ELF
885/* The pdr segment for per procedure frame/regmask info */
886
887static segT pdr_seg;
888#endif
889
e7af610e
NC
890static const char *
891mips_isa_to_str (isa)
892 int isa;
893{
894 const struct mips_cpu_info *ci;
895 static char s[20];
896
897 ci = mips_cpu_info_from_isa (isa);
898 if (ci != NULL)
899 return (ci->name);
900
901 sprintf (s, "ISA#%d", isa);
902 return s;
903}
904
905static const char *
156c2f8b
NC
906mips_cpu_to_str (cpu)
907 int cpu;
908{
e7af610e 909 const struct mips_cpu_info *ci;
156c2f8b 910 static char s[16];
e7af610e
NC
911
912 ci = mips_cpu_info_from_cpu (cpu);
913 if (ci != NULL)
914 return (ci->name);
915
916 sprintf (s, "CPU#%d", cpu);
917 return s;
156c2f8b
NC
918}
919
e013f690
TS
920/* The default target format to use. */
921
922const char *
923mips_target_format ()
924{
925 switch (OUTPUT_FLAVOR)
926 {
927 case bfd_target_aout_flavour:
928 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
929 case bfd_target_ecoff_flavour:
930 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
931 case bfd_target_coff_flavour:
932 return "pe-mips";
933 case bfd_target_elf_flavour:
934#ifdef TE_TMIPS
935 /* This is traditional mips */
936 return (target_big_endian
937 ? (HAVE_64BIT_OBJECTS ? "elf64-tradbigmips"
938 : "elf32-tradbigmips")
939 : (HAVE_64BIT_OBJECTS ? "elf64-tradlittlemips"
940 : "elf32-tradlittlemips"));
941#else
942 return (target_big_endian
943 ? (HAVE_64BIT_OBJECTS ? "elf64-bigmips" : "elf32-bigmips")
944 : (HAVE_64BIT_OBJECTS ? "elf64-littlemips"
945 : "elf32-littlemips"));
946#endif
947 default:
948 abort ();
949 return NULL;
950 }
951}
952
156c2f8b
NC
953/* This function is called once, at assembler startup time. It should
954 set up all the tables, etc. that the MD part of the assembler will need. */
955
252b5132
RH
956void
957md_begin ()
958{
252b5132 959 register const char *retval = NULL;
156c2f8b 960 int i = 0;
252b5132
RH
961 const char *cpu;
962 char *a = NULL;
963 int broken = 0;
964 int mips_isa_from_cpu;
e7af610e
NC
965 int target_cpu_had_mips16 = 0;
966 const struct mips_cpu_info *ci;
252b5132 967
056350c6
NC
968 /* GP relative stuff not working for PE */
969 if (strncmp (TARGET_OS, "pe", 2) == 0
970 && g_switch_value != 0)
971 {
972 if (g_switch_seen)
973 as_bad (_("-G not supported in this configuration."));
974 g_switch_value = 0;
975 }
976
252b5132
RH
977 cpu = TARGET_CPU;
978 if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
979 {
980 a = xmalloc (sizeof TARGET_CPU);
981 strcpy (a, TARGET_CPU);
982 a[(sizeof TARGET_CPU) - 3] = '\0';
983 cpu = a;
984 }
985
e7af610e 986 if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
252b5132 987 {
e7af610e
NC
988 target_cpu_had_mips16 = 1;
989 cpu += sizeof "mips16" - 1;
252b5132
RH
990 }
991
e7af610e
NC
992 if (mips_opts.mips16 < 0)
993 mips_opts.mips16 = target_cpu_had_mips16;
252b5132 994
6dce9e24
TS
995 /* Backward compatibility for historic -mcpu= option. Check for
996 incompatible options, warn if -mcpu is used. */
997 if (mips_cpu != CPU_UNKNOWN
998 && mips_arch != CPU_UNKNOWN
999 && mips_cpu != mips_arch)
1000 {
1001 as_fatal (_("The -mcpu option can't be used together with -march. "
1002 "Use -mtune instead of -mcpu."));
1003 }
1004
1005 if (mips_cpu != CPU_UNKNOWN
1006 && mips_tune != CPU_UNKNOWN
1007 && mips_cpu != mips_tune)
1008 {
1009 as_fatal (_("The -mcpu option can't be used together with -mtune. "
1010 "Use -march instead of -mcpu."));
1011 }
1012
1013 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1014 {
1015 ci = mips_cpu_info_from_cpu (mips_cpu);
1016 assert (ci != NULL);
1017 mips_arch = ci->cpu;
1018 as_warn (_("The -mcpu option is deprecated. Please use -march and "
1019 "-mtune instead."));
1020 }
1021
ec68c924 1022 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
e7af610e
NC
1023 specified on the command line, or some other value if one was.
1024 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1025 the command line, or will be set otherwise if one was. */
ec68c924 1026 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
252b5132 1027 {
1ff54a84
TS
1028 /* We have to check if the isa is the default isa of arch. Otherwise
1029 we'll get invalid object file headers. */
1030 ci = mips_cpu_info_from_cpu (mips_arch);
1031 assert (ci != NULL);
1032 if (mips_opts.isa != ci->isa)
1033 {
1034 /* This really should be an error instead of a warning, but old
1035 compilers only have -mcpu which sets both arch and tune. For
1036 now, we discard arch and preserve tune. */
1037 as_warn (_("The -march option is incompatible to -mipsN and "
1038 "therefore ignored."));
1039 if (mips_tune == CPU_UNKNOWN)
1040 mips_tune = mips_arch;
1041 ci = mips_cpu_info_from_isa (mips_opts.isa);
1042 assert (ci != NULL);
1043 mips_arch = ci->cpu;
1044 }
252b5132 1045 }
ec68c924 1046 else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
252b5132 1047 {
ec68c924
EC
1048 /* We have ARCH, we need ISA. */
1049 ci = mips_cpu_info_from_cpu (mips_arch);
e7af610e
NC
1050 assert (ci != NULL);
1051 mips_opts.isa = ci->isa;
1052 }
ec68c924 1053 else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
e7af610e 1054 {
ec68c924 1055 /* We have ISA, we need default ARCH. */
e7af610e
NC
1056 ci = mips_cpu_info_from_isa (mips_opts.isa);
1057 assert (ci != NULL);
ec68c924
EC
1058 mips_arch = ci->cpu;
1059 }
e7af610e
NC
1060 else
1061 {
ec68c924 1062 /* We need to set both ISA and ARCH from target cpu. */
e7af610e
NC
1063 ci = mips_cpu_info_from_name (cpu);
1064 if (ci == NULL)
beae10d5 1065 ci = mips_cpu_info_from_cpu (CPU_R3000);
e7af610e
NC
1066 assert (ci != NULL);
1067 mips_opts.isa = ci->isa;
ec68c924 1068 mips_arch = ci->cpu;
252b5132
RH
1069 }
1070
ec68c924
EC
1071 if (mips_tune == CPU_UNKNOWN)
1072 mips_tune = mips_arch;
1073
1074 ci = mips_cpu_info_from_cpu (mips_arch);
e7af610e
NC
1075 assert (ci != NULL);
1076 mips_isa_from_cpu = ci->isa;
1077
252b5132 1078 /* End of TARGET_CPU processing, get rid of malloced memory
bdaaa2e1 1079 if necessary. */
252b5132
RH
1080 cpu = NULL;
1081 if (a != NULL)
1082 {
156c2f8b
NC
1083 free (a);
1084 a = NULL;
252b5132
RH
1085 }
1086
e7af610e 1087 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
252b5132
RH
1088 as_bad (_("trap exception not supported at ISA 1"));
1089
1090 /* Set the EABI kind based on the ISA before the user gets
1091 to change the ISA with directives. This isn't really
bdaaa2e1 1092 the best, but then neither is basing the abi on the isa. */
9ce8a5dd 1093 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
e013f690 1094 && mips_abi == EABI_ABI)
252b5132
RH
1095 mips_eabi64 = 1;
1096
e7af610e
NC
1097 /* If they asked for mips1 or mips2 and a cpu that is
1098 mips3 or greater, then mark the object file 32BITMODE. */
1099 if (mips_isa_from_cpu != ISA_UNKNOWN
1100 && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
1101 && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
1102 mips_32bitmode = 1;
252b5132 1103
ec68c924 1104 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
252b5132
RH
1105 as_warn (_("Could not set architecture and machine"));
1106
1107 file_mips_isa = mips_opts.isa;
1108
1109 op_hash = hash_new ();
1110
1111 for (i = 0; i < NUMOPCODES;)
1112 {
1113 const char *name = mips_opcodes[i].name;
1114
1115 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1116 if (retval != NULL)
1117 {
1118 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1119 mips_opcodes[i].name, retval);
1120 /* Probably a memory allocation problem? Give up now. */
1121 as_fatal (_("Broken assembler. No assembly attempted."));
1122 }
1123 do
1124 {
1125 if (mips_opcodes[i].pinfo != INSN_MACRO)
1126 {
1127 if (!validate_mips_insn (&mips_opcodes[i]))
1128 broken = 1;
1129 }
1130 ++i;
1131 }
1132 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1133 }
1134
1135 mips16_op_hash = hash_new ();
1136
1137 i = 0;
1138 while (i < bfd_mips16_num_opcodes)
1139 {
1140 const char *name = mips16_opcodes[i].name;
1141
1142 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1143 if (retval != NULL)
1144 as_fatal (_("internal: can't hash `%s': %s"),
1145 mips16_opcodes[i].name, retval);
1146 do
1147 {
1148 if (mips16_opcodes[i].pinfo != INSN_MACRO
1149 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1150 != mips16_opcodes[i].match))
1151 {
1152 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1153 mips16_opcodes[i].name, mips16_opcodes[i].args);
1154 broken = 1;
1155 }
1156 ++i;
1157 }
1158 while (i < bfd_mips16_num_opcodes
1159 && strcmp (mips16_opcodes[i].name, name) == 0);
1160 }
1161
1162 if (broken)
1163 as_fatal (_("Broken assembler. No assembly attempted."));
1164
1165 /* We add all the general register names to the symbol table. This
1166 helps us detect invalid uses of them. */
1167 for (i = 0; i < 32; i++)
1168 {
1169 char buf[5];
1170
1171 sprintf (buf, "$%d", i);
1172 symbol_table_insert (symbol_new (buf, reg_section, i,
1173 &zero_address_frag));
1174 }
1175 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1176 &zero_address_frag));
1177 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1178 &zero_address_frag));
1179 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1180 &zero_address_frag));
1181 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1182 &zero_address_frag));
1183 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1184 &zero_address_frag));
1185 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1186 &zero_address_frag));
1187 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1188 &zero_address_frag));
1189
1190 mips_no_prev_insn (false);
1191
1192 mips_gprmask = 0;
1193 mips_cprmask[0] = 0;
1194 mips_cprmask[1] = 0;
1195 mips_cprmask[2] = 0;
1196 mips_cprmask[3] = 0;
1197
1198 /* set the default alignment for the text section (2**2) */
1199 record_alignment (text_section, 2);
1200
1201 if (USE_GLOBAL_POINTER_OPT)
1202 bfd_set_gp_size (stdoutput, g_switch_value);
1203
1204 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1205 {
1206 /* On a native system, sections must be aligned to 16 byte
1207 boundaries. When configured for an embedded ELF target, we
1208 don't bother. */
1209 if (strcmp (TARGET_OS, "elf") != 0)
1210 {
1211 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1212 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1213 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1214 }
1215
1216 /* Create a .reginfo section for register masks and a .mdebug
1217 section for debugging information. */
1218 {
1219 segT seg;
1220 subsegT subseg;
1221 flagword flags;
1222 segT sec;
1223
1224 seg = now_seg;
1225 subseg = now_subseg;
1226
1227 /* The ABI says this section should be loaded so that the
1228 running program can access it. However, we don't load it
1229 if we are configured for an embedded target */
1230 flags = SEC_READONLY | SEC_DATA;
1231 if (strcmp (TARGET_OS, "elf") != 0)
1232 flags |= SEC_ALLOC | SEC_LOAD;
1233
e013f690 1234 if (! HAVE_NEWABI)
252b5132
RH
1235 {
1236 sec = subseg_new (".reginfo", (subsegT) 0);
1237
252b5132
RH
1238 (void) bfd_set_section_flags (stdoutput, sec, flags);
1239 (void) bfd_set_section_alignment (stdoutput, sec, 2);
bdaaa2e1 1240
252b5132
RH
1241#ifdef OBJ_ELF
1242 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1243#endif
1244 }
1245 else
1246 {
1247 /* The 64-bit ABI uses a .MIPS.options section rather than
1248 .reginfo section. */
1249 sec = subseg_new (".MIPS.options", (subsegT) 0);
1250 (void) bfd_set_section_flags (stdoutput, sec, flags);
1251 (void) bfd_set_section_alignment (stdoutput, sec, 3);
1252
1253#ifdef OBJ_ELF
1254 /* Set up the option header. */
1255 {
1256 Elf_Internal_Options opthdr;
1257 char *f;
1258
1259 opthdr.kind = ODK_REGINFO;
1260 opthdr.size = (sizeof (Elf_External_Options)
1261 + sizeof (Elf64_External_RegInfo));
1262 opthdr.section = 0;
1263 opthdr.info = 0;
1264 f = frag_more (sizeof (Elf_External_Options));
1265 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1266 (Elf_External_Options *) f);
1267
1268 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1269 }
1270#endif
1271 }
1272
1273 if (ECOFF_DEBUGGING)
1274 {
1275 sec = subseg_new (".mdebug", (subsegT) 0);
1276 (void) bfd_set_section_flags (stdoutput, sec,
1277 SEC_HAS_CONTENTS | SEC_READONLY);
1278 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1279 }
1280
1281#ifdef MIPS_STABS_ELF
1282 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1283 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1284 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1285 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1286#endif
1287
1288 subseg_set (seg, subseg);
1289 }
1290 }
1291
1292 if (! ECOFF_DEBUGGING)
1293 md_obj_begin ();
1294}
1295
1296void
1297md_mips_end ()
1298{
1299 if (! ECOFF_DEBUGGING)
1300 md_obj_end ();
1301}
1302
1303void
1304md_assemble (str)
1305 char *str;
1306{
1307 struct mips_cl_insn insn;
f6688943
TS
1308 bfd_reloc_code_real_type unused_reloc[3]
1309 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1310
1311 imm_expr.X_op = O_absent;
252b5132
RH
1312 imm_unmatched_hi = false;
1313 offset_expr.X_op = O_absent;
f6688943
TS
1314 imm_reloc[0] = BFD_RELOC_UNUSED;
1315 imm_reloc[1] = BFD_RELOC_UNUSED;
1316 imm_reloc[2] = BFD_RELOC_UNUSED;
1317 offset_reloc[0] = BFD_RELOC_UNUSED;
1318 offset_reloc[1] = BFD_RELOC_UNUSED;
1319 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1320
1321 if (mips_opts.mips16)
1322 mips16_ip (str, &insn);
1323 else
1324 {
1325 mips_ip (str, &insn);
beae10d5
KH
1326 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1327 str, insn.insn_opcode));
252b5132
RH
1328 }
1329
1330 if (insn_error)
1331 {
1332 as_bad ("%s `%s'", insn_error, str);
1333 return;
1334 }
1335
1336 if (insn.insn_mo->pinfo == INSN_MACRO)
1337 {
1338 if (mips_opts.mips16)
1339 mips16_macro (&insn);
1340 else
1341 macro (&insn);
1342 }
1343 else
1344 {
1345 if (imm_expr.X_op != O_absent)
1346 append_insn ((char *) NULL, &insn, &imm_expr, imm_reloc,
1347 imm_unmatched_hi);
1348 else if (offset_expr.X_op != O_absent)
1349 append_insn ((char *) NULL, &insn, &offset_expr, offset_reloc, false);
1350 else
f6688943 1351 append_insn ((char *) NULL, &insn, NULL, unused_reloc, false);
252b5132
RH
1352 }
1353}
1354
1355/* See whether instruction IP reads register REG. CLASS is the type
1356 of register. */
1357
1358static int
1359insn_uses_reg (ip, reg, class)
1360 struct mips_cl_insn *ip;
1361 unsigned int reg;
1362 enum mips_regclass class;
1363{
1364 if (class == MIPS16_REG)
1365 {
1366 assert (mips_opts.mips16);
1367 reg = mips16_to_32_reg_map[reg];
1368 class = MIPS_GR_REG;
1369 }
1370
1371 /* Don't report on general register 0, since it never changes. */
1372 if (class == MIPS_GR_REG && reg == 0)
1373 return 0;
1374
1375 if (class == MIPS_FP_REG)
1376 {
1377 assert (! mips_opts.mips16);
1378 /* If we are called with either $f0 or $f1, we must check $f0.
1379 This is not optimal, because it will introduce an unnecessary
1380 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1381 need to distinguish reading both $f0 and $f1 or just one of
1382 them. Note that we don't have to check the other way,
1383 because there is no instruction that sets both $f0 and $f1
1384 and requires a delay. */
1385 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1386 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1387 == (reg &~ (unsigned) 1)))
1388 return 1;
1389 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1390 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1391 == (reg &~ (unsigned) 1)))
1392 return 1;
1393 }
1394 else if (! mips_opts.mips16)
1395 {
1396 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1397 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1398 return 1;
1399 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1400 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1401 return 1;
1402 }
1403 else
1404 {
1405 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1406 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1407 & MIPS16OP_MASK_RX)]
1408 == reg))
1409 return 1;
1410 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1411 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1412 & MIPS16OP_MASK_RY)]
1413 == reg))
1414 return 1;
1415 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1416 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1417 & MIPS16OP_MASK_MOVE32Z)]
1418 == reg))
1419 return 1;
1420 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1421 return 1;
1422 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1423 return 1;
1424 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1425 return 1;
1426 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1427 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1428 & MIPS16OP_MASK_REGR32) == reg)
1429 return 1;
1430 }
1431
1432 return 0;
1433}
1434
1435/* This function returns true if modifying a register requires a
1436 delay. */
1437
1438static int
1439reg_needs_delay (reg)
156c2f8b 1440 unsigned int reg;
252b5132
RH
1441{
1442 unsigned long prev_pinfo;
1443
1444 prev_pinfo = prev_insn.insn_mo->pinfo;
1445 if (! mips_opts.noreorder
9ce8a5dd 1446 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1447 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1448 || (! gpr_interlocks
1449 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1450 {
1451 /* A load from a coprocessor or from memory. All load
1452 delays delay the use of general register rt for one
1453 instruction on the r3000. The r6000 and r4000 use
1454 interlocks. */
bdaaa2e1 1455 /* Itbl support may require additional care here. */
252b5132
RH
1456 know (prev_pinfo & INSN_WRITE_GPR_T);
1457 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1458 return 1;
1459 }
1460
1461 return 0;
1462}
1463
1464/* Mark instruction labels in mips16 mode. This permits the linker to
1465 handle them specially, such as generating jalx instructions when
1466 needed. We also make them odd for the duration of the assembly, in
1467 order to generate the right sort of code. We will make them even
1468 in the adjust_symtab routine, while leaving them marked. This is
1469 convenient for the debugger and the disassembler. The linker knows
1470 to make them odd again. */
1471
1472static void
1473mips16_mark_labels ()
1474{
1475 if (mips_opts.mips16)
1476 {
1477 struct insn_label_list *l;
98aa84af 1478 valueT val;
252b5132
RH
1479
1480 for (l = insn_labels; l != NULL; l = l->next)
1481 {
1482#ifdef OBJ_ELF
1483 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1484 S_SET_OTHER (l->label, STO_MIPS16);
1485#endif
98aa84af
AM
1486 val = S_GET_VALUE (l->label);
1487 if ((val & 1) == 0)
1488 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1489 }
1490 }
1491}
1492
1493/* Output an instruction. PLACE is where to put the instruction; if
1494 it is NULL, this uses frag_more to get room. IP is the instruction
1495 information. ADDRESS_EXPR is an operand of the instruction to be
1496 used with RELOC_TYPE. */
1497
1498static void
1499append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1500 char *place;
1501 struct mips_cl_insn *ip;
1502 expressionS *address_expr;
f6688943 1503 bfd_reloc_code_real_type *reloc_type;
252b5132
RH
1504 boolean unmatched_hi;
1505{
1506 register unsigned long prev_pinfo, pinfo;
1507 char *f;
f6688943 1508 fixS *fixp[3];
252b5132
RH
1509 int nops = 0;
1510
1511 /* Mark instruction labels in mips16 mode. */
1512 if (mips_opts.mips16)
1513 mips16_mark_labels ();
1514
1515 prev_pinfo = prev_insn.insn_mo->pinfo;
1516 pinfo = ip->insn_mo->pinfo;
1517
1518 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1519 {
1520 int prev_prev_nop;
1521
1522 /* If the previous insn required any delay slots, see if we need
1523 to insert a NOP or two. There are eight kinds of possible
1524 hazards, of which an instruction can have at most one type.
1525 (1) a load from memory delay
1526 (2) a load from a coprocessor delay
1527 (3) an unconditional branch delay
1528 (4) a conditional branch delay
1529 (5) a move to coprocessor register delay
1530 (6) a load coprocessor register from memory delay
1531 (7) a coprocessor condition code delay
1532 (8) a HI/LO special register delay
1533
1534 There are a lot of optimizations we could do that we don't.
1535 In particular, we do not, in general, reorder instructions.
1536 If you use gcc with optimization, it will reorder
1537 instructions and generally do much more optimization then we
1538 do here; repeating all that work in the assembler would only
1539 benefit hand written assembly code, and does not seem worth
1540 it. */
1541
1542 /* This is how a NOP is emitted. */
1543#define emit_nop() \
1544 (mips_opts.mips16 \
1545 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1546 : md_number_to_chars (frag_more (4), 0, 4))
1547
1548 /* The previous insn might require a delay slot, depending upon
1549 the contents of the current insn. */
1550 if (! mips_opts.mips16
9ce8a5dd 1551 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1552 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1553 && ! cop_interlocks)
1554 || (! gpr_interlocks
1555 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1556 {
1557 /* A load from a coprocessor or from memory. All load
1558 delays delay the use of general register rt for one
1559 instruction on the r3000. The r6000 and r4000 use
1560 interlocks. */
beae10d5 1561 /* Itbl support may require additional care here. */
252b5132
RH
1562 know (prev_pinfo & INSN_WRITE_GPR_T);
1563 if (mips_optimize == 0
1564 || insn_uses_reg (ip,
1565 ((prev_insn.insn_opcode >> OP_SH_RT)
1566 & OP_MASK_RT),
1567 MIPS_GR_REG))
1568 ++nops;
1569 }
1570 else if (! mips_opts.mips16
9ce8a5dd 1571 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 1572 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1573 && ! cop_interlocks)
e7af610e 1574 || (mips_opts.isa == ISA_MIPS1
252b5132
RH
1575 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1576 {
1577 /* A generic coprocessor delay. The previous instruction
1578 modified a coprocessor general or control register. If
1579 it modified a control register, we need to avoid any
1580 coprocessor instruction (this is probably not always
1581 required, but it sometimes is). If it modified a general
1582 register, we avoid using that register.
1583
1584 On the r6000 and r4000 loading a coprocessor register
1585 from memory is interlocked, and does not require a delay.
1586
1587 This case is not handled very well. There is no special
1588 knowledge of CP0 handling, and the coprocessors other
1589 than the floating point unit are not distinguished at
1590 all. */
1591 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1592 Need to modify this to include knowledge about
252b5132
RH
1593 user specified delays! */
1594 if (prev_pinfo & INSN_WRITE_FPR_T)
1595 {
1596 if (mips_optimize == 0
1597 || insn_uses_reg (ip,
1598 ((prev_insn.insn_opcode >> OP_SH_FT)
1599 & OP_MASK_FT),
1600 MIPS_FP_REG))
1601 ++nops;
1602 }
1603 else if (prev_pinfo & INSN_WRITE_FPR_S)
1604 {
1605 if (mips_optimize == 0
1606 || insn_uses_reg (ip,
1607 ((prev_insn.insn_opcode >> OP_SH_FS)
1608 & OP_MASK_FS),
1609 MIPS_FP_REG))
1610 ++nops;
1611 }
1612 else
1613 {
1614 /* We don't know exactly what the previous instruction
1615 does. If the current instruction uses a coprocessor
1616 register, we must insert a NOP. If previous
1617 instruction may set the condition codes, and the
1618 current instruction uses them, we must insert two
1619 NOPS. */
bdaaa2e1 1620 /* Itbl support may require additional care here. */
252b5132
RH
1621 if (mips_optimize == 0
1622 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1623 && (pinfo & INSN_READ_COND_CODE)))
1624 nops += 2;
1625 else if (pinfo & INSN_COP)
1626 ++nops;
1627 }
1628 }
1629 else if (! mips_opts.mips16
9ce8a5dd 1630 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1631 && (prev_pinfo & INSN_WRITE_COND_CODE)
1632 && ! cop_interlocks)
1633 {
1634 /* The previous instruction sets the coprocessor condition
1635 codes, but does not require a general coprocessor delay
1636 (this means it is a floating point comparison
1637 instruction). If this instruction uses the condition
1638 codes, we need to insert a single NOP. */
beae10d5 1639 /* Itbl support may require additional care here. */
252b5132
RH
1640 if (mips_optimize == 0
1641 || (pinfo & INSN_READ_COND_CODE))
1642 ++nops;
1643 }
6b76fefe
CM
1644
1645 /* If we're fixing up mfhi/mflo for the r7000 and the
1646 previous insn was an mfhi/mflo and the current insn
1647 reads the register that the mfhi/mflo wrote to, then
1648 insert two nops. */
1649
1650 else if (mips_7000_hilo_fix
1651 && MF_HILO_INSN (prev_pinfo)
1652 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1653 & OP_MASK_RD),
1654 MIPS_GR_REG))
6b76fefe
CM
1655 {
1656 nops += 2;
1657 }
1658
1659 /* If we're fixing up mfhi/mflo for the r7000 and the
1660 2nd previous insn was an mfhi/mflo and the current insn
1661 reads the register that the mfhi/mflo wrote to, then
1662 insert one nop. */
1663
1664 else if (mips_7000_hilo_fix
1665 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1666 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1667 & OP_MASK_RD),
1668 MIPS_GR_REG))
bdaaa2e1 1669
6b76fefe
CM
1670 {
1671 nops += 1;
1672 }
bdaaa2e1 1673
252b5132
RH
1674 else if (prev_pinfo & INSN_READ_LO)
1675 {
1676 /* The previous instruction reads the LO register; if the
1677 current instruction writes to the LO register, we must
bdaaa2e1
KH
1678 insert two NOPS. Some newer processors have interlocks.
1679 Also the tx39's multiply instructions can be exectuted
252b5132 1680 immediatly after a read from HI/LO (without the delay),
bdaaa2e1
KH
1681 though the tx39's divide insns still do require the
1682 delay. */
252b5132 1683 if (! (hilo_interlocks
ec68c924 1684 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1685 && (mips_optimize == 0
1686 || (pinfo & INSN_WRITE_LO)))
1687 nops += 2;
1688 /* Most mips16 branch insns don't have a delay slot.
1689 If a read from LO is immediately followed by a branch
1690 to a write to LO we have a read followed by a write
1691 less than 2 insns away. We assume the target of
1692 a branch might be a write to LO, and insert a nop
bdaaa2e1 1693 between a read and an immediately following branch. */
252b5132
RH
1694 else if (mips_opts.mips16
1695 && (mips_optimize == 0
1696 || (pinfo & MIPS16_INSN_BRANCH)))
1697 nops += 1;
1698 }
1699 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1700 {
1701 /* The previous instruction reads the HI register; if the
1702 current instruction writes to the HI register, we must
1703 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1704 Also the note tx39's multiply above. */
252b5132 1705 if (! (hilo_interlocks
ec68c924 1706 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1707 && (mips_optimize == 0
1708 || (pinfo & INSN_WRITE_HI)))
1709 nops += 2;
1710 /* Most mips16 branch insns don't have a delay slot.
1711 If a read from HI is immediately followed by a branch
1712 to a write to HI we have a read followed by a write
1713 less than 2 insns away. We assume the target of
1714 a branch might be a write to HI, and insert a nop
bdaaa2e1 1715 between a read and an immediately following branch. */
252b5132
RH
1716 else if (mips_opts.mips16
1717 && (mips_optimize == 0
1718 || (pinfo & MIPS16_INSN_BRANCH)))
1719 nops += 1;
1720 }
1721
1722 /* If the previous instruction was in a noreorder section, then
1723 we don't want to insert the nop after all. */
bdaaa2e1 1724 /* Itbl support may require additional care here. */
252b5132
RH
1725 if (prev_insn_unreordered)
1726 nops = 0;
1727
1728 /* There are two cases which require two intervening
1729 instructions: 1) setting the condition codes using a move to
1730 coprocessor instruction which requires a general coprocessor
1731 delay and then reading the condition codes 2) reading the HI
1732 or LO register and then writing to it (except on processors
1733 which have interlocks). If we are not already emitting a NOP
1734 instruction, we must check for these cases compared to the
1735 instruction previous to the previous instruction. */
1736 if ((! mips_opts.mips16
9ce8a5dd 1737 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1738 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1739 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1740 && (pinfo & INSN_READ_COND_CODE)
1741 && ! cop_interlocks)
1742 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1743 && (pinfo & INSN_WRITE_LO)
1744 && ! (hilo_interlocks
ec68c924 1745 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1746 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1747 && (pinfo & INSN_WRITE_HI)
1748 && ! (hilo_interlocks
ec68c924 1749 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1750 prev_prev_nop = 1;
1751 else
1752 prev_prev_nop = 0;
1753
1754 if (prev_prev_insn_unreordered)
1755 prev_prev_nop = 0;
1756
1757 if (prev_prev_nop && nops == 0)
1758 ++nops;
1759
1760 /* If we are being given a nop instruction, don't bother with
1761 one of the nops we would otherwise output. This will only
1762 happen when a nop instruction is used with mips_optimize set
1763 to 0. */
1764 if (nops > 0
1765 && ! mips_opts.noreorder
156c2f8b 1766 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1767 --nops;
1768
1769 /* Now emit the right number of NOP instructions. */
1770 if (nops > 0 && ! mips_opts.noreorder)
1771 {
1772 fragS *old_frag;
1773 unsigned long old_frag_offset;
1774 int i;
1775 struct insn_label_list *l;
1776
1777 old_frag = frag_now;
1778 old_frag_offset = frag_now_fix ();
1779
1780 for (i = 0; i < nops; i++)
1781 emit_nop ();
1782
1783 if (listing)
1784 {
1785 listing_prev_line ();
1786 /* We may be at the start of a variant frag. In case we
1787 are, make sure there is enough space for the frag
1788 after the frags created by listing_prev_line. The
1789 argument to frag_grow here must be at least as large
1790 as the argument to all other calls to frag_grow in
1791 this file. We don't have to worry about being in the
1792 middle of a variant frag, because the variants insert
1793 all needed nop instructions themselves. */
1794 frag_grow (40);
1795 }
1796
1797 for (l = insn_labels; l != NULL; l = l->next)
1798 {
98aa84af
AM
1799 valueT val;
1800
252b5132 1801 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1802 symbol_set_frag (l->label, frag_now);
98aa84af 1803 val = (valueT) frag_now_fix ();
252b5132
RH
1804 /* mips16 text labels are stored as odd. */
1805 if (mips_opts.mips16)
98aa84af
AM
1806 val += 1;
1807 S_SET_VALUE (l->label, val);
252b5132
RH
1808 }
1809
1810#ifndef NO_ECOFF_DEBUGGING
1811 if (ECOFF_DEBUGGING)
1812 ecoff_fix_loc (old_frag, old_frag_offset);
1813#endif
1814 }
1815 else if (prev_nop_frag != NULL)
1816 {
1817 /* We have a frag holding nops we may be able to remove. If
1818 we don't need any nops, we can decrease the size of
1819 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1820 need some nops, we count them in prev_nops_required. */
252b5132
RH
1821 if (prev_nop_frag_since == 0)
1822 {
1823 if (nops == 0)
1824 {
1825 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1826 --prev_nop_frag_holds;
1827 }
1828 else
1829 prev_nop_frag_required += nops;
1830 }
1831 else
1832 {
1833 if (prev_prev_nop == 0)
1834 {
1835 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1836 --prev_nop_frag_holds;
1837 }
1838 else
1839 ++prev_nop_frag_required;
1840 }
1841
1842 if (prev_nop_frag_holds <= prev_nop_frag_required)
1843 prev_nop_frag = NULL;
1844
1845 ++prev_nop_frag_since;
1846
1847 /* Sanity check: by the time we reach the second instruction
1848 after prev_nop_frag, we should have used up all the nops
1849 one way or another. */
1850 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1851 }
1852 }
1853
f6688943 1854 if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
1855 {
1856 /* We need to set up a variant frag. */
1857 assert (mips_opts.mips16 && address_expr != NULL);
1858 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 1859 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
1860 mips16_small, mips16_ext,
1861 (prev_pinfo
1862 & INSN_UNCOND_BRANCH_DELAY),
f6688943 1863 (*prev_insn_reloc_type
252b5132
RH
1864 == BFD_RELOC_MIPS16_JMP)),
1865 make_expr_symbol (address_expr), (offsetT) 0,
1866 (char *) NULL);
1867 }
1868 else if (place != NULL)
1869 f = place;
1870 else if (mips_opts.mips16
1871 && ! ip->use_extend
f6688943 1872 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
1873 {
1874 /* Make sure there is enough room to swap this instruction with
1875 a following jump instruction. */
1876 frag_grow (6);
1877 f = frag_more (2);
1878 }
1879 else
1880 {
1881 if (mips_opts.mips16
1882 && mips_opts.noreorder
1883 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1884 as_warn (_("extended instruction in delay slot"));
1885
1886 f = frag_more (4);
1887 }
1888
f6688943
TS
1889 fixp[0] = fixp[1] = fixp[2] = NULL;
1890 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
1891 {
1892 if (address_expr->X_op == O_constant)
1893 {
f6688943
TS
1894 unsigned long tmp;
1895
1896 switch (*reloc_type)
252b5132
RH
1897 {
1898 case BFD_RELOC_32:
1899 ip->insn_opcode |= address_expr->X_add_number;
1900 break;
1901
f6688943
TS
1902 case BFD_RELOC_MIPS_HIGHEST:
1903 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1904 tmp >>= 16;
1905 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1906 break;
1907
1908 case BFD_RELOC_MIPS_HIGHER:
1909 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1910 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1911 break;
1912
1913 case BFD_RELOC_HI16_S:
1914 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1915 >> 16) & 0xffff;
1916 break;
1917
1918 case BFD_RELOC_HI16:
1919 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1920 break;
1921
252b5132
RH
1922 case BFD_RELOC_LO16:
1923 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1924 break;
1925
1926 case BFD_RELOC_MIPS_JMP:
1927 if ((address_expr->X_add_number & 3) != 0)
1928 as_bad (_("jump to misaligned address (0x%lx)"),
1929 (unsigned long) address_expr->X_add_number);
1930 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1931 break;
1932
1933 case BFD_RELOC_MIPS16_JMP:
1934 if ((address_expr->X_add_number & 3) != 0)
1935 as_bad (_("jump to misaligned address (0x%lx)"),
1936 (unsigned long) address_expr->X_add_number);
1937 ip->insn_opcode |=
1938 (((address_expr->X_add_number & 0x7c0000) << 3)
1939 | ((address_expr->X_add_number & 0xf800000) >> 7)
1940 | ((address_expr->X_add_number & 0x3fffc) >> 2));
1941 break;
1942
cb56d3d3 1943 case BFD_RELOC_16_PCREL:
233b8738 1944 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
cb56d3d3
TS
1945 break;
1946
252b5132
RH
1947 case BFD_RELOC_16_PCREL_S2:
1948 goto need_reloc;
1949
1950 default:
1951 internalError ();
1952 }
1953 }
1954 else
1955 {
1956 need_reloc:
f6688943 1957 /* Don't generate a reloc if we are writing into a variant frag. */
252b5132
RH
1958 if (place == NULL)
1959 {
f6688943
TS
1960 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1961 address_expr,
1962 (*reloc_type == BFD_RELOC_16_PCREL
1963 || *reloc_type == BFD_RELOC_16_PCREL_S2),
1964 reloc_type[0]);
1965
1966 /* These relocations can have a addend that won't fit in
1967 4 octets for 64bit assembly. */
1968 if (HAVE_64BIT_GPRS &&
1969 (*reloc_type == BFD_RELOC_16
1970 || *reloc_type == BFD_RELOC_32
1971 || *reloc_type == BFD_RELOC_MIPS_JMP
1972 || *reloc_type == BFD_RELOC_HI16_S
1973 || *reloc_type == BFD_RELOC_LO16
1974 || *reloc_type == BFD_RELOC_GPREL16
1975 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1976 || *reloc_type == BFD_RELOC_GPREL32
1977 || *reloc_type == BFD_RELOC_64
1978 || *reloc_type == BFD_RELOC_CTOR
1979 || *reloc_type == BFD_RELOC_MIPS_SUB
1980 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1981 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1982 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1983 || *reloc_type == BFD_RELOC_MIPS_REL16
1984 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1985 fixp[0]->fx_no_overflow = 1;
1986
252b5132
RH
1987 if (unmatched_hi)
1988 {
1989 struct mips_hi_fixup *hi_fixup;
1990
f6688943 1991 assert (*reloc_type == BFD_RELOC_HI16_S);
252b5132
RH
1992 hi_fixup = ((struct mips_hi_fixup *)
1993 xmalloc (sizeof (struct mips_hi_fixup)));
f6688943 1994 hi_fixup->fixp = fixp[0];
252b5132
RH
1995 hi_fixup->seg = now_seg;
1996 hi_fixup->next = mips_hi_fixup_list;
1997 mips_hi_fixup_list = hi_fixup;
1998 }
f6688943
TS
1999
2000 if (reloc_type[1] != BFD_RELOC_UNUSED)
2001 {
2002 /* FIXME: This symbol can be one of
2003 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
2004 address_expr->X_op = O_absent;
2005 address_expr->X_add_symbol = 0;
2006 address_expr->X_add_number = 0;
2007
2008 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2009 4, address_expr, false,
2010 reloc_type[1]);
2011
2012 /* These relocations can have a addend that won't fit in
2013 4 octets for 64bit assembly. */
2014 if (HAVE_64BIT_GPRS &&
2015 (*reloc_type == BFD_RELOC_16
2016 || *reloc_type == BFD_RELOC_32
2017 || *reloc_type == BFD_RELOC_MIPS_JMP
2018 || *reloc_type == BFD_RELOC_HI16_S
2019 || *reloc_type == BFD_RELOC_LO16
2020 || *reloc_type == BFD_RELOC_GPREL16
2021 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2022 || *reloc_type == BFD_RELOC_GPREL32
2023 || *reloc_type == BFD_RELOC_64
2024 || *reloc_type == BFD_RELOC_CTOR
2025 || *reloc_type == BFD_RELOC_MIPS_SUB
2026 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2027 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2028 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2029 || *reloc_type == BFD_RELOC_MIPS_REL16
2030 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2031 fixp[1]->fx_no_overflow = 1;
2032
2033 if (reloc_type[2] != BFD_RELOC_UNUSED)
2034 {
2035 address_expr->X_op = O_absent;
2036 address_expr->X_add_symbol = 0;
2037 address_expr->X_add_number = 0;
2038
2039 fixp[2] = fix_new_exp (frag_now,
2040 f - frag_now->fr_literal, 4,
2041 address_expr, false,
2042 reloc_type[2]);
2043
2044 /* These relocations can have a addend that won't fit in
2045 4 octets for 64bit assembly. */
2046 if (HAVE_64BIT_GPRS &&
2047 (*reloc_type == BFD_RELOC_16
2048 || *reloc_type == BFD_RELOC_32
2049 || *reloc_type == BFD_RELOC_MIPS_JMP
2050 || *reloc_type == BFD_RELOC_HI16_S
2051 || *reloc_type == BFD_RELOC_LO16
2052 || *reloc_type == BFD_RELOC_GPREL16
2053 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2054 || *reloc_type == BFD_RELOC_GPREL32
2055 || *reloc_type == BFD_RELOC_64
2056 || *reloc_type == BFD_RELOC_CTOR
2057 || *reloc_type == BFD_RELOC_MIPS_SUB
2058 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2059 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2060 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2061 || *reloc_type == BFD_RELOC_MIPS_REL16
2062 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2063 fixp[2]->fx_no_overflow = 1;
2064 }
2065 }
252b5132
RH
2066 }
2067 }
2068 }
2069
2070 if (! mips_opts.mips16)
2071 md_number_to_chars (f, ip->insn_opcode, 4);
f6688943 2072 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2073 {
2074 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2075 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2076 }
2077 else
2078 {
2079 if (ip->use_extend)
2080 {
2081 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2082 f += 2;
2083 }
2084 md_number_to_chars (f, ip->insn_opcode, 2);
2085 }
2086
2087 /* Update the register mask information. */
2088 if (! mips_opts.mips16)
2089 {
2090 if (pinfo & INSN_WRITE_GPR_D)
2091 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2092 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2093 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2094 if (pinfo & INSN_READ_GPR_S)
2095 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2096 if (pinfo & INSN_WRITE_GPR_31)
2097 mips_gprmask |= 1 << 31;
2098 if (pinfo & INSN_WRITE_FPR_D)
2099 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2100 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2101 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2102 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2103 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2104 if ((pinfo & INSN_READ_FPR_R) != 0)
2105 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2106 if (pinfo & INSN_COP)
2107 {
bdaaa2e1
KH
2108 /* We don't keep enough information to sort these cases out.
2109 The itbl support does keep this information however, although
2110 we currently don't support itbl fprmats as part of the cop
2111 instruction. May want to add this support in the future. */
252b5132
RH
2112 }
2113 /* Never set the bit for $0, which is always zero. */
beae10d5 2114 mips_gprmask &= ~1 << 0;
252b5132
RH
2115 }
2116 else
2117 {
2118 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2119 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2120 & MIPS16OP_MASK_RX);
2121 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2122 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2123 & MIPS16OP_MASK_RY);
2124 if (pinfo & MIPS16_INSN_WRITE_Z)
2125 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2126 & MIPS16OP_MASK_RZ);
2127 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2128 mips_gprmask |= 1 << TREG;
2129 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2130 mips_gprmask |= 1 << SP;
2131 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2132 mips_gprmask |= 1 << RA;
2133 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2134 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2135 if (pinfo & MIPS16_INSN_READ_Z)
2136 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2137 & MIPS16OP_MASK_MOVE32Z);
2138 if (pinfo & MIPS16_INSN_READ_GPR_X)
2139 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2140 & MIPS16OP_MASK_REGR32);
2141 }
2142
2143 if (place == NULL && ! mips_opts.noreorder)
2144 {
2145 /* Filling the branch delay slot is more complex. We try to
2146 switch the branch with the previous instruction, which we can
2147 do if the previous instruction does not set up a condition
2148 that the branch tests and if the branch is not itself the
2149 target of any branch. */
2150 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2151 || (pinfo & INSN_COND_BRANCH_DELAY))
2152 {
2153 if (mips_optimize < 2
2154 /* If we have seen .set volatile or .set nomove, don't
2155 optimize. */
2156 || mips_opts.nomove != 0
2157 /* If we had to emit any NOP instructions, then we
2158 already know we can not swap. */
2159 || nops != 0
2160 /* If we don't even know the previous insn, we can not
bdaaa2e1 2161 swap. */
252b5132
RH
2162 || ! prev_insn_valid
2163 /* If the previous insn is already in a branch delay
2164 slot, then we can not swap. */
2165 || prev_insn_is_delay_slot
2166 /* If the previous previous insn was in a .set
2167 noreorder, we can't swap. Actually, the MIPS
2168 assembler will swap in this situation. However, gcc
2169 configured -with-gnu-as will generate code like
2170 .set noreorder
2171 lw $4,XXX
2172 .set reorder
2173 INSN
2174 bne $4,$0,foo
2175 in which we can not swap the bne and INSN. If gcc is
2176 not configured -with-gnu-as, it does not output the
2177 .set pseudo-ops. We don't have to check
2178 prev_insn_unreordered, because prev_insn_valid will
2179 be 0 in that case. We don't want to use
2180 prev_prev_insn_valid, because we do want to be able
2181 to swap at the start of a function. */
2182 || prev_prev_insn_unreordered
2183 /* If the branch is itself the target of a branch, we
2184 can not swap. We cheat on this; all we check for is
2185 whether there is a label on this instruction. If
2186 there are any branches to anything other than a
2187 label, users must use .set noreorder. */
2188 || insn_labels != NULL
2189 /* If the previous instruction is in a variant frag, we
2190 can not do the swap. This does not apply to the
2191 mips16, which uses variant frags for different
2192 purposes. */
2193 || (! mips_opts.mips16
2194 && prev_insn_frag->fr_type == rs_machine_dependent)
2195 /* If the branch reads the condition codes, we don't
2196 even try to swap, because in the sequence
2197 ctc1 $X,$31
2198 INSN
2199 INSN
2200 bc1t LABEL
2201 we can not swap, and I don't feel like handling that
2202 case. */
2203 || (! mips_opts.mips16
9ce8a5dd 2204 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2205 && (pinfo & INSN_READ_COND_CODE))
2206 /* We can not swap with an instruction that requires a
2207 delay slot, becase the target of the branch might
2208 interfere with that instruction. */
2209 || (! mips_opts.mips16
9ce8a5dd 2210 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 2211 && (prev_pinfo
bdaaa2e1 2212 /* Itbl support may require additional care here. */
252b5132
RH
2213 & (INSN_LOAD_COPROC_DELAY
2214 | INSN_COPROC_MOVE_DELAY
2215 | INSN_WRITE_COND_CODE)))
2216 || (! (hilo_interlocks
ec68c924 2217 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2218 && (prev_pinfo
2219 & (INSN_READ_LO
2220 | INSN_READ_HI)))
2221 || (! mips_opts.mips16
2222 && ! gpr_interlocks
2223 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2224 || (! mips_opts.mips16
e7af610e 2225 && mips_opts.isa == ISA_MIPS1
bdaaa2e1 2226 /* Itbl support may require additional care here. */
252b5132
RH
2227 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2228 /* We can not swap with a branch instruction. */
2229 || (prev_pinfo
2230 & (INSN_UNCOND_BRANCH_DELAY
2231 | INSN_COND_BRANCH_DELAY
2232 | INSN_COND_BRANCH_LIKELY))
2233 /* We do not swap with a trap instruction, since it
2234 complicates trap handlers to have the trap
2235 instruction be in a delay slot. */
2236 || (prev_pinfo & INSN_TRAP)
2237 /* If the branch reads a register that the previous
2238 instruction sets, we can not swap. */
2239 || (! mips_opts.mips16
2240 && (prev_pinfo & INSN_WRITE_GPR_T)
2241 && insn_uses_reg (ip,
2242 ((prev_insn.insn_opcode >> OP_SH_RT)
2243 & OP_MASK_RT),
2244 MIPS_GR_REG))
2245 || (! mips_opts.mips16
2246 && (prev_pinfo & INSN_WRITE_GPR_D)
2247 && insn_uses_reg (ip,
2248 ((prev_insn.insn_opcode >> OP_SH_RD)
2249 & OP_MASK_RD),
2250 MIPS_GR_REG))
2251 || (mips_opts.mips16
2252 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2253 && insn_uses_reg (ip,
2254 ((prev_insn.insn_opcode
2255 >> MIPS16OP_SH_RX)
2256 & MIPS16OP_MASK_RX),
2257 MIPS16_REG))
2258 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2259 && insn_uses_reg (ip,
2260 ((prev_insn.insn_opcode
2261 >> MIPS16OP_SH_RY)
2262 & MIPS16OP_MASK_RY),
2263 MIPS16_REG))
2264 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2265 && insn_uses_reg (ip,
2266 ((prev_insn.insn_opcode
2267 >> MIPS16OP_SH_RZ)
2268 & MIPS16OP_MASK_RZ),
2269 MIPS16_REG))
2270 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2271 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2272 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2273 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2274 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2275 && insn_uses_reg (ip,
2276 MIPS16OP_EXTRACT_REG32R (prev_insn.
2277 insn_opcode),
2278 MIPS_GR_REG))))
2279 /* If the branch writes a register that the previous
2280 instruction sets, we can not swap (we know that
2281 branches write only to RD or to $31). */
2282 || (! mips_opts.mips16
2283 && (prev_pinfo & INSN_WRITE_GPR_T)
2284 && (((pinfo & INSN_WRITE_GPR_D)
2285 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2286 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2287 || ((pinfo & INSN_WRITE_GPR_31)
2288 && (((prev_insn.insn_opcode >> OP_SH_RT)
2289 & OP_MASK_RT)
2290 == 31))))
2291 || (! mips_opts.mips16
2292 && (prev_pinfo & INSN_WRITE_GPR_D)
2293 && (((pinfo & INSN_WRITE_GPR_D)
2294 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2295 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2296 || ((pinfo & INSN_WRITE_GPR_31)
2297 && (((prev_insn.insn_opcode >> OP_SH_RD)
2298 & OP_MASK_RD)
2299 == 31))))
2300 || (mips_opts.mips16
2301 && (pinfo & MIPS16_INSN_WRITE_31)
2302 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2303 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2304 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2305 == RA))))
2306 /* If the branch writes a register that the previous
2307 instruction reads, we can not swap (we know that
2308 branches only write to RD or to $31). */
2309 || (! mips_opts.mips16
2310 && (pinfo & INSN_WRITE_GPR_D)
2311 && insn_uses_reg (&prev_insn,
2312 ((ip->insn_opcode >> OP_SH_RD)
2313 & OP_MASK_RD),
2314 MIPS_GR_REG))
2315 || (! mips_opts.mips16
2316 && (pinfo & INSN_WRITE_GPR_31)
2317 && insn_uses_reg (&prev_insn, 31, MIPS_GR_REG))
2318 || (mips_opts.mips16
2319 && (pinfo & MIPS16_INSN_WRITE_31)
2320 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2321 /* If we are generating embedded PIC code, the branch
2322 might be expanded into a sequence which uses $at, so
2323 we can't swap with an instruction which reads it. */
2324 || (mips_pic == EMBEDDED_PIC
2325 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2326 /* If the previous previous instruction has a load
2327 delay, and sets a register that the branch reads, we
2328 can not swap. */
2329 || (! mips_opts.mips16
9ce8a5dd 2330 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
bdaaa2e1 2331 /* Itbl support may require additional care here. */
252b5132
RH
2332 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2333 || (! gpr_interlocks
2334 && (prev_prev_insn.insn_mo->pinfo
2335 & INSN_LOAD_MEMORY_DELAY)))
2336 && insn_uses_reg (ip,
2337 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2338 & OP_MASK_RT),
2339 MIPS_GR_REG))
2340 /* If one instruction sets a condition code and the
2341 other one uses a condition code, we can not swap. */
2342 || ((pinfo & INSN_READ_COND_CODE)
2343 && (prev_pinfo & INSN_WRITE_COND_CODE))
2344 || ((pinfo & INSN_WRITE_COND_CODE)
2345 && (prev_pinfo & INSN_READ_COND_CODE))
2346 /* If the previous instruction uses the PC, we can not
2347 swap. */
2348 || (mips_opts.mips16
2349 && (prev_pinfo & MIPS16_INSN_READ_PC))
2350 /* If the previous instruction was extended, we can not
2351 swap. */
2352 || (mips_opts.mips16 && prev_insn_extended)
2353 /* If the previous instruction had a fixup in mips16
2354 mode, we can not swap. This normally means that the
2355 previous instruction was a 4 byte branch anyhow. */
f6688943 2356 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2357 /* If the previous instruction is a sync, sync.l, or
2358 sync.p, we can not swap. */
f173e82e 2359 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2360 {
2361 /* We could do even better for unconditional branches to
2362 portions of this object file; we could pick up the
2363 instruction at the destination, put it in the delay
2364 slot, and bump the destination address. */
2365 emit_nop ();
2366 /* Update the previous insn information. */
2367 prev_prev_insn = *ip;
2368 prev_insn.insn_mo = &dummy_opcode;
2369 }
2370 else
2371 {
2372 /* It looks like we can actually do the swap. */
2373 if (! mips_opts.mips16)
2374 {
2375 char *prev_f;
2376 char temp[4];
2377
2378 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2379 memcpy (temp, prev_f, 4);
2380 memcpy (prev_f, f, 4);
2381 memcpy (f, temp, 4);
f6688943
TS
2382 if (prev_insn_fixp[0])
2383 {
2384 prev_insn_fixp[0]->fx_frag = frag_now;
2385 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2386 }
2387 if (prev_insn_fixp[1])
2388 {
2389 prev_insn_fixp[1]->fx_frag = frag_now;
2390 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2391 }
2392 if (prev_insn_fixp[2])
252b5132 2393 {
f6688943
TS
2394 prev_insn_fixp[2]->fx_frag = frag_now;
2395 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2396 }
f6688943 2397 if (fixp[0])
252b5132 2398 {
f6688943
TS
2399 fixp[0]->fx_frag = prev_insn_frag;
2400 fixp[0]->fx_where = prev_insn_where;
2401 }
2402 if (fixp[1])
2403 {
2404 fixp[1]->fx_frag = prev_insn_frag;
2405 fixp[1]->fx_where = prev_insn_where;
2406 }
2407 if (fixp[2])
2408 {
2409 fixp[2]->fx_frag = prev_insn_frag;
2410 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2411 }
2412 }
2413 else
2414 {
2415 char *prev_f;
2416 char temp[2];
2417
f6688943
TS
2418 assert (prev_insn_fixp[0] == NULL);
2419 assert (prev_insn_fixp[1] == NULL);
2420 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2421 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2422 memcpy (temp, prev_f, 2);
2423 memcpy (prev_f, f, 2);
f6688943 2424 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2425 {
f6688943 2426 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2427 memcpy (f, temp, 2);
2428 }
2429 else
2430 {
2431 memcpy (f, f + 2, 2);
2432 memcpy (f + 2, temp, 2);
2433 }
f6688943
TS
2434 if (fixp[0])
2435 {
2436 fixp[0]->fx_frag = prev_insn_frag;
2437 fixp[0]->fx_where = prev_insn_where;
2438 }
2439 if (fixp[1])
2440 {
2441 fixp[1]->fx_frag = prev_insn_frag;
2442 fixp[1]->fx_where = prev_insn_where;
2443 }
2444 if (fixp[2])
252b5132 2445 {
f6688943
TS
2446 fixp[2]->fx_frag = prev_insn_frag;
2447 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2448 }
2449 }
2450
2451 /* Update the previous insn information; leave prev_insn
2452 unchanged. */
2453 prev_prev_insn = *ip;
2454 }
2455 prev_insn_is_delay_slot = 1;
2456
2457 /* If that was an unconditional branch, forget the previous
2458 insn information. */
2459 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2460 {
2461 prev_prev_insn.insn_mo = &dummy_opcode;
2462 prev_insn.insn_mo = &dummy_opcode;
2463 }
2464
f6688943
TS
2465 prev_insn_fixp[0] = NULL;
2466 prev_insn_fixp[1] = NULL;
2467 prev_insn_fixp[2] = NULL;
2468 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2469 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2470 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2471 prev_insn_extended = 0;
2472 }
2473 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2474 {
2475 /* We don't yet optimize a branch likely. What we should do
2476 is look at the target, copy the instruction found there
2477 into the delay slot, and increment the branch to jump to
2478 the next instruction. */
2479 emit_nop ();
2480 /* Update the previous insn information. */
2481 prev_prev_insn = *ip;
2482 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2483 prev_insn_fixp[0] = NULL;
2484 prev_insn_fixp[1] = NULL;
2485 prev_insn_fixp[2] = NULL;
2486 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2487 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2488 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2489 prev_insn_extended = 0;
2490 }
2491 else
2492 {
2493 /* Update the previous insn information. */
2494 if (nops > 0)
2495 prev_prev_insn.insn_mo = &dummy_opcode;
2496 else
2497 prev_prev_insn = prev_insn;
2498 prev_insn = *ip;
2499
2500 /* Any time we see a branch, we always fill the delay slot
2501 immediately; since this insn is not a branch, we know it
2502 is not in a delay slot. */
2503 prev_insn_is_delay_slot = 0;
2504
f6688943
TS
2505 prev_insn_fixp[0] = fixp[0];
2506 prev_insn_fixp[1] = fixp[1];
2507 prev_insn_fixp[2] = fixp[2];
2508 prev_insn_reloc_type[0] = reloc_type[0];
2509 prev_insn_reloc_type[1] = reloc_type[1];
2510 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2511 if (mips_opts.mips16)
2512 prev_insn_extended = (ip->use_extend
f6688943 2513 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2514 }
2515
2516 prev_prev_insn_unreordered = prev_insn_unreordered;
2517 prev_insn_unreordered = 0;
2518 prev_insn_frag = frag_now;
2519 prev_insn_where = f - frag_now->fr_literal;
2520 prev_insn_valid = 1;
2521 }
2522 else if (place == NULL)
2523 {
2524 /* We need to record a bit of information even when we are not
2525 reordering, in order to determine the base address for mips16
2526 PC relative relocs. */
2527 prev_prev_insn = prev_insn;
2528 prev_insn = *ip;
f6688943
TS
2529 prev_insn_reloc_type[0] = reloc_type[0];
2530 prev_insn_reloc_type[1] = reloc_type[1];
2531 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2532 prev_prev_insn_unreordered = prev_insn_unreordered;
2533 prev_insn_unreordered = 1;
2534 }
2535
2536 /* We just output an insn, so the next one doesn't have a label. */
2537 mips_clear_insn_labels ();
2538
2539 /* We must ensure that a fixup associated with an unmatched %hi
2540 reloc does not become a variant frag. Otherwise, the
2541 rearrangement of %hi relocs in frob_file may confuse
2542 tc_gen_reloc. */
2543 if (unmatched_hi)
2544 {
2545 frag_wane (frag_now);
2546 frag_new (0);
2547 }
2548}
2549
2550/* This function forgets that there was any previous instruction or
2551 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2552 know whether nops are needed before a noreorder section. */
252b5132
RH
2553
2554static void
2555mips_no_prev_insn (preserve)
2556 int preserve;
2557{
2558 if (! preserve)
2559 {
2560 prev_insn.insn_mo = &dummy_opcode;
2561 prev_prev_insn.insn_mo = &dummy_opcode;
2562 prev_nop_frag = NULL;
2563 prev_nop_frag_holds = 0;
2564 prev_nop_frag_required = 0;
2565 prev_nop_frag_since = 0;
2566 }
2567 prev_insn_valid = 0;
2568 prev_insn_is_delay_slot = 0;
2569 prev_insn_unreordered = 0;
2570 prev_insn_extended = 0;
f6688943
TS
2571 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2572 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2573 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2574 prev_prev_insn_unreordered = 0;
2575 mips_clear_insn_labels ();
2576}
2577
2578/* This function must be called whenever we turn on noreorder or emit
2579 something other than instructions. It inserts any NOPS which might
2580 be needed by the previous instruction, and clears the information
2581 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2582 instructions are to follow. */
252b5132
RH
2583
2584static void
2585mips_emit_delays (insns)
2586 boolean insns;
2587{
2588 if (! mips_opts.noreorder)
2589 {
2590 int nops;
2591
2592 nops = 0;
2593 if ((! mips_opts.mips16
9ce8a5dd 2594 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2595 && (! cop_interlocks
2596 && (prev_insn.insn_mo->pinfo
2597 & (INSN_LOAD_COPROC_DELAY
2598 | INSN_COPROC_MOVE_DELAY
2599 | INSN_WRITE_COND_CODE))))
2600 || (! hilo_interlocks
2601 && (prev_insn.insn_mo->pinfo
2602 & (INSN_READ_LO
2603 | INSN_READ_HI)))
2604 || (! mips_opts.mips16
2605 && ! gpr_interlocks
bdaaa2e1 2606 && (prev_insn.insn_mo->pinfo
252b5132
RH
2607 & INSN_LOAD_MEMORY_DELAY))
2608 || (! mips_opts.mips16
e7af610e 2609 && mips_opts.isa == ISA_MIPS1
252b5132
RH
2610 && (prev_insn.insn_mo->pinfo
2611 & INSN_COPROC_MEMORY_DELAY)))
2612 {
beae10d5 2613 /* Itbl support may require additional care here. */
252b5132
RH
2614 ++nops;
2615 if ((! mips_opts.mips16
9ce8a5dd 2616 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2617 && (! cop_interlocks
2618 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2619 || (! hilo_interlocks
2620 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2621 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2622 ++nops;
2623
2624 if (prev_insn_unreordered)
2625 nops = 0;
2626 }
2627 else if ((! mips_opts.mips16
9ce8a5dd 2628 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2629 && (! cop_interlocks
2630 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2631 || (! hilo_interlocks
2632 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2633 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2634 {
beae10d5 2635 /* Itbl support may require additional care here. */
252b5132
RH
2636 if (! prev_prev_insn_unreordered)
2637 ++nops;
2638 }
2639
2640 if (nops > 0)
2641 {
2642 struct insn_label_list *l;
2643
2644 if (insns)
2645 {
2646 /* Record the frag which holds the nop instructions, so
2647 that we can remove them if we don't need them. */
2648 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2649 prev_nop_frag = frag_now;
2650 prev_nop_frag_holds = nops;
2651 prev_nop_frag_required = 0;
2652 prev_nop_frag_since = 0;
2653 }
2654
2655 for (; nops > 0; --nops)
2656 emit_nop ();
2657
2658 if (insns)
2659 {
2660 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2661 decrease the size of prev_nop_frag. */
252b5132
RH
2662 frag_wane (frag_now);
2663 frag_new (0);
2664 }
2665
2666 for (l = insn_labels; l != NULL; l = l->next)
2667 {
98aa84af
AM
2668 valueT val;
2669
252b5132 2670 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2671 symbol_set_frag (l->label, frag_now);
98aa84af 2672 val = (valueT) frag_now_fix ();
252b5132
RH
2673 /* mips16 text labels are stored as odd. */
2674 if (mips_opts.mips16)
98aa84af
AM
2675 val += 1;
2676 S_SET_VALUE (l->label, val);
252b5132
RH
2677 }
2678 }
2679 }
2680
2681 /* Mark instruction labels in mips16 mode. */
2682 if (mips_opts.mips16 && insns)
2683 mips16_mark_labels ();
2684
2685 mips_no_prev_insn (insns);
2686}
2687
2688/* Build an instruction created by a macro expansion. This is passed
2689 a pointer to the count of instructions created so far, an
2690 expression, the name of the instruction to build, an operand format
2691 string, and corresponding arguments. */
2692
2693#ifdef USE_STDARG
2694static void
2695macro_build (char *place,
2696 int *counter,
2697 expressionS * ep,
2698 const char *name,
2699 const char *fmt,
2700 ...)
2701#else
2702static void
2703macro_build (place, counter, ep, name, fmt, va_alist)
2704 char *place;
2705 int *counter;
2706 expressionS *ep;
2707 const char *name;
2708 const char *fmt;
2709 va_dcl
2710#endif
2711{
2712 struct mips_cl_insn insn;
f6688943 2713 bfd_reloc_code_real_type r[3];
252b5132 2714 va_list args;
252b5132
RH
2715
2716#ifdef USE_STDARG
2717 va_start (args, fmt);
2718#else
2719 va_start (args);
2720#endif
2721
2722 /*
2723 * If the macro is about to expand into a second instruction,
2724 * print a warning if needed. We need to pass ip as a parameter
2725 * to generate a better warning message here...
2726 */
2727 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2728 as_warn (_("Macro instruction expanded into multiple instructions"));
2729
2730 if (place == NULL)
2731 *counter += 1; /* bump instruction counter */
2732
2733 if (mips_opts.mips16)
2734 {
2735 mips16_macro_build (place, counter, ep, name, fmt, args);
2736 va_end (args);
2737 return;
2738 }
2739
f6688943
TS
2740 r[0] = BFD_RELOC_UNUSED;
2741 r[1] = BFD_RELOC_UNUSED;
2742 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2743 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2744 assert (insn.insn_mo);
2745 assert (strcmp (name, insn.insn_mo->name) == 0);
2746
2747 /* Search until we get a match for NAME. */
2748 while (1)
2749 {
252b5132
RH
2750 if (strcmp (fmt, insn.insn_mo->args) == 0
2751 && insn.insn_mo->pinfo != INSN_MACRO
ea1fb5dc 2752 && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
ec68c924 2753 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
2754 break;
2755
2756 ++insn.insn_mo;
2757 assert (insn.insn_mo->name);
2758 assert (strcmp (name, insn.insn_mo->name) == 0);
2759 }
2760
2761 insn.insn_opcode = insn.insn_mo->match;
2762 for (;;)
2763 {
2764 switch (*fmt++)
2765 {
2766 case '\0':
2767 break;
2768
2769 case ',':
2770 case '(':
2771 case ')':
2772 continue;
2773
2774 case 't':
2775 case 'w':
2776 case 'E':
38487616 2777 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
2778 continue;
2779
2780 case 'c':
38487616
TS
2781 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2782 continue;
2783
252b5132
RH
2784 case 'T':
2785 case 'W':
38487616 2786 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
2787 continue;
2788
2789 case 'd':
2790 case 'G':
38487616 2791 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
2792 continue;
2793
4372b673
NC
2794 case 'U':
2795 {
2796 int tmp = va_arg (args, int);
2797
38487616
TS
2798 insn.insn_opcode |= tmp << OP_SH_RT;
2799 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 2800 continue;
4372b673
NC
2801 }
2802
252b5132
RH
2803 case 'V':
2804 case 'S':
38487616 2805 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
2806 continue;
2807
2808 case 'z':
2809 continue;
2810
2811 case '<':
38487616 2812 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
2813 continue;
2814
2815 case 'D':
38487616 2816 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
2817 continue;
2818
2819 case 'B':
38487616 2820 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
2821 continue;
2822
4372b673 2823 case 'J':
38487616 2824 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
2825 continue;
2826
252b5132 2827 case 'q':
38487616 2828 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
2829 continue;
2830
2831 case 'b':
2832 case 's':
2833 case 'r':
2834 case 'v':
38487616 2835 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
2836 continue;
2837
2838 case 'i':
2839 case 'j':
2840 case 'o':
f6688943
TS
2841 *r = (bfd_reloc_code_real_type) va_arg (args, int);
2842 assert (*r == BFD_RELOC_MIPS_GPREL
2843 || *r == BFD_RELOC_MIPS_LITERAL
2844 || *r == BFD_RELOC_MIPS_HIGHER
2845 || *r == BFD_RELOC_HI16_S
2846 || *r == BFD_RELOC_LO16
2847 || *r == BFD_RELOC_MIPS_GOT16
2848 || *r == BFD_RELOC_MIPS_CALL16
2849 || *r == BFD_RELOC_MIPS_GOT_LO16
2850 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 2851 || (ep->X_op == O_subtract
f6688943 2852 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
2853 continue;
2854
2855 case 'u':
f6688943 2856 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
2857 assert (ep != NULL
2858 && (ep->X_op == O_constant
2859 || (ep->X_op == O_symbol
f6688943
TS
2860 && (*r == BFD_RELOC_MIPS_HIGHEST
2861 || *r == BFD_RELOC_HI16_S
2862 || *r == BFD_RELOC_HI16
2863 || *r == BFD_RELOC_GPREL16
2864 || *r == BFD_RELOC_MIPS_GOT_HI16
2865 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 2866 || (ep->X_op == O_subtract
f6688943 2867 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
2868 continue;
2869
2870 case 'p':
2871 assert (ep != NULL);
2872 /*
2873 * This allows macro() to pass an immediate expression for
2874 * creating short branches without creating a symbol.
2875 * Note that the expression still might come from the assembly
2876 * input, in which case the value is not checked for range nor
2877 * is a relocation entry generated (yuck).
2878 */
2879 if (ep->X_op == O_constant)
2880 {
2881 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2882 ep = NULL;
2883 }
2884 else
cb56d3d3 2885 if (mips_pic == EMBEDDED_PIC)
f6688943 2886 *r = BFD_RELOC_16_PCREL_S2;
cb56d3d3 2887 else
f6688943 2888 *r = BFD_RELOC_16_PCREL;
252b5132
RH
2889 continue;
2890
2891 case 'a':
2892 assert (ep != NULL);
f6688943 2893 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
2894 continue;
2895
2896 case 'C':
2897 insn.insn_opcode |= va_arg (args, unsigned long);
2898 continue;
2899
2900 default:
2901 internalError ();
2902 }
2903 break;
2904 }
2905 va_end (args);
f6688943 2906 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
2907
2908 append_insn (place, &insn, ep, r, false);
2909}
2910
2911static void
2912mips16_macro_build (place, counter, ep, name, fmt, args)
2913 char *place;
43841e91 2914 int *counter ATTRIBUTE_UNUSED;
252b5132
RH
2915 expressionS *ep;
2916 const char *name;
2917 const char *fmt;
2918 va_list args;
2919{
2920 struct mips_cl_insn insn;
f6688943
TS
2921 bfd_reloc_code_real_type r[3]
2922 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 2923
252b5132
RH
2924 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2925 assert (insn.insn_mo);
2926 assert (strcmp (name, insn.insn_mo->name) == 0);
2927
2928 while (strcmp (fmt, insn.insn_mo->args) != 0
2929 || insn.insn_mo->pinfo == INSN_MACRO)
2930 {
2931 ++insn.insn_mo;
2932 assert (insn.insn_mo->name);
2933 assert (strcmp (name, insn.insn_mo->name) == 0);
2934 }
2935
2936 insn.insn_opcode = insn.insn_mo->match;
2937 insn.use_extend = false;
2938
2939 for (;;)
2940 {
2941 int c;
2942
2943 c = *fmt++;
2944 switch (c)
2945 {
2946 case '\0':
2947 break;
2948
2949 case ',':
2950 case '(':
2951 case ')':
2952 continue;
2953
2954 case 'y':
2955 case 'w':
2956 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2957 continue;
2958
2959 case 'x':
2960 case 'v':
2961 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2962 continue;
2963
2964 case 'z':
2965 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2966 continue;
2967
2968 case 'Z':
2969 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2970 continue;
2971
2972 case '0':
2973 case 'S':
2974 case 'P':
2975 case 'R':
2976 continue;
2977
2978 case 'X':
2979 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2980 continue;
2981
2982 case 'Y':
2983 {
2984 int regno;
2985
2986 regno = va_arg (args, int);
2987 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2988 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2989 }
2990 continue;
2991
2992 case '<':
2993 case '>':
2994 case '4':
2995 case '5':
2996 case 'H':
2997 case 'W':
2998 case 'D':
2999 case 'j':
3000 case '8':
3001 case 'V':
3002 case 'C':
3003 case 'U':
3004 case 'k':
3005 case 'K':
3006 case 'p':
3007 case 'q':
3008 {
3009 assert (ep != NULL);
3010
3011 if (ep->X_op != O_constant)
f6688943 3012 *r = BFD_RELOC_UNUSED + c;
252b5132
RH
3013 else
3014 {
3015 mips16_immed ((char *) NULL, 0, c, ep->X_add_number, false,
3016 false, false, &insn.insn_opcode,
3017 &insn.use_extend, &insn.extend);
3018 ep = NULL;
f6688943 3019 *r = BFD_RELOC_UNUSED;
252b5132
RH
3020 }
3021 }
3022 continue;
3023
3024 case '6':
3025 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3026 continue;
3027 }
3028
3029 break;
3030 }
3031
f6688943 3032 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
3033
3034 append_insn (place, &insn, ep, r, false);
3035}
3036
3037/*
3038 * Generate a "lui" instruction.
3039 */
3040static void
3041macro_build_lui (place, counter, ep, regnum)
3042 char *place;
3043 int *counter;
3044 expressionS *ep;
3045 int regnum;
3046{
3047 expressionS high_expr;
3048 struct mips_cl_insn insn;
f6688943
TS
3049 bfd_reloc_code_real_type r[3]
3050 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
3051 CONST char *name = "lui";
3052 CONST char *fmt = "t,u";
3053
3054 assert (! mips_opts.mips16);
3055
3056 if (place == NULL)
3057 high_expr = *ep;
3058 else
3059 {
3060 high_expr.X_op = O_constant;
3061 high_expr.X_add_number = ep->X_add_number;
3062 }
3063
3064 if (high_expr.X_op == O_constant)
3065 {
3066 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3067 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3068 >> 16) & 0xffff;
f6688943 3069 *r = BFD_RELOC_UNUSED;
252b5132 3070 }
f6688943 3071 else if (! HAVE_NEWABI)
252b5132
RH
3072 {
3073 assert (ep->X_op == O_symbol);
3074 /* _gp_disp is a special case, used from s_cpload. */
3075 assert (mips_pic == NO_PIC
3076 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
f6688943 3077 *r = BFD_RELOC_HI16_S;
252b5132
RH
3078 }
3079
3080 /*
3081 * If the macro is about to expand into a second instruction,
3082 * print a warning if needed. We need to pass ip as a parameter
3083 * to generate a better warning message here...
3084 */
3085 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3086 as_warn (_("Macro instruction expanded into multiple instructions"));
3087
3088 if (place == NULL)
3089 *counter += 1; /* bump instruction counter */
3090
3091 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3092 assert (insn.insn_mo);
3093 assert (strcmp (name, insn.insn_mo->name) == 0);
3094 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3095
3096 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3097 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3098 {
3099 insn.insn_opcode |= high_expr.X_add_number;
3100 append_insn (place, &insn, NULL, r, false);
3101 }
3102 else
3103 append_insn (place, &insn, &high_expr, r, false);
3104}
3105
3106/* set_at()
3107 * Generates code to set the $at register to true (one)
3108 * if reg is less than the immediate expression.
3109 */
3110static void
3111set_at (counter, reg, unsignedp)
3112 int *counter;
3113 int reg;
3114 int unsignedp;
3115{
3116 if (imm_expr.X_op == O_constant
3117 && imm_expr.X_add_number >= -0x8000
3118 && imm_expr.X_add_number < 0x8000)
3119 macro_build ((char *) NULL, counter, &imm_expr,
3120 unsignedp ? "sltiu" : "slti",
3121 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3122 else
3123 {
3124 load_register (counter, AT, &imm_expr, 0);
3125 macro_build ((char *) NULL, counter, NULL,
3126 unsignedp ? "sltu" : "slt",
3127 "d,v,t", AT, reg, AT);
3128 }
3129}
3130
3131/* Warn if an expression is not a constant. */
3132
3133static void
3134check_absolute_expr (ip, ex)
3135 struct mips_cl_insn *ip;
3136 expressionS *ex;
3137{
3138 if (ex->X_op == O_big)
3139 as_bad (_("unsupported large constant"));
3140 else if (ex->X_op != O_constant)
3141 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3142}
3143
3144/* Count the leading zeroes by performing a binary chop. This is a
3145 bulky bit of source, but performance is a LOT better for the
3146 majority of values than a simple loop to count the bits:
3147 for (lcnt = 0; (lcnt < 32); lcnt++)
3148 if ((v) & (1 << (31 - lcnt)))
3149 break;
3150 However it is not code size friendly, and the gain will drop a bit
3151 on certain cached systems.
3152*/
3153#define COUNT_TOP_ZEROES(v) \
3154 (((v) & ~0xffff) == 0 \
3155 ? ((v) & ~0xff) == 0 \
3156 ? ((v) & ~0xf) == 0 \
3157 ? ((v) & ~0x3) == 0 \
3158 ? ((v) & ~0x1) == 0 \
3159 ? !(v) \
3160 ? 32 \
3161 : 31 \
3162 : 30 \
3163 : ((v) & ~0x7) == 0 \
3164 ? 29 \
3165 : 28 \
3166 : ((v) & ~0x3f) == 0 \
3167 ? ((v) & ~0x1f) == 0 \
3168 ? 27 \
3169 : 26 \
3170 : ((v) & ~0x7f) == 0 \
3171 ? 25 \
3172 : 24 \
3173 : ((v) & ~0xfff) == 0 \
3174 ? ((v) & ~0x3ff) == 0 \
3175 ? ((v) & ~0x1ff) == 0 \
3176 ? 23 \
3177 : 22 \
3178 : ((v) & ~0x7ff) == 0 \
3179 ? 21 \
3180 : 20 \
3181 : ((v) & ~0x3fff) == 0 \
3182 ? ((v) & ~0x1fff) == 0 \
3183 ? 19 \
3184 : 18 \
3185 : ((v) & ~0x7fff) == 0 \
3186 ? 17 \
3187 : 16 \
3188 : ((v) & ~0xffffff) == 0 \
3189 ? ((v) & ~0xfffff) == 0 \
3190 ? ((v) & ~0x3ffff) == 0 \
3191 ? ((v) & ~0x1ffff) == 0 \
3192 ? 15 \
3193 : 14 \
3194 : ((v) & ~0x7ffff) == 0 \
3195 ? 13 \
3196 : 12 \
3197 : ((v) & ~0x3fffff) == 0 \
3198 ? ((v) & ~0x1fffff) == 0 \
3199 ? 11 \
3200 : 10 \
3201 : ((v) & ~0x7fffff) == 0 \
3202 ? 9 \
3203 : 8 \
3204 : ((v) & ~0xfffffff) == 0 \
3205 ? ((v) & ~0x3ffffff) == 0 \
3206 ? ((v) & ~0x1ffffff) == 0 \
3207 ? 7 \
3208 : 6 \
3209 : ((v) & ~0x7ffffff) == 0 \
3210 ? 5 \
3211 : 4 \
3212 : ((v) & ~0x3fffffff) == 0 \
3213 ? ((v) & ~0x1fffffff) == 0 \
3214 ? 3 \
3215 : 2 \
3216 : ((v) & ~0x7fffffff) == 0 \
3217 ? 1 \
3218 : 0)
3219
3220/* load_register()
3221 * This routine generates the least number of instructions neccessary to load
3222 * an absolute expression value into a register.
3223 */
3224static void
3225load_register (counter, reg, ep, dbl)
3226 int *counter;
3227 int reg;
3228 expressionS *ep;
3229 int dbl;
3230{
3231 int freg;
3232 expressionS hi32, lo32;
3233
3234 if (ep->X_op != O_big)
3235 {
3236 assert (ep->X_op == O_constant);
3237 if (ep->X_add_number < 0x8000
3238 && (ep->X_add_number >= 0
3239 || (ep->X_add_number >= -0x8000
3240 && (! dbl
3241 || ! ep->X_unsigned
3242 || sizeof (ep->X_add_number) > 4))))
3243 {
3244 /* We can handle 16 bit signed values with an addiu to
3245 $zero. No need to ever use daddiu here, since $zero and
3246 the result are always correct in 32 bit mode. */
3247 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3248 (int) BFD_RELOC_LO16);
3249 return;
3250 }
3251 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3252 {
3253 /* We can handle 16 bit unsigned values with an ori to
3254 $zero. */
3255 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3256 (int) BFD_RELOC_LO16);
3257 return;
3258 }
3259 else if ((((ep->X_add_number &~ (offsetT) 0x7fffffff) == 0
3260 || ((ep->X_add_number &~ (offsetT) 0x7fffffff)
3261 == ~ (offsetT) 0x7fffffff))
3262 && (! dbl
3263 || ! ep->X_unsigned
3264 || sizeof (ep->X_add_number) > 4
3265 || (ep->X_add_number & 0x80000000) == 0))
ca4e0257 3266 || ((HAVE_32BIT_GPRS || ! dbl)
252b5132 3267 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
ca4e0257 3268 || (HAVE_32BIT_GPRS
252b5132
RH
3269 && ! dbl
3270 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3271 == ~ (offsetT) 0xffffffff)))
3272 {
3273 /* 32 bit values require an lui. */
3274 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3275 (int) BFD_RELOC_HI16);
3276 if ((ep->X_add_number & 0xffff) != 0)
3277 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3278 (int) BFD_RELOC_LO16);
3279 return;
3280 }
3281 }
3282
3283 /* The value is larger than 32 bits. */
3284
ca4e0257 3285 if (HAVE_32BIT_GPRS)
252b5132
RH
3286 {
3287 as_bad (_("Number larger than 32 bits"));
3288 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3289 (int) BFD_RELOC_LO16);
3290 return;
3291 }
3292
3293 if (ep->X_op != O_big)
3294 {
3295 hi32 = *ep;
3296 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3297 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3298 hi32.X_add_number &= 0xffffffff;
3299 lo32 = *ep;
3300 lo32.X_add_number &= 0xffffffff;
3301 }
3302 else
3303 {
3304 assert (ep->X_add_number > 2);
3305 if (ep->X_add_number == 3)
3306 generic_bignum[3] = 0;
3307 else if (ep->X_add_number > 4)
3308 as_bad (_("Number larger than 64 bits"));
3309 lo32.X_op = O_constant;
3310 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3311 hi32.X_op = O_constant;
3312 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3313 }
3314
3315 if (hi32.X_add_number == 0)
3316 freg = 0;
3317 else
3318 {
3319 int shift, bit;
3320 unsigned long hi, lo;
3321
3322 if (hi32.X_add_number == 0xffffffff)
beae10d5
KH
3323 {
3324 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3325 {
3326 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
252b5132 3327 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3328 return;
3329 }
3330 if (lo32.X_add_number & 0x80000000)
3331 {
3332 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3333 (int) BFD_RELOC_HI16);
252b5132
RH
3334 if (lo32.X_add_number & 0xffff)
3335 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3336 reg, reg, (int) BFD_RELOC_LO16);
beae10d5
KH
3337 return;
3338 }
3339 }
252b5132
RH
3340
3341 /* Check for 16bit shifted constant. We know that hi32 is
3342 non-zero, so start the mask on the first bit of the hi32
3343 value. */
3344 shift = 17;
3345 do
beae10d5
KH
3346 {
3347 unsigned long himask, lomask;
3348
3349 if (shift < 32)
3350 {
3351 himask = 0xffff >> (32 - shift);
3352 lomask = (0xffff << shift) & 0xffffffff;
3353 }
3354 else
3355 {
3356 himask = 0xffff << (shift - 32);
3357 lomask = 0;
3358 }
3359 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3360 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3361 {
3362 expressionS tmp;
3363
3364 tmp.X_op = O_constant;
3365 if (shift < 32)
3366 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3367 | (lo32.X_add_number >> shift));
3368 else
3369 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3370 macro_build ((char *) NULL, counter, &tmp,
3371 "ori", "t,r,i", reg, 0,
3372 (int) BFD_RELOC_LO16);
3373 macro_build ((char *) NULL, counter, NULL,
3374 (shift >= 32) ? "dsll32" : "dsll",
3375 "d,w,<", reg, reg,
3376 (shift >= 32) ? shift - 32 : shift);
3377 return;
3378 }
3379 shift++;
3380 }
3381 while (shift <= (64 - 16));
252b5132
RH
3382
3383 /* Find the bit number of the lowest one bit, and store the
3384 shifted value in hi/lo. */
3385 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3386 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3387 if (lo != 0)
3388 {
3389 bit = 0;
3390 while ((lo & 1) == 0)
3391 {
3392 lo >>= 1;
3393 ++bit;
3394 }
3395 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3396 hi >>= bit;
3397 }
3398 else
3399 {
3400 bit = 32;
3401 while ((hi & 1) == 0)
3402 {
3403 hi >>= 1;
3404 ++bit;
3405 }
3406 lo = hi;
3407 hi = 0;
3408 }
3409
3410 /* Optimize if the shifted value is a (power of 2) - 1. */
3411 if ((hi == 0 && ((lo + 1) & lo) == 0)
3412 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3413 {
3414 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3415 if (shift != 0)
beae10d5 3416 {
252b5132
RH
3417 expressionS tmp;
3418
3419 /* This instruction will set the register to be all
3420 ones. */
beae10d5
KH
3421 tmp.X_op = O_constant;
3422 tmp.X_add_number = (offsetT) -1;
3423 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
252b5132 3424 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3425 if (bit != 0)
3426 {
3427 bit += shift;
3428 macro_build ((char *) NULL, counter, NULL,
3429 (bit >= 32) ? "dsll32" : "dsll",
3430 "d,w,<", reg, reg,
3431 (bit >= 32) ? bit - 32 : bit);
3432 }
3433 macro_build ((char *) NULL, counter, NULL,
252b5132 3434 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3435 "d,w,<", reg, reg,
252b5132 3436 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3437 return;
3438 }
3439 }
252b5132
RH
3440
3441 /* Sign extend hi32 before calling load_register, because we can
3442 generally get better code when we load a sign extended value. */
3443 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3444 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3445 load_register (counter, reg, &hi32, 0);
3446 freg = reg;
3447 }
3448 if ((lo32.X_add_number & 0xffff0000) == 0)
3449 {
3450 if (freg != 0)
3451 {
3452 macro_build ((char *) NULL, counter, NULL, "dsll32", "d,w,<", reg,
3453 freg, 0);
3454 freg = reg;
3455 }
3456 }
3457 else
3458 {
3459 expressionS mid16;
3460
3461 if ((freg == 0) && (lo32.X_add_number == 0xffffffff))
beae10d5 3462 {
252b5132
RH
3463 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3464 (int) BFD_RELOC_HI16);
beae10d5
KH
3465 macro_build ((char *) NULL, counter, NULL, "dsrl32", "d,w,<", reg,
3466 reg, 0);
3467 return;
3468 }
252b5132
RH
3469
3470 if (freg != 0)
3471 {
3472 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
3473 freg, 16);
3474 freg = reg;
3475 }
3476 mid16 = lo32;
3477 mid16.X_add_number >>= 16;
3478 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3479 freg, (int) BFD_RELOC_LO16);
3480 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
3481 reg, 16);
3482 freg = reg;
3483 }
3484 if ((lo32.X_add_number & 0xffff) != 0)
3485 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3486 (int) BFD_RELOC_LO16);
3487}
3488
3489/* Load an address into a register. */
3490
3491static void
3492load_address (counter, reg, ep)
3493 int *counter;
3494 int reg;
3495 expressionS *ep;
3496{
3497 char *p;
3498
3499 if (ep->X_op != O_constant
3500 && ep->X_op != O_symbol)
3501 {
3502 as_bad (_("expression too complex"));
3503 ep->X_op = O_constant;
3504 }
3505
3506 if (ep->X_op == O_constant)
3507 {
3508 load_register (counter, reg, ep, 0);
3509 return;
3510 }
3511
3512 if (mips_pic == NO_PIC)
3513 {
3514 /* If this is a reference to a GP relative symbol, we want
3515 addiu $reg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
3516 Otherwise we want
3517 lui $reg,<sym> (BFD_RELOC_HI16_S)
3518 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3519 If we have an addend, we always use the latter form. */
7283eb86 3520 if ((valueT) ep->X_add_number > MAX_GPREL_OFFSET
beae10d5 3521 || nopic_need_relax (ep->X_add_symbol, 1))
252b5132
RH
3522 p = NULL;
3523 else
3524 {
3525 frag_grow (20);
3526 macro_build ((char *) NULL, counter, ep,
ca4e0257 3527 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3528 "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
3529 p = frag_var (rs_machine_dependent, 8, 0,
3530 RELAX_ENCODE (4, 8, 0, 4, 0,
3531 mips_opts.warn_about_macros),
3532 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3533 }
3534 macro_build_lui (p, counter, ep, reg);
3535 if (p != NULL)
3536 p += 4;
3537 macro_build (p, counter, ep,
ca4e0257 3538 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3539 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3540 }
3541 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3542 {
3543 expressionS ex;
3544
3545 /* If this is a reference to an external symbol, we want
3546 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3547 Otherwise we want
3548 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3549 nop
3550 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3551 If there is a constant, it must be added in after. */
3552 ex.X_add_number = ep->X_add_number;
3553 ep->X_add_number = 0;
3554 frag_grow (20);
3555 macro_build ((char *) NULL, counter, ep,
ca4e0257 3556 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
3557 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3558 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3559 p = frag_var (rs_machine_dependent, 4, 0,
3560 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3561 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3562 macro_build (p, counter, ep,
ca4e0257 3563 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3564 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3565 if (ex.X_add_number != 0)
3566 {
3567 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3568 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3569 ex.X_op = O_constant;
3570 macro_build ((char *) NULL, counter, &ex,
ca4e0257 3571 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3572 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3573 }
3574 }
3575 else if (mips_pic == SVR4_PIC)
3576 {
3577 expressionS ex;
3578 int off;
3579
3580 /* This is the large GOT case. If this is a reference to an
3581 external symbol, we want
3582 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3583 addu $reg,$reg,$gp
3584 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3585 Otherwise, for a reference to a local symbol, we want
3586 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3587 nop
3588 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3589 If there is a constant, it must be added in after. */
3590 ex.X_add_number = ep->X_add_number;
3591 ep->X_add_number = 0;
3592 if (reg_needs_delay (GP))
3593 off = 4;
3594 else
3595 off = 0;
3596 frag_grow (32);
3597 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3598 (int) BFD_RELOC_MIPS_GOT_HI16);
3599 macro_build ((char *) NULL, counter, (expressionS *) NULL,
ca4e0257 3600 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
3601 "d,v,t", reg, reg, GP);
3602 macro_build ((char *) NULL, counter, ep,
ca4e0257 3603 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
3604 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3605 p = frag_var (rs_machine_dependent, 12 + off, 0,
3606 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3607 mips_opts.warn_about_macros),
3608 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3609 if (off > 0)
3610 {
3611 /* We need a nop before loading from $gp. This special
3612 check is required because the lui which starts the main
3613 instruction stream does not refer to $gp, and so will not
3614 insert the nop which may be required. */
3615 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3616 p += 4;
3617 }
9a41af64 3618 macro_build (p, counter, ep, HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
3619 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3620 p += 4;
3621 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3622 p += 4;
9a41af64 3623 macro_build (p, counter, ep, HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3624 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3625 if (ex.X_add_number != 0)
3626 {
3627 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3628 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3629 ex.X_op = O_constant;
3630 macro_build ((char *) NULL, counter, &ex,
ca4e0257 3631 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3632 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3633 }
3634 }
3635 else if (mips_pic == EMBEDDED_PIC)
3636 {
3637 /* We always do
3638 addiu $reg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
3639 */
3640 macro_build ((char *) NULL, counter, ep,
ca4e0257 3641 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3642 "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
3643 }
3644 else
3645 abort ();
3646}
3647
ea1fb5dc
RS
3648/* Move the contents of register SOURCE into register DEST. */
3649
3650static void
3651move_register (counter, dest, source)
3652 int *counter;
3653 int dest;
3654 int source;
3655{
3656 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3657 HAVE_32BIT_GPRS ? "addu" : "daddu",
3658 "d,v,t", dest, source, 0);
3659}
3660
252b5132
RH
3661/*
3662 * Build macros
3663 * This routine implements the seemingly endless macro or synthesized
3664 * instructions and addressing modes in the mips assembly language. Many
3665 * of these macros are simple and are similar to each other. These could
3666 * probably be handled by some kind of table or grammer aproach instead of
3667 * this verbose method. Others are not simple macros but are more like
3668 * optimizing code generation.
3669 * One interesting optimization is when several store macros appear
3670 * consecutivly that would load AT with the upper half of the same address.
3671 * The ensuing load upper instructions are ommited. This implies some kind
3672 * of global optimization. We currently only optimize within a single macro.
3673 * For many of the load and store macros if the address is specified as a
3674 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3675 * first load register 'at' with zero and use it as the base register. The
3676 * mips assembler simply uses register $zero. Just one tiny optimization
3677 * we're missing.
3678 */
3679static void
3680macro (ip)
3681 struct mips_cl_insn *ip;
3682{
3683 register int treg, sreg, dreg, breg;
3684 int tempreg;
3685 int mask;
3686 int icnt = 0;
43841e91 3687 int used_at = 0;
252b5132
RH
3688 expressionS expr1;
3689 const char *s;
3690 const char *s2;
3691 const char *fmt;
3692 int likely = 0;
3693 int dbl = 0;
3694 int coproc = 0;
3695 int lr = 0;
3696 int imm = 0;
3697 offsetT maxnum;
3698 int off;
3699 bfd_reloc_code_real_type r;
3700 char *p;
3701 int hold_mips_optimize;
3702
3703 assert (! mips_opts.mips16);
3704
3705 treg = (ip->insn_opcode >> 16) & 0x1f;
3706 dreg = (ip->insn_opcode >> 11) & 0x1f;
3707 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3708 mask = ip->insn_mo->mask;
3709
3710 expr1.X_op = O_constant;
3711 expr1.X_op_symbol = NULL;
3712 expr1.X_add_symbol = NULL;
3713 expr1.X_add_number = 1;
3714
3715 switch (mask)
3716 {
3717 case M_DABS:
3718 dbl = 1;
3719 case M_ABS:
3720 /* bgez $a0,.+12
3721 move v0,$a0
3722 sub v0,$zero,$a0
3723 */
3724
3725 mips_emit_delays (true);
3726 ++mips_opts.noreorder;
3727 mips_any_noreorder = 1;
3728
3729 expr1.X_add_number = 8;
3730 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3731 if (dreg == sreg)
3732 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3733 else
ea1fb5dc 3734 move_register (&icnt, dreg, sreg);
252b5132 3735 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 3736 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
3737
3738 --mips_opts.noreorder;
3739 return;
3740
3741 case M_ADD_I:
3742 s = "addi";
3743 s2 = "add";
3744 goto do_addi;
3745 case M_ADDU_I:
3746 s = "addiu";
3747 s2 = "addu";
3748 goto do_addi;
3749 case M_DADD_I:
3750 dbl = 1;
3751 s = "daddi";
3752 s2 = "dadd";
3753 goto do_addi;
3754 case M_DADDU_I:
3755 dbl = 1;
3756 s = "daddiu";
3757 s2 = "daddu";
3758 do_addi:
3759 if (imm_expr.X_op == O_constant
3760 && imm_expr.X_add_number >= -0x8000
3761 && imm_expr.X_add_number < 0x8000)
3762 {
3763 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3764 (int) BFD_RELOC_LO16);
3765 return;
3766 }
3767 load_register (&icnt, AT, &imm_expr, dbl);
3768 macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
3769 break;
3770
3771 case M_AND_I:
3772 s = "andi";
3773 s2 = "and";
3774 goto do_bit;
3775 case M_OR_I:
3776 s = "ori";
3777 s2 = "or";
3778 goto do_bit;
3779 case M_NOR_I:
3780 s = "";
3781 s2 = "nor";
3782 goto do_bit;
3783 case M_XOR_I:
3784 s = "xori";
3785 s2 = "xor";
3786 do_bit:
3787 if (imm_expr.X_op == O_constant
3788 && imm_expr.X_add_number >= 0
3789 && imm_expr.X_add_number < 0x10000)
3790 {
3791 if (mask != M_NOR_I)
3792 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3793 sreg, (int) BFD_RELOC_LO16);
3794 else
3795 {
3796 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3797 treg, sreg, (int) BFD_RELOC_LO16);
3798 macro_build ((char *) NULL, &icnt, NULL, "nor", "d,v,t",
3799 treg, treg, 0);
3800 }
3801 return;
3802 }
3803
3804 load_register (&icnt, AT, &imm_expr, 0);
3805 macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
3806 break;
3807
3808 case M_BEQ_I:
3809 s = "beq";
3810 goto beq_i;
3811 case M_BEQL_I:
3812 s = "beql";
3813 likely = 1;
3814 goto beq_i;
3815 case M_BNE_I:
3816 s = "bne";
3817 goto beq_i;
3818 case M_BNEL_I:
3819 s = "bnel";
3820 likely = 1;
3821 beq_i:
3822 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3823 {
3824 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3825 0);
3826 return;
3827 }
3828 load_register (&icnt, AT, &imm_expr, 0);
3829 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3830 break;
3831
3832 case M_BGEL:
3833 likely = 1;
3834 case M_BGE:
3835 if (treg == 0)
3836 {
3837 macro_build ((char *) NULL, &icnt, &offset_expr,
3838 likely ? "bgezl" : "bgez",
3839 "s,p", sreg);
3840 return;
3841 }
3842 if (sreg == 0)
3843 {
3844 macro_build ((char *) NULL, &icnt, &offset_expr,
3845 likely ? "blezl" : "blez",
3846 "s,p", treg);
3847 return;
3848 }
3849 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
3850 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3851 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3852 break;
3853
3854 case M_BGTL_I:
3855 likely = 1;
3856 case M_BGT_I:
3857 /* check for > max integer */
3858 maxnum = 0x7fffffff;
ca4e0257 3859 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
3860 {
3861 maxnum <<= 16;
3862 maxnum |= 0xffff;
3863 maxnum <<= 16;
3864 maxnum |= 0xffff;
3865 }
3866 if (imm_expr.X_op == O_constant
3867 && imm_expr.X_add_number >= maxnum
ca4e0257 3868 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
3869 {
3870 do_false:
3871 /* result is always false */
3872 if (! likely)
3873 {
39c0a331
L
3874 if (warn_nops)
3875 as_warn (_("Branch %s is always false (nop)"),
3876 ip->insn_mo->name);
252b5132
RH
3877 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3878 }
3879 else
3880 {
39c0a331
L
3881 if (warn_nops)
3882 as_warn (_("Branch likely %s is always false"),
3883 ip->insn_mo->name);
252b5132
RH
3884 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3885 "s,t,p", 0, 0);
3886 }
3887 return;
3888 }
3889 if (imm_expr.X_op != O_constant)
3890 as_bad (_("Unsupported large constant"));
3891 imm_expr.X_add_number++;
3892 /* FALLTHROUGH */
3893 case M_BGE_I:
3894 case M_BGEL_I:
3895 if (mask == M_BGEL_I)
3896 likely = 1;
3897 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3898 {
3899 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3900 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
3901 return;
3902 }
3903 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3904 {
3905 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3906 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
3907 return;
3908 }
3909 maxnum = 0x7fffffff;
ca4e0257 3910 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
3911 {
3912 maxnum <<= 16;
3913 maxnum |= 0xffff;
3914 maxnum <<= 16;
3915 maxnum |= 0xffff;
3916 }
3917 maxnum = - maxnum - 1;
3918 if (imm_expr.X_op == O_constant
3919 && imm_expr.X_add_number <= maxnum
ca4e0257 3920 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
3921 {
3922 do_true:
3923 /* result is always true */
3924 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
3925 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
3926 return;
3927 }
3928 set_at (&icnt, sreg, 0);
3929 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3930 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3931 break;
3932
3933 case M_BGEUL:
3934 likely = 1;
3935 case M_BGEU:
3936 if (treg == 0)
3937 goto do_true;
3938 if (sreg == 0)
3939 {
3940 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3941 likely ? "beql" : "beq", "s,t,p", 0, treg);
252b5132
RH
3942 return;
3943 }
3944 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
3945 treg);
3946 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3947 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3948 break;
3949
3950 case M_BGTUL_I:
3951 likely = 1;
3952 case M_BGTU_I:
3953 if (sreg == 0
ca4e0257 3954 || (HAVE_32BIT_GPRS
252b5132
RH
3955 && imm_expr.X_op == O_constant
3956 && imm_expr.X_add_number == 0xffffffff))
3957 goto do_false;
3958 if (imm_expr.X_op != O_constant)
3959 as_bad (_("Unsupported large constant"));
3960 imm_expr.X_add_number++;
3961 /* FALLTHROUGH */
3962 case M_BGEU_I:
3963 case M_BGEUL_I:
3964 if (mask == M_BGEUL_I)
3965 likely = 1;
3966 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3967 goto do_true;
3968 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3969 {
3970 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3971 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
3972 return;
3973 }
3974 set_at (&icnt, sreg, 1);
3975 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3976 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3977 break;
3978
3979 case M_BGTL:
3980 likely = 1;
3981 case M_BGT:
3982 if (treg == 0)
3983 {
3984 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3985 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
3986 return;
3987 }
3988 if (sreg == 0)
3989 {
3990 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3991 likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
3992 return;
3993 }
3994 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
3995 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3996 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
3997 break;
3998
3999 case M_BGTUL:
4000 likely = 1;
4001 case M_BGTU:
4002 if (treg == 0)
4003 {
4004 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4005 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4006 return;
4007 }
4008 if (sreg == 0)
4009 goto do_false;
4010 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
4011 sreg);
4012 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4013 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4014 break;
4015
4016 case M_BLEL:
4017 likely = 1;
4018 case M_BLE:
4019 if (treg == 0)
4020 {
4021 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4022 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4023 return;
4024 }
4025 if (sreg == 0)
4026 {
4027 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4028 likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4029 return;
4030 }
4031 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4032 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4033 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4034 break;
4035
4036 case M_BLEL_I:
4037 likely = 1;
4038 case M_BLE_I:
4039 maxnum = 0x7fffffff;
ca4e0257 4040 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4041 {
4042 maxnum <<= 16;
4043 maxnum |= 0xffff;
4044 maxnum <<= 16;
4045 maxnum |= 0xffff;
4046 }
4047 if (imm_expr.X_op == O_constant
4048 && imm_expr.X_add_number >= maxnum
ca4e0257 4049 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4050 goto do_true;
4051 if (imm_expr.X_op != O_constant)
4052 as_bad (_("Unsupported large constant"));
4053 imm_expr.X_add_number++;
4054 /* FALLTHROUGH */
4055 case M_BLT_I:
4056 case M_BLTL_I:
4057 if (mask == M_BLTL_I)
4058 likely = 1;
4059 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4060 {
4061 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4062 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4063 return;
4064 }
4065 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4066 {
4067 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4068 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4069 return;
4070 }
4071 set_at (&icnt, sreg, 0);
4072 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4073 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4074 break;
4075
4076 case M_BLEUL:
4077 likely = 1;
4078 case M_BLEU:
4079 if (treg == 0)
4080 {
4081 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4082 likely ? "beql" : "beq", "s,t,p", sreg, 0);
252b5132
RH
4083 return;
4084 }
4085 if (sreg == 0)
4086 goto do_true;
4087 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
4088 sreg);
4089 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4090 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4091 break;
4092
4093 case M_BLEUL_I:
4094 likely = 1;
4095 case M_BLEU_I:
4096 if (sreg == 0
ca4e0257 4097 || (HAVE_32BIT_GPRS
252b5132
RH
4098 && imm_expr.X_op == O_constant
4099 && imm_expr.X_add_number == 0xffffffff))
4100 goto do_true;
4101 if (imm_expr.X_op != O_constant)
4102 as_bad (_("Unsupported large constant"));
4103 imm_expr.X_add_number++;
4104 /* FALLTHROUGH */
4105 case M_BLTU_I:
4106 case M_BLTUL_I:
4107 if (mask == M_BLTUL_I)
4108 likely = 1;
4109 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4110 goto do_false;
4111 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4112 {
4113 macro_build ((char *) NULL, &icnt, &offset_expr,
4114 likely ? "beql" : "beq",
4115 "s,t,p", sreg, 0);
4116 return;
4117 }
4118 set_at (&icnt, sreg, 1);
4119 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4120 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4121 break;
4122
4123 case M_BLTL:
4124 likely = 1;
4125 case M_BLT:
4126 if (treg == 0)
4127 {
4128 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4129 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4130 return;
4131 }
4132 if (sreg == 0)
4133 {
4134 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4135 likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4136 return;
4137 }
4138 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4139 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4140 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4141 break;
4142
4143 case M_BLTUL:
4144 likely = 1;
4145 case M_BLTU:
4146 if (treg == 0)
4147 goto do_false;
4148 if (sreg == 0)
4149 {
4150 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4151 likely ? "bnel" : "bne", "s,t,p", 0, treg);
252b5132
RH
4152 return;
4153 }
4154 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
4155 treg);
4156 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4157 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4158 break;
4159
4160 case M_DDIV_3:
4161 dbl = 1;
4162 case M_DIV_3:
4163 s = "mflo";
4164 goto do_div3;
4165 case M_DREM_3:
4166 dbl = 1;
4167 case M_REM_3:
4168 s = "mfhi";
4169 do_div3:
4170 if (treg == 0)
4171 {
4172 as_warn (_("Divide by zero."));
4173 if (mips_trap)
4174 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
4175 else
9117d219 4176 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4177 return;
4178 }
4179
4180 mips_emit_delays (true);
4181 ++mips_opts.noreorder;
4182 mips_any_noreorder = 1;
4183 if (mips_trap)
4184 {
4185 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
4186 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 4187 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4188 }
4189 else
4190 {
4191 expr1.X_add_number = 8;
4192 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4193 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 4194 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
9117d219 4195 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4196 }
4197 expr1.X_add_number = -1;
4198 macro_build ((char *) NULL, &icnt, &expr1,
4199 dbl ? "daddiu" : "addiu",
4200 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4201 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4202 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4203 if (dbl)
4204 {
4205 expr1.X_add_number = 1;
4206 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4207 (int) BFD_RELOC_LO16);
4208 macro_build ((char *) NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT,
4209 31);
4210 }
4211 else
4212 {
4213 expr1.X_add_number = 0x80000000;
4214 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4215 (int) BFD_RELOC_HI16);
4216 }
4217 if (mips_trap)
4218 {
4219 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", sreg, AT);
4220 /* We want to close the noreorder block as soon as possible, so
4221 that later insns are available for delay slot filling. */
4222 --mips_opts.noreorder;
4223 }
4224 else
4225 {
4226 expr1.X_add_number = 8;
4227 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4228 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
4229
4230 /* We want to close the noreorder block as soon as possible, so
4231 that later insns are available for delay slot filling. */
4232 --mips_opts.noreorder;
4233
9117d219 4234 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
252b5132
RH
4235 }
4236 macro_build ((char *) NULL, &icnt, NULL, s, "d", dreg);
4237 break;
4238
4239 case M_DIV_3I:
4240 s = "div";
4241 s2 = "mflo";
4242 goto do_divi;
4243 case M_DIVU_3I:
4244 s = "divu";
4245 s2 = "mflo";
4246 goto do_divi;
4247 case M_REM_3I:
4248 s = "div";
4249 s2 = "mfhi";
4250 goto do_divi;
4251 case M_REMU_3I:
4252 s = "divu";
4253 s2 = "mfhi";
4254 goto do_divi;
4255 case M_DDIV_3I:
4256 dbl = 1;
4257 s = "ddiv";
4258 s2 = "mflo";
4259 goto do_divi;
4260 case M_DDIVU_3I:
4261 dbl = 1;
4262 s = "ddivu";
4263 s2 = "mflo";
4264 goto do_divi;
4265 case M_DREM_3I:
4266 dbl = 1;
4267 s = "ddiv";
4268 s2 = "mfhi";
4269 goto do_divi;
4270 case M_DREMU_3I:
4271 dbl = 1;
4272 s = "ddivu";
4273 s2 = "mfhi";
4274 do_divi:
4275 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4276 {
4277 as_warn (_("Divide by zero."));
4278 if (mips_trap)
4279 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
4280 else
beae10d5 4281 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4282 return;
4283 }
4284 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4285 {
4286 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4287 move_register (&icnt, dreg, sreg);
252b5132 4288 else
ea1fb5dc 4289 move_register (&icnt, dreg, 0);
252b5132
RH
4290 return;
4291 }
4292 if (imm_expr.X_op == O_constant
4293 && imm_expr.X_add_number == -1
4294 && s[strlen (s) - 1] != 'u')
4295 {
4296 if (strcmp (s2, "mflo") == 0)
4297 {
9a41af64
TS
4298 macro_build ((char *) NULL, &icnt, NULL, dbl ? "dneg" : "neg",
4299 "d,w", dreg, sreg);
252b5132
RH
4300 }
4301 else
ea1fb5dc 4302 move_register (&icnt, dreg, 0);
252b5132
RH
4303 return;
4304 }
4305
4306 load_register (&icnt, AT, &imm_expr, dbl);
4307 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
4308 macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
4309 break;
4310
4311 case M_DIVU_3:
4312 s = "divu";
4313 s2 = "mflo";
4314 goto do_divu3;
4315 case M_REMU_3:
4316 s = "divu";
4317 s2 = "mfhi";
4318 goto do_divu3;
4319 case M_DDIVU_3:
4320 s = "ddivu";
4321 s2 = "mflo";
4322 goto do_divu3;
4323 case M_DREMU_3:
4324 s = "ddivu";
4325 s2 = "mfhi";
4326 do_divu3:
4327 mips_emit_delays (true);
4328 ++mips_opts.noreorder;
4329 mips_any_noreorder = 1;
4330 if (mips_trap)
4331 {
4332 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
4333 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4334 /* We want to close the noreorder block as soon as possible, so
4335 that later insns are available for delay slot filling. */
4336 --mips_opts.noreorder;
4337 }
4338 else
4339 {
4340 expr1.X_add_number = 8;
4341 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4342 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4343
4344 /* We want to close the noreorder block as soon as possible, so
4345 that later insns are available for delay slot filling. */
4346 --mips_opts.noreorder;
beae10d5 4347 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4348 }
4349 macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
4350 return;
4351
4352 case M_DLA_AB:
4353 dbl = 1;
4354 case M_LA_AB:
4355 /* Load the address of a symbol into a register. If breg is not
4356 zero, we then add a base register to it. */
4357
4358 /* When generating embedded PIC code, we permit expressions of
4359 the form
4360 la $4,foo-bar
bb2d6cd7 4361 where bar is an address in the current section. These are used
252b5132
RH
4362 when getting the addresses of functions. We don't permit
4363 X_add_number to be non-zero, because if the symbol is
4364 external the relaxing code needs to know that any addend is
4365 purely the offset to X_op_symbol. */
4366 if (mips_pic == EMBEDDED_PIC
4367 && offset_expr.X_op == O_subtract
49309057 4368 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4369 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4370 : (symbol_equated_p (offset_expr.X_op_symbol)
4371 && (S_GET_SEGMENT
4372 (symbol_get_value_expression (offset_expr.X_op_symbol)
4373 ->X_add_symbol)
bb2d6cd7 4374 == now_seg)))
252b5132 4375 && breg == 0
bb2d6cd7
GK
4376 && (offset_expr.X_add_number == 0
4377 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132
RH
4378 {
4379 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4380 treg, (int) BFD_RELOC_PCREL_HI16_S);
4381 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 4382 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4383 "t,r,j", treg, treg, (int) BFD_RELOC_PCREL_LO16);
4384 return;
4385 }
4386
4387 if (offset_expr.X_op != O_symbol
4388 && offset_expr.X_op != O_constant)
4389 {
4390 as_bad (_("expression too complex"));
4391 offset_expr.X_op = O_constant;
4392 }
4393
4394 if (treg == breg)
4395 {
4396 tempreg = AT;
4397 used_at = 1;
4398 }
4399 else
4400 {
4401 tempreg = treg;
4402 used_at = 0;
4403 }
4404
4405 if (offset_expr.X_op == O_constant)
4406 load_register (&icnt, tempreg, &offset_expr, dbl);
4407 else if (mips_pic == NO_PIC)
4408 {
4409 /* If this is a reference to an GP relative symbol, we want
4410 addiu $tempreg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
4411 Otherwise we want
4412 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4413 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4414 If we have a constant, we need two instructions anyhow,
4415 so we may as well always use the latter form. */
7283eb86 4416 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
4417 || nopic_need_relax (offset_expr.X_add_symbol, 1))
4418 p = NULL;
4419 else
4420 {
4421 frag_grow (20);
4422 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 4423 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4424 "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
4425 p = frag_var (rs_machine_dependent, 8, 0,
4426 RELAX_ENCODE (4, 8, 0, 4, 0,
4427 mips_opts.warn_about_macros),
4428 offset_expr.X_add_symbol, (offsetT) 0,
4429 (char *) NULL);
4430 }
4431 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4432 if (p != NULL)
4433 p += 4;
4434 macro_build (p, &icnt, &offset_expr,
ca4e0257 4435 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4436 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4437 }
4438 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4439 {
9117d219
NC
4440 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4441
252b5132
RH
4442 /* If this is a reference to an external symbol, and there
4443 is no constant, we want
4444 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9117d219
NC
4445 or if tempreg is PIC_CALL_REG
4446 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
4447 For a local symbol, we want
4448 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4449 nop
4450 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4451
4452 If we have a small constant, and this is a reference to
4453 an external symbol, we want
4454 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4455 nop
4456 addiu $tempreg,$tempreg,<constant>
4457 For a local symbol, we want the same instruction
4458 sequence, but we output a BFD_RELOC_LO16 reloc on the
4459 addiu instruction.
4460
4461 If we have a large constant, and this is a reference to
4462 an external symbol, we want
4463 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4464 lui $at,<hiconstant>
4465 addiu $at,$at,<loconstant>
4466 addu $tempreg,$tempreg,$at
4467 For a local symbol, we want the same instruction
4468 sequence, but we output a BFD_RELOC_LO16 reloc on the
4469 addiu instruction. */
4470 expr1.X_add_number = offset_expr.X_add_number;
4471 offset_expr.X_add_number = 0;
4472 frag_grow (32);
9117d219
NC
4473 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4474 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
252b5132
RH
4475 macro_build ((char *) NULL, &icnt, &offset_expr,
4476 dbl ? "ld" : "lw",
9117d219 4477 "t,o(b)", tempreg, lw_reloc_type, GP);
252b5132
RH
4478 if (expr1.X_add_number == 0)
4479 {
4480 int off;
4481
4482 if (breg == 0)
4483 off = 0;
4484 else
4485 {
4486 /* We're going to put in an addu instruction using
4487 tempreg, so we may as well insert the nop right
4488 now. */
4489 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4490 "nop", "");
4491 off = 4;
4492 }
4493 p = frag_var (rs_machine_dependent, 8 - off, 0,
4494 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4495 (breg == 0
4496 ? mips_opts.warn_about_macros
4497 : 0)),
4498 offset_expr.X_add_symbol, (offsetT) 0,
4499 (char *) NULL);
4500 if (breg == 0)
4501 {
4502 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4503 p += 4;
4504 }
4505 macro_build (p, &icnt, &expr1,
ca4e0257 4506 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4507 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4508 /* FIXME: If breg == 0, and the next instruction uses
4509 $tempreg, then if this variant case is used an extra
4510 nop will be generated. */
4511 }
4512 else if (expr1.X_add_number >= -0x8000
4513 && expr1.X_add_number < 0x8000)
4514 {
4515 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4516 "nop", "");
4517 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4518 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4519 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4520 (void) frag_var (rs_machine_dependent, 0, 0,
4521 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4522 offset_expr.X_add_symbol, (offsetT) 0,
4523 (char *) NULL);
4524 }
4525 else
4526 {
4527 int off1;
4528
4529 /* If we are going to add in a base register, and the
4530 target register and the base register are the same,
4531 then we are using AT as a temporary register. Since
4532 we want to load the constant into AT, we add our
4533 current AT (from the global offset table) and the
4534 register into the register now, and pretend we were
4535 not using a base register. */
4536 if (breg != treg)
4537 off1 = 0;
4538 else
4539 {
4540 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4541 "nop", "");
4542 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4543 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4544 "d,v,t", treg, AT, breg);
4545 breg = 0;
4546 tempreg = treg;
4547 off1 = -8;
4548 }
4549
4550 /* Set mips_optimize around the lui instruction to avoid
4551 inserting an unnecessary nop after the lw. */
4552 hold_mips_optimize = mips_optimize;
4553 mips_optimize = 2;
4554 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
4555 mips_optimize = hold_mips_optimize;
4556
4557 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4558 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4559 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4560 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4561 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4562 "d,v,t", tempreg, tempreg, AT);
4563 (void) frag_var (rs_machine_dependent, 0, 0,
4564 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4565 offset_expr.X_add_symbol, (offsetT) 0,
4566 (char *) NULL);
4567 used_at = 1;
4568 }
4569 }
4570 else if (mips_pic == SVR4_PIC)
4571 {
4572 int gpdel;
9117d219
NC
4573 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4574 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
252b5132
RH
4575
4576 /* This is the large GOT case. If this is a reference to an
4577 external symbol, and there is no constant, we want
4578 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4579 addu $tempreg,$tempreg,$gp
4580 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9117d219
NC
4581 or if tempreg is PIC_CALL_REG
4582 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4583 addu $tempreg,$tempreg,$gp
4584 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
4585 For a local symbol, we want
4586 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4587 nop
4588 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4589
4590 If we have a small constant, and this is a reference to
4591 an external symbol, we want
4592 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4593 addu $tempreg,$tempreg,$gp
4594 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4595 nop
4596 addiu $tempreg,$tempreg,<constant>
4597 For a local symbol, we want
4598 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4599 nop
4600 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4601
4602 If we have a large constant, and this is a reference to
4603 an external symbol, we want
4604 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4605 addu $tempreg,$tempreg,$gp
4606 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4607 lui $at,<hiconstant>
4608 addiu $at,$at,<loconstant>
4609 addu $tempreg,$tempreg,$at
4610 For a local symbol, we want
4611 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4612 lui $at,<hiconstant>
4613 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4614 addu $tempreg,$tempreg,$at
4615 */
4616 expr1.X_add_number = offset_expr.X_add_number;
4617 offset_expr.X_add_number = 0;
4618 frag_grow (52);
4619 if (reg_needs_delay (GP))
4620 gpdel = 4;
4621 else
4622 gpdel = 0;
9117d219
NC
4623 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4624 {
4625 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4626 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4627 }
252b5132 4628 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 4629 tempreg, lui_reloc_type);
252b5132 4630 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4631 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4632 "d,v,t", tempreg, tempreg, GP);
4633 macro_build ((char *) NULL, &icnt, &offset_expr,
4634 dbl ? "ld" : "lw",
9117d219 4635 "t,o(b)", tempreg, lw_reloc_type, tempreg);
252b5132
RH
4636 if (expr1.X_add_number == 0)
4637 {
4638 int off;
4639
4640 if (breg == 0)
4641 off = 0;
4642 else
4643 {
4644 /* We're going to put in an addu instruction using
4645 tempreg, so we may as well insert the nop right
4646 now. */
4647 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4648 "nop", "");
4649 off = 4;
4650 }
4651
4652 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4653 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4654 8 + gpdel, 0,
4655 (breg == 0
4656 ? mips_opts.warn_about_macros
4657 : 0)),
4658 offset_expr.X_add_symbol, (offsetT) 0,
4659 (char *) NULL);
4660 }
4661 else if (expr1.X_add_number >= -0x8000
4662 && expr1.X_add_number < 0x8000)
4663 {
4664 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4665 "nop", "");
4666 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4667 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4668 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4669
4670 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4671 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4672 (breg == 0
4673 ? mips_opts.warn_about_macros
4674 : 0)),
4675 offset_expr.X_add_symbol, (offsetT) 0,
4676 (char *) NULL);
4677 }
4678 else
4679 {
4680 int adj, dreg;
4681
4682 /* If we are going to add in a base register, and the
4683 target register and the base register are the same,
4684 then we are using AT as a temporary register. Since
4685 we want to load the constant into AT, we add our
4686 current AT (from the global offset table) and the
4687 register into the register now, and pretend we were
4688 not using a base register. */
4689 if (breg != treg)
4690 {
4691 adj = 0;
4692 dreg = tempreg;
4693 }
4694 else
4695 {
4696 assert (tempreg == AT);
4697 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4698 "nop", "");
4699 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4700 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4701 "d,v,t", treg, AT, breg);
4702 dreg = treg;
4703 adj = 8;
4704 }
4705
4706 /* Set mips_optimize around the lui instruction to avoid
4707 inserting an unnecessary nop after the lw. */
4708 hold_mips_optimize = mips_optimize;
4709 mips_optimize = 2;
4710 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
4711 mips_optimize = hold_mips_optimize;
4712
4713 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4714 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4715 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4716 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4717 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4718 "d,v,t", dreg, dreg, AT);
4719
4720 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4721 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4722 8 + gpdel, 0,
4723 (breg == 0
4724 ? mips_opts.warn_about_macros
4725 : 0)),
4726 offset_expr.X_add_symbol, (offsetT) 0,
4727 (char *) NULL);
4728
4729 used_at = 1;
4730 }
4731
4732 if (gpdel > 0)
4733 {
4734 /* This is needed because this instruction uses $gp, but
4735 the first instruction on the main stream does not. */
4736 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4737 p += 4;
4738 }
4739 macro_build (p, &icnt, &offset_expr,
4740 dbl ? "ld" : "lw",
4741 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
4742 p += 4;
4743 if (expr1.X_add_number >= -0x8000
4744 && expr1.X_add_number < 0x8000)
4745 {
4746 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4747 p += 4;
4748 macro_build (p, &icnt, &expr1,
ca4e0257 4749 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4750 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4751 /* FIXME: If add_number is 0, and there was no base
4752 register, the external symbol case ended with a load,
4753 so if the symbol turns out to not be external, and
4754 the next instruction uses tempreg, an unnecessary nop
4755 will be inserted. */
4756 }
4757 else
4758 {
4759 if (breg == treg)
4760 {
4761 /* We must add in the base register now, as in the
4762 external symbol case. */
4763 assert (tempreg == AT);
4764 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4765 p += 4;
4766 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 4767 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4768 "d,v,t", treg, AT, breg);
4769 p += 4;
4770 tempreg = treg;
4771 /* We set breg to 0 because we have arranged to add
4772 it in in both cases. */
4773 breg = 0;
4774 }
4775
4776 macro_build_lui (p, &icnt, &expr1, AT);
4777 p += 4;
4778 macro_build (p, &icnt, &expr1,
ca4e0257 4779 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4780 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4781 p += 4;
4782 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 4783 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4784 "d,v,t", tempreg, tempreg, AT);
4785 p += 4;
4786 }
4787 }
4788 else if (mips_pic == EMBEDDED_PIC)
4789 {
4790 /* We use
4791 addiu $tempreg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
4792 */
4793 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 4794 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4795 "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
4796 }
4797 else
4798 abort ();
4799
4800 if (breg != 0)
4801 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4802 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4803 "d,v,t", treg, tempreg, breg);
4804
4805 if (! used_at)
4806 return;
4807
4808 break;
4809
4810 case M_J_A:
4811 /* The j instruction may not be used in PIC code, since it
4812 requires an absolute address. We convert it to a b
4813 instruction. */
4814 if (mips_pic == NO_PIC)
4815 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
4816 else
4817 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4818 return;
4819
4820 /* The jal instructions must be handled as macros because when
4821 generating PIC code they expand to multi-instruction
4822 sequences. Normally they are simple instructions. */
4823 case M_JAL_1:
4824 dreg = RA;
4825 /* Fall through. */
4826 case M_JAL_2:
4827 if (mips_pic == NO_PIC
4828 || mips_pic == EMBEDDED_PIC)
4829 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
4830 "d,s", dreg, sreg);
4831 else if (mips_pic == SVR4_PIC)
4832 {
4833 if (sreg != PIC_CALL_REG)
4834 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 4835
252b5132
RH
4836 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
4837 "d,s", dreg, sreg);
4838 if (mips_cprestore_offset < 0)
4839 as_warn (_("No .cprestore pseudo-op used in PIC code"));
4840 else
4841 {
4842 expr1.X_add_number = mips_cprestore_offset;
4843 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4844 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
4845 "t,o(b)", GP, (int) BFD_RELOC_LO16, mips_frame_reg);
4846 }
4847 }
4848 else
4849 abort ();
4850
4851 return;
4852
4853 case M_JAL_A:
4854 if (mips_pic == NO_PIC)
4855 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
4856 else if (mips_pic == SVR4_PIC)
4857 {
4858 /* If this is a reference to an external symbol, and we are
4859 using a small GOT, we want
4860 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4861 nop
4862 jalr $25
4863 nop
4864 lw $gp,cprestore($sp)
4865 The cprestore value is set using the .cprestore
4866 pseudo-op. If we are using a big GOT, we want
4867 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4868 addu $25,$25,$gp
4869 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
4870 nop
4871 jalr $25
4872 nop
4873 lw $gp,cprestore($sp)
4874 If the symbol is not external, we want
4875 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4876 nop
4877 addiu $25,$25,<sym> (BFD_RELOC_LO16)
4878 jalr $25
4879 nop
4880 lw $gp,cprestore($sp) */
4881 frag_grow (40);
4882 if (! mips_big_got)
4883 {
4884 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 4885 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
4886 "t,o(b)", PIC_CALL_REG,
4887 (int) BFD_RELOC_MIPS_CALL16, GP);
4888 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4889 "nop", "");
4890 p = frag_var (rs_machine_dependent, 4, 0,
4891 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
4892 offset_expr.X_add_symbol, (offsetT) 0,
4893 (char *) NULL);
4894 }
4895 else
4896 {
4897 int gpdel;
4898
4899 if (reg_needs_delay (GP))
4900 gpdel = 4;
4901 else
4902 gpdel = 0;
4903 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4904 PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
4905 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4906 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4907 "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
4908 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 4909 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
4910 "t,o(b)", PIC_CALL_REG,
4911 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
4912 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4913 "nop", "");
4914 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4915 RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
4916 0, 0),
4917 offset_expr.X_add_symbol, (offsetT) 0,
4918 (char *) NULL);
4919 if (gpdel > 0)
4920 {
4921 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4922 p += 4;
4923 }
4924 macro_build (p, &icnt, &offset_expr,
ca4e0257 4925 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
4926 "t,o(b)", PIC_CALL_REG,
4927 (int) BFD_RELOC_MIPS_GOT16, GP);
4928 p += 4;
4929 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4930 p += 4;
bdaaa2e1 4931 }
252b5132 4932 macro_build (p, &icnt, &offset_expr,
ca4e0257 4933 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4934 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
4935 (int) BFD_RELOC_LO16);
4936 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4937 "jalr", "s", PIC_CALL_REG);
4938 if (mips_cprestore_offset < 0)
4939 as_warn (_("No .cprestore pseudo-op used in PIC code"));
4940 else
4941 {
4942 if (mips_opts.noreorder)
4943 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4944 "nop", "");
4945 expr1.X_add_number = mips_cprestore_offset;
4946 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4947 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
4948 "t,o(b)", GP, (int) BFD_RELOC_LO16,
4949 mips_frame_reg);
4950 }
4951 }
4952 else if (mips_pic == EMBEDDED_PIC)
4953 {
4954 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
4955 /* The linker may expand the call to a longer sequence which
4956 uses $at, so we must break rather than return. */
4957 break;
4958 }
4959 else
4960 abort ();
4961
4962 return;
4963
4964 case M_LB_AB:
4965 s = "lb";
4966 goto ld;
4967 case M_LBU_AB:
4968 s = "lbu";
4969 goto ld;
4970 case M_LH_AB:
4971 s = "lh";
4972 goto ld;
4973 case M_LHU_AB:
4974 s = "lhu";
4975 goto ld;
4976 case M_LW_AB:
4977 s = "lw";
4978 goto ld;
4979 case M_LWC0_AB:
4980 s = "lwc0";
bdaaa2e1 4981 /* Itbl support may require additional care here. */
252b5132
RH
4982 coproc = 1;
4983 goto ld;
4984 case M_LWC1_AB:
4985 s = "lwc1";
bdaaa2e1 4986 /* Itbl support may require additional care here. */
252b5132
RH
4987 coproc = 1;
4988 goto ld;
4989 case M_LWC2_AB:
4990 s = "lwc2";
bdaaa2e1 4991 /* Itbl support may require additional care here. */
252b5132
RH
4992 coproc = 1;
4993 goto ld;
4994 case M_LWC3_AB:
4995 s = "lwc3";
bdaaa2e1 4996 /* Itbl support may require additional care here. */
252b5132
RH
4997 coproc = 1;
4998 goto ld;
4999 case M_LWL_AB:
5000 s = "lwl";
5001 lr = 1;
5002 goto ld;
5003 case M_LWR_AB:
5004 s = "lwr";
5005 lr = 1;
5006 goto ld;
5007 case M_LDC1_AB:
ec68c924 5008 if (mips_arch == CPU_R4650)
252b5132
RH
5009 {
5010 as_bad (_("opcode not supported on this processor"));
5011 return;
5012 }
5013 s = "ldc1";
bdaaa2e1 5014 /* Itbl support may require additional care here. */
252b5132
RH
5015 coproc = 1;
5016 goto ld;
5017 case M_LDC2_AB:
5018 s = "ldc2";
bdaaa2e1 5019 /* Itbl support may require additional care here. */
252b5132
RH
5020 coproc = 1;
5021 goto ld;
5022 case M_LDC3_AB:
5023 s = "ldc3";
bdaaa2e1 5024 /* Itbl support may require additional care here. */
252b5132
RH
5025 coproc = 1;
5026 goto ld;
5027 case M_LDL_AB:
5028 s = "ldl";
5029 lr = 1;
5030 goto ld;
5031 case M_LDR_AB:
5032 s = "ldr";
5033 lr = 1;
5034 goto ld;
5035 case M_LL_AB:
5036 s = "ll";
5037 goto ld;
5038 case M_LLD_AB:
5039 s = "lld";
5040 goto ld;
5041 case M_LWU_AB:
5042 s = "lwu";
5043 ld:
5044 if (breg == treg || coproc || lr)
5045 {
5046 tempreg = AT;
5047 used_at = 1;
5048 }
5049 else
5050 {
5051 tempreg = treg;
5052 used_at = 0;
5053 }
5054 goto ld_st;
5055 case M_SB_AB:
5056 s = "sb";
5057 goto st;
5058 case M_SH_AB:
5059 s = "sh";
5060 goto st;
5061 case M_SW_AB:
5062 s = "sw";
5063 goto st;
5064 case M_SWC0_AB:
5065 s = "swc0";
bdaaa2e1 5066 /* Itbl support may require additional care here. */
252b5132
RH
5067 coproc = 1;
5068 goto st;
5069 case M_SWC1_AB:
5070 s = "swc1";
bdaaa2e1 5071 /* Itbl support may require additional care here. */
252b5132
RH
5072 coproc = 1;
5073 goto st;
5074 case M_SWC2_AB:
5075 s = "swc2";
bdaaa2e1 5076 /* Itbl support may require additional care here. */
252b5132
RH
5077 coproc = 1;
5078 goto st;
5079 case M_SWC3_AB:
5080 s = "swc3";
bdaaa2e1 5081 /* Itbl support may require additional care here. */
252b5132
RH
5082 coproc = 1;
5083 goto st;
5084 case M_SWL_AB:
5085 s = "swl";
5086 goto st;
5087 case M_SWR_AB:
5088 s = "swr";
5089 goto st;
5090 case M_SC_AB:
5091 s = "sc";
5092 goto st;
5093 case M_SCD_AB:
5094 s = "scd";
5095 goto st;
5096 case M_SDC1_AB:
ec68c924 5097 if (mips_arch == CPU_R4650)
252b5132
RH
5098 {
5099 as_bad (_("opcode not supported on this processor"));
5100 return;
5101 }
5102 s = "sdc1";
5103 coproc = 1;
bdaaa2e1 5104 /* Itbl support may require additional care here. */
252b5132
RH
5105 goto st;
5106 case M_SDC2_AB:
5107 s = "sdc2";
bdaaa2e1 5108 /* Itbl support may require additional care here. */
252b5132
RH
5109 coproc = 1;
5110 goto st;
5111 case M_SDC3_AB:
5112 s = "sdc3";
bdaaa2e1 5113 /* Itbl support may require additional care here. */
252b5132
RH
5114 coproc = 1;
5115 goto st;
5116 case M_SDL_AB:
5117 s = "sdl";
5118 goto st;
5119 case M_SDR_AB:
5120 s = "sdr";
5121 st:
5122 tempreg = AT;
5123 used_at = 1;
5124 ld_st:
bdaaa2e1 5125 /* Itbl support may require additional care here. */
252b5132
RH
5126 if (mask == M_LWC1_AB
5127 || mask == M_SWC1_AB
5128 || mask == M_LDC1_AB
5129 || mask == M_SDC1_AB
5130 || mask == M_L_DAB
5131 || mask == M_S_DAB)
5132 fmt = "T,o(b)";
5133 else if (coproc)
5134 fmt = "E,o(b)";
5135 else
5136 fmt = "t,o(b)";
5137
5138 if (offset_expr.X_op != O_constant
5139 && offset_expr.X_op != O_symbol)
5140 {
5141 as_bad (_("expression too complex"));
5142 offset_expr.X_op = O_constant;
5143 }
5144
5145 /* A constant expression in PIC code can be handled just as it
5146 is in non PIC code. */
5147 if (mips_pic == NO_PIC
5148 || offset_expr.X_op == O_constant)
5149 {
5150 /* If this is a reference to a GP relative symbol, and there
5151 is no base register, we want
5152 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5153 Otherwise, if there is no base register, we want
5154 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5155 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5156 If we have a constant, we need two instructions anyhow,
5157 so we always use the latter form.
5158
5159 If we have a base register, and this is a reference to a
5160 GP relative symbol, we want
5161 addu $tempreg,$breg,$gp
5162 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GPREL)
5163 Otherwise we want
5164 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5165 addu $tempreg,$tempreg,$breg
5166 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5167 With a constant we always use the latter case. */
5168 if (breg == 0)
5169 {
e7d556df 5170 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5171 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5172 p = NULL;
5173 else
5174 {
5175 frag_grow (20);
5176 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5177 treg, (int) BFD_RELOC_MIPS_GPREL, GP);
5178 p = frag_var (rs_machine_dependent, 8, 0,
5179 RELAX_ENCODE (4, 8, 0, 4, 0,
5180 (mips_opts.warn_about_macros
5181 || (used_at
5182 && mips_opts.noat))),
5183 offset_expr.X_add_symbol, (offsetT) 0,
5184 (char *) NULL);
5185 used_at = 0;
5186 }
5187 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5188 if (p != NULL)
5189 p += 4;
5190 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5191 (int) BFD_RELOC_LO16, tempreg);
5192 }
5193 else
5194 {
e7d556df 5195 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5196 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5197 p = NULL;
5198 else
5199 {
5200 frag_grow (28);
5201 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5202 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5203 "d,v,t", tempreg, breg, GP);
5204 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5205 treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
5206 p = frag_var (rs_machine_dependent, 12, 0,
5207 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5208 offset_expr.X_add_symbol, (offsetT) 0,
5209 (char *) NULL);
5210 }
5211 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5212 if (p != NULL)
5213 p += 4;
5214 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5215 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5216 "d,v,t", tempreg, tempreg, breg);
5217 if (p != NULL)
5218 p += 4;
5219 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5220 (int) BFD_RELOC_LO16, tempreg);
5221 }
5222 }
5223 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5224 {
5225 /* If this is a reference to an external symbol, we want
5226 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5227 nop
5228 <op> $treg,0($tempreg)
5229 Otherwise we want
5230 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5231 nop
5232 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5233 <op> $treg,0($tempreg)
5234 If there is a base register, we add it to $tempreg before
5235 the <op>. If there is a constant, we stick it in the
5236 <op> instruction. We don't handle constants larger than
5237 16 bits, because we have no way to load the upper 16 bits
5238 (actually, we could handle them for the subset of cases
5239 in which we are not using $at). */
5240 assert (offset_expr.X_op == O_symbol);
5241 expr1.X_add_number = offset_expr.X_add_number;
5242 offset_expr.X_add_number = 0;
5243 if (expr1.X_add_number < -0x8000
5244 || expr1.X_add_number >= 0x8000)
5245 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5246 frag_grow (20);
5247 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5248 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5249 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5250 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
bdaaa2e1 5251 p = frag_var (rs_machine_dependent, 4, 0,
252b5132
RH
5252 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5253 offset_expr.X_add_symbol, (offsetT) 0,
5254 (char *) NULL);
5255 macro_build (p, &icnt, &offset_expr,
ca4e0257 5256 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5257 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5258 if (breg != 0)
5259 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5260 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5261 "d,v,t", tempreg, tempreg, breg);
5262 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5263 (int) BFD_RELOC_LO16, tempreg);
5264 }
5265 else if (mips_pic == SVR4_PIC)
5266 {
5267 int gpdel;
5268
5269 /* If this is a reference to an external symbol, we want
5270 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5271 addu $tempreg,$tempreg,$gp
5272 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5273 <op> $treg,0($tempreg)
5274 Otherwise we want
5275 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5276 nop
5277 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5278 <op> $treg,0($tempreg)
5279 If there is a base register, we add it to $tempreg before
5280 the <op>. If there is a constant, we stick it in the
5281 <op> instruction. We don't handle constants larger than
5282 16 bits, because we have no way to load the upper 16 bits
5283 (actually, we could handle them for the subset of cases
5284 in which we are not using $at). */
5285 assert (offset_expr.X_op == O_symbol);
5286 expr1.X_add_number = offset_expr.X_add_number;
5287 offset_expr.X_add_number = 0;
5288 if (expr1.X_add_number < -0x8000
5289 || expr1.X_add_number >= 0x8000)
5290 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5291 if (reg_needs_delay (GP))
5292 gpdel = 4;
5293 else
5294 gpdel = 0;
5295 frag_grow (36);
5296 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5297 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5298 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5299 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5300 "d,v,t", tempreg, tempreg, GP);
5301 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5302 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5303 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5304 tempreg);
5305 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5306 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5307 offset_expr.X_add_symbol, (offsetT) 0, (char *) NULL);
5308 if (gpdel > 0)
5309 {
5310 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5311 p += 4;
5312 }
5313 macro_build (p, &icnt, &offset_expr,
ca4e0257 5314 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5315 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5316 p += 4;
5317 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5318 p += 4;
5319 macro_build (p, &icnt, &offset_expr,
ca4e0257 5320 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5321 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5322 if (breg != 0)
5323 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5324 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5325 "d,v,t", tempreg, tempreg, breg);
5326 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5327 (int) BFD_RELOC_LO16, tempreg);
5328 }
5329 else if (mips_pic == EMBEDDED_PIC)
5330 {
5331 /* If there is no base register, we want
5332 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5333 If there is a base register, we want
5334 addu $tempreg,$breg,$gp
5335 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GPREL)
5336 */
5337 assert (offset_expr.X_op == O_symbol);
5338 if (breg == 0)
5339 {
5340 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5341 treg, (int) BFD_RELOC_MIPS_GPREL, GP);
5342 used_at = 0;
5343 }
5344 else
5345 {
5346 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5347 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5348 "d,v,t", tempreg, breg, GP);
5349 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5350 treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
5351 }
5352 }
5353 else
5354 abort ();
5355
5356 if (! used_at)
5357 return;
5358
5359 break;
5360
5361 case M_LI:
5362 case M_LI_S:
5363 load_register (&icnt, treg, &imm_expr, 0);
5364 return;
5365
5366 case M_DLI:
5367 load_register (&icnt, treg, &imm_expr, 1);
5368 return;
5369
5370 case M_LI_SS:
5371 if (imm_expr.X_op == O_constant)
5372 {
5373 load_register (&icnt, AT, &imm_expr, 0);
5374 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5375 "mtc1", "t,G", AT, treg);
5376 break;
5377 }
5378 else
5379 {
5380 assert (offset_expr.X_op == O_symbol
5381 && strcmp (segment_name (S_GET_SEGMENT
5382 (offset_expr.X_add_symbol)),
5383 ".lit4") == 0
5384 && offset_expr.X_add_number == 0);
5385 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5386 treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5387 return;
5388 }
5389
5390 case M_LI_D:
ca4e0257
RS
5391 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5392 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5393 order 32 bits of the value and the low order 32 bits are either
5394 zero or in OFFSET_EXPR. */
252b5132
RH
5395 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5396 {
ca4e0257 5397 if (HAVE_64BIT_GPRS)
252b5132
RH
5398 load_register (&icnt, treg, &imm_expr, 1);
5399 else
5400 {
5401 int hreg, lreg;
5402
5403 if (target_big_endian)
5404 {
5405 hreg = treg;
5406 lreg = treg + 1;
5407 }
5408 else
5409 {
5410 hreg = treg + 1;
5411 lreg = treg;
5412 }
5413
5414 if (hreg <= 31)
5415 load_register (&icnt, hreg, &imm_expr, 0);
5416 if (lreg <= 31)
5417 {
5418 if (offset_expr.X_op == O_absent)
ea1fb5dc 5419 move_register (&icnt, lreg, 0);
252b5132
RH
5420 else
5421 {
5422 assert (offset_expr.X_op == O_constant);
5423 load_register (&icnt, lreg, &offset_expr, 0);
5424 }
5425 }
5426 }
5427 return;
5428 }
5429
5430 /* We know that sym is in the .rdata section. First we get the
5431 upper 16 bits of the address. */
5432 if (mips_pic == NO_PIC)
5433 {
5434 /* FIXME: This won't work for a 64 bit address. */
5435 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
5436 }
5437 else if (mips_pic == SVR4_PIC)
5438 {
5439 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5440 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5441 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5442 }
5443 else if (mips_pic == EMBEDDED_PIC)
5444 {
5445 /* For embedded PIC we pick up the entire address off $gp in
5446 a single instruction. */
5447 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5448 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5449 "t,r,j", AT, GP, (int) BFD_RELOC_MIPS_GPREL);
5450 offset_expr.X_op = O_constant;
5451 offset_expr.X_add_number = 0;
5452 }
5453 else
5454 abort ();
bdaaa2e1 5455
252b5132 5456 /* Now we load the register(s). */
ca4e0257 5457 if (HAVE_64BIT_GPRS)
252b5132
RH
5458 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5459 treg, (int) BFD_RELOC_LO16, AT);
5460 else
5461 {
5462 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5463 treg, (int) BFD_RELOC_LO16, AT);
5464 if (treg != 31)
5465 {
5466 /* FIXME: How in the world do we deal with the possible
5467 overflow here? */
5468 offset_expr.X_add_number += 4;
5469 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5470 treg + 1, (int) BFD_RELOC_LO16, AT);
5471 }
5472 }
5473
5474 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5475 does not become a variant frag. */
5476 frag_wane (frag_now);
5477 frag_new (0);
5478
5479 break;
5480
5481 case M_LI_DD:
ca4e0257
RS
5482 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5483 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5484 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5485 the value and the low order 32 bits are either zero or in
5486 OFFSET_EXPR. */
252b5132
RH
5487 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5488 {
ca4e0257
RS
5489 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5490 if (HAVE_64BIT_FPRS)
5491 {
5492 assert (HAVE_64BIT_GPRS);
5493 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5494 "dmtc1", "t,S", AT, treg);
5495 }
252b5132
RH
5496 else
5497 {
5498 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5499 "mtc1", "t,G", AT, treg + 1);
5500 if (offset_expr.X_op == O_absent)
5501 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5502 "mtc1", "t,G", 0, treg);
5503 else
5504 {
5505 assert (offset_expr.X_op == O_constant);
5506 load_register (&icnt, AT, &offset_expr, 0);
5507 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5508 "mtc1", "t,G", AT, treg);
5509 }
5510 }
5511 break;
5512 }
5513
5514 assert (offset_expr.X_op == O_symbol
5515 && offset_expr.X_add_number == 0);
5516 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5517 if (strcmp (s, ".lit8") == 0)
5518 {
e7af610e 5519 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5520 {
5521 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5522 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5523 return;
5524 }
5525 breg = GP;
5526 r = BFD_RELOC_MIPS_LITERAL;
5527 goto dob;
5528 }
5529 else
5530 {
5531 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5532 if (mips_pic == SVR4_PIC)
5533 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5534 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5535 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5536 else
5537 {
5538 /* FIXME: This won't work for a 64 bit address. */
5539 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
5540 }
bdaaa2e1 5541
e7af610e 5542 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5543 {
5544 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5545 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5546
5547 /* To avoid confusion in tc_gen_reloc, we must ensure
5548 that this does not become a variant frag. */
5549 frag_wane (frag_now);
5550 frag_new (0);
5551
5552 break;
5553 }
5554 breg = AT;
5555 r = BFD_RELOC_LO16;
5556 goto dob;
5557 }
5558
5559 case M_L_DOB:
ec68c924 5560 if (mips_arch == CPU_R4650)
252b5132
RH
5561 {
5562 as_bad (_("opcode not supported on this processor"));
5563 return;
5564 }
5565 /* Even on a big endian machine $fn comes before $fn+1. We have
5566 to adjust when loading from memory. */
5567 r = BFD_RELOC_LO16;
5568 dob:
e7af610e 5569 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
5570 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5571 target_big_endian ? treg + 1 : treg,
5572 (int) r, breg);
5573 /* FIXME: A possible overflow which I don't know how to deal
5574 with. */
5575 offset_expr.X_add_number += 4;
5576 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5577 target_big_endian ? treg : treg + 1,
5578 (int) r, breg);
5579
5580 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5581 does not become a variant frag. */
5582 frag_wane (frag_now);
5583 frag_new (0);
5584
5585 if (breg != AT)
5586 return;
5587 break;
5588
5589 case M_L_DAB:
5590 /*
5591 * The MIPS assembler seems to check for X_add_number not
5592 * being double aligned and generating:
5593 * lui at,%hi(foo+1)
5594 * addu at,at,v1
5595 * addiu at,at,%lo(foo+1)
5596 * lwc1 f2,0(at)
5597 * lwc1 f3,4(at)
5598 * But, the resulting address is the same after relocation so why
5599 * generate the extra instruction?
5600 */
ec68c924 5601 if (mips_arch == CPU_R4650)
252b5132
RH
5602 {
5603 as_bad (_("opcode not supported on this processor"));
5604 return;
5605 }
bdaaa2e1 5606 /* Itbl support may require additional care here. */
252b5132 5607 coproc = 1;
e7af610e 5608 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5609 {
5610 s = "ldc1";
5611 goto ld;
5612 }
5613
5614 s = "lwc1";
5615 fmt = "T,o(b)";
5616 goto ldd_std;
5617
5618 case M_S_DAB:
ec68c924 5619 if (mips_arch == CPU_R4650)
252b5132
RH
5620 {
5621 as_bad (_("opcode not supported on this processor"));
5622 return;
5623 }
5624
e7af610e 5625 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5626 {
5627 s = "sdc1";
5628 goto st;
5629 }
5630
5631 s = "swc1";
5632 fmt = "T,o(b)";
bdaaa2e1 5633 /* Itbl support may require additional care here. */
252b5132
RH
5634 coproc = 1;
5635 goto ldd_std;
5636
5637 case M_LD_AB:
ca4e0257 5638 if (HAVE_64BIT_GPRS)
252b5132
RH
5639 {
5640 s = "ld";
5641 goto ld;
5642 }
5643
5644 s = "lw";
5645 fmt = "t,o(b)";
5646 goto ldd_std;
5647
5648 case M_SD_AB:
ca4e0257 5649 if (HAVE_64BIT_GPRS)
252b5132
RH
5650 {
5651 s = "sd";
5652 goto st;
5653 }
5654
5655 s = "sw";
5656 fmt = "t,o(b)";
5657
5658 ldd_std:
5659 if (offset_expr.X_op != O_symbol
5660 && offset_expr.X_op != O_constant)
5661 {
5662 as_bad (_("expression too complex"));
5663 offset_expr.X_op = O_constant;
5664 }
5665
5666 /* Even on a big endian machine $fn comes before $fn+1. We have
5667 to adjust when loading from memory. We set coproc if we must
5668 load $fn+1 first. */
bdaaa2e1 5669 /* Itbl support may require additional care here. */
252b5132
RH
5670 if (! target_big_endian)
5671 coproc = 0;
5672
5673 if (mips_pic == NO_PIC
5674 || offset_expr.X_op == O_constant)
5675 {
5676 /* If this is a reference to a GP relative symbol, we want
5677 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5678 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
5679 If we have a base register, we use this
5680 addu $at,$breg,$gp
5681 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
5682 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
5683 If this is not a GP relative symbol, we want
5684 lui $at,<sym> (BFD_RELOC_HI16_S)
5685 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5686 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5687 If there is a base register, we add it to $at after the
5688 lui instruction. If there is a constant, we always use
5689 the last case. */
e7d556df 5690 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5691 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5692 {
5693 p = NULL;
5694 used_at = 1;
5695 }
5696 else
5697 {
5698 int off;
5699
5700 if (breg == 0)
5701 {
5702 frag_grow (28);
5703 tempreg = GP;
5704 off = 0;
5705 used_at = 0;
5706 }
5707 else
5708 {
5709 frag_grow (36);
5710 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5711 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5712 "d,v,t", AT, breg, GP);
5713 tempreg = AT;
5714 off = 4;
5715 used_at = 1;
5716 }
5717
beae10d5 5718 /* Itbl support may require additional care here. */
252b5132
RH
5719 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5720 coproc ? treg + 1 : treg,
5721 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5722 offset_expr.X_add_number += 4;
5723
5724 /* Set mips_optimize to 2 to avoid inserting an
5725 undesired nop. */
5726 hold_mips_optimize = mips_optimize;
5727 mips_optimize = 2;
beae10d5 5728 /* Itbl support may require additional care here. */
252b5132
RH
5729 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5730 coproc ? treg : treg + 1,
5731 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5732 mips_optimize = hold_mips_optimize;
5733
5734 p = frag_var (rs_machine_dependent, 12 + off, 0,
5735 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
5736 used_at && mips_opts.noat),
5737 offset_expr.X_add_symbol, (offsetT) 0,
5738 (char *) NULL);
5739
5740 /* We just generated two relocs. When tc_gen_reloc
5741 handles this case, it will skip the first reloc and
5742 handle the second. The second reloc already has an
5743 extra addend of 4, which we added above. We must
5744 subtract it out, and then subtract another 4 to make
5745 the first reloc come out right. The second reloc
5746 will come out right because we are going to add 4 to
5747 offset_expr when we build its instruction below.
5748
5749 If we have a symbol, then we don't want to include
5750 the offset, because it will wind up being included
5751 when we generate the reloc. */
5752
5753 if (offset_expr.X_op == O_constant)
5754 offset_expr.X_add_number -= 8;
5755 else
5756 {
5757 offset_expr.X_add_number = -4;
5758 offset_expr.X_op = O_constant;
5759 }
5760 }
5761 macro_build_lui (p, &icnt, &offset_expr, AT);
5762 if (p != NULL)
5763 p += 4;
5764 if (breg != 0)
5765 {
5766 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5767 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5768 "d,v,t", AT, breg, AT);
5769 if (p != NULL)
5770 p += 4;
5771 }
beae10d5 5772 /* Itbl support may require additional care here. */
252b5132
RH
5773 macro_build (p, &icnt, &offset_expr, s, fmt,
5774 coproc ? treg + 1 : treg,
5775 (int) BFD_RELOC_LO16, AT);
5776 if (p != NULL)
5777 p += 4;
5778 /* FIXME: How do we handle overflow here? */
5779 offset_expr.X_add_number += 4;
beae10d5 5780 /* Itbl support may require additional care here. */
252b5132
RH
5781 macro_build (p, &icnt, &offset_expr, s, fmt,
5782 coproc ? treg : treg + 1,
5783 (int) BFD_RELOC_LO16, AT);
bdaaa2e1 5784 }
252b5132
RH
5785 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5786 {
5787 int off;
5788
5789 /* If this is a reference to an external symbol, we want
5790 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5791 nop
5792 <op> $treg,0($at)
5793 <op> $treg+1,4($at)
5794 Otherwise we want
5795 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5796 nop
5797 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5798 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5799 If there is a base register we add it to $at before the
5800 lwc1 instructions. If there is a constant we include it
5801 in the lwc1 instructions. */
5802 used_at = 1;
5803 expr1.X_add_number = offset_expr.X_add_number;
5804 offset_expr.X_add_number = 0;
5805 if (expr1.X_add_number < -0x8000
5806 || expr1.X_add_number >= 0x8000 - 4)
5807 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5808 if (breg == 0)
5809 off = 0;
5810 else
5811 off = 4;
5812 frag_grow (24 + off);
5813 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5814 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5815 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5816 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5817 if (breg != 0)
5818 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5819 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 5820 "d,v,t", AT, breg, AT);
beae10d5 5821 /* Itbl support may require additional care here. */
252b5132
RH
5822 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5823 coproc ? treg + 1 : treg,
5824 (int) BFD_RELOC_LO16, AT);
5825 expr1.X_add_number += 4;
5826
5827 /* Set mips_optimize to 2 to avoid inserting an undesired
5828 nop. */
5829 hold_mips_optimize = mips_optimize;
5830 mips_optimize = 2;
beae10d5 5831 /* Itbl support may require additional care here. */
252b5132
RH
5832 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5833 coproc ? treg : treg + 1,
5834 (int) BFD_RELOC_LO16, AT);
5835 mips_optimize = hold_mips_optimize;
5836
5837 (void) frag_var (rs_machine_dependent, 0, 0,
5838 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
5839 offset_expr.X_add_symbol, (offsetT) 0,
5840 (char *) NULL);
5841 }
5842 else if (mips_pic == SVR4_PIC)
5843 {
5844 int gpdel, off;
5845
5846 /* If this is a reference to an external symbol, we want
5847 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5848 addu $at,$at,$gp
5849 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
5850 nop
5851 <op> $treg,0($at)
5852 <op> $treg+1,4($at)
5853 Otherwise we want
5854 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5855 nop
5856 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5857 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5858 If there is a base register we add it to $at before the
5859 lwc1 instructions. If there is a constant we include it
5860 in the lwc1 instructions. */
5861 used_at = 1;
5862 expr1.X_add_number = offset_expr.X_add_number;
5863 offset_expr.X_add_number = 0;
5864 if (expr1.X_add_number < -0x8000
5865 || expr1.X_add_number >= 0x8000 - 4)
5866 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5867 if (reg_needs_delay (GP))
5868 gpdel = 4;
5869 else
5870 gpdel = 0;
5871 if (breg == 0)
5872 off = 0;
5873 else
5874 off = 4;
5875 frag_grow (56);
5876 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5877 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
5878 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5879 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5880 "d,v,t", AT, AT, GP);
5881 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5882 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5883 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
5884 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5885 if (breg != 0)
5886 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5887 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 5888 "d,v,t", AT, breg, AT);
beae10d5 5889 /* Itbl support may require additional care here. */
252b5132
RH
5890 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5891 coproc ? treg + 1 : treg,
5892 (int) BFD_RELOC_LO16, AT);
5893 expr1.X_add_number += 4;
5894
5895 /* Set mips_optimize to 2 to avoid inserting an undesired
5896 nop. */
5897 hold_mips_optimize = mips_optimize;
5898 mips_optimize = 2;
beae10d5 5899 /* Itbl support may require additional care here. */
252b5132
RH
5900 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5901 coproc ? treg : treg + 1,
5902 (int) BFD_RELOC_LO16, AT);
5903 mips_optimize = hold_mips_optimize;
5904 expr1.X_add_number -= 4;
5905
5906 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
5907 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
5908 8 + gpdel + off, 1, 0),
5909 offset_expr.X_add_symbol, (offsetT) 0,
5910 (char *) NULL);
5911 if (gpdel > 0)
5912 {
5913 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5914 p += 4;
5915 }
5916 macro_build (p, &icnt, &offset_expr,
ca4e0257 5917 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5918 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5919 p += 4;
5920 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5921 p += 4;
5922 if (breg != 0)
5923 {
5924 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5925 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5926 "d,v,t", AT, breg, AT);
5927 p += 4;
5928 }
beae10d5 5929 /* Itbl support may require additional care here. */
252b5132
RH
5930 macro_build (p, &icnt, &expr1, s, fmt,
5931 coproc ? treg + 1 : treg,
5932 (int) BFD_RELOC_LO16, AT);
5933 p += 4;
5934 expr1.X_add_number += 4;
5935
5936 /* Set mips_optimize to 2 to avoid inserting an undesired
5937 nop. */
5938 hold_mips_optimize = mips_optimize;
5939 mips_optimize = 2;
beae10d5 5940 /* Itbl support may require additional care here. */
252b5132
RH
5941 macro_build (p, &icnt, &expr1, s, fmt,
5942 coproc ? treg : treg + 1,
5943 (int) BFD_RELOC_LO16, AT);
5944 mips_optimize = hold_mips_optimize;
5945 }
5946 else if (mips_pic == EMBEDDED_PIC)
5947 {
5948 /* If there is no base register, we use
5949 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5950 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
5951 If we have a base register, we use
5952 addu $at,$breg,$gp
5953 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
5954 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
5955 */
5956 if (breg == 0)
5957 {
5958 tempreg = GP;
5959 used_at = 0;
5960 }
5961 else
5962 {
5963 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5964 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5965 "d,v,t", AT, breg, GP);
5966 tempreg = AT;
5967 used_at = 1;
5968 }
5969
beae10d5 5970 /* Itbl support may require additional care here. */
252b5132
RH
5971 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5972 coproc ? treg + 1 : treg,
5973 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5974 offset_expr.X_add_number += 4;
beae10d5 5975 /* Itbl support may require additional care here. */
252b5132
RH
5976 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5977 coproc ? treg : treg + 1,
5978 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5979 }
5980 else
5981 abort ();
5982
5983 if (! used_at)
5984 return;
5985
5986 break;
5987
5988 case M_LD_OB:
5989 s = "lw";
5990 goto sd_ob;
5991 case M_SD_OB:
5992 s = "sw";
5993 sd_ob:
ca4e0257 5994 assert (HAVE_32BIT_ADDRESSES);
252b5132
RH
5995 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
5996 (int) BFD_RELOC_LO16, breg);
5997 offset_expr.X_add_number += 4;
5998 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
5999 (int) BFD_RELOC_LO16, breg);
6000 return;
6001
6002 /* New code added to support COPZ instructions.
6003 This code builds table entries out of the macros in mip_opcodes.
6004 R4000 uses interlocks to handle coproc delays.
6005 Other chips (like the R3000) require nops to be inserted for delays.
6006
f72c8c98 6007 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6008 In order to fill delay slots for non-interlocked chips,
6009 we must have a way to specify delays based on the coprocessor.
6010 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6011 What are the side-effects of the cop instruction?
6012 What cache support might we have and what are its effects?
6013 Both coprocessor & memory require delays. how long???
bdaaa2e1 6014 What registers are read/set/modified?
252b5132
RH
6015
6016 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6017 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6018
6019 case M_COP0:
6020 s = "c0";
6021 goto copz;
6022 case M_COP1:
6023 s = "c1";
6024 goto copz;
6025 case M_COP2:
6026 s = "c2";
6027 goto copz;
6028 case M_COP3:
6029 s = "c3";
6030 copz:
6031 /* For now we just do C (same as Cz). The parameter will be
6032 stored in insn_opcode by mips_ip. */
6033 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6034 ip->insn_opcode);
6035 return;
6036
ea1fb5dc
RS
6037 case M_MOVE:
6038 move_register (&icnt, dreg, sreg);
6039 return;
6040
252b5132
RH
6041#ifdef LOSING_COMPILER
6042 default:
6043 /* Try and see if this is a new itbl instruction.
6044 This code builds table entries out of the macros in mip_opcodes.
6045 FIXME: For now we just assemble the expression and pass it's
6046 value along as a 32-bit immediate.
bdaaa2e1 6047 We may want to have the assembler assemble this value,
252b5132
RH
6048 so that we gain the assembler's knowledge of delay slots,
6049 symbols, etc.
6050 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6051 if (itbl_have_entries
252b5132 6052 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6053 {
252b5132
RH
6054 s = ip->insn_mo->name;
6055 s2 = "cop3";
6056 coproc = ITBL_DECODE_PNUM (immed_expr);;
6057 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6058 return;
beae10d5 6059 }
252b5132
RH
6060 macro2 (ip);
6061 return;
6062 }
6063 if (mips_opts.noat)
6064 as_warn (_("Macro used $at after \".set noat\""));
6065}
bdaaa2e1 6066
252b5132
RH
6067static void
6068macro2 (ip)
6069 struct mips_cl_insn *ip;
6070{
6071 register int treg, sreg, dreg, breg;
6072 int tempreg;
6073 int mask;
6074 int icnt = 0;
6075 int used_at;
6076 expressionS expr1;
6077 const char *s;
6078 const char *s2;
6079 const char *fmt;
6080 int likely = 0;
6081 int dbl = 0;
6082 int coproc = 0;
6083 int lr = 0;
6084 int imm = 0;
6085 int off;
6086 offsetT maxnum;
6087 bfd_reloc_code_real_type r;
6088 char *p;
bdaaa2e1 6089
252b5132
RH
6090 treg = (ip->insn_opcode >> 16) & 0x1f;
6091 dreg = (ip->insn_opcode >> 11) & 0x1f;
6092 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6093 mask = ip->insn_mo->mask;
bdaaa2e1 6094
252b5132
RH
6095 expr1.X_op = O_constant;
6096 expr1.X_op_symbol = NULL;
6097 expr1.X_add_symbol = NULL;
6098 expr1.X_add_number = 1;
bdaaa2e1 6099
252b5132
RH
6100 switch (mask)
6101 {
6102#endif /* LOSING_COMPILER */
6103
6104 case M_DMUL:
6105 dbl = 1;
6106 case M_MUL:
6107 macro_build ((char *) NULL, &icnt, NULL,
6108 dbl ? "dmultu" : "multu",
6109 "s,t", sreg, treg);
6110 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
6111 return;
6112
6113 case M_DMUL_I:
6114 dbl = 1;
6115 case M_MUL_I:
6116 /* The MIPS assembler some times generates shifts and adds. I'm
6117 not trying to be that fancy. GCC should do this for us
6118 anyway. */
6119 load_register (&icnt, AT, &imm_expr, dbl);
6120 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 6121 dbl ? "dmult" : "mult", "s,t", sreg, AT);
252b5132
RH
6122 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
6123 break;
6124
6125 case M_DMULO_I:
6126 dbl = 1;
6127 case M_MULO_I:
6128 imm = 1;
6129 goto do_mulo;
6130
6131 case M_DMULO:
6132 dbl = 1;
6133 case M_MULO:
6134 do_mulo:
6135 mips_emit_delays (true);
6136 ++mips_opts.noreorder;
6137 mips_any_noreorder = 1;
6138 if (imm)
6139 load_register (&icnt, AT, &imm_expr, dbl);
6140 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 6141 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
252b5132
RH
6142 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
6143 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 6144 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, 31);
252b5132
RH
6145 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
6146 if (mips_trap)
6147 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", dreg, AT);
6148 else
6149 {
6150 expr1.X_add_number = 8;
6151 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
6152 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
beae10d5 6153 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
252b5132
RH
6154 }
6155 --mips_opts.noreorder;
6156 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
6157 break;
6158
6159 case M_DMULOU_I:
6160 dbl = 1;
6161 case M_MULOU_I:
6162 imm = 1;
6163 goto do_mulou;
6164
6165 case M_DMULOU:
6166 dbl = 1;
6167 case M_MULOU:
6168 do_mulou:
6169 mips_emit_delays (true);
6170 ++mips_opts.noreorder;
6171 mips_any_noreorder = 1;
6172 if (imm)
6173 load_register (&icnt, AT, &imm_expr, dbl);
6174 macro_build ((char *) NULL, &icnt, NULL,
6175 dbl ? "dmultu" : "multu",
6176 "s,t", sreg, imm ? AT : treg);
6177 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
6178 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
6179 if (mips_trap)
6180 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", AT, 0);
6181 else
6182 {
6183 expr1.X_add_number = 8;
6184 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6185 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
beae10d5 6186 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
252b5132
RH
6187 }
6188 --mips_opts.noreorder;
6189 break;
6190
6191 case M_ROL:
6192 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
6193 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
6194 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg,
6195 treg);
6196 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6197 break;
6198
6199 case M_ROL_I:
6200 if (imm_expr.X_op != O_constant)
6201 as_bad (_("rotate count too large"));
6202 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg,
6203 (int) (imm_expr.X_add_number & 0x1f));
6204 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
6205 (int) ((0 - imm_expr.X_add_number) & 0x1f));
6206 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6207 break;
6208
6209 case M_ROR:
6210 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
6211 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
6212 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg,
6213 treg);
6214 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6215 break;
6216
6217 case M_ROR_I:
6218 if (imm_expr.X_op != O_constant)
6219 as_bad (_("rotate count too large"));
6220 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg,
6221 (int) (imm_expr.X_add_number & 0x1f));
6222 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
6223 (int) ((0 - imm_expr.X_add_number) & 0x1f));
6224 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6225 break;
6226
6227 case M_S_DOB:
ec68c924 6228 if (mips_arch == CPU_R4650)
252b5132
RH
6229 {
6230 as_bad (_("opcode not supported on this processor"));
6231 return;
6232 }
e7af610e 6233 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6234 /* Even on a big endian machine $fn comes before $fn+1. We have
6235 to adjust when storing to memory. */
6236 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6237 target_big_endian ? treg + 1 : treg,
6238 (int) BFD_RELOC_LO16, breg);
6239 offset_expr.X_add_number += 4;
6240 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6241 target_big_endian ? treg : treg + 1,
6242 (int) BFD_RELOC_LO16, breg);
6243 return;
6244
6245 case M_SEQ:
6246 if (sreg == 0)
6247 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6248 treg, (int) BFD_RELOC_LO16);
6249 else if (treg == 0)
6250 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6251 sreg, (int) BFD_RELOC_LO16);
6252 else
6253 {
6254 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6255 sreg, treg);
6256 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6257 dreg, (int) BFD_RELOC_LO16);
6258 }
6259 return;
6260
6261 case M_SEQ_I:
6262 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6263 {
6264 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6265 sreg, (int) BFD_RELOC_LO16);
6266 return;
6267 }
6268 if (sreg == 0)
6269 {
6270 as_warn (_("Instruction %s: result is always false"),
6271 ip->insn_mo->name);
ea1fb5dc 6272 move_register (&icnt, dreg, 0);
252b5132
RH
6273 return;
6274 }
6275 if (imm_expr.X_op == O_constant
6276 && imm_expr.X_add_number >= 0
6277 && imm_expr.X_add_number < 0x10000)
6278 {
6279 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6280 sreg, (int) BFD_RELOC_LO16);
6281 used_at = 0;
6282 }
6283 else if (imm_expr.X_op == O_constant
6284 && imm_expr.X_add_number > -0x8000
6285 && imm_expr.X_add_number < 0)
6286 {
6287 imm_expr.X_add_number = -imm_expr.X_add_number;
6288 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6289 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6290 "t,r,j", dreg, sreg,
6291 (int) BFD_RELOC_LO16);
6292 used_at = 0;
6293 }
6294 else
6295 {
6296 load_register (&icnt, AT, &imm_expr, 0);
6297 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6298 sreg, AT);
6299 used_at = 1;
6300 }
6301 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6302 (int) BFD_RELOC_LO16);
6303 if (used_at)
6304 break;
6305 return;
6306
6307 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6308 s = "slt";
6309 goto sge;
6310 case M_SGEU:
6311 s = "sltu";
6312 sge:
6313 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
6314 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6315 (int) BFD_RELOC_LO16);
6316 return;
6317
6318 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6319 case M_SGEU_I:
6320 if (imm_expr.X_op == O_constant
6321 && imm_expr.X_add_number >= -0x8000
6322 && imm_expr.X_add_number < 0x8000)
6323 {
6324 macro_build ((char *) NULL, &icnt, &imm_expr,
6325 mask == M_SGE_I ? "slti" : "sltiu",
6326 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6327 used_at = 0;
6328 }
6329 else
6330 {
6331 load_register (&icnt, AT, &imm_expr, 0);
6332 macro_build ((char *) NULL, &icnt, NULL,
6333 mask == M_SGE_I ? "slt" : "sltu",
6334 "d,v,t", dreg, sreg, AT);
6335 used_at = 1;
6336 }
6337 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6338 (int) BFD_RELOC_LO16);
6339 if (used_at)
6340 break;
6341 return;
6342
6343 case M_SGT: /* sreg > treg <==> treg < sreg */
6344 s = "slt";
6345 goto sgt;
6346 case M_SGTU:
6347 s = "sltu";
6348 sgt:
6349 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
6350 return;
6351
6352 case M_SGT_I: /* sreg > I <==> I < sreg */
6353 s = "slt";
6354 goto sgti;
6355 case M_SGTU_I:
6356 s = "sltu";
6357 sgti:
6358 load_register (&icnt, AT, &imm_expr, 0);
6359 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
6360 break;
6361
6362 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
6363 s = "slt";
6364 goto sle;
6365 case M_SLEU:
6366 s = "sltu";
6367 sle:
6368 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
6369 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6370 (int) BFD_RELOC_LO16);
6371 return;
6372
6373 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6374 s = "slt";
6375 goto slei;
6376 case M_SLEU_I:
6377 s = "sltu";
6378 slei:
6379 load_register (&icnt, AT, &imm_expr, 0);
6380 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
6381 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6382 (int) BFD_RELOC_LO16);
6383 break;
6384
6385 case M_SLT_I:
6386 if (imm_expr.X_op == O_constant
6387 && imm_expr.X_add_number >= -0x8000
6388 && imm_expr.X_add_number < 0x8000)
6389 {
6390 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6391 dreg, sreg, (int) BFD_RELOC_LO16);
6392 return;
6393 }
6394 load_register (&icnt, AT, &imm_expr, 0);
6395 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
6396 break;
6397
6398 case M_SLTU_I:
6399 if (imm_expr.X_op == O_constant
6400 && imm_expr.X_add_number >= -0x8000
6401 && imm_expr.X_add_number < 0x8000)
6402 {
6403 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6404 dreg, sreg, (int) BFD_RELOC_LO16);
6405 return;
6406 }
6407 load_register (&icnt, AT, &imm_expr, 0);
6408 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg,
6409 AT);
6410 break;
6411
6412 case M_SNE:
6413 if (sreg == 0)
6414 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6415 treg);
6416 else if (treg == 0)
6417 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6418 sreg);
6419 else
6420 {
6421 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6422 sreg, treg);
6423 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6424 dreg);
6425 }
6426 return;
6427
6428 case M_SNE_I:
6429 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6430 {
6431 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6432 sreg);
6433 return;
6434 }
6435 if (sreg == 0)
6436 {
6437 as_warn (_("Instruction %s: result is always true"),
6438 ip->insn_mo->name);
6439 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 6440 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6441 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6442 return;
6443 }
6444 if (imm_expr.X_op == O_constant
6445 && imm_expr.X_add_number >= 0
6446 && imm_expr.X_add_number < 0x10000)
6447 {
6448 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6449 dreg, sreg, (int) BFD_RELOC_LO16);
6450 used_at = 0;
6451 }
6452 else if (imm_expr.X_op == O_constant
6453 && imm_expr.X_add_number > -0x8000
6454 && imm_expr.X_add_number < 0)
6455 {
6456 imm_expr.X_add_number = -imm_expr.X_add_number;
6457 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6458 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6459 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6460 used_at = 0;
6461 }
6462 else
6463 {
6464 load_register (&icnt, AT, &imm_expr, 0);
6465 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6466 sreg, AT);
6467 used_at = 1;
6468 }
6469 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
6470 if (used_at)
6471 break;
6472 return;
6473
6474 case M_DSUB_I:
6475 dbl = 1;
6476 case M_SUB_I:
6477 if (imm_expr.X_op == O_constant
6478 && imm_expr.X_add_number > -0x8000
6479 && imm_expr.X_add_number <= 0x8000)
6480 {
6481 imm_expr.X_add_number = -imm_expr.X_add_number;
6482 macro_build ((char *) NULL, &icnt, &imm_expr,
6483 dbl ? "daddi" : "addi",
6484 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6485 return;
6486 }
6487 load_register (&icnt, AT, &imm_expr, dbl);
6488 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 6489 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
6490 break;
6491
6492 case M_DSUBU_I:
6493 dbl = 1;
6494 case M_SUBU_I:
6495 if (imm_expr.X_op == O_constant
6496 && imm_expr.X_add_number > -0x8000
6497 && imm_expr.X_add_number <= 0x8000)
6498 {
6499 imm_expr.X_add_number = -imm_expr.X_add_number;
6500 macro_build ((char *) NULL, &icnt, &imm_expr,
6501 dbl ? "daddiu" : "addiu",
6502 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6503 return;
6504 }
6505 load_register (&icnt, AT, &imm_expr, dbl);
6506 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 6507 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
6508 break;
6509
6510 case M_TEQ_I:
6511 s = "teq";
6512 goto trap;
6513 case M_TGE_I:
6514 s = "tge";
6515 goto trap;
6516 case M_TGEU_I:
6517 s = "tgeu";
6518 goto trap;
6519 case M_TLT_I:
6520 s = "tlt";
6521 goto trap;
6522 case M_TLTU_I:
6523 s = "tltu";
6524 goto trap;
6525 case M_TNE_I:
6526 s = "tne";
6527 trap:
6528 load_register (&icnt, AT, &imm_expr, 0);
6529 macro_build ((char *) NULL, &icnt, NULL, s, "s,t", sreg, AT);
6530 break;
6531
252b5132 6532 case M_TRUNCWS:
43841e91 6533 case M_TRUNCWD:
e7af610e 6534 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6535 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
6536 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
6537
6538 /*
6539 * Is the double cfc1 instruction a bug in the mips assembler;
6540 * or is there a reason for it?
6541 */
6542 mips_emit_delays (true);
6543 ++mips_opts.noreorder;
6544 mips_any_noreorder = 1;
6545 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
6546 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
6547 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6548 expr1.X_add_number = 3;
6549 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
6550 (int) BFD_RELOC_LO16);
6551 expr1.X_add_number = 2;
6552 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
6553 (int) BFD_RELOC_LO16);
6554 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", AT, 31);
6555 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6556 macro_build ((char *) NULL, &icnt, NULL,
6557 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
6558 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", treg, 31);
6559 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6560 --mips_opts.noreorder;
6561 break;
6562
6563 case M_ULH:
6564 s = "lb";
6565 goto ulh;
6566 case M_ULHU:
6567 s = "lbu";
6568 ulh:
6569 if (offset_expr.X_add_number >= 0x7fff)
6570 as_bad (_("operand overflow"));
6571 /* avoid load delay */
6572 if (! target_big_endian)
6573 offset_expr.X_add_number += 1;
6574 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6575 (int) BFD_RELOC_LO16, breg);
6576 if (! target_big_endian)
6577 offset_expr.X_add_number -= 1;
6578 else
6579 offset_expr.X_add_number += 1;
6580 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
6581 (int) BFD_RELOC_LO16, breg);
6582 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
6583 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
6584 break;
6585
6586 case M_ULD:
6587 s = "ldl";
6588 s2 = "ldr";
6589 off = 7;
6590 goto ulw;
6591 case M_ULW:
6592 s = "lwl";
6593 s2 = "lwr";
6594 off = 3;
6595 ulw:
6596 if (offset_expr.X_add_number >= 0x8000 - off)
6597 as_bad (_("operand overflow"));
6598 if (! target_big_endian)
6599 offset_expr.X_add_number += off;
6600 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6601 (int) BFD_RELOC_LO16, breg);
6602 if (! target_big_endian)
6603 offset_expr.X_add_number -= off;
6604 else
6605 offset_expr.X_add_number += off;
6606 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
6607 (int) BFD_RELOC_LO16, breg);
6608 return;
6609
6610 case M_ULD_A:
6611 s = "ldl";
6612 s2 = "ldr";
6613 off = 7;
6614 goto ulwa;
6615 case M_ULW_A:
6616 s = "lwl";
6617 s2 = "lwr";
6618 off = 3;
6619 ulwa:
6620 load_address (&icnt, AT, &offset_expr);
6621 if (breg != 0)
6622 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6623 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6624 "d,v,t", AT, AT, breg);
6625 if (! target_big_endian)
6626 expr1.X_add_number = off;
6627 else
6628 expr1.X_add_number = 0;
6629 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
6630 (int) BFD_RELOC_LO16, AT);
6631 if (! target_big_endian)
6632 expr1.X_add_number = 0;
6633 else
6634 expr1.X_add_number = off;
6635 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
6636 (int) BFD_RELOC_LO16, AT);
6637 break;
6638
6639 case M_ULH_A:
6640 case M_ULHU_A:
6641 load_address (&icnt, AT, &offset_expr);
6642 if (breg != 0)
6643 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6644 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6645 "d,v,t", AT, AT, breg);
6646 if (target_big_endian)
6647 expr1.X_add_number = 0;
6648 macro_build ((char *) NULL, &icnt, &expr1,
6649 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
6650 (int) BFD_RELOC_LO16, AT);
6651 if (target_big_endian)
6652 expr1.X_add_number = 1;
6653 else
6654 expr1.X_add_number = 0;
6655 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
6656 (int) BFD_RELOC_LO16, AT);
6657 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
6658 treg, 8);
6659 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
6660 treg, AT);
6661 break;
6662
6663 case M_USH:
6664 if (offset_expr.X_add_number >= 0x7fff)
6665 as_bad (_("operand overflow"));
6666 if (target_big_endian)
6667 offset_expr.X_add_number += 1;
6668 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
6669 (int) BFD_RELOC_LO16, breg);
6670 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
6671 if (target_big_endian)
6672 offset_expr.X_add_number -= 1;
6673 else
6674 offset_expr.X_add_number += 1;
6675 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
6676 (int) BFD_RELOC_LO16, breg);
6677 break;
6678
6679 case M_USD:
6680 s = "sdl";
6681 s2 = "sdr";
6682 off = 7;
6683 goto usw;
6684 case M_USW:
6685 s = "swl";
6686 s2 = "swr";
6687 off = 3;
6688 usw:
6689 if (offset_expr.X_add_number >= 0x8000 - off)
6690 as_bad (_("operand overflow"));
6691 if (! target_big_endian)
6692 offset_expr.X_add_number += off;
6693 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6694 (int) BFD_RELOC_LO16, breg);
6695 if (! target_big_endian)
6696 offset_expr.X_add_number -= off;
6697 else
6698 offset_expr.X_add_number += off;
6699 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
6700 (int) BFD_RELOC_LO16, breg);
6701 return;
6702
6703 case M_USD_A:
6704 s = "sdl";
6705 s2 = "sdr";
6706 off = 7;
6707 goto uswa;
6708 case M_USW_A:
6709 s = "swl";
6710 s2 = "swr";
6711 off = 3;
6712 uswa:
6713 load_address (&icnt, AT, &offset_expr);
6714 if (breg != 0)
6715 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6716 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6717 "d,v,t", AT, AT, breg);
6718 if (! target_big_endian)
6719 expr1.X_add_number = off;
6720 else
6721 expr1.X_add_number = 0;
6722 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
6723 (int) BFD_RELOC_LO16, AT);
6724 if (! target_big_endian)
6725 expr1.X_add_number = 0;
6726 else
6727 expr1.X_add_number = off;
6728 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
6729 (int) BFD_RELOC_LO16, AT);
6730 break;
6731
6732 case M_USH_A:
6733 load_address (&icnt, AT, &offset_expr);
6734 if (breg != 0)
6735 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6736 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6737 "d,v,t", AT, AT, breg);
6738 if (! target_big_endian)
6739 expr1.X_add_number = 0;
6740 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
6741 (int) BFD_RELOC_LO16, AT);
6742 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", treg,
6743 treg, 8);
6744 if (! target_big_endian)
6745 expr1.X_add_number = 1;
6746 else
6747 expr1.X_add_number = 0;
6748 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
6749 (int) BFD_RELOC_LO16, AT);
6750 if (! target_big_endian)
6751 expr1.X_add_number = 0;
6752 else
6753 expr1.X_add_number = 1;
6754 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
6755 (int) BFD_RELOC_LO16, AT);
6756 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
6757 treg, 8);
6758 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
6759 treg, AT);
6760 break;
6761
6762 default:
6763 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 6764 are added dynamically. */
252b5132
RH
6765 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
6766 break;
6767 }
6768 if (mips_opts.noat)
6769 as_warn (_("Macro used $at after \".set noat\""));
6770}
6771
6772/* Implement macros in mips16 mode. */
6773
6774static void
6775mips16_macro (ip)
6776 struct mips_cl_insn *ip;
6777{
6778 int mask;
6779 int xreg, yreg, zreg, tmp;
6780 int icnt;
6781 expressionS expr1;
6782 int dbl;
6783 const char *s, *s2, *s3;
6784
6785 mask = ip->insn_mo->mask;
6786
6787 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
6788 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
6789 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
6790
6791 icnt = 0;
6792
6793 expr1.X_op = O_constant;
6794 expr1.X_op_symbol = NULL;
6795 expr1.X_add_symbol = NULL;
6796 expr1.X_add_number = 1;
6797
6798 dbl = 0;
6799
6800 switch (mask)
6801 {
6802 default:
6803 internalError ();
6804
6805 case M_DDIV_3:
6806 dbl = 1;
6807 case M_DIV_3:
6808 s = "mflo";
6809 goto do_div3;
6810 case M_DREM_3:
6811 dbl = 1;
6812 case M_REM_3:
6813 s = "mfhi";
6814 do_div3:
6815 mips_emit_delays (true);
6816 ++mips_opts.noreorder;
6817 mips_any_noreorder = 1;
6818 macro_build ((char *) NULL, &icnt, NULL,
6819 dbl ? "ddiv" : "div",
6820 "0,x,y", xreg, yreg);
6821 expr1.X_add_number = 2;
6822 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
beae10d5 6823 macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
bdaaa2e1 6824
252b5132
RH
6825 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
6826 since that causes an overflow. We should do that as well,
6827 but I don't see how to do the comparisons without a temporary
6828 register. */
6829 --mips_opts.noreorder;
6830 macro_build ((char *) NULL, &icnt, NULL, s, "x", zreg);
6831 break;
6832
6833 case M_DIVU_3:
6834 s = "divu";
6835 s2 = "mflo";
6836 goto do_divu3;
6837 case M_REMU_3:
6838 s = "divu";
6839 s2 = "mfhi";
6840 goto do_divu3;
6841 case M_DDIVU_3:
6842 s = "ddivu";
6843 s2 = "mflo";
6844 goto do_divu3;
6845 case M_DREMU_3:
6846 s = "ddivu";
6847 s2 = "mfhi";
6848 do_divu3:
6849 mips_emit_delays (true);
6850 ++mips_opts.noreorder;
6851 mips_any_noreorder = 1;
6852 macro_build ((char *) NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
6853 expr1.X_add_number = 2;
6854 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
6855 macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
6856 --mips_opts.noreorder;
6857 macro_build ((char *) NULL, &icnt, NULL, s2, "x", zreg);
6858 break;
6859
6860 case M_DMUL:
6861 dbl = 1;
6862 case M_MUL:
6863 macro_build ((char *) NULL, &icnt, NULL,
9a41af64 6864 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
252b5132
RH
6865 macro_build ((char *) NULL, &icnt, NULL, "mflo", "x", zreg);
6866 return;
6867
6868 case M_DSUBU_I:
6869 dbl = 1;
6870 goto do_subu;
6871 case M_SUBU_I:
6872 do_subu:
6873 if (imm_expr.X_op != O_constant)
6874 as_bad (_("Unsupported large constant"));
6875 imm_expr.X_add_number = -imm_expr.X_add_number;
6876 macro_build ((char *) NULL, &icnt, &imm_expr,
9a41af64 6877 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
6878 break;
6879
6880 case M_SUBU_I_2:
6881 if (imm_expr.X_op != O_constant)
6882 as_bad (_("Unsupported large constant"));
6883 imm_expr.X_add_number = -imm_expr.X_add_number;
6884 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
6885 "x,k", xreg);
6886 break;
6887
6888 case M_DSUBU_I_2:
6889 if (imm_expr.X_op != O_constant)
6890 as_bad (_("Unsupported large constant"));
6891 imm_expr.X_add_number = -imm_expr.X_add_number;
6892 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
6893 "y,j", yreg);
6894 break;
6895
6896 case M_BEQ:
6897 s = "cmp";
6898 s2 = "bteqz";
6899 goto do_branch;
6900 case M_BNE:
6901 s = "cmp";
6902 s2 = "btnez";
6903 goto do_branch;
6904 case M_BLT:
6905 s = "slt";
6906 s2 = "btnez";
6907 goto do_branch;
6908 case M_BLTU:
6909 s = "sltu";
6910 s2 = "btnez";
6911 goto do_branch;
6912 case M_BLE:
6913 s = "slt";
6914 s2 = "bteqz";
6915 goto do_reverse_branch;
6916 case M_BLEU:
6917 s = "sltu";
6918 s2 = "bteqz";
6919 goto do_reverse_branch;
6920 case M_BGE:
6921 s = "slt";
6922 s2 = "bteqz";
6923 goto do_branch;
6924 case M_BGEU:
6925 s = "sltu";
6926 s2 = "bteqz";
6927 goto do_branch;
6928 case M_BGT:
6929 s = "slt";
6930 s2 = "btnez";
6931 goto do_reverse_branch;
6932 case M_BGTU:
6933 s = "sltu";
6934 s2 = "btnez";
6935
6936 do_reverse_branch:
6937 tmp = xreg;
6938 xreg = yreg;
6939 yreg = tmp;
6940
6941 do_branch:
6942 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
6943 xreg, yreg);
6944 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
6945 break;
6946
6947 case M_BEQ_I:
6948 s = "cmpi";
6949 s2 = "bteqz";
6950 s3 = "x,U";
6951 goto do_branch_i;
6952 case M_BNE_I:
6953 s = "cmpi";
6954 s2 = "btnez";
6955 s3 = "x,U";
6956 goto do_branch_i;
6957 case M_BLT_I:
6958 s = "slti";
6959 s2 = "btnez";
6960 s3 = "x,8";
6961 goto do_branch_i;
6962 case M_BLTU_I:
6963 s = "sltiu";
6964 s2 = "btnez";
6965 s3 = "x,8";
6966 goto do_branch_i;
6967 case M_BLE_I:
6968 s = "slti";
6969 s2 = "btnez";
6970 s3 = "x,8";
6971 goto do_addone_branch_i;
6972 case M_BLEU_I:
6973 s = "sltiu";
6974 s2 = "btnez";
6975 s3 = "x,8";
6976 goto do_addone_branch_i;
6977 case M_BGE_I:
6978 s = "slti";
6979 s2 = "bteqz";
6980 s3 = "x,8";
6981 goto do_branch_i;
6982 case M_BGEU_I:
6983 s = "sltiu";
6984 s2 = "bteqz";
6985 s3 = "x,8";
6986 goto do_branch_i;
6987 case M_BGT_I:
6988 s = "slti";
6989 s2 = "bteqz";
6990 s3 = "x,8";
6991 goto do_addone_branch_i;
6992 case M_BGTU_I:
6993 s = "sltiu";
6994 s2 = "bteqz";
6995 s3 = "x,8";
6996
6997 do_addone_branch_i:
6998 if (imm_expr.X_op != O_constant)
6999 as_bad (_("Unsupported large constant"));
7000 ++imm_expr.X_add_number;
7001
7002 do_branch_i:
7003 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7004 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7005 break;
7006
7007 case M_ABS:
7008 expr1.X_add_number = 0;
7009 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7010 if (xreg != yreg)
ea1fb5dc 7011 move_register (&icnt, xreg, yreg);
252b5132
RH
7012 expr1.X_add_number = 2;
7013 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7014 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7015 "neg", "x,w", xreg, xreg);
7016 }
7017}
7018
7019/* For consistency checking, verify that all bits are specified either
7020 by the match/mask part of the instruction definition, or by the
7021 operand list. */
7022static int
7023validate_mips_insn (opc)
7024 const struct mips_opcode *opc;
7025{
7026 const char *p = opc->args;
7027 char c;
7028 unsigned long used_bits = opc->mask;
7029
7030 if ((used_bits & opc->match) != opc->match)
7031 {
7032 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7033 opc->name, opc->args);
7034 return 0;
7035 }
7036#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7037 while (*p)
7038 switch (c = *p++)
7039 {
7040 case ',': break;
7041 case '(': break;
7042 case ')': break;
7043 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7044 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7045 case 'A': break;
4372b673 7046 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7047 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7048 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7049 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7050 case 'F': break;
7051 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7052 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7053 case 'I': break;
e972090a 7054 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
252b5132
RH
7055 case 'L': break;
7056 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7057 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7058 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7059 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7060 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7061 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7062 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7063 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7064 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7065 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7066 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7067 case 'f': break;
7068 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7069 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7070 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7071 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7072 case 'l': break;
7073 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7074 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7075 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7076 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7077 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7078 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7079 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7080 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7081 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7082 case 'x': break;
7083 case 'z': break;
7084 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
7085 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7086 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
252b5132
RH
7087 default:
7088 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7089 c, opc->name, opc->args);
7090 return 0;
7091 }
7092#undef USE_BITS
7093 if (used_bits != 0xffffffff)
7094 {
7095 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7096 ~used_bits & 0xffffffff, opc->name, opc->args);
7097 return 0;
7098 }
7099 return 1;
7100}
7101
7102/* This routine assembles an instruction into its binary format. As a
7103 side effect, it sets one of the global variables imm_reloc or
7104 offset_reloc to the type of relocation to do if one of the operands
7105 is an address expression. */
7106
7107static void
7108mips_ip (str, ip)
7109 char *str;
7110 struct mips_cl_insn *ip;
7111{
7112 char *s;
7113 const char *args;
43841e91 7114 char c = 0;
252b5132
RH
7115 struct mips_opcode *insn;
7116 char *argsStart;
7117 unsigned int regno;
7118 unsigned int lastregno = 0;
7119 char *s_reset;
7120 char save_c = 0;
7121 int full_opcode_match = 1;
7122
7123 insn_error = NULL;
7124
7125 /* If the instruction contains a '.', we first try to match an instruction
7126 including the '.'. Then we try again without the '.'. */
7127 insn = NULL;
3882b010 7128 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
7129 continue;
7130
7131 /* If we stopped on whitespace, then replace the whitespace with null for
7132 the call to hash_find. Save the character we replaced just in case we
7133 have to re-parse the instruction. */
3882b010 7134 if (ISSPACE (*s))
252b5132
RH
7135 {
7136 save_c = *s;
7137 *s++ = '\0';
7138 }
bdaaa2e1 7139
252b5132
RH
7140 insn = (struct mips_opcode *) hash_find (op_hash, str);
7141
7142 /* If we didn't find the instruction in the opcode table, try again, but
7143 this time with just the instruction up to, but not including the
7144 first '.'. */
7145 if (insn == NULL)
7146 {
bdaaa2e1 7147 /* Restore the character we overwrite above (if any). */
252b5132
RH
7148 if (save_c)
7149 *(--s) = save_c;
7150
7151 /* Scan up to the first '.' or whitespace. */
3882b010
L
7152 for (s = str;
7153 *s != '\0' && *s != '.' && !ISSPACE (*s);
7154 ++s)
252b5132
RH
7155 continue;
7156
7157 /* If we did not find a '.', then we can quit now. */
7158 if (*s != '.')
7159 {
7160 insn_error = "unrecognized opcode";
7161 return;
7162 }
7163
7164 /* Lookup the instruction in the hash table. */
7165 *s++ = '\0';
7166 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7167 {
7168 insn_error = "unrecognized opcode";
7169 return;
7170 }
7171
7172 full_opcode_match = 0;
7173 }
7174
7175 argsStart = s;
7176 for (;;)
7177 {
252b5132
RH
7178 boolean ok;
7179
7180 assert (strcmp (insn->name, str) == 0);
7181
ea1fb5dc 7182 if (OPCODE_IS_MEMBER (insn, mips_opts.isa, mips_arch))
252b5132 7183 ok = true;
bdaaa2e1 7184 else
252b5132 7185 ok = false;
bdaaa2e1 7186
252b5132
RH
7187 if (insn->pinfo != INSN_MACRO)
7188 {
ec68c924 7189 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
252b5132
RH
7190 ok = false;
7191 }
7192
7193 if (! ok)
7194 {
7195 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7196 && strcmp (insn->name, insn[1].name) == 0)
7197 {
7198 ++insn;
7199 continue;
7200 }
252b5132 7201 else
beae10d5 7202 {
268f6bed
L
7203 if (!insn_error)
7204 {
7205 static char buf[100];
7206 sprintf (buf,
7207 _("opcode not supported on this processor: %s (%s)"),
ec68c924 7208 mips_cpu_to_str (mips_arch),
268f6bed 7209 mips_isa_to_str (mips_opts.isa));
bdaaa2e1 7210
268f6bed
L
7211 insn_error = buf;
7212 }
7213 if (save_c)
7214 *(--s) = save_c;
2bd7f1f3 7215 return;
252b5132 7216 }
252b5132
RH
7217 }
7218
7219 ip->insn_mo = insn;
7220 ip->insn_opcode = insn->match;
268f6bed 7221 insn_error = NULL;
252b5132
RH
7222 for (args = insn->args;; ++args)
7223 {
7224 if (*s == ' ')
7225 ++s;
7226 switch (*args)
7227 {
7228 case '\0': /* end of args */
7229 if (*s == '\0')
7230 return;
7231 break;
7232
7233 case ',':
7234 if (*s++ == *args)
7235 continue;
7236 s--;
7237 switch (*++args)
7238 {
7239 case 'r':
7240 case 'v':
38487616 7241 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
7242 continue;
7243
7244 case 'w':
38487616
TS
7245 ip->insn_opcode |= lastregno << OP_SH_RT;
7246 continue;
7247
252b5132 7248 case 'W':
38487616 7249 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
7250 continue;
7251
7252 case 'V':
38487616 7253 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
7254 continue;
7255 }
7256 break;
7257
7258 case '(':
7259 /* Handle optional base register.
7260 Either the base register is omitted or
bdaaa2e1 7261 we must have a left paren. */
252b5132
RH
7262 /* This is dependent on the next operand specifier
7263 is a base register specification. */
7264 assert (args[1] == 'b' || args[1] == '5'
7265 || args[1] == '-' || args[1] == '4');
7266 if (*s == '\0')
7267 return;
7268
7269 case ')': /* these must match exactly */
7270 if (*s++ == *args)
7271 continue;
7272 break;
7273
7274 case '<': /* must be at least one digit */
7275 /*
7276 * According to the manual, if the shift amount is greater
7277 * than 31 or less than 0 the the shift amount should be
7278 * mod 32. In reality the mips assembler issues an error.
7279 * We issue a warning and mask out all but the low 5 bits.
7280 */
7281 my_getExpression (&imm_expr, s);
7282 check_absolute_expr (ip, &imm_expr);
7283 if ((unsigned long) imm_expr.X_add_number > 31)
7284 {
7285 as_warn (_("Improper shift amount (%ld)"),
7286 (long) imm_expr.X_add_number);
38487616 7287 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 7288 }
38487616 7289 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
7290 imm_expr.X_op = O_absent;
7291 s = expr_end;
7292 continue;
7293
7294 case '>': /* shift amount minus 32 */
7295 my_getExpression (&imm_expr, s);
7296 check_absolute_expr (ip, &imm_expr);
7297 if ((unsigned long) imm_expr.X_add_number < 32
7298 || (unsigned long) imm_expr.X_add_number > 63)
7299 break;
38487616 7300 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
7301 imm_expr.X_op = O_absent;
7302 s = expr_end;
7303 continue;
7304
252b5132
RH
7305 case 'k': /* cache code */
7306 case 'h': /* prefx code */
7307 my_getExpression (&imm_expr, s);
7308 check_absolute_expr (ip, &imm_expr);
7309 if ((unsigned long) imm_expr.X_add_number > 31)
7310 {
7311 as_warn (_("Invalid value for `%s' (%lu)"),
7312 ip->insn_mo->name,
7313 (unsigned long) imm_expr.X_add_number);
7314 imm_expr.X_add_number &= 0x1f;
7315 }
7316 if (*args == 'k')
7317 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7318 else
7319 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7320 imm_expr.X_op = O_absent;
7321 s = expr_end;
7322 continue;
7323
7324 case 'c': /* break code */
7325 my_getExpression (&imm_expr, s);
7326 check_absolute_expr (ip, &imm_expr);
7327 if ((unsigned) imm_expr.X_add_number > 1023)
7328 {
7329 as_warn (_("Illegal break code (%ld)"),
7330 (long) imm_expr.X_add_number);
38487616 7331 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 7332 }
38487616 7333 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
7334 imm_expr.X_op = O_absent;
7335 s = expr_end;
7336 continue;
7337
7338 case 'q': /* lower break code */
7339 my_getExpression (&imm_expr, s);
7340 check_absolute_expr (ip, &imm_expr);
7341 if ((unsigned) imm_expr.X_add_number > 1023)
7342 {
7343 as_warn (_("Illegal lower break code (%ld)"),
7344 (long) imm_expr.X_add_number);
38487616 7345 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 7346 }
38487616 7347 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
7348 imm_expr.X_op = O_absent;
7349 s = expr_end;
7350 continue;
7351
4372b673 7352 case 'B': /* 20-bit syscall/break code. */
156c2f8b 7353 my_getExpression (&imm_expr, s);
156c2f8b 7354 check_absolute_expr (ip, &imm_expr);
38487616 7355 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE20)
4372b673 7356 as_warn (_("Illegal 20-bit code (%ld)"),
252b5132 7357 (long) imm_expr.X_add_number);
38487616 7358 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
7359 imm_expr.X_op = O_absent;
7360 s = expr_end;
7361 continue;
7362
7363 case 'C': /* Coprocessor code */
beae10d5 7364 my_getExpression (&imm_expr, s);
252b5132 7365 check_absolute_expr (ip, &imm_expr);
beae10d5 7366 if ((unsigned long) imm_expr.X_add_number >= (1<<25))
252b5132 7367 {
beae10d5 7368 as_warn (_("Coproccesor code > 25 bits (%ld)"),
252b5132 7369 (long) imm_expr.X_add_number);
beae10d5 7370 imm_expr.X_add_number &= ((1<<25) - 1);
252b5132 7371 }
beae10d5
KH
7372 ip->insn_opcode |= imm_expr.X_add_number;
7373 imm_expr.X_op = O_absent;
7374 s = expr_end;
7375 continue;
252b5132 7376
4372b673
NC
7377 case 'J': /* 19-bit wait code. */
7378 my_getExpression (&imm_expr, s);
7379 check_absolute_expr (ip, &imm_expr);
38487616 7380 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE19)
4372b673
NC
7381 as_warn (_("Illegal 19-bit code (%ld)"),
7382 (long) imm_expr.X_add_number);
38487616 7383 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
7384 imm_expr.X_op = O_absent;
7385 s = expr_end;
7386 continue;
7387
252b5132 7388 case 'P': /* Performance register */
beae10d5 7389 my_getExpression (&imm_expr, s);
252b5132 7390 check_absolute_expr (ip, &imm_expr);
beae10d5 7391 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 7392 {
38487616 7393 as_warn (_("Invalid performance register (%ld)"),
252b5132 7394 (long) imm_expr.X_add_number);
38487616 7395 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 7396 }
38487616 7397 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
7398 imm_expr.X_op = O_absent;
7399 s = expr_end;
7400 continue;
252b5132
RH
7401
7402 case 'b': /* base register */
7403 case 'd': /* destination register */
7404 case 's': /* source register */
7405 case 't': /* target register */
7406 case 'r': /* both target and source */
7407 case 'v': /* both dest and source */
7408 case 'w': /* both dest and target */
7409 case 'E': /* coprocessor target register */
7410 case 'G': /* coprocessor destination register */
7411 case 'x': /* ignore register name */
7412 case 'z': /* must be zero register */
4372b673 7413 case 'U': /* destination register (clo/clz). */
252b5132
RH
7414 s_reset = s;
7415 if (s[0] == '$')
7416 {
7417
3882b010 7418 if (ISDIGIT (s[1]))
252b5132
RH
7419 {
7420 ++s;
7421 regno = 0;
7422 do
7423 {
7424 regno *= 10;
7425 regno += *s - '0';
7426 ++s;
7427 }
3882b010 7428 while (ISDIGIT (*s));
252b5132
RH
7429 if (regno > 31)
7430 as_bad (_("Invalid register number (%d)"), regno);
7431 }
7432 else if (*args == 'E' || *args == 'G')
7433 goto notreg;
7434 else
7435 {
7436 if (s[1] == 'f' && s[2] == 'p')
7437 {
7438 s += 3;
7439 regno = FP;
7440 }
7441 else if (s[1] == 's' && s[2] == 'p')
7442 {
7443 s += 3;
7444 regno = SP;
7445 }
7446 else if (s[1] == 'g' && s[2] == 'p')
7447 {
7448 s += 3;
7449 regno = GP;
7450 }
7451 else if (s[1] == 'a' && s[2] == 't')
7452 {
7453 s += 3;
7454 regno = AT;
7455 }
7456 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7457 {
7458 s += 4;
7459 regno = KT0;
7460 }
7461 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7462 {
7463 s += 4;
7464 regno = KT1;
7465 }
7466 else if (itbl_have_entries)
7467 {
7468 char *p, *n;
d7ba4a77 7469 unsigned long r;
252b5132 7470
d7ba4a77 7471 p = s + 1; /* advance past '$' */
252b5132
RH
7472 n = itbl_get_field (&p); /* n is name */
7473
d7ba4a77
ILT
7474 /* See if this is a register defined in an
7475 itbl entry. */
7476 if (itbl_get_reg_val (n, &r))
252b5132
RH
7477 {
7478 /* Get_field advances to the start of
7479 the next field, so we need to back
d7ba4a77 7480 rack to the end of the last field. */
bdaaa2e1 7481 if (p)
252b5132 7482 s = p - 1;
bdaaa2e1 7483 else
d7ba4a77 7484 s = strchr (s, '\0');
252b5132
RH
7485 regno = r;
7486 }
7487 else
7488 goto notreg;
beae10d5 7489 }
252b5132
RH
7490 else
7491 goto notreg;
7492 }
7493 if (regno == AT
7494 && ! mips_opts.noat
7495 && *args != 'E'
7496 && *args != 'G')
7497 as_warn (_("Used $at without \".set noat\""));
7498 c = *args;
7499 if (*s == ' ')
7500 s++;
7501 if (args[1] != *s)
7502 {
7503 if (c == 'r' || c == 'v' || c == 'w')
7504 {
7505 regno = lastregno;
7506 s = s_reset;
7507 args++;
7508 }
7509 }
7510 /* 'z' only matches $0. */
7511 if (c == 'z' && regno != 0)
7512 break;
7513
bdaaa2e1
KH
7514 /* Now that we have assembled one operand, we use the args string
7515 * to figure out where it goes in the instruction. */
252b5132
RH
7516 switch (c)
7517 {
7518 case 'r':
7519 case 's':
7520 case 'v':
7521 case 'b':
38487616 7522 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
7523 break;
7524 case 'd':
7525 case 'G':
38487616 7526 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 7527 break;
4372b673 7528 case 'U':
38487616
TS
7529 ip->insn_opcode |= regno << OP_SH_RD;
7530 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 7531 break;
252b5132
RH
7532 case 'w':
7533 case 't':
7534 case 'E':
38487616 7535 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
7536 break;
7537 case 'x':
7538 /* This case exists because on the r3000 trunc
7539 expands into a macro which requires a gp
7540 register. On the r6000 or r4000 it is
7541 assembled into a single instruction which
7542 ignores the register. Thus the insn version
7543 is MIPS_ISA2 and uses 'x', and the macro
7544 version is MIPS_ISA1 and uses 't'. */
7545 break;
7546 case 'z':
7547 /* This case is for the div instruction, which
7548 acts differently if the destination argument
7549 is $0. This only matches $0, and is checked
7550 outside the switch. */
7551 break;
7552 case 'D':
7553 /* Itbl operand; not yet implemented. FIXME ?? */
7554 break;
7555 /* What about all other operands like 'i', which
7556 can be specified in the opcode table? */
7557 }
7558 lastregno = regno;
7559 continue;
7560 }
7561 notreg:
7562 switch (*args++)
7563 {
7564 case 'r':
7565 case 'v':
38487616 7566 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
7567 continue;
7568 case 'w':
38487616 7569 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
7570 continue;
7571 }
7572 break;
7573
7574 case 'D': /* floating point destination register */
7575 case 'S': /* floating point source register */
7576 case 'T': /* floating point target register */
7577 case 'R': /* floating point source register */
7578 case 'V':
7579 case 'W':
7580 s_reset = s;
3882b010
L
7581 if (s[0] == '$' && s[1] == 'f'
7582 && ISDIGIT (s[2]))
252b5132
RH
7583 {
7584 s += 2;
7585 regno = 0;
7586 do
7587 {
7588 regno *= 10;
7589 regno += *s - '0';
7590 ++s;
7591 }
3882b010 7592 while (ISDIGIT (*s));
252b5132
RH
7593
7594 if (regno > 31)
7595 as_bad (_("Invalid float register number (%d)"), regno);
7596
7597 if ((regno & 1) != 0
ca4e0257 7598 && HAVE_32BIT_FPRS
252b5132
RH
7599 && ! (strcmp (str, "mtc1") == 0
7600 || strcmp (str, "mfc1") == 0
7601 || strcmp (str, "lwc1") == 0
7602 || strcmp (str, "swc1") == 0
7603 || strcmp (str, "l.s") == 0
7604 || strcmp (str, "s.s") == 0))
7605 as_warn (_("Float register should be even, was %d"),
7606 regno);
7607
7608 c = *args;
7609 if (*s == ' ')
7610 s++;
7611 if (args[1] != *s)
7612 {
7613 if (c == 'V' || c == 'W')
7614 {
7615 regno = lastregno;
7616 s = s_reset;
7617 args++;
7618 }
7619 }
7620 switch (c)
7621 {
7622 case 'D':
38487616 7623 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
7624 break;
7625 case 'V':
7626 case 'S':
38487616 7627 ip->insn_opcode |= regno << OP_SH_FS;
252b5132
RH
7628 break;
7629 case 'W':
7630 case 'T':
38487616 7631 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
7632 break;
7633 case 'R':
38487616 7634 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
7635 break;
7636 }
7637 lastregno = regno;
7638 continue;
7639 }
7640
252b5132
RH
7641 switch (*args++)
7642 {
7643 case 'V':
38487616 7644 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
7645 continue;
7646 case 'W':
38487616 7647 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
7648 continue;
7649 }
7650 break;
7651
7652 case 'I':
7653 my_getExpression (&imm_expr, s);
7654 if (imm_expr.X_op != O_big
7655 && imm_expr.X_op != O_constant)
7656 insn_error = _("absolute expression required");
7657 s = expr_end;
7658 continue;
7659
7660 case 'A':
7661 my_getExpression (&offset_expr, s);
f6688943 7662 *imm_reloc = BFD_RELOC_32;
252b5132
RH
7663 s = expr_end;
7664 continue;
7665
7666 case 'F':
7667 case 'L':
7668 case 'f':
7669 case 'l':
7670 {
7671 int f64;
ca4e0257 7672 int using_gprs;
252b5132
RH
7673 char *save_in;
7674 char *err;
7675 unsigned char temp[8];
7676 int len;
7677 unsigned int length;
7678 segT seg;
7679 subsegT subseg;
7680 char *p;
7681
7682 /* These only appear as the last operand in an
7683 instruction, and every instruction that accepts
7684 them in any variant accepts them in all variants.
7685 This means we don't have to worry about backing out
7686 any changes if the instruction does not match.
7687
7688 The difference between them is the size of the
7689 floating point constant and where it goes. For 'F'
7690 and 'L' the constant is 64 bits; for 'f' and 'l' it
7691 is 32 bits. Where the constant is placed is based
7692 on how the MIPS assembler does things:
7693 F -- .rdata
7694 L -- .lit8
7695 f -- immediate value
7696 l -- .lit4
7697
7698 The .lit4 and .lit8 sections are only used if
7699 permitted by the -G argument.
7700
7701 When generating embedded PIC code, we use the
7702 .lit8 section but not the .lit4 section (we can do
7703 .lit4 inline easily; we need to put .lit8
7704 somewhere in the data segment, and using .lit8
7705 permits the linker to eventually combine identical
ca4e0257
RS
7706 .lit8 entries).
7707
7708 The code below needs to know whether the target register
7709 is 32 or 64 bits wide. It relies on the fact 'f' and
7710 'F' are used with GPR-based instructions and 'l' and
7711 'L' are used with FPR-based instructions. */
252b5132
RH
7712
7713 f64 = *args == 'F' || *args == 'L';
ca4e0257 7714 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
7715
7716 save_in = input_line_pointer;
7717 input_line_pointer = s;
7718 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
7719 length = len;
7720 s = input_line_pointer;
7721 input_line_pointer = save_in;
7722 if (err != NULL && *err != '\0')
7723 {
7724 as_bad (_("Bad floating point constant: %s"), err);
7725 memset (temp, '\0', sizeof temp);
7726 length = f64 ? 8 : 4;
7727 }
7728
156c2f8b 7729 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
7730
7731 if (*args == 'f'
7732 || (*args == 'l'
7733 && (! USE_GLOBAL_POINTER_OPT
7734 || mips_pic == EMBEDDED_PIC
7735 || g_switch_value < 4
7736 || (temp[0] == 0 && temp[1] == 0)
7737 || (temp[2] == 0 && temp[3] == 0))))
7738 {
7739 imm_expr.X_op = O_constant;
7740 if (! target_big_endian)
7741 imm_expr.X_add_number = bfd_getl32 (temp);
7742 else
7743 imm_expr.X_add_number = bfd_getb32 (temp);
7744 }
7745 else if (length > 4
119d663a 7746 && ! mips_disable_float_construction
ca4e0257
RS
7747 /* Constants can only be constructed in GPRs and
7748 copied to FPRs if the GPRs are at least as wide
7749 as the FPRs. Force the constant into memory if
7750 we are using 64-bit FPRs but the GPRs are only
7751 32 bits wide. */
7752 && (using_gprs
7753 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
7754 && ((temp[0] == 0 && temp[1] == 0)
7755 || (temp[2] == 0 && temp[3] == 0))
7756 && ((temp[4] == 0 && temp[5] == 0)
7757 || (temp[6] == 0 && temp[7] == 0)))
7758 {
ca4e0257
RS
7759 /* The value is simple enough to load with a couple of
7760 instructions. If using 32-bit registers, set
7761 imm_expr to the high order 32 bits and offset_expr to
7762 the low order 32 bits. Otherwise, set imm_expr to
7763 the entire 64 bit constant. */
7764 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
7765 {
7766 imm_expr.X_op = O_constant;
7767 offset_expr.X_op = O_constant;
7768 if (! target_big_endian)
7769 {
7770 imm_expr.X_add_number = bfd_getl32 (temp + 4);
7771 offset_expr.X_add_number = bfd_getl32 (temp);
7772 }
7773 else
7774 {
7775 imm_expr.X_add_number = bfd_getb32 (temp);
7776 offset_expr.X_add_number = bfd_getb32 (temp + 4);
7777 }
7778 if (offset_expr.X_add_number == 0)
7779 offset_expr.X_op = O_absent;
7780 }
7781 else if (sizeof (imm_expr.X_add_number) > 4)
7782 {
7783 imm_expr.X_op = O_constant;
7784 if (! target_big_endian)
7785 imm_expr.X_add_number = bfd_getl64 (temp);
7786 else
7787 imm_expr.X_add_number = bfd_getb64 (temp);
7788 }
7789 else
7790 {
7791 imm_expr.X_op = O_big;
7792 imm_expr.X_add_number = 4;
7793 if (! target_big_endian)
7794 {
7795 generic_bignum[0] = bfd_getl16 (temp);
7796 generic_bignum[1] = bfd_getl16 (temp + 2);
7797 generic_bignum[2] = bfd_getl16 (temp + 4);
7798 generic_bignum[3] = bfd_getl16 (temp + 6);
7799 }
7800 else
7801 {
7802 generic_bignum[0] = bfd_getb16 (temp + 6);
7803 generic_bignum[1] = bfd_getb16 (temp + 4);
7804 generic_bignum[2] = bfd_getb16 (temp + 2);
7805 generic_bignum[3] = bfd_getb16 (temp);
7806 }
7807 }
7808 }
7809 else
7810 {
7811 const char *newname;
7812 segT new_seg;
7813
7814 /* Switch to the right section. */
7815 seg = now_seg;
7816 subseg = now_subseg;
7817 switch (*args)
7818 {
7819 default: /* unused default case avoids warnings. */
7820 case 'L':
7821 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
7822 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
7823 || mips_pic == EMBEDDED_PIC)
252b5132
RH
7824 newname = ".lit8";
7825 break;
7826 case 'F':
bb2d6cd7
GK
7827 if (mips_pic == EMBEDDED_PIC)
7828 newname = ".lit8";
7829 else
7830 newname = RDATA_SECTION_NAME;
252b5132
RH
7831 break;
7832 case 'l':
7833 assert (!USE_GLOBAL_POINTER_OPT
7834 || g_switch_value >= 4);
7835 newname = ".lit4";
7836 break;
7837 }
7838 new_seg = subseg_new (newname, (subsegT) 0);
7839 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
7840 bfd_set_section_flags (stdoutput, new_seg,
7841 (SEC_ALLOC
7842 | SEC_LOAD
7843 | SEC_READONLY
7844 | SEC_DATA));
7845 frag_align (*args == 'l' ? 2 : 3, 0, 0);
7846 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
7847 && strcmp (TARGET_OS, "elf") != 0)
7848 record_alignment (new_seg, 4);
7849 else
7850 record_alignment (new_seg, *args == 'l' ? 2 : 3);
7851 if (seg == now_seg)
7852 as_bad (_("Can't use floating point insn in this section"));
7853
7854 /* Set the argument to the current address in the
7855 section. */
7856 offset_expr.X_op = O_symbol;
7857 offset_expr.X_add_symbol =
7858 symbol_new ("L0\001", now_seg,
7859 (valueT) frag_now_fix (), frag_now);
7860 offset_expr.X_add_number = 0;
7861
7862 /* Put the floating point number into the section. */
7863 p = frag_more ((int) length);
7864 memcpy (p, temp, length);
7865
7866 /* Switch back to the original section. */
7867 subseg_set (seg, subseg);
7868 }
7869 }
7870 continue;
7871
7872 case 'i': /* 16 bit unsigned immediate */
7873 case 'j': /* 16 bit signed immediate */
f6688943 7874 *imm_reloc = BFD_RELOC_LO16;
252b5132 7875 c = my_getSmallExpression (&imm_expr, s);
fb1b3232 7876 if (c != S_EX_NONE)
252b5132 7877 {
fb1b3232 7878 if (c != S_EX_LO)
252b5132
RH
7879 {
7880 if (imm_expr.X_op == O_constant)
7881 imm_expr.X_add_number =
7882 (imm_expr.X_add_number >> 16) & 0xffff;
fb1b3232 7883 else if (c == S_EX_HIGHEST)
f6688943 7884 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
fb1b3232 7885 else if (c == S_EX_HIGHER)
f6688943 7886 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
fb1b3232 7887 else if (c == S_EX_HI)
252b5132 7888 {
f6688943 7889 *imm_reloc = BFD_RELOC_HI16_S;
252b5132
RH
7890 imm_unmatched_hi = true;
7891 }
7892 else
f6688943 7893 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
7894 }
7895 else if (imm_expr.X_op == O_constant)
7896 imm_expr.X_add_number &= 0xffff;
7897 }
7898 if (*args == 'i')
7899 {
fb1b3232 7900 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 7901 || ((imm_expr.X_add_number < 0
beae10d5
KH
7902 || imm_expr.X_add_number >= 0x10000)
7903 && imm_expr.X_op == O_constant))
252b5132
RH
7904 {
7905 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7906 !strcmp (insn->name, insn[1].name))
7907 break;
2ae7e77b
AH
7908 if (imm_expr.X_op == O_constant
7909 || imm_expr.X_op == O_big)
252b5132
RH
7910 as_bad (_("16 bit expression not in range 0..65535"));
7911 }
7912 }
7913 else
7914 {
7915 int more;
7916 offsetT max;
7917
7918 /* The upper bound should be 0x8000, but
7919 unfortunately the MIPS assembler accepts numbers
7920 from 0x8000 to 0xffff and sign extends them, and
7921 we want to be compatible. We only permit this
7922 extended range for an instruction which does not
7923 provide any further alternates, since those
7924 alternates may handle other cases. People should
7925 use the numbers they mean, rather than relying on
7926 a mysterious sign extension. */
7927 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7928 strcmp (insn->name, insn[1].name) == 0);
7929 if (more)
7930 max = 0x8000;
7931 else
7932 max = 0x10000;
fb1b3232 7933 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 7934 || ((imm_expr.X_add_number < -0x8000
beae10d5
KH
7935 || imm_expr.X_add_number >= max)
7936 && imm_expr.X_op == O_constant)
252b5132
RH
7937 || (more
7938 && imm_expr.X_add_number < 0
ca4e0257 7939 && HAVE_64BIT_GPRS
252b5132
RH
7940 && imm_expr.X_unsigned
7941 && sizeof (imm_expr.X_add_number) <= 4))
7942 {
7943 if (more)
7944 break;
2ae7e77b
AH
7945 if (imm_expr.X_op == O_constant
7946 || imm_expr.X_op == O_big)
252b5132
RH
7947 as_bad (_("16 bit expression not in range -32768..32767"));
7948 }
7949 }
7950 s = expr_end;
7951 continue;
7952
7953 case 'o': /* 16 bit offset */
7954 c = my_getSmallExpression (&offset_expr, s);
7955
7956 /* If this value won't fit into a 16 bit offset, then go
7957 find a macro that will generate the 32 bit offset
7958 code pattern. As a special hack, we accept the
7959 difference of two local symbols as a constant. This
7960 is required to suppose embedded PIC switches, which
7961 use an instruction which looks like
7962 lw $4,$L12-$LS12($4)
7963 The problem with handling this in a more general
7964 fashion is that the macro function doesn't expect to
7965 see anything which can be handled in a single
7966 constant instruction. */
fb1b3232 7967 if (c == S_EX_NONE
252b5132
RH
7968 && (offset_expr.X_op != O_constant
7969 || offset_expr.X_add_number >= 0x8000
7970 || offset_expr.X_add_number < -0x8000)
7971 && (mips_pic != EMBEDDED_PIC
7972 || offset_expr.X_op != O_subtract
3dacdf2e
NC
7973 || (S_GET_SEGMENT (offset_expr.X_add_symbol)
7974 != S_GET_SEGMENT (offset_expr.X_op_symbol))))
252b5132
RH
7975 break;
7976
fb1b3232 7977 if (c == S_EX_HI)
252b5132
RH
7978 {
7979 if (offset_expr.X_op != O_constant)
7980 break;
7981 offset_expr.X_add_number =
7982 (offset_expr.X_add_number >> 16) & 0xffff;
7983 }
f6688943 7984 *offset_reloc = BFD_RELOC_LO16;
252b5132
RH
7985 s = expr_end;
7986 continue;
7987
7988 case 'p': /* pc relative offset */
cb56d3d3 7989 if (mips_pic == EMBEDDED_PIC)
f6688943 7990 *offset_reloc = BFD_RELOC_16_PCREL_S2;
cb56d3d3 7991 else
f6688943 7992 *offset_reloc = BFD_RELOC_16_PCREL;
252b5132
RH
7993 my_getExpression (&offset_expr, s);
7994 s = expr_end;
7995 continue;
7996
7997 case 'u': /* upper 16 bits */
7998 c = my_getSmallExpression (&imm_expr, s);
f6688943 7999 *imm_reloc = BFD_RELOC_LO16;
e7d556df 8000 if (c != S_EX_NONE)
252b5132 8001 {
fb1b3232 8002 if (c != S_EX_LO)
252b5132
RH
8003 {
8004 if (imm_expr.X_op == O_constant)
8005 imm_expr.X_add_number =
8006 (imm_expr.X_add_number >> 16) & 0xffff;
fb1b3232 8007 else if (c == S_EX_HIGHEST)
f6688943 8008 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
fb1b3232 8009 else if (c == S_EX_HI)
252b5132 8010 {
f6688943 8011 *imm_reloc = BFD_RELOC_HI16_S;
252b5132
RH
8012 imm_unmatched_hi = true;
8013 }
8014 else
f6688943 8015 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8016 }
8017 else if (imm_expr.X_op == O_constant)
8018 imm_expr.X_add_number &= 0xffff;
8019 }
8020 if (imm_expr.X_op == O_constant
8021 && (imm_expr.X_add_number < 0
8022 || imm_expr.X_add_number >= 0x10000))
8023 as_bad (_("lui expression not in range 0..65535"));
8024 s = expr_end;
8025 continue;
8026
8027 case 'a': /* 26 bit address */
8028 my_getExpression (&offset_expr, s);
8029 s = expr_end;
f6688943 8030 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
8031 continue;
8032
8033 case 'N': /* 3 bit branch condition code */
8034 case 'M': /* 3 bit compare condition code */
8035 if (strncmp (s, "$fcc", 4) != 0)
8036 break;
8037 s += 4;
8038 regno = 0;
8039 do
8040 {
8041 regno *= 10;
8042 regno += *s - '0';
8043 ++s;
8044 }
3882b010 8045 while (ISDIGIT (*s));
252b5132
RH
8046 if (regno > 7)
8047 as_bad (_("invalid condition code register $fcc%d"), regno);
8048 if (*args == 'N')
8049 ip->insn_opcode |= regno << OP_SH_BCC;
8050 else
8051 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 8052 continue;
252b5132 8053
156c2f8b
NC
8054 case 'H':
8055 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8056 s += 2;
3882b010 8057 if (ISDIGIT (*s))
156c2f8b
NC
8058 {
8059 c = 0;
8060 do
8061 {
8062 c *= 10;
8063 c += *s - '0';
8064 ++s;
8065 }
3882b010 8066 while (ISDIGIT (*s));
156c2f8b
NC
8067 }
8068 else
8069 c = 8; /* Invalid sel value. */
8070
8071 if (c > 7)
8072 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8073 ip->insn_opcode |= c;
8074 continue;
8075
252b5132
RH
8076 default:
8077 as_bad (_("bad char = '%c'\n"), *args);
8078 internalError ();
8079 }
8080 break;
8081 }
8082 /* Args don't match. */
8083 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8084 !strcmp (insn->name, insn[1].name))
8085 {
8086 ++insn;
8087 s = argsStart;
268f6bed 8088 insn_error = _("illegal operands");
252b5132
RH
8089 continue;
8090 }
268f6bed
L
8091 if (save_c)
8092 *(--s) = save_c;
252b5132
RH
8093 insn_error = _("illegal operands");
8094 return;
8095 }
8096}
8097
8098/* This routine assembles an instruction into its binary format when
8099 assembling for the mips16. As a side effect, it sets one of the
8100 global variables imm_reloc or offset_reloc to the type of
8101 relocation to do if one of the operands is an address expression.
8102 It also sets mips16_small and mips16_ext if the user explicitly
8103 requested a small or extended instruction. */
8104
8105static void
8106mips16_ip (str, ip)
8107 char *str;
8108 struct mips_cl_insn *ip;
8109{
8110 char *s;
8111 const char *args;
8112 struct mips_opcode *insn;
8113 char *argsstart;
8114 unsigned int regno;
8115 unsigned int lastregno = 0;
8116 char *s_reset;
8117
8118 insn_error = NULL;
8119
8120 mips16_small = false;
8121 mips16_ext = false;
8122
3882b010 8123 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
8124 ;
8125 switch (*s)
8126 {
8127 case '\0':
8128 break;
8129
8130 case ' ':
8131 *s++ = '\0';
8132 break;
8133
8134 case '.':
8135 if (s[1] == 't' && s[2] == ' ')
8136 {
8137 *s = '\0';
8138 mips16_small = true;
8139 s += 3;
8140 break;
8141 }
8142 else if (s[1] == 'e' && s[2] == ' ')
8143 {
8144 *s = '\0';
8145 mips16_ext = true;
8146 s += 3;
8147 break;
8148 }
8149 /* Fall through. */
8150 default:
8151 insn_error = _("unknown opcode");
8152 return;
8153 }
8154
8155 if (mips_opts.noautoextend && ! mips16_ext)
8156 mips16_small = true;
8157
8158 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8159 {
8160 insn_error = _("unrecognized opcode");
8161 return;
8162 }
8163
8164 argsstart = s;
8165 for (;;)
8166 {
8167 assert (strcmp (insn->name, str) == 0);
8168
8169 ip->insn_mo = insn;
8170 ip->insn_opcode = insn->match;
8171 ip->use_extend = false;
8172 imm_expr.X_op = O_absent;
f6688943
TS
8173 imm_reloc[0] = BFD_RELOC_UNUSED;
8174 imm_reloc[1] = BFD_RELOC_UNUSED;
8175 imm_reloc[2] = BFD_RELOC_UNUSED;
252b5132 8176 offset_expr.X_op = O_absent;
f6688943
TS
8177 offset_reloc[0] = BFD_RELOC_UNUSED;
8178 offset_reloc[1] = BFD_RELOC_UNUSED;
8179 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
8180 for (args = insn->args; 1; ++args)
8181 {
8182 int c;
8183
8184 if (*s == ' ')
8185 ++s;
8186
8187 /* In this switch statement we call break if we did not find
8188 a match, continue if we did find a match, or return if we
8189 are done. */
8190
8191 c = *args;
8192 switch (c)
8193 {
8194 case '\0':
8195 if (*s == '\0')
8196 {
8197 /* Stuff the immediate value in now, if we can. */
8198 if (imm_expr.X_op == O_constant
f6688943 8199 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
8200 && insn->pinfo != INSN_MACRO)
8201 {
8202 mips16_immed ((char *) NULL, 0,
f6688943 8203 *imm_reloc - BFD_RELOC_UNUSED,
252b5132
RH
8204 imm_expr.X_add_number, true, mips16_small,
8205 mips16_ext, &ip->insn_opcode,
8206 &ip->use_extend, &ip->extend);
8207 imm_expr.X_op = O_absent;
f6688943 8208 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
8209 }
8210
8211 return;
8212 }
8213 break;
8214
8215 case ',':
8216 if (*s++ == c)
8217 continue;
8218 s--;
8219 switch (*++args)
8220 {
8221 case 'v':
8222 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8223 continue;
8224 case 'w':
8225 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8226 continue;
8227 }
8228 break;
8229
8230 case '(':
8231 case ')':
8232 if (*s++ == c)
8233 continue;
8234 break;
8235
8236 case 'v':
8237 case 'w':
8238 if (s[0] != '$')
8239 {
8240 if (c == 'v')
8241 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8242 else
8243 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8244 ++args;
8245 continue;
8246 }
8247 /* Fall through. */
8248 case 'x':
8249 case 'y':
8250 case 'z':
8251 case 'Z':
8252 case '0':
8253 case 'S':
8254 case 'R':
8255 case 'X':
8256 case 'Y':
8257 if (s[0] != '$')
8258 break;
8259 s_reset = s;
3882b010 8260 if (ISDIGIT (s[1]))
252b5132
RH
8261 {
8262 ++s;
8263 regno = 0;
8264 do
8265 {
8266 regno *= 10;
8267 regno += *s - '0';
8268 ++s;
8269 }
3882b010 8270 while (ISDIGIT (*s));
252b5132
RH
8271 if (regno > 31)
8272 {
8273 as_bad (_("invalid register number (%d)"), regno);
8274 regno = 2;
8275 }
8276 }
8277 else
8278 {
8279 if (s[1] == 'f' && s[2] == 'p')
8280 {
8281 s += 3;
8282 regno = FP;
8283 }
8284 else if (s[1] == 's' && s[2] == 'p')
8285 {
8286 s += 3;
8287 regno = SP;
8288 }
8289 else if (s[1] == 'g' && s[2] == 'p')
8290 {
8291 s += 3;
8292 regno = GP;
8293 }
8294 else if (s[1] == 'a' && s[2] == 't')
8295 {
8296 s += 3;
8297 regno = AT;
8298 }
8299 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8300 {
8301 s += 4;
8302 regno = KT0;
8303 }
8304 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8305 {
8306 s += 4;
8307 regno = KT1;
8308 }
8309 else
8310 break;
8311 }
8312
8313 if (*s == ' ')
8314 ++s;
8315 if (args[1] != *s)
8316 {
8317 if (c == 'v' || c == 'w')
8318 {
8319 regno = mips16_to_32_reg_map[lastregno];
8320 s = s_reset;
8321 args++;
8322 }
8323 }
8324
8325 switch (c)
8326 {
8327 case 'x':
8328 case 'y':
8329 case 'z':
8330 case 'v':
8331 case 'w':
8332 case 'Z':
8333 regno = mips32_to_16_reg_map[regno];
8334 break;
8335
8336 case '0':
8337 if (regno != 0)
8338 regno = ILLEGAL_REG;
8339 break;
8340
8341 case 'S':
8342 if (regno != SP)
8343 regno = ILLEGAL_REG;
8344 break;
8345
8346 case 'R':
8347 if (regno != RA)
8348 regno = ILLEGAL_REG;
8349 break;
8350
8351 case 'X':
8352 case 'Y':
8353 if (regno == AT && ! mips_opts.noat)
8354 as_warn (_("used $at without \".set noat\""));
8355 break;
8356
8357 default:
8358 internalError ();
8359 }
8360
8361 if (regno == ILLEGAL_REG)
8362 break;
8363
8364 switch (c)
8365 {
8366 case 'x':
8367 case 'v':
8368 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
8369 break;
8370 case 'y':
8371 case 'w':
8372 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
8373 break;
8374 case 'z':
8375 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
8376 break;
8377 case 'Z':
8378 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
8379 case '0':
8380 case 'S':
8381 case 'R':
8382 break;
8383 case 'X':
8384 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
8385 break;
8386 case 'Y':
8387 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
8388 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
8389 break;
8390 default:
8391 internalError ();
8392 }
8393
8394 lastregno = regno;
8395 continue;
8396
8397 case 'P':
8398 if (strncmp (s, "$pc", 3) == 0)
8399 {
8400 s += 3;
8401 continue;
8402 }
8403 break;
8404
8405 case '<':
8406 case '>':
8407 case '[':
8408 case ']':
8409 case '4':
8410 case '5':
8411 case 'H':
8412 case 'W':
8413 case 'D':
8414 case 'j':
8415 case '8':
8416 case 'V':
8417 case 'C':
8418 case 'U':
8419 case 'k':
8420 case 'K':
8421 if (s[0] == '%'
8422 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
8423 {
8424 /* This is %gprel(SYMBOL). We need to read SYMBOL,
8425 and generate the appropriate reloc. If the text
8426 inside %gprel is not a symbol name with an
8427 optional offset, then we generate a normal reloc
8428 and will probably fail later. */
8429 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
8430 if (imm_expr.X_op == O_symbol)
8431 {
8432 mips16_ext = true;
f6688943 8433 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132
RH
8434 s = expr_end;
8435 ip->use_extend = true;
8436 ip->extend = 0;
8437 continue;
8438 }
8439 }
8440 else
8441 {
8442 /* Just pick up a normal expression. */
8443 my_getExpression (&imm_expr, s);
8444 }
8445
8446 if (imm_expr.X_op == O_register)
8447 {
8448 /* What we thought was an expression turned out to
8449 be a register. */
8450
8451 if (s[0] == '(' && args[1] == '(')
8452 {
8453 /* It looks like the expression was omitted
8454 before a register indirection, which means
8455 that the expression is implicitly zero. We
8456 still set up imm_expr, so that we handle
8457 explicit extensions correctly. */
8458 imm_expr.X_op = O_constant;
8459 imm_expr.X_add_number = 0;
f6688943 8460 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
8461 continue;
8462 }
8463
8464 break;
8465 }
8466
8467 /* We need to relax this instruction. */
f6688943 8468 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
8469 s = expr_end;
8470 continue;
8471
8472 case 'p':
8473 case 'q':
8474 case 'A':
8475 case 'B':
8476 case 'E':
8477 /* We use offset_reloc rather than imm_reloc for the PC
8478 relative operands. This lets macros with both
8479 immediate and address operands work correctly. */
8480 my_getExpression (&offset_expr, s);
8481
8482 if (offset_expr.X_op == O_register)
8483 break;
8484
8485 /* We need to relax this instruction. */
f6688943 8486 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
8487 s = expr_end;
8488 continue;
8489
8490 case '6': /* break code */
8491 my_getExpression (&imm_expr, s);
8492 check_absolute_expr (ip, &imm_expr);
8493 if ((unsigned long) imm_expr.X_add_number > 63)
8494 {
8495 as_warn (_("Invalid value for `%s' (%lu)"),
8496 ip->insn_mo->name,
8497 (unsigned long) imm_expr.X_add_number);
8498 imm_expr.X_add_number &= 0x3f;
8499 }
8500 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
8501 imm_expr.X_op = O_absent;
8502 s = expr_end;
8503 continue;
8504
8505 case 'a': /* 26 bit address */
8506 my_getExpression (&offset_expr, s);
8507 s = expr_end;
f6688943 8508 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
8509 ip->insn_opcode <<= 16;
8510 continue;
8511
8512 case 'l': /* register list for entry macro */
8513 case 'L': /* register list for exit macro */
8514 {
8515 int mask;
8516
8517 if (c == 'l')
8518 mask = 0;
8519 else
8520 mask = 7 << 3;
8521 while (*s != '\0')
8522 {
8523 int freg, reg1, reg2;
8524
8525 while (*s == ' ' || *s == ',')
8526 ++s;
8527 if (*s != '$')
8528 {
8529 as_bad (_("can't parse register list"));
8530 break;
8531 }
8532 ++s;
8533 if (*s != 'f')
8534 freg = 0;
8535 else
8536 {
8537 freg = 1;
8538 ++s;
8539 }
8540 reg1 = 0;
3882b010 8541 while (ISDIGIT (*s))
252b5132
RH
8542 {
8543 reg1 *= 10;
8544 reg1 += *s - '0';
8545 ++s;
8546 }
8547 if (*s == ' ')
8548 ++s;
8549 if (*s != '-')
8550 reg2 = reg1;
8551 else
8552 {
8553 ++s;
8554 if (*s != '$')
8555 break;
8556 ++s;
8557 if (freg)
8558 {
8559 if (*s == 'f')
8560 ++s;
8561 else
8562 {
8563 as_bad (_("invalid register list"));
8564 break;
8565 }
8566 }
8567 reg2 = 0;
3882b010 8568 while (ISDIGIT (*s))
252b5132
RH
8569 {
8570 reg2 *= 10;
8571 reg2 += *s - '0';
8572 ++s;
8573 }
8574 }
8575 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
8576 {
8577 mask &= ~ (7 << 3);
8578 mask |= 5 << 3;
8579 }
8580 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
8581 {
8582 mask &= ~ (7 << 3);
8583 mask |= 6 << 3;
8584 }
8585 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
8586 mask |= (reg2 - 3) << 3;
8587 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
8588 mask |= (reg2 - 15) << 1;
8589 else if (reg1 == 31 && reg2 == 31)
8590 mask |= 1;
8591 else
8592 {
8593 as_bad (_("invalid register list"));
8594 break;
8595 }
8596 }
8597 /* The mask is filled in in the opcode table for the
8598 benefit of the disassembler. We remove it before
8599 applying the actual mask. */
8600 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
8601 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
8602 }
8603 continue;
8604
8605 case 'e': /* extend code */
8606 my_getExpression (&imm_expr, s);
8607 check_absolute_expr (ip, &imm_expr);
8608 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
8609 {
8610 as_warn (_("Invalid value for `%s' (%lu)"),
8611 ip->insn_mo->name,
8612 (unsigned long) imm_expr.X_add_number);
8613 imm_expr.X_add_number &= 0x7ff;
8614 }
8615 ip->insn_opcode |= imm_expr.X_add_number;
8616 imm_expr.X_op = O_absent;
8617 s = expr_end;
8618 continue;
8619
8620 default:
8621 internalError ();
8622 }
8623 break;
8624 }
8625
8626 /* Args don't match. */
8627 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
8628 strcmp (insn->name, insn[1].name) == 0)
8629 {
8630 ++insn;
8631 s = argsstart;
8632 continue;
8633 }
8634
8635 insn_error = _("illegal operands");
8636
8637 return;
8638 }
8639}
8640
8641/* This structure holds information we know about a mips16 immediate
8642 argument type. */
8643
e972090a
NC
8644struct mips16_immed_operand
8645{
252b5132
RH
8646 /* The type code used in the argument string in the opcode table. */
8647 int type;
8648 /* The number of bits in the short form of the opcode. */
8649 int nbits;
8650 /* The number of bits in the extended form of the opcode. */
8651 int extbits;
8652 /* The amount by which the short form is shifted when it is used;
8653 for example, the sw instruction has a shift count of 2. */
8654 int shift;
8655 /* The amount by which the short form is shifted when it is stored
8656 into the instruction code. */
8657 int op_shift;
8658 /* Non-zero if the short form is unsigned. */
8659 int unsp;
8660 /* Non-zero if the extended form is unsigned. */
8661 int extu;
8662 /* Non-zero if the value is PC relative. */
8663 int pcrel;
8664};
8665
8666/* The mips16 immediate operand types. */
8667
8668static const struct mips16_immed_operand mips16_immed_operands[] =
8669{
8670 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
8671 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
8672 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
8673 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
8674 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
8675 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
8676 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
8677 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
8678 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
8679 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
8680 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
8681 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
8682 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
8683 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
8684 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
8685 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
8686 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
8687 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
8688 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
8689 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
8690 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
8691};
8692
8693#define MIPS16_NUM_IMMED \
8694 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
8695
8696/* Handle a mips16 instruction with an immediate value. This or's the
8697 small immediate value into *INSN. It sets *USE_EXTEND to indicate
8698 whether an extended value is needed; if one is needed, it sets
8699 *EXTEND to the value. The argument type is TYPE. The value is VAL.
8700 If SMALL is true, an unextended opcode was explicitly requested.
8701 If EXT is true, an extended opcode was explicitly requested. If
8702 WARN is true, warn if EXT does not match reality. */
8703
8704static void
8705mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
8706 extend)
8707 char *file;
8708 unsigned int line;
8709 int type;
8710 offsetT val;
8711 boolean warn;
8712 boolean small;
8713 boolean ext;
8714 unsigned long *insn;
8715 boolean *use_extend;
8716 unsigned short *extend;
8717{
8718 register const struct mips16_immed_operand *op;
8719 int mintiny, maxtiny;
8720 boolean needext;
8721
8722 op = mips16_immed_operands;
8723 while (op->type != type)
8724 {
8725 ++op;
8726 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
8727 }
8728
8729 if (op->unsp)
8730 {
8731 if (type == '<' || type == '>' || type == '[' || type == ']')
8732 {
8733 mintiny = 1;
8734 maxtiny = 1 << op->nbits;
8735 }
8736 else
8737 {
8738 mintiny = 0;
8739 maxtiny = (1 << op->nbits) - 1;
8740 }
8741 }
8742 else
8743 {
8744 mintiny = - (1 << (op->nbits - 1));
8745 maxtiny = (1 << (op->nbits - 1)) - 1;
8746 }
8747
8748 /* Branch offsets have an implicit 0 in the lowest bit. */
8749 if (type == 'p' || type == 'q')
8750 val /= 2;
8751
8752 if ((val & ((1 << op->shift) - 1)) != 0
8753 || val < (mintiny << op->shift)
8754 || val > (maxtiny << op->shift))
8755 needext = true;
8756 else
8757 needext = false;
8758
8759 if (warn && ext && ! needext)
beae10d5
KH
8760 as_warn_where (file, line,
8761 _("extended operand requested but not required"));
252b5132
RH
8762 if (small && needext)
8763 as_bad_where (file, line, _("invalid unextended operand value"));
8764
8765 if (small || (! ext && ! needext))
8766 {
8767 int insnval;
8768
8769 *use_extend = false;
8770 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
8771 insnval <<= op->op_shift;
8772 *insn |= insnval;
8773 }
8774 else
8775 {
8776 long minext, maxext;
8777 int extval;
8778
8779 if (op->extu)
8780 {
8781 minext = 0;
8782 maxext = (1 << op->extbits) - 1;
8783 }
8784 else
8785 {
8786 minext = - (1 << (op->extbits - 1));
8787 maxext = (1 << (op->extbits - 1)) - 1;
8788 }
8789 if (val < minext || val > maxext)
8790 as_bad_where (file, line,
8791 _("operand value out of range for instruction"));
8792
8793 *use_extend = true;
8794 if (op->extbits == 16)
8795 {
8796 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
8797 val &= 0x1f;
8798 }
8799 else if (op->extbits == 15)
8800 {
8801 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
8802 val &= 0xf;
8803 }
8804 else
8805 {
8806 extval = ((val & 0x1f) << 6) | (val & 0x20);
8807 val = 0;
8808 }
8809
8810 *extend = (unsigned short) extval;
8811 *insn |= val;
8812 }
8813}
8814\f
252b5132
RH
8815
8816static int
8817my_getSmallExpression (ep, str)
8818 expressionS *ep;
8819 char *str;
8820{
8821 char *sp;
fb1b3232
TS
8822 char *oldstr = str;
8823 int c = S_EX_NONE;
252b5132
RH
8824
8825 if (*str == ' ')
8826 str++;
fb1b3232
TS
8827 if (*str == '(')
8828 c = S_EX_NONE;
8829 else if (str[0] == '%'
3882b010
L
8830 && TOLOWER (str[1]) == 'l'
8831 && TOLOWER (str[2]) == 'o'
fb1b3232 8832 && str[3] == '(')
252b5132 8833 {
fb1b3232
TS
8834 c = S_EX_LO;
8835 str += sizeof ("%lo(") - 2;
8836 }
8837 else if (str[0] == '%'
3882b010
L
8838 && TOLOWER (str[1]) == 'h'
8839 && TOLOWER (str[2]) == 'i'
fb1b3232
TS
8840 && str[3] == '(')
8841 {
8842 c = S_EX_HI;
8843 str += sizeof ("%hi(") - 2;
8844 }
8845 else if (str[0] == '%'
3882b010
L
8846 && TOLOWER (str[1]) == 'h'
8847 && TOLOWER (str[2]) == 'i'
8848 && TOLOWER (str[3]) == 'g'
8849 && TOLOWER (str[4]) == 'h'
8850 && TOLOWER (str[5]) == 'e'
8851 && TOLOWER (str[6]) == 'r'
fb1b3232
TS
8852 && str[7] == '(')
8853 {
8854 c = S_EX_HIGHER;
8855 str += sizeof ("%higher(") - 2;
8856 }
8857 else if (str[0] == '%'
3882b010
L
8858 && TOLOWER (str[1]) == 'h'
8859 && TOLOWER (str[2]) == 'i'
8860 && TOLOWER (str[3]) == 'g'
8861 && TOLOWER (str[4]) == 'h'
8862 && TOLOWER (str[5]) == 'e'
8863 && TOLOWER (str[6]) == 's'
8864 && TOLOWER (str[7]) == 't'
fb1b3232
TS
8865 && str[8] == '(')
8866 {
8867 c = S_EX_HIGHEST;
8868 str += sizeof ("%highest(") - 2;
8869 }
8870/* currently unsupported */
8871#if 0
8872 else if (str[0] == '%'
3882b010
L
8873 && TOLOWER (str[1]) == 'g'
8874 && TOLOWER (str[2]) == 'p'
8875 && TOLOWER (str[3]) == '_'
8876 && TOLOWER (str[4]) == 'r'
8877 && TOLOWER (str[5]) == 'e'
8878 && TOLOWER (str[6]) == 'l'
e7d556df 8879 && str[7] == '(')
fb1b3232
TS
8880 {
8881 c = S_EX_GPREL;
e7d556df 8882 str += sizeof ("%gp_rel(") - 2;
fb1b3232
TS
8883 }
8884 else if (str[0] == '%'
3882b010
L
8885 && TOLOWER (str[1]) == 'n'
8886 && TOLOWER (str[2]) == 'e'
8887 && TOLOWER (str[3]) == 'g'
fb1b3232
TS
8888 && str[4] == '(')
8889 {
8890 c = S_EX_NEG;
8891 str += sizeof ("%neg(") - 2;
8892 }
8893#endif
8894 else
8895 {
8896 my_getExpression (ep, str);
8897 return c;
8898 }
252b5132 8899
fb1b3232
TS
8900 /*
8901 * A small expression may be followed by a base register.
8902 * Scan to the end of this operand, and then back over a possible
8903 * base register. Then scan the small expression up to that
8904 * point. (Based on code in sparc.c...)
8905 */
8906 for (sp = str; *sp && *sp != ','; sp++)
8907 ;
8908 if (sp - 4 >= str && sp[-1] == ')')
8909 {
3882b010 8910 if (ISDIGIT (sp[-2]))
252b5132 8911 {
3882b010 8912 for (sp -= 3; sp >= str && ISDIGIT (*sp); sp--)
fb1b3232
TS
8913 ;
8914 if (*sp == '$' && sp > str && sp[-1] == '(')
252b5132 8915 {
fb1b3232
TS
8916 sp--;
8917 goto do_it;
252b5132 8918 }
fb1b3232
TS
8919 }
8920 else if (sp - 5 >= str
8921 && sp[-5] == '('
8922 && sp[-4] == '$'
8923 && ((sp[-3] == 'f' && sp[-2] == 'p')
8924 || (sp[-3] == 's' && sp[-2] == 'p')
8925 || (sp[-3] == 'g' && sp[-2] == 'p')
8926 || (sp[-3] == 'a' && sp[-2] == 't')))
8927 {
8928 sp -= 5;
8929 do_it:
8930 if (sp == str)
252b5132 8931 {
fb1b3232
TS
8932 /* no expression means zero offset */
8933 if (c != S_EX_NONE)
252b5132 8934 {
fb1b3232
TS
8935 /* %xx(reg) is an error */
8936 ep->X_op = O_absent;
8937 expr_end = oldstr;
252b5132
RH
8938 }
8939 else
8940 {
fb1b3232
TS
8941 ep->X_op = O_constant;
8942 expr_end = sp;
252b5132 8943 }
fb1b3232
TS
8944 ep->X_add_symbol = NULL;
8945 ep->X_op_symbol = NULL;
8946 ep->X_add_number = 0;
252b5132 8947 }
fb1b3232
TS
8948 else
8949 {
8950 *sp = '\0';
8951 my_getExpression (ep, str);
8952 *sp = '(';
8953 }
8954 return c;
252b5132
RH
8955 }
8956 }
8957 my_getExpression (ep, str);
fb1b3232
TS
8958
8959 /* => %highest, %higher, %hi, %lo, %gprel, %neg encountered */
8960 return c;
252b5132
RH
8961}
8962
8963static void
8964my_getExpression (ep, str)
8965 expressionS *ep;
8966 char *str;
8967{
8968 char *save_in;
98aa84af 8969 valueT val;
252b5132
RH
8970
8971 save_in = input_line_pointer;
8972 input_line_pointer = str;
8973 expression (ep);
8974 expr_end = input_line_pointer;
8975 input_line_pointer = save_in;
8976
8977 /* If we are in mips16 mode, and this is an expression based on `.',
8978 then we bump the value of the symbol by 1 since that is how other
8979 text symbols are handled. We don't bother to handle complex
8980 expressions, just `.' plus or minus a constant. */
8981 if (mips_opts.mips16
8982 && ep->X_op == O_symbol
8983 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
8984 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
8985 && symbol_get_frag (ep->X_add_symbol) == frag_now
8986 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
8987 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
8988 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
8989}
8990
8991/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
8992 of type TYPE, and store the appropriate bytes in *LITP. The number
8993 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
8994 returned, or NULL on OK. */
8995
8996char *
8997md_atof (type, litP, sizeP)
8998 int type;
8999 char *litP;
9000 int *sizeP;
9001{
9002 int prec;
9003 LITTLENUM_TYPE words[4];
9004 char *t;
9005 int i;
9006
9007 switch (type)
9008 {
9009 case 'f':
9010 prec = 2;
9011 break;
9012
9013 case 'd':
9014 prec = 4;
9015 break;
9016
9017 default:
9018 *sizeP = 0;
9019 return _("bad call to md_atof");
9020 }
9021
9022 t = atof_ieee (input_line_pointer, type, words);
9023 if (t)
9024 input_line_pointer = t;
9025
9026 *sizeP = prec * 2;
9027
9028 if (! target_big_endian)
9029 {
9030 for (i = prec - 1; i >= 0; i--)
9031 {
9032 md_number_to_chars (litP, (valueT) words[i], 2);
9033 litP += 2;
9034 }
9035 }
9036 else
9037 {
9038 for (i = 0; i < prec; i++)
9039 {
9040 md_number_to_chars (litP, (valueT) words[i], 2);
9041 litP += 2;
9042 }
9043 }
bdaaa2e1 9044
252b5132
RH
9045 return NULL;
9046}
9047
9048void
9049md_number_to_chars (buf, val, n)
9050 char *buf;
9051 valueT val;
9052 int n;
9053{
9054 if (target_big_endian)
9055 number_to_chars_bigendian (buf, val, n);
9056 else
9057 number_to_chars_littleendian (buf, val, n);
9058}
9059\f
e013f690
TS
9060static int support_64bit_objects(void)
9061{
9062 const char **list, **l;
9063
9064 list = bfd_target_list ();
9065 for (l = list; *l != NULL; l++)
9066#ifdef TE_TMIPS
9067 /* This is traditional mips */
9068 if (strcmp (*l, "elf64-tradbigmips") == 0
9069 || strcmp (*l, "elf64-tradlittlemips") == 0)
9070#else
9071 if (strcmp (*l, "elf64-bigmips") == 0
9072 || strcmp (*l, "elf64-littlemips") == 0)
9073#endif
9074 break;
9075 free (list);
9076 return (*l != NULL);
9077}
9078
39c0a331 9079CONST char *md_shortopts = "nO::g::G:";
252b5132 9080
e972090a
NC
9081struct option md_longopts[] =
9082{
252b5132
RH
9083#define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9084 {"mips0", no_argument, NULL, OPTION_MIPS1},
9085 {"mips1", no_argument, NULL, OPTION_MIPS1},
9086#define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9087 {"mips2", no_argument, NULL, OPTION_MIPS2},
9088#define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9089 {"mips3", no_argument, NULL, OPTION_MIPS3},
9090#define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9091 {"mips4", no_argument, NULL, OPTION_MIPS4},
9092#define OPTION_MCPU (OPTION_MD_BASE + 5)
9093 {"mcpu", required_argument, NULL, OPTION_MCPU},
9094#define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 6)
9095 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
156c2f8b 9096#define OPTION_TRAP (OPTION_MD_BASE + 7)
252b5132
RH
9097 {"trap", no_argument, NULL, OPTION_TRAP},
9098 {"no-break", no_argument, NULL, OPTION_TRAP},
156c2f8b 9099#define OPTION_BREAK (OPTION_MD_BASE + 8)
252b5132
RH
9100 {"break", no_argument, NULL, OPTION_BREAK},
9101 {"no-trap", no_argument, NULL, OPTION_BREAK},
156c2f8b 9102#define OPTION_EB (OPTION_MD_BASE + 9)
252b5132 9103 {"EB", no_argument, NULL, OPTION_EB},
156c2f8b 9104#define OPTION_EL (OPTION_MD_BASE + 10)
252b5132 9105 {"EL", no_argument, NULL, OPTION_EL},
156c2f8b 9106#define OPTION_M4650 (OPTION_MD_BASE + 11)
252b5132 9107 {"m4650", no_argument, NULL, OPTION_M4650},
156c2f8b 9108#define OPTION_NO_M4650 (OPTION_MD_BASE + 12)
252b5132 9109 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
156c2f8b 9110#define OPTION_M4010 (OPTION_MD_BASE + 13)
252b5132 9111 {"m4010", no_argument, NULL, OPTION_M4010},
156c2f8b 9112#define OPTION_NO_M4010 (OPTION_MD_BASE + 14)
252b5132 9113 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
156c2f8b 9114#define OPTION_M4100 (OPTION_MD_BASE + 15)
252b5132 9115 {"m4100", no_argument, NULL, OPTION_M4100},
156c2f8b 9116#define OPTION_NO_M4100 (OPTION_MD_BASE + 16)
252b5132 9117 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
156c2f8b 9118#define OPTION_MIPS16 (OPTION_MD_BASE + 17)
252b5132 9119 {"mips16", no_argument, NULL, OPTION_MIPS16},
156c2f8b 9120#define OPTION_NO_MIPS16 (OPTION_MD_BASE + 18)
252b5132 9121 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
156c2f8b 9122#define OPTION_M3900 (OPTION_MD_BASE + 19)
252b5132 9123 {"m3900", no_argument, NULL, OPTION_M3900},
156c2f8b 9124#define OPTION_NO_M3900 (OPTION_MD_BASE + 20)
252b5132 9125 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
156c2f8b 9126#define OPTION_MABI (OPTION_MD_BASE + 21)
252b5132 9127 {"mabi", required_argument, NULL, OPTION_MABI},
156c2f8b 9128#define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 22)
6b76fefe 9129 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
156c2f8b 9130#define OPTION_NO_M7000_HILO_FIX (OPTION_MD_BASE + 23)
6b76fefe 9131 {"no-fix-7000", no_argument, NULL, OPTION_NO_M7000_HILO_FIX},
156c2f8b 9132#define OPTION_GP32 (OPTION_MD_BASE + 24)
c97ef257 9133 {"mgp32", no_argument, NULL, OPTION_GP32},
156c2f8b 9134#define OPTION_GP64 (OPTION_MD_BASE + 25)
c97ef257 9135 {"mgp64", no_argument, NULL, OPTION_GP64},
156c2f8b 9136#define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 26)
119d663a 9137 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
156c2f8b 9138#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 27)
119d663a 9139 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
156c2f8b
NC
9140#define OPTION_MIPS32 (OPTION_MD_BASE + 28)
9141 {"mips32", no_argument, NULL, OPTION_MIPS32},
84ea6cf2
NC
9142#define OPTION_MIPS5 (OPTION_MD_BASE + 29)
9143 {"mips5", no_argument, NULL, OPTION_MIPS5},
9144#define OPTION_MIPS64 (OPTION_MD_BASE + 30)
9145 {"mips64", no_argument, NULL, OPTION_MIPS64},
ec68c924
EC
9146#define OPTION_MARCH (OPTION_MD_BASE + 31)
9147 {"march", required_argument, NULL, OPTION_MARCH},
9148#define OPTION_MTUNE (OPTION_MD_BASE + 32)
9149 {"mtune", required_argument, NULL, OPTION_MTUNE},
ca4e0257
RS
9150#define OPTION_FP32 (OPTION_MD_BASE + 33)
9151 {"mfp32", no_argument, NULL, OPTION_FP32},
156c2f8b
NC
9152#ifdef OBJ_ELF
9153#define OPTION_ELF_BASE (OPTION_MD_BASE + 35)
9154#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9155#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
9156#define OPTION_XGOT (OPTION_ELF_BASE + 2)
9157#define OPTION_32 (OPTION_ELF_BASE + 3)
e013f690
TS
9158#define OPTION_N32 (OPTION_ELF_BASE + 4)
9159#define OPTION_64 (OPTION_ELF_BASE + 5)
156c2f8b
NC
9160 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
9161 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9162 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
9163 {"xgot", no_argument, NULL, OPTION_XGOT},
9164 {"32", no_argument, NULL, OPTION_32},
e013f690 9165 {"n32", no_argument, NULL, OPTION_N32},
156c2f8b
NC
9166 {"64", no_argument, NULL, OPTION_64},
9167#endif
119d663a 9168
252b5132
RH
9169 {NULL, no_argument, NULL, 0}
9170};
156c2f8b 9171size_t md_longopts_size = sizeof (md_longopts);
252b5132
RH
9172
9173int
9174md_parse_option (c, arg)
9175 int c;
9176 char *arg;
9177{
9178 switch (c)
9179 {
119d663a
NC
9180 case OPTION_CONSTRUCT_FLOATS:
9181 mips_disable_float_construction = 0;
9182 break;
bdaaa2e1 9183
119d663a
NC
9184 case OPTION_NO_CONSTRUCT_FLOATS:
9185 mips_disable_float_construction = 1;
9186 break;
bdaaa2e1 9187
252b5132
RH
9188 case OPTION_TRAP:
9189 mips_trap = 1;
9190 break;
9191
9192 case OPTION_BREAK:
9193 mips_trap = 0;
9194 break;
9195
9196 case OPTION_EB:
9197 target_big_endian = 1;
9198 break;
9199
9200 case OPTION_EL:
9201 target_big_endian = 0;
9202 break;
9203
39c0a331
L
9204 case 'n':
9205 warn_nops = 1;
9206 break;
9207
252b5132
RH
9208 case 'O':
9209 if (arg && arg[1] == '0')
9210 mips_optimize = 1;
9211 else
9212 mips_optimize = 2;
9213 break;
9214
9215 case 'g':
9216 if (arg == NULL)
9217 mips_debug = 2;
9218 else
9219 mips_debug = atoi (arg);
9220 /* When the MIPS assembler sees -g or -g2, it does not do
9221 optimizations which limit full symbolic debugging. We take
9222 that to be equivalent to -O0. */
9223 if (mips_debug == 2)
9224 mips_optimize = 1;
9225 break;
9226
9227 case OPTION_MIPS1:
e7af610e 9228 mips_opts.isa = ISA_MIPS1;
252b5132
RH
9229 break;
9230
9231 case OPTION_MIPS2:
e7af610e 9232 mips_opts.isa = ISA_MIPS2;
252b5132
RH
9233 break;
9234
9235 case OPTION_MIPS3:
e7af610e 9236 mips_opts.isa = ISA_MIPS3;
252b5132
RH
9237 break;
9238
9239 case OPTION_MIPS4:
e7af610e
NC
9240 mips_opts.isa = ISA_MIPS4;
9241 break;
9242
84ea6cf2
NC
9243 case OPTION_MIPS5:
9244 mips_opts.isa = ISA_MIPS5;
9245 break;
9246
e7af610e
NC
9247 case OPTION_MIPS32:
9248 mips_opts.isa = ISA_MIPS32;
252b5132
RH
9249 break;
9250
84ea6cf2
NC
9251 case OPTION_MIPS64:
9252 mips_opts.isa = ISA_MIPS64;
9253 break;
9254
ec68c924
EC
9255 case OPTION_MTUNE:
9256 case OPTION_MARCH:
252b5132
RH
9257 case OPTION_MCPU:
9258 {
ec68c924
EC
9259 int cpu = CPU_UNKNOWN;
9260
e7af610e 9261 /* Identify the processor type. */
ec68c924 9262 if (strcasecmp (arg, "default") != 0)
252b5132 9263 {
e7af610e 9264 const struct mips_cpu_info *ci;
252b5132 9265
e7af610e
NC
9266 ci = mips_cpu_info_from_name (arg);
9267 if (ci == NULL || ci->is_isa)
ec68c924
EC
9268 {
9269 switch (c)
9270 {
9271 case OPTION_MTUNE:
9272 as_fatal (_("invalid architecture -mtune=%s"), arg);
9273 break;
9274 case OPTION_MARCH:
9275 as_fatal (_("invalid architecture -march=%s"), arg);
9276 break;
9277 case OPTION_MCPU:
9278 as_fatal (_("invalid architecture -mcpu=%s"), arg);
9279 break;
9280 }
9281 }
e7af610e 9282 else
ec68c924
EC
9283 cpu = ci->cpu;
9284 }
9285
9286 switch (c)
9287 {
9288 case OPTION_MTUNE:
6dce9e24
TS
9289 if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
9290 as_warn(_("A different -mtune= was already specified, is now "
9291 "-mtune=%s"), arg);
ec68c924
EC
9292 mips_tune = cpu;
9293 break;
9294 case OPTION_MARCH:
6dce9e24
TS
9295 if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
9296 as_warn(_("A different -march= was already specified, is now "
9297 "-march=%s"), arg);
ec68c924
EC
9298 mips_arch = cpu;
9299 break;
9300 case OPTION_MCPU:
6dce9e24
TS
9301 if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
9302 as_warn(_("A different -mcpu= was already specified, is now "
9303 "-mcpu=%s"), arg);
ec68c924 9304 mips_cpu = cpu;
252b5132
RH
9305 }
9306 }
9307 break;
9308
9309 case OPTION_M4650:
6dce9e24
TS
9310 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
9311 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
9312 as_warn(_("A different -march= or -mtune= was already specified, "
9313 "is now -m4650"));
ec68c924
EC
9314 mips_arch = CPU_R4650;
9315 mips_tune = CPU_R4650;
252b5132
RH
9316 break;
9317
9318 case OPTION_NO_M4650:
9319 break;
9320
9321 case OPTION_M4010:
6dce9e24
TS
9322 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
9323 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
9324 as_warn(_("A different -march= or -mtune= was already specified, "
9325 "is now -m4010"));
ec68c924
EC
9326 mips_arch = CPU_R4010;
9327 mips_tune = CPU_R4010;
252b5132
RH
9328 break;
9329
9330 case OPTION_NO_M4010:
9331 break;
9332
9333 case OPTION_M4100:
6dce9e24
TS
9334 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
9335 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
9336 as_warn(_("A different -march= or -mtune= was already specified, "
9337 "is now -m4100"));
ec68c924
EC
9338 mips_arch = CPU_VR4100;
9339 mips_tune = CPU_VR4100;
252b5132
RH
9340 break;
9341
9342 case OPTION_NO_M4100:
9343 break;
9344
252b5132 9345 case OPTION_M3900:
6dce9e24
TS
9346 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
9347 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
9348 as_warn(_("A different -march= or -mtune= was already specified, "
9349 "is now -m3900"));
ec68c924
EC
9350 mips_arch = CPU_R3900;
9351 mips_tune = CPU_R3900;
252b5132 9352 break;
bdaaa2e1 9353
252b5132
RH
9354 case OPTION_NO_M3900:
9355 break;
9356
9357 case OPTION_MIPS16:
9358 mips_opts.mips16 = 1;
9359 mips_no_prev_insn (false);
9360 break;
9361
9362 case OPTION_NO_MIPS16:
9363 mips_opts.mips16 = 0;
9364 mips_no_prev_insn (false);
9365 break;
9366
9367 case OPTION_MEMBEDDED_PIC:
9368 mips_pic = EMBEDDED_PIC;
9369 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
9370 {
9371 as_bad (_("-G may not be used with embedded PIC code"));
9372 return 0;
9373 }
9374 g_switch_value = 0x7fffffff;
9375 break;
9376
0f074f60 9377#ifdef OBJ_ELF
252b5132
RH
9378 /* When generating ELF code, we permit -KPIC and -call_shared to
9379 select SVR4_PIC, and -non_shared to select no PIC. This is
9380 intended to be compatible with Irix 5. */
9381 case OPTION_CALL_SHARED:
9382 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9383 {
9384 as_bad (_("-call_shared is supported only for ELF format"));
9385 return 0;
9386 }
9387 mips_pic = SVR4_PIC;
9388 if (g_switch_seen && g_switch_value != 0)
9389 {
9390 as_bad (_("-G may not be used with SVR4 PIC code"));
9391 return 0;
9392 }
9393 g_switch_value = 0;
9394 break;
9395
9396 case OPTION_NON_SHARED:
9397 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9398 {
9399 as_bad (_("-non_shared is supported only for ELF format"));
9400 return 0;
9401 }
9402 mips_pic = NO_PIC;
9403 break;
9404
9405 /* The -xgot option tells the assembler to use 32 offsets when
9406 accessing the got in SVR4_PIC mode. It is for Irix
9407 compatibility. */
9408 case OPTION_XGOT:
9409 mips_big_got = 1;
9410 break;
0f074f60 9411#endif /* OBJ_ELF */
252b5132
RH
9412
9413 case 'G':
9414 if (! USE_GLOBAL_POINTER_OPT)
9415 {
9416 as_bad (_("-G is not supported for this configuration"));
9417 return 0;
9418 }
9419 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
9420 {
9421 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
9422 return 0;
9423 }
9424 else
9425 g_switch_value = atoi (arg);
9426 g_switch_seen = 1;
9427 break;
9428
0f074f60 9429#ifdef OBJ_ELF
252b5132
RH
9430 /* The -32 and -64 options tell the assembler to output the 32
9431 bit or the 64 bit MIPS ELF format. */
9432 case OPTION_32:
e013f690 9433 mips_abi = O32_ABI;
252b5132
RH
9434 break;
9435
e013f690
TS
9436 case OPTION_N32:
9437 mips_abi = N32_ABI;
9438 break;
252b5132 9439
e013f690
TS
9440 case OPTION_64:
9441 mips_abi = N64_ABI;
9442 if (! support_64bit_objects())
9443 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132
RH
9444 break;
9445
c97ef257
AH
9446 case OPTION_GP32:
9447 mips_gp32 = 1;
e013f690
TS
9448 if (mips_abi != O32_ABI)
9449 mips_abi = NO_ABI;
c97ef257
AH
9450 break;
9451
9452 case OPTION_GP64:
9453 mips_gp32 = 0;
e013f690
TS
9454 if (mips_abi == O32_ABI)
9455 mips_abi = NO_ABI;
c97ef257 9456 break;
252b5132 9457
ca4e0257
RS
9458 case OPTION_FP32:
9459 mips_fp32 = 1;
e013f690
TS
9460 if (mips_abi != O32_ABI)
9461 mips_abi = NO_ABI;
ca4e0257
RS
9462 break;
9463
252b5132 9464 case OPTION_MABI:
e013f690
TS
9465 if (strcmp (arg, "32") == 0)
9466 mips_abi = O32_ABI;
9467 else if (strcmp (arg, "o64") == 0)
9468 mips_abi = O64_ABI;
9469 else if (strcmp (arg, "n32") == 0)
9470 mips_abi = N32_ABI;
9471 else if (strcmp (arg, "64") == 0)
9472 {
9473 mips_abi = N64_ABI;
9474 if (! support_64bit_objects())
9475 as_fatal (_("No compiled in support for 64 bit object file "
9476 "format"));
9477 }
9478 else if (strcmp (arg, "eabi") == 0)
9479 mips_abi = EABI_ABI;
9480 else
9481 mips_abi = NO_ABI;
252b5132 9482 break;
e013f690 9483#endif /* OBJ_ELF */
252b5132 9484
6b76fefe
CM
9485 case OPTION_M7000_HILO_FIX:
9486 mips_7000_hilo_fix = true;
9487 break;
9488
9489 case OPTION_NO_M7000_HILO_FIX:
9490 mips_7000_hilo_fix = false;
9491 break;
9492
252b5132
RH
9493 default:
9494 return 0;
9495 }
9496
9497 return 1;
9498}
9499
252b5132
RH
9500static void
9501show (stream, string, col_p, first_p)
9502 FILE *stream;
9503 char *string;
9504 int *col_p;
9505 int *first_p;
9506{
9507 if (*first_p)
9508 {
9509 fprintf (stream, "%24s", "");
9510 *col_p = 24;
9511 }
9512 else
9513 {
9514 fprintf (stream, ", ");
9515 *col_p += 2;
9516 }
9517
9518 if (*col_p + strlen (string) > 72)
9519 {
9520 fprintf (stream, "\n%24s", "");
9521 *col_p = 24;
9522 }
9523
9524 fprintf (stream, "%s", string);
9525 *col_p += strlen (string);
9526
9527 *first_p = 0;
9528}
9529
252b5132
RH
9530void
9531md_show_usage (stream)
9532 FILE *stream;
9533{
9534 int column, first;
9535
beae10d5 9536 fprintf (stream, _("\
252b5132
RH
9537MIPS options:\n\
9538-membedded-pic generate embedded position independent code\n\
9539-EB generate big endian output\n\
9540-EL generate little endian output\n\
9a41af64 9541-g, -g2 do not remove unneeded NOPs or swap branches\n\
252b5132
RH
9542-G NUM allow referencing objects up to NUM bytes\n\
9543 implicitly with the gp register [default 8]\n"));
beae10d5 9544 fprintf (stream, _("\
252b5132
RH
9545-mips1 generate MIPS ISA I instructions\n\
9546-mips2 generate MIPS ISA II instructions\n\
9547-mips3 generate MIPS ISA III instructions\n\
9548-mips4 generate MIPS ISA IV instructions\n\
84ea6cf2 9549-mips5 generate MIPS ISA V instructions\n\
e7af610e 9550-mips32 generate MIPS32 ISA instructions\n\
84ea6cf2 9551-mips64 generate MIPS64 ISA instructions\n\
ec68c924 9552-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
252b5132
RH
9553
9554 first = 1;
9555
9556 show (stream, "2000", &column, &first);
9557 show (stream, "3000", &column, &first);
9558 show (stream, "3900", &column, &first);
9559 show (stream, "4000", &column, &first);
9560 show (stream, "4010", &column, &first);
9561 show (stream, "4100", &column, &first);
9562 show (stream, "4111", &column, &first);
9563 show (stream, "4300", &column, &first);
9564 show (stream, "4400", &column, &first);
9565 show (stream, "4600", &column, &first);
9566 show (stream, "4650", &column, &first);
9567 show (stream, "5000", &column, &first);
18ae5d72
EC
9568 show (stream, "5200", &column, &first);
9569 show (stream, "5230", &column, &first);
9570 show (stream, "5231", &column, &first);
9571 show (stream, "5261", &column, &first);
9572 show (stream, "5721", &column, &first);
252b5132
RH
9573 show (stream, "6000", &column, &first);
9574 show (stream, "8000", &column, &first);
9575 show (stream, "10000", &column, &first);
d1cf510e 9576 show (stream, "12000", &column, &first);
c6c98b38 9577 show (stream, "sb-1", &column, &first);
252b5132
RH
9578 fputc ('\n', stream);
9579
9580 fprintf (stream, _("\
ec68c924 9581-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
252b5132
RH
9582-no-mCPU don't generate code specific to CPU.\n\
9583 For -mCPU and -no-mCPU, CPU must be one of:\n"));
9584
9585 first = 1;
9586
9587 show (stream, "3900", &column, &first);
9588 show (stream, "4010", &column, &first);
9589 show (stream, "4100", &column, &first);
9590 show (stream, "4650", &column, &first);
9591 fputc ('\n', stream);
9592
beae10d5 9593 fprintf (stream, _("\
252b5132
RH
9594-mips16 generate mips16 instructions\n\
9595-no-mips16 do not generate mips16 instructions\n"));
beae10d5 9596 fprintf (stream, _("\
ca4e0257
RS
9597-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
9598-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
252b5132
RH
9599-O0 remove unneeded NOPs, do not swap branches\n\
9600-O remove unneeded NOPs and swap branches\n\
63486801 9601-n warn about NOPs generated from macros\n\
119d663a 9602--[no-]construct-floats [dis]allow floating point values to be constructed\n\
252b5132
RH
9603--trap, --no-break trap exception on div by 0 and mult overflow\n\
9604--break, --no-trap break exception on div by 0 and mult overflow\n"));
9605#ifdef OBJ_ELF
beae10d5 9606 fprintf (stream, _("\
252b5132
RH
9607-KPIC, -call_shared generate SVR4 position independent code\n\
9608-non_shared do not generate position independent code\n\
9609-xgot assume a 32 bit GOT\n\
e013f690
TS
9610-32 create o32 ABI object file (default)\n\
9611-n32 create n32 ABI object file\n\
9612-64 create 64 ABI object file\n"));
252b5132
RH
9613#endif
9614}
9615\f
9616void
9617mips_init_after_args ()
9618{
9619 /* initialize opcodes */
9620 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 9621 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
9622}
9623
9624long
9625md_pcrel_from (fixP)
9626 fixS *fixP;
9627{
9628 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
9629 && fixP->fx_addsy != (symbolS *) NULL
9630 && ! S_IS_DEFINED (fixP->fx_addsy))
9631 {
cb56d3d3
TS
9632 if (mips_pic == EMBEDDED_PIC)
9633 {
9634 /* This makes a branch to an undefined symbol be a branch to the
9635 current location. */
9636 return 4;
9637 }
9638 else
9639 {
9640 return 1;
9641 }
252b5132
RH
9642 }
9643
9644 /* return the address of the delay slot */
9645 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
9646}
9647
252b5132
RH
9648/* This is called before the symbol table is processed. In order to
9649 work with gcc when using mips-tfile, we must keep all local labels.
9650 However, in other cases, we want to discard them. If we were
9651 called with -g, but we didn't see any debugging information, it may
9652 mean that gcc is smuggling debugging information through to
9653 mips-tfile, in which case we must generate all local labels. */
9654
9655void
9656mips_frob_file_before_adjust ()
9657{
9658#ifndef NO_ECOFF_DEBUGGING
9659 if (ECOFF_DEBUGGING
9660 && mips_debug != 0
9661 && ! ecoff_debugging_seen)
9662 flag_keep_locals = 1;
9663#endif
9664}
9665
9666/* Sort any unmatched HI16_S relocs so that they immediately precede
9667 the corresponding LO reloc. This is called before md_apply_fix and
9668 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
9669 explicit use of the %hi modifier. */
9670
9671void
9672mips_frob_file ()
9673{
9674 struct mips_hi_fixup *l;
9675
9676 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
9677 {
9678 segment_info_type *seginfo;
9679 int pass;
9680
9681 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
9682
9683 /* Check quickly whether the next fixup happens to be a matching
9684 %lo. */
9685 if (l->fixp->fx_next != NULL
9686 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
9687 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
9688 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
9689 continue;
9690
9691 /* Look through the fixups for this segment for a matching %lo.
9692 When we find one, move the %hi just in front of it. We do
9693 this in two passes. In the first pass, we try to find a
9694 unique %lo. In the second pass, we permit multiple %hi
9695 relocs for a single %lo (this is a GNU extension). */
9696 seginfo = seg_info (l->seg);
9697 for (pass = 0; pass < 2; pass++)
9698 {
9699 fixS *f, *prev;
9700
9701 prev = NULL;
9702 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
9703 {
9704 /* Check whether this is a %lo fixup which matches l->fixp. */
9705 if (f->fx_r_type == BFD_RELOC_LO16
9706 && f->fx_addsy == l->fixp->fx_addsy
9707 && f->fx_offset == l->fixp->fx_offset
9708 && (pass == 1
9709 || prev == NULL
9710 || prev->fx_r_type != BFD_RELOC_HI16_S
9711 || prev->fx_addsy != f->fx_addsy
9712 || prev->fx_offset != f->fx_offset))
9713 {
9714 fixS **pf;
9715
9716 /* Move l->fixp before f. */
9717 for (pf = &seginfo->fix_root;
9718 *pf != l->fixp;
9719 pf = &(*pf)->fx_next)
9720 assert (*pf != NULL);
9721
9722 *pf = l->fixp->fx_next;
9723
9724 l->fixp->fx_next = f;
9725 if (prev == NULL)
9726 seginfo->fix_root = l->fixp;
9727 else
9728 prev->fx_next = l->fixp;
9729
9730 break;
9731 }
9732
9733 prev = f;
9734 }
9735
9736 if (f != NULL)
9737 break;
9738
9739#if 0 /* GCC code motion plus incomplete dead code elimination
9740 can leave a %hi without a %lo. */
9741 if (pass == 1)
9742 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
9743 _("Unmatched %%hi reloc"));
9744#endif
9745 }
9746 }
9747}
9748
9749/* When generating embedded PIC code we need to use a special
9750 relocation to represent the difference of two symbols in the .text
9751 section (switch tables use a difference of this sort). See
9752 include/coff/mips.h for details. This macro checks whether this
9753 fixup requires the special reloc. */
9754#define SWITCH_TABLE(fixp) \
9755 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 9756 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
9757 && (fixp)->fx_addsy != NULL \
9758 && (fixp)->fx_subsy != NULL \
9759 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
9760 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
9761
9762/* When generating embedded PIC code we must keep all PC relative
9763 relocations, in case the linker has to relax a call. We also need
f6688943
TS
9764 to keep relocations for switch table entries.
9765
9766 We may have combined relocations without symbols in the N32/N64 ABI.
9767 We have to prevent gas from dropping them. */
252b5132 9768
252b5132
RH
9769int
9770mips_force_relocation (fixp)
9771 fixS *fixp;
9772{
9773 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
9774 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
9775 return 1;
9776
f6688943
TS
9777 if (HAVE_NEWABI
9778 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
9779 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
9780 || fixp->fx_r_type == BFD_RELOC_HI16_S
9781 || fixp->fx_r_type == BFD_RELOC_LO16))
9782 return 1;
9783
252b5132
RH
9784 return (mips_pic == EMBEDDED_PIC
9785 && (fixp->fx_pcrel
9786 || SWITCH_TABLE (fixp)
9787 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
9788 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
9789}
9790
9791/* Apply a fixup to the object file. */
9792
9793int
9794md_apply_fix (fixP, valueP)
9795 fixS *fixP;
9796 valueT *valueP;
9797{
9798 unsigned char *buf;
98aa84af
AM
9799 long insn;
9800 valueT value;
252b5132
RH
9801
9802 assert (fixP->fx_size == 4
9803 || fixP->fx_r_type == BFD_RELOC_16
f6688943
TS
9804 || fixP->fx_r_type == BFD_RELOC_32
9805 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
9806 || fixP->fx_r_type == BFD_RELOC_HI16_S
9807 || fixP->fx_r_type == BFD_RELOC_LO16
9808 || fixP->fx_r_type == BFD_RELOC_GPREL16
9809 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
9810 || fixP->fx_r_type == BFD_RELOC_GPREL32
252b5132 9811 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
9812 || fixP->fx_r_type == BFD_RELOC_CTOR
9813 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
9814 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
9815 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
9816 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
9817 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
9818 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
252b5132
RH
9819 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
9820 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
9821
9822 value = *valueP;
9823
9824 /* If we aren't adjusting this fixup to be against the section
9825 symbol, we need to adjust the value. */
9826#ifdef OBJ_ELF
9827 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 9828 {
98aa84af 9829 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16
ea4ff978
L
9830 || ((S_IS_WEAK (fixP->fx_addsy)
9831 || S_IS_EXTERN (fixP->fx_addsy))
9832 && !S_IS_COMMON (fixP->fx_addsy))
98aa84af
AM
9833 || (symbol_used_in_reloc_p (fixP->fx_addsy)
9834 && (((bfd_get_section_flags (stdoutput,
9835 S_GET_SEGMENT (fixP->fx_addsy))
9836 & SEC_LINK_ONCE) != 0)
9837 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
9838 ".gnu.linkonce",
9839 sizeof (".gnu.linkonce") - 1))))
252b5132 9840
98aa84af
AM
9841 {
9842 valueT symval = S_GET_VALUE (fixP->fx_addsy);
9843 value -= symval;
236bd878
RS
9844 if (value != 0
9845 && ! fixP->fx_pcrel
9846 && fixP->fx_r_type != BFD_RELOC_MIPS_GPREL)
98aa84af
AM
9847 {
9848 /* In this case, the bfd_install_relocation routine will
9849 incorrectly add the symbol value back in. We just want
7461da6e 9850 the addend to appear in the object file. */
98aa84af 9851 value -= symval;
7461da6e
RS
9852
9853 /* Make sure the addend is still non-zero. If it became zero
9854 after the last operation, set it to a spurious value and
9855 subtract the same value from the object file's contents. */
9856 if (value == 0)
9857 {
9858 value = 8;
9859
9860 /* The in-place addends for LO16 relocations are signed;
9861 leave the matching HI16 in-place addends as zero. */
9862 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
9863 {
9864 reloc_howto_type *howto;
9865 bfd_vma contents, mask, field;
9866
9867 howto = bfd_reloc_type_lookup (stdoutput,
9868 fixP->fx_r_type);
9869
9870 contents = bfd_get_bits (fixP->fx_frag->fr_literal
9871 + fixP->fx_where,
9872 fixP->fx_size * 8,
9873 target_big_endian);
9874
9875 /* MASK has bits set where the relocation should go.
9876 FIELD is -value, shifted into the appropriate place
9877 for this relocation. */
9878 mask = 1 << (howto->bitsize - 1);
9879 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
9880 field = (-value >> howto->rightshift) << howto->bitpos;
9881
9882 bfd_put_bits ((field & mask) | (contents & ~mask),
9883 fixP->fx_frag->fr_literal + fixP->fx_where,
9884 fixP->fx_size * 8,
9885 target_big_endian);
9886 }
9887 }
98aa84af
AM
9888 }
9889 }
252b5132 9890
bb2d6cd7
GK
9891 /* This code was generated using trial and error and so is
9892 fragile and not trustworthy. If you change it, you should
9893 rerun the elf-rel, elf-rel2, and empic testcases and ensure
9894 they still pass. */
9895 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
9896 {
9897 value += fixP->fx_frag->fr_address + fixP->fx_where;
9898
9899 /* BFD's REL handling, for MIPS, is _very_ weird.
9900 This gives the right results, but it can't possibly
9901 be the way things are supposed to work. */
cb56d3d3
TS
9902 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
9903 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
bb2d6cd7
GK
9904 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
9905 value += fixP->fx_frag->fr_address + fixP->fx_where;
9906 }
9907 }
9908#endif
252b5132
RH
9909
9910 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc */
9911
9912 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
9913 fixP->fx_done = 1;
9914
9915 switch (fixP->fx_r_type)
9916 {
9917 case BFD_RELOC_MIPS_JMP:
9918 case BFD_RELOC_HI16:
9919 case BFD_RELOC_HI16_S:
9920 case BFD_RELOC_MIPS_GPREL:
9921 case BFD_RELOC_MIPS_LITERAL:
9922 case BFD_RELOC_MIPS_CALL16:
9923 case BFD_RELOC_MIPS_GOT16:
9924 case BFD_RELOC_MIPS_GPREL32:
9925 case BFD_RELOC_MIPS_GOT_HI16:
9926 case BFD_RELOC_MIPS_GOT_LO16:
9927 case BFD_RELOC_MIPS_CALL_HI16:
9928 case BFD_RELOC_MIPS_CALL_LO16:
9929 case BFD_RELOC_MIPS16_GPREL:
9930 if (fixP->fx_pcrel)
9931 as_bad_where (fixP->fx_file, fixP->fx_line,
9932 _("Invalid PC relative reloc"));
9933 /* Nothing needed to do. The value comes from the reloc entry */
9934 break;
9935
9936 case BFD_RELOC_MIPS16_JMP:
9937 /* We currently always generate a reloc against a symbol, which
9938 means that we don't want an addend even if the symbol is
9939 defined. */
9940 fixP->fx_addnumber = 0;
9941 break;
9942
9943 case BFD_RELOC_PCREL_HI16_S:
9944 /* The addend for this is tricky if it is internal, so we just
9945 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 9946 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
9947 && !fixP->fx_done
9948 && value != 0)
9949 break;
9950 if (fixP->fx_addsy
9951 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
9952 {
9953 /* For an external symbol adjust by the address to make it
9954 pcrel_offset. We use the address of the RELLO reloc
9955 which follows this one. */
9956 value += (fixP->fx_next->fx_frag->fr_address
9957 + fixP->fx_next->fx_where);
9958 }
e7d556df 9959 value = ((value + 0x8000) >> 16) & 0xffff;
252b5132
RH
9960 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9961 if (target_big_endian)
9962 buf += 2;
9963 md_number_to_chars (buf, value, 2);
9964 break;
9965
9966 case BFD_RELOC_PCREL_LO16:
9967 /* The addend for this is tricky if it is internal, so we just
9968 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 9969 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
9970 && !fixP->fx_done
9971 && value != 0)
9972 break;
9973 if (fixP->fx_addsy
9974 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
9975 value += fixP->fx_frag->fr_address + fixP->fx_where;
9976 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9977 if (target_big_endian)
9978 buf += 2;
9979 md_number_to_chars (buf, value, 2);
9980 break;
9981
9982 case BFD_RELOC_64:
9983 /* This is handled like BFD_RELOC_32, but we output a sign
9984 extended value if we are only 32 bits. */
9985 if (fixP->fx_done
9986 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
9987 {
9988 if (8 <= sizeof (valueT))
9989 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9990 value, 8);
9991 else
9992 {
9993 long w1, w2;
9994 long hiv;
9995
9996 w1 = w2 = fixP->fx_where;
9997 if (target_big_endian)
9998 w1 += 4;
9999 else
10000 w2 += 4;
10001 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10002 if ((value & 0x80000000) != 0)
10003 hiv = 0xffffffff;
10004 else
10005 hiv = 0;
10006 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10007 }
10008 }
10009 break;
10010
056350c6 10011 case BFD_RELOC_RVA:
252b5132
RH
10012 case BFD_RELOC_32:
10013 /* If we are deleting this reloc entry, we must fill in the
10014 value now. This can happen if we have a .word which is not
10015 resolved when it appears but is later defined. We also need
10016 to fill in the value if this is an embedded PIC switch table
10017 entry. */
10018 if (fixP->fx_done
10019 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10020 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10021 value, 4);
10022 break;
10023
10024 case BFD_RELOC_16:
10025 /* If we are deleting this reloc entry, we must fill in the
10026 value now. */
10027 assert (fixP->fx_size == 2);
10028 if (fixP->fx_done)
10029 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10030 value, 2);
10031 break;
10032
10033 case BFD_RELOC_LO16:
10034 /* When handling an embedded PIC switch statement, we can wind
10035 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
10036 if (fixP->fx_done)
10037 {
98aa84af 10038 if (value + 0x8000 > 0xffff)
252b5132
RH
10039 as_bad_where (fixP->fx_file, fixP->fx_line,
10040 _("relocation overflow"));
10041 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10042 if (target_big_endian)
10043 buf += 2;
10044 md_number_to_chars (buf, value, 2);
10045 }
10046 break;
10047
10048 case BFD_RELOC_16_PCREL_S2:
cb56d3d3
TS
10049 if ((value & 0x3) != 0)
10050 as_bad_where (fixP->fx_file, fixP->fx_line,
10051 _("Branch to odd address (%lx)"), (long) value);
10052
10053 /* Fall through. */
10054
10055 case BFD_RELOC_16_PCREL:
252b5132
RH
10056 /*
10057 * We need to save the bits in the instruction since fixup_segment()
10058 * might be deleting the relocation entry (i.e., a branch within
10059 * the current segment).
10060 */
bb2d6cd7
GK
10061 if (!fixP->fx_done && value != 0)
10062 break;
10063 /* If 'value' is zero, the remaining reloc code won't actually
10064 do the store, so it must be done here. This is probably
10065 a bug somewhere. */
10066 if (!fixP->fx_done)
10067 value -= fixP->fx_frag->fr_address + fixP->fx_where;
bdaaa2e1 10068
98aa84af 10069 value = (offsetT) value >> 2;
252b5132
RH
10070
10071 /* update old instruction data */
10072 buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
10073 if (target_big_endian)
10074 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10075 else
10076 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10077
98aa84af 10078 if (value + 0x8000 <= 0xffff)
252b5132
RH
10079 insn |= value & 0xffff;
10080 else
10081 {
10082 /* The branch offset is too large. If this is an
10083 unconditional branch, and we are not generating PIC code,
10084 we can convert it to an absolute jump instruction. */
10085 if (mips_pic == NO_PIC
10086 && fixP->fx_done
10087 && fixP->fx_frag->fr_address >= text_section->vma
10088 && (fixP->fx_frag->fr_address
10089 < text_section->vma + text_section->_raw_size)
10090 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
10091 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
10092 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10093 {
10094 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
10095 insn = 0x0c000000; /* jal */
10096 else
10097 insn = 0x08000000; /* j */
10098 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10099 fixP->fx_done = 0;
10100 fixP->fx_addsy = section_symbol (text_section);
10101 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10102 }
10103 else
10104 {
10105 /* FIXME. It would be possible in principle to handle
10106 conditional branches which overflow. They could be
10107 transformed into a branch around a jump. This would
10108 require setting up variant frags for each different
10109 branch type. The native MIPS assembler attempts to
10110 handle these cases, but it appears to do it
10111 incorrectly. */
10112 as_bad_where (fixP->fx_file, fixP->fx_line,
10113 _("Branch out of range"));
10114 }
10115 }
10116
10117 md_number_to_chars ((char *) buf, (valueT) insn, 4);
10118 break;
10119
10120 case BFD_RELOC_VTABLE_INHERIT:
10121 fixP->fx_done = 0;
10122 if (fixP->fx_addsy
10123 && !S_IS_DEFINED (fixP->fx_addsy)
10124 && !S_IS_WEAK (fixP->fx_addsy))
10125 S_SET_WEAK (fixP->fx_addsy);
10126 break;
10127
10128 case BFD_RELOC_VTABLE_ENTRY:
10129 fixP->fx_done = 0;
10130 break;
10131
10132 default:
10133 internalError ();
10134 }
10135
10136 return 1;
10137}
10138
10139#if 0
10140void
10141printInsn (oc)
10142 unsigned long oc;
10143{
10144 const struct mips_opcode *p;
10145 int treg, sreg, dreg, shamt;
10146 short imm;
10147 const char *args;
10148 int i;
10149
10150 for (i = 0; i < NUMOPCODES; ++i)
10151 {
10152 p = &mips_opcodes[i];
10153 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
10154 {
10155 printf ("%08lx %s\t", oc, p->name);
10156 treg = (oc >> 16) & 0x1f;
10157 sreg = (oc >> 21) & 0x1f;
10158 dreg = (oc >> 11) & 0x1f;
10159 shamt = (oc >> 6) & 0x1f;
10160 imm = oc;
10161 for (args = p->args;; ++args)
10162 {
10163 switch (*args)
10164 {
10165 case '\0':
10166 printf ("\n");
10167 break;
10168
10169 case ',':
10170 case '(':
10171 case ')':
10172 printf ("%c", *args);
10173 continue;
10174
10175 case 'r':
10176 assert (treg == sreg);
10177 printf ("$%d,$%d", treg, sreg);
10178 continue;
10179
10180 case 'd':
10181 case 'G':
10182 printf ("$%d", dreg);
10183 continue;
10184
10185 case 't':
10186 case 'E':
10187 printf ("$%d", treg);
10188 continue;
10189
10190 case 'k':
10191 printf ("0x%x", treg);
10192 continue;
10193
10194 case 'b':
10195 case 's':
10196 printf ("$%d", sreg);
10197 continue;
10198
10199 case 'a':
10200 printf ("0x%08lx", oc & 0x1ffffff);
10201 continue;
10202
10203 case 'i':
10204 case 'j':
10205 case 'o':
10206 case 'u':
10207 printf ("%d", imm);
10208 continue;
10209
10210 case '<':
10211 case '>':
10212 printf ("$%d", shamt);
10213 continue;
10214
10215 default:
10216 internalError ();
10217 }
10218 break;
10219 }
10220 return;
10221 }
10222 }
10223 printf (_("%08lx UNDEFINED\n"), oc);
10224}
10225#endif
10226
10227static symbolS *
10228get_symbol ()
10229{
10230 int c;
10231 char *name;
10232 symbolS *p;
10233
10234 name = input_line_pointer;
10235 c = get_symbol_end ();
10236 p = (symbolS *) symbol_find_or_make (name);
10237 *input_line_pointer = c;
10238 return p;
10239}
10240
10241/* Align the current frag to a given power of two. The MIPS assembler
10242 also automatically adjusts any preceding label. */
10243
10244static void
10245mips_align (to, fill, label)
10246 int to;
10247 int fill;
10248 symbolS *label;
10249{
10250 mips_emit_delays (false);
10251 frag_align (to, fill, 0);
10252 record_alignment (now_seg, to);
10253 if (label != NULL)
10254 {
10255 assert (S_GET_SEGMENT (label) == now_seg);
49309057 10256 symbol_set_frag (label, frag_now);
252b5132
RH
10257 S_SET_VALUE (label, (valueT) frag_now_fix ());
10258 }
10259}
10260
10261/* Align to a given power of two. .align 0 turns off the automatic
10262 alignment used by the data creating pseudo-ops. */
10263
10264static void
10265s_align (x)
43841e91 10266 int x ATTRIBUTE_UNUSED;
252b5132
RH
10267{
10268 register int temp;
10269 register long temp_fill;
10270 long max_alignment = 15;
10271
10272 /*
10273
10274 o Note that the assembler pulls down any immediately preceeding label
10275 to the aligned address.
10276 o It's not documented but auto alignment is reinstated by
10277 a .align pseudo instruction.
10278 o Note also that after auto alignment is turned off the mips assembler
10279 issues an error on attempt to assemble an improperly aligned data item.
10280 We don't.
10281
10282 */
10283
10284 temp = get_absolute_expression ();
10285 if (temp > max_alignment)
10286 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
10287 else if (temp < 0)
10288 {
10289 as_warn (_("Alignment negative: 0 assumed."));
10290 temp = 0;
10291 }
10292 if (*input_line_pointer == ',')
10293 {
10294 input_line_pointer++;
10295 temp_fill = get_absolute_expression ();
10296 }
10297 else
10298 temp_fill = 0;
10299 if (temp)
10300 {
10301 auto_align = 1;
10302 mips_align (temp, (int) temp_fill,
10303 insn_labels != NULL ? insn_labels->label : NULL);
10304 }
10305 else
10306 {
10307 auto_align = 0;
10308 }
10309
10310 demand_empty_rest_of_line ();
10311}
10312
10313void
10314mips_flush_pending_output ()
10315{
10316 mips_emit_delays (false);
10317 mips_clear_insn_labels ();
10318}
10319
10320static void
10321s_change_sec (sec)
10322 int sec;
10323{
10324 segT seg;
10325
10326 /* When generating embedded PIC code, we only use the .text, .lit8,
10327 .sdata and .sbss sections. We change the .data and .rdata
10328 pseudo-ops to use .sdata. */
10329 if (mips_pic == EMBEDDED_PIC
10330 && (sec == 'd' || sec == 'r'))
10331 sec = 's';
10332
10333#ifdef OBJ_ELF
10334 /* The ELF backend needs to know that we are changing sections, so
10335 that .previous works correctly. We could do something like check
10336 for a obj_section_change_hook macro, but that might be confusing
10337 as it would not be appropriate to use it in the section changing
10338 functions in read.c, since obj-elf.c intercepts those. FIXME:
10339 This should be cleaner, somehow. */
10340 obj_elf_section_change_hook ();
10341#endif
10342
10343 mips_emit_delays (false);
10344 switch (sec)
10345 {
10346 case 't':
10347 s_text (0);
10348 break;
10349 case 'd':
10350 s_data (0);
10351 break;
10352 case 'b':
10353 subseg_set (bss_section, (subsegT) get_absolute_expression ());
10354 demand_empty_rest_of_line ();
10355 break;
10356
10357 case 'r':
10358 if (USE_GLOBAL_POINTER_OPT)
10359 {
10360 seg = subseg_new (RDATA_SECTION_NAME,
10361 (subsegT) get_absolute_expression ());
10362 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
10363 {
10364 bfd_set_section_flags (stdoutput, seg,
10365 (SEC_ALLOC
10366 | SEC_LOAD
10367 | SEC_READONLY
10368 | SEC_RELOC
10369 | SEC_DATA));
10370 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 10371 record_alignment (seg, 4);
252b5132
RH
10372 }
10373 demand_empty_rest_of_line ();
10374 }
10375 else
10376 {
10377 as_bad (_("No read only data section in this object file format"));
10378 demand_empty_rest_of_line ();
10379 return;
10380 }
10381 break;
10382
10383 case 's':
10384 if (USE_GLOBAL_POINTER_OPT)
10385 {
10386 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
10387 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
10388 {
10389 bfd_set_section_flags (stdoutput, seg,
10390 SEC_ALLOC | SEC_LOAD | SEC_RELOC
10391 | SEC_DATA);
10392 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 10393 record_alignment (seg, 4);
252b5132
RH
10394 }
10395 demand_empty_rest_of_line ();
10396 break;
10397 }
10398 else
10399 {
10400 as_bad (_("Global pointers not supported; recompile -G 0"));
10401 demand_empty_rest_of_line ();
10402 return;
10403 }
10404 }
10405
10406 auto_align = 1;
10407}
10408
10409void
10410mips_enable_auto_align ()
10411{
10412 auto_align = 1;
10413}
10414
10415static void
10416s_cons (log_size)
10417 int log_size;
10418{
10419 symbolS *label;
10420
10421 label = insn_labels != NULL ? insn_labels->label : NULL;
10422 mips_emit_delays (false);
10423 if (log_size > 0 && auto_align)
10424 mips_align (log_size, 0, label);
10425 mips_clear_insn_labels ();
10426 cons (1 << log_size);
10427}
10428
10429static void
10430s_float_cons (type)
10431 int type;
10432{
10433 symbolS *label;
10434
10435 label = insn_labels != NULL ? insn_labels->label : NULL;
10436
10437 mips_emit_delays (false);
10438
10439 if (auto_align)
49309057
ILT
10440 {
10441 if (type == 'd')
10442 mips_align (3, 0, label);
10443 else
10444 mips_align (2, 0, label);
10445 }
252b5132
RH
10446
10447 mips_clear_insn_labels ();
10448
10449 float_cons (type);
10450}
10451
10452/* Handle .globl. We need to override it because on Irix 5 you are
10453 permitted to say
10454 .globl foo .text
10455 where foo is an undefined symbol, to mean that foo should be
10456 considered to be the address of a function. */
10457
10458static void
10459s_mips_globl (x)
43841e91 10460 int x ATTRIBUTE_UNUSED;
252b5132
RH
10461{
10462 char *name;
10463 int c;
10464 symbolS *symbolP;
10465 flagword flag;
10466
10467 name = input_line_pointer;
10468 c = get_symbol_end ();
10469 symbolP = symbol_find_or_make (name);
10470 *input_line_pointer = c;
10471 SKIP_WHITESPACE ();
10472
10473 /* On Irix 5, every global symbol that is not explicitly labelled as
10474 being a function is apparently labelled as being an object. */
10475 flag = BSF_OBJECT;
10476
10477 if (! is_end_of_line[(unsigned char) *input_line_pointer])
10478 {
10479 char *secname;
10480 asection *sec;
10481
10482 secname = input_line_pointer;
10483 c = get_symbol_end ();
10484 sec = bfd_get_section_by_name (stdoutput, secname);
10485 if (sec == NULL)
10486 as_bad (_("%s: no such section"), secname);
10487 *input_line_pointer = c;
10488
10489 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
10490 flag = BSF_FUNCTION;
10491 }
10492
49309057 10493 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
10494
10495 S_SET_EXTERNAL (symbolP);
10496 demand_empty_rest_of_line ();
10497}
10498
10499static void
10500s_option (x)
43841e91 10501 int x ATTRIBUTE_UNUSED;
252b5132
RH
10502{
10503 char *opt;
10504 char c;
10505
10506 opt = input_line_pointer;
10507 c = get_symbol_end ();
10508
10509 if (*opt == 'O')
10510 {
10511 /* FIXME: What does this mean? */
10512 }
10513 else if (strncmp (opt, "pic", 3) == 0)
10514 {
10515 int i;
10516
10517 i = atoi (opt + 3);
10518 if (i == 0)
10519 mips_pic = NO_PIC;
10520 else if (i == 2)
10521 mips_pic = SVR4_PIC;
10522 else
10523 as_bad (_(".option pic%d not supported"), i);
10524
10525 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
10526 {
10527 if (g_switch_seen && g_switch_value != 0)
10528 as_warn (_("-G may not be used with SVR4 PIC code"));
10529 g_switch_value = 0;
10530 bfd_set_gp_size (stdoutput, 0);
10531 }
10532 }
10533 else
10534 as_warn (_("Unrecognized option \"%s\""), opt);
10535
10536 *input_line_pointer = c;
10537 demand_empty_rest_of_line ();
10538}
10539
10540/* This structure is used to hold a stack of .set values. */
10541
e972090a
NC
10542struct mips_option_stack
10543{
252b5132
RH
10544 struct mips_option_stack *next;
10545 struct mips_set_options options;
10546};
10547
10548static struct mips_option_stack *mips_opts_stack;
10549
10550/* Handle the .set pseudo-op. */
10551
10552static void
10553s_mipsset (x)
43841e91 10554 int x ATTRIBUTE_UNUSED;
252b5132
RH
10555{
10556 char *name = input_line_pointer, ch;
10557
10558 while (!is_end_of_line[(unsigned char) *input_line_pointer])
10559 input_line_pointer++;
10560 ch = *input_line_pointer;
10561 *input_line_pointer = '\0';
10562
10563 if (strcmp (name, "reorder") == 0)
10564 {
10565 if (mips_opts.noreorder && prev_nop_frag != NULL)
10566 {
10567 /* If we still have pending nops, we can discard them. The
10568 usual nop handling will insert any that are still
bdaaa2e1 10569 needed. */
252b5132
RH
10570 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
10571 * (mips_opts.mips16 ? 2 : 4));
10572 prev_nop_frag = NULL;
10573 }
10574 mips_opts.noreorder = 0;
10575 }
10576 else if (strcmp (name, "noreorder") == 0)
10577 {
10578 mips_emit_delays (true);
10579 mips_opts.noreorder = 1;
10580 mips_any_noreorder = 1;
10581 }
10582 else if (strcmp (name, "at") == 0)
10583 {
10584 mips_opts.noat = 0;
10585 }
10586 else if (strcmp (name, "noat") == 0)
10587 {
10588 mips_opts.noat = 1;
10589 }
10590 else if (strcmp (name, "macro") == 0)
10591 {
10592 mips_opts.warn_about_macros = 0;
10593 }
10594 else if (strcmp (name, "nomacro") == 0)
10595 {
10596 if (mips_opts.noreorder == 0)
10597 as_bad (_("`noreorder' must be set before `nomacro'"));
10598 mips_opts.warn_about_macros = 1;
10599 }
10600 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
10601 {
10602 mips_opts.nomove = 0;
10603 }
10604 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
10605 {
10606 mips_opts.nomove = 1;
10607 }
10608 else if (strcmp (name, "bopt") == 0)
10609 {
10610 mips_opts.nobopt = 0;
10611 }
10612 else if (strcmp (name, "nobopt") == 0)
10613 {
10614 mips_opts.nobopt = 1;
10615 }
10616 else if (strcmp (name, "mips16") == 0
10617 || strcmp (name, "MIPS-16") == 0)
10618 mips_opts.mips16 = 1;
10619 else if (strcmp (name, "nomips16") == 0
10620 || strcmp (name, "noMIPS-16") == 0)
10621 mips_opts.mips16 = 0;
10622 else if (strncmp (name, "mips", 4) == 0)
10623 {
10624 int isa;
553178e4
TS
10625 static int saved_mips_gp32;
10626 static int saved_mips_fp32;
e013f690 10627 static enum mips_abi_level saved_mips_abi;
553178e4 10628 static int is_saved;
252b5132
RH
10629
10630 /* Permit the user to change the ISA on the fly. Needless to
10631 say, misuse can cause serious problems. */
10632 isa = atoi (name + 4);
553178e4
TS
10633 switch (isa)
10634 {
10635 case 0:
10636 mips_gp32 = saved_mips_gp32;
10637 mips_fp32 = saved_mips_fp32;
e013f690 10638 mips_abi = saved_mips_abi;
553178e4
TS
10639 is_saved = 0;
10640 break;
10641 case 1:
10642 case 2:
10643 case 32:
10644 if (! is_saved)
10645 {
10646 saved_mips_gp32 = mips_gp32;
10647 saved_mips_fp32 = mips_fp32;
e013f690 10648 saved_mips_abi = mips_abi;
553178e4
TS
10649 }
10650 mips_gp32 = 1;
10651 mips_fp32 = 1;
10652 is_saved = 1;
10653 break;
10654 case 3:
10655 case 4:
10656 case 5:
10657 case 64:
10658 if (! is_saved)
10659 {
10660 saved_mips_gp32 = mips_gp32;
10661 saved_mips_fp32 = mips_fp32;
e013f690 10662 saved_mips_abi = mips_abi;
553178e4
TS
10663 }
10664 mips_gp32 = 0;
10665 mips_fp32 = 0;
e013f690 10666 mips_abi = NO_ABI;
553178e4
TS
10667 is_saved = 1;
10668 break;
10669 default:
10670 as_bad (_("unknown ISA level"));
10671 break;
10672 }
10673
e7af610e
NC
10674 switch (isa)
10675 {
84ea6cf2
NC
10676 case 0: mips_opts.isa = file_mips_isa; break;
10677 case 1: mips_opts.isa = ISA_MIPS1; break;
10678 case 2: mips_opts.isa = ISA_MIPS2; break;
10679 case 3: mips_opts.isa = ISA_MIPS3; break;
84ea6cf2 10680 case 4: mips_opts.isa = ISA_MIPS4; break;
9a41af64 10681 case 5: mips_opts.isa = ISA_MIPS5; break;
84ea6cf2
NC
10682 case 32: mips_opts.isa = ISA_MIPS32; break;
10683 case 64: mips_opts.isa = ISA_MIPS64; break;
10684 default: as_bad (_("unknown ISA level")); break;
e7af610e 10685 }
252b5132
RH
10686 }
10687 else if (strcmp (name, "autoextend") == 0)
10688 mips_opts.noautoextend = 0;
10689 else if (strcmp (name, "noautoextend") == 0)
10690 mips_opts.noautoextend = 1;
10691 else if (strcmp (name, "push") == 0)
10692 {
10693 struct mips_option_stack *s;
10694
10695 s = (struct mips_option_stack *) xmalloc (sizeof *s);
10696 s->next = mips_opts_stack;
10697 s->options = mips_opts;
10698 mips_opts_stack = s;
10699 }
10700 else if (strcmp (name, "pop") == 0)
10701 {
10702 struct mips_option_stack *s;
10703
10704 s = mips_opts_stack;
10705 if (s == NULL)
10706 as_bad (_(".set pop with no .set push"));
10707 else
10708 {
10709 /* If we're changing the reorder mode we need to handle
10710 delay slots correctly. */
10711 if (s->options.noreorder && ! mips_opts.noreorder)
10712 mips_emit_delays (true);
10713 else if (! s->options.noreorder && mips_opts.noreorder)
10714 {
10715 if (prev_nop_frag != NULL)
10716 {
10717 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
10718 * (mips_opts.mips16 ? 2 : 4));
10719 prev_nop_frag = NULL;
10720 }
10721 }
10722
10723 mips_opts = s->options;
10724 mips_opts_stack = s->next;
10725 free (s);
10726 }
10727 }
10728 else
10729 {
10730 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
10731 }
10732 *input_line_pointer = ch;
10733 demand_empty_rest_of_line ();
10734}
10735
10736/* Handle the .abicalls pseudo-op. I believe this is equivalent to
10737 .option pic2. It means to generate SVR4 PIC calls. */
10738
10739static void
10740s_abicalls (ignore)
43841e91 10741 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10742{
10743 mips_pic = SVR4_PIC;
10744 if (USE_GLOBAL_POINTER_OPT)
10745 {
10746 if (g_switch_seen && g_switch_value != 0)
10747 as_warn (_("-G may not be used with SVR4 PIC code"));
10748 g_switch_value = 0;
10749 }
10750 bfd_set_gp_size (stdoutput, 0);
10751 demand_empty_rest_of_line ();
10752}
10753
10754/* Handle the .cpload pseudo-op. This is used when generating SVR4
10755 PIC code. It sets the $gp register for the function based on the
10756 function address, which is in the register named in the argument.
10757 This uses a relocation against _gp_disp, which is handled specially
10758 by the linker. The result is:
10759 lui $gp,%hi(_gp_disp)
10760 addiu $gp,$gp,%lo(_gp_disp)
10761 addu $gp,$gp,.cpload argument
10762 The .cpload argument is normally $25 == $t9. */
10763
10764static void
10765s_cpload (ignore)
43841e91 10766 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10767{
10768 expressionS ex;
10769 int icnt = 0;
10770
10771 /* If we are not generating SVR4 PIC code, .cpload is ignored. */
10772 if (mips_pic != SVR4_PIC)
10773 {
10774 s_ignore (0);
10775 return;
10776 }
10777
10778 /* .cpload should be a in .set noreorder section. */
10779 if (mips_opts.noreorder == 0)
10780 as_warn (_(".cpload not in noreorder section"));
10781
10782 ex.X_op = O_symbol;
10783 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
10784 ex.X_op_symbol = NULL;
10785 ex.X_add_number = 0;
10786
10787 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 10788 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132
RH
10789
10790 macro_build_lui ((char *) NULL, &icnt, &ex, GP);
10791 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
10792 (int) BFD_RELOC_LO16);
10793
10794 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
10795 GP, GP, tc_get_register (0));
10796
10797 demand_empty_rest_of_line ();
10798}
10799
10800/* Handle the .cprestore pseudo-op. This stores $gp into a given
10801 offset from $sp. The offset is remembered, and after making a PIC
10802 call $gp is restored from that location. */
10803
10804static void
10805s_cprestore (ignore)
43841e91 10806 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10807{
10808 expressionS ex;
10809 int icnt = 0;
10810
10811 /* If we are not generating SVR4 PIC code, .cprestore is ignored. */
10812 if (mips_pic != SVR4_PIC)
10813 {
10814 s_ignore (0);
10815 return;
10816 }
10817
10818 mips_cprestore_offset = get_absolute_expression ();
10819
10820 ex.X_op = O_constant;
10821 ex.X_add_symbol = NULL;
10822 ex.X_op_symbol = NULL;
10823 ex.X_add_number = mips_cprestore_offset;
10824
10825 macro_build ((char *) NULL, &icnt, &ex,
ca4e0257 10826 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
252b5132
RH
10827 "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
10828
10829 demand_empty_rest_of_line ();
10830}
10831
10832/* Handle the .gpword pseudo-op. This is used when generating PIC
10833 code. It generates a 32 bit GP relative reloc. */
10834
10835static void
10836s_gpword (ignore)
43841e91 10837 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10838{
10839 symbolS *label;
10840 expressionS ex;
10841 char *p;
10842
10843 /* When not generating PIC code, this is treated as .word. */
10844 if (mips_pic != SVR4_PIC)
10845 {
10846 s_cons (2);
10847 return;
10848 }
10849
10850 label = insn_labels != NULL ? insn_labels->label : NULL;
10851 mips_emit_delays (true);
10852 if (auto_align)
10853 mips_align (2, 0, label);
10854 mips_clear_insn_labels ();
10855
10856 expression (&ex);
10857
10858 if (ex.X_op != O_symbol || ex.X_add_number != 0)
10859 {
10860 as_bad (_("Unsupported use of .gpword"));
10861 ignore_rest_of_line ();
10862 }
10863
10864 p = frag_more (4);
10865 md_number_to_chars (p, (valueT) 0, 4);
10866 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
10867 BFD_RELOC_MIPS_GPREL32);
10868
10869 demand_empty_rest_of_line ();
10870}
10871
10872/* Handle the .cpadd pseudo-op. This is used when dealing with switch
10873 tables in SVR4 PIC code. */
10874
10875static void
10876s_cpadd (ignore)
43841e91 10877 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10878{
10879 int icnt = 0;
10880 int reg;
10881
10882 /* This is ignored when not generating SVR4 PIC code. */
10883 if (mips_pic != SVR4_PIC)
10884 {
10885 s_ignore (0);
10886 return;
10887 }
10888
10889 /* Add $gp to the register named as an argument. */
10890 reg = tc_get_register (0);
10891 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 10892 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
10893 "d,v,t", reg, reg, GP);
10894
bdaaa2e1 10895 demand_empty_rest_of_line ();
252b5132
RH
10896}
10897
10898/* Handle the .insn pseudo-op. This marks instruction labels in
10899 mips16 mode. This permits the linker to handle them specially,
10900 such as generating jalx instructions when needed. We also make
10901 them odd for the duration of the assembly, in order to generate the
10902 right sort of code. We will make them even in the adjust_symtab
10903 routine, while leaving them marked. This is convenient for the
10904 debugger and the disassembler. The linker knows to make them odd
10905 again. */
10906
10907static void
10908s_insn (ignore)
43841e91 10909 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10910{
10911 if (mips_opts.mips16)
10912 mips16_mark_labels ();
10913
10914 demand_empty_rest_of_line ();
10915}
10916
10917/* Handle a .stabn directive. We need these in order to mark a label
10918 as being a mips16 text label correctly. Sometimes the compiler
10919 will emit a label, followed by a .stabn, and then switch sections.
10920 If the label and .stabn are in mips16 mode, then the label is
10921 really a mips16 text label. */
10922
10923static void
10924s_mips_stab (type)
10925 int type;
10926{
10927 if (type == 'n' && mips_opts.mips16)
10928 mips16_mark_labels ();
10929
10930 s_stab (type);
10931}
10932
10933/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
10934 */
10935
10936static void
10937s_mips_weakext (ignore)
43841e91 10938 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
10939{
10940 char *name;
10941 int c;
10942 symbolS *symbolP;
10943 expressionS exp;
10944
10945 name = input_line_pointer;
10946 c = get_symbol_end ();
10947 symbolP = symbol_find_or_make (name);
10948 S_SET_WEAK (symbolP);
10949 *input_line_pointer = c;
10950
10951 SKIP_WHITESPACE ();
10952
10953 if (! is_end_of_line[(unsigned char) *input_line_pointer])
10954 {
10955 if (S_IS_DEFINED (symbolP))
10956 {
10957 as_bad ("Ignoring attempt to redefine symbol `%s'.",
10958 S_GET_NAME (symbolP));
10959 ignore_rest_of_line ();
10960 return;
10961 }
bdaaa2e1 10962
252b5132
RH
10963 if (*input_line_pointer == ',')
10964 {
10965 ++input_line_pointer;
10966 SKIP_WHITESPACE ();
10967 }
bdaaa2e1 10968
252b5132
RH
10969 expression (&exp);
10970 if (exp.X_op != O_symbol)
10971 {
10972 as_bad ("bad .weakext directive");
10973 ignore_rest_of_line();
10974 return;
10975 }
49309057 10976 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
10977 }
10978
10979 demand_empty_rest_of_line ();
10980}
10981
10982/* Parse a register string into a number. Called from the ECOFF code
10983 to parse .frame. The argument is non-zero if this is the frame
10984 register, so that we can record it in mips_frame_reg. */
10985
10986int
10987tc_get_register (frame)
10988 int frame;
10989{
10990 int reg;
10991
10992 SKIP_WHITESPACE ();
10993 if (*input_line_pointer++ != '$')
10994 {
10995 as_warn (_("expected `$'"));
10996 reg = 0;
10997 }
3882b010 10998 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
10999 {
11000 reg = get_absolute_expression ();
11001 if (reg < 0 || reg >= 32)
11002 {
11003 as_warn (_("Bad register number"));
11004 reg = 0;
11005 }
11006 }
11007 else
11008 {
11009 if (strncmp (input_line_pointer, "fp", 2) == 0)
11010 reg = FP;
11011 else if (strncmp (input_line_pointer, "sp", 2) == 0)
11012 reg = SP;
11013 else if (strncmp (input_line_pointer, "gp", 2) == 0)
11014 reg = GP;
11015 else if (strncmp (input_line_pointer, "at", 2) == 0)
11016 reg = AT;
11017 else
11018 {
11019 as_warn (_("Unrecognized register name"));
11020 reg = 0;
11021 }
11022 input_line_pointer += 2;
11023 }
11024 if (frame)
11025 mips_frame_reg = reg != 0 ? reg : SP;
11026 return reg;
11027}
11028
11029valueT
11030md_section_align (seg, addr)
11031 asection *seg;
11032 valueT addr;
11033{
11034 int align = bfd_get_section_alignment (stdoutput, seg);
11035
11036#ifdef OBJ_ELF
11037 /* We don't need to align ELF sections to the full alignment.
11038 However, Irix 5 may prefer that we align them at least to a 16
11039 byte boundary. We don't bother to align the sections if we are
11040 targeted for an embedded system. */
11041 if (strcmp (TARGET_OS, "elf") == 0)
11042 return addr;
11043 if (align > 4)
11044 align = 4;
11045#endif
11046
11047 return ((addr + (1 << align) - 1) & (-1 << align));
11048}
11049
11050/* Utility routine, called from above as well. If called while the
11051 input file is still being read, it's only an approximation. (For
11052 example, a symbol may later become defined which appeared to be
11053 undefined earlier.) */
11054
11055static int
11056nopic_need_relax (sym, before_relaxing)
11057 symbolS *sym;
11058 int before_relaxing;
11059{
11060 if (sym == 0)
11061 return 0;
11062
11063 if (USE_GLOBAL_POINTER_OPT)
11064 {
11065 const char *symname;
11066 int change;
11067
11068 /* Find out whether this symbol can be referenced off the GP
11069 register. It can be if it is smaller than the -G size or if
11070 it is in the .sdata or .sbss section. Certain symbols can
11071 not be referenced off the GP, although it appears as though
11072 they can. */
11073 symname = S_GET_NAME (sym);
11074 if (symname != (const char *) NULL
11075 && (strcmp (symname, "eprol") == 0
11076 || strcmp (symname, "etext") == 0
11077 || strcmp (symname, "_gp") == 0
11078 || strcmp (symname, "edata") == 0
11079 || strcmp (symname, "_fbss") == 0
11080 || strcmp (symname, "_fdata") == 0
11081 || strcmp (symname, "_ftext") == 0
11082 || strcmp (symname, "end") == 0
11083 || strcmp (symname, "_gp_disp") == 0))
11084 change = 1;
11085 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
11086 && (0
11087#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
11088 || (symbol_get_obj (sym)->ecoff_extern_size != 0
11089 && (symbol_get_obj (sym)->ecoff_extern_size
11090 <= g_switch_value))
252b5132
RH
11091#endif
11092 /* We must defer this decision until after the whole
11093 file has been read, since there might be a .extern
11094 after the first use of this symbol. */
11095 || (before_relaxing
11096#ifndef NO_ECOFF_DEBUGGING
49309057 11097 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
11098#endif
11099 && S_GET_VALUE (sym) == 0)
11100 || (S_GET_VALUE (sym) != 0
11101 && S_GET_VALUE (sym) <= g_switch_value)))
11102 change = 0;
11103 else
11104 {
11105 const char *segname;
11106
11107 segname = segment_name (S_GET_SEGMENT (sym));
11108 assert (strcmp (segname, ".lit8") != 0
11109 && strcmp (segname, ".lit4") != 0);
11110 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
11111 && strcmp (segname, ".sbss") != 0
11112 && strncmp (segname, ".sdata.", 7) != 0
11113 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
11114 }
11115 return change;
11116 }
11117 else
11118 /* We are not optimizing for the GP register. */
11119 return 1;
11120}
11121
11122/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
11123 extended opcode. SEC is the section the frag is in. */
11124
11125static int
11126mips16_extended_frag (fragp, sec, stretch)
11127 fragS *fragp;
11128 asection *sec;
11129 long stretch;
11130{
11131 int type;
11132 register const struct mips16_immed_operand *op;
11133 offsetT val;
11134 int mintiny, maxtiny;
11135 segT symsec;
98aa84af 11136 fragS *sym_frag;
252b5132
RH
11137
11138 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
11139 return 0;
11140 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
11141 return 1;
11142
11143 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
11144 op = mips16_immed_operands;
11145 while (op->type != type)
11146 {
11147 ++op;
11148 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
11149 }
11150
11151 if (op->unsp)
11152 {
11153 if (type == '<' || type == '>' || type == '[' || type == ']')
11154 {
11155 mintiny = 1;
11156 maxtiny = 1 << op->nbits;
11157 }
11158 else
11159 {
11160 mintiny = 0;
11161 maxtiny = (1 << op->nbits) - 1;
11162 }
11163 }
11164 else
11165 {
11166 mintiny = - (1 << (op->nbits - 1));
11167 maxtiny = (1 << (op->nbits - 1)) - 1;
11168 }
11169
98aa84af 11170 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 11171 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 11172 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
11173
11174 if (op->pcrel)
11175 {
11176 addressT addr;
11177
11178 /* We won't have the section when we are called from
11179 mips_relax_frag. However, we will always have been called
11180 from md_estimate_size_before_relax first. If this is a
11181 branch to a different section, we mark it as such. If SEC is
11182 NULL, and the frag is not marked, then it must be a branch to
11183 the same section. */
11184 if (sec == NULL)
11185 {
11186 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
11187 return 1;
11188 }
11189 else
11190 {
98aa84af 11191 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
11192 if (symsec != sec)
11193 {
11194 fragp->fr_subtype =
11195 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11196
11197 /* FIXME: We should support this, and let the linker
11198 catch branches and loads that are out of range. */
11199 as_bad_where (fragp->fr_file, fragp->fr_line,
11200 _("unsupported PC relative reference to different section"));
11201
11202 return 1;
11203 }
98aa84af
AM
11204 if (fragp != sym_frag && sym_frag->fr_address == 0)
11205 /* Assume non-extended on the first relaxation pass.
11206 The address we have calculated will be bogus if this is
11207 a forward branch to another frag, as the forward frag
11208 will have fr_address == 0. */
11209 return 0;
252b5132
RH
11210 }
11211
11212 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
11213 the same section. If the relax_marker of the symbol fragment
11214 differs from the relax_marker of this fragment, we have not
11215 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
11216 in STRETCH in order to get a better estimate of the address.
11217 This particularly matters because of the shift bits. */
11218 if (stretch != 0
98aa84af 11219 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
11220 {
11221 fragS *f;
11222
11223 /* Adjust stretch for any alignment frag. Note that if have
11224 been expanding the earlier code, the symbol may be
11225 defined in what appears to be an earlier frag. FIXME:
11226 This doesn't handle the fr_subtype field, which specifies
11227 a maximum number of bytes to skip when doing an
11228 alignment. */
98aa84af 11229 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
11230 {
11231 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
11232 {
11233 if (stretch < 0)
11234 stretch = - ((- stretch)
11235 & ~ ((1 << (int) f->fr_offset) - 1));
11236 else
11237 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
11238 if (stretch == 0)
11239 break;
11240 }
11241 }
11242 if (f != NULL)
11243 val += stretch;
11244 }
11245
11246 addr = fragp->fr_address + fragp->fr_fix;
11247
11248 /* The base address rules are complicated. The base address of
11249 a branch is the following instruction. The base address of a
11250 PC relative load or add is the instruction itself, but if it
11251 is in a delay slot (in which case it can not be extended) use
11252 the address of the instruction whose delay slot it is in. */
11253 if (type == 'p' || type == 'q')
11254 {
11255 addr += 2;
11256
11257 /* If we are currently assuming that this frag should be
11258 extended, then, the current address is two bytes
bdaaa2e1 11259 higher. */
252b5132
RH
11260 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11261 addr += 2;
11262
11263 /* Ignore the low bit in the target, since it will be set
11264 for a text label. */
11265 if ((val & 1) != 0)
11266 --val;
11267 }
11268 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
11269 addr -= 4;
11270 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
11271 addr -= 2;
11272
11273 val -= addr & ~ ((1 << op->shift) - 1);
11274
11275 /* Branch offsets have an implicit 0 in the lowest bit. */
11276 if (type == 'p' || type == 'q')
11277 val /= 2;
11278
11279 /* If any of the shifted bits are set, we must use an extended
11280 opcode. If the address depends on the size of this
11281 instruction, this can lead to a loop, so we arrange to always
11282 use an extended opcode. We only check this when we are in
11283 the main relaxation loop, when SEC is NULL. */
11284 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
11285 {
11286 fragp->fr_subtype =
11287 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11288 return 1;
11289 }
11290
11291 /* If we are about to mark a frag as extended because the value
11292 is precisely maxtiny + 1, then there is a chance of an
11293 infinite loop as in the following code:
11294 la $4,foo
11295 .skip 1020
11296 .align 2
11297 foo:
11298 In this case when the la is extended, foo is 0x3fc bytes
11299 away, so the la can be shrunk, but then foo is 0x400 away, so
11300 the la must be extended. To avoid this loop, we mark the
11301 frag as extended if it was small, and is about to become
11302 extended with a value of maxtiny + 1. */
11303 if (val == ((maxtiny + 1) << op->shift)
11304 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
11305 && sec == NULL)
11306 {
11307 fragp->fr_subtype =
11308 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11309 return 1;
11310 }
11311 }
11312 else if (symsec != absolute_section && sec != NULL)
11313 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
11314
11315 if ((val & ((1 << op->shift) - 1)) != 0
11316 || val < (mintiny << op->shift)
11317 || val > (maxtiny << op->shift))
11318 return 1;
11319 else
11320 return 0;
11321}
11322
11323/* Estimate the size of a frag before relaxing. Unless this is the
11324 mips16, we are not really relaxing here, and the final size is
11325 encoded in the subtype information. For the mips16, we have to
11326 decide whether we are using an extended opcode or not. */
11327
252b5132
RH
11328int
11329md_estimate_size_before_relax (fragp, segtype)
11330 fragS *fragp;
11331 asection *segtype;
11332{
43841e91 11333 int change = 0;
8614eeee 11334 boolean linkonce = false;
252b5132
RH
11335
11336 if (RELAX_MIPS16_P (fragp->fr_subtype))
11337 {
11338 if (mips16_extended_frag (fragp, segtype, 0))
11339 {
11340 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
11341 return 4;
11342 }
11343 else
11344 {
11345 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
11346 return 2;
11347 }
11348 }
11349
11350 if (mips_pic == NO_PIC)
11351 {
11352 change = nopic_need_relax (fragp->fr_symbol, 0);
11353 }
11354 else if (mips_pic == SVR4_PIC)
11355 {
11356 symbolS *sym;
11357 asection *symsec;
11358
11359 sym = fragp->fr_symbol;
11360
11361 /* Handle the case of a symbol equated to another symbol. */
e0890092 11362 while (symbol_equated_reloc_p (sym))
252b5132
RH
11363 {
11364 symbolS *n;
11365
11366 /* It's possible to get a loop here in a badly written
11367 program. */
49309057 11368 n = symbol_get_value_expression (sym)->X_add_symbol;
252b5132
RH
11369 if (n == sym)
11370 break;
11371 sym = n;
11372 }
11373
11374 symsec = S_GET_SEGMENT (sym);
11375
8614eeee
UC
11376 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
11377 if (symsec != segtype && ! S_IS_LOCAL (sym))
beae10d5
KH
11378 {
11379 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
11380 != 0)
11381 linkonce = true;
11382
11383 /* The GNU toolchain uses an extension for ELF: a section
11384 beginning with the magic string .gnu.linkonce is a linkonce
11385 section. */
11386 if (strncmp (segment_name (symsec), ".gnu.linkonce",
11387 sizeof ".gnu.linkonce" - 1) == 0)
11388 linkonce = true;
11389 }
8614eeee 11390
252b5132
RH
11391 /* This must duplicate the test in adjust_reloc_syms. */
11392 change = (symsec != &bfd_und_section
11393 && symsec != &bfd_abs_section
426b0403 11394 && ! bfd_is_com_section (symsec)
8614eeee 11395 && !linkonce
426b0403 11396#ifdef OBJ_ELF
ea4ff978 11397 /* A global or weak symbol is treated as external. */
46bac6de
L
11398 && (OUTPUT_FLAVOR == bfd_target_elf_flavour
11399 && ! (S_IS_EXTERN (sym) || S_IS_WEAK (sym)))
426b0403
AM
11400#endif
11401 );
252b5132
RH
11402 }
11403 else
11404 abort ();
11405
11406 if (change)
11407 {
11408 /* Record the offset to the first reloc in the fr_opcode field.
11409 This lets md_convert_frag and tc_gen_reloc know that the code
11410 must be expanded. */
11411 fragp->fr_opcode = (fragp->fr_literal
11412 + fragp->fr_fix
11413 - RELAX_OLD (fragp->fr_subtype)
11414 + RELAX_RELOC1 (fragp->fr_subtype));
11415 /* FIXME: This really needs as_warn_where. */
11416 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
11417 as_warn (_("AT used after \".set noat\" or macro used after "
11418 "\".set nomacro\""));
11419
11420 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
11421 }
11422
9a41af64 11423 return 0;
252b5132
RH
11424}
11425
11426/* This is called to see whether a reloc against a defined symbol
11427 should be converted into a reloc against a section. Don't adjust
11428 MIPS16 jump relocations, so we don't have to worry about the format
11429 of the offset in the .o file. Don't adjust relocations against
11430 mips16 symbols, so that the linker can find them if it needs to set
11431 up a stub. */
11432
11433int
11434mips_fix_adjustable (fixp)
11435 fixS *fixp;
11436{
ea4ff978
L
11437#ifdef OBJ_ELF
11438 /* Prevent all adjustments to global symbols. */
46bac6de
L
11439 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11440 && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
ea4ff978
L
11441 return 0;
11442#endif
252b5132
RH
11443 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
11444 return 0;
11445 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11446 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
11447 return 0;
11448 if (fixp->fx_addsy == NULL)
11449 return 1;
11450#ifdef OBJ_ELF
11451 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11452 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
11453 && fixp->fx_subsy == NULL)
11454 return 0;
11455#endif
11456 return 1;
11457}
11458
11459/* Translate internal representation of relocation info to BFD target
11460 format. */
11461
11462arelent **
11463tc_gen_reloc (section, fixp)
43841e91 11464 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
11465 fixS *fixp;
11466{
11467 static arelent *retval[4];
11468 arelent *reloc;
11469 bfd_reloc_code_real_type code;
11470
11471 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
11472 retval[1] = NULL;
11473
49309057
ILT
11474 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
11475 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
11476 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
11477
11478 if (mips_pic == EMBEDDED_PIC
11479 && SWITCH_TABLE (fixp))
11480 {
11481 /* For a switch table entry we use a special reloc. The addend
11482 is actually the difference between the reloc address and the
11483 subtrahend. */
11484 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
11485 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
11486 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
11487 fixp->fx_r_type = BFD_RELOC_GPREL32;
11488 }
bb2d6cd7
GK
11489 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11490 reloc->addend = fixp->fx_addnumber;
252b5132
RH
11491 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
11492 {
11493 /* We use a special addend for an internal RELLO reloc. */
49309057 11494 if (symbol_section_p (fixp->fx_addsy))
252b5132
RH
11495 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
11496 else
11497 reloc->addend = fixp->fx_addnumber + reloc->address;
11498 }
11499 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
11500 {
11501 assert (fixp->fx_next != NULL
11502 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
11503 /* We use a special addend for an internal RELHI reloc. The
11504 reloc is relative to the RELLO; adjust the addend
11505 accordingly. */
49309057 11506 if (symbol_section_p (fixp->fx_addsy))
252b5132
RH
11507 reloc->addend = (fixp->fx_next->fx_frag->fr_address
11508 + fixp->fx_next->fx_where
11509 - S_GET_VALUE (fixp->fx_subsy));
11510 else
11511 reloc->addend = (fixp->fx_addnumber
11512 + fixp->fx_next->fx_frag->fr_address
11513 + fixp->fx_next->fx_where);
11514 }
252b5132
RH
11515 else
11516 {
11517 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
11518 /* A gruesome hack which is a result of the gruesome gas reloc
11519 handling. */
11520 reloc->addend = reloc->address;
11521 else
11522 reloc->addend = -reloc->address;
11523 }
11524
11525 /* If this is a variant frag, we may need to adjust the existing
11526 reloc and generate a new one. */
11527 if (fixp->fx_frag->fr_opcode != NULL
11528 && (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
11529 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11530 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
11531 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
11532 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
11533 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
11534 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16))
11535 {
11536 arelent *reloc2;
11537
11538 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
11539
11540 /* If this is not the last reloc in this frag, then we have two
11541 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
11542 CALL_HI16/CALL_LO16, both of which are being replaced. Let
11543 the second one handle all of them. */
11544 if (fixp->fx_next != NULL
11545 && fixp->fx_frag == fixp->fx_next->fx_frag)
11546 {
11547 assert ((fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
11548 && fixp->fx_next->fx_r_type == BFD_RELOC_MIPS_GPREL)
11549 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
11550 && (fixp->fx_next->fx_r_type
11551 == BFD_RELOC_MIPS_GOT_LO16))
11552 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
11553 && (fixp->fx_next->fx_r_type
11554 == BFD_RELOC_MIPS_CALL_LO16)));
11555 retval[0] = NULL;
11556 return retval;
11557 }
11558
11559 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
11560 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
11561 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
11562 retval[2] = NULL;
49309057
ILT
11563 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
11564 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
11565 reloc2->address = (reloc->address
11566 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
11567 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
11568 reloc2->addend = fixp->fx_addnumber;
11569 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
11570 assert (reloc2->howto != NULL);
11571
11572 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
11573 {
11574 arelent *reloc3;
11575
11576 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
11577 retval[3] = NULL;
11578 *reloc3 = *reloc2;
11579 reloc3->address += 4;
11580 }
11581
11582 if (mips_pic == NO_PIC)
11583 {
11584 assert (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL);
11585 fixp->fx_r_type = BFD_RELOC_HI16_S;
11586 }
11587 else if (mips_pic == SVR4_PIC)
11588 {
11589 switch (fixp->fx_r_type)
11590 {
11591 default:
11592 abort ();
11593 case BFD_RELOC_MIPS_GOT16:
11594 break;
11595 case BFD_RELOC_MIPS_CALL16:
11596 case BFD_RELOC_MIPS_GOT_LO16:
11597 case BFD_RELOC_MIPS_CALL_LO16:
11598 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
11599 break;
11600 }
11601 }
11602 else
11603 abort ();
11604 }
11605
11606 /* Since MIPS ELF uses Rel instead of Rela, encode the vtable entry
11607 to be used in the relocation's section offset. */
11608 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
11609 {
11610 reloc->address = reloc->addend;
11611 reloc->addend = 0;
11612 }
11613
11614 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
11615 fixup_segment converted a non-PC relative reloc into a PC
11616 relative reloc. In such a case, we need to convert the reloc
11617 code. */
11618 code = fixp->fx_r_type;
11619 if (fixp->fx_pcrel)
11620 {
11621 switch (code)
11622 {
11623 case BFD_RELOC_8:
11624 code = BFD_RELOC_8_PCREL;
11625 break;
11626 case BFD_RELOC_16:
11627 code = BFD_RELOC_16_PCREL;
11628 break;
11629 case BFD_RELOC_32:
11630 code = BFD_RELOC_32_PCREL;
11631 break;
11632 case BFD_RELOC_64:
11633 code = BFD_RELOC_64_PCREL;
11634 break;
11635 case BFD_RELOC_8_PCREL:
11636 case BFD_RELOC_16_PCREL:
11637 case BFD_RELOC_32_PCREL:
11638 case BFD_RELOC_64_PCREL:
11639 case BFD_RELOC_16_PCREL_S2:
11640 case BFD_RELOC_PCREL_HI16_S:
11641 case BFD_RELOC_PCREL_LO16:
11642 break;
11643 default:
11644 as_bad_where (fixp->fx_file, fixp->fx_line,
11645 _("Cannot make %s relocation PC relative"),
11646 bfd_get_reloc_code_name (code));
11647 }
11648 }
11649
11650 /* To support a PC relative reloc when generating embedded PIC code
11651 for ECOFF, we use a Cygnus extension. We check for that here to
11652 make sure that we don't let such a reloc escape normally. */
bb2d6cd7
GK
11653 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11654 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132
RH
11655 && code == BFD_RELOC_16_PCREL_S2
11656 && mips_pic != EMBEDDED_PIC)
11657 reloc->howto = NULL;
11658 else
11659 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
11660
11661 if (reloc->howto == NULL)
11662 {
11663 as_bad_where (fixp->fx_file, fixp->fx_line,
11664 _("Can not represent %s relocation in this object file format"),
11665 bfd_get_reloc_code_name (code));
11666 retval[0] = NULL;
11667 }
11668
11669 return retval;
11670}
11671
11672/* Relax a machine dependent frag. This returns the amount by which
11673 the current size of the frag should change. */
11674
11675int
11676mips_relax_frag (fragp, stretch)
11677 fragS *fragp;
11678 long stretch;
11679{
11680 if (! RELAX_MIPS16_P (fragp->fr_subtype))
11681 return 0;
11682
11683 if (mips16_extended_frag (fragp, (asection *) NULL, stretch))
11684 {
11685 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11686 return 0;
11687 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
11688 return 2;
11689 }
11690 else
11691 {
11692 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11693 return 0;
11694 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
11695 return -2;
11696 }
11697
11698 return 0;
11699}
11700
11701/* Convert a machine dependent frag. */
11702
11703void
11704md_convert_frag (abfd, asec, fragp)
43841e91 11705 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
11706 segT asec;
11707 fragS *fragp;
11708{
11709 int old, new;
11710 char *fixptr;
11711
11712 if (RELAX_MIPS16_P (fragp->fr_subtype))
11713 {
11714 int type;
11715 register const struct mips16_immed_operand *op;
11716 boolean small, ext;
11717 offsetT val;
11718 bfd_byte *buf;
11719 unsigned long insn;
11720 boolean use_extend;
11721 unsigned short extend;
11722
11723 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
11724 op = mips16_immed_operands;
11725 while (op->type != type)
11726 ++op;
11727
11728 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11729 {
11730 small = false;
11731 ext = true;
11732 }
11733 else
11734 {
11735 small = true;
11736 ext = false;
11737 }
11738
6386f3a7 11739 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
11740 val = S_GET_VALUE (fragp->fr_symbol);
11741 if (op->pcrel)
11742 {
11743 addressT addr;
11744
11745 addr = fragp->fr_address + fragp->fr_fix;
11746
11747 /* The rules for the base address of a PC relative reloc are
11748 complicated; see mips16_extended_frag. */
11749 if (type == 'p' || type == 'q')
11750 {
11751 addr += 2;
11752 if (ext)
11753 addr += 2;
11754 /* Ignore the low bit in the target, since it will be
11755 set for a text label. */
11756 if ((val & 1) != 0)
11757 --val;
11758 }
11759 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
11760 addr -= 4;
11761 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
11762 addr -= 2;
11763
11764 addr &= ~ (addressT) ((1 << op->shift) - 1);
11765 val -= addr;
11766
11767 /* Make sure the section winds up with the alignment we have
11768 assumed. */
11769 if (op->shift > 0)
11770 record_alignment (asec, op->shift);
11771 }
11772
11773 if (ext
11774 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
11775 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
11776 as_warn_where (fragp->fr_file, fragp->fr_line,
11777 _("extended instruction in delay slot"));
11778
11779 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
11780
11781 if (target_big_endian)
11782 insn = bfd_getb16 (buf);
11783 else
11784 insn = bfd_getl16 (buf);
11785
11786 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
11787 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
11788 small, ext, &insn, &use_extend, &extend);
11789
11790 if (use_extend)
11791 {
11792 md_number_to_chars (buf, 0xf000 | extend, 2);
11793 fragp->fr_fix += 2;
11794 buf += 2;
11795 }
11796
11797 md_number_to_chars (buf, insn, 2);
11798 fragp->fr_fix += 2;
11799 buf += 2;
11800 }
11801 else
11802 {
11803 if (fragp->fr_opcode == NULL)
11804 return;
11805
11806 old = RELAX_OLD (fragp->fr_subtype);
11807 new = RELAX_NEW (fragp->fr_subtype);
11808 fixptr = fragp->fr_literal + fragp->fr_fix;
11809
11810 if (new > 0)
11811 memcpy (fixptr - old, fixptr, new);
11812
11813 fragp->fr_fix += new - old;
11814 }
11815}
11816
11817#ifdef OBJ_ELF
11818
11819/* This function is called after the relocs have been generated.
11820 We've been storing mips16 text labels as odd. Here we convert them
11821 back to even for the convenience of the debugger. */
11822
11823void
11824mips_frob_file_after_relocs ()
11825{
11826 asymbol **syms;
11827 unsigned int count, i;
11828
11829 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11830 return;
11831
11832 syms = bfd_get_outsymbols (stdoutput);
11833 count = bfd_get_symcount (stdoutput);
11834 for (i = 0; i < count; i++, syms++)
11835 {
11836 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
11837 && ((*syms)->value & 1) != 0)
11838 {
11839 (*syms)->value &= ~1;
11840 /* If the symbol has an odd size, it was probably computed
11841 incorrectly, so adjust that as well. */
11842 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
11843 ++elf_symbol (*syms)->internal_elf_sym.st_size;
11844 }
11845 }
11846}
11847
11848#endif
11849
11850/* This function is called whenever a label is defined. It is used
11851 when handling branch delays; if a branch has a label, we assume we
11852 can not move it. */
11853
11854void
11855mips_define_label (sym)
11856 symbolS *sym;
11857{
11858 struct insn_label_list *l;
11859
11860 if (free_insn_labels == NULL)
11861 l = (struct insn_label_list *) xmalloc (sizeof *l);
11862 else
11863 {
11864 l = free_insn_labels;
11865 free_insn_labels = l->next;
11866 }
11867
11868 l->label = sym;
11869 l->next = insn_labels;
11870 insn_labels = l;
11871}
11872\f
11873#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11874
11875/* Some special processing for a MIPS ELF file. */
11876
11877void
11878mips_elf_final_processing ()
11879{
11880 /* Write out the register information. */
e013f690 11881 if (! HAVE_NEWABI)
252b5132
RH
11882 {
11883 Elf32_RegInfo s;
11884
11885 s.ri_gprmask = mips_gprmask;
11886 s.ri_cprmask[0] = mips_cprmask[0];
11887 s.ri_cprmask[1] = mips_cprmask[1];
11888 s.ri_cprmask[2] = mips_cprmask[2];
11889 s.ri_cprmask[3] = mips_cprmask[3];
11890 /* The gp_value field is set by the MIPS ELF backend. */
11891
11892 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
11893 ((Elf32_External_RegInfo *)
11894 mips_regmask_frag));
11895 }
11896 else
11897 {
11898 Elf64_Internal_RegInfo s;
11899
11900 s.ri_gprmask = mips_gprmask;
11901 s.ri_pad = 0;
11902 s.ri_cprmask[0] = mips_cprmask[0];
11903 s.ri_cprmask[1] = mips_cprmask[1];
11904 s.ri_cprmask[2] = mips_cprmask[2];
11905 s.ri_cprmask[3] = mips_cprmask[3];
11906 /* The gp_value field is set by the MIPS ELF backend. */
11907
11908 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
11909 ((Elf64_External_RegInfo *)
11910 mips_regmask_frag));
11911 }
11912
11913 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
11914 sort of BFD interface for this. */
11915 if (mips_any_noreorder)
11916 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
11917 if (mips_pic != NO_PIC)
11918 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
11919
bdaaa2e1 11920 /* Set the MIPS ELF ABI flags. */
e013f690 11921 if (mips_abi == NO_ABI)
252b5132 11922 ;
e013f690 11923 else if (mips_abi == O32_ABI)
252b5132 11924 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
e013f690 11925 else if (mips_abi == O64_ABI)
252b5132 11926 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
e013f690 11927 else if (mips_abi == EABI_ABI)
252b5132
RH
11928 {
11929 if (mips_eabi64)
11930 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
11931 else
11932 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
11933 }
e013f690 11934 else if (mips_abi == N32_ABI)
be00bddd
TS
11935 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
11936
11937 /* Nothing to do for "64". */
252b5132
RH
11938
11939 if (mips_32bitmode)
11940 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
11941}
11942
11943#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
11944\f
beae10d5
KH
11945typedef struct proc {
11946 symbolS *isym;
11947 unsigned long reg_mask;
11948 unsigned long reg_offset;
11949 unsigned long fpreg_mask;
11950 unsigned long fpreg_offset;
11951 unsigned long frame_offset;
11952 unsigned long frame_reg;
11953 unsigned long pc_reg;
11954} procS;
252b5132
RH
11955
11956static procS cur_proc;
11957static procS *cur_proc_ptr;
11958static int numprocs;
11959
0a9ef439 11960/* Fill in an rs_align_code fragment. */
a19d8eb0 11961
0a9ef439
RH
11962void
11963mips_handle_align (fragp)
11964 fragS *fragp;
a19d8eb0 11965{
0a9ef439
RH
11966 if (fragp->fr_type != rs_align_code)
11967 return;
11968
11969 if (mips_opts.mips16)
a19d8eb0
CP
11970 {
11971 static const unsigned char be_nop[] = { 0x65, 0x00 };
11972 static const unsigned char le_nop[] = { 0x00, 0x65 };
11973
0a9ef439
RH
11974 int bytes;
11975 char *p;
a19d8eb0 11976
0a9ef439
RH
11977 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
11978 p = fragp->fr_literal + fragp->fr_fix;
11979
11980 if (bytes & 1)
11981 {
11982 *p++ = 0;
11983 fragp->fr_fix += 1;
11984 }
11985
11986 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
11987 fragp->fr_var = 2;
a19d8eb0
CP
11988 }
11989
0a9ef439 11990 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
11991}
11992
252b5132
RH
11993static void
11994md_obj_begin ()
11995{
11996}
11997
11998static void
11999md_obj_end ()
12000{
12001 /* check for premature end, nesting errors, etc */
12002 if (cur_proc_ptr)
9a41af64 12003 as_warn (_("missing .end at end of assembly"));
252b5132
RH
12004}
12005
12006static long
12007get_number ()
12008{
12009 int negative = 0;
12010 long val = 0;
12011
12012 if (*input_line_pointer == '-')
12013 {
12014 ++input_line_pointer;
12015 negative = 1;
12016 }
3882b010 12017 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
12018 as_bad (_("Expected simple number."));
12019 if (input_line_pointer[0] == '0')
12020 {
12021 if (input_line_pointer[1] == 'x')
12022 {
12023 input_line_pointer += 2;
3882b010 12024 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
12025 {
12026 val <<= 4;
12027 val |= hex_value (*input_line_pointer++);
12028 }
12029 return negative ? -val : val;
12030 }
12031 else
12032 {
12033 ++input_line_pointer;
3882b010 12034 while (ISDIGIT (*input_line_pointer))
252b5132
RH
12035 {
12036 val <<= 3;
12037 val |= *input_line_pointer++ - '0';
12038 }
12039 return negative ? -val : val;
12040 }
12041 }
3882b010 12042 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
12043 {
12044 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
12045 *input_line_pointer, *input_line_pointer);
12046 as_warn (_("Invalid number"));
12047 return -1;
12048 }
3882b010 12049 while (ISDIGIT (*input_line_pointer))
252b5132
RH
12050 {
12051 val *= 10;
12052 val += *input_line_pointer++ - '0';
12053 }
12054 return negative ? -val : val;
12055}
12056
12057/* The .file directive; just like the usual .file directive, but there
12058 is an initial number which is the ECOFF file index. */
12059
12060static void
12061s_file (x)
43841e91 12062 int x ATTRIBUTE_UNUSED;
252b5132
RH
12063{
12064 int line;
12065
12066 line = get_number ();
12067 s_app_file (0);
12068}
12069
252b5132
RH
12070/* The .end directive. */
12071
12072static void
12073s_mips_end (x)
43841e91 12074 int x ATTRIBUTE_UNUSED;
252b5132
RH
12075{
12076 symbolS *p;
12077 int maybe_text;
12078
12079 if (!is_end_of_line[(unsigned char) *input_line_pointer])
12080 {
12081 p = get_symbol ();
12082 demand_empty_rest_of_line ();
12083 }
12084 else
12085 p = NULL;
12086
12087#ifdef BFD_ASSEMBLER
12088 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
12089 maybe_text = 1;
12090 else
12091 maybe_text = 0;
12092#else
12093 if (now_seg != data_section && now_seg != bss_section)
12094 maybe_text = 1;
12095 else
12096 maybe_text = 0;
12097#endif
12098
12099 if (!maybe_text)
12100 as_warn (_(".end not in text section"));
12101
12102 if (!cur_proc_ptr)
12103 {
12104 as_warn (_(".end directive without a preceding .ent directive."));
12105 demand_empty_rest_of_line ();
12106 return;
12107 }
12108
12109 if (p != NULL)
12110 {
12111 assert (S_GET_NAME (p));
12112 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
12113 as_warn (_(".end symbol does not match .ent symbol."));
12114 }
12115 else
12116 as_warn (_(".end directive missing or unknown symbol"));
12117
12118#ifdef MIPS_STABS_ELF
12119 {
12120 segT saved_seg = now_seg;
12121 subsegT saved_subseg = now_subseg;
252b5132 12122 valueT dot;
252b5132
RH
12123 expressionS exp;
12124 char *fragp;
12125
12126 dot = frag_now_fix ();
12127
12128#ifdef md_flush_pending_output
12129 md_flush_pending_output ();
12130#endif
12131
12132 assert (pdr_seg);
12133 subseg_set (pdr_seg, 0);
12134
beae10d5 12135 /* Write the symbol. */
252b5132
RH
12136 exp.X_op = O_symbol;
12137 exp.X_add_symbol = p;
12138 exp.X_add_number = 0;
12139 emit_expr (&exp, 4);
12140
beae10d5 12141 fragp = frag_more (7 * 4);
252b5132 12142
beae10d5
KH
12143 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
12144 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
12145 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
12146 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
12147 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
12148 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
12149 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
252b5132
RH
12150
12151 subseg_set (saved_seg, saved_subseg);
12152 }
12153#endif
12154
12155 cur_proc_ptr = NULL;
12156}
12157
12158/* The .aent and .ent directives. */
12159
12160static void
12161s_mips_ent (aent)
12162 int aent;
12163{
12164 int number = 0;
12165 symbolS *symbolP;
12166 int maybe_text;
12167
12168 symbolP = get_symbol ();
12169 if (*input_line_pointer == ',')
12170 input_line_pointer++;
12171 SKIP_WHITESPACE ();
3882b010 12172 if (ISDIGIT (*input_line_pointer)
d9a62219 12173 || *input_line_pointer == '-')
252b5132
RH
12174 number = get_number ();
12175
12176#ifdef BFD_ASSEMBLER
12177 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
12178 maybe_text = 1;
12179 else
12180 maybe_text = 0;
12181#else
12182 if (now_seg != data_section && now_seg != bss_section)
12183 maybe_text = 1;
12184 else
12185 maybe_text = 0;
12186#endif
12187
12188 if (!maybe_text)
12189 as_warn (_(".ent or .aent not in text section."));
12190
12191 if (!aent && cur_proc_ptr)
9a41af64 12192 as_warn (_("missing .end"));
252b5132
RH
12193
12194 if (!aent)
12195 {
12196 cur_proc_ptr = &cur_proc;
12197 memset (cur_proc_ptr, '\0', sizeof (procS));
12198
12199 cur_proc_ptr->isym = symbolP;
12200
49309057 12201 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132
RH
12202
12203 numprocs++;
12204 }
12205
12206 demand_empty_rest_of_line ();
12207}
12208
12209/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 12210 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 12211 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 12212 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
12213 symbol table (in the mdebug section). */
12214
12215static void
12216s_mips_frame (ignore)
2b3c5a5d 12217 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12218{
12219#ifdef MIPS_STABS_ELF
12220
12221 long val;
12222
beae10d5 12223 if (cur_proc_ptr == (procS *) NULL)
252b5132
RH
12224 {
12225 as_warn (_(".frame outside of .ent"));
12226 demand_empty_rest_of_line ();
12227 return;
12228 }
12229
12230 cur_proc_ptr->frame_reg = tc_get_register (1);
12231
12232 SKIP_WHITESPACE ();
12233 if (*input_line_pointer++ != ','
12234 || get_absolute_expression_and_terminator (&val) != ',')
12235 {
12236 as_warn (_("Bad .frame directive"));
12237 --input_line_pointer;
12238 demand_empty_rest_of_line ();
12239 return;
12240 }
12241
12242 cur_proc_ptr->frame_offset = val;
12243 cur_proc_ptr->pc_reg = tc_get_register (0);
12244
12245 demand_empty_rest_of_line ();
12246#else
12247 s_ignore (ignore);
12248#endif /* MIPS_STABS_ELF */
12249}
12250
bdaaa2e1
KH
12251/* The .fmask and .mask directives. If the mdebug section is present
12252 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 12253 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 12254 information correctly. We can't use the ecoff routines because they
252b5132
RH
12255 make reference to the ecoff symbol table (in the mdebug section). */
12256
12257static void
12258s_mips_mask (reg_type)
12259 char reg_type;
12260{
12261#ifdef MIPS_STABS_ELF
12262 long mask, off;
bdaaa2e1 12263
252b5132
RH
12264 if (cur_proc_ptr == (procS *) NULL)
12265 {
12266 as_warn (_(".mask/.fmask outside of .ent"));
12267 demand_empty_rest_of_line ();
12268 return;
12269 }
12270
12271 if (get_absolute_expression_and_terminator (&mask) != ',')
12272 {
12273 as_warn (_("Bad .mask/.fmask directive"));
12274 --input_line_pointer;
12275 demand_empty_rest_of_line ();
12276 return;
12277 }
12278
12279 off = get_absolute_expression ();
12280
12281 if (reg_type == 'F')
12282 {
12283 cur_proc_ptr->fpreg_mask = mask;
12284 cur_proc_ptr->fpreg_offset = off;
12285 }
12286 else
12287 {
12288 cur_proc_ptr->reg_mask = mask;
12289 cur_proc_ptr->reg_offset = off;
12290 }
12291
12292 demand_empty_rest_of_line ();
12293#else
12294 s_ignore (reg_type);
12295#endif /* MIPS_STABS_ELF */
12296}
12297
12298/* The .loc directive. */
12299
12300#if 0
12301static void
12302s_loc (x)
12303 int x;
12304{
12305 symbolS *symbolP;
12306 int lineno;
12307 int addroff;
12308
12309 assert (now_seg == text_section);
12310
12311 lineno = get_number ();
12312 addroff = frag_now_fix ();
12313
12314 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
12315 S_SET_TYPE (symbolP, N_SLINE);
12316 S_SET_OTHER (symbolP, 0);
12317 S_SET_DESC (symbolP, lineno);
12318 symbolP->sy_segment = now_seg;
12319}
12320#endif
e7af610e
NC
12321
12322/* CPU name/ISA/number mapping table.
12323
12324 Entries are grouped by type. The first matching CPU or ISA entry
12325 gets chosen by CPU or ISA, so it should be the 'canonical' name
12326 for that type. Entries after that within the type are sorted
12327 alphabetically.
12328
12329 Case is ignored in comparison, so put the canonical entry in the
12330 appropriate case but everything else in lower case to ease eye pain. */
e972090a
NC
12331static const struct mips_cpu_info mips_cpu_info_table[] =
12332{
e7af610e
NC
12333 /* MIPS1 ISA */
12334 { "MIPS1", 1, ISA_MIPS1, CPU_R3000, },
12335 { "mips", 1, ISA_MIPS1, CPU_R3000, },
12336
12337 /* MIPS2 ISA */
12338 { "MIPS2", 1, ISA_MIPS2, CPU_R6000, },
12339
12340 /* MIPS3 ISA */
12341 { "MIPS3", 1, ISA_MIPS3, CPU_R4000, },
12342
12343 /* MIPS4 ISA */
12344 { "MIPS4", 1, ISA_MIPS4, CPU_R8000, },
12345
84ea6cf2
NC
12346 /* MIPS5 ISA */
12347 { "MIPS5", 1, ISA_MIPS5, CPU_MIPS5, },
12348 { "Generic-MIPS5", 0, ISA_MIPS5, CPU_MIPS5, },
12349
e7af610e
NC
12350 /* MIPS32 ISA */
12351 { "MIPS32", 1, ISA_MIPS32, CPU_MIPS32, },
3c02b2ab 12352 { "mipsisa32", 0, ISA_MIPS32, CPU_MIPS32, },
e7af610e 12353 { "Generic-MIPS32", 0, ISA_MIPS32, CPU_MIPS32, },
3c02b2ab
EC
12354 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
12355 { "4km", 0, ISA_MIPS32, CPU_MIPS32, },
12356 { "4kp", 0, ISA_MIPS32, CPU_MIPS32, },
12357
12358 /* For historical reasons. */
12359 { "MIPS64", 1, ISA_MIPS3, CPU_R4000, },
e7af610e 12360
84ea6cf2 12361 /* MIPS64 ISA */
3c02b2ab 12362 { "mipsisa64", 1, ISA_MIPS64, CPU_MIPS64, },
84ea6cf2 12363 { "Generic-MIPS64", 0, ISA_MIPS64, CPU_MIPS64, },
3c02b2ab
EC
12364 { "5kc", 0, ISA_MIPS64, CPU_MIPS64, },
12365 { "20kc", 0, ISA_MIPS64, CPU_MIPS64, },
e7af610e
NC
12366
12367 /* R2000 CPU */
12368 { "R2000", 0, ISA_MIPS1, CPU_R2000, },
12369 { "2000", 0, ISA_MIPS1, CPU_R2000, },
12370 { "2k", 0, ISA_MIPS1, CPU_R2000, },
12371 { "r2k", 0, ISA_MIPS1, CPU_R2000, },
12372
12373 /* R3000 CPU */
12374 { "R3000", 0, ISA_MIPS1, CPU_R3000, },
12375 { "3000", 0, ISA_MIPS1, CPU_R3000, },
12376 { "3k", 0, ISA_MIPS1, CPU_R3000, },
12377 { "r3k", 0, ISA_MIPS1, CPU_R3000, },
12378
12379 /* TX3900 CPU */
12380 { "R3900", 0, ISA_MIPS1, CPU_R3900, },
12381 { "3900", 0, ISA_MIPS1, CPU_R3900, },
e972090a 12382 { "mipstx39", 0, ISA_MIPS1, CPU_R3900, },
e7af610e
NC
12383
12384 /* R4000 CPU */
12385 { "R4000", 0, ISA_MIPS3, CPU_R4000, },
12386 { "4000", 0, ISA_MIPS3, CPU_R4000, },
12387 { "4k", 0, ISA_MIPS3, CPU_R4000, }, /* beware */
12388 { "r4k", 0, ISA_MIPS3, CPU_R4000, },
12389
12390 /* R4010 CPU */
12391 { "R4010", 0, ISA_MIPS2, CPU_R4010, },
12392 { "4010", 0, ISA_MIPS2, CPU_R4010, },
12393
12394 /* R4400 CPU */
12395 { "R4400", 0, ISA_MIPS3, CPU_R4400, },
12396 { "4400", 0, ISA_MIPS3, CPU_R4400, },
12397
12398 /* R4600 CPU */
12399 { "R4600", 0, ISA_MIPS3, CPU_R4600, },
12400 { "4600", 0, ISA_MIPS3, CPU_R4600, },
12401 { "mips64orion", 0, ISA_MIPS3, CPU_R4600, },
12402 { "orion", 0, ISA_MIPS3, CPU_R4600, },
12403
12404 /* R4650 CPU */
12405 { "R4650", 0, ISA_MIPS3, CPU_R4650, },
12406 { "4650", 0, ISA_MIPS3, CPU_R4650, },
12407
12408 /* R6000 CPU */
12409 { "R6000", 0, ISA_MIPS2, CPU_R6000, },
12410 { "6000", 0, ISA_MIPS2, CPU_R6000, },
12411 { "6k", 0, ISA_MIPS2, CPU_R6000, },
12412 { "r6k", 0, ISA_MIPS2, CPU_R6000, },
12413
12414 /* R8000 CPU */
12415 { "R8000", 0, ISA_MIPS4, CPU_R8000, },
12416 { "8000", 0, ISA_MIPS4, CPU_R8000, },
12417 { "8k", 0, ISA_MIPS4, CPU_R8000, },
12418 { "r8k", 0, ISA_MIPS4, CPU_R8000, },
12419
12420 /* R10000 CPU */
12421 { "R10000", 0, ISA_MIPS4, CPU_R10000, },
12422 { "10000", 0, ISA_MIPS4, CPU_R10000, },
12423 { "10k", 0, ISA_MIPS4, CPU_R10000, },
12424 { "r10k", 0, ISA_MIPS4, CPU_R10000, },
12425
d1cf510e
NC
12426 /* R12000 CPU */
12427 { "R12000", 0, ISA_MIPS4, CPU_R12000, },
12428 { "12000", 0, ISA_MIPS4, CPU_R12000, },
12429 { "12k", 0, ISA_MIPS4, CPU_R12000, },
12430 { "r12k", 0, ISA_MIPS4, CPU_R12000, },
12431
e7af610e
NC
12432 /* VR4100 CPU */
12433 { "VR4100", 0, ISA_MIPS3, CPU_VR4100, },
12434 { "4100", 0, ISA_MIPS3, CPU_VR4100, },
12435 { "mips64vr4100", 0, ISA_MIPS3, CPU_VR4100, },
12436 { "r4100", 0, ISA_MIPS3, CPU_VR4100, },
12437
12438 /* VR4111 CPU */
12439 { "VR4111", 0, ISA_MIPS3, CPU_R4111, },
12440 { "4111", 0, ISA_MIPS3, CPU_R4111, },
12441 { "mips64vr4111", 0, ISA_MIPS3, CPU_R4111, },
12442 { "r4111", 0, ISA_MIPS3, CPU_R4111, },
12443
12444 /* VR4300 CPU */
12445 { "VR4300", 0, ISA_MIPS3, CPU_R4300, },
12446 { "4300", 0, ISA_MIPS3, CPU_R4300, },
12447 { "mips64vr4300", 0, ISA_MIPS3, CPU_R4300, },
12448 { "r4300", 0, ISA_MIPS3, CPU_R4300, },
12449
12450 /* VR5000 CPU */
12451 { "VR5000", 0, ISA_MIPS4, CPU_R5000, },
12452 { "5000", 0, ISA_MIPS4, CPU_R5000, },
12453 { "5k", 0, ISA_MIPS4, CPU_R5000, },
12454 { "mips64vr5000", 0, ISA_MIPS4, CPU_R5000, },
12455 { "r5000", 0, ISA_MIPS4, CPU_R5000, },
12456 { "r5200", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 12457 { "rm5200", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 12458 { "r5230", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 12459 { "rm5230", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 12460 { "r5231", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 12461 { "rm5231", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 12462 { "r5261", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 12463 { "rm5261", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 12464 { "r5721", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 12465 { "rm5721", 0, ISA_MIPS4, CPU_R5000, },
e7af610e
NC
12466 { "r5k", 0, ISA_MIPS4, CPU_R5000, },
12467 { "r7000", 0, ISA_MIPS4, CPU_R5000, },
12468
c6c98b38
NC
12469 /* SiByte SB-1 CPU */
12470 { "SB-1", 0, ISA_MIPS64, CPU_SB1, },
e972090a 12471 { "sb-1250", 0, ISA_MIPS64, CPU_SB1, },
c6c98b38
NC
12472 { "sb1", 0, ISA_MIPS64, CPU_SB1, },
12473 { "sb1250", 0, ISA_MIPS64, CPU_SB1, },
12474
beae10d5 12475 /* End marker. */
e7af610e
NC
12476 { NULL, 0, 0, 0, },
12477};
12478
12479static const struct mips_cpu_info *
12480mips_cpu_info_from_name (name)
12481 const char *name;
12482{
12483 int i;
12484
12485 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
beae10d5 12486 if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
e7af610e
NC
12487 return (&mips_cpu_info_table[i]);
12488
e972090a 12489 return NULL;
e7af610e
NC
12490}
12491
12492static const struct mips_cpu_info *
12493mips_cpu_info_from_isa (isa)
12494 int isa;
12495{
12496 int i;
12497
12498 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
12499 if (mips_cpu_info_table[i].is_isa
12500 && isa == mips_cpu_info_table[i].isa)
12501 return (&mips_cpu_info_table[i]);
12502
e972090a 12503 return NULL;
e7af610e
NC
12504}
12505
12506static const struct mips_cpu_info *
12507mips_cpu_info_from_cpu (cpu)
12508 int cpu;
12509{
12510 int i;
12511
12512 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
12513 if (!mips_cpu_info_table[i].is_isa
12514 && cpu == mips_cpu_info_table[i].cpu)
12515 return (&mips_cpu_info_table[i]);
12516
e972090a 12517 return NULL;
e7af610e 12518}
This page took 0.803923 seconds and 4 git commands to generate.