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