* config/tc-mips.c (load_register): Correctly handle 32 bit values
[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 || sizeof (ep->X_add_number) > 4
1664 || (ep->X_add_number & 0x80000000) == 0))
1665 {
1666 /* 32 bit values require an lui. */
1667 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
1668 (int) BFD_RELOC_HI16);
1669 if ((ep->X_add_number & 0xffff) != 0)
1670 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
1671 (int) BFD_RELOC_LO16);
1672 return;
1673 }
1674 else
1675 {
1676 /* 32 bit value with high bit set being loaded into a 64 bit
1677 register. We can't use lui, because that would
1678 incorrectly set the 32 high bits. */
1679 generic_bignum[3] = 0;
1680 generic_bignum[2] = 0;
1681 generic_bignum[1] = (ep->X_add_number >> 16) & 0xffff;
1682 generic_bignum[0] = ep->X_add_number & 0xffff;
1683 tmp.X_op = O_big;
1684 tmp.X_add_number = 4;
1685 ep = &tmp;
1686 }
1687 }
1688
1689 /* The value is larger than 32 bits. */
1690
1691 if (mips_isa < 3)
1692 {
1693 as_bad ("Number larger than 32 bits");
1694 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
1695 (int) BFD_RELOC_LO16);
1696 return;
1697 }
1698
1699 if (ep->X_op != O_big)
1700 {
1701 hi32 = *ep;
1702 shift = 32;
1703 hi32.X_add_number >>= shift;
1704 hi32.X_add_number &= 0xffffffff;
1705 if ((hi32.X_add_number & 0x80000000) != 0)
1706 hi32.X_add_number |= ~ (offsetT) 0xffffffff;
1707 lo32 = *ep;
1708 lo32.X_add_number &= 0xffffffff;
1709 }
1710 else
1711 {
1712 assert (ep->X_add_number > 2);
1713 if (ep->X_add_number == 3)
1714 generic_bignum[3] = 0;
1715 else if (ep->X_add_number > 4)
1716 as_bad ("Number larger than 64 bits");
1717 lo32.X_op = O_constant;
1718 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
1719 hi32.X_op = O_constant;
1720 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
1721 }
1722
1723 load_register (counter, reg, &hi32);
1724 if ((lo32.X_add_number & 0xffff0000) == 0)
1725 macro_build ((char *) NULL, counter, NULL, "dsll32", "d,w,<", reg,
1726 reg, 0);
1727 else
1728 {
1729 expressionS mid16;
1730
1731 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
1732 reg, 16);
1733 mid16 = lo32;
1734 mid16.X_add_number >>= 16;
1735 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
1736 reg, (int) BFD_RELOC_LO16);
1737 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
1738 reg, 16);
1739 }
1740 if ((lo32.X_add_number & 0xffff) != 0)
1741 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, reg,
1742 (int) BFD_RELOC_LO16);
1743 }
1744
1745 /* Load an address into a register. */
1746
1747 static void
1748 load_address (counter, reg, ep)
1749 int *counter;
1750 int reg;
1751 expressionS *ep;
1752 {
1753 char *p;
1754
1755 if (ep->X_op != O_constant
1756 && ep->X_op != O_symbol)
1757 {
1758 as_bad ("expression too complex");
1759 ep->X_op = O_constant;
1760 }
1761
1762 if (ep->X_op == O_constant)
1763 {
1764 load_register (counter, reg, ep);
1765 return;
1766 }
1767
1768 if (mips_pic == NO_PIC)
1769 {
1770 /* If this is a reference to a GP relative symbol, we want
1771 addiu $reg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
1772 Otherwise we want
1773 lui $reg,<sym> (BFD_RELOC_HI16_S)
1774 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
1775 If we have an addend, we always use the latter form. */
1776 if (ep->X_add_number != 0)
1777 p = NULL;
1778 else
1779 {
1780 frag_grow (20);
1781 macro_build ((char *) NULL, counter, ep,
1782 mips_isa < 3 ? "addiu" : "daddiu",
1783 "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
1784 p = frag_var (rs_machine_dependent, 8, 0,
1785 RELAX_ENCODE (4, 8, 0, 4, 0, mips_warn_about_macros),
1786 ep->X_add_symbol, (long) 0, (char *) NULL);
1787 }
1788 macro_build_lui (p, counter, ep, reg);
1789 if (p != NULL)
1790 p += 4;
1791 macro_build (p, counter, ep,
1792 mips_isa < 3 ? "addiu" : "daddiu",
1793 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
1794 }
1795 else if (mips_pic == SVR4_PIC)
1796 {
1797 expressionS ex;
1798
1799 /* If this is a reference to an external symbol, we want
1800 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1801 Otherwise we want
1802 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1803 nop
1804 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
1805 If there is a constant, it must be added in after. */
1806 ex.X_add_number = ep->X_add_number;
1807 ep->X_add_number = 0;
1808 frag_grow (20);
1809 macro_build ((char *) NULL, counter, ep,
1810 mips_isa < 3 ? "lw" : "ld",
1811 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
1812 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
1813 p = frag_var (rs_machine_dependent, 4, 0,
1814 RELAX_ENCODE (0, 4, -8, 0, 0, mips_warn_about_macros),
1815 ep->X_add_symbol, (long) 0, (char *) NULL);
1816 macro_build (p, counter, ep,
1817 mips_isa < 3 ? "addiu" : "daddiu",
1818 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
1819 if (ex.X_add_number != 0)
1820 {
1821 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
1822 as_bad ("PIC code offset overflow (max 16 signed bits)");
1823 ex.X_op = O_constant;
1824 macro_build (p, counter, &ex,
1825 mips_isa < 3 ? "addiu" : "daddiu",
1826 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
1827 }
1828 }
1829 else if (mips_pic == EMBEDDED_PIC)
1830 {
1831 /* We always do
1832 addiu $reg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
1833 */
1834 macro_build ((char *) NULL, counter, ep,
1835 mips_isa < 3 ? "addiu" : "daddiu",
1836 "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
1837 }
1838 else
1839 abort ();
1840 }
1841
1842 /*
1843 * Build macros
1844 * This routine implements the seemingly endless macro or synthesized
1845 * instructions and addressing modes in the mips assembly language. Many
1846 * of these macros are simple and are similar to each other. These could
1847 * probably be handled by some kind of table or grammer aproach instead of
1848 * this verbose method. Others are not simple macros but are more like
1849 * optimizing code generation.
1850 * One interesting optimization is when several store macros appear
1851 * consecutivly that would load AT with the upper half of the same address.
1852 * The ensuing load upper instructions are ommited. This implies some kind
1853 * of global optimization. We currently only optimize within a single macro.
1854 * For many of the load and store macros if the address is specified as a
1855 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
1856 * first load register 'at' with zero and use it as the base register. The
1857 * mips assembler simply uses register $zero. Just one tiny optimization
1858 * we're missing.
1859 */
1860 static void
1861 macro (ip)
1862 struct mips_cl_insn *ip;
1863 {
1864 register int treg, sreg, dreg, breg;
1865 int tempreg;
1866 int mask;
1867 int icnt = 0;
1868 int used_at;
1869 expressionS expr1;
1870 const char *s;
1871 const char *s2;
1872 const char *fmt;
1873 int likely = 0;
1874 int dbl = 0;
1875 int coproc = 0;
1876 int lr = 0;
1877 offsetT maxnum;
1878 int off;
1879 bfd_reloc_code_real_type r;
1880 char *p;
1881 int hold_mips_optimize;
1882
1883 treg = (ip->insn_opcode >> 16) & 0x1f;
1884 dreg = (ip->insn_opcode >> 11) & 0x1f;
1885 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
1886 mask = ip->insn_mo->mask;
1887
1888 expr1.X_op = O_constant;
1889 expr1.X_op_symbol = NULL;
1890 expr1.X_add_symbol = NULL;
1891 expr1.X_add_number = 1;
1892
1893 switch (mask)
1894 {
1895 case M_DABS:
1896 dbl = 1;
1897 case M_ABS:
1898 /* bgez $a0,.+12
1899 move v0,$a0
1900 sub v0,$zero,$a0
1901 */
1902
1903 mips_emit_delays ();
1904 ++mips_noreorder;
1905 mips_any_noreorder = 1;
1906
1907 expr1.X_add_number = 8;
1908 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
1909 if (dreg == sreg)
1910 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
1911 else
1912 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, sreg, 0);
1913 macro_build ((char *) NULL, &icnt, NULL,
1914 dbl ? "dsub" : "sub",
1915 "d,v,t", dreg, 0, sreg);
1916
1917 --mips_noreorder;
1918 return;
1919
1920 case M_ADD_I:
1921 s = "addi";
1922 s2 = "add";
1923 goto do_addi;
1924 case M_ADDU_I:
1925 s = "addiu";
1926 s2 = "addu";
1927 goto do_addi;
1928 case M_DADD_I:
1929 dbl = 1;
1930 s = "daddi";
1931 s2 = "dadd";
1932 goto do_addi;
1933 case M_DADDU_I:
1934 dbl = 1;
1935 s = "daddiu";
1936 s2 = "daddu";
1937 do_addi:
1938 if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
1939 {
1940 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
1941 (int) BFD_RELOC_LO16);
1942 return;
1943 }
1944 load_register (&icnt, AT, &imm_expr);
1945 macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
1946 break;
1947
1948 case M_AND_I:
1949 s = "andi";
1950 s2 = "and";
1951 goto do_bit;
1952 case M_OR_I:
1953 s = "ori";
1954 s2 = "or";
1955 goto do_bit;
1956 case M_NOR_I:
1957 s = "";
1958 s2 = "nor";
1959 goto do_bit;
1960 case M_XOR_I:
1961 s = "xori";
1962 s2 = "xor";
1963 do_bit:
1964 if (imm_expr.X_add_number >= 0 && imm_expr.X_add_number < 0x10000)
1965 {
1966 if (mask != M_NOR_I)
1967 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
1968 sreg, (int) BFD_RELOC_LO16);
1969 else
1970 {
1971 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
1972 treg, sreg, (int) BFD_RELOC_LO16);
1973 macro_build ((char *) NULL, &icnt, NULL, "nor", "d,v,t",
1974 treg, treg, 0);
1975 }
1976 return;
1977 }
1978
1979 load_register (&icnt, AT, &imm_expr);
1980 macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
1981 break;
1982
1983 case M_BEQ_I:
1984 s = "beq";
1985 goto beq_i;
1986 case M_BEQL_I:
1987 s = "beql";
1988 likely = 1;
1989 goto beq_i;
1990 case M_BNE_I:
1991 s = "bne";
1992 goto beq_i;
1993 case M_BNEL_I:
1994 s = "bnel";
1995 likely = 1;
1996 beq_i:
1997 if (imm_expr.X_add_number == 0)
1998 {
1999 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
2000 0);
2001 return;
2002 }
2003 load_register (&icnt, AT, &imm_expr);
2004 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
2005 break;
2006
2007 case M_BGEL:
2008 likely = 1;
2009 case M_BGE:
2010 if (treg == 0)
2011 {
2012 macro_build ((char *) NULL, &icnt, &offset_expr,
2013 likely ? "bgezl" : "bgez",
2014 "s,p", sreg);
2015 return;
2016 }
2017 if (sreg == 0)
2018 {
2019 macro_build ((char *) NULL, &icnt, &offset_expr,
2020 likely ? "blezl" : "blez",
2021 "s,p", treg);
2022 return;
2023 }
2024 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
2025 macro_build ((char *) NULL, &icnt, &offset_expr,
2026 likely ? "beql" : "beq",
2027 "s,t,p", AT, 0);
2028 break;
2029
2030 case M_BGTL_I:
2031 likely = 1;
2032 case M_BGT_I:
2033 /* check for > max integer */
2034 maxnum = 0x7fffffff;
2035 if (mips_isa >= 3)
2036 {
2037 maxnum <<= 16;
2038 maxnum |= 0xffff;
2039 maxnum <<= 16;
2040 maxnum |= 0xffff;
2041 }
2042 if (imm_expr.X_add_number >= maxnum
2043 && (mips_isa < 3 || sizeof (maxnum) > 4))
2044 {
2045 do_false:
2046 /* result is always false */
2047 if (! likely)
2048 {
2049 as_warn ("Branch %s is always false (nop)", ip->insn_mo->name);
2050 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
2051 }
2052 else
2053 {
2054 as_warn ("Branch likely %s is always false", ip->insn_mo->name);
2055 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
2056 "s,t,p", 0, 0);
2057 }
2058 return;
2059 }
2060 imm_expr.X_add_number++;
2061 /* FALLTHROUGH */
2062 case M_BGE_I:
2063 case M_BGEL_I:
2064 if (mask == M_BGEL_I)
2065 likely = 1;
2066 if (imm_expr.X_add_number == 0)
2067 {
2068 macro_build ((char *) NULL, &icnt, &offset_expr,
2069 likely ? "bgezl" : "bgez",
2070 "s,p", sreg);
2071 return;
2072 }
2073 if (imm_expr.X_add_number == 1)
2074 {
2075 macro_build ((char *) NULL, &icnt, &offset_expr,
2076 likely ? "bgtzl" : "bgtz",
2077 "s,p", sreg);
2078 return;
2079 }
2080 maxnum = 0x7fffffff;
2081 if (mips_isa >= 3)
2082 {
2083 maxnum <<= 16;
2084 maxnum |= 0xffff;
2085 maxnum <<= 16;
2086 maxnum |= 0xffff;
2087 }
2088 maxnum = - maxnum - 1;
2089 if (imm_expr.X_add_number <= maxnum
2090 && (mips_isa < 3 || sizeof (maxnum) > 4))
2091 {
2092 do_true:
2093 /* result is always true */
2094 as_warn ("Branch %s is always true", ip->insn_mo->name);
2095 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
2096 return;
2097 }
2098 set_at (&icnt, sreg, 0);
2099 macro_build ((char *) NULL, &icnt, &offset_expr,
2100 likely ? "beql" : "beq",
2101 "s,t,p", AT, 0);
2102 break;
2103
2104 case M_BGEUL:
2105 likely = 1;
2106 case M_BGEU:
2107 if (treg == 0)
2108 goto do_true;
2109 if (sreg == 0)
2110 {
2111 macro_build ((char *) NULL, &icnt, &offset_expr,
2112 likely ? "beql" : "beq",
2113 "s,t,p", 0, treg);
2114 return;
2115 }
2116 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
2117 treg);
2118 macro_build ((char *) NULL, &icnt, &offset_expr,
2119 likely ? "beql" : "beq",
2120 "s,t,p", AT, 0);
2121 break;
2122
2123 case M_BGTUL_I:
2124 likely = 1;
2125 case M_BGTU_I:
2126 if (sreg == 0 || imm_expr.X_add_number == -1)
2127 goto do_false;
2128 imm_expr.X_add_number++;
2129 /* FALLTHROUGH */
2130 case M_BGEU_I:
2131 case M_BGEUL_I:
2132 if (mask == M_BGEUL_I)
2133 likely = 1;
2134 if (imm_expr.X_add_number == 0)
2135 goto do_true;
2136 if (imm_expr.X_add_number == 1)
2137 {
2138 macro_build ((char *) NULL, &icnt, &offset_expr,
2139 likely ? "bnel" : "bne",
2140 "s,t,p", sreg, 0);
2141 return;
2142 }
2143 set_at (&icnt, sreg, 1);
2144 macro_build ((char *) NULL, &icnt, &offset_expr,
2145 likely ? "beql" : "beq",
2146 "s,t,p", AT, 0);
2147 break;
2148
2149 case M_BGTL:
2150 likely = 1;
2151 case M_BGT:
2152 if (treg == 0)
2153 {
2154 macro_build ((char *) NULL, &icnt, &offset_expr,
2155 likely ? "bgtzl" : "bgtz",
2156 "s,p", sreg);
2157 return;
2158 }
2159 if (sreg == 0)
2160 {
2161 macro_build ((char *) NULL, &icnt, &offset_expr,
2162 likely ? "bltzl" : "bltz",
2163 "s,p", treg);
2164 return;
2165 }
2166 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
2167 macro_build ((char *) NULL, &icnt, &offset_expr,
2168 likely ? "bnel" : "bne",
2169 "s,t,p", AT, 0);
2170 break;
2171
2172 case M_BGTUL:
2173 likely = 1;
2174 case M_BGTU:
2175 if (treg == 0)
2176 {
2177 macro_build ((char *) NULL, &icnt, &offset_expr,
2178 likely ? "bnel" : "bne",
2179 "s,t,p", sreg, 0);
2180 return;
2181 }
2182 if (sreg == 0)
2183 goto do_false;
2184 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
2185 sreg);
2186 macro_build ((char *) NULL, &icnt, &offset_expr,
2187 likely ? "bnel" : "bne",
2188 "s,t,p", AT, 0);
2189 break;
2190
2191 case M_BLEL:
2192 likely = 1;
2193 case M_BLE:
2194 if (treg == 0)
2195 {
2196 macro_build ((char *) NULL, &icnt, &offset_expr,
2197 likely ? "blezl" : "blez",
2198 "s,p", sreg);
2199 return;
2200 }
2201 if (sreg == 0)
2202 {
2203 macro_build ((char *) NULL, &icnt, &offset_expr,
2204 likely ? "bgezl" : "bgez",
2205 "s,p", treg);
2206 return;
2207 }
2208 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
2209 macro_build ((char *) NULL, &icnt, &offset_expr,
2210 likely ? "beql" : "beq",
2211 "s,t,p", AT, 0);
2212 break;
2213
2214 case M_BLEL_I:
2215 likely = 1;
2216 case M_BLE_I:
2217 maxnum = 0x7fffffff;
2218 if (mips_isa >= 3)
2219 {
2220 maxnum <<= 16;
2221 maxnum |= 0xffff;
2222 maxnum <<= 16;
2223 maxnum |= 0xffff;
2224 }
2225 if (imm_expr.X_add_number >= maxnum
2226 && (mips_isa < 3 || sizeof (maxnum) > 4))
2227 goto do_true;
2228 imm_expr.X_add_number++;
2229 /* FALLTHROUGH */
2230 case M_BLT_I:
2231 case M_BLTL_I:
2232 if (mask == M_BLTL_I)
2233 likely = 1;
2234 if (imm_expr.X_add_number == 0)
2235 {
2236 macro_build ((char *) NULL, &icnt, &offset_expr,
2237 likely ? "bltzl" : "bltz",
2238 "s,p", sreg);
2239 return;
2240 }
2241 if (imm_expr.X_add_number == 1)
2242 {
2243 macro_build ((char *) NULL, &icnt, &offset_expr,
2244 likely ? "blezl" : "blez",
2245 "s,p", sreg);
2246 return;
2247 }
2248 set_at (&icnt, sreg, 0);
2249 macro_build ((char *) NULL, &icnt, &offset_expr,
2250 likely ? "bnel" : "bne",
2251 "s,t,p", AT, 0);
2252 break;
2253
2254 case M_BLEUL:
2255 likely = 1;
2256 case M_BLEU:
2257 if (treg == 0)
2258 {
2259 macro_build ((char *) NULL, &icnt, &offset_expr,
2260 likely ? "beql" : "beq",
2261 "s,t,p", sreg, 0);
2262 return;
2263 }
2264 if (sreg == 0)
2265 goto do_true;
2266 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
2267 sreg);
2268 macro_build ((char *) NULL, &icnt, &offset_expr,
2269 likely ? "beql" : "beq",
2270 "s,t,p", AT, 0);
2271 break;
2272
2273 case M_BLEUL_I:
2274 likely = 1;
2275 case M_BLEU_I:
2276 if (sreg == 0 || imm_expr.X_add_number == -1)
2277 goto do_true;
2278 imm_expr.X_add_number++;
2279 /* FALLTHROUGH */
2280 case M_BLTU_I:
2281 case M_BLTUL_I:
2282 if (mask == M_BLTUL_I)
2283 likely = 1;
2284 if (imm_expr.X_add_number == 0)
2285 goto do_false;
2286 if (imm_expr.X_add_number == 1)
2287 {
2288 macro_build ((char *) NULL, &icnt, &offset_expr,
2289 likely ? "beql" : "beq",
2290 "s,t,p", sreg, 0);
2291 return;
2292 }
2293 set_at (&icnt, sreg, 1);
2294 macro_build ((char *) NULL, &icnt, &offset_expr,
2295 likely ? "bnel" : "bne",
2296 "s,t,p", AT, 0);
2297 break;
2298
2299 case M_BLTL:
2300 likely = 1;
2301 case M_BLT:
2302 if (treg == 0)
2303 {
2304 macro_build ((char *) NULL, &icnt, &offset_expr,
2305 likely ? "bltzl" : "bltz",
2306 "s,p", sreg);
2307 return;
2308 }
2309 if (sreg == 0)
2310 {
2311 macro_build ((char *) NULL, &icnt, &offset_expr,
2312 likely ? "bgtzl" : "bgtz",
2313 "s,p", treg);
2314 return;
2315 }
2316 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
2317 macro_build ((char *) NULL, &icnt, &offset_expr,
2318 likely ? "bnel" : "bne",
2319 "s,t,p", AT, 0);
2320 break;
2321
2322 case M_BLTUL:
2323 likely = 1;
2324 case M_BLTU:
2325 if (treg == 0)
2326 goto do_false;
2327 if (sreg == 0)
2328 {
2329 macro_build ((char *) NULL, &icnt, &offset_expr,
2330 likely ? "bnel" : "bne",
2331 "s,t,p", 0, treg);
2332 return;
2333 }
2334 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
2335 treg);
2336 macro_build ((char *) NULL, &icnt, &offset_expr,
2337 likely ? "bnel" : "bne",
2338 "s,t,p", AT, 0);
2339 break;
2340
2341 case M_DDIV_3:
2342 dbl = 1;
2343 case M_DIV_3:
2344 s = "mflo";
2345 goto do_div3;
2346 case M_DREM_3:
2347 dbl = 1;
2348 case M_REM_3:
2349 s = "mfhi";
2350 do_div3:
2351 if (treg == 0)
2352 {
2353 as_warn ("Divide by zero.");
2354 if (mips_trap)
2355 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
2356 else
2357 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
2358 return;
2359 }
2360
2361 mips_emit_delays ();
2362 ++mips_noreorder;
2363 mips_any_noreorder = 1;
2364 macro_build ((char *) NULL, &icnt, NULL,
2365 dbl ? "ddiv" : "div",
2366 "z,s,t", sreg, treg);
2367 if (mips_trap)
2368 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
2369 else
2370 {
2371 expr1.X_add_number = 8;
2372 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2373 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
2374 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
2375 }
2376 expr1.X_add_number = -1;
2377 macro_build ((char *) NULL, &icnt, &expr1,
2378 dbl ? "daddiu" : "addiu",
2379 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
2380 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
2381 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
2382 if (dbl)
2383 {
2384 expr1.X_add_number = 1;
2385 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
2386 (int) BFD_RELOC_LO16);
2387 macro_build ((char *) NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT,
2388 31);
2389 }
2390 else
2391 {
2392 expr1.X_add_number = 0x80000000;
2393 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
2394 (int) BFD_RELOC_HI16);
2395 }
2396 if (mips_trap)
2397 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", sreg, AT);
2398 else
2399 {
2400 expr1.X_add_number = 8;
2401 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
2402 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
2403 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
2404 }
2405 --mips_noreorder;
2406 macro_build ((char *) NULL, &icnt, NULL, s, "d", dreg);
2407 break;
2408
2409 case M_DIV_3I:
2410 s = "div";
2411 s2 = "mflo";
2412 goto do_divi;
2413 case M_DIVU_3I:
2414 s = "divu";
2415 s2 = "mflo";
2416 goto do_divi;
2417 case M_REM_3I:
2418 s = "div";
2419 s2 = "mfhi";
2420 goto do_divi;
2421 case M_REMU_3I:
2422 s = "divu";
2423 s2 = "mfhi";
2424 goto do_divi;
2425 case M_DDIV_3I:
2426 dbl = 1;
2427 s = "ddiv";
2428 s2 = "mflo";
2429 goto do_divi;
2430 case M_DDIVU_3I:
2431 dbl = 1;
2432 s = "ddivu";
2433 s2 = "mflo";
2434 goto do_divi;
2435 case M_DREM_3I:
2436 dbl = 1;
2437 s = "ddiv";
2438 s2 = "mfhi";
2439 goto do_divi;
2440 case M_DREMU_3I:
2441 dbl = 1;
2442 s = "ddivu";
2443 s2 = "mfhi";
2444 do_divi:
2445 if (imm_expr.X_add_number == 0)
2446 {
2447 as_warn ("Divide by zero.");
2448 if (mips_trap)
2449 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
2450 else
2451 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
2452 return;
2453 }
2454 if (imm_expr.X_add_number == 1)
2455 {
2456 if (strcmp (s2, "mflo") == 0)
2457 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg,
2458 sreg);
2459 else
2460 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
2461 return;
2462 }
2463 if (imm_expr.X_add_number == -1
2464 && s[strlen (s) - 1] != 'u')
2465 {
2466 if (strcmp (s2, "mflo") == 0)
2467 {
2468 if (dbl)
2469 macro_build ((char *) NULL, &icnt, NULL, "dneg", "d,w", dreg,
2470 sreg);
2471 else
2472 macro_build ((char *) NULL, &icnt, NULL, "neg", "d,w", dreg,
2473 sreg);
2474 }
2475 else
2476 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
2477 return;
2478 }
2479
2480 load_register (&icnt, AT, &imm_expr);
2481 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
2482 macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
2483 break;
2484
2485 case M_DIVU_3:
2486 s = "divu";
2487 s2 = "mflo";
2488 goto do_divu3;
2489 case M_REMU_3:
2490 s = "divu";
2491 s2 = "mfhi";
2492 goto do_divu3;
2493 case M_DDIVU_3:
2494 s = "ddivu";
2495 s2 = "mflo";
2496 goto do_divu3;
2497 case M_DREMU_3:
2498 s = "ddivu";
2499 s2 = "mfhi";
2500 do_divu3:
2501 mips_emit_delays ();
2502 ++mips_noreorder;
2503 mips_any_noreorder = 1;
2504 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
2505 if (mips_trap)
2506 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
2507 else
2508 {
2509 expr1.X_add_number = 8;
2510 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2511 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
2512 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
2513 }
2514 --mips_noreorder;
2515 macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
2516 return;
2517
2518 case M_LA_AB:
2519 /* Load the address of a symbol into a register. If breg is not
2520 zero, we then add a base register to it. */
2521
2522 /* When generating embedded PIC code, we permit expressions of
2523 the form
2524 la $4,foo-bar
2525 where bar is an address in the .text section. These are used
2526 when getting the addresses of functions. We don't permit
2527 X_add_number to be non-zero, because if the symbol is
2528 external the relaxing code needs to know that any addend is
2529 purely the offset to X_op_symbol. */
2530 if (mips_pic == EMBEDDED_PIC
2531 && offset_expr.X_op == O_subtract
2532 && now_seg == text_section
2533 && (offset_expr.X_op_symbol->sy_value.X_op == O_constant
2534 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == text_section
2535 : (offset_expr.X_op_symbol->sy_value.X_op == O_symbol
2536 && (S_GET_SEGMENT (offset_expr.X_op_symbol
2537 ->sy_value.X_add_symbol)
2538 == text_section)))
2539 && breg == 0
2540 && offset_expr.X_add_number == 0)
2541 {
2542 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
2543 treg, (int) BFD_RELOC_PCREL_HI16_S);
2544 macro_build ((char *) NULL, &icnt, &offset_expr,
2545 mips_isa < 3 ? "addiu" : "daddiu",
2546 "t,r,j", treg, treg, (int) BFD_RELOC_PCREL_LO16);
2547 return;
2548 }
2549
2550 if (offset_expr.X_op != O_symbol
2551 && offset_expr.X_op != O_constant)
2552 {
2553 as_bad ("expression too complex");
2554 offset_expr.X_op = O_constant;
2555 }
2556
2557 if (treg == breg)
2558 {
2559 tempreg = AT;
2560 used_at = 1;
2561 }
2562 else
2563 {
2564 tempreg = treg;
2565 used_at = 0;
2566 }
2567
2568 if (offset_expr.X_op == O_constant)
2569 load_register (&icnt, tempreg, &offset_expr);
2570 else if (mips_pic == NO_PIC)
2571 {
2572 /* If this is a reference to an GP relative symbol, we want
2573 addiu $tempreg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
2574 Otherwise we want
2575 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
2576 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
2577 If we have a constant, we need two instructions anyhow,
2578 so we may as well always use the latter form. */
2579 if (offset_expr.X_add_number != 0)
2580 p = NULL;
2581 else
2582 {
2583 frag_grow (20);
2584 macro_build ((char *) NULL, &icnt, &offset_expr,
2585 mips_isa < 3 ? "addiu" : "daddiu",
2586 "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
2587 p = frag_var (rs_machine_dependent, 8, 0,
2588 RELAX_ENCODE (4, 8, 0, 4, 0,
2589 mips_warn_about_macros),
2590 offset_expr.X_add_symbol, (long) 0,
2591 (char *) NULL);
2592 }
2593 macro_build_lui (p, &icnt, &offset_expr, tempreg);
2594 if (p != NULL)
2595 p += 4;
2596 macro_build (p, &icnt, &offset_expr,
2597 mips_isa < 3 ? "addiu" : "daddiu",
2598 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
2599 }
2600 else if (mips_pic == SVR4_PIC)
2601 {
2602 /* If this is a reference to an external symbol, and there
2603 is no constant, we want
2604 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
2605 For a local symbol, we want
2606 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
2607 nop
2608 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
2609
2610 If we have a small constant, and this is a reference to
2611 an external symbol, we want
2612 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
2613 nop
2614 addiu $tempreg,$tempreg,<constant>
2615 For a local symbol, we want the same instruction
2616 sequence, but we output a BFD_RELOC_LO16 reloc on the
2617 addiu instruction.
2618
2619 If we have a large constant, and this is a reference to
2620 an external symbol, we want
2621 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
2622 lui $at,<hiconstant>
2623 addiu $at,$at,<loconstant>
2624 addu $tempreg,$tempreg,$at
2625 For a local symbol, we want the same instruction
2626 sequence, but we output a BFD_RELOC_LO16 reloc on the
2627 addiu instruction. */
2628 expr1.X_add_number = offset_expr.X_add_number;
2629 offset_expr.X_add_number = 0;
2630 frag_grow (32);
2631 macro_build ((char *) NULL, &icnt, &offset_expr,
2632 mips_isa < 3 ? "lw" : "ld",
2633 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
2634 if (expr1.X_add_number == 0)
2635 {
2636 int off;
2637
2638 if (breg == 0)
2639 off = 0;
2640 else
2641 {
2642 /* We're going to put in an addu instruction using
2643 tempreg, so we may as well insert the nop right
2644 now. */
2645 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
2646 "nop", "");
2647 off = 4;
2648 }
2649 p = frag_var (rs_machine_dependent, 8 - off, 0,
2650 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
2651 (breg == 0
2652 ? mips_warn_about_macros
2653 : 0)),
2654 offset_expr.X_add_symbol, (long) 0,
2655 (char *) NULL);
2656 if (breg == 0)
2657 {
2658 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
2659 p += 4;
2660 }
2661 macro_build (p, &icnt, &expr1,
2662 mips_isa < 3 ? "addiu" : "daddiu",
2663 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
2664 /* FIXME: If breg == 0, and the next instruction uses
2665 $tempreg, then if this variant case is used an extra
2666 nop will be generated. */
2667 }
2668 else if (expr1.X_add_number >= -0x8000
2669 && expr1.X_add_number < 0x8000)
2670 {
2671 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
2672 "nop", "");
2673 macro_build ((char *) NULL, &icnt, &expr1,
2674 mips_isa < 3 ? "addiu" : "daddiu",
2675 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
2676 (void) frag_var (rs_machine_dependent, 0, 0,
2677 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
2678 offset_expr.X_add_symbol, (long) 0,
2679 (char *) NULL);
2680 }
2681 else
2682 {
2683 int off1;
2684
2685 /* If we are going to add in a base register, and the
2686 target register and the base register are the same,
2687 then we are using AT as a temporary register. Since
2688 we want to load the constant into AT, we add our
2689 current AT (from the global offset table) and the
2690 register into the register now, and pretend we were
2691 not using a base register. */
2692 if (breg != treg)
2693 off1 = 0;
2694 else
2695 {
2696 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
2697 "nop", "");
2698 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
2699 mips_isa < 3 ? "addu" : "daddu",
2700 "d,v,t", treg, AT, breg);
2701 breg = 0;
2702 tempreg = treg;
2703 off1 = -8;
2704 }
2705
2706 /* Set mips_optimize around the lui instruction to avoid
2707 inserting an unnecessary nop after the lw. */
2708 hold_mips_optimize = mips_optimize;
2709 mips_optimize = 2;
2710 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
2711 mips_optimize = hold_mips_optimize;
2712
2713 macro_build ((char *) NULL, &icnt, &expr1,
2714 mips_isa < 3 ? "addiu" : "daddiu",
2715 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
2716 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
2717 mips_isa < 3 ? "addu" : "daddu",
2718 "d,v,t", tempreg, tempreg, AT);
2719 (void) frag_var (rs_machine_dependent, 0, 0,
2720 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
2721 offset_expr.X_add_symbol, (long) 0,
2722 (char *) NULL);
2723 used_at = 1;
2724 }
2725 }
2726 else if (mips_pic == EMBEDDED_PIC)
2727 {
2728 /* We use
2729 addiu $tempreg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
2730 */
2731 macro_build ((char *) NULL, &icnt, &offset_expr,
2732 mips_isa < 3 ? "addiu" : "daddiu",
2733 "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
2734 }
2735 else
2736 abort ();
2737
2738 if (breg != 0)
2739 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
2740 mips_isa < 3 ? "addu" : "daddu",
2741 "d,v,t", treg, tempreg, breg);
2742
2743 if (! used_at)
2744 return;
2745
2746 break;
2747
2748 case M_J_A:
2749 /* The j instruction may not be used in PIC code, since it
2750 requires an absolute address. We convert it to a b
2751 instruction. */
2752 if (mips_pic == NO_PIC)
2753 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
2754 else
2755 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
2756 return;
2757
2758 /* The jal instructions must be handled as macros because when
2759 generating PIC code they expand to multi-instruction
2760 sequences. Normally they are simple instructions. */
2761 case M_JAL_1:
2762 dreg = RA;
2763 /* Fall through. */
2764 case M_JAL_2:
2765 if (mips_pic == NO_PIC
2766 || mips_pic == EMBEDDED_PIC)
2767 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
2768 "d,s", dreg, sreg);
2769 else if (mips_pic == SVR4_PIC)
2770 {
2771 if (sreg != PIC_CALL_REG)
2772 as_warn ("MIPS PIC call to register other than $25");
2773
2774 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
2775 "d,s", dreg, sreg);
2776 if (mips_cprestore_offset < 0)
2777 as_warn ("No .cprestore pseudo-op used in PIC code");
2778 else
2779 {
2780 expr1.X_add_number = mips_cprestore_offset;
2781 macro_build ((char *) NULL, &icnt, &expr1,
2782 mips_isa < 3 ? "lw" : "ld",
2783 "t,o(b)", GP, (int) BFD_RELOC_LO16, mips_frame_reg);
2784 }
2785 }
2786 else
2787 abort ();
2788
2789 return;
2790
2791 case M_JAL_A:
2792 if (mips_pic == NO_PIC)
2793 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
2794 else if (mips_pic == SVR4_PIC)
2795 {
2796 /* If this is a reference to an external symbol, we want
2797 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
2798 nop
2799 jalr $25
2800 nop
2801 lw $gp,cprestore($sp)
2802 The cprestore value is set using the .cprestore
2803 pseudo-op. If the symbol is not external, we want
2804 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
2805 nop
2806 addiu $25,$25,<sym> (BFD_RELOC_LO16)
2807 jalr $25
2808 nop
2809 lw $gp,cprestore($sp)
2810 */
2811 frag_grow (20);
2812 macro_build ((char *) NULL, &icnt, &offset_expr,
2813 mips_isa < 3 ? "lw" : "ld",
2814 "t,o(b)", PIC_CALL_REG,
2815 (int) BFD_RELOC_MIPS_CALL16, GP);
2816 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
2817 p = frag_var (rs_machine_dependent, 4, 0,
2818 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
2819 offset_expr.X_add_symbol, (long) 0, (char *) NULL);
2820 macro_build (p, &icnt, &offset_expr,
2821 mips_isa < 3 ? "addiu" : "daddiu",
2822 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
2823 (int) BFD_RELOC_LO16);
2824 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
2825 "jalr", "s", PIC_CALL_REG);
2826 if (mips_cprestore_offset < 0)
2827 as_warn ("No .cprestore pseudo-op used in PIC code");
2828 else
2829 {
2830 if (mips_noreorder)
2831 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
2832 "nop", "");
2833 expr1.X_add_number = mips_cprestore_offset;
2834 macro_build ((char *) NULL, &icnt, &expr1,
2835 mips_isa < 3 ? "lw" : "ld",
2836 "t,o(b)", GP, (int) BFD_RELOC_LO16,
2837 mips_frame_reg);
2838 }
2839 }
2840 else if (mips_pic == EMBEDDED_PIC)
2841 {
2842 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
2843 /* The linker may expand the call to a longer sequence which
2844 uses $at, so we must break rather than return. */
2845 break;
2846 }
2847 else
2848 abort ();
2849
2850 return;
2851
2852 case M_LB_AB:
2853 s = "lb";
2854 goto ld;
2855 case M_LBU_AB:
2856 s = "lbu";
2857 goto ld;
2858 case M_LH_AB:
2859 s = "lh";
2860 goto ld;
2861 case M_LHU_AB:
2862 s = "lhu";
2863 goto ld;
2864 case M_LW_AB:
2865 s = "lw";
2866 goto ld;
2867 case M_LWC0_AB:
2868 s = "lwc0";
2869 coproc = 1;
2870 goto ld;
2871 case M_LWC1_AB:
2872 s = "lwc1";
2873 coproc = 1;
2874 goto ld;
2875 case M_LWC2_AB:
2876 s = "lwc2";
2877 coproc = 1;
2878 goto ld;
2879 case M_LWC3_AB:
2880 s = "lwc3";
2881 coproc = 1;
2882 goto ld;
2883 case M_LWL_AB:
2884 s = "lwl";
2885 lr = 1;
2886 goto ld;
2887 case M_LWR_AB:
2888 s = "lwr";
2889 lr = 1;
2890 goto ld;
2891 case M_LDC1_AB:
2892 s = "ldc1";
2893 coproc = 1;
2894 goto ld;
2895 case M_LDC2_AB:
2896 s = "ldc2";
2897 coproc = 1;
2898 goto ld;
2899 case M_LDC3_AB:
2900 s = "ldc3";
2901 coproc = 1;
2902 goto ld;
2903 case M_LDL_AB:
2904 s = "ldl";
2905 lr = 1;
2906 goto ld;
2907 case M_LDR_AB:
2908 s = "ldr";
2909 lr = 1;
2910 goto ld;
2911 case M_LL_AB:
2912 s = "ll";
2913 goto ld;
2914 case M_LLD_AB:
2915 s = "lld";
2916 goto ld;
2917 case M_LWU_AB:
2918 s = "lwu";
2919 ld:
2920 if (breg == treg || coproc || lr)
2921 {
2922 tempreg = AT;
2923 used_at = 1;
2924 }
2925 else
2926 {
2927 tempreg = treg;
2928 used_at = 0;
2929 }
2930 goto ld_st;
2931 case M_SB_AB:
2932 s = "sb";
2933 goto st;
2934 case M_SH_AB:
2935 s = "sh";
2936 goto st;
2937 case M_SW_AB:
2938 s = "sw";
2939 goto st;
2940 case M_SWC0_AB:
2941 s = "swc0";
2942 coproc = 1;
2943 goto st;
2944 case M_SWC1_AB:
2945 s = "swc1";
2946 coproc = 1;
2947 goto st;
2948 case M_SWC2_AB:
2949 s = "swc2";
2950 coproc = 1;
2951 goto st;
2952 case M_SWC3_AB:
2953 s = "swc3";
2954 coproc = 1;
2955 goto st;
2956 case M_SWL_AB:
2957 s = "swl";
2958 goto st;
2959 case M_SWR_AB:
2960 s = "swr";
2961 goto st;
2962 case M_SC_AB:
2963 s = "sc";
2964 goto st;
2965 case M_SCD_AB:
2966 s = "scd";
2967 goto st;
2968 case M_SDC1_AB:
2969 s = "sdc1";
2970 coproc = 1;
2971 goto st;
2972 case M_SDC2_AB:
2973 s = "sdc2";
2974 coproc = 1;
2975 goto st;
2976 case M_SDC3_AB:
2977 s = "sdc3";
2978 coproc = 1;
2979 goto st;
2980 case M_SDL_AB:
2981 s = "sdl";
2982 goto st;
2983 case M_SDR_AB:
2984 s = "sdr";
2985 st:
2986 tempreg = AT;
2987 used_at = 1;
2988 ld_st:
2989 if (mask == M_LWC1_AB
2990 || mask == M_SWC1_AB
2991 || mask == M_LDC1_AB
2992 || mask == M_SDC1_AB
2993 || mask == M_L_DAB
2994 || mask == M_S_DAB)
2995 fmt = "T,o(b)";
2996 else if (coproc)
2997 fmt = "E,o(b)";
2998 else
2999 fmt = "t,o(b)";
3000
3001 if (offset_expr.X_op != O_constant
3002 && offset_expr.X_op != O_symbol)
3003 {
3004 as_bad ("expression too complex");
3005 offset_expr.X_op = O_constant;
3006 }
3007
3008 /* A constant expression in PIC code can be handled just as it
3009 is in non PIC code. */
3010 if (mips_pic == NO_PIC
3011 || offset_expr.X_op == O_constant)
3012 {
3013 /* If this is a reference to a GP relative symbol, and there
3014 is no base register, we want
3015 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
3016 Otherwise, if there is no base register, we want
3017 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
3018 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
3019 If we have a constant, we need two instructions anyhow,
3020 so we always use the latter form.
3021
3022 If we have a base register, and this is a reference to a
3023 GP relative symbol, we want
3024 addu $tempreg,$breg,$gp
3025 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GPREL)
3026 Otherwise we want
3027 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
3028 addu $tempreg,$tempreg,$breg
3029 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
3030 With a constant we always use the latter case. */
3031 if (breg == 0)
3032 {
3033 if (offset_expr.X_add_number != 0)
3034 p = NULL;
3035 else
3036 {
3037 frag_grow (20);
3038 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
3039 treg, (int) BFD_RELOC_MIPS_GPREL, GP);
3040 p = frag_var (rs_machine_dependent, 8, 0,
3041 RELAX_ENCODE (4, 8, 0, 4, 0,
3042 (mips_warn_about_macros
3043 || (used_at && mips_noat))),
3044 offset_expr.X_add_symbol, (long) 0,
3045 (char *) NULL);
3046 used_at = 0;
3047 }
3048 macro_build_lui (p, &icnt, &offset_expr, tempreg);
3049 if (p != NULL)
3050 p += 4;
3051 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
3052 (int) BFD_RELOC_LO16, tempreg);
3053 }
3054 else
3055 {
3056 if (offset_expr.X_add_number != 0)
3057 p = NULL;
3058 else
3059 {
3060 frag_grow (28);
3061 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3062 mips_isa < 3 ? "addu" : "daddu",
3063 "d,v,t", tempreg, breg, GP);
3064 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
3065 treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
3066 p = frag_var (rs_machine_dependent, 12, 0,
3067 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
3068 offset_expr.X_add_symbol, (long) 0,
3069 (char *) NULL);
3070 }
3071 macro_build_lui (p, &icnt, &offset_expr, tempreg);
3072 if (p != NULL)
3073 p += 4;
3074 macro_build (p, &icnt, (expressionS *) NULL,
3075 mips_isa < 3 ? "addu" : "daddu",
3076 "d,v,t", tempreg, tempreg, breg);
3077 if (p != NULL)
3078 p += 4;
3079 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
3080 (int) BFD_RELOC_LO16, tempreg);
3081 }
3082 }
3083 else if (mips_pic == SVR4_PIC)
3084 {
3085 /* If this is a reference to an external symbol, we want
3086 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3087 nop
3088 <op> $treg,0($tempreg)
3089 Otherwise we want
3090 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3091 nop
3092 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
3093 <op> $treg,0($tempreg)
3094 If there is a base register, we add it to $tempreg before
3095 the <op>. If there is a constant, we stick it in the
3096 <op> instruction. We don't handle constants larger than
3097 16 bits, because we have no way to load the upper 16 bits
3098 (actually, we could handle them for the subset of cases
3099 in which we are not using $at). */
3100 assert (offset_expr.X_op == O_symbol);
3101 expr1.X_add_number = offset_expr.X_add_number;
3102 offset_expr.X_add_number = 0;
3103 if (expr1.X_add_number < -0x8000
3104 || expr1.X_add_number >= 0x8000)
3105 as_bad ("PIC code offset overflow (max 16 signed bits)");
3106 frag_grow (20);
3107 macro_build ((char *) NULL, &icnt, &offset_expr,
3108 mips_isa < 3 ? "lw" : "ld",
3109 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
3110 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
3111 p = frag_var (rs_machine_dependent, 4, 0,
3112 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
3113 offset_expr.X_add_symbol, (long) 0,
3114 (char *) NULL);
3115 macro_build (p, &icnt, &offset_expr,
3116 mips_isa < 3 ? "addiu" : "daddiu",
3117 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
3118 if (breg != 0)
3119 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3120 mips_isa < 3 ? "addu" : "daddu",
3121 "d,v,t", tempreg, tempreg, breg);
3122 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
3123 (int) BFD_RELOC_LO16, tempreg);
3124 }
3125 else if (mips_pic == EMBEDDED_PIC)
3126 {
3127 /* If there is no base register, we want
3128 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
3129 If there is a base register, we want
3130 addu $tempreg,$breg,$gp
3131 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GPREL)
3132 */
3133 assert (offset_expr.X_op == O_symbol);
3134 if (breg == 0)
3135 {
3136 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
3137 treg, (int) BFD_RELOC_MIPS_GPREL, GP);
3138 used_at = 0;
3139 }
3140 else
3141 {
3142 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3143 mips_isa < 3 ? "addu" : "daddu",
3144 "d,v,t", tempreg, breg, GP);
3145 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
3146 treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
3147 }
3148 }
3149 else
3150 abort ();
3151
3152 if (! used_at)
3153 return;
3154
3155 break;
3156
3157 case M_LI:
3158 case M_LI_S:
3159 load_register (&icnt, treg, &imm_expr);
3160 return;
3161
3162 case M_LI_SS:
3163 if (imm_expr.X_op == O_constant)
3164 {
3165 load_register (&icnt, AT, &imm_expr);
3166 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3167 "mtc1", "t,G", AT, treg);
3168 break;
3169 }
3170 else
3171 {
3172 assert (offset_expr.X_op == O_symbol
3173 && strcmp (segment_name (S_GET_SEGMENT
3174 (offset_expr.X_add_symbol)),
3175 ".lit4") == 0
3176 && offset_expr.X_add_number == 0);
3177 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
3178 treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
3179 return;
3180 }
3181
3182 case M_LI_D:
3183 /* We know that sym is in the .rdata section. First we get the
3184 upper 16 bits of the address. */
3185 if (mips_pic == NO_PIC)
3186 {
3187 /* FIXME: This won't work for a 64 bit address. */
3188 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
3189 }
3190 else if (mips_pic == SVR4_PIC)
3191 {
3192 macro_build ((char *) NULL, &icnt, &offset_expr,
3193 mips_isa < 3 ? "lw" : "ld",
3194 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
3195 }
3196 else if (mips_pic == EMBEDDED_PIC)
3197 {
3198 /* For embedded PIC we pick up the entire address off $gp in
3199 a single instruction. */
3200 macro_build ((char *) NULL, &icnt, &offset_expr,
3201 mips_isa < 3 ? "addiu" : "daddiu",
3202 "t,r,j", AT, GP, (int) BFD_RELOC_MIPS_GPREL);
3203 offset_expr.X_op = O_constant;
3204 offset_expr.X_add_number = 0;
3205 }
3206 else
3207 abort ();
3208
3209 /* Now we load the register(s). */
3210 if (mips_isa >= 3)
3211 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
3212 treg, (int) BFD_RELOC_LO16, AT);
3213 else
3214 {
3215 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
3216 treg, (int) BFD_RELOC_LO16, AT);
3217 if (treg != 31)
3218 {
3219 /* FIXME: How in the world do we deal with the possible
3220 overflow here? */
3221 offset_expr.X_add_number += 4;
3222 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
3223 treg + 1, (int) BFD_RELOC_LO16, AT);
3224 }
3225 }
3226
3227 /* To avoid confusion in tc_gen_reloc, we must ensure that this
3228 does not become a variant frag. */
3229 frag_wane (frag_now);
3230 frag_new (0);
3231
3232 break;
3233
3234 case M_LI_DD:
3235 assert (offset_expr.X_op == O_symbol
3236 && offset_expr.X_add_number == 0);
3237 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
3238 if (strcmp (s, ".lit8") == 0)
3239 {
3240 if (mips_isa >= 2)
3241 {
3242 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
3243 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
3244 return;
3245 }
3246 breg = GP;
3247 r = BFD_RELOC_MIPS_LITERAL;
3248 goto dob;
3249 }
3250 else
3251 {
3252 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
3253 if (mips_pic == SVR4_PIC)
3254 macro_build ((char *) NULL, &icnt, &offset_expr,
3255 mips_isa < 3 ? "lw" : "ld",
3256 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
3257 else
3258 {
3259 /* FIXME: This won't work for a 64 bit address. */
3260 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
3261 }
3262
3263 if (mips_isa >= 2)
3264 {
3265 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
3266 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
3267
3268 /* To avoid confusion in tc_gen_reloc, we must ensure
3269 that this does not become a variant frag. */
3270 frag_wane (frag_now);
3271 frag_new (0);
3272
3273 break;
3274 }
3275 breg = AT;
3276 r = BFD_RELOC_LO16;
3277 goto dob;
3278 }
3279
3280 case M_L_DOB:
3281 /* Even on a big endian machine $fn comes before $fn+1. We have
3282 to adjust when loading from memory. */
3283 r = BFD_RELOC_LO16;
3284 dob:
3285 assert (mips_isa < 2);
3286 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
3287 byte_order == LITTLE_ENDIAN ? treg : treg + 1,
3288 (int) r, breg);
3289 /* FIXME: A possible overflow which I don't know how to deal
3290 with. */
3291 offset_expr.X_add_number += 4;
3292 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
3293 byte_order == LITTLE_ENDIAN ? treg + 1 : treg,
3294 (int) r, breg);
3295
3296 /* To avoid confusion in tc_gen_reloc, we must ensure that this
3297 does not become a variant frag. */
3298 frag_wane (frag_now);
3299 frag_new (0);
3300
3301 if (breg != AT)
3302 return;
3303 break;
3304
3305 case M_L_DAB:
3306 /*
3307 * The MIPS assembler seems to check for X_add_number not
3308 * being double aligned and generating:
3309 * lui at,%hi(foo+1)
3310 * addu at,at,v1
3311 * addiu at,at,%lo(foo+1)
3312 * lwc1 f2,0(at)
3313 * lwc1 f3,4(at)
3314 * But, the resulting address is the same after relocation so why
3315 * generate the extra instruction?
3316 */
3317 coproc = 1;
3318 if (mips_isa >= 2)
3319 {
3320 s = "ldc1";
3321 goto ld;
3322 }
3323
3324 s = "lwc1";
3325 fmt = "T,o(b)";
3326 goto ldd_std;
3327
3328 case M_S_DAB:
3329 if (mips_isa >= 2)
3330 {
3331 s = "sdc1";
3332 goto st;
3333 }
3334
3335 s = "swc1";
3336 fmt = "T,o(b)";
3337 coproc = 1;
3338 goto ldd_std;
3339
3340 case M_LD_AB:
3341 if (mips_isa >= 3)
3342 {
3343 s = "ld";
3344 goto ld;
3345 }
3346
3347 s = "lw";
3348 fmt = "t,o(b)";
3349 goto ldd_std;
3350
3351 case M_SD_AB:
3352 if (mips_isa >= 3)
3353 {
3354 s = "sd";
3355 goto st;
3356 }
3357
3358 s = "sw";
3359 fmt = "t,o(b)";
3360
3361 ldd_std:
3362 if (offset_expr.X_op != O_symbol
3363 && offset_expr.X_op != O_constant)
3364 {
3365 as_bad ("expression too complex");
3366 offset_expr.X_op = O_constant;
3367 }
3368
3369 /* Even on a big endian machine $fn comes before $fn+1. We have
3370 to adjust when loading from memory. We set coproc if we must
3371 load $fn+1 first. */
3372 if (byte_order == LITTLE_ENDIAN)
3373 coproc = 0;
3374
3375 if (mips_pic == NO_PIC
3376 || offset_expr.X_op == O_constant)
3377 {
3378 /* If this is a reference to a GP relative symbol, we want
3379 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
3380 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
3381 If we have a base register, we use this
3382 addu $at,$breg,$gp
3383 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
3384 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
3385 If this is not a GP relative symbol, we want
3386 lui $at,<sym> (BFD_RELOC_HI16_S)
3387 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
3388 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
3389 If there is a base register, we add it to $at after the
3390 lui instruction. If there is a constant, we always use
3391 the last case. */
3392 if (offset_expr.X_add_number != 0)
3393 {
3394 p = NULL;
3395 used_at = 1;
3396 }
3397 else
3398 {
3399 int off;
3400
3401 if (breg == 0)
3402 {
3403 frag_grow (28);
3404 tempreg = GP;
3405 off = 0;
3406 used_at = 0;
3407 }
3408 else
3409 {
3410 frag_grow (36);
3411 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3412 mips_isa < 3 ? "addu" : "daddu",
3413 "d,v,t", AT, breg, GP);
3414 tempreg = AT;
3415 off = 4;
3416 used_at = 1;
3417 }
3418
3419 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
3420 coproc ? treg + 1 : treg,
3421 (int) BFD_RELOC_MIPS_GPREL, tempreg);
3422 offset_expr.X_add_number += 4;
3423
3424 /* Set mips_optimize to 2 to avoid inserting an
3425 undesired nop. */
3426 hold_mips_optimize = mips_optimize;
3427 mips_optimize = 2;
3428 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
3429 coproc ? treg : treg + 1,
3430 (int) BFD_RELOC_MIPS_GPREL, tempreg);
3431 mips_optimize = hold_mips_optimize;
3432
3433 p = frag_var (rs_machine_dependent, 12 + off, 0,
3434 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
3435 used_at && mips_noat),
3436 offset_expr.X_add_symbol, (long) 0,
3437 (char *) NULL);
3438
3439 /* We just generated two relocs. When tc_gen_reloc
3440 handles this case, it will skip the first reloc and
3441 handle the second. The second reloc already has an
3442 extra addend of 4, which we added above. We must
3443 subtract it out, and then subtract another 4 to make
3444 the first reloc come out right. The second reloc
3445 will come out right because we are going to add 4 to
3446 offset_expr when we build its instruction below. */
3447 offset_expr.X_add_number -= 8;
3448 offset_expr.X_op = O_constant;
3449 }
3450 macro_build_lui (p, &icnt, &offset_expr, AT);
3451 if (p != NULL)
3452 p += 4;
3453 if (breg != 0)
3454 {
3455 macro_build (p, &icnt, (expressionS *) NULL,
3456 mips_isa < 3 ? "addu" : "daddu",
3457 "d,v,t", AT, breg, AT);
3458 if (p != NULL)
3459 p += 4;
3460 }
3461 macro_build (p, &icnt, &offset_expr, s, fmt,
3462 coproc ? treg + 1 : treg,
3463 (int) BFD_RELOC_LO16, AT);
3464 if (p != NULL)
3465 p += 4;
3466 /* FIXME: How do we handle overflow here? */
3467 offset_expr.X_add_number += 4;
3468 macro_build (p, &icnt, &offset_expr, s, fmt,
3469 coproc ? treg : treg + 1,
3470 (int) BFD_RELOC_LO16, AT);
3471 }
3472 else if (mips_pic == SVR4_PIC)
3473 {
3474 int off;
3475
3476 /* If this is a reference to an external symbol, we want
3477 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3478 nop
3479 <op> $treg,0($at)
3480 <op> $treg+1,4($at)
3481 Otherwise we want
3482 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3483 nop
3484 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
3485 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
3486 If there is a base register we add it to $at before the
3487 lwc1 instructions. If there is a constant we include it
3488 in the lwc1 instructions. */
3489 used_at = 1;
3490 expr1.X_add_number = offset_expr.X_add_number;
3491 offset_expr.X_add_number = 0;
3492 if (expr1.X_add_number < -0x8000
3493 || expr1.X_add_number >= 0x8000 - 4)
3494 as_bad ("PIC code offset overflow (max 16 signed bits)");
3495 if (breg == 0)
3496 off = 0;
3497 else
3498 off = 4;
3499 frag_grow (24 + off);
3500 macro_build ((char *) NULL, &icnt, &offset_expr,
3501 mips_isa < 3 ? "lw" : "ld",
3502 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
3503 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
3504 if (breg != 0)
3505 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3506 mips_isa < 3 ? "addu" : "daddu",
3507 "d,v,t", AT, breg, AT);
3508 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
3509 coproc ? treg + 1 : treg,
3510 (int) BFD_RELOC_LO16, AT);
3511 expr1.X_add_number += 4;
3512
3513 /* Set mips_optimize to 2 to avoid inserting an undesired
3514 nop. */
3515 hold_mips_optimize = mips_optimize;
3516 mips_optimize = 2;
3517 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
3518 coproc ? treg : treg + 1,
3519 (int) BFD_RELOC_LO16, AT);
3520 mips_optimize = hold_mips_optimize;
3521
3522 (void) frag_var (rs_machine_dependent, 0, 0,
3523 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
3524 offset_expr.X_add_symbol, (long) 0,
3525 (char *) NULL);
3526 }
3527 else if (mips_pic == EMBEDDED_PIC)
3528 {
3529 /* If there is no base register, we use
3530 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
3531 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
3532 If we have a base register, we use
3533 addu $at,$breg,$gp
3534 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
3535 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
3536 */
3537 if (breg == 0)
3538 {
3539 tempreg = GP;
3540 used_at = 0;
3541 }
3542 else
3543 {
3544 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3545 mips_isa < 3 ? "addu" : "daddu",
3546 "d,v,t", AT, breg, GP);
3547 tempreg = AT;
3548 used_at = 1;
3549 }
3550
3551 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
3552 coproc ? treg + 1 : treg,
3553 (int) BFD_RELOC_MIPS_GPREL, tempreg);
3554 offset_expr.X_add_number += 4;
3555 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
3556 coproc ? treg : treg + 1,
3557 (int) BFD_RELOC_MIPS_GPREL, tempreg);
3558 }
3559 else
3560 abort ();
3561
3562 if (! used_at)
3563 return;
3564
3565 break;
3566
3567 case M_LD_OB:
3568 s = "lw";
3569 goto sd_ob;
3570 case M_SD_OB:
3571 s = "sw";
3572 sd_ob:
3573 assert (mips_isa < 3);
3574 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
3575 (int) BFD_RELOC_LO16, breg);
3576 offset_expr.X_add_number += 4;
3577 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
3578 (int) BFD_RELOC_LO16, breg);
3579 return;
3580 #ifdef LOSING_COMPILER
3581 default:
3582 macro2 (ip);
3583 return;
3584 }
3585 if (mips_noat)
3586 as_warn ("Macro used $at after \".set noat\"");
3587 }
3588
3589 static void
3590 macro2 (ip)
3591 struct mips_cl_insn *ip;
3592 {
3593 register int treg, sreg, dreg, breg;
3594 int tempreg;
3595 int mask;
3596 int icnt = 0;
3597 int used_at;
3598 expressionS expr1;
3599 const char *s;
3600 const char *s2;
3601 const char *fmt;
3602 int likely = 0;
3603 int dbl = 0;
3604 int coproc = 0;
3605 int lr = 0;
3606 int off;
3607 offsetT maxnum;
3608 bfd_reloc_code_real_type r;
3609 char *p;
3610
3611 treg = (ip->insn_opcode >> 16) & 0x1f;
3612 dreg = (ip->insn_opcode >> 11) & 0x1f;
3613 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3614 mask = ip->insn_mo->mask;
3615
3616 expr1.X_op = O_constant;
3617 expr1.X_op_symbol = NULL;
3618 expr1.X_add_symbol = NULL;
3619 expr1.X_add_number = 1;
3620
3621 switch (mask)
3622 {
3623 #endif /* LOSING_COMPILER */
3624
3625 case M_DMUL:
3626 dbl = 1;
3627 case M_MUL:
3628 macro_build ((char *) NULL, &icnt, NULL,
3629 dbl ? "dmultu" : "multu",
3630 "s,t", sreg, treg);
3631 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
3632 return;
3633
3634 case M_DMUL_I:
3635 dbl = 1;
3636 case M_MUL_I:
3637 /* The MIPS assembler some times generates shifts and adds. I'm
3638 not trying to be that fancy. GCC should do this for us
3639 anyway. */
3640 load_register (&icnt, AT, &imm_expr);
3641 macro_build ((char *) NULL, &icnt, NULL,
3642 dbl ? "dmult" : "mult",
3643 "s,t", sreg, AT);
3644 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
3645 break;
3646
3647 case M_DMULO:
3648 dbl = 1;
3649 case M_MULO:
3650 mips_emit_delays ();
3651 ++mips_noreorder;
3652 mips_any_noreorder = 1;
3653 macro_build ((char *) NULL, &icnt, NULL,
3654 dbl ? "dmult" : "mult",
3655 "s,t", sreg, treg);
3656 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
3657 macro_build ((char *) NULL, &icnt, NULL,
3658 dbl ? "dsra32" : "sra",
3659 "d,w,<", dreg, dreg, 31);
3660 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
3661 if (mips_trap)
3662 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", dreg, AT);
3663 else
3664 {
3665 expr1.X_add_number = 8;
3666 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
3667 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3668 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
3669 }
3670 --mips_noreorder;
3671 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
3672 break;
3673
3674 case M_DMULOU:
3675 dbl = 1;
3676 case M_MULOU:
3677 mips_emit_delays ();
3678 ++mips_noreorder;
3679 mips_any_noreorder = 1;
3680 macro_build ((char *) NULL, &icnt, NULL,
3681 dbl ? "dmultu" : "multu",
3682 "s,t", sreg, treg);
3683 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
3684 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
3685 if (mips_trap)
3686 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", AT, 0);
3687 else
3688 {
3689 expr1.X_add_number = 8;
3690 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
3691 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3692 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
3693 }
3694 --mips_noreorder;
3695 break;
3696
3697 case M_ROL:
3698 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
3699 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
3700 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg,
3701 treg);
3702 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
3703 break;
3704
3705 case M_ROL_I:
3706 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg,
3707 imm_expr.X_add_number & 0x1f);
3708 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
3709 (0 - imm_expr.X_add_number) & 0x1f);
3710 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
3711 break;
3712
3713 case M_ROR:
3714 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
3715 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
3716 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg,
3717 treg);
3718 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
3719 break;
3720
3721 case M_ROR_I:
3722 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg,
3723 imm_expr.X_add_number & 0x1f);
3724 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
3725 (0 - imm_expr.X_add_number) & 0x1f);
3726 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
3727 break;
3728
3729 case M_S_DOB:
3730 assert (mips_isa < 2);
3731 /* Even on a big endian machine $fn comes before $fn+1. We have
3732 to adjust when storing to memory. */
3733 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
3734 byte_order == LITTLE_ENDIAN ? treg : treg + 1,
3735 (int) BFD_RELOC_LO16, breg);
3736 offset_expr.X_add_number += 4;
3737 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
3738 byte_order == LITTLE_ENDIAN ? treg + 1 : treg,
3739 (int) BFD_RELOC_LO16, breg);
3740 return;
3741
3742 case M_SEQ:
3743 if (sreg == 0)
3744 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
3745 treg, (int) BFD_RELOC_LO16);
3746 else if (treg == 0)
3747 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
3748 sreg, (int) BFD_RELOC_LO16);
3749 else
3750 {
3751 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
3752 sreg, treg);
3753 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
3754 dreg, (int) BFD_RELOC_LO16);
3755 }
3756 return;
3757
3758 case M_SEQ_I:
3759 if (imm_expr.X_add_number == 0)
3760 {
3761 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
3762 sreg, (int) BFD_RELOC_LO16);
3763 return;
3764 }
3765 if (sreg == 0)
3766 {
3767 as_warn ("Instruction %s: result is always false",
3768 ip->insn_mo->name);
3769 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
3770 return;
3771 }
3772 if (imm_expr.X_add_number >= 0 && imm_expr.X_add_number < 0x10000)
3773 {
3774 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
3775 sreg, (int) BFD_RELOC_LO16);
3776 used_at = 0;
3777 }
3778 else if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number < 0)
3779 {
3780 imm_expr.X_add_number = -imm_expr.X_add_number;
3781 macro_build ((char *) NULL, &icnt, &imm_expr,
3782 mips_isa < 3 ? "addiu" : "daddiu",
3783 "t,r,j", dreg, sreg,
3784 (int) BFD_RELOC_LO16);
3785 used_at = 0;
3786 }
3787 else
3788 {
3789 load_register (&icnt, AT, &imm_expr);
3790 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
3791 sreg, AT);
3792 used_at = 1;
3793 }
3794 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
3795 (int) BFD_RELOC_LO16);
3796 if (used_at)
3797 break;
3798 return;
3799
3800 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
3801 s = "slt";
3802 goto sge;
3803 case M_SGEU:
3804 s = "sltu";
3805 sge:
3806 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
3807 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
3808 (int) BFD_RELOC_LO16);
3809 return;
3810
3811 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
3812 case M_SGEU_I:
3813 if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
3814 {
3815 macro_build ((char *) NULL, &icnt, &expr1,
3816 mask == M_SGE_I ? "slti" : "sltiu",
3817 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
3818 used_at = 0;
3819 }
3820 else
3821 {
3822 load_register (&icnt, AT, &imm_expr);
3823 macro_build ((char *) NULL, &icnt, NULL,
3824 mask == M_SGE_I ? "slt" : "sltu",
3825 "d,v,t", dreg, sreg, AT);
3826 used_at = 1;
3827 }
3828 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
3829 (int) BFD_RELOC_LO16);
3830 if (used_at)
3831 break;
3832 return;
3833
3834 case M_SGT: /* sreg > treg <==> treg < sreg */
3835 s = "slt";
3836 goto sgt;
3837 case M_SGTU:
3838 s = "sltu";
3839 sgt:
3840 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
3841 return;
3842
3843 case M_SGT_I: /* sreg > I <==> I < sreg */
3844 s = "slt";
3845 goto sgti;
3846 case M_SGTU_I:
3847 s = "sltu";
3848 sgti:
3849 load_register (&icnt, AT, &imm_expr);
3850 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
3851 break;
3852
3853 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
3854 s = "slt";
3855 goto sle;
3856 case M_SLEU:
3857 s = "sltu";
3858 sle:
3859 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
3860 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
3861 (int) BFD_RELOC_LO16);
3862 return;
3863
3864 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
3865 s = "slt";
3866 goto slei;
3867 case M_SLEU_I:
3868 s = "sltu";
3869 slei:
3870 load_register (&icnt, AT, &imm_expr);
3871 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
3872 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
3873 (int) BFD_RELOC_LO16);
3874 break;
3875
3876 case M_SLT_I:
3877 if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
3878 {
3879 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
3880 dreg, sreg, (int) BFD_RELOC_LO16);
3881 return;
3882 }
3883 load_register (&icnt, AT, &imm_expr);
3884 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
3885 break;
3886
3887 case M_SLTU_I:
3888 if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
3889 {
3890 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
3891 dreg, sreg, (int) BFD_RELOC_LO16);
3892 return;
3893 }
3894 load_register (&icnt, AT, &imm_expr);
3895 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg,
3896 AT);
3897 break;
3898
3899 case M_SNE:
3900 if (sreg == 0)
3901 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
3902 treg);
3903 else if (treg == 0)
3904 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
3905 sreg);
3906 else
3907 {
3908 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
3909 sreg, treg);
3910 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
3911 dreg);
3912 }
3913 return;
3914
3915 case M_SNE_I:
3916 if (imm_expr.X_add_number == 0)
3917 {
3918 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
3919 sreg);
3920 return;
3921 }
3922 if (sreg == 0)
3923 {
3924 as_warn ("Instruction %s: result is always true",
3925 ip->insn_mo->name);
3926 macro_build ((char *) NULL, &icnt, &expr1,
3927 mips_isa < 3 ? "addiu" : "daddiu",
3928 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
3929 return;
3930 }
3931 if (imm_expr.X_add_number >= 0 && imm_expr.X_add_number < 0x10000)
3932 {
3933 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
3934 dreg, sreg, (int) BFD_RELOC_LO16);
3935 used_at = 0;
3936 }
3937 else if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number < 0)
3938 {
3939 imm_expr.X_add_number = -imm_expr.X_add_number;
3940 macro_build ((char *) NULL, &icnt, &imm_expr,
3941 mips_isa < 3 ? "addiu" : "daddiu",
3942 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
3943 used_at = 0;
3944 }
3945 else
3946 {
3947 load_register (&icnt, AT, &imm_expr);
3948 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
3949 sreg, AT);
3950 used_at = 1;
3951 }
3952 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
3953 if (used_at)
3954 break;
3955 return;
3956
3957 case M_DSUB_I:
3958 dbl = 1;
3959 case M_SUB_I:
3960 if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number <= 0x8000)
3961 {
3962 imm_expr.X_add_number = -imm_expr.X_add_number;
3963 macro_build ((char *) NULL, &icnt, &imm_expr,
3964 dbl ? "daddi" : "addi",
3965 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
3966 return;
3967 }
3968 load_register (&icnt, AT, &imm_expr);
3969 macro_build ((char *) NULL, &icnt, NULL,
3970 dbl ? "dsub" : "sub",
3971 "d,v,t", dreg, sreg, AT);
3972 break;
3973
3974 case M_DSUBU_I:
3975 dbl = 1;
3976 case M_SUBU_I:
3977 if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number <= 0x8000)
3978 {
3979 imm_expr.X_add_number = -imm_expr.X_add_number;
3980 macro_build ((char *) NULL, &icnt, &imm_expr,
3981 dbl ? "daddiu" : "addiu",
3982 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
3983 return;
3984 }
3985 load_register (&icnt, AT, &imm_expr);
3986 macro_build ((char *) NULL, &icnt, NULL,
3987 dbl ? "dsubu" : "subu",
3988 "d,v,t", dreg, sreg, AT);
3989 break;
3990
3991 case M_TEQ_I:
3992 s = "teq";
3993 goto trap;
3994 case M_TGE_I:
3995 s = "tge";
3996 goto trap;
3997 case M_TGEU_I:
3998 s = "tgeu";
3999 goto trap;
4000 case M_TLT_I:
4001 s = "tlt";
4002 goto trap;
4003 case M_TLTU_I:
4004 s = "tltu";
4005 goto trap;
4006 case M_TNE_I:
4007 s = "tne";
4008 trap:
4009 load_register (&icnt, AT, &imm_expr);
4010 macro_build ((char *) NULL, &icnt, NULL, s, "s,t", sreg, AT);
4011 break;
4012
4013 case M_TRUNCWD:
4014 case M_TRUNCWS:
4015 assert (mips_isa < 2);
4016 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
4017 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
4018
4019 /*
4020 * Is the double cfc1 instruction a bug in the mips assembler;
4021 * or is there a reason for it?
4022 */
4023 mips_emit_delays ();
4024 ++mips_noreorder;
4025 mips_any_noreorder = 1;
4026 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
4027 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
4028 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
4029 expr1.X_add_number = 3;
4030 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
4031 (int) BFD_RELOC_LO16);
4032 expr1.X_add_number = 2;
4033 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
4034 (int) BFD_RELOC_LO16);
4035 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", AT, 31);
4036 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
4037 macro_build ((char *) NULL, &icnt, NULL,
4038 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
4039 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", treg, 31);
4040 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
4041 --mips_noreorder;
4042 break;
4043
4044 case M_ULH:
4045 s = "lb";
4046 goto ulh;
4047 case M_ULHU:
4048 s = "lbu";
4049 ulh:
4050 if (offset_expr.X_add_number >= 0x7fff)
4051 as_bad ("operand overflow");
4052 /* avoid load delay */
4053 if (byte_order == LITTLE_ENDIAN)
4054 offset_expr.X_add_number += 1;
4055 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
4056 (int) BFD_RELOC_LO16, breg);
4057 if (byte_order == LITTLE_ENDIAN)
4058 offset_expr.X_add_number -= 1;
4059 else
4060 offset_expr.X_add_number += 1;
4061 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
4062 (int) BFD_RELOC_LO16, breg);
4063 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
4064 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
4065 break;
4066
4067 case M_ULD:
4068 s = "ldl";
4069 s2 = "ldr";
4070 off = 7;
4071 goto ulw;
4072 case M_ULW:
4073 s = "lwl";
4074 s2 = "lwr";
4075 off = 3;
4076 ulw:
4077 if (offset_expr.X_add_number >= 0x8000 - off)
4078 as_bad ("operand overflow");
4079 if (byte_order == LITTLE_ENDIAN)
4080 offset_expr.X_add_number += off;
4081 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
4082 (int) BFD_RELOC_LO16, breg);
4083 if (byte_order == LITTLE_ENDIAN)
4084 offset_expr.X_add_number -= off;
4085 else
4086 offset_expr.X_add_number += off;
4087 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
4088 (int) BFD_RELOC_LO16, breg);
4089 return;
4090
4091 case M_ULD_A:
4092 s = "ldl";
4093 s2 = "ldr";
4094 off = 7;
4095 goto ulwa;
4096 case M_ULW_A:
4097 s = "lwl";
4098 s2 = "lwr";
4099 off = 3;
4100 ulwa:
4101 load_address (&icnt, AT, &offset_expr);
4102 if (byte_order == LITTLE_ENDIAN)
4103 expr1.X_add_number = off;
4104 else
4105 expr1.X_add_number = 0;
4106 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
4107 (int) BFD_RELOC_LO16, AT);
4108 if (byte_order == LITTLE_ENDIAN)
4109 expr1.X_add_number = 0;
4110 else
4111 expr1.X_add_number = off;
4112 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
4113 (int) BFD_RELOC_LO16, AT);
4114 break;
4115
4116 case M_ULH_A:
4117 case M_ULHU_A:
4118 load_address (&icnt, AT, &offset_expr);
4119 if (byte_order == BIG_ENDIAN)
4120 expr1.X_add_number = 0;
4121 macro_build ((char *) NULL, &icnt, &expr1,
4122 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
4123 (int) BFD_RELOC_LO16, AT);
4124 if (byte_order == BIG_ENDIAN)
4125 expr1.X_add_number = 1;
4126 else
4127 expr1.X_add_number = 0;
4128 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
4129 (int) BFD_RELOC_LO16, AT);
4130 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
4131 treg, 8);
4132 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
4133 treg, AT);
4134 break;
4135
4136 case M_USH:
4137 if (offset_expr.X_add_number >= 0x7fff)
4138 as_bad ("operand overflow");
4139 if (byte_order == BIG_ENDIAN)
4140 offset_expr.X_add_number += 1;
4141 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
4142 (int) BFD_RELOC_LO16, breg);
4143 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
4144 if (byte_order == BIG_ENDIAN)
4145 offset_expr.X_add_number -= 1;
4146 else
4147 offset_expr.X_add_number += 1;
4148 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
4149 (int) BFD_RELOC_LO16, breg);
4150 break;
4151
4152 case M_USD:
4153 s = "sdl";
4154 s2 = "sdr";
4155 off = 7;
4156 goto usw;
4157 case M_USW:
4158 s = "swl";
4159 s2 = "swr";
4160 off = 3;
4161 usw:
4162 if (offset_expr.X_add_number >= 0x8000 - off)
4163 as_bad ("operand overflow");
4164 if (byte_order == LITTLE_ENDIAN)
4165 offset_expr.X_add_number += off;
4166 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
4167 (int) BFD_RELOC_LO16, breg);
4168 if (byte_order == LITTLE_ENDIAN)
4169 offset_expr.X_add_number -= off;
4170 else
4171 offset_expr.X_add_number += off;
4172 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
4173 (int) BFD_RELOC_LO16, breg);
4174 return;
4175
4176 case M_USD_A:
4177 s = "sdl";
4178 s2 = "sdr";
4179 off = 7;
4180 goto uswa;
4181 case M_USW_A:
4182 s = "swl";
4183 s2 = "swr";
4184 off = 3;
4185 uswa:
4186 load_address (&icnt, AT, &offset_expr);
4187 if (byte_order == LITTLE_ENDIAN)
4188 expr1.X_add_number = off;
4189 else
4190 expr1.X_add_number = 0;
4191 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
4192 (int) BFD_RELOC_LO16, AT);
4193 if (byte_order == LITTLE_ENDIAN)
4194 expr1.X_add_number = 0;
4195 else
4196 expr1.X_add_number = off;
4197 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
4198 (int) BFD_RELOC_LO16, AT);
4199 break;
4200
4201 case M_USH_A:
4202 load_address (&icnt, AT, &offset_expr);
4203 if (byte_order == LITTLE_ENDIAN)
4204 expr1.X_add_number = 0;
4205 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
4206 (int) BFD_RELOC_LO16, AT);
4207 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", treg,
4208 treg, 8);
4209 if (byte_order == LITTLE_ENDIAN)
4210 expr1.X_add_number = 1;
4211 else
4212 expr1.X_add_number = 0;
4213 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
4214 (int) BFD_RELOC_LO16, AT);
4215 if (byte_order == LITTLE_ENDIAN)
4216 expr1.X_add_number = 0;
4217 else
4218 expr1.X_add_number = 1;
4219 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
4220 (int) BFD_RELOC_LO16, AT);
4221 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
4222 treg, 8);
4223 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
4224 treg, AT);
4225 break;
4226
4227 default:
4228 as_bad ("Macro %s not implemented yet", ip->insn_mo->name);
4229 break;
4230 }
4231 if (mips_noat)
4232 as_warn ("Macro used $at after \".set noat\"");
4233 }
4234
4235
4236 /*
4237 This routine assembles an instruction into its binary format. As a side
4238 effect it sets one of the global variables imm_reloc or offset_reloc to the
4239 type of relocation to do if one of the operands is an address expression.
4240 */
4241 static void
4242 mips_ip (str, ip)
4243 char *str;
4244 struct mips_cl_insn *ip;
4245 {
4246 char *s;
4247 const char *args;
4248 char c;
4249 struct mips_opcode *insn;
4250 char *argsStart;
4251 unsigned int regno;
4252 unsigned int lastregno = 0;
4253 char *s_reset;
4254
4255 insn_error = NULL;
4256
4257 for (s = str; islower (*s) || (*s >= '0' && *s <= '3') || *s == '.'; ++s)
4258 continue;
4259 switch (*s)
4260 {
4261 case '\0':
4262 break;
4263
4264 case ' ':
4265 *s++ = '\0';
4266 break;
4267
4268 default:
4269 as_fatal ("Unknown opcode: `%s'", str);
4270 }
4271 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
4272 {
4273 as_warn ("`%s' not in hash table.", str);
4274 insn_error = "ERROR: Unrecognized opcode";
4275 return;
4276 }
4277 argsStart = s;
4278 for (;;)
4279 {
4280 int insn_isa;
4281
4282 assert (strcmp (insn->name, str) == 0);
4283
4284 if (insn->pinfo == INSN_MACRO)
4285 insn_isa = insn->match;
4286 else if ((insn->pinfo & INSN_ISA) == INSN_ISA2)
4287 insn_isa = 2;
4288 else if ((insn->pinfo & INSN_ISA) == INSN_ISA3)
4289 insn_isa = 3;
4290 else
4291 insn_isa = 1;
4292
4293 if (insn_isa > mips_isa
4294 || ((insn->pinfo & INSN_ISA) == INSN_4650
4295 && ! mips_4650))
4296 {
4297 if (insn + 1 < &mips_opcodes[NUMOPCODES]
4298 && strcmp (insn->name, insn[1].name) == 0)
4299 {
4300 ++insn;
4301 continue;
4302 }
4303 as_warn ("Instruction not supported on this processor");
4304 }
4305
4306 ip->insn_mo = insn;
4307 ip->insn_opcode = insn->match;
4308 for (args = insn->args;; ++args)
4309 {
4310 if (*s == ' ')
4311 ++s;
4312 switch (*args)
4313 {
4314 case '\0': /* end of args */
4315 if (*s == '\0')
4316 return;
4317 break;
4318
4319 case ',':
4320 if (*s++ == *args)
4321 continue;
4322 s--;
4323 switch (*++args)
4324 {
4325 case 'r':
4326 case 'v':
4327 ip->insn_opcode |= lastregno << 21;
4328 continue;
4329
4330 case 'w':
4331 case 'W':
4332 ip->insn_opcode |= lastregno << 16;
4333 continue;
4334
4335 case 'V':
4336 ip->insn_opcode |= lastregno << 11;
4337 continue;
4338 }
4339 break;
4340
4341 case '(':
4342 /* handle optional base register.
4343 Either the base register is omitted or
4344 we must have a left paren. */
4345 /* this is dependent on the next operand specifier
4346 is a 'b' for base register */
4347 assert (args[1] == 'b');
4348 if (*s == '\0')
4349 return;
4350
4351 case ')': /* these must match exactly */
4352 if (*s++ == *args)
4353 continue;
4354 break;
4355
4356 case '<': /* must be at least one digit */
4357 /*
4358 * According to the manual, if the shift amount is greater
4359 * than 31 or less than 0 the the shift amount should be
4360 * mod 32. In reality the mips assembler issues an error.
4361 * We issue a warning and mask out all but the low 5 bits.
4362 */
4363 my_getExpression (&imm_expr, s);
4364 check_absolute_expr (ip, &imm_expr);
4365 if ((unsigned long) imm_expr.X_add_number > 31)
4366 {
4367 as_warn ("Improper shift amount (%ld)",
4368 (long) imm_expr.X_add_number);
4369 imm_expr.X_add_number = imm_expr.X_add_number & 0x1f;
4370 }
4371 ip->insn_opcode |= imm_expr.X_add_number << 6;
4372 imm_expr.X_op = O_absent;
4373 s = expr_end;
4374 continue;
4375
4376 case '>': /* shift amount minus 32 */
4377 my_getExpression (&imm_expr, s);
4378 check_absolute_expr (ip, &imm_expr);
4379 if ((unsigned long) imm_expr.X_add_number < 32
4380 || (unsigned long) imm_expr.X_add_number > 63)
4381 break;
4382 ip->insn_opcode |= (imm_expr.X_add_number - 32) << 6;
4383 imm_expr.X_op = O_absent;
4384 s = expr_end;
4385 continue;
4386
4387 case 'k': /* cache code */
4388 my_getExpression (&imm_expr, s);
4389 check_absolute_expr (ip, &imm_expr);
4390 if ((unsigned long) imm_expr.X_add_number > 31)
4391 {
4392 as_warn ("Invalid cahce opcode (%lu)",
4393 (unsigned long) imm_expr.X_add_number);
4394 imm_expr.X_add_number &= 0x1f;
4395 }
4396 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
4397 imm_expr.X_op = O_absent;
4398 s = expr_end;
4399 continue;
4400
4401 case 'c': /* break code */
4402 my_getExpression (&imm_expr, s);
4403 check_absolute_expr (ip, &imm_expr);
4404 if ((unsigned) imm_expr.X_add_number > 1023)
4405 as_warn ("Illegal break code (%ld)",
4406 (long) imm_expr.X_add_number);
4407 ip->insn_opcode |= imm_expr.X_add_number << 16;
4408 imm_expr.X_op = O_absent;
4409 s = expr_end;
4410 continue;
4411
4412 case 'B': /* syscall code */
4413 my_getExpression (&imm_expr, s);
4414 check_absolute_expr (ip, &imm_expr);
4415 if ((unsigned) imm_expr.X_add_number > 0xfffff)
4416 as_warn ("Illegal syscall code (%ld)",
4417 (long) imm_expr.X_add_number);
4418 ip->insn_opcode |= imm_expr.X_add_number << 6;
4419 imm_expr.X_op = O_absent;
4420 s = expr_end;
4421 continue;
4422
4423 case 'C': /* Coprocessor code */
4424 my_getExpression (&imm_expr, s);
4425 check_absolute_expr (ip, &imm_expr);
4426 if ((unsigned long) imm_expr.X_add_number >= (1<<25))
4427 {
4428 as_warn ("Coproccesor code > 25 bits (%ld)",
4429 (long) imm_expr.X_add_number);
4430 imm_expr.X_add_number &= ((1<<25) - 1);
4431 }
4432 ip->insn_opcode |= imm_expr.X_add_number;
4433 imm_expr.X_op = O_absent;
4434 s = expr_end;
4435 continue;
4436
4437 case 'b': /* base register */
4438 case 'd': /* destination register */
4439 case 's': /* source register */
4440 case 't': /* target register */
4441 case 'r': /* both target and source */
4442 case 'v': /* both dest and source */
4443 case 'w': /* both dest and target */
4444 case 'E': /* coprocessor target register */
4445 case 'G': /* coprocessor destination register */
4446 case 'x': /* ignore register name */
4447 case 'z': /* must be zero register */
4448 s_reset = s;
4449 if (s[0] == '$')
4450 {
4451 if (isdigit (s[1]))
4452 {
4453 ++s;
4454 regno = 0;
4455 do
4456 {
4457 regno *= 10;
4458 regno += *s - '0';
4459 ++s;
4460 }
4461 while (isdigit (*s));
4462 if (regno > 31)
4463 as_bad ("Invalid register number (%d)", regno);
4464 }
4465 else if (*args == 'E' || *args == 'G')
4466 goto notreg;
4467 else
4468 {
4469 if (s[1] == 'f' && s[2] == 'p')
4470 {
4471 s += 3;
4472 regno = FP;
4473 }
4474 else if (s[1] == 's' && s[2] == 'p')
4475 {
4476 s += 3;
4477 regno = SP;
4478 }
4479 else if (s[1] == 'g' && s[2] == 'p')
4480 {
4481 s += 3;
4482 regno = GP;
4483 }
4484 else if (s[1] == 'a' && s[2] == 't')
4485 {
4486 s += 3;
4487 regno = AT;
4488 }
4489 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
4490 {
4491 s += 4;
4492 regno = KT0;
4493 }
4494 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
4495 {
4496 s += 4;
4497 regno = KT1;
4498 }
4499 else
4500 goto notreg;
4501 }
4502 if (regno == AT && ! mips_noat)
4503 as_warn ("Used $at without \".set noat\"");
4504 c = *args;
4505 if (*s == ' ')
4506 s++;
4507 if (args[1] != *s)
4508 {
4509 if (c == 'r' || c == 'v' || c == 'w')
4510 {
4511 regno = lastregno;
4512 s = s_reset;
4513 args++;
4514 }
4515 }
4516 /* 'z' only matches $0. */
4517 if (c == 'z' && regno != 0)
4518 break;
4519 switch (c)
4520 {
4521 case 'r':
4522 case 's':
4523 case 'v':
4524 case 'b':
4525 ip->insn_opcode |= regno << 21;
4526 break;
4527 case 'd':
4528 case 'G':
4529 ip->insn_opcode |= regno << 11;
4530 break;
4531 case 'w':
4532 case 't':
4533 case 'E':
4534 ip->insn_opcode |= regno << 16;
4535 break;
4536 case 'x':
4537 /* This case exists because on the r3000 trunc
4538 expands into a macro which requires a gp
4539 register. On the r6000 or r4000 it is
4540 assembled into a single instruction which
4541 ignores the register. Thus the insn version
4542 is MIPS_ISA2 and uses 'x', and the macro
4543 version is MIPS_ISA1 and uses 't'. */
4544 break;
4545 case 'z':
4546 /* This case is for the div instruction, which
4547 acts differently if the destination argument
4548 is $0. This only matches $0, and is checked
4549 outside the switch. */
4550 break;
4551 }
4552 lastregno = regno;
4553 continue;
4554 }
4555 notreg:
4556 switch (*args++)
4557 {
4558 case 'r':
4559 case 'v':
4560 ip->insn_opcode |= lastregno << 21;
4561 continue;
4562 case 'w':
4563 ip->insn_opcode |= lastregno << 16;
4564 continue;
4565 }
4566 break;
4567
4568 case 'D': /* floating point destination register */
4569 case 'S': /* floating point source register */
4570 case 'T': /* floating point target register */
4571 case 'V':
4572 case 'W':
4573 s_reset = s;
4574 if (s[0] == '$' && s[1] == 'f' && isdigit (s[2]))
4575 {
4576 s += 2;
4577 regno = 0;
4578 do
4579 {
4580 regno *= 10;
4581 regno += *s - '0';
4582 ++s;
4583 }
4584 while (isdigit (*s));
4585
4586 if (regno > 31)
4587 as_bad ("Invalid float register number (%d)", regno);
4588
4589 if ((regno & 1) != 0
4590 && mips_isa < 3
4591 && ! (strcmp (str, "mtc1") == 0 ||
4592 strcmp (str, "mfc1") == 0 ||
4593 strcmp (str, "lwc1") == 0 ||
4594 strcmp (str, "swc1") == 0))
4595 as_warn ("Float register should be even, was %d",
4596 regno);
4597
4598 c = *args;
4599 if (*s == ' ')
4600 s++;
4601 if (args[1] != *s)
4602 {
4603 if (c == 'V' || c == 'W')
4604 {
4605 regno = lastregno;
4606 s = s_reset;
4607 args++;
4608 }
4609 }
4610 switch (c)
4611 {
4612 case 'D':
4613 ip->insn_opcode |= regno << 6;
4614 break;
4615 case 'V':
4616 case 'S':
4617 ip->insn_opcode |= regno << 11;
4618 break;
4619 case 'W':
4620 case 'T':
4621 ip->insn_opcode |= regno << 16;
4622 }
4623 lastregno = regno;
4624 continue;
4625 }
4626 switch (*args++)
4627 {
4628 case 'V':
4629 ip->insn_opcode |= lastregno << 11;
4630 continue;
4631 case 'W':
4632 ip->insn_opcode |= lastregno << 16;
4633 continue;
4634 }
4635 break;
4636
4637 case 'I':
4638 my_getExpression (&imm_expr, s);
4639 if (imm_expr.X_op != O_big)
4640 check_absolute_expr (ip, &imm_expr);
4641 s = expr_end;
4642 continue;
4643
4644 case 'A':
4645 my_getExpression (&offset_expr, s);
4646 imm_reloc = BFD_RELOC_32;
4647 s = expr_end;
4648 continue;
4649
4650 case 'F':
4651 case 'L':
4652 case 'f':
4653 case 'l':
4654 {
4655 int f64;
4656 char *save_in;
4657 char *err;
4658 unsigned char temp[8];
4659 int len;
4660 unsigned int length;
4661 segT seg;
4662 subsegT subseg;
4663 char *p;
4664
4665 /* These only appear as the last operand in an
4666 instruction, and every instruction that accepts
4667 them in any variant accepts them in all variants.
4668 This means we don't have to worry about backing out
4669 any changes if the instruction does not match.
4670
4671 The difference between them is the size of the
4672 floating point constant and where it goes. For 'F'
4673 and 'L' the constant is 64 bits; for 'f' and 'l' it
4674 is 32 bits. Where the constant is placed is based
4675 on how the MIPS assembler does things:
4676 F -- .rdata
4677 L -- .lit8
4678 f -- immediate value
4679 l -- .lit4
4680
4681 The .lit4 and .lit8 sections are only used if
4682 permitted by the -G argument.
4683
4684 When generating embedded PIC code, we use the
4685 .lit8 section but not the .lit4 section (we can do
4686 .lit4 inline easily; we need to put .lit8
4687 somewhere in the data segment, and using .lit8
4688 permits the linker to eventually combine identical
4689 .lit8 entries). */
4690
4691 f64 = *args == 'F' || *args == 'L';
4692
4693 save_in = input_line_pointer;
4694 input_line_pointer = s;
4695 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
4696 length = len;
4697 s = input_line_pointer;
4698 input_line_pointer = save_in;
4699 if (err != NULL && *err != '\0')
4700 {
4701 as_bad ("Bad floating point constant: %s", err);
4702 memset (temp, '\0', sizeof temp);
4703 length = f64 ? 8 : 4;
4704 }
4705
4706 assert (length == (f64 ? 8 : 4));
4707
4708 if (*args == 'f'
4709 || (*args == 'l'
4710 #ifdef GPOPT
4711 && (mips_pic == EMBEDDED_PIC
4712 || g_switch_value < 4)
4713 #endif
4714 ))
4715 {
4716 imm_expr.X_op = O_constant;
4717 if (byte_order == LITTLE_ENDIAN)
4718 imm_expr.X_add_number =
4719 (((((((int) temp[3] << 8)
4720 | temp[2]) << 8)
4721 | temp[1]) << 8)
4722 | temp[0]);
4723 else
4724 imm_expr.X_add_number =
4725 (((((((int) temp[0] << 8)
4726 | temp[1]) << 8)
4727 | temp[2]) << 8)
4728 | temp[3]);
4729 }
4730 else
4731 {
4732 const char *newname;
4733 segT new_seg;
4734
4735 /* Switch to the right section. */
4736 seg = now_seg;
4737 subseg = now_subseg;
4738 switch (*args)
4739 {
4740 default: /* unused default case avoids warnings. */
4741 case 'L':
4742 newname = RDATA_SECTION_NAME;
4743 #ifdef GPOPT
4744 if (g_switch_value >= 8)
4745 newname = ".lit8";
4746 #endif
4747 break;
4748 case 'F':
4749 newname = RDATA_SECTION_NAME;
4750 break;
4751 case 'l':
4752 #ifdef GPOPT
4753 assert (g_switch_value >= 4);
4754 #endif
4755 newname = ".lit4";
4756 break;
4757 }
4758 new_seg = subseg_new (newname, (subsegT) 0);
4759 frag_align (*args == 'l' ? 2 : 3, 0);
4760 #ifdef OBJ_ELF
4761 record_alignment (new_seg, 4);
4762 #else
4763 record_alignment (new_seg, *args == 'l' ? 2 : 3);
4764 #endif
4765 if (seg == now_seg)
4766 as_bad ("Can't use floating point insn in this section");
4767
4768 /* Set the argument to the current address in the
4769 section. */
4770 offset_expr.X_op = O_symbol;
4771 offset_expr.X_add_symbol =
4772 symbol_new ("L0\001", now_seg,
4773 (valueT) frag_now_fix (), frag_now);
4774 offset_expr.X_add_number = 0;
4775
4776 /* Put the floating point number into the section. */
4777 p = frag_more ((int) length);
4778 memcpy (p, temp, length);
4779
4780 /* Switch back to the original section. */
4781 subseg_set (seg, subseg);
4782 }
4783 }
4784 continue;
4785
4786 case 'i': /* 16 bit unsigned immediate */
4787 case 'j': /* 16 bit signed immediate */
4788 imm_reloc = BFD_RELOC_LO16;
4789 c = my_getSmallExpression (&imm_expr, s);
4790 if (c)
4791 {
4792 if (c != 'l')
4793 {
4794 if (imm_expr.X_op == O_constant)
4795 imm_expr.X_add_number =
4796 (imm_expr.X_add_number >> 16) & 0xffff;
4797 else if (c == 'h')
4798 imm_reloc = BFD_RELOC_HI16_S;
4799 else
4800 imm_reloc = BFD_RELOC_HI16;
4801 }
4802 }
4803 else if (imm_expr.X_op != O_big)
4804 check_absolute_expr (ip, &imm_expr);
4805 if (*args == 'i')
4806 {
4807 if (imm_expr.X_op == O_big
4808 || imm_expr.X_add_number < 0
4809 || imm_expr.X_add_number >= 0x10000)
4810 {
4811 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
4812 !strcmp (insn->name, insn[1].name))
4813 break;
4814 as_bad ("16 bit expression not in range 0..65535");
4815 }
4816 }
4817 else
4818 {
4819 int more;
4820 offsetT max;
4821
4822 /* The upper bound should be 0x8000, but
4823 unfortunately the MIPS assembler accepts numbers
4824 from 0x8000 to 0xffff and sign extends them, and
4825 we want to be compatible. We only permit this
4826 extended range for an instruction which does not
4827 provide any further alternates, since those
4828 alternates may handle other cases. People should
4829 use the numbers they mean, rather than relying on
4830 a mysterious sign extension. */
4831 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
4832 strcmp (insn->name, insn[1].name) == 0);
4833 if (more)
4834 max = 0x8000;
4835 else
4836 max = 0x10000;
4837 if (imm_expr.X_op == O_big
4838 || imm_expr.X_add_number < -0x8000
4839 || imm_expr.X_add_number >= max)
4840 {
4841 if (more)
4842 break;
4843 as_bad ("16 bit expression not in range -32768..32767");
4844 }
4845 }
4846 s = expr_end;
4847 continue;
4848
4849 case 'o': /* 16 bit offset */
4850 c = my_getSmallExpression (&offset_expr, s);
4851
4852 /* If this value won't fit into a 16 bit offset, then go
4853 find a macro that will generate the 32 bit offset
4854 code pattern. As a special hack, we accept the
4855 difference of two local symbols as a constant. This
4856 is required to suppose embedded PIC switches, which
4857 use an instruction which looks like
4858 lw $4,$L12-$LS12($4)
4859 The problem with handling this in a more general
4860 fashion is that the macro function doesn't expect to
4861 see anything which can be handled in a single
4862 constant instruction. */
4863 if (c == 0
4864 && (offset_expr.X_op != O_constant
4865 || offset_expr.X_add_number >= 0x8000
4866 || offset_expr.X_add_number < -0x8000)
4867 && (mips_pic != EMBEDDED_PIC
4868 || offset_expr.X_op != O_subtract
4869 || now_seg != text_section
4870 || (S_GET_SEGMENT (offset_expr.X_op_symbol)
4871 != text_section)))
4872 break;
4873
4874 offset_reloc = BFD_RELOC_LO16;
4875 if (c == 'h' || c == 'H')
4876 {
4877 assert (offset_expr.X_op == O_constant);
4878 offset_expr.X_add_number =
4879 (offset_expr.X_add_number >> 16) & 0xffff;
4880 }
4881 s = expr_end;
4882 continue;
4883
4884 case 'p': /* pc relative offset */
4885 offset_reloc = BFD_RELOC_16_PCREL_S2;
4886 my_getExpression (&offset_expr, s);
4887 s = expr_end;
4888 continue;
4889
4890 case 'u': /* upper 16 bits */
4891 c = my_getSmallExpression (&imm_expr, s);
4892 if (imm_expr.X_op == O_constant
4893 && (imm_expr.X_add_number < 0
4894 || imm_expr.X_add_number >= 0x10000))
4895 as_bad ("lui expression not in range 0..65535");
4896 imm_reloc = BFD_RELOC_LO16;
4897 if (c)
4898 {
4899 if (c != 'l')
4900 {
4901 if (imm_expr.X_op == O_constant)
4902 imm_expr.X_add_number =
4903 (imm_expr.X_add_number >> 16) & 0xffff;
4904 else if (c == 'h')
4905 imm_reloc = BFD_RELOC_HI16_S;
4906 else
4907 imm_reloc = BFD_RELOC_HI16;
4908 }
4909 }
4910 s = expr_end;
4911 continue;
4912
4913 case 'a': /* 26 bit address */
4914 my_getExpression (&offset_expr, s);
4915 s = expr_end;
4916 offset_reloc = BFD_RELOC_MIPS_JMP;
4917 continue;
4918
4919 default:
4920 fprintf (stderr, "bad char = '%c'\n", *args);
4921 internalError ();
4922 }
4923 break;
4924 }
4925 /* Args don't match. */
4926 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
4927 !strcmp (insn->name, insn[1].name))
4928 {
4929 ++insn;
4930 s = argsStart;
4931 continue;
4932 }
4933 insn_error = "ERROR: Illegal operands";
4934 return;
4935 }
4936 }
4937
4938 #define LP '('
4939 #define RP ')'
4940
4941 static int
4942 my_getSmallExpression (ep, str)
4943 expressionS *ep;
4944 char *str;
4945 {
4946 char *sp;
4947 int c = 0;
4948
4949 if (*str == ' ')
4950 str++;
4951 if (*str == LP
4952 || (*str == '%' &&
4953 ((str[1] == 'h' && str[2] == 'i')
4954 || (str[1] == 'H' && str[2] == 'I')
4955 || (str[1] == 'l' && str[2] == 'o'))
4956 && str[3] == LP))
4957 {
4958 if (*str == LP)
4959 c = 0;
4960 else
4961 {
4962 c = str[1];
4963 str += 3;
4964 }
4965
4966 /*
4967 * A small expression may be followed by a base register.
4968 * Scan to the end of this operand, and then back over a possible
4969 * base register. Then scan the small expression up to that
4970 * point. (Based on code in sparc.c...)
4971 */
4972 for (sp = str; *sp && *sp != ','; sp++)
4973 ;
4974 if (sp - 4 >= str && sp[-1] == RP)
4975 {
4976 if (isdigit (sp[-2]))
4977 {
4978 for (sp -= 3; sp >= str && isdigit (*sp); sp--)
4979 ;
4980 if (*sp == '$' && sp > str && sp[-1] == LP)
4981 {
4982 sp--;
4983 goto do_it;
4984 }
4985 }
4986 else if (sp - 5 >= str
4987 && sp[-5] == LP
4988 && sp[-4] == '$'
4989 && ((sp[-3] == 'f' && sp[-2] == 'p')
4990 || (sp[-3] == 's' && sp[-2] == 'p')
4991 || (sp[-3] == 'g' && sp[-2] == 'p')
4992 || (sp[-3] == 'a' && sp[-2] == 't')))
4993 {
4994 sp -= 5;
4995 do_it:
4996 if (sp == str)
4997 {
4998 /* no expression means zero offset */
4999 if (c)
5000 {
5001 /* %xx(reg) is an error */
5002 ep->X_op = O_absent;
5003 expr_end = str - 3;
5004 }
5005 else
5006 {
5007 ep->X_op = O_constant;
5008 expr_end = sp;
5009 }
5010 ep->X_add_symbol = NULL;
5011 ep->X_op_symbol = NULL;
5012 ep->X_add_number = 0;
5013 }
5014 else
5015 {
5016 *sp = '\0';
5017 my_getExpression (ep, str);
5018 *sp = LP;
5019 }
5020 return c;
5021 }
5022 }
5023 }
5024 my_getExpression (ep, str);
5025 return c; /* => %hi or %lo encountered */
5026 }
5027
5028 static void
5029 my_getExpression (ep, str)
5030 expressionS *ep;
5031 char *str;
5032 {
5033 char *save_in;
5034
5035 save_in = input_line_pointer;
5036 input_line_pointer = str;
5037 expression (ep);
5038 expr_end = input_line_pointer;
5039 input_line_pointer = save_in;
5040 }
5041
5042 /* Turn a string in input_line_pointer into a floating point constant
5043 of type type, and store the appropriate bytes in *litP. The number
5044 of LITTLENUMS emitted is stored in *sizeP . An error message is
5045 returned, or NULL on OK. */
5046
5047 char *
5048 md_atof (type, litP, sizeP)
5049 int type;
5050 char *litP;
5051 int *sizeP;
5052 {
5053 int prec;
5054 LITTLENUM_TYPE words[4];
5055 char *t;
5056 int i;
5057
5058 switch (type)
5059 {
5060 case 'f':
5061 prec = 2;
5062 break;
5063
5064 case 'd':
5065 prec = 4;
5066 break;
5067
5068 default:
5069 *sizeP = 0;
5070 return "bad call to md_atof";
5071 }
5072
5073 t = atof_ieee (input_line_pointer, type, words);
5074 if (t)
5075 input_line_pointer = t;
5076
5077 *sizeP = prec * 2;
5078
5079 if (byte_order == LITTLE_ENDIAN)
5080 {
5081 for (i = prec - 1; i >= 0; i--)
5082 {
5083 md_number_to_chars (litP, (valueT) words[i], 2);
5084 litP += 2;
5085 }
5086 }
5087 else
5088 {
5089 for (i = 0; i < prec; i++)
5090 {
5091 md_number_to_chars (litP, (valueT) words[i], 2);
5092 litP += 2;
5093 }
5094 }
5095
5096 return NULL;
5097 }
5098
5099 void
5100 md_number_to_chars (buf, val, n)
5101 char *buf;
5102 valueT val;
5103 int n;
5104 {
5105 switch (byte_order)
5106 {
5107 case LITTLE_ENDIAN:
5108 number_to_chars_littleendian (buf, val, n);
5109 break;
5110
5111 case BIG_ENDIAN:
5112 number_to_chars_bigendian (buf, val, n);
5113 break;
5114
5115 default:
5116 internalError ();
5117 }
5118 }
5119 \f
5120 #ifdef GPOPT
5121 CONST char *md_shortopts = "O::g::G:";
5122 #else
5123 CONST char *md_shortopts = "O::g::";
5124 #endif
5125 struct option md_longopts[] = {
5126 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
5127 {"mips0", no_argument, NULL, OPTION_MIPS1},
5128 {"mips1", no_argument, NULL, OPTION_MIPS1},
5129 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
5130 {"mips2", no_argument, NULL, OPTION_MIPS2},
5131 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
5132 {"mips3", no_argument, NULL, OPTION_MIPS3},
5133 #define OPTION_MCPU (OPTION_MD_BASE + 4)
5134 {"mcpu", required_argument, NULL, OPTION_MCPU},
5135 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 5)
5136 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
5137 #define OPTION_TRAP (OPTION_MD_BASE + 8)
5138 {"trap", no_argument, NULL, OPTION_TRAP},
5139 {"no-break", no_argument, NULL, OPTION_TRAP},
5140 #define OPTION_BREAK (OPTION_MD_BASE + 9)
5141 {"break", no_argument, NULL, OPTION_BREAK},
5142 {"no-trap", no_argument, NULL, OPTION_BREAK},
5143 #define OPTION_EB (OPTION_MD_BASE + 10)
5144 {"EB", no_argument, NULL, OPTION_EB},
5145 #define OPTION_EL (OPTION_MD_BASE + 11)
5146 {"EL", no_argument, NULL, OPTION_EL},
5147 #define OPTION_M4650 (OPTION_MD_BASE + 12)
5148 {"m4650", no_argument, NULL, OPTION_M4650},
5149 #define OPTION_NO_M4650 (OPTION_MD_BASE + 13)
5150 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
5151
5152 #ifdef OBJ_ELF
5153 #define OPTION_CALL_SHARED (OPTION_MD_BASE + 6)
5154 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
5155 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
5156 #define OPTION_NON_SHARED (OPTION_MD_BASE + 7)
5157 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
5158 #endif
5159
5160 {NULL, no_argument, NULL, 0}
5161 };
5162 size_t md_longopts_size = sizeof(md_longopts);
5163
5164 int
5165 md_parse_option (c, arg)
5166 int c;
5167 char *arg;
5168 {
5169 switch (c)
5170 {
5171 case OPTION_TRAP:
5172 mips_trap = 1;
5173 break;
5174
5175 case OPTION_BREAK:
5176 mips_trap = 0;
5177 break;
5178
5179 case OPTION_EB:
5180 byte_order = BIG_ENDIAN;
5181 #ifdef OBJ_AOUT
5182 mips_target_format = "a.out-mips-big";
5183 #endif
5184 #ifdef OBJ_ECOFF
5185 mips_target_format = "ecoff-bigmips";
5186 #endif
5187 #ifdef OBJ_ELF
5188 mips_target_format = "elf32-bigmips";
5189 #endif
5190 break;
5191
5192 case OPTION_EL:
5193 byte_order = LITTLE_ENDIAN;
5194 #ifdef OBJ_AOUT
5195 mips_target_format = "a.out-mips-little";
5196 #endif
5197 #ifdef OBJ_ECOFF
5198 mips_target_format = "ecoff-littlemips";
5199 #endif
5200 #ifdef OBJ_ELF
5201 mips_target_format = "elf32-littlemips";
5202 #endif
5203 break;
5204
5205 case 'O':
5206 if (arg && arg[1] == '0')
5207 mips_optimize = 1;
5208 else
5209 mips_optimize = 2;
5210 break;
5211
5212 case 'g':
5213 if (arg == NULL || arg[1] == '2')
5214 mips_optimize = 0;
5215 break;
5216
5217 case OPTION_MIPS1:
5218 mips_isa = 1;
5219 if (mips_cpu == -1)
5220 mips_cpu = 3000;
5221 break;
5222
5223 case OPTION_MIPS2:
5224 mips_isa = 2;
5225 if (mips_cpu == -1)
5226 mips_cpu = 6000;
5227 break;
5228
5229 case OPTION_MIPS3:
5230 mips_isa = 3;
5231 if (mips_cpu == -1)
5232 mips_cpu = 4000;
5233 break;
5234
5235 case OPTION_MCPU:
5236 {
5237 char *p;
5238
5239 /* Identify the processor type */
5240 p = arg;
5241 if (strcmp (p, "default") == 0
5242 || strcmp (p, "DEFAULT") == 0)
5243 mips_cpu = -1;
5244 else
5245 {
5246 if (*p == 'r' || *p == 'R')
5247 p++;
5248
5249 mips_cpu = -1;
5250 switch (*p)
5251 {
5252 case '2':
5253 if (strcmp (p, "2000") == 0
5254 || strcmp (p, "2k") == 0
5255 || strcmp (p, "2K") == 0)
5256 mips_cpu = 2000;
5257 break;
5258
5259 case '3':
5260 if (strcmp (p, "3000") == 0
5261 || strcmp (p, "3k") == 0
5262 || strcmp (p, "3K") == 0)
5263 mips_cpu = 3000;
5264 break;
5265
5266 case '4':
5267 if (strcmp (p, "4000") == 0
5268 || strcmp (p, "4k") == 0
5269 || strcmp (p, "4K") == 0)
5270 mips_cpu = 4000;
5271 else if (strcmp (p, "4400") == 0)
5272 mips_cpu = 4400;
5273 else if (strcmp (p, "4600") == 0)
5274 mips_cpu = 4600;
5275 else if (strcmp (p, "4650") == 0)
5276 {
5277 mips_cpu = 4650;
5278 if (mips_4650 < 0)
5279 mips_4650 = 1;
5280 }
5281 break;
5282
5283 case '6':
5284 if (strcmp (p, "6000") == 0
5285 || strcmp (p, "6k") == 0
5286 || strcmp (p, "6K") == 0)
5287 mips_cpu = 6000;
5288 break;
5289
5290 case 'o':
5291 if (strcmp (p, "orion") == 0)
5292 mips_cpu = 4600;
5293 break;
5294 }
5295
5296 if (mips_cpu == -1)
5297 {
5298 as_bad ("invalid architecture -mcpu=%s", arg);
5299 return 0;
5300 }
5301 }
5302 }
5303 break;
5304
5305 case OPTION_M4650:
5306 mips_4650 = 1;
5307 break;
5308
5309 case OPTION_NO_M4650:
5310 mips_4650 = 0;
5311 break;
5312
5313 case OPTION_MEMBEDDED_PIC:
5314 mips_pic = EMBEDDED_PIC;
5315 #ifdef GPOPT
5316 if (g_switch_seen)
5317 {
5318 as_bad ("-G may not be used with embedded PIC code");
5319 return 0;
5320 }
5321 g_switch_value = 0x7fffffff;
5322 #endif
5323 break;
5324
5325 #ifdef OBJ_ELF
5326 /* When generating ELF code, we permit -KPIC and -call_shared to
5327 select SVR4_PIC, and -non_shared to select no PIC. This is
5328 intended to be compatible with Irix 5. */
5329 case OPTION_CALL_SHARED:
5330 mips_pic = SVR4_PIC;
5331 if (g_switch_seen && g_switch_value != 0)
5332 {
5333 as_bad ("-G may not be used with SVR4 PIC code");
5334 return 0;
5335 }
5336 g_switch_value = 0;
5337 break;
5338
5339 case OPTION_NON_SHARED:
5340 mips_pic = NO_PIC;
5341 break;
5342 #endif /* OBJ_ELF */
5343
5344 #ifdef GPOPT
5345 case 'G':
5346 if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
5347 {
5348 as_bad ("-G may not be used with SVR4 or embedded PIC code");
5349 return 0;
5350 }
5351 else
5352 g_switch_value = atoi (arg);
5353 g_switch_seen = 1;
5354 break;
5355 #endif
5356
5357 default:
5358 return 0;
5359 }
5360
5361 return 1;
5362 }
5363
5364 void
5365 md_show_usage (stream)
5366 FILE *stream;
5367 {
5368 fprintf(stream, "\
5369 MIPS options:\n\
5370 -membedded-pic generate embedded position independent code\n\
5371 -EB generate big endian output\n\
5372 -EL generate little endian output\n\
5373 -g, -g2 do not remove uneeded NOPs or swap branches\n\
5374 -G NUM allow referencing objects up to NUM bytes\n\
5375 implicitly with the gp register [default 8]\n");
5376 fprintf(stream, "\
5377 -mips1, -mcpu=r{2,3}000 generate code for r2000 and r3000\n\
5378 -mips2, -mcpu=r6000 generate code for r6000\n\
5379 -mips3, -mcpu=r4000 generate code for r4000\n\
5380 -O0 remove unneeded NOPs, do not swap branches\n\
5381 -O remove unneeded NOPs and swap branches\n\
5382 --trap, --no-break trap exception on div by 0 and mult overflow\n\
5383 --break, --no-trap break exception on div by 0 and mult overflow\n");
5384 #ifdef OBJ_ELF
5385 fprintf(stream, "\
5386 -KPIC, -call_shared generate SVR4 position independent code\n\
5387 -non_shared do not generate position independent code\n");
5388 #endif
5389 }
5390 \f
5391 long
5392 md_pcrel_from (fixP)
5393 fixS *fixP;
5394 {
5395 #ifndef OBJ_AOUT
5396 if (fixP->fx_addsy != (symbolS *) NULL
5397 && ! S_IS_DEFINED (fixP->fx_addsy))
5398 {
5399 /* This makes a branch to an undefined symbol be a branch to the
5400 current location. */
5401 return 4;
5402 }
5403 #endif
5404
5405 /* return the address of the delay slot */
5406 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
5407 }
5408
5409 /* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
5410 reloc for a cons. We could use the definition there, except that
5411 we want to handle 64 bit relocs specially. */
5412
5413 void
5414 cons_fix_new_mips (frag, where, nbytes, exp)
5415 fragS *frag;
5416 int where;
5417 unsigned int nbytes;
5418 expressionS *exp;
5419 {
5420 /* If we are assembling in 32 bit mode, turn an 8 byte reloc into a
5421 4 byte reloc.
5422 FIXME: There is no way to select anything but 32 bit mode right
5423 now. */
5424 if (nbytes == 8)
5425 {
5426 if (byte_order == BIG_ENDIAN)
5427 where += 4;
5428 nbytes = 4;
5429 }
5430
5431 if (nbytes != 2 && nbytes != 4)
5432 as_bad ("Unsupported reloc size %d", nbytes);
5433
5434 fix_new_exp (frag_now, where, (int) nbytes, exp, 0,
5435 nbytes == 2 ? BFD_RELOC_16 : BFD_RELOC_32);
5436 }
5437
5438 /* When generating embedded PIC code we need to use a special
5439 relocation to represent the difference of two symbols in the .text
5440 section (switch tables use a difference of this sort). See
5441 include/coff/mips.h for details. This macro checks whether this
5442 fixup requires the special reloc. */
5443 #define SWITCH_TABLE(fixp) \
5444 ((fixp)->fx_r_type == BFD_RELOC_32 \
5445 && (fixp)->fx_addsy != NULL \
5446 && (fixp)->fx_subsy != NULL \
5447 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
5448 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
5449
5450 /* When generating embedded PIC code we must keep all PC relative
5451 relocations, in case the linker has to relax a call. We also need
5452 to keep relocations for switch table entries. */
5453
5454 /*ARGSUSED*/
5455 int
5456 mips_force_relocation (fixp)
5457 fixS *fixp;
5458 {
5459 return (mips_pic == EMBEDDED_PIC
5460 && (fixp->fx_pcrel
5461 || SWITCH_TABLE (fixp)
5462 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
5463 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
5464 }
5465
5466 /* Apply a fixup to the object file. */
5467
5468 int
5469 md_apply_fix (fixP, valueP)
5470 fixS *fixP;
5471 valueT *valueP;
5472 {
5473 unsigned char *buf;
5474 long insn, value;
5475
5476 assert (fixP->fx_size == 4 || fixP->fx_r_type == BFD_RELOC_16);
5477
5478 value = *valueP;
5479 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc */
5480
5481 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
5482 fixP->fx_done = 1;
5483
5484 switch (fixP->fx_r_type)
5485 {
5486 case BFD_RELOC_MIPS_JMP:
5487 case BFD_RELOC_HI16:
5488 case BFD_RELOC_HI16_S:
5489 case BFD_RELOC_MIPS_GPREL:
5490 case BFD_RELOC_MIPS_LITERAL:
5491 case BFD_RELOC_MIPS_CALL16:
5492 case BFD_RELOC_MIPS_GOT16:
5493 case BFD_RELOC_MIPS_GPREL32:
5494 if (fixP->fx_pcrel)
5495 as_bad_where (fixP->fx_file, fixP->fx_line,
5496 "Invalid PC relative reloc");
5497 /* Nothing needed to do. The value comes from the reloc entry */
5498 break;
5499
5500 case BFD_RELOC_PCREL_HI16_S:
5501 /* The addend for this is tricky if it is internal, so we just
5502 do everything here rather than in bfd_perform_relocation. */
5503 if ((fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) == 0)
5504 {
5505 /* For an external symbol adjust by the address to make it
5506 pcrel_offset. We use the address of the RELLO reloc
5507 which follows this one. */
5508 value += (fixP->fx_next->fx_frag->fr_address
5509 + fixP->fx_next->fx_where);
5510 }
5511 if (value & 0x8000)
5512 value += 0x10000;
5513 value >>= 16;
5514 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
5515 if (byte_order == BIG_ENDIAN)
5516 buf += 2;
5517 md_number_to_chars (buf, value, 2);
5518 break;
5519
5520 case BFD_RELOC_PCREL_LO16:
5521 /* The addend for this is tricky if it is internal, so we just
5522 do everything here rather than in bfd_perform_relocation. */
5523 if ((fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) == 0)
5524 value += fixP->fx_frag->fr_address + fixP->fx_where;
5525 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
5526 if (byte_order == BIG_ENDIAN)
5527 buf += 2;
5528 md_number_to_chars (buf, value, 2);
5529 break;
5530
5531 case BFD_RELOC_32:
5532 /* If we are deleting this reloc entry, we must fill in the
5533 value now. This can happen if we have a .word which is not
5534 resolved when it appears but is later defined. We also need
5535 to fill in the value if this is an embedded PIC switch table
5536 entry. */
5537 if (fixP->fx_done
5538 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
5539 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
5540 value, 4);
5541 break;
5542
5543 case BFD_RELOC_16:
5544 /* If we are deleting this reloc entry, we must fill in the
5545 value now. */
5546 assert (fixP->fx_size == 2);
5547 if (fixP->fx_done)
5548 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
5549 value, 2);
5550 break;
5551
5552 case BFD_RELOC_LO16:
5553 /* When handling an embedded PIC switch statement, we can wind
5554 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
5555 if (fixP->fx_done)
5556 {
5557 if (value < -0x8000 || value > 0x7fff)
5558 as_bad_where (fixP->fx_file, fixP->fx_line,
5559 "relocation overflow");
5560 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
5561 if (byte_order == BIG_ENDIAN)
5562 buf += 2;
5563 md_number_to_chars (buf, value, 2);
5564 }
5565 break;
5566
5567 case BFD_RELOC_16_PCREL_S2:
5568 /*
5569 * We need to save the bits in the instruction since fixup_segment()
5570 * might be deleting the relocation entry (i.e., a branch within
5571 * the current segment).
5572 */
5573 if (value & 0x3)
5574 as_warn_where (fixP->fx_file, fixP->fx_line,
5575 "Branch to odd address (%lx)", value);
5576 value >>= 2;
5577
5578 /* update old instruction data */
5579 buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
5580 switch (byte_order)
5581 {
5582 case LITTLE_ENDIAN:
5583 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
5584 break;
5585
5586 case BIG_ENDIAN:
5587 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
5588 break;
5589
5590 default:
5591 internalError ();
5592 return 0;
5593 }
5594
5595 if (value >= -0x8000 && value < 0x8000)
5596 insn |= value & 0xffff;
5597 else
5598 {
5599 /* The branch offset is too large. If this is an
5600 unconditional branch, and we are not generating PIC code,
5601 we can convert it to an absolute jump instruction. */
5602 if (mips_pic == NO_PIC
5603 && fixP->fx_done
5604 && fixP->fx_frag->fr_address >= text_section->vma
5605 && (fixP->fx_frag->fr_address
5606 < text_section->vma + text_section->_raw_size)
5607 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
5608 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
5609 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
5610 {
5611 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
5612 insn = 0x0c000000; /* jal */
5613 else
5614 insn = 0x08000000; /* j */
5615 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
5616 fixP->fx_done = 0;
5617 fixP->fx_addsy = section_symbol (text_section);
5618 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
5619 }
5620 else
5621 {
5622 /* FIXME. It would be possible in principle to handle
5623 conditional branches which overflow. They could be
5624 transformed into a branch around a jump. This would
5625 require setting up variant frags for each different
5626 branch type. The native MIPS assembler attempts to
5627 handle these cases, but it appears to do it
5628 incorrectly. */
5629 as_bad_where (fixP->fx_file, fixP->fx_line,
5630 "Relocation overflow");
5631 }
5632 }
5633
5634 md_number_to_chars ((char *) buf, (valueT) insn, 4);
5635 break;
5636
5637 default:
5638 internalError ();
5639 }
5640
5641 return 1;
5642 }
5643
5644 #if 0
5645 void
5646 printInsn (oc)
5647 unsigned long oc;
5648 {
5649 const struct mips_opcode *p;
5650 int treg, sreg, dreg, shamt;
5651 short imm;
5652 const char *args;
5653 int i;
5654
5655 for (i = 0; i < NUMOPCODES; ++i)
5656 {
5657 p = &mips_opcodes[i];
5658 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
5659 {
5660 printf ("%08lx %s\t", oc, p->name);
5661 treg = (oc >> 16) & 0x1f;
5662 sreg = (oc >> 21) & 0x1f;
5663 dreg = (oc >> 11) & 0x1f;
5664 shamt = (oc >> 6) & 0x1f;
5665 imm = oc;
5666 for (args = p->args;; ++args)
5667 {
5668 switch (*args)
5669 {
5670 case '\0':
5671 printf ("\n");
5672 break;
5673
5674 case ',':
5675 case '(':
5676 case ')':
5677 printf ("%c", *args);
5678 continue;
5679
5680 case 'r':
5681 assert (treg == sreg);
5682 printf ("$%d,$%d", treg, sreg);
5683 continue;
5684
5685 case 'd':
5686 case 'G':
5687 printf ("$%d", dreg);
5688 continue;
5689
5690 case 't':
5691 case 'E':
5692 printf ("$%d", treg);
5693 continue;
5694
5695 case 'k':
5696 printf ("0x%x", treg);
5697 continue;
5698
5699 case 'b':
5700 case 's':
5701 printf ("$%d", sreg);
5702 continue;
5703
5704 case 'a':
5705 printf ("0x%08lx", oc & 0x1ffffff);
5706 continue;
5707
5708 case 'i':
5709 case 'j':
5710 case 'o':
5711 case 'u':
5712 printf ("%d", imm);
5713 continue;
5714
5715 case '<':
5716 case '>':
5717 printf ("$%d", shamt);
5718 continue;
5719
5720 default:
5721 internalError ();
5722 }
5723 break;
5724 }
5725 return;
5726 }
5727 }
5728 printf ("%08lx UNDEFINED\n", oc);
5729 }
5730 #endif
5731
5732 static symbolS *
5733 get_symbol ()
5734 {
5735 int c;
5736 char *name;
5737 symbolS *p;
5738
5739 name = input_line_pointer;
5740 c = get_symbol_end ();
5741 p = (symbolS *) symbol_find_or_make (name);
5742 *input_line_pointer = c;
5743 return p;
5744 }
5745
5746 /* Align the current frag to a given power of two. The MIPS assembler
5747 also automatically adjusts any preceding label. */
5748
5749 static void
5750 mips_align (to, fill, label)
5751 int to;
5752 int fill;
5753 symbolS *label;
5754 {
5755 mips_emit_delays ();
5756 frag_align (to, fill);
5757 record_alignment (now_seg, to);
5758 if (label != NULL)
5759 {
5760 assert (S_GET_SEGMENT (label) == now_seg);
5761 label->sy_frag = frag_now;
5762 S_SET_VALUE (label, (valueT) frag_now_fix ());
5763 }
5764 }
5765
5766 /* Align to a given power of two. .align 0 turns off the automatic
5767 alignment used by the data creating pseudo-ops. */
5768
5769 static void
5770 s_align (x)
5771 int x;
5772 {
5773 register int temp;
5774 register long temp_fill;
5775 long max_alignment = 15;
5776
5777 /*
5778
5779 o Note that the assembler pulls down any immediately preceeding label
5780 to the aligned address.
5781 o It's not documented but auto alignment is reinstated by
5782 a .align pseudo instruction.
5783 o Note also that after auto alignment is turned off the mips assembler
5784 issues an error on attempt to assemble an improperly aligned data item.
5785 We don't.
5786
5787 */
5788
5789 temp = get_absolute_expression ();
5790 if (temp > max_alignment)
5791 as_bad ("Alignment too large: %d. assumed.", temp = max_alignment);
5792 else if (temp < 0)
5793 {
5794 as_warn ("Alignment negative: 0 assumed.");
5795 temp = 0;
5796 }
5797 if (*input_line_pointer == ',')
5798 {
5799 input_line_pointer++;
5800 temp_fill = get_absolute_expression ();
5801 }
5802 else
5803 temp_fill = 0;
5804 if (temp)
5805 {
5806 auto_align = 1;
5807 mips_align (temp, (int) temp_fill, insn_label);
5808 }
5809 else
5810 {
5811 auto_align = 0;
5812 }
5813
5814 demand_empty_rest_of_line ();
5815 }
5816
5817 /* Handle .ascii and .asciiz. This just calls stringer and forgets
5818 that there was a previous instruction. */
5819
5820 static void
5821 s_stringer (append_zero)
5822 int append_zero;
5823 {
5824 mips_emit_delays ();
5825 insn_label = NULL;
5826 stringer (append_zero);
5827 }
5828
5829 static void
5830 s_change_sec (sec)
5831 int sec;
5832 {
5833 #ifdef GPOPT
5834 segT seg;
5835 #endif
5836
5837 /* When generating embedded PIC code, we only use the .text, .lit8,
5838 .sdata and .sbss sections. We change the .data and .rdata
5839 pseudo-ops to use .sdata. */
5840 if (mips_pic == EMBEDDED_PIC
5841 && (sec == 'd' || sec == 'r'))
5842 sec = 's';
5843
5844 mips_emit_delays ();
5845 switch (sec)
5846 {
5847 case 't':
5848 s_text (0);
5849 break;
5850 case 'd':
5851 s_data (0);
5852 break;
5853 case 'b':
5854 subseg_set (bss_section, (subsegT) get_absolute_expression ());
5855 demand_empty_rest_of_line ();
5856 break;
5857
5858 case 'r':
5859 #ifdef GPOPT
5860 seg = subseg_new (RDATA_SECTION_NAME,
5861 (subsegT) get_absolute_expression ());
5862 #ifdef OBJ_ELF
5863 bfd_set_section_flags (stdoutput, seg,
5864 (SEC_ALLOC
5865 | SEC_LOAD
5866 | SEC_READONLY
5867 | SEC_RELOC
5868 | SEC_DATA));
5869 bfd_set_section_alignment (stdoutput, seg, 4);
5870 #endif
5871 demand_empty_rest_of_line ();
5872 #else /* ! defined (GPOPT) */
5873 as_bad ("No read only data section in this object file format");
5874 demand_empty_rest_of_line ();
5875 return;
5876 #endif /* ! defined (GPOPT) */
5877 break;
5878
5879 case 's':
5880 #ifdef GPOPT
5881 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
5882 #ifdef OBJ_ELF
5883 bfd_set_section_flags (stdoutput, seg,
5884 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
5885 bfd_set_section_alignment (stdoutput, seg, 4);
5886 #endif
5887 demand_empty_rest_of_line ();
5888 break;
5889 #else /* ! defined (GPOPT) */
5890 as_bad ("Global pointers not supported; recompile -G 0");
5891 demand_empty_rest_of_line ();
5892 return;
5893 #endif /* ! defined (GPOPT) */
5894 }
5895
5896 auto_align = 1;
5897 }
5898
5899 #ifdef OBJ_ELF
5900
5901 /* Handle the ELF .section pseudo-op. This is a wrapper around
5902 obj_elf_section. */
5903
5904 static void
5905 s_elf_section (x)
5906 int x;
5907 {
5908 mips_emit_delays ();
5909 obj_elf_section (x);
5910 auto_align = 1;
5911 }
5912
5913 #endif /* OBJ_ELF */
5914
5915 static void
5916 s_cons (log_size)
5917 int log_size;
5918 {
5919 symbolS *label;
5920
5921 label = insn_label;
5922 mips_emit_delays ();
5923 if (log_size > 0 && auto_align)
5924 mips_align (log_size, 0, label);
5925 insn_label = NULL;
5926 cons (1 << log_size);
5927 }
5928
5929 static void
5930 s_err (x)
5931 int x;
5932 {
5933 as_fatal ("Encountered `.err', aborting assembly");
5934 }
5935
5936 static void
5937 s_extern (x)
5938 int x;
5939 {
5940 valueT size;
5941 symbolS *symbolP;
5942
5943 symbolP = get_symbol ();
5944 if (*input_line_pointer == ',')
5945 input_line_pointer++;
5946 size = get_absolute_expression ();
5947 S_SET_EXTERNAL (symbolP);
5948
5949 #ifdef ECOFF_DEBUGGING
5950 symbolP->ecoff_extern_size = size;
5951 #endif
5952 }
5953
5954 static void
5955 s_float_cons (type)
5956 int type;
5957 {
5958 symbolS *label;
5959
5960 label = insn_label;
5961
5962 mips_emit_delays ();
5963
5964 if (auto_align)
5965 if (type == 'd')
5966 mips_align (3, 0, label);
5967 else
5968 mips_align (2, 0, label);
5969
5970 insn_label = NULL;
5971
5972 float_cons (type);
5973 }
5974
5975 /* Handle .globl. We need to override it because on Irix 5 you are
5976 permitted to say
5977 .globl foo .text
5978 where foo is an undefined symbol, to mean that foo should be
5979 considered to be the address of a function. */
5980
5981 static void
5982 s_mips_globl (x)
5983 int x;
5984 {
5985 char *name;
5986 int c;
5987 symbolS *symbolP;
5988
5989 name = input_line_pointer;
5990 c = get_symbol_end ();
5991 symbolP = symbol_find_or_make (name);
5992 *input_line_pointer = c;
5993 SKIP_WHITESPACE ();
5994 if (! is_end_of_line[(unsigned char) *input_line_pointer])
5995 {
5996 char *secname;
5997 asection *sec;
5998
5999 secname = input_line_pointer;
6000 c = get_symbol_end ();
6001 sec = bfd_get_section_by_name (stdoutput, secname);
6002 if (sec == NULL)
6003 as_bad ("%s: no such section", secname);
6004 *input_line_pointer = c;
6005
6006 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
6007 symbolP->bsym->flags |= BSF_FUNCTION;
6008 }
6009
6010 S_SET_EXTERNAL (symbolP);
6011 demand_empty_rest_of_line ();
6012 }
6013
6014 static void
6015 s_option (x)
6016 int x;
6017 {
6018 char *opt;
6019 char c;
6020
6021 opt = input_line_pointer;
6022 c = get_symbol_end ();
6023
6024 if (*opt == 'O')
6025 {
6026 /* FIXME: What does this mean? */
6027 }
6028 else if (strncmp (opt, "pic", 3) == 0)
6029 {
6030 int i;
6031
6032 i = atoi (opt + 3);
6033 if (i == 0)
6034 mips_pic = NO_PIC;
6035 else if (i == 2)
6036 mips_pic = SVR4_PIC;
6037 else
6038 as_bad (".option pic%d not supported", i);
6039
6040 #ifdef GPOPT
6041 if (mips_pic == SVR4_PIC)
6042 {
6043 if (g_switch_seen && g_switch_value != 0)
6044 as_warn ("-G may not be used with SVR4 PIC code");
6045 g_switch_value = 0;
6046 bfd_set_gp_size (stdoutput, 0);
6047 }
6048 #endif
6049 }
6050 else
6051 as_warn ("Unrecognized option \"%s\"", opt);
6052
6053 *input_line_pointer = c;
6054 demand_empty_rest_of_line ();
6055 }
6056
6057 static void
6058 s_mipsset (x)
6059 int x;
6060 {
6061 char *name = input_line_pointer, ch;
6062
6063 while (!is_end_of_line[(unsigned char) *input_line_pointer])
6064 input_line_pointer++;
6065 ch = *input_line_pointer;
6066 *input_line_pointer = '\0';
6067
6068 if (strcmp (name, "reorder") == 0)
6069 {
6070 if (mips_noreorder)
6071 {
6072 prev_insn_unreordered = 1;
6073 prev_prev_insn_unreordered = 1;
6074 }
6075 mips_noreorder = 0;
6076 }
6077 else if (strcmp (name, "noreorder") == 0)
6078 {
6079 mips_emit_delays ();
6080 mips_noreorder = 1;
6081 mips_any_noreorder = 1;
6082 }
6083 else if (strcmp (name, "at") == 0)
6084 {
6085 mips_noat = 0;
6086 }
6087 else if (strcmp (name, "noat") == 0)
6088 {
6089 mips_noat = 1;
6090 }
6091 else if (strcmp (name, "macro") == 0)
6092 {
6093 mips_warn_about_macros = 0;
6094 }
6095 else if (strcmp (name, "nomacro") == 0)
6096 {
6097 if (mips_noreorder == 0)
6098 as_bad ("`noreorder' must be set before `nomacro'");
6099 mips_warn_about_macros = 1;
6100 }
6101 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
6102 {
6103 mips_nomove = 0;
6104 }
6105 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
6106 {
6107 mips_nomove = 1;
6108 }
6109 else if (strcmp (name, "bopt") == 0)
6110 {
6111 mips_nobopt = 0;
6112 }
6113 else if (strcmp (name, "nobopt") == 0)
6114 {
6115 mips_nobopt = 1;
6116 }
6117 else if (strncmp (name, "mips", 4) == 0)
6118 {
6119 int isa;
6120
6121 /* Permit the user to change the ISA on the fly. Needless to
6122 say, misuse can cause serious problems. */
6123 isa = atoi (name + 4);
6124 if (isa == 0)
6125 mips_isa = file_mips_isa;
6126 else if (isa < 1 || isa > 3)
6127 as_bad ("unknown ISA level");
6128 else
6129 mips_isa = isa;
6130 }
6131 else
6132 {
6133 as_warn ("Tried to set unrecognized symbol: %s\n", name);
6134 }
6135 *input_line_pointer = ch;
6136 demand_empty_rest_of_line ();
6137 }
6138
6139 /* The same as the usual .space directive, except that we have to
6140 forget about any previous instruction. */
6141
6142 static void
6143 s_mips_space (param)
6144 int param;
6145 {
6146 mips_emit_delays ();
6147 insn_label = NULL;
6148 s_space (param);
6149 }
6150
6151 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
6152 .option pic2. It means to generate SVR4 PIC calls. */
6153
6154 static void
6155 s_abicalls (ignore)
6156 int ignore;
6157 {
6158 mips_pic = SVR4_PIC;
6159 #ifdef GPOPT
6160 if (g_switch_seen && g_switch_value != 0)
6161 as_warn ("-G may not be used with SVR4 PIC code");
6162 g_switch_value = 0;
6163 #endif
6164 bfd_set_gp_size (stdoutput, 0);
6165 demand_empty_rest_of_line ();
6166 }
6167
6168 /* Handle the .cpload pseudo-op. This is used when generating SVR4
6169 PIC code. It sets the $gp register for the function based on the
6170 function address, which is in the register named in the argument.
6171 This uses a relocation against _gp_disp, which is handled specially
6172 by the linker. The result is:
6173 lui $gp,%hi(_gp_disp)
6174 addiu $gp,$gp,%lo(_gp_disp)
6175 addu $gp,$gp,.cpload argument
6176 The .cpload argument is normally $25 == $t9. */
6177
6178 static void
6179 s_cpload (ignore)
6180 int ignore;
6181 {
6182 expressionS ex;
6183 int icnt = 0;
6184
6185 /* If we are not generating SVR4 PIC code, .cpload is ignored. */
6186 if (mips_pic != SVR4_PIC)
6187 {
6188 s_ignore (0);
6189 return;
6190 }
6191
6192 /* .cpload should be a in .set noreorder section. */
6193 if (mips_noreorder == 0)
6194 as_warn (".cpload not in noreorder section");
6195
6196 ex.X_op = O_symbol;
6197 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
6198 ex.X_op_symbol = NULL;
6199 ex.X_add_number = 0;
6200
6201 macro_build_lui ((char *) NULL, &icnt, &ex, GP);
6202 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
6203 (int) BFD_RELOC_LO16);
6204
6205 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
6206 GP, GP, tc_get_register (0));
6207
6208 demand_empty_rest_of_line ();
6209 }
6210
6211 /* Handle the .cprestore pseudo-op. This stores $gp into a given
6212 offset from $sp. The offset is remembered, and after making a PIC
6213 call $gp is restored from that location. */
6214
6215 static void
6216 s_cprestore (ignore)
6217 int ignore;
6218 {
6219 expressionS ex;
6220 int icnt = 0;
6221
6222 /* If we are not generating SVR4 PIC code, .cprestore is ignored. */
6223 if (mips_pic != SVR4_PIC)
6224 {
6225 s_ignore (0);
6226 return;
6227 }
6228
6229 mips_cprestore_offset = get_absolute_expression ();
6230
6231 ex.X_op = O_constant;
6232 ex.X_add_symbol = NULL;
6233 ex.X_op_symbol = NULL;
6234 ex.X_add_number = mips_cprestore_offset;
6235
6236 macro_build ((char *) NULL, &icnt, &ex,
6237 mips_isa < 3 ? "sw" : "sd",
6238 "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
6239
6240 demand_empty_rest_of_line ();
6241 }
6242
6243 /* Handle the .gpword pseudo-op. This is used when generating PIC
6244 code. It generates a 32 bit GP relative reloc. */
6245
6246 static void
6247 s_gpword (ignore)
6248 int ignore;
6249 {
6250 symbolS *label;
6251 expressionS ex;
6252 char *p;
6253
6254 /* When not generating PIC code, this is treated as .word. */
6255 if (mips_pic != SVR4_PIC)
6256 {
6257 s_cons (2);
6258 return;
6259 }
6260
6261 label = insn_label;
6262 mips_emit_delays ();
6263 if (auto_align)
6264 mips_align (2, 0, label);
6265 insn_label = NULL;
6266
6267 expression (&ex);
6268
6269 if (ex.X_op != O_symbol || ex.X_add_number != 0)
6270 {
6271 as_bad ("Unsupported use of .gpword");
6272 ignore_rest_of_line ();
6273 }
6274
6275 p = frag_more (4);
6276 md_number_to_chars (p, (valueT) 0, 4);
6277 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
6278 BFD_RELOC_MIPS_GPREL32);
6279
6280 demand_empty_rest_of_line ();
6281 }
6282
6283 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
6284 tables in SVR4 PIC code. */
6285
6286 static void
6287 s_cpadd (ignore)
6288 int ignore;
6289 {
6290 int icnt = 0;
6291 int reg;
6292
6293 /* This is ignored when not generating SVR4 PIC code. */
6294 if (mips_pic != SVR4_PIC)
6295 {
6296 s_ignore (0);
6297 return;
6298 }
6299
6300 /* Add $gp to the register named as an argument. */
6301 reg = tc_get_register (0);
6302 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6303 mips_isa < 3 ? "addu" : "daddu",
6304 "d,v,t", reg, reg, GP);
6305
6306 demand_empty_rest_of_line ();
6307 }
6308
6309 /* Parse a register string into a number. Called from the ECOFF code
6310 to parse .frame. The argument is non-zero if this is the frame
6311 register, so that we can record it in mips_frame_reg. */
6312
6313 int
6314 tc_get_register (frame)
6315 int frame;
6316 {
6317 int reg;
6318
6319 SKIP_WHITESPACE ();
6320 if (*input_line_pointer++ != '$')
6321 {
6322 as_warn ("expected `$'");
6323 reg = 0;
6324 }
6325 else if (isdigit ((unsigned char) *input_line_pointer))
6326 {
6327 reg = get_absolute_expression ();
6328 if (reg < 0 || reg >= 32)
6329 {
6330 as_warn ("Bad register number");
6331 reg = 0;
6332 }
6333 }
6334 else
6335 {
6336 if (strncmp (input_line_pointer, "fp", 2) == 0)
6337 reg = FP;
6338 else if (strncmp (input_line_pointer, "sp", 2) == 0)
6339 reg = SP;
6340 else if (strncmp (input_line_pointer, "gp", 2) == 0)
6341 reg = GP;
6342 else if (strncmp (input_line_pointer, "at", 2) == 0)
6343 reg = AT;
6344 else
6345 {
6346 as_warn ("Unrecognized register name");
6347 reg = 0;
6348 }
6349 input_line_pointer += 2;
6350 }
6351 if (frame)
6352 mips_frame_reg = reg != 0 ? reg : SP;
6353 return reg;
6354 }
6355
6356 valueT
6357 md_section_align (seg, addr)
6358 asection *seg;
6359 valueT addr;
6360 {
6361 int align = bfd_get_section_alignment (stdoutput, seg);
6362
6363 return ((addr + (1 << align) - 1) & (-1 << align));
6364 }
6365
6366 /* Estimate the size of a frag before relaxing. We are not really
6367 relaxing here, and the final size is encoded in the subtype
6368 information. */
6369
6370 /*ARGSUSED*/
6371 int
6372 md_estimate_size_before_relax (fragp, segtype)
6373 fragS *fragp;
6374 asection *segtype;
6375 {
6376 int change;
6377
6378 if (mips_pic == NO_PIC)
6379 {
6380 #ifdef GPOPT
6381 const char *symname;
6382
6383 /* Find out whether this symbol can be referenced off the GP
6384 register. It can be if it is smaller than the -G size or if
6385 it is in the .sdata or .sbss section. Certain symbols can
6386 not be referenced off the GP, although it appears as though
6387 they can. */
6388 symname = S_GET_NAME (fragp->fr_symbol);
6389 if (symname != (const char *) NULL
6390 && (strcmp (symname, "eprol") == 0
6391 || strcmp (symname, "etext") == 0
6392 || strcmp (symname, "_gp") == 0
6393 || strcmp (symname, "edata") == 0
6394 || strcmp (symname, "_fbss") == 0
6395 || strcmp (symname, "_fdata") == 0
6396 || strcmp (symname, "_ftext") == 0
6397 || strcmp (symname, "end") == 0
6398 || strcmp (symname, "_gp_disp") == 0))
6399 change = 1;
6400 else if (! S_IS_DEFINED (fragp->fr_symbol)
6401 && ((fragp->fr_symbol->ecoff_extern_size != 0
6402 && fragp->fr_symbol->ecoff_extern_size <= g_switch_value)
6403 || (S_GET_VALUE (fragp->fr_symbol) != 0
6404 && S_GET_VALUE (fragp->fr_symbol) <= g_switch_value)))
6405 change = 0;
6406 else
6407 {
6408 const char *segname;
6409
6410 segname = segment_name (S_GET_SEGMENT (fragp->fr_symbol));
6411 assert (strcmp (segname, ".lit8") != 0
6412 && strcmp (segname, ".lit4") != 0);
6413 change = (strcmp (segname, ".sdata") != 0
6414 && strcmp (segname, ".sbss") != 0);
6415 }
6416 #else /* ! defined (GPOPT) */
6417 /* We are not optimizing for the GP register. */
6418 change = 1;
6419 #endif /* ! defined (GPOPT) */
6420 }
6421 else if (mips_pic == SVR4_PIC)
6422 {
6423 asection *symsec = fragp->fr_symbol->bsym->section;
6424
6425 /* This must duplicate the test in adjust_reloc_syms. */
6426 change = (symsec != &bfd_und_section
6427 && symsec != &bfd_abs_section
6428 && ! bfd_is_com_section (symsec));
6429 }
6430 else
6431 abort ();
6432
6433 if (change)
6434 {
6435 /* Record the offset to the first reloc in the fr_opcode field.
6436 This lets md_convert_frag and tc_gen_reloc know that the code
6437 must be expanded. */
6438 fragp->fr_opcode = (fragp->fr_literal
6439 + fragp->fr_fix
6440 - RELAX_OLD (fragp->fr_subtype)
6441 + RELAX_RELOC1 (fragp->fr_subtype));
6442 /* FIXME: This really needs as_warn_where. */
6443 if (RELAX_WARN (fragp->fr_subtype))
6444 as_warn ("AT used after \".set noat\" or macro used after \".set nomacro\"");
6445 }
6446
6447 if (! change)
6448 return 0;
6449 else
6450 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
6451 }
6452
6453 /* Translate internal representation of relocation info to BFD target
6454 format. */
6455
6456 arelent **
6457 tc_gen_reloc (section, fixp)
6458 asection *section;
6459 fixS *fixp;
6460 {
6461 static arelent *retval[4];
6462 arelent *reloc;
6463
6464 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
6465 retval[1] = NULL;
6466
6467 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
6468 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
6469
6470 if (mips_pic == EMBEDDED_PIC
6471 && SWITCH_TABLE (fixp))
6472 {
6473 /* For a switch table entry we use a special reloc. The addend
6474 is actually the difference between the reloc address and the
6475 subtrahend. */
6476 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
6477 #ifndef OBJ_ECOFF
6478 as_fatal ("Double check fx_r_type in tc-mips.c:tc_gen_reloc");
6479 #endif
6480 fixp->fx_r_type = BFD_RELOC_GPREL32;
6481 }
6482 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
6483 {
6484 /* We use a special addend for an internal RELLO reloc. */
6485 if (fixp->fx_addsy->bsym->flags & BSF_SECTION_SYM)
6486 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
6487 else
6488 reloc->addend = fixp->fx_addnumber + reloc->address;
6489 }
6490 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
6491 {
6492 assert (fixp->fx_next != NULL
6493 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
6494 /* We use a special addend for an internal RELHI reloc. The
6495 reloc is relative to the RELLO; adjust the addend
6496 accordingly. */
6497 if (fixp->fx_addsy->bsym->flags & BSF_SECTION_SYM)
6498 reloc->addend = (fixp->fx_next->fx_frag->fr_address
6499 + fixp->fx_next->fx_where
6500 - S_GET_VALUE (fixp->fx_subsy));
6501 else
6502 reloc->addend = (fixp->fx_addnumber
6503 + fixp->fx_next->fx_frag->fr_address
6504 + fixp->fx_next->fx_where);
6505 }
6506 else if (fixp->fx_pcrel == 0)
6507 reloc->addend = fixp->fx_addnumber;
6508 else
6509 {
6510 #ifndef OBJ_AOUT
6511 /* A gruesome hack which is a result of the gruesome gas reloc
6512 handling. */
6513 reloc->addend = reloc->address;
6514 #else
6515 reloc->addend = -reloc->address;
6516 #endif
6517 }
6518
6519 /* If this is a variant frag, we may need to adjust the existing
6520 reloc and generate a new one. */
6521 if (fixp->fx_frag->fr_opcode != NULL
6522 && (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
6523 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
6524 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16))
6525 {
6526 arelent *reloc2;
6527
6528 /* If this is not the last reloc in this frag, then we have two
6529 GPREL relocs, both of which are being replaced. Let the
6530 second one handle all of them. */
6531 if (fixp->fx_next != NULL
6532 && fixp->fx_frag == fixp->fx_next->fx_frag)
6533 {
6534 assert (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
6535 && fixp->fx_next->fx_r_type == BFD_RELOC_MIPS_GPREL);
6536 retval[0] = NULL;
6537 return retval;
6538 }
6539
6540 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
6541 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
6542 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
6543 retval[2] = NULL;
6544 reloc2->sym_ptr_ptr = &fixp->fx_addsy->bsym;
6545 reloc2->address = (reloc->address
6546 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
6547 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
6548 reloc2->addend = fixp->fx_addnumber;
6549 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
6550 assert (reloc2->howto != NULL);
6551
6552 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
6553 {
6554 arelent *reloc3;
6555
6556 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
6557 retval[3] = NULL;
6558 *reloc3 = *reloc2;
6559 reloc3->address += 4;
6560 }
6561
6562 if (mips_pic == NO_PIC)
6563 {
6564 assert (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL);
6565 fixp->fx_r_type = BFD_RELOC_HI16_S;
6566 }
6567 else if (mips_pic == SVR4_PIC)
6568 {
6569 if (fixp->fx_r_type != BFD_RELOC_MIPS_GOT16)
6570 {
6571 assert (fixp->fx_r_type == BFD_RELOC_MIPS_CALL16);
6572 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
6573 }
6574 }
6575 else
6576 abort ();
6577 }
6578
6579 /* To support a PC relative reloc when generating embedded PIC code
6580 for ECOFF, we use a Cygnus extension. We check for that here to
6581 make sure that we don't let such a reloc escape normally. */
6582 #ifdef OBJ_ECOFF
6583 if (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
6584 && mips_pic != EMBEDDED_PIC)
6585 reloc->howto = NULL;
6586 else
6587 #endif
6588 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
6589
6590 if (reloc->howto == NULL)
6591 {
6592 as_bad_where (fixp->fx_file, fixp->fx_line,
6593 "Can not represent relocation in this object file format");
6594 retval[0] = NULL;
6595 }
6596
6597 return retval;
6598 }
6599
6600 /* Convert a machine dependent frag. */
6601
6602 void
6603 md_convert_frag (abfd, asec, fragp)
6604 bfd *abfd;
6605 segT asec;
6606 fragS *fragp;
6607 {
6608 int old, new;
6609 char *fixptr;
6610
6611 if (fragp->fr_opcode == NULL)
6612 return;
6613
6614 old = RELAX_OLD (fragp->fr_subtype);
6615 new = RELAX_NEW (fragp->fr_subtype);
6616 fixptr = fragp->fr_literal + fragp->fr_fix;
6617
6618 if (new > 0)
6619 memcpy (fixptr - old, fixptr, new);
6620
6621 fragp->fr_fix += new - old;
6622 }
6623
6624 /* This function is called whenever a label is defined. It is used
6625 when handling branch delays; if a branch has a label, we assume we
6626 can not move it. */
6627
6628 void
6629 mips_define_label (sym)
6630 symbolS *sym;
6631 {
6632 insn_label = sym;
6633 }
6634 \f
6635 #ifdef OBJ_ELF
6636
6637 /* Some special processing for a MIPS ELF file. */
6638
6639 void
6640 mips_elf_final_processing ()
6641 {
6642 Elf32_RegInfo s;
6643
6644 /* Write out the .reginfo section. */
6645 s.ri_gprmask = mips_gprmask;
6646 s.ri_cprmask[0] = mips_cprmask[0];
6647 s.ri_cprmask[1] = mips_cprmask[1];
6648 s.ri_cprmask[2] = mips_cprmask[2];
6649 s.ri_cprmask[3] = mips_cprmask[3];
6650 /* The gp_value field is set by the MIPS ELF backend. */
6651
6652 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
6653 ((Elf32_External_RegInfo *)
6654 mips_regmask_frag));
6655
6656 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
6657 sort of BFD interface for this. */
6658 if (mips_any_noreorder)
6659 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
6660 if (mips_pic != NO_PIC)
6661 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
6662 }
6663
6664 #endif /* OBJ_ELF */
6665 \f
6666 #ifndef ECOFF_DEBUGGING
6667
6668 /* These functions should really be defined by the object file format,
6669 since they are related to debugging information. However, this
6670 code has to work for the a.out format, which does not define them,
6671 so we provide simple versions here. These don't actually generate
6672 any debugging information, but they do simple checking and someday
6673 somebody may make them useful. */
6674
6675 typedef struct loc
6676 {
6677 struct loc *loc_next;
6678 unsigned long loc_fileno;
6679 unsigned long loc_lineno;
6680 unsigned long loc_offset;
6681 unsigned short loc_delta;
6682 unsigned short loc_count;
6683 #if 0
6684 fragS *loc_frag;
6685 #endif
6686 }
6687 locS;
6688
6689 typedef struct proc
6690 {
6691 struct proc *proc_next;
6692 struct symbol *proc_isym;
6693 struct symbol *proc_end;
6694 unsigned long proc_reg_mask;
6695 unsigned long proc_reg_offset;
6696 unsigned long proc_fpreg_mask;
6697 unsigned long proc_fpreg_offset;
6698 unsigned long proc_frameoffset;
6699 unsigned long proc_framereg;
6700 unsigned long proc_pcreg;
6701 locS *proc_iline;
6702 struct file *proc_file;
6703 int proc_index;
6704 }
6705 procS;
6706
6707 typedef struct file
6708 {
6709 struct file *file_next;
6710 unsigned long file_fileno;
6711 struct symbol *file_symbol;
6712 struct symbol *file_end;
6713 struct proc *file_proc;
6714 int file_numprocs;
6715 }
6716 fileS;
6717
6718 static struct obstack proc_frags;
6719 static procS *proc_lastP;
6720 static procS *proc_rootP;
6721 static int numprocs;
6722
6723 static void
6724 md_obj_begin ()
6725 {
6726 obstack_begin (&proc_frags, 0x2000);
6727 }
6728
6729 static void
6730 md_obj_end ()
6731 {
6732 /* check for premature end, nesting errors, etc */
6733 if (proc_lastP && proc_lastP->proc_end == NULL)
6734 as_warn ("missing `.end' at end of assembly");
6735 }
6736
6737 static long
6738 get_number ()
6739 {
6740 int negative = 0;
6741 long val = 0;
6742
6743 if (*input_line_pointer == '-')
6744 {
6745 ++input_line_pointer;
6746 negative = 1;
6747 }
6748 if (!isdigit (*input_line_pointer))
6749 as_bad ("Expected simple number.");
6750 if (input_line_pointer[0] == '0')
6751 {
6752 if (input_line_pointer[1] == 'x')
6753 {
6754 input_line_pointer += 2;
6755 while (isxdigit (*input_line_pointer))
6756 {
6757 val <<= 4;
6758 val |= hex_value (*input_line_pointer++);
6759 }
6760 return negative ? -val : val;
6761 }
6762 else
6763 {
6764 ++input_line_pointer;
6765 while (isdigit (*input_line_pointer))
6766 {
6767 val <<= 3;
6768 val |= *input_line_pointer++ - '0';
6769 }
6770 return negative ? -val : val;
6771 }
6772 }
6773 if (!isdigit (*input_line_pointer))
6774 {
6775 printf (" *input_line_pointer == '%c' 0x%02x\n",
6776 *input_line_pointer, *input_line_pointer);
6777 as_warn ("Invalid number");
6778 return -1;
6779 }
6780 while (isdigit (*input_line_pointer))
6781 {
6782 val *= 10;
6783 val += *input_line_pointer++ - '0';
6784 }
6785 return negative ? -val : val;
6786 }
6787
6788 /* The .file directive; just like the usual .file directive, but there
6789 is an initial number which is the ECOFF file index. */
6790
6791 static void
6792 s_file (x)
6793 int x;
6794 {
6795 int line;
6796
6797 line = get_number ();
6798 s_app_file (0);
6799 }
6800
6801
6802 /* The .end directive. */
6803
6804 static void
6805 s_mipsend (x)
6806 int x;
6807 {
6808 symbolS *p;
6809
6810 if (!is_end_of_line[(unsigned char) *input_line_pointer])
6811 {
6812 p = get_symbol ();
6813 demand_empty_rest_of_line ();
6814 }
6815 else
6816 p = NULL;
6817 if (now_seg != text_section)
6818 as_warn (".end not in text section");
6819 if (!proc_lastP)
6820 {
6821 as_warn (".end and no .ent seen yet.");
6822 return;
6823 }
6824
6825 if (p != NULL)
6826 {
6827 assert (S_GET_NAME (p));
6828 if (strcmp (S_GET_NAME (p), S_GET_NAME (proc_lastP->proc_isym)))
6829 as_warn (".end symbol does not match .ent symbol.");
6830 }
6831
6832 proc_lastP->proc_end = (symbolS *) 1;
6833 }
6834
6835 /* The .aent and .ent directives. */
6836
6837 static void
6838 s_ent (aent)
6839 int aent;
6840 {
6841 int number = 0;
6842 procS *procP;
6843 symbolS *symbolP;
6844
6845 symbolP = get_symbol ();
6846 if (*input_line_pointer == ',')
6847 input_line_pointer++;
6848 SKIP_WHITESPACE ();
6849 if (isdigit (*input_line_pointer) || *input_line_pointer == '-')
6850 number = get_number ();
6851 if (now_seg != text_section)
6852 as_warn (".ent or .aent not in text section.");
6853
6854 if (!aent && proc_lastP && proc_lastP->proc_end == NULL)
6855 as_warn ("missing `.end'");
6856
6857 if (!aent)
6858 {
6859 procP = (procS *) obstack_alloc (&proc_frags, sizeof (*procP));
6860 procP->proc_isym = symbolP;
6861 procP->proc_reg_mask = 0;
6862 procP->proc_reg_offset = 0;
6863 procP->proc_fpreg_mask = 0;
6864 procP->proc_fpreg_offset = 0;
6865 procP->proc_frameoffset = 0;
6866 procP->proc_framereg = 0;
6867 procP->proc_pcreg = 0;
6868 procP->proc_end = NULL;
6869 procP->proc_next = NULL;
6870 if (proc_lastP)
6871 proc_lastP->proc_next = procP;
6872 else
6873 proc_rootP = procP;
6874 proc_lastP = procP;
6875 numprocs++;
6876 }
6877 demand_empty_rest_of_line ();
6878 }
6879
6880 /* The .frame directive. */
6881
6882 #if 0
6883 static void
6884 s_frame (x)
6885 int x;
6886 {
6887 char str[100];
6888 symbolS *symP;
6889 int frame_reg;
6890 int frame_off;
6891 int pcreg;
6892
6893 frame_reg = tc_get_register (1);
6894 if (*input_line_pointer == ',')
6895 input_line_pointer++;
6896 frame_off = get_absolute_expression ();
6897 if (*input_line_pointer == ',')
6898 input_line_pointer++;
6899 pcreg = tc_get_register (0);
6900
6901 /* bob third eye */
6902 assert (proc_rootP);
6903 proc_rootP->proc_framereg = frame_reg;
6904 proc_rootP->proc_frameoffset = frame_off;
6905 proc_rootP->proc_pcreg = pcreg;
6906 /* bob macho .frame */
6907
6908 /* We don't have to write out a frame stab for unoptimized code. */
6909 if (!(frame_reg == FP && frame_off == 0))
6910 {
6911 if (!proc_lastP)
6912 as_warn ("No .ent for .frame to use.");
6913 (void) sprintf (str, "R%d;%d", frame_reg, frame_off);
6914 symP = symbol_new (str, N_VFP, 0, frag_now);
6915 S_SET_TYPE (symP, N_RMASK);
6916 S_SET_OTHER (symP, 0);
6917 S_SET_DESC (symP, 0);
6918 symP->sy_forward = proc_lastP->proc_isym;
6919 /* bob perhaps I should have used pseudo set */
6920 }
6921 demand_empty_rest_of_line ();
6922 }
6923 #endif
6924
6925 /* The .fmask and .mask directives. */
6926
6927 #if 0
6928 static void
6929 s_mask (reg_type)
6930 char reg_type;
6931 {
6932 char str[100], *strP;
6933 symbolS *symP;
6934 int i;
6935 unsigned int mask;
6936 int off;
6937
6938 mask = get_number ();
6939 if (*input_line_pointer == ',')
6940 input_line_pointer++;
6941 off = get_absolute_expression ();
6942
6943 /* bob only for coff */
6944 assert (proc_rootP);
6945 if (reg_type == 'F')
6946 {
6947 proc_rootP->proc_fpreg_mask = mask;
6948 proc_rootP->proc_fpreg_offset = off;
6949 }
6950 else
6951 {
6952 proc_rootP->proc_reg_mask = mask;
6953 proc_rootP->proc_reg_offset = off;
6954 }
6955
6956 /* bob macho .mask + .fmask */
6957
6958 /* We don't have to write out a mask stab if no saved regs. */
6959 if (!(mask == 0))
6960 {
6961 if (!proc_lastP)
6962 as_warn ("No .ent for .mask to use.");
6963 strP = str;
6964 for (i = 0; i < 32; i++)
6965 {
6966 if (mask % 2)
6967 {
6968 sprintf (strP, "%c%d,", reg_type, i);
6969 strP += strlen (strP);
6970 }
6971 mask /= 2;
6972 }
6973 sprintf (strP, ";%d,", off);
6974 symP = symbol_new (str, N_RMASK, 0, frag_now);
6975 S_SET_TYPE (symP, N_RMASK);
6976 S_SET_OTHER (symP, 0);
6977 S_SET_DESC (symP, 0);
6978 symP->sy_forward = proc_lastP->proc_isym;
6979 /* bob perhaps I should have used pseudo set */
6980 }
6981 }
6982 #endif
6983
6984 /* The .loc directive. */
6985
6986 #if 0
6987 static void
6988 s_loc (x)
6989 int x;
6990 {
6991 symbolS *symbolP;
6992 int lineno;
6993 int addroff;
6994
6995 assert (now_seg == text_section);
6996
6997 lineno = get_number ();
6998 addroff = frag_now_fix ();
6999
7000 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
7001 S_SET_TYPE (symbolP, N_SLINE);
7002 S_SET_OTHER (symbolP, 0);
7003 S_SET_DESC (symbolP, lineno);
7004 symbolP->sy_segment = now_seg;
7005 }
7006 #endif
7007
7008 #endif /* ! defined (ECOFF_DEBUGGING) */