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