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