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