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