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