]> Git Repo - binutils.git/blob - gas/config/tc-mips.c
* config/tc-mips.c (append_insn): Give an error for jumps to a
[binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
3    Contributed by the OSF and Ralph Campbell.
4    Written by Keith Knowles and Ralph Campbell, working independently.
5    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
6    Support.
7
8    This file is part of GAS.
9
10    GAS is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2, or (at your option)
13    any later version.
14
15    GAS is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with GAS; see the file COPYING.  If not, write to the Free
22    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.  */
24
25 #include "as.h"
26 #include "config.h"
27 #include "subsegs.h"
28
29 #include <ctype.h>
30
31 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
37
38 #include "opcode/mips.h"
39
40 #ifdef OBJ_MAYBE_ELF
41 /* Clean up namespace so we can include obj-elf.h too.  */
42 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
43 #undef OBJ_PROCESS_STAB
44 #undef OUTPUT_FLAVOR
45 #undef S_GET_ALIGN
46 #undef S_GET_SIZE
47 #undef S_SET_ALIGN
48 #undef S_SET_SIZE
49 #undef TARGET_SYMBOL_FIELDS
50 #undef obj_frob_file
51 #undef obj_frob_file_after_relocs
52 #undef obj_frob_symbol
53 #undef obj_pop_insert
54 #undef obj_sec_sym_ok_for_reloc
55
56 #include "obj-elf.h"
57 /* Fix any of them that we actually care about.  */
58 #undef OUTPUT_FLAVOR
59 #define OUTPUT_FLAVOR mips_output_flavor()
60 #endif
61
62 #if defined (OBJ_ELF)
63 #include "elf/mips.h"
64 #endif
65
66 #ifndef ECOFF_DEBUGGING
67 #define NO_ECOFF_DEBUGGING
68 #define ECOFF_DEBUGGING 0
69 #endif
70
71 #include "ecoff.h"
72
73 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
74 static char *mips_regmask_frag;
75 #endif
76
77 #define AT  1
78 #define TREG 24
79 #define PIC_CALL_REG 25
80 #define KT0 26
81 #define KT1 27
82 #define GP  28
83 #define SP  29
84 #define FP  30
85 #define RA  31
86
87 #define ILLEGAL_REG (32)
88
89 extern int target_big_endian;
90
91 /* 1 is we should use the 64 bit MIPS ELF ABI, 0 if we should use the
92    32 bit ABI.  This has no meaning for ECOFF.  */
93 static int mips_64;
94
95 /* The default target format to use.  */
96 const char *
97 mips_target_format ()
98 {
99   switch (OUTPUT_FLAVOR)
100     {
101     case bfd_target_aout_flavour:
102       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
103     case bfd_target_ecoff_flavour:
104       return target_big_endian ? "ecoff-bigmips" : "ecoff-littlemips";
105     case bfd_target_elf_flavour:
106       return (target_big_endian
107               ? (mips_64 ? "elf64-bigmips" : "elf32-bigmips")
108               : (mips_64 ? "elf64-littlemips" : "elf32-littlemips"));
109     default:
110       abort ();
111     }
112 }
113
114 /* The name of the readonly data section.  */
115 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
116                             ? ".data" \
117                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
118                             ? ".rdata" \
119                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
120                             ? ".rodata" \
121                             : (abort (), ""))
122
123 /* These variables are filled in with the masks of registers used.
124    The object format code reads them and puts them in the appropriate
125    place.  */
126 unsigned long mips_gprmask;
127 unsigned long mips_cprmask[4];
128
129 /* MIPS ISA (Instruction Set Architecture) level (may be changed
130    temporarily using .set mipsN).  */
131 static int mips_isa = -1;
132
133 /* MIPS ISA we are using for this output file.  */
134 static int file_mips_isa;
135
136 /* Whether we are assembling for the mips16 processor.  */
137 static int mips16 = -1;
138
139 /* The CPU type as a number: 2000, 3000, 4000, 4400, etc.  */
140 static int mips_cpu = -1;
141
142 /* Whether the 4650 instructions (mad/madu) are permitted.  */
143 static int mips_4650 = -1;
144
145 /* Whether the 4010 instructions are permitted.  */
146 static int mips_4010 = -1;
147
148 /* Whether the 4100 MADD16 and DMADD16 are permitted. */
149 static int mips_4100 = -1;
150
151 /* Whether the processor uses hardware interlocks, and thus does not
152    require nops to be inserted.  */
153 static int interlocks = -1;
154
155 /* As with "interlocks" this is used by hardware that has FP
156    (co-processor) interlocks.  */
157 static int cop_interlocks = -1;
158
159 /* MIPS PIC level.  */
160
161 enum mips_pic_level
162 {
163   /* Do not generate PIC code.  */
164   NO_PIC,
165
166   /* Generate PIC code as in Irix 4.  This is not implemented, and I'm
167      not sure what it is supposed to do.  */
168   IRIX4_PIC,
169
170   /* Generate PIC code as in the SVR4 MIPS ABI.  */
171   SVR4_PIC,
172
173   /* Generate PIC code without using a global offset table: the data
174      segment has a maximum size of 64K, all data references are off
175      the $gp register, and all text references are PC relative.  This
176      is used on some embedded systems.  */
177   EMBEDDED_PIC
178 };
179
180 static enum mips_pic_level mips_pic;
181
182 /* 1 if we should generate 32 bit offsets from the GP register in
183    SVR4_PIC mode.  Currently has no meaning in other modes.  */
184 static int mips_big_got;
185
186 /* 1 if trap instructions should used for overflow rather than break
187    instructions.  */
188 static int mips_trap;
189
190 /* 1 if we should autoextend mips16 instructions.  */
191 static int mips16_autoextend = 1;
192
193 static int mips_warn_about_macros;
194 static int mips_noreorder;
195 static int mips_any_noreorder;
196 static int mips_nomove;
197 static int mips_noat;
198 static int mips_nobopt;
199
200 /* The size of the small data section.  */
201 static int g_switch_value = 8;
202 /* Whether the -G option was used.  */
203 static int g_switch_seen = 0;
204
205 #define N_RMASK 0xc4
206 #define N_VFP   0xd4
207
208 /* If we can determine in advance that GP optimization won't be
209    possible, we can skip the relaxation stuff that tries to produce
210    GP-relative references.  This makes delay slot optimization work
211    better.
212
213    This function can only provide a guess, but it seems to work for
214    gcc output.  If it guesses wrong, the only loss should be in
215    efficiency; it shouldn't introduce any bugs.
216
217    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
218    fixed it for the non-PIC mode.  KR 95/04/07  */
219 static int nopic_need_relax PARAMS ((symbolS *));
220
221 /* handle of the OPCODE hash table */
222 static struct hash_control *op_hash = NULL;
223
224 /* The opcode hash table we use for the mips16.  */
225 static struct hash_control *mips16_op_hash = NULL;
226
227 /* This array holds the chars that always start a comment.  If the
228     pre-processor is disabled, these aren't very useful */
229 const char comment_chars[] = "#";
230
231 /* This array holds the chars that only start a comment at the beginning of
232    a line.  If the line seems to have the form '# 123 filename'
233    .line and .file directives will appear in the pre-processed output */
234 /* Note that input_file.c hand checks for '#' at the beginning of the
235    first line of the input file.  This is because the compiler outputs
236    #NO_APP at the beginning of its output. */
237 /* Also note that C style comments are always supported.  */
238 const char line_comment_chars[] = "#";
239
240 /* This array holds machine specific line separator characters. */
241 const char line_separator_chars[] = "";
242
243 /* Chars that can be used to separate mant from exp in floating point nums */
244 const char EXP_CHARS[] = "eE";
245
246 /* Chars that mean this number is a floating point constant */
247 /* As in 0f12.456 */
248 /* or    0d1.2345e12 */
249 const char FLT_CHARS[] = "rRsSfFdDxXpP";
250
251 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
252    changed in read.c .  Ideally it shouldn't have to know about it at all,
253    but nothing is ideal around here.
254  */
255
256 static char *insn_error;
257
258 static int auto_align = 1;
259
260 /* When outputting SVR4 PIC code, the assembler needs to know the
261    offset in the stack frame from which to restore the $gp register.
262    This is set by the .cprestore pseudo-op, and saved in this
263    variable.  */
264 static offsetT mips_cprestore_offset = -1;
265
266 /* This is the register which holds the stack frame, as set by the
267    .frame pseudo-op.  This is needed to implement .cprestore.  */
268 static int mips_frame_reg = SP;
269
270 /* To output NOP instructions correctly, we need to keep information
271    about the previous two instructions.  */
272
273 /* Whether we are optimizing.  The default value of 2 means to remove
274    unneeded NOPs and swap branch instructions when possible.  A value
275    of 1 means to not swap branches.  A value of 0 means to always
276    insert NOPs.  */
277 static int mips_optimize = 2;
278
279 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
280    equivalent to seeing no -g option at all.  */
281 static int mips_debug = 0;
282
283 /* The previous instruction.  */
284 static struct mips_cl_insn prev_insn;
285
286 /* The instruction before prev_insn.  */
287 static struct mips_cl_insn prev_prev_insn;
288
289 /* If we don't want information for prev_insn or prev_prev_insn, we
290    point the insn_mo field at this dummy integer.  */
291 static const struct mips_opcode dummy_opcode = { 0 };
292
293 /* Non-zero if prev_insn is valid.  */
294 static int prev_insn_valid;
295
296 /* The frag for the previous instruction.  */
297 static struct frag *prev_insn_frag;
298
299 /* The offset into prev_insn_frag for the previous instruction.  */
300 static long prev_insn_where;
301
302 /* The reloc type for the previous instruction, if any.  */
303 static bfd_reloc_code_real_type prev_insn_reloc_type;
304
305 /* The reloc for the previous instruction, if any.  */
306 static fixS *prev_insn_fixp;
307
308 /* Non-zero if the previous instruction was in a delay slot.  */
309 static int prev_insn_is_delay_slot;
310
311 /* Non-zero if the previous instruction was in a .set noreorder.  */
312 static int prev_insn_unreordered;
313
314 /* Non-zero if the previous instruction uses an extend opcode (if
315    mips16).  */
316 static int prev_insn_extended;
317
318 /* Non-zero if the previous previous instruction was in a .set
319    noreorder.  */
320 static int prev_prev_insn_unreordered;
321
322 /* For ECOFF and ELF, relocations against symbols are done in two
323    parts, with a HI relocation and a LO relocation.  Each relocation
324    has only 16 bits of space to store an addend.  This means that in
325    order for the linker to handle carries correctly, it must be able
326    to locate both the HI and the LO relocation.  This means that the
327    relocations must appear in order in the relocation table.
328
329    In order to implement this, we keep track of each unmatched HI
330    relocation.  We then sort them so that they immediately precede the
331    corresponding LO relocation. */
332
333 struct mips_hi_fixup
334 {
335   /* Next HI fixup.  */
336   struct mips_hi_fixup *next;
337   /* This fixup.  */
338   fixS *fixp;
339   /* The section this fixup is in.  */
340   segT seg;
341 };
342
343 /* The list of unmatched HI relocs.  */
344
345 static struct mips_hi_fixup *mips_hi_fixup_list;
346
347 /* Map normal MIPS register numbers to mips16 register numbers.  */
348
349 #define X ILLEGAL_REG
350 static const int mips32_to_16_reg_map[] =
351 {
352   X, X, 2, 3, 4, 5, 6, 7,
353   X, X, X, X, X, X, X, X,
354   0, 1, X, X, X, X, X, X,
355   X, X, X, X, X, X, X, X
356 };
357 #undef X
358
359 /* Map mips16 register numbers to normal MIPS register numbers.  */
360
361 static const int mips16_to_32_reg_map[] =
362 {
363   16, 17, 2, 3, 4, 5, 6, 7
364 };
365 \f
366 /* Since the MIPS does not have multiple forms of PC relative
367    instructions, we do not have to do relaxing as is done on other
368    platforms.  However, we do have to handle GP relative addressing
369    correctly, which turns out to be a similar problem.
370
371    Every macro that refers to a symbol can occur in (at least) two
372    forms, one with GP relative addressing and one without.  For
373    example, loading a global variable into a register generally uses
374    a macro instruction like this:
375      lw $4,i
376    If i can be addressed off the GP register (this is true if it is in
377    the .sbss or .sdata section, or if it is known to be smaller than
378    the -G argument) this will generate the following instruction:
379      lw $4,i($gp)
380    This instruction will use a GPREL reloc.  If i can not be addressed
381    off the GP register, the following instruction sequence will be used:
382      lui $at,i
383      lw $4,i($at)
384    In this case the first instruction will have a HI16 reloc, and the
385    second reloc will have a LO16 reloc.  Both relocs will be against
386    the symbol i.
387
388    The issue here is that we may not know whether i is GP addressable
389    until after we see the instruction that uses it.  Therefore, we
390    want to be able to choose the final instruction sequence only at
391    the end of the assembly.  This is similar to the way other
392    platforms choose the size of a PC relative instruction only at the
393    end of assembly.
394
395    When generating position independent code we do not use GP
396    addressing in quite the same way, but the issue still arises as
397    external symbols and local symbols must be handled differently.
398
399    We handle these issues by actually generating both possible
400    instruction sequences.  The longer one is put in a frag_var with
401    type rs_machine_dependent.  We encode what to do with the frag in
402    the subtype field.  We encode (1) the number of existing bytes to
403    replace, (2) the number of new bytes to use, (3) the offset from
404    the start of the existing bytes to the first reloc we must generate
405    (that is, the offset is applied from the start of the existing
406    bytes after they are replaced by the new bytes, if any), (4) the
407    offset from the start of the existing bytes to the second reloc,
408    (5) whether a third reloc is needed (the third reloc is always four
409    bytes after the second reloc), and (6) whether to warn if this
410    variant is used (this is sometimes needed if .set nomacro or .set
411    noat is in effect).  All these numbers are reasonably small.
412
413    Generating two instruction sequences must be handled carefully to
414    ensure that delay slots are handled correctly.  Fortunately, there
415    are a limited number of cases.  When the second instruction
416    sequence is generated, append_insn is directed to maintain the
417    existing delay slot information, so it continues to apply to any
418    code after the second instruction sequence.  This means that the
419    second instruction sequence must not impose any requirements not
420    required by the first instruction sequence.
421
422    These variant frags are then handled in functions called by the
423    machine independent code.  md_estimate_size_before_relax returns
424    the final size of the frag.  md_convert_frag sets up the final form
425    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
426    one if needed.  */
427 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
428   ((relax_substateT) \
429    (((old) << 23) \
430     | ((new) << 16) \
431     | (((reloc1) + 64) << 9) \
432     | (((reloc2) + 64) << 2) \
433     | ((reloc3) ? (1 << 1) : 0) \
434     | ((warn) ? 1 : 0)))
435 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
436 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
437 #define RELAX_RELOC1(i) ((bfd_vma)(((i) >> 9) & 0x7f) - 64)
438 #define RELAX_RELOC2(i) ((bfd_vma)(((i) >> 2) & 0x7f) - 64)
439 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
440 #define RELAX_WARN(i) ((i) & 1)
441
442 /* For mips16 code, we use an entirely different form of relaxation.
443    mips16 supports two versions of most instructions which take
444    immediate values: a small one which takes some small value, and a
445    larger one which takes a 16 bit value.  Since branches also follow
446    this pattern, relaxing these values is required.
447
448    We can assemble both mips16 and normal MIPS code in a single
449    object.  Therefore, we need to support this type of relaxation at
450    the same time that we support the relaxation described above.  We
451    use the high bit of the subtype field to distinguish these cases.
452
453    The information we store for this type of relaxation is the
454    argument code found in the opcode file for this relocation, whether
455    the user explicitly requested a small or extended form, and whether
456    the relocation is in a jump or jal delay slot.  That tells us the
457    size of the value, and how it should be stored.  We also store
458    whether the fragment is considered to be extended or not.  We also
459    store whether this is known to be a branch to a different section,
460    whether we have tried to relax this frag yet, and whether we have
461    ever extended a PC relative fragment because of a shift count.  */
462 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
463   (0x80000000                                                   \
464    | ((type) & 0xff)                                            \
465    | ((small) ? 0x100 : 0)                                      \
466    | ((ext) ? 0x200 : 0)                                        \
467    | ((dslot) ? 0x400 : 0)                                      \
468    | ((jal_dslot) ? 0x800 : 0))
469 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
470 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
471 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
472 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
473 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
474 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
475 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
476 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
477 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
478 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
479 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
480 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
481 \f
482 /* Prototypes for static functions.  */
483
484 #ifdef __STDC__
485 #define internalError() \
486     as_fatal ("internal Error, line %d, %s", __LINE__, __FILE__)
487 #else
488 #define internalError() as_fatal ("MIPS internal Error");
489 #endif
490
491 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
492
493 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
494                                   unsigned int reg, enum mips_regclass class));
495 static int reg_needs_delay PARAMS ((int));
496 static void append_insn PARAMS ((char *place,
497                                  struct mips_cl_insn * ip,
498                                  expressionS * p,
499                                  bfd_reloc_code_real_type r,
500                                  boolean));
501 static void mips_no_prev_insn PARAMS ((void));
502 static void mips_emit_delays PARAMS ((boolean));
503 #ifdef USE_STDARG
504 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
505                                  const char *name, const char *fmt,
506                                  ...));
507 #else
508 static void macro_build ();
509 #endif
510 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
511                                         const char *, const char *,
512                                         va_list));
513 static void macro_build_lui PARAMS ((char *place, int *counter,
514                                      expressionS * ep, int regnum));
515 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
516 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
517                                          expressionS *));
518 static void load_register PARAMS ((int *, int, expressionS *, int));
519 static void load_address PARAMS ((int *counter, int reg, expressionS *ep));
520 static void macro PARAMS ((struct mips_cl_insn * ip));
521 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
522 #ifdef LOSING_COMPILER
523 static void macro2 PARAMS ((struct mips_cl_insn * ip));
524 #endif
525 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
526 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
527 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
528                                   boolean, boolean, unsigned long *,
529                                   boolean *, unsigned short *));
530 static int my_getSmallExpression PARAMS ((expressionS * ep, char *str));
531 static void my_getExpression PARAMS ((expressionS * ep, char *str));
532 static symbolS *get_symbol PARAMS ((void));
533 static void mips_align PARAMS ((int to, int fill, symbolS *label));
534 static void s_align PARAMS ((int));
535 static void s_change_sec PARAMS ((int));
536 static void s_cons PARAMS ((int));
537 static void s_float_cons PARAMS ((int));
538 static void s_mips_globl PARAMS ((int));
539 static void s_option PARAMS ((int));
540 static void s_mipsset PARAMS ((int));
541 static void s_abicalls PARAMS ((int));
542 static void s_cpload PARAMS ((int));
543 static void s_cprestore PARAMS ((int));
544 static void s_gpword PARAMS ((int));
545 static void s_cpadd PARAMS ((int));
546 static void md_obj_begin PARAMS ((void));
547 static void md_obj_end PARAMS ((void));
548 static long get_number PARAMS ((void));
549 static void s_ent PARAMS ((int));
550 static void s_mipsend PARAMS ((int));
551 static void s_file PARAMS ((int));
552 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
553 \f
554 /* Pseudo-op table.
555
556    The following pseudo-ops from the Kane and Heinrich MIPS book
557    should be defined here, but are currently unsupported: .alias,
558    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
559
560    The following pseudo-ops from the Kane and Heinrich MIPS book are
561    specific to the type of debugging information being generated, and
562    should be defined by the object format: .aent, .begin, .bend,
563    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
564    .vreg.
565
566    The following pseudo-ops from the Kane and Heinrich MIPS book are
567    not MIPS CPU specific, but are also not specific to the object file
568    format.  This file is probably the best place to define them, but
569    they are not currently supported: .asm0, .endr, .lab, .repeat,
570    .struct, .weakext.  */
571
572 static const pseudo_typeS mips_pseudo_table[] =
573 {
574  /* MIPS specific pseudo-ops.  */
575   {"option", s_option, 0},
576   {"set", s_mipsset, 0},
577   {"rdata", s_change_sec, 'r'},
578   {"sdata", s_change_sec, 's'},
579   {"livereg", s_ignore, 0},
580   {"abicalls", s_abicalls, 0},
581   {"cpload", s_cpload, 0},
582   {"cprestore", s_cprestore, 0},
583   {"gpword", s_gpword, 0},
584   {"cpadd", s_cpadd, 0},
585
586  /* Relatively generic pseudo-ops that happen to be used on MIPS
587      chips.  */
588   {"asciiz", stringer, 1},
589   {"bss", s_change_sec, 'b'},
590   {"err", s_err, 0},
591   {"half", s_cons, 1},
592   {"dword", s_cons, 3},
593
594  /* These pseudo-ops are defined in read.c, but must be overridden
595      here for one reason or another.  */
596   {"align", s_align, 0},
597   {"byte", s_cons, 0},
598   {"data", s_change_sec, 'd'},
599   {"double", s_float_cons, 'd'},
600   {"float", s_float_cons, 'f'},
601   {"globl", s_mips_globl, 0},
602   {"global", s_mips_globl, 0},
603   {"hword", s_cons, 1},
604   {"int", s_cons, 2},
605   {"long", s_cons, 2},
606   {"octa", s_cons, 4},
607   {"quad", s_cons, 3},
608   {"short", s_cons, 1},
609   {"single", s_float_cons, 'f'},
610   {"text", s_change_sec, 't'},
611   {"word", s_cons, 2},
612   { 0 },
613 };
614
615 static const pseudo_typeS mips_nonecoff_pseudo_table[] = {
616  /* These pseudo-ops should be defined by the object file format.
617     However, a.out doesn't support them, so we have versions here.  */
618   {"aent", s_ent, 1},
619   {"bgnb", s_ignore, 0},
620   {"end", s_mipsend, 0},
621   {"endb", s_ignore, 0},
622   {"ent", s_ent, 0},
623   {"file", s_file, 0},
624   {"fmask", s_ignore, 'F'},
625   {"frame", s_ignore, 0},
626   {"loc", s_ignore, 0},
627   {"mask", s_ignore, 'R'},
628   {"verstamp", s_ignore, 0},
629   { 0 },
630 };
631
632 extern void pop_insert PARAMS ((const pseudo_typeS *));
633
634 void
635 mips_pop_insert ()
636 {
637   pop_insert (mips_pseudo_table);
638   if (! ECOFF_DEBUGGING)
639     pop_insert (mips_nonecoff_pseudo_table);
640 }
641 \f
642 /* Symbols labelling the current insn.  */
643
644 struct insn_label_list
645 {
646   struct insn_label_list *next;
647   symbolS *label;
648 };
649
650 static struct insn_label_list *insn_labels;
651 static struct insn_label_list *free_insn_labels;
652
653 static void mips_clear_insn_labels PARAMS ((void));
654
655 static inline void
656 mips_clear_insn_labels ()
657 {
658   register struct insn_label_list **pl;
659
660   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
661     ;
662   *pl = insn_labels;
663   insn_labels = NULL;
664 }
665 \f
666 static char *expr_end;
667
668 /* Expressions which appear in instructions.  These are set by
669    mips_ip.  */
670
671 static expressionS imm_expr;
672 static expressionS offset_expr;
673
674 /* Relocs associated with imm_expr and offset_expr.  */
675
676 static bfd_reloc_code_real_type imm_reloc;
677 static bfd_reloc_code_real_type offset_reloc;
678
679 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
680
681 static boolean imm_unmatched_hi;
682
683 /* These are set by mips16_ip if an explicit extension is used.  */
684
685 static boolean mips16_small, mips16_ext;
686
687 /*
688  * This function is called once, at assembler startup time.  It should
689  * set up all the tables, etc. that the MD part of the assembler will need.
690  */
691 void
692 md_begin ()
693 {
694   boolean ok = false;
695   register const char *retval = NULL;
696   register unsigned int i = 0;
697
698   if (mips_isa == -1)
699     {
700       const char *cpu;
701       char *a = NULL;
702
703       cpu = TARGET_CPU;
704       if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
705         {
706           a = xmalloc (sizeof TARGET_CPU);
707           strcpy (a, TARGET_CPU);
708           a[(sizeof TARGET_CPU) - 3] = '\0';
709           cpu = a;
710         }
711
712       if (strcmp (cpu, "mips") == 0)
713         {
714           mips_isa = 1;
715           if (mips_cpu == -1)
716             mips_cpu = 3000;
717         }
718       else if (strcmp (cpu, "r6000") == 0
719                || strcmp (cpu, "mips2") == 0)
720         {
721           mips_isa = 2;
722           if (mips_cpu == -1)
723             mips_cpu = 6000;
724         }
725       else if (strcmp (cpu, "mips64") == 0
726                || strcmp (cpu, "r4000") == 0
727                || strcmp (cpu, "mips3") == 0)
728         {
729           mips_isa = 3;
730           if (mips_cpu == -1)
731             mips_cpu = 4000;
732         }
733       else if (strcmp (cpu, "r4400") == 0)
734         {
735           mips_isa = 3;
736           if (mips_cpu == -1)
737             mips_cpu = 4400;
738         }
739       else if (strcmp (cpu, "mips64orion") == 0
740                || strcmp (cpu, "r4600") == 0)
741         {
742           mips_isa = 3;
743           if (mips_cpu == -1)
744             mips_cpu = 4600;
745         }
746       else if (strcmp (cpu, "r4650") == 0)
747         {
748           mips_isa = 3;
749           if (mips_cpu == -1)
750             mips_cpu = 4650;
751           if (mips_4650 == -1)
752             mips_4650 = 1;
753         }
754       else if (strcmp (cpu, "mips64vr4300") == 0)
755         {
756           mips_isa = 3;
757           if (mips_cpu == -1)
758             mips_cpu = 4300;
759         }
760       else if (strcmp (cpu, "mips64vr4100") == 0)
761         {
762           mips_isa = 3;
763           if (mips_cpu == -1)
764             mips_cpu = 4100;
765           if (mips_4100 == -1)
766             mips_4100 = 1;
767         }
768       else if (strcmp (cpu, "r4010") == 0)
769         {
770           mips_isa = 2;
771           if (mips_cpu == -1)
772             mips_cpu = 4010;
773           if (mips_4010 == -1)
774             mips_4010 = 1;
775         }
776       else if (strcmp (cpu, "r5000") == 0
777                || strcmp (cpu, "mips64vr5000") == 0)
778         {
779           mips_isa = 4;
780           if (mips_cpu == -1)
781             mips_cpu = 5000;
782         }
783       else if (strcmp (cpu, "r8000") == 0
784                || strcmp (cpu, "mips4") == 0)
785         {
786           mips_isa = 4;
787           if (mips_cpu == -1)
788             mips_cpu = 8000;
789         }
790       else if (strcmp (cpu, "r10000") == 0)
791         {
792           mips_isa = 4;
793           if (mips_cpu == -1)
794             mips_cpu = 10000;
795         }
796       else if (strcmp (cpu, "mips16") == 0)
797         {
798           mips_isa = 3;
799           if (mips_cpu == -1)
800             mips_cpu = 0; /* FIXME */
801         }
802       else
803         {
804           mips_isa = 1;
805           if (mips_cpu == -1)
806             mips_cpu = 3000;
807         }
808
809       if (a != NULL)
810         free (a);
811     }
812
813   if (mips16 < 0)
814     {
815       if (strncmp (TARGET_CPU, "mips16", sizeof "mips16" - 1) == 0)
816         mips16 = 1;
817       else
818         mips16 = 0;
819     }
820
821   if (mips_4650 < 0)
822     mips_4650 = 0;
823
824   if (mips_4010 < 0)
825     mips_4010 = 0;
826
827   if (mips_4100 < 0)
828     mips_4100 = 0;
829
830   if (mips_4010 || mips_4100 || mips_cpu == 4300)
831     interlocks = 1;
832   else
833     interlocks = 0;
834
835   if (mips_cpu == 4300)
836     cop_interlocks = 1;
837   else
838     cop_interlocks = 0;
839
840   if (mips_isa < 2 && mips_trap)
841     as_bad ("trap exception not supported at ISA 1");
842
843   switch (mips_isa)
844     {
845     case 1:
846       ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 3000);
847       break;
848     case 2:
849       ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 6000);
850       break;
851     case 3:
852       ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 4000);
853       break;
854     case 4:
855       ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 8000);
856       break;
857     }
858   if (! ok)
859     as_warn ("Could not set architecture and machine");
860
861   file_mips_isa = mips_isa;
862
863   op_hash = hash_new ();
864
865   for (i = 0; i < NUMOPCODES;)
866     {
867       const char *name = mips_opcodes[i].name;
868
869       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
870       if (retval != NULL)
871         {
872           fprintf (stderr, "internal error: can't hash `%s': %s\n",
873                    mips_opcodes[i].name, retval);
874           as_fatal ("Broken assembler.  No assembly attempted.");
875         }
876       do
877         {
878           if (mips_opcodes[i].pinfo != INSN_MACRO
879               && ((mips_opcodes[i].match & mips_opcodes[i].mask)
880                   != mips_opcodes[i].match))
881             {
882               fprintf (stderr, "internal error: bad opcode: `%s' \"%s\"\n",
883                        mips_opcodes[i].name, mips_opcodes[i].args);
884               as_fatal ("Broken assembler.  No assembly attempted.");
885             }
886           ++i;
887         }
888       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
889     }
890
891   mips16_op_hash = hash_new ();
892
893   i = 0;
894   while (i < bfd_mips16_num_opcodes)
895     {
896       const char *name = mips16_opcodes[i].name;
897
898       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
899       if (retval != NULL)
900         as_fatal ("internal error: can't hash `%s': %s\n",
901                   mips16_opcodes[i].name, retval);
902       do
903         {
904           if (mips16_opcodes[i].pinfo != INSN_MACRO
905               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
906                   != mips16_opcodes[i].match))
907             as_fatal ("internal error: bad opcode: `%s' \"%s\"\n",
908                       mips16_opcodes[i].name, mips16_opcodes[i].args);
909           ++i;
910         }
911       while (i < bfd_mips16_num_opcodes
912              && strcmp (mips16_opcodes[i].name, name) == 0);
913     }
914
915   mips_no_prev_insn ();
916
917   mips_gprmask = 0;
918   mips_cprmask[0] = 0;
919   mips_cprmask[1] = 0;
920   mips_cprmask[2] = 0;
921   mips_cprmask[3] = 0;
922
923   /* set the default alignment for the text section (2**2) */
924   record_alignment (text_section, 2);
925
926   if (USE_GLOBAL_POINTER_OPT)
927     bfd_set_gp_size (stdoutput, g_switch_value);
928
929   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
930     {
931       /* On a native system, sections must be aligned to 16 byte
932          boundaries.  When configured for an embedded ELF target, we
933          don't bother.  */
934       if (strcmp (TARGET_OS, "elf") != 0)
935         {
936           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
937           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
938           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
939         }
940
941       /* Create a .reginfo section for register masks and a .mdebug
942          section for debugging information.  */
943       {
944         segT seg;
945         subsegT subseg;
946         flagword flags;
947         segT sec;
948
949         seg = now_seg;
950         subseg = now_subseg;
951
952         /* The ABI says this section should be loaded so that the
953            running program can access it.  However, we don't load it
954            if we are configured for an embedded target */
955         flags = SEC_READONLY | SEC_DATA;
956         if (strcmp (TARGET_OS, "elf") != 0)
957           flags |= SEC_ALLOC | SEC_LOAD;
958
959         if (! mips_64)
960           {
961             sec = subseg_new (".reginfo", (subsegT) 0);
962
963
964             (void) bfd_set_section_flags (stdoutput, sec, flags);
965             (void) bfd_set_section_alignment (stdoutput, sec, 2);
966         
967 #ifdef OBJ_ELF
968             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
969 #endif
970           }
971         else
972           {
973             /* The 64-bit ABI uses a .MIPS.options section rather than
974                .reginfo section.  */
975             sec = subseg_new (".MIPS.options", (subsegT) 0);
976             (void) bfd_set_section_flags (stdoutput, sec, flags);
977             (void) bfd_set_section_alignment (stdoutput, sec, 3);
978
979 #ifdef OBJ_ELF
980             /* Set up the option header.  */
981             {
982               Elf_Internal_Options opthdr;
983               char *f;
984
985               opthdr.kind = ODK_REGINFO;
986               opthdr.size = (sizeof (Elf_External_Options)
987                              + sizeof (Elf64_External_RegInfo));
988               opthdr.section = 0;
989               opthdr.info = 0;
990               f = frag_more (sizeof (Elf_External_Options));
991               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
992                                              (Elf_External_Options *) f);
993
994               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
995             }
996 #endif
997           }
998
999         if (ECOFF_DEBUGGING)
1000           {
1001             sec = subseg_new (".mdebug", (subsegT) 0);
1002             (void) bfd_set_section_flags (stdoutput, sec,
1003                                           SEC_HAS_CONTENTS | SEC_READONLY);
1004             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1005           }
1006
1007         subseg_set (seg, subseg);
1008       }
1009     }
1010
1011   if (! ECOFF_DEBUGGING)
1012     md_obj_begin ();
1013 }
1014
1015 void
1016 md_mips_end ()
1017 {
1018   if (! ECOFF_DEBUGGING)
1019     md_obj_end ();
1020 }
1021
1022 void
1023 md_assemble (str)
1024      char *str;
1025 {
1026   struct mips_cl_insn insn;
1027
1028   imm_expr.X_op = O_absent;
1029   imm_reloc = BFD_RELOC_UNUSED;
1030   imm_unmatched_hi = false;
1031   offset_expr.X_op = O_absent;
1032   offset_reloc = BFD_RELOC_UNUSED;
1033
1034   if (mips16)
1035     mips16_ip (str, &insn);
1036   else
1037     mips_ip (str, &insn);
1038
1039   if (insn_error)
1040     {
1041       as_bad ("%s `%s'", insn_error, str);
1042       return;
1043     }
1044
1045   if (insn.insn_mo->pinfo == INSN_MACRO)
1046     {
1047       if (mips16)
1048         mips16_macro (&insn);
1049       else
1050         macro (&insn);
1051     }
1052   else
1053     {
1054       if (imm_expr.X_op != O_absent)
1055         append_insn ((char *) NULL, &insn, &imm_expr, imm_reloc,
1056                      imm_unmatched_hi);
1057       else if (offset_expr.X_op != O_absent)
1058         append_insn ((char *) NULL, &insn, &offset_expr, offset_reloc, false);
1059       else
1060         append_insn ((char *) NULL, &insn, NULL, BFD_RELOC_UNUSED, false);
1061     }
1062 }
1063
1064 /* See whether instruction IP reads register REG.  CLASS is the type
1065    of register.  */
1066
1067 static int
1068 insn_uses_reg (ip, reg, class)
1069      struct mips_cl_insn *ip;
1070      unsigned int reg;
1071      enum mips_regclass class;
1072 {
1073   if (class == MIPS16_REG)
1074     {
1075       assert (mips16);
1076       reg = mips16_to_32_reg_map[reg];
1077       class = MIPS_GR_REG;
1078     }
1079
1080   /* Don't report on general register 0, since it never changes.  */
1081   if (class == MIPS_GR_REG && reg == 0)
1082     return 0;
1083
1084   if (class == MIPS_FP_REG)
1085     {
1086       assert (! mips16);
1087       /* If we are called with either $f0 or $f1, we must check $f0.
1088          This is not optimal, because it will introduce an unnecessary
1089          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1090          need to distinguish reading both $f0 and $f1 or just one of
1091          them.  Note that we don't have to check the other way,
1092          because there is no instruction that sets both $f0 and $f1
1093          and requires a delay.  */
1094       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1095           && (((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS)
1096               == (reg &~ (unsigned) 1)))
1097         return 1;
1098       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1099           && (((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT)
1100               == (reg &~ (unsigned) 1)))
1101         return 1;
1102     }
1103   else if (! mips16)
1104     {
1105       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1106           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1107         return 1;
1108       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1109           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1110         return 1;
1111     }
1112   else
1113     {
1114       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1115           && ((ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX) == reg)
1116         return 1;
1117       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1118           && ((ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY) == reg)
1119         return 1;
1120       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1121           && ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1122               & MIPS16OP_MASK_MOVE32Z) == reg)
1123         return 1;
1124       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1125         return 1;
1126       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1127         return 1;
1128       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1129         return 1;
1130       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1131           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1132               & MIPS16OP_MASK_REGR32) == reg)
1133         return 1;
1134     }
1135
1136   return 0;
1137 }
1138
1139 /* This function returns true if modifying a register requires a
1140    delay.  */
1141
1142 static int
1143 reg_needs_delay (reg)
1144      int reg;
1145 {
1146   unsigned long prev_pinfo;
1147
1148   prev_pinfo = prev_insn.insn_mo->pinfo;
1149   if (! mips_noreorder
1150       && mips_isa < 4
1151       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1152           || (mips_isa < 2
1153               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1154     {
1155       /* A load from a coprocessor or from memory.  All load
1156          delays delay the use of general register rt for one
1157          instruction on the r3000.  The r6000 and r4000 use
1158          interlocks.  */
1159       know (prev_pinfo & INSN_WRITE_GPR_T);
1160       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1161         return 1;
1162     }
1163
1164   return 0;
1165 }
1166
1167 /* Output an instruction.  PLACE is where to put the instruction; if
1168    it is NULL, this uses frag_more to get room.  IP is the instruction
1169    information.  ADDRESS_EXPR is an operand of the instruction to be
1170    used with RELOC_TYPE.  */
1171
1172 static void
1173 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1174      char *place;
1175      struct mips_cl_insn *ip;
1176      expressionS *address_expr;
1177      bfd_reloc_code_real_type reloc_type;
1178      boolean unmatched_hi;
1179 {
1180   register unsigned long prev_pinfo, pinfo;
1181   char *f;
1182   fixS *fixp;
1183   int nops = 0;
1184
1185   /* Mark instruction labels in mips16 mode.  This permits the linker
1186      to handle them specially, such as generating jalx instructions
1187      when needed.  We also make them odd for the duration of the
1188      assembly, in order to generate the right sort of code.  We will
1189      make them even in the adjust_symtab routine, while leaving them
1190      marked.  This is convenient for the debugger and the
1191      disassembler.  The linker knows to make them odd again.  */
1192   if (mips16)
1193     {
1194       struct insn_label_list *l;
1195
1196       for (l = insn_labels; l != NULL; l = l->next)
1197         {
1198 #ifdef S_SET_OTHER
1199           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1200             S_SET_OTHER (l->label, STO_MIPS16);
1201 #endif
1202           ++l->label->sy_value.X_add_number;
1203         }
1204     }
1205
1206   prev_pinfo = prev_insn.insn_mo->pinfo;
1207   pinfo = ip->insn_mo->pinfo;
1208
1209   if (place == NULL && ! mips_noreorder)
1210     {
1211       /* If the previous insn required any delay slots, see if we need
1212          to insert a NOP or two.  There are eight kinds of possible
1213          hazards, of which an instruction can have at most one type.
1214          (1) a load from memory delay
1215          (2) a load from a coprocessor delay
1216          (3) an unconditional branch delay
1217          (4) a conditional branch delay
1218          (5) a move to coprocessor register delay
1219          (6) a load coprocessor register from memory delay
1220          (7) a coprocessor condition code delay
1221          (8) a HI/LO special register delay
1222
1223          There are a lot of optimizations we could do that we don't.
1224          In particular, we do not, in general, reorder instructions.
1225          If you use gcc with optimization, it will reorder
1226          instructions and generally do much more optimization then we
1227          do here; repeating all that work in the assembler would only
1228          benefit hand written assembly code, and does not seem worth
1229          it.  */
1230
1231       /* This is how a NOP is emitted.  */
1232 #define emit_nop()                                      \
1233   (mips16                                               \
1234    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1235    : md_number_to_chars (frag_more (4), 0, 4))
1236
1237       /* The previous insn might require a delay slot, depending upon
1238          the contents of the current insn.  */
1239       if (! mips16
1240           && mips_isa < 4
1241           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1242                && ! cop_interlocks)
1243               || (mips_isa < 2
1244                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1245         {
1246           /* A load from a coprocessor or from memory.  All load
1247              delays delay the use of general register rt for one
1248              instruction on the r3000.  The r6000 and r4000 use
1249              interlocks.  */
1250           know (prev_pinfo & INSN_WRITE_GPR_T);
1251           if (mips_optimize == 0
1252               || insn_uses_reg (ip,
1253                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1254                                  & OP_MASK_RT),
1255                                 MIPS_GR_REG))
1256             ++nops;
1257         }
1258       else if (! mips16
1259                && mips_isa < 4
1260                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1261                     && ! cop_interlocks)
1262                    || (mips_isa < 2
1263                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1264         {
1265           /* A generic coprocessor delay.  The previous instruction
1266              modified a coprocessor general or control register.  If
1267              it modified a control register, we need to avoid any
1268              coprocessor instruction (this is probably not always
1269              required, but it sometimes is).  If it modified a general
1270              register, we avoid using that register.
1271
1272              On the r6000 and r4000 loading a coprocessor register
1273              from memory is interlocked, and does not require a delay.
1274
1275              This case is not handled very well.  There is no special
1276              knowledge of CP0 handling, and the coprocessors other
1277              than the floating point unit are not distinguished at
1278              all.  */
1279           if (prev_pinfo & INSN_WRITE_FPR_T)
1280             {
1281               if (mips_optimize == 0
1282                   || insn_uses_reg (ip,
1283                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1284                                      & OP_MASK_FT),
1285                                     MIPS_FP_REG))
1286                 ++nops;
1287             }
1288           else if (prev_pinfo & INSN_WRITE_FPR_S)
1289             {
1290               if (mips_optimize == 0
1291                   || insn_uses_reg (ip,
1292                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1293                                      & OP_MASK_FS),
1294                                     MIPS_FP_REG))
1295                 ++nops;
1296             }
1297           else
1298             {
1299               /* We don't know exactly what the previous instruction
1300                  does.  If the current instruction uses a coprocessor
1301                  register, we must insert a NOP.  If previous
1302                  instruction may set the condition codes, and the
1303                  current instruction uses them, we must insert two
1304                  NOPS.  */
1305               if (mips_optimize == 0
1306                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1307                       && (pinfo & INSN_READ_COND_CODE)))
1308                 nops += 2;
1309               else if (pinfo & INSN_COP)
1310                 ++nops;
1311             }
1312         }
1313       else if (! mips16
1314                && mips_isa < 4
1315                && (prev_pinfo & INSN_WRITE_COND_CODE)
1316                && ! cop_interlocks)
1317         {
1318           /* The previous instruction sets the coprocessor condition
1319              codes, but does not require a general coprocessor delay
1320              (this means it is a floating point comparison
1321              instruction).  If this instruction uses the condition
1322              codes, we need to insert a single NOP.  */
1323           if (mips_optimize == 0
1324               || (pinfo & INSN_READ_COND_CODE))
1325             ++nops;
1326         }
1327       else if (prev_pinfo & INSN_READ_LO)
1328         {
1329           /* The previous instruction reads the LO register; if the
1330              current instruction writes to the LO register, we must
1331              insert two NOPS.  Some newer processors have interlocks.  */
1332           if (! interlocks
1333               && (mips_optimize == 0
1334                   || (pinfo & INSN_WRITE_LO)))
1335             nops += 2;
1336         }
1337       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1338         {
1339           /* The previous instruction reads the HI register; if the
1340              current instruction writes to the HI register, we must
1341              insert a NOP.  Some newer processors have interlocks.  */
1342           if (! interlocks
1343               && (mips_optimize == 0
1344                   || (pinfo & INSN_WRITE_HI)))
1345             nops += 2;
1346         }
1347
1348       /* There are two cases which require two intervening
1349          instructions: 1) setting the condition codes using a move to
1350          coprocessor instruction which requires a general coprocessor
1351          delay and then reading the condition codes 2) reading the HI
1352          or LO register and then writing to it (except on processors
1353          which have interlocks).  If we are not already emitting a NOP
1354          instruction, we must check for these cases compared to the
1355          instruction previous to the previous instruction.  */
1356       if (nops == 0
1357           && ((! mips16
1358                && mips_isa < 4
1359                && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1360                && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1361                && (pinfo & INSN_READ_COND_CODE)
1362                && ! cop_interlocks)
1363               || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1364                   && (pinfo & INSN_WRITE_LO)
1365                   && ! interlocks)
1366               || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1367                   && (pinfo & INSN_WRITE_HI)
1368                   && ! interlocks)))
1369         ++nops;
1370
1371       /* If we are being given a nop instruction, don't bother with
1372          one of the nops we would otherwise output.  This will only
1373          happen when a nop instruction is used with mips_optimize set
1374          to 0.  */
1375       if (nops > 0 && ip->insn_opcode == (mips16 ? 0x6500 : 0))
1376         --nops;
1377
1378       /* Now emit the right number of NOP instructions.  */
1379       if (nops > 0)
1380         {
1381           fragS *old_frag;
1382           unsigned long old_frag_offset;
1383           int i;
1384           struct insn_label_list *l;
1385
1386           old_frag = frag_now;
1387           old_frag_offset = frag_now_fix ();
1388
1389           for (i = 0; i < nops; i++)
1390             emit_nop ();
1391
1392           if (listing)
1393             {
1394               listing_prev_line ();
1395               /* We may be at the start of a variant frag.  In case we
1396                  are, make sure there is enough space for the frag
1397                  after the frags created by listing_prev_line.  The
1398                  argument to frag_grow here must be at least as large
1399                  as the argument to all other calls to frag_grow in
1400                  this file.  We don't have to worry about being in the
1401                  middle of a variant frag, because the variants insert
1402                  all needed nop instructions themselves.  */
1403               frag_grow (40);
1404             }
1405
1406           for (l = insn_labels; l != NULL; l = l->next)
1407             {
1408               assert (S_GET_SEGMENT (l->label) == now_seg);
1409               l->label->sy_frag = frag_now;
1410               S_SET_VALUE (l->label, (valueT) frag_now_fix ());
1411               /* mips16 text labels are stored as odd.  */
1412               if (mips16)
1413                 ++l->label->sy_value.X_add_number;
1414             }
1415
1416 #ifndef NO_ECOFF_DEBUGGING
1417           if (ECOFF_DEBUGGING)
1418             ecoff_fix_loc (old_frag, old_frag_offset);
1419 #endif
1420         }
1421     }
1422   
1423   if (reloc_type > BFD_RELOC_UNUSED)
1424     {
1425       /* We need to set up a variant frag.  */
1426       assert (mips16 && address_expr != NULL);
1427       f = frag_var (rs_machine_dependent, 4, 0,
1428                     RELAX_MIPS16_ENCODE (reloc_type - BFD_RELOC_UNUSED,
1429                                          mips16_small, mips16_ext,
1430                                          (prev_pinfo
1431                                           & INSN_UNCOND_BRANCH_DELAY),
1432                                          (prev_insn_reloc_type
1433                                           == BFD_RELOC_MIPS16_JMP)),
1434                     make_expr_symbol (address_expr), (long) 0,
1435                     (char *) NULL);
1436     }
1437   else if (place != NULL)
1438     f = place;
1439   else if (mips16 && ! ip->use_extend && reloc_type != BFD_RELOC_MIPS16_JMP)
1440     {
1441       /* Make sure there is enough room to swap this instruction with
1442          a following jump instruction.  */
1443       frag_grow (6);
1444       f = frag_more (2);
1445     }
1446   else
1447     f = frag_more (4);
1448   fixp = NULL;
1449   if (address_expr != NULL && reloc_type < BFD_RELOC_UNUSED)
1450     {
1451       if (address_expr->X_op == O_constant)
1452         {
1453           switch (reloc_type)
1454             {
1455             case BFD_RELOC_32:
1456               ip->insn_opcode |= address_expr->X_add_number;
1457               break;
1458
1459             case BFD_RELOC_LO16:
1460               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1461               break;
1462
1463             case BFD_RELOC_MIPS_JMP:
1464               if ((address_expr->X_add_number & 3) != 0)
1465                 as_bad ("jump to misaligned address (0x%lx)",
1466                         (unsigned long) address_expr->X_add_number);
1467               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1468               break;
1469
1470             case BFD_RELOC_MIPS16_JMP:
1471               if ((address_expr->X_add_number & 3) != 0)
1472                 as_bad ("jump to misaligned address (0x%lx)",
1473                         (unsigned long) address_expr->X_add_number);
1474               ip->insn_opcode |=
1475                 (((address_expr->X_add_number & 0x7c0000) << 3)
1476                  | ((address_expr->X_add_number & 0xf800000) >> 7)
1477                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
1478               break;
1479
1480             case BFD_RELOC_16_PCREL_S2:
1481               goto need_reloc;
1482
1483             default:
1484               internalError ();
1485             }
1486         }
1487       else
1488         {
1489         need_reloc:
1490           /* Don't generate a reloc if we are writing into a variant
1491              frag.  */
1492           if (place == NULL)
1493             {
1494               fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1495                                   address_expr,
1496                                   reloc_type == BFD_RELOC_16_PCREL_S2,
1497                                   reloc_type);
1498               if (unmatched_hi)
1499                 {
1500                   struct mips_hi_fixup *hi_fixup;
1501
1502                   assert (reloc_type == BFD_RELOC_HI16_S);
1503                   hi_fixup = ((struct mips_hi_fixup *)
1504                               xmalloc (sizeof (struct mips_hi_fixup)));
1505                   hi_fixup->fixp = fixp;
1506                   hi_fixup->seg = now_seg;
1507                   hi_fixup->next = mips_hi_fixup_list;
1508                   mips_hi_fixup_list = hi_fixup;
1509                 }
1510             }
1511         }
1512     }
1513
1514   if (! mips16 || reloc_type == BFD_RELOC_MIPS16_JMP)
1515     md_number_to_chars (f, ip->insn_opcode, 4);
1516   else
1517     {
1518       if (ip->use_extend)
1519         {
1520           md_number_to_chars (f, 0xf000 | ip->extend, 2);
1521           f += 2;
1522         }
1523       md_number_to_chars (f, ip->insn_opcode, 2);
1524     }
1525
1526   /* Update the register mask information.  */
1527   if (! mips16)
1528     {
1529       if (pinfo & INSN_WRITE_GPR_D)
1530         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
1531       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
1532         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
1533       if (pinfo & INSN_READ_GPR_S)
1534         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
1535       if (pinfo & INSN_WRITE_GPR_31)
1536         mips_gprmask |= 1 << 31;
1537       if (pinfo & INSN_WRITE_FPR_D)
1538         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
1539       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
1540         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
1541       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
1542         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
1543       if ((pinfo & INSN_READ_FPR_R) != 0)
1544         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
1545       if (pinfo & INSN_COP)
1546         {
1547           /* We don't keep enough information to sort these cases out.  */
1548         }
1549       /* Never set the bit for $0, which is always zero.  */
1550       mips_gprmask &=~ 1 << 0;
1551     }
1552   else
1553     {
1554       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
1555         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
1556                               & MIPS16OP_MASK_RX);
1557       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
1558         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
1559                               & MIPS16OP_MASK_RY);
1560       if (pinfo & MIPS16_INSN_WRITE_Z)
1561         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
1562                               & MIPS16OP_MASK_RZ);
1563       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
1564         mips_gprmask |= 1 << TREG;
1565       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
1566         mips_gprmask |= 1 << SP;
1567       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
1568         mips_gprmask |= 1 << RA;
1569       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
1570         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
1571       if (pinfo & MIPS16_INSN_READ_Z)
1572         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1573                               & MIPS16OP_MASK_MOVE32Z);
1574       if (pinfo & MIPS16_INSN_READ_GPR_X)
1575         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1576                               & MIPS16OP_MASK_REGR32);
1577     }
1578
1579   if (place == NULL && ! mips_noreorder)
1580     {
1581       /* Filling the branch delay slot is more complex.  We try to
1582          switch the branch with the previous instruction, which we can
1583          do if the previous instruction does not set up a condition
1584          that the branch tests and if the branch is not itself the
1585          target of any branch.  */
1586       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
1587           || (pinfo & INSN_COND_BRANCH_DELAY))
1588         {
1589           if (mips_optimize < 2
1590               /* If we have seen .set volatile or .set nomove, don't
1591                  optimize.  */
1592               || mips_nomove != 0
1593               /* If we had to emit any NOP instructions, then we
1594                  already know we can not swap.  */
1595               || nops != 0
1596               /* If we don't even know the previous insn, we can not
1597                  swap. */
1598               || ! prev_insn_valid
1599               /* If the previous insn is already in a branch delay
1600                  slot, then we can not swap.  */
1601               || prev_insn_is_delay_slot
1602               /* If the previous previous insn was in a .set
1603                  noreorder, we can't swap.  Actually, the MIPS
1604                  assembler will swap in this situation.  However, gcc
1605                  configured -with-gnu-as will generate code like
1606                    .set noreorder
1607                    lw   $4,XXX
1608                    .set reorder
1609                    INSN
1610                    bne  $4,$0,foo
1611                  in which we can not swap the bne and INSN.  If gcc is
1612                  not configured -with-gnu-as, it does not output the
1613                  .set pseudo-ops.  We don't have to check
1614                  prev_insn_unreordered, because prev_insn_valid will
1615                  be 0 in that case.  We don't want to use
1616                  prev_prev_insn_valid, because we do want to be able
1617                  to swap at the start of a function.  */
1618               || prev_prev_insn_unreordered
1619               /* If the branch is itself the target of a branch, we
1620                  can not swap.  We cheat on this; all we check for is
1621                  whether there is a label on this instruction.  If
1622                  there are any branches to anything other than a
1623                  label, users must use .set noreorder.  */
1624               || insn_labels != NULL
1625               /* If the previous instruction is in a variant frag, we
1626                  can not do the swap.  This does not apply to the
1627                  mips16, which uses variant frags for different
1628                  purposes.  */
1629               || (! mips16
1630                   && prev_insn_frag->fr_type == rs_machine_dependent)
1631               /* If the branch reads the condition codes, we don't
1632                  even try to swap, because in the sequence
1633                    ctc1 $X,$31
1634                    INSN
1635                    INSN
1636                    bc1t LABEL
1637                  we can not swap, and I don't feel like handling that
1638                  case.  */
1639               || (! mips16
1640                   && mips_isa < 4
1641                   && (pinfo & INSN_READ_COND_CODE))
1642               /* We can not swap with an instruction that requires a
1643                  delay slot, becase the target of the branch might
1644                  interfere with that instruction.  */
1645               || (! mips16
1646                   && mips_isa < 4
1647                   && (prev_pinfo
1648                       & (INSN_LOAD_COPROC_DELAY
1649                          | INSN_COPROC_MOVE_DELAY
1650                          | INSN_WRITE_COND_CODE)))
1651               || (! interlocks
1652                   && (prev_pinfo
1653                       & (INSN_READ_LO
1654                          | INSN_READ_HI)))
1655               || (! mips16
1656                   && mips_isa < 2
1657                   && (prev_pinfo
1658                       & (INSN_LOAD_MEMORY_DELAY
1659                          | INSN_COPROC_MEMORY_DELAY)))
1660               /* We can not swap with a branch instruction.  */
1661               || (prev_pinfo
1662                   & (INSN_UNCOND_BRANCH_DELAY
1663                      | INSN_COND_BRANCH_DELAY
1664                      | INSN_COND_BRANCH_LIKELY))
1665               /* We do not swap with a trap instruction, since it
1666                  complicates trap handlers to have the trap
1667                  instruction be in a delay slot.  */
1668               || (prev_pinfo & INSN_TRAP)
1669               /* If the branch reads a register that the previous
1670                  instruction sets, we can not swap.  */
1671               || (! mips16
1672                   && (prev_pinfo & INSN_WRITE_GPR_T)
1673                   && insn_uses_reg (ip,
1674                                     ((prev_insn.insn_opcode >> OP_SH_RT)
1675                                      & OP_MASK_RT),
1676                                     MIPS_GR_REG))
1677               || (! mips16
1678                   && (prev_pinfo & INSN_WRITE_GPR_D)
1679                   && insn_uses_reg (ip,
1680                                     ((prev_insn.insn_opcode >> OP_SH_RD)
1681                                      & OP_MASK_RD),
1682                                     MIPS_GR_REG))
1683               || (mips16
1684                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
1685                        && insn_uses_reg (ip,
1686                                          ((prev_insn.insn_opcode
1687                                            >> MIPS16OP_SH_RX)
1688                                           & MIPS16OP_MASK_RX),
1689                                          MIPS16_REG))
1690                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
1691                           && insn_uses_reg (ip,
1692                                             ((prev_insn.insn_opcode
1693                                               >> MIPS16OP_SH_RY)
1694                                              & MIPS16OP_MASK_RY),
1695                                             MIPS16_REG))
1696                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
1697                           && insn_uses_reg (ip,
1698                                             ((prev_insn.insn_opcode
1699                                               >> MIPS16OP_SH_RZ)
1700                                              & MIPS16OP_MASK_RZ),
1701                                             MIPS16_REG))
1702                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
1703                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
1704                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
1705                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
1706                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
1707                           && insn_uses_reg (ip,
1708                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
1709                                                                      insn_opcode),
1710                                             MIPS_GR_REG))))
1711               /* If the branch writes a register that the previous
1712                  instruction sets, we can not swap (we know that
1713                  branches write only to RD or to $31).  */
1714               || (! mips16
1715                   && (prev_pinfo & INSN_WRITE_GPR_T)
1716                   && (((pinfo & INSN_WRITE_GPR_D)
1717                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
1718                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
1719                       || ((pinfo & INSN_WRITE_GPR_31)
1720                           && (((prev_insn.insn_opcode >> OP_SH_RT)
1721                                & OP_MASK_RT)
1722                               == 31))))
1723               || (! mips16
1724                   && (prev_pinfo & INSN_WRITE_GPR_D)
1725                   && (((pinfo & INSN_WRITE_GPR_D)
1726                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
1727                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
1728                       || ((pinfo & INSN_WRITE_GPR_31)
1729                           && (((prev_insn.insn_opcode >> OP_SH_RD)
1730                                & OP_MASK_RD)
1731                               == 31))))
1732               || (mips16
1733                   && (pinfo & MIPS16_INSN_WRITE_31)
1734                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
1735                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
1736                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
1737                               == RA))))
1738               /* If the branch writes a register that the previous
1739                  instruction reads, we can not swap (we know that
1740                  branches only write to RD or to $31).  */
1741               || (! mips16
1742                   && (pinfo & INSN_WRITE_GPR_D)
1743                   && insn_uses_reg (&prev_insn,
1744                                     ((ip->insn_opcode >> OP_SH_RD)
1745                                      & OP_MASK_RD),
1746                                     MIPS_GR_REG))
1747               || (! mips16
1748                   && (pinfo & INSN_WRITE_GPR_31)
1749                   && insn_uses_reg (&prev_insn, 31, MIPS_GR_REG))
1750               || (mips16
1751                   && (pinfo & MIPS16_INSN_WRITE_31)
1752                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
1753               /* If we are generating embedded PIC code, the branch
1754                  might be expanded into a sequence which uses $at, so
1755                  we can't swap with an instruction which reads it.  */
1756               || (mips_pic == EMBEDDED_PIC
1757                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
1758               /* If the previous previous instruction has a load
1759                  delay, and sets a register that the branch reads, we
1760                  can not swap.  */
1761               || (! mips16
1762                   && mips_isa < 4
1763                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
1764                       || (mips_isa < 2
1765                           && (prev_prev_insn.insn_mo->pinfo
1766                               & INSN_LOAD_MEMORY_DELAY)))
1767                   && insn_uses_reg (ip,
1768                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
1769                                      & OP_MASK_RT),
1770                                     MIPS_GR_REG))
1771               /* If one instruction sets a condition code and the
1772                  other one uses a condition code, we can not swap.  */
1773               || ((pinfo & INSN_READ_COND_CODE)
1774                   && (prev_pinfo & INSN_WRITE_COND_CODE))
1775               || ((pinfo & INSN_WRITE_COND_CODE)
1776                   && (prev_pinfo & INSN_READ_COND_CODE))
1777               /* If the previous instruction uses the PC, we can not
1778                  swap.  */
1779               || (mips16
1780                   && (prev_pinfo & MIPS16_INSN_READ_PC))
1781               /* If the previous instruction was extended, we can not
1782                  swap.  */
1783               || (mips16 && prev_insn_extended)
1784               /* If the previous instruction had a fixup in mips16
1785                  mode, we can not swap.  This normally means that the
1786                  previous instruction was a 4 byte branch anyhow.  */
1787               || (mips16 && prev_insn_fixp))
1788             {
1789               /* We could do even better for unconditional branches to
1790                  portions of this object file; we could pick up the
1791                  instruction at the destination, put it in the delay
1792                  slot, and bump the destination address.  */
1793               emit_nop ();
1794               /* Update the previous insn information.  */
1795               prev_prev_insn = *ip;
1796               prev_insn.insn_mo = &dummy_opcode;
1797             }
1798           else
1799             {
1800               /* It looks like we can actually do the swap.  */
1801               if (! mips16)
1802                 {
1803                   char *prev_f;
1804                   char temp[4];
1805
1806                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
1807                   memcpy (temp, prev_f, 4);
1808                   memcpy (prev_f, f, 4);
1809                   memcpy (f, temp, 4);
1810                   if (prev_insn_fixp)
1811                     {
1812                       prev_insn_fixp->fx_frag = frag_now;
1813                       prev_insn_fixp->fx_where = f - frag_now->fr_literal;
1814                     }
1815                   if (fixp)
1816                     {
1817                       fixp->fx_frag = prev_insn_frag;
1818                       fixp->fx_where = prev_insn_where;
1819                     }
1820                 }
1821               else if (reloc_type > BFD_RELOC_UNUSED)
1822                 {
1823                   char *prev_f;
1824                   char temp[2];
1825
1826                   /* We are in mips16 mode, and we have just created a
1827                      variant frag.  We need to extract the old
1828                      instruction from the end of the previous frag,
1829                      and add it to a new frag.  */
1830                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
1831                   memcpy (temp, prev_f, 2);
1832                   prev_insn_frag->fr_fix -= 2;
1833                   if (prev_insn_frag->fr_type == rs_machine_dependent)
1834                     {
1835                       assert (prev_insn_where == prev_insn_frag->fr_fix);
1836                       memcpy (prev_f, prev_f + 2, 2);
1837                     }
1838                   memcpy (frag_more (2), temp, 2);
1839                 }
1840               else
1841                 {
1842                   char *prev_f;
1843                   char temp[2];
1844
1845                   assert (prev_insn_fixp == NULL);
1846                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
1847                   memcpy (temp, prev_f, 2);
1848                   memcpy (prev_f, f, 2);
1849                   if (reloc_type != BFD_RELOC_MIPS16_JMP)
1850                     memcpy (f, temp, 2);
1851                   else
1852                     {
1853                       memcpy (f, f + 2, 2);
1854                       memcpy (f + 2, temp, 2);
1855                     }
1856                   if (fixp)
1857                     {
1858                       fixp->fx_frag = prev_insn_frag;
1859                       fixp->fx_where = prev_insn_where;
1860                     }
1861                 }
1862
1863               /* Update the previous insn information; leave prev_insn
1864                  unchanged.  */
1865               prev_prev_insn = *ip;
1866             }
1867           prev_insn_is_delay_slot = 1;
1868
1869           /* If that was an unconditional branch, forget the previous
1870              insn information.  */
1871           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
1872             {
1873               prev_prev_insn.insn_mo = &dummy_opcode;
1874               prev_insn.insn_mo = &dummy_opcode;
1875             }
1876
1877           prev_insn_fixp = NULL;
1878           prev_insn_reloc_type = BFD_RELOC_UNUSED;
1879           prev_insn_extended = 0;
1880         }
1881       else if (pinfo & INSN_COND_BRANCH_LIKELY)
1882         {
1883           /* We don't yet optimize a branch likely.  What we should do
1884              is look at the target, copy the instruction found there
1885              into the delay slot, and increment the branch to jump to
1886              the next instruction.  */
1887           emit_nop ();
1888           /* Update the previous insn information.  */
1889           prev_prev_insn = *ip;
1890           prev_insn.insn_mo = &dummy_opcode;
1891           prev_insn_fixp = NULL;
1892           prev_insn_reloc_type = BFD_RELOC_UNUSED;
1893           prev_insn_extended = 0;
1894         }
1895       else
1896         {
1897           /* Update the previous insn information.  */
1898           if (nops > 0)
1899             prev_prev_insn.insn_mo = &dummy_opcode;
1900           else
1901             prev_prev_insn = prev_insn;
1902           prev_insn = *ip;
1903
1904           /* Any time we see a branch, we always fill the delay slot
1905              immediately; since this insn is not a branch, we know it
1906              is not in a delay slot.  */
1907           prev_insn_is_delay_slot = 0;
1908
1909           prev_insn_fixp = fixp;
1910           prev_insn_reloc_type = reloc_type;
1911           if (mips16)
1912             prev_insn_extended = (ip->use_extend
1913                                   || reloc_type > BFD_RELOC_UNUSED);
1914         }
1915
1916       prev_prev_insn_unreordered = prev_insn_unreordered;
1917       prev_insn_unreordered = 0;
1918       prev_insn_frag = frag_now;
1919       prev_insn_where = f - frag_now->fr_literal;
1920       prev_insn_valid = 1;
1921     }
1922   else if (place == NULL)
1923     {
1924       /* We need to record a bit of information even when we are not
1925          reordering, in order to determine the base address for mips16
1926          PC relative relocs.  */
1927       prev_insn = *ip;
1928       prev_insn_reloc_type = reloc_type;
1929     }
1930
1931   /* We just output an insn, so the next one doesn't have a label.  */
1932   mips_clear_insn_labels ();
1933 }
1934
1935 /* This function forgets that there was any previous instruction or
1936    label.  */
1937
1938 static void
1939 mips_no_prev_insn ()
1940 {
1941   prev_insn.insn_mo = &dummy_opcode;
1942   prev_prev_insn.insn_mo = &dummy_opcode;
1943   prev_insn_valid = 0;
1944   prev_insn_is_delay_slot = 0;
1945   prev_insn_unreordered = 0;
1946   prev_insn_extended = 0;
1947   prev_insn_reloc_type = BFD_RELOC_UNUSED;
1948   prev_prev_insn_unreordered = 0;
1949   mips_clear_insn_labels ();
1950 }
1951
1952 /* This function must be called whenever we turn on noreorder or emit
1953    something other than instructions.  It inserts any NOPS which might
1954    be needed by the previous instruction, and clears the information
1955    kept for the previous instructions.  The INSNS parameter is true if
1956    instructions are to follow. */
1957
1958 static void
1959 mips_emit_delays (insns)
1960      boolean insns;
1961 {
1962   if (! mips_noreorder)
1963     {
1964       int nop;
1965
1966       nop = 0;
1967       if ((! mips16
1968            && mips_isa < 4
1969            && (! cop_interlocks
1970                && (prev_insn.insn_mo->pinfo
1971                    & (INSN_LOAD_COPROC_DELAY
1972                       | INSN_COPROC_MOVE_DELAY
1973                       | INSN_WRITE_COND_CODE))))
1974           || (! interlocks
1975               && (prev_insn.insn_mo->pinfo
1976                   & (INSN_READ_LO
1977                      | INSN_READ_HI)))
1978           || (! mips16
1979               && mips_isa < 2
1980               && (prev_insn.insn_mo->pinfo
1981                   & (INSN_LOAD_MEMORY_DELAY
1982                      | INSN_COPROC_MEMORY_DELAY))))
1983         {
1984           nop = 1;
1985           if ((! mips16
1986                && mips_isa < 4
1987                && (! cop_interlocks
1988                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
1989               || (! interlocks
1990                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
1991                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
1992             emit_nop ();
1993         }
1994       else if ((! mips16
1995                 && mips_isa < 4
1996                 && (! cop_interlocks
1997                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
1998                || (! interlocks
1999                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2000                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2001         nop = 1;
2002       if (nop)
2003         {
2004           struct insn_label_list *l;
2005
2006           emit_nop ();
2007           for (l = insn_labels; l != NULL; l = l->next)
2008             {
2009               assert (S_GET_SEGMENT (l->label) == now_seg);
2010               l->label->sy_frag = frag_now;
2011               S_SET_VALUE (l->label, (valueT) frag_now_fix ());
2012               /* mips16 text labels are stored as odd.  */
2013               if (mips16)
2014                 ++l->label->sy_value.X_add_number;
2015             }
2016         }
2017     }
2018
2019   /* Mark instruction labels in mips16 mode.  This permits the linker
2020      to handle them specially, such as generating jalx instructions
2021      when needed.  We also make them odd for the duration of the
2022      assembly, in order to generate the right sort of code.  We will
2023      make them even in the adjust_symtab routine, while leaving them
2024      marked.  This is convenient for the debugger and the
2025      disassembler.  The linker knows to make them odd again.  */
2026   if (mips16 && insns)
2027     {
2028       struct insn_label_list *l;
2029
2030       for (l = insn_labels; l != NULL; l = l->next)
2031         {
2032 #ifdef S_SET_OTHER
2033           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
2034             S_SET_OTHER (l->label, STO_MIPS16);
2035 #endif
2036           if ((l->label->sy_value.X_add_number & 1) == 0)
2037             ++l->label->sy_value.X_add_number;
2038         }
2039     }
2040
2041   mips_no_prev_insn ();
2042 }
2043
2044 /* Build an instruction created by a macro expansion.  This is passed
2045    a pointer to the count of instructions created so far, an
2046    expression, the name of the instruction to build, an operand format
2047    string, and corresponding arguments.  */
2048
2049 #ifdef USE_STDARG
2050 static void
2051 macro_build (char *place,
2052              int *counter,
2053              expressionS * ep,
2054              const char *name,
2055              const char *fmt,
2056              ...)
2057 #else
2058 static void
2059 macro_build (place, counter, ep, name, fmt, va_alist)
2060      char *place;
2061      int *counter;
2062      expressionS *ep;
2063      const char *name;
2064      const char *fmt;
2065      va_dcl
2066 #endif
2067 {
2068   struct mips_cl_insn insn;
2069   bfd_reloc_code_real_type r;
2070   va_list args;
2071
2072 #ifdef USE_STDARG
2073   va_start (args, fmt);
2074 #else
2075   va_start (args);
2076 #endif
2077
2078   /*
2079    * If the macro is about to expand into a second instruction,
2080    * print a warning if needed. We need to pass ip as a parameter
2081    * to generate a better warning message here...
2082    */
2083   if (mips_warn_about_macros && place == NULL && *counter == 1)
2084     as_warn ("Macro instruction expanded into multiple instructions");
2085
2086   if (place == NULL)
2087     *counter += 1;              /* bump instruction counter */
2088
2089   if (mips16)
2090     {
2091       mips16_macro_build (place, counter, ep, name, fmt, args);
2092       va_end (args);
2093       return;
2094     }
2095
2096   r = BFD_RELOC_UNUSED;
2097   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2098   assert (insn.insn_mo);
2099   assert (strcmp (name, insn.insn_mo->name) == 0);
2100
2101   while (strcmp (fmt, insn.insn_mo->args) != 0
2102          || insn.insn_mo->pinfo == INSN_MACRO
2103          || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_ISA2
2104              && mips_isa < 2)
2105          || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_ISA3
2106              && mips_isa < 3)
2107          || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_ISA4
2108              && mips_isa < 4)
2109          || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_4650
2110              && ! mips_4650)
2111          || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_4010
2112              && ! mips_4010)
2113          || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_4100
2114              && ! mips_4100))
2115     {
2116       ++insn.insn_mo;
2117       assert (insn.insn_mo->name);
2118       assert (strcmp (name, insn.insn_mo->name) == 0);
2119     }
2120   insn.insn_opcode = insn.insn_mo->match;
2121   for (;;)
2122     {
2123       switch (*fmt++)
2124         {
2125         case '\0':
2126           break;
2127
2128         case ',':
2129         case '(':
2130         case ')':
2131           continue;
2132
2133         case 't':
2134         case 'w':
2135         case 'E':
2136           insn.insn_opcode |= va_arg (args, int) << 16;
2137           continue;
2138
2139         case 'c':
2140         case 'T':
2141         case 'W':
2142           insn.insn_opcode |= va_arg (args, int) << 16;
2143           continue;
2144
2145         case 'd':
2146         case 'G':
2147           insn.insn_opcode |= va_arg (args, int) << 11;
2148           continue;
2149
2150         case 'V':
2151         case 'S':
2152           insn.insn_opcode |= va_arg (args, int) << 11;
2153           continue;
2154
2155         case 'z':
2156           continue;
2157
2158         case '<':
2159           insn.insn_opcode |= va_arg (args, int) << 6;
2160           continue;
2161
2162         case 'D':
2163           insn.insn_opcode |= va_arg (args, int) << 6;
2164           continue;
2165
2166         case 'B':
2167           insn.insn_opcode |= va_arg (args, int) << 6;
2168           continue;
2169
2170         case 'b':
2171         case 's':
2172         case 'r':
2173         case 'v':
2174           insn.insn_opcode |= va_arg (args, int) << 21;
2175           continue;
2176
2177         case 'i':
2178         case 'j':
2179         case 'o':
2180           r = (bfd_reloc_code_real_type) va_arg (args, int);
2181           assert (r == BFD_RELOC_MIPS_GPREL
2182                   || r == BFD_RELOC_MIPS_LITERAL
2183                   || r == BFD_RELOC_LO16
2184                   || r == BFD_RELOC_MIPS_GOT16
2185                   || r == BFD_RELOC_MIPS_CALL16
2186                   || r == BFD_RELOC_MIPS_GOT_LO16
2187                   || r == BFD_RELOC_MIPS_CALL_LO16
2188                   || (ep->X_op == O_subtract
2189                       && now_seg == text_section
2190                       && r == BFD_RELOC_PCREL_LO16));
2191           continue;
2192
2193         case 'u':
2194           r = (bfd_reloc_code_real_type) va_arg (args, int);
2195           assert (ep != NULL
2196                   && (ep->X_op == O_constant
2197                       || (ep->X_op == O_symbol
2198                           && (r == BFD_RELOC_HI16_S
2199                               || r == BFD_RELOC_HI16
2200                               || r == BFD_RELOC_MIPS_GOT_HI16
2201                               || r == BFD_RELOC_MIPS_CALL_HI16))
2202                       || (ep->X_op == O_subtract
2203                           && now_seg == text_section
2204                           && r == BFD_RELOC_PCREL_HI16_S)));
2205           if (ep->X_op == O_constant)
2206             {
2207               insn.insn_opcode |= (ep->X_add_number >> 16) & 0xffff;
2208               ep = NULL;
2209               r = BFD_RELOC_UNUSED;
2210             }
2211           continue;
2212
2213         case 'p':
2214           assert (ep != NULL);
2215           /*
2216            * This allows macro() to pass an immediate expression for
2217            * creating short branches without creating a symbol.
2218            * Note that the expression still might come from the assembly
2219            * input, in which case the value is not checked for range nor
2220            * is a relocation entry generated (yuck).
2221            */
2222           if (ep->X_op == O_constant)
2223             {
2224               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2225               ep = NULL;
2226             }
2227           else
2228             r = BFD_RELOC_16_PCREL_S2;
2229           continue;
2230
2231         case 'a':
2232           assert (ep != NULL);
2233           r = BFD_RELOC_MIPS_JMP;
2234           continue;
2235
2236         default:
2237           internalError ();
2238         }
2239       break;
2240     }
2241   va_end (args);
2242   assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2243
2244   append_insn (place, &insn, ep, r, false);
2245 }
2246
2247 static void
2248 mips16_macro_build (place, counter, ep, name, fmt, args)
2249      char *place;
2250      int *counter;
2251      expressionS *ep;
2252      const char *name;
2253      const char *fmt;
2254      va_list args;
2255 {
2256   struct mips_cl_insn insn;
2257   bfd_reloc_code_real_type r;
2258
2259   r = BFD_RELOC_UNUSED;
2260   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2261   assert (insn.insn_mo);
2262   assert (strcmp (name, insn.insn_mo->name) == 0);
2263
2264   while (strcmp (fmt, insn.insn_mo->args) != 0
2265          || insn.insn_mo->pinfo == INSN_MACRO)
2266     {
2267       ++insn.insn_mo;
2268       assert (insn.insn_mo->name);
2269       assert (strcmp (name, insn.insn_mo->name) == 0);
2270     }
2271
2272   insn.insn_opcode = insn.insn_mo->match;
2273   insn.use_extend = false;
2274
2275   for (;;)
2276     {
2277       int c;
2278
2279       c = *fmt++;
2280       switch (c)
2281         {
2282         case '\0':
2283           break;
2284
2285         case ',':
2286         case '(':
2287         case ')':
2288           continue;
2289
2290         case 'y':
2291         case 'w':
2292           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2293           continue;
2294
2295         case 'x':
2296         case 'v':
2297           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2298           continue;
2299
2300         case 'z':
2301           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2302           continue;
2303
2304         case 'Z':
2305           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2306           continue;
2307
2308         case '0':
2309         case 'S':
2310         case 'P':
2311         case 'R':
2312           continue;
2313
2314         case 'X':
2315           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2316           continue;
2317
2318         case 'Y':
2319           {
2320             int regno;
2321
2322             regno = va_arg (args, int);
2323             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2324             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2325           }
2326           continue;
2327
2328         case '<':
2329         case '>':
2330         case '4':
2331         case '5':
2332         case 'H':
2333         case 'W':
2334         case 'D':
2335         case 'j':
2336         case '8':
2337         case 'V':
2338         case 'C':
2339         case 'U':
2340         case 'k':
2341         case 'K':
2342         case 'p':
2343         case 'q':
2344           {
2345             assert (ep != NULL);
2346
2347             if (ep->X_op != O_constant)
2348               r = BFD_RELOC_UNUSED + c;
2349             else
2350               {
2351                 mips16_immed ((char *) NULL, 0, c, ep->X_add_number, false,
2352                               false, false, &insn.insn_opcode,
2353                               &insn.use_extend, &insn.extend);
2354                 ep = NULL;
2355                 r = BFD_RELOC_UNUSED;
2356               }
2357           }
2358           continue;
2359
2360         case '6':
2361           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2362           continue;
2363         }
2364
2365       break;
2366     }
2367
2368   assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2369
2370   append_insn (place, &insn, ep, r, false);
2371 }
2372
2373 /*
2374  * Generate a "lui" instruction.
2375  */
2376 static void
2377 macro_build_lui (place, counter, ep, regnum)
2378      char *place;
2379      int *counter;
2380      expressionS *ep;
2381      int regnum;
2382 {
2383   expressionS high_expr;
2384   struct mips_cl_insn insn;
2385   bfd_reloc_code_real_type r;
2386   CONST char *name = "lui";
2387   CONST char *fmt = "t,u";
2388
2389   assert (! mips16);
2390
2391   if (place == NULL)
2392     high_expr = *ep;
2393   else
2394     {
2395       high_expr.X_op = O_constant;
2396       high_expr.X_add_number = ep->X_add_number;
2397     }
2398
2399   if (high_expr.X_op == O_constant)
2400     {
2401       /* we can compute the instruction now without a relocation entry */
2402       if (high_expr.X_add_number & 0x8000)
2403         high_expr.X_add_number += 0x10000;
2404       high_expr.X_add_number =
2405         ((unsigned long) high_expr.X_add_number >> 16) & 0xffff;
2406       r = BFD_RELOC_UNUSED;
2407     }
2408   else
2409     {
2410       assert (ep->X_op == O_symbol);
2411       /* _gp_disp is a special case, used from s_cpload.  */
2412       assert (mips_pic == NO_PIC
2413               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
2414       r = BFD_RELOC_HI16_S;
2415     }
2416
2417   /*
2418    * If the macro is about to expand into a second instruction,
2419    * print a warning if needed. We need to pass ip as a parameter
2420    * to generate a better warning message here...
2421    */
2422   if (mips_warn_about_macros && place == NULL && *counter == 1)
2423     as_warn ("Macro instruction expanded into multiple instructions");
2424
2425   if (place == NULL)
2426     *counter += 1;              /* bump instruction counter */
2427
2428   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2429   assert (insn.insn_mo);
2430   assert (strcmp (name, insn.insn_mo->name) == 0);
2431   assert (strcmp (fmt, insn.insn_mo->args) == 0);
2432
2433   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
2434   if (r == BFD_RELOC_UNUSED)
2435     {
2436       insn.insn_opcode |= high_expr.X_add_number;
2437       append_insn (place, &insn, NULL, r, false);
2438     }
2439   else
2440     append_insn (place, &insn, &high_expr, r, false);
2441 }
2442
2443 /*                      set_at()
2444  * Generates code to set the $at register to true (one)
2445  * if reg is less than the immediate expression.
2446  */
2447 static void
2448 set_at (counter, reg, unsignedp)
2449      int *counter;
2450      int reg;
2451      int unsignedp;
2452 {
2453   if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
2454     macro_build ((char *) NULL, counter, &imm_expr,
2455                  unsignedp ? "sltiu" : "slti",
2456                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
2457   else
2458     {
2459       load_register (counter, AT, &imm_expr, 0);
2460       macro_build ((char *) NULL, counter, NULL,
2461                    unsignedp ? "sltu" : "slt",
2462                    "d,v,t", AT, reg, AT);
2463     }
2464 }
2465
2466 /* Warn if an expression is not a constant.  */
2467
2468 static void
2469 check_absolute_expr (ip, ex)
2470      struct mips_cl_insn *ip;
2471      expressionS *ex;
2472 {
2473   if (ex->X_op != O_constant)
2474     as_warn ("Instruction %s requires absolute expression", ip->insn_mo->name);
2475 }
2476
2477 /* Count the leading zeroes by performing a binary chop. This is a
2478    bulky bit of source, but performance is a LOT better for the
2479    majority of values than a simple loop to count the bits:
2480        for (lcnt = 0; (lcnt < 32); lcnt++)
2481          if ((v) & (1 << (31 - lcnt)))
2482            break;
2483   However it is not code size friendly, and the gain will drop a bit
2484   on certain cached systems.
2485 */
2486 #define COUNT_TOP_ZEROES(v)             \
2487   (((v) & ~0xffff) == 0                 \
2488    ? ((v) & ~0xff) == 0                 \
2489      ? ((v) & ~0xf) == 0                \
2490        ? ((v) & ~0x3) == 0              \
2491          ? ((v) & ~0x1) == 0            \
2492            ? !(v)                       \
2493              ? 32                       \
2494              : 31                       \
2495            : 30                         \
2496          : ((v) & ~0x7) == 0            \
2497            ? 29                         \
2498            : 28                         \
2499        : ((v) & ~0x3f) == 0             \
2500          ? ((v) & ~0x1f) == 0           \
2501            ? 27                         \
2502            : 26                         \
2503          : ((v) & ~0x7f) == 0           \
2504            ? 25                         \
2505            : 24                         \
2506      : ((v) & ~0xfff) == 0              \
2507        ? ((v) & ~0x3ff) == 0            \
2508          ? ((v) & ~0x1ff) == 0          \
2509            ? 23                         \
2510            : 22                         \
2511          : ((v) & ~0x7ff) == 0          \
2512            ? 21                         \
2513            : 20                         \
2514        : ((v) & ~0x3fff) == 0           \
2515          ? ((v) & ~0x1fff) == 0         \
2516            ? 19                         \
2517            : 18                         \
2518          : ((v) & ~0x7fff) == 0         \
2519            ? 17                         \
2520            : 16                         \
2521    : ((v) & ~0xffffff) == 0             \
2522      ? ((v) & ~0xfffff) == 0            \
2523        ? ((v) & ~0x3ffff) == 0          \
2524          ? ((v) & ~0x1ffff) == 0        \
2525            ? 15                         \
2526            : 14                         \
2527          : ((v) & ~0x7ffff) == 0        \
2528            ? 13                         \
2529            : 12                         \
2530        : ((v) & ~0x3fffff) == 0         \
2531          ? ((v) & ~0x1fffff) == 0       \
2532            ? 11                         \
2533            : 10                         \
2534          : ((v) & ~0x7fffff) == 0       \
2535            ? 9                          \
2536            : 8                          \
2537      : ((v) & ~0xfffffff) == 0          \
2538        ? ((v) & ~0x3ffffff) == 0        \
2539          ? ((v) & ~0x1ffffff) == 0      \
2540            ? 7                          \
2541            : 6                          \
2542          : ((v) & ~0x7ffffff) == 0      \
2543            ? 5                          \
2544            : 4                          \
2545        : ((v) & ~0x3fffffff) == 0       \
2546          ? ((v) & ~0x1fffffff) == 0     \
2547            ? 3                          \
2548            : 2                          \
2549          : ((v) & ~0x7fffffff) == 0     \
2550            ? 1                          \
2551            : 0)
2552
2553 /*                      load_register()
2554  *  This routine generates the least number of instructions neccessary to load
2555  *  an absolute expression value into a register.
2556  */
2557 static void
2558 load_register (counter, reg, ep, dbl)
2559      int *counter;
2560      int reg;
2561      expressionS *ep;
2562      int dbl;
2563 {
2564   int freg;
2565   expressionS hi32, lo32;
2566
2567   if (ep->X_op != O_big)
2568     {
2569       assert (ep->X_op == O_constant);
2570       if (ep->X_add_number < 0x8000
2571           && (ep->X_add_number >= 0
2572               || (ep->X_add_number >= -0x8000
2573                   && (! dbl
2574                       || ! ep->X_unsigned
2575                       || sizeof (ep->X_add_number) > 4))))
2576         {
2577           /* We can handle 16 bit signed values with an addiu to
2578              $zero.  No need to ever use daddiu here, since $zero and
2579              the result are always correct in 32 bit mode.  */
2580           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
2581                        (int) BFD_RELOC_LO16);
2582           return;
2583         }
2584       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
2585         {
2586           /* We can handle 16 bit unsigned values with an ori to
2587              $zero.  */
2588           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
2589                        (int) BFD_RELOC_LO16);
2590           return;
2591         }
2592       else if ((((ep->X_add_number &~ (offsetT) 0x7fffffff) == 0
2593                  || ((ep->X_add_number &~ (offsetT) 0x7fffffff)
2594                      == ~ (offsetT) 0x7fffffff))
2595                 && (! dbl
2596                     || ! ep->X_unsigned
2597                     || sizeof (ep->X_add_number) > 4
2598                     || (ep->X_add_number & 0x80000000) == 0))
2599                || ((mips_isa < 3 || !dbl)
2600                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0))
2601         {
2602           /* 32 bit values require an lui.  */
2603           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
2604                        (int) BFD_RELOC_HI16);
2605           if ((ep->X_add_number & 0xffff) != 0)
2606             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
2607                          (int) BFD_RELOC_LO16);
2608           return;
2609         }
2610     }
2611
2612   /* The value is larger than 32 bits.  */
2613
2614   if (mips_isa < 3)
2615     {
2616       as_bad ("Number larger than 32 bits");
2617       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
2618                    (int) BFD_RELOC_LO16);
2619       return;
2620     }
2621
2622   if (ep->X_op != O_big)
2623     {
2624       hi32 = *ep;
2625       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
2626       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
2627       hi32.X_add_number &= 0xffffffff;
2628       lo32 = *ep;
2629       lo32.X_add_number &= 0xffffffff;
2630     }
2631   else
2632     {
2633       assert (ep->X_add_number > 2);
2634       if (ep->X_add_number == 3)
2635         generic_bignum[3] = 0;
2636       else if (ep->X_add_number > 4)
2637         as_bad ("Number larger than 64 bits");
2638       lo32.X_op = O_constant;
2639       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
2640       hi32.X_op = O_constant;
2641       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
2642     }
2643
2644   if (hi32.X_add_number == 0)
2645     freg = 0;
2646   else
2647     {
2648       int shift, bit;
2649       unsigned long hi, lo;
2650
2651       if (hi32.X_add_number == 0xffffffff)
2652         {
2653           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
2654             {
2655               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
2656                            reg, 0, (int) BFD_RELOC_LO16);
2657               return;
2658             }
2659           if (lo32.X_add_number & 0x80000000)
2660             {
2661               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
2662                            (int) BFD_RELOC_HI16);
2663               if (lo32.X_add_number & 0xffff)
2664                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
2665                              reg, reg, (int) BFD_RELOC_LO16);
2666               return;
2667             }
2668         }
2669
2670       /* Check for 16bit shifted constant.  We know that hi32 is
2671          non-zero, so start the mask on the first bit of the hi32
2672          value.  */
2673       shift = 17;
2674       do
2675        {
2676          unsigned long himask, lomask;
2677
2678          if (shift < 32)
2679            {
2680              himask = 0xffff >> (32 - shift);
2681              lomask = (0xffff << shift) & 0xffffffff;
2682            }
2683          else
2684            {
2685              himask = 0xffff << (shift - 32);
2686              lomask = 0;
2687            }
2688          if ((hi32.X_add_number & ~ (offsetT) himask) == 0
2689              && (lo32.X_add_number & ~ (offsetT) lomask) == 0)
2690            {
2691              expressionS tmp;
2692
2693              tmp.X_op = O_constant;
2694              if (shift < 32)
2695                tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
2696                                    | (lo32.X_add_number >> shift));
2697              else
2698                tmp.X_add_number = hi32.X_add_number >> (shift - 32);
2699              macro_build ((char *) NULL, counter, &tmp, "ori", "t,r,i", reg, 0,
2700                           (int) BFD_RELOC_LO16);
2701              macro_build ((char *) NULL, counter, NULL,
2702                           (shift >= 32) ? "dsll32" : "dsll",
2703                           "d,w,<", reg, reg,
2704                           (shift >= 32) ? shift - 32 : shift);
2705              return;
2706            }
2707          shift++;
2708        } while (shift <= (64 - 16));
2709
2710       /* Find the bit number of the lowest one bit, and store the
2711          shifted value in hi/lo.  */
2712       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
2713       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
2714       if (lo != 0)
2715         {
2716           bit = 0;
2717           while ((lo & 1) == 0)
2718             {
2719               lo >>= 1;
2720               ++bit;
2721             }
2722           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
2723           hi >>= bit;
2724         }
2725       else
2726         {
2727           bit = 32;
2728           while ((hi & 1) == 0)
2729             {
2730               hi >>= 1;
2731               ++bit;
2732             }
2733           lo = hi;
2734           hi = 0;
2735         }
2736
2737       /* Optimize if the shifted value is a (power of 2) - 1.  */
2738       if ((hi == 0 && ((lo + 1) & lo) == 0)
2739           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
2740         {
2741           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
2742           if (shift != 0)
2743             {
2744               expressionS tmp;
2745
2746               /* This instruction will set the register to be all
2747                  ones.  */
2748               tmp.X_op = O_constant;
2749               tmp.X_add_number = (offsetT) -1;
2750               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
2751                            reg, 0, (int) BFD_RELOC_LO16);
2752               if (bit != 0)
2753                 {
2754                   bit += shift;
2755                   macro_build ((char *) NULL, counter, NULL,
2756                                (bit >= 32) ? "dsll32" : "dsll",
2757                                "d,w,<", reg, reg,
2758                                (bit >= 32) ? bit - 32 : bit);
2759                 }
2760               macro_build ((char *) NULL, counter, NULL,
2761                            (shift >= 32) ? "dsrl32" : "dsrl",
2762                            "d,w,<", reg, reg,
2763                            (shift >= 32) ? shift - 32 : shift);
2764               return;
2765             }
2766         }
2767
2768       /* Sign extend hi32 before calling load_register, because we can
2769          generally get better code when we load a sign extended value.  */
2770       if ((hi32.X_add_number & 0x80000000) != 0)
2771         hi32.X_add_number |= ~ (offsetT) 0xffffffff;
2772       load_register (counter, reg, &hi32, 0);
2773       freg = reg;
2774     }
2775   if ((lo32.X_add_number & 0xffff0000) == 0)
2776     {
2777       if (freg != 0)
2778         {
2779           macro_build ((char *) NULL, counter, NULL, "dsll32", "d,w,<", reg,
2780                        freg, 0);
2781           freg = reg;
2782         }
2783     }
2784   else
2785     {
2786       expressionS mid16;
2787
2788       if ((freg == 0) && (lo32.X_add_number == 0xffffffff))
2789         {
2790           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
2791                        (int) BFD_RELOC_HI16);
2792           macro_build ((char *) NULL, counter, NULL, "dsrl32", "d,w,<", reg,
2793                        reg, 0);
2794           return;
2795         }
2796
2797       if (freg != 0)
2798         {
2799           macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
2800                        freg, 16);
2801           freg = reg;
2802         }
2803       mid16 = lo32;
2804       mid16.X_add_number >>= 16;
2805       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
2806                    freg, (int) BFD_RELOC_LO16);
2807       macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
2808                    reg, 16);
2809       freg = reg;
2810     }
2811   if ((lo32.X_add_number & 0xffff) != 0)
2812     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
2813                  (int) BFD_RELOC_LO16);
2814 }
2815
2816 /* Load an address into a register.  */
2817
2818 static void
2819 load_address (counter, reg, ep)
2820      int *counter;
2821      int reg;
2822      expressionS *ep;
2823 {
2824   char *p;
2825
2826   if (ep->X_op != O_constant
2827       && ep->X_op != O_symbol)
2828     {
2829       as_bad ("expression too complex");
2830       ep->X_op = O_constant;
2831     }
2832
2833   if (ep->X_op == O_constant)
2834     {
2835       load_register (counter, reg, ep, 0);
2836       return;
2837     }
2838
2839   if (mips_pic == NO_PIC)
2840     {
2841       /* If this is a reference to a GP relative symbol, we want
2842            addiu        $reg,$gp,<sym>          (BFD_RELOC_MIPS_GPREL)
2843          Otherwise we want
2844            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
2845            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
2846          If we have an addend, we always use the latter form.  */
2847       if ((valueT) ep->X_add_number >= MAX_GPREL_OFFSET
2848           || nopic_need_relax (ep->X_add_symbol))
2849         p = NULL;
2850       else
2851         {
2852           frag_grow (20);
2853           macro_build ((char *) NULL, counter, ep,
2854                        mips_isa < 3 ? "addiu" : "daddiu",
2855                        "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
2856           p = frag_var (rs_machine_dependent, 8, 0,
2857                         RELAX_ENCODE (4, 8, 0, 4, 0, mips_warn_about_macros),
2858                         ep->X_add_symbol, (long) 0, (char *) NULL);
2859         }
2860       macro_build_lui (p, counter, ep, reg);
2861       if (p != NULL)
2862         p += 4;
2863       macro_build (p, counter, ep,
2864                    mips_isa < 3 ? "addiu" : "daddiu",
2865                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
2866     }
2867   else if (mips_pic == SVR4_PIC && ! mips_big_got)
2868     {
2869       expressionS ex;
2870
2871       /* If this is a reference to an external symbol, we want
2872            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
2873          Otherwise we want
2874            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
2875            nop
2876            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
2877          If there is a constant, it must be added in after.  */
2878       ex.X_add_number = ep->X_add_number;
2879       ep->X_add_number = 0;
2880       frag_grow (20);
2881       macro_build ((char *) NULL, counter, ep,
2882                    mips_isa < 3 ? "lw" : "ld",
2883                    "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
2884       macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
2885       p = frag_var (rs_machine_dependent, 4, 0,
2886                     RELAX_ENCODE (0, 4, -8, 0, 0, mips_warn_about_macros),
2887                     ep->X_add_symbol, (long) 0, (char *) NULL);
2888       macro_build (p, counter, ep,
2889                    mips_isa < 3 ? "addiu" : "daddiu",
2890                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
2891       if (ex.X_add_number != 0)
2892         {
2893           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
2894             as_bad ("PIC code offset overflow (max 16 signed bits)");
2895           ex.X_op = O_constant;
2896           macro_build ((char *) NULL, counter, &ex,
2897                        mips_isa < 3 ? "addiu" : "daddiu",
2898                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
2899         }
2900     }
2901   else if (mips_pic == SVR4_PIC)
2902     {
2903       expressionS ex;
2904       int off;
2905
2906       /* This is the large GOT case.  If this is a reference to an
2907          external symbol, we want
2908            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
2909            addu         $reg,$reg,$gp
2910            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
2911          Otherwise, for a reference to a local symbol, we want
2912            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
2913            nop
2914            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
2915          If there is a constant, it must be added in after.  */
2916       ex.X_add_number = ep->X_add_number;
2917       ep->X_add_number = 0;
2918       if (reg_needs_delay (GP))
2919         off = 4;
2920       else
2921         off = 0;
2922       frag_grow (32);
2923       macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
2924                    (int) BFD_RELOC_MIPS_GOT_HI16);
2925       macro_build ((char *) NULL, counter, (expressionS *) NULL,
2926                    mips_isa < 3 ? "addu" : "daddu",
2927                    "d,v,t", reg, reg, GP);
2928       macro_build ((char *) NULL, counter, ep,
2929                    mips_isa < 3 ? "lw" : "ld",
2930                    "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
2931       p = frag_var (rs_machine_dependent, 12 + off, 0,
2932                     RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
2933                                   mips_warn_about_macros),
2934                     ep->X_add_symbol, (long) 0, (char *) NULL);
2935       if (off > 0)
2936         {
2937           /* We need a nop before loading from $gp.  This special
2938              check is required because the lui which starts the main
2939              instruction stream does not refer to $gp, and so will not
2940              insert the nop which may be required.  */
2941           macro_build (p, counter, (expressionS *) NULL, "nop", "");
2942           p += 4;
2943         }
2944       macro_build (p, counter, ep,
2945                    mips_isa < 3 ? "lw" : "ld",
2946                    "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
2947       p += 4;
2948       macro_build (p, counter, (expressionS *) NULL, "nop", "");
2949       p += 4;
2950       macro_build (p, counter, ep,
2951                    mips_isa < 3 ? "addiu" : "daddiu",
2952                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
2953       if (ex.X_add_number != 0)
2954         {
2955           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
2956             as_bad ("PIC code offset overflow (max 16 signed bits)");
2957           ex.X_op = O_constant;
2958           macro_build ((char *) NULL, counter, &ex,
2959                        mips_isa < 3 ? "addiu" : "daddiu",
2960                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
2961         }
2962     }
2963   else if (mips_pic == EMBEDDED_PIC)
2964     {
2965       /* We always do
2966            addiu        $reg,$gp,<sym>          (BFD_RELOC_MIPS_GPREL)
2967          */
2968       macro_build ((char *) NULL, counter, ep,
2969                    mips_isa < 3 ? "addiu" : "daddiu",
2970                    "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
2971     }
2972   else
2973     abort ();
2974 }
2975
2976 /*
2977  *                      Build macros
2978  *   This routine implements the seemingly endless macro or synthesized
2979  * instructions and addressing modes in the mips assembly language. Many
2980  * of these macros are simple and are similar to each other. These could
2981  * probably be handled by some kind of table or grammer aproach instead of
2982  * this verbose method. Others are not simple macros but are more like
2983  * optimizing code generation.
2984  *   One interesting optimization is when several store macros appear
2985  * consecutivly that would load AT with the upper half of the same address.
2986  * The ensuing load upper instructions are ommited. This implies some kind
2987  * of global optimization. We currently only optimize within a single macro.
2988  *   For many of the load and store macros if the address is specified as a
2989  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
2990  * first load register 'at' with zero and use it as the base register. The
2991  * mips assembler simply uses register $zero. Just one tiny optimization
2992  * we're missing.
2993  */
2994 static void
2995 macro (ip)
2996      struct mips_cl_insn *ip;
2997 {
2998   register int treg, sreg, dreg, breg;
2999   int tempreg;
3000   int mask;
3001   int icnt = 0;
3002   int used_at;
3003   expressionS expr1;
3004   const char *s;
3005   const char *s2;
3006   const char *fmt;
3007   int likely = 0;
3008   int dbl = 0;
3009   int coproc = 0;
3010   int lr = 0;
3011   offsetT maxnum;
3012   int off;
3013   bfd_reloc_code_real_type r;
3014   char *p;
3015   int hold_mips_optimize;
3016
3017   assert (! mips16);
3018
3019   treg = (ip->insn_opcode >> 16) & 0x1f;
3020   dreg = (ip->insn_opcode >> 11) & 0x1f;
3021   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3022   mask = ip->insn_mo->mask;
3023
3024   expr1.X_op = O_constant;
3025   expr1.X_op_symbol = NULL;
3026   expr1.X_add_symbol = NULL;
3027   expr1.X_add_number = 1;
3028
3029   switch (mask)
3030     {
3031     case M_DABS:
3032       dbl = 1;
3033     case M_ABS:
3034       /* bgez $a0,.+12
3035          move v0,$a0
3036          sub v0,$zero,$a0
3037          */
3038
3039       mips_emit_delays (true);
3040       ++mips_noreorder;
3041       mips_any_noreorder = 1;
3042
3043       expr1.X_add_number = 8;
3044       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3045       if (dreg == sreg)
3046         macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3047       else
3048         macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, sreg, 0);
3049       macro_build ((char *) NULL, &icnt, NULL,
3050                    dbl ? "dsub" : "sub",
3051                    "d,v,t", dreg, 0, sreg);
3052
3053       --mips_noreorder;
3054       return;
3055
3056     case M_ADD_I:
3057       s = "addi";
3058       s2 = "add";
3059       goto do_addi;
3060     case M_ADDU_I:
3061       s = "addiu";
3062       s2 = "addu";
3063       goto do_addi;
3064     case M_DADD_I:
3065       dbl = 1;
3066       s = "daddi";
3067       s2 = "dadd";
3068       goto do_addi;
3069     case M_DADDU_I:
3070       dbl = 1;
3071       s = "daddiu";
3072       s2 = "daddu";
3073     do_addi:
3074       if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
3075         {
3076           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3077                        (int) BFD_RELOC_LO16);
3078           return;
3079         }
3080       load_register (&icnt, AT, &imm_expr, dbl);
3081       macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
3082       break;
3083
3084     case M_AND_I:
3085       s = "andi";
3086       s2 = "and";
3087       goto do_bit;
3088     case M_OR_I:
3089       s = "ori";
3090       s2 = "or";
3091       goto do_bit;
3092     case M_NOR_I:
3093       s = "";
3094       s2 = "nor";
3095       goto do_bit;
3096     case M_XOR_I:
3097       s = "xori";
3098       s2 = "xor";
3099     do_bit:
3100       if (imm_expr.X_add_number >= 0 && imm_expr.X_add_number < 0x10000)
3101         {
3102           if (mask != M_NOR_I)
3103             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3104                          sreg, (int) BFD_RELOC_LO16);
3105           else
3106             {
3107               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3108                            treg, sreg, (int) BFD_RELOC_LO16);
3109               macro_build ((char *) NULL, &icnt, NULL, "nor", "d,v,t",
3110                            treg, treg, 0);
3111             }
3112           return;
3113         }
3114
3115       load_register (&icnt, AT, &imm_expr, 0);
3116       macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
3117       break;
3118
3119     case M_BEQ_I:
3120       s = "beq";
3121       goto beq_i;
3122     case M_BEQL_I:
3123       s = "beql";
3124       likely = 1;
3125       goto beq_i;
3126     case M_BNE_I:
3127       s = "bne";
3128       goto beq_i;
3129     case M_BNEL_I:
3130       s = "bnel";
3131       likely = 1;
3132     beq_i:
3133       if (imm_expr.X_add_number == 0)
3134         {
3135           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3136                        0);
3137           return;
3138         }
3139       load_register (&icnt, AT, &imm_expr, 0);
3140       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3141       break;
3142
3143     case M_BGEL:
3144       likely = 1;
3145     case M_BGE:
3146       if (treg == 0)
3147         {
3148           macro_build ((char *) NULL, &icnt, &offset_expr,
3149                        likely ? "bgezl" : "bgez",
3150                        "s,p", sreg);
3151           return;
3152         }
3153       if (sreg == 0)
3154         {
3155           macro_build ((char *) NULL, &icnt, &offset_expr,
3156                        likely ? "blezl" : "blez",
3157                        "s,p", treg);
3158           return;
3159         }
3160       macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
3161       macro_build ((char *) NULL, &icnt, &offset_expr,
3162                    likely ? "beql" : "beq",
3163                    "s,t,p", AT, 0);
3164       break;
3165
3166     case M_BGTL_I:
3167       likely = 1;
3168     case M_BGT_I:
3169       /* check for > max integer */
3170       maxnum = 0x7fffffff;
3171       if (mips_isa >= 3)
3172         {
3173           maxnum <<= 16;
3174           maxnum |= 0xffff;
3175           maxnum <<= 16;
3176           maxnum |= 0xffff;
3177         }
3178       if (imm_expr.X_add_number >= maxnum
3179           && (mips_isa < 3 || sizeof (maxnum) > 4))
3180         {
3181         do_false:
3182           /* result is always false */
3183           if (! likely)
3184             {
3185               as_warn ("Branch %s is always false (nop)", ip->insn_mo->name);
3186               macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3187             }
3188           else
3189             {
3190               as_warn ("Branch likely %s is always false", ip->insn_mo->name);
3191               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3192                            "s,t,p", 0, 0);
3193             }
3194           return;
3195         }
3196       imm_expr.X_add_number++;
3197       /* FALLTHROUGH */
3198     case M_BGE_I:
3199     case M_BGEL_I:
3200       if (mask == M_BGEL_I)
3201         likely = 1;
3202       if (imm_expr.X_add_number == 0)
3203         {
3204           macro_build ((char *) NULL, &icnt, &offset_expr,
3205                        likely ? "bgezl" : "bgez",
3206                        "s,p", sreg);
3207           return;
3208         }
3209       if (imm_expr.X_add_number == 1)
3210         {
3211           macro_build ((char *) NULL, &icnt, &offset_expr,
3212                        likely ? "bgtzl" : "bgtz",
3213                        "s,p", sreg);
3214           return;
3215         }
3216       maxnum = 0x7fffffff;
3217       if (mips_isa >= 3)
3218         {
3219           maxnum <<= 16;
3220           maxnum |= 0xffff;
3221           maxnum <<= 16;
3222           maxnum |= 0xffff;
3223         }
3224       maxnum = - maxnum - 1;
3225       if (imm_expr.X_add_number <= maxnum
3226           && (mips_isa < 3 || sizeof (maxnum) > 4))
3227         {
3228         do_true:
3229           /* result is always true */
3230           as_warn ("Branch %s is always true", ip->insn_mo->name);
3231           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
3232           return;
3233         }
3234       set_at (&icnt, sreg, 0);
3235       macro_build ((char *) NULL, &icnt, &offset_expr,
3236                    likely ? "beql" : "beq",
3237                    "s,t,p", AT, 0);
3238       break;
3239
3240     case M_BGEUL:
3241       likely = 1;
3242     case M_BGEU:
3243       if (treg == 0)
3244         goto do_true;
3245       if (sreg == 0)
3246         {
3247           macro_build ((char *) NULL, &icnt, &offset_expr,
3248                        likely ? "beql" : "beq",
3249                        "s,t,p", 0, treg);
3250           return;
3251         }
3252       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
3253                    treg);
3254       macro_build ((char *) NULL, &icnt, &offset_expr,
3255                    likely ? "beql" : "beq",
3256                    "s,t,p", AT, 0);
3257       break;
3258
3259     case M_BGTUL_I:
3260       likely = 1;
3261     case M_BGTU_I:
3262       if (sreg == 0 || imm_expr.X_add_number == -1)
3263         goto do_false;
3264       imm_expr.X_add_number++;
3265       /* FALLTHROUGH */
3266     case M_BGEU_I:
3267     case M_BGEUL_I:
3268       if (mask == M_BGEUL_I)
3269         likely = 1;
3270       if (imm_expr.X_add_number == 0)
3271         goto do_true;
3272       if (imm_expr.X_add_number == 1)
3273         {
3274           macro_build ((char *) NULL, &icnt, &offset_expr,
3275                        likely ? "bnel" : "bne",
3276                        "s,t,p", sreg, 0);
3277           return;
3278         }
3279       set_at (&icnt, sreg, 1);
3280       macro_build ((char *) NULL, &icnt, &offset_expr,
3281                    likely ? "beql" : "beq",
3282                    "s,t,p", AT, 0);
3283       break;
3284
3285     case M_BGTL:
3286       likely = 1;
3287     case M_BGT:
3288       if (treg == 0)
3289         {
3290           macro_build ((char *) NULL, &icnt, &offset_expr,
3291                        likely ? "bgtzl" : "bgtz",
3292                        "s,p", sreg);
3293           return;
3294         }
3295       if (sreg == 0)
3296         {
3297           macro_build ((char *) NULL, &icnt, &offset_expr,
3298                        likely ? "bltzl" : "bltz",
3299                        "s,p", treg);
3300           return;
3301         }
3302       macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
3303       macro_build ((char *) NULL, &icnt, &offset_expr,
3304                    likely ? "bnel" : "bne",
3305                    "s,t,p", AT, 0);
3306       break;
3307
3308     case M_BGTUL:
3309       likely = 1;
3310     case M_BGTU:
3311       if (treg == 0)
3312         {
3313           macro_build ((char *) NULL, &icnt, &offset_expr,
3314                        likely ? "bnel" : "bne",
3315                        "s,t,p", sreg, 0);
3316           return;
3317         }
3318       if (sreg == 0)
3319         goto do_false;
3320       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
3321                    sreg);
3322       macro_build ((char *) NULL, &icnt, &offset_expr,
3323                    likely ? "bnel" : "bne",
3324                    "s,t,p", AT, 0);
3325       break;
3326
3327     case M_BLEL:
3328       likely = 1;
3329     case M_BLE:
3330       if (treg == 0)
3331         {
3332           macro_build ((char *) NULL, &icnt, &offset_expr,
3333                        likely ? "blezl" : "blez",
3334                        "s,p", sreg);
3335           return;
3336         }
3337       if (sreg == 0)
3338         {
3339           macro_build ((char *) NULL, &icnt, &offset_expr,
3340                        likely ? "bgezl" : "bgez",
3341                        "s,p", treg);
3342           return;
3343         }
3344       macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
3345       macro_build ((char *) NULL, &icnt, &offset_expr,
3346                    likely ? "beql" : "beq",
3347                    "s,t,p", AT, 0);
3348       break;
3349
3350     case M_BLEL_I:
3351       likely = 1;
3352     case M_BLE_I:
3353       maxnum = 0x7fffffff;
3354       if (mips_isa >= 3)
3355         {
3356           maxnum <<= 16;
3357           maxnum |= 0xffff;
3358           maxnum <<= 16;
3359           maxnum |= 0xffff;
3360         }
3361       if (imm_expr.X_add_number >= maxnum
3362           && (mips_isa < 3 || sizeof (maxnum) > 4))
3363         goto do_true;
3364       imm_expr.X_add_number++;
3365       /* FALLTHROUGH */
3366     case M_BLT_I:
3367     case M_BLTL_I:
3368       if (mask == M_BLTL_I)
3369         likely = 1;
3370       if (imm_expr.X_add_number == 0)
3371         {
3372           macro_build ((char *) NULL, &icnt, &offset_expr,
3373                        likely ? "bltzl" : "bltz",
3374                        "s,p", sreg);
3375           return;
3376         }
3377       if (imm_expr.X_add_number == 1)
3378         {
3379           macro_build ((char *) NULL, &icnt, &offset_expr,
3380                        likely ? "blezl" : "blez",
3381                        "s,p", sreg);
3382           return;
3383         }
3384       set_at (&icnt, sreg, 0);
3385       macro_build ((char *) NULL, &icnt, &offset_expr,
3386                    likely ? "bnel" : "bne",
3387                    "s,t,p", AT, 0);
3388       break;
3389
3390     case M_BLEUL:
3391       likely = 1;
3392     case M_BLEU:
3393       if (treg == 0)
3394         {
3395           macro_build ((char *) NULL, &icnt, &offset_expr,
3396                        likely ? "beql" : "beq",
3397                        "s,t,p", sreg, 0);
3398           return;
3399         }
3400       if (sreg == 0)
3401         goto do_true;
3402       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
3403                    sreg);
3404       macro_build ((char *) NULL, &icnt, &offset_expr,
3405                    likely ? "beql" : "beq",
3406                    "s,t,p", AT, 0);
3407       break;
3408
3409     case M_BLEUL_I:
3410       likely = 1;
3411     case M_BLEU_I:
3412       if (sreg == 0 || imm_expr.X_add_number == -1)
3413         goto do_true;
3414       imm_expr.X_add_number++;
3415       /* FALLTHROUGH */
3416     case M_BLTU_I:
3417     case M_BLTUL_I:
3418       if (mask == M_BLTUL_I)
3419         likely = 1;
3420       if (imm_expr.X_add_number == 0)
3421         goto do_false;
3422       if (imm_expr.X_add_number == 1)
3423         {
3424           macro_build ((char *) NULL, &icnt, &offset_expr,
3425                        likely ? "beql" : "beq",
3426                        "s,t,p", sreg, 0);
3427           return;
3428         }
3429       set_at (&icnt, sreg, 1);
3430       macro_build ((char *) NULL, &icnt, &offset_expr,
3431                    likely ? "bnel" : "bne",
3432                    "s,t,p", AT, 0);
3433       break;
3434
3435     case M_BLTL:
3436       likely = 1;
3437     case M_BLT:
3438       if (treg == 0)
3439         {
3440           macro_build ((char *) NULL, &icnt, &offset_expr,
3441                        likely ? "bltzl" : "bltz",
3442                        "s,p", sreg);
3443           return;
3444         }
3445       if (sreg == 0)
3446         {
3447           macro_build ((char *) NULL, &icnt, &offset_expr,
3448                        likely ? "bgtzl" : "bgtz",
3449                        "s,p", treg);
3450           return;
3451         }
3452       macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
3453       macro_build ((char *) NULL, &icnt, &offset_expr,
3454                    likely ? "bnel" : "bne",
3455                    "s,t,p", AT, 0);
3456       break;
3457
3458     case M_BLTUL:
3459       likely = 1;
3460     case M_BLTU:
3461       if (treg == 0)
3462         goto do_false;
3463       if (sreg == 0)
3464         {
3465           macro_build ((char *) NULL, &icnt, &offset_expr,
3466                        likely ? "bnel" : "bne",
3467                        "s,t,p", 0, treg);
3468           return;
3469         }
3470       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
3471                    treg);
3472       macro_build ((char *) NULL, &icnt, &offset_expr,
3473                    likely ? "bnel" : "bne",
3474                    "s,t,p", AT, 0);
3475       break;
3476
3477     case M_DDIV_3:
3478       dbl = 1;
3479     case M_DIV_3:
3480       s = "mflo";
3481       goto do_div3;
3482     case M_DREM_3:
3483       dbl = 1;
3484     case M_REM_3:
3485       s = "mfhi";
3486     do_div3:
3487       if (treg == 0)
3488         {
3489           as_warn ("Divide by zero.");
3490           if (mips_trap)
3491             macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
3492           else
3493             macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
3494           return;
3495         }
3496
3497       mips_emit_delays (true);
3498       ++mips_noreorder;
3499       mips_any_noreorder = 1;
3500       macro_build ((char *) NULL, &icnt, NULL,
3501                    dbl ? "ddiv" : "div",
3502                    "z,s,t", sreg, treg);
3503       if (mips_trap)
3504         macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
3505       else
3506         {
3507           expr1.X_add_number = 8;
3508           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
3509           macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3510           macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
3511         }
3512       expr1.X_add_number = -1;
3513       macro_build ((char *) NULL, &icnt, &expr1,
3514                    dbl ? "daddiu" : "addiu",
3515                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
3516       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
3517       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
3518       if (dbl)
3519         {
3520           expr1.X_add_number = 1;
3521           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
3522                        (int) BFD_RELOC_LO16);
3523           macro_build ((char *) NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT,
3524                        31);
3525         }
3526       else
3527         {
3528           expr1.X_add_number = 0x80000000;
3529           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
3530                        (int) BFD_RELOC_HI16);
3531         }
3532       if (mips_trap)
3533         macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", sreg, AT);
3534       else
3535         {
3536           expr1.X_add_number = 8;
3537           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
3538           macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3539           macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
3540         }
3541       --mips_noreorder;
3542       macro_build ((char *) NULL, &icnt, NULL, s, "d", dreg);
3543       break;
3544
3545     case M_DIV_3I:
3546       s = "div";
3547       s2 = "mflo";
3548       goto do_divi;
3549     case M_DIVU_3I:
3550       s = "divu";
3551       s2 = "mflo";
3552       goto do_divi;
3553     case M_REM_3I:
3554       s = "div";
3555       s2 = "mfhi";
3556       goto do_divi;
3557     case M_REMU_3I:
3558       s = "divu";
3559       s2 = "mfhi";
3560       goto do_divi;
3561     case M_DDIV_3I:
3562       dbl = 1;
3563       s = "ddiv";
3564       s2 = "mflo";
3565       goto do_divi;
3566     case M_DDIVU_3I:
3567       dbl = 1;
3568       s = "ddivu";
3569       s2 = "mflo";
3570       goto do_divi;
3571     case M_DREM_3I:
3572       dbl = 1;
3573       s = "ddiv";
3574       s2 = "mfhi";
3575       goto do_divi;
3576     case M_DREMU_3I:
3577       dbl = 1;
3578       s = "ddivu";
3579       s2 = "mfhi";
3580     do_divi:
3581       if (imm_expr.X_add_number == 0)
3582         {
3583           as_warn ("Divide by zero.");
3584           if (mips_trap)
3585             macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
3586           else
3587             macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
3588           return;
3589         }
3590       if (imm_expr.X_add_number == 1)
3591         {
3592           if (strcmp (s2, "mflo") == 0)
3593             macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg,
3594                          sreg);
3595           else
3596             macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
3597           return;
3598         }
3599       if (imm_expr.X_add_number == -1
3600           && s[strlen (s) - 1] != 'u')
3601         {
3602           if (strcmp (s2, "mflo") == 0)
3603             {
3604               if (dbl)
3605                 macro_build ((char *) NULL, &icnt, NULL, "dneg", "d,w", dreg,
3606                              sreg);
3607               else
3608                 macro_build ((char *) NULL, &icnt, NULL, "neg", "d,w", dreg,
3609                              sreg);
3610             }
3611           else
3612             macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
3613           return;
3614         }
3615
3616       load_register (&icnt, AT, &imm_expr, dbl);
3617       macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
3618       macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
3619       break;
3620
3621     case M_DIVU_3:
3622       s = "divu";
3623       s2 = "mflo";
3624       goto do_divu3;
3625     case M_REMU_3:
3626       s = "divu";
3627       s2 = "mfhi";
3628       goto do_divu3;
3629     case M_DDIVU_3:
3630       s = "ddivu";
3631       s2 = "mflo";
3632       goto do_divu3;
3633     case M_DREMU_3:
3634       s = "ddivu";
3635       s2 = "mfhi";
3636     do_divu3:
3637       mips_emit_delays (true);
3638       ++mips_noreorder;
3639       mips_any_noreorder = 1;
3640       macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
3641       if (mips_trap)
3642         macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
3643       else
3644         {
3645           expr1.X_add_number = 8;
3646           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
3647           macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3648           macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
3649         }
3650       --mips_noreorder;
3651       macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
3652       return;
3653
3654     case M_DLA_AB:
3655       dbl = 1;
3656     case M_LA_AB:
3657       /* Load the address of a symbol into a register.  If breg is not
3658          zero, we then add a base register to it.  */
3659
3660       /* When generating embedded PIC code, we permit expressions of
3661          the form
3662            la   $4,foo-bar
3663          where bar is an address in the .text section.  These are used
3664          when getting the addresses of functions.  We don't permit
3665          X_add_number to be non-zero, because if the symbol is
3666          external the relaxing code needs to know that any addend is
3667          purely the offset to X_op_symbol.  */
3668       if (mips_pic == EMBEDDED_PIC
3669           && offset_expr.X_op == O_subtract
3670           && now_seg == text_section
3671           && (offset_expr.X_op_symbol->sy_value.X_op == O_constant
3672               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == text_section
3673               : (offset_expr.X_op_symbol->sy_value.X_op == O_symbol
3674                  && (S_GET_SEGMENT (offset_expr.X_op_symbol
3675                                     ->sy_value.X_add_symbol)
3676                      == text_section)))
3677           && breg == 0
3678           && offset_expr.X_add_number == 0)
3679         {
3680           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
3681                        treg, (int) BFD_RELOC_PCREL_HI16_S);
3682           macro_build ((char *) NULL, &icnt, &offset_expr,
3683                        mips_isa < 3 ? "addiu" : "daddiu",
3684                        "t,r,j", treg, treg, (int) BFD_RELOC_PCREL_LO16);
3685           return;
3686         }
3687
3688       if (offset_expr.X_op != O_symbol
3689           && offset_expr.X_op != O_constant)
3690         {
3691           as_bad ("expression too complex");
3692           offset_expr.X_op = O_constant;
3693         }
3694
3695       if (treg == breg)
3696         {
3697           tempreg = AT;
3698           used_at = 1;
3699         }
3700       else
3701         {
3702           tempreg = treg;
3703           used_at = 0;
3704         }
3705
3706       if (offset_expr.X_op == O_constant)
3707         load_register (&icnt, tempreg, &offset_expr, dbl);
3708       else if (mips_pic == NO_PIC)
3709         {
3710           /* If this is a reference to an GP relative symbol, we want
3711                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_MIPS_GPREL)
3712              Otherwise we want
3713                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
3714                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
3715              If we have a constant, we need two instructions anyhow,
3716              so we may as well always use the latter form.  */
3717           if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
3718               || nopic_need_relax (offset_expr.X_add_symbol))
3719             p = NULL;
3720           else
3721             {
3722               frag_grow (20);
3723               macro_build ((char *) NULL, &icnt, &offset_expr,
3724                            mips_isa < 3 ? "addiu" : "daddiu",
3725                            "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
3726               p = frag_var (rs_machine_dependent, 8, 0,
3727                             RELAX_ENCODE (4, 8, 0, 4, 0,
3728                                           mips_warn_about_macros),
3729                             offset_expr.X_add_symbol, (long) 0,
3730                             (char *) NULL);
3731             }
3732           macro_build_lui (p, &icnt, &offset_expr, tempreg);
3733           if (p != NULL)
3734             p += 4;
3735           macro_build (p, &icnt, &offset_expr,
3736                        mips_isa < 3 ? "addiu" : "daddiu",
3737                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
3738         }
3739       else if (mips_pic == SVR4_PIC && ! mips_big_got)
3740         {
3741           /* If this is a reference to an external symbol, and there
3742              is no constant, we want
3743                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
3744              For a local symbol, we want
3745                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
3746                nop
3747                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
3748
3749              If we have a small constant, and this is a reference to
3750              an external symbol, we want
3751                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
3752                nop
3753                addiu    $tempreg,$tempreg,<constant>
3754              For a local symbol, we want the same instruction
3755              sequence, but we output a BFD_RELOC_LO16 reloc on the
3756              addiu instruction.
3757
3758              If we have a large constant, and this is a reference to
3759              an external symbol, we want
3760                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
3761                lui      $at,<hiconstant>
3762                addiu    $at,$at,<loconstant>
3763                addu     $tempreg,$tempreg,$at
3764              For a local symbol, we want the same instruction
3765              sequence, but we output a BFD_RELOC_LO16 reloc on the
3766              addiu instruction.  */
3767           expr1.X_add_number = offset_expr.X_add_number;
3768           offset_expr.X_add_number = 0;
3769           frag_grow (32);
3770           macro_build ((char *) NULL, &icnt, &offset_expr,
3771                        dbl ? "ld" : "lw",
3772                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
3773           if (expr1.X_add_number == 0)
3774             {
3775               int off;
3776
3777               if (breg == 0)
3778                 off = 0;
3779               else
3780                 {
3781                   /* We're going to put in an addu instruction using
3782                      tempreg, so we may as well insert the nop right
3783                      now.  */
3784                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3785                                "nop", "");
3786                   off = 4;
3787                 }
3788               p = frag_var (rs_machine_dependent, 8 - off, 0,
3789                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
3790                                           (breg == 0
3791                                            ? mips_warn_about_macros
3792                                            : 0)),
3793                             offset_expr.X_add_symbol, (long) 0,
3794                             (char *) NULL);
3795               if (breg == 0)
3796                 {
3797                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
3798                   p += 4;
3799                 }
3800               macro_build (p, &icnt, &expr1,
3801                            mips_isa < 3 ? "addiu" : "daddiu",
3802                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
3803               /* FIXME: If breg == 0, and the next instruction uses
3804                  $tempreg, then if this variant case is used an extra
3805                  nop will be generated.  */
3806             }
3807           else if (expr1.X_add_number >= -0x8000
3808                    && expr1.X_add_number < 0x8000)
3809             {
3810               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3811                            "nop", "");
3812               macro_build ((char *) NULL, &icnt, &expr1,
3813                            mips_isa < 3 ? "addiu" : "daddiu",
3814                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
3815               (void) frag_var (rs_machine_dependent, 0, 0,
3816                                RELAX_ENCODE (0, 0, -12, -4, 0, 0),
3817                                offset_expr.X_add_symbol, (long) 0,
3818                                (char *) NULL);
3819             }
3820           else
3821             {
3822               int off1;
3823
3824               /* If we are going to add in a base register, and the
3825                  target register and the base register are the same,
3826                  then we are using AT as a temporary register.  Since
3827                  we want to load the constant into AT, we add our
3828                  current AT (from the global offset table) and the
3829                  register into the register now, and pretend we were
3830                  not using a base register.  */
3831               if (breg != treg)
3832                 off1 = 0;
3833               else
3834                 {
3835                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3836                                "nop", "");
3837                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3838                                mips_isa < 3 ? "addu" : "daddu",
3839                                "d,v,t", treg, AT, breg);
3840                   breg = 0;
3841                   tempreg = treg;
3842                   off1 = -8;
3843                 }
3844
3845               /* Set mips_optimize around the lui instruction to avoid
3846                  inserting an unnecessary nop after the lw.  */
3847               hold_mips_optimize = mips_optimize;
3848               mips_optimize = 2;
3849               macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
3850               mips_optimize = hold_mips_optimize;
3851
3852               macro_build ((char *) NULL, &icnt, &expr1,
3853                            mips_isa < 3 ? "addiu" : "daddiu",
3854                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
3855               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3856                            mips_isa < 3 ? "addu" : "daddu",
3857                            "d,v,t", tempreg, tempreg, AT);
3858               (void) frag_var (rs_machine_dependent, 0, 0,
3859                                RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
3860                                offset_expr.X_add_symbol, (long) 0,
3861                                (char *) NULL);
3862               used_at = 1;
3863             }
3864         }
3865       else if (mips_pic == SVR4_PIC)
3866         {
3867           int gpdel;
3868
3869           /* This is the large GOT case.  If this is a reference to an
3870              external symbol, and there is no constant, we want
3871                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
3872                addu     $tempreg,$tempreg,$gp
3873                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
3874              For a local symbol, we want
3875                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
3876                nop
3877                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
3878
3879              If we have a small constant, and this is a reference to
3880              an external symbol, we want
3881                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
3882                addu     $tempreg,$tempreg,$gp
3883                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
3884                nop
3885                addiu    $tempreg,$tempreg,<constant>
3886              For a local symbol, we want
3887                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
3888                nop
3889                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
3890
3891              If we have a large constant, and this is a reference to
3892              an external symbol, we want
3893                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
3894                addu     $tempreg,$tempreg,$gp
3895                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
3896                lui      $at,<hiconstant>
3897                addiu    $at,$at,<loconstant>
3898                addu     $tempreg,$tempreg,$at
3899              For a local symbol, we want
3900                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
3901                lui      $at,<hiconstant>
3902                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
3903                addu     $tempreg,$tempreg,$at
3904              */
3905           expr1.X_add_number = offset_expr.X_add_number;
3906           offset_expr.X_add_number = 0;
3907           frag_grow (52);
3908           if (reg_needs_delay (GP))
3909             gpdel = 4;
3910           else
3911             gpdel = 0;
3912           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
3913                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
3914           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3915                        mips_isa < 3 ? "addu" : "daddu",
3916                        "d,v,t", tempreg, tempreg, GP);
3917           macro_build ((char *) NULL, &icnt, &offset_expr,
3918                        dbl ? "ld" : "lw",
3919                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
3920                        tempreg);
3921           if (expr1.X_add_number == 0)
3922             {
3923               int off;
3924
3925               if (breg == 0)
3926                 off = 0;
3927               else
3928                 {
3929                   /* We're going to put in an addu instruction using
3930                      tempreg, so we may as well insert the nop right
3931                      now.  */
3932                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3933                                "nop", "");
3934                   off = 4;
3935                 }
3936
3937               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
3938                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
3939                                           8 + gpdel, 0,
3940                                           (breg == 0
3941                                            ? mips_warn_about_macros
3942                                            : 0)),
3943                             offset_expr.X_add_symbol, (long) 0,
3944                             (char *) NULL);
3945             }
3946           else if (expr1.X_add_number >= -0x8000
3947                    && expr1.X_add_number < 0x8000)
3948             {
3949               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3950                            "nop", "");
3951               macro_build ((char *) NULL, &icnt, &expr1,
3952                            mips_isa < 3 ? "addiu" : "daddiu",
3953                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
3954
3955               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
3956                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
3957                                           (breg == 0
3958                                            ? mips_warn_about_macros
3959                                            : 0)),
3960                             offset_expr.X_add_symbol, (long) 0,
3961                             (char *) NULL);
3962             }
3963           else
3964             {
3965               int adj, dreg;
3966
3967               /* If we are going to add in a base register, and the
3968                  target register and the base register are the same,
3969                  then we are using AT as a temporary register.  Since
3970                  we want to load the constant into AT, we add our
3971                  current AT (from the global offset table) and the
3972                  register into the register now, and pretend we were
3973                  not using a base register.  */
3974               if (breg != treg)
3975                 {
3976                   adj = 0;
3977                   dreg = tempreg;
3978                 }
3979               else
3980                 {
3981                   assert (tempreg == AT);
3982                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3983                                "nop", "");
3984                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3985                                mips_isa < 3 ? "addu" : "daddu",
3986                                "d,v,t", treg, AT, breg);
3987                   dreg = treg;
3988                   adj = 8;
3989                 }
3990
3991               /* Set mips_optimize around the lui instruction to avoid
3992                  inserting an unnecessary nop after the lw.  */
3993               hold_mips_optimize = mips_optimize;
3994               mips_optimize = 2;
3995               macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
3996               mips_optimize = hold_mips_optimize;
3997
3998               macro_build ((char *) NULL, &icnt, &expr1,
3999                            mips_isa < 3 ? "addiu" : "daddiu",
4000                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4001               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4002                            mips_isa < 3 ? "addu" : "daddu",
4003                            "d,v,t", dreg, dreg, AT);
4004
4005               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4006                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4007                                           8 + gpdel, 0,
4008                                           (breg == 0
4009                                            ? mips_warn_about_macros
4010                                            : 0)),
4011                             offset_expr.X_add_symbol, (long) 0,
4012                             (char *) NULL);
4013
4014               used_at = 1;
4015             }
4016
4017           if (gpdel > 0)
4018             {
4019               /* This is needed because this instruction uses $gp, but
4020                  the first instruction on the main stream does not.  */
4021               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4022               p += 4;
4023             }
4024           macro_build (p, &icnt, &offset_expr,
4025                        dbl ? "ld" : "lw",
4026                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
4027           p += 4;
4028           if (expr1.X_add_number >= -0x8000
4029               && expr1.X_add_number < 0x8000)
4030             {
4031               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4032               p += 4;
4033               macro_build (p, &icnt, &expr1,
4034                            mips_isa < 3 ? "addiu" : "daddiu",
4035                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4036               /* FIXME: If add_number is 0, and there was no base
4037                  register, the external symbol case ended with a load,
4038                  so if the symbol turns out to not be external, and
4039                  the next instruction uses tempreg, an unnecessary nop
4040                  will be inserted.  */
4041             }
4042           else
4043             {
4044               if (breg == treg)
4045                 {
4046                   /* We must add in the base register now, as in the
4047                      external symbol case.  */
4048                   assert (tempreg == AT);
4049                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4050                   p += 4;
4051                   macro_build (p, &icnt, (expressionS *) NULL,
4052                                mips_isa < 3 ? "addu" : "daddu",
4053                                "d,v,t", treg, AT, breg);
4054                   p += 4;
4055                   tempreg = treg;
4056                   /* We set breg to 0 because we have arranged to add
4057                      it in in both cases.  */
4058                   breg = 0;
4059                 }
4060
4061               macro_build_lui (p, &icnt, &expr1, AT);
4062               p += 4;
4063               macro_build (p, &icnt, &expr1,
4064                            mips_isa < 3 ? "addiu" : "daddiu",
4065                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4066               p += 4;
4067               macro_build (p, &icnt, (expressionS *) NULL,
4068                            mips_isa < 3 ? "addu" : "daddu",
4069                            "d,v,t", tempreg, tempreg, AT);
4070               p += 4;
4071             }
4072         }
4073       else if (mips_pic == EMBEDDED_PIC)
4074         {
4075           /* We use
4076                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_MIPS_GPREL)
4077              */
4078           macro_build ((char *) NULL, &icnt, &offset_expr,
4079                        mips_isa < 3 ? "addiu" : "daddiu",
4080                        "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
4081         }
4082       else
4083         abort ();
4084
4085       if (breg != 0)
4086         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4087                      mips_isa < 3 ? "addu" : "daddu",
4088                      "d,v,t", treg, tempreg, breg);
4089
4090       if (! used_at)
4091         return;
4092
4093       break;
4094
4095     case M_J_A:
4096       /* The j instruction may not be used in PIC code, since it
4097          requires an absolute address.  We convert it to a b
4098          instruction.  */
4099       if (mips_pic == NO_PIC)
4100         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
4101       else
4102         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4103       return;
4104
4105       /* The jal instructions must be handled as macros because when
4106          generating PIC code they expand to multi-instruction
4107          sequences.  Normally they are simple instructions.  */
4108     case M_JAL_1:
4109       dreg = RA;
4110       /* Fall through.  */
4111     case M_JAL_2:
4112       if (mips_pic == NO_PIC
4113           || mips_pic == EMBEDDED_PIC)
4114         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
4115                      "d,s", dreg, sreg);
4116       else if (mips_pic == SVR4_PIC)
4117         {
4118           if (sreg != PIC_CALL_REG)
4119             as_warn ("MIPS PIC call to register other than $25");
4120       
4121           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
4122                        "d,s", dreg, sreg);
4123           if (mips_cprestore_offset < 0)
4124             as_warn ("No .cprestore pseudo-op used in PIC code");
4125           else
4126             {
4127               expr1.X_add_number = mips_cprestore_offset;
4128               macro_build ((char *) NULL, &icnt, &expr1,
4129                            mips_isa < 3 ? "lw" : "ld",
4130                            "t,o(b)", GP, (int) BFD_RELOC_LO16, mips_frame_reg);
4131             }
4132         }
4133       else
4134         abort ();
4135
4136       return;
4137
4138     case M_JAL_A:
4139       if (mips_pic == NO_PIC)
4140         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
4141       else if (mips_pic == SVR4_PIC)
4142         {
4143           /* If this is a reference to an external symbol, and we are
4144              using a small GOT, we want
4145                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
4146                nop
4147                jalr     $25
4148                nop
4149                lw       $gp,cprestore($sp)
4150              The cprestore value is set using the .cprestore
4151              pseudo-op.  If we are using a big GOT, we want
4152                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
4153                addu     $25,$25,$gp
4154                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
4155                nop
4156                jalr     $25
4157                nop
4158                lw       $gp,cprestore($sp)
4159              If the symbol is not external, we want
4160                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
4161                nop
4162                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
4163                jalr     $25
4164                nop
4165                lw $gp,cprestore($sp) */
4166           frag_grow (40);
4167           if (! mips_big_got)
4168             {
4169               macro_build ((char *) NULL, &icnt, &offset_expr,
4170                            mips_isa < 3 ? "lw" : "ld",
4171                            "t,o(b)", PIC_CALL_REG,
4172                            (int) BFD_RELOC_MIPS_CALL16, GP);
4173               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4174                            "nop", "");
4175               p = frag_var (rs_machine_dependent, 4, 0,
4176                             RELAX_ENCODE (0, 4, -8, 0, 0, 0),
4177                             offset_expr.X_add_symbol, (long) 0, (char *) NULL);
4178             }
4179           else
4180             {
4181               int gpdel;
4182
4183               if (reg_needs_delay (GP))
4184                 gpdel = 4;
4185               else
4186                 gpdel = 0;
4187               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4188                            PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
4189               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4190                            mips_isa < 3 ? "addu" : "daddu",
4191                            "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
4192               macro_build ((char *) NULL, &icnt, &offset_expr,
4193                            mips_isa < 3 ? "lw" : "ld",
4194                            "t,o(b)", PIC_CALL_REG,
4195                            (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
4196               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4197                            "nop", "");
4198               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4199                             RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
4200                                           0, 0),
4201                             offset_expr.X_add_symbol, (long) 0, (char *) NULL);
4202               if (gpdel > 0)
4203                 {
4204                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4205                   p += 4;
4206                 }
4207               macro_build (p, &icnt, &offset_expr,
4208                            mips_isa < 3 ? "lw" : "ld",
4209                            "t,o(b)", PIC_CALL_REG,
4210                            (int) BFD_RELOC_MIPS_GOT16, GP);
4211               p += 4;
4212               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4213               p += 4;
4214             }                      
4215           macro_build (p, &icnt, &offset_expr,
4216                        mips_isa < 3 ? "addiu" : "daddiu",
4217                        "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
4218                        (int) BFD_RELOC_LO16);
4219           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4220                        "jalr", "s", PIC_CALL_REG);
4221           if (mips_cprestore_offset < 0)
4222             as_warn ("No .cprestore pseudo-op used in PIC code");
4223           else
4224             {
4225               if (mips_noreorder)
4226                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4227                              "nop", "");
4228               expr1.X_add_number = mips_cprestore_offset;
4229               macro_build ((char *) NULL, &icnt, &expr1,
4230                            mips_isa < 3 ? "lw" : "ld",
4231                            "t,o(b)", GP, (int) BFD_RELOC_LO16,
4232                            mips_frame_reg);
4233             }
4234         }
4235       else if (mips_pic == EMBEDDED_PIC)
4236         {
4237           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
4238           /* The linker may expand the call to a longer sequence which
4239              uses $at, so we must break rather than return.  */
4240           break;
4241         }
4242       else
4243         abort ();
4244
4245       return;
4246
4247     case M_LB_AB:
4248       s = "lb";
4249       goto ld;
4250     case M_LBU_AB:
4251       s = "lbu";
4252       goto ld;
4253     case M_LH_AB:
4254       s = "lh";
4255       goto ld;
4256     case M_LHU_AB:
4257       s = "lhu";
4258       goto ld;
4259     case M_LW_AB:
4260       s = "lw";
4261       goto ld;
4262     case M_LWC0_AB:
4263       s = "lwc0";
4264       coproc = 1;
4265       goto ld;
4266     case M_LWC1_AB:
4267       s = "lwc1";
4268       coproc = 1;
4269       goto ld;
4270     case M_LWC2_AB:
4271       s = "lwc2";
4272       coproc = 1;
4273       goto ld;
4274     case M_LWC3_AB:
4275       s = "lwc3";
4276       coproc = 1;
4277       goto ld;
4278     case M_LWL_AB:
4279       s = "lwl";
4280       lr = 1;
4281       goto ld;
4282     case M_LWR_AB:
4283       s = "lwr";
4284       lr = 1;
4285       goto ld;
4286     case M_LDC1_AB:
4287       s = "ldc1";
4288       coproc = 1;
4289       goto ld;
4290     case M_LDC2_AB:
4291       s = "ldc2";
4292       coproc = 1;
4293       goto ld;
4294     case M_LDC3_AB:
4295       s = "ldc3";
4296       coproc = 1;
4297       goto ld;
4298     case M_LDL_AB:
4299       s = "ldl";
4300       lr = 1;
4301       goto ld;
4302     case M_LDR_AB:
4303       s = "ldr";
4304       lr = 1;
4305       goto ld;
4306     case M_LL_AB:
4307       s = "ll";
4308       goto ld;
4309     case M_LLD_AB:
4310       s = "lld";
4311       goto ld;
4312     case M_LWU_AB:
4313       s = "lwu";
4314     ld:
4315       if (breg == treg || coproc || lr)
4316         {
4317           tempreg = AT;
4318           used_at = 1;
4319         }
4320       else
4321         {
4322           tempreg = treg;
4323           used_at = 0;
4324         }
4325       goto ld_st;
4326     case M_SB_AB:
4327       s = "sb";
4328       goto st;
4329     case M_SH_AB:
4330       s = "sh";
4331       goto st;
4332     case M_SW_AB:
4333       s = "sw";
4334       goto st;
4335     case M_SWC0_AB:
4336       s = "swc0";
4337       coproc = 1;
4338       goto st;
4339     case M_SWC1_AB:
4340       s = "swc1";
4341       coproc = 1;
4342       goto st;
4343     case M_SWC2_AB:
4344       s = "swc2";
4345       coproc = 1;
4346       goto st;
4347     case M_SWC3_AB:
4348       s = "swc3";
4349       coproc = 1;
4350       goto st;
4351     case M_SWL_AB:
4352       s = "swl";
4353       goto st;
4354     case M_SWR_AB:
4355       s = "swr";
4356       goto st;
4357     case M_SC_AB:
4358       s = "sc";
4359       goto st;
4360     case M_SCD_AB:
4361       s = "scd";
4362       goto st;
4363     case M_SDC1_AB:
4364       s = "sdc1";
4365       coproc = 1;
4366       goto st;
4367     case M_SDC2_AB:
4368       s = "sdc2";
4369       coproc = 1;
4370       goto st;
4371     case M_SDC3_AB:
4372       s = "sdc3";
4373       coproc = 1;
4374       goto st;
4375     case M_SDL_AB:
4376       s = "sdl";
4377       goto st;
4378     case M_SDR_AB:
4379       s = "sdr";
4380     st:
4381       tempreg = AT;
4382       used_at = 1;
4383     ld_st:
4384       if (mask == M_LWC1_AB
4385           || mask == M_SWC1_AB
4386           || mask == M_LDC1_AB
4387           || mask == M_SDC1_AB
4388           || mask == M_L_DAB
4389           || mask == M_S_DAB)
4390         fmt = "T,o(b)";
4391       else if (coproc)
4392         fmt = "E,o(b)";
4393       else
4394         fmt = "t,o(b)";
4395
4396       if (offset_expr.X_op != O_constant
4397           && offset_expr.X_op != O_symbol)
4398         {
4399           as_bad ("expression too complex");
4400           offset_expr.X_op = O_constant;
4401         }
4402
4403       /* A constant expression in PIC code can be handled just as it
4404          is in non PIC code.  */
4405       if (mips_pic == NO_PIC
4406           || offset_expr.X_op == O_constant)
4407         {
4408           /* If this is a reference to a GP relative symbol, and there
4409              is no base register, we want
4410                <op>     $treg,<sym>($gp)        (BFD_RELOC_MIPS_GPREL)
4411              Otherwise, if there is no base register, we want
4412                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4413                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
4414              If we have a constant, we need two instructions anyhow,
4415              so we always use the latter form.
4416
4417              If we have a base register, and this is a reference to a
4418              GP relative symbol, we want
4419                addu     $tempreg,$breg,$gp
4420                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GPREL)
4421              Otherwise we want
4422                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4423                addu     $tempreg,$tempreg,$breg
4424                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
4425              With a constant we always use the latter case.  */
4426           if (breg == 0)
4427             {
4428               if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
4429                   || nopic_need_relax (offset_expr.X_add_symbol))
4430                 p = NULL;
4431               else
4432                 {
4433                   frag_grow (20);
4434                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4435                                treg, (int) BFD_RELOC_MIPS_GPREL, GP);
4436                   p = frag_var (rs_machine_dependent, 8, 0,
4437                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4438                                               (mips_warn_about_macros
4439                                                || (used_at && mips_noat))),
4440                                 offset_expr.X_add_symbol, (long) 0,
4441                                 (char *) NULL);
4442                   used_at = 0;
4443                 }
4444               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4445               if (p != NULL)
4446                 p += 4;
4447               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
4448                            (int) BFD_RELOC_LO16, tempreg);
4449             }
4450           else
4451             {
4452               if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
4453                   || nopic_need_relax (offset_expr.X_add_symbol))
4454                 p = NULL;
4455               else
4456                 {
4457                   frag_grow (28);
4458                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4459                                mips_isa < 3 ? "addu" : "daddu",
4460                                "d,v,t", tempreg, breg, GP);
4461                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4462                                treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
4463                   p = frag_var (rs_machine_dependent, 12, 0,
4464                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
4465                                 offset_expr.X_add_symbol, (long) 0,
4466                                 (char *) NULL);
4467                 }
4468               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4469               if (p != NULL)
4470                 p += 4;
4471               macro_build (p, &icnt, (expressionS *) NULL,
4472                            mips_isa < 3 ? "addu" : "daddu",
4473                            "d,v,t", tempreg, tempreg, breg);
4474               if (p != NULL)
4475                 p += 4;
4476               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
4477                            (int) BFD_RELOC_LO16, tempreg);
4478             }
4479         }
4480       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4481         {
4482           /* If this is a reference to an external symbol, we want
4483                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4484                nop
4485                <op>     $treg,0($tempreg)
4486              Otherwise we want
4487                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4488                nop
4489                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4490                <op>     $treg,0($tempreg)
4491              If there is a base register, we add it to $tempreg before
4492              the <op>.  If there is a constant, we stick it in the
4493              <op> instruction.  We don't handle constants larger than
4494              16 bits, because we have no way to load the upper 16 bits
4495              (actually, we could handle them for the subset of cases
4496              in which we are not using $at).  */
4497           assert (offset_expr.X_op == O_symbol);
4498           expr1.X_add_number = offset_expr.X_add_number;
4499           offset_expr.X_add_number = 0;
4500           if (expr1.X_add_number < -0x8000
4501               || expr1.X_add_number >= 0x8000)
4502             as_bad ("PIC code offset overflow (max 16 signed bits)");
4503           frag_grow (20);
4504           macro_build ((char *) NULL, &icnt, &offset_expr,
4505                        mips_isa < 3 ? "lw" : "ld",
4506                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
4507           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
4508           p = frag_var (rs_machine_dependent, 4, 0, 
4509                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
4510                         offset_expr.X_add_symbol, (long) 0,
4511                         (char *) NULL);
4512           macro_build (p, &icnt, &offset_expr,
4513                        mips_isa < 3 ? "addiu" : "daddiu",
4514                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4515           if (breg != 0)
4516             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4517                          mips_isa < 3 ? "addu" : "daddu",
4518                          "d,v,t", tempreg, tempreg, breg);
4519           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
4520                        (int) BFD_RELOC_LO16, tempreg);
4521         }
4522       else if (mips_pic == SVR4_PIC)
4523         {
4524           int gpdel;
4525
4526           /* If this is a reference to an external symbol, we want
4527                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4528                addu     $tempreg,$tempreg,$gp
4529                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4530                <op>     $treg,0($tempreg)
4531              Otherwise we want
4532                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4533                nop
4534                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4535                <op>     $treg,0($tempreg)
4536              If there is a base register, we add it to $tempreg before
4537              the <op>.  If there is a constant, we stick it in the
4538              <op> instruction.  We don't handle constants larger than
4539              16 bits, because we have no way to load the upper 16 bits
4540              (actually, we could handle them for the subset of cases
4541              in which we are not using $at).  */
4542           assert (offset_expr.X_op == O_symbol);
4543           expr1.X_add_number = offset_expr.X_add_number;
4544           offset_expr.X_add_number = 0;
4545           if (expr1.X_add_number < -0x8000
4546               || expr1.X_add_number >= 0x8000)
4547             as_bad ("PIC code offset overflow (max 16 signed bits)");
4548           if (reg_needs_delay (GP))
4549             gpdel = 4;
4550           else
4551             gpdel = 0;
4552           frag_grow (36);
4553           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4554                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
4555           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4556                        mips_isa < 3 ? "addu" : "daddu",
4557                        "d,v,t", tempreg, tempreg, GP);
4558           macro_build ((char *) NULL, &icnt, &offset_expr,
4559                        mips_isa < 3 ? "lw" : "ld",
4560                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
4561                        tempreg);
4562           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4563                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
4564                         offset_expr.X_add_symbol, (long) 0, (char *) NULL);
4565           if (gpdel > 0)
4566             {
4567               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4568               p += 4;
4569             }
4570           macro_build (p, &icnt, &offset_expr,
4571                        mips_isa < 3 ? "lw" : "ld",
4572                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
4573           p += 4;
4574           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4575           p += 4;
4576           macro_build (p, &icnt, &offset_expr,
4577                        mips_isa < 3 ? "addiu" : "daddiu",
4578                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4579           if (breg != 0)
4580             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4581                          mips_isa < 3 ? "addu" : "daddu",
4582                          "d,v,t", tempreg, tempreg, breg);
4583           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
4584                        (int) BFD_RELOC_LO16, tempreg);
4585         }
4586       else if (mips_pic == EMBEDDED_PIC)
4587         {
4588           /* If there is no base register, we want
4589                <op>     $treg,<sym>($gp)        (BFD_RELOC_MIPS_GPREL)
4590              If there is a base register, we want
4591                addu     $tempreg,$breg,$gp
4592                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GPREL)
4593              */
4594           assert (offset_expr.X_op == O_symbol);
4595           if (breg == 0)
4596             {
4597               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4598                            treg, (int) BFD_RELOC_MIPS_GPREL, GP);
4599               used_at = 0;
4600             }
4601           else
4602             {
4603               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4604                            mips_isa < 3 ? "addu" : "daddu",
4605                            "d,v,t", tempreg, breg, GP);
4606               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4607                            treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
4608             }
4609         }
4610       else
4611         abort ();
4612
4613       if (! used_at)
4614         return;
4615
4616       break;
4617
4618     case M_LI:
4619     case M_LI_S:
4620       load_register (&icnt, treg, &imm_expr, 0);
4621       return;
4622
4623     case M_DLI:
4624       load_register (&icnt, treg, &imm_expr, 1);
4625       return;
4626
4627     case M_LI_SS:
4628       if (imm_expr.X_op == O_constant)
4629         {
4630           load_register (&icnt, AT, &imm_expr, 0);
4631           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4632                        "mtc1", "t,G", AT, treg);
4633           break;
4634         }
4635       else
4636         {
4637           assert (offset_expr.X_op == O_symbol
4638                   && strcmp (segment_name (S_GET_SEGMENT
4639                                            (offset_expr.X_add_symbol)),
4640                              ".lit4") == 0
4641                   && offset_expr.X_add_number == 0);
4642           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
4643                        treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
4644           return;
4645         }
4646
4647     case M_LI_D:
4648       /* We know that sym is in the .rdata section.  First we get the
4649          upper 16 bits of the address.  */
4650       if (mips_pic == NO_PIC)
4651         {
4652           /* FIXME: This won't work for a 64 bit address.  */
4653           macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
4654         }
4655       else if (mips_pic == SVR4_PIC)
4656         {
4657           macro_build ((char *) NULL, &icnt, &offset_expr,
4658                        mips_isa < 3 ? "lw" : "ld",
4659                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
4660         }
4661       else if (mips_pic == EMBEDDED_PIC)
4662         {
4663           /* For embedded PIC we pick up the entire address off $gp in
4664              a single instruction.  */
4665           macro_build ((char *) NULL, &icnt, &offset_expr,
4666                        mips_isa < 3 ? "addiu" : "daddiu",
4667                        "t,r,j", AT, GP, (int) BFD_RELOC_MIPS_GPREL);
4668           offset_expr.X_op = O_constant;
4669           offset_expr.X_add_number = 0;
4670         }
4671       else
4672         abort ();
4673         
4674       /* Now we load the register(s).  */
4675       if (mips_isa >= 3)
4676         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
4677                      treg, (int) BFD_RELOC_LO16, AT);
4678       else
4679         {
4680           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
4681                        treg, (int) BFD_RELOC_LO16, AT);
4682           if (treg != 31)
4683             {
4684               /* FIXME: How in the world do we deal with the possible
4685                  overflow here?  */
4686               offset_expr.X_add_number += 4;
4687               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
4688                            treg + 1, (int) BFD_RELOC_LO16, AT);
4689             }
4690         }
4691
4692       /* To avoid confusion in tc_gen_reloc, we must ensure that this
4693          does not become a variant frag.  */
4694       frag_wane (frag_now);
4695       frag_new (0);
4696
4697       break;
4698
4699     case M_LI_DD:
4700       assert (offset_expr.X_op == O_symbol
4701               && offset_expr.X_add_number == 0);
4702       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
4703       if (strcmp (s, ".lit8") == 0)
4704         {
4705           if (mips_isa >= 2)
4706             {
4707               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
4708                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
4709               return;
4710             }
4711           breg = GP;
4712           r = BFD_RELOC_MIPS_LITERAL;
4713           goto dob;
4714         }
4715       else
4716         {
4717           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
4718           if (mips_pic == SVR4_PIC)
4719             macro_build ((char *) NULL, &icnt, &offset_expr,
4720                          mips_isa < 3 ? "lw" : "ld",
4721                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
4722           else
4723             {
4724               /* FIXME: This won't work for a 64 bit address.  */
4725               macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
4726             }
4727               
4728           if (mips_isa >= 2)
4729             {
4730               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
4731                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
4732
4733               /* To avoid confusion in tc_gen_reloc, we must ensure
4734                  that this does not become a variant frag.  */
4735               frag_wane (frag_now);
4736               frag_new (0);
4737
4738               break;
4739             }
4740           breg = AT;
4741           r = BFD_RELOC_LO16;
4742           goto dob;
4743         }
4744
4745     case M_L_DOB:
4746       /* Even on a big endian machine $fn comes before $fn+1.  We have
4747          to adjust when loading from memory.  */
4748       r = BFD_RELOC_LO16;
4749     dob:
4750       assert (mips_isa < 2);
4751       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
4752                    target_big_endian ? treg + 1 : treg,
4753                    (int) r, breg);
4754       /* FIXME: A possible overflow which I don't know how to deal
4755          with.  */
4756       offset_expr.X_add_number += 4;
4757       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
4758                    target_big_endian ? treg : treg + 1,
4759                    (int) r, breg);
4760
4761       /* To avoid confusion in tc_gen_reloc, we must ensure that this
4762          does not become a variant frag.  */
4763       frag_wane (frag_now);
4764       frag_new (0);
4765
4766       if (breg != AT)
4767         return;
4768       break;
4769
4770     case M_L_DAB:
4771       /*
4772        * The MIPS assembler seems to check for X_add_number not
4773        * being double aligned and generating:
4774        *        lui     at,%hi(foo+1)
4775        *        addu    at,at,v1
4776        *        addiu   at,at,%lo(foo+1)
4777        *        lwc1    f2,0(at)
4778        *        lwc1    f3,4(at)
4779        * But, the resulting address is the same after relocation so why
4780        * generate the extra instruction?
4781        */
4782       coproc = 1;
4783       if (mips_isa >= 2)
4784         {
4785           s = "ldc1";
4786           goto ld;
4787         }
4788
4789       s = "lwc1";
4790       fmt = "T,o(b)";
4791       goto ldd_std;
4792
4793     case M_S_DAB:
4794       if (mips_isa >= 2)
4795         {
4796           s = "sdc1";
4797           goto st;
4798         }
4799
4800       s = "swc1";
4801       fmt = "T,o(b)";
4802       coproc = 1;
4803       goto ldd_std;
4804
4805     case M_LD_AB:
4806       if (mips_isa >= 3)
4807         {
4808           s = "ld";
4809           goto ld;
4810         }
4811
4812       s = "lw";
4813       fmt = "t,o(b)";
4814       goto ldd_std;
4815
4816     case M_SD_AB:
4817       if (mips_isa >= 3)
4818         {
4819           s = "sd";
4820           goto st;
4821         }
4822
4823       s = "sw";
4824       fmt = "t,o(b)";
4825
4826     ldd_std:
4827       if (offset_expr.X_op != O_symbol
4828           && offset_expr.X_op != O_constant)
4829         {
4830           as_bad ("expression too complex");
4831           offset_expr.X_op = O_constant;
4832         }
4833
4834       /* Even on a big endian machine $fn comes before $fn+1.  We have
4835          to adjust when loading from memory.  We set coproc if we must
4836          load $fn+1 first.  */
4837       if (! target_big_endian)
4838         coproc = 0;
4839
4840       if (mips_pic == NO_PIC
4841           || offset_expr.X_op == O_constant)
4842         {
4843           /* If this is a reference to a GP relative symbol, we want
4844                <op>     $treg,<sym>($gp)        (BFD_RELOC_MIPS_GPREL)
4845                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_MIPS_GPREL)
4846              If we have a base register, we use this
4847                addu     $at,$breg,$gp
4848                <op>     $treg,<sym>($at)        (BFD_RELOC_MIPS_GPREL)
4849                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_MIPS_GPREL)
4850              If this is not a GP relative symbol, we want
4851                lui      $at,<sym>               (BFD_RELOC_HI16_S)
4852                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
4853                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
4854              If there is a base register, we add it to $at after the
4855              lui instruction.  If there is a constant, we always use
4856              the last case.  */
4857           if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
4858               || nopic_need_relax (offset_expr.X_add_symbol))
4859             {
4860               p = NULL;
4861               used_at = 1;
4862             }
4863           else
4864             {
4865               int off;
4866
4867               if (breg == 0)
4868                 {
4869                   frag_grow (28);
4870                   tempreg = GP;
4871                   off = 0;
4872                   used_at = 0;
4873                 }
4874               else
4875                 {
4876                   frag_grow (36);
4877                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4878                                mips_isa < 3 ? "addu" : "daddu",
4879                                "d,v,t", AT, breg, GP);
4880                   tempreg = AT;
4881                   off = 4;
4882                   used_at = 1;
4883                 }
4884
4885               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4886                            coproc ? treg + 1 : treg,
4887                            (int) BFD_RELOC_MIPS_GPREL, tempreg);
4888               offset_expr.X_add_number += 4;
4889
4890               /* Set mips_optimize to 2 to avoid inserting an
4891                  undesired nop.  */
4892               hold_mips_optimize = mips_optimize;
4893               mips_optimize = 2;
4894               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4895                            coproc ? treg : treg + 1,
4896                            (int) BFD_RELOC_MIPS_GPREL, tempreg);
4897               mips_optimize = hold_mips_optimize;
4898
4899               p = frag_var (rs_machine_dependent, 12 + off, 0,
4900                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
4901                                           used_at && mips_noat),
4902                             offset_expr.X_add_symbol, (long) 0,
4903                             (char *) NULL);
4904
4905               /* We just generated two relocs.  When tc_gen_reloc
4906                  handles this case, it will skip the first reloc and
4907                  handle the second.  The second reloc already has an
4908                  extra addend of 4, which we added above.  We must
4909                  subtract it out, and then subtract another 4 to make
4910                  the first reloc come out right.  The second reloc
4911                  will come out right because we are going to add 4 to
4912                  offset_expr when we build its instruction below.  */
4913               offset_expr.X_add_number -= 8;
4914               offset_expr.X_op = O_constant;
4915             }
4916           macro_build_lui (p, &icnt, &offset_expr, AT);
4917           if (p != NULL)
4918             p += 4;
4919           if (breg != 0)
4920             {
4921               macro_build (p, &icnt, (expressionS *) NULL,
4922                            mips_isa < 3 ? "addu" : "daddu",
4923                            "d,v,t", AT, breg, AT);
4924               if (p != NULL)
4925                 p += 4;
4926             }
4927           macro_build (p, &icnt, &offset_expr, s, fmt,
4928                        coproc ? treg + 1 : treg,
4929                        (int) BFD_RELOC_LO16, AT);
4930           if (p != NULL)
4931             p += 4;
4932           /* FIXME: How do we handle overflow here?  */
4933           offset_expr.X_add_number += 4;
4934           macro_build (p, &icnt, &offset_expr, s, fmt,
4935                        coproc ? treg : treg + 1,
4936                        (int) BFD_RELOC_LO16, AT);
4937         }         
4938       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4939         {
4940           int off;
4941
4942           /* If this is a reference to an external symbol, we want
4943                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
4944                nop
4945                <op>     $treg,0($at)
4946                <op>     $treg+1,4($at)
4947              Otherwise we want
4948                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
4949                nop
4950                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
4951                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
4952              If there is a base register we add it to $at before the
4953              lwc1 instructions.  If there is a constant we include it
4954              in the lwc1 instructions.  */
4955           used_at = 1;
4956           expr1.X_add_number = offset_expr.X_add_number;
4957           offset_expr.X_add_number = 0;
4958           if (expr1.X_add_number < -0x8000
4959               || expr1.X_add_number >= 0x8000 - 4)
4960             as_bad ("PIC code offset overflow (max 16 signed bits)");
4961           if (breg == 0)
4962             off = 0;
4963           else
4964             off = 4;
4965           frag_grow (24 + off);
4966           macro_build ((char *) NULL, &icnt, &offset_expr,
4967                        mips_isa < 3 ? "lw" : "ld",
4968                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
4969           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
4970           if (breg != 0)
4971             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4972                          mips_isa < 3 ? "addu" : "daddu",
4973                          "d,v,t", AT, breg, AT);
4974           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
4975                        coproc ? treg + 1 : treg,
4976                        (int) BFD_RELOC_LO16, AT);
4977           expr1.X_add_number += 4;
4978
4979           /* Set mips_optimize to 2 to avoid inserting an undesired
4980              nop.  */
4981           hold_mips_optimize = mips_optimize;
4982           mips_optimize = 2;
4983           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
4984                        coproc ? treg : treg + 1,
4985                        (int) BFD_RELOC_LO16, AT);
4986           mips_optimize = hold_mips_optimize;
4987
4988           (void) frag_var (rs_machine_dependent, 0, 0,
4989                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
4990                            offset_expr.X_add_symbol, (long) 0,
4991                            (char *) NULL);
4992         }
4993       else if (mips_pic == SVR4_PIC)
4994         {
4995           int gpdel, off;
4996
4997           /* If this is a reference to an external symbol, we want
4998                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
4999                addu     $at,$at,$gp
5000                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
5001                nop
5002                <op>     $treg,0($at)
5003                <op>     $treg+1,4($at)
5004              Otherwise we want
5005                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5006                nop
5007                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
5008                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
5009              If there is a base register we add it to $at before the
5010              lwc1 instructions.  If there is a constant we include it
5011              in the lwc1 instructions.  */
5012           used_at = 1;
5013           expr1.X_add_number = offset_expr.X_add_number;
5014           offset_expr.X_add_number = 0;
5015           if (expr1.X_add_number < -0x8000
5016               || expr1.X_add_number >= 0x8000 - 4)
5017             as_bad ("PIC code offset overflow (max 16 signed bits)");
5018           if (reg_needs_delay (GP))
5019             gpdel = 4;
5020           else
5021             gpdel = 0;
5022           if (breg == 0)
5023             off = 0;
5024           else
5025             off = 4;
5026           frag_grow (56);
5027           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5028                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
5029           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5030                        mips_isa < 3 ? "addu" : "daddu",
5031                        "d,v,t", AT, AT, GP);
5032           macro_build ((char *) NULL, &icnt, &offset_expr,
5033                        mips_isa < 3 ? "lw" : "ld",
5034                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
5035           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5036           if (breg != 0)
5037             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5038                          mips_isa < 3 ? "addu" : "daddu",
5039                          "d,v,t", AT, breg, AT);
5040           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5041                        coproc ? treg + 1 : treg,
5042                        (int) BFD_RELOC_LO16, AT);
5043           expr1.X_add_number += 4;
5044
5045           /* Set mips_optimize to 2 to avoid inserting an undesired
5046              nop.  */
5047           hold_mips_optimize = mips_optimize;
5048           mips_optimize = 2;
5049           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5050                        coproc ? treg : treg + 1,
5051                        (int) BFD_RELOC_LO16, AT);
5052           mips_optimize = hold_mips_optimize;
5053           expr1.X_add_number -= 4;
5054
5055           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
5056                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
5057                                       8 + gpdel + off, 1, 0),
5058                         offset_expr.X_add_symbol, (long) 0,
5059                         (char *) NULL);
5060           if (gpdel > 0)
5061             {
5062               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5063               p += 4;
5064             }
5065           macro_build (p, &icnt, &offset_expr,
5066                        mips_isa < 3 ? "lw" : "ld",
5067                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5068           p += 4;
5069           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5070           p += 4;
5071           if (breg != 0)
5072             {
5073               macro_build (p, &icnt, (expressionS *) NULL,
5074                            mips_isa < 3 ? "addu" : "daddu",
5075                            "d,v,t", AT, breg, AT);
5076               p += 4;
5077             }
5078           macro_build (p, &icnt, &expr1, s, fmt,
5079                        coproc ? treg + 1 : treg,
5080                        (int) BFD_RELOC_LO16, AT);
5081           p += 4;
5082           expr1.X_add_number += 4;
5083
5084           /* Set mips_optimize to 2 to avoid inserting an undesired
5085              nop.  */
5086           hold_mips_optimize = mips_optimize;
5087           mips_optimize = 2;
5088           macro_build (p, &icnt, &expr1, s, fmt,
5089                        coproc ? treg : treg + 1,
5090                        (int) BFD_RELOC_LO16, AT);
5091           mips_optimize = hold_mips_optimize;
5092         }
5093       else if (mips_pic == EMBEDDED_PIC)
5094         {
5095           /* If there is no base register, we use
5096                <op>     $treg,<sym>($gp)        (BFD_RELOC_MIPS_GPREL)
5097                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_MIPS_GPREL)
5098              If we have a base register, we use
5099                addu     $at,$breg,$gp
5100                <op>     $treg,<sym>($at)        (BFD_RELOC_MIPS_GPREL)
5101                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_MIPS_GPREL)
5102              */
5103           if (breg == 0)
5104             {
5105               tempreg = GP;
5106               used_at = 0;
5107             }
5108           else
5109             {
5110               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5111                            mips_isa < 3 ? "addu" : "daddu",
5112                            "d,v,t", AT, breg, GP);
5113               tempreg = AT;
5114               used_at = 1;
5115             }
5116
5117           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5118                        coproc ? treg + 1 : treg,
5119                        (int) BFD_RELOC_MIPS_GPREL, tempreg);
5120           offset_expr.X_add_number += 4;
5121           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5122                        coproc ? treg : treg + 1,
5123                        (int) BFD_RELOC_MIPS_GPREL, tempreg);
5124         }
5125       else
5126         abort ();
5127
5128       if (! used_at)
5129         return;
5130
5131       break;
5132
5133     case M_LD_OB:
5134       s = "lw";
5135       goto sd_ob;
5136     case M_SD_OB:
5137       s = "sw";
5138     sd_ob:
5139       assert (mips_isa < 3);
5140       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
5141                    (int) BFD_RELOC_LO16, breg);
5142       offset_expr.X_add_number += 4;
5143       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
5144                    (int) BFD_RELOC_LO16, breg);
5145       return;
5146 #ifdef LOSING_COMPILER
5147     default:
5148       macro2 (ip);
5149       return;
5150     }
5151   if (mips_noat)
5152     as_warn ("Macro used $at after \".set noat\"");
5153 }
5154           
5155 static void
5156 macro2 (ip)
5157      struct mips_cl_insn *ip;
5158 {
5159   register int treg, sreg, dreg, breg;
5160   int tempreg;
5161   int mask;
5162   int icnt = 0;
5163   int used_at;
5164   expressionS expr1;
5165   const char *s;
5166   const char *s2;
5167   const char *fmt;
5168   int likely = 0;
5169   int dbl = 0;
5170   int coproc = 0;
5171   int lr = 0;
5172   int off;
5173   offsetT maxnum;
5174   bfd_reloc_code_real_type r;
5175   char *p;
5176           
5177   treg = (ip->insn_opcode >> 16) & 0x1f;
5178   dreg = (ip->insn_opcode >> 11) & 0x1f;
5179   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
5180   mask = ip->insn_mo->mask;
5181           
5182   expr1.X_op = O_constant;
5183   expr1.X_op_symbol = NULL;
5184   expr1.X_add_symbol = NULL;
5185   expr1.X_add_number = 1;
5186           
5187   switch (mask)
5188     {
5189 #endif /* LOSING_COMPILER */
5190
5191     case M_DMUL:
5192       dbl = 1;
5193     case M_MUL:
5194       macro_build ((char *) NULL, &icnt, NULL,
5195                    dbl ? "dmultu" : "multu",
5196                    "s,t", sreg, treg);
5197       macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5198       return;
5199
5200     case M_DMUL_I:
5201       dbl = 1;
5202     case M_MUL_I:
5203       /* The MIPS assembler some times generates shifts and adds.  I'm
5204          not trying to be that fancy. GCC should do this for us
5205          anyway.  */
5206       load_register (&icnt, AT, &imm_expr, dbl);
5207       macro_build ((char *) NULL, &icnt, NULL,
5208                    dbl ? "dmult" : "mult",
5209                    "s,t", sreg, AT);
5210       macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5211       break;
5212
5213     case M_DMULO:
5214       dbl = 1;
5215     case M_MULO:
5216       mips_emit_delays (true);
5217       ++mips_noreorder;
5218       mips_any_noreorder = 1;
5219       macro_build ((char *) NULL, &icnt, NULL,
5220                    dbl ? "dmult" : "mult",
5221                    "s,t", sreg, treg);
5222       macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5223       macro_build ((char *) NULL, &icnt, NULL,
5224                    dbl ? "dsra32" : "sra",
5225                    "d,w,<", dreg, dreg, 31);
5226       macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
5227       if (mips_trap)
5228         macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", dreg, AT);
5229       else
5230         {
5231           expr1.X_add_number = 8;
5232           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
5233           macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
5234           macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
5235         }
5236       --mips_noreorder;
5237       macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5238       break;
5239
5240     case M_DMULOU:
5241       dbl = 1;
5242     case M_MULOU:
5243       mips_emit_delays (true);
5244       ++mips_noreorder;
5245       mips_any_noreorder = 1;
5246       macro_build ((char *) NULL, &icnt, NULL,
5247                    dbl ? "dmultu" : "multu",
5248                    "s,t", sreg, treg);
5249       macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
5250       macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5251       if (mips_trap)
5252         macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", AT, 0);
5253       else
5254         {
5255           expr1.X_add_number = 8;
5256           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
5257           macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
5258           macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
5259         }
5260       --mips_noreorder;
5261       break;
5262
5263     case M_ROL:
5264       macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
5265       macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
5266       macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg,
5267                    treg);
5268       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
5269       break;
5270
5271     case M_ROL_I:
5272       macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg,
5273                    (int) (imm_expr.X_add_number & 0x1f));
5274       macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
5275                    (int) ((0 - imm_expr.X_add_number) & 0x1f));
5276       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
5277       break;
5278
5279     case M_ROR:
5280       macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
5281       macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
5282       macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg,
5283                    treg);
5284       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
5285       break;
5286
5287     case M_ROR_I:
5288       macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg,
5289                    (int) (imm_expr.X_add_number & 0x1f));
5290       macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
5291                    (int) ((0 - imm_expr.X_add_number) & 0x1f));
5292       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
5293       break;
5294
5295     case M_S_DOB:
5296       assert (mips_isa < 2);
5297       /* Even on a big endian machine $fn comes before $fn+1.  We have
5298          to adjust when storing to memory.  */
5299       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
5300                    target_big_endian ? treg + 1 : treg,
5301                    (int) BFD_RELOC_LO16, breg);
5302       offset_expr.X_add_number += 4;
5303       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
5304                    target_big_endian ? treg : treg + 1,
5305                    (int) BFD_RELOC_LO16, breg);
5306       return;
5307
5308     case M_SEQ:
5309       if (sreg == 0)
5310         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
5311                      treg, (int) BFD_RELOC_LO16);
5312       else if (treg == 0)
5313         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
5314                      sreg, (int) BFD_RELOC_LO16);
5315       else
5316         {
5317           macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
5318                        sreg, treg);
5319           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
5320                        dreg, (int) BFD_RELOC_LO16);
5321         }
5322       return;
5323
5324     case M_SEQ_I:
5325       if (imm_expr.X_add_number == 0)
5326         {
5327           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
5328                        sreg, (int) BFD_RELOC_LO16);
5329           return;
5330         }
5331       if (sreg == 0)
5332         {
5333           as_warn ("Instruction %s: result is always false",
5334                    ip->insn_mo->name);
5335           macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
5336           return;
5337         }
5338       if (imm_expr.X_add_number >= 0 && imm_expr.X_add_number < 0x10000)
5339         {
5340           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
5341                        sreg, (int) BFD_RELOC_LO16);
5342           used_at = 0;
5343         }
5344       else if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number < 0)
5345         {
5346           imm_expr.X_add_number = -imm_expr.X_add_number;
5347           macro_build ((char *) NULL, &icnt, &imm_expr,
5348                        mips_isa < 3 ? "addiu" : "daddiu",
5349                        "t,r,j", dreg, sreg,
5350                        (int) BFD_RELOC_LO16);
5351           used_at = 0;
5352         }
5353       else
5354         {
5355           load_register (&icnt, AT, &imm_expr, 0);
5356           macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
5357                        sreg, AT);
5358           used_at = 1;
5359         }
5360       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
5361                    (int) BFD_RELOC_LO16);
5362       if (used_at)
5363         break;
5364       return;
5365
5366     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
5367       s = "slt";
5368       goto sge;
5369     case M_SGEU:
5370       s = "sltu";
5371     sge:
5372       macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
5373       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
5374                    (int) BFD_RELOC_LO16);
5375       return;
5376
5377     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
5378     case M_SGEU_I:
5379       if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
5380         {
5381           macro_build ((char *) NULL, &icnt, &expr1,
5382                        mask == M_SGE_I ? "slti" : "sltiu",
5383                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
5384           used_at = 0;
5385         }
5386       else
5387         {
5388           load_register (&icnt, AT, &imm_expr, 0);
5389           macro_build ((char *) NULL, &icnt, NULL,
5390                        mask == M_SGE_I ? "slt" : "sltu",
5391                        "d,v,t", dreg, sreg, AT);
5392           used_at = 1;
5393         }
5394       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
5395                    (int) BFD_RELOC_LO16);
5396       if (used_at)
5397         break;
5398       return;
5399
5400     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
5401       s = "slt";
5402       goto sgt;
5403     case M_SGTU:
5404       s = "sltu";
5405     sgt:
5406       macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
5407       return;
5408
5409     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
5410       s = "slt";
5411       goto sgti;
5412     case M_SGTU_I:
5413       s = "sltu";
5414     sgti:
5415       load_register (&icnt, AT, &imm_expr, 0);
5416       macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
5417       break;
5418
5419     case M_SLE:         /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
5420       s = "slt";
5421       goto sle;
5422     case M_SLEU:
5423       s = "sltu";
5424     sle:
5425       macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
5426       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
5427                    (int) BFD_RELOC_LO16);
5428       return;
5429
5430     case M_SLE_I:               /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
5431       s = "slt";
5432       goto slei;
5433     case M_SLEU_I:
5434       s = "sltu";
5435     slei:
5436       load_register (&icnt, AT, &imm_expr, 0);
5437       macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
5438       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
5439                    (int) BFD_RELOC_LO16);
5440       break;
5441
5442     case M_SLT_I:
5443       if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
5444         {
5445           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
5446                        dreg, sreg, (int) BFD_RELOC_LO16);
5447           return;
5448         }
5449       load_register (&icnt, AT, &imm_expr, 0);
5450       macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
5451       break;
5452
5453     case M_SLTU_I:
5454       if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
5455         {
5456           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
5457                        dreg, sreg, (int) BFD_RELOC_LO16);
5458           return;
5459         }
5460       load_register (&icnt, AT, &imm_expr, 0);
5461       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg,
5462                    AT);
5463       break;
5464
5465     case M_SNE:
5466       if (sreg == 0)
5467         macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
5468                      treg);
5469       else if (treg == 0)
5470         macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
5471                      sreg);
5472       else
5473         {
5474           macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
5475                        sreg, treg);
5476           macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
5477                        dreg);
5478         }
5479       return;
5480
5481     case M_SNE_I:
5482       if (imm_expr.X_add_number == 0)
5483         {
5484           macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
5485                        sreg);
5486           return;
5487         }
5488       if (sreg == 0)
5489         {
5490           as_warn ("Instruction %s: result is always true",
5491                    ip->insn_mo->name);
5492           macro_build ((char *) NULL, &icnt, &expr1,
5493                        mips_isa < 3 ? "addiu" : "daddiu",
5494                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
5495           return;
5496         }
5497       if (imm_expr.X_add_number >= 0 && imm_expr.X_add_number < 0x10000)
5498         {
5499           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
5500                        dreg, sreg, (int) BFD_RELOC_LO16);
5501           used_at = 0;
5502         }
5503       else if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number < 0)
5504         {
5505           imm_expr.X_add_number = -imm_expr.X_add_number;
5506           macro_build ((char *) NULL, &icnt, &imm_expr,
5507                        mips_isa < 3 ? "addiu" : "daddiu",
5508                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
5509           used_at = 0;
5510         }
5511       else
5512         {
5513           load_register (&icnt, AT, &imm_expr, 0);
5514           macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
5515                        sreg, AT);
5516           used_at = 1;
5517         }
5518       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
5519       if (used_at)
5520         break;
5521       return;
5522
5523     case M_DSUB_I:
5524       dbl = 1;
5525     case M_SUB_I:
5526       if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number <= 0x8000)
5527         {
5528           imm_expr.X_add_number = -imm_expr.X_add_number;
5529           macro_build ((char *) NULL, &icnt, &imm_expr,
5530                        dbl ? "daddi" : "addi",
5531                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
5532           return;
5533         }
5534       load_register (&icnt, AT, &imm_expr, dbl);
5535       macro_build ((char *) NULL, &icnt, NULL,
5536                    dbl ? "dsub" : "sub",
5537                    "d,v,t", dreg, sreg, AT);
5538       break;
5539
5540     case M_DSUBU_I:
5541       dbl = 1;
5542     case M_SUBU_I:
5543       if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number <= 0x8000)
5544         {
5545           imm_expr.X_add_number = -imm_expr.X_add_number;
5546           macro_build ((char *) NULL, &icnt, &imm_expr,
5547                        dbl ? "daddiu" : "addiu",
5548                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
5549           return;
5550         }
5551       load_register (&icnt, AT, &imm_expr, dbl);
5552       macro_build ((char *) NULL, &icnt, NULL,
5553                    dbl ? "dsubu" : "subu",
5554                    "d,v,t", dreg, sreg, AT);
5555       break;
5556
5557     case M_TEQ_I:
5558       s = "teq";
5559       goto trap;
5560     case M_TGE_I:
5561       s = "tge";
5562       goto trap;
5563     case M_TGEU_I:
5564       s = "tgeu";
5565       goto trap;
5566     case M_TLT_I:
5567       s = "tlt";
5568       goto trap;
5569     case M_TLTU_I:
5570       s = "tltu";
5571       goto trap;
5572     case M_TNE_I:
5573       s = "tne";
5574     trap:
5575       load_register (&icnt, AT, &imm_expr, 0);
5576       macro_build ((char *) NULL, &icnt, NULL, s, "s,t", sreg, AT);
5577       break;
5578
5579     case M_TRUNCWD:
5580     case M_TRUNCWS:
5581       assert (mips_isa < 2);
5582       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
5583       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
5584
5585       /*
5586        * Is the double cfc1 instruction a bug in the mips assembler;
5587        * or is there a reason for it?
5588        */
5589       mips_emit_delays (true);
5590       ++mips_noreorder;
5591       mips_any_noreorder = 1;
5592       macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
5593       macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
5594       macro_build ((char *) NULL, &icnt, NULL, "nop", "");
5595       expr1.X_add_number = 3;
5596       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
5597                    (int) BFD_RELOC_LO16);
5598       expr1.X_add_number = 2;
5599       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
5600                      (int) BFD_RELOC_LO16);
5601       macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", AT, 31);
5602       macro_build ((char *) NULL, &icnt, NULL, "nop", "");
5603       macro_build ((char *) NULL, &icnt, NULL,
5604               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
5605       macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", treg, 31);
5606       macro_build ((char *) NULL, &icnt, NULL, "nop", "");
5607       --mips_noreorder;
5608       break;
5609
5610     case M_ULH:
5611       s = "lb";
5612       goto ulh;
5613     case M_ULHU:
5614       s = "lbu";
5615     ulh:
5616       if (offset_expr.X_add_number >= 0x7fff)
5617         as_bad ("operand overflow");
5618       /* avoid load delay */
5619       if (! target_big_endian)
5620         offset_expr.X_add_number += 1;
5621       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
5622                    (int) BFD_RELOC_LO16, breg);
5623       if (! target_big_endian)
5624         offset_expr.X_add_number -= 1;
5625       else
5626         offset_expr.X_add_number += 1;
5627       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
5628                    (int) BFD_RELOC_LO16, breg);
5629       macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
5630       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
5631       break;
5632
5633     case M_ULD:
5634       s = "ldl";
5635       s2 = "ldr";
5636       off = 7;
5637       goto ulw;
5638     case M_ULW:
5639       s = "lwl";
5640       s2 = "lwr";
5641       off = 3;
5642     ulw:
5643       if (offset_expr.X_add_number >= 0x8000 - off)
5644         as_bad ("operand overflow");
5645       if (! target_big_endian)
5646         offset_expr.X_add_number += off;
5647       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
5648                    (int) BFD_RELOC_LO16, breg);
5649       if (! target_big_endian)
5650         offset_expr.X_add_number -= off;
5651       else
5652         offset_expr.X_add_number += off;
5653       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
5654                    (int) BFD_RELOC_LO16, breg);
5655       return;
5656
5657     case M_ULD_A:
5658       s = "ldl";
5659       s2 = "ldr";
5660       off = 7;
5661       goto ulwa;
5662     case M_ULW_A:
5663       s = "lwl";
5664       s2 = "lwr";
5665       off = 3;
5666     ulwa:
5667       load_address (&icnt, AT, &offset_expr);
5668       if (breg != 0)
5669         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5670                      mips_isa < 3 ? "addu" : "daddu",
5671                      "d,v,t", AT, AT, breg);
5672       if (! target_big_endian)
5673         expr1.X_add_number = off;
5674       else
5675         expr1.X_add_number = 0;
5676       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
5677                    (int) BFD_RELOC_LO16, AT);
5678       if (! target_big_endian)
5679         expr1.X_add_number = 0;
5680       else
5681         expr1.X_add_number = off;
5682       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
5683                    (int) BFD_RELOC_LO16, AT);
5684       break;
5685
5686     case M_ULH_A:
5687     case M_ULHU_A:
5688       load_address (&icnt, AT, &offset_expr);
5689       if (breg != 0)
5690         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5691                      mips_isa < 3 ? "addu" : "daddu",
5692                      "d,v,t", AT, AT, breg);
5693       if (target_big_endian)
5694         expr1.X_add_number = 0;
5695       macro_build ((char *) NULL, &icnt, &expr1,
5696                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
5697                    (int) BFD_RELOC_LO16, AT);
5698       if (target_big_endian)
5699         expr1.X_add_number = 1;
5700       else
5701         expr1.X_add_number = 0;
5702       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
5703                    (int) BFD_RELOC_LO16, AT);
5704       macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
5705                    treg, 8);
5706       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
5707                    treg, AT);
5708       break;
5709
5710     case M_USH:
5711       if (offset_expr.X_add_number >= 0x7fff)
5712         as_bad ("operand overflow");
5713       if (target_big_endian)
5714         offset_expr.X_add_number += 1;
5715       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
5716                    (int) BFD_RELOC_LO16, breg);
5717       macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
5718       if (target_big_endian)
5719         offset_expr.X_add_number -= 1;
5720       else
5721         offset_expr.X_add_number += 1;
5722       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
5723                    (int) BFD_RELOC_LO16, breg);
5724       break;
5725
5726     case M_USD:
5727       s = "sdl";
5728       s2 = "sdr";
5729       off = 7;
5730       goto usw;
5731     case M_USW:
5732       s = "swl";
5733       s2 = "swr";
5734       off = 3;
5735     usw:
5736       if (offset_expr.X_add_number >= 0x8000 - off)
5737         as_bad ("operand overflow");
5738       if (! target_big_endian)
5739         offset_expr.X_add_number += off;
5740       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
5741                    (int) BFD_RELOC_LO16, breg);
5742       if (! target_big_endian)
5743         offset_expr.X_add_number -= off;
5744       else
5745         offset_expr.X_add_number += off;
5746       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
5747                    (int) BFD_RELOC_LO16, breg);
5748       return;
5749
5750     case M_USD_A:
5751       s = "sdl";
5752       s2 = "sdr";
5753       off = 7;
5754       goto uswa;
5755     case M_USW_A:
5756       s = "swl";
5757       s2 = "swr";
5758       off = 3;
5759     uswa:
5760       load_address (&icnt, AT, &offset_expr);
5761       if (breg != 0)
5762         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5763                      mips_isa < 3 ? "addu" : "daddu",
5764                      "d,v,t", AT, AT, breg);
5765       if (! target_big_endian)
5766         expr1.X_add_number = off;
5767       else
5768         expr1.X_add_number = 0;
5769       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
5770                    (int) BFD_RELOC_LO16, AT);
5771       if (! target_big_endian)
5772         expr1.X_add_number = 0;
5773       else
5774         expr1.X_add_number = off;
5775       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
5776                    (int) BFD_RELOC_LO16, AT);
5777       break;
5778
5779     case M_USH_A:
5780       load_address (&icnt, AT, &offset_expr);
5781       if (breg != 0)
5782         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5783                      mips_isa < 3 ? "addu" : "daddu",
5784                      "d,v,t", AT, AT, breg);
5785       if (! target_big_endian)
5786         expr1.X_add_number = 0;
5787       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
5788                    (int) BFD_RELOC_LO16, AT);
5789       macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", treg,
5790                    treg, 8);
5791       if (! target_big_endian)
5792         expr1.X_add_number = 1;
5793       else
5794         expr1.X_add_number = 0;
5795       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
5796                    (int) BFD_RELOC_LO16, AT);
5797       if (! target_big_endian)
5798         expr1.X_add_number = 0;
5799       else
5800         expr1.X_add_number = 1;
5801       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
5802                    (int) BFD_RELOC_LO16, AT);
5803       macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
5804                    treg, 8);
5805       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
5806                    treg, AT);
5807       break;
5808
5809     default:
5810       as_bad ("Macro %s not implemented yet", ip->insn_mo->name);
5811       break;
5812     }
5813   if (mips_noat)
5814     as_warn ("Macro used $at after \".set noat\"");
5815 }
5816
5817 /* Implement macros in mips16 mode.  */
5818
5819 static void
5820 mips16_macro (ip)
5821      struct mips_cl_insn *ip;
5822 {
5823   int mask;
5824   int xreg, yreg, zreg, tmp;
5825   int icnt;
5826   expressionS expr1;
5827   int dbl;
5828   const char *s, *s2, *s3;
5829
5830   mask = ip->insn_mo->mask;
5831
5832   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
5833   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
5834   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
5835
5836   icnt = 0;
5837
5838   expr1.X_op = O_constant;
5839   expr1.X_op_symbol = NULL;
5840   expr1.X_add_symbol = NULL;
5841   expr1.X_add_number = 1;
5842
5843   dbl = 0;
5844
5845   switch (mask)
5846     {
5847     default:
5848       internalError ();
5849
5850     case M_DDIV_3:
5851       dbl = 1;
5852     case M_DIV_3:
5853       s = "mflo";
5854       goto do_div3;
5855     case M_DREM_3:
5856       dbl = 1;
5857     case M_REM_3:
5858       s = "mfhi";
5859     do_div3:
5860       mips_emit_delays (true);
5861       ++mips_noreorder;
5862       mips_any_noreorder = 1;
5863       macro_build ((char *) NULL, &icnt, NULL,
5864                    dbl ? "ddiv" : "div",
5865                    "0,x,y", xreg, yreg);
5866       expr1.X_add_number = 2;
5867       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
5868       macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
5869       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
5870          since that causes an overflow.  We should do that as well,
5871          but I don't see how to do the comparisons without a temporary
5872          register.  */
5873       --mips_noreorder;
5874       macro_build ((char *) NULL, &icnt, NULL, s, "x", zreg);
5875       break;
5876
5877     case M_DIVU_3:
5878       s = "divu";
5879       s2 = "mflo";
5880       goto do_divu3;
5881     case M_REMU_3:
5882       s = "divu";
5883       s2 = "mfhi";
5884       goto do_divu3;
5885     case M_DDIVU_3:
5886       s = "ddivu";
5887       s2 = "mflo";
5888       goto do_divu3;
5889     case M_DREMU_3:
5890       s = "ddivu";
5891       s2 = "mfhi";
5892     do_divu3:
5893       mips_emit_delays (true);
5894       ++mips_noreorder;
5895       mips_any_noreorder = 1;
5896       macro_build ((char *) NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
5897       expr1.X_add_number = 2;
5898       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
5899       macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
5900       --mips_noreorder;
5901       macro_build ((char *) NULL, &icnt, NULL, s2, "x", zreg);
5902       break;
5903
5904     case M_DSUBU_I:
5905       dbl = 1;
5906       goto do_subu;
5907     case M_SUBU_I:
5908     do_subu:
5909       imm_expr.X_add_number = -imm_expr.X_add_number;
5910       macro_build ((char *) NULL, &icnt, &imm_expr,
5911                    dbl ? "daddiu" : "addiu",
5912                    "y,x,4", yreg, xreg);
5913       break;
5914
5915     case M_SUBU_I_2:
5916       imm_expr.X_add_number = -imm_expr.X_add_number;
5917       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
5918                    "x,k", xreg);
5919       break;
5920
5921     case M_DSUBU_I_2:
5922       imm_expr.X_add_number = -imm_expr.X_add_number;
5923       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
5924                    "y,j", yreg);
5925       break;
5926
5927     case M_BEQ:
5928       s = "cmp";
5929       s2 = "bteqz";
5930       goto do_branch;
5931     case M_BNE:
5932       s = "cmp";
5933       s2 = "btnez";
5934       goto do_branch;
5935     case M_BLT:
5936       s = "slt";
5937       s2 = "btnez";
5938       goto do_branch;
5939     case M_BLTU:
5940       s = "sltu";
5941       s2 = "btnez";
5942       goto do_branch;
5943     case M_BLE:
5944       s = "slt";
5945       s2 = "bteqz";
5946       goto do_reverse_branch;
5947     case M_BLEU:
5948       s = "sltu";
5949       s2 = "bteqz";
5950       goto do_reverse_branch;
5951     case M_BGE:
5952       s = "slt";
5953       s2 = "bteqz";
5954       goto do_branch;
5955     case M_BGEU:
5956       s = "sltu";
5957       s2 = "bteqz";
5958       goto do_branch;
5959     case M_BGT:
5960       s = "slt";
5961       s2 = "btnez";
5962       goto do_reverse_branch;
5963     case M_BGTU:
5964       s = "sltu";
5965       s2 = "btnez";
5966
5967     do_reverse_branch:
5968       tmp = xreg;
5969       xreg = yreg;
5970       yreg = tmp;
5971
5972     do_branch:
5973       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
5974                    xreg, yreg);
5975       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
5976       break;
5977
5978     case M_BEQ_I:
5979       s = "cmpi";
5980       s2 = "bteqz";
5981       s3 = "x,U";
5982       goto do_branch_i;
5983     case M_BNE_I:
5984       s = "cmpi";
5985       s2 = "btnez";
5986       s3 = "x,U";
5987       goto do_branch_i;
5988     case M_BLT_I:
5989       s = "slti";
5990       s2 = "btnez";
5991       s3 = "x,8";
5992       goto do_branch_i;
5993     case M_BLTU_I:
5994       s = "sltiu";
5995       s2 = "btnez";
5996       s3 = "x,8";
5997       goto do_branch_i;
5998     case M_BLE_I:
5999       s = "slti";
6000       s2 = "btnez";
6001       s3 = "x,8";
6002       goto do_addone_branch_i;
6003     case M_BLEU_I:
6004       s = "sltiu";
6005       s2 = "btnez";
6006       s3 = "x,8";
6007       goto do_addone_branch_i;
6008     case M_BGE_I:
6009       s = "slti";
6010       s2 = "bteqz";
6011       s3 = "x,8";
6012       goto do_branch_i;
6013     case M_BGEU_I:
6014       s = "sltiu";
6015       s2 = "bteqz";
6016       s3 = "x,8";
6017       goto do_branch_i;
6018     case M_BGT_I:
6019       s = "slti";
6020       s2 = "bteqz";
6021       s3 = "x,8";
6022       goto do_addone_branch_i;
6023     case M_BGTU_I:
6024       s = "sltiu";
6025       s2 = "bteqz";
6026       s3 = "x,8";
6027
6028     do_addone_branch_i:
6029       ++imm_expr.X_add_number;
6030
6031     do_branch_i:
6032       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
6033       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
6034       break;
6035
6036     case M_ABS:
6037       expr1.X_add_number = 0;
6038       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8",  yreg);
6039       if (xreg != yreg)
6040         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6041                      "move", "y,X", xreg, yreg);
6042       expr1.X_add_number = 2;
6043       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
6044       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6045                    "neg", "x,w", xreg, xreg);
6046     }
6047 }
6048
6049 /* This routine assembles an instruction into its binary format.  As a
6050    side effect, it sets one of the global variables imm_reloc or
6051    offset_reloc to the type of relocation to do if one of the operands
6052    is an address expression.  */
6053
6054 static void
6055 mips_ip (str, ip)
6056      char *str;
6057      struct mips_cl_insn *ip;
6058 {
6059   char *s;
6060   const char *args;
6061   char c;
6062   struct mips_opcode *insn;
6063   char *argsStart;
6064   unsigned int regno;
6065   unsigned int lastregno = 0;
6066   char *s_reset;
6067
6068   insn_error = NULL;
6069
6070   for (s = str; islower (*s) || (*s >= '0' && *s <= '3') || *s == '6' || *s == '.'; ++s)
6071     continue;
6072   switch (*s)
6073     {
6074     case '\0':
6075       break;
6076
6077     case ' ':
6078       *s++ = '\0';
6079       break;
6080
6081     default:
6082       as_fatal ("Unknown opcode: `%s'", str);
6083     }
6084   if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
6085     {
6086       insn_error = "unrecognized opcode";
6087       return;
6088     }
6089   argsStart = s;
6090   for (;;)
6091     {
6092       int insn_isa;
6093
6094       assert (strcmp (insn->name, str) == 0);
6095
6096       if (insn->pinfo == INSN_MACRO)
6097         insn_isa = insn->match;
6098       else if ((insn->pinfo & INSN_ISA) == INSN_ISA2)
6099         insn_isa = 2;
6100       else if ((insn->pinfo & INSN_ISA) == INSN_ISA3)
6101         insn_isa = 3;
6102       else if ((insn->pinfo & INSN_ISA) == INSN_ISA4)
6103         insn_isa = 4;
6104       else
6105         insn_isa = 1;
6106
6107       if (insn_isa > mips_isa
6108           || ((insn->pinfo & INSN_ISA) == INSN_4650
6109               && ! mips_4650)
6110           || ((insn->pinfo & INSN_ISA) == INSN_4010
6111               && ! mips_4010)
6112           || ((insn->pinfo & INSN_ISA) == INSN_4100
6113               && ! mips_4100))
6114         {
6115           if (insn + 1 < &mips_opcodes[NUMOPCODES]
6116               && strcmp (insn->name, insn[1].name) == 0)
6117             {
6118               ++insn;
6119               continue;
6120             }
6121           if (insn_isa <= mips_isa)
6122             insn_error = "opcode not supported on this processor";
6123           else
6124             {
6125               static char buf[100];
6126
6127               sprintf (buf, "opcode requires -mips%d or greater", insn_isa);
6128               insn_error = buf;
6129             }
6130           return;
6131         }
6132
6133       ip->insn_mo = insn;
6134       ip->insn_opcode = insn->match;
6135       for (args = insn->args;; ++args)
6136         {
6137           if (*s == ' ')
6138             ++s;
6139           switch (*args)
6140             {
6141             case '\0':          /* end of args */
6142               if (*s == '\0')
6143                 return;
6144               break;
6145
6146             case ',':
6147               if (*s++ == *args)
6148                 continue;
6149               s--;
6150               switch (*++args)
6151                 {
6152                 case 'r':
6153                 case 'v':
6154                   ip->insn_opcode |= lastregno << 21;
6155                   continue;
6156
6157                 case 'w':
6158                 case 'W':
6159                   ip->insn_opcode |= lastregno << 16;
6160                   continue;
6161
6162                 case 'V':
6163                   ip->insn_opcode |= lastregno << 11;
6164                   continue;
6165                 }
6166               break;
6167
6168             case '(':
6169               /* handle optional base register.
6170                  Either the base register is omitted or
6171                  we must have a left paren. */
6172               /* this is dependent on the next operand specifier
6173                  is a 'b' for base register */
6174               assert (args[1] == 'b');
6175               if (*s == '\0')
6176                 return;
6177
6178             case ')':           /* these must match exactly */
6179               if (*s++ == *args)
6180                 continue;
6181               break;
6182
6183             case '<':           /* must be at least one digit */
6184               /*
6185                * According to the manual, if the shift amount is greater
6186                * than 31 or less than 0 the the shift amount should be
6187                * mod 32. In reality the mips assembler issues an error.
6188                * We issue a warning and mask out all but the low 5 bits.
6189                */
6190               my_getExpression (&imm_expr, s);
6191               check_absolute_expr (ip, &imm_expr);
6192               if ((unsigned long) imm_expr.X_add_number > 31)
6193                 {
6194                   as_warn ("Improper shift amount (%ld)",
6195                            (long) imm_expr.X_add_number);
6196                   imm_expr.X_add_number = imm_expr.X_add_number & 0x1f;
6197                 }
6198               ip->insn_opcode |= imm_expr.X_add_number << 6;
6199               imm_expr.X_op = O_absent;
6200               s = expr_end;
6201               continue;
6202
6203             case '>':           /* shift amount minus 32 */
6204               my_getExpression (&imm_expr, s);
6205               check_absolute_expr (ip, &imm_expr);
6206               if ((unsigned long) imm_expr.X_add_number < 32
6207                   || (unsigned long) imm_expr.X_add_number > 63)
6208                 break;
6209               ip->insn_opcode |= (imm_expr.X_add_number - 32) << 6;
6210               imm_expr.X_op = O_absent;
6211               s = expr_end;
6212               continue;
6213
6214             case 'k':           /* cache code */
6215             case 'h':           /* prefx code */
6216               my_getExpression (&imm_expr, s);
6217               check_absolute_expr (ip, &imm_expr);
6218               if ((unsigned long) imm_expr.X_add_number > 31)
6219                 {
6220                   as_warn ("Invalid value for `%s' (%lu)",
6221                            ip->insn_mo->name,
6222                            (unsigned long) imm_expr.X_add_number);
6223                   imm_expr.X_add_number &= 0x1f;
6224                 }
6225               if (*args == 'k')
6226                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
6227               else
6228                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
6229               imm_expr.X_op = O_absent;
6230               s = expr_end;
6231               continue;
6232
6233             case 'c':           /* break code */
6234               my_getExpression (&imm_expr, s);
6235               check_absolute_expr (ip, &imm_expr);
6236               if ((unsigned) imm_expr.X_add_number > 1023)
6237                 as_warn ("Illegal break code (%ld)",
6238                          (long) imm_expr.X_add_number);
6239               ip->insn_opcode |= imm_expr.X_add_number << 16;
6240               imm_expr.X_op = O_absent;
6241               s = expr_end;
6242               continue;
6243
6244             case 'B':           /* syscall code */
6245               my_getExpression (&imm_expr, s);
6246               check_absolute_expr (ip, &imm_expr);
6247               if ((unsigned) imm_expr.X_add_number > 0xfffff)
6248                 as_warn ("Illegal syscall code (%ld)",
6249                          (long) imm_expr.X_add_number);
6250               ip->insn_opcode |= imm_expr.X_add_number << 6;
6251               imm_expr.X_op = O_absent;
6252               s = expr_end;
6253               continue;
6254
6255             case 'C':           /* Coprocessor code */
6256               my_getExpression (&imm_expr, s);
6257               check_absolute_expr (ip, &imm_expr);
6258               if ((unsigned long) imm_expr.X_add_number >= (1<<25))
6259                 {
6260                   as_warn ("Coproccesor code > 25 bits (%ld)",
6261                            (long) imm_expr.X_add_number);
6262                   imm_expr.X_add_number &= ((1<<25) - 1);
6263                 }
6264               ip->insn_opcode |= imm_expr.X_add_number;
6265               imm_expr.X_op = O_absent;
6266               s = expr_end;
6267               continue;
6268
6269             case 'b':           /* base register */
6270             case 'd':           /* destination register */
6271             case 's':           /* source register */
6272             case 't':           /* target register */
6273             case 'r':           /* both target and source */
6274             case 'v':           /* both dest and source */
6275             case 'w':           /* both dest and target */
6276             case 'E':           /* coprocessor target register */
6277             case 'G':           /* coprocessor destination register */
6278             case 'x':           /* ignore register name */
6279             case 'z':           /* must be zero register */
6280               s_reset = s;
6281               if (s[0] == '$')
6282                 {
6283                   if (isdigit (s[1]))
6284                     {
6285                       ++s;
6286                       regno = 0;
6287                       do
6288                         {
6289                           regno *= 10;
6290                           regno += *s - '0';
6291                           ++s;
6292                         }
6293                       while (isdigit (*s));
6294                       if (regno > 31)
6295                         as_bad ("Invalid register number (%d)", regno);
6296                     }
6297                   else if (*args == 'E' || *args == 'G')
6298                     goto notreg;
6299                   else
6300                     {
6301                       if (s[1] == 'f' && s[2] == 'p')
6302                         {
6303                           s += 3;
6304                           regno = FP;
6305                         }
6306                       else if (s[1] == 's' && s[2] == 'p')
6307                         {
6308                           s += 3;
6309                           regno = SP;
6310                         }
6311                       else if (s[1] == 'g' && s[2] == 'p')
6312                         {
6313                           s += 3;
6314                           regno = GP;
6315                         }
6316                       else if (s[1] == 'a' && s[2] == 't')
6317                         {
6318                           s += 3;
6319                           regno = AT;
6320                         }
6321                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
6322                         {
6323                           s += 4;
6324                           regno = KT0;
6325                         }
6326                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
6327                         {
6328                           s += 4;
6329                           regno = KT1;
6330                         }
6331                       else
6332                         goto notreg;
6333                     }
6334                   if (regno == AT
6335                       && ! mips_noat
6336                       && *args != 'E'
6337                       && *args != 'G')
6338                     as_warn ("Used $at without \".set noat\"");
6339                   c = *args;
6340                   if (*s == ' ')
6341                     s++;
6342                   if (args[1] != *s)
6343                     {
6344                       if (c == 'r' || c == 'v' || c == 'w')
6345                         {
6346                           regno = lastregno;
6347                           s = s_reset;
6348                           args++;
6349                         }
6350                     }
6351                   /* 'z' only matches $0.  */
6352                   if (c == 'z' && regno != 0)
6353                     break;
6354                   switch (c)
6355                     {
6356                     case 'r':
6357                     case 's':
6358                     case 'v':
6359                     case 'b':
6360                       ip->insn_opcode |= regno << 21;
6361                       break;
6362                     case 'd':
6363                     case 'G':
6364                       ip->insn_opcode |= regno << 11;
6365                       break;
6366                     case 'w':
6367                     case 't':
6368                     case 'E':
6369                       ip->insn_opcode |= regno << 16;
6370                       break;
6371                     case 'x':
6372                       /* This case exists because on the r3000 trunc
6373                          expands into a macro which requires a gp
6374                          register.  On the r6000 or r4000 it is
6375                          assembled into a single instruction which
6376                          ignores the register.  Thus the insn version
6377                          is MIPS_ISA2 and uses 'x', and the macro
6378                          version is MIPS_ISA1 and uses 't'.  */
6379                       break;
6380                     case 'z':
6381                       /* This case is for the div instruction, which
6382                          acts differently if the destination argument
6383                          is $0.  This only matches $0, and is checked
6384                          outside the switch.  */
6385                       break;
6386                     }
6387                   lastregno = regno;
6388                   continue;
6389                 }
6390             notreg:
6391               switch (*args++)
6392                 {
6393                 case 'r':
6394                 case 'v':
6395                   ip->insn_opcode |= lastregno << 21;
6396                   continue;
6397                 case 'w':
6398                   ip->insn_opcode |= lastregno << 16;
6399                   continue;
6400                 }
6401               break;
6402
6403             case 'D':           /* floating point destination register */
6404             case 'S':           /* floating point source register */
6405             case 'T':           /* floating point target register */
6406             case 'R':           /* floating point source register */
6407             case 'V':
6408             case 'W':
6409               s_reset = s;
6410               if (s[0] == '$' && s[1] == 'f' && isdigit (s[2]))
6411                 {
6412                   s += 2;
6413                   regno = 0;
6414                   do
6415                     {
6416                       regno *= 10;
6417                       regno += *s - '0';
6418                       ++s;
6419                     }
6420                   while (isdigit (*s));
6421
6422                   if (regno > 31)
6423                     as_bad ("Invalid float register number (%d)", regno);
6424
6425                   if ((regno & 1) != 0
6426                       && mips_isa < 3
6427                       && ! (strcmp (str, "mtc1") == 0
6428                             || strcmp (str, "mfc1") == 0
6429                             || strcmp (str, "lwc1") == 0
6430                             || strcmp (str, "swc1") == 0
6431                             || strcmp (str, "l.s") == 0
6432                             || strcmp (str, "s.s") == 0))
6433                     as_warn ("Float register should be even, was %d",
6434                              regno);
6435
6436                   c = *args;
6437                   if (*s == ' ')
6438                     s++;
6439                   if (args[1] != *s)
6440                     {
6441                       if (c == 'V' || c == 'W')
6442                         {
6443                           regno = lastregno;
6444                           s = s_reset;
6445                           args++;
6446                         }
6447                     }
6448                   switch (c)
6449                     {
6450                     case 'D':
6451                       ip->insn_opcode |= regno << 6;
6452                       break;
6453                     case 'V':
6454                     case 'S':
6455                       ip->insn_opcode |= regno << 11;
6456                       break;
6457                     case 'W':
6458                     case 'T':
6459                       ip->insn_opcode |= regno << 16;
6460                       break;
6461                     case 'R':
6462                       ip->insn_opcode |= regno << 21;
6463                       break;
6464                     }
6465                   lastregno = regno;
6466                   continue;
6467                 }
6468               switch (*args++)
6469                 {
6470                 case 'V':
6471                   ip->insn_opcode |= lastregno << 11;
6472                   continue;
6473                 case 'W':
6474                   ip->insn_opcode |= lastregno << 16;
6475                   continue;
6476                 }
6477               break;
6478
6479             case 'I':
6480               my_getExpression (&imm_expr, s);
6481               if (imm_expr.X_op != O_big
6482                   && imm_expr.X_op != O_constant)
6483                 insn_error = "absolute expression required";
6484               s = expr_end;
6485               continue;
6486
6487             case 'A':
6488               my_getExpression (&offset_expr, s);
6489               imm_reloc = BFD_RELOC_32;
6490               s = expr_end;
6491               continue;
6492
6493             case 'F':
6494             case 'L':
6495             case 'f':
6496             case 'l':
6497               {
6498                 int f64;
6499                 char *save_in;
6500                 char *err;
6501                 unsigned char temp[8];
6502                 int len;
6503                 unsigned int length;
6504                 segT seg;
6505                 subsegT subseg;
6506                 char *p;
6507
6508                 /* These only appear as the last operand in an
6509                    instruction, and every instruction that accepts
6510                    them in any variant accepts them in all variants.
6511                    This means we don't have to worry about backing out
6512                    any changes if the instruction does not match.
6513
6514                    The difference between them is the size of the
6515                    floating point constant and where it goes.  For 'F'
6516                    and 'L' the constant is 64 bits; for 'f' and 'l' it
6517                    is 32 bits.  Where the constant is placed is based
6518                    on how the MIPS assembler does things:
6519                     F -- .rdata
6520                     L -- .lit8
6521                     f -- immediate value
6522                     l -- .lit4
6523
6524                     The .lit4 and .lit8 sections are only used if
6525                     permitted by the -G argument.
6526
6527                     When generating embedded PIC code, we use the
6528                     .lit8 section but not the .lit4 section (we can do
6529                     .lit4 inline easily; we need to put .lit8
6530                     somewhere in the data segment, and using .lit8
6531                     permits the linker to eventually combine identical
6532                     .lit8 entries).  */
6533
6534                 f64 = *args == 'F' || *args == 'L';
6535
6536                 save_in = input_line_pointer;
6537                 input_line_pointer = s;
6538                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
6539                 length = len;
6540                 s = input_line_pointer;
6541                 input_line_pointer = save_in;
6542                 if (err != NULL && *err != '\0')
6543                   {
6544                     as_bad ("Bad floating point constant: %s", err);
6545                     memset (temp, '\0', sizeof temp);
6546                     length = f64 ? 8 : 4;
6547                   }
6548
6549                 assert (length == (f64 ? 8 : 4));
6550
6551                 if (*args == 'f'
6552                     || (*args == 'l'
6553                         && (! USE_GLOBAL_POINTER_OPT
6554                             || mips_pic == EMBEDDED_PIC
6555                             || g_switch_value < 4)
6556                         ))
6557                   {
6558                     imm_expr.X_op = O_constant;
6559                     if (! target_big_endian)
6560                       imm_expr.X_add_number =
6561                         (((((((int) temp[3] << 8)
6562                              | temp[2]) << 8)
6563                            | temp[1]) << 8)
6564                          | temp[0]);
6565                     else
6566                       imm_expr.X_add_number =
6567                         (((((((int) temp[0] << 8)
6568                              | temp[1]) << 8)
6569                            | temp[2]) << 8)
6570                          | temp[3]);
6571                   }
6572                 else
6573                   {
6574                     const char *newname;
6575                     segT new_seg;
6576
6577                     /* Switch to the right section.  */
6578                     seg = now_seg;
6579                     subseg = now_subseg;
6580                     switch (*args)
6581                       {
6582                       default: /* unused default case avoids warnings.  */
6583                       case 'L':
6584                         newname = RDATA_SECTION_NAME;
6585                         if (USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
6586                           newname = ".lit8";
6587                         break;
6588                       case 'F':
6589                         newname = RDATA_SECTION_NAME;
6590                         break;
6591                       case 'l':
6592                         assert (!USE_GLOBAL_POINTER_OPT
6593                                 || g_switch_value >= 4);
6594                         newname = ".lit4";
6595                         break;
6596                       }
6597                     new_seg = subseg_new (newname, (subsegT) 0);
6598                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
6599                       bfd_set_section_flags (stdoutput, new_seg,
6600                                              (SEC_ALLOC
6601                                               | SEC_LOAD
6602                                               | SEC_READONLY
6603                                               | SEC_DATA));
6604                     frag_align (*args == 'l' ? 2 : 3, 0);
6605                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
6606                       record_alignment (new_seg, 4);
6607                     else
6608                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
6609                     if (seg == now_seg)
6610                       as_bad ("Can't use floating point insn in this section");
6611
6612                     /* Set the argument to the current address in the
6613                        section.  */
6614                     offset_expr.X_op = O_symbol;
6615                     offset_expr.X_add_symbol =
6616                       symbol_new ("L0\001", now_seg,
6617                                   (valueT) frag_now_fix (), frag_now);
6618                     offset_expr.X_add_number = 0;
6619
6620                     /* Put the floating point number into the section.  */
6621                     p = frag_more ((int) length);
6622                     memcpy (p, temp, length);
6623
6624                     /* Switch back to the original section.  */
6625                     subseg_set (seg, subseg);
6626                   }
6627               }
6628               continue;
6629
6630             case 'i':           /* 16 bit unsigned immediate */
6631             case 'j':           /* 16 bit signed immediate */
6632               imm_reloc = BFD_RELOC_LO16;
6633               c = my_getSmallExpression (&imm_expr, s);
6634               if (c != '\0')
6635                 {
6636                   if (c != 'l')
6637                     {
6638                       if (imm_expr.X_op == O_constant)
6639                         imm_expr.X_add_number =
6640                           (imm_expr.X_add_number >> 16) & 0xffff;
6641                       else if (c == 'h')
6642                         {
6643                           imm_reloc = BFD_RELOC_HI16_S;
6644                           imm_unmatched_hi = true;
6645                         }
6646                       else
6647                         imm_reloc = BFD_RELOC_HI16;
6648                     }
6649                 }
6650               if (*args == 'i')
6651                 {
6652                   if ((c == '\0' && imm_expr.X_op != O_constant)
6653                       || ((imm_expr.X_add_number < 0
6654                            || imm_expr.X_add_number >= 0x10000)
6655                           && imm_expr.X_op == O_constant))
6656                     {
6657                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
6658                           !strcmp (insn->name, insn[1].name))
6659                         break;
6660                       if (imm_expr.X_op != O_constant
6661                           && imm_expr.X_op != O_big)
6662                         insn_error = "absolute expression required";
6663                       else
6664                         as_bad ("16 bit expression not in range 0..65535");
6665                     }
6666                 }
6667               else
6668                 {
6669                   int more;
6670                   offsetT max;
6671
6672                   /* The upper bound should be 0x8000, but
6673                      unfortunately the MIPS assembler accepts numbers
6674                      from 0x8000 to 0xffff and sign extends them, and
6675                      we want to be compatible.  We only permit this
6676                      extended range for an instruction which does not
6677                      provide any further alternates, since those
6678                      alternates may handle other cases.  People should
6679                      use the numbers they mean, rather than relying on
6680                      a mysterious sign extension.  */
6681                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
6682                           strcmp (insn->name, insn[1].name) == 0);
6683                   if (more)
6684                     max = 0x8000;
6685                   else
6686                     max = 0x10000;
6687                   if ((c == '\0' && imm_expr.X_op != O_constant)
6688                       || ((imm_expr.X_add_number < -0x8000
6689                            || imm_expr.X_add_number >= max)
6690                           && imm_expr.X_op == O_constant)
6691                       || (more
6692                           && imm_expr.X_add_number < 0
6693                           && mips_isa >= 3
6694                           && imm_expr.X_unsigned
6695                           && sizeof (imm_expr.X_add_number) <= 4))
6696                     {
6697                       if (more)
6698                         break;
6699                       if (imm_expr.X_op != O_constant
6700                           && imm_expr.X_op != O_big)
6701                         insn_error = "absolute expression required";
6702                       else
6703                         as_bad ("16 bit expression not in range -32768..32767");
6704                     }
6705                 }
6706               s = expr_end;
6707               continue;
6708
6709             case 'o':           /* 16 bit offset */
6710               c = my_getSmallExpression (&offset_expr, s);
6711
6712               /* If this value won't fit into a 16 bit offset, then go
6713                  find a macro that will generate the 32 bit offset
6714                  code pattern.  As a special hack, we accept the
6715                  difference of two local symbols as a constant.  This
6716                  is required to suppose embedded PIC switches, which
6717                  use an instruction which looks like
6718                      lw $4,$L12-$LS12($4)
6719                  The problem with handling this in a more general
6720                  fashion is that the macro function doesn't expect to
6721                  see anything which can be handled in a single
6722                  constant instruction.  */
6723               if (c == 0
6724                   && (offset_expr.X_op != O_constant
6725                       || offset_expr.X_add_number >= 0x8000
6726                       || offset_expr.X_add_number < -0x8000)
6727                   && (mips_pic != EMBEDDED_PIC
6728                       || offset_expr.X_op != O_subtract
6729                       || now_seg != text_section
6730                       || (S_GET_SEGMENT (offset_expr.X_op_symbol)
6731                           != text_section)))
6732                 break;
6733
6734               offset_reloc = BFD_RELOC_LO16;
6735               if (c == 'h' || c == 'H')
6736                 {
6737                   assert (offset_expr.X_op == O_constant);
6738                   offset_expr.X_add_number =
6739                     (offset_expr.X_add_number >> 16) & 0xffff;
6740                 }
6741               s = expr_end;
6742               continue;
6743
6744             case 'p':           /* pc relative offset */
6745               offset_reloc = BFD_RELOC_16_PCREL_S2;
6746               my_getExpression (&offset_expr, s);
6747               s = expr_end;
6748               continue;
6749
6750             case 'u':           /* upper 16 bits */
6751               c = my_getSmallExpression (&imm_expr, s);
6752               if (imm_expr.X_op == O_constant
6753                   && (imm_expr.X_add_number < 0
6754                       || imm_expr.X_add_number >= 0x10000))
6755                 as_bad ("lui expression not in range 0..65535");
6756               imm_reloc = BFD_RELOC_LO16;
6757               if (c)
6758                 {
6759                   if (c != 'l')
6760                     {
6761                       if (imm_expr.X_op == O_constant)
6762                         imm_expr.X_add_number =
6763                           (imm_expr.X_add_number >> 16) & 0xffff;
6764                       else if (c == 'h')
6765                         {
6766                           imm_reloc = BFD_RELOC_HI16_S;
6767                           imm_unmatched_hi = true;
6768                         }
6769                       else
6770                         imm_reloc = BFD_RELOC_HI16;
6771                     }
6772                 }
6773               s = expr_end;
6774               continue;
6775
6776             case 'a':           /* 26 bit address */
6777               my_getExpression (&offset_expr, s);
6778               s = expr_end;
6779               offset_reloc = BFD_RELOC_MIPS_JMP;
6780               continue;
6781
6782             case 'N':           /* 3 bit branch condition code */
6783             case 'M':           /* 3 bit compare condition code */
6784               if (strncmp (s, "$fcc", 4) != 0)
6785                 break;
6786               s += 4;
6787               regno = 0;
6788               do
6789                 {
6790                   regno *= 10;
6791                   regno += *s - '0';
6792                   ++s;
6793                 }
6794               while (isdigit (*s));
6795               if (regno > 7)
6796                 as_bad ("invalid condition code register $fcc%d", regno);
6797               if (*args == 'N')
6798                 ip->insn_opcode |= regno << OP_SH_BCC;
6799               else
6800                 ip->insn_opcode |= regno << OP_SH_CCC;
6801               continue;
6802
6803             default:
6804               fprintf (stderr, "bad char = '%c'\n", *args);
6805               internalError ();
6806             }
6807           break;
6808         }
6809       /* Args don't match.  */
6810       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
6811           !strcmp (insn->name, insn[1].name))
6812         {
6813           ++insn;
6814           s = argsStart;
6815           continue;
6816         }
6817       insn_error = "illegal operands";
6818       return;
6819     }
6820 }
6821
6822 /* This routine assembles an instruction into its binary format when
6823    assembling for the mips16.  As a side effect, it sets one of the
6824    global variables imm_reloc or offset_reloc to the type of
6825    relocation to do if one of the operands is an address expression.
6826    It also sets mips16_small and mips16_ext if the user explicitly
6827    requested a small or extended instruction.  */
6828
6829 static void
6830 mips16_ip (str, ip)
6831      char *str;
6832      struct mips_cl_insn *ip;
6833 {
6834   char *s;
6835   const char *args;
6836   struct mips_opcode *insn;
6837   char *argsstart;
6838   unsigned int regno;
6839   unsigned int lastregno = 0;
6840   char *s_reset;
6841
6842   insn_error = NULL;
6843
6844   mips16_small = false;
6845   mips16_ext = false;
6846
6847   for (s = str; islower (*s); ++s)
6848     ;
6849   switch (*s)
6850     {
6851     case '\0':
6852       break;
6853
6854     case ' ':
6855       *s++ = '\0';
6856       break;
6857
6858     case '.':
6859       if (s[1] == 't' && s[2] == ' ')
6860         {
6861           *s = '\0';
6862           mips16_small = true;
6863           s += 3;
6864           break;
6865         }
6866       else if (s[1] == 'e' && s[2] == ' ')
6867         {
6868           *s = '\0';
6869           mips16_ext = true;
6870           s += 3;
6871           break;
6872         }
6873       /* Fall through.  */
6874     default:
6875       insn_error = "unknown opcode";
6876       return;
6877     }
6878
6879   if (! mips16_autoextend && ! mips16_ext)
6880     mips16_small = true;
6881
6882   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
6883     {
6884       insn_error = "unrecognized opcode";
6885       return;
6886     }
6887
6888   argsstart = s;
6889   for (;;)
6890     {
6891       assert (strcmp (insn->name, str) == 0);
6892
6893       ip->insn_mo = insn;
6894       ip->insn_opcode = insn->match;
6895       ip->use_extend = false;
6896       imm_expr.X_op = O_absent;
6897       imm_reloc = BFD_RELOC_UNUSED;
6898       offset_expr.X_op = O_absent;
6899       offset_reloc = BFD_RELOC_UNUSED;
6900       for (args = insn->args; 1; ++args)
6901         {
6902           int c;
6903
6904           if (*s == ' ')
6905             ++s;
6906
6907           /* In this switch statement we call break if we did not find
6908              a match, continue if we did find a match, or return if we
6909              are done.  */
6910
6911           c = *args;
6912           switch (c)
6913             {
6914             case '\0':
6915               if (*s == '\0')
6916                 {
6917                   /* Stuff the immediate value in now, if we can.  */
6918                   if (imm_expr.X_op == O_constant
6919                       && imm_reloc > BFD_RELOC_UNUSED
6920                       && insn->pinfo != INSN_MACRO)
6921                     {
6922                       mips16_immed ((char *) NULL, 0,
6923                                     imm_reloc - BFD_RELOC_UNUSED,
6924                                     imm_expr.X_add_number, true, mips16_small,
6925                                     mips16_ext, &ip->insn_opcode,
6926                                     &ip->use_extend, &ip->extend);
6927                       imm_expr.X_op = O_absent;
6928                       imm_reloc = BFD_RELOC_UNUSED;
6929                     }
6930
6931                   return;
6932                 }
6933               break;
6934
6935             case ',':
6936               if (*s++ == c)
6937                 continue;
6938               s--;
6939               switch (*++args)
6940                 {
6941                 case 'v':
6942                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
6943                   continue;
6944                 case 'w':
6945                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
6946                   continue;
6947                 }
6948               break;
6949
6950             case '(':
6951             case ')':
6952               if (*s++ == c)
6953                 continue;
6954               break;
6955
6956             case 'v':
6957             case 'w':
6958               if (s[0] != '$')
6959                 {
6960                   if (c == 'v')
6961                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
6962                   else
6963                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
6964                   ++args;
6965                   continue;
6966                 }
6967               /* Fall through.  */
6968             case 'x':
6969             case 'y':
6970             case 'z':
6971             case 'Z':
6972             case '0':
6973             case 'S':
6974             case 'R':
6975             case 'X':
6976             case 'Y':
6977               if (s[0] != '$')
6978                 break;
6979               s_reset = s;
6980               if (isdigit (s[1]))
6981                 {
6982                   ++s;
6983                   regno = 0;
6984                   do
6985                     {
6986                       regno *= 10;
6987                       regno += *s - '0';
6988                       ++s;
6989                     }
6990                   while (isdigit (*s));
6991                   if (regno > 31)
6992                     {
6993                       as_bad ("invalid register number (%d)", regno);
6994                       regno = 2;
6995                     }
6996                 }
6997               else
6998                 {
6999                   if (s[1] == 'f' && s[2] == 'p')
7000                     {
7001                       s += 3;
7002                       regno = FP;
7003                     }
7004                   else if (s[1] == 's' && s[2] == 'p')
7005                     {
7006                       s += 3;
7007                       regno = SP;
7008                     }
7009                   else if (s[1] == 'g' && s[2] == 'p')
7010                     {
7011                       s += 3;
7012                       regno = GP;
7013                     }
7014                   else if (s[1] == 'a' && s[2] == 't')
7015                     {
7016                       s += 3;
7017                       regno = AT;
7018                     }
7019                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7020                     {
7021                       s += 4;
7022                       regno = KT0;
7023                     }
7024                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7025                     {
7026                       s += 4;
7027                       regno = KT1;
7028                     }
7029                   else
7030                     break;
7031                 }
7032
7033               if (*s == ' ')
7034                 ++s;
7035               if (args[1] != *s)
7036                 {
7037                   if (c == 'v' || c == 'w')
7038                     {
7039                       regno = mips16_to_32_reg_map[lastregno];
7040                       s = s_reset;
7041                       args++;
7042                     }
7043                 }
7044
7045               switch (c)
7046                 {
7047                 case 'x':
7048                 case 'y':
7049                 case 'z':
7050                 case 'v':
7051                 case 'w':
7052                 case 'Z':
7053                   regno = mips32_to_16_reg_map[regno];
7054                   break;
7055
7056                 case '0':
7057                   if (regno != 0)
7058                     regno = ILLEGAL_REG;
7059                   break;
7060
7061                 case 'S':
7062                   if (regno != SP)
7063                     regno = ILLEGAL_REG;
7064                   break;
7065
7066                 case 'R':
7067                   if (regno != RA)
7068                     regno = ILLEGAL_REG;
7069                   break;
7070
7071                 case 'X':
7072                 case 'Y':
7073                   if (regno == AT && ! mips_noat)
7074                     as_warn ("used $at without \".set noat\"");
7075                   break;
7076
7077                 default:
7078                   internalError ();
7079                 }
7080
7081               if (regno == ILLEGAL_REG)
7082                 break;
7083
7084               switch (c)
7085                 {
7086                 case 'x':
7087                 case 'v':
7088                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
7089                   break;
7090                 case 'y':
7091                 case 'w':
7092                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
7093                   break;
7094                 case 'z':
7095                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
7096                   break;
7097                 case 'Z':
7098                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
7099                 case '0':
7100                 case 'S':
7101                 case 'R':
7102                   break;
7103                 case 'X':
7104                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
7105                   break;
7106                 case 'Y':
7107                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
7108                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
7109                   break;
7110                 default:
7111                   internalError ();
7112                 }
7113
7114               lastregno = regno;
7115               continue;
7116
7117             case 'P':
7118               if (strncmp (s, "$pc", 3) == 0)
7119                 {
7120                   s += 3;
7121                   continue;
7122                 }
7123               break;
7124
7125             case '<':
7126             case '>':
7127             case '[':
7128             case ']':
7129             case '4':
7130             case '5':
7131             case 'H':
7132             case 'W':
7133             case 'D':
7134             case 'j':
7135             case '8':
7136             case 'V':
7137             case 'C':
7138             case 'U':
7139             case 'k':
7140             case 'K':
7141               if (s[0] == '$' && isdigit (s[1]))
7142                 {
7143                   /* Looks like a register name.  */
7144                   break;
7145                 }
7146
7147               if (s[0] == '('
7148                   && args[1] == '('
7149                   && s[1] == '$')
7150                 {
7151                   /* It looks like the expression was omitted before a
7152                      register indirection, which means that the
7153                      expression is implicitly zero.  We still set up
7154                      imm_expr, so that we handle explicit extensions
7155                      correctly.  */
7156                   imm_expr.X_op = O_constant;
7157                   imm_expr.X_add_number = 0;
7158                   imm_reloc = (int) BFD_RELOC_UNUSED + c;
7159                   continue;
7160                 }
7161
7162               my_getExpression (&imm_expr, s);
7163               /* We need to relax this instruction.  */
7164               imm_reloc = (int) BFD_RELOC_UNUSED + c;
7165               s = expr_end;
7166               continue;
7167
7168             case 'p':
7169             case 'q':
7170             case 'A':
7171             case 'B':
7172             case 'E':
7173               /* We use offset_reloc rather than imm_reloc for the PC
7174                  relative operands.  This lets macros with both
7175                  immediate and address operands work correctly.  */
7176               if (s[0] == '$' && isdigit (s[1]))
7177                 {
7178                   /* Looks like a register name.  */
7179                   break;
7180                 }
7181               my_getExpression (&offset_expr, s);
7182               /* We need to relax this instruction.  */
7183               offset_reloc = (int) BFD_RELOC_UNUSED + c;
7184               s = expr_end;
7185               continue;
7186
7187             case '6':           /* break code */
7188               my_getExpression (&imm_expr, s);
7189               check_absolute_expr (ip, &imm_expr);
7190               if ((unsigned long) imm_expr.X_add_number > 63)
7191                 {
7192                   as_warn ("Invalid value for `%s' (%lu)",
7193                            ip->insn_mo->name,
7194                            (unsigned long) imm_expr.X_add_number);
7195                   imm_expr.X_add_number &= 0x3f;
7196                 }
7197               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
7198               imm_expr.X_op = O_absent;
7199               s = expr_end;
7200               continue;
7201
7202             case 'a':           /* 26 bit address */
7203               my_getExpression (&offset_expr, s);
7204               s = expr_end;
7205               offset_reloc = BFD_RELOC_MIPS16_JMP;
7206               ip->insn_opcode <<= 16;
7207               continue;
7208
7209             case 'l':           /* register list for entry macro */
7210             case 'L':           /* register list for exit macro */
7211               {
7212                 int mask;
7213
7214                 if (c == 'l')
7215                   mask = 0;
7216                 else
7217                   mask = 7 << 3;
7218                 while (*s != '\0')
7219                   {
7220                     int reg1, reg2;
7221
7222                     while (*s == ' ' || *s == ',')
7223                       ++s;
7224                     if (*s != '$')
7225                       {
7226                         as_bad ("can't parse register list");
7227                         break;
7228                       }
7229                     ++s;
7230                     reg1 = 0;
7231                     while (isdigit (*s))
7232                       {
7233                         reg1 *= 10;
7234                         reg1 += *s - '0';
7235                         ++s;
7236                       }
7237                     if (*s == ' ')
7238                       ++s;
7239                     if (*s != '-')
7240                       reg2 = reg1;
7241                     else
7242                       {
7243                         ++s;
7244                         if (*s != '$')
7245                           break;
7246                         ++s;
7247                         reg2 = 0;
7248                         while (isdigit (*s))
7249                           {
7250                             reg2 *= 10;
7251                             reg2 += *s - '0';
7252                             ++s;
7253                           }
7254                       }
7255                     if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
7256                       mask |= (reg2 - 3) << 3;
7257                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
7258                       mask |= (reg2 - 15) << 1;
7259                     else if (reg1 == 31 && reg2 == 31)
7260                       mask |= 1;
7261                     else
7262                       as_bad ("invalid register list");
7263                   }
7264                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
7265               }
7266             continue;
7267
7268             default:
7269               internalError ();
7270             }
7271           break;
7272         }
7273
7274       /* Args don't match.  */
7275       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
7276           strcmp (insn->name, insn[1].name) == 0)
7277         {
7278           ++insn;
7279           s = argsstart;
7280           continue;
7281         }
7282
7283       insn_error = "illegal operands";
7284
7285       return;
7286     }
7287 }
7288
7289 /* This structure holds information we know about a mips16 immediate
7290    argument type.  */
7291
7292 struct mips16_immed_operand
7293 {
7294   /* The type code used in the argument string in the opcode table.  */
7295   int type;
7296   /* The number of bits in the short form of the opcode.  */
7297   int nbits;
7298   /* The number of bits in the extended form of the opcode.  */
7299   int extbits;
7300   /* The amount by which the short form is shifted when it is used;
7301      for example, the sw instruction has a shift count of 2.  */
7302   int shift;
7303   /* The amount by which the short form is shifted when it is stored
7304      into the instruction code.  */
7305   int op_shift;
7306   /* Non-zero if the short form is unsigned.  */
7307   int unsp;
7308   /* Non-zero if the extended form is unsigned.  */
7309   int extu;
7310   /* Non-zero if the value is PC relative.  */
7311   int pcrel;
7312 };
7313
7314 /* The mips16 immediate operand types.  */
7315
7316 static const struct mips16_immed_operand mips16_immed_operands[] =
7317 {
7318   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
7319   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
7320   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
7321   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
7322   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
7323   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
7324   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
7325   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
7326   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
7327   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
7328   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
7329   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
7330   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
7331   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
7332   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
7333   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
7334   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
7335   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
7336   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
7337   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
7338   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
7339 };
7340
7341 #define MIPS16_NUM_IMMED \
7342   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
7343
7344 /* Handle a mips16 instruction with an immediate value.  This or's the
7345    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
7346    whether an extended value is needed; if one is needed, it sets
7347    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
7348    If SMALL is true, an unextended opcode was explicitly requested.
7349    If EXT is true, an extended opcode was explicitly requested.  If
7350    WARN is true, warn if EXT does not match reality.  */
7351
7352 static void
7353 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
7354               extend)
7355      char *file;
7356      unsigned int line;
7357      int type;
7358      offsetT val;
7359      boolean warn;
7360      boolean small;
7361      boolean ext;
7362      unsigned long *insn;
7363      boolean *use_extend;
7364      unsigned short *extend;
7365 {
7366   register const struct mips16_immed_operand *op;
7367   int mintiny, maxtiny;
7368   boolean needext;
7369
7370   op = mips16_immed_operands;
7371   while (op->type != type)
7372     {
7373       ++op;
7374       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
7375     }
7376
7377   if (op->unsp)
7378     {
7379       if (type == '<' || type == '>' || type == '[' || type == ']')
7380         {
7381           mintiny = 1;
7382           maxtiny = 1 << op->nbits;
7383         }
7384       else
7385         {
7386           mintiny = 0;
7387           maxtiny = (1 << op->nbits) - 1;
7388         }
7389     }
7390   else
7391     {
7392       mintiny = - (1 << (op->nbits - 1));
7393       maxtiny = (1 << (op->nbits - 1)) - 1;
7394     }
7395
7396   /* Branch offsets have an implicit 0 in the lowest bit.  */
7397   if (type == 'p' || type == 'q')
7398     val /= 2;
7399
7400   if ((val & ((1 << op->shift) - 1)) != 0
7401       || val < (mintiny << op->shift)
7402       || val > (maxtiny << op->shift))
7403     needext = true;
7404   else
7405     needext = false;
7406
7407   if (warn && ext && ! needext)
7408     as_warn_where (file, line, "extended operand requested but not required");
7409   if (small && needext)
7410     as_bad_where (file, line, "invalid unextended operand value");
7411
7412   if (small || (! ext && ! needext))
7413     {
7414       int insnval;
7415
7416       *use_extend = false;
7417       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
7418       insnval <<= op->op_shift;
7419       *insn |= insnval;
7420     }
7421   else
7422     {
7423       long minext, maxext;
7424       int extval;
7425
7426       if (op->extu)
7427         {
7428           minext = 0;
7429           maxext = (1 << op->extbits) - 1;
7430         }
7431       else
7432         {
7433           minext = - (1 << (op->extbits - 1));
7434           maxext = (1 << (op->extbits - 1)) - 1;
7435         }
7436       if (val < minext || val > maxext)
7437         as_bad_where (file, line,
7438                       "operand value out of range for instruction");
7439
7440       *use_extend = true;
7441       if (op->extbits == 16)
7442         {
7443           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
7444           val &= 0x1f;
7445         }
7446       else if (op->extbits == 15)
7447         {
7448           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
7449           val &= 0xf;
7450         }
7451       else
7452         {
7453           extval = ((val & 0x1f) << 6) | (val & 0x20);
7454           val = 0;
7455         }
7456
7457       *extend = (unsigned short) extval;
7458       *insn |= val;
7459     }
7460 }
7461 \f
7462 #define LP '('
7463 #define RP ')'
7464
7465 static int
7466 my_getSmallExpression (ep, str)
7467      expressionS *ep;
7468      char *str;
7469 {
7470   char *sp;
7471   int c = 0;
7472
7473   if (*str == ' ')
7474     str++;
7475   if (*str == LP
7476       || (*str == '%' &&
7477           ((str[1] == 'h' && str[2] == 'i')
7478            || (str[1] == 'H' && str[2] == 'I')
7479            || (str[1] == 'l' && str[2] == 'o'))
7480           && str[3] == LP))
7481     {
7482       if (*str == LP)
7483         c = 0;
7484       else
7485         {
7486           c = str[1];
7487           str += 3;
7488         }
7489
7490       /*
7491        * A small expression may be followed by a base register.
7492        * Scan to the end of this operand, and then back over a possible
7493        * base register.  Then scan the small expression up to that
7494        * point.  (Based on code in sparc.c...)
7495        */
7496       for (sp = str; *sp && *sp != ','; sp++)
7497         ;
7498       if (sp - 4 >= str && sp[-1] == RP)
7499         {
7500           if (isdigit (sp[-2]))
7501             {
7502               for (sp -= 3; sp >= str && isdigit (*sp); sp--)
7503                 ;
7504               if (*sp == '$' && sp > str && sp[-1] == LP)
7505                 {
7506                   sp--;
7507                   goto do_it;
7508                 }
7509             }
7510           else if (sp - 5 >= str
7511                    && sp[-5] == LP
7512                    && sp[-4] == '$'
7513                    && ((sp[-3] == 'f' && sp[-2] == 'p')
7514                        || (sp[-3] == 's' && sp[-2] == 'p')
7515                        || (sp[-3] == 'g' && sp[-2] == 'p')
7516                        || (sp[-3] == 'a' && sp[-2] == 't')))
7517             {
7518               sp -= 5;
7519             do_it:
7520               if (sp == str)
7521                 {
7522                   /* no expression means zero offset */
7523                   if (c)
7524                     {
7525                       /* %xx(reg) is an error */
7526                       ep->X_op = O_absent;
7527                       expr_end = str - 3;
7528                     }
7529                   else
7530                     {
7531                       ep->X_op = O_constant;
7532                       expr_end = sp;
7533                     }
7534                   ep->X_add_symbol = NULL;
7535                   ep->X_op_symbol = NULL;
7536                   ep->X_add_number = 0;
7537                 }
7538               else
7539                 {
7540                   *sp = '\0';
7541                   my_getExpression (ep, str);
7542                   *sp = LP;
7543                 }
7544               return c;
7545             }
7546         }
7547     }
7548   my_getExpression (ep, str);
7549   return c;                     /* => %hi or %lo encountered */
7550 }
7551
7552 static void
7553 my_getExpression (ep, str)
7554      expressionS *ep;
7555      char *str;
7556 {
7557   char *save_in;
7558
7559   save_in = input_line_pointer;
7560   input_line_pointer = str;
7561   expression (ep);
7562   expr_end = input_line_pointer;
7563   input_line_pointer = save_in;
7564 }
7565
7566 /* Turn a string in input_line_pointer into a floating point constant
7567    of type type, and store the appropriate bytes in *litP.  The number
7568    of LITTLENUMS emitted is stored in *sizeP .  An error message is
7569    returned, or NULL on OK.  */
7570
7571 char *
7572 md_atof (type, litP, sizeP)
7573      int type;
7574      char *litP;
7575      int *sizeP;
7576 {
7577   int prec;
7578   LITTLENUM_TYPE words[4];
7579   char *t;
7580   int i;
7581
7582   switch (type)
7583     {
7584     case 'f':
7585       prec = 2;
7586       break;
7587
7588     case 'd':
7589       prec = 4;
7590       break;
7591
7592     default:
7593       *sizeP = 0;
7594       return "bad call to md_atof";
7595     }
7596
7597   t = atof_ieee (input_line_pointer, type, words);
7598   if (t)
7599     input_line_pointer = t;
7600
7601   *sizeP = prec * 2;
7602
7603   if (! target_big_endian)
7604     {
7605       for (i = prec - 1; i >= 0; i--)
7606         {
7607           md_number_to_chars (litP, (valueT) words[i], 2);
7608           litP += 2;
7609         }
7610     }
7611   else
7612     {
7613       for (i = 0; i < prec; i++)
7614         {
7615           md_number_to_chars (litP, (valueT) words[i], 2);
7616           litP += 2;
7617         }
7618     }
7619      
7620   return NULL;
7621 }
7622
7623 void
7624 md_number_to_chars (buf, val, n)
7625      char *buf;
7626      valueT val;
7627      int n;
7628 {
7629   if (target_big_endian)
7630     number_to_chars_bigendian (buf, val, n);
7631   else
7632     number_to_chars_littleendian (buf, val, n);
7633 }
7634 \f
7635 CONST char *md_shortopts = "O::g::G:";
7636
7637 struct option md_longopts[] = {
7638 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
7639   {"mips0", no_argument, NULL, OPTION_MIPS1},
7640   {"mips1", no_argument, NULL, OPTION_MIPS1},
7641 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
7642   {"mips2", no_argument, NULL, OPTION_MIPS2},
7643 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
7644   {"mips3", no_argument, NULL, OPTION_MIPS3},
7645 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
7646   {"mips4", no_argument, NULL, OPTION_MIPS4},
7647 #define OPTION_MCPU (OPTION_MD_BASE + 5)
7648   {"mcpu", required_argument, NULL, OPTION_MCPU},
7649 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 6)
7650   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
7651 #define OPTION_TRAP (OPTION_MD_BASE + 9)
7652   {"trap", no_argument, NULL, OPTION_TRAP},
7653   {"no-break", no_argument, NULL, OPTION_TRAP},
7654 #define OPTION_BREAK (OPTION_MD_BASE + 10)
7655   {"break", no_argument, NULL, OPTION_BREAK},
7656   {"no-trap", no_argument, NULL, OPTION_BREAK},
7657 #define OPTION_EB (OPTION_MD_BASE + 11)
7658   {"EB", no_argument, NULL, OPTION_EB},
7659 #define OPTION_EL (OPTION_MD_BASE + 12)
7660   {"EL", no_argument, NULL, OPTION_EL},
7661 #define OPTION_M4650 (OPTION_MD_BASE + 13)
7662   {"m4650", no_argument, NULL, OPTION_M4650},
7663 #define OPTION_NO_M4650 (OPTION_MD_BASE + 14)
7664   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
7665 #define OPTION_M4010 (OPTION_MD_BASE + 15)
7666   {"m4010", no_argument, NULL, OPTION_M4010},
7667 #define OPTION_NO_M4010 (OPTION_MD_BASE + 16)
7668   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
7669 #define OPTION_M4100 (OPTION_MD_BASE + 17)
7670   {"m4100", no_argument, NULL, OPTION_M4100},
7671 #define OPTION_NO_M4100 (OPTION_MD_BASE + 18)
7672   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
7673 #define OPTION_MIPS16 (OPTION_MD_BASE + 22)
7674   {"mips16", no_argument, NULL, OPTION_MIPS16},
7675 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 23)
7676   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
7677
7678 #define OPTION_CALL_SHARED (OPTION_MD_BASE + 7)
7679 #define OPTION_NON_SHARED (OPTION_MD_BASE + 8)
7680 #define OPTION_XGOT (OPTION_MD_BASE + 19)
7681 #define OPTION_32 (OPTION_MD_BASE + 20)
7682 #define OPTION_64 (OPTION_MD_BASE + 21)
7683 #ifdef OBJ_ELF
7684   {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
7685   {"xgot", no_argument, NULL, OPTION_XGOT},
7686   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
7687   {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
7688   {"32", no_argument, NULL, OPTION_32},
7689   {"64", no_argument, NULL, OPTION_64},
7690 #endif
7691
7692   {NULL, no_argument, NULL, 0}
7693 };
7694 size_t md_longopts_size = sizeof(md_longopts);
7695
7696 int
7697 md_parse_option (c, arg)
7698      int c;
7699      char *arg;
7700 {
7701   switch (c)
7702     {
7703     case OPTION_TRAP:
7704       mips_trap = 1;
7705       break;
7706
7707     case OPTION_BREAK:
7708       mips_trap = 0;
7709       break;
7710
7711     case OPTION_EB:
7712       target_big_endian = 1;
7713       break;
7714
7715     case OPTION_EL:
7716       target_big_endian = 0;
7717       break;
7718
7719     case 'O':
7720       if (arg && arg[1] == '0')
7721         mips_optimize = 1;
7722       else
7723         mips_optimize = 2;
7724       break;
7725
7726     case 'g':
7727       if (arg == NULL)
7728         mips_debug = 2;
7729       else
7730         mips_debug = atoi (arg);
7731       /* When the MIPS assembler sees -g or -g2, it does not do
7732          optimizations which limit full symbolic debugging.  We take
7733          that to be equivalent to -O0.  */
7734       if (mips_debug == 2)
7735         mips_optimize = 0;
7736       break;
7737
7738     case OPTION_MIPS1:
7739       mips_isa = 1;
7740       if (mips_cpu == -1)
7741         mips_cpu = 3000;
7742       break;
7743
7744     case OPTION_MIPS2:
7745       mips_isa = 2;
7746       if (mips_cpu == -1)
7747         mips_cpu = 6000;
7748       break;
7749
7750     case OPTION_MIPS3:
7751       mips_isa = 3;
7752       if (mips_cpu == -1)
7753         mips_cpu = 4000;
7754       break;
7755
7756     case OPTION_MIPS4:
7757       mips_isa = 4;
7758       if (mips_cpu == -1)
7759         mips_cpu = 8000;
7760       break;
7761
7762     case OPTION_MCPU:
7763       {
7764         char *p;
7765
7766         /* Identify the processor type */
7767         p = arg;
7768         if (strcmp (p, "default") == 0
7769             || strcmp (p, "DEFAULT") == 0)
7770           mips_cpu = -1;
7771         else
7772           {
7773             int sv = 0;
7774
7775             /* We need to cope with the various "vr" prefixes for the 4300
7776                processor.  */
7777             if (*p == 'v' || *p == 'V')
7778               {
7779                 sv = 1;
7780                 p++;
7781               }
7782
7783             if (*p == 'r' || *p == 'R')
7784               p++;
7785
7786             mips_cpu = -1;
7787             switch (*p)
7788               {
7789               case '1':
7790                 if (strcmp (p, "10000") == 0
7791                     || strcmp (p, "10k") == 0
7792                     || strcmp (p, "10K") == 0)
7793                   mips_cpu = 10000;
7794                 break;
7795
7796               case '2':
7797                 if (strcmp (p, "2000") == 0
7798                     || strcmp (p, "2k") == 0
7799                     || strcmp (p, "2K") == 0)
7800                   mips_cpu = 2000;
7801                 break;
7802
7803               case '3':
7804                 if (strcmp (p, "3000") == 0
7805                     || strcmp (p, "3k") == 0
7806                     || strcmp (p, "3K") == 0)
7807                   mips_cpu = 3000;
7808                 break;
7809
7810               case '4':
7811                 if (strcmp (p, "4000") == 0
7812                     || strcmp (p, "4k") == 0
7813                     || strcmp (p, "4K") == 0)
7814                   mips_cpu = 4000;
7815                 else if (strcmp (p, "4100") == 0)
7816                   {
7817                     mips_cpu = 4100;
7818                     if (mips_4100 < 0)
7819                       mips_4100 = 1;
7820                   }
7821                 else if (strcmp (p, "4300") == 0)
7822                   mips_cpu = 4300;
7823                 else if (strcmp (p, "4400") == 0)
7824                   mips_cpu = 4400;
7825                 else if (strcmp (p, "4600") == 0)
7826                   mips_cpu = 4600;
7827                 else if (strcmp (p, "4650") == 0)
7828                   {
7829                     mips_cpu = 4650;
7830                     if (mips_4650 < 0)
7831                       mips_4650 = 1;
7832                   }
7833                 else if (strcmp (p, "4010") == 0)
7834                   {
7835                     mips_cpu = 4010;
7836                     if (mips_4010 < 0)
7837                       mips_4010 = 1;
7838                   }
7839                 break;
7840
7841               case '5':
7842                 if (strcmp (p, "5000") == 0
7843                     || strcmp (p, "5k") == 0
7844                     || strcmp (p, "5K") == 0)
7845                   mips_cpu = 5000;
7846                 break;
7847
7848               case '6':
7849                 if (strcmp (p, "6000") == 0
7850                     || strcmp (p, "6k") == 0
7851                     || strcmp (p, "6K") == 0)
7852                   mips_cpu = 6000;
7853                 break;
7854
7855               case '8':
7856                 if (strcmp (p, "8000") == 0
7857                     || strcmp (p, "8k") == 0
7858                     || strcmp (p, "8K") == 0)
7859                   mips_cpu = 8000;
7860                 break;
7861
7862               case 'o':
7863                 if (strcmp (p, "orion") == 0)
7864                   mips_cpu = 4600;
7865                 break;
7866               }
7867
7868             if (sv && mips_cpu != 4300 && mips_cpu != 4100 && mips_cpu != 5000)
7869               {
7870                 as_bad ("ignoring invalid leading 'v' in -mcpu=%s switch", arg);
7871                 return 0;
7872               }
7873
7874             if (mips_cpu == -1)
7875               {
7876                 as_bad ("invalid architecture -mcpu=%s", arg);
7877                 return 0;
7878               }
7879           }
7880       }
7881       break;
7882
7883     case OPTION_M4650:
7884       mips_4650 = 1;
7885       break;
7886
7887     case OPTION_NO_M4650:
7888       mips_4650 = 0;
7889       break;
7890
7891     case OPTION_M4010:
7892       mips_4010 = 1;
7893       break;
7894
7895     case OPTION_NO_M4010:
7896       mips_4010 = 0;
7897       break;
7898
7899     case OPTION_M4100:
7900       mips_4100 = 1;
7901       break;
7902
7903     case OPTION_NO_M4100:
7904       mips_4100 = 0;
7905       break;
7906
7907     case OPTION_MIPS16:
7908       mips16 = 1;
7909       mips_no_prev_insn ();
7910       break;
7911
7912     case OPTION_NO_MIPS16:
7913       mips16 = 0;
7914       mips_no_prev_insn ();
7915       break;
7916
7917     case OPTION_MEMBEDDED_PIC:
7918       mips_pic = EMBEDDED_PIC;
7919       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
7920         {
7921           as_bad ("-G may not be used with embedded PIC code");
7922           return 0;
7923         }
7924       g_switch_value = 0x7fffffff;
7925       break;
7926
7927       /* When generating ELF code, we permit -KPIC and -call_shared to
7928          select SVR4_PIC, and -non_shared to select no PIC.  This is
7929          intended to be compatible with Irix 5.  */
7930     case OPTION_CALL_SHARED:
7931       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
7932         {
7933           as_bad ("-call_shared is supported only for ELF format");
7934           return 0;
7935         }
7936       mips_pic = SVR4_PIC;
7937       if (g_switch_seen && g_switch_value != 0)
7938         {
7939           as_bad ("-G may not be used with SVR4 PIC code");
7940           return 0;
7941         }
7942       g_switch_value = 0;
7943       break;
7944
7945     case OPTION_NON_SHARED:
7946       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
7947         {
7948           as_bad ("-non_shared is supported only for ELF format");
7949           return 0;
7950         }
7951       mips_pic = NO_PIC;
7952       break;
7953
7954       /* The -xgot option tells the assembler to use 32 offsets when
7955          accessing the got in SVR4_PIC mode.  It is for Irix
7956          compatibility.  */
7957     case OPTION_XGOT:
7958       mips_big_got = 1;
7959       break;
7960
7961     case 'G':
7962       if (! USE_GLOBAL_POINTER_OPT)
7963         {
7964           as_bad ("-G is not supported for this configuration");
7965           return 0;
7966         }
7967       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
7968         {
7969           as_bad ("-G may not be used with SVR4 or embedded PIC code");
7970           return 0;
7971         }
7972       else
7973         g_switch_value = atoi (arg);
7974       g_switch_seen = 1;
7975       break;
7976
7977       /* The -32 and -64 options tell the assembler to output the 32
7978          bit or the 64 bit MIPS ELF format.  */
7979     case OPTION_32:
7980       mips_64 = 0;
7981       break;
7982
7983     case OPTION_64:
7984       {
7985         const char **list, **l;
7986
7987         list = bfd_target_list ();
7988         for (l = list; *l != NULL; l++)
7989           if (strcmp (*l, "elf64-bigmips") == 0
7990               || strcmp (*l, "elf64-littlemips") == 0)
7991             break;
7992         if (*l == NULL)
7993           as_fatal ("No compiled in support for 64 bit object file format");
7994         free (list);
7995         mips_64 = 1;
7996       }
7997       break;
7998
7999     default:
8000       return 0;
8001     }
8002
8003   return 1;
8004 }
8005
8006 void
8007 md_show_usage (stream)
8008      FILE *stream;
8009 {
8010   fprintf(stream, "\
8011 MIPS options:\n\
8012 -membedded-pic          generate embedded position independent code\n\
8013 -EB                     generate big endian output\n\
8014 -EL                     generate little endian output\n\
8015 -g, -g2                 do not remove uneeded NOPs or swap branches\n\
8016 -G NUM                  allow referencing objects up to NUM bytes\n\
8017                         implicitly with the gp register [default 8]\n");
8018   fprintf(stream, "\
8019 -mips1, -mcpu=r{2,3}000 generate code for r2000 and r3000\n\
8020 -mips2, -mcpu=r6000     generate code for r6000\n\
8021 -mips3, -mcpu=r4000     generate code for r4000\n\
8022 -mips4, -mcpu=r8000     generate code for r8000\n\
8023 -mcpu=vr4300            generate code for vr4300\n\
8024 -mcpu=vr4100            generate code for vr4100\n\
8025 -m4650                  permit R4650 instructions\n\
8026 -no-m4650               do not permit R4650 instructions\n\
8027 -m4010                  permit R4010 instructions\n\
8028 -no-m4010               do not permit R4010 instructions\n\
8029 -m4100                  permit VR4100 instructions\n\
8030 -no-m4100               do not permit VR4100 instructions\n");
8031   fprintf(stream, "\
8032 -mips16                 generate mips16 instructions\n\
8033 -no-mips16              do not generate mips16 instructions\n");
8034   fprintf(stream, "\
8035 -O0                     remove unneeded NOPs, do not swap branches\n\
8036 -O                      remove unneeded NOPs and swap branches\n\
8037 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
8038 --break, --no-trap      break exception on div by 0 and mult overflow\n");
8039 #ifdef OBJ_ELF
8040   fprintf(stream, "\
8041 -KPIC, -call_shared     generate SVR4 position independent code\n\
8042 -non_shared             do not generate position independent code\n\
8043 -xgot                   assume a 32 bit GOT\n\
8044 -32                     create 32 bit object file (default)\n\
8045 -64                     create 64 bit object file\n");
8046 #endif
8047 }
8048 \f
8049 long
8050 md_pcrel_from (fixP)
8051      fixS *fixP;
8052 {
8053   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
8054       && fixP->fx_addsy != (symbolS *) NULL
8055       && ! S_IS_DEFINED (fixP->fx_addsy))
8056     {
8057       /* This makes a branch to an undefined symbol be a branch to the
8058          current location.  */
8059       return 4;
8060     }
8061
8062   /* return the address of the delay slot */
8063   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
8064 }
8065
8066 /* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
8067    reloc for a cons.  We could use the definition there, except that
8068    we want to handle 64 bit relocs specially.  */
8069
8070 void
8071 cons_fix_new_mips (frag, where, nbytes, exp)
8072      fragS *frag;
8073      int where;
8074      unsigned int nbytes;
8075      expressionS *exp;
8076 {
8077 #ifndef OBJ_ELF
8078   /* If we are assembling in 32 bit mode, turn an 8 byte reloc into a
8079      4 byte reloc.  */
8080   if (nbytes == 8 && ! mips_64)
8081     {
8082       if (target_big_endian)
8083         where += 4;
8084       nbytes = 4;
8085     }
8086 #endif
8087
8088   if (nbytes != 2 && nbytes != 4 && nbytes != 8)
8089     as_bad ("Unsupported reloc size %d", nbytes);
8090
8091   fix_new_exp (frag_now, where, (int) nbytes, exp, 0,
8092                (nbytes == 2
8093                 ? BFD_RELOC_16
8094                 : (nbytes == 4 ? BFD_RELOC_32 : BFD_RELOC_64)));
8095 }
8096
8097 /* Sort any unmatched HI16_S relocs so that they immediately precede
8098    the corresponding LO reloc.  This is called before md_apply_fix and
8099    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
8100    explicit use of the %hi modifier.  */
8101
8102 void
8103 mips_frob_file ()
8104 {
8105   struct mips_hi_fixup *l;
8106
8107   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
8108     {
8109       segment_info_type *seginfo;
8110       int pass;
8111
8112       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
8113
8114       /* Check quickly whether the next fixup happens to be a matching
8115          %lo.  */
8116       if (l->fixp->fx_next != NULL
8117           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
8118           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
8119           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
8120         continue;
8121
8122       /* Look through the fixups for this segment for a matching %lo.
8123          When we find one, move the %hi just in front of it.  We do
8124          this in two passes.  In the first pass, we try to find a
8125          unique %lo.  In the second pass, we permit multiple %hi
8126          relocs for a single %lo (this is a GNU extension).  */
8127       seginfo = seg_info (l->seg);
8128       for (pass = 0; pass < 2; pass++)
8129         {
8130           fixS *f, *prev;
8131
8132           prev = NULL;
8133           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
8134             {
8135               /* Check whether this is a %lo fixup which matches l->fixp.  */
8136               if (f->fx_r_type == BFD_RELOC_LO16
8137                   && f->fx_addsy == l->fixp->fx_addsy
8138                   && f->fx_offset == l->fixp->fx_offset
8139                   && (pass == 1
8140                       || prev == NULL
8141                       || prev->fx_r_type != BFD_RELOC_HI16_S
8142                       || prev->fx_addsy != f->fx_addsy
8143                       || prev->fx_offset !=  f->fx_offset))
8144                 {
8145                   fixS **pf;
8146
8147                   /* Move l->fixp before f.  */
8148                   for (pf = &seginfo->fix_root;
8149                        *pf != l->fixp;
8150                        pf = &(*pf)->fx_next)
8151                     assert (*pf != NULL);
8152
8153                   *pf = l->fixp->fx_next;
8154
8155                   l->fixp->fx_next = f;
8156                   if (prev == NULL)
8157                     seginfo->fix_root = l->fixp;
8158                   else
8159                     prev->fx_next = l->fixp;
8160
8161                   break;
8162                 }
8163
8164               prev = f;
8165             }
8166
8167           if (f != NULL)
8168             break;
8169
8170           if (pass == 1)
8171             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
8172                            "Unmatched %%hi reloc");
8173         }
8174     }
8175 }
8176
8177 /* When generating embedded PIC code we need to use a special
8178    relocation to represent the difference of two symbols in the .text
8179    section (switch tables use a difference of this sort).  See
8180    include/coff/mips.h for details.  This macro checks whether this
8181    fixup requires the special reloc.  */
8182 #define SWITCH_TABLE(fixp) \
8183   ((fixp)->fx_r_type == BFD_RELOC_32 \
8184    && (fixp)->fx_addsy != NULL \
8185    && (fixp)->fx_subsy != NULL \
8186    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
8187    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
8188
8189 /* When generating embedded PIC code we must keep all PC relative
8190    relocations, in case the linker has to relax a call.  We also need
8191    to keep relocations for switch table entries.  */
8192
8193 /*ARGSUSED*/
8194 int
8195 mips_force_relocation (fixp)
8196      fixS *fixp;
8197 {
8198   return (mips_pic == EMBEDDED_PIC
8199           && (fixp->fx_pcrel
8200               || SWITCH_TABLE (fixp)
8201               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
8202               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
8203 }
8204
8205 /* Apply a fixup to the object file.  */
8206
8207 int
8208 md_apply_fix (fixP, valueP)
8209      fixS *fixP;
8210      valueT *valueP;
8211 {
8212   unsigned char *buf;
8213   long insn, value;
8214
8215   assert (fixP->fx_size == 4
8216           || fixP->fx_r_type == BFD_RELOC_16
8217           || fixP->fx_r_type == BFD_RELOC_64);
8218
8219   value = *valueP;
8220   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc */
8221
8222   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
8223     fixP->fx_done = 1;
8224
8225   switch (fixP->fx_r_type)
8226     {
8227     case BFD_RELOC_MIPS_JMP:
8228     case BFD_RELOC_HI16:
8229     case BFD_RELOC_HI16_S:
8230     case BFD_RELOC_MIPS_GPREL:
8231     case BFD_RELOC_MIPS_LITERAL:
8232     case BFD_RELOC_MIPS_CALL16:
8233     case BFD_RELOC_MIPS_GOT16:
8234     case BFD_RELOC_MIPS_GPREL32:
8235     case BFD_RELOC_MIPS_GOT_HI16:
8236     case BFD_RELOC_MIPS_GOT_LO16:
8237     case BFD_RELOC_MIPS_CALL_HI16:
8238     case BFD_RELOC_MIPS_CALL_LO16:
8239       if (fixP->fx_pcrel)
8240         as_bad_where (fixP->fx_file, fixP->fx_line,
8241                       "Invalid PC relative reloc");
8242       /* Nothing needed to do. The value comes from the reloc entry */
8243       break;
8244
8245     case BFD_RELOC_MIPS16_JMP:
8246       /* We currently always generate a reloc against a symbol, which
8247          means that we don't want an addend even if the symbol is
8248          defined.  */
8249       fixP->fx_addnumber = 0;
8250       break;
8251
8252     case BFD_RELOC_PCREL_HI16_S:
8253       /* The addend for this is tricky if it is internal, so we just
8254          do everything here rather than in bfd_perform_relocation.  */
8255       if ((fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) == 0)
8256         {
8257           /* For an external symbol adjust by the address to make it
8258              pcrel_offset.  We use the address of the RELLO reloc
8259              which follows this one.  */
8260           value += (fixP->fx_next->fx_frag->fr_address
8261                     + fixP->fx_next->fx_where);
8262         }
8263       if (value & 0x8000)
8264         value += 0x10000;
8265       value >>= 16;
8266       buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
8267       if (target_big_endian)
8268         buf += 2;
8269       md_number_to_chars (buf, value, 2);
8270       break;
8271
8272     case BFD_RELOC_PCREL_LO16:
8273       /* The addend for this is tricky if it is internal, so we just
8274          do everything here rather than in bfd_perform_relocation.  */
8275       if ((fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) == 0)
8276         value += fixP->fx_frag->fr_address + fixP->fx_where;
8277       buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
8278       if (target_big_endian)
8279         buf += 2;
8280       md_number_to_chars (buf, value, 2);
8281       break;
8282
8283     case BFD_RELOC_64:
8284       /* This is handled like BFD_RELOC_32, but we output a sign
8285          extended value if we are only 32 bits.  */
8286       if (fixP->fx_done
8287           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
8288         {
8289           if (8 <= sizeof (valueT))
8290             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
8291                                 value, 8);
8292           else
8293             {
8294               long w1, w2;
8295               long hiv;
8296
8297               w1 = w2 = fixP->fx_where;
8298               if (target_big_endian)
8299                 w1 += 4;
8300               else
8301                 w2 += 4;
8302               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
8303               if ((value & 0x80000000) != 0)
8304                 hiv = 0xffffffff;
8305               else
8306                 hiv = 0;
8307               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
8308             }
8309         }
8310       break;
8311
8312     case BFD_RELOC_32:
8313       /* If we are deleting this reloc entry, we must fill in the
8314          value now.  This can happen if we have a .word which is not
8315          resolved when it appears but is later defined.  We also need
8316          to fill in the value if this is an embedded PIC switch table
8317          entry.  */
8318       if (fixP->fx_done
8319           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
8320         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
8321                             value, 4);
8322       break;
8323
8324     case BFD_RELOC_16:
8325       /* If we are deleting this reloc entry, we must fill in the
8326          value now.  */
8327       assert (fixP->fx_size == 2);
8328       if (fixP->fx_done)
8329         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
8330                             value, 2);
8331       break;
8332
8333     case BFD_RELOC_LO16:
8334       /* When handling an embedded PIC switch statement, we can wind
8335          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
8336       if (fixP->fx_done)
8337         {
8338           if (value < -0x8000 || value > 0x7fff)
8339             as_bad_where (fixP->fx_file, fixP->fx_line,
8340                           "relocation overflow");
8341           buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
8342           if (target_big_endian)
8343             buf += 2;
8344           md_number_to_chars (buf, value, 2);
8345         }
8346       break;
8347
8348     case BFD_RELOC_16_PCREL_S2:
8349       /*
8350        * We need to save the bits in the instruction since fixup_segment()
8351        * might be deleting the relocation entry (i.e., a branch within
8352        * the current segment).
8353        */
8354       if ((value & 0x3) != 0)
8355         as_bad_where (fixP->fx_file, fixP->fx_line,
8356                       "Branch to odd address (%lx)", value);
8357       value >>= 2;
8358
8359       /* update old instruction data */
8360       buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
8361       if (target_big_endian)
8362         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
8363       else
8364         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
8365
8366       if (value >= -0x8000 && value < 0x8000)
8367         insn |= value & 0xffff;
8368       else
8369         {
8370           /* The branch offset is too large.  If this is an
8371              unconditional branch, and we are not generating PIC code,
8372              we can convert it to an absolute jump instruction.  */
8373           if (mips_pic == NO_PIC
8374               && fixP->fx_done
8375               && fixP->fx_frag->fr_address >= text_section->vma
8376               && (fixP->fx_frag->fr_address
8377                   < text_section->vma + text_section->_raw_size)
8378               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
8379                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
8380                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
8381             {
8382               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
8383                 insn = 0x0c000000;      /* jal */
8384               else
8385                 insn = 0x08000000;      /* j */
8386               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
8387               fixP->fx_done = 0;
8388               fixP->fx_addsy = section_symbol (text_section);
8389               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
8390             }
8391           else
8392             {
8393               /* FIXME.  It would be possible in principle to handle
8394                  conditional branches which overflow.  They could be
8395                  transformed into a branch around a jump.  This would
8396                  require setting up variant frags for each different
8397                  branch type.  The native MIPS assembler attempts to
8398                  handle these cases, but it appears to do it
8399                  incorrectly.  */
8400               as_bad_where (fixP->fx_file, fixP->fx_line,
8401                             "Relocation overflow");
8402             }
8403         }
8404
8405       md_number_to_chars ((char *) buf, (valueT) insn, 4);
8406       break;
8407
8408     default:
8409       internalError ();
8410     }
8411
8412   return 1;
8413 }
8414
8415 #if 0
8416 void
8417 printInsn (oc)
8418      unsigned long oc;
8419 {
8420   const struct mips_opcode *p;
8421   int treg, sreg, dreg, shamt;
8422   short imm;
8423   const char *args;
8424   int i;
8425
8426   for (i = 0; i < NUMOPCODES; ++i)
8427     {
8428       p = &mips_opcodes[i];
8429       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
8430         {
8431           printf ("%08lx %s\t", oc, p->name);
8432           treg = (oc >> 16) & 0x1f;
8433           sreg = (oc >> 21) & 0x1f;
8434           dreg = (oc >> 11) & 0x1f;
8435           shamt = (oc >> 6) & 0x1f;
8436           imm = oc;
8437           for (args = p->args;; ++args)
8438             {
8439               switch (*args)
8440                 {
8441                 case '\0':
8442                   printf ("\n");
8443                   break;
8444
8445                 case ',':
8446                 case '(':
8447                 case ')':
8448                   printf ("%c", *args);
8449                   continue;
8450
8451                 case 'r':
8452                   assert (treg == sreg);
8453                   printf ("$%d,$%d", treg, sreg);
8454                   continue;
8455
8456                 case 'd':
8457                 case 'G':
8458                   printf ("$%d", dreg);
8459                   continue;
8460
8461                 case 't':
8462                 case 'E':
8463                   printf ("$%d", treg);
8464                   continue;
8465
8466                 case 'k':
8467                   printf ("0x%x", treg);
8468                   continue;
8469
8470                 case 'b':
8471                 case 's':
8472                   printf ("$%d", sreg);
8473                   continue;
8474
8475                 case 'a':
8476                   printf ("0x%08lx", oc & 0x1ffffff);
8477                   continue;
8478
8479                 case 'i':
8480                 case 'j':
8481                 case 'o':
8482                 case 'u':
8483                   printf ("%d", imm);
8484                   continue;
8485
8486                 case '<':
8487                 case '>':
8488                   printf ("$%d", shamt);
8489                   continue;
8490
8491                 default:
8492                   internalError ();
8493                 }
8494               break;
8495             }
8496           return;
8497         }
8498     }
8499   printf ("%08lx  UNDEFINED\n", oc);
8500 }
8501 #endif
8502
8503 static symbolS *
8504 get_symbol ()
8505 {
8506   int c;
8507   char *name;
8508   symbolS *p;
8509
8510   name = input_line_pointer;
8511   c = get_symbol_end ();
8512   p = (symbolS *) symbol_find_or_make (name);
8513   *input_line_pointer = c;
8514   return p;
8515 }
8516
8517 /* Align the current frag to a given power of two.  The MIPS assembler
8518    also automatically adjusts any preceding label.  */
8519
8520 static void
8521 mips_align (to, fill, label)
8522      int to;
8523      int fill;
8524      symbolS *label;
8525 {
8526   mips_emit_delays (false);
8527   frag_align (to, fill);
8528   record_alignment (now_seg, to);
8529   if (label != NULL)
8530     {
8531       assert (S_GET_SEGMENT (label) == now_seg);
8532       label->sy_frag = frag_now;
8533       S_SET_VALUE (label, (valueT) frag_now_fix ());
8534     }
8535 }
8536
8537 /* Align to a given power of two.  .align 0 turns off the automatic
8538    alignment used by the data creating pseudo-ops.  */
8539
8540 static void
8541 s_align (x)
8542      int x;
8543 {
8544   register int temp;
8545   register long temp_fill;
8546   long max_alignment = 15;
8547
8548   /*
8549
8550     o  Note that the assembler pulls down any immediately preceeding label
8551        to the aligned address.
8552     o  It's not documented but auto alignment is reinstated by
8553        a .align pseudo instruction.
8554     o  Note also that after auto alignment is turned off the mips assembler
8555        issues an error on attempt to assemble an improperly aligned data item.
8556        We don't.
8557
8558     */
8559
8560   temp = get_absolute_expression ();
8561   if (temp > max_alignment)
8562     as_bad ("Alignment too large: %d. assumed.", temp = max_alignment);
8563   else if (temp < 0)
8564     {
8565       as_warn ("Alignment negative: 0 assumed.");
8566       temp = 0;
8567     }
8568   if (*input_line_pointer == ',')
8569     {
8570       input_line_pointer++;
8571       temp_fill = get_absolute_expression ();
8572     }
8573   else
8574     temp_fill = 0;
8575   if (temp)
8576     {
8577       auto_align = 1;
8578       mips_align (temp, (int) temp_fill,
8579                   insn_labels != NULL ? insn_labels->label : NULL);
8580     }
8581   else
8582     {
8583       auto_align = 0;
8584     }
8585
8586   demand_empty_rest_of_line ();
8587 }
8588
8589 void
8590 mips_flush_pending_output ()
8591 {
8592   mips_emit_delays (false);
8593   mips_clear_insn_labels ();
8594 }
8595
8596 static void
8597 s_change_sec (sec)
8598      int sec;
8599 {
8600   segT seg;
8601
8602   /* When generating embedded PIC code, we only use the .text, .lit8,
8603      .sdata and .sbss sections.  We change the .data and .rdata
8604      pseudo-ops to use .sdata.  */
8605   if (mips_pic == EMBEDDED_PIC
8606       && (sec == 'd' || sec == 'r'))
8607     sec = 's';
8608
8609   mips_emit_delays (false);
8610   switch (sec)
8611     {
8612     case 't':
8613       s_text (0);
8614       break;
8615     case 'd':
8616       s_data (0);
8617       break;
8618     case 'b':
8619       subseg_set (bss_section, (subsegT) get_absolute_expression ());
8620       demand_empty_rest_of_line ();
8621       break;
8622
8623     case 'r':
8624       if (USE_GLOBAL_POINTER_OPT)
8625         {
8626           seg = subseg_new (RDATA_SECTION_NAME,
8627                             (subsegT) get_absolute_expression ());
8628           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8629             {
8630               bfd_set_section_flags (stdoutput, seg,
8631                                      (SEC_ALLOC
8632                                       | SEC_LOAD
8633                                       | SEC_READONLY
8634                                       | SEC_RELOC
8635                                       | SEC_DATA));
8636               if (strcmp (TARGET_OS, "elf") != 0)
8637                 bfd_set_section_alignment (stdoutput, seg, 4);
8638             }
8639           demand_empty_rest_of_line ();
8640         }
8641       else
8642         {
8643           as_bad ("No read only data section in this object file format");
8644           demand_empty_rest_of_line ();
8645           return;
8646         }
8647       break;
8648
8649     case 's':
8650       if (USE_GLOBAL_POINTER_OPT)
8651         {
8652           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
8653           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8654             {
8655               bfd_set_section_flags (stdoutput, seg,
8656                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
8657                                      | SEC_DATA);
8658               if (strcmp (TARGET_OS, "elf") != 0)
8659                 bfd_set_section_alignment (stdoutput, seg, 4);
8660             }
8661           demand_empty_rest_of_line ();
8662           break;
8663         }
8664       else
8665         {
8666           as_bad ("Global pointers not supported; recompile -G 0");
8667           demand_empty_rest_of_line ();
8668           return;
8669         }
8670     }
8671
8672   auto_align = 1;
8673 }
8674
8675 void
8676 mips_enable_auto_align ()
8677 {
8678   auto_align = 1;
8679 }
8680
8681 static void
8682 s_cons (log_size)
8683      int log_size;
8684 {
8685   symbolS *label;
8686
8687   label = insn_labels != NULL ? insn_labels->label : NULL;
8688   mips_emit_delays (false);
8689   if (log_size > 0 && auto_align)
8690     mips_align (log_size, 0, label);
8691   mips_clear_insn_labels ();
8692   cons (1 << log_size);
8693 }
8694
8695 static void
8696 s_float_cons (type)
8697      int type;
8698 {
8699   symbolS *label;
8700
8701   label = insn_labels != NULL ? insn_labels->label : NULL;
8702
8703   mips_emit_delays (false);
8704
8705   if (auto_align)
8706     if (type == 'd')
8707       mips_align (3, 0, label);
8708     else
8709       mips_align (2, 0, label);
8710
8711   mips_clear_insn_labels ();
8712
8713   float_cons (type);
8714 }
8715
8716 /* Handle .globl.  We need to override it because on Irix 5 you are
8717    permitted to say
8718        .globl foo .text
8719    where foo is an undefined symbol, to mean that foo should be
8720    considered to be the address of a function.  */
8721
8722 static void
8723 s_mips_globl (x)
8724      int x;
8725 {
8726   char *name;
8727   int c;
8728   symbolS *symbolP;
8729   flagword flag;
8730
8731   name = input_line_pointer;
8732   c = get_symbol_end ();
8733   symbolP = symbol_find_or_make (name);
8734   *input_line_pointer = c;
8735   SKIP_WHITESPACE ();
8736
8737   /* On Irix 5, every global symbol that is not explicitly labelled as
8738      being a function is apparently labelled as being an object.  */
8739   flag = BSF_OBJECT;
8740
8741   if (! is_end_of_line[(unsigned char) *input_line_pointer])
8742     {
8743       char *secname;
8744       asection *sec;
8745
8746       secname = input_line_pointer;
8747       c = get_symbol_end ();
8748       sec = bfd_get_section_by_name (stdoutput, secname);
8749       if (sec == NULL)
8750         as_bad ("%s: no such section", secname);
8751       *input_line_pointer = c;
8752
8753       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
8754         flag = BSF_FUNCTION;
8755     }
8756
8757   symbolP->bsym->flags |= flag;
8758
8759   S_SET_EXTERNAL (symbolP);
8760   demand_empty_rest_of_line ();
8761 }
8762
8763 static void
8764 s_option (x)
8765      int x;
8766 {
8767   char *opt;
8768   char c;
8769
8770   opt = input_line_pointer;
8771   c = get_symbol_end ();
8772
8773   if (*opt == 'O')
8774     {
8775       /* FIXME: What does this mean?  */
8776     }
8777   else if (strncmp (opt, "pic", 3) == 0)
8778     {
8779       int i;
8780
8781       i = atoi (opt + 3);
8782       if (i == 0)
8783         mips_pic = NO_PIC;
8784       else if (i == 2)
8785         mips_pic = SVR4_PIC;
8786       else
8787         as_bad (".option pic%d not supported", i);
8788
8789       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
8790         {
8791           if (g_switch_seen && g_switch_value != 0)
8792             as_warn ("-G may not be used with SVR4 PIC code");
8793           g_switch_value = 0;
8794           bfd_set_gp_size (stdoutput, 0);
8795         }
8796     }
8797   else
8798     as_warn ("Unrecognized option \"%s\"", opt);
8799
8800   *input_line_pointer = c;
8801   demand_empty_rest_of_line ();
8802 }
8803
8804 static void
8805 s_mipsset (x)
8806      int x;
8807 {
8808   char *name = input_line_pointer, ch;
8809
8810   while (!is_end_of_line[(unsigned char) *input_line_pointer])
8811     input_line_pointer++;
8812   ch = *input_line_pointer;
8813   *input_line_pointer = '\0';
8814
8815   if (strcmp (name, "reorder") == 0)
8816     {
8817       if (mips_noreorder)
8818         {
8819           prev_insn_unreordered = 1;
8820           prev_prev_insn_unreordered = 1;
8821         }
8822       mips_noreorder = 0;
8823     }
8824   else if (strcmp (name, "noreorder") == 0)
8825     {
8826       mips_emit_delays (true);
8827       mips_noreorder = 1;
8828       mips_any_noreorder = 1;
8829     }
8830   else if (strcmp (name, "at") == 0)
8831     {
8832       mips_noat = 0;
8833     }
8834   else if (strcmp (name, "noat") == 0)
8835     {
8836       mips_noat = 1;
8837     }
8838   else if (strcmp (name, "macro") == 0)
8839     {
8840       mips_warn_about_macros = 0;
8841     }
8842   else if (strcmp (name, "nomacro") == 0)
8843     {
8844       if (mips_noreorder == 0)
8845         as_bad ("`noreorder' must be set before `nomacro'");
8846       mips_warn_about_macros = 1;
8847     }
8848   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
8849     {
8850       mips_nomove = 0;
8851     }
8852   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
8853     {
8854       mips_nomove = 1;
8855     }
8856   else if (strcmp (name, "bopt") == 0)
8857     {
8858       mips_nobopt = 0;
8859     }
8860   else if (strcmp (name, "nobopt") == 0)
8861     {
8862       mips_nobopt = 1;
8863     }
8864   else if (strcmp (name, "mips16") == 0
8865            || strcmp (name, "MIPS-16") == 0)
8866     mips16 = 1;
8867   else if (strcmp (name, "nomips16") == 0
8868            || strcmp (name, "noMIPS-16") == 0)
8869     mips16 = 0;
8870   else if (strncmp (name, "mips", 4) == 0)
8871     {
8872       int isa;
8873
8874       /* Permit the user to change the ISA on the fly.  Needless to
8875          say, misuse can cause serious problems.  */
8876       isa = atoi (name + 4);
8877       if (isa == 0)
8878         mips_isa = file_mips_isa;
8879       else if (isa < 1 || isa > 4)
8880         as_bad ("unknown ISA level");
8881       else
8882         mips_isa = isa;
8883     }
8884   else if (strcmp (name, "autoextend") == 0)
8885     mips16_autoextend = 1;
8886   else if (strcmp (name, "noautoextend") == 0)
8887     mips16_autoextend = 0;
8888   else
8889     {
8890       as_warn ("Tried to set unrecognized symbol: %s\n", name);
8891     }
8892   *input_line_pointer = ch;
8893   demand_empty_rest_of_line ();
8894 }
8895
8896 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
8897    .option pic2.  It means to generate SVR4 PIC calls.  */
8898
8899 static void
8900 s_abicalls (ignore)
8901      int ignore;
8902 {
8903   mips_pic = SVR4_PIC;
8904   if (USE_GLOBAL_POINTER_OPT)
8905     {
8906       if (g_switch_seen && g_switch_value != 0)
8907         as_warn ("-G may not be used with SVR4 PIC code");
8908       g_switch_value = 0;
8909     }
8910   bfd_set_gp_size (stdoutput, 0);
8911   demand_empty_rest_of_line ();
8912 }
8913
8914 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
8915    PIC code.  It sets the $gp register for the function based on the
8916    function address, which is in the register named in the argument.
8917    This uses a relocation against _gp_disp, which is handled specially
8918    by the linker.  The result is:
8919         lui     $gp,%hi(_gp_disp)
8920         addiu   $gp,$gp,%lo(_gp_disp)
8921         addu    $gp,$gp,.cpload argument
8922    The .cpload argument is normally $25 == $t9.  */
8923
8924 static void
8925 s_cpload (ignore)
8926      int ignore;
8927 {
8928   expressionS ex;
8929   int icnt = 0;
8930
8931   /* If we are not generating SVR4 PIC code, .cpload is ignored.  */
8932   if (mips_pic != SVR4_PIC)
8933     {
8934       s_ignore (0);
8935       return;
8936     }
8937
8938   /* .cpload should be a in .set noreorder section.  */
8939   if (mips_noreorder == 0)
8940     as_warn (".cpload not in noreorder section");
8941
8942   ex.X_op = O_symbol;
8943   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
8944   ex.X_op_symbol = NULL;
8945   ex.X_add_number = 0;
8946
8947   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
8948   ex.X_add_symbol->bsym->flags |= BSF_OBJECT;
8949
8950   macro_build_lui ((char *) NULL, &icnt, &ex, GP);
8951   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
8952                (int) BFD_RELOC_LO16);
8953
8954   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
8955                GP, GP, tc_get_register (0));
8956
8957   demand_empty_rest_of_line ();
8958 }
8959
8960 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
8961    offset from $sp.  The offset is remembered, and after making a PIC
8962    call $gp is restored from that location.  */
8963
8964 static void
8965 s_cprestore (ignore)
8966      int ignore;
8967 {
8968   expressionS ex;
8969   int icnt = 0;
8970
8971   /* If we are not generating SVR4 PIC code, .cprestore is ignored.  */
8972   if (mips_pic != SVR4_PIC)
8973     {
8974       s_ignore (0);
8975       return;
8976     }
8977
8978   mips_cprestore_offset = get_absolute_expression ();
8979
8980   ex.X_op = O_constant;
8981   ex.X_add_symbol = NULL;
8982   ex.X_op_symbol = NULL;
8983   ex.X_add_number = mips_cprestore_offset;
8984
8985   macro_build ((char *) NULL, &icnt, &ex,
8986                mips_isa < 3 ? "sw" : "sd",
8987                "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
8988
8989   demand_empty_rest_of_line ();
8990 }
8991
8992 /* Handle the .gpword pseudo-op.  This is used when generating PIC
8993    code.  It generates a 32 bit GP relative reloc.  */
8994
8995 static void
8996 s_gpword (ignore)
8997      int ignore;
8998 {
8999   symbolS *label;
9000   expressionS ex;
9001   char *p;
9002
9003   /* When not generating PIC code, this is treated as .word.  */
9004   if (mips_pic != SVR4_PIC)
9005     {
9006       s_cons (2);
9007       return;
9008     }
9009
9010   label = insn_labels != NULL ? insn_labels->label : NULL;
9011   mips_emit_delays (true);
9012   if (auto_align)
9013     mips_align (2, 0, label);
9014   mips_clear_insn_labels ();
9015
9016   expression (&ex);
9017
9018   if (ex.X_op != O_symbol || ex.X_add_number != 0)
9019     {
9020       as_bad ("Unsupported use of .gpword");
9021       ignore_rest_of_line ();
9022     }
9023
9024   p = frag_more (4);
9025   md_number_to_chars (p, (valueT) 0, 4);
9026   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
9027                BFD_RELOC_MIPS_GPREL32);
9028
9029   demand_empty_rest_of_line ();
9030 }
9031
9032 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
9033    tables in SVR4 PIC code.  */
9034
9035 static void
9036 s_cpadd (ignore)
9037      int ignore;
9038 {
9039   int icnt = 0;
9040   int reg;
9041
9042   /* This is ignored when not generating SVR4 PIC code.  */
9043   if (mips_pic != SVR4_PIC)
9044     {
9045       s_ignore (0);
9046       return;
9047     }
9048
9049   /* Add $gp to the register named as an argument.  */
9050   reg = tc_get_register (0);
9051   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9052                mips_isa < 3 ? "addu" : "daddu",
9053                "d,v,t", reg, reg, GP);
9054
9055   demand_empty_rest_of_line ();  
9056 }
9057
9058 /* Parse a register string into a number.  Called from the ECOFF code
9059    to parse .frame.  The argument is non-zero if this is the frame
9060    register, so that we can record it in mips_frame_reg.  */
9061
9062 int
9063 tc_get_register (frame)
9064      int frame;
9065 {
9066   int reg;
9067
9068   SKIP_WHITESPACE ();
9069   if (*input_line_pointer++ != '$')
9070     {
9071       as_warn ("expected `$'");
9072       reg = 0;
9073     }
9074   else if (isdigit ((unsigned char) *input_line_pointer))
9075     {
9076       reg = get_absolute_expression ();
9077       if (reg < 0 || reg >= 32)
9078         {
9079           as_warn ("Bad register number");
9080           reg = 0;
9081         }
9082     }
9083   else
9084     {
9085       if (strncmp (input_line_pointer, "fp", 2) == 0)
9086         reg = FP;
9087       else if (strncmp (input_line_pointer, "sp", 2) == 0)
9088         reg = SP;
9089       else if (strncmp (input_line_pointer, "gp", 2) == 0)
9090         reg = GP;
9091       else if (strncmp (input_line_pointer, "at", 2) == 0)
9092         reg = AT;
9093       else
9094         {
9095           as_warn ("Unrecognized register name");
9096           reg = 0;
9097         }
9098       input_line_pointer += 2;
9099     }
9100   if (frame)
9101     mips_frame_reg = reg != 0 ? reg : SP;
9102   return reg;
9103 }
9104
9105 valueT
9106 md_section_align (seg, addr)
9107      asection *seg;
9108      valueT addr;
9109 {
9110   int align = bfd_get_section_alignment (stdoutput, seg);
9111
9112 #ifdef OBJ_ELF
9113   /* We don't need to align ELF sections to the full alignment.
9114      However, Irix 5 may prefer that we align them at least to a 16
9115      byte boundary.  We don't bother to align the sections if we are
9116      targeted for an embedded system.  */
9117   if (strcmp (TARGET_OS, "elf") == 0)
9118     return addr;
9119   if (align > 4)
9120     align = 4;
9121 #endif
9122
9123   return ((addr + (1 << align) - 1) & (-1 << align));
9124 }
9125
9126 /* Utility routine, called from above as well.  If called while the
9127    input file is still being read, it's only an approximation.  (For
9128    example, a symbol may later become defined which appeared to be
9129    undefined earlier.)  */
9130
9131 static int
9132 nopic_need_relax (sym)
9133      symbolS *sym;
9134 {
9135   if (sym == 0)
9136     return 0;
9137
9138   if (USE_GLOBAL_POINTER_OPT)
9139     {
9140       const char *symname;
9141       int change;
9142
9143       /* Find out whether this symbol can be referenced off the GP
9144          register.  It can be if it is smaller than the -G size or if
9145          it is in the .sdata or .sbss section.  Certain symbols can
9146          not be referenced off the GP, although it appears as though
9147          they can.  */
9148       symname = S_GET_NAME (sym);
9149       if (symname != (const char *) NULL
9150           && (strcmp (symname, "eprol") == 0
9151               || strcmp (symname, "etext") == 0
9152               || strcmp (symname, "_gp") == 0
9153               || strcmp (symname, "edata") == 0
9154               || strcmp (symname, "_fbss") == 0
9155               || strcmp (symname, "_fdata") == 0
9156               || strcmp (symname, "_ftext") == 0
9157               || strcmp (symname, "end") == 0
9158               || strcmp (symname, "_gp_disp") == 0))
9159         change = 1;
9160       else if (! S_IS_DEFINED (sym)
9161                && (0
9162 #ifndef NO_ECOFF_DEBUGGING
9163                    || (sym->ecoff_extern_size != 0
9164                        && sym->ecoff_extern_size <= g_switch_value)
9165 #endif
9166                    || (S_GET_VALUE (sym) != 0
9167                        && S_GET_VALUE (sym) <= g_switch_value)))
9168         change = 0;
9169       else
9170         {
9171           const char *segname;
9172
9173           segname = segment_name (S_GET_SEGMENT (sym));
9174           assert (strcmp (segname, ".lit8") != 0
9175                   && strcmp (segname, ".lit4") != 0);
9176           change = (strcmp (segname, ".sdata") != 0
9177                     && strcmp (segname, ".sbss") != 0);
9178         }
9179       return change;
9180     }
9181   else
9182     /* We are not optimizing for the GP register.  */
9183     return 1;
9184 }
9185
9186 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
9187    extended opcode.  SEC is the section the frag is in.  */
9188
9189 static int
9190 mips16_extended_frag (fragp, sec, stretch)
9191      fragS *fragp;
9192      asection *sec;
9193      long stretch;
9194 {
9195   int type;
9196   register const struct mips16_immed_operand *op;
9197   offsetT val;
9198   int mintiny, maxtiny;
9199   segT symsec;
9200
9201   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
9202     return 0;
9203   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
9204     return 1;
9205
9206   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
9207   op = mips16_immed_operands;
9208   while (op->type != type)
9209     {
9210       ++op;
9211       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9212     }
9213
9214   if (op->unsp)
9215     {
9216       if (type == '<' || type == '>' || type == '[' || type == ']')
9217         {
9218           mintiny = 1;
9219           maxtiny = 1 << op->nbits;
9220         }
9221       else
9222         {
9223           mintiny = 0;
9224           maxtiny = (1 << op->nbits) - 1;
9225         }
9226     }
9227   else
9228     {
9229       mintiny = - (1 << (op->nbits - 1));
9230       maxtiny = (1 << (op->nbits - 1)) - 1;
9231     }
9232
9233   /* We can't call S_GET_VALUE here, because we don't want to lock in
9234      a particular frag address.  */
9235   if (fragp->fr_symbol->sy_value.X_op == O_constant)
9236     {
9237       val = (fragp->fr_symbol->sy_value.X_add_number
9238              + fragp->fr_symbol->sy_frag->fr_address);
9239       symsec = S_GET_SEGMENT (fragp->fr_symbol);
9240     }
9241   else if (fragp->fr_symbol->sy_value.X_op == O_symbol
9242            && (fragp->fr_symbol->sy_value.X_add_symbol->sy_value.X_op
9243                == O_constant))
9244     {
9245       val = (fragp->fr_symbol->sy_value.X_add_symbol->sy_value.X_add_number
9246              + fragp->fr_symbol->sy_value.X_add_symbol->sy_frag->fr_address
9247              + fragp->fr_symbol->sy_value.X_add_number
9248              + fragp->fr_symbol->sy_frag->fr_address);
9249       symsec = S_GET_SEGMENT (fragp->fr_symbol->sy_value.X_add_symbol);
9250     }
9251   else
9252     return 1;
9253
9254   if (op->pcrel)
9255     {
9256       addressT addr;
9257
9258       /* We won't have the section when we are called from
9259          mips_relax_frag.  However, we will always have been called
9260          from md_estimate_size_before_relax first.  If this is a
9261          branch to a different section, we mark it as such.  If SEC is
9262          NULL, and the frag is not marked, then it must be a branch to
9263          the same section.  */
9264       if (sec == NULL)
9265         {
9266           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
9267             return 1;
9268         }
9269       else
9270         {
9271           if (symsec != sec)
9272             {
9273               fragp->fr_subtype =
9274                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
9275
9276               /* FIXME: We should support this, and let the linker
9277                  catch branches and loads that are out of range.  */
9278               as_bad_where (fragp->fr_file, fragp->fr_line,
9279                             "unsupported PC relative reference to different section");
9280
9281               return 1;
9282             }
9283         }
9284
9285       /* In this case, we know for sure that the symbol fragment is in
9286          the same section.  If the fr_address of the symbol fragment
9287          is greater then the address of this fragment we want to add
9288          in STRETCH in order to get a better estimate of the address.
9289          This particularly matters because of the shift bits.  */
9290       if (stretch != 0
9291           && fragp->fr_symbol->sy_frag->fr_address >= fragp->fr_address)
9292         {
9293           fragS *f;
9294
9295           /* Adjust stretch for any alignment frag.  */
9296           for (f = fragp; f != fragp->fr_symbol->sy_frag; f = f->fr_next)
9297             {
9298               assert (f != NULL);
9299               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
9300                 {
9301                   if (stretch < 0)
9302                     stretch = - ((- stretch)
9303                                  & ~ ((1 << (int) f->fr_offset) - 1));
9304                   else
9305                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
9306                   if (stretch == 0)
9307                     break;
9308                 }
9309             }
9310           val += stretch;
9311         }
9312
9313       addr = fragp->fr_address + fragp->fr_fix;
9314
9315       /* The base address rules are complicated.  The base address of
9316          a branch is the following instruction.  The base address of a
9317          PC relative load or add is the instruction itself, but if it
9318          is extended add 2, and if it is in a delay slot (in which
9319          case it can not be extended) use the address of the
9320          instruction whose delay slot it is in.  */
9321       if (type == 'p' || type == 'q')
9322         {
9323           addr += 2;
9324           /* Ignore the low bit in the target, since it will be set
9325              for a text label.  */
9326           if ((val & 1) != 0)
9327             --val;
9328         }
9329       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
9330         addr -= 4;
9331       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
9332         addr -= 2;
9333
9334       /* If we are currently assuming that this frag should be
9335          extended, then the current address is two bytes higher.  */
9336       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
9337         addr += 2;
9338
9339       val -= addr & ~ ((1 << op->shift) - 1);
9340
9341       /* Branch offsets have an implicit 0 in the lowest bit.  */
9342       if (type == 'p' || type == 'q')
9343         val /= 2;
9344
9345       /* If any of the shifted bits are set, we must use an extended
9346          opcode.  If the address depends on the size of this
9347          instruction, this can lead to a loop, so we arrange to always
9348          use an extended opcode.  We only check this when we are in
9349          the main relaxation loop, when SEC is NULL.  */
9350       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
9351         {
9352           fragp->fr_subtype =
9353             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
9354           return 1;
9355         }
9356
9357       /* If we are about to mark a frag as extended because the value
9358          is precisely maxtiny + 1, then there is a chance of an
9359          infinite loop as in the following code:
9360              la $4,foo
9361              .skip      1020
9362              .align     2
9363            foo:
9364          In this case when the la is extended, foo is 0x3fc bytes
9365          away, so the la can be shrunk, but then foo is 0x400 away, so
9366          the la must be extended.  To avoid this loop, we mark the
9367          frag as extended if it was small, and is about to become
9368          extended with a value of maxtiny + 1.  */
9369       if (val == ((maxtiny + 1) << op->shift)
9370           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
9371           && sec == NULL)
9372         {
9373           fragp->fr_subtype =
9374             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
9375           return 1;
9376         }
9377     }
9378   else if (symsec != absolute_section && sec != NULL)
9379     as_bad_where (fragp->fr_file, fragp->fr_line, "unsupported relocation");
9380
9381   if ((val & ((1 << op->shift) - 1)) != 0
9382       || val < (mintiny << op->shift)
9383       || val > (maxtiny << op->shift))
9384     return 1;
9385   else
9386     return 0;
9387 }
9388
9389 /* Estimate the size of a frag before relaxing.  Unless this is the
9390    mips16, we are not really relaxing here, and the final size is
9391    encoded in the subtype information.  For the mips16, we have to
9392    decide whether we are using an extended opcode or not.  */
9393
9394 /*ARGSUSED*/
9395 int
9396 md_estimate_size_before_relax (fragp, segtype)
9397      fragS *fragp;
9398      asection *segtype;
9399 {
9400   int change;
9401
9402   if (RELAX_MIPS16_P (fragp->fr_subtype))
9403     {
9404       if (mips16_extended_frag (fragp, segtype, 0))
9405         {
9406           fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
9407           return 4;
9408         }
9409       else
9410         {
9411           fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
9412           return 2;
9413         }
9414     }
9415
9416   if (mips_pic == NO_PIC)
9417     {
9418       change = nopic_need_relax (fragp->fr_symbol);
9419     }
9420   else if (mips_pic == SVR4_PIC)
9421     {
9422       asection *symsec = fragp->fr_symbol->bsym->section;
9423
9424       /* This must duplicate the test in adjust_reloc_syms.  */
9425       change = (symsec != &bfd_und_section
9426                 && symsec != &bfd_abs_section
9427                 && ! bfd_is_com_section (symsec));
9428     }
9429   else
9430     abort ();
9431
9432   if (change)
9433     {
9434       /* Record the offset to the first reloc in the fr_opcode field.
9435          This lets md_convert_frag and tc_gen_reloc know that the code
9436          must be expanded.  */
9437       fragp->fr_opcode = (fragp->fr_literal
9438                           + fragp->fr_fix
9439                           - RELAX_OLD (fragp->fr_subtype)
9440                           + RELAX_RELOC1 (fragp->fr_subtype));
9441       /* FIXME: This really needs as_warn_where.  */
9442       if (RELAX_WARN (fragp->fr_subtype))
9443         as_warn ("AT used after \".set noat\" or macro used after \".set nomacro\"");
9444     }
9445
9446   if (! change)
9447     return 0;
9448   else
9449     return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
9450 }
9451
9452 /* Translate internal representation of relocation info to BFD target
9453    format.  */
9454
9455 arelent **
9456 tc_gen_reloc (section, fixp)
9457      asection *section;
9458      fixS *fixp;
9459 {
9460   static arelent *retval[4];
9461   arelent *reloc;
9462   bfd_reloc_code_real_type code;
9463
9464   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
9465   retval[1] = NULL;
9466
9467   reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
9468   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
9469
9470   if (mips_pic == EMBEDDED_PIC
9471       && SWITCH_TABLE (fixp))
9472     {
9473       /* For a switch table entry we use a special reloc.  The addend
9474          is actually the difference between the reloc address and the
9475          subtrahend.  */
9476       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
9477       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
9478         as_fatal ("Double check fx_r_type in tc-mips.c:tc_gen_reloc");
9479       fixp->fx_r_type = BFD_RELOC_GPREL32;
9480     }
9481   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
9482     {
9483       /* We use a special addend for an internal RELLO reloc.  */
9484       if (fixp->fx_addsy->bsym->flags & BSF_SECTION_SYM)
9485         reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
9486       else
9487         reloc->addend = fixp->fx_addnumber + reloc->address;
9488     }
9489   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
9490     {
9491       assert (fixp->fx_next != NULL
9492               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
9493       /* We use a special addend for an internal RELHI reloc.  The
9494          reloc is relative to the RELLO; adjust the addend
9495          accordingly.  */
9496       if (fixp->fx_addsy->bsym->flags & BSF_SECTION_SYM)
9497         reloc->addend = (fixp->fx_next->fx_frag->fr_address
9498                          + fixp->fx_next->fx_where
9499                          - S_GET_VALUE (fixp->fx_subsy));
9500       else
9501         reloc->addend = (fixp->fx_addnumber
9502                          + fixp->fx_next->fx_frag->fr_address
9503                          + fixp->fx_next->fx_where);
9504     }
9505   else if (fixp->fx_pcrel == 0)
9506     reloc->addend = fixp->fx_addnumber;
9507   else
9508     {
9509       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
9510         /* A gruesome hack which is a result of the gruesome gas reloc
9511            handling.  */
9512         reloc->addend = reloc->address;
9513       else
9514         reloc->addend = -reloc->address;
9515     }
9516
9517   /* If this is a variant frag, we may need to adjust the existing
9518      reloc and generate a new one.  */
9519   if (fixp->fx_frag->fr_opcode != NULL
9520       && (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
9521           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
9522           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
9523           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
9524           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
9525           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
9526           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16))
9527     {
9528       arelent *reloc2;
9529
9530       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
9531
9532       /* If this is not the last reloc in this frag, then we have two
9533          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
9534          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
9535          the second one handle all of them.  */
9536       if (fixp->fx_next != NULL
9537           && fixp->fx_frag == fixp->fx_next->fx_frag)
9538         {
9539           assert ((fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
9540                    && fixp->fx_next->fx_r_type == BFD_RELOC_MIPS_GPREL)
9541                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
9542                       && (fixp->fx_next->fx_r_type
9543                           == BFD_RELOC_MIPS_GOT_LO16))
9544                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
9545                       && (fixp->fx_next->fx_r_type
9546                           == BFD_RELOC_MIPS_CALL_LO16)));
9547           retval[0] = NULL;
9548           return retval;
9549         }
9550
9551       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
9552       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
9553       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
9554       retval[2] = NULL;
9555       reloc2->sym_ptr_ptr = &fixp->fx_addsy->bsym;
9556       reloc2->address = (reloc->address
9557                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
9558                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
9559       reloc2->addend = fixp->fx_addnumber;
9560       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
9561       assert (reloc2->howto != NULL);
9562
9563       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
9564         {
9565           arelent *reloc3;
9566
9567           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
9568           retval[3] = NULL;
9569           *reloc3 = *reloc2;
9570           reloc3->address += 4;
9571         }
9572
9573       if (mips_pic == NO_PIC)
9574         {
9575           assert (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL);
9576           fixp->fx_r_type = BFD_RELOC_HI16_S;
9577         }
9578       else if (mips_pic == SVR4_PIC)
9579         {
9580           switch (fixp->fx_r_type)
9581             {
9582             default:
9583               abort ();
9584             case BFD_RELOC_MIPS_GOT16:
9585               break;
9586             case BFD_RELOC_MIPS_CALL16:
9587             case BFD_RELOC_MIPS_GOT_LO16:
9588             case BFD_RELOC_MIPS_CALL_LO16:
9589               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
9590               break;
9591             }
9592         }
9593       else
9594         abort ();
9595     }
9596
9597   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
9598      fixup_segment converted a non-PC relative reloc into a PC
9599      relative reloc.  In such a case, we need to convert the reloc
9600      code.  */
9601   code = fixp->fx_r_type;
9602   if (fixp->fx_pcrel)
9603     {
9604       switch (code)
9605         {
9606         case BFD_RELOC_8:
9607           code = BFD_RELOC_8_PCREL;
9608           break;
9609         case BFD_RELOC_16:
9610           code = BFD_RELOC_16_PCREL;
9611           break;
9612         case BFD_RELOC_32:
9613           code = BFD_RELOC_32_PCREL;
9614           break;
9615         case BFD_RELOC_64:
9616           code = BFD_RELOC_64_PCREL;
9617           break;
9618         case BFD_RELOC_8_PCREL:
9619         case BFD_RELOC_16_PCREL:
9620         case BFD_RELOC_32_PCREL:
9621         case BFD_RELOC_64_PCREL:
9622         case BFD_RELOC_16_PCREL_S2:
9623         case BFD_RELOC_PCREL_HI16_S:
9624         case BFD_RELOC_PCREL_LO16:
9625           break;
9626         default:
9627           as_bad_where (fixp->fx_file, fixp->fx_line,
9628                         "Cannot make %s relocation PC relative",
9629                         bfd_get_reloc_code_name (code));
9630         }
9631     }
9632
9633   /* To support a PC relative reloc when generating embedded PIC code
9634      for ECOFF, we use a Cygnus extension.  We check for that here to
9635      make sure that we don't let such a reloc escape normally.  */
9636   if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour
9637       && code == BFD_RELOC_16_PCREL_S2
9638       && mips_pic != EMBEDDED_PIC)
9639     reloc->howto = NULL;
9640   else
9641     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
9642
9643   if (reloc->howto == NULL)
9644     {
9645       as_bad_where (fixp->fx_file, fixp->fx_line,
9646                     "Can not represent %s relocation in this object file format",
9647                     bfd_get_reloc_code_name (code));
9648       retval[0] = NULL;
9649     }
9650
9651   return retval;
9652 }
9653
9654 /* Relax a machine dependent frag.  This returns the amount by which
9655    the current size of the frag should change.  */
9656
9657 int
9658 mips_relax_frag (fragp, stretch)
9659      fragS *fragp;
9660      long stretch;
9661 {
9662   if (! RELAX_MIPS16_P (fragp->fr_subtype))
9663     return 0;
9664
9665   if (mips16_extended_frag (fragp, (asection *) NULL, stretch))
9666     {
9667       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
9668         return 0;
9669       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
9670       return 2;
9671     }
9672   else
9673     {
9674       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
9675         return 0;
9676       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
9677       return -2;
9678     }
9679
9680   return 0;
9681 }
9682
9683 /* Convert a machine dependent frag.  */
9684
9685 void
9686 md_convert_frag (abfd, asec, fragp)
9687      bfd *abfd;
9688      segT asec;
9689      fragS *fragp;
9690 {
9691   int old, new;
9692   char *fixptr;
9693
9694   if (RELAX_MIPS16_P (fragp->fr_subtype))
9695     {
9696       int type;
9697       register const struct mips16_immed_operand *op;
9698       boolean small, ext;
9699       offsetT val;
9700       bfd_byte *buf;
9701       unsigned long insn;
9702       boolean use_extend;
9703       unsigned short extend;
9704
9705       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
9706       op = mips16_immed_operands;
9707       while (op->type != type)
9708         ++op;
9709
9710       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
9711         {
9712           small = false;
9713           ext = true;
9714         }
9715       else
9716         {
9717           small = true;
9718           ext = false;
9719         }
9720
9721       resolve_symbol_value (fragp->fr_symbol);
9722       val = S_GET_VALUE (fragp->fr_symbol);
9723       if (op->pcrel)
9724         {
9725           addressT addr;
9726
9727           addr = fragp->fr_address + fragp->fr_fix;
9728
9729           /* The rules for the base address of a PC relative reloc are
9730              complicated; see mips16_extended_frag.  */
9731           if (type == 'p' || type == 'q')
9732             {
9733               addr += 2;
9734               /* Ignore the low bit in the target, since it will be
9735                  set for a text label.  */
9736               if ((val & 1) != 0)
9737                 --val;
9738             }
9739           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
9740             addr -= 4;
9741           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
9742             addr -= 2;
9743
9744           if (ext)
9745             addr += 2;
9746           addr &= ~ (addressT) ((1 << op->shift) - 1);
9747           val -= addr;
9748
9749           /* Make sure the section winds up with the alignment we have
9750              assumed.  */
9751           if (op->shift > 0)
9752             record_alignment (asec, op->shift);
9753         }
9754
9755       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
9756
9757       if (target_big_endian)
9758         insn = bfd_getb16 (buf);
9759       else
9760         insn = bfd_getl16 (buf);
9761
9762       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
9763                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
9764                     small, ext, &insn, &use_extend, &extend);
9765
9766       if (use_extend)
9767         {
9768           md_number_to_chars (buf, 0xf000 | extend, 2);
9769           fragp->fr_fix += 2;
9770           buf += 2;
9771         }
9772
9773       md_number_to_chars (buf, insn, 2);
9774       fragp->fr_fix += 2;
9775       buf += 2;
9776     }
9777   else
9778     {
9779       if (fragp->fr_opcode == NULL)
9780         return;
9781
9782       old = RELAX_OLD (fragp->fr_subtype);
9783       new = RELAX_NEW (fragp->fr_subtype);
9784       fixptr = fragp->fr_literal + fragp->fr_fix;
9785
9786       if (new > 0)
9787         memcpy (fixptr - old, fixptr, new);
9788
9789       fragp->fr_fix += new - old;
9790     }
9791 }
9792
9793 #ifdef OBJ_ELF
9794
9795 /* This function is called after the relocs have been generated.
9796    We've been storing mips16 text labels as odd.  Here we convert them
9797    back to even for the convenience of the debugger.  */
9798
9799 void
9800 mips_frob_file_after_relocs ()
9801 {
9802   asymbol **syms;
9803   unsigned int count, i;
9804
9805   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9806     return;
9807
9808   syms = bfd_get_outsymbols (stdoutput);
9809   count = bfd_get_symcount (stdoutput);
9810   for (i = 0; i < count; i++, syms++)
9811     {
9812       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
9813           && ((*syms)->value & 1) != 0)
9814         {
9815           (*syms)->value &= ~1;
9816           /* If the symbol has an odd size, it was probably computed
9817              incorrectly, so adjust that as well.  */
9818           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
9819             ++elf_symbol (*syms)->internal_elf_sym.st_size;
9820         }
9821     }
9822 }
9823
9824 #endif
9825
9826 /* This function is called whenever a label is defined.  It is used
9827    when handling branch delays; if a branch has a label, we assume we
9828    can not move it.  */
9829
9830 void
9831 mips_define_label (sym)
9832      symbolS *sym;
9833 {
9834   struct insn_label_list *l;
9835
9836   if (free_insn_labels == NULL)
9837     l = (struct insn_label_list *) xmalloc (sizeof *l);
9838   else
9839     {
9840       l = free_insn_labels;
9841       free_insn_labels = l->next;
9842     }
9843
9844   l->label = sym;
9845   l->next = insn_labels;
9846   insn_labels = l;
9847 }
9848
9849 /* Decide whether a label is local.  This is called by LOCAL_LABEL.
9850    In order to work with gcc when using mips-tfile, we must keep all
9851    local labels.  However, in other cases, we want to discard them,
9852    since they are useless.  */
9853
9854 int
9855 mips_local_label (name)
9856      const char *name;
9857 {
9858 #ifndef NO_ECOFF_DEBUGGING
9859   if (ECOFF_DEBUGGING
9860       && mips_debug != 0
9861       && ! ecoff_debugging_seen)
9862     {
9863       /* We were called with -g, but we didn't see any debugging
9864          information.  That may mean that gcc is smuggling debugging
9865          information through to mips-tfile, in which case we must
9866          generate all local labels.  */
9867       return 0;
9868     }
9869 #endif
9870
9871   /* Here it's OK to discard local labels.  */
9872
9873   return name[0] == '$';
9874 }
9875 \f
9876 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9877
9878 /* Some special processing for a MIPS ELF file.  */
9879
9880 void
9881 mips_elf_final_processing ()
9882 {
9883   /* Write out the register information.  */
9884   if (! mips_64)
9885     {
9886       Elf32_RegInfo s;
9887
9888       s.ri_gprmask = mips_gprmask;
9889       s.ri_cprmask[0] = mips_cprmask[0];
9890       s.ri_cprmask[1] = mips_cprmask[1];
9891       s.ri_cprmask[2] = mips_cprmask[2];
9892       s.ri_cprmask[3] = mips_cprmask[3];
9893       /* The gp_value field is set by the MIPS ELF backend.  */
9894
9895       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
9896                                        ((Elf32_External_RegInfo *)
9897                                         mips_regmask_frag));
9898     }
9899   else
9900     {
9901       Elf64_Internal_RegInfo s;
9902
9903       s.ri_gprmask = mips_gprmask;
9904       s.ri_pad = 0;
9905       s.ri_cprmask[0] = mips_cprmask[0];
9906       s.ri_cprmask[1] = mips_cprmask[1];
9907       s.ri_cprmask[2] = mips_cprmask[2];
9908       s.ri_cprmask[3] = mips_cprmask[3];
9909       /* The gp_value field is set by the MIPS ELF backend.  */
9910
9911       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
9912                                        ((Elf64_External_RegInfo *)
9913                                         mips_regmask_frag));
9914     }
9915
9916   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
9917      sort of BFD interface for this.  */
9918   if (mips_any_noreorder)
9919     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
9920   if (mips_pic != NO_PIC)
9921     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
9922 }
9923
9924 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
9925 \f
9926 /* These functions should really be defined by the object file format,
9927    since they are related to debugging information.  However, this
9928    code has to work for the a.out format, which does not define them,
9929    so we provide simple versions here.  These don't actually generate
9930    any debugging information, but they do simple checking and someday
9931    somebody may make them useful.  */
9932
9933 typedef struct loc
9934 {
9935   struct loc *loc_next;
9936   unsigned long loc_fileno;
9937   unsigned long loc_lineno;
9938   unsigned long loc_offset;
9939   unsigned short loc_delta;
9940   unsigned short loc_count;
9941 #if 0
9942   fragS *loc_frag;
9943 #endif
9944 }
9945 locS;
9946
9947 typedef struct proc
9948   {
9949     struct proc *proc_next;
9950     struct symbol *proc_isym;
9951     struct symbol *proc_end;
9952     unsigned long proc_reg_mask;
9953     unsigned long proc_reg_offset;
9954     unsigned long proc_fpreg_mask;
9955     unsigned long proc_fpreg_offset;
9956     unsigned long proc_frameoffset;
9957     unsigned long proc_framereg;
9958     unsigned long proc_pcreg;
9959     locS *proc_iline;
9960     struct file *proc_file;
9961     int proc_index;
9962   }
9963 procS;
9964
9965 typedef struct file
9966   {
9967     struct file *file_next;
9968     unsigned long file_fileno;
9969     struct symbol *file_symbol;
9970     struct symbol *file_end;
9971     struct proc *file_proc;
9972     int file_numprocs;
9973   }
9974 fileS;
9975
9976 static struct obstack proc_frags;
9977 static procS *proc_lastP;
9978 static procS *proc_rootP;
9979 static int numprocs;
9980
9981 static void
9982 md_obj_begin ()
9983 {
9984   obstack_begin (&proc_frags, 0x2000);
9985 }
9986
9987 static void
9988 md_obj_end ()
9989 {
9990   /* check for premature end, nesting errors, etc */
9991   if (proc_lastP && proc_lastP->proc_end == NULL)
9992     as_warn ("missing `.end' at end of assembly");
9993 }
9994
9995 static long
9996 get_number ()
9997 {
9998   int negative = 0;
9999   long val = 0;
10000
10001   if (*input_line_pointer == '-')
10002     {
10003       ++input_line_pointer;
10004       negative = 1;
10005     }
10006   if (!isdigit (*input_line_pointer))
10007     as_bad ("Expected simple number.");
10008   if (input_line_pointer[0] == '0')
10009     {
10010       if (input_line_pointer[1] == 'x')
10011         {
10012           input_line_pointer += 2;
10013           while (isxdigit (*input_line_pointer))
10014             {
10015               val <<= 4;
10016               val |= hex_value (*input_line_pointer++);
10017             }
10018           return negative ? -val : val;
10019         }
10020       else
10021         {
10022           ++input_line_pointer;
10023           while (isdigit (*input_line_pointer))
10024             {
10025               val <<= 3;
10026               val |= *input_line_pointer++ - '0';
10027             }
10028           return negative ? -val : val;
10029         }
10030     }
10031   if (!isdigit (*input_line_pointer))
10032     {
10033       printf (" *input_line_pointer == '%c' 0x%02x\n",
10034               *input_line_pointer, *input_line_pointer);
10035       as_warn ("Invalid number");
10036       return -1;
10037     }
10038   while (isdigit (*input_line_pointer))
10039     {
10040       val *= 10;
10041       val += *input_line_pointer++ - '0';
10042     }
10043   return negative ? -val : val;
10044 }
10045
10046 /* The .file directive; just like the usual .file directive, but there
10047    is an initial number which is the ECOFF file index.  */
10048
10049 static void
10050 s_file (x)
10051      int x;
10052 {
10053   int line;
10054
10055   line = get_number ();
10056   s_app_file (0);
10057 }
10058
10059
10060 /* The .end directive.  */
10061
10062 static void
10063 s_mipsend (x)
10064      int x;
10065 {
10066   symbolS *p;
10067
10068   if (!is_end_of_line[(unsigned char) *input_line_pointer])
10069     {
10070       p = get_symbol ();
10071       demand_empty_rest_of_line ();
10072     }
10073   else
10074     p = NULL;
10075   if (now_seg != text_section)
10076     as_warn (".end not in text section");
10077   if (!proc_lastP)
10078     {
10079       as_warn (".end and no .ent seen yet.");
10080       return;
10081     }
10082
10083   if (p != NULL)
10084     {
10085       assert (S_GET_NAME (p));
10086       if (strcmp (S_GET_NAME (p), S_GET_NAME (proc_lastP->proc_isym)))
10087         as_warn (".end symbol does not match .ent symbol.");
10088     }
10089
10090   proc_lastP->proc_end = (symbolS *) 1;
10091 }
10092
10093 /* The .aent and .ent directives.  */
10094
10095 static void
10096 s_ent (aent)
10097      int aent;
10098 {
10099   int number = 0;
10100   procS *procP;
10101   symbolS *symbolP;
10102
10103   symbolP = get_symbol ();
10104   if (*input_line_pointer == ',')
10105     input_line_pointer++;
10106   SKIP_WHITESPACE ();
10107   if (isdigit (*input_line_pointer) || *input_line_pointer == '-')
10108     number = get_number ();
10109   if (now_seg != text_section)
10110     as_warn (".ent or .aent not in text section.");
10111
10112   if (!aent && proc_lastP && proc_lastP->proc_end == NULL)
10113     as_warn ("missing `.end'");
10114
10115   if (!aent)
10116     {
10117       procP = (procS *) obstack_alloc (&proc_frags, sizeof (*procP));
10118       procP->proc_isym = symbolP;
10119       procP->proc_reg_mask = 0;
10120       procP->proc_reg_offset = 0;
10121       procP->proc_fpreg_mask = 0;
10122       procP->proc_fpreg_offset = 0;
10123       procP->proc_frameoffset = 0;
10124       procP->proc_framereg = 0;
10125       procP->proc_pcreg = 0;
10126       procP->proc_end = NULL;
10127       procP->proc_next = NULL;
10128       if (proc_lastP)
10129         proc_lastP->proc_next = procP;
10130       else
10131         proc_rootP = procP;
10132       proc_lastP = procP;
10133       numprocs++;
10134     }
10135   demand_empty_rest_of_line ();
10136 }
10137
10138 /* The .frame directive.  */
10139
10140 #if 0
10141 static void
10142 s_frame (x)
10143      int x;
10144 {
10145   char str[100];
10146   symbolS *symP;
10147   int frame_reg;
10148   int frame_off;
10149   int pcreg;
10150
10151   frame_reg = tc_get_register (1);
10152   if (*input_line_pointer == ',')
10153     input_line_pointer++;
10154   frame_off = get_absolute_expression ();
10155   if (*input_line_pointer == ',')
10156     input_line_pointer++;
10157   pcreg = tc_get_register (0);
10158
10159   /* bob third eye */
10160   assert (proc_rootP);
10161   proc_rootP->proc_framereg = frame_reg;
10162   proc_rootP->proc_frameoffset = frame_off;
10163   proc_rootP->proc_pcreg = pcreg;
10164   /* bob macho .frame */
10165
10166   /* We don't have to write out a frame stab for unoptimized code. */
10167   if (!(frame_reg == FP && frame_off == 0))
10168     {
10169       if (!proc_lastP)
10170         as_warn ("No .ent for .frame to use.");
10171       (void) sprintf (str, "R%d;%d", frame_reg, frame_off);
10172       symP = symbol_new (str, N_VFP, 0, frag_now);
10173       S_SET_TYPE (symP, N_RMASK);
10174       S_SET_OTHER (symP, 0);
10175       S_SET_DESC (symP, 0);
10176       symP->sy_forward = proc_lastP->proc_isym;
10177       /* bob perhaps I should have used pseudo set */
10178     }
10179   demand_empty_rest_of_line ();
10180 }
10181 #endif
10182
10183 /* The .fmask and .mask directives.  */
10184
10185 #if 0
10186 static void
10187 s_mask (reg_type)
10188      char reg_type;
10189 {
10190   char str[100], *strP;
10191   symbolS *symP;
10192   int i;
10193   unsigned int mask;
10194   int off;
10195
10196   mask = get_number ();
10197   if (*input_line_pointer == ',')
10198     input_line_pointer++;
10199   off = get_absolute_expression ();
10200
10201   /* bob only for coff */
10202   assert (proc_rootP);
10203   if (reg_type == 'F')
10204     {
10205       proc_rootP->proc_fpreg_mask = mask;
10206       proc_rootP->proc_fpreg_offset = off;
10207     }
10208   else
10209     {
10210       proc_rootP->proc_reg_mask = mask;
10211       proc_rootP->proc_reg_offset = off;
10212     }
10213
10214   /* bob macho .mask + .fmask */
10215
10216   /* We don't have to write out a mask stab if no saved regs. */
10217   if (!(mask == 0))
10218     {
10219       if (!proc_lastP)
10220         as_warn ("No .ent for .mask to use.");
10221       strP = str;
10222       for (i = 0; i < 32; i++)
10223         {
10224           if (mask % 2)
10225             {
10226               sprintf (strP, "%c%d,", reg_type, i);
10227               strP += strlen (strP);
10228             }
10229           mask /= 2;
10230         }
10231       sprintf (strP, ";%d,", off);
10232       symP = symbol_new (str, N_RMASK, 0, frag_now);
10233       S_SET_TYPE (symP, N_RMASK);
10234       S_SET_OTHER (symP, 0);
10235       S_SET_DESC (symP, 0);
10236       symP->sy_forward = proc_lastP->proc_isym;
10237       /* bob perhaps I should have used pseudo set */
10238     }
10239 }
10240 #endif
10241
10242 /* The .loc directive.  */
10243
10244 #if 0
10245 static void
10246 s_loc (x)
10247      int x;
10248 {
10249   symbolS *symbolP;
10250   int lineno;
10251   int addroff;
10252
10253   assert (now_seg == text_section);
10254
10255   lineno = get_number ();
10256   addroff = frag_now_fix ();
10257
10258   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
10259   S_SET_TYPE (symbolP, N_SLINE);
10260   S_SET_OTHER (symbolP, 0);
10261   S_SET_DESC (symbolP, lineno);
10262   symbolP->sy_segment = now_seg;
10263 }
10264 #endif
This page took 0.674784 seconds and 4 git commands to generate.