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