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