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