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