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