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