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