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