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