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