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