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