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