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