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