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