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