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