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