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