]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-mips.c
* sparc-opc.c: Fix opcode values for fpack16, and fpackfix. Fix
[thirdparty/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
3d3c5039 1/* tc-mips.c -- assemble code for a MIPS chip.
fb251650 2 Copyright (C) 1993, 1995, 1996 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"
b2b8c24e 28#include "libiberty.h"
3d3c5039
ILT
29
30#include <ctype.h>
31
1dc1e798 32#ifdef USE_STDARG
3d3c5039 33#include <stdarg.h>
1dc1e798
KR
34#endif
35#ifdef USE_VARARGS
3d3c5039 36#include <varargs.h>
1dc1e798 37#endif
3d3c5039 38
918692a5 39#include "opcode/mips.h"
3d3c5039 40
739708fa
KR
41#ifdef OBJ_MAYBE_ELF
42/* Clean up namespace so we can include obj-elf.h too. */
43static int mips_output_flavor () { return OUTPUT_FLAVOR; }
44#undef OBJ_PROCESS_STAB
45#undef OUTPUT_FLAVOR
46#undef S_GET_ALIGN
47#undef S_GET_SIZE
48#undef S_SET_ALIGN
49#undef S_SET_SIZE
50#undef TARGET_SYMBOL_FIELDS
51#undef obj_frob_file
52#undef obj_frob_symbol
53#undef obj_pop_insert
54#undef obj_sec_sym_ok_for_reloc
55
56#include "obj-elf.h"
57/* Fix any of them that we actually care about. */
58#undef OUTPUT_FLAVOR
59#define OUTPUT_FLAVOR mips_output_flavor()
60#endif
61
62#if defined (OBJ_ELF)
f2a663d3 63#include "elf/mips.h"
1dc1e798 64#endif
f2a663d3 65
739708fa 66#ifndef ECOFF_DEBUGGING
c625fc23 67#define NO_ECOFF_DEBUGGING
739708fa
KR
68#define ECOFF_DEBUGGING 0
69#endif
70
22ba90ce
ILT
71#include "ecoff.h"
72
a8aed9dd 73#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
f2a663d3 74static char *mips_regmask_frag;
a8aed9dd 75#endif
f2a663d3 76
3d3c5039 77#define AT 1
9226253a 78#define PIC_CALL_REG 25
b2b8c24e
ILT
79#define KT0 26
80#define KT1 27
670a50eb 81#define GP 28
9226253a
ILT
82#define SP 29
83#define FP 30
3d3c5039
ILT
84#define RA 31
85
1dc1e798 86extern int target_big_endian;
88225433 87
7f9880e5
ILT
88/* 1 is we should use the 64 bit MIPS ELF ABI, 0 if we should use the
89 32 bit ABI. This has no meaning for ECOFF. */
90static int mips_64;
91
04cb3372 92/* The default target format to use. */
1dc1e798
KR
93const char *
94mips_target_format ()
95{
96 switch (OUTPUT_FLAVOR)
97 {
98 case bfd_target_aout_flavour:
99 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
100 case bfd_target_ecoff_flavour:
101 return target_big_endian ? "ecoff-bigmips" : "ecoff-littlemips";
102 case bfd_target_elf_flavour:
7f9880e5
ILT
103 return (target_big_endian
104 ? (mips_64 ? "elf64-bigmips" : "elf32-bigmips")
105 : (mips_64 ? "elf64-littlemips" : "elf32-littlemips"));
1dc1e798
KR
106 default:
107 abort ();
108 }
109}
04cb3372 110
d2c71068 111/* The name of the readonly data section. */
1dc1e798
KR
112#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
113 ? ".data" \
114 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
115 ? ".rdata" \
116 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117 ? ".rodata" \
118 : (abort (), ""))
d2c71068 119
1aa6938e
ILT
120/* These variables are filled in with the masks of registers used.
121 The object format code reads them and puts them in the appropriate
122 place. */
123unsigned long mips_gprmask;
124unsigned long mips_cprmask[4];
125
1051c97f
ILT
126/* MIPS ISA (Instruction Set Architecture) level (may be changed
127 temporarily using .set mipsN). */
8358c818
ILT
128static int mips_isa = -1;
129
1051c97f
ILT
130/* MIPS ISA we are using for this output file. */
131static int file_mips_isa;
132
8c63448a 133/* The CPU type as a number: 2000, 3000, 4000, 4400, etc. */
4bb0cc41 134static int mips_cpu = -1;
8c63448a 135
b2b8c24e
ILT
136/* Whether the 4650 instructions (mad/madu) are permitted. */
137static int mips_4650 = -1;
138
e532b44c
ILT
139/* Whether the 4010 instructions are permitted. */
140static int mips_4010 = -1;
141
c625fc23
JSC
142/* Whether the 4100 MADD16 and DMADD16 are permitted. */
143static int mips_4100 = -1;
144
e532b44c
ILT
145/* Whether the processor uses hardware interlocks, and thus does not
146 require nops to be inserted. */
147static int interlocks = -1;
148
344a8d61
JSC
149/* As with "interlocks" this is used by hardware that has FP
150 (co-processor) interlocks. */
151static int cop_interlocks = -1;
152
d9aba805
ILT
153/* MIPS PIC level. */
154
155enum mips_pic_level
156{
157 /* Do not generate PIC code. */
158 NO_PIC,
159
160 /* Generate PIC code as in Irix 4. This is not implemented, and I'm
161 not sure what it is supposed to do. */
162 IRIX4_PIC,
163
164 /* Generate PIC code as in the SVR4 MIPS ABI. */
165 SVR4_PIC,
166
167 /* Generate PIC code without using a global offset table: the data
168 segment has a maximum size of 64K, all data references are off
169 the $gp register, and all text references are PC relative. This
170 is used on some embedded systems. */
171 EMBEDDED_PIC
172};
173
174static enum mips_pic_level mips_pic;
9226253a 175
fb251650
ILT
176/* 1 if we should generate 32 bit offsets from the GP register in
177 SVR4_PIC mode. Currently has no meaning in other modes. */
178static int mips_big_got;
179
8ea7f4e8
ILT
180/* 1 if trap instructions should used for overflow rather than break
181 instructions. */
182static int mips_trap;
183
3d3c5039
ILT
184static int mips_warn_about_macros;
185static int mips_noreorder;
0dd2d296 186static int mips_any_noreorder;
3d3c5039
ILT
187static int mips_nomove;
188static int mips_noat;
189static int mips_nobopt;
190
670a50eb
ILT
191/* The size of the small data section. */
192static int g_switch_value = 8;
42562568
ILT
193/* Whether the -G option was used. */
194static int g_switch_seen = 0;
670a50eb 195
3d3c5039
ILT
196#define N_RMASK 0xc4
197#define N_VFP 0xd4
198
d8a1c247
KR
199/* If we can determine in advance that GP optimization won't be
200 possible, we can skip the relaxation stuff that tries to produce
201 GP-relative references. This makes delay slot optimization work
202 better.
203
204 This function can only provide a guess, but it seems to work for
205 gcc output. If it guesses wrong, the only loss should be in
206 efficiency; it shouldn't introduce any bugs.
207
208 I don't know if a fix is needed for the SVR4_PIC mode. I've only
209 fixed it for the non-PIC mode. KR 95/04/07 */
210static int nopic_need_relax PARAMS ((symbolS *));
211
3d3c5039
ILT
212/* handle of the OPCODE hash table */
213static struct hash_control *op_hash = NULL;
214
215/* This array holds the chars that always start a comment. If the
216 pre-processor is disabled, these aren't very useful */
217const char comment_chars[] = "#";
218
219/* This array holds the chars that only start a comment at the beginning of
220 a line. If the line seems to have the form '# 123 filename'
221 .line and .file directives will appear in the pre-processed output */
222/* Note that input_file.c hand checks for '#' at the beginning of the
223 first line of the input file. This is because the compiler outputs
224 #NO_APP at the beginning of its output. */
225/* Also note that C style comments are always supported. */
226const char line_comment_chars[] = "#";
227
228/* This array holds machine specific line separator characters. */
229const char line_separator_chars[] = "";
230
231/* Chars that can be used to separate mant from exp in floating point nums */
232const char EXP_CHARS[] = "eE";
233
234/* Chars that mean this number is a floating point constant */
235/* As in 0f12.456 */
236/* or 0d1.2345e12 */
237const char FLT_CHARS[] = "rRsSfFdDxXpP";
238
239/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
240 changed in read.c . Ideally it shouldn't have to know about it at all,
241 but nothing is ideal around here.
242 */
243
670a50eb 244static char *insn_error;
3d3c5039 245
22ba90ce 246static int byte_order;
3d3c5039
ILT
247
248static int auto_align = 1;
becfe05e
ILT
249
250/* Symbol labelling the current insn. */
251static symbolS *insn_label;
252
9226253a
ILT
253/* When outputting SVR4 PIC code, the assembler needs to know the
254 offset in the stack frame from which to restore the $gp register.
255 This is set by the .cprestore pseudo-op, and saved in this
256 variable. */
0dd2d296
ILT
257static offsetT mips_cprestore_offset = -1;
258
259/* This is the register which holds the stack frame, as set by the
260 .frame pseudo-op. This is needed to implement .cprestore. */
261static int mips_frame_reg = SP;
9226253a 262
becfe05e
ILT
263/* To output NOP instructions correctly, we need to keep information
264 about the previous two instructions. */
265
0aa07269
ILT
266/* Whether we are optimizing. The default value of 2 means to remove
267 unneeded NOPs and swap branch instructions when possible. A value
268 of 1 means to not swap branches. A value of 0 means to always
269 insert NOPs. */
270static int mips_optimize = 2;
4e95866e 271
22ba90ce
ILT
272/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
273 equivalent to seeing no -g option at all. */
274static int mips_debug = 0;
275
becfe05e
ILT
276/* The previous instruction. */
277static struct mips_cl_insn prev_insn;
278
279/* The instruction before prev_insn. */
280static struct mips_cl_insn prev_prev_insn;
281
282/* If we don't want information for prev_insn or prev_prev_insn, we
283 point the insn_mo field at this dummy integer. */
284static const struct mips_opcode dummy_opcode = { 0 };
285
286/* Non-zero if prev_insn is valid. */
287static int prev_insn_valid;
288
289/* The frag for the previous instruction. */
290static struct frag *prev_insn_frag;
291
292/* The offset into prev_insn_frag for the previous instruction. */
293static long prev_insn_where;
294
295/* The reloc for the previous instruction, if any. */
296static fixS *prev_insn_fixp;
297
298/* Non-zero if the previous instruction was in a delay slot. */
299static int prev_insn_is_delay_slot;
4e95866e
ILT
300
301/* Non-zero if the previous instruction was in a .set noreorder. */
302static int prev_insn_unreordered;
303
304/* Non-zero if the previous previous instruction was in a .set
305 noreorder. */
306static int prev_prev_insn_unreordered;
867a58b3
ILT
307
308/* For ECOFF and ELF, relocations against symbols are done in two
309 parts, with a HI relocation and a LO relocation. Each relocation
310 has only 16 bits of space to store an addend. This means that in
311 order for the linker to handle carries correctly, it must be able
312 to locate both the HI and the LO relocation. This means that the
313 relocations must appear in order in the relocation table.
314
315 In order to implement this, we keep track of each unmatched HI
316 relocation. We then sort them so that they immediately precede the
317 corresponding LO relocation. */
318
319struct mips_hi_fixup
320{
321 /* Next HI fixup. */
322 struct mips_hi_fixup *next;
323 /* This fixup. */
324 fixS *fixp;
325 /* The section this fixup is in. */
326 segT seg;
327};
328
329/* The list of unmatched HI relocs. */
330
331static struct mips_hi_fixup *mips_hi_fixup_list;
3d3c5039 332\f
0dd2d296
ILT
333/* Since the MIPS does not have multiple forms of PC relative
334 instructions, we do not have to do relaxing as is done on other
335 platforms. However, we do have to handle GP relative addressing
336 correctly, which turns out to be a similar problem.
337
338 Every macro that refers to a symbol can occur in (at least) two
339 forms, one with GP relative addressing and one without. For
340 example, loading a global variable into a register generally uses
23dc1ae3 341 a macro instruction like this:
0dd2d296
ILT
342 lw $4,i
343 If i can be addressed off the GP register (this is true if it is in
344 the .sbss or .sdata section, or if it is known to be smaller than
345 the -G argument) this will generate the following instruction:
346 lw $4,i($gp)
347 This instruction will use a GPREL reloc. If i can not be addressed
348 off the GP register, the following instruction sequence will be used:
349 lui $at,i
350 lw $4,i($at)
351 In this case the first instruction will have a HI16 reloc, and the
352 second reloc will have a LO16 reloc. Both relocs will be against
353 the symbol i.
354
355 The issue here is that we may not know whether i is GP addressable
356 until after we see the instruction that uses it. Therefore, we
357 want to be able to choose the final instruction sequence only at
358 the end of the assembly. This is similar to the way other
23dc1ae3 359 platforms choose the size of a PC relative instruction only at the
0dd2d296
ILT
360 end of assembly.
361
362 When generating position independent code we do not use GP
23dc1ae3
ILT
363 addressing in quite the same way, but the issue still arises as
364 external symbols and local symbols must be handled differently.
0dd2d296
ILT
365
366 We handle these issues by actually generating both possible
367 instruction sequences. The longer one is put in a frag_var with
368 type rs_machine_dependent. We encode what to do with the frag in
369 the subtype field. We encode (1) the number of existing bytes to
370 replace, (2) the number of new bytes to use, (3) the offset from
371 the start of the existing bytes to the first reloc we must generate
372 (that is, the offset is applied from the start of the existing
373 bytes after they are replaced by the new bytes, if any), (4) the
374 offset from the start of the existing bytes to the second reloc,
375 (5) whether a third reloc is needed (the third reloc is always four
376 bytes after the second reloc), and (6) whether to warn if this
377 variant is used (this is sometimes needed if .set nomacro or .set
378 noat is in effect). All these numbers are reasonably small.
379
380 Generating two instruction sequences must be handled carefully to
23dc1ae3
ILT
381 ensure that delay slots are handled correctly. Fortunately, there
382 are a limited number of cases. When the second instruction
383 sequence is generated, append_insn is directed to maintain the
384 existing delay slot information, so it continues to apply to any
385 code after the second instruction sequence. This means that the
386 second instruction sequence must not impose any requirements not
387 required by the first instruction sequence.
0dd2d296
ILT
388
389 These variant frags are then handled in functions called by the
390 machine independent code. md_estimate_size_before_relax returns
391 the final size of the frag. md_convert_frag sets up the final form
392 of the frag. tc_gen_reloc adjust the first reloc and adds a second
393 one if needed. */
394#define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
395 ((relax_substateT) \
396 (((old) << 24) \
397 | ((new) << 16) \
398 | (((reloc1) + 64) << 9) \
399 | (((reloc2) + 64) << 2) \
400 | ((reloc3) ? (1 << 1) : 0) \
401 | ((warn) ? 1 : 0)))
402#define RELAX_OLD(i) (((i) >> 24) & 0xff)
403#define RELAX_NEW(i) (((i) >> 16) & 0xff)
483971bd
KR
404#define RELAX_RELOC1(i) ((bfd_vma)(((i) >> 9) & 0x7f) - 64)
405#define RELAX_RELOC2(i) ((bfd_vma)(((i) >> 2) & 0x7f) - 64)
0dd2d296
ILT
406#define RELAX_RELOC3(i) (((i) >> 1) & 1)
407#define RELAX_WARN(i) ((i) & 1)
408\f
3d3c5039
ILT
409/* Prototypes for static functions. */
410
411#ifdef __STDC__
412#define internalError() \
413 as_fatal ("internal Error, line %d, %s", __LINE__, __FILE__)
414#else
415#define internalError() as_fatal ("MIPS internal Error");
416#endif
417
becfe05e 418static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
604633ae 419 unsigned int reg, int fpr));
fb251650 420static int reg_needs_delay PARAMS ((int));
0dd2d296
ILT
421static void append_insn PARAMS ((char *place,
422 struct mips_cl_insn * ip,
670a50eb 423 expressionS * p,
867a58b3
ILT
424 bfd_reloc_code_real_type r,
425 boolean));
becfe05e
ILT
426static void mips_no_prev_insn PARAMS ((void));
427static void mips_emit_delays PARAMS ((void));
c625fc23 428#ifdef USE_STDARG
0dd2d296 429static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
3d3c5039
ILT
430 const char *name, const char *fmt,
431 ...));
c625fc23
JSC
432#else
433static void macro_build ();
434#endif
0dd2d296
ILT
435static void macro_build_lui PARAMS ((char *place, int *counter,
436 expressionS * ep, int regnum));
6e8dda9c 437static void set_at PARAMS ((int *counter, int reg, int unsignedp));
670a50eb 438static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
19ed8960 439 expressionS *));
d8a1c247 440static void load_register PARAMS ((int *, int, expressionS *, int));
0dd2d296 441static void load_address PARAMS ((int *counter, int reg, expressionS *ep));
670a50eb 442static void macro PARAMS ((struct mips_cl_insn * ip));
917fae09
SS
443#ifdef LOSING_COMPILER
444static void macro2 PARAMS ((struct mips_cl_insn * ip));
445#endif
670a50eb
ILT
446static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
447static int my_getSmallExpression PARAMS ((expressionS * ep, char *str));
448static void my_getExpression PARAMS ((expressionS * ep, char *str));
3d3c5039 449static symbolS *get_symbol PARAMS ((void));
23dc1ae3 450static void mips_align PARAMS ((int to, int fill, symbolS *label));
3d3c5039
ILT
451static void s_align PARAMS ((int));
452static void s_change_sec PARAMS ((int));
453static void s_cons PARAMS ((int));
3d3c5039 454static void s_float_cons PARAMS ((int));
c1444ec4 455static void s_mips_globl PARAMS ((int));
3d3c5039
ILT
456static void s_option PARAMS ((int));
457static void s_mipsset PARAMS ((int));
9226253a
ILT
458static void s_abicalls PARAMS ((int));
459static void s_cpload PARAMS ((int));
460static void s_cprestore PARAMS ((int));
0dd2d296
ILT
461static void s_gpword PARAMS ((int));
462static void s_cpadd PARAMS ((int));
3d3c5039
ILT
463static void md_obj_begin PARAMS ((void));
464static void md_obj_end PARAMS ((void));
465static long get_number PARAMS ((void));
466static void s_ent PARAMS ((int));
467static void s_mipsend PARAMS ((int));
468static void s_file PARAMS ((int));
3d3c5039
ILT
469\f
470/* Pseudo-op table.
471
472 The following pseudo-ops from the Kane and Heinrich MIPS book
473 should be defined here, but are currently unsupported: .alias,
474 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
475
476 The following pseudo-ops from the Kane and Heinrich MIPS book are
477 specific to the type of debugging information being generated, and
478 should be defined by the object format: .aent, .begin, .bend,
479 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
480 .vreg.
481
482 The following pseudo-ops from the Kane and Heinrich MIPS book are
483 not MIPS CPU specific, but are also not specific to the object file
484 format. This file is probably the best place to define them, but
485 they are not currently supported: .asm0, .endr, .lab, .repeat,
486 .struct, .weakext. */
487
739708fa 488static const pseudo_typeS mips_pseudo_table[] =
3d3c5039 489{
670a50eb
ILT
490 /* MIPS specific pseudo-ops. */
491 {"option", s_option, 0},
492 {"set", s_mipsset, 0},
dd3f1f76
ILT
493 {"rdata", s_change_sec, 'r'},
494 {"sdata", s_change_sec, 's'},
495 {"livereg", s_ignore, 0},
739708fa
KR
496 {"abicalls", s_abicalls, 0},
497 {"cpload", s_cpload, 0},
498 {"cprestore", s_cprestore, 0},
499 {"gpword", s_gpword, 0},
500 {"cpadd", s_cpadd, 0},
3d3c5039 501
670a50eb 502 /* Relatively generic pseudo-ops that happen to be used on MIPS
3d3c5039 503 chips. */
739708fa 504 {"asciiz", stringer, 1},
670a50eb
ILT
505 {"bss", s_change_sec, 'b'},
506 {"err", s_err, 0},
507 {"half", s_cons, 1},
52aa70b5 508 {"dword", s_cons, 3},
3d3c5039 509
670a50eb 510 /* These pseudo-ops are defined in read.c, but must be overridden
3d3c5039 511 here for one reason or another. */
670a50eb
ILT
512 {"align", s_align, 0},
513 {"byte", s_cons, 0},
514 {"data", s_change_sec, 'd'},
becfe05e 515 {"double", s_float_cons, 'd'},
becfe05e 516 {"float", s_float_cons, 'f'},
c1444ec4
ILT
517 {"globl", s_mips_globl, 0},
518 {"global", s_mips_globl, 0},
eb8fd0e9
ILT
519 {"hword", s_cons, 1},
520 {"int", s_cons, 2},
521 {"long", s_cons, 2},
522 {"octa", s_cons, 4},
523 {"quad", s_cons, 3},
524 {"short", s_cons, 1},
525 {"single", s_float_cons, 'f'},
670a50eb
ILT
526 {"text", s_change_sec, 't'},
527 {"word", s_cons, 2},
739708fa
KR
528 { 0 },
529};
3d3c5039 530
739708fa 531static const pseudo_typeS mips_nonecoff_pseudo_table[] = {
670a50eb 532 /* These pseudo-ops should be defined by the object file format.
0dd2d296 533 However, a.out doesn't support them, so we have versions here. */
670a50eb 534 {"aent", s_ent, 1},
9226253a 535 {"bgnb", s_ignore, 0},
670a50eb 536 {"end", s_mipsend, 0},
9226253a 537 {"endb", s_ignore, 0},
670a50eb
ILT
538 {"ent", s_ent, 0},
539 {"file", s_file, 0},
540 {"fmask", s_ignore, 'F'},
541 {"frame", s_ignore, 0},
542 {"loc", s_ignore, 0},
543 {"mask", s_ignore, 'R'},
544 {"verstamp", s_ignore, 0},
739708fa
KR
545 { 0 },
546};
61420a20 547
739708fa
KR
548extern void pop_insert PARAMS ((const pseudo_typeS *));
549
550void
551mips_pop_insert ()
552{
553 pop_insert (mips_pseudo_table);
554 if (! ECOFF_DEBUGGING)
555 pop_insert (mips_nonecoff_pseudo_table);
739708fa 556}
3d3c5039 557\f
3d3c5039
ILT
558static char *expr_end;
559
867a58b3
ILT
560/* Expressions which appear in instructions. These are set by
561 mips_ip. */
562
3d3c5039
ILT
563static expressionS imm_expr;
564static expressionS offset_expr;
867a58b3
ILT
565
566/* Relocs associated with imm_expr and offset_expr. */
567
3d3c5039
ILT
568static bfd_reloc_code_real_type imm_reloc;
569static bfd_reloc_code_real_type offset_reloc;
570
867a58b3
ILT
571/* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
572
573static boolean imm_unmatched_hi;
574
3d3c5039
ILT
575/*
576 * This function is called once, at assembler startup time. It should
577 * set up all the tables, etc. that the MD part of the assembler will need.
578 */
579void
670a50eb 580md_begin ()
3d3c5039 581{
0dd2d296 582 boolean ok = false;
604633ae 583 register const char *retval = NULL;
670a50eb 584 register unsigned int i = 0;
3d3c5039 585
8358c818
ILT
586 if (mips_isa == -1)
587 {
8c63448a
ILT
588 const char *cpu;
589 char *a = NULL;
590
591 cpu = TARGET_CPU;
592 if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
593 {
594 a = xmalloc (sizeof TARGET_CPU);
595 strcpy (a, TARGET_CPU);
596 a[(sizeof TARGET_CPU) - 3] = '\0';
597 cpu = a;
598 }
599
600 if (strcmp (cpu, "mips") == 0)
601 {
602 mips_isa = 1;
4bb0cc41
ILT
603 if (mips_cpu == -1)
604 mips_cpu = 3000;
8c63448a
ILT
605 }
606 else if (strcmp (cpu, "r6000") == 0
607 || strcmp (cpu, "mips2") == 0)
608 {
609 mips_isa = 2;
4bb0cc41
ILT
610 if (mips_cpu == -1)
611 mips_cpu = 6000;
8c63448a
ILT
612 }
613 else if (strcmp (cpu, "mips64") == 0
614 || strcmp (cpu, "r4000") == 0
615 || strcmp (cpu, "mips3") == 0)
616 {
617 mips_isa = 3;
4bb0cc41
ILT
618 if (mips_cpu == -1)
619 mips_cpu = 4000;
8c63448a
ILT
620 }
621 else if (strcmp (cpu, "r4400") == 0)
622 {
623 mips_isa = 3;
4bb0cc41
ILT
624 if (mips_cpu == -1)
625 mips_cpu = 4400;
8c63448a
ILT
626 }
627 else if (strcmp (cpu, "mips64orion") == 0
628 || strcmp (cpu, "r4600") == 0)
629 {
630 mips_isa = 3;
4bb0cc41
ILT
631 if (mips_cpu == -1)
632 mips_cpu = 4600;
8c63448a 633 }
b2b8c24e
ILT
634 else if (strcmp (cpu, "r4650") == 0)
635 {
636 mips_isa = 3;
637 if (mips_cpu == -1)
638 mips_cpu = 4650;
639 if (mips_4650 == -1)
640 mips_4650 = 1;
641 }
c625fc23
JSC
642 else if (strcmp (cpu, "mips64vr4300") == 0)
643 {
644 mips_isa = 3;
645 if (mips_cpu == -1)
646 mips_cpu = 4300;
647 }
648 else if (strcmp (cpu, "mips64vr4100") == 0)
649 {
650 mips_isa = 3;
651 if (mips_cpu == -1)
652 mips_cpu = 4100;
653 if (mips_4100 == -1)
654 mips_4100 = 1;
655 }
e532b44c
ILT
656 else if (strcmp (cpu, "r4010") == 0)
657 {
658 mips_isa = 2;
659 if (mips_cpu == -1)
660 mips_cpu = 4010;
661 if (mips_4010 == -1)
662 mips_4010 = 1;
663 }
d8a1c247
KR
664 else if (strcmp (cpu, "r8000") == 0
665 || strcmp (cpu, "mips4") == 0)
666 {
667 mips_isa = 4;
668 if (mips_cpu == -1)
669 mips_cpu = 8000;
670 }
671 else if (strcmp (cpu, "r10000") == 0)
672 {
673 mips_isa = 4;
674 if (mips_cpu == -1)
675 mips_cpu = 10000;
676 }
8358c818 677 else
8c63448a
ILT
678 {
679 mips_isa = 1;
4bb0cc41
ILT
680 if (mips_cpu == -1)
681 mips_cpu = 3000;
8c63448a
ILT
682 }
683
684 if (a != NULL)
685 free (a);
8358c818
ILT
686 }
687
b2b8c24e
ILT
688 if (mips_4650 < 0)
689 mips_4650 = 0;
690
e532b44c
ILT
691 if (mips_4010 < 0)
692 mips_4010 = 0;
693
c625fc23
JSC
694 if (mips_4100 < 0)
695 mips_4100 = 0;
696
344a8d61 697 if (mips_4650 || mips_4010 || mips_4100 || mips_cpu == 4300)
e532b44c
ILT
698 interlocks = 1;
699 else
700 interlocks = 0;
701
344a8d61
JSC
702 if (mips_cpu == 4300)
703 cop_interlocks = 1;
704 else
705 cop_interlocks = 0;
706
8ea7f4e8
ILT
707 if (mips_isa < 2 && mips_trap)
708 as_bad ("trap exception not supported at ISA 1");
709
97f99d11
ILT
710 switch (mips_isa)
711 {
712 case 1:
713 ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 3000);
714 break;
715 case 2:
716 ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 6000);
717 break;
718 case 3:
719 ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 4000);
720 break;
d8a1c247
KR
721 case 4:
722 ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 8000);
723 break;
97f99d11
ILT
724 }
725 if (! ok)
726 as_warn ("Could not set architecture and machine");
727
1051c97f
ILT
728 file_mips_isa = mips_isa;
729
13fe1379
ILT
730 op_hash = hash_new ();
731
670a50eb
ILT
732 for (i = 0; i < NUMOPCODES;)
733 {
734 const char *name = mips_opcodes[i].name;
735
604633ae 736 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
abdad6bc 737 if (retval != NULL)
670a50eb
ILT
738 {
739 fprintf (stderr, "internal error: can't hash `%s': %s\n",
740 mips_opcodes[i].name, retval);
741 as_fatal ("Broken assembler. No assembly attempted.");
742 }
743 do
744 {
8358c818
ILT
745 if (mips_opcodes[i].pinfo != INSN_MACRO
746 && ((mips_opcodes[i].match & mips_opcodes[i].mask)
747 != mips_opcodes[i].match))
670a50eb
ILT
748 {
749 fprintf (stderr, "internal error: bad opcode: `%s' \"%s\"\n",
750 mips_opcodes[i].name, mips_opcodes[i].args);
751 as_fatal ("Broken assembler. No assembly attempted.");
3d3c5039 752 }
670a50eb
ILT
753 ++i;
754 }
755 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3d3c5039
ILT
756 }
757
becfe05e
ILT
758 mips_no_prev_insn ();
759
1aa6938e
ILT
760 mips_gprmask = 0;
761 mips_cprmask[0] = 0;
762 mips_cprmask[1] = 0;
763 mips_cprmask[2] = 0;
764 mips_cprmask[3] = 0;
765
8358c818
ILT
766 /* set the default alignment for the text section (2**2) */
767 record_alignment (text_section, 2);
768
1dc1e798
KR
769 if (USE_GLOBAL_POINTER_OPT)
770 bfd_set_gp_size (stdoutput, g_switch_value);
abdad6bc 771
1dc1e798
KR
772 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
773 {
774 /* Sections must be aligned to 16 byte boundaries. */
775 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
776 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
777 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
778
779 /* Create a .reginfo section for register masks and a .mdebug
780 section for debugging information. */
781 {
782 segT seg;
783 subsegT subseg;
784 segT sec;
785
786 seg = now_seg;
787 subseg = now_subseg;
1dc1e798 788
87178180
ILT
789 if (! mips_64)
790 {
791 sec = subseg_new (".reginfo", (subsegT) 0);
8358c818 792
87178180
ILT
793 /* The ABI says this section should be loaded so that the
794 running program can access it. */
795 (void) bfd_set_section_flags (stdoutput, sec,
796 (SEC_ALLOC | SEC_LOAD
797 | SEC_READONLY | SEC_DATA));
798 (void) bfd_set_section_alignment (stdoutput, sec, 2);
799
f2a663d3 800#ifdef OBJ_ELF
87178180 801 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1dc1e798 802#endif
87178180
ILT
803 }
804 else
805 {
806 /* The 64-bit ABI uses a .MIPS.options section rather than
807 .reginfo section. */
808 sec = subseg_new (".MIPS.options", (subsegT) 0);
809 (void) bfd_set_section_flags (stdoutput, sec,
810 (SEC_ALLOC | SEC_LOAD
811 | SEC_READONLY | SEC_DATA));
812 (void) bfd_set_section_alignment (stdoutput, sec, 3);
813
814#ifdef OBJ_ELF
815 /* Set up the option header. */
816 {
817 Elf_Internal_Options opthdr;
818 char *f;
819
820 opthdr.kind = ODK_REGINFO;
821 opthdr.size = (sizeof (Elf_External_Options)
822 + sizeof (Elf64_External_RegInfo));
823 opthdr.section = 0;
824 opthdr.info = 0;
825 f = frag_more (sizeof (Elf_External_Options));
826 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
827 (Elf_External_Options *) f);
828
829 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
830 }
831#endif
832 }
f2a663d3 833
739708fa
KR
834 if (ECOFF_DEBUGGING)
835 {
836 sec = subseg_new (".mdebug", (subsegT) 0);
837 (void) bfd_set_section_flags (stdoutput, sec,
838 SEC_HAS_CONTENTS | SEC_READONLY);
839 (void) bfd_set_section_alignment (stdoutput, sec, 2);
840 }
0dd2d296 841
1dc1e798
KR
842 subseg_set (seg, subseg);
843 }
844 }
f2a663d3 845
739708fa
KR
846 if (! ECOFF_DEBUGGING)
847 md_obj_begin ();
3d3c5039
ILT
848}
849
850void
13fe1379 851md_mips_end ()
3d3c5039 852{
739708fa
KR
853 if (! ECOFF_DEBUGGING)
854 md_obj_end ();
3d3c5039
ILT
855}
856
857void
670a50eb
ILT
858md_assemble (str)
859 char *str;
3d3c5039 860{
670a50eb 861 struct mips_cl_insn insn;
3d3c5039 862
5ac34ac3 863 imm_expr.X_op = O_absent;
867a58b3
ILT
864 imm_reloc = BFD_RELOC_UNUSED;
865 imm_unmatched_hi = false;
5ac34ac3 866 offset_expr.X_op = O_absent;
867a58b3 867 offset_reloc = BFD_RELOC_UNUSED;
3d3c5039 868
670a50eb
ILT
869 mips_ip (str, &insn);
870 if (insn_error)
871 {
872 as_bad ("%s `%s'", insn_error, str);
873 return;
874 }
875 if (insn.insn_mo->pinfo == INSN_MACRO)
876 {
877 macro (&insn);
3d3c5039 878 }
670a50eb
ILT
879 else
880 {
5ac34ac3 881 if (imm_expr.X_op != O_absent)
867a58b3
ILT
882 append_insn ((char *) NULL, &insn, &imm_expr, imm_reloc,
883 imm_unmatched_hi);
5ac34ac3 884 else if (offset_expr.X_op != O_absent)
867a58b3 885 append_insn ((char *) NULL, &insn, &offset_expr, offset_reloc, false);
670a50eb 886 else
867a58b3 887 append_insn ((char *) NULL, &insn, NULL, BFD_RELOC_UNUSED, false);
3d3c5039
ILT
888 }
889}
890
becfe05e
ILT
891/* See whether instruction IP reads register REG. If FPR is non-zero,
892 REG is a floating point register. */
893
894static int
895insn_uses_reg (ip, reg, fpr)
896 struct mips_cl_insn *ip;
604633ae 897 unsigned int reg;
becfe05e
ILT
898 int fpr;
899{
900 /* Don't report on general register 0, since it never changes. */
901 if (! fpr && reg == 0)
902 return 0;
903
904 if (fpr)
905 {
906 /* If we are called with either $f0 or $f1, we must check $f0.
907 This is not optimal, because it will introduce an unnecessary
908 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
909 need to distinguish reading both $f0 and $f1 or just one of
910 them. Note that we don't have to check the other way,
911 because there is no instruction that sets both $f0 and $f1
912 and requires a delay. */
913 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
604633ae
ILT
914 && (((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS)
915 == (reg &~ (unsigned) 1)))
becfe05e
ILT
916 return 1;
917 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
604633ae
ILT
918 && (((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT)
919 == (reg &~ (unsigned) 1)))
becfe05e
ILT
920 return 1;
921 }
922 else
923 {
924 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
925 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
926 return 1;
927 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
928 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
929 return 1;
930 }
931
932 return 0;
933}
934
fb251650
ILT
935/* This function returns true if modifying a register requires a
936 delay. */
937
938static int
939reg_needs_delay (reg)
940 int reg;
941{
942 unsigned long prev_pinfo;
943
944 prev_pinfo = prev_insn.insn_mo->pinfo;
945 if (! mips_noreorder
946 && mips_isa < 4
947 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
948 || (mips_isa < 2
949 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
950 {
951 /* A load from a coprocessor or from memory. All load
952 delays delay the use of general register rt for one
953 instruction on the r3000. The r6000 and r4000 use
954 interlocks. */
955 know (prev_pinfo & INSN_WRITE_GPR_T);
956 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
957 return 1;
958 }
959
960 return 0;
961}
962
0dd2d296
ILT
963/* Output an instruction. PLACE is where to put the instruction; if
964 it is NULL, this uses frag_more to get room. IP is the instruction
965 information. ADDRESS_EXPR is an operand of the instruction to be
966 used with RELOC_TYPE. */
3d3c5039 967
3d3c5039 968static void
867a58b3 969append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
0dd2d296 970 char *place;
670a50eb
ILT
971 struct mips_cl_insn *ip;
972 expressionS *address_expr;
973 bfd_reloc_code_real_type reloc_type;
867a58b3 974 boolean unmatched_hi;
3d3c5039 975{
1aa6938e 976 register unsigned long prev_pinfo, pinfo;
670a50eb 977 char *f;
becfe05e
ILT
978 fixS *fixp;
979 int nops = 0;
3d3c5039 980
1aa6938e
ILT
981 prev_pinfo = prev_insn.insn_mo->pinfo;
982 pinfo = ip->insn_mo->pinfo;
983
0dd2d296 984 if (place == NULL && ! mips_noreorder)
becfe05e
ILT
985 {
986 /* If the previous insn required any delay slots, see if we need
8358c818 987 to insert a NOP or two. There are eight kinds of possible
becfe05e 988 hazards, of which an instruction can have at most one type.
8358c818
ILT
989 (1) a load from memory delay
990 (2) a load from a coprocessor delay
991 (3) an unconditional branch delay
992 (4) a conditional branch delay
993 (5) a move to coprocessor register delay
994 (6) a load coprocessor register from memory delay
995 (7) a coprocessor condition code delay
996 (8) a HI/LO special register delay
becfe05e
ILT
997
998 There are a lot of optimizations we could do that we don't.
999 In particular, we do not, in general, reorder instructions.
1000 If you use gcc with optimization, it will reorder
1001 instructions and generally do much more optimization then we
1002 do here; repeating all that work in the assembler would only
1003 benefit hand written assembly code, and does not seem worth
1004 it. */
1005
1006 /* This is how a NOP is emitted. */
1007#define emit_nop() md_number_to_chars (frag_more (4), 0, 4)
1008
1009 /* The previous insn might require a delay slot, depending upon
1010 the contents of the current insn. */
d8a1c247 1011 if (mips_isa < 4
5af96dce
ILT
1012 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1013 && ! cop_interlocks)
d8a1c247
KR
1014 || (mips_isa < 2
1015 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
8358c818
ILT
1016 {
1017 /* A load from a coprocessor or from memory. All load
1018 delays delay the use of general register rt for one
1019 instruction on the r3000. The r6000 and r4000 use
1020 interlocks. */
1aa6938e 1021 know (prev_pinfo & INSN_WRITE_GPR_T);
0aa07269
ILT
1022 if (mips_optimize == 0
1023 || insn_uses_reg (ip,
1024 ((prev_insn.insn_opcode >> OP_SH_RT)
1025 & OP_MASK_RT),
1026 0))
becfe05e
ILT
1027 ++nops;
1028 }
d8a1c247 1029 else if (mips_isa < 4
5af96dce
ILT
1030 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1031 && ! cop_interlocks)
d8a1c247
KR
1032 || (mips_isa < 2
1033 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
becfe05e
ILT
1034 {
1035 /* A generic coprocessor delay. The previous instruction
1036 modified a coprocessor general or control register. If
1037 it modified a control register, we need to avoid any
1038 coprocessor instruction (this is probably not always
1039 required, but it sometimes is). If it modified a general
1040 register, we avoid using that register.
1041
8358c818
ILT
1042 On the r6000 and r4000 loading a coprocessor register
1043 from memory is interlocked, and does not require a delay.
1044
becfe05e
ILT
1045 This case is not handled very well. There is no special
1046 knowledge of CP0 handling, and the coprocessors other
1047 than the floating point unit are not distinguished at
1048 all. */
1aa6938e 1049 if (prev_pinfo & INSN_WRITE_FPR_T)
becfe05e 1050 {
0aa07269
ILT
1051 if (mips_optimize == 0
1052 || insn_uses_reg (ip,
8358c818
ILT
1053 ((prev_insn.insn_opcode >> OP_SH_FT)
1054 & OP_MASK_FT),
0aa07269 1055 1))
becfe05e
ILT
1056 ++nops;
1057 }
1aa6938e 1058 else if (prev_pinfo & INSN_WRITE_FPR_S)
becfe05e 1059 {
0aa07269
ILT
1060 if (mips_optimize == 0
1061 || insn_uses_reg (ip,
8358c818
ILT
1062 ((prev_insn.insn_opcode >> OP_SH_FS)
1063 & OP_MASK_FS),
0aa07269 1064 1))
becfe05e
ILT
1065 ++nops;
1066 }
1067 else
1068 {
1069 /* We don't know exactly what the previous instruction
1070 does. If the current instruction uses a coprocessor
1071 register, we must insert a NOP. If previous
1072 instruction may set the condition codes, and the
1073 current instruction uses them, we must insert two
1074 NOPS. */
0aa07269 1075 if (mips_optimize == 0
1aa6938e
ILT
1076 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1077 && (pinfo & INSN_READ_COND_CODE)))
becfe05e 1078 nops += 2;
1aa6938e 1079 else if (pinfo & INSN_COP)
becfe05e
ILT
1080 ++nops;
1081 }
1082 }
d8a1c247 1083 else if (mips_isa < 4
344a8d61
JSC
1084 && (prev_pinfo & INSN_WRITE_COND_CODE)
1085 && ! cop_interlocks)
becfe05e
ILT
1086 {
1087 /* The previous instruction sets the coprocessor condition
1088 codes, but does not require a general coprocessor delay
1089 (this means it is a floating point comparison
1090 instruction). If this instruction uses the condition
1091 codes, we need to insert a single NOP. */
0aa07269 1092 if (mips_optimize == 0
1aa6938e 1093 || (pinfo & INSN_READ_COND_CODE))
becfe05e
ILT
1094 ++nops;
1095 }
1aa6938e 1096 else if (prev_pinfo & INSN_READ_LO)
becfe05e
ILT
1097 {
1098 /* The previous instruction reads the LO register; if the
1099 current instruction writes to the LO register, we must
344a8d61
JSC
1100 insert two NOPS. The R4650, VR4100 and VR4300 have
1101 interlocks. */
e532b44c 1102 if (! interlocks
b2b8c24e
ILT
1103 && (mips_optimize == 0
1104 || (pinfo & INSN_WRITE_LO)))
becfe05e
ILT
1105 nops += 2;
1106 }
1107 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1108 {
1109 /* The previous instruction reads the HI register; if the
1110 current instruction writes to the HI register, we must
344a8d61
JSC
1111 insert a NOP. The R4650, VR4100 and VR4300 have
1112 interlocks. */
e532b44c 1113 if (! interlocks
b2b8c24e
ILT
1114 && (mips_optimize == 0
1115 || (pinfo & INSN_WRITE_HI)))
becfe05e
ILT
1116 nops += 2;
1117 }
1118
1119 /* There are two cases which require two intervening
1120 instructions: 1) setting the condition codes using a move to
1121 coprocessor instruction which requires a general coprocessor
1122 delay and then reading the condition codes 2) reading the HI
b2b8c24e 1123 or LO register and then writing to it (except on the R4650,
344a8d61
JSC
1124 VR4100, and VR4300 which have interlocks). If we are not
1125 already emitting a NOP instruction, we must check for these
1126 cases compared to the instruction previous to the previous
c625fc23 1127 instruction. */
becfe05e 1128 if (nops == 0
d8a1c247
KR
1129 && ((mips_isa < 4
1130 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
becfe05e 1131 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
344a8d61
JSC
1132 && (pinfo & INSN_READ_COND_CODE)
1133 && ! cop_interlocks)
becfe05e 1134 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
b2b8c24e 1135 && (pinfo & INSN_WRITE_LO)
e532b44c 1136 && ! interlocks)
becfe05e 1137 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
b2b8c24e 1138 && (pinfo & INSN_WRITE_HI)
e532b44c 1139 && ! interlocks)))
becfe05e
ILT
1140 ++nops;
1141
0dd2d296
ILT
1142 /* If we are being given a nop instruction, don't bother with
1143 one of the nops we would otherwise output. This will only
1144 happen when a nop instruction is used with mips_optimize set
1145 to 0. */
1146 if (nops > 0 && ip->insn_opcode == 0)
1147 --nops;
1148
becfe05e
ILT
1149 /* Now emit the right number of NOP instructions. */
1150 if (nops > 0)
1151 {
5af96dce
ILT
1152 fragS *old_frag;
1153 unsigned long old_frag_offset;
8c63448a
ILT
1154 int i;
1155
5af96dce
ILT
1156 old_frag = frag_now;
1157 old_frag_offset = frag_now_fix ();
1158
8c63448a 1159 for (i = 0; i < nops; i++)
becfe05e 1160 emit_nop ();
5af96dce 1161
af255ca0 1162 if (listing)
546f5536
ILT
1163 {
1164 listing_prev_line ();
1165 /* We may be at the start of a variant frag. In case we
1166 are, make sure there is enough space for the frag
1167 after the frags created by listing_prev_line. The
1168 argument to frag_grow here must be at least as large
1169 as the argument to all other calls to frag_grow in
1170 this file. We don't have to worry about being in the
1171 middle of a variant frag, because the variants insert
1172 all needed nop instructions themselves. */
1173 frag_grow (40);
1174 }
5af96dce 1175
becfe05e
ILT
1176 if (insn_label != NULL)
1177 {
1178 assert (S_GET_SEGMENT (insn_label) == now_seg);
1179 insn_label->sy_frag = frag_now;
604633ae 1180 S_SET_VALUE (insn_label, (valueT) frag_now_fix ());
becfe05e 1181 }
5af96dce
ILT
1182
1183#ifndef NO_ECOFF_DEBUGGING
1184 if (ECOFF_DEBUGGING)
1185 ecoff_fix_loc (old_frag, old_frag_offset);
1186#endif
becfe05e
ILT
1187 }
1188 }
1189
0dd2d296
ILT
1190 if (place == NULL)
1191 f = frag_more (4);
1192 else
1193 f = place;
becfe05e 1194 fixp = NULL;
670a50eb
ILT
1195 if (address_expr != NULL)
1196 {
5ac34ac3 1197 if (address_expr->X_op == O_constant)
670a50eb
ILT
1198 {
1199 switch (reloc_type)
1200 {
3d3c5039 1201 case BFD_RELOC_32:
670a50eb
ILT
1202 ip->insn_opcode |= address_expr->X_add_number;
1203 break;
3d3c5039
ILT
1204
1205 case BFD_RELOC_LO16:
670a50eb
ILT
1206 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1207 break;
3d3c5039
ILT
1208
1209 case BFD_RELOC_MIPS_JMP:
1210 case BFD_RELOC_16_PCREL_S2:
670a50eb 1211 goto need_reloc;
3d3c5039
ILT
1212
1213 default:
670a50eb 1214 internalError ();
3d3c5039 1215 }
670a50eb
ILT
1216 }
1217 else
1218 {
1219 assert (reloc_type != BFD_RELOC_UNUSED);
3d3c5039 1220 need_reloc:
0dd2d296
ILT
1221 /* Don't generate a reloc if we are writing into a variant
1222 frag. */
1223 if (place == NULL)
867a58b3
ILT
1224 {
1225 fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1226 address_expr,
1227 reloc_type == BFD_RELOC_16_PCREL_S2,
1228 reloc_type);
1229 if (unmatched_hi)
1230 {
1231 struct mips_hi_fixup *hi_fixup;
1232
1233 assert (reloc_type == BFD_RELOC_HI16_S);
1234 hi_fixup = ((struct mips_hi_fixup *)
1235 xmalloc (sizeof (struct mips_hi_fixup)));
1236 hi_fixup->fixp = fixp;
1237 hi_fixup->seg = now_seg;
1238 hi_fixup->next = mips_hi_fixup_list;
1239 mips_hi_fixup_list = hi_fixup;
1240 }
1241 }
3d3c5039
ILT
1242 }
1243 }
becfe05e 1244
670a50eb
ILT
1245 md_number_to_chars (f, ip->insn_opcode, 4);
1246
1aa6938e
ILT
1247 /* Update the register mask information. */
1248 if (pinfo & INSN_WRITE_GPR_D)
1249 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
1250 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
1251 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
1252 if (pinfo & INSN_READ_GPR_S)
1253 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
1254 if (pinfo & INSN_WRITE_GPR_31)
1255 mips_gprmask |= 1 << 31;
1256 if (pinfo & INSN_WRITE_FPR_D)
1257 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
1258 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
1259 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
1260 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
1261 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
d8a1c247
KR
1262 if ((pinfo & INSN_READ_FPR_R) != 0)
1263 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
1aa6938e
ILT
1264 if (pinfo & INSN_COP)
1265 {
1266 /* We don't keep enough information to sort these cases out. */
1267 }
1268 /* Never set the bit for $0, which is always zero. */
1269 mips_gprmask &=~ 1 << 0;
1270
0dd2d296 1271 if (place == NULL && ! mips_noreorder)
670a50eb 1272 {
becfe05e
ILT
1273 /* Filling the branch delay slot is more complex. We try to
1274 switch the branch with the previous instruction, which we can
1275 do if the previous instruction does not set up a condition
1276 that the branch tests and if the branch is not itself the
1277 target of any branch. */
1aa6938e
ILT
1278 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
1279 || (pinfo & INSN_COND_BRANCH_DELAY))
becfe05e 1280 {
0aa07269 1281 if (mips_optimize < 2
19ed8960
ILT
1282 /* If we have seen .set volatile or .set nomove, don't
1283 optimize. */
1284 || mips_nomove != 0
4e95866e
ILT
1285 /* If we had to emit any NOP instructions, then we
1286 already know we can not swap. */
1287 || nops != 0
becfe05e
ILT
1288 /* If we don't even know the previous insn, we can not
1289 swap. */
1290 || ! prev_insn_valid
1291 /* If the previous insn is already in a branch delay
1292 slot, then we can not swap. */
1293 || prev_insn_is_delay_slot
4e95866e
ILT
1294 /* If the previous previous insn was in a .set
1295 noreorder, we can't swap. Actually, the MIPS
1296 assembler will swap in this situation. However, gcc
1297 configured -with-gnu-as will generate code like
1298 .set noreorder
1299 lw $4,XXX
1300 .set reorder
1301 INSN
1302 bne $4,$0,foo
1303 in which we can not swap the bne and INSN. If gcc is
1304 not configured -with-gnu-as, it does not output the
1305 .set pseudo-ops. We don't have to check
1306 prev_insn_unreordered, because prev_insn_valid will
1307 be 0 in that case. We don't want to use
1308 prev_prev_insn_valid, because we do want to be able
1309 to swap at the start of a function. */
1310 || prev_prev_insn_unreordered
becfe05e
ILT
1311 /* If the branch is itself the target of a branch, we
1312 can not swap. We cheat on this; all we check for is
1313 whether there is a label on this instruction. If
1314 there are any branches to anything other than a
1315 label, users must use .set noreorder. */
1316 || insn_label != NULL
777ad64d
ILT
1317 /* If the previous instruction is in a variant frag, we
1318 can not do the swap. */
1319 || prev_insn_frag->fr_type == rs_machine_dependent
becfe05e
ILT
1320 /* If the branch reads the condition codes, we don't
1321 even try to swap, because in the sequence
1322 ctc1 $X,$31
1323 INSN
1324 INSN
1325 bc1t LABEL
1326 we can not swap, and I don't feel like handling that
1327 case. */
d8a1c247
KR
1328 || (mips_isa < 4
1329 && (pinfo & INSN_READ_COND_CODE))
becfe05e
ILT
1330 /* We can not swap with an instruction that requires a
1331 delay slot, becase the target of the branch might
1332 interfere with that instruction. */
d8a1c247
KR
1333 || (mips_isa < 4
1334 && (prev_pinfo
1335 & (INSN_LOAD_COPROC_DELAY
1336 | INSN_COPROC_MOVE_DELAY
1337 | INSN_WRITE_COND_CODE)))
e532b44c 1338 || (! interlocks
b2b8c24e
ILT
1339 && (prev_pinfo
1340 & (INSN_READ_LO
1341 | INSN_READ_HI)))
8358c818 1342 || (mips_isa < 2
1aa6938e 1343 && (prev_pinfo
8358c818
ILT
1344 & (INSN_LOAD_MEMORY_DELAY
1345 | INSN_COPROC_MEMORY_DELAY)))
becfe05e 1346 /* We can not swap with a branch instruction. */
1aa6938e 1347 || (prev_pinfo
6e8dda9c
ILT
1348 & (INSN_UNCOND_BRANCH_DELAY
1349 | INSN_COND_BRANCH_DELAY
1350 | INSN_COND_BRANCH_LIKELY))
abdad6bc
ILT
1351 /* We do not swap with a trap instruction, since it
1352 complicates trap handlers to have the trap
1353 instruction be in a delay slot. */
1aa6938e 1354 || (prev_pinfo & INSN_TRAP)
becfe05e
ILT
1355 /* If the branch reads a register that the previous
1356 instruction sets, we can not swap. */
1aa6938e 1357 || ((prev_pinfo & INSN_WRITE_GPR_T)
becfe05e
ILT
1358 && insn_uses_reg (ip,
1359 ((prev_insn.insn_opcode >> OP_SH_RT)
1360 & OP_MASK_RT),
1361 0))
1aa6938e 1362 || ((prev_pinfo & INSN_WRITE_GPR_D)
becfe05e
ILT
1363 && insn_uses_reg (ip,
1364 ((prev_insn.insn_opcode >> OP_SH_RD)
1365 & OP_MASK_RD),
1366 0))
1849d646
ILT
1367 /* If the branch writes a register that the previous
1368 instruction sets, we can not swap (we know that
1369 branches write only to RD or to $31). */
1aa6938e
ILT
1370 || ((prev_pinfo & INSN_WRITE_GPR_T)
1371 && (((pinfo & INSN_WRITE_GPR_D)
1849d646
ILT
1372 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
1373 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
1aa6938e 1374 || ((pinfo & INSN_WRITE_GPR_31)
1849d646
ILT
1375 && (((prev_insn.insn_opcode >> OP_SH_RT)
1376 & OP_MASK_RT)
1377 == 31))))
1aa6938e
ILT
1378 || ((prev_pinfo & INSN_WRITE_GPR_D)
1379 && (((pinfo & INSN_WRITE_GPR_D)
1849d646
ILT
1380 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
1381 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
1aa6938e 1382 || ((pinfo & INSN_WRITE_GPR_31)
1849d646
ILT
1383 && (((prev_insn.insn_opcode >> OP_SH_RD)
1384 & OP_MASK_RD)
1385 == 31))))
becfe05e
ILT
1386 /* If the branch writes a register that the previous
1387 instruction reads, we can not swap (we know that
1388 branches only write to RD or to $31). */
1aa6938e 1389 || ((pinfo & INSN_WRITE_GPR_D)
becfe05e
ILT
1390 && insn_uses_reg (&prev_insn,
1391 ((ip->insn_opcode >> OP_SH_RD)
1392 & OP_MASK_RD),
1393 0))
1aa6938e 1394 || ((pinfo & INSN_WRITE_GPR_31)
becfe05e 1395 && insn_uses_reg (&prev_insn, 31, 0))
5b63f465
ILT
1396 /* If we are generating embedded PIC code, the branch
1397 might be expanded into a sequence which uses $at, so
1398 we can't swap with an instruction which reads it. */
1399 || (mips_pic == EMBEDDED_PIC
1400 && insn_uses_reg (&prev_insn, AT, 0))
becfe05e
ILT
1401 /* If the previous previous instruction has a load
1402 delay, and sets a register that the branch reads, we
1403 can not swap. */
d8a1c247
KR
1404 || (mips_isa < 4
1405 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
1406 || (mips_isa < 2
1407 && (prev_prev_insn.insn_mo->pinfo
1408 & INSN_LOAD_MEMORY_DELAY)))
becfe05e
ILT
1409 && insn_uses_reg (ip,
1410 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
1411 & OP_MASK_RT),
1412 0)))
1413 {
1414 /* We could do even better for unconditional branches to
1415 portions of this object file; we could pick up the
1416 instruction at the destination, put it in the delay
1417 slot, and bump the destination address. */
1418 emit_nop ();
1419 /* Update the previous insn information. */
1420 prev_prev_insn = *ip;
1421 prev_insn.insn_mo = &dummy_opcode;
1422 }
1423 else
1424 {
1425 char *prev_f;
1426 char temp[4];
1427
1428 /* It looks like we can actually do the swap. */
1429 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
1430 memcpy (temp, prev_f, 4);
1431 memcpy (prev_f, f, 4);
1432 memcpy (f, temp, 4);
1433 if (prev_insn_fixp)
1434 {
1435 prev_insn_fixp->fx_frag = frag_now;
1436 prev_insn_fixp->fx_where = f - frag_now->fr_literal;
1437 }
1438 if (fixp)
1439 {
1440 fixp->fx_frag = prev_insn_frag;
1441 fixp->fx_where = prev_insn_where;
1442 }
1443 /* Update the previous insn information; leave prev_insn
1444 unchanged. */
1445 prev_prev_insn = *ip;
1446 }
1447 prev_insn_is_delay_slot = 1;
1448
1449 /* If that was an unconditional branch, forget the previous
1450 insn information. */
1aa6938e 1451 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
becfe05e
ILT
1452 {
1453 prev_prev_insn.insn_mo = &dummy_opcode;
1454 prev_insn.insn_mo = &dummy_opcode;
1455 }
1456 }
1aa6938e 1457 else if (pinfo & INSN_COND_BRANCH_LIKELY)
8358c818
ILT
1458 {
1459 /* We don't yet optimize a branch likely. What we should do
1460 is look at the target, copy the instruction found there
1461 into the delay slot, and increment the branch to jump to
1462 the next instruction. */
1463 emit_nop ();
1464 /* Update the previous insn information. */
1465 prev_prev_insn = *ip;
1466 prev_insn.insn_mo = &dummy_opcode;
1467 }
becfe05e 1468 else
670a50eb 1469 {
becfe05e
ILT
1470 /* Update the previous insn information. */
1471 if (nops > 0)
1472 prev_prev_insn.insn_mo = &dummy_opcode;
1473 else
1474 prev_prev_insn = prev_insn;
1475 prev_insn = *ip;
1476
1477 /* Any time we see a branch, we always fill the delay slot
1478 immediately; since this insn is not a branch, we know it
1479 is not in a delay slot. */
1480 prev_insn_is_delay_slot = 0;
1481 }
1482
4e95866e
ILT
1483 prev_prev_insn_unreordered = prev_insn_unreordered;
1484 prev_insn_unreordered = 0;
becfe05e
ILT
1485 prev_insn_frag = frag_now;
1486 prev_insn_where = f - frag_now->fr_literal;
1487 prev_insn_fixp = fixp;
1488 prev_insn_valid = 1;
1489 }
3d3c5039 1490
becfe05e
ILT
1491 /* We just output an insn, so the next one doesn't have a label. */
1492 insn_label = NULL;
1493}
1494
1495/* This function forgets that there was any previous instruction or
1496 label. */
1497
1498static void
1499mips_no_prev_insn ()
1500{
1501 prev_insn.insn_mo = &dummy_opcode;
1502 prev_prev_insn.insn_mo = &dummy_opcode;
1503 prev_insn_valid = 0;
1504 prev_insn_is_delay_slot = 0;
4e95866e
ILT
1505 prev_insn_unreordered = 0;
1506 prev_prev_insn_unreordered = 0;
becfe05e
ILT
1507 insn_label = NULL;
1508}
1509
1510/* This function must be called whenever we turn on noreorder or emit
1511 something other than instructions. It inserts any NOPS which might
1512 be needed by the previous instruction, and clears the information
1513 kept for the previous instructions. */
1514
1515static void
1516mips_emit_delays ()
1517{
1518 if (! mips_noreorder)
1519 {
1520 int nop;
1521
1522 nop = 0;
d8a1c247 1523 if ((mips_isa < 4
344a8d61
JSC
1524 && (! cop_interlocks
1525 && (prev_insn.insn_mo->pinfo
1526 & (INSN_LOAD_COPROC_DELAY
1527 | INSN_COPROC_MOVE_DELAY
1528 | INSN_WRITE_COND_CODE))))
e532b44c 1529 || (! interlocks
b2b8c24e
ILT
1530 && (prev_insn.insn_mo->pinfo
1531 & (INSN_READ_LO
1532 | INSN_READ_HI)))
8358c818
ILT
1533 || (mips_isa < 2
1534 && (prev_insn.insn_mo->pinfo
1535 & (INSN_LOAD_MEMORY_DELAY
1536 | INSN_COPROC_MEMORY_DELAY))))
becfe05e
ILT
1537 {
1538 nop = 1;
d8a1c247 1539 if ((mips_isa < 4
344a8d61
JSC
1540 && (! cop_interlocks
1541 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
e532b44c 1542 || (! interlocks
b2b8c24e
ILT
1543 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
1544 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
becfe05e
ILT
1545 emit_nop ();
1546 }
d8a1c247 1547 else if ((mips_isa < 4
344a8d61
JSC
1548 && (! cop_interlocks
1549 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
e532b44c 1550 || (! interlocks
b2b8c24e
ILT
1551 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1552 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
becfe05e
ILT
1553 nop = 1;
1554 if (nop)
670a50eb 1555 {
becfe05e
ILT
1556 emit_nop ();
1557 if (insn_label != NULL)
1558 {
1559 assert (S_GET_SEGMENT (insn_label) == now_seg);
1560 insn_label->sy_frag = frag_now;
604633ae 1561 S_SET_VALUE (insn_label, (valueT) frag_now_fix ());
becfe05e 1562 }
3d3c5039
ILT
1563 }
1564 }
0221ddf7
ILT
1565
1566 mips_no_prev_insn ();
3d3c5039
ILT
1567}
1568
670a50eb
ILT
1569/* Build an instruction created by a macro expansion. This is passed
1570 a pointer to the count of instructions created so far, an
1571 expression, the name of the instruction to build, an operand format
1572 string, and corresponding arguments. */
1573
1dc1e798 1574#ifdef USE_STDARG
3d3c5039 1575static void
0dd2d296
ILT
1576macro_build (char *place,
1577 int *counter,
670a50eb 1578 expressionS * ep,
3d3c5039
ILT
1579 const char *name,
1580 const char *fmt,
1581 ...)
1dc1e798 1582#else
3d3c5039 1583static void
0dd2d296
ILT
1584macro_build (place, counter, ep, name, fmt, va_alist)
1585 char *place;
3d3c5039
ILT
1586 int *counter;
1587 expressionS *ep;
1588 const char *name;
1589 const char *fmt;
1590 va_dcl
1dc1e798 1591#endif
3d3c5039 1592{
670a50eb
ILT
1593 struct mips_cl_insn insn;
1594 bfd_reloc_code_real_type r;
1595 va_list args;
3d3c5039 1596
1dc1e798 1597#ifdef USE_STDARG
670a50eb 1598 va_start (args, fmt);
3d3c5039 1599#else
670a50eb 1600 va_start (args);
3d3c5039
ILT
1601#endif
1602
670a50eb
ILT
1603 /*
1604 * If the macro is about to expand into a second instruction,
1605 * print a warning if needed. We need to pass ip as a parameter
1606 * to generate a better warning message here...
1607 */
0dd2d296 1608 if (mips_warn_about_macros && place == NULL && *counter == 1)
670a50eb
ILT
1609 as_warn ("Macro instruction expanded into multiple instructions");
1610
0dd2d296
ILT
1611 if (place == NULL)
1612 *counter += 1; /* bump instruction counter */
670a50eb
ILT
1613
1614 r = BFD_RELOC_UNUSED;
1615 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
1616 assert (insn.insn_mo);
1617 assert (strcmp (name, insn.insn_mo->name) == 0);
1618
9226253a 1619 while (strcmp (fmt, insn.insn_mo->args) != 0
c625fc23
JSC
1620 || insn.insn_mo->pinfo == INSN_MACRO
1621 || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_ISA2
1622 && mips_isa < 2)
1623 || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_ISA3
1624 && mips_isa < 3)
1625 || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_ISA4
1626 && mips_isa < 4)
1627 || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_4650
1628 && ! mips_4650)
1629 || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_4010
1630 && ! mips_4010)
1631 || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_4100
1632 && ! mips_4100))
670a50eb
ILT
1633 {
1634 ++insn.insn_mo;
1635 assert (insn.insn_mo->name);
1636 assert (strcmp (name, insn.insn_mo->name) == 0);
3d3c5039 1637 }
670a50eb
ILT
1638 insn.insn_opcode = insn.insn_mo->match;
1639 for (;;)
1640 {
1641 switch (*fmt++)
1642 {
3d3c5039 1643 case '\0':
670a50eb 1644 break;
3d3c5039
ILT
1645
1646 case ',':
1647 case '(':
1648 case ')':
670a50eb 1649 continue;
3d3c5039
ILT
1650
1651 case 't':
1652 case 'w':
918692a5 1653 case 'E':
670a50eb
ILT
1654 insn.insn_opcode |= va_arg (args, int) << 16;
1655 continue;
3d3c5039
ILT
1656
1657 case 'c':
1658 case 'T':
1659 case 'W':
670a50eb
ILT
1660 insn.insn_opcode |= va_arg (args, int) << 16;
1661 continue;
3d3c5039
ILT
1662
1663 case 'd':
918692a5 1664 case 'G':
670a50eb
ILT
1665 insn.insn_opcode |= va_arg (args, int) << 11;
1666 continue;
3d3c5039
ILT
1667
1668 case 'V':
1669 case 'S':
670a50eb
ILT
1670 insn.insn_opcode |= va_arg (args, int) << 11;
1671 continue;
3d3c5039 1672
ff3a5c18
ILT
1673 case 'z':
1674 continue;
1675
3d3c5039 1676 case '<':
670a50eb
ILT
1677 insn.insn_opcode |= va_arg (args, int) << 6;
1678 continue;
3d3c5039
ILT
1679
1680 case 'D':
670a50eb
ILT
1681 insn.insn_opcode |= va_arg (args, int) << 6;
1682 continue;
3d3c5039 1683
918692a5
ILT
1684 case 'B':
1685 insn.insn_opcode |= va_arg (args, int) << 6;
1686 continue;
1687
3d3c5039
ILT
1688 case 'b':
1689 case 's':
1690 case 'r':
1691 case 'v':
670a50eb
ILT
1692 insn.insn_opcode |= va_arg (args, int) << 21;
1693 continue;
3d3c5039
ILT
1694
1695 case 'i':
1696 case 'j':
1697 case 'o':
9226253a 1698 r = (bfd_reloc_code_real_type) va_arg (args, int);
0dd2d296
ILT
1699 assert (r == BFD_RELOC_MIPS_GPREL
1700 || r == BFD_RELOC_MIPS_LITERAL
1701 || r == BFD_RELOC_LO16
1702 || r == BFD_RELOC_MIPS_GOT16
ecd4ca1c 1703 || r == BFD_RELOC_MIPS_CALL16
fb251650
ILT
1704 || r == BFD_RELOC_MIPS_GOT_LO16
1705 || r == BFD_RELOC_MIPS_CALL_LO16
ecd4ca1c
ILT
1706 || (ep->X_op == O_subtract
1707 && now_seg == text_section
ecd4ca1c 1708 && r == BFD_RELOC_PCREL_LO16));
670a50eb 1709 continue;
3d3c5039 1710
6e8dda9c 1711 case 'u':
ecd4ca1c
ILT
1712 r = (bfd_reloc_code_real_type) va_arg (args, int);
1713 assert (ep != NULL
1714 && (ep->X_op == O_constant
1715 || (ep->X_op == O_symbol
1716 && (r == BFD_RELOC_HI16_S
fb251650
ILT
1717 || r == BFD_RELOC_HI16
1718 || r == BFD_RELOC_MIPS_GOT_HI16
1719 || r == BFD_RELOC_MIPS_CALL_HI16))
ecd4ca1c
ILT
1720 || (ep->X_op == O_subtract
1721 && now_seg == text_section
ecd4ca1c
ILT
1722 && r == BFD_RELOC_PCREL_HI16_S)));
1723 if (ep->X_op == O_constant)
1724 {
1725 insn.insn_opcode |= (ep->X_add_number >> 16) & 0xffff;
1726 ep = NULL;
1727 r = BFD_RELOC_UNUSED;
1728 }
6e8dda9c
ILT
1729 continue;
1730
3d3c5039 1731 case 'p':
670a50eb
ILT
1732 assert (ep != NULL);
1733 /*
1734 * This allows macro() to pass an immediate expression for
1735 * creating short branches without creating a symbol.
1736 * Note that the expression still might come from the assembly
1737 * input, in which case the value is not checked for range nor
1738 * is a relocation entry generated (yuck).
1739 */
5ac34ac3 1740 if (ep->X_op == O_constant)
670a50eb
ILT
1741 {
1742 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
1743 ep = NULL;
1744 }
1745 else
1746 r = BFD_RELOC_16_PCREL_S2;
1747 continue;
3d3c5039 1748
9226253a
ILT
1749 case 'a':
1750 assert (ep != NULL);
1751 r = BFD_RELOC_MIPS_JMP;
1752 continue;
1753
3d3c5039 1754 default:
670a50eb 1755 internalError ();
3d3c5039 1756 }
670a50eb 1757 break;
3d3c5039 1758 }
670a50eb
ILT
1759 va_end (args);
1760 assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
1761
867a58b3 1762 append_insn (place, &insn, ep, r, false);
3d3c5039
ILT
1763}
1764
1765/*
1766 * Generate a "lui" instruction.
1767 */
1768static void
0dd2d296
ILT
1769macro_build_lui (place, counter, ep, regnum)
1770 char *place;
3d3c5039
ILT
1771 int *counter;
1772 expressionS *ep;
1773 int regnum;
1774{
670a50eb
ILT
1775 expressionS high_expr;
1776 struct mips_cl_insn insn;
1777 bfd_reloc_code_real_type r;
1778 CONST char *name = "lui";
1779 CONST char *fmt = "t,u";
1780
0dd2d296
ILT
1781 if (place == NULL)
1782 high_expr = *ep;
1783 else
1784 {
1785 high_expr.X_op = O_constant;
fb251650 1786 high_expr.X_add_number = ep->X_add_number;
0dd2d296 1787 }
670a50eb 1788
5ac34ac3 1789 if (high_expr.X_op == O_constant)
670a50eb
ILT
1790 {
1791 /* we can compute the instruction now without a relocation entry */
1792 if (high_expr.X_add_number & 0x8000)
1793 high_expr.X_add_number += 0x10000;
1794 high_expr.X_add_number =
1795 ((unsigned long) high_expr.X_add_number >> 16) & 0xffff;
1796 r = BFD_RELOC_UNUSED;
1797 }
1798 else
0dd2d296
ILT
1799 {
1800 assert (ep->X_op == O_symbol);
1801 /* _gp_disp is a special case, used from s_cpload. */
d9aba805 1802 assert (mips_pic == NO_PIC
0dd2d296
ILT
1803 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
1804 r = BFD_RELOC_HI16_S;
1805 }
670a50eb
ILT
1806
1807 /*
1808 * If the macro is about to expand into a second instruction,
1809 * print a warning if needed. We need to pass ip as a parameter
1810 * to generate a better warning message here...
1811 */
0dd2d296 1812 if (mips_warn_about_macros && place == NULL && *counter == 1)
670a50eb
ILT
1813 as_warn ("Macro instruction expanded into multiple instructions");
1814
0dd2d296
ILT
1815 if (place == NULL)
1816 *counter += 1; /* bump instruction counter */
670a50eb
ILT
1817
1818 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
1819 assert (insn.insn_mo);
1820 assert (strcmp (name, insn.insn_mo->name) == 0);
1821 assert (strcmp (fmt, insn.insn_mo->args) == 0);
1822
0dd2d296 1823 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
670a50eb
ILT
1824 if (r == BFD_RELOC_UNUSED)
1825 {
1826 insn.insn_opcode |= high_expr.X_add_number;
867a58b3 1827 append_insn (place, &insn, NULL, r, false);
670a50eb
ILT
1828 }
1829 else
867a58b3 1830 append_insn (place, &insn, &high_expr, r, false);
3d3c5039
ILT
1831}
1832
1833/* set_at()
1834 * Generates code to set the $at register to true (one)
1835 * if reg is less than the immediate expression.
1836 */
1837static void
6e8dda9c 1838set_at (counter, reg, unsignedp)
3d3c5039
ILT
1839 int *counter;
1840 int reg;
6e8dda9c 1841 int unsignedp;
3d3c5039 1842{
6e8dda9c 1843 if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
0dd2d296 1844 macro_build ((char *) NULL, counter, &imm_expr,
6e8dda9c 1845 unsignedp ? "sltiu" : "slti",
9226253a 1846 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
6e8dda9c 1847 else
670a50eb 1848 {
d8a1c247 1849 load_register (counter, AT, &imm_expr, 0);
0dd2d296 1850 macro_build ((char *) NULL, counter, NULL,
6e8dda9c
ILT
1851 unsignedp ? "sltu" : "slt",
1852 "d,v,t", AT, reg, AT);
670a50eb 1853 }
3d3c5039
ILT
1854}
1855
6e8dda9c 1856/* Warn if an expression is not a constant. */
3d3c5039
ILT
1857
1858static void
19ed8960 1859check_absolute_expr (ip, ex)
3d3c5039 1860 struct mips_cl_insn *ip;
19ed8960 1861 expressionS *ex;
3d3c5039 1862{
19ed8960 1863 if (ex->X_op != O_constant)
670a50eb 1864 as_warn ("Instruction %s requires absolute expression", ip->insn_mo->name);
3d3c5039
ILT
1865}
1866
1867/* load_register()
1868 * This routine generates the least number of instructions neccessary to load
1869 * an absolute expression value into a register.
1870 */
1871static void
d8a1c247 1872load_register (counter, reg, ep, dbl)
670a50eb 1873 int *counter;
670a50eb
ILT
1874 int reg;
1875 expressionS *ep;
d8a1c247 1876 int dbl;
3d3c5039 1877{
d8a1c247 1878 int shift, freg;
7811254c 1879 expressionS hi32, lo32, tmp;
847a01cd
ILT
1880
1881 if (ep->X_op != O_big)
6e8dda9c 1882 {
847a01cd 1883 assert (ep->X_op == O_constant);
d8a1c247
KR
1884 if (ep->X_add_number < 0x8000
1885 && (ep->X_add_number >= 0
1886 || (ep->X_add_number >= -0x8000
1887 && (! dbl
1888 || ! ep->X_unsigned
1889 || sizeof (ep->X_add_number) > 4))))
847a01cd
ILT
1890 {
1891 /* We can handle 16 bit signed values with an addiu to
1892 $zero. No need to ever use daddiu here, since $zero and
1893 the result are always correct in 32 bit mode. */
1894 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
1895 (int) BFD_RELOC_LO16);
1896 return;
1897 }
1898 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
1899 {
1900 /* We can handle 16 bit unsigned values with an ori to
1901 $zero. */
1902 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
1903 (int) BFD_RELOC_LO16);
1904 return;
1905 }
98bfd087
ILT
1906 else if ((((ep->X_add_number &~ (offsetT) 0x7fffffff) == 0
1907 || ((ep->X_add_number &~ (offsetT) 0x7fffffff)
1908 == ~ (offsetT) 0x7fffffff))
1909 && (! dbl
1910 || ! ep->X_unsigned
1911 || sizeof (ep->X_add_number) > 4
1912 || (ep->X_add_number & 0x80000000) == 0))
1913 || (mips_isa < 3
1914 && (ep->X_add_number &~ 0xffffffff) == 0))
847a01cd
ILT
1915 {
1916 /* 32 bit values require an lui. */
1917 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
1918 (int) BFD_RELOC_HI16);
1919 if ((ep->X_add_number & 0xffff) != 0)
1920 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
1921 (int) BFD_RELOC_LO16);
1922 return;
1923 }
7811254c
ILT
1924 else
1925 {
1926 /* 32 bit value with high bit set being loaded into a 64 bit
1927 register. We can't use lui, because that would
1928 incorrectly set the 32 high bits. */
1929 generic_bignum[3] = 0;
1930 generic_bignum[2] = 0;
1931 generic_bignum[1] = (ep->X_add_number >> 16) & 0xffff;
1932 generic_bignum[0] = ep->X_add_number & 0xffff;
1933 tmp.X_op = O_big;
1934 tmp.X_add_number = 4;
1935 ep = &tmp;
1936 }
6e8dda9c 1937 }
847a01cd
ILT
1938
1939 /* The value is larger than 32 bits. */
1940
1941 if (mips_isa < 3)
670a50eb 1942 {
6e8dda9c 1943 as_bad ("Number larger than 32 bits");
0dd2d296 1944 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
9226253a 1945 (int) BFD_RELOC_LO16);
847a01cd 1946 return;
6e8dda9c 1947 }
6e8dda9c 1948
847a01cd
ILT
1949 if (ep->X_op != O_big)
1950 {
6e8dda9c
ILT
1951 hi32 = *ep;
1952 shift = 32;
1953 hi32.X_add_number >>= shift;
1954 hi32.X_add_number &= 0xffffffff;
1955 if ((hi32.X_add_number & 0x80000000) != 0)
1956 hi32.X_add_number |= ~ (offsetT) 0xffffffff;
6e8dda9c
ILT
1957 lo32 = *ep;
1958 lo32.X_add_number &= 0xffffffff;
670a50eb 1959 }
847a01cd
ILT
1960 else
1961 {
1962 assert (ep->X_add_number > 2);
1963 if (ep->X_add_number == 3)
1964 generic_bignum[3] = 0;
1965 else if (ep->X_add_number > 4)
1966 as_bad ("Number larger than 64 bits");
1967 lo32.X_op = O_constant;
1968 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
1969 hi32.X_op = O_constant;
1970 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
1971 }
1972
d8a1c247
KR
1973 if (hi32.X_add_number == 0)
1974 freg = 0;
1975 else
1976 {
fb251650
ILT
1977 if (hi32.X_add_number == 0xffffffff)
1978 {
1979 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
1980 {
1981 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j", reg, 0,
1982 (int) BFD_RELOC_LO16);
1983 return;
1984 }
1985 if (lo32.X_add_number & 0x80000000)
1986 {
1987 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
1988 (int) BFD_RELOC_HI16);
1989 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, reg,
1990 (int) BFD_RELOC_LO16);
1991 return;
1992 }
1993 }
d8a1c247
KR
1994 load_register (counter, reg, &hi32, 0);
1995 freg = reg;
1996 }
847a01cd 1997 if ((lo32.X_add_number & 0xffff0000) == 0)
d8a1c247
KR
1998 {
1999 if (freg != 0)
2000 {
2001 macro_build ((char *) NULL, counter, NULL, "dsll32", "d,w,<", reg,
2002 freg, 0);
2003 freg = reg;
2004 }
2005 }
847a01cd
ILT
2006 else
2007 {
2008 expressionS mid16;
2009
fb251650
ILT
2010 if ((freg == 0) && (lo32.X_add_number == 0xffffffff))
2011 {
2012 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
2013 (int) BFD_RELOC_HI16);
2014 macro_build ((char *) NULL, counter, NULL, "dsrl32", "d,w,<", reg,
2015 reg, 32);
2016 return;
2017 }
2018
d8a1c247
KR
2019 if (freg != 0)
2020 {
2021 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
2022 freg, 16);
2023 freg = reg;
2024 }
847a01cd
ILT
2025 mid16 = lo32;
2026 mid16.X_add_number >>= 16;
2027 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
d8a1c247 2028 freg, (int) BFD_RELOC_LO16);
847a01cd
ILT
2029 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
2030 reg, 16);
d8a1c247 2031 freg = reg;
847a01cd
ILT
2032 }
2033 if ((lo32.X_add_number & 0xffff) != 0)
d8a1c247 2034 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
847a01cd 2035 (int) BFD_RELOC_LO16);
3d3c5039
ILT
2036}
2037
0dd2d296
ILT
2038/* Load an address into a register. */
2039
2040static void
2041load_address (counter, reg, ep)
2042 int *counter;
2043 int reg;
2044 expressionS *ep;
2045{
2046 char *p;
2047
2048 if (ep->X_op != O_constant
2049 && ep->X_op != O_symbol)
2050 {
2051 as_bad ("expression too complex");
2052 ep->X_op = O_constant;
2053 }
2054
2055 if (ep->X_op == O_constant)
d9aba805 2056 {
d8a1c247 2057 load_register (counter, reg, ep, 0);
d9aba805
ILT
2058 return;
2059 }
2060
2061 if (mips_pic == NO_PIC)
0dd2d296
ILT
2062 {
2063 /* If this is a reference to a GP relative symbol, we want
2064 addiu $reg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
2065 Otherwise we want
04cb3372 2066 lui $reg,<sym> (BFD_RELOC_HI16_S)
0dd2d296
ILT
2067 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
2068 If we have an addend, we always use the latter form. */
7a15a226
ILT
2069 if ((valueT) ep->X_add_number >= MAX_GPREL_OFFSET
2070 || nopic_need_relax (ep->X_add_symbol))
0dd2d296
ILT
2071 p = NULL;
2072 else
2073 {
8ea7f4e8 2074 frag_grow (20);
0dd2d296
ILT
2075 macro_build ((char *) NULL, counter, ep,
2076 mips_isa < 3 ? "addiu" : "daddiu",
2077 "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
2078 p = frag_var (rs_machine_dependent, 8, 0,
847a01cd 2079 RELAX_ENCODE (4, 8, 0, 4, 0, mips_warn_about_macros),
0dd2d296
ILT
2080 ep->X_add_symbol, (long) 0, (char *) NULL);
2081 }
2082 macro_build_lui (p, counter, ep, reg);
2083 if (p != NULL)
2084 p += 4;
2085 macro_build (p, counter, ep,
2086 mips_isa < 3 ? "addiu" : "daddiu",
2087 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
2088 }
fb251650 2089 else if (mips_pic == SVR4_PIC && ! mips_big_got)
0dd2d296
ILT
2090 {
2091 expressionS ex;
2092
2093 /* If this is a reference to an external symbol, we want
2094 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
2095 Otherwise we want
2096 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
2097 nop
2098 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d9aba805 2099 If there is a constant, it must be added in after. */
0dd2d296
ILT
2100 ex.X_add_number = ep->X_add_number;
2101 ep->X_add_number = 0;
8ea7f4e8 2102 frag_grow (20);
0dd2d296
ILT
2103 macro_build ((char *) NULL, counter, ep,
2104 mips_isa < 3 ? "lw" : "ld",
2105 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
2106 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
2107 p = frag_var (rs_machine_dependent, 4, 0,
2108 RELAX_ENCODE (0, 4, -8, 0, 0, mips_warn_about_macros),
2109 ep->X_add_symbol, (long) 0, (char *) NULL);
2110 macro_build (p, counter, ep,
2111 mips_isa < 3 ? "addiu" : "daddiu",
2112 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
2113 if (ex.X_add_number != 0)
2114 {
2115 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
2116 as_bad ("PIC code offset overflow (max 16 signed bits)");
2117 ex.X_op = O_constant;
fb251650
ILT
2118 macro_build ((char *) NULL, counter, &ex,
2119 mips_isa < 3 ? "addiu" : "daddiu",
2120 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
2121 }
2122 }
2123 else if (mips_pic == SVR4_PIC)
2124 {
2125 expressionS ex;
2126 int off;
2127
2128 /* This is the large GOT case. If this is a reference to an
2129 external symbol, we want
2130 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
2131 addu $reg,$reg,$gp
2132 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
2133 Otherwise, for a reference to a local symbol, we want
2134 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
2135 nop
2136 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
2137 If there is a constant, it must be added in after. */
2138 ex.X_add_number = ep->X_add_number;
2139 ep->X_add_number = 0;
2140 if (reg_needs_delay (GP))
2141 off = 4;
2142 else
2143 off = 0;
2144 frag_grow (32);
2145 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
2146 (int) BFD_RELOC_MIPS_GOT_HI16);
2147 macro_build ((char *) NULL, counter, (expressionS *) NULL,
2148 mips_isa < 3 ? "addu" : "daddu",
2149 "d,v,t", reg, reg, GP);
2150 macro_build ((char *) NULL, counter, ep,
2151 mips_isa < 3 ? "lw" : "ld",
2152 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
2153 p = frag_var (rs_machine_dependent, 12 + off, 0,
2154 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
2155 mips_warn_about_macros),
2156 ep->X_add_symbol, (long) 0, (char *) NULL);
2157 if (off > 0)
2158 {
2159 /* We need a nop before loading from $gp. This special
2160 check is required because the lui which starts the main
2161 instruction stream does not refer to $gp, and so will not
2162 insert the nop which may be required. */
2163 macro_build (p, counter, (expressionS *) NULL, "nop", "");
2164 p += 4;
2165 }
2166 macro_build (p, counter, ep,
2167 mips_isa < 3 ? "lw" : "ld",
2168 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
2169 p += 4;
2170 macro_build (p, counter, (expressionS *) NULL, "nop", "");
2171 p += 4;
2172 macro_build (p, counter, ep,
2173 mips_isa < 3 ? "addiu" : "daddiu",
2174 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
2175 if (ex.X_add_number != 0)
2176 {
2177 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
2178 as_bad ("PIC code offset overflow (max 16 signed bits)");
2179 ex.X_op = O_constant;
2180 macro_build ((char *) NULL, counter, &ex,
0dd2d296
ILT
2181 mips_isa < 3 ? "addiu" : "daddiu",
2182 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
2183 }
d9aba805
ILT
2184 }
2185 else if (mips_pic == EMBEDDED_PIC)
2186 {
2187 /* We always do
2188 addiu $reg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
2189 */
2190 macro_build ((char *) NULL, counter, ep,
2191 mips_isa < 3 ? "addiu" : "daddiu",
2192 "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
2193 }
2194 else
2195 abort ();
0dd2d296
ILT
2196}
2197
3d3c5039
ILT
2198/*
2199 * Build macros
2200 * This routine implements the seemingly endless macro or synthesized
2201 * instructions and addressing modes in the mips assembly language. Many
2202 * of these macros are simple and are similar to each other. These could
2203 * probably be handled by some kind of table or grammer aproach instead of
2204 * this verbose method. Others are not simple macros but are more like
2205 * optimizing code generation.
2206 * One interesting optimization is when several store macros appear
2207 * consecutivly that would load AT with the upper half of the same address.
2208 * The ensuing load upper instructions are ommited. This implies some kind
2209 * of global optimization. We currently only optimize within a single macro.
2210 * For many of the load and store macros if the address is specified as a
2211 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
2212 * first load register 'at' with zero and use it as the base register. The
2213 * mips assembler simply uses register $zero. Just one tiny optimization
2214 * we're missing.
2215 */
2216static void
2217macro (ip)
2218 struct mips_cl_insn *ip;
2219{
670a50eb
ILT
2220 register int treg, sreg, dreg, breg;
2221 int tempreg;
2222 int mask;
2223 int icnt = 0;
2224 int used_at;
670a50eb
ILT
2225 expressionS expr1;
2226 const char *s;
8358c818 2227 const char *s2;
670a50eb 2228 const char *fmt;
8358c818
ILT
2229 int likely = 0;
2230 int dbl = 0;
2231 int coproc = 0;
b2b8c24e 2232 int lr = 0;
6e8dda9c 2233 offsetT maxnum;
adcf2b9d 2234 int off;
9226253a 2235 bfd_reloc_code_real_type r;
0dd2d296 2236 char *p;
55933a58 2237 int hold_mips_optimize;
670a50eb
ILT
2238
2239 treg = (ip->insn_opcode >> 16) & 0x1f;
2240 dreg = (ip->insn_opcode >> 11) & 0x1f;
2241 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
2242 mask = ip->insn_mo->mask;
2243
5ac34ac3
ILT
2244 expr1.X_op = O_constant;
2245 expr1.X_op_symbol = NULL;
670a50eb
ILT
2246 expr1.X_add_symbol = NULL;
2247 expr1.X_add_number = 1;
2248
2249 switch (mask)
2250 {
6e8dda9c
ILT
2251 case M_DABS:
2252 dbl = 1;
3d3c5039 2253 case M_ABS:
6e8dda9c
ILT
2254 /* bgez $a0,.+12
2255 move v0,$a0
2256 sub v0,$zero,$a0
2257 */
3d3c5039 2258
becfe05e
ILT
2259 mips_emit_delays ();
2260 ++mips_noreorder;
0dd2d296 2261 mips_any_noreorder = 1;
3d3c5039 2262
670a50eb 2263 expr1.X_add_number = 8;
0dd2d296 2264 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
6e8dda9c 2265 if (dreg == sreg)
0dd2d296 2266 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
6e8dda9c 2267 else
0dd2d296
ILT
2268 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, sreg, 0);
2269 macro_build ((char *) NULL, &icnt, NULL,
6e8dda9c
ILT
2270 dbl ? "dsub" : "sub",
2271 "d,v,t", dreg, 0, sreg);
3d3c5039 2272
becfe05e 2273 --mips_noreorder;
670a50eb 2274 return;
3d3c5039
ILT
2275
2276 case M_ADD_I:
8358c818
ILT
2277 s = "addi";
2278 s2 = "add";
2279 goto do_addi;
3d3c5039 2280 case M_ADDU_I:
8358c818
ILT
2281 s = "addiu";
2282 s2 = "addu";
2283 goto do_addi;
2284 case M_DADD_I:
6e8dda9c 2285 dbl = 1;
8358c818
ILT
2286 s = "daddi";
2287 s2 = "dadd";
2288 goto do_addi;
2289 case M_DADDU_I:
6e8dda9c 2290 dbl = 1;
8358c818
ILT
2291 s = "daddiu";
2292 s2 = "daddu";
2293 do_addi:
6e8dda9c 2294 if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
670a50eb 2295 {
0dd2d296 2296 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
9226253a 2297 (int) BFD_RELOC_LO16);
670a50eb 2298 return;
3d3c5039 2299 }
d8a1c247 2300 load_register (&icnt, AT, &imm_expr, dbl);
0dd2d296 2301 macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
670a50eb 2302 break;
3d3c5039
ILT
2303
2304 case M_AND_I:
6e8dda9c
ILT
2305 s = "andi";
2306 s2 = "and";
2307 goto do_bit;
3d3c5039 2308 case M_OR_I:
6e8dda9c
ILT
2309 s = "ori";
2310 s2 = "or";
2311 goto do_bit;
3d3c5039 2312 case M_NOR_I:
6e8dda9c
ILT
2313 s = "";
2314 s2 = "nor";
2315 goto do_bit;
3d3c5039 2316 case M_XOR_I:
6e8dda9c
ILT
2317 s = "xori";
2318 s2 = "xor";
2319 do_bit:
2320 if (imm_expr.X_add_number >= 0 && imm_expr.X_add_number < 0x10000)
670a50eb 2321 {
6e8dda9c 2322 if (mask != M_NOR_I)
0dd2d296
ILT
2323 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
2324 sreg, (int) BFD_RELOC_LO16);
6e8dda9c 2325 else
670a50eb 2326 {
0dd2d296
ILT
2327 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
2328 treg, sreg, (int) BFD_RELOC_LO16);
1c803e52 2329 macro_build ((char *) NULL, &icnt, NULL, "nor", "d,v,t",
0dd2d296 2330 treg, treg, 0);
3d3c5039 2331 }
6e8dda9c 2332 return;
3d3c5039 2333 }
6e8dda9c 2334
d8a1c247 2335 load_register (&icnt, AT, &imm_expr, 0);
0dd2d296 2336 macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
670a50eb 2337 break;
3d3c5039
ILT
2338
2339 case M_BEQ_I:
8358c818
ILT
2340 s = "beq";
2341 goto beq_i;
2342 case M_BEQL_I:
2343 s = "beql";
2344 likely = 1;
2345 goto beq_i;
3d3c5039 2346 case M_BNE_I:
8358c818
ILT
2347 s = "bne";
2348 goto beq_i;
2349 case M_BNEL_I:
2350 s = "bnel";
2351 likely = 1;
2352 beq_i:
670a50eb
ILT
2353 if (imm_expr.X_add_number == 0)
2354 {
0dd2d296
ILT
2355 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
2356 0);
670a50eb
ILT
2357 return;
2358 }
d8a1c247 2359 load_register (&icnt, AT, &imm_expr, 0);
0dd2d296 2360 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
670a50eb 2361 break;
3d3c5039 2362
8358c818
ILT
2363 case M_BGEL:
2364 likely = 1;
3d3c5039 2365 case M_BGE:
670a50eb
ILT
2366 if (treg == 0)
2367 {
0dd2d296 2368 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2369 likely ? "bgezl" : "bgez",
2370 "s,p", sreg);
670a50eb 2371 return;
3d3c5039 2372 }
9a7d824a
ILT
2373 if (sreg == 0)
2374 {
0dd2d296 2375 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2376 likely ? "blezl" : "blez",
2377 "s,p", treg);
9a7d824a
ILT
2378 return;
2379 }
0dd2d296
ILT
2380 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
2381 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2382 likely ? "beql" : "beq",
2383 "s,t,p", AT, 0);
670a50eb 2384 break;
3d3c5039 2385
8358c818
ILT
2386 case M_BGTL_I:
2387 likely = 1;
3d3c5039 2388 case M_BGT_I:
9a7d824a 2389 /* check for > max integer */
6e8dda9c
ILT
2390 maxnum = 0x7fffffff;
2391 if (mips_isa >= 3)
2392 {
2393 maxnum <<= 16;
2394 maxnum |= 0xffff;
2395 maxnum <<= 16;
2396 maxnum |= 0xffff;
2397 }
7b777690
ILT
2398 if (imm_expr.X_add_number >= maxnum
2399 && (mips_isa < 3 || sizeof (maxnum) > 4))
9a7d824a
ILT
2400 {
2401 do_false:
2402 /* result is always false */
8358c818
ILT
2403 if (! likely)
2404 {
2405 as_warn ("Branch %s is always false (nop)", ip->insn_mo->name);
0dd2d296 2406 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
8358c818
ILT
2407 }
2408 else
2409 {
2410 as_warn ("Branch likely %s is always false", ip->insn_mo->name);
0dd2d296
ILT
2411 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
2412 "s,t,p", 0, 0);
8358c818 2413 }
9a7d824a
ILT
2414 return;
2415 }
670a50eb
ILT
2416 imm_expr.X_add_number++;
2417 /* FALLTHROUGH */
3d3c5039 2418 case M_BGE_I:
8358c818
ILT
2419 case M_BGEL_I:
2420 if (mask == M_BGEL_I)
2421 likely = 1;
670a50eb
ILT
2422 if (imm_expr.X_add_number == 0)
2423 {
0dd2d296 2424 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2425 likely ? "bgezl" : "bgez",
2426 "s,p", sreg);
670a50eb 2427 return;
3d3c5039 2428 }
670a50eb
ILT
2429 if (imm_expr.X_add_number == 1)
2430 {
0dd2d296 2431 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2432 likely ? "bgtzl" : "bgtz",
2433 "s,p", sreg);
670a50eb 2434 return;
3d3c5039 2435 }
6e8dda9c
ILT
2436 maxnum = 0x7fffffff;
2437 if (mips_isa >= 3)
2438 {
2439 maxnum <<= 16;
2440 maxnum |= 0xffff;
2441 maxnum <<= 16;
2442 maxnum |= 0xffff;
2443 }
2444 maxnum = - maxnum - 1;
7b777690
ILT
2445 if (imm_expr.X_add_number <= maxnum
2446 && (mips_isa < 3 || sizeof (maxnum) > 4))
9a7d824a
ILT
2447 {
2448 do_true:
2449 /* result is always true */
2450 as_warn ("Branch %s is always true", ip->insn_mo->name);
0dd2d296 2451 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
9a7d824a
ILT
2452 return;
2453 }
6e8dda9c 2454 set_at (&icnt, sreg, 0);
0dd2d296 2455 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2456 likely ? "beql" : "beq",
2457 "s,t,p", AT, 0);
670a50eb 2458 break;
3d3c5039 2459
8358c818
ILT
2460 case M_BGEUL:
2461 likely = 1;
3d3c5039 2462 case M_BGEU:
670a50eb 2463 if (treg == 0)
9a7d824a
ILT
2464 goto do_true;
2465 if (sreg == 0)
670a50eb 2466 {
0dd2d296 2467 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2468 likely ? "beql" : "beq",
2469 "s,t,p", 0, treg);
670a50eb 2470 return;
3d3c5039 2471 }
0dd2d296
ILT
2472 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
2473 treg);
2474 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2475 likely ? "beql" : "beq",
2476 "s,t,p", AT, 0);
670a50eb 2477 break;
3d3c5039 2478
8358c818
ILT
2479 case M_BGTUL_I:
2480 likely = 1;
9a7d824a 2481 case M_BGTU_I:
6e8dda9c 2482 if (sreg == 0 || imm_expr.X_add_number == -1)
9a7d824a
ILT
2483 goto do_false;
2484 imm_expr.X_add_number++;
2485 /* FALLTHROUGH */
3d3c5039 2486 case M_BGEU_I:
8358c818
ILT
2487 case M_BGEUL_I:
2488 if (mask == M_BGEUL_I)
2489 likely = 1;
670a50eb 2490 if (imm_expr.X_add_number == 0)
9a7d824a 2491 goto do_true;
670a50eb
ILT
2492 if (imm_expr.X_add_number == 1)
2493 {
0dd2d296 2494 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2495 likely ? "bnel" : "bne",
2496 "s,t,p", sreg, 0);
670a50eb 2497 return;
3d3c5039 2498 }
6e8dda9c 2499 set_at (&icnt, sreg, 1);
0dd2d296 2500 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2501 likely ? "beql" : "beq",
2502 "s,t,p", AT, 0);
670a50eb 2503 break;
3d3c5039 2504
8358c818
ILT
2505 case M_BGTL:
2506 likely = 1;
3d3c5039 2507 case M_BGT:
670a50eb
ILT
2508 if (treg == 0)
2509 {
0dd2d296 2510 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2511 likely ? "bgtzl" : "bgtz",
2512 "s,p", sreg);
670a50eb 2513 return;
3d3c5039 2514 }
9a7d824a
ILT
2515 if (sreg == 0)
2516 {
0dd2d296 2517 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2518 likely ? "bltzl" : "bltz",
2519 "s,p", treg);
9a7d824a
ILT
2520 return;
2521 }
0dd2d296
ILT
2522 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
2523 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2524 likely ? "bnel" : "bne",
2525 "s,t,p", AT, 0);
670a50eb 2526 break;
3d3c5039 2527
8358c818
ILT
2528 case M_BGTUL:
2529 likely = 1;
3d3c5039 2530 case M_BGTU:
670a50eb
ILT
2531 if (treg == 0)
2532 {
0dd2d296 2533 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2534 likely ? "bnel" : "bne",
2535 "s,t,p", sreg, 0);
670a50eb 2536 return;
3d3c5039 2537 }
9a7d824a
ILT
2538 if (sreg == 0)
2539 goto do_false;
0dd2d296
ILT
2540 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
2541 sreg);
2542 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2543 likely ? "bnel" : "bne",
2544 "s,t,p", AT, 0);
670a50eb 2545 break;
3d3c5039 2546
8358c818
ILT
2547 case M_BLEL:
2548 likely = 1;
3d3c5039 2549 case M_BLE:
670a50eb
ILT
2550 if (treg == 0)
2551 {
0dd2d296 2552 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2553 likely ? "blezl" : "blez",
2554 "s,p", sreg);
670a50eb
ILT
2555 return;
2556 }
9a7d824a
ILT
2557 if (sreg == 0)
2558 {
0dd2d296 2559 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2560 likely ? "bgezl" : "bgez",
2561 "s,p", treg);
9a7d824a
ILT
2562 return;
2563 }
0dd2d296
ILT
2564 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
2565 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2566 likely ? "beql" : "beq",
2567 "s,t,p", AT, 0);
670a50eb 2568 break;
3d3c5039 2569
8358c818
ILT
2570 case M_BLEL_I:
2571 likely = 1;
3d3c5039 2572 case M_BLE_I:
6e8dda9c
ILT
2573 maxnum = 0x7fffffff;
2574 if (mips_isa >= 3)
2575 {
2576 maxnum <<= 16;
2577 maxnum |= 0xffff;
2578 maxnum <<= 16;
2579 maxnum |= 0xffff;
2580 }
7b777690
ILT
2581 if (imm_expr.X_add_number >= maxnum
2582 && (mips_isa < 3 || sizeof (maxnum) > 4))
9a7d824a
ILT
2583 goto do_true;
2584 imm_expr.X_add_number++;
2585 /* FALLTHROUGH */
9a7d824a 2586 case M_BLT_I:
8358c818
ILT
2587 case M_BLTL_I:
2588 if (mask == M_BLTL_I)
2589 likely = 1;
670a50eb
ILT
2590 if (imm_expr.X_add_number == 0)
2591 {
0dd2d296 2592 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2593 likely ? "bltzl" : "bltz",
2594 "s,p", sreg);
670a50eb
ILT
2595 return;
2596 }
9a7d824a 2597 if (imm_expr.X_add_number == 1)
670a50eb 2598 {
0dd2d296 2599 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2600 likely ? "blezl" : "blez",
2601 "s,p", sreg);
670a50eb
ILT
2602 return;
2603 }
6e8dda9c 2604 set_at (&icnt, sreg, 0);
0dd2d296 2605 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2606 likely ? "bnel" : "bne",
2607 "s,t,p", AT, 0);
670a50eb 2608 break;
3d3c5039 2609
8358c818
ILT
2610 case M_BLEUL:
2611 likely = 1;
3d3c5039 2612 case M_BLEU:
670a50eb
ILT
2613 if (treg == 0)
2614 {
0dd2d296 2615 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2616 likely ? "beql" : "beq",
2617 "s,t,p", sreg, 0);
670a50eb 2618 return;
3d3c5039 2619 }
9a7d824a
ILT
2620 if (sreg == 0)
2621 goto do_true;
0dd2d296
ILT
2622 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
2623 sreg);
2624 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2625 likely ? "beql" : "beq",
2626 "s,t,p", AT, 0);
670a50eb 2627 break;
3d3c5039 2628
8358c818
ILT
2629 case M_BLEUL_I:
2630 likely = 1;
3d3c5039 2631 case M_BLEU_I:
6e8dda9c 2632 if (sreg == 0 || imm_expr.X_add_number == -1)
9a7d824a
ILT
2633 goto do_true;
2634 imm_expr.X_add_number++;
2635 /* FALLTHROUGH */
9a7d824a 2636 case M_BLTU_I:
8358c818
ILT
2637 case M_BLTUL_I:
2638 if (mask == M_BLTUL_I)
2639 likely = 1;
670a50eb 2640 if (imm_expr.X_add_number == 0)
9a7d824a
ILT
2641 goto do_false;
2642 if (imm_expr.X_add_number == 1)
670a50eb 2643 {
0dd2d296 2644 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2645 likely ? "beql" : "beq",
2646 "s,t,p", sreg, 0);
670a50eb 2647 return;
3d3c5039 2648 }
6e8dda9c 2649 set_at (&icnt, sreg, 1);
0dd2d296 2650 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2651 likely ? "bnel" : "bne",
2652 "s,t,p", AT, 0);
670a50eb 2653 break;
3d3c5039 2654
8358c818
ILT
2655 case M_BLTL:
2656 likely = 1;
3d3c5039 2657 case M_BLT:
670a50eb
ILT
2658 if (treg == 0)
2659 {
0dd2d296 2660 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2661 likely ? "bltzl" : "bltz",
2662 "s,p", sreg);
670a50eb 2663 return;
3d3c5039 2664 }
9a7d824a 2665 if (sreg == 0)
670a50eb 2666 {
0dd2d296 2667 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2668 likely ? "bgtzl" : "bgtz",
2669 "s,p", treg);
670a50eb 2670 return;
3d3c5039 2671 }
0dd2d296
ILT
2672 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
2673 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2674 likely ? "bnel" : "bne",
2675 "s,t,p", AT, 0);
670a50eb 2676 break;
3d3c5039 2677
8358c818
ILT
2678 case M_BLTUL:
2679 likely = 1;
3d3c5039 2680 case M_BLTU:
670a50eb 2681 if (treg == 0)
9a7d824a
ILT
2682 goto do_false;
2683 if (sreg == 0)
670a50eb 2684 {
0dd2d296 2685 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2686 likely ? "bnel" : "bne",
2687 "s,t,p", 0, treg);
670a50eb
ILT
2688 return;
2689 }
0dd2d296
ILT
2690 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
2691 treg);
2692 macro_build ((char *) NULL, &icnt, &offset_expr,
8358c818
ILT
2693 likely ? "bnel" : "bne",
2694 "s,t,p", AT, 0);
670a50eb 2695 break;
3d3c5039 2696
8358c818
ILT
2697 case M_DDIV_3:
2698 dbl = 1;
3d3c5039 2699 case M_DIV_3:
8358c818
ILT
2700 s = "mflo";
2701 goto do_div3;
2702 case M_DREM_3:
2703 dbl = 1;
3d3c5039 2704 case M_REM_3:
8358c818
ILT
2705 s = "mfhi";
2706 do_div3:
670a50eb
ILT
2707 if (treg == 0)
2708 {
2709 as_warn ("Divide by zero.");
8ea7f4e8
ILT
2710 if (mips_trap)
2711 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
2712 else
2713 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
670a50eb
ILT
2714 return;
2715 }
2716
becfe05e
ILT
2717 mips_emit_delays ();
2718 ++mips_noreorder;
0dd2d296
ILT
2719 mips_any_noreorder = 1;
2720 macro_build ((char *) NULL, &icnt, NULL,
8358c818 2721 dbl ? "ddiv" : "div",
ff3a5c18 2722 "z,s,t", sreg, treg);
8ea7f4e8
ILT
2723 if (mips_trap)
2724 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
2725 else
2726 {
2727 expr1.X_add_number = 8;
2728 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2729 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
2730 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
2731 }
670a50eb 2732 expr1.X_add_number = -1;
0dd2d296 2733 macro_build ((char *) NULL, &icnt, &expr1,
8358c818 2734 dbl ? "daddiu" : "addiu",
9226253a 2735 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
8ea7f4e8 2736 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
0dd2d296 2737 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
8358c818
ILT
2738 if (dbl)
2739 {
2740 expr1.X_add_number = 1;
0dd2d296 2741 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
9226253a 2742 (int) BFD_RELOC_LO16);
0dd2d296
ILT
2743 macro_build ((char *) NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT,
2744 31);
8358c818
ILT
2745 }
2746 else
2747 {
2748 expr1.X_add_number = 0x80000000;
ecd4ca1c
ILT
2749 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
2750 (int) BFD_RELOC_HI16);
8358c818 2751 }
8ea7f4e8
ILT
2752 if (mips_trap)
2753 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", sreg, AT);
2754 else
2755 {
2756 expr1.X_add_number = 8;
2757 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
2758 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
2759 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
2760 }
becfe05e 2761 --mips_noreorder;
0dd2d296 2762 macro_build ((char *) NULL, &icnt, NULL, s, "d", dreg);
670a50eb 2763 break;
3d3c5039
ILT
2764
2765 case M_DIV_3I:
8358c818
ILT
2766 s = "div";
2767 s2 = "mflo";
2768 goto do_divi;
3d3c5039 2769 case M_DIVU_3I:
8358c818
ILT
2770 s = "divu";
2771 s2 = "mflo";
2772 goto do_divi;
3d3c5039 2773 case M_REM_3I:
8358c818
ILT
2774 s = "div";
2775 s2 = "mfhi";
2776 goto do_divi;
3d3c5039 2777 case M_REMU_3I:
8358c818
ILT
2778 s = "divu";
2779 s2 = "mfhi";
2780 goto do_divi;
2781 case M_DDIV_3I:
2782 dbl = 1;
2783 s = "ddiv";
2784 s2 = "mflo";
2785 goto do_divi;
2786 case M_DDIVU_3I:
2787 dbl = 1;
2788 s = "ddivu";
2789 s2 = "mflo";
2790 goto do_divi;
2791 case M_DREM_3I:
2792 dbl = 1;
2793 s = "ddiv";
2794 s2 = "mfhi";
2795 goto do_divi;
2796 case M_DREMU_3I:
2797 dbl = 1;
2798 s = "ddivu";
2799 s2 = "mfhi";
2800 do_divi:
670a50eb
ILT
2801 if (imm_expr.X_add_number == 0)
2802 {
2803 as_warn ("Divide by zero.");
8ea7f4e8
ILT
2804 if (mips_trap)
2805 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
2806 else
2807 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
670a50eb
ILT
2808 return;
2809 }
2810 if (imm_expr.X_add_number == 1)
2811 {
8358c818 2812 if (strcmp (s2, "mflo") == 0)
0dd2d296
ILT
2813 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg,
2814 sreg);
3d3c5039 2815 else
0dd2d296 2816 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
3d3c5039
ILT
2817 return;
2818 }
8358c818
ILT
2819 if (imm_expr.X_add_number == -1
2820 && s[strlen (s) - 1] != 'u')
2821 {
2822 if (strcmp (s2, "mflo") == 0)
2823 {
2824 if (dbl)
0dd2d296
ILT
2825 macro_build ((char *) NULL, &icnt, NULL, "dneg", "d,w", dreg,
2826 sreg);
8358c818 2827 else
0dd2d296
ILT
2828 macro_build ((char *) NULL, &icnt, NULL, "neg", "d,w", dreg,
2829 sreg);
8358c818
ILT
2830 }
2831 else
0dd2d296 2832 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
8358c818
ILT
2833 return;
2834 }
3d3c5039 2835
d8a1c247 2836 load_register (&icnt, AT, &imm_expr, dbl);
0dd2d296
ILT
2837 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
2838 macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
670a50eb
ILT
2839 break;
2840
2841 case M_DIVU_3:
8358c818
ILT
2842 s = "divu";
2843 s2 = "mflo";
2844 goto do_divu3;
670a50eb 2845 case M_REMU_3:
8358c818
ILT
2846 s = "divu";
2847 s2 = "mfhi";
2848 goto do_divu3;
2849 case M_DDIVU_3:
2850 s = "ddivu";
2851 s2 = "mflo";
2852 goto do_divu3;
2853 case M_DREMU_3:
2854 s = "ddivu";
2855 s2 = "mfhi";
2856 do_divu3:
becfe05e
ILT
2857 mips_emit_delays ();
2858 ++mips_noreorder;
0dd2d296
ILT
2859 mips_any_noreorder = 1;
2860 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
8ea7f4e8
ILT
2861 if (mips_trap)
2862 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
2863 else
2864 {
2865 expr1.X_add_number = 8;
2866 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2867 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
2868 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
2869 }
becfe05e 2870 --mips_noreorder;
0dd2d296 2871 macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
670a50eb 2872 return;
3d3c5039 2873
d8a1c247
KR
2874 case M_DLA_AB:
2875 dbl = 1;
0dd2d296 2876 case M_LA_AB:
d9aba805
ILT
2877 /* Load the address of a symbol into a register. If breg is not
2878 zero, we then add a base register to it. */
ecd4ca1c
ILT
2879
2880 /* When generating embedded PIC code, we permit expressions of
2881 the form
2882 la $4,foo-bar
2883 where bar is an address in the .text section. These are used
2884 when getting the addresses of functions. We don't permit
2885 X_add_number to be non-zero, because if the symbol is
2886 external the relaxing code needs to know that any addend is
2887 purely the offset to X_op_symbol. */
2888 if (mips_pic == EMBEDDED_PIC
2889 && offset_expr.X_op == O_subtract
2890 && now_seg == text_section
847a01cd
ILT
2891 && (offset_expr.X_op_symbol->sy_value.X_op == O_constant
2892 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == text_section
2893 : (offset_expr.X_op_symbol->sy_value.X_op == O_symbol
2894 && (S_GET_SEGMENT (offset_expr.X_op_symbol
2895 ->sy_value.X_add_symbol)
2896 == text_section)))
ecd4ca1c
ILT
2897 && breg == 0
2898 && offset_expr.X_add_number == 0)
2899 {
2900 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
2901 treg, (int) BFD_RELOC_PCREL_HI16_S);
2902 macro_build ((char *) NULL, &icnt, &offset_expr,
2903 mips_isa < 3 ? "addiu" : "daddiu",
2904 "t,r,j", treg, treg, (int) BFD_RELOC_PCREL_LO16);
2905 return;
2906 }
2907
0dd2d296
ILT
2908 if (offset_expr.X_op != O_symbol
2909 && offset_expr.X_op != O_constant)
670a50eb 2910 {
0dd2d296
ILT
2911 as_bad ("expression too complex");
2912 offset_expr.X_op = O_constant;
2913 }
2914
2915 if (treg == breg)
2916 {
2917 tempreg = AT;
2918 used_at = 1;
3d3c5039 2919 }
670a50eb
ILT
2920 else
2921 {
0dd2d296
ILT
2922 tempreg = treg;
2923 used_at = 0;
670a50eb 2924 }
3d3c5039 2925
5ac34ac3 2926 if (offset_expr.X_op == O_constant)
d8a1c247 2927 load_register (&icnt, tempreg, &offset_expr, dbl);
d9aba805 2928 else if (mips_pic == NO_PIC)
670a50eb 2929 {
0dd2d296
ILT
2930 /* If this is a reference to an GP relative symbol, we want
2931 addiu $tempreg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
2932 Otherwise we want
2933 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
2934 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
2935 If we have a constant, we need two instructions anyhow,
2936 so we may as well always use the latter form. */
7a15a226 2937 if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
d8a1c247 2938 || nopic_need_relax (offset_expr.X_add_symbol))
0dd2d296
ILT
2939 p = NULL;
2940 else
2941 {
8ea7f4e8 2942 frag_grow (20);
0dd2d296
ILT
2943 macro_build ((char *) NULL, &icnt, &offset_expr,
2944 mips_isa < 3 ? "addiu" : "daddiu",
2945 "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
2946 p = frag_var (rs_machine_dependent, 8, 0,
2947 RELAX_ENCODE (4, 8, 0, 4, 0,
2948 mips_warn_about_macros),
2949 offset_expr.X_add_symbol, (long) 0,
2950 (char *) NULL);
2951 }
2952 macro_build_lui (p, &icnt, &offset_expr, tempreg);
2953 if (p != NULL)
2954 p += 4;
2955 macro_build (p, &icnt, &offset_expr,
6e8dda9c 2956 mips_isa < 3 ? "addiu" : "daddiu",
0dd2d296
ILT
2957 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
2958 }
fb251650 2959 else if (mips_pic == SVR4_PIC && ! mips_big_got)
0dd2d296
ILT
2960 {
2961 /* If this is a reference to an external symbol, and there
2962 is no constant, we want
2963 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
2964 For a local symbol, we want
2965 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
2966 nop
2967 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
2968
2969 If we have a small constant, and this is a reference to
2970 an external symbol, we want
2971 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
2972 nop
2973 addiu $tempreg,$tempreg,<constant>
2974 For a local symbol, we want the same instruction
2975 sequence, but we output a BFD_RELOC_LO16 reloc on the
2976 addiu instruction.
2977
2978 If we have a large constant, and this is a reference to
2979 an external symbol, we want
2980 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
2981 lui $at,<hiconstant>
2982 addiu $at,$at,<loconstant>
2983 addu $tempreg,$tempreg,$at
2984 For a local symbol, we want the same instruction
2985 sequence, but we output a BFD_RELOC_LO16 reloc on the
2986 addiu instruction. */
2987 expr1.X_add_number = offset_expr.X_add_number;
2988 offset_expr.X_add_number = 0;
8ea7f4e8 2989 frag_grow (32);
0dd2d296 2990 macro_build ((char *) NULL, &icnt, &offset_expr,
d8a1c247 2991 dbl ? "ld" : "lw",
0dd2d296
ILT
2992 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
2993 if (expr1.X_add_number == 0)
2994 {
2995 int off;
2996
2997 if (breg == 0)
2998 off = 0;
2999 else
3000 {
3001 /* We're going to put in an addu instruction using
3002 tempreg, so we may as well insert the nop right
3003 now. */
3004 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3005 "nop", "");
3006 off = 4;
3007 }
3008 p = frag_var (rs_machine_dependent, 8 - off, 0,
3009 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
3010 (breg == 0
3011 ? mips_warn_about_macros
3012 : 0)),
3013 offset_expr.X_add_symbol, (long) 0,
3014 (char *) NULL);
3015 if (breg == 0)
3016 {
3017 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
3018 p += 4;
3019 }
3020 macro_build (p, &icnt, &expr1,
3021 mips_isa < 3 ? "addiu" : "daddiu",
3022 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
3023 /* FIXME: If breg == 0, and the next instruction uses
3024 $tempreg, then if this variant case is used an extra
3025 nop will be generated. */
3026 }
3027 else if (expr1.X_add_number >= -0x8000
3028 && expr1.X_add_number < 0x8000)
3029 {
3030 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3031 "nop", "");
3032 macro_build ((char *) NULL, &icnt, &expr1,
3033 mips_isa < 3 ? "addiu" : "daddiu",
3034 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
3035 (void) frag_var (rs_machine_dependent, 0, 0,
3036 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
3037 offset_expr.X_add_symbol, (long) 0,
3038 (char *) NULL);
3039 }
3040 else
3041 {
3042 int off1;
3043
3044 /* If we are going to add in a base register, and the
3045 target register and the base register are the same,
3046 then we are using AT as a temporary register. Since
3047 we want to load the constant into AT, we add our
3048 current AT (from the global offset table) and the
3049 register into the register now, and pretend we were
3050 not using a base register. */
3051 if (breg != treg)
3052 off1 = 0;
3053 else
3054 {
3055 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3056 "nop", "");
3057 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3058 mips_isa < 3 ? "addu" : "daddu",
3059 "d,v,t", treg, AT, breg);
3060 breg = 0;
3061 tempreg = treg;
3062 off1 = -8;
3063 }
3064
55933a58
ILT
3065 /* Set mips_optimize around the lui instruction to avoid
3066 inserting an unnecessary nop after the lw. */
3067 hold_mips_optimize = mips_optimize;
3068 mips_optimize = 2;
0dd2d296 3069 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
55933a58
ILT
3070 mips_optimize = hold_mips_optimize;
3071
0dd2d296
ILT
3072 macro_build ((char *) NULL, &icnt, &expr1,
3073 mips_isa < 3 ? "addiu" : "daddiu",
3074 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
3075 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3076 mips_isa < 3 ? "addu" : "daddu",
3077 "d,v,t", tempreg, tempreg, AT);
3078 (void) frag_var (rs_machine_dependent, 0, 0,
3079 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
3080 offset_expr.X_add_symbol, (long) 0,
3081 (char *) NULL);
3082 used_at = 1;
3083 }
670a50eb 3084 }
fb251650
ILT
3085 else if (mips_pic == SVR4_PIC)
3086 {
3087 int gpdel;
3088
3089 /* This is the large GOT case. If this is a reference to an
3090 external symbol, and there is no constant, we want
3091 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3092 addu $tempreg,$tempreg,$gp
3093 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
3094 For a local symbol, we want
3095 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3096 nop
3097 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
3098
3099 If we have a small constant, and this is a reference to
3100 an external symbol, we want
3101 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3102 addu $tempreg,$tempreg,$gp
3103 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
3104 nop
3105 addiu $tempreg,$tempreg,<constant>
3106 For a local symbol, we want
3107 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3108 nop
3109 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
3110
3111 If we have a large constant, and this is a reference to
3112 an external symbol, we want
3113 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3114 addu $tempreg,$tempreg,$gp
3115 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
3116 lui $at,<hiconstant>
3117 addiu $at,$at,<loconstant>
3118 addu $tempreg,$tempreg,$at
3119 For a local symbol, we want
3120 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3121 lui $at,<hiconstant>
3122 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
3123 addu $tempreg,$tempreg,$at
3124 */
3125 expr1.X_add_number = offset_expr.X_add_number;
3126 offset_expr.X_add_number = 0;
3127 frag_grow (52);
3128 if (reg_needs_delay (GP))
3129 gpdel = 4;
3130 else
3131 gpdel = 0;
3132 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
3133 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
3134 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3135 mips_isa < 3 ? "addu" : "daddu",
3136 "d,v,t", tempreg, tempreg, GP);
3137 macro_build ((char *) NULL, &icnt, &offset_expr,
3138 dbl ? "ld" : "lw",
3139 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
3140 tempreg);
3141 if (expr1.X_add_number == 0)
3142 {
3143 int off;
3144
3145 if (breg == 0)
3146 off = 0;
3147 else
3148 {
3149 /* We're going to put in an addu instruction using
3150 tempreg, so we may as well insert the nop right
3151 now. */
3152 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3153 "nop", "");
3154 off = 4;
3155 }
3156
3157 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
3158 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
3159 8 + gpdel, 0,
3160 (breg == 0
3161 ? mips_warn_about_macros
3162 : 0)),
3163 offset_expr.X_add_symbol, (long) 0,
3164 (char *) NULL);
3165 }
3166 else if (expr1.X_add_number >= -0x8000
3167 && expr1.X_add_number < 0x8000)
3168 {
3169 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3170 "nop", "");
3171 macro_build ((char *) NULL, &icnt, &expr1,
3172 mips_isa < 3 ? "addiu" : "daddiu",
3173 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
3174
3175 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
3176 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
3177 (breg == 0
3178 ? mips_warn_about_macros
3179 : 0)),
3180 offset_expr.X_add_symbol, (long) 0,
3181 (char *) NULL);
3182 }
3183 else
3184 {
3185 int adj, dreg;
3186
3187 /* If we are going to add in a base register, and the
3188 target register and the base register are the same,
3189 then we are using AT as a temporary register. Since
3190 we want to load the constant into AT, we add our
3191 current AT (from the global offset table) and the
3192 register into the register now, and pretend we were
3193 not using a base register. */
3194 if (breg != treg)
3195 {
3196 adj = 0;
3197 dreg = tempreg;
3198 }
3199 else
3200 {
3201 assert (tempreg == AT);
3202 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3203 "nop", "");
3204 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3205 mips_isa < 3 ? "addu" : "daddu",
3206 "d,v,t", treg, AT, breg);
3207 dreg = treg;
3208 adj = 8;
3209 }
3210
3211 /* Set mips_optimize around the lui instruction to avoid
3212 inserting an unnecessary nop after the lw. */
3213 hold_mips_optimize = mips_optimize;
3214 mips_optimize = 2;
3215 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
3216 mips_optimize = hold_mips_optimize;
3217
3218 macro_build ((char *) NULL, &icnt, &expr1,
3219 mips_isa < 3 ? "addiu" : "daddiu",
3220 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
3221 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3222 mips_isa < 3 ? "addu" : "daddu",
3223 "d,v,t", dreg, dreg, AT);
3224
3225 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
3226 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
3227 8 + gpdel, 0,
3228 (breg == 0
3229 ? mips_warn_about_macros
3230 : 0)),
3231 offset_expr.X_add_symbol, (long) 0,
3232 (char *) NULL);
3233
3234 used_at = 1;
3235 }
3236
3237 if (gpdel > 0)
3238 {
3239 /* This is needed because this instruction uses $gp, but
3240 the first instruction on the main stream does not. */
3241 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
3242 p += 4;
3243 }
3244 macro_build (p, &icnt, &offset_expr,
3245 dbl ? "ld" : "lw",
3246 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
3247 p += 4;
3248 if (expr1.X_add_number >= -0x8000
3249 && expr1.X_add_number < 0x8000)
3250 {
3251 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
3252 p += 4;
3253 macro_build (p, &icnt, &expr1,
3254 mips_isa < 3 ? "addiu" : "daddiu",
3255 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
3256 /* FIXME: If add_number is 0, and there was no base
3257 register, the external symbol case ended with a load,
3258 so if the symbol turns out to not be external, and
3259 the next instruction uses tempreg, an unnecessary nop
3260 will be inserted. */
3261 }
3262 else
3263 {
3264 if (breg == treg)
3265 {
3266 /* We must add in the base register now, as in the
3267 external symbol case. */
3268 assert (tempreg == AT);
3269 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
3270 p += 4;
3271 macro_build (p, &icnt, (expressionS *) NULL,
3272 mips_isa < 3 ? "addu" : "daddu",
3273 "d,v,t", treg, AT, breg);
3274 p += 4;
3275 tempreg = treg;
3276 /* We set breg to 0 because we have arranged to add
3277 it in in both cases. */
3278 breg = 0;
3279 }
3280
3281 macro_build_lui (p, &icnt, &expr1, AT);
3282 p += 4;
3283 macro_build (p, &icnt, &expr1,
3284 mips_isa < 3 ? "addiu" : "daddiu",
3285 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
3286 p += 4;
3287 macro_build (p, &icnt, (expressionS *) NULL,
3288 mips_isa < 3 ? "addu" : "daddu",
3289 "d,v,t", tempreg, tempreg, AT);
3290 p += 4;
3291 }
3292 }
d9aba805
ILT
3293 else if (mips_pic == EMBEDDED_PIC)
3294 {
3295 /* We use
3296 addiu $tempreg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
3297 */
3298 macro_build ((char *) NULL, &icnt, &offset_expr,
3299 mips_isa < 3 ? "addiu" : "daddiu",
3300 "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
3301 }
3302 else
3303 abort ();
0dd2d296 3304
670a50eb 3305 if (breg != 0)
0dd2d296
ILT
3306 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3307 mips_isa < 3 ? "addu" : "daddu",
3308 "d,v,t", treg, tempreg, breg);
3309
3310 if (! used_at)
3311 return;
3312
3313 break;
3314
3315 case M_J_A:
3316 /* The j instruction may not be used in PIC code, since it
3317 requires an absolute address. We convert it to a b
3318 instruction. */
d9aba805 3319 if (mips_pic == NO_PIC)
0dd2d296
ILT
3320 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
3321 else
3322 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
670a50eb 3323 return;
3d3c5039 3324
9226253a
ILT
3325 /* The jal instructions must be handled as macros because when
3326 generating PIC code they expand to multi-instruction
3327 sequences. Normally they are simple instructions. */
3328 case M_JAL_1:
3329 dreg = RA;
3330 /* Fall through. */
3331 case M_JAL_2:
d9aba805
ILT
3332 if (mips_pic == NO_PIC
3333 || mips_pic == EMBEDDED_PIC)
3334 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
3335 "d,s", dreg, sreg);
3336 else if (mips_pic == SVR4_PIC)
9226253a 3337 {
d9aba805
ILT
3338 if (sreg != PIC_CALL_REG)
3339 as_warn ("MIPS PIC call to register other than $25");
3340
0dd2d296
ILT
3341 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
3342 "d,s", dreg, sreg);
d9aba805
ILT
3343 if (mips_cprestore_offset < 0)
3344 as_warn ("No .cprestore pseudo-op used in PIC code");
3345 else
3346 {
3347 expr1.X_add_number = mips_cprestore_offset;
3348 macro_build ((char *) NULL, &icnt, &expr1,
3349 mips_isa < 3 ? "lw" : "ld",
3350 "t,o(b)", GP, (int) BFD_RELOC_LO16, mips_frame_reg);
3351 }
9226253a 3352 }
0dd2d296 3353 else
d9aba805
ILT
3354 abort ();
3355
9226253a
ILT
3356 return;
3357
3358 case M_JAL_A:
d9aba805
ILT
3359 if (mips_pic == NO_PIC)
3360 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
3361 else if (mips_pic == SVR4_PIC)
9226253a 3362 {
fb251650
ILT
3363 /* If this is a reference to an external symbol, and we are
3364 using a small GOT, we want
d9aba805
ILT
3365 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
3366 nop
3367 jalr $25
3368 nop
3369 lw $gp,cprestore($sp)
3370 The cprestore value is set using the .cprestore
fb251650
ILT
3371 pseudo-op. If we are using a big GOT, we want
3372 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
3373 addu $25,$25,$gp
3374 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
3375 nop
3376 jalr $25
3377 nop
3378 lw $gp,cprestore($sp)
3379 If the symbol is not external, we want
d9aba805
ILT
3380 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3381 nop
3382 addiu $25,$25,<sym> (BFD_RELOC_LO16)
3383 jalr $25
3384 nop
fb251650
ILT
3385 lw $gp,cprestore($sp) */
3386 frag_grow (40);
3387 if (! mips_big_got)
3388 {
3389 macro_build ((char *) NULL, &icnt, &offset_expr,
3390 mips_isa < 3 ? "lw" : "ld",
3391 "t,o(b)", PIC_CALL_REG,
3392 (int) BFD_RELOC_MIPS_CALL16, GP);
3393 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3394 "nop", "");
3395 p = frag_var (rs_machine_dependent, 4, 0,
3396 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
3397 offset_expr.X_add_symbol, (long) 0, (char *) NULL);
3398 }
3399 else
3400 {
3401 int gpdel;
3402
3403 if (reg_needs_delay (GP))
3404 gpdel = 4;
3405 else
3406 gpdel = 0;
3407 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
3408 PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
3409 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3410 mips_isa < 3 ? "addu" : "daddu",
3411 "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
3412 macro_build ((char *) NULL, &icnt, &offset_expr,
3413 mips_isa < 3 ? "lw" : "ld",
3414 "t,o(b)", PIC_CALL_REG,
3415 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
3416 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3417 "nop", "");
3418 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
3419 RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
3420 0, 0),
3421 offset_expr.X_add_symbol, (long) 0, (char *) NULL);
3422 if (gpdel > 0)
3423 {
3424 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
3425 p += 4;
3426 }
3427 macro_build (p, &icnt, &offset_expr,
3428 mips_isa < 3 ? "lw" : "ld",
3429 "t,o(b)", PIC_CALL_REG,
3430 (int) BFD_RELOC_MIPS_GOT16, GP);
3431 p += 4;
3432 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
3433 p += 4;
3434 }
d9aba805
ILT
3435 macro_build (p, &icnt, &offset_expr,
3436 mips_isa < 3 ? "addiu" : "daddiu",
3437 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
3438 (int) BFD_RELOC_LO16);
3439 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3440 "jalr", "s", PIC_CALL_REG);
3441 if (mips_cprestore_offset < 0)
3442 as_warn ("No .cprestore pseudo-op used in PIC code");
3443 else
3444 {
3445 if (mips_noreorder)
3446 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3447 "nop", "");
3448 expr1.X_add_number = mips_cprestore_offset;
3449 macro_build ((char *) NULL, &icnt, &expr1,
3450 mips_isa < 3 ? "lw" : "ld",
3451 "t,o(b)", GP, (int) BFD_RELOC_LO16,
3452 mips_frame_reg);
3453 }
0dd2d296 3454 }
d9aba805 3455 else if (mips_pic == EMBEDDED_PIC)
5b63f465
ILT
3456 {
3457 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
3458 /* The linker may expand the call to a longer sequence which
3459 uses $at, so we must break rather than return. */
3460 break;
3461 }
d9aba805
ILT
3462 else
3463 abort ();
3464
9226253a
ILT
3465 return;
3466
3d3c5039 3467 case M_LB_AB:
670a50eb
ILT
3468 s = "lb";
3469 goto ld;
3d3c5039 3470 case M_LBU_AB:
670a50eb
ILT
3471 s = "lbu";
3472 goto ld;
3d3c5039 3473 case M_LH_AB:
670a50eb
ILT
3474 s = "lh";
3475 goto ld;
3d3c5039 3476 case M_LHU_AB:
670a50eb
ILT
3477 s = "lhu";
3478 goto ld;
3d3c5039 3479 case M_LW_AB:
670a50eb
ILT
3480 s = "lw";
3481 goto ld;
3d3c5039 3482 case M_LWC0_AB:
670a50eb 3483 s = "lwc0";
8358c818 3484 coproc = 1;
670a50eb 3485 goto ld;
3d3c5039 3486 case M_LWC1_AB:
670a50eb 3487 s = "lwc1";
8358c818 3488 coproc = 1;
670a50eb 3489 goto ld;
3d3c5039 3490 case M_LWC2_AB:
670a50eb 3491 s = "lwc2";
8358c818 3492 coproc = 1;
670a50eb 3493 goto ld;
3d3c5039 3494 case M_LWC3_AB:
670a50eb 3495 s = "lwc3";
8358c818 3496 coproc = 1;
670a50eb 3497 goto ld;
3d3c5039 3498 case M_LWL_AB:
670a50eb 3499 s = "lwl";
b2b8c24e 3500 lr = 1;
670a50eb 3501 goto ld;
3d3c5039 3502 case M_LWR_AB:
670a50eb 3503 s = "lwr";
b2b8c24e 3504 lr = 1;
8358c818
ILT
3505 goto ld;
3506 case M_LDC1_AB:
3507 s = "ldc1";
3508 coproc = 1;
3509 goto ld;
3510 case M_LDC2_AB:
3511 s = "ldc2";
3512 coproc = 1;
3513 goto ld;
3514 case M_LDC3_AB:
3515 s = "ldc3";
3516 coproc = 1;
3517 goto ld;
3518 case M_LDL_AB:
3519 s = "ldl";
b2b8c24e 3520 lr = 1;
8358c818
ILT
3521 goto ld;
3522 case M_LDR_AB:
3523 s = "ldr";
b2b8c24e 3524 lr = 1;
8358c818
ILT
3525 goto ld;
3526 case M_LL_AB:
3527 s = "ll";
3528 goto ld;
3529 case M_LLD_AB:
3530 s = "lld";
3531 goto ld;
3532 case M_LWU_AB:
3533 s = "lwu";
3d3c5039 3534 ld:
b2b8c24e 3535 if (breg == treg || coproc || lr)
670a50eb
ILT
3536 {
3537 tempreg = AT;
3538 used_at = 1;
3539 }
3540 else
3541 {
3542 tempreg = treg;
3543 used_at = 0;
3544 }
3545 goto ld_st;
3d3c5039 3546 case M_SB_AB:
670a50eb
ILT
3547 s = "sb";
3548 goto st;
3d3c5039 3549 case M_SH_AB:
670a50eb
ILT
3550 s = "sh";
3551 goto st;
3d3c5039 3552 case M_SW_AB:
670a50eb
ILT
3553 s = "sw";
3554 goto st;
3d3c5039 3555 case M_SWC0_AB:
670a50eb 3556 s = "swc0";
8358c818 3557 coproc = 1;
670a50eb 3558 goto st;
3d3c5039 3559 case M_SWC1_AB:
670a50eb 3560 s = "swc1";
8358c818 3561 coproc = 1;
670a50eb 3562 goto st;
3d3c5039 3563 case M_SWC2_AB:
670a50eb 3564 s = "swc2";
8358c818 3565 coproc = 1;
670a50eb 3566 goto st;
3d3c5039 3567 case M_SWC3_AB:
670a50eb 3568 s = "swc3";
8358c818 3569 coproc = 1;
670a50eb 3570 goto st;
3d3c5039 3571 case M_SWL_AB:
670a50eb
ILT
3572 s = "swl";
3573 goto st;
3d3c5039 3574 case M_SWR_AB:
670a50eb 3575 s = "swr";
8358c818
ILT
3576 goto st;
3577 case M_SC_AB:
3578 s = "sc";
3579 goto st;
3580 case M_SCD_AB:
3581 s = "scd";
3582 goto st;
3583 case M_SDC1_AB:
3584 s = "sdc1";
3585 coproc = 1;
3586 goto st;
3587 case M_SDC2_AB:
3588 s = "sdc2";
3589 coproc = 1;
3590 goto st;
3591 case M_SDC3_AB:
3592 s = "sdc3";
3593 coproc = 1;
3594 goto st;
3595 case M_SDL_AB:
3596 s = "sdl";
3597 goto st;
3598 case M_SDR_AB:
3599 s = "sdr";
3d3c5039 3600 st:
670a50eb
ILT
3601 tempreg = AT;
3602 used_at = 1;
3d3c5039 3603 ld_st:
8358c818
ILT
3604 if (mask == M_LWC1_AB
3605 || mask == M_SWC1_AB
8358c818 3606 || mask == M_LDC1_AB
0dd2d296
ILT
3607 || mask == M_SDC1_AB
3608 || mask == M_L_DAB
3609 || mask == M_S_DAB)
670a50eb 3610 fmt = "T,o(b)";
8358c818 3611 else if (coproc)
19ed8960 3612 fmt = "E,o(b)";
670a50eb
ILT
3613 else
3614 fmt = "t,o(b)";
0dd2d296
ILT
3615
3616 if (offset_expr.X_op != O_constant
3617 && offset_expr.X_op != O_symbol)
3618 {
3619 as_bad ("expression too complex");
3620 offset_expr.X_op = O_constant;
3621 }
3622
3623 /* A constant expression in PIC code can be handled just as it
3624 is in non PIC code. */
d9aba805 3625 if (mips_pic == NO_PIC
0dd2d296 3626 || offset_expr.X_op == O_constant)
670a50eb 3627 {
0dd2d296
ILT
3628 /* If this is a reference to a GP relative symbol, and there
3629 is no base register, we want
3630 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
d9aba805 3631 Otherwise, if there is no base register, we want
0dd2d296
ILT
3632 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
3633 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
3634 If we have a constant, we need two instructions anyhow,
3635 so we always use the latter form.
3636
3637 If we have a base register, and this is a reference to a
3638 GP relative symbol, we want
3639 addu $tempreg,$breg,$gp
3640 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GPREL)
3641 Otherwise we want
3642 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
3643 addu $tempreg,$tempreg,$breg
3644 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
3645 With a constant we always use the latter case. */
670a50eb
ILT
3646 if (breg == 0)
3647 {
7a15a226 3648 if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
d8a1c247 3649 || nopic_need_relax (offset_expr.X_add_symbol))
0dd2d296
ILT
3650 p = NULL;
3651 else
3652 {
8ea7f4e8 3653 frag_grow (20);
0dd2d296
ILT
3654 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
3655 treg, (int) BFD_RELOC_MIPS_GPREL, GP);
3656 p = frag_var (rs_machine_dependent, 8, 0,
3657 RELAX_ENCODE (4, 8, 0, 4, 0,
8197b589
ILT
3658 (mips_warn_about_macros
3659 || (used_at && mips_noat))),
0dd2d296
ILT
3660 offset_expr.X_add_symbol, (long) 0,
3661 (char *) NULL);
8197b589 3662 used_at = 0;
0dd2d296
ILT
3663 }
3664 macro_build_lui (p, &icnt, &offset_expr, tempreg);
3665 if (p != NULL)
3666 p += 4;
3667 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
3668 (int) BFD_RELOC_LO16, tempreg);
3669 }
3670 else
3671 {
7a15a226 3672 if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
d8a1c247 3673 || nopic_need_relax (offset_expr.X_add_symbol))
0dd2d296
ILT
3674 p = NULL;
3675 else
3676 {
8ea7f4e8 3677 frag_grow (28);
0dd2d296
ILT
3678 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3679 mips_isa < 3 ? "addu" : "daddu",
3680 "d,v,t", tempreg, breg, GP);
3681 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
3682 treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
3683 p = frag_var (rs_machine_dependent, 12, 0,
3684 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
3685 offset_expr.X_add_symbol, (long) 0,
3686 (char *) NULL);
3687 }
3688 macro_build_lui (p, &icnt, &offset_expr, tempreg);
3689 if (p != NULL)
3690 p += 4;
3691 macro_build (p, &icnt, (expressionS *) NULL,
3692 mips_isa < 3 ? "addu" : "daddu",
3693 "d,v,t", tempreg, tempreg, breg);
3694 if (p != NULL)
3695 p += 4;
3696 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
3697 (int) BFD_RELOC_LO16, tempreg);
670a50eb 3698 }
670a50eb 3699 }
fb251650 3700 else if (mips_pic == SVR4_PIC && ! mips_big_got)
670a50eb 3701 {
0dd2d296
ILT
3702 /* If this is a reference to an external symbol, we want
3703 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3704 nop
3705 <op> $treg,0($tempreg)
3706 Otherwise we want
3707 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3708 nop
3709 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
3710 <op> $treg,0($tempreg)
3711 If there is a base register, we add it to $tempreg before
3712 the <op>. If there is a constant, we stick it in the
3713 <op> instruction. We don't handle constants larger than
3714 16 bits, because we have no way to load the upper 16 bits
3715 (actually, we could handle them for the subset of cases
3716 in which we are not using $at). */
3717 assert (offset_expr.X_op == O_symbol);
3718 expr1.X_add_number = offset_expr.X_add_number;
3719 offset_expr.X_add_number = 0;
3720 if (expr1.X_add_number < -0x8000
3721 || expr1.X_add_number >= 0x8000)
3722 as_bad ("PIC code offset overflow (max 16 signed bits)");
8ea7f4e8 3723 frag_grow (20);
0dd2d296
ILT
3724 macro_build ((char *) NULL, &icnt, &offset_expr,
3725 mips_isa < 3 ? "lw" : "ld",
3726 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
3727 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
3728 p = frag_var (rs_machine_dependent, 4, 0,
3729 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
3730 offset_expr.X_add_symbol, (long) 0,
3731 (char *) NULL);
3732 macro_build (p, &icnt, &offset_expr,
3733 mips_isa < 3 ? "addiu" : "daddiu",
3734 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
670a50eb 3735 if (breg != 0)
0dd2d296 3736 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6e8dda9c
ILT
3737 mips_isa < 3 ? "addu" : "daddu",
3738 "d,v,t", tempreg, tempreg, breg);
0dd2d296
ILT
3739 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
3740 (int) BFD_RELOC_LO16, tempreg);
670a50eb 3741 }
fb251650
ILT
3742 else if (mips_pic == SVR4_PIC)
3743 {
3744 int gpdel;
3745
3746 /* If this is a reference to an external symbol, we want
3747 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3748 addu $tempreg,$tempreg,$gp
3749 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
3750 <op> $treg,0($tempreg)
3751 Otherwise we want
3752 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3753 nop
3754 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
3755 <op> $treg,0($tempreg)
3756 If there is a base register, we add it to $tempreg before
3757 the <op>. If there is a constant, we stick it in the
3758 <op> instruction. We don't handle constants larger than
3759 16 bits, because we have no way to load the upper 16 bits
3760 (actually, we could handle them for the subset of cases
3761 in which we are not using $at). */
3762 assert (offset_expr.X_op == O_symbol);
3763 expr1.X_add_number = offset_expr.X_add_number;
3764 offset_expr.X_add_number = 0;
3765 if (expr1.X_add_number < -0x8000
3766 || expr1.X_add_number >= 0x8000)
3767 as_bad ("PIC code offset overflow (max 16 signed bits)");
3768 if (reg_needs_delay (GP))
3769 gpdel = 4;
3770 else
3771 gpdel = 0;
3772 frag_grow (36);
3773 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
3774 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
3775 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3776 mips_isa < 3 ? "addu" : "daddu",
3777 "d,v,t", tempreg, tempreg, GP);
3778 macro_build ((char *) NULL, &icnt, &offset_expr,
3779 mips_isa < 3 ? "lw" : "ld",
867a58b3
ILT
3780 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
3781 tempreg);
fb251650
ILT
3782 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
3783 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
3784 offset_expr.X_add_symbol, (long) 0, (char *) NULL);
3785 if (gpdel > 0)
3786 {
3787 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
3788 p += 4;
3789 }
3790 macro_build (p, &icnt, &offset_expr,
3791 mips_isa < 3 ? "lw" : "ld",
867a58b3 3792 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
fb251650
ILT
3793 p += 4;
3794 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
3795 p += 4;
3796 macro_build (p, &icnt, &offset_expr,
3797 mips_isa < 3 ? "addiu" : "daddiu",
3798 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
3799 if (breg != 0)
3800 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3801 mips_isa < 3 ? "addu" : "daddu",
3802 "d,v,t", tempreg, tempreg, breg);
3803 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
3804 (int) BFD_RELOC_LO16, tempreg);
3805 }
d9aba805
ILT
3806 else if (mips_pic == EMBEDDED_PIC)
3807 {
3808 /* If there is no base register, we want
3809 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
3810 If there is a base register, we want
3811 addu $tempreg,$breg,$gp
3812 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GPREL)
3813 */
3814 assert (offset_expr.X_op == O_symbol);
3815 if (breg == 0)
3816 {
3817 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
3818 treg, (int) BFD_RELOC_MIPS_GPREL, GP);
3819 used_at = 0;
3820 }
3821 else
3822 {
3823 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3824 mips_isa < 3 ? "addu" : "daddu",
3825 "d,v,t", tempreg, breg, GP);
3826 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
3827 treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
3828 }
3829 }
3830 else
3831 abort ();
0dd2d296
ILT
3832
3833 if (! used_at)
3834 return;
3835
3836 break;
3d3c5039
ILT
3837
3838 case M_LI:
19ed8960 3839 case M_LI_S:
d8a1c247
KR
3840 load_register (&icnt, treg, &imm_expr, 0);
3841 return;
3842
3843 case M_DLI:
3844 load_register (&icnt, treg, &imm_expr, 1);
670a50eb 3845 return;
3d3c5039 3846
0dd2d296 3847 case M_LI_SS:
55933a58 3848 if (imm_expr.X_op == O_constant)
0dd2d296 3849 {
d8a1c247 3850 load_register (&icnt, AT, &imm_expr, 0);
d2c71068
ILT
3851 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3852 "mtc1", "t,G", AT, treg);
3853 break;
0dd2d296 3854 }
d9aba805 3855 else
d2c71068 3856 {
55933a58
ILT
3857 assert (offset_expr.X_op == O_symbol
3858 && strcmp (segment_name (S_GET_SEGMENT
3859 (offset_expr.X_add_symbol)),
3860 ".lit4") == 0
3861 && offset_expr.X_add_number == 0);
3862 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
3863 treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
d2c71068
ILT
3864 return;
3865 }
0dd2d296 3866
3d3c5039 3867 case M_LI_D:
d9aba805
ILT
3868 /* We know that sym is in the .rdata section. First we get the
3869 upper 16 bits of the address. */
3870 if (mips_pic == NO_PIC)
0dd2d296
ILT
3871 {
3872 /* FIXME: This won't work for a 64 bit address. */
3873 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
3874 }
d9aba805 3875 else if (mips_pic == SVR4_PIC)
0dd2d296
ILT
3876 {
3877 macro_build ((char *) NULL, &icnt, &offset_expr,
3878 mips_isa < 3 ? "lw" : "ld",
3879 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
3880 }
d9aba805
ILT
3881 else if (mips_pic == EMBEDDED_PIC)
3882 {
3883 /* For embedded PIC we pick up the entire address off $gp in
3884 a single instruction. */
3885 macro_build ((char *) NULL, &icnt, &offset_expr,
3886 mips_isa < 3 ? "addiu" : "daddiu",
3887 "t,r,j", AT, GP, (int) BFD_RELOC_MIPS_GPREL);
3888 offset_expr.X_op = O_constant;
3889 offset_expr.X_add_number = 0;
3890 }
3891 else
3892 abort ();
3893
0dd2d296 3894 /* Now we load the register(s). */
8358c818 3895 if (mips_isa >= 3)
0dd2d296
ILT
3896 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
3897 treg, (int) BFD_RELOC_LO16, AT);
8358c818
ILT
3898 else
3899 {
0dd2d296
ILT
3900 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
3901 treg, (int) BFD_RELOC_LO16, AT);
3902 if (treg != 31)
3903 {
3904 /* FIXME: How in the world do we deal with the possible
3905 overflow here? */
3906 offset_expr.X_add_number += 4;
3907 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
3908 treg + 1, (int) BFD_RELOC_LO16, AT);
3909 }
8358c818 3910 }
d2c71068
ILT
3911
3912 /* To avoid confusion in tc_gen_reloc, we must ensure that this
3913 does not become a variant frag. */
3914 frag_wane (frag_now);
3915 frag_new (0);
3916
670a50eb 3917 break;
3d3c5039
ILT
3918
3919 case M_LI_DD:
55933a58
ILT
3920 assert (offset_expr.X_op == O_symbol
3921 && offset_expr.X_add_number == 0);
3922 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
3923 if (strcmp (s, ".lit8") == 0)
8358c818 3924 {
0dd2d296
ILT
3925 if (mips_isa >= 2)
3926 {
3927 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
3928 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
3929 return;
3930 }
3931 breg = GP;
3932 r = BFD_RELOC_MIPS_LITERAL;
3933 goto dob;
3934 }
55933a58 3935 else
0dd2d296 3936 {
55933a58
ILT
3937 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
3938 if (mips_pic == SVR4_PIC)
3939 macro_build ((char *) NULL, &icnt, &offset_expr,
3940 mips_isa < 3 ? "lw" : "ld",
3941 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
3942 else
3943 {
3944 /* FIXME: This won't work for a 64 bit address. */
3945 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
3946 }
3947
0dd2d296
ILT
3948 if (mips_isa >= 2)
3949 {
3950 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
55933a58 3951 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
94b68f04
ILT
3952
3953 /* To avoid confusion in tc_gen_reloc, we must ensure
3954 that this does not become a variant frag. */
3955 frag_wane (frag_now);
3956 frag_new (0);
3957
0dd2d296
ILT
3958 break;
3959 }
3960 breg = AT;
3961 r = BFD_RELOC_LO16;
3962 goto dob;
8358c818 3963 }
9226253a 3964
3d3c5039 3965 case M_L_DOB:
9a7d824a
ILT
3966 /* Even on a big endian machine $fn comes before $fn+1. We have
3967 to adjust when loading from memory. */
9226253a
ILT
3968 r = BFD_RELOC_LO16;
3969 dob:
8358c818 3970 assert (mips_isa < 2);
0dd2d296 3971 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
9a7d824a 3972 byte_order == LITTLE_ENDIAN ? treg : treg + 1,
9226253a 3973 (int) r, breg);
0dd2d296
ILT
3974 /* FIXME: A possible overflow which I don't know how to deal
3975 with. */
670a50eb 3976 offset_expr.X_add_number += 4;
0dd2d296 3977 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
9a7d824a 3978 byte_order == LITTLE_ENDIAN ? treg + 1 : treg,
9226253a 3979 (int) r, breg);
d2c71068
ILT
3980
3981 /* To avoid confusion in tc_gen_reloc, we must ensure that this
3982 does not become a variant frag. */
3983 frag_wane (frag_now);
3984 frag_new (0);
3985
0dd2d296
ILT
3986 if (breg != AT)
3987 return;
3988 break;
3d3c5039
ILT
3989
3990 case M_L_DAB:
670a50eb
ILT
3991 /*
3992 * The MIPS assembler seems to check for X_add_number not
3993 * being double aligned and generating:
3994 * lui at,%hi(foo+1)
3995 * addu at,at,v1
3996 * addiu at,at,%lo(foo+1)
3997 * lwc1 f2,0(at)
3998 * lwc1 f3,4(at)
3999 * But, the resulting address is the same after relocation so why
4000 * generate the extra instruction?
4001 */
4032d3f0 4002 coproc = 1;
0dd2d296 4003 if (mips_isa >= 2)
670a50eb 4004 {
0dd2d296
ILT
4005 s = "ldc1";
4006 goto ld;
670a50eb 4007 }
0dd2d296
ILT
4008
4009 s = "lwc1";
4010 fmt = "T,o(b)";
0dd2d296
ILT
4011 goto ldd_std;
4012
4013 case M_S_DAB:
8358c818 4014 if (mips_isa >= 2)
8358c818 4015 {
0dd2d296
ILT
4016 s = "sdc1";
4017 goto st;
8358c818 4018 }
3d3c5039 4019
0dd2d296
ILT
4020 s = "swc1";
4021 fmt = "T,o(b)";
4022 coproc = 1;
4023 goto ldd_std;
3d3c5039
ILT
4024
4025 case M_LD_AB:
0dd2d296 4026 if (mips_isa >= 3)
670a50eb 4027 {
0dd2d296
ILT
4028 s = "ld";
4029 goto ld;
670a50eb 4030 }
0dd2d296
ILT
4031
4032 s = "lw";
4033 fmt = "t,o(b)";
4034 goto ldd_std;
4035
4036 case M_SD_AB:
4037 if (mips_isa >= 3)
670a50eb 4038 {
0dd2d296
ILT
4039 s = "sd";
4040 goto st;
670a50eb 4041 }
0dd2d296 4042
670a50eb 4043 s = "sw";
0dd2d296
ILT
4044 fmt = "t,o(b)";
4045
4046 ldd_std:
4047 if (offset_expr.X_op != O_symbol
4048 && offset_expr.X_op != O_constant)
670a50eb 4049 {
0dd2d296
ILT
4050 as_bad ("expression too complex");
4051 offset_expr.X_op = O_constant;
4052 }
4053
4054 /* Even on a big endian machine $fn comes before $fn+1. We have
4055 to adjust when loading from memory. We set coproc if we must
4056 load $fn+1 first. */
4057 if (byte_order == LITTLE_ENDIAN)
4058 coproc = 0;
4059
d9aba805 4060 if (mips_pic == NO_PIC
0dd2d296
ILT
4061 || offset_expr.X_op == O_constant)
4062 {
4063 /* If this is a reference to a GP relative symbol, we want
4064 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
4065 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
4066 If we have a base register, we use this
4067 addu $at,$breg,$gp
4068 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
4069 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
4070 If this is not a GP relative symbol, we want
4071 lui $at,<sym> (BFD_RELOC_HI16_S)
4072 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
4073 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
4074 If there is a base register, we add it to $at after the
4075 lui instruction. If there is a constant, we always use
4076 the last case. */
7a15a226 4077 if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
d8a1c247 4078 || nopic_need_relax (offset_expr.X_add_symbol))
670a50eb 4079 {
0dd2d296
ILT
4080 p = NULL;
4081 used_at = 1;
670a50eb
ILT
4082 }
4083 else
0dd2d296
ILT
4084 {
4085 int off;
4086
4087 if (breg == 0)
4088 {
8ea7f4e8 4089 frag_grow (28);
0dd2d296
ILT
4090 tempreg = GP;
4091 off = 0;
4092 used_at = 0;
4093 }
4094 else
4095 {
8ea7f4e8 4096 frag_grow (36);
0dd2d296
ILT
4097 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4098 mips_isa < 3 ? "addu" : "daddu",
4099 "d,v,t", AT, breg, GP);
4100 tempreg = AT;
4101 off = 4;
4102 used_at = 1;
4103 }
4104
4105 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4106 coproc ? treg + 1 : treg,
4107 (int) BFD_RELOC_MIPS_GPREL, tempreg);
4108 offset_expr.X_add_number += 4;
55933a58
ILT
4109
4110 /* Set mips_optimize to 2 to avoid inserting an
4111 undesired nop. */
4112 hold_mips_optimize = mips_optimize;
4113 mips_optimize = 2;
0dd2d296
ILT
4114 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4115 coproc ? treg : treg + 1,
4116 (int) BFD_RELOC_MIPS_GPREL, tempreg);
55933a58
ILT
4117 mips_optimize = hold_mips_optimize;
4118
0dd2d296
ILT
4119 p = frag_var (rs_machine_dependent, 12 + off, 0,
4120 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
8197b589 4121 used_at && mips_noat),
0dd2d296
ILT
4122 offset_expr.X_add_symbol, (long) 0,
4123 (char *) NULL);
777ad64d
ILT
4124
4125 /* We just generated two relocs. When tc_gen_reloc
4126 handles this case, it will skip the first reloc and
4127 handle the second. The second reloc already has an
4128 extra addend of 4, which we added above. We must
4129 subtract it out, and then subtract another 4 to make
4130 the first reloc come out right. The second reloc
4131 will come out right because we are going to add 4 to
4132 offset_expr when we build its instruction below. */
4133 offset_expr.X_add_number -= 8;
0dd2d296
ILT
4134 offset_expr.X_op = O_constant;
4135 }
4136 macro_build_lui (p, &icnt, &offset_expr, AT);
4137 if (p != NULL)
4138 p += 4;
4139 if (breg != 0)
4140 {
4141 macro_build (p, &icnt, (expressionS *) NULL,
4142 mips_isa < 3 ? "addu" : "daddu",
4143 "d,v,t", AT, breg, AT);
4144 if (p != NULL)
4145 p += 4;
4146 }
4147 macro_build (p, &icnt, &offset_expr, s, fmt,
4148 coproc ? treg + 1 : treg,
4149 (int) BFD_RELOC_LO16, AT);
4150 if (p != NULL)
4151 p += 4;
4152 /* FIXME: How do we handle overflow here? */
4153 offset_expr.X_add_number += 4;
4154 macro_build (p, &icnt, &offset_expr, s, fmt,
4155 coproc ? treg : treg + 1,
4156 (int) BFD_RELOC_LO16, AT);
4157 }
fb251650 4158 else if (mips_pic == SVR4_PIC && ! mips_big_got)
670a50eb 4159 {
0dd2d296
ILT
4160 int off;
4161
4162 /* If this is a reference to an external symbol, we want
4163 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4164 nop
4165 <op> $treg,0($at)
4166 <op> $treg+1,4($at)
4167 Otherwise we want
4168 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4169 nop
4170 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
4171 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
4172 If there is a base register we add it to $at before the
4173 lwc1 instructions. If there is a constant we include it
4174 in the lwc1 instructions. */
4175 used_at = 1;
4176 expr1.X_add_number = offset_expr.X_add_number;
4177 offset_expr.X_add_number = 0;
4178 if (expr1.X_add_number < -0x8000
4179 || expr1.X_add_number >= 0x8000 - 4)
4180 as_bad ("PIC code offset overflow (max 16 signed bits)");
4181 if (breg == 0)
4182 off = 0;
4183 else
4184 off = 4;
8ea7f4e8 4185 frag_grow (24 + off);
0dd2d296
ILT
4186 macro_build ((char *) NULL, &icnt, &offset_expr,
4187 mips_isa < 3 ? "lw" : "ld",
4188 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
4189 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
670a50eb 4190 if (breg != 0)
0dd2d296 4191 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6e8dda9c 4192 mips_isa < 3 ? "addu" : "daddu",
0dd2d296
ILT
4193 "d,v,t", AT, breg, AT);
4194 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
4195 coproc ? treg + 1 : treg,
4196 (int) BFD_RELOC_LO16, AT);
4197 expr1.X_add_number += 4;
55933a58
ILT
4198
4199 /* Set mips_optimize to 2 to avoid inserting an undesired
4200 nop. */
4201 hold_mips_optimize = mips_optimize;
4202 mips_optimize = 2;
0dd2d296
ILT
4203 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
4204 coproc ? treg : treg + 1,
4205 (int) BFD_RELOC_LO16, AT);
55933a58
ILT
4206 mips_optimize = hold_mips_optimize;
4207
0dd2d296
ILT
4208 (void) frag_var (rs_machine_dependent, 0, 0,
4209 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
4210 offset_expr.X_add_symbol, (long) 0,
4211 (char *) NULL);
8358c818 4212 }
fb251650
ILT
4213 else if (mips_pic == SVR4_PIC)
4214 {
4215 int gpdel, off;
4216
4217 /* If this is a reference to an external symbol, we want
4218 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4219 addu $at,$at,$gp
4220 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
4221 nop
4222 <op> $treg,0($at)
4223 <op> $treg+1,4($at)
4224 Otherwise we want
4225 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4226 nop
4227 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
4228 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
4229 If there is a base register we add it to $at before the
4230 lwc1 instructions. If there is a constant we include it
4231 in the lwc1 instructions. */
4232 used_at = 1;
4233 expr1.X_add_number = offset_expr.X_add_number;
4234 offset_expr.X_add_number = 0;
4235 if (expr1.X_add_number < -0x8000
4236 || expr1.X_add_number >= 0x8000 - 4)
4237 as_bad ("PIC code offset overflow (max 16 signed bits)");
4238 if (reg_needs_delay (GP))
4239 gpdel = 4;
4240 else
4241 gpdel = 0;
4242 if (breg == 0)
4243 off = 0;
4244 else
4245 off = 4;
4246 frag_grow (56);
4247 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4248 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
4249 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4250 mips_isa < 3 ? "addu" : "daddu",
4251 "d,v,t", AT, AT, GP);
4252 macro_build ((char *) NULL, &icnt, &offset_expr,
4253 mips_isa < 3 ? "lw" : "ld",
4254 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
4255 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
4256 if (breg != 0)
4257 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4258 mips_isa < 3 ? "addu" : "daddu",
4259 "d,v,t", AT, breg, AT);
4260 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
4261 coproc ? treg + 1 : treg,
4262 (int) BFD_RELOC_LO16, AT);
4263 expr1.X_add_number += 4;
4264
4265 /* Set mips_optimize to 2 to avoid inserting an undesired
4266 nop. */
4267 hold_mips_optimize = mips_optimize;
4268 mips_optimize = 2;
4269 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
4270 coproc ? treg : treg + 1,
4271 (int) BFD_RELOC_LO16, AT);
4272 mips_optimize = hold_mips_optimize;
4273 expr1.X_add_number -= 4;
4274
4275 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
4276 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
4277 8 + gpdel + off, 1, 0),
4278 offset_expr.X_add_symbol, (long) 0,
4279 (char *) NULL);
4280 if (gpdel > 0)
4281 {
4282 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4283 p += 4;
4284 }
4285 macro_build (p, &icnt, &offset_expr,
4286 mips_isa < 3 ? "lw" : "ld",
4287 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
4288 p += 4;
4289 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4290 p += 4;
4291 if (breg != 0)
4292 {
4293 macro_build (p, &icnt, (expressionS *) NULL,
4294 mips_isa < 3 ? "addu" : "daddu",
4295 "d,v,t", AT, breg, AT);
4296 p += 4;
4297 }
4298 macro_build (p, &icnt, &expr1, s, fmt,
4299 coproc ? treg + 1 : treg,
4300 (int) BFD_RELOC_LO16, AT);
4301 p += 4;
4302 expr1.X_add_number += 4;
4303
4304 /* Set mips_optimize to 2 to avoid inserting an undesired
4305 nop. */
4306 hold_mips_optimize = mips_optimize;
4307 mips_optimize = 2;
4308 macro_build (p, &icnt, &expr1, s, fmt,
4309 coproc ? treg : treg + 1,
4310 (int) BFD_RELOC_LO16, AT);
4311 mips_optimize = hold_mips_optimize;
4312 }
d9aba805
ILT
4313 else if (mips_pic == EMBEDDED_PIC)
4314 {
4315 /* If there is no base register, we use
4316 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
4317 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
4318 If we have a base register, we use
4319 addu $at,$breg,$gp
4320 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
4321 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
4322 */
4323 if (breg == 0)
4324 {
4325 tempreg = GP;
4326 used_at = 0;
4327 }
4328 else
4329 {
4330 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4331 mips_isa < 3 ? "addu" : "daddu",
4332 "d,v,t", AT, breg, GP);
4333 tempreg = AT;
4334 used_at = 1;
4335 }
4336
4337 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4338 coproc ? treg + 1 : treg,
4339 (int) BFD_RELOC_MIPS_GPREL, tempreg);
4340 offset_expr.X_add_number += 4;
4341 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4342 coproc ? treg : treg + 1,
4343 (int) BFD_RELOC_MIPS_GPREL, tempreg);
4344 }
4345 else
4346 abort ();
0dd2d296
ILT
4347
4348 if (! used_at)
4349 return;
4350
4351 break;
4352
4353 case M_LD_OB:
4354 s = "lw";
4355 goto sd_ob;
4356 case M_SD_OB:
4357 s = "sw";
4358 sd_ob:
4359 assert (mips_isa < 3);
4360 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
4361 (int) BFD_RELOC_LO16, breg);
4362 offset_expr.X_add_number += 4;
4363 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
4364 (int) BFD_RELOC_LO16, breg);
670a50eb 4365 return;
917fae09
SS
4366#ifdef LOSING_COMPILER
4367 default:
4368 macro2 (ip);
4369 return;
4370 }
4371 if (mips_noat)
4372 as_warn ("Macro used $at after \".set noat\"");
4373}
4374
4375static void
4376macro2 (ip)
4377 struct mips_cl_insn *ip;
4378{
4379 register int treg, sreg, dreg, breg;
4380 int tempreg;
4381 int mask;
4382 int icnt = 0;
4383 int used_at;
4384 expressionS expr1;
4385 const char *s;
4386 const char *s2;
4387 const char *fmt;
4388 int likely = 0;
4389 int dbl = 0;
4390 int coproc = 0;
adcf2b9d
ILT
4391 int lr = 0;
4392 int off;
917fae09
SS
4393 offsetT maxnum;
4394 bfd_reloc_code_real_type r;
4395 char *p;
4396
4397 treg = (ip->insn_opcode >> 16) & 0x1f;
4398 dreg = (ip->insn_opcode >> 11) & 0x1f;
4399 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4400 mask = ip->insn_mo->mask;
4401
4402 expr1.X_op = O_constant;
4403 expr1.X_op_symbol = NULL;
4404 expr1.X_add_symbol = NULL;
4405 expr1.X_add_number = 1;
4406
4407 switch (mask)
4408 {
4409#endif /* LOSING_COMPILER */
3d3c5039 4410
8358c818
ILT
4411 case M_DMUL:
4412 dbl = 1;
3d3c5039 4413 case M_MUL:
0dd2d296 4414 macro_build ((char *) NULL, &icnt, NULL,
8358c818
ILT
4415 dbl ? "dmultu" : "multu",
4416 "s,t", sreg, treg);
0dd2d296 4417 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
670a50eb 4418 return;
3d3c5039 4419
8358c818
ILT
4420 case M_DMUL_I:
4421 dbl = 1;
3d3c5039 4422 case M_MUL_I:
8358c818
ILT
4423 /* The MIPS assembler some times generates shifts and adds. I'm
4424 not trying to be that fancy. GCC should do this for us
4425 anyway. */
d8a1c247 4426 load_register (&icnt, AT, &imm_expr, dbl);
0dd2d296 4427 macro_build ((char *) NULL, &icnt, NULL,
8358c818
ILT
4428 dbl ? "dmult" : "mult",
4429 "s,t", sreg, AT);
0dd2d296 4430 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
670a50eb 4431 break;
3d3c5039 4432
8358c818
ILT
4433 case M_DMULO:
4434 dbl = 1;
4435 case M_MULO:
4436 mips_emit_delays ();
4437 ++mips_noreorder;
0dd2d296
ILT
4438 mips_any_noreorder = 1;
4439 macro_build ((char *) NULL, &icnt, NULL,
8358c818
ILT
4440 dbl ? "dmult" : "mult",
4441 "s,t", sreg, treg);
0dd2d296
ILT
4442 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
4443 macro_build ((char *) NULL, &icnt, NULL,
8358c818
ILT
4444 dbl ? "dsra32" : "sra",
4445 "d,w,<", dreg, dreg, 31);
0dd2d296 4446 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
8ea7f4e8
ILT
4447 if (mips_trap)
4448 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", dreg, AT);
4449 else
4450 {
4451 expr1.X_add_number = 8;
4452 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
4453 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
4454 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
4455 }
8358c818 4456 --mips_noreorder;
0dd2d296 4457 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
8358c818
ILT
4458 break;
4459
4460 case M_DMULOU:
4461 dbl = 1;
4462 case M_MULOU:
4463 mips_emit_delays ();
4464 ++mips_noreorder;
0dd2d296
ILT
4465 mips_any_noreorder = 1;
4466 macro_build ((char *) NULL, &icnt, NULL,
8358c818
ILT
4467 dbl ? "dmultu" : "multu",
4468 "s,t", sreg, treg);
0dd2d296
ILT
4469 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
4470 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
8ea7f4e8
ILT
4471 if (mips_trap)
4472 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", AT, 0);
4473 else
4474 {
4475 expr1.X_add_number = 8;
4476 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
4477 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
4478 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
4479 }
8358c818
ILT
4480 --mips_noreorder;
4481 break;
4482
3d3c5039 4483 case M_ROL:
0dd2d296
ILT
4484 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
4485 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
4486 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg,
4487 treg);
4488 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
670a50eb 4489 break;
3d3c5039
ILT
4490
4491 case M_ROL_I:
0dd2d296 4492 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg,
a40aee38 4493 (int) (imm_expr.X_add_number & 0x1f));
0dd2d296 4494 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
a40aee38 4495 (int) ((0 - imm_expr.X_add_number) & 0x1f));
0dd2d296 4496 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
670a50eb 4497 break;
3d3c5039
ILT
4498
4499 case M_ROR:
0dd2d296
ILT
4500 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
4501 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
4502 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg,
4503 treg);
4504 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
670a50eb 4505 break;
3d3c5039
ILT
4506
4507 case M_ROR_I:
0dd2d296 4508 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg,
a40aee38 4509 (int) (imm_expr.X_add_number & 0x1f));
0dd2d296 4510 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
a40aee38 4511 (int) ((0 - imm_expr.X_add_number) & 0x1f));
0dd2d296 4512 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
670a50eb 4513 break;
3d3c5039
ILT
4514
4515 case M_S_DOB:
8358c818 4516 assert (mips_isa < 2);
9a7d824a
ILT
4517 /* Even on a big endian machine $fn comes before $fn+1. We have
4518 to adjust when storing to memory. */
0dd2d296 4519 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
9a7d824a 4520 byte_order == LITTLE_ENDIAN ? treg : treg + 1,
9226253a 4521 (int) BFD_RELOC_LO16, breg);
670a50eb 4522 offset_expr.X_add_number += 4;
0dd2d296 4523 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
9a7d824a 4524 byte_order == LITTLE_ENDIAN ? treg + 1 : treg,
9226253a 4525 (int) BFD_RELOC_LO16, breg);
670a50eb 4526 return;
3d3c5039 4527
3d3c5039 4528 case M_SEQ:
670a50eb 4529 if (sreg == 0)
0dd2d296
ILT
4530 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
4531 treg, (int) BFD_RELOC_LO16);
670a50eb 4532 else if (treg == 0)
0dd2d296
ILT
4533 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
4534 sreg, (int) BFD_RELOC_LO16);
670a50eb
ILT
4535 else
4536 {
0dd2d296
ILT
4537 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
4538 sreg, treg);
4539 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
4540 dreg, (int) BFD_RELOC_LO16);
3d3c5039 4541 }
670a50eb 4542 return;
3d3c5039
ILT
4543
4544 case M_SEQ_I:
670a50eb
ILT
4545 if (imm_expr.X_add_number == 0)
4546 {
0dd2d296
ILT
4547 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
4548 sreg, (int) BFD_RELOC_LO16);
670a50eb 4549 return;
3d3c5039 4550 }
670a50eb
ILT
4551 if (sreg == 0)
4552 {
9a7d824a 4553 as_warn ("Instruction %s: result is always false",
6e8dda9c 4554 ip->insn_mo->name);
0dd2d296 4555 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
670a50eb 4556 return;
3d3c5039 4557 }
6e8dda9c 4558 if (imm_expr.X_add_number >= 0 && imm_expr.X_add_number < 0x10000)
670a50eb 4559 {
0dd2d296
ILT
4560 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
4561 sreg, (int) BFD_RELOC_LO16);
670a50eb 4562 used_at = 0;
6e8dda9c
ILT
4563 }
4564 else if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number < 0)
4565 {
4566 imm_expr.X_add_number = -imm_expr.X_add_number;
0dd2d296 4567 macro_build ((char *) NULL, &icnt, &imm_expr,
6e8dda9c 4568 mips_isa < 3 ? "addiu" : "daddiu",
9226253a
ILT
4569 "t,r,j", dreg, sreg,
4570 (int) BFD_RELOC_LO16);
6e8dda9c
ILT
4571 used_at = 0;
4572 }
4573 else
4574 {
d8a1c247 4575 load_register (&icnt, AT, &imm_expr, 0);
0dd2d296
ILT
4576 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
4577 sreg, AT);
670a50eb
ILT
4578 used_at = 1;
4579 }
0dd2d296 4580 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
9226253a 4581 (int) BFD_RELOC_LO16);
670a50eb
ILT
4582 if (used_at)
4583 break;
4584 return;
3d3c5039
ILT
4585
4586 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
670a50eb
ILT
4587 s = "slt";
4588 goto sge;
3d3c5039 4589 case M_SGEU:
670a50eb 4590 s = "sltu";
3d3c5039 4591 sge:
0dd2d296
ILT
4592 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
4593 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
9226253a 4594 (int) BFD_RELOC_LO16);
670a50eb 4595 return;
3d3c5039 4596
670a50eb 4597 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
3d3c5039 4598 case M_SGEU_I:
6e8dda9c 4599 if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
670a50eb 4600 {
0dd2d296 4601 macro_build ((char *) NULL, &icnt, &expr1,
6e8dda9c 4602 mask == M_SGE_I ? "slti" : "sltiu",
9226253a 4603 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
670a50eb
ILT
4604 used_at = 0;
4605 }
4606 else
4607 {
d8a1c247 4608 load_register (&icnt, AT, &imm_expr, 0);
0dd2d296 4609 macro_build ((char *) NULL, &icnt, NULL,
6e8dda9c
ILT
4610 mask == M_SGE_I ? "slt" : "sltu",
4611 "d,v,t", dreg, sreg, AT);
670a50eb
ILT
4612 used_at = 1;
4613 }
0dd2d296 4614 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
9226253a 4615 (int) BFD_RELOC_LO16);
670a50eb
ILT
4616 if (used_at)
4617 break;
4618 return;
3d3c5039
ILT
4619
4620 case M_SGT: /* sreg > treg <==> treg < sreg */
670a50eb
ILT
4621 s = "slt";
4622 goto sgt;
3d3c5039 4623 case M_SGTU:
670a50eb 4624 s = "sltu";
3d3c5039 4625 sgt:
0dd2d296 4626 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
670a50eb 4627 return;
3d3c5039 4628
670a50eb
ILT
4629 case M_SGT_I: /* sreg > I <==> I < sreg */
4630 s = "slt";
4631 goto sgti;
3d3c5039 4632 case M_SGTU_I:
670a50eb 4633 s = "sltu";
3d3c5039 4634 sgti:
d8a1c247 4635 load_register (&icnt, AT, &imm_expr, 0);
0dd2d296 4636 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
670a50eb 4637 break;
3d3c5039 4638
670a50eb
ILT
4639 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
4640 s = "slt";
4641 goto sle;
3d3c5039 4642 case M_SLEU:
670a50eb 4643 s = "sltu";
3d3c5039 4644 sle:
0dd2d296
ILT
4645 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
4646 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
9226253a 4647 (int) BFD_RELOC_LO16);
670a50eb 4648 return;
3d3c5039 4649
670a50eb
ILT
4650 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
4651 s = "slt";
4652 goto slei;
3d3c5039 4653 case M_SLEU_I:
670a50eb 4654 s = "sltu";
3d3c5039 4655 slei:
d8a1c247 4656 load_register (&icnt, AT, &imm_expr, 0);
0dd2d296
ILT
4657 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
4658 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
9226253a 4659 (int) BFD_RELOC_LO16);
670a50eb 4660 break;
3d3c5039
ILT
4661
4662 case M_SLT_I:
6e8dda9c 4663 if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
670a50eb 4664 {
0dd2d296
ILT
4665 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
4666 dreg, sreg, (int) BFD_RELOC_LO16);
670a50eb 4667 return;
3d3c5039 4668 }
d8a1c247 4669 load_register (&icnt, AT, &imm_expr, 0);
0dd2d296 4670 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
670a50eb 4671 break;
3d3c5039
ILT
4672
4673 case M_SLTU_I:
6e8dda9c 4674 if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
670a50eb 4675 {
0dd2d296
ILT
4676 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
4677 dreg, sreg, (int) BFD_RELOC_LO16);
670a50eb 4678 return;
3d3c5039 4679 }
d8a1c247 4680 load_register (&icnt, AT, &imm_expr, 0);
0dd2d296
ILT
4681 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg,
4682 AT);
670a50eb 4683 break;
3d3c5039
ILT
4684
4685 case M_SNE:
670a50eb 4686 if (sreg == 0)
0dd2d296
ILT
4687 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
4688 treg);
670a50eb 4689 else if (treg == 0)
0dd2d296
ILT
4690 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
4691 sreg);
670a50eb
ILT
4692 else
4693 {
0dd2d296
ILT
4694 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
4695 sreg, treg);
4696 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
4697 dreg);
3d3c5039 4698 }
670a50eb 4699 return;
3d3c5039
ILT
4700
4701 case M_SNE_I:
670a50eb
ILT
4702 if (imm_expr.X_add_number == 0)
4703 {
0dd2d296
ILT
4704 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
4705 sreg);
670a50eb 4706 return;
3d3c5039 4707 }
670a50eb
ILT
4708 if (sreg == 0)
4709 {
9a7d824a 4710 as_warn ("Instruction %s: result is always true",
6e8dda9c 4711 ip->insn_mo->name);
0dd2d296 4712 macro_build ((char *) NULL, &icnt, &expr1,
6e8dda9c 4713 mips_isa < 3 ? "addiu" : "daddiu",
9226253a 4714 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
670a50eb 4715 return;
3d3c5039 4716 }
6e8dda9c 4717 if (imm_expr.X_add_number >= 0 && imm_expr.X_add_number < 0x10000)
670a50eb 4718 {
0dd2d296
ILT
4719 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
4720 dreg, sreg, (int) BFD_RELOC_LO16);
670a50eb 4721 used_at = 0;
6e8dda9c
ILT
4722 }
4723 else if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number < 0)
4724 {
4725 imm_expr.X_add_number = -imm_expr.X_add_number;
0dd2d296 4726 macro_build ((char *) NULL, &icnt, &imm_expr,
6e8dda9c 4727 mips_isa < 3 ? "addiu" : "daddiu",
9226253a 4728 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6e8dda9c
ILT
4729 used_at = 0;
4730 }
4731 else
4732 {
d8a1c247 4733 load_register (&icnt, AT, &imm_expr, 0);
0dd2d296
ILT
4734 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
4735 sreg, AT);
670a50eb
ILT
4736 used_at = 1;
4737 }
0dd2d296 4738 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
670a50eb
ILT
4739 if (used_at)
4740 break;
4741 return;
3d3c5039 4742
8358c818
ILT
4743 case M_DSUB_I:
4744 dbl = 1;
3d3c5039 4745 case M_SUB_I:
6e8dda9c 4746 if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number <= 0x8000)
670a50eb
ILT
4747 {
4748 imm_expr.X_add_number = -imm_expr.X_add_number;
0dd2d296 4749 macro_build ((char *) NULL, &icnt, &imm_expr,
8358c818 4750 dbl ? "daddi" : "addi",
9226253a 4751 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
670a50eb 4752 return;
3d3c5039 4753 }
d8a1c247 4754 load_register (&icnt, AT, &imm_expr, dbl);
0dd2d296 4755 macro_build ((char *) NULL, &icnt, NULL,
8358c818
ILT
4756 dbl ? "dsub" : "sub",
4757 "d,v,t", dreg, sreg, AT);
670a50eb 4758 break;
3d3c5039 4759
8358c818
ILT
4760 case M_DSUBU_I:
4761 dbl = 1;
3d3c5039 4762 case M_SUBU_I:
6e8dda9c 4763 if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number <= 0x8000)
670a50eb
ILT
4764 {
4765 imm_expr.X_add_number = -imm_expr.X_add_number;
0dd2d296 4766 macro_build ((char *) NULL, &icnt, &imm_expr,
8358c818 4767 dbl ? "daddiu" : "addiu",
9226253a 4768 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
670a50eb 4769 return;
3d3c5039 4770 }
d8a1c247 4771 load_register (&icnt, AT, &imm_expr, dbl);
0dd2d296 4772 macro_build ((char *) NULL, &icnt, NULL,
8358c818
ILT
4773 dbl ? "dsubu" : "subu",
4774 "d,v,t", dreg, sreg, AT);
4775 break;
4776
4777 case M_TEQ_I:
4778 s = "teq";
4779 goto trap;
4780 case M_TGE_I:
4781 s = "tge";
4782 goto trap;
4783 case M_TGEU_I:
4784 s = "tgeu";
4785 goto trap;
4786 case M_TLT_I:
4787 s = "tlt";
4788 goto trap;
4789 case M_TLTU_I:
4790 s = "tltu";
4791 goto trap;
4792 case M_TNE_I:
4793 s = "tne";
4794 trap:
d8a1c247 4795 load_register (&icnt, AT, &imm_expr, 0);
0dd2d296 4796 macro_build ((char *) NULL, &icnt, NULL, s, "s,t", sreg, AT);
670a50eb 4797 break;
3d3c5039
ILT
4798
4799 case M_TRUNCWD:
4800 case M_TRUNCWS:
8358c818 4801 assert (mips_isa < 2);
670a50eb
ILT
4802 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
4803 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
4804
4805 /*
4806 * Is the double cfc1 instruction a bug in the mips assembler;
4807 * or is there a reason for it?
4808 */
becfe05e
ILT
4809 mips_emit_delays ();
4810 ++mips_noreorder;
0dd2d296
ILT
4811 mips_any_noreorder = 1;
4812 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
4813 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
4814 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
670a50eb 4815 expr1.X_add_number = 3;
0dd2d296 4816 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
9226253a 4817 (int) BFD_RELOC_LO16);
670a50eb 4818 expr1.X_add_number = 2;
0dd2d296 4819 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
9226253a 4820 (int) BFD_RELOC_LO16);
0dd2d296
ILT
4821 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", AT, 31);
4822 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
4823 macro_build ((char *) NULL, &icnt, NULL,
670a50eb 4824 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
0dd2d296
ILT
4825 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", treg, 31);
4826 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
becfe05e 4827 --mips_noreorder;
670a50eb 4828 break;
3d3c5039
ILT
4829
4830 case M_ULH:
670a50eb
ILT
4831 s = "lb";
4832 goto ulh;
3d3c5039 4833 case M_ULHU:
670a50eb 4834 s = "lbu";
3d3c5039 4835 ulh:
8ea7f4e8
ILT
4836 if (offset_expr.X_add_number >= 0x7fff)
4837 as_bad ("operand overflow");
670a50eb 4838 /* avoid load delay */
8ea7f4e8
ILT
4839 if (byte_order == LITTLE_ENDIAN)
4840 offset_expr.X_add_number += 1;
0dd2d296 4841 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
9226253a 4842 (int) BFD_RELOC_LO16, breg);
8ea7f4e8
ILT
4843 if (byte_order == LITTLE_ENDIAN)
4844 offset_expr.X_add_number -= 1;
4845 else
4846 offset_expr.X_add_number += 1;
0dd2d296 4847 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
9226253a 4848 (int) BFD_RELOC_LO16, breg);
0dd2d296
ILT
4849 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
4850 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
670a50eb 4851 break;
3d3c5039 4852
adcf2b9d
ILT
4853 case M_ULD:
4854 s = "ldl";
4855 s2 = "ldr";
4856 off = 7;
4857 goto ulw;
3d3c5039 4858 case M_ULW:
adcf2b9d
ILT
4859 s = "lwl";
4860 s2 = "lwr";
4861 off = 3;
4862 ulw:
4863 if (offset_expr.X_add_number >= 0x8000 - off)
8ea7f4e8
ILT
4864 as_bad ("operand overflow");
4865 if (byte_order == LITTLE_ENDIAN)
adcf2b9d
ILT
4866 offset_expr.X_add_number += off;
4867 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
9226253a 4868 (int) BFD_RELOC_LO16, breg);
8ea7f4e8 4869 if (byte_order == LITTLE_ENDIAN)
adcf2b9d 4870 offset_expr.X_add_number -= off;
8ea7f4e8 4871 else
adcf2b9d
ILT
4872 offset_expr.X_add_number += off;
4873 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
9226253a 4874 (int) BFD_RELOC_LO16, breg);
670a50eb 4875 return;
3d3c5039 4876
adcf2b9d
ILT
4877 case M_ULD_A:
4878 s = "ldl";
4879 s2 = "ldr";
4880 off = 7;
4881 goto ulwa;
4882 case M_ULW_A:
4883 s = "lwl";
4884 s2 = "lwr";
4885 off = 3;
4886 ulwa:
4887 load_address (&icnt, AT, &offset_expr);
c625fc23
JSC
4888 if (breg != 0)
4889 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4890 mips_isa < 3 ? "addu" : "daddu",
4891 "d,v,t", AT, AT, breg);
adcf2b9d
ILT
4892 if (byte_order == LITTLE_ENDIAN)
4893 expr1.X_add_number = off;
4894 else
4895 expr1.X_add_number = 0;
4896 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
4897 (int) BFD_RELOC_LO16, AT);
4898 if (byte_order == LITTLE_ENDIAN)
4899 expr1.X_add_number = 0;
4900 else
4901 expr1.X_add_number = off;
4902 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
4903 (int) BFD_RELOC_LO16, AT);
4904 break;
4905
3d3c5039
ILT
4906 case M_ULH_A:
4907 case M_ULHU_A:
0dd2d296 4908 load_address (&icnt, AT, &offset_expr);
c625fc23
JSC
4909 if (breg != 0)
4910 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4911 mips_isa < 3 ? "addu" : "daddu",
4912 "d,v,t", AT, AT, breg);
adcf2b9d
ILT
4913 if (byte_order == BIG_ENDIAN)
4914 expr1.X_add_number = 0;
4915 macro_build ((char *) NULL, &icnt, &expr1,
4916 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
4917 (int) BFD_RELOC_LO16, AT);
4918 if (byte_order == BIG_ENDIAN)
4919 expr1.X_add_number = 1;
670a50eb 4920 else
adcf2b9d
ILT
4921 expr1.X_add_number = 0;
4922 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
4923 (int) BFD_RELOC_LO16, AT);
4924 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
4925 treg, 8);
4926 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
4927 treg, AT);
670a50eb 4928 break;
3d3c5039
ILT
4929
4930 case M_USH:
8ea7f4e8
ILT
4931 if (offset_expr.X_add_number >= 0x7fff)
4932 as_bad ("operand overflow");
4933 if (byte_order == BIG_ENDIAN)
4934 offset_expr.X_add_number += 1;
0dd2d296 4935 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
9226253a 4936 (int) BFD_RELOC_LO16, breg);
0dd2d296 4937 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
8ea7f4e8
ILT
4938 if (byte_order == BIG_ENDIAN)
4939 offset_expr.X_add_number -= 1;
4940 else
4941 offset_expr.X_add_number += 1;
0dd2d296 4942 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
9226253a 4943 (int) BFD_RELOC_LO16, breg);
670a50eb 4944 break;
3d3c5039 4945
adcf2b9d
ILT
4946 case M_USD:
4947 s = "sdl";
4948 s2 = "sdr";
4949 off = 7;
4950 goto usw;
3d3c5039 4951 case M_USW:
adcf2b9d
ILT
4952 s = "swl";
4953 s2 = "swr";
4954 off = 3;
4955 usw:
4956 if (offset_expr.X_add_number >= 0x8000 - off)
8ea7f4e8
ILT
4957 as_bad ("operand overflow");
4958 if (byte_order == LITTLE_ENDIAN)
adcf2b9d
ILT
4959 offset_expr.X_add_number += off;
4960 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
9226253a 4961 (int) BFD_RELOC_LO16, breg);
8ea7f4e8 4962 if (byte_order == LITTLE_ENDIAN)
adcf2b9d 4963 offset_expr.X_add_number -= off;
8ea7f4e8 4964 else
adcf2b9d
ILT
4965 offset_expr.X_add_number += off;
4966 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
9226253a 4967 (int) BFD_RELOC_LO16, breg);
670a50eb 4968 return;
3d3c5039 4969
adcf2b9d
ILT
4970 case M_USD_A:
4971 s = "sdl";
4972 s2 = "sdr";
4973 off = 7;
4974 goto uswa;
3d3c5039 4975 case M_USW_A:
adcf2b9d
ILT
4976 s = "swl";
4977 s2 = "swr";
4978 off = 3;
4979 uswa:
0dd2d296 4980 load_address (&icnt, AT, &offset_expr);
c625fc23
JSC
4981 if (breg != 0)
4982 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4983 mips_isa < 3 ? "addu" : "daddu",
4984 "d,v,t", AT, AT, breg);
adcf2b9d
ILT
4985 if (byte_order == LITTLE_ENDIAN)
4986 expr1.X_add_number = off;
670a50eb 4987 else
adcf2b9d
ILT
4988 expr1.X_add_number = 0;
4989 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
4990 (int) BFD_RELOC_LO16, AT);
4991 if (byte_order == LITTLE_ENDIAN)
4992 expr1.X_add_number = 0;
4993 else
4994 expr1.X_add_number = off;
4995 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
4996 (int) BFD_RELOC_LO16, AT);
4997 break;
4998
4999 case M_USH_A:
5000 load_address (&icnt, AT, &offset_expr);
c625fc23
JSC
5001 if (breg != 0)
5002 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5003 mips_isa < 3 ? "addu" : "daddu",
5004 "d,v,t", AT, AT, breg);
adcf2b9d
ILT
5005 if (byte_order == LITTLE_ENDIAN)
5006 expr1.X_add_number = 0;
5007 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
5008 (int) BFD_RELOC_LO16, AT);
5009 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", treg,
5010 treg, 8);
5011 if (byte_order == LITTLE_ENDIAN)
5012 expr1.X_add_number = 1;
5013 else
5014 expr1.X_add_number = 0;
5015 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
5016 (int) BFD_RELOC_LO16, AT);
5017 if (byte_order == LITTLE_ENDIAN)
5018 expr1.X_add_number = 0;
5019 else
5020 expr1.X_add_number = 1;
5021 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
5022 (int) BFD_RELOC_LO16, AT);
5023 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
5024 treg, 8);
5025 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
5026 treg, AT);
670a50eb 5027 break;
3d3c5039
ILT
5028
5029 default:
670a50eb 5030 as_bad ("Macro %s not implemented yet", ip->insn_mo->name);
8358c818 5031 break;
3d3c5039 5032 }
670a50eb
ILT
5033 if (mips_noat)
5034 as_warn ("Macro used $at after \".set noat\"");
3d3c5039
ILT
5035}
5036
867a58b3
ILT
5037/* This routine assembles an instruction into its binary format. As a
5038 side effect, it sets one of the global variables imm_reloc or
5039 offset_reloc to the type of relocation to do if one of the operands
5040 is an address expression. */
3d3c5039 5041
3d3c5039
ILT
5042static void
5043mips_ip (str, ip)
5044 char *str;
5045 struct mips_cl_insn *ip;
5046{
670a50eb
ILT
5047 char *s;
5048 const char *args;
5049 char c;
5050 struct mips_opcode *insn;
5051 char *argsStart;
5052 unsigned int regno;
5053 unsigned int lastregno = 0;
5054 char *s_reset;
5055
5056 insn_error = NULL;
5057
c625fc23 5058 for (s = str; islower (*s) || (*s >= '0' && *s <= '3') || *s == '6' || *s == '.'; ++s)
670a50eb
ILT
5059 continue;
5060 switch (*s)
5061 {
3d3c5039 5062 case '\0':
670a50eb 5063 break;
3d3c5039
ILT
5064
5065 case ' ':
670a50eb
ILT
5066 *s++ = '\0';
5067 break;
3d3c5039
ILT
5068
5069 default:
460531da 5070 as_fatal ("Unknown opcode: `%s'", str);
3d3c5039 5071 }
670a50eb
ILT
5072 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
5073 {
9753202d 5074 insn_error = "unrecognized opcode";
670a50eb 5075 return;
3d3c5039 5076 }
670a50eb
ILT
5077 argsStart = s;
5078 for (;;)
5079 {
8358c818
ILT
5080 int insn_isa;
5081
670a50eb 5082 assert (strcmp (insn->name, str) == 0);
8358c818
ILT
5083
5084 if (insn->pinfo == INSN_MACRO)
5085 insn_isa = insn->match;
b2b8c24e 5086 else if ((insn->pinfo & INSN_ISA) == INSN_ISA2)
8358c818 5087 insn_isa = 2;
b2b8c24e 5088 else if ((insn->pinfo & INSN_ISA) == INSN_ISA3)
8358c818 5089 insn_isa = 3;
d8a1c247
KR
5090 else if ((insn->pinfo & INSN_ISA) == INSN_ISA4)
5091 insn_isa = 4;
8358c818
ILT
5092 else
5093 insn_isa = 1;
5094
b2b8c24e
ILT
5095 if (insn_isa > mips_isa
5096 || ((insn->pinfo & INSN_ISA) == INSN_4650
e532b44c
ILT
5097 && ! mips_4650)
5098 || ((insn->pinfo & INSN_ISA) == INSN_4010
c625fc23
JSC
5099 && ! mips_4010)
5100 || ((insn->pinfo & INSN_ISA) == INSN_4100
5101 && ! mips_4100))
8358c818
ILT
5102 {
5103 if (insn + 1 < &mips_opcodes[NUMOPCODES]
5104 && strcmp (insn->name, insn[1].name) == 0)
5105 {
5106 ++insn;
5107 continue;
5108 }
9753202d
ILT
5109 insn_error = "opcode not supported on this processor";
5110 return;
8358c818
ILT
5111 }
5112
670a50eb
ILT
5113 ip->insn_mo = insn;
5114 ip->insn_opcode = insn->match;
5115 for (args = insn->args;; ++args)
5116 {
5117 if (*s == ' ')
5118 ++s;
5119 switch (*args)
5120 {
5121 case '\0': /* end of args */
5122 if (*s == '\0')
5123 return;
5124 break;
3d3c5039
ILT
5125
5126 case ',':
670a50eb
ILT
5127 if (*s++ == *args)
5128 continue;
5129 s--;
5130 switch (*++args)
5131 {
3d3c5039
ILT
5132 case 'r':
5133 case 'v':
670a50eb
ILT
5134 ip->insn_opcode |= lastregno << 21;
5135 continue;
3d3c5039
ILT
5136
5137 case 'w':
5138 case 'W':
670a50eb
ILT
5139 ip->insn_opcode |= lastregno << 16;
5140 continue;
3d3c5039
ILT
5141
5142 case 'V':
670a50eb
ILT
5143 ip->insn_opcode |= lastregno << 11;
5144 continue;
3d3c5039 5145 }
670a50eb 5146 break;
3d3c5039
ILT
5147
5148 case '(':
670a50eb
ILT
5149 /* handle optional base register.
5150 Either the base register is omitted or
5151 we must have a left paren. */
5152 /* this is dependent on the next operand specifier
5153 is a 'b' for base register */
5154 assert (args[1] == 'b');
5155 if (*s == '\0')
5156 return;
3d3c5039 5157
670a50eb
ILT
5158 case ')': /* these must match exactly */
5159 if (*s++ == *args)
3d3c5039 5160 continue;
670a50eb
ILT
5161 break;
5162
5163 case '<': /* must be at least one digit */
5164 /*
5165 * According to the manual, if the shift amount is greater
5166 * than 31 or less than 0 the the shift amount should be
5167 * mod 32. In reality the mips assembler issues an error.
9226253a 5168 * We issue a warning and mask out all but the low 5 bits.
670a50eb
ILT
5169 */
5170 my_getExpression (&imm_expr, s);
5171 check_absolute_expr (ip, &imm_expr);
5172 if ((unsigned long) imm_expr.X_add_number > 31)
5173 {
58d4951d
ILT
5174 as_warn ("Improper shift amount (%ld)",
5175 (long) imm_expr.X_add_number);
9226253a 5176 imm_expr.X_add_number = imm_expr.X_add_number & 0x1f;
670a50eb
ILT
5177 }
5178 ip->insn_opcode |= imm_expr.X_add_number << 6;
5ac34ac3 5179 imm_expr.X_op = O_absent;
670a50eb
ILT
5180 s = expr_end;
5181 continue;
5182
56c96faa
ILT
5183 case '>': /* shift amount minus 32 */
5184 my_getExpression (&imm_expr, s);
5185 check_absolute_expr (ip, &imm_expr);
5186 if ((unsigned long) imm_expr.X_add_number < 32
5187 || (unsigned long) imm_expr.X_add_number > 63)
5188 break;
5189 ip->insn_opcode |= (imm_expr.X_add_number - 32) << 6;
5190 imm_expr.X_op = O_absent;
5191 s = expr_end;
5192 continue;
5193
9226253a 5194 case 'k': /* cache code */
d8a1c247 5195 case 'h': /* prefx code */
9226253a
ILT
5196 my_getExpression (&imm_expr, s);
5197 check_absolute_expr (ip, &imm_expr);
5198 if ((unsigned long) imm_expr.X_add_number > 31)
5199 {
d8a1c247
KR
5200 as_warn ("Invalid value for `%s' (%lu)",
5201 ip->insn_mo->name,
9226253a
ILT
5202 (unsigned long) imm_expr.X_add_number);
5203 imm_expr.X_add_number &= 0x1f;
5204 }
d8a1c247
KR
5205 if (*args == 'k')
5206 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
5207 else
5208 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
9226253a
ILT
5209 imm_expr.X_op = O_absent;
5210 s = expr_end;
5211 continue;
5212
670a50eb
ILT
5213 case 'c': /* break code */
5214 my_getExpression (&imm_expr, s);
5215 check_absolute_expr (ip, &imm_expr);
5216 if ((unsigned) imm_expr.X_add_number > 1023)
58d4951d
ILT
5217 as_warn ("Illegal break code (%ld)",
5218 (long) imm_expr.X_add_number);
670a50eb 5219 ip->insn_opcode |= imm_expr.X_add_number << 16;
5ac34ac3 5220 imm_expr.X_op = O_absent;
670a50eb
ILT
5221 s = expr_end;
5222 continue;
5223
918692a5
ILT
5224 case 'B': /* syscall code */
5225 my_getExpression (&imm_expr, s);
5226 check_absolute_expr (ip, &imm_expr);
5227 if ((unsigned) imm_expr.X_add_number > 0xfffff)
58d4951d
ILT
5228 as_warn ("Illegal syscall code (%ld)",
5229 (long) imm_expr.X_add_number);
918692a5 5230 ip->insn_opcode |= imm_expr.X_add_number << 6;
5ac34ac3 5231 imm_expr.X_op = O_absent;
918692a5
ILT
5232 s = expr_end;
5233 continue;
5234
0aa07269
ILT
5235 case 'C': /* Coprocessor code */
5236 my_getExpression (&imm_expr, s);
5237 check_absolute_expr (ip, &imm_expr);
5238 if ((unsigned long) imm_expr.X_add_number >= (1<<25))
5239 {
58d4951d
ILT
5240 as_warn ("Coproccesor code > 25 bits (%ld)",
5241 (long) imm_expr.X_add_number);
0aa07269
ILT
5242 imm_expr.X_add_number &= ((1<<25) - 1);
5243 }
5244 ip->insn_opcode |= imm_expr.X_add_number;
5245 imm_expr.X_op = O_absent;
5246 s = expr_end;
5247 continue;
5248
670a50eb
ILT
5249 case 'b': /* base register */
5250 case 'd': /* destination register */
5251 case 's': /* source register */
5252 case 't': /* target register */
5253 case 'r': /* both target and source */
5254 case 'v': /* both dest and source */
5255 case 'w': /* both dest and target */
918692a5
ILT
5256 case 'E': /* coprocessor target register */
5257 case 'G': /* coprocessor destination register */
8358c818 5258 case 'x': /* ignore register name */
ff3a5c18 5259 case 'z': /* must be zero register */
670a50eb
ILT
5260 s_reset = s;
5261 if (s[0] == '$')
5262 {
5263 if (isdigit (s[1]))
5264 {
5265 ++s;
5266 regno = 0;
5267 do
5268 {
5269 regno *= 10;
5270 regno += *s - '0';
5271 ++s;
5272 }
5273 while (isdigit (*s));
0aa07269
ILT
5274 if (regno > 31)
5275 as_bad ("Invalid register number (%d)", regno);
670a50eb 5276 }
0dd2d296
ILT
5277 else if (*args == 'E' || *args == 'G')
5278 goto notreg;
5279 else
670a50eb 5280 {
0aa07269
ILT
5281 if (s[1] == 'f' && s[2] == 'p')
5282 {
5283 s += 3;
9226253a 5284 regno = FP;
0aa07269
ILT
5285 }
5286 else if (s[1] == 's' && s[2] == 'p')
5287 {
5288 s += 3;
9226253a 5289 regno = SP;
0aa07269
ILT
5290 }
5291 else if (s[1] == 'g' && s[2] == 'p')
5292 {
5293 s += 3;
9226253a 5294 regno = GP;
0aa07269
ILT
5295 }
5296 else if (s[1] == 'a' && s[2] == 't')
5297 {
5298 s += 3;
9226253a 5299 regno = AT;
0aa07269 5300 }
b2b8c24e
ILT
5301 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
5302 {
5303 s += 4;
5304 regno = KT0;
5305 }
5306 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
5307 {
5308 s += 4;
5309 regno = KT1;
5310 }
0aa07269
ILT
5311 else
5312 goto notreg;
670a50eb 5313 }
9753202d
ILT
5314 if (regno == AT
5315 && ! mips_noat
5316 && *args != 'E'
5317 && *args != 'G')
13fe1379 5318 as_warn ("Used $at without \".set noat\"");
670a50eb
ILT
5319 c = *args;
5320 if (*s == ' ')
5321 s++;
5322 if (args[1] != *s)
5323 {
5324 if (c == 'r' || c == 'v' || c == 'w')
5325 {
5326 regno = lastregno;
5327 s = s_reset;
5328 args++;
5329 }
5330 }
ff3a5c18
ILT
5331 /* 'z' only matches $0. */
5332 if (c == 'z' && regno != 0)
5333 break;
670a50eb
ILT
5334 switch (c)
5335 {
3d3c5039
ILT
5336 case 'r':
5337 case 's':
5338 case 'v':
5339 case 'b':
670a50eb
ILT
5340 ip->insn_opcode |= regno << 21;
5341 break;
3d3c5039 5342 case 'd':
918692a5 5343 case 'G':
670a50eb
ILT
5344 ip->insn_opcode |= regno << 11;
5345 break;
3d3c5039
ILT
5346 case 'w':
5347 case 't':
918692a5 5348 case 'E':
670a50eb 5349 ip->insn_opcode |= regno << 16;
8358c818
ILT
5350 break;
5351 case 'x':
5352 /* This case exists because on the r3000 trunc
5353 expands into a macro which requires a gp
5354 register. On the r6000 or r4000 it is
5355 assembled into a single instruction which
5356 ignores the register. Thus the insn version
5357 is MIPS_ISA2 and uses 'x', and the macro
5358 version is MIPS_ISA1 and uses 't'. */
5359 break;
ff3a5c18
ILT
5360 case 'z':
5361 /* This case is for the div instruction, which
5362 acts differently if the destination argument
5363 is $0. This only matches $0, and is checked
5364 outside the switch. */
5365 break;
3d3c5039 5366 }
670a50eb
ILT
5367 lastregno = regno;
5368 continue;
3d3c5039
ILT
5369 }
5370 notreg:
670a50eb
ILT
5371 switch (*args++)
5372 {
3d3c5039
ILT
5373 case 'r':
5374 case 'v':
670a50eb
ILT
5375 ip->insn_opcode |= lastregno << 21;
5376 continue;
3d3c5039 5377 case 'w':
670a50eb
ILT
5378 ip->insn_opcode |= lastregno << 16;
5379 continue;
3d3c5039 5380 }
670a50eb 5381 break;
3d3c5039 5382
670a50eb
ILT
5383 case 'D': /* floating point destination register */
5384 case 'S': /* floating point source register */
5385 case 'T': /* floating point target register */
d8a1c247 5386 case 'R': /* floating point source register */
3d3c5039
ILT
5387 case 'V':
5388 case 'W':
670a50eb
ILT
5389 s_reset = s;
5390 if (s[0] == '$' && s[1] == 'f' && isdigit (s[2]))
5391 {
5392 s += 2;
5393 regno = 0;
5394 do
5395 {
5396 regno *= 10;
5397 regno += *s - '0';
5398 ++s;
5399 }
5400 while (isdigit (*s));
5401
5402 if (regno > 31)
5403 as_bad ("Invalid float register number (%d)", regno);
5404
9226253a
ILT
5405 if ((regno & 1) != 0
5406 && mips_isa < 3
5407 && ! (strcmp (str, "mtc1") == 0 ||
5408 strcmp (str, "mfc1") == 0 ||
5409 strcmp (str, "lwc1") == 0 ||
5410 strcmp (str, "swc1") == 0))
670a50eb
ILT
5411 as_warn ("Float register should be even, was %d",
5412 regno);
5413
5414 c = *args;
5415 if (*s == ' ')
5416 s++;
5417 if (args[1] != *s)
5418 {
5419 if (c == 'V' || c == 'W')
5420 {
5421 regno = lastregno;
5422 s = s_reset;
5423 args++;
3d3c5039
ILT
5424 }
5425 }
670a50eb
ILT
5426 switch (c)
5427 {
3d3c5039 5428 case 'D':
670a50eb
ILT
5429 ip->insn_opcode |= regno << 6;
5430 break;
3d3c5039
ILT
5431 case 'V':
5432 case 'S':
670a50eb
ILT
5433 ip->insn_opcode |= regno << 11;
5434 break;
3d3c5039
ILT
5435 case 'W':
5436 case 'T':
670a50eb 5437 ip->insn_opcode |= regno << 16;
d8a1c247
KR
5438 break;
5439 case 'R':
5440 ip->insn_opcode |= regno << 21;
5441 break;
3d3c5039 5442 }
670a50eb
ILT
5443 lastregno = regno;
5444 continue;
3d3c5039 5445 }
670a50eb
ILT
5446 switch (*args++)
5447 {
3d3c5039 5448 case 'V':
670a50eb
ILT
5449 ip->insn_opcode |= lastregno << 11;
5450 continue;
3d3c5039 5451 case 'W':
670a50eb
ILT
5452 ip->insn_opcode |= lastregno << 16;
5453 continue;
3d3c5039 5454 }
670a50eb 5455 break;
3d3c5039
ILT
5456
5457 case 'I':
670a50eb 5458 my_getExpression (&imm_expr, s);
9753202d
ILT
5459 if (imm_expr.X_op != O_big
5460 && imm_expr.X_op != O_constant)
5461 insn_error = "absolute expression required";
670a50eb
ILT
5462 s = expr_end;
5463 continue;
3d3c5039
ILT
5464
5465 case 'A':
670a50eb
ILT
5466 my_getExpression (&offset_expr, s);
5467 imm_reloc = BFD_RELOC_32;
5468 s = expr_end;
5469 continue;
3d3c5039
ILT
5470
5471 case 'F':
19ed8960
ILT
5472 case 'L':
5473 case 'f':
5474 case 'l':
5475 {
5476 int f64;
5477 char *save_in;
5478 char *err;
5479 unsigned char temp[8];
604633ae
ILT
5480 int len;
5481 unsigned int length;
19ed8960
ILT
5482 segT seg;
5483 subsegT subseg;
5484 char *p;
5485
5486 /* These only appear as the last operand in an
5487 instruction, and every instruction that accepts
5488 them in any variant accepts them in all variants.
5489 This means we don't have to worry about backing out
5490 any changes if the instruction does not match.
5491
5492 The difference between them is the size of the
5493 floating point constant and where it goes. For 'F'
5494 and 'L' the constant is 64 bits; for 'f' and 'l' it
5495 is 32 bits. Where the constant is placed is based
5496 on how the MIPS assembler does things:
5497 F -- .rdata
5498 L -- .lit8
5499 f -- immediate value
5500 l -- .lit4
0dd2d296 5501
55933a58
ILT
5502 The .lit4 and .lit8 sections are only used if
5503 permitted by the -G argument.
5504
5505 When generating embedded PIC code, we use the
5506 .lit8 section but not the .lit4 section (we can do
5507 .lit4 inline easily; we need to put .lit8
5508 somewhere in the data segment, and using .lit8
5509 permits the linker to eventually combine identical
5510 .lit8 entries). */
19ed8960
ILT
5511
5512 f64 = *args == 'F' || *args == 'L';
5513
5514 save_in = input_line_pointer;
5515 input_line_pointer = s;
604633ae
ILT
5516 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
5517 length = len;
19ed8960
ILT
5518 s = input_line_pointer;
5519 input_line_pointer = save_in;
5520 if (err != NULL && *err != '\0')
5521 {
5522 as_bad ("Bad floating point constant: %s", err);
5523 memset (temp, '\0', sizeof temp);
5524 length = f64 ? 8 : 4;
5525 }
5526
5527 assert (length == (f64 ? 8 : 4));
5528
0dd2d296 5529 if (*args == 'f'
55933a58 5530 || (*args == 'l'
1dc1e798
KR
5531 && (! USE_GLOBAL_POINTER_OPT
5532 || mips_pic == EMBEDDED_PIC
1113140a 5533 || g_switch_value < 4)
1113140a 5534 ))
19ed8960
ILT
5535 {
5536 imm_expr.X_op = O_constant;
5537 if (byte_order == LITTLE_ENDIAN)
5538 imm_expr.X_add_number =
5539 (((((((int) temp[3] << 8)
5540 | temp[2]) << 8)
5541 | temp[1]) << 8)
5542 | temp[0]);
5543 else
5544 imm_expr.X_add_number =
5545 (((((((int) temp[0] << 8)
5546 | temp[1]) << 8)
5547 | temp[2]) << 8)
5548 | temp[3]);
5549 }
5550 else
5551 {
0dd2d296
ILT
5552 const char *newname;
5553 segT new_seg;
5554
19ed8960
ILT
5555 /* Switch to the right section. */
5556 seg = now_seg;
5557 subseg = now_subseg;
5558 switch (*args)
5559 {
0dd2d296 5560 default: /* unused default case avoids warnings. */
19ed8960 5561 case 'L':
1113140a 5562 newname = RDATA_SECTION_NAME;
1dc1e798 5563 if (USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
1113140a 5564 newname = ".lit8";
0dd2d296
ILT
5565 break;
5566 case 'F':
d2c71068 5567 newname = RDATA_SECTION_NAME;
19ed8960
ILT
5568 break;
5569 case 'l':
1dc1e798
KR
5570 assert (!USE_GLOBAL_POINTER_OPT
5571 || g_switch_value >= 4);
0dd2d296 5572 newname = ".lit4";
19ed8960
ILT
5573 break;
5574 }
0dd2d296 5575 new_seg = subseg_new (newname, (subsegT) 0);
9b61d62b
ILT
5576 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
5577 bfd_set_section_flags (stdoutput, new_seg,
5578 (SEC_ALLOC
5579 | SEC_LOAD
5580 | SEC_READONLY
5581 | SEC_DATA));
0221ddf7 5582 frag_align (*args == 'l' ? 2 : 3, 0);
1dc1e798
KR
5583 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
5584 record_alignment (new_seg, 4);
5585 else
5586 record_alignment (new_seg, *args == 'l' ? 2 : 3);
19ed8960
ILT
5587 if (seg == now_seg)
5588 as_bad ("Can't use floating point insn in this section");
5589
5590 /* Set the argument to the current address in the
6e8dda9c 5591 section. */
19ed8960
ILT
5592 offset_expr.X_op = O_symbol;
5593 offset_expr.X_add_symbol =
5594 symbol_new ("L0\001", now_seg,
5595 (valueT) frag_now_fix (), frag_now);
5596 offset_expr.X_add_number = 0;
5597
5598 /* Put the floating point number into the section. */
604633ae 5599 p = frag_more ((int) length);
19ed8960
ILT
5600 memcpy (p, temp, length);
5601
5602 /* Switch back to the original section. */
5603 subseg_set (seg, subseg);
5604 }
5605 }
670a50eb
ILT
5606 continue;
5607
5608 case 'i': /* 16 bit unsigned immediate */
5609 case 'j': /* 16 bit signed immediate */
5610 imm_reloc = BFD_RELOC_LO16;
5611 c = my_getSmallExpression (&imm_expr, s);
344a8d61 5612 if (c != '\0')
670a50eb
ILT
5613 {
5614 if (c != 'l')
5615 {
5ac34ac3 5616 if (imm_expr.X_op == O_constant)
670a50eb
ILT
5617 imm_expr.X_add_number =
5618 (imm_expr.X_add_number >> 16) & 0xffff;
5619 else if (c == 'h')
867a58b3
ILT
5620 {
5621 imm_reloc = BFD_RELOC_HI16_S;
5622 imm_unmatched_hi = true;
5623 }
670a50eb
ILT
5624 else
5625 imm_reloc = BFD_RELOC_HI16;
3d3c5039 5626 }
670a50eb 5627 }
670a50eb
ILT
5628 if (*args == 'i')
5629 {
344a8d61 5630 if ((c == '\0' && imm_expr.X_op != O_constant)
a8aed9dd
ILT
5631 || ((imm_expr.X_add_number < 0
5632 || imm_expr.X_add_number >= 0x10000)
5633 && imm_expr.X_op == O_constant))
99c24539
ILT
5634 {
5635 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
5636 !strcmp (insn->name, insn[1].name))
5637 break;
9753202d
ILT
5638 if (imm_expr.X_op != O_constant
5639 && imm_expr.X_op != O_big)
5640 insn_error = "absolute expression required";
5641 else
5642 as_bad ("16 bit expression not in range 0..65535");
99c24539 5643 }
670a50eb
ILT
5644 }
5645 else
5646 {
d9aba805
ILT
5647 int more;
5648 offsetT max;
5649
be22008b
ILT
5650 /* The upper bound should be 0x8000, but
5651 unfortunately the MIPS assembler accepts numbers
5652 from 0x8000 to 0xffff and sign extends them, and
d9aba805
ILT
5653 we want to be compatible. We only permit this
5654 extended range for an instruction which does not
5655 provide any further alternates, since those
5656 alternates may handle other cases. People should
5657 use the numbers they mean, rather than relying on
5658 a mysterious sign extension. */
5659 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
5660 strcmp (insn->name, insn[1].name) == 0);
5661 if (more)
5662 max = 0x8000;
5663 else
5664 max = 0x10000;
344a8d61 5665 if ((c == '\0' && imm_expr.X_op != O_constant)
a8aed9dd
ILT
5666 || ((imm_expr.X_add_number < -0x8000
5667 || imm_expr.X_add_number >= max)
5668 && imm_expr.X_op == O_constant)
d8a1c247
KR
5669 || (more
5670 && imm_expr.X_add_number < 0
5671 && mips_isa >= 3
5672 && imm_expr.X_unsigned
5673 && sizeof (imm_expr.X_add_number) <= 4))
99c24539 5674 {
d9aba805 5675 if (more)
99c24539 5676 break;
9753202d
ILT
5677 if (imm_expr.X_op != O_constant
5678 && imm_expr.X_op != O_big)
5679 insn_error = "absolute expression required";
5680 else
5681 as_bad ("16 bit expression not in range -32768..32767");
99c24539 5682 }
3d3c5039 5683 }
670a50eb
ILT
5684 s = expr_end;
5685 continue;
5686
5687 case 'o': /* 16 bit offset */
5688 c = my_getSmallExpression (&offset_expr, s);
f3645945
ILT
5689
5690 /* If this value won't fit into a 16 bit offset, then go
5691 find a macro that will generate the 32 bit offset
5692 code pattern. As a special hack, we accept the
5693 difference of two local symbols as a constant. This
5694 is required to suppose embedded PIC switches, which
5695 use an instruction which looks like
5696 lw $4,$L12-$LS12($4)
5697 The problem with handling this in a more general
5698 fashion is that the macro function doesn't expect to
5699 see anything which can be handled in a single
5700 constant instruction. */
6f0b87c3
SS
5701 if (c == 0
5702 && (offset_expr.X_op != O_constant
5703 || offset_expr.X_add_number >= 0x8000
5704 || offset_expr.X_add_number < -0x8000)
f3645945
ILT
5705 && (mips_pic != EMBEDDED_PIC
5706 || offset_expr.X_op != O_subtract
9da4c5d1
ILT
5707 || now_seg != text_section
5708 || (S_GET_SEGMENT (offset_expr.X_op_symbol)
5709 != text_section)))
670a50eb 5710 break;
3d3c5039 5711
670a50eb
ILT
5712 offset_reloc = BFD_RELOC_LO16;
5713 if (c == 'h' || c == 'H')
6e8dda9c
ILT
5714 {
5715 assert (offset_expr.X_op == O_constant);
5716 offset_expr.X_add_number =
5717 (offset_expr.X_add_number >> 16) & 0xffff;
5718 }
670a50eb
ILT
5719 s = expr_end;
5720 continue;
5721
5722 case 'p': /* pc relative offset */
5723 offset_reloc = BFD_RELOC_16_PCREL_S2;
5724 my_getExpression (&offset_expr, s);
5725 s = expr_end;
5726 continue;
5727
5728 case 'u': /* upper 16 bits */
5729 c = my_getSmallExpression (&imm_expr, s);
36a87ad7
ILT
5730 if (imm_expr.X_op == O_constant
5731 && (imm_expr.X_add_number < 0
5732 || imm_expr.X_add_number >= 0x10000))
670a50eb
ILT
5733 as_bad ("lui expression not in range 0..65535");
5734 imm_reloc = BFD_RELOC_LO16;
5735 if (c)
5736 {
5737 if (c != 'l')
5738 {
5ac34ac3 5739 if (imm_expr.X_op == O_constant)
670a50eb
ILT
5740 imm_expr.X_add_number =
5741 (imm_expr.X_add_number >> 16) & 0xffff;
5742 else if (c == 'h')
867a58b3
ILT
5743 {
5744 imm_reloc = BFD_RELOC_HI16_S;
5745 imm_unmatched_hi = true;
5746 }
670a50eb
ILT
5747 else
5748 imm_reloc = BFD_RELOC_HI16;
3d3c5039
ILT
5749 }
5750 }
670a50eb
ILT
5751 s = expr_end;
5752 continue;
3d3c5039 5753
670a50eb
ILT
5754 case 'a': /* 26 bit address */
5755 my_getExpression (&offset_expr, s);
5756 s = expr_end;
5757 offset_reloc = BFD_RELOC_MIPS_JMP;
5758 continue;
3d3c5039 5759
d8a1c247
KR
5760 case 'N': /* 3 bit branch condition code */
5761 case 'M': /* 3 bit compare condition code */
5762 my_getExpression (&imm_expr, s);
5763 check_absolute_expr (ip, &imm_expr);
5764 if ((unsigned long) imm_expr.X_add_number > 7)
5765 {
5766 as_warn ("Condition code > 7 (%ld)",
5767 (long) imm_expr.X_add_number);
5768 imm_expr.X_add_number &= 7;
5769 }
5770 if (*args == 'N')
5771 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_BCC;
5772 else
5773 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CCC;
5774 imm_expr.X_op = O_absent;
5775 s = expr_end;
5776 continue;
5777
3d3c5039 5778 default:
670a50eb
ILT
5779 fprintf (stderr, "bad char = '%c'\n", *args);
5780 internalError ();
3d3c5039 5781 }
670a50eb 5782 break;
3d3c5039 5783 }
670a50eb
ILT
5784 /* Args don't match. */
5785 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
5786 !strcmp (insn->name, insn[1].name))
5787 {
5788 ++insn;
5789 s = argsStart;
5790 continue;
3d3c5039 5791 }
9753202d 5792 insn_error = "illegal operands";
670a50eb 5793 return;
3d3c5039
ILT
5794 }
5795}
5796
5797#define LP '('
5798#define RP ')'
5799
5800static int
5801my_getSmallExpression (ep, str)
670a50eb
ILT
5802 expressionS *ep;
5803 char *str;
3d3c5039 5804{
670a50eb
ILT
5805 char *sp;
5806 int c = 0;
5807
5808 if (*str == ' ')
5809 str++;
5810 if (*str == LP
5811 || (*str == '%' &&
5812 ((str[1] == 'h' && str[2] == 'i')
5813 || (str[1] == 'H' && str[2] == 'I')
5814 || (str[1] == 'l' && str[2] == 'o'))
5815 && str[3] == LP))
5816 {
5817 if (*str == LP)
5818 c = 0;
5819 else
5820 {
5821 c = str[1];
5822 str += 3;
5823 }
5824
5825 /*
5826 * A small expression may be followed by a base register.
5827 * Scan to the end of this operand, and then back over a possible
5828 * base register. Then scan the small expression up to that
5829 * point. (Based on code in sparc.c...)
5830 */
5831 for (sp = str; *sp && *sp != ','; sp++)
5832 ;
5833 if (sp - 4 >= str && sp[-1] == RP)
5834 {
5835 if (isdigit (sp[-2]))
5836 {
5837 for (sp -= 3; sp >= str && isdigit (*sp); sp--)
5838 ;
5839 if (*sp == '$' && sp > str && sp[-1] == LP)
5840 {
5841 sp--;
5842 goto do_it;
3d3c5039 5843 }
670a50eb
ILT
5844 }
5845 else if (sp - 5 >= str
5846 && sp[-5] == LP
5847 && sp[-4] == '$'
5848 && ((sp[-3] == 'f' && sp[-2] == 'p')
5849 || (sp[-3] == 's' && sp[-2] == 'p')
5850 || (sp[-3] == 'g' && sp[-2] == 'p')
5851 || (sp[-3] == 'a' && sp[-2] == 't')))
5852 {
5853 sp -= 5;
3d3c5039 5854 do_it:
670a50eb
ILT
5855 if (sp == str)
5856 {
5857 /* no expression means zero offset */
5858 if (c)
5859 {
5860 /* %xx(reg) is an error */
5ac34ac3 5861 ep->X_op = O_absent;
670a50eb 5862 expr_end = str - 3;
3d3c5039 5863 }
670a50eb
ILT
5864 else
5865 {
52aa70b5 5866 ep->X_op = O_constant;
670a50eb
ILT
5867 expr_end = sp;
5868 }
5869 ep->X_add_symbol = NULL;
5ac34ac3 5870 ep->X_op_symbol = NULL;
670a50eb
ILT
5871 ep->X_add_number = 0;
5872 }
5873 else
5874 {
5875 *sp = '\0';
5876 my_getExpression (ep, str);
5877 *sp = LP;
3d3c5039 5878 }
670a50eb 5879 return c;
3d3c5039
ILT
5880 }
5881 }
5882 }
670a50eb
ILT
5883 my_getExpression (ep, str);
5884 return c; /* => %hi or %lo encountered */
3d3c5039
ILT
5885}
5886
5887static void
5888my_getExpression (ep, str)
670a50eb
ILT
5889 expressionS *ep;
5890 char *str;
3d3c5039 5891{
670a50eb 5892 char *save_in;
670a50eb
ILT
5893
5894 save_in = input_line_pointer;
5895 input_line_pointer = str;
5ac34ac3 5896 expression (ep);
670a50eb
ILT
5897 expr_end = input_line_pointer;
5898 input_line_pointer = save_in;
3d3c5039
ILT
5899}
5900
becfe05e
ILT
5901/* Turn a string in input_line_pointer into a floating point constant
5902 of type type, and store the appropriate bytes in *litP. The number
5903 of LITTLENUMS emitted is stored in *sizeP . An error message is
5904 returned, or NULL on OK. */
5905
3d3c5039 5906char *
670a50eb 5907md_atof (type, litP, sizeP)
becfe05e 5908 int type;
3d3c5039
ILT
5909 char *litP;
5910 int *sizeP;
5911{
becfe05e
ILT
5912 int prec;
5913 LITTLENUM_TYPE words[4];
5914 char *t;
5915 int i;
5916
5917 switch (type)
5918 {
5919 case 'f':
5920 prec = 2;
5921 break;
5922
5923 case 'd':
5924 prec = 4;
5925 break;
5926
5927 default:
5928 *sizeP = 0;
5929 return "bad call to md_atof";
5930 }
5931
5932 t = atof_ieee (input_line_pointer, type, words);
5933 if (t)
5934 input_line_pointer = t;
5935
5936 *sizeP = prec * 2;
5937
5938 if (byte_order == LITTLE_ENDIAN)
5939 {
5940 for (i = prec - 1; i >= 0; i--)
5941 {
5942 md_number_to_chars (litP, (valueT) words[i], 2);
5943 litP += 2;
5944 }
5945 }
5946 else
5947 {
5948 for (i = 0; i < prec; i++)
5949 {
5950 md_number_to_chars (litP, (valueT) words[i], 2);
5951 litP += 2;
5952 }
5953 }
5954
670a50eb 5955 return NULL;
3d3c5039
ILT
5956}
5957
5958void
5959md_number_to_chars (buf, val, n)
5960 char *buf;
918692a5 5961 valueT val;
3d3c5039
ILT
5962 int n;
5963{
670a50eb
ILT
5964 switch (byte_order)
5965 {
3d3c5039 5966 case LITTLE_ENDIAN:
13fe1379
ILT
5967 number_to_chars_littleendian (buf, val, n);
5968 break;
3d3c5039
ILT
5969
5970 case BIG_ENDIAN:
13fe1379
ILT
5971 number_to_chars_bigendian (buf, val, n);
5972 break;
3d3c5039
ILT
5973
5974 default:
670a50eb 5975 internalError ();
3d3c5039
ILT
5976 }
5977}
f3d817d8 5978\f
e8d4d475 5979CONST char *md_shortopts = "O::g::G:";
1dc1e798 5980
f3d817d8
DM
5981struct option md_longopts[] = {
5982#define OPTION_MIPS1 (OPTION_MD_BASE + 1)
5983 {"mips0", no_argument, NULL, OPTION_MIPS1},
5984 {"mips1", no_argument, NULL, OPTION_MIPS1},
5985#define OPTION_MIPS2 (OPTION_MD_BASE + 2)
5986 {"mips2", no_argument, NULL, OPTION_MIPS2},
5987#define OPTION_MIPS3 (OPTION_MD_BASE + 3)
5988 {"mips3", no_argument, NULL, OPTION_MIPS3},
d8a1c247
KR
5989#define OPTION_MIPS4 (OPTION_MD_BASE + 4)
5990 {"mips4", no_argument, NULL, OPTION_MIPS4},
5991#define OPTION_MCPU (OPTION_MD_BASE + 5)
f3d817d8 5992 {"mcpu", required_argument, NULL, OPTION_MCPU},
d8a1c247 5993#define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 6)
f3d817d8 5994 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
d8a1c247 5995#define OPTION_TRAP (OPTION_MD_BASE + 9)
f3d817d8
DM
5996 {"trap", no_argument, NULL, OPTION_TRAP},
5997 {"no-break", no_argument, NULL, OPTION_TRAP},
d8a1c247 5998#define OPTION_BREAK (OPTION_MD_BASE + 10)
f3d817d8
DM
5999 {"break", no_argument, NULL, OPTION_BREAK},
6000 {"no-trap", no_argument, NULL, OPTION_BREAK},
d8a1c247 6001#define OPTION_EB (OPTION_MD_BASE + 11)
e8d4d475 6002 {"EB", no_argument, NULL, OPTION_EB},
d8a1c247 6003#define OPTION_EL (OPTION_MD_BASE + 12)
e8d4d475 6004 {"EL", no_argument, NULL, OPTION_EL},
d8a1c247 6005#define OPTION_M4650 (OPTION_MD_BASE + 13)
b2b8c24e 6006 {"m4650", no_argument, NULL, OPTION_M4650},
d8a1c247 6007#define OPTION_NO_M4650 (OPTION_MD_BASE + 14)
b2b8c24e 6008 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
e532b44c
ILT
6009#define OPTION_M4010 (OPTION_MD_BASE + 15)
6010 {"m4010", no_argument, NULL, OPTION_M4010},
6011#define OPTION_NO_M4010 (OPTION_MD_BASE + 16)
6012 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
c625fc23
JSC
6013#define OPTION_M4100 (OPTION_MD_BASE + 17)
6014 {"m4100", no_argument, NULL, OPTION_M4100},
6015#define OPTION_NO_M4100 (OPTION_MD_BASE + 18)
6016 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
f3d817d8 6017
d8a1c247 6018#define OPTION_CALL_SHARED (OPTION_MD_BASE + 7)
1dc1e798 6019#define OPTION_NON_SHARED (OPTION_MD_BASE + 8)
fb251650 6020#define OPTION_XGOT (OPTION_MD_BASE + 19)
7f9880e5
ILT
6021#define OPTION_32 (OPTION_MD_BASE + 20)
6022#define OPTION_64 (OPTION_MD_BASE + 21)
1dc1e798 6023#ifdef OBJ_ELF
f3d817d8 6024 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
fb251650 6025 {"xgot", no_argument, NULL, OPTION_XGOT},
f3d817d8 6026 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
f3d817d8 6027 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
7f9880e5
ILT
6028 {"32", no_argument, NULL, OPTION_32},
6029 {"64", no_argument, NULL, OPTION_64},
f3d817d8
DM
6030#endif
6031
6032 {NULL, no_argument, NULL, 0}
6033};
6034size_t md_longopts_size = sizeof(md_longopts);
3d3c5039
ILT
6035
6036int
f3d817d8
DM
6037md_parse_option (c, arg)
6038 int c;
6039 char *arg;
3d3c5039 6040{
f3d817d8 6041 switch (c)
670a50eb 6042 {
f3d817d8
DM
6043 case OPTION_TRAP:
6044 mips_trap = 1;
6045 break;
670a50eb 6046
f3d817d8
DM
6047 case OPTION_BREAK:
6048 mips_trap = 0;
6049 break;
6050
e8d4d475 6051 case OPTION_EB:
1dc1e798 6052 target_big_endian = 1;
e8d4d475 6053 break;
04cb3372 6054
e8d4d475 6055 case OPTION_EL:
1dc1e798 6056 target_big_endian = 0;
f3d817d8 6057 break;
670a50eb 6058
f3d817d8
DM
6059 case 'O':
6060 if (arg && arg[1] == '0')
0aa07269
ILT
6061 mips_optimize = 1;
6062 else
6063 mips_optimize = 2;
f3d817d8 6064 break;
0aa07269 6065
f3d817d8 6066 case 'g':
22ba90ce
ILT
6067 if (arg == NULL)
6068 mips_debug = 2;
6069 else
6070 mips_debug = atoi (arg);
6071 /* When the MIPS assembler sees -g or -g2, it does not do
6072 optimizations which limit full symbolic debugging. We take
6073 that to be equivalent to -O0. */
6074 if (mips_debug == 2)
0aa07269 6075 mips_optimize = 0;
f3d817d8 6076 break;
4e95866e 6077
f3d817d8
DM
6078 case OPTION_MIPS1:
6079 mips_isa = 1;
4bb0cc41
ILT
6080 if (mips_cpu == -1)
6081 mips_cpu = 3000;
f3d817d8 6082 break;
8358c818 6083
f3d817d8
DM
6084 case OPTION_MIPS2:
6085 mips_isa = 2;
4bb0cc41
ILT
6086 if (mips_cpu == -1)
6087 mips_cpu = 6000;
f3d817d8 6088 break;
8358c818 6089
f3d817d8
DM
6090 case OPTION_MIPS3:
6091 mips_isa = 3;
4bb0cc41
ILT
6092 if (mips_cpu == -1)
6093 mips_cpu = 4000;
f3d817d8 6094 break;
8358c818 6095
d8a1c247
KR
6096 case OPTION_MIPS4:
6097 mips_isa = 4;
6098 if (mips_cpu == -1)
6099 mips_cpu = 8000;
6100 break;
6101
f3d817d8
DM
6102 case OPTION_MCPU:
6103 {
6104 char *p;
6105
6106 /* Identify the processor type */
6107 p = arg;
6108 if (strcmp (p, "default") == 0
6109 || strcmp (p, "DEFAULT") == 0)
4bb0cc41 6110 mips_cpu = -1;
f3d817d8
DM
6111 else
6112 {
c625fc23
JSC
6113 int sv = 0;
6114
6115 /* We need to cope with the various "vr" prefixes for the 4300
6116 processor. */
6117 if (*p == 'v' || *p == 'V')
6118 {
6119 sv = 1;
6120 p++;
6121 }
6122
f3d817d8
DM
6123 if (*p == 'r' || *p == 'R')
6124 p++;
8358c818 6125
4bb0cc41 6126 mips_cpu = -1;
f3d817d8
DM
6127 switch (*p)
6128 {
d8a1c247
KR
6129 case '1':
6130 if (strcmp (p, "10000") == 0
6131 || strcmp (p, "10k") == 0
6132 || strcmp (p, "10K") == 0)
6133 mips_cpu = 10000;
6134 break;
6135
f3d817d8
DM
6136 case '2':
6137 if (strcmp (p, "2000") == 0
6138 || strcmp (p, "2k") == 0
6139 || strcmp (p, "2K") == 0)
4bb0cc41 6140 mips_cpu = 2000;
f3d817d8 6141 break;
8358c818 6142
f3d817d8
DM
6143 case '3':
6144 if (strcmp (p, "3000") == 0
6145 || strcmp (p, "3k") == 0
6146 || strcmp (p, "3K") == 0)
4bb0cc41 6147 mips_cpu = 3000;
f3d817d8 6148 break;
8358c818 6149
f3d817d8
DM
6150 case '4':
6151 if (strcmp (p, "4000") == 0
6152 || strcmp (p, "4k") == 0
8c63448a 6153 || strcmp (p, "4K") == 0)
4bb0cc41 6154 mips_cpu = 4000;
c625fc23
JSC
6155 else if (strcmp (p, "4100") == 0)
6156 {
6157 mips_cpu = 4100;
6158 if (mips_4100 < 0)
6159 mips_4100 = 1;
6160 }
6161 else if (strcmp (p, "4300") == 0)
6162 mips_cpu = 4300;
8c63448a 6163 else if (strcmp (p, "4400") == 0)
4bb0cc41 6164 mips_cpu = 4400;
8c63448a 6165 else if (strcmp (p, "4600") == 0)
4bb0cc41 6166 mips_cpu = 4600;
b2b8c24e
ILT
6167 else if (strcmp (p, "4650") == 0)
6168 {
6169 mips_cpu = 4650;
6170 if (mips_4650 < 0)
6171 mips_4650 = 1;
6172 }
e532b44c
ILT
6173 else if (strcmp (p, "4010") == 0)
6174 {
6175 mips_cpu = 4010;
6176 if (mips_4010 < 0)
6177 mips_4010 = 1;
6178 }
f3d817d8 6179 break;
8358c818 6180
f3d817d8
DM
6181 case '6':
6182 if (strcmp (p, "6000") == 0
6183 || strcmp (p, "6k") == 0
6184 || strcmp (p, "6K") == 0)
4bb0cc41 6185 mips_cpu = 6000;
f3d817d8 6186 break;
55933a58 6187
d8a1c247
KR
6188 case '8':
6189 if (strcmp (p, "8000") == 0
6190 || strcmp (p, "8k") == 0
6191 || strcmp (p, "8K") == 0)
6192 mips_cpu = 8000;
6193 break;
6194
55933a58
ILT
6195 case 'o':
6196 if (strcmp (p, "orion") == 0)
4bb0cc41 6197 mips_cpu = 4600;
55933a58 6198 break;
f3d817d8 6199 }
8358c818 6200
c625fc23
JSC
6201 if (sv && mips_cpu != 4300 && mips_cpu != 4100)
6202 {
6203 as_bad ("ignoring invalid leading 'v' in -mcpu=%s switch", arg);
6204 return 0;
6205 }
6206
4bb0cc41 6207 if (mips_cpu == -1)
f3d817d8
DM
6208 {
6209 as_bad ("invalid architecture -mcpu=%s", arg);
6210 return 0;
6211 }
6212 }
6213 }
6214 break;
8358c818 6215
b2b8c24e
ILT
6216 case OPTION_M4650:
6217 mips_4650 = 1;
6218 break;
6219
6220 case OPTION_NO_M4650:
6221 mips_4650 = 0;
6222 break;
6223
e532b44c
ILT
6224 case OPTION_M4010:
6225 mips_4010 = 1;
6226 break;
6227
6228 case OPTION_NO_M4010:
6229 mips_4010 = 0;
6230 break;
6231
c625fc23
JSC
6232 case OPTION_M4100:
6233 mips_4100 = 1;
6234 break;
6235
6236 case OPTION_NO_M4100:
6237 mips_4100 = 0;
6238 break;
6239
f3d817d8 6240 case OPTION_MEMBEDDED_PIC:
d9aba805 6241 mips_pic = EMBEDDED_PIC;
1dc1e798 6242 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
f3d817d8
DM
6243 {
6244 as_bad ("-G may not be used with embedded PIC code");
6245 return 0;
6246 }
5b63f465 6247 g_switch_value = 0x7fffffff;
f3d817d8 6248 break;
d9aba805 6249
fb251650
ILT
6250 /* When generating ELF code, we permit -KPIC and -call_shared to
6251 select SVR4_PIC, and -non_shared to select no PIC. This is
6252 intended to be compatible with Irix 5. */
f3d817d8 6253 case OPTION_CALL_SHARED:
1dc1e798
KR
6254 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
6255 {
6256 as_bad ("-call_shared is supported only for ELF format");
6257 return 0;
6258 }
d9aba805
ILT
6259 mips_pic = SVR4_PIC;
6260 if (g_switch_seen && g_switch_value != 0)
f3d817d8
DM
6261 {
6262 as_bad ("-G may not be used with SVR4 PIC code");
6263 return 0;
6264 }
d9aba805 6265 g_switch_value = 0;
f3d817d8
DM
6266 break;
6267
6268 case OPTION_NON_SHARED:
1dc1e798
KR
6269 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
6270 {
6271 as_bad ("-non_shared is supported only for ELF format");
6272 return 0;
6273 }
d9aba805 6274 mips_pic = NO_PIC;
f3d817d8 6275 break;
8358c818 6276
fb251650
ILT
6277 /* The -xgot option tells the assembler to use 32 offsets when
6278 accessing the got in SVR4_PIC mode. It is for Irix
6279 compatibility. */
6280 case OPTION_XGOT:
6281 mips_big_got = 1;
6282 break;
6283
f3d817d8 6284 case 'G':
1dc1e798
KR
6285 if (! USE_GLOBAL_POINTER_OPT)
6286 {
6287 as_bad ("-G is not supported for this configuration");
6288 return 0;
6289 }
6290 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
670a50eb 6291 {
f3d817d8
DM
6292 as_bad ("-G may not be used with SVR4 or embedded PIC code");
6293 return 0;
670a50eb
ILT
6294 }
6295 else
f3d817d8 6296 g_switch_value = atoi (arg);
42562568 6297 g_switch_seen = 1;
f3d817d8 6298 break;
4e95866e 6299
7f9880e5
ILT
6300 /* The -32 and -64 options tell the assembler to output the 32
6301 bit or the 64 bit MIPS ELF format. */
6302 case OPTION_32:
6303 mips_64 = 0;
6304 break;
6305
6306 case OPTION_64:
9c44af60
ILT
6307 {
6308 const char **list, **l;
6309
6310 list = bfd_target_list ();
6311 for (l = list; *l != NULL; l++)
6312 if (strcmp (*l, "elf64-bigmips") == 0
6313 || strcmp (*l, "elf64-littlemips") == 0)
6314 break;
6315 if (*l == NULL)
6316 as_fatal ("No compiled in support for 64 bit object file format");
6317 free (list);
6318 mips_64 = 1;
6319 }
7f9880e5
ILT
6320 break;
6321
f3d817d8
DM
6322 default:
6323 return 0;
8ea7f4e8
ILT
6324 }
6325
f3d817d8 6326 return 1;
8ea7f4e8
ILT
6327}
6328
f3d817d8
DM
6329void
6330md_show_usage (stream)
6331 FILE *stream;
6332{
6333 fprintf(stream, "\
6334MIPS options:\n\
6335-membedded-pic generate embedded position independent code\n\
f3d817d8
DM
6336-EB generate big endian output\n\
6337-EL generate little endian output\n\
6338-g, -g2 do not remove uneeded NOPs or swap branches\n\
6339-G NUM allow referencing objects up to NUM bytes\n\
6f0b87c3
SS
6340 implicitly with the gp register [default 8]\n");
6341 fprintf(stream, "\
f3d817d8
DM
6342-mips1, -mcpu=r{2,3}000 generate code for r2000 and r3000\n\
6343-mips2, -mcpu=r6000 generate code for r6000\n\
6344-mips3, -mcpu=r4000 generate code for r4000\n\
d8a1c247 6345-mips4, -mcpu=r8000 generate code for r8000\n\
c625fc23 6346-mcpu=vr4300 generate code for vr4300\n\
fb251650 6347-mcpu=vr4100 generate code for vr4100\n\
e532b44c
ILT
6348-m4650 permit R4650 instructions\n\
6349-no-m4650 do not permit R4650 instructions\n\
6350-m4010 permit R4010 instructions\n\
6351-no-m4010 do not permit R4010 instructions\n\
c625fc23
JSC
6352-m4100 permit VR4100 instructions\n\
6353-no-m4100 do not permit VR4100 instructions\n");
6354 fprintf(stream, "\
f3d817d8
DM
6355-O0 remove unneeded NOPs, do not swap branches\n\
6356-O remove unneeded NOPs and swap branches\n\
6357--trap, --no-break trap exception on div by 0 and mult overflow\n\
6358--break, --no-trap break exception on div by 0 and mult overflow\n");
6359#ifdef OBJ_ELF
6360 fprintf(stream, "\
6361-KPIC, -call_shared generate SVR4 position independent code\n\
fb251650 6362-non_shared do not generate position independent code\n\
7f9880e5
ILT
6363-xgot assume a 32 bit GOT\n\
6364-32 create 32 bit object file (default)\n\
6365-64 create 64 bit object file\n");
f3d817d8
DM
6366#endif
6367}
22ba90ce
ILT
6368
6369void
6370mips_init_after_args ()
6371{
6372 if (target_big_endian)
6373 byte_order = BIG_ENDIAN;
6374 else
6375 byte_order = LITTLE_ENDIAN;
6376}
f3d817d8 6377\f
3d3c5039
ILT
6378long
6379md_pcrel_from (fixP)
6380 fixS *fixP;
6381{
1dc1e798
KR
6382 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
6383 && fixP->fx_addsy != (symbolS *) NULL
5b63f465
ILT
6384 && ! S_IS_DEFINED (fixP->fx_addsy))
6385 {
6386 /* This makes a branch to an undefined symbol be a branch to the
6387 current location. */
6388 return 4;
6389 }
5b63f465 6390
670a50eb
ILT
6391 /* return the address of the delay slot */
6392 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
3d3c5039
ILT
6393}
6394
abdad6bc
ILT
6395/* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
6396 reloc for a cons. We could use the definition there, except that
6397 we want to handle 64 bit relocs specially. */
6398
6399void
6400cons_fix_new_mips (frag, where, nbytes, exp)
6401 fragS *frag;
6402 int where;
6403 unsigned int nbytes;
6404 expressionS *exp;
6405{
6406 /* If we are assembling in 32 bit mode, turn an 8 byte reloc into a
7f9880e5
ILT
6407 4 byte reloc. */
6408 if (nbytes == 8 && ! mips_64)
abdad6bc
ILT
6409 {
6410 if (byte_order == BIG_ENDIAN)
6411 where += 4;
6412 nbytes = 4;
6413 }
6414
7f9880e5 6415 if (nbytes != 2 && nbytes != 4 && nbytes != 8)
abdad6bc
ILT
6416 as_bad ("Unsupported reloc size %d", nbytes);
6417
6418 fix_new_exp (frag_now, where, (int) nbytes, exp, 0,
7f9880e5
ILT
6419 (nbytes == 2
6420 ? BFD_RELOC_16
6421 : (nbytes == 4 ? BFD_RELOC_32 : BFD_RELOC_64)));
abdad6bc
ILT
6422}
6423
867a58b3
ILT
6424/* Sort any unmatched HI16_S relocs so that they immediately precede
6425 the corresponding LO reloc. This is called before md_apply_fix and
6426 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
6427 explicit use of the %hi modifier. */
6428
6429void
6430mips_frob_file ()
6431{
6432 struct mips_hi_fixup *l;
6433
6434 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
6435 {
6436 segment_info_type *seginfo;
9b61d62b 6437 int pass;
867a58b3
ILT
6438
6439 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
6440
6441 /* Check quickly whether the next fixup happens to be a matching
6442 %lo. */
6443 if (l->fixp->fx_next != NULL
6444 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
6445 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
6446 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
6447 continue;
6448
6449 /* Look through the fixups for this segment for a matching %lo.
9b61d62b
ILT
6450 When we find one, move the %hi just in front of it. We do
6451 this in two passes. In the first pass, we try to find a
6452 unique %lo. In the second pass, we permit multiple %hi
6453 relocs for a single %lo (this is a GNU extension). */
867a58b3 6454 seginfo = seg_info (l->seg);
9b61d62b
ILT
6455 for (pass = 0; pass < 2; pass++)
6456 {
6457 fixS *f, *prev;
6458
6459 prev = NULL;
6460 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
867a58b3 6461 {
9b61d62b
ILT
6462 /* Check whether this is a %lo fixup which matches l->fixp. */
6463 if (f->fx_r_type == BFD_RELOC_LO16
6464 && f->fx_addsy == l->fixp->fx_addsy
6465 && f->fx_offset == l->fixp->fx_offset
6466 && (pass == 1
6467 || prev == NULL
6468 || prev->fx_r_type != BFD_RELOC_HI16_S
6469 || prev->fx_addsy != f->fx_addsy
6470 || prev->fx_offset != f->fx_offset))
6471 {
6472 fixS **pf;
867a58b3 6473
9b61d62b
ILT
6474 /* Move l->fixp before f. */
6475 for (pf = &seginfo->fix_root;
6476 *pf != l->fixp;
6477 pf = &(*pf)->fx_next)
6478 assert (*pf != NULL);
867a58b3 6479
9b61d62b 6480 *pf = l->fixp->fx_next;
867a58b3 6481
9b61d62b
ILT
6482 l->fixp->fx_next = f;
6483 if (prev == NULL)
6484 seginfo->fix_root = l->fixp;
6485 else
6486 prev->fx_next = l->fixp;
867a58b3 6487
9b61d62b
ILT
6488 break;
6489 }
6490
6491 prev = f;
867a58b3
ILT
6492 }
6493
9b61d62b
ILT
6494 if (f != NULL)
6495 break;
867a58b3 6496
9b61d62b
ILT
6497 if (pass == 1)
6498 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
6499 "Unmatched %%hi reloc");
6500 }
867a58b3
ILT
6501 }
6502}
6503
1c803e52
ILT
6504/* When generating embedded PIC code we need to use a special
6505 relocation to represent the difference of two symbols in the .text
6506 section (switch tables use a difference of this sort). See
6507 include/coff/mips.h for details. This macro checks whether this
6508 fixup requires the special reloc. */
6509#define SWITCH_TABLE(fixp) \
6510 ((fixp)->fx_r_type == BFD_RELOC_32 \
6511 && (fixp)->fx_addsy != NULL \
6512 && (fixp)->fx_subsy != NULL \
6513 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
6514 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
6515
5b63f465 6516/* When generating embedded PIC code we must keep all PC relative
1c803e52
ILT
6517 relocations, in case the linker has to relax a call. We also need
6518 to keep relocations for switch table entries. */
5b63f465
ILT
6519
6520/*ARGSUSED*/
6521int
6522mips_force_relocation (fixp)
6523 fixS *fixp;
6524{
1c803e52 6525 return (mips_pic == EMBEDDED_PIC
ecd4ca1c
ILT
6526 && (fixp->fx_pcrel
6527 || SWITCH_TABLE (fixp)
6528 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
6529 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
5b63f465
ILT
6530}
6531
6532/* Apply a fixup to the object file. */
6533
3d3c5039
ILT
6534int
6535md_apply_fix (fixP, valueP)
6536 fixS *fixP;
918692a5 6537 valueT *valueP;
3d3c5039 6538{
670a50eb
ILT
6539 unsigned char *buf;
6540 long insn, value;
3d3c5039 6541
49ad0c4c 6542 assert (fixP->fx_size == 4 || fixP->fx_r_type == BFD_RELOC_16);
3d3c5039 6543
670a50eb
ILT
6544 value = *valueP;
6545 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc */
3d3c5039 6546
5b63f465
ILT
6547 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
6548 fixP->fx_done = 1;
6549
670a50eb
ILT
6550 switch (fixP->fx_r_type)
6551 {
3d3c5039
ILT
6552 case BFD_RELOC_MIPS_JMP:
6553 case BFD_RELOC_HI16:
6554 case BFD_RELOC_HI16_S:
670a50eb 6555 case BFD_RELOC_MIPS_GPREL:
9226253a
ILT
6556 case BFD_RELOC_MIPS_LITERAL:
6557 case BFD_RELOC_MIPS_CALL16:
0dd2d296
ILT
6558 case BFD_RELOC_MIPS_GOT16:
6559 case BFD_RELOC_MIPS_GPREL32:
fb251650
ILT
6560 case BFD_RELOC_MIPS_GOT_HI16:
6561 case BFD_RELOC_MIPS_GOT_LO16:
6562 case BFD_RELOC_MIPS_CALL_HI16:
6563 case BFD_RELOC_MIPS_CALL_LO16:
ecd4ca1c 6564 if (fixP->fx_pcrel)
7b777690
ILT
6565 as_bad_where (fixP->fx_file, fixP->fx_line,
6566 "Invalid PC relative reloc");
670a50eb 6567 /* Nothing needed to do. The value comes from the reloc entry */
5b63f465 6568 break;
3d3c5039 6569
ecd4ca1c
ILT
6570 case BFD_RELOC_PCREL_HI16_S:
6571 /* The addend for this is tricky if it is internal, so we just
6572 do everything here rather than in bfd_perform_relocation. */
6573 if ((fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) == 0)
6574 {
6575 /* For an external symbol adjust by the address to make it
6576 pcrel_offset. We use the address of the RELLO reloc
6577 which follows this one. */
6578 value += (fixP->fx_next->fx_frag->fr_address
6579 + fixP->fx_next->fx_where);
6580 }
6581 if (value & 0x8000)
6582 value += 0x10000;
6583 value >>= 16;
0221ddf7 6584 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
ecd4ca1c
ILT
6585 if (byte_order == BIG_ENDIAN)
6586 buf += 2;
6587 md_number_to_chars (buf, value, 2);
6588 break;
6589
6590 case BFD_RELOC_PCREL_LO16:
6591 /* The addend for this is tricky if it is internal, so we just
6592 do everything here rather than in bfd_perform_relocation. */
6593 if ((fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) == 0)
6594 value += fixP->fx_frag->fr_address + fixP->fx_where;
0221ddf7 6595 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
ecd4ca1c
ILT
6596 if (byte_order == BIG_ENDIAN)
6597 buf += 2;
6598 md_number_to_chars (buf, value, 2);
6599 break;
6600
f3645945
ILT
6601 case BFD_RELOC_32:
6602 /* If we are deleting this reloc entry, we must fill in the
6603 value now. This can happen if we have a .word which is not
1c803e52
ILT
6604 resolved when it appears but is later defined. We also need
6605 to fill in the value if this is an embedded PIC switch table
6606 entry. */
6607 if (fixP->fx_done
6608 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
f3645945
ILT
6609 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
6610 value, 4);
6611 break;
6612
49ad0c4c
ILT
6613 case BFD_RELOC_16:
6614 /* If we are deleting this reloc entry, we must fill in the
6615 value now. */
6616 assert (fixP->fx_size == 2);
6617 if (fixP->fx_done)
6618 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
6619 value, 2);
6620 break;
6621
f3645945
ILT
6622 case BFD_RELOC_LO16:
6623 /* When handling an embedded PIC switch statement, we can wind
6624 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
6625 if (fixP->fx_done)
6626 {
6627 if (value < -0x8000 || value > 0x7fff)
6628 as_bad_where (fixP->fx_file, fixP->fx_line,
6629 "relocation overflow");
0221ddf7 6630 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
f3645945
ILT
6631 if (byte_order == BIG_ENDIAN)
6632 buf += 2;
6633 md_number_to_chars (buf, value, 2);
6634 }
6635 break;
6636
3d3c5039 6637 case BFD_RELOC_16_PCREL_S2:
670a50eb
ILT
6638 /*
6639 * We need to save the bits in the instruction since fixup_segment()
6640 * might be deleting the relocation entry (i.e., a branch within
6641 * the current segment).
6642 */
6643 if (value & 0x3)
7b777690
ILT
6644 as_warn_where (fixP->fx_file, fixP->fx_line,
6645 "Branch to odd address (%lx)", value);
670a50eb 6646 value >>= 2;
670a50eb
ILT
6647
6648 /* update old instruction data */
6649 buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
6650 switch (byte_order)
6651 {
3d3c5039 6652 case LITTLE_ENDIAN:
670a50eb
ILT
6653 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
6654 break;
3d3c5039
ILT
6655
6656 case BIG_ENDIAN:
670a50eb
ILT
6657 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
6658 break;
3d3c5039
ILT
6659
6660 default:
670a50eb
ILT
6661 internalError ();
6662 return 0;
3d3c5039 6663 }
9da4c5d1
ILT
6664
6665 if (value >= -0x8000 && value < 0x8000)
6666 insn |= value & 0xffff;
6667 else
6668 {
6669 /* The branch offset is too large. If this is an
6670 unconditional branch, and we are not generating PIC code,
6671 we can convert it to an absolute jump instruction. */
6672 if (mips_pic == NO_PIC
6673 && fixP->fx_done
6674 && fixP->fx_frag->fr_address >= text_section->vma
6675 && (fixP->fx_frag->fr_address
6676 < text_section->vma + text_section->_raw_size)
6677 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6678 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
6679 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6680 {
6681 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
6682 insn = 0x0c000000; /* jal */
6683 else
6684 insn = 0x08000000; /* j */
6685 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
6686 fixP->fx_done = 0;
6687 fixP->fx_addsy = section_symbol (text_section);
6688 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
6689 }
6690 else
6691 {
6692 /* FIXME. It would be possible in principle to handle
6693 conditional branches which overflow. They could be
6694 transformed into a branch around a jump. This would
6695 require setting up variant frags for each different
6696 branch type. The native MIPS assembler attempts to
6697 handle these cases, but it appears to do it
6698 incorrectly. */
6699 as_bad_where (fixP->fx_file, fixP->fx_line,
6700 "Relocation overflow");
6701 }
6702 }
6703
604633ae 6704 md_number_to_chars ((char *) buf, (valueT) insn, 4);
670a50eb 6705 break;
3d3c5039
ILT
6706
6707 default:
670a50eb 6708 internalError ();
3d3c5039 6709 }
5b63f465 6710
670a50eb 6711 return 1;
3d3c5039
ILT
6712}
6713
6714#if 0
6715void
670a50eb
ILT
6716printInsn (oc)
6717 unsigned long oc;
3d3c5039 6718{
670a50eb
ILT
6719 const struct mips_opcode *p;
6720 int treg, sreg, dreg, shamt;
6721 short imm;
6722 const char *args;
6723 int i;
3d3c5039 6724
670a50eb
ILT
6725 for (i = 0; i < NUMOPCODES; ++i)
6726 {
6727 p = &mips_opcodes[i];
6728 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
6729 {
6730 printf ("%08lx %s\t", oc, p->name);
6731 treg = (oc >> 16) & 0x1f;
6732 sreg = (oc >> 21) & 0x1f;
6733 dreg = (oc >> 11) & 0x1f;
6734 shamt = (oc >> 6) & 0x1f;
6735 imm = oc;
6736 for (args = p->args;; ++args)
6737 {
6738 switch (*args)
6739 {
3d3c5039 6740 case '\0':
670a50eb
ILT
6741 printf ("\n");
6742 break;
3d3c5039
ILT
6743
6744 case ',':
6745 case '(':
6746 case ')':
670a50eb
ILT
6747 printf ("%c", *args);
6748 continue;
3d3c5039
ILT
6749
6750 case 'r':
670a50eb
ILT
6751 assert (treg == sreg);
6752 printf ("$%d,$%d", treg, sreg);
6753 continue;
3d3c5039
ILT
6754
6755 case 'd':
918692a5 6756 case 'G':
670a50eb
ILT
6757 printf ("$%d", dreg);
6758 continue;
3d3c5039
ILT
6759
6760 case 't':
918692a5 6761 case 'E':
670a50eb
ILT
6762 printf ("$%d", treg);
6763 continue;
3d3c5039 6764
9226253a
ILT
6765 case 'k':
6766 printf ("0x%x", treg);
6767 continue;
6768
3d3c5039
ILT
6769 case 'b':
6770 case 's':
670a50eb
ILT
6771 printf ("$%d", sreg);
6772 continue;
3d3c5039
ILT
6773
6774 case 'a':
670a50eb
ILT
6775 printf ("0x%08lx", oc & 0x1ffffff);
6776 continue;
3d3c5039
ILT
6777
6778 case 'i':
6779 case 'j':
6780 case 'o':
6781 case 'u':
670a50eb
ILT
6782 printf ("%d", imm);
6783 continue;
3d3c5039
ILT
6784
6785 case '<':
56c96faa 6786 case '>':
670a50eb
ILT
6787 printf ("$%d", shamt);
6788 continue;
3d3c5039
ILT
6789
6790 default:
670a50eb 6791 internalError ();
3d3c5039 6792 }
670a50eb 6793 break;
3d3c5039 6794 }
670a50eb 6795 return;
3d3c5039
ILT
6796 }
6797 }
670a50eb 6798 printf ("%08lx UNDEFINED\n", oc);
3d3c5039
ILT
6799}
6800#endif
6801
6802static symbolS *
6803get_symbol ()
6804{
670a50eb
ILT
6805 int c;
6806 char *name;
6807 symbolS *p;
6808
6809 name = input_line_pointer;
6810 c = get_symbol_end ();
6811 p = (symbolS *) symbol_find_or_make (name);
6812 *input_line_pointer = c;
6813 return p;
3d3c5039
ILT
6814}
6815
becfe05e
ILT
6816/* Align the current frag to a given power of two. The MIPS assembler
6817 also automatically adjusts any preceding label. */
6818
6819static void
23dc1ae3 6820mips_align (to, fill, label)
becfe05e
ILT
6821 int to;
6822 int fill;
23dc1ae3 6823 symbolS *label;
becfe05e
ILT
6824{
6825 mips_emit_delays ();
6826 frag_align (to, fill);
6827 record_alignment (now_seg, to);
23dc1ae3 6828 if (label != NULL)
becfe05e 6829 {
23dc1ae3
ILT
6830 assert (S_GET_SEGMENT (label) == now_seg);
6831 label->sy_frag = frag_now;
6832 S_SET_VALUE (label, (valueT) frag_now_fix ());
becfe05e
ILT
6833 }
6834}
6835
6836/* Align to a given power of two. .align 0 turns off the automatic
6837 alignment used by the data creating pseudo-ops. */
6838
3d3c5039
ILT
6839static void
6840s_align (x)
6841 int x;
6842{
670a50eb
ILT
6843 register int temp;
6844 register long temp_fill;
6845 long max_alignment = 15;
3d3c5039 6846
670a50eb 6847 /*
3d3c5039
ILT
6848
6849 o Note that the assembler pulls down any immediately preceeding label
6850 to the aligned address.
6851 o It's not documented but auto alignment is reinstated by
6852 a .align pseudo instruction.
6853 o Note also that after auto alignment is turned off the mips assembler
6854 issues an error on attempt to assemble an improperly aligned data item.
6855 We don't.
6856
6857 */
6858
670a50eb
ILT
6859 temp = get_absolute_expression ();
6860 if (temp > max_alignment)
6861 as_bad ("Alignment too large: %d. assumed.", temp = max_alignment);
6862 else if (temp < 0)
6863 {
6864 as_warn ("Alignment negative: 0 assumed.");
6865 temp = 0;
6866 }
6867 if (*input_line_pointer == ',')
6868 {
6869 input_line_pointer++;
6870 temp_fill = get_absolute_expression ();
6871 }
6872 else
6873 temp_fill = 0;
6874 if (temp)
6875 {
6876 auto_align = 1;
23dc1ae3 6877 mips_align (temp, (int) temp_fill, insn_label);
3d3c5039 6878 }
670a50eb
ILT
6879 else
6880 {
6881 auto_align = 0;
3d3c5039
ILT
6882 }
6883
670a50eb 6884 demand_empty_rest_of_line ();
3d3c5039
ILT
6885}
6886
739708fa
KR
6887void
6888mips_flush_pending_output ()
becfe05e
ILT
6889{
6890 mips_emit_delays ();
1849d646 6891 insn_label = NULL;
becfe05e
ILT
6892}
6893
3d3c5039
ILT
6894static void
6895s_change_sec (sec)
6896 int sec;
6897{
88225433 6898 segT seg;
becfe05e 6899
5b63f465
ILT
6900 /* When generating embedded PIC code, we only use the .text, .lit8,
6901 .sdata and .sbss sections. We change the .data and .rdata
6902 pseudo-ops to use .sdata. */
6903 if (mips_pic == EMBEDDED_PIC
6904 && (sec == 'd' || sec == 'r'))
6905 sec = 's';
6906
becfe05e 6907 mips_emit_delays ();
670a50eb
ILT
6908 switch (sec)
6909 {
3d3c5039 6910 case 't':
604633ae 6911 s_text (0);
670a50eb 6912 break;
3d3c5039 6913 case 'd':
604633ae 6914 s_data (0);
670a50eb 6915 break;
3d3c5039 6916 case 'b':
670a50eb 6917 subseg_set (bss_section, (subsegT) get_absolute_expression ());
670a50eb
ILT
6918 demand_empty_rest_of_line ();
6919 break;
88225433
ILT
6920
6921 case 'r':
1dc1e798
KR
6922 if (USE_GLOBAL_POINTER_OPT)
6923 {
6924 seg = subseg_new (RDATA_SECTION_NAME,
6925 (subsegT) get_absolute_expression ());
6926 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
6927 {
6928 bfd_set_section_flags (stdoutput, seg,
6929 (SEC_ALLOC
6930 | SEC_LOAD
6931 | SEC_READONLY
6932 | SEC_RELOC
6933 | SEC_DATA));
6934 bfd_set_section_alignment (stdoutput, seg, 4);
6935 }
6936 demand_empty_rest_of_line ();
6937 }
6938 else
6939 {
6940 as_bad ("No read only data section in this object file format");
6941 demand_empty_rest_of_line ();
6942 return;
6943 }
88225433 6944 break;
88225433
ILT
6945
6946 case 's':
1dc1e798
KR
6947 if (USE_GLOBAL_POINTER_OPT)
6948 {
6949 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
6950 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
6951 {
6952 bfd_set_section_flags (stdoutput, seg,
6953 SEC_ALLOC | SEC_LOAD | SEC_RELOC
6954 | SEC_DATA);
6955 bfd_set_section_alignment (stdoutput, seg, 4);
6956 }
6957 demand_empty_rest_of_line ();
6958 break;
6959 }
6960 else
6961 {
6962 as_bad ("Global pointers not supported; recompile -G 0");
6963 demand_empty_rest_of_line ();
6964 return;
6965 }
3d3c5039 6966 }
88225433 6967
670a50eb 6968 auto_align = 1;
3d3c5039
ILT
6969}
6970
739708fa
KR
6971void
6972mips_enable_auto_align ()
9da4c5d1 6973{
9da4c5d1
ILT
6974 auto_align = 1;
6975}
6976
3d3c5039
ILT
6977static void
6978s_cons (log_size)
6979 int log_size;
6980{
23dc1ae3
ILT
6981 symbolS *label;
6982
6983 label = insn_label;
becfe05e 6984 mips_emit_delays ();
670a50eb 6985 if (log_size > 0 && auto_align)
23dc1ae3 6986 mips_align (log_size, 0, label);
1849d646 6987 insn_label = NULL;
670a50eb 6988 cons (1 << log_size);
3d3c5039
ILT
6989}
6990
3d3c5039 6991static void
becfe05e
ILT
6992s_float_cons (type)
6993 int type;
3d3c5039 6994{
23dc1ae3
ILT
6995 symbolS *label;
6996
6997 label = insn_label;
6998
becfe05e 6999 mips_emit_delays ();
670a50eb
ILT
7000
7001 if (auto_align)
becfe05e 7002 if (type == 'd')
23dc1ae3 7003 mips_align (3, 0, label);
670a50eb 7004 else
23dc1ae3 7005 mips_align (2, 0, label);
670a50eb 7006
1849d646
ILT
7007 insn_label = NULL;
7008
becfe05e 7009 float_cons (type);
3d3c5039
ILT
7010}
7011
c1444ec4
ILT
7012/* Handle .globl. We need to override it because on Irix 5 you are
7013 permitted to say
7014 .globl foo .text
7015 where foo is an undefined symbol, to mean that foo should be
7016 considered to be the address of a function. */
7017
7018static void
7019s_mips_globl (x)
7020 int x;
7021{
7022 char *name;
7023 int c;
7024 symbolS *symbolP;
fb251650 7025 flagword flag;
c1444ec4
ILT
7026
7027 name = input_line_pointer;
7028 c = get_symbol_end ();
7029 symbolP = symbol_find_or_make (name);
7030 *input_line_pointer = c;
7031 SKIP_WHITESPACE ();
fb251650
ILT
7032
7033 /* On Irix 5, every global symbol that is not explicitly labelled as
7034 being a function is apparently labelled as being an object. */
7035 flag = BSF_OBJECT;
7036
c1444ec4
ILT
7037 if (! is_end_of_line[(unsigned char) *input_line_pointer])
7038 {
7039 char *secname;
7040 asection *sec;
7041
7042 secname = input_line_pointer;
7043 c = get_symbol_end ();
7044 sec = bfd_get_section_by_name (stdoutput, secname);
7045 if (sec == NULL)
7046 as_bad ("%s: no such section", secname);
7047 *input_line_pointer = c;
7048
7049 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
fb251650 7050 flag = BSF_FUNCTION;
c1444ec4
ILT
7051 }
7052
fb251650
ILT
7053 symbolP->bsym->flags |= flag;
7054
c1444ec4
ILT
7055 S_SET_EXTERNAL (symbolP);
7056 demand_empty_rest_of_line ();
7057}
7058
3d3c5039
ILT
7059static void
7060s_option (x)
7061 int x;
7062{
dd3f1f76
ILT
7063 char *opt;
7064 char c;
7065
7066 opt = input_line_pointer;
7067 c = get_symbol_end ();
7068
dd3f1f76 7069 if (*opt == 'O')
9226253a
ILT
7070 {
7071 /* FIXME: What does this mean? */
7072 }
dd3f1f76 7073 else if (strncmp (opt, "pic", 3) == 0)
9226253a 7074 {
d9aba805 7075 int i;
42562568 7076
d9aba805
ILT
7077 i = atoi (opt + 3);
7078 if (i == 0)
7079 mips_pic = NO_PIC;
7080 else if (i == 2)
7081 mips_pic = SVR4_PIC;
7082 else
7083 as_bad (".option pic%d not supported", i);
7084
1dc1e798 7085 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
42562568
ILT
7086 {
7087 if (g_switch_seen && g_switch_value != 0)
d9aba805 7088 as_warn ("-G may not be used with SVR4 PIC code");
42562568
ILT
7089 g_switch_value = 0;
7090 bfd_set_gp_size (stdoutput, 0);
7091 }
9226253a 7092 }
dd3f1f76
ILT
7093 else
7094 as_warn ("Unrecognized option \"%s\"", opt);
7095
7096 *input_line_pointer = c;
670a50eb 7097 demand_empty_rest_of_line ();
3d3c5039
ILT
7098}
7099
7100static void
7101s_mipsset (x)
7102 int x;
7103{
670a50eb
ILT
7104 char *name = input_line_pointer, ch;
7105
7106 while (!is_end_of_line[(unsigned char) *input_line_pointer])
7107 input_line_pointer++;
7108 ch = *input_line_pointer;
7109 *input_line_pointer = '\0';
7110
7111 if (strcmp (name, "reorder") == 0)
7112 {
4e95866e
ILT
7113 if (mips_noreorder)
7114 {
7115 prev_insn_unreordered = 1;
7116 prev_prev_insn_unreordered = 1;
7117 }
670a50eb
ILT
7118 mips_noreorder = 0;
7119 }
7120 else if (strcmp (name, "noreorder") == 0)
7121 {
becfe05e 7122 mips_emit_delays ();
670a50eb 7123 mips_noreorder = 1;
0dd2d296 7124 mips_any_noreorder = 1;
670a50eb
ILT
7125 }
7126 else if (strcmp (name, "at") == 0)
7127 {
7128 mips_noat = 0;
7129 }
7130 else if (strcmp (name, "noat") == 0)
7131 {
7132 mips_noat = 1;
3d3c5039 7133 }
670a50eb
ILT
7134 else if (strcmp (name, "macro") == 0)
7135 {
7136 mips_warn_about_macros = 0;
7137 }
7138 else if (strcmp (name, "nomacro") == 0)
7139 {
7140 if (mips_noreorder == 0)
7141 as_bad ("`noreorder' must be set before `nomacro'");
7142 mips_warn_about_macros = 1;
7143 }
7144 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
7145 {
7146 mips_nomove = 0;
7147 }
7148 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
7149 {
7150 mips_nomove = 1;
7151 }
7152 else if (strcmp (name, "bopt") == 0)
7153 {
7154 mips_nobopt = 0;
7155 }
7156 else if (strcmp (name, "nobopt") == 0)
7157 {
7158 mips_nobopt = 1;
7159 }
1051c97f
ILT
7160 else if (strncmp (name, "mips", 4) == 0)
7161 {
7162 int isa;
7163
7164 /* Permit the user to change the ISA on the fly. Needless to
7165 say, misuse can cause serious problems. */
7166 isa = atoi (name + 4);
7167 if (isa == 0)
7168 mips_isa = file_mips_isa;
d8a1c247 7169 else if (isa < 1 || isa > 4)
1051c97f
ILT
7170 as_bad ("unknown ISA level");
7171 else
7172 mips_isa = isa;
7173 }
670a50eb
ILT
7174 else
7175 {
7176 as_warn ("Tried to set unrecognized symbol: %s\n", name);
7177 }
7178 *input_line_pointer = ch;
7179 demand_empty_rest_of_line ();
3d3c5039
ILT
7180}
7181
9226253a
ILT
7182/* Handle the .abicalls pseudo-op. I believe this is equivalent to
7183 .option pic2. It means to generate SVR4 PIC calls. */
7184
7185static void
7186s_abicalls (ignore)
7187 int ignore;
7188{
d9aba805 7189 mips_pic = SVR4_PIC;
1dc1e798
KR
7190 if (USE_GLOBAL_POINTER_OPT)
7191 {
7192 if (g_switch_seen && g_switch_value != 0)
7193 as_warn ("-G may not be used with SVR4 PIC code");
7194 g_switch_value = 0;
7195 }
d9aba805 7196 bfd_set_gp_size (stdoutput, 0);
9226253a
ILT
7197 demand_empty_rest_of_line ();
7198}
7199
7200/* Handle the .cpload pseudo-op. This is used when generating SVR4
7201 PIC code. It sets the $gp register for the function based on the
7202 function address, which is in the register named in the argument.
7203 This uses a relocation against _gp_disp, which is handled specially
7204 by the linker. The result is:
7205 lui $gp,%hi(_gp_disp)
7206 addiu $gp,$gp,%lo(_gp_disp)
7207 addu $gp,$gp,.cpload argument
0dd2d296 7208 The .cpload argument is normally $25 == $t9. */
9226253a
ILT
7209
7210static void
7211s_cpload (ignore)
7212 int ignore;
7213{
7214 expressionS ex;
7215 int icnt = 0;
7216
d9aba805
ILT
7217 /* If we are not generating SVR4 PIC code, .cpload is ignored. */
7218 if (mips_pic != SVR4_PIC)
0dd2d296
ILT
7219 {
7220 s_ignore (0);
7221 return;
7222 }
7223
7224 /* .cpload should be a in .set noreorder section. */
7225 if (mips_noreorder == 0)
7226 as_warn (".cpload not in noreorder section");
7227
9226253a
ILT
7228 ex.X_op = O_symbol;
7229 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
7230 ex.X_op_symbol = NULL;
7231 ex.X_add_number = 0;
7232
fb251650
ILT
7233 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
7234 ex.X_add_symbol->bsym->flags |= BSF_OBJECT;
7235
0dd2d296
ILT
7236 macro_build_lui ((char *) NULL, &icnt, &ex, GP);
7237 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
9226253a
ILT
7238 (int) BFD_RELOC_LO16);
7239
0dd2d296
ILT
7240 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
7241 GP, GP, tc_get_register (0));
9226253a
ILT
7242
7243 demand_empty_rest_of_line ();
7244}
7245
7246/* Handle the .cprestore pseudo-op. This stores $gp into a given
7247 offset from $sp. The offset is remembered, and after making a PIC
7248 call $gp is restored from that location. */
7249
7250static void
7251s_cprestore (ignore)
7252 int ignore;
7253{
7254 expressionS ex;
7255 int icnt = 0;
7256
d9aba805
ILT
7257 /* If we are not generating SVR4 PIC code, .cprestore is ignored. */
7258 if (mips_pic != SVR4_PIC)
0dd2d296
ILT
7259 {
7260 s_ignore (0);
7261 return;
7262 }
7263
9226253a
ILT
7264 mips_cprestore_offset = get_absolute_expression ();
7265
7266 ex.X_op = O_constant;
7267 ex.X_add_symbol = NULL;
7268 ex.X_op_symbol = NULL;
7269 ex.X_add_number = mips_cprestore_offset;
7270
0dd2d296 7271 macro_build ((char *) NULL, &icnt, &ex,
9226253a
ILT
7272 mips_isa < 3 ? "sw" : "sd",
7273 "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
7274
7275 demand_empty_rest_of_line ();
7276}
7277
0dd2d296
ILT
7278/* Handle the .gpword pseudo-op. This is used when generating PIC
7279 code. It generates a 32 bit GP relative reloc. */
7280
7281static void
7282s_gpword (ignore)
7283 int ignore;
7284{
23dc1ae3 7285 symbolS *label;
0dd2d296
ILT
7286 expressionS ex;
7287 char *p;
7288
7289 /* When not generating PIC code, this is treated as .word. */
7dfa376e 7290 if (mips_pic != SVR4_PIC)
0dd2d296
ILT
7291 {
7292 s_cons (2);
7293 return;
7294 }
7295
23dc1ae3 7296 label = insn_label;
0dd2d296
ILT
7297 mips_emit_delays ();
7298 if (auto_align)
23dc1ae3 7299 mips_align (2, 0, label);
0dd2d296
ILT
7300 insn_label = NULL;
7301
7302 expression (&ex);
7303
7304 if (ex.X_op != O_symbol || ex.X_add_number != 0)
7305 {
7306 as_bad ("Unsupported use of .gpword");
7307 ignore_rest_of_line ();
7308 }
7309
7310 p = frag_more (4);
7311 md_number_to_chars (p, (valueT) 0, 4);
7312 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
7313 BFD_RELOC_MIPS_GPREL32);
7314
7315 demand_empty_rest_of_line ();
7316}
7317
7318/* Handle the .cpadd pseudo-op. This is used when dealing with switch
7319 tables in SVR4 PIC code. */
7320
7321static void
7322s_cpadd (ignore)
7323 int ignore;
7324{
7325 int icnt = 0;
7326 int reg;
7327
7328 /* This is ignored when not generating SVR4 PIC code. */
7dfa376e 7329 if (mips_pic != SVR4_PIC)
0dd2d296
ILT
7330 {
7331 s_ignore (0);
7332 return;
7333 }
7334
7335 /* Add $gp to the register named as an argument. */
7336 reg = tc_get_register (0);
7337 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7338 mips_isa < 3 ? "addu" : "daddu",
7339 "d,v,t", reg, reg, GP);
7340
7341 demand_empty_rest_of_line ();
7342}
7343
9226253a 7344/* Parse a register string into a number. Called from the ECOFF code
0dd2d296
ILT
7345 to parse .frame. The argument is non-zero if this is the frame
7346 register, so that we can record it in mips_frame_reg. */
9226253a 7347
3d3c5039 7348int
0dd2d296
ILT
7349tc_get_register (frame)
7350 int frame;
3d3c5039
ILT
7351{
7352 int reg;
7353
7354 SKIP_WHITESPACE ();
7355 if (*input_line_pointer++ != '$')
7356 {
7357 as_warn ("expected `$'");
0dd2d296 7358 reg = 0;
3d3c5039 7359 }
0dd2d296 7360 else if (isdigit ((unsigned char) *input_line_pointer))
3d3c5039
ILT
7361 {
7362 reg = get_absolute_expression ();
7363 if (reg < 0 || reg >= 32)
7364 {
7365 as_warn ("Bad register number");
7366 reg = 0;
7367 }
7368 }
7369 else
7370 {
7371 if (strncmp (input_line_pointer, "fp", 2) == 0)
9226253a 7372 reg = FP;
3d3c5039 7373 else if (strncmp (input_line_pointer, "sp", 2) == 0)
9226253a 7374 reg = SP;
3d3c5039 7375 else if (strncmp (input_line_pointer, "gp", 2) == 0)
9226253a 7376 reg = GP;
3d3c5039 7377 else if (strncmp (input_line_pointer, "at", 2) == 0)
9226253a 7378 reg = AT;
3d3c5039
ILT
7379 else
7380 {
7381 as_warn ("Unrecognized register name");
0dd2d296 7382 reg = 0;
3d3c5039
ILT
7383 }
7384 input_line_pointer += 2;
7385 }
0dd2d296
ILT
7386 if (frame)
7387 mips_frame_reg = reg != 0 ? reg : SP;
3d3c5039
ILT
7388 return reg;
7389}
7390
0dd2d296
ILT
7391valueT
7392md_section_align (seg, addr)
7393 asection *seg;
7394 valueT addr;
7395{
7396 int align = bfd_get_section_alignment (stdoutput, seg);
7397
7398 return ((addr + (1 << align) - 1) & (-1 << align));
7399}
7400
d8a1c247
KR
7401/* Utility routine, called from above as well. If called while the
7402 input file is still being read, it's only an approximation. (For
7403 example, a symbol may later become defined which appeared to be
7404 undefined earlier.) */
22ba90ce
ILT
7405
7406static int
7407nopic_need_relax (sym)
d8a1c247
KR
7408 symbolS *sym;
7409{
7410 if (sym == 0)
7411 return 0;
7412
1dc1e798
KR
7413 if (USE_GLOBAL_POINTER_OPT)
7414 {
7415 const char *symname;
7416 int change;
7417
7418 /* Find out whether this symbol can be referenced off the GP
7419 register. It can be if it is smaller than the -G size or if
7420 it is in the .sdata or .sbss section. Certain symbols can
7421 not be referenced off the GP, although it appears as though
7422 they can. */
7423 symname = S_GET_NAME (sym);
7424 if (symname != (const char *) NULL
7425 && (strcmp (symname, "eprol") == 0
7426 || strcmp (symname, "etext") == 0
7427 || strcmp (symname, "_gp") == 0
7428 || strcmp (symname, "edata") == 0
7429 || strcmp (symname, "_fbss") == 0
7430 || strcmp (symname, "_fdata") == 0
7431 || strcmp (symname, "_ftext") == 0
7432 || strcmp (symname, "end") == 0
7433 || strcmp (symname, "_gp_disp") == 0))
7434 change = 1;
7435 else if (! S_IS_DEFINED (sym)
c625fc23
JSC
7436 && (0
7437#ifndef NO_ECOFF_DEBUGGING
7438 || (sym->ecoff_extern_size != 0
7439 && sym->ecoff_extern_size <= g_switch_value)
7440#endif
1dc1e798
KR
7441 || (S_GET_VALUE (sym) != 0
7442 && S_GET_VALUE (sym) <= g_switch_value)))
7443 change = 0;
7444 else
7445 {
7446 const char *segname;
d8a1c247 7447
1dc1e798
KR
7448 segname = segment_name (S_GET_SEGMENT (sym));
7449 assert (strcmp (segname, ".lit8") != 0
7450 && strcmp (segname, ".lit4") != 0);
7451 change = (strcmp (segname, ".sdata") != 0
7452 && strcmp (segname, ".sbss") != 0);
7453 }
7454 return change;
7455 }
7456 else
7457 /* We are not optimizing for the GP register. */
7458 return 1;
d8a1c247
KR
7459}
7460
22ba90ce
ILT
7461/* Estimate the size of a frag before relaxing. We are not really
7462 relaxing here, and the final size is encoded in the subtype
7463 information. */
7464
0dd2d296
ILT
7465/*ARGSUSED*/
7466int
7467md_estimate_size_before_relax (fragp, segtype)
7468 fragS *fragp;
7469 asection *segtype;
7470{
7471 int change;
7472
d9aba805 7473 if (mips_pic == NO_PIC)
0dd2d296 7474 {
d8a1c247 7475 change = nopic_need_relax (fragp->fr_symbol);
0dd2d296 7476 }
d9aba805 7477 else if (mips_pic == SVR4_PIC)
0dd2d296
ILT
7478 {
7479 asection *symsec = fragp->fr_symbol->bsym->section;
7480
7481 /* This must duplicate the test in adjust_reloc_syms. */
7482 change = (symsec != &bfd_und_section
7483 && symsec != &bfd_abs_section
7484 && ! bfd_is_com_section (symsec));
7485 }
d9aba805
ILT
7486 else
7487 abort ();
0dd2d296
ILT
7488
7489 if (change)
7490 {
7491 /* Record the offset to the first reloc in the fr_opcode field.
7492 This lets md_convert_frag and tc_gen_reloc know that the code
7493 must be expanded. */
7494 fragp->fr_opcode = (fragp->fr_literal
7495 + fragp->fr_fix
7496 - RELAX_OLD (fragp->fr_subtype)
7497 + RELAX_RELOC1 (fragp->fr_subtype));
7498 /* FIXME: This really needs as_warn_where. */
7499 if (RELAX_WARN (fragp->fr_subtype))
7500 as_warn ("AT used after \".set noat\" or macro used after \".set nomacro\"");
7501 }
7502
7503 if (! change)
7504 return 0;
7505 else
7506 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
7507}
7508
7509/* Translate internal representation of relocation info to BFD target
7510 format. */
7511
7512arelent **
3d3c5039
ILT
7513tc_gen_reloc (section, fixp)
7514 asection *section;
7515 fixS *fixp;
7516{
0dd2d296 7517 static arelent *retval[4];
3d3c5039 7518 arelent *reloc;
a8aed9dd 7519 bfd_reloc_code_real_type code;
3d3c5039 7520
0dd2d296
ILT
7521 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
7522 retval[1] = NULL;
3d3c5039
ILT
7523
7524 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
7525 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1c803e52
ILT
7526
7527 if (mips_pic == EMBEDDED_PIC
7528 && SWITCH_TABLE (fixp))
7529 {
7530 /* For a switch table entry we use a special reloc. The addend
7531 is actually the difference between the reloc address and the
7532 subtrahend. */
7533 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
1dc1e798
KR
7534 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
7535 as_fatal ("Double check fx_r_type in tc-mips.c:tc_gen_reloc");
1c803e52
ILT
7536 fixp->fx_r_type = BFD_RELOC_GPREL32;
7537 }
ecd4ca1c
ILT
7538 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
7539 {
7540 /* We use a special addend for an internal RELLO reloc. */
7541 if (fixp->fx_addsy->bsym->flags & BSF_SECTION_SYM)
7542 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
7543 else
7544 reloc->addend = fixp->fx_addnumber + reloc->address;
7545 }
7546 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
7547 {
7548 assert (fixp->fx_next != NULL
7549 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
7550 /* We use a special addend for an internal RELHI reloc. The
7551 reloc is relative to the RELLO; adjust the addend
7552 accordingly. */
7553 if (fixp->fx_addsy->bsym->flags & BSF_SECTION_SYM)
7554 reloc->addend = (fixp->fx_next->fx_frag->fr_address
7555 + fixp->fx_next->fx_where
7556 - S_GET_VALUE (fixp->fx_subsy));
7557 else
7558 reloc->addend = (fixp->fx_addnumber
7559 + fixp->fx_next->fx_frag->fr_address
7560 + fixp->fx_next->fx_where);
7561 }
1c803e52 7562 else if (fixp->fx_pcrel == 0)
3d3c5039
ILT
7563 reloc->addend = fixp->fx_addnumber;
7564 else
5b63f465 7565 {
1dc1e798
KR
7566 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
7567 /* A gruesome hack which is a result of the gruesome gas reloc
7568 handling. */
7569 reloc->addend = reloc->address;
7570 else
7571 reloc->addend = -reloc->address;
5b63f465 7572 }
0dd2d296
ILT
7573
7574 /* If this is a variant frag, we may need to adjust the existing
7575 reloc and generate a new one. */
7576 if (fixp->fx_frag->fr_opcode != NULL
7577 && (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
7578 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
fb251650
ILT
7579 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
7580 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
7581 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
7582 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
7583 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16))
0dd2d296
ILT
7584 {
7585 arelent *reloc2;
7586
7587 /* If this is not the last reloc in this frag, then we have two
fb251650
ILT
7588 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
7589 CALL_HI16/CALL_LO16, both of which are being replaced. Let
7590 the second one handle all of them. */
0dd2d296
ILT
7591 if (fixp->fx_next != NULL
7592 && fixp->fx_frag == fixp->fx_next->fx_frag)
7593 {
fb251650
ILT
7594 assert ((fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
7595 && fixp->fx_next->fx_r_type == BFD_RELOC_MIPS_GPREL)
7596 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
7597 && (fixp->fx_next->fx_r_type
7598 == BFD_RELOC_MIPS_GOT_LO16))
7599 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
7600 && (fixp->fx_next->fx_r_type
7601 == BFD_RELOC_MIPS_CALL_LO16)));
0dd2d296
ILT
7602 retval[0] = NULL;
7603 return retval;
7604 }
7605
7606 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
7607 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
7608 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
7609 retval[2] = NULL;
7610 reloc2->sym_ptr_ptr = &fixp->fx_addsy->bsym;
7611 reloc2->address = (reloc->address
7612 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
7613 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
7614 reloc2->addend = fixp->fx_addnumber;
7615 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
7616 assert (reloc2->howto != NULL);
7617
7618 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
7619 {
7620 arelent *reloc3;
7621
7622 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
7623 retval[3] = NULL;
7624 *reloc3 = *reloc2;
7625 reloc3->address += 4;
7626 }
7627
d9aba805 7628 if (mips_pic == NO_PIC)
0dd2d296
ILT
7629 {
7630 assert (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL);
7631 fixp->fx_r_type = BFD_RELOC_HI16_S;
7632 }
d9aba805 7633 else if (mips_pic == SVR4_PIC)
0dd2d296 7634 {
fb251650 7635 switch (fixp->fx_r_type)
0dd2d296 7636 {
fb251650
ILT
7637 default:
7638 abort ();
7639 case BFD_RELOC_MIPS_GOT16:
7640 break;
7641 case BFD_RELOC_MIPS_CALL16:
7642 case BFD_RELOC_MIPS_GOT_LO16:
7643 case BFD_RELOC_MIPS_CALL_LO16:
0dd2d296 7644 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
fb251650 7645 break;
0dd2d296
ILT
7646 }
7647 }
d9aba805
ILT
7648 else
7649 abort ();
0dd2d296
ILT
7650 }
7651
a8aed9dd
ILT
7652 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
7653 fixup_segment converted a non-PC relative reloc into a PC
7654 relative reloc. In such a case, we need to convert the reloc
7655 code. */
7656 code = fixp->fx_r_type;
7657 if (fixp->fx_pcrel)
7658 {
7659 switch (code)
7660 {
7661 case BFD_RELOC_8:
7662 code = BFD_RELOC_8_PCREL;
7663 break;
7664 case BFD_RELOC_16:
7665 code = BFD_RELOC_16_PCREL;
7666 break;
7667 case BFD_RELOC_32:
7668 code = BFD_RELOC_32_PCREL;
7669 break;
7670 case BFD_RELOC_8_PCREL:
7671 case BFD_RELOC_16_PCREL:
7672 case BFD_RELOC_32_PCREL:
7673 case BFD_RELOC_16_PCREL_S2:
7674 break;
7675 default:
7676 as_bad_where (fixp->fx_file, fixp->fx_line,
7677 "Cannot make %s relocation PC relative",
7678 bfd_get_reloc_code_name (code));
7679 }
7680 }
7681
d9aba805
ILT
7682 /* To support a PC relative reloc when generating embedded PIC code
7683 for ECOFF, we use a Cygnus extension. We check for that here to
7684 make sure that we don't let such a reloc escape normally. */
1dc1e798 7685 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour
a8aed9dd 7686 && code == BFD_RELOC_16_PCREL_S2
d9aba805
ILT
7687 && mips_pic != EMBEDDED_PIC)
7688 reloc->howto = NULL;
7689 else
a8aed9dd 7690 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
0dd2d296 7691
52aa70b5
JW
7692 if (reloc->howto == NULL)
7693 {
7694 as_bad_where (fixp->fx_file, fixp->fx_line,
a8aed9dd
ILT
7695 "Can not represent %s relocation in this object file format",
7696 bfd_get_reloc_code_name (code));
0dd2d296 7697 retval[0] = NULL;
52aa70b5 7698 }
3d3c5039 7699
0dd2d296 7700 return retval;
3d3c5039
ILT
7701}
7702
0dd2d296
ILT
7703/* Convert a machine dependent frag. */
7704
7705void
7706md_convert_frag (abfd, asec, fragp)
7707 bfd *abfd;
7708 segT asec;
7709 fragS *fragp;
3d3c5039 7710{
0dd2d296
ILT
7711 int old, new;
7712 char *fixptr;
3d3c5039 7713
0dd2d296
ILT
7714 if (fragp->fr_opcode == NULL)
7715 return;
3d3c5039 7716
0dd2d296
ILT
7717 old = RELAX_OLD (fragp->fr_subtype);
7718 new = RELAX_NEW (fragp->fr_subtype);
7719 fixptr = fragp->fr_literal + fragp->fr_fix;
7720
7721 if (new > 0)
7722 memcpy (fixptr - old, fixptr, new);
7723
7724 fragp->fr_fix += new - old;
7725}
becfe05e
ILT
7726
7727/* This function is called whenever a label is defined. It is used
7728 when handling branch delays; if a branch has a label, we assume we
7729 can not move it. */
7730
7731void
7732mips_define_label (sym)
7733 symbolS *sym;
7734{
7735 insn_label = sym;
7736}
22ba90ce
ILT
7737
7738/* Decide whether a label is local. This is called by LOCAL_LABEL.
7739 In order to work with gcc when using mips-tfile, we must keep all
7740 local labels. However, in other cases, we want to discard them,
7741 since they are useless. */
7742
7743int
7744mips_local_label (name)
7745 const char *name;
7746{
c625fc23 7747#ifndef NO_ECOFF_DEBUGGING
22ba90ce
ILT
7748 if (ECOFF_DEBUGGING
7749 && mips_debug != 0
7750 && ! ecoff_debugging_seen)
7751 {
7752 /* We were called with -g, but we didn't see any debugging
7753 information. That may mean that gcc is smuggling debugging
7754 information through to mips-tfile, in which case we must
7755 generate all local labels. */
7756 return 0;
7757 }
c625fc23 7758#endif
22ba90ce
ILT
7759
7760 /* Here it's OK to discard local labels. */
7761
3af584b4 7762 return name[0] == '$';
22ba90ce 7763}
3d3c5039 7764\f
739708fa 7765#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
f2a663d3 7766
0dd2d296 7767/* Some special processing for a MIPS ELF file. */
f2a663d3
ILT
7768
7769void
7770mips_elf_final_processing ()
7771{
87178180
ILT
7772 /* Write out the register information. */
7773 if (! mips_64)
7774 {
7775 Elf32_RegInfo s;
7776
7777 s.ri_gprmask = mips_gprmask;
7778 s.ri_cprmask[0] = mips_cprmask[0];
7779 s.ri_cprmask[1] = mips_cprmask[1];
7780 s.ri_cprmask[2] = mips_cprmask[2];
7781 s.ri_cprmask[3] = mips_cprmask[3];
7782 /* The gp_value field is set by the MIPS ELF backend. */
7783
7784 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
7785 ((Elf32_External_RegInfo *)
7786 mips_regmask_frag));
7787 }
7788 else
7789 {
7790 Elf64_Internal_RegInfo s;
7791
7792 s.ri_gprmask = mips_gprmask;
7793 s.ri_pad = 0;
7794 s.ri_cprmask[0] = mips_cprmask[0];
7795 s.ri_cprmask[1] = mips_cprmask[1];
7796 s.ri_cprmask[2] = mips_cprmask[2];
7797 s.ri_cprmask[3] = mips_cprmask[3];
7798 /* The gp_value field is set by the MIPS ELF backend. */
7799
7800 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
7801 ((Elf64_External_RegInfo *)
7802 mips_regmask_frag));
7803 }
0dd2d296
ILT
7804
7805 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
7806 sort of BFD interface for this. */
7807 if (mips_any_noreorder)
7808 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
d9aba805 7809 if (mips_pic != NO_PIC)
0dd2d296 7810 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
f2a663d3
ILT
7811}
7812
739708fa 7813#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
f2a663d3 7814\f
3d3c5039
ILT
7815/* These functions should really be defined by the object file format,
7816 since they are related to debugging information. However, this
7817 code has to work for the a.out format, which does not define them,
7818 so we provide simple versions here. These don't actually generate
7819 any debugging information, but they do simple checking and someday
7820 somebody may make them useful. */
7821
670a50eb
ILT
7822typedef struct loc
7823{
7824 struct loc *loc_next;
7825 unsigned long loc_fileno;
7826 unsigned long loc_lineno;
7827 unsigned long loc_offset;
7828 unsigned short loc_delta;
7829 unsigned short loc_count;
3d3c5039 7830#if 0
670a50eb 7831 fragS *loc_frag;
3d3c5039 7832#endif
670a50eb
ILT
7833}
7834locS;
3d3c5039 7835
670a50eb
ILT
7836typedef struct proc
7837 {
3d3c5039
ILT
7838 struct proc *proc_next;
7839 struct symbol *proc_isym;
7840 struct symbol *proc_end;
7841 unsigned long proc_reg_mask;
7842 unsigned long proc_reg_offset;
7843 unsigned long proc_fpreg_mask;
7844 unsigned long proc_fpreg_offset;
7845 unsigned long proc_frameoffset;
7846 unsigned long proc_framereg;
7847 unsigned long proc_pcreg;
7848 locS *proc_iline;
7849 struct file *proc_file;
7850 int proc_index;
670a50eb
ILT
7851 }
7852procS;
3d3c5039 7853
670a50eb
ILT
7854typedef struct file
7855 {
3d3c5039
ILT
7856 struct file *file_next;
7857 unsigned long file_fileno;
7858 struct symbol *file_symbol;
7859 struct symbol *file_end;
7860 struct proc *file_proc;
7861 int file_numprocs;
670a50eb
ILT
7862 }
7863fileS;
3d3c5039
ILT
7864
7865static struct obstack proc_frags;
7866static procS *proc_lastP;
7867static procS *proc_rootP;
7868static int numprocs;
7869
7870static void
7871md_obj_begin ()
7872{
670a50eb 7873 obstack_begin (&proc_frags, 0x2000);
3d3c5039
ILT
7874}
7875
7876static void
7877md_obj_end ()
7878{
7879 /* check for premature end, nesting errors, etc */
7880 if (proc_lastP && proc_lastP->proc_end == NULL)
670a50eb 7881 as_warn ("missing `.end' at end of assembly");
3d3c5039
ILT
7882}
7883
3d3c5039
ILT
7884static long
7885get_number ()
7886{
670a50eb
ILT
7887 int negative = 0;
7888 long val = 0;
3d3c5039 7889
670a50eb
ILT
7890 if (*input_line_pointer == '-')
7891 {
7892 ++input_line_pointer;
7893 negative = 1;
3d3c5039 7894 }
670a50eb
ILT
7895 if (!isdigit (*input_line_pointer))
7896 as_bad ("Expected simple number.");
7897 if (input_line_pointer[0] == '0')
7898 {
7899 if (input_line_pointer[1] == 'x')
7900 {
7901 input_line_pointer += 2;
7902 while (isxdigit (*input_line_pointer))
7903 {
7904 val <<= 4;
3a762a0b 7905 val |= hex_value (*input_line_pointer++);
3d3c5039 7906 }
670a50eb
ILT
7907 return negative ? -val : val;
7908 }
7909 else
7910 {
7911 ++input_line_pointer;
7912 while (isdigit (*input_line_pointer))
7913 {
7914 val <<= 3;
7915 val |= *input_line_pointer++ - '0';
3d3c5039 7916 }
670a50eb 7917 return negative ? -val : val;
3d3c5039
ILT
7918 }
7919 }
670a50eb
ILT
7920 if (!isdigit (*input_line_pointer))
7921 {
7922 printf (" *input_line_pointer == '%c' 0x%02x\n",
7923 *input_line_pointer, *input_line_pointer);
7924 as_warn ("Invalid number");
7925 return -1;
3d3c5039 7926 }
670a50eb
ILT
7927 while (isdigit (*input_line_pointer))
7928 {
7929 val *= 10;
7930 val += *input_line_pointer++ - '0';
3d3c5039 7931 }
670a50eb 7932 return negative ? -val : val;
3d3c5039
ILT
7933}
7934
7935/* The .file directive; just like the usual .file directive, but there
7936 is an initial number which is the ECOFF file index. */
7937
7938static void
7939s_file (x)
7940 int x;
7941{
670a50eb 7942 int line;
3d3c5039 7943
670a50eb 7944 line = get_number ();
9a7d824a 7945 s_app_file (0);
3d3c5039
ILT
7946}
7947
7948
7949/* The .end directive. */
7950
7951static void
7952s_mipsend (x)
7953 int x;
7954{
670a50eb
ILT
7955 symbolS *p;
7956
7957 if (!is_end_of_line[(unsigned char) *input_line_pointer])
7958 {
7959 p = get_symbol ();
7960 demand_empty_rest_of_line ();
7961 }
7962 else
7963 p = NULL;
7964 if (now_seg != text_section)
7965 as_warn (".end not in text section");
7966 if (!proc_lastP)
7967 {
7968 as_warn (".end and no .ent seen yet.");
7969 return;
3d3c5039
ILT
7970 }
7971
670a50eb
ILT
7972 if (p != NULL)
7973 {
7974 assert (S_GET_NAME (p));
7975 if (strcmp (S_GET_NAME (p), S_GET_NAME (proc_lastP->proc_isym)))
7976 as_warn (".end symbol does not match .ent symbol.");
3d3c5039
ILT
7977 }
7978
670a50eb 7979 proc_lastP->proc_end = (symbolS *) 1;
3d3c5039
ILT
7980}
7981
7982/* The .aent and .ent directives. */
7983
7984static void
7985s_ent (aent)
7986 int aent;
7987{
670a50eb
ILT
7988 int number = 0;
7989 procS *procP;
7990 symbolS *symbolP;
7991
7992 symbolP = get_symbol ();
7993 if (*input_line_pointer == ',')
7994 input_line_pointer++;
dd3f1f76 7995 SKIP_WHITESPACE ();
670a50eb
ILT
7996 if (isdigit (*input_line_pointer) || *input_line_pointer == '-')
7997 number = get_number ();
7998 if (now_seg != text_section)
7999 as_warn (".ent or .aent not in text section.");
8000
8001 if (!aent && proc_lastP && proc_lastP->proc_end == NULL)
8002 as_warn ("missing `.end'");
8003
8004 if (!aent)
8005 {
8006 procP = (procS *) obstack_alloc (&proc_frags, sizeof (*procP));
8007 procP->proc_isym = symbolP;
8008 procP->proc_reg_mask = 0;
8009 procP->proc_reg_offset = 0;
8010 procP->proc_fpreg_mask = 0;
8011 procP->proc_fpreg_offset = 0;
8012 procP->proc_frameoffset = 0;
8013 procP->proc_framereg = 0;
8014 procP->proc_pcreg = 0;
8015 procP->proc_end = NULL;
8016 procP->proc_next = NULL;
8017 if (proc_lastP)
8018 proc_lastP->proc_next = procP;
8019 else
8020 proc_rootP = procP;
8021 proc_lastP = procP;
8022 numprocs++;
3d3c5039 8023 }
670a50eb 8024 demand_empty_rest_of_line ();
3d3c5039
ILT
8025}
8026
8027/* The .frame directive. */
8028
88225433 8029#if 0
3d3c5039
ILT
8030static void
8031s_frame (x)
670a50eb 8032 int x;
3d3c5039 8033{
670a50eb
ILT
8034 char str[100];
8035 symbolS *symP;
8036 int frame_reg;
8037 int frame_off;
8038 int pcreg;
8039
0dd2d296 8040 frame_reg = tc_get_register (1);
670a50eb
ILT
8041 if (*input_line_pointer == ',')
8042 input_line_pointer++;
5ac34ac3 8043 frame_off = get_absolute_expression ();
670a50eb
ILT
8044 if (*input_line_pointer == ',')
8045 input_line_pointer++;
0dd2d296 8046 pcreg = tc_get_register (0);
670a50eb
ILT
8047
8048 /* bob third eye */
8049 assert (proc_rootP);
8050 proc_rootP->proc_framereg = frame_reg;
8051 proc_rootP->proc_frameoffset = frame_off;
8052 proc_rootP->proc_pcreg = pcreg;
8053 /* bob macho .frame */
8054
8055 /* We don't have to write out a frame stab for unoptimized code. */
9226253a 8056 if (!(frame_reg == FP && frame_off == 0))
670a50eb
ILT
8057 {
8058 if (!proc_lastP)
8059 as_warn ("No .ent for .frame to use.");
8060 (void) sprintf (str, "R%d;%d", frame_reg, frame_off);
8061 symP = symbol_new (str, N_VFP, 0, frag_now);
8062 S_SET_TYPE (symP, N_RMASK);
8063 S_SET_OTHER (symP, 0);
8064 S_SET_DESC (symP, 0);
8065 symP->sy_forward = proc_lastP->proc_isym;
8066 /* bob perhaps I should have used pseudo set */
3d3c5039 8067 }
670a50eb 8068 demand_empty_rest_of_line ();
3d3c5039 8069}
88225433 8070#endif
3d3c5039
ILT
8071
8072/* The .fmask and .mask directives. */
8073
88225433 8074#if 0
3d3c5039
ILT
8075static void
8076s_mask (reg_type)
8077 char reg_type;
8078{
670a50eb
ILT
8079 char str[100], *strP;
8080 symbolS *symP;
8081 int i;
8082 unsigned int mask;
8083 int off;
8084
8085 mask = get_number ();
8086 if (*input_line_pointer == ',')
8087 input_line_pointer++;
8088 off = get_absolute_expression ();
8089
8090 /* bob only for coff */
8091 assert (proc_rootP);
8092 if (reg_type == 'F')
8093 {
8094 proc_rootP->proc_fpreg_mask = mask;
8095 proc_rootP->proc_fpreg_offset = off;
3d3c5039 8096 }
670a50eb
ILT
8097 else
8098 {
8099 proc_rootP->proc_reg_mask = mask;
8100 proc_rootP->proc_reg_offset = off;
8101 }
8102
8103 /* bob macho .mask + .fmask */
3d3c5039 8104
670a50eb
ILT
8105 /* We don't have to write out a mask stab if no saved regs. */
8106 if (!(mask == 0))
8107 {
8108 if (!proc_lastP)
8109 as_warn ("No .ent for .mask to use.");
8110 strP = str;
8111 for (i = 0; i < 32; i++)
8112 {
8113 if (mask % 2)
8114 {
8115 sprintf (strP, "%c%d,", reg_type, i);
8116 strP += strlen (strP);
8117 }
3d3c5039 8118 mask /= 2;
670a50eb
ILT
8119 }
8120 sprintf (strP, ";%d,", off);
8121 symP = symbol_new (str, N_RMASK, 0, frag_now);
8122 S_SET_TYPE (symP, N_RMASK);
8123 S_SET_OTHER (symP, 0);
8124 S_SET_DESC (symP, 0);
8125 symP->sy_forward = proc_lastP->proc_isym;
8126 /* bob perhaps I should have used pseudo set */
3d3c5039 8127 }
3d3c5039 8128}
88225433 8129#endif
3d3c5039
ILT
8130
8131/* The .loc directive. */
8132
88225433 8133#if 0
3d3c5039
ILT
8134static void
8135s_loc (x)
8136 int x;
8137{
670a50eb
ILT
8138 symbolS *symbolP;
8139 int lineno;
8140 int addroff;
3d3c5039 8141
670a50eb 8142 assert (now_seg == text_section);
3d3c5039 8143
670a50eb 8144 lineno = get_number ();
87e48495 8145 addroff = frag_now_fix ();
3d3c5039 8146
670a50eb
ILT
8147 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
8148 S_SET_TYPE (symbolP, N_SLINE);
8149 S_SET_OTHER (symbolP, 0);
8150 S_SET_DESC (symbolP, lineno);
8151 symbolP->sy_segment = now_seg;
3d3c5039 8152}
88225433 8153#endif