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