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