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