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