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