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