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