[ gas/ChangeLog ]
[platform/upstream/binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4    Contributed by the OSF and Ralph Campbell.
5    Written by Keith Knowles and Ralph Campbell, working independently.
6    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7    Support.
8
9    This file is part of GAS.
10
11    GAS is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2, or (at your option)
14    any later version.
15
16    GAS is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GAS; see the file COPYING.  If not, write to the Free
23    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.  */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-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 #include "dwarf2dbg.h"
41
42 #ifdef DEBUG
43 #define DBG(x) printf x
44 #else
45 #define DBG(x)
46 #endif
47
48 #ifdef OBJ_MAYBE_ELF
49 /* Clean up namespace so we can include obj-elf.h too.  */
50 static int mips_output_flavor PARAMS ((void));
51 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52 #undef OBJ_PROCESS_STAB
53 #undef OUTPUT_FLAVOR
54 #undef S_GET_ALIGN
55 #undef S_GET_SIZE
56 #undef S_SET_ALIGN
57 #undef S_SET_SIZE
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 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65 #include "obj-elf.h"
66 /* Fix any of them that we actually care about.  */
67 #undef OUTPUT_FLAVOR
68 #define OUTPUT_FLAVOR mips_output_flavor()
69 #endif
70
71 #if defined (OBJ_ELF)
72 #include "elf/mips.h"
73 #endif
74
75 #ifndef ECOFF_DEBUGGING
76 #define NO_ECOFF_DEBUGGING
77 #define ECOFF_DEBUGGING 0
78 #endif
79
80 int mips_flag_mdebug = -1;
81
82 #include "ecoff.h"
83
84 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
85 static char *mips_regmask_frag;
86 #endif
87
88 #define ZERO 0
89 #define AT  1
90 #define TREG 24
91 #define PIC_CALL_REG 25
92 #define KT0 26
93 #define KT1 27
94 #define GP  28
95 #define SP  29
96 #define FP  30
97 #define RA  31
98
99 #define ILLEGAL_REG (32)
100
101 /* Allow override of standard little-endian ECOFF format.  */
102
103 #ifndef ECOFF_LITTLE_FORMAT
104 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
105 #endif
106
107 extern int target_big_endian;
108
109 /* The name of the readonly data section.  */
110 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
111                             ? ".data" \
112                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
113                             ? ".rdata" \
114                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
115                             ? ".rdata" \
116                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117                             ? ".rodata" \
118                             : (abort (), ""))
119
120 /* The ABI to use.  */
121 enum mips_abi_level
122 {
123   NO_ABI = 0,
124   O32_ABI,
125   O64_ABI,
126   N32_ABI,
127   N64_ABI,
128   EABI_ABI
129 };
130
131 /* MIPS ABI we are using for this output file.  */
132 static enum mips_abi_level mips_abi = NO_ABI;
133
134 /* This is the set of options which may be modified by the .set
135    pseudo-op.  We use a struct so that .set push and .set pop are more
136    reliable.  */
137
138 struct mips_set_options
139 {
140   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
141      if it has not been initialized.  Changed by `.set mipsN', and the
142      -mipsN command line option, and the default CPU.  */
143   int isa;
144   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
145      if they have not been initialized.  Changed by `.set <asename>', by
146      command line options, and based on the default architecture.  */
147   int ase_mips3d;
148   int ase_mdmx;
149   /* Whether we are assembling for the mips16 processor.  0 if we are
150      not, 1 if we are, and -1 if the value has not been initialized.
151      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
152      -nomips16 command line options, and the default CPU.  */
153   int mips16;
154   /* Non-zero if we should not reorder instructions.  Changed by `.set
155      reorder' and `.set noreorder'.  */
156   int noreorder;
157   /* Non-zero if we should not permit the $at ($1) register to be used
158      in instructions.  Changed by `.set at' and `.set noat'.  */
159   int noat;
160   /* Non-zero if we should warn when a macro instruction expands into
161      more than one machine instruction.  Changed by `.set nomacro' and
162      `.set macro'.  */
163   int warn_about_macros;
164   /* Non-zero if we should not move instructions.  Changed by `.set
165      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
166   int nomove;
167   /* Non-zero if we should not optimize branches by moving the target
168      of the branch into the delay slot.  Actually, we don't perform
169      this optimization anyhow.  Changed by `.set bopt' and `.set
170      nobopt'.  */
171   int nobopt;
172   /* Non-zero if we should not autoextend mips16 instructions.
173      Changed by `.set autoextend' and `.set noautoextend'.  */
174   int noautoextend;
175   /* Restrict general purpose registers and floating point registers
176      to 32 bit.  This is initially determined when -mgp32 or -mfp32
177      is passed but can changed if the assembler code uses .set mipsN.  */
178   int gp32;
179   int fp32;
180 };
181
182 /* True if -mgp32 was passed.  */
183 static int file_mips_gp32 = -1;
184
185 /* True if -mfp32 was passed.  */
186 static int file_mips_fp32 = -1;
187
188 /* This is the struct we use to hold the current set of options.  Note
189    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
190    -1 to indicate that they have not been initialized.  */
191
192 static struct mips_set_options mips_opts =
193 {
194   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
195 };
196
197 /* These variables are filled in with the masks of registers used.
198    The object format code reads them and puts them in the appropriate
199    place.  */
200 unsigned long mips_gprmask;
201 unsigned long mips_cprmask[4];
202
203 /* MIPS ISA we are using for this output file.  */
204 static int file_mips_isa = ISA_UNKNOWN;
205
206 /* True if -mips16 was passed or implied by arguments passed on the
207    command line (e.g., by -march).  */
208 static int file_ase_mips16;
209
210 /* True if -mips3d was passed or implied by arguments passed on the
211    command line (e.g., by -march).  */
212 static int file_ase_mips3d;
213
214 /* True if -mdmx was passed or implied by arguments passed on the
215    command line (e.g., by -march).  */
216 static int file_ase_mdmx;
217
218 /* The argument of the -march= flag.  The architecture we are assembling.  */
219 static int mips_arch = CPU_UNKNOWN;
220 static const char *mips_arch_string;
221 static const struct mips_cpu_info *mips_arch_info;
222
223 /* The argument of the -mtune= flag.  The architecture for which we
224    are optimizing.  */
225 static int mips_tune = CPU_UNKNOWN;
226 static const char *mips_tune_string;
227 static const struct mips_cpu_info *mips_tune_info;
228
229 /* True when generating 32-bit code for a 64-bit processor.  */
230 static int mips_32bitmode = 0;
231
232 /* Some ISA's have delay slots for instructions which read or write
233    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
234    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
235    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
236    delay slot in this ISA.  The uses of this macro assume that any
237    ISA that has delay slots for one of these, has them for all.  They
238    also assume that ISAs which don't have delays for these insns, don't
239    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
240 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
241    (ISA) == ISA_MIPS1                       \
242    || (ISA) == ISA_MIPS2                    \
243    || (ISA) == ISA_MIPS3                    \
244    )
245
246 /* True if the given ABI requires 32-bit registers.  */
247 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
248
249 /* Likewise 64-bit registers.  */
250 #define ABI_NEEDS_64BIT_REGS(ABI) \
251   ((ABI) == N32_ABI               \
252    || (ABI) == N64_ABI            \
253    || (ABI) == O64_ABI)
254
255 /*  Return true if ISA supports 64 bit gp register instructions.  */
256 #define ISA_HAS_64BIT_REGS(ISA) (    \
257    (ISA) == ISA_MIPS3                \
258    || (ISA) == ISA_MIPS4             \
259    || (ISA) == ISA_MIPS5             \
260    || (ISA) == ISA_MIPS64            \
261    )
262
263 #define HAVE_32BIT_GPRS                            \
264     (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
265
266 #define HAVE_32BIT_FPRS                            \
267     (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
268
269 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
270 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
271
272 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
273
274 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
275
276 /* We can only have 64bit addresses if the object file format
277    supports it.  */
278 #define HAVE_32BIT_ADDRESSES                           \
279    (HAVE_32BIT_GPRS                                    \
280     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
281          || ! HAVE_64BIT_OBJECTS)                      \
282         && mips_pic != EMBEDDED_PIC))
283
284 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
285
286 /* Return true if the given CPU supports the MIPS16 ASE.  */
287 #define CPU_HAS_MIPS16(cpu)                            \
288    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0)
289
290 /* Return true if the given CPU supports the MIPS3D ASE.  */
291 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
292                                  )
293
294 /* Return true if the given CPU supports the MDMX ASE.  */
295 #define CPU_HAS_MDMX(cpu)       (false                 \
296                                  )
297
298 /* Whether the processor uses hardware interlocks to protect
299    reads from the HI and LO registers, and thus does not
300    require nops to be inserted.  */
301
302 #define hilo_interlocks (mips_arch == CPU_R4010                       \
303                          || mips_arch == CPU_SB1                      \
304                          )
305
306 /* Whether the processor uses hardware interlocks to protect reads
307    from the GPRs, and thus does not require nops to be inserted.  */
308 #define gpr_interlocks \
309   (mips_opts.isa != ISA_MIPS1  \
310    || mips_arch == CPU_R3900)
311
312 /* As with other "interlocks" this is used by hardware that has FP
313    (co-processor) interlocks.  */
314 /* Itbl support may require additional care here.  */
315 #define cop_interlocks (mips_arch == CPU_R4300                        \
316                         || mips_arch == CPU_SB1                       \
317                         )
318
319 /* Is this a mfhi or mflo instruction?  */
320 #define MF_HILO_INSN(PINFO) \
321           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
322
323 /* MIPS PIC level.  */
324
325 enum mips_pic_level mips_pic;
326
327 /* Warn about all NOPS that the assembler generates.  */
328 static int warn_nops = 0;
329
330 /* 1 if we should generate 32 bit offsets from the $gp register in
331    SVR4_PIC mode.  Currently has no meaning in other modes.  */
332 static int mips_big_got = 0;
333
334 /* 1 if trap instructions should used for overflow rather than break
335    instructions.  */
336 static int mips_trap = 0;
337
338 /* 1 if double width floating point constants should not be constructed
339    by assembling two single width halves into two single width floating
340    point registers which just happen to alias the double width destination
341    register.  On some architectures this aliasing can be disabled by a bit
342    in the status register, and the setting of this bit cannot be determined
343    automatically at assemble time.  */
344 static int mips_disable_float_construction;
345
346 /* Non-zero if any .set noreorder directives were used.  */
347
348 static int mips_any_noreorder;
349
350 /* Non-zero if nops should be inserted when the register referenced in
351    an mfhi/mflo instruction is read in the next two instructions.  */
352 static int mips_7000_hilo_fix;
353
354 /* The size of the small data section.  */
355 static unsigned int g_switch_value = 8;
356 /* Whether the -G option was used.  */
357 static int g_switch_seen = 0;
358
359 #define N_RMASK 0xc4
360 #define N_VFP   0xd4
361
362 /* If we can determine in advance that GP optimization won't be
363    possible, we can skip the relaxation stuff that tries to produce
364    GP-relative references.  This makes delay slot optimization work
365    better.
366
367    This function can only provide a guess, but it seems to work for
368    gcc output.  It needs to guess right for gcc, otherwise gcc
369    will put what it thinks is a GP-relative instruction in a branch
370    delay slot.
371
372    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
373    fixed it for the non-PIC mode.  KR 95/04/07  */
374 static int nopic_need_relax PARAMS ((symbolS *, int));
375
376 /* handle of the OPCODE hash table */
377 static struct hash_control *op_hash = NULL;
378
379 /* The opcode hash table we use for the mips16.  */
380 static struct hash_control *mips16_op_hash = NULL;
381
382 /* This array holds the chars that always start a comment.  If the
383     pre-processor is disabled, these aren't very useful */
384 const char comment_chars[] = "#";
385
386 /* This array holds the chars that only start a comment at the beginning of
387    a line.  If the line seems to have the form '# 123 filename'
388    .line and .file directives will appear in the pre-processed output */
389 /* Note that input_file.c hand checks for '#' at the beginning of the
390    first line of the input file.  This is because the compiler outputs
391    #NO_APP at the beginning of its output.  */
392 /* Also note that C style comments are always supported.  */
393 const char line_comment_chars[] = "#";
394
395 /* This array holds machine specific line separator characters.  */
396 const char line_separator_chars[] = ";";
397
398 /* Chars that can be used to separate mant from exp in floating point nums */
399 const char EXP_CHARS[] = "eE";
400
401 /* Chars that mean this number is a floating point constant */
402 /* As in 0f12.456 */
403 /* or    0d1.2345e12 */
404 const char FLT_CHARS[] = "rRsSfFdDxXpP";
405
406 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
407    changed in read.c .  Ideally it shouldn't have to know about it at all,
408    but nothing is ideal around here.
409  */
410
411 static char *insn_error;
412
413 static int auto_align = 1;
414
415 /* When outputting SVR4 PIC code, the assembler needs to know the
416    offset in the stack frame from which to restore the $gp register.
417    This is set by the .cprestore pseudo-op, and saved in this
418    variable.  */
419 static offsetT mips_cprestore_offset = -1;
420
421 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
422    more optimizations, it can use a register value instead of a memory-saved
423    offset and even an other register than $gp as global pointer.  */
424 static offsetT mips_cpreturn_offset = -1;
425 static int mips_cpreturn_register = -1;
426 static int mips_gp_register = GP;
427 static int mips_gprel_offset = 0;
428
429 /* Whether mips_cprestore_offset has been set in the current function
430    (or whether it has already been warned about, if not).  */
431 static int mips_cprestore_valid = 0;
432
433 /* This is the register which holds the stack frame, as set by the
434    .frame pseudo-op.  This is needed to implement .cprestore.  */
435 static int mips_frame_reg = SP;
436
437 /* Whether mips_frame_reg has been set in the current function
438    (or whether it has already been warned about, if not).  */
439 static int mips_frame_reg_valid = 0;
440
441 /* To output NOP instructions correctly, we need to keep information
442    about the previous two instructions.  */
443
444 /* Whether we are optimizing.  The default value of 2 means to remove
445    unneeded NOPs and swap branch instructions when possible.  A value
446    of 1 means to not swap branches.  A value of 0 means to always
447    insert NOPs.  */
448 static int mips_optimize = 2;
449
450 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
451    equivalent to seeing no -g option at all.  */
452 static int mips_debug = 0;
453
454 /* The previous instruction.  */
455 static struct mips_cl_insn prev_insn;
456
457 /* The instruction before prev_insn.  */
458 static struct mips_cl_insn prev_prev_insn;
459
460 /* If we don't want information for prev_insn or prev_prev_insn, we
461    point the insn_mo field at this dummy integer.  */
462 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
463
464 /* Non-zero if prev_insn is valid.  */
465 static int prev_insn_valid;
466
467 /* The frag for the previous instruction.  */
468 static struct frag *prev_insn_frag;
469
470 /* The offset into prev_insn_frag for the previous instruction.  */
471 static long prev_insn_where;
472
473 /* The reloc type for the previous instruction, if any.  */
474 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
475
476 /* The reloc for the previous instruction, if any.  */
477 static fixS *prev_insn_fixp[3];
478
479 /* Non-zero if the previous instruction was in a delay slot.  */
480 static int prev_insn_is_delay_slot;
481
482 /* Non-zero if the previous instruction was in a .set noreorder.  */
483 static int prev_insn_unreordered;
484
485 /* Non-zero if the previous instruction uses an extend opcode (if
486    mips16).  */
487 static int prev_insn_extended;
488
489 /* Non-zero if the previous previous instruction was in a .set
490    noreorder.  */
491 static int prev_prev_insn_unreordered;
492
493 /* If this is set, it points to a frag holding nop instructions which
494    were inserted before the start of a noreorder section.  If those
495    nops turn out to be unnecessary, the size of the frag can be
496    decreased.  */
497 static fragS *prev_nop_frag;
498
499 /* The number of nop instructions we created in prev_nop_frag.  */
500 static int prev_nop_frag_holds;
501
502 /* The number of nop instructions that we know we need in
503    prev_nop_frag.  */
504 static int prev_nop_frag_required;
505
506 /* The number of instructions we've seen since prev_nop_frag.  */
507 static int prev_nop_frag_since;
508
509 /* For ECOFF and ELF, relocations against symbols are done in two
510    parts, with a HI relocation and a LO relocation.  Each relocation
511    has only 16 bits of space to store an addend.  This means that in
512    order for the linker to handle carries correctly, it must be able
513    to locate both the HI and the LO relocation.  This means that the
514    relocations must appear in order in the relocation table.
515
516    In order to implement this, we keep track of each unmatched HI
517    relocation.  We then sort them so that they immediately precede the
518    corresponding LO relocation.  */
519
520 struct mips_hi_fixup
521 {
522   /* Next HI fixup.  */
523   struct mips_hi_fixup *next;
524   /* This fixup.  */
525   fixS *fixp;
526   /* The section this fixup is in.  */
527   segT seg;
528 };
529
530 /* The list of unmatched HI relocs.  */
531
532 static struct mips_hi_fixup *mips_hi_fixup_list;
533
534 /* Map normal MIPS register numbers to mips16 register numbers.  */
535
536 #define X ILLEGAL_REG
537 static const int mips32_to_16_reg_map[] =
538 {
539   X, X, 2, 3, 4, 5, 6, 7,
540   X, X, X, X, X, X, X, X,
541   0, 1, X, X, X, X, X, X,
542   X, X, X, X, X, X, X, X
543 };
544 #undef X
545
546 /* Map mips16 register numbers to normal MIPS register numbers.  */
547
548 static const unsigned int mips16_to_32_reg_map[] =
549 {
550   16, 17, 2, 3, 4, 5, 6, 7
551 };
552 \f
553 /* Since the MIPS does not have multiple forms of PC relative
554    instructions, we do not have to do relaxing as is done on other
555    platforms.  However, we do have to handle GP relative addressing
556    correctly, which turns out to be a similar problem.
557
558    Every macro that refers to a symbol can occur in (at least) two
559    forms, one with GP relative addressing and one without.  For
560    example, loading a global variable into a register generally uses
561    a macro instruction like this:
562      lw $4,i
563    If i can be addressed off the GP register (this is true if it is in
564    the .sbss or .sdata section, or if it is known to be smaller than
565    the -G argument) this will generate the following instruction:
566      lw $4,i($gp)
567    This instruction will use a GPREL reloc.  If i can not be addressed
568    off the GP register, the following instruction sequence will be used:
569      lui $at,i
570      lw $4,i($at)
571    In this case the first instruction will have a HI16 reloc, and the
572    second reloc will have a LO16 reloc.  Both relocs will be against
573    the symbol i.
574
575    The issue here is that we may not know whether i is GP addressable
576    until after we see the instruction that uses it.  Therefore, we
577    want to be able to choose the final instruction sequence only at
578    the end of the assembly.  This is similar to the way other
579    platforms choose the size of a PC relative instruction only at the
580    end of assembly.
581
582    When generating position independent code we do not use GP
583    addressing in quite the same way, but the issue still arises as
584    external symbols and local symbols must be handled differently.
585
586    We handle these issues by actually generating both possible
587    instruction sequences.  The longer one is put in a frag_var with
588    type rs_machine_dependent.  We encode what to do with the frag in
589    the subtype field.  We encode (1) the number of existing bytes to
590    replace, (2) the number of new bytes to use, (3) the offset from
591    the start of the existing bytes to the first reloc we must generate
592    (that is, the offset is applied from the start of the existing
593    bytes after they are replaced by the new bytes, if any), (4) the
594    offset from the start of the existing bytes to the second reloc,
595    (5) whether a third reloc is needed (the third reloc is always four
596    bytes after the second reloc), and (6) whether to warn if this
597    variant is used (this is sometimes needed if .set nomacro or .set
598    noat is in effect).  All these numbers are reasonably small.
599
600    Generating two instruction sequences must be handled carefully to
601    ensure that delay slots are handled correctly.  Fortunately, there
602    are a limited number of cases.  When the second instruction
603    sequence is generated, append_insn is directed to maintain the
604    existing delay slot information, so it continues to apply to any
605    code after the second instruction sequence.  This means that the
606    second instruction sequence must not impose any requirements not
607    required by the first instruction sequence.
608
609    These variant frags are then handled in functions called by the
610    machine independent code.  md_estimate_size_before_relax returns
611    the final size of the frag.  md_convert_frag sets up the final form
612    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
613    one if needed.  */
614 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
615   ((relax_substateT) \
616    (((old) << 23) \
617     | ((new) << 16) \
618     | (((reloc1) + 64) << 9) \
619     | (((reloc2) + 64) << 2) \
620     | ((reloc3) ? (1 << 1) : 0) \
621     | ((warn) ? 1 : 0)))
622 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
623 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
624 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
625 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
626 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
627 #define RELAX_WARN(i) ((i) & 1)
628
629 /* For mips16 code, we use an entirely different form of relaxation.
630    mips16 supports two versions of most instructions which take
631    immediate values: a small one which takes some small value, and a
632    larger one which takes a 16 bit value.  Since branches also follow
633    this pattern, relaxing these values is required.
634
635    We can assemble both mips16 and normal MIPS code in a single
636    object.  Therefore, we need to support this type of relaxation at
637    the same time that we support the relaxation described above.  We
638    use the high bit of the subtype field to distinguish these cases.
639
640    The information we store for this type of relaxation is the
641    argument code found in the opcode file for this relocation, whether
642    the user explicitly requested a small or extended form, and whether
643    the relocation is in a jump or jal delay slot.  That tells us the
644    size of the value, and how it should be stored.  We also store
645    whether the fragment is considered to be extended or not.  We also
646    store whether this is known to be a branch to a different section,
647    whether we have tried to relax this frag yet, and whether we have
648    ever extended a PC relative fragment because of a shift count.  */
649 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
650   (0x80000000                                                   \
651    | ((type) & 0xff)                                            \
652    | ((small) ? 0x100 : 0)                                      \
653    | ((ext) ? 0x200 : 0)                                        \
654    | ((dslot) ? 0x400 : 0)                                      \
655    | ((jal_dslot) ? 0x800 : 0))
656 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
657 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
658 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
659 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
660 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
661 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
662 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
663 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
664 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
665 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
666 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
667 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
668
669 /* Is the given value a sign-extended 32-bit value?  */
670 #define IS_SEXT_32BIT_NUM(x)                                            \
671   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
672    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
673
674 /* Is the given value a sign-extended 16-bit value?  */
675 #define IS_SEXT_16BIT_NUM(x)                                            \
676   (((x) &~ (offsetT) 0x7fff) == 0                                       \
677    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
678
679 \f
680 /* Prototypes for static functions.  */
681
682 #ifdef __STDC__
683 #define internalError() \
684     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
685 #else
686 #define internalError() as_fatal (_("MIPS internal Error"));
687 #endif
688
689 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
690
691 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
692                                   unsigned int reg, enum mips_regclass class));
693 static int reg_needs_delay PARAMS ((unsigned int));
694 static void mips16_mark_labels PARAMS ((void));
695 static void append_insn PARAMS ((char *place,
696                                  struct mips_cl_insn * ip,
697                                  expressionS * p,
698                                  bfd_reloc_code_real_type *r,
699                                  boolean));
700 static void mips_no_prev_insn PARAMS ((int));
701 static void mips_emit_delays PARAMS ((boolean));
702 #ifdef USE_STDARG
703 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
704                                  const char *name, const char *fmt,
705                                  ...));
706 #else
707 static void macro_build ();
708 #endif
709 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
710                                         const char *, const char *,
711                                         va_list));
712 static void macro_build_jalr PARAMS ((int, expressionS *));
713 static void macro_build_lui PARAMS ((char *place, int *counter,
714                                      expressionS * ep, int regnum));
715 static void macro_build_ldst_constoffset PARAMS ((char *place, int *counter,
716                                                   expressionS * ep, const char *op,
717                                                   int valreg, int breg));
718 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
719 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
720                                          expressionS *));
721 static void load_register PARAMS ((int *, int, expressionS *, int));
722 static void load_address PARAMS ((int *, int, expressionS *, int *));
723 static void move_register PARAMS ((int *, int, int));
724 static void macro PARAMS ((struct mips_cl_insn * ip));
725 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
726 #ifdef LOSING_COMPILER
727 static void macro2 PARAMS ((struct mips_cl_insn * ip));
728 #endif
729 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
730 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
731 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
732                                   boolean, boolean, unsigned long *,
733                                   boolean *, unsigned short *));
734 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
735 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
736 static int my_getSmallExpression PARAMS ((expressionS *, char *));
737 static void my_getExpression PARAMS ((expressionS *, char *));
738 #ifdef OBJ_ELF
739 static int support_64bit_objects PARAMS((void));
740 #endif
741 static void mips_set_option_string PARAMS ((const char **, const char *));
742 static symbolS *get_symbol PARAMS ((void));
743 static void mips_align PARAMS ((int to, int fill, symbolS *label));
744 static void s_align PARAMS ((int));
745 static void s_change_sec PARAMS ((int));
746 static void s_change_section PARAMS ((int));
747 static void s_cons PARAMS ((int));
748 static void s_float_cons PARAMS ((int));
749 static void s_mips_globl PARAMS ((int));
750 static void s_option PARAMS ((int));
751 static void s_mipsset PARAMS ((int));
752 static void s_abicalls PARAMS ((int));
753 static void s_cpload PARAMS ((int));
754 static void s_cpsetup PARAMS ((int));
755 static void s_cplocal PARAMS ((int));
756 static void s_cprestore PARAMS ((int));
757 static void s_cpreturn PARAMS ((int));
758 static void s_gpvalue PARAMS ((int));
759 static void s_gpword PARAMS ((int));
760 static void s_cpadd PARAMS ((int));
761 static void s_insn PARAMS ((int));
762 static void md_obj_begin PARAMS ((void));
763 static void md_obj_end PARAMS ((void));
764 static long get_number PARAMS ((void));
765 static void s_mips_ent PARAMS ((int));
766 static void s_mips_end PARAMS ((int));
767 static void s_mips_frame PARAMS ((int));
768 static void s_mips_mask PARAMS ((int));
769 static void s_mips_stab PARAMS ((int));
770 static void s_mips_weakext PARAMS ((int));
771 static void s_mips_file PARAMS ((int));
772 static void s_mips_loc PARAMS ((int));
773 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
774 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
775 static void show PARAMS ((FILE *, const char *, int *, int *));
776 #ifdef OBJ_ELF
777 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
778 #endif
779
780 /* Return values of my_getSmallExpression().  */
781
782 enum small_ex_type
783 {
784   S_EX_NONE = 0,
785   S_EX_REGISTER,
786
787   /* Direct relocation creation by %percent_op().  */
788   S_EX_HALF,
789   S_EX_HI,
790   S_EX_LO,
791   S_EX_GP_REL,
792   S_EX_GOT,
793   S_EX_CALL16,
794   S_EX_GOT_DISP,
795   S_EX_GOT_PAGE,
796   S_EX_GOT_OFST,
797   S_EX_GOT_HI,
798   S_EX_GOT_LO,
799   S_EX_NEG,
800   S_EX_HIGHER,
801   S_EX_HIGHEST,
802   S_EX_CALL_HI,
803   S_EX_CALL_LO
804 };
805
806 /* Table and functions used to map between CPU/ISA names, and
807    ISA levels, and CPU numbers.  */
808
809 struct mips_cpu_info
810 {
811   const char *name;           /* CPU or ISA name.  */
812   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
813   int isa;                    /* ISA level.  */
814   int cpu;                    /* CPU number (default CPU if ISA).  */
815 };
816
817 static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
818 static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
819 static boolean mips_strict_matching_cpu_name_p PARAMS ((const char *,
820                                                         const char *));
821 static boolean mips_matching_cpu_name_p PARAMS ((const char *, const char *));
822 static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
823                                                            const char *));
824 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
825 \f
826 /* Pseudo-op table.
827
828    The following pseudo-ops from the Kane and Heinrich MIPS book
829    should be defined here, but are currently unsupported: .alias,
830    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
831
832    The following pseudo-ops from the Kane and Heinrich MIPS book are
833    specific to the type of debugging information being generated, and
834    should be defined by the object format: .aent, .begin, .bend,
835    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
836    .vreg.
837
838    The following pseudo-ops from the Kane and Heinrich MIPS book are
839    not MIPS CPU specific, but are also not specific to the object file
840    format.  This file is probably the best place to define them, but
841    they are not currently supported: .asm0, .endr, .lab, .repeat,
842    .struct.  */
843
844 static const pseudo_typeS mips_pseudo_table[] =
845 {
846   /* MIPS specific pseudo-ops.  */
847   {"option", s_option, 0},
848   {"set", s_mipsset, 0},
849   {"rdata", s_change_sec, 'r'},
850   {"sdata", s_change_sec, 's'},
851   {"livereg", s_ignore, 0},
852   {"abicalls", s_abicalls, 0},
853   {"cpload", s_cpload, 0},
854   {"cpsetup", s_cpsetup, 0},
855   {"cplocal", s_cplocal, 0},
856   {"cprestore", s_cprestore, 0},
857   {"cpreturn", s_cpreturn, 0},
858   {"gpvalue", s_gpvalue, 0},
859   {"gpword", s_gpword, 0},
860   {"cpadd", s_cpadd, 0},
861   {"insn", s_insn, 0},
862
863   /* Relatively generic pseudo-ops that happen to be used on MIPS
864      chips.  */
865   {"asciiz", stringer, 1},
866   {"bss", s_change_sec, 'b'},
867   {"err", s_err, 0},
868   {"half", s_cons, 1},
869   {"dword", s_cons, 3},
870   {"weakext", s_mips_weakext, 0},
871
872   /* These pseudo-ops are defined in read.c, but must be overridden
873      here for one reason or another.  */
874   {"align", s_align, 0},
875   {"byte", s_cons, 0},
876   {"data", s_change_sec, 'd'},
877   {"double", s_float_cons, 'd'},
878   {"float", s_float_cons, 'f'},
879   {"globl", s_mips_globl, 0},
880   {"global", s_mips_globl, 0},
881   {"hword", s_cons, 1},
882   {"int", s_cons, 2},
883   {"long", s_cons, 2},
884   {"octa", s_cons, 4},
885   {"quad", s_cons, 3},
886   {"section", s_change_section, 0},
887   {"short", s_cons, 1},
888   {"single", s_float_cons, 'f'},
889   {"stabn", s_mips_stab, 'n'},
890   {"text", s_change_sec, 't'},
891   {"word", s_cons, 2},
892
893   { "extern", ecoff_directive_extern, 0},
894
895   { NULL, NULL, 0 },
896 };
897
898 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
899 {
900   /* These pseudo-ops should be defined by the object file format.
901      However, a.out doesn't support them, so we have versions here.  */
902   {"aent", s_mips_ent, 1},
903   {"bgnb", s_ignore, 0},
904   {"end", s_mips_end, 0},
905   {"endb", s_ignore, 0},
906   {"ent", s_mips_ent, 0},
907   {"file", s_mips_file, 0},
908   {"fmask", s_mips_mask, 'F'},
909   {"frame", s_mips_frame, 0},
910   {"loc", s_mips_loc, 0},
911   {"mask", s_mips_mask, 'R'},
912   {"verstamp", s_ignore, 0},
913   { NULL, NULL, 0 },
914 };
915
916 extern void pop_insert PARAMS ((const pseudo_typeS *));
917
918 void
919 mips_pop_insert ()
920 {
921   pop_insert (mips_pseudo_table);
922   if (! ECOFF_DEBUGGING)
923     pop_insert (mips_nonecoff_pseudo_table);
924 }
925 \f
926 /* Symbols labelling the current insn.  */
927
928 struct insn_label_list
929 {
930   struct insn_label_list *next;
931   symbolS *label;
932 };
933
934 static struct insn_label_list *insn_labels;
935 static struct insn_label_list *free_insn_labels;
936
937 static void mips_clear_insn_labels PARAMS ((void));
938
939 static inline void
940 mips_clear_insn_labels ()
941 {
942   register struct insn_label_list **pl;
943
944   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
945     ;
946   *pl = insn_labels;
947   insn_labels = NULL;
948 }
949 \f
950 static char *expr_end;
951
952 /* Expressions which appear in instructions.  These are set by
953    mips_ip.  */
954
955 static expressionS imm_expr;
956 static expressionS offset_expr;
957
958 /* Relocs associated with imm_expr and offset_expr.  */
959
960 static bfd_reloc_code_real_type imm_reloc[3]
961   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
962 static bfd_reloc_code_real_type offset_reloc[3]
963   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
964
965 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
966
967 static boolean imm_unmatched_hi;
968
969 /* These are set by mips16_ip if an explicit extension is used.  */
970
971 static boolean mips16_small, mips16_ext;
972
973 #ifdef OBJ_ELF
974 /* The pdr segment for per procedure frame/regmask info.  Not used for
975    ECOFF debugging.  */
976
977 static segT pdr_seg;
978 #endif
979
980 /* The default target format to use.  */
981
982 const char *
983 mips_target_format ()
984 {
985   switch (OUTPUT_FLAVOR)
986     {
987     case bfd_target_aout_flavour:
988       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
989     case bfd_target_ecoff_flavour:
990       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
991     case bfd_target_coff_flavour:
992       return "pe-mips";
993     case bfd_target_elf_flavour:
994 #ifdef TE_TMIPS
995       /* This is traditional mips.  */
996       return (target_big_endian
997               ? (HAVE_64BIT_OBJECTS
998                  ? "elf64-tradbigmips"
999                  : (HAVE_NEWABI
1000                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1001               : (HAVE_64BIT_OBJECTS
1002                  ? "elf64-tradlittlemips"
1003                  : (HAVE_NEWABI
1004                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1005 #else
1006       return (target_big_endian
1007               ? (HAVE_64BIT_OBJECTS
1008                  ? "elf64-bigmips"
1009                  : (HAVE_NEWABI
1010                     ? "elf32-nbigmips" : "elf32-bigmips"))
1011               : (HAVE_64BIT_OBJECTS
1012                  ? "elf64-littlemips"
1013                  : (HAVE_NEWABI
1014                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1015 #endif
1016     default:
1017       abort ();
1018       return NULL;
1019     }
1020 }
1021
1022 /* This function is called once, at assembler startup time.  It should
1023    set up all the tables, etc. that the MD part of the assembler will need.  */
1024
1025 void
1026 md_begin ()
1027 {
1028   register const char *retval = NULL;
1029   int i = 0;
1030   int broken = 0;
1031
1032   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1033     as_warn (_("Could not set architecture and machine"));
1034
1035   op_hash = hash_new ();
1036
1037   for (i = 0; i < NUMOPCODES;)
1038     {
1039       const char *name = mips_opcodes[i].name;
1040
1041       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1042       if (retval != NULL)
1043         {
1044           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1045                    mips_opcodes[i].name, retval);
1046           /* Probably a memory allocation problem?  Give up now.  */
1047           as_fatal (_("Broken assembler.  No assembly attempted."));
1048         }
1049       do
1050         {
1051           if (mips_opcodes[i].pinfo != INSN_MACRO)
1052             {
1053               if (!validate_mips_insn (&mips_opcodes[i]))
1054                 broken = 1;
1055             }
1056           ++i;
1057         }
1058       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1059     }
1060
1061   mips16_op_hash = hash_new ();
1062
1063   i = 0;
1064   while (i < bfd_mips16_num_opcodes)
1065     {
1066       const char *name = mips16_opcodes[i].name;
1067
1068       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1069       if (retval != NULL)
1070         as_fatal (_("internal: can't hash `%s': %s"),
1071                   mips16_opcodes[i].name, retval);
1072       do
1073         {
1074           if (mips16_opcodes[i].pinfo != INSN_MACRO
1075               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1076                   != mips16_opcodes[i].match))
1077             {
1078               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1079                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1080               broken = 1;
1081             }
1082           ++i;
1083         }
1084       while (i < bfd_mips16_num_opcodes
1085              && strcmp (mips16_opcodes[i].name, name) == 0);
1086     }
1087
1088   if (broken)
1089     as_fatal (_("Broken assembler.  No assembly attempted."));
1090
1091   /* We add all the general register names to the symbol table.  This
1092      helps us detect invalid uses of them.  */
1093   for (i = 0; i < 32; i++)
1094     {
1095       char buf[5];
1096
1097       sprintf (buf, "$%d", i);
1098       symbol_table_insert (symbol_new (buf, reg_section, i,
1099                                        &zero_address_frag));
1100     }
1101   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1102                                    &zero_address_frag));
1103   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1104                                    &zero_address_frag));
1105   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1106                                    &zero_address_frag));
1107   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1108                                    &zero_address_frag));
1109   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1110                                    &zero_address_frag));
1111   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1112                                    &zero_address_frag));
1113   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1114                                    &zero_address_frag));
1115   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1116                                    &zero_address_frag));
1117   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1118                                    &zero_address_frag));
1119
1120   mips_no_prev_insn (false);
1121
1122   mips_gprmask = 0;
1123   mips_cprmask[0] = 0;
1124   mips_cprmask[1] = 0;
1125   mips_cprmask[2] = 0;
1126   mips_cprmask[3] = 0;
1127
1128   /* set the default alignment for the text section (2**2) */
1129   record_alignment (text_section, 2);
1130
1131   if (USE_GLOBAL_POINTER_OPT)
1132     bfd_set_gp_size (stdoutput, g_switch_value);
1133
1134   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1135     {
1136       /* On a native system, sections must be aligned to 16 byte
1137          boundaries.  When configured for an embedded ELF target, we
1138          don't bother.  */
1139       if (strcmp (TARGET_OS, "elf") != 0)
1140         {
1141           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1142           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1143           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1144         }
1145
1146       /* Create a .reginfo section for register masks and a .mdebug
1147          section for debugging information.  */
1148       {
1149         segT seg;
1150         subsegT subseg;
1151         flagword flags;
1152         segT sec;
1153
1154         seg = now_seg;
1155         subseg = now_subseg;
1156
1157         /* The ABI says this section should be loaded so that the
1158            running program can access it.  However, we don't load it
1159            if we are configured for an embedded target */
1160         flags = SEC_READONLY | SEC_DATA;
1161         if (strcmp (TARGET_OS, "elf") != 0)
1162           flags |= SEC_ALLOC | SEC_LOAD;
1163
1164         if (mips_abi != N64_ABI)
1165           {
1166             sec = subseg_new (".reginfo", (subsegT) 0);
1167
1168             bfd_set_section_flags (stdoutput, sec, flags);
1169             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1170
1171 #ifdef OBJ_ELF
1172             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1173 #endif
1174           }
1175         else
1176           {
1177             /* The 64-bit ABI uses a .MIPS.options section rather than
1178                .reginfo section.  */
1179             sec = subseg_new (".MIPS.options", (subsegT) 0);
1180             bfd_set_section_flags (stdoutput, sec, flags);
1181             bfd_set_section_alignment (stdoutput, sec, 3);
1182
1183 #ifdef OBJ_ELF
1184             /* Set up the option header.  */
1185             {
1186               Elf_Internal_Options opthdr;
1187               char *f;
1188
1189               opthdr.kind = ODK_REGINFO;
1190               opthdr.size = (sizeof (Elf_External_Options)
1191                              + sizeof (Elf64_External_RegInfo));
1192               opthdr.section = 0;
1193               opthdr.info = 0;
1194               f = frag_more (sizeof (Elf_External_Options));
1195               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1196                                              (Elf_External_Options *) f);
1197
1198               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1199             }
1200 #endif
1201           }
1202
1203         if (ECOFF_DEBUGGING)
1204           {
1205             sec = subseg_new (".mdebug", (subsegT) 0);
1206             (void) bfd_set_section_flags (stdoutput, sec,
1207                                           SEC_HAS_CONTENTS | SEC_READONLY);
1208             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1209           }
1210 #ifdef OBJ_ELF
1211         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1212           {
1213             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1214             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1215                                           SEC_READONLY | SEC_RELOC
1216                                           | SEC_DEBUGGING);
1217             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1218           }
1219 #endif
1220
1221         subseg_set (seg, subseg);
1222       }
1223     }
1224
1225   if (! ECOFF_DEBUGGING)
1226     md_obj_begin ();
1227 }
1228
1229 void
1230 md_mips_end ()
1231 {
1232   if (! ECOFF_DEBUGGING)
1233     md_obj_end ();
1234 }
1235
1236 void
1237 md_assemble (str)
1238      char *str;
1239 {
1240   struct mips_cl_insn insn;
1241   bfd_reloc_code_real_type unused_reloc[3]
1242     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1243
1244   imm_expr.X_op = O_absent;
1245   imm_unmatched_hi = false;
1246   offset_expr.X_op = O_absent;
1247   imm_reloc[0] = BFD_RELOC_UNUSED;
1248   imm_reloc[1] = BFD_RELOC_UNUSED;
1249   imm_reloc[2] = BFD_RELOC_UNUSED;
1250   offset_reloc[0] = BFD_RELOC_UNUSED;
1251   offset_reloc[1] = BFD_RELOC_UNUSED;
1252   offset_reloc[2] = BFD_RELOC_UNUSED;
1253
1254   if (mips_opts.mips16)
1255     mips16_ip (str, &insn);
1256   else
1257     {
1258       mips_ip (str, &insn);
1259       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1260             str, insn.insn_opcode));
1261     }
1262
1263   if (insn_error)
1264     {
1265       as_bad ("%s `%s'", insn_error, str);
1266       return;
1267     }
1268
1269   if (insn.insn_mo->pinfo == INSN_MACRO)
1270     {
1271       if (mips_opts.mips16)
1272         mips16_macro (&insn);
1273       else
1274         macro (&insn);
1275     }
1276   else
1277     {
1278       if (imm_expr.X_op != O_absent)
1279         append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1280       else if (offset_expr.X_op != O_absent)
1281         append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1282       else
1283         append_insn (NULL, &insn, NULL, unused_reloc, false);
1284     }
1285 }
1286
1287 /* See whether instruction IP reads register REG.  CLASS is the type
1288    of register.  */
1289
1290 static int
1291 insn_uses_reg (ip, reg, class)
1292      struct mips_cl_insn *ip;
1293      unsigned int reg;
1294      enum mips_regclass class;
1295 {
1296   if (class == MIPS16_REG)
1297     {
1298       assert (mips_opts.mips16);
1299       reg = mips16_to_32_reg_map[reg];
1300       class = MIPS_GR_REG;
1301     }
1302
1303   /* Don't report on general register ZERO, since it never changes.  */
1304   if (class == MIPS_GR_REG && reg == ZERO)
1305     return 0;
1306
1307   if (class == MIPS_FP_REG)
1308     {
1309       assert (! mips_opts.mips16);
1310       /* If we are called with either $f0 or $f1, we must check $f0.
1311          This is not optimal, because it will introduce an unnecessary
1312          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1313          need to distinguish reading both $f0 and $f1 or just one of
1314          them.  Note that we don't have to check the other way,
1315          because there is no instruction that sets both $f0 and $f1
1316          and requires a delay.  */
1317       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1318           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1319               == (reg &~ (unsigned) 1)))
1320         return 1;
1321       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1322           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1323               == (reg &~ (unsigned) 1)))
1324         return 1;
1325     }
1326   else if (! mips_opts.mips16)
1327     {
1328       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1329           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1330         return 1;
1331       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1332           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1333         return 1;
1334     }
1335   else
1336     {
1337       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1338           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1339                                     & MIPS16OP_MASK_RX)]
1340               == reg))
1341         return 1;
1342       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1343           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1344                                     & MIPS16OP_MASK_RY)]
1345               == reg))
1346         return 1;
1347       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1348           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1349                                     & MIPS16OP_MASK_MOVE32Z)]
1350               == reg))
1351         return 1;
1352       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1353         return 1;
1354       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1355         return 1;
1356       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1357         return 1;
1358       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1359           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1360               & MIPS16OP_MASK_REGR32) == reg)
1361         return 1;
1362     }
1363
1364   return 0;
1365 }
1366
1367 /* This function returns true if modifying a register requires a
1368    delay.  */
1369
1370 static int
1371 reg_needs_delay (reg)
1372      unsigned int reg;
1373 {
1374   unsigned long prev_pinfo;
1375
1376   prev_pinfo = prev_insn.insn_mo->pinfo;
1377   if (! mips_opts.noreorder
1378       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1379       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1380           || (! gpr_interlocks
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 (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1390         return 1;
1391     }
1392
1393   return 0;
1394 }
1395
1396 /* Mark instruction labels in mips16 mode.  This permits the linker to
1397    handle them specially, such as generating jalx instructions when
1398    needed.  We also make them odd for the duration of the assembly, in
1399    order to generate the right sort of code.  We will make them even
1400    in the adjust_symtab routine, while leaving them marked.  This is
1401    convenient for the debugger and the disassembler.  The linker knows
1402    to make them odd again.  */
1403
1404 static void
1405 mips16_mark_labels ()
1406 {
1407   if (mips_opts.mips16)
1408     {
1409       struct insn_label_list *l;
1410       valueT val;
1411
1412       for (l = insn_labels; l != NULL; l = l->next)
1413         {
1414 #ifdef OBJ_ELF
1415           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1416             S_SET_OTHER (l->label, STO_MIPS16);
1417 #endif
1418           val = S_GET_VALUE (l->label);
1419           if ((val & 1) == 0)
1420             S_SET_VALUE (l->label, val + 1);
1421         }
1422     }
1423 }
1424
1425 /* Output an instruction.  PLACE is where to put the instruction; if
1426    it is NULL, this uses frag_more to get room.  IP is the instruction
1427    information.  ADDRESS_EXPR is an operand of the instruction to be
1428    used with RELOC_TYPE.  */
1429
1430 static void
1431 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1432      char *place;
1433      struct mips_cl_insn *ip;
1434      expressionS *address_expr;
1435      bfd_reloc_code_real_type *reloc_type;
1436      boolean unmatched_hi;
1437 {
1438   register unsigned long prev_pinfo, pinfo;
1439   char *f;
1440   fixS *fixp[3];
1441   int nops = 0;
1442
1443   /* Mark instruction labels in mips16 mode.  */
1444   mips16_mark_labels ();
1445
1446   prev_pinfo = prev_insn.insn_mo->pinfo;
1447   pinfo = ip->insn_mo->pinfo;
1448
1449   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1450     {
1451       int prev_prev_nop;
1452
1453       /* If the previous insn required any delay slots, see if we need
1454          to insert a NOP or two.  There are eight kinds of possible
1455          hazards, of which an instruction can have at most one type.
1456          (1) a load from memory delay
1457          (2) a load from a coprocessor delay
1458          (3) an unconditional branch delay
1459          (4) a conditional branch delay
1460          (5) a move to coprocessor register delay
1461          (6) a load coprocessor register from memory delay
1462          (7) a coprocessor condition code delay
1463          (8) a HI/LO special register delay
1464
1465          There are a lot of optimizations we could do that we don't.
1466          In particular, we do not, in general, reorder instructions.
1467          If you use gcc with optimization, it will reorder
1468          instructions and generally do much more optimization then we
1469          do here; repeating all that work in the assembler would only
1470          benefit hand written assembly code, and does not seem worth
1471          it.  */
1472
1473       /* This is how a NOP is emitted.  */
1474 #define emit_nop()                                      \
1475   (mips_opts.mips16                                     \
1476    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1477    : md_number_to_chars (frag_more (4), 0, 4))
1478
1479       /* The previous insn might require a delay slot, depending upon
1480          the contents of the current insn.  */
1481       if (! mips_opts.mips16
1482           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1483           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1484                && ! cop_interlocks)
1485               || (! gpr_interlocks
1486                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1487         {
1488           /* A load from a coprocessor or from memory.  All load
1489              delays delay the use of general register rt for one
1490              instruction on the r3000.  The r6000 and r4000 use
1491              interlocks.  */
1492           /* Itbl support may require additional care here.  */
1493           know (prev_pinfo & INSN_WRITE_GPR_T);
1494           if (mips_optimize == 0
1495               || insn_uses_reg (ip,
1496                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1497                                  & OP_MASK_RT),
1498                                 MIPS_GR_REG))
1499             ++nops;
1500         }
1501       else if (! mips_opts.mips16
1502                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1503                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1504                     && ! cop_interlocks)
1505                    || (mips_opts.isa == ISA_MIPS1
1506                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1507         {
1508           /* A generic coprocessor delay.  The previous instruction
1509              modified a coprocessor general or control register.  If
1510              it modified a control register, we need to avoid any
1511              coprocessor instruction (this is probably not always
1512              required, but it sometimes is).  If it modified a general
1513              register, we avoid using that register.
1514
1515              On the r6000 and r4000 loading a coprocessor register
1516              from memory is interlocked, and does not require a delay.
1517
1518              This case is not handled very well.  There is no special
1519              knowledge of CP0 handling, and the coprocessors other
1520              than the floating point unit are not distinguished at
1521              all.  */
1522           /* Itbl support may require additional care here. FIXME!
1523              Need to modify this to include knowledge about
1524              user specified delays!  */
1525           if (prev_pinfo & INSN_WRITE_FPR_T)
1526             {
1527               if (mips_optimize == 0
1528                   || insn_uses_reg (ip,
1529                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1530                                      & OP_MASK_FT),
1531                                     MIPS_FP_REG))
1532                 ++nops;
1533             }
1534           else if (prev_pinfo & INSN_WRITE_FPR_S)
1535             {
1536               if (mips_optimize == 0
1537                   || insn_uses_reg (ip,
1538                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1539                                      & OP_MASK_FS),
1540                                     MIPS_FP_REG))
1541                 ++nops;
1542             }
1543           else
1544             {
1545               /* We don't know exactly what the previous instruction
1546                  does.  If the current instruction uses a coprocessor
1547                  register, we must insert a NOP.  If previous
1548                  instruction may set the condition codes, and the
1549                  current instruction uses them, we must insert two
1550                  NOPS.  */
1551               /* Itbl support may require additional care here.  */
1552               if (mips_optimize == 0
1553                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1554                       && (pinfo & INSN_READ_COND_CODE)))
1555                 nops += 2;
1556               else if (pinfo & INSN_COP)
1557                 ++nops;
1558             }
1559         }
1560       else if (! mips_opts.mips16
1561                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1562                && (prev_pinfo & INSN_WRITE_COND_CODE)
1563                && ! cop_interlocks)
1564         {
1565           /* The previous instruction sets the coprocessor condition
1566              codes, but does not require a general coprocessor delay
1567              (this means it is a floating point comparison
1568              instruction).  If this instruction uses the condition
1569              codes, we need to insert a single NOP.  */
1570           /* Itbl support may require additional care here.  */
1571           if (mips_optimize == 0
1572               || (pinfo & INSN_READ_COND_CODE))
1573             ++nops;
1574         }
1575
1576       /* If we're fixing up mfhi/mflo for the r7000 and the
1577          previous insn was an mfhi/mflo and the current insn
1578          reads the register that the mfhi/mflo wrote to, then
1579          insert two nops.  */
1580
1581       else if (mips_7000_hilo_fix
1582                && MF_HILO_INSN (prev_pinfo)
1583                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1584                                       & OP_MASK_RD),
1585                                  MIPS_GR_REG))
1586         {
1587           nops += 2;
1588         }
1589
1590       /* If we're fixing up mfhi/mflo for the r7000 and the
1591          2nd previous insn was an mfhi/mflo and the current insn
1592          reads the register that the mfhi/mflo wrote to, then
1593          insert one nop.  */
1594
1595       else if (mips_7000_hilo_fix
1596                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1597                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1598                                        & OP_MASK_RD),
1599                                     MIPS_GR_REG))
1600
1601         {
1602           ++nops;
1603         }
1604
1605       else if (prev_pinfo & INSN_READ_LO)
1606         {
1607           /* The previous instruction reads the LO register; if the
1608              current instruction writes to the LO register, we must
1609              insert two NOPS.  Some newer processors have interlocks.
1610              Also the tx39's multiply instructions can be exectuted
1611              immediatly after a read from HI/LO (without the delay),
1612              though the tx39's divide insns still do require the
1613              delay.  */
1614           if (! (hilo_interlocks
1615                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1616               && (mips_optimize == 0
1617                   || (pinfo & INSN_WRITE_LO)))
1618             nops += 2;
1619           /* Most mips16 branch insns don't have a delay slot.
1620              If a read from LO is immediately followed by a branch
1621              to a write to LO we have a read followed by a write
1622              less than 2 insns away.  We assume the target of
1623              a branch might be a write to LO, and insert a nop
1624              between a read and an immediately following branch.  */
1625           else if (mips_opts.mips16
1626                    && (mips_optimize == 0
1627                        || (pinfo & MIPS16_INSN_BRANCH)))
1628             ++nops;
1629         }
1630       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1631         {
1632           /* The previous instruction reads the HI register; if the
1633              current instruction writes to the HI register, we must
1634              insert a NOP.  Some newer processors have interlocks.
1635              Also the note tx39's multiply above.  */
1636           if (! (hilo_interlocks
1637                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1638               && (mips_optimize == 0
1639                   || (pinfo & INSN_WRITE_HI)))
1640             nops += 2;
1641           /* Most mips16 branch insns don't have a delay slot.
1642              If a read from HI is immediately followed by a branch
1643              to a write to HI we have a read followed by a write
1644              less than 2 insns away.  We assume the target of
1645              a branch might be a write to HI, and insert a nop
1646              between a read and an immediately following branch.  */
1647           else if (mips_opts.mips16
1648                    && (mips_optimize == 0
1649                        || (pinfo & MIPS16_INSN_BRANCH)))
1650             ++nops;
1651         }
1652
1653       /* If the previous instruction was in a noreorder section, then
1654          we don't want to insert the nop after all.  */
1655       /* Itbl support may require additional care here.  */
1656       if (prev_insn_unreordered)
1657         nops = 0;
1658
1659       /* There are two cases which require two intervening
1660          instructions: 1) setting the condition codes using a move to
1661          coprocessor instruction which requires a general coprocessor
1662          delay and then reading the condition codes 2) reading the HI
1663          or LO register and then writing to it (except on processors
1664          which have interlocks).  If we are not already emitting a NOP
1665          instruction, we must check for these cases compared to the
1666          instruction previous to the previous instruction.  */
1667       if ((! mips_opts.mips16
1668            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1669            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1670            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1671            && (pinfo & INSN_READ_COND_CODE)
1672            && ! cop_interlocks)
1673           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1674               && (pinfo & INSN_WRITE_LO)
1675               && ! (hilo_interlocks
1676                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1677           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1678               && (pinfo & INSN_WRITE_HI)
1679               && ! (hilo_interlocks
1680                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1681         prev_prev_nop = 1;
1682       else
1683         prev_prev_nop = 0;
1684
1685       if (prev_prev_insn_unreordered)
1686         prev_prev_nop = 0;
1687
1688       if (prev_prev_nop && nops == 0)
1689         ++nops;
1690
1691       /* If we are being given a nop instruction, don't bother with
1692          one of the nops we would otherwise output.  This will only
1693          happen when a nop instruction is used with mips_optimize set
1694          to 0.  */
1695       if (nops > 0
1696           && ! mips_opts.noreorder
1697           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1698         --nops;
1699
1700       /* Now emit the right number of NOP instructions.  */
1701       if (nops > 0 && ! mips_opts.noreorder)
1702         {
1703           fragS *old_frag;
1704           unsigned long old_frag_offset;
1705           int i;
1706           struct insn_label_list *l;
1707
1708           old_frag = frag_now;
1709           old_frag_offset = frag_now_fix ();
1710
1711           for (i = 0; i < nops; i++)
1712             emit_nop ();
1713
1714           if (listing)
1715             {
1716               listing_prev_line ();
1717               /* We may be at the start of a variant frag.  In case we
1718                  are, make sure there is enough space for the frag
1719                  after the frags created by listing_prev_line.  The
1720                  argument to frag_grow here must be at least as large
1721                  as the argument to all other calls to frag_grow in
1722                  this file.  We don't have to worry about being in the
1723                  middle of a variant frag, because the variants insert
1724                  all needed nop instructions themselves.  */
1725               frag_grow (40);
1726             }
1727
1728           for (l = insn_labels; l != NULL; l = l->next)
1729             {
1730               valueT val;
1731
1732               assert (S_GET_SEGMENT (l->label) == now_seg);
1733               symbol_set_frag (l->label, frag_now);
1734               val = (valueT) frag_now_fix ();
1735               /* mips16 text labels are stored as odd.  */
1736               if (mips_opts.mips16)
1737                 ++val;
1738               S_SET_VALUE (l->label, val);
1739             }
1740
1741 #ifndef NO_ECOFF_DEBUGGING
1742           if (ECOFF_DEBUGGING)
1743             ecoff_fix_loc (old_frag, old_frag_offset);
1744 #endif
1745         }
1746       else if (prev_nop_frag != NULL)
1747         {
1748           /* We have a frag holding nops we may be able to remove.  If
1749              we don't need any nops, we can decrease the size of
1750              prev_nop_frag by the size of one instruction.  If we do
1751              need some nops, we count them in prev_nops_required.  */
1752           if (prev_nop_frag_since == 0)
1753             {
1754               if (nops == 0)
1755                 {
1756                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1757                   --prev_nop_frag_holds;
1758                 }
1759               else
1760                 prev_nop_frag_required += nops;
1761             }
1762           else
1763             {
1764               if (prev_prev_nop == 0)
1765                 {
1766                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1767                   --prev_nop_frag_holds;
1768                 }
1769               else
1770                 ++prev_nop_frag_required;
1771             }
1772
1773           if (prev_nop_frag_holds <= prev_nop_frag_required)
1774             prev_nop_frag = NULL;
1775
1776           ++prev_nop_frag_since;
1777
1778           /* Sanity check: by the time we reach the second instruction
1779              after prev_nop_frag, we should have used up all the nops
1780              one way or another.  */
1781           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1782         }
1783     }
1784
1785   if (*reloc_type > BFD_RELOC_UNUSED)
1786     {
1787       /* We need to set up a variant frag.  */
1788       assert (mips_opts.mips16 && address_expr != NULL);
1789       f = frag_var (rs_machine_dependent, 4, 0,
1790                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1791                                          mips16_small, mips16_ext,
1792                                          (prev_pinfo
1793                                           & INSN_UNCOND_BRANCH_DELAY),
1794                                          (*prev_insn_reloc_type
1795                                           == BFD_RELOC_MIPS16_JMP)),
1796                     make_expr_symbol (address_expr), 0, NULL);
1797     }
1798   else if (place != NULL)
1799     f = place;
1800   else if (mips_opts.mips16
1801            && ! ip->use_extend
1802            && *reloc_type != BFD_RELOC_MIPS16_JMP)
1803     {
1804       /* Make sure there is enough room to swap this instruction with
1805          a following jump instruction.  */
1806       frag_grow (6);
1807       f = frag_more (2);
1808     }
1809   else
1810     {
1811       if (mips_opts.mips16
1812           && mips_opts.noreorder
1813           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1814         as_warn (_("extended instruction in delay slot"));
1815
1816       f = frag_more (4);
1817     }
1818
1819   fixp[0] = fixp[1] = fixp[2] = NULL;
1820   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1821     {
1822       if (address_expr->X_op == O_constant)
1823         {
1824           valueT tmp;
1825
1826           switch (*reloc_type)
1827             {
1828             case BFD_RELOC_32:
1829               ip->insn_opcode |= address_expr->X_add_number;
1830               break;
1831
1832             case BFD_RELOC_MIPS_HIGHEST:
1833               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1834               tmp >>= 16;
1835               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1836               break;
1837
1838             case BFD_RELOC_MIPS_HIGHER:
1839               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1840               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1841               break;
1842
1843             case BFD_RELOC_HI16_S:
1844               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1845                                   >> 16) & 0xffff;
1846               break;
1847
1848             case BFD_RELOC_HI16:
1849               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1850               break;
1851
1852             case BFD_RELOC_LO16:
1853             case BFD_RELOC_MIPS_GOT_DISP:
1854               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1855               break;
1856
1857             case BFD_RELOC_MIPS_JMP:
1858               if ((address_expr->X_add_number & 3) != 0)
1859                 as_bad (_("jump to misaligned address (0x%lx)"),
1860                         (unsigned long) address_expr->X_add_number);
1861               if (address_expr->X_add_number & ~0xfffffff
1862                   || address_expr->X_add_number > 0x7fffffc)
1863                 as_bad (_("jump address range overflow (0x%lx)"),
1864                         (unsigned long) address_expr->X_add_number);
1865               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1866               break;
1867
1868             case BFD_RELOC_MIPS16_JMP:
1869               if ((address_expr->X_add_number & 3) != 0)
1870                 as_bad (_("jump to misaligned address (0x%lx)"),
1871                         (unsigned long) address_expr->X_add_number);
1872               if (address_expr->X_add_number & ~0xfffffff
1873                   || address_expr->X_add_number > 0x7fffffc)
1874                 as_bad (_("jump address range overflow (0x%lx)"),
1875                         (unsigned long) address_expr->X_add_number);
1876               ip->insn_opcode |=
1877                 (((address_expr->X_add_number & 0x7c0000) << 3)
1878                  | ((address_expr->X_add_number & 0xf800000) >> 7)
1879                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
1880               break;
1881
1882             case BFD_RELOC_16_PCREL:
1883               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1884               break;
1885
1886             case BFD_RELOC_16_PCREL_S2:
1887               goto need_reloc;
1888
1889             default:
1890               internalError ();
1891             }
1892         }
1893       else
1894         {
1895         need_reloc:
1896           /* Don't generate a reloc if we are writing into a variant frag.  */
1897           if (place == NULL)
1898             {
1899               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1900                                      address_expr,
1901                                      (*reloc_type == BFD_RELOC_16_PCREL
1902                                       || *reloc_type == BFD_RELOC_16_PCREL_S2),
1903                                      reloc_type[0]);
1904
1905               /* These relocations can have an addend that won't fit in
1906                  4 octets for 64bit assembly.  */
1907               if (HAVE_64BIT_GPRS &&
1908                   (*reloc_type == BFD_RELOC_16
1909                    || *reloc_type == BFD_RELOC_32
1910                    || *reloc_type == BFD_RELOC_MIPS_JMP
1911                    || *reloc_type == BFD_RELOC_HI16_S
1912                    || *reloc_type == BFD_RELOC_LO16
1913                    || *reloc_type == BFD_RELOC_GPREL16
1914                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
1915                    || *reloc_type == BFD_RELOC_GPREL32
1916                    || *reloc_type == BFD_RELOC_64
1917                    || *reloc_type == BFD_RELOC_CTOR
1918                    || *reloc_type == BFD_RELOC_MIPS_SUB
1919                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1920                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
1921                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1922                    || *reloc_type == BFD_RELOC_MIPS_REL16
1923                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1924                 fixp[0]->fx_no_overflow = 1;
1925
1926               if (unmatched_hi)
1927                 {
1928                   struct mips_hi_fixup *hi_fixup;
1929
1930                   assert (*reloc_type == BFD_RELOC_HI16_S);
1931                   hi_fixup = ((struct mips_hi_fixup *)
1932                               xmalloc (sizeof (struct mips_hi_fixup)));
1933                   hi_fixup->fixp = fixp[0];
1934                   hi_fixup->seg = now_seg;
1935                   hi_fixup->next = mips_hi_fixup_list;
1936                   mips_hi_fixup_list = hi_fixup;
1937                 }
1938
1939               if (reloc_type[1] != BFD_RELOC_UNUSED)
1940                 {
1941                   /* FIXME: This symbol can be one of
1942                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
1943                   address_expr->X_op = O_absent;
1944                   address_expr->X_add_symbol = 0;
1945                   address_expr->X_add_number = 0;
1946
1947                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
1948                                          4, address_expr, false,
1949                                          reloc_type[1]);
1950
1951                   /* These relocations can have an addend that won't fit in
1952                      4 octets for 64bit assembly.  */
1953                   if (HAVE_64BIT_GPRS &&
1954                       (*reloc_type == BFD_RELOC_16
1955                        || *reloc_type == BFD_RELOC_32
1956                        || *reloc_type == BFD_RELOC_MIPS_JMP
1957                        || *reloc_type == BFD_RELOC_HI16_S
1958                        || *reloc_type == BFD_RELOC_LO16
1959                        || *reloc_type == BFD_RELOC_GPREL16
1960                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
1961                        || *reloc_type == BFD_RELOC_GPREL32
1962                        || *reloc_type == BFD_RELOC_64
1963                        || *reloc_type == BFD_RELOC_CTOR
1964                        || *reloc_type == BFD_RELOC_MIPS_SUB
1965                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1966                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
1967                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1968                        || *reloc_type == BFD_RELOC_MIPS_REL16
1969                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1970                     fixp[1]->fx_no_overflow = 1;
1971
1972                   if (reloc_type[2] != BFD_RELOC_UNUSED)
1973                     {
1974                       address_expr->X_op = O_absent;
1975                       address_expr->X_add_symbol = 0;
1976                       address_expr->X_add_number = 0;
1977
1978                       fixp[2] = fix_new_exp (frag_now,
1979                                              f - frag_now->fr_literal, 4,
1980                                              address_expr, false,
1981                                              reloc_type[2]);
1982
1983                       /* These relocations can have an addend that won't fit in
1984                          4 octets for 64bit assembly.  */
1985                       if (HAVE_64BIT_GPRS &&
1986                           (*reloc_type == BFD_RELOC_16
1987                            || *reloc_type == BFD_RELOC_32
1988                            || *reloc_type == BFD_RELOC_MIPS_JMP
1989                            || *reloc_type == BFD_RELOC_HI16_S
1990                            || *reloc_type == BFD_RELOC_LO16
1991                            || *reloc_type == BFD_RELOC_GPREL16
1992                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
1993                            || *reloc_type == BFD_RELOC_GPREL32
1994                            || *reloc_type == BFD_RELOC_64
1995                            || *reloc_type == BFD_RELOC_CTOR
1996                            || *reloc_type == BFD_RELOC_MIPS_SUB
1997                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1998                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
1999                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2000                            || *reloc_type == BFD_RELOC_MIPS_REL16
2001                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2002                         fixp[2]->fx_no_overflow = 1;
2003                     }
2004                 }
2005             }
2006         }
2007     }
2008
2009   if (! mips_opts.mips16)
2010     {
2011       md_number_to_chars (f, ip->insn_opcode, 4);
2012 #ifdef OBJ_ELF
2013       dwarf2_emit_insn (4);
2014 #endif
2015     }
2016   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2017     {
2018       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2019       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2020 #ifdef OBJ_ELF
2021       dwarf2_emit_insn (4);
2022 #endif
2023     }
2024   else
2025     {
2026       if (ip->use_extend)
2027         {
2028           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2029           f += 2;
2030         }
2031       md_number_to_chars (f, ip->insn_opcode, 2);
2032 #ifdef OBJ_ELF
2033       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2034 #endif
2035     }
2036
2037   /* Update the register mask information.  */
2038   if (! mips_opts.mips16)
2039     {
2040       if (pinfo & INSN_WRITE_GPR_D)
2041         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2042       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2043         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2044       if (pinfo & INSN_READ_GPR_S)
2045         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2046       if (pinfo & INSN_WRITE_GPR_31)
2047         mips_gprmask |= 1 << RA;
2048       if (pinfo & INSN_WRITE_FPR_D)
2049         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2050       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2051         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2052       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2053         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2054       if ((pinfo & INSN_READ_FPR_R) != 0)
2055         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2056       if (pinfo & INSN_COP)
2057         {
2058           /* We don't keep enough information to sort these cases out.
2059              The itbl support does keep this information however, although
2060              we currently don't support itbl fprmats as part of the cop
2061              instruction.  May want to add this support in the future.  */
2062         }
2063       /* Never set the bit for $0, which is always zero.  */
2064       mips_gprmask &= ~1 << 0;
2065     }
2066   else
2067     {
2068       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2069         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2070                               & MIPS16OP_MASK_RX);
2071       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2072         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2073                               & MIPS16OP_MASK_RY);
2074       if (pinfo & MIPS16_INSN_WRITE_Z)
2075         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2076                               & MIPS16OP_MASK_RZ);
2077       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2078         mips_gprmask |= 1 << TREG;
2079       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2080         mips_gprmask |= 1 << SP;
2081       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2082         mips_gprmask |= 1 << RA;
2083       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2084         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2085       if (pinfo & MIPS16_INSN_READ_Z)
2086         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2087                               & MIPS16OP_MASK_MOVE32Z);
2088       if (pinfo & MIPS16_INSN_READ_GPR_X)
2089         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2090                               & MIPS16OP_MASK_REGR32);
2091     }
2092
2093   if (place == NULL && ! mips_opts.noreorder)
2094     {
2095       /* Filling the branch delay slot is more complex.  We try to
2096          switch the branch with the previous instruction, which we can
2097          do if the previous instruction does not set up a condition
2098          that the branch tests and if the branch is not itself the
2099          target of any branch.  */
2100       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2101           || (pinfo & INSN_COND_BRANCH_DELAY))
2102         {
2103           if (mips_optimize < 2
2104               /* If we have seen .set volatile or .set nomove, don't
2105                  optimize.  */
2106               || mips_opts.nomove != 0
2107               /* If we had to emit any NOP instructions, then we
2108                  already know we can not swap.  */
2109               || nops != 0
2110               /* If we don't even know the previous insn, we can not
2111                  swap.  */
2112               || ! prev_insn_valid
2113               /* If the previous insn is already in a branch delay
2114                  slot, then we can not swap.  */
2115               || prev_insn_is_delay_slot
2116               /* If the previous previous insn was in a .set
2117                  noreorder, we can't swap.  Actually, the MIPS
2118                  assembler will swap in this situation.  However, gcc
2119                  configured -with-gnu-as will generate code like
2120                    .set noreorder
2121                    lw   $4,XXX
2122                    .set reorder
2123                    INSN
2124                    bne  $4,$0,foo
2125                  in which we can not swap the bne and INSN.  If gcc is
2126                  not configured -with-gnu-as, it does not output the
2127                  .set pseudo-ops.  We don't have to check
2128                  prev_insn_unreordered, because prev_insn_valid will
2129                  be 0 in that case.  We don't want to use
2130                  prev_prev_insn_valid, because we do want to be able
2131                  to swap at the start of a function.  */
2132               || prev_prev_insn_unreordered
2133               /* If the branch is itself the target of a branch, we
2134                  can not swap.  We cheat on this; all we check for is
2135                  whether there is a label on this instruction.  If
2136                  there are any branches to anything other than a
2137                  label, users must use .set noreorder.  */
2138               || insn_labels != NULL
2139               /* If the previous instruction is in a variant frag, we
2140                  can not do the swap.  This does not apply to the
2141                  mips16, which uses variant frags for different
2142                  purposes.  */
2143               || (! mips_opts.mips16
2144                   && prev_insn_frag->fr_type == rs_machine_dependent)
2145               /* If the branch reads the condition codes, we don't
2146                  even try to swap, because in the sequence
2147                    ctc1 $X,$31
2148                    INSN
2149                    INSN
2150                    bc1t LABEL
2151                  we can not swap, and I don't feel like handling that
2152                  case.  */
2153               || (! mips_opts.mips16
2154                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2155                   && (pinfo & INSN_READ_COND_CODE))
2156               /* We can not swap with an instruction that requires a
2157                  delay slot, becase the target of the branch might
2158                  interfere with that instruction.  */
2159               || (! mips_opts.mips16
2160                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2161                   && (prev_pinfo
2162               /* Itbl support may require additional care here.  */
2163                       & (INSN_LOAD_COPROC_DELAY
2164                          | INSN_COPROC_MOVE_DELAY
2165                          | INSN_WRITE_COND_CODE)))
2166               || (! (hilo_interlocks
2167                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2168                   && (prev_pinfo
2169                       & (INSN_READ_LO
2170                          | INSN_READ_HI)))
2171               || (! mips_opts.mips16
2172                   && ! gpr_interlocks
2173                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2174               || (! mips_opts.mips16
2175                   && mips_opts.isa == ISA_MIPS1
2176                   /* Itbl support may require additional care here.  */
2177                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2178               /* We can not swap with a branch instruction.  */
2179               || (prev_pinfo
2180                   & (INSN_UNCOND_BRANCH_DELAY
2181                      | INSN_COND_BRANCH_DELAY
2182                      | INSN_COND_BRANCH_LIKELY))
2183               /* We do not swap with a trap instruction, since it
2184                  complicates trap handlers to have the trap
2185                  instruction be in a delay slot.  */
2186               || (prev_pinfo & INSN_TRAP)
2187               /* If the branch reads a register that the previous
2188                  instruction sets, we can not swap.  */
2189               || (! mips_opts.mips16
2190                   && (prev_pinfo & INSN_WRITE_GPR_T)
2191                   && insn_uses_reg (ip,
2192                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2193                                      & OP_MASK_RT),
2194                                     MIPS_GR_REG))
2195               || (! mips_opts.mips16
2196                   && (prev_pinfo & INSN_WRITE_GPR_D)
2197                   && insn_uses_reg (ip,
2198                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2199                                      & OP_MASK_RD),
2200                                     MIPS_GR_REG))
2201               || (mips_opts.mips16
2202                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2203                        && insn_uses_reg (ip,
2204                                          ((prev_insn.insn_opcode
2205                                            >> MIPS16OP_SH_RX)
2206                                           & MIPS16OP_MASK_RX),
2207                                          MIPS16_REG))
2208                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2209                           && insn_uses_reg (ip,
2210                                             ((prev_insn.insn_opcode
2211                                               >> MIPS16OP_SH_RY)
2212                                              & MIPS16OP_MASK_RY),
2213                                             MIPS16_REG))
2214                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2215                           && insn_uses_reg (ip,
2216                                             ((prev_insn.insn_opcode
2217                                               >> MIPS16OP_SH_RZ)
2218                                              & MIPS16OP_MASK_RZ),
2219                                             MIPS16_REG))
2220                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2221                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2222                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2223                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2224                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2225                           && insn_uses_reg (ip,
2226                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2227                                                                      insn_opcode),
2228                                             MIPS_GR_REG))))
2229               /* If the branch writes a register that the previous
2230                  instruction sets, we can not swap (we know that
2231                  branches write only to RD or to $31).  */
2232               || (! mips_opts.mips16
2233                   && (prev_pinfo & INSN_WRITE_GPR_T)
2234                   && (((pinfo & INSN_WRITE_GPR_D)
2235                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2236                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2237                       || ((pinfo & INSN_WRITE_GPR_31)
2238                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2239                                & OP_MASK_RT)
2240                               == RA))))
2241               || (! mips_opts.mips16
2242                   && (prev_pinfo & INSN_WRITE_GPR_D)
2243                   && (((pinfo & INSN_WRITE_GPR_D)
2244                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2245                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2246                       || ((pinfo & INSN_WRITE_GPR_31)
2247                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2248                                & OP_MASK_RD)
2249                               == RA))))
2250               || (mips_opts.mips16
2251                   && (pinfo & MIPS16_INSN_WRITE_31)
2252                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2253                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2254                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2255                               == RA))))
2256               /* If the branch writes a register that the previous
2257                  instruction reads, we can not swap (we know that
2258                  branches only write to RD or to $31).  */
2259               || (! mips_opts.mips16
2260                   && (pinfo & INSN_WRITE_GPR_D)
2261                   && insn_uses_reg (&prev_insn,
2262                                     ((ip->insn_opcode >> OP_SH_RD)
2263                                      & OP_MASK_RD),
2264                                     MIPS_GR_REG))
2265               || (! mips_opts.mips16
2266                   && (pinfo & INSN_WRITE_GPR_31)
2267                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2268               || (mips_opts.mips16
2269                   && (pinfo & MIPS16_INSN_WRITE_31)
2270                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2271               /* If we are generating embedded PIC code, the branch
2272                  might be expanded into a sequence which uses $at, so
2273                  we can't swap with an instruction which reads it.  */
2274               || (mips_pic == EMBEDDED_PIC
2275                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2276               /* If the previous previous instruction has a load
2277                  delay, and sets a register that the branch reads, we
2278                  can not swap.  */
2279               || (! mips_opts.mips16
2280                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2281               /* Itbl support may require additional care here.  */
2282                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2283                       || (! gpr_interlocks
2284                           && (prev_prev_insn.insn_mo->pinfo
2285                               & INSN_LOAD_MEMORY_DELAY)))
2286                   && insn_uses_reg (ip,
2287                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2288                                      & OP_MASK_RT),
2289                                     MIPS_GR_REG))
2290               /* If one instruction sets a condition code and the
2291                  other one uses a condition code, we can not swap.  */
2292               || ((pinfo & INSN_READ_COND_CODE)
2293                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2294               || ((pinfo & INSN_WRITE_COND_CODE)
2295                   && (prev_pinfo & INSN_READ_COND_CODE))
2296               /* If the previous instruction uses the PC, we can not
2297                  swap.  */
2298               || (mips_opts.mips16
2299                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2300               /* If the previous instruction was extended, we can not
2301                  swap.  */
2302               || (mips_opts.mips16 && prev_insn_extended)
2303               /* If the previous instruction had a fixup in mips16
2304                  mode, we can not swap.  This normally means that the
2305                  previous instruction was a 4 byte branch anyhow.  */
2306               || (mips_opts.mips16 && prev_insn_fixp[0])
2307               /* If the previous instruction is a sync, sync.l, or
2308                  sync.p, we can not swap.  */
2309               || (prev_pinfo & INSN_SYNC))
2310             {
2311               /* We could do even better for unconditional branches to
2312                  portions of this object file; we could pick up the
2313                  instruction at the destination, put it in the delay
2314                  slot, and bump the destination address.  */
2315               emit_nop ();
2316               /* Update the previous insn information.  */
2317               prev_prev_insn = *ip;
2318               prev_insn.insn_mo = &dummy_opcode;
2319             }
2320           else
2321             {
2322               /* It looks like we can actually do the swap.  */
2323               if (! mips_opts.mips16)
2324                 {
2325                   char *prev_f;
2326                   char temp[4];
2327
2328                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2329                   memcpy (temp, prev_f, 4);
2330                   memcpy (prev_f, f, 4);
2331                   memcpy (f, temp, 4);
2332                   if (prev_insn_fixp[0])
2333                     {
2334                       prev_insn_fixp[0]->fx_frag = frag_now;
2335                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2336                     }
2337                   if (prev_insn_fixp[1])
2338                     {
2339                       prev_insn_fixp[1]->fx_frag = frag_now;
2340                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2341                     }
2342                   if (prev_insn_fixp[2])
2343                     {
2344                       prev_insn_fixp[2]->fx_frag = frag_now;
2345                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2346                     }
2347                   if (fixp[0])
2348                     {
2349                       fixp[0]->fx_frag = prev_insn_frag;
2350                       fixp[0]->fx_where = prev_insn_where;
2351                     }
2352                   if (fixp[1])
2353                     {
2354                       fixp[1]->fx_frag = prev_insn_frag;
2355                       fixp[1]->fx_where = prev_insn_where;
2356                     }
2357                   if (fixp[2])
2358                     {
2359                       fixp[2]->fx_frag = prev_insn_frag;
2360                       fixp[2]->fx_where = prev_insn_where;
2361                     }
2362                 }
2363               else
2364                 {
2365                   char *prev_f;
2366                   char temp[2];
2367
2368                   assert (prev_insn_fixp[0] == NULL);
2369                   assert (prev_insn_fixp[1] == NULL);
2370                   assert (prev_insn_fixp[2] == NULL);
2371                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2372                   memcpy (temp, prev_f, 2);
2373                   memcpy (prev_f, f, 2);
2374                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2375                     {
2376                       assert (*reloc_type == BFD_RELOC_UNUSED);
2377                       memcpy (f, temp, 2);
2378                     }
2379                   else
2380                     {
2381                       memcpy (f, f + 2, 2);
2382                       memcpy (f + 2, temp, 2);
2383                     }
2384                   if (fixp[0])
2385                     {
2386                       fixp[0]->fx_frag = prev_insn_frag;
2387                       fixp[0]->fx_where = prev_insn_where;
2388                     }
2389                   if (fixp[1])
2390                     {
2391                       fixp[1]->fx_frag = prev_insn_frag;
2392                       fixp[1]->fx_where = prev_insn_where;
2393                     }
2394                   if (fixp[2])
2395                     {
2396                       fixp[2]->fx_frag = prev_insn_frag;
2397                       fixp[2]->fx_where = prev_insn_where;
2398                     }
2399                 }
2400
2401               /* Update the previous insn information; leave prev_insn
2402                  unchanged.  */
2403               prev_prev_insn = *ip;
2404             }
2405           prev_insn_is_delay_slot = 1;
2406
2407           /* If that was an unconditional branch, forget the previous
2408              insn information.  */
2409           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2410             {
2411               prev_prev_insn.insn_mo = &dummy_opcode;
2412               prev_insn.insn_mo = &dummy_opcode;
2413             }
2414
2415           prev_insn_fixp[0] = NULL;
2416           prev_insn_fixp[1] = NULL;
2417           prev_insn_fixp[2] = NULL;
2418           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2419           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2420           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2421           prev_insn_extended = 0;
2422         }
2423       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2424         {
2425           /* We don't yet optimize a branch likely.  What we should do
2426              is look at the target, copy the instruction found there
2427              into the delay slot, and increment the branch to jump to
2428              the next instruction.  */
2429           emit_nop ();
2430           /* Update the previous insn information.  */
2431           prev_prev_insn = *ip;
2432           prev_insn.insn_mo = &dummy_opcode;
2433           prev_insn_fixp[0] = NULL;
2434           prev_insn_fixp[1] = NULL;
2435           prev_insn_fixp[2] = NULL;
2436           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2437           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2438           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2439           prev_insn_extended = 0;
2440         }
2441       else
2442         {
2443           /* Update the previous insn information.  */
2444           if (nops > 0)
2445             prev_prev_insn.insn_mo = &dummy_opcode;
2446           else
2447             prev_prev_insn = prev_insn;
2448           prev_insn = *ip;
2449
2450           /* Any time we see a branch, we always fill the delay slot
2451              immediately; since this insn is not a branch, we know it
2452              is not in a delay slot.  */
2453           prev_insn_is_delay_slot = 0;
2454
2455           prev_insn_fixp[0] = fixp[0];
2456           prev_insn_fixp[1] = fixp[1];
2457           prev_insn_fixp[2] = fixp[2];
2458           prev_insn_reloc_type[0] = reloc_type[0];
2459           prev_insn_reloc_type[1] = reloc_type[1];
2460           prev_insn_reloc_type[2] = reloc_type[2];
2461           if (mips_opts.mips16)
2462             prev_insn_extended = (ip->use_extend
2463                                   || *reloc_type > BFD_RELOC_UNUSED);
2464         }
2465
2466       prev_prev_insn_unreordered = prev_insn_unreordered;
2467       prev_insn_unreordered = 0;
2468       prev_insn_frag = frag_now;
2469       prev_insn_where = f - frag_now->fr_literal;
2470       prev_insn_valid = 1;
2471     }
2472   else if (place == NULL)
2473     {
2474       /* We need to record a bit of information even when we are not
2475          reordering, in order to determine the base address for mips16
2476          PC relative relocs.  */
2477       prev_prev_insn = prev_insn;
2478       prev_insn = *ip;
2479       prev_insn_reloc_type[0] = reloc_type[0];
2480       prev_insn_reloc_type[1] = reloc_type[1];
2481       prev_insn_reloc_type[2] = reloc_type[2];
2482       prev_prev_insn_unreordered = prev_insn_unreordered;
2483       prev_insn_unreordered = 1;
2484     }
2485
2486   /* We just output an insn, so the next one doesn't have a label.  */
2487   mips_clear_insn_labels ();
2488
2489   /* We must ensure that a fixup associated with an unmatched %hi
2490      reloc does not become a variant frag.  Otherwise, the
2491      rearrangement of %hi relocs in frob_file may confuse
2492      tc_gen_reloc.  */
2493   if (unmatched_hi)
2494     {
2495       frag_wane (frag_now);
2496       frag_new (0);
2497     }
2498 }
2499
2500 /* This function forgets that there was any previous instruction or
2501    label.  If PRESERVE is non-zero, it remembers enough information to
2502    know whether nops are needed before a noreorder section.  */
2503
2504 static void
2505 mips_no_prev_insn (preserve)
2506      int preserve;
2507 {
2508   if (! preserve)
2509     {
2510       prev_insn.insn_mo = &dummy_opcode;
2511       prev_prev_insn.insn_mo = &dummy_opcode;
2512       prev_nop_frag = NULL;
2513       prev_nop_frag_holds = 0;
2514       prev_nop_frag_required = 0;
2515       prev_nop_frag_since = 0;
2516     }
2517   prev_insn_valid = 0;
2518   prev_insn_is_delay_slot = 0;
2519   prev_insn_unreordered = 0;
2520   prev_insn_extended = 0;
2521   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2522   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2523   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2524   prev_prev_insn_unreordered = 0;
2525   mips_clear_insn_labels ();
2526 }
2527
2528 /* This function must be called whenever we turn on noreorder or emit
2529    something other than instructions.  It inserts any NOPS which might
2530    be needed by the previous instruction, and clears the information
2531    kept for the previous instructions.  The INSNS parameter is true if
2532    instructions are to follow.  */
2533
2534 static void
2535 mips_emit_delays (insns)
2536      boolean insns;
2537 {
2538   if (! mips_opts.noreorder)
2539     {
2540       int nops;
2541
2542       nops = 0;
2543       if ((! mips_opts.mips16
2544            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2545            && (! cop_interlocks
2546                && (prev_insn.insn_mo->pinfo
2547                    & (INSN_LOAD_COPROC_DELAY
2548                       | INSN_COPROC_MOVE_DELAY
2549                       | INSN_WRITE_COND_CODE))))
2550           || (! hilo_interlocks
2551               && (prev_insn.insn_mo->pinfo
2552                   & (INSN_READ_LO
2553                      | INSN_READ_HI)))
2554           || (! mips_opts.mips16
2555               && ! gpr_interlocks
2556               && (prev_insn.insn_mo->pinfo
2557                   & INSN_LOAD_MEMORY_DELAY))
2558           || (! mips_opts.mips16
2559               && mips_opts.isa == ISA_MIPS1
2560               && (prev_insn.insn_mo->pinfo
2561                   & INSN_COPROC_MEMORY_DELAY)))
2562         {
2563           /* Itbl support may require additional care here.  */
2564           ++nops;
2565           if ((! mips_opts.mips16
2566                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2567                && (! cop_interlocks
2568                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2569               || (! hilo_interlocks
2570                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2571                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2572             ++nops;
2573
2574           if (prev_insn_unreordered)
2575             nops = 0;
2576         }
2577       else if ((! mips_opts.mips16
2578                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2579                 && (! cop_interlocks
2580                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2581                || (! hilo_interlocks
2582                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2583                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2584         {
2585           /* Itbl support may require additional care here.  */
2586           if (! prev_prev_insn_unreordered)
2587             ++nops;
2588         }
2589
2590       if (nops > 0)
2591         {
2592           struct insn_label_list *l;
2593
2594           if (insns)
2595             {
2596               /* Record the frag which holds the nop instructions, so
2597                  that we can remove them if we don't need them.  */
2598               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2599               prev_nop_frag = frag_now;
2600               prev_nop_frag_holds = nops;
2601               prev_nop_frag_required = 0;
2602               prev_nop_frag_since = 0;
2603             }
2604
2605           for (; nops > 0; --nops)
2606             emit_nop ();
2607
2608           if (insns)
2609             {
2610               /* Move on to a new frag, so that it is safe to simply
2611                  decrease the size of prev_nop_frag.  */
2612               frag_wane (frag_now);
2613               frag_new (0);
2614             }
2615
2616           for (l = insn_labels; l != NULL; l = l->next)
2617             {
2618               valueT val;
2619
2620               assert (S_GET_SEGMENT (l->label) == now_seg);
2621               symbol_set_frag (l->label, frag_now);
2622               val = (valueT) frag_now_fix ();
2623               /* mips16 text labels are stored as odd.  */
2624               if (mips_opts.mips16)
2625                 ++val;
2626               S_SET_VALUE (l->label, val);
2627             }
2628         }
2629     }
2630
2631   /* Mark instruction labels in mips16 mode.  */
2632   if (insns)
2633     mips16_mark_labels ();
2634
2635   mips_no_prev_insn (insns);
2636 }
2637
2638 /* Build an instruction created by a macro expansion.  This is passed
2639    a pointer to the count of instructions created so far, an
2640    expression, the name of the instruction to build, an operand format
2641    string, and corresponding arguments.  */
2642
2643 #ifdef USE_STDARG
2644 static void
2645 macro_build (char *place,
2646              int *counter,
2647              expressionS * ep,
2648              const char *name,
2649              const char *fmt,
2650              ...)
2651 #else
2652 static void
2653 macro_build (place, counter, ep, name, fmt, va_alist)
2654      char *place;
2655      int *counter;
2656      expressionS *ep;
2657      const char *name;
2658      const char *fmt;
2659      va_dcl
2660 #endif
2661 {
2662   struct mips_cl_insn insn;
2663   bfd_reloc_code_real_type r[3];
2664   va_list args;
2665
2666 #ifdef USE_STDARG
2667   va_start (args, fmt);
2668 #else
2669   va_start (args);
2670 #endif
2671
2672   /*
2673    * If the macro is about to expand into a second instruction,
2674    * print a warning if needed. We need to pass ip as a parameter
2675    * to generate a better warning message here...
2676    */
2677   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2678     as_warn (_("Macro instruction expanded into multiple instructions"));
2679
2680   /*
2681    * If the macro is about to expand into a second instruction,
2682    * and it is in a delay slot, print a warning.
2683    */
2684   if (place == NULL
2685       && *counter == 1
2686       && mips_opts.noreorder
2687       && (prev_prev_insn.insn_mo->pinfo
2688           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2689              | INSN_COND_BRANCH_LIKELY)) != 0)
2690     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2691
2692   if (place == NULL)
2693     ++*counter;         /* bump instruction counter */
2694
2695   if (mips_opts.mips16)
2696     {
2697       mips16_macro_build (place, counter, ep, name, fmt, args);
2698       va_end (args);
2699       return;
2700     }
2701
2702   r[0] = BFD_RELOC_UNUSED;
2703   r[1] = BFD_RELOC_UNUSED;
2704   r[2] = BFD_RELOC_UNUSED;
2705   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2706   assert (insn.insn_mo);
2707   assert (strcmp (name, insn.insn_mo->name) == 0);
2708
2709   /* Search until we get a match for NAME.  */
2710   while (1)
2711     {
2712       /* It is assumed here that macros will never generate 
2713          MDMX or MIPS-3D instructions.  */
2714       if (strcmp (fmt, insn.insn_mo->args) == 0
2715           && insn.insn_mo->pinfo != INSN_MACRO
2716           && OPCODE_IS_MEMBER (insn.insn_mo,
2717                                (mips_opts.isa
2718                                 | (mips_opts.mips16 ? INSN_MIPS16 : 0)),
2719                                mips_arch)
2720           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2721         break;
2722
2723       ++insn.insn_mo;
2724       assert (insn.insn_mo->name);
2725       assert (strcmp (name, insn.insn_mo->name) == 0);
2726     }
2727
2728   insn.insn_opcode = insn.insn_mo->match;
2729   for (;;)
2730     {
2731       switch (*fmt++)
2732         {
2733         case '\0':
2734           break;
2735
2736         case ',':
2737         case '(':
2738         case ')':
2739           continue;
2740
2741         case 't':
2742         case 'w':
2743         case 'E':
2744           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2745           continue;
2746
2747         case 'c':
2748           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2749           continue;
2750
2751         case 'T':
2752         case 'W':
2753           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2754           continue;
2755
2756         case 'd':
2757         case 'G':
2758           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2759           continue;
2760
2761         case 'U':
2762           {
2763             int tmp = va_arg (args, int);
2764
2765             insn.insn_opcode |= tmp << OP_SH_RT;
2766             insn.insn_opcode |= tmp << OP_SH_RD;
2767             continue;
2768           }
2769
2770         case 'V':
2771         case 'S':
2772           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2773           continue;
2774
2775         case 'z':
2776           continue;
2777
2778         case '<':
2779           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2780           continue;
2781
2782         case 'D':
2783           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2784           continue;
2785
2786         case 'B':
2787           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2788           continue;
2789
2790         case 'J':
2791           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2792           continue;
2793
2794         case 'q':
2795           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2796           continue;
2797
2798         case 'b':
2799         case 's':
2800         case 'r':
2801         case 'v':
2802           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2803           continue;
2804
2805         case 'i':
2806         case 'j':
2807         case 'o':
2808           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2809           assert (*r == BFD_RELOC_GPREL16
2810                   || *r == BFD_RELOC_MIPS_LITERAL
2811                   || *r == BFD_RELOC_MIPS_HIGHER
2812                   || *r == BFD_RELOC_HI16_S
2813                   || *r == BFD_RELOC_LO16
2814                   || *r == BFD_RELOC_MIPS_GOT16
2815                   || *r == BFD_RELOC_MIPS_CALL16
2816                   || *r == BFD_RELOC_MIPS_GOT_DISP
2817                   || *r == BFD_RELOC_MIPS_GOT_PAGE
2818                   || *r == BFD_RELOC_MIPS_GOT_OFST
2819                   || *r == BFD_RELOC_MIPS_GOT_LO16
2820                   || *r == BFD_RELOC_MIPS_CALL_LO16
2821                   || (ep->X_op == O_subtract
2822                       && *r == BFD_RELOC_PCREL_LO16));
2823           continue;
2824
2825         case 'u':
2826           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2827           assert (ep != NULL
2828                   && (ep->X_op == O_constant
2829                       || (ep->X_op == O_symbol
2830                           && (*r == BFD_RELOC_MIPS_HIGHEST
2831                               || *r == BFD_RELOC_HI16_S
2832                               || *r == BFD_RELOC_HI16
2833                               || *r == BFD_RELOC_GPREL16
2834                               || *r == BFD_RELOC_MIPS_GOT_HI16
2835                               || *r == BFD_RELOC_MIPS_CALL_HI16))
2836                       || (ep->X_op == O_subtract
2837                           && *r == BFD_RELOC_PCREL_HI16_S)));
2838           continue;
2839
2840         case 'p':
2841           assert (ep != NULL);
2842           /*
2843            * This allows macro() to pass an immediate expression for
2844            * creating short branches without creating a symbol.
2845            * Note that the expression still might come from the assembly
2846            * input, in which case the value is not checked for range nor
2847            * is a relocation entry generated (yuck).
2848            */
2849           if (ep->X_op == O_constant)
2850             {
2851               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2852               ep = NULL;
2853             }
2854           else
2855             if (mips_pic == EMBEDDED_PIC)
2856               *r = BFD_RELOC_16_PCREL_S2;
2857             else
2858               *r = BFD_RELOC_16_PCREL;
2859           continue;
2860
2861         case 'a':
2862           assert (ep != NULL);
2863           *r = BFD_RELOC_MIPS_JMP;
2864           continue;
2865
2866         case 'C':
2867           insn.insn_opcode |= va_arg (args, unsigned long);
2868           continue;
2869
2870         default:
2871           internalError ();
2872         }
2873       break;
2874     }
2875   va_end (args);
2876   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2877
2878   append_insn (place, &insn, ep, r, false);
2879 }
2880
2881 static void
2882 mips16_macro_build (place, counter, ep, name, fmt, args)
2883      char *place;
2884      int *counter ATTRIBUTE_UNUSED;
2885      expressionS *ep;
2886      const char *name;
2887      const char *fmt;
2888      va_list args;
2889 {
2890   struct mips_cl_insn insn;
2891   bfd_reloc_code_real_type r[3]
2892     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2893
2894   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2895   assert (insn.insn_mo);
2896   assert (strcmp (name, insn.insn_mo->name) == 0);
2897
2898   while (strcmp (fmt, insn.insn_mo->args) != 0
2899          || insn.insn_mo->pinfo == INSN_MACRO)
2900     {
2901       ++insn.insn_mo;
2902       assert (insn.insn_mo->name);
2903       assert (strcmp (name, insn.insn_mo->name) == 0);
2904     }
2905
2906   insn.insn_opcode = insn.insn_mo->match;
2907   insn.use_extend = false;
2908
2909   for (;;)
2910     {
2911       int c;
2912
2913       c = *fmt++;
2914       switch (c)
2915         {
2916         case '\0':
2917           break;
2918
2919         case ',':
2920         case '(':
2921         case ')':
2922           continue;
2923
2924         case 'y':
2925         case 'w':
2926           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2927           continue;
2928
2929         case 'x':
2930         case 'v':
2931           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2932           continue;
2933
2934         case 'z':
2935           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2936           continue;
2937
2938         case 'Z':
2939           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2940           continue;
2941
2942         case '0':
2943         case 'S':
2944         case 'P':
2945         case 'R':
2946           continue;
2947
2948         case 'X':
2949           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2950           continue;
2951
2952         case 'Y':
2953           {
2954             int regno;
2955
2956             regno = va_arg (args, int);
2957             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2958             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2959           }
2960           continue;
2961
2962         case '<':
2963         case '>':
2964         case '4':
2965         case '5':
2966         case 'H':
2967         case 'W':
2968         case 'D':
2969         case 'j':
2970         case '8':
2971         case 'V':
2972         case 'C':
2973         case 'U':
2974         case 'k':
2975         case 'K':
2976         case 'p':
2977         case 'q':
2978           {
2979             assert (ep != NULL);
2980
2981             if (ep->X_op != O_constant)
2982               *r = (int) BFD_RELOC_UNUSED + c;
2983             else
2984               {
2985                 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
2986                               false, &insn.insn_opcode, &insn.use_extend,
2987                               &insn.extend);
2988                 ep = NULL;
2989                 *r = BFD_RELOC_UNUSED;
2990               }
2991           }
2992           continue;
2993
2994         case '6':
2995           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2996           continue;
2997         }
2998
2999       break;
3000     }
3001
3002   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3003
3004   append_insn (place, &insn, ep, r, false);
3005 }
3006
3007 /*
3008  * Generate a "jalr" instruction with a relocation hint to the called
3009  * function.  This occurs in NewABI PIC code.
3010  */
3011 static void
3012 macro_build_jalr (icnt, ep)
3013      int icnt;
3014      expressionS *ep;
3015 {
3016   char *f;
3017   
3018   if (HAVE_NEWABI)
3019     {
3020       frag_grow (4);
3021       f = frag_more (0);
3022     }
3023   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3024                RA, PIC_CALL_REG);
3025   if (HAVE_NEWABI)
3026     fix_new_exp (frag_now, f - frag_now->fr_literal,
3027                  0, ep, false, BFD_RELOC_MIPS_JALR);
3028 }
3029
3030 /*
3031  * Generate a "lui" instruction.
3032  */
3033 static void
3034 macro_build_lui (place, counter, ep, regnum)
3035      char *place;
3036      int *counter;
3037      expressionS *ep;
3038      int regnum;
3039 {
3040   expressionS high_expr;
3041   struct mips_cl_insn insn;
3042   bfd_reloc_code_real_type r[3]
3043     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3044   const char *name = "lui";
3045   const char *fmt = "t,u";
3046
3047   assert (! mips_opts.mips16);
3048
3049   if (place == NULL)
3050     high_expr = *ep;
3051   else
3052     {
3053       high_expr.X_op = O_constant;
3054       high_expr.X_add_number = ep->X_add_number;
3055     }
3056
3057   if (high_expr.X_op == O_constant)
3058     {
3059       /* we can compute the instruction now without a relocation entry */
3060       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3061                                 >> 16) & 0xffff;
3062       *r = BFD_RELOC_UNUSED;
3063     }
3064   else if (! HAVE_NEWABI)
3065     {
3066       assert (ep->X_op == O_symbol);
3067       /* _gp_disp is a special case, used from s_cpload.  */
3068       assert (mips_pic == NO_PIC
3069               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3070       *r = BFD_RELOC_HI16_S;
3071     }
3072
3073   /*
3074    * If the macro is about to expand into a second instruction,
3075    * print a warning if needed. We need to pass ip as a parameter
3076    * to generate a better warning message here...
3077    */
3078   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3079     as_warn (_("Macro instruction expanded into multiple instructions"));
3080
3081   if (place == NULL)
3082     ++*counter;         /* bump instruction counter */
3083
3084   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3085   assert (insn.insn_mo);
3086   assert (strcmp (name, insn.insn_mo->name) == 0);
3087   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3088
3089   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3090   if (*r == BFD_RELOC_UNUSED)
3091     {
3092       insn.insn_opcode |= high_expr.X_add_number;
3093       append_insn (place, &insn, NULL, r, false);
3094     }
3095   else
3096     append_insn (place, &insn, &high_expr, r, false);
3097 }
3098
3099 /* Generate a sequence of instructions to do a load or store from a constant
3100    offset off of a base register (breg) into/from a target register (treg),
3101    using AT if necessary.  */
3102 static void
3103 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3104      char *place;
3105      int *counter;
3106      expressionS *ep;
3107      const char *op;
3108      int treg, breg;
3109 {
3110   assert (ep->X_op == O_constant);
3111
3112   /* Right now, this routine can only handle signed 32-bit contants.  */
3113   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3114     as_warn (_("operand overflow"));
3115
3116   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3117     {
3118       /* Signed 16-bit offset will fit in the op.  Easy!  */
3119       macro_build (place, counter, ep, op, "t,o(b)", treg,
3120                    (int) BFD_RELOC_LO16, breg);
3121     }
3122   else
3123     {
3124       /* 32-bit offset, need multiple instructions and AT, like:
3125            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3126            addu     $tempreg,$tempreg,$breg
3127            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3128          to handle the complete offset.  */
3129       macro_build_lui (place, counter, ep, AT);
3130       if (place != NULL)
3131         place += 4;
3132       macro_build (place, counter, (expressionS *) NULL,
3133                    HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3134                    "d,v,t", AT, AT, breg);
3135       if (place != NULL)
3136         place += 4;
3137       macro_build (place, counter, ep, op, "t,o(b)", treg,
3138                    (int) BFD_RELOC_LO16, AT);
3139
3140       if (mips_opts.noat)
3141         as_warn (_("Macro used $at after \".set noat\""));
3142     }
3143 }
3144
3145 /*                      set_at()
3146  * Generates code to set the $at register to true (one)
3147  * if reg is less than the immediate expression.
3148  */
3149 static void
3150 set_at (counter, reg, unsignedp)
3151      int *counter;
3152      int reg;
3153      int unsignedp;
3154 {
3155   if (imm_expr.X_op == O_constant
3156       && imm_expr.X_add_number >= -0x8000
3157       && imm_expr.X_add_number < 0x8000)
3158     macro_build ((char *) NULL, counter, &imm_expr,
3159                  unsignedp ? "sltiu" : "slti",
3160                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3161   else
3162     {
3163       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3164       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3165                    unsignedp ? "sltu" : "slt",
3166                    "d,v,t", AT, reg, AT);
3167     }
3168 }
3169
3170 /* Warn if an expression is not a constant.  */
3171
3172 static void
3173 check_absolute_expr (ip, ex)
3174      struct mips_cl_insn *ip;
3175      expressionS *ex;
3176 {
3177   if (ex->X_op == O_big)
3178     as_bad (_("unsupported large constant"));
3179   else if (ex->X_op != O_constant)
3180     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3181 }
3182
3183 /* Count the leading zeroes by performing a binary chop. This is a
3184    bulky bit of source, but performance is a LOT better for the
3185    majority of values than a simple loop to count the bits:
3186        for (lcnt = 0; (lcnt < 32); lcnt++)
3187          if ((v) & (1 << (31 - lcnt)))
3188            break;
3189   However it is not code size friendly, and the gain will drop a bit
3190   on certain cached systems.
3191 */
3192 #define COUNT_TOP_ZEROES(v)             \
3193   (((v) & ~0xffff) == 0                 \
3194    ? ((v) & ~0xff) == 0                 \
3195      ? ((v) & ~0xf) == 0                \
3196        ? ((v) & ~0x3) == 0              \
3197          ? ((v) & ~0x1) == 0            \
3198            ? !(v)                       \
3199              ? 32                       \
3200              : 31                       \
3201            : 30                         \
3202          : ((v) & ~0x7) == 0            \
3203            ? 29                         \
3204            : 28                         \
3205        : ((v) & ~0x3f) == 0             \
3206          ? ((v) & ~0x1f) == 0           \
3207            ? 27                         \
3208            : 26                         \
3209          : ((v) & ~0x7f) == 0           \
3210            ? 25                         \
3211            : 24                         \
3212      : ((v) & ~0xfff) == 0              \
3213        ? ((v) & ~0x3ff) == 0            \
3214          ? ((v) & ~0x1ff) == 0          \
3215            ? 23                         \
3216            : 22                         \
3217          : ((v) & ~0x7ff) == 0          \
3218            ? 21                         \
3219            : 20                         \
3220        : ((v) & ~0x3fff) == 0           \
3221          ? ((v) & ~0x1fff) == 0         \
3222            ? 19                         \
3223            : 18                         \
3224          : ((v) & ~0x7fff) == 0         \
3225            ? 17                         \
3226            : 16                         \
3227    : ((v) & ~0xffffff) == 0             \
3228      ? ((v) & ~0xfffff) == 0            \
3229        ? ((v) & ~0x3ffff) == 0          \
3230          ? ((v) & ~0x1ffff) == 0        \
3231            ? 15                         \
3232            : 14                         \
3233          : ((v) & ~0x7ffff) == 0        \
3234            ? 13                         \
3235            : 12                         \
3236        : ((v) & ~0x3fffff) == 0         \
3237          ? ((v) & ~0x1fffff) == 0       \
3238            ? 11                         \
3239            : 10                         \
3240          : ((v) & ~0x7fffff) == 0       \
3241            ? 9                          \
3242            : 8                          \
3243      : ((v) & ~0xfffffff) == 0          \
3244        ? ((v) & ~0x3ffffff) == 0        \
3245          ? ((v) & ~0x1ffffff) == 0      \
3246            ? 7                          \
3247            : 6                          \
3248          : ((v) & ~0x7ffffff) == 0      \
3249            ? 5                          \
3250            : 4                          \
3251        : ((v) & ~0x3fffffff) == 0       \
3252          ? ((v) & ~0x1fffffff) == 0     \
3253            ? 3                          \
3254            : 2                          \
3255          : ((v) & ~0x7fffffff) == 0     \
3256            ? 1                          \
3257            : 0)
3258
3259 /*                      load_register()
3260  *  This routine generates the least number of instructions neccessary to load
3261  *  an absolute expression value into a register.
3262  */
3263 static void
3264 load_register (counter, reg, ep, dbl)
3265      int *counter;
3266      int reg;
3267      expressionS *ep;
3268      int dbl;
3269 {
3270   int freg;
3271   expressionS hi32, lo32;
3272
3273   if (ep->X_op != O_big)
3274     {
3275       assert (ep->X_op == O_constant);
3276       if (ep->X_add_number < 0x8000
3277           && (ep->X_add_number >= 0
3278               || (ep->X_add_number >= -0x8000
3279                   && (! dbl
3280                       || ! ep->X_unsigned
3281                       || sizeof (ep->X_add_number) > 4))))
3282         {
3283           /* We can handle 16 bit signed values with an addiu to
3284              $zero.  No need to ever use daddiu here, since $zero and
3285              the result are always correct in 32 bit mode.  */
3286           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3287                        (int) BFD_RELOC_LO16);
3288           return;
3289         }
3290       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3291         {
3292           /* We can handle 16 bit unsigned values with an ori to
3293              $zero.  */
3294           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3295                        (int) BFD_RELOC_LO16);
3296           return;
3297         }
3298       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3299                 && (! dbl
3300                     || ! ep->X_unsigned
3301                     || sizeof (ep->X_add_number) > 4
3302                     || (ep->X_add_number & 0x80000000) == 0))
3303                || ((HAVE_32BIT_GPRS || ! dbl)
3304                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3305                || (HAVE_32BIT_GPRS
3306                    && ! dbl
3307                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3308                        == ~ (offsetT) 0xffffffff)))
3309         {
3310           /* 32 bit values require an lui.  */
3311           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3312                        (int) BFD_RELOC_HI16);
3313           if ((ep->X_add_number & 0xffff) != 0)
3314             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3315                          (int) BFD_RELOC_LO16);
3316           return;
3317         }
3318     }
3319
3320   /* The value is larger than 32 bits.  */
3321
3322   if (HAVE_32BIT_GPRS)
3323     {
3324       as_bad (_("Number (0x%lx) larger than 32 bits"),
3325               (unsigned long) ep->X_add_number);
3326       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3327                    (int) BFD_RELOC_LO16);
3328       return;
3329     }
3330
3331   if (ep->X_op != O_big)
3332     {
3333       hi32 = *ep;
3334       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3335       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3336       hi32.X_add_number &= 0xffffffff;
3337       lo32 = *ep;
3338       lo32.X_add_number &= 0xffffffff;
3339     }
3340   else
3341     {
3342       assert (ep->X_add_number > 2);
3343       if (ep->X_add_number == 3)
3344         generic_bignum[3] = 0;
3345       else if (ep->X_add_number > 4)
3346         as_bad (_("Number larger than 64 bits"));
3347       lo32.X_op = O_constant;
3348       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3349       hi32.X_op = O_constant;
3350       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3351     }
3352
3353   if (hi32.X_add_number == 0)
3354     freg = 0;
3355   else
3356     {
3357       int shift, bit;
3358       unsigned long hi, lo;
3359
3360       if (hi32.X_add_number == (offsetT) 0xffffffff)
3361         {
3362           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3363             {
3364               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3365                            reg, 0, (int) BFD_RELOC_LO16);
3366               return;
3367             }
3368           if (lo32.X_add_number & 0x80000000)
3369             {
3370               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3371                            (int) BFD_RELOC_HI16);
3372               if (lo32.X_add_number & 0xffff)
3373                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3374                              reg, reg, (int) BFD_RELOC_LO16);
3375               return;
3376             }
3377         }
3378
3379       /* Check for 16bit shifted constant.  We know that hi32 is
3380          non-zero, so start the mask on the first bit of the hi32
3381          value.  */
3382       shift = 17;
3383       do
3384         {
3385           unsigned long himask, lomask;
3386
3387           if (shift < 32)
3388             {
3389               himask = 0xffff >> (32 - shift);
3390               lomask = (0xffff << shift) & 0xffffffff;
3391             }
3392           else
3393             {
3394               himask = 0xffff << (shift - 32);
3395               lomask = 0;
3396             }
3397           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3398               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3399             {
3400               expressionS tmp;
3401
3402               tmp.X_op = O_constant;
3403               if (shift < 32)
3404                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3405                                     | (lo32.X_add_number >> shift));
3406               else
3407                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3408               macro_build ((char *) NULL, counter, &tmp,
3409                            "ori", "t,r,i", reg, 0,
3410                            (int) BFD_RELOC_LO16);
3411               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3412                            (shift >= 32) ? "dsll32" : "dsll",
3413                            "d,w,<", reg, reg,
3414                            (shift >= 32) ? shift - 32 : shift);
3415               return;
3416             }
3417           ++shift;
3418         }
3419       while (shift <= (64 - 16));
3420
3421       /* Find the bit number of the lowest one bit, and store the
3422          shifted value in hi/lo.  */
3423       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3424       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3425       if (lo != 0)
3426         {
3427           bit = 0;
3428           while ((lo & 1) == 0)
3429             {
3430               lo >>= 1;
3431               ++bit;
3432             }
3433           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3434           hi >>= bit;
3435         }
3436       else
3437         {
3438           bit = 32;
3439           while ((hi & 1) == 0)
3440             {
3441               hi >>= 1;
3442               ++bit;
3443             }
3444           lo = hi;
3445           hi = 0;
3446         }
3447
3448       /* Optimize if the shifted value is a (power of 2) - 1.  */
3449       if ((hi == 0 && ((lo + 1) & lo) == 0)
3450           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3451         {
3452           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3453           if (shift != 0)
3454             {
3455               expressionS tmp;
3456
3457               /* This instruction will set the register to be all
3458                  ones.  */
3459               tmp.X_op = O_constant;
3460               tmp.X_add_number = (offsetT) -1;
3461               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3462                            reg, 0, (int) BFD_RELOC_LO16);
3463               if (bit != 0)
3464                 {
3465                   bit += shift;
3466                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3467                                (bit >= 32) ? "dsll32" : "dsll",
3468                                "d,w,<", reg, reg,
3469                                (bit >= 32) ? bit - 32 : bit);
3470                 }
3471               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3472                            (shift >= 32) ? "dsrl32" : "dsrl",
3473                            "d,w,<", reg, reg,
3474                            (shift >= 32) ? shift - 32 : shift);
3475               return;
3476             }
3477         }
3478
3479       /* Sign extend hi32 before calling load_register, because we can
3480          generally get better code when we load a sign extended value.  */
3481       if ((hi32.X_add_number & 0x80000000) != 0)
3482         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3483       load_register (counter, reg, &hi32, 0);
3484       freg = reg;
3485     }
3486   if ((lo32.X_add_number & 0xffff0000) == 0)
3487     {
3488       if (freg != 0)
3489         {
3490           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3491                        "dsll32", "d,w,<", reg, freg, 0);
3492           freg = reg;
3493         }
3494     }
3495   else
3496     {
3497       expressionS mid16;
3498
3499       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3500         {
3501           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3502                        (int) BFD_RELOC_HI16);
3503           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3504                        "dsrl32", "d,w,<", reg, reg, 0);
3505           return;
3506         }
3507
3508       if (freg != 0)
3509         {
3510           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3511                        "d,w,<", reg, freg, 16);
3512           freg = reg;
3513         }
3514       mid16 = lo32;
3515       mid16.X_add_number >>= 16;
3516       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3517                    freg, (int) BFD_RELOC_LO16);
3518       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3519                    "d,w,<", reg, reg, 16);
3520       freg = reg;
3521     }
3522   if ((lo32.X_add_number & 0xffff) != 0)
3523     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3524                  (int) BFD_RELOC_LO16);
3525 }
3526
3527 /* Load an address into a register.  */
3528
3529 static void
3530 load_address (counter, reg, ep, used_at)
3531      int *counter;
3532      int reg;
3533      expressionS *ep;
3534      int *used_at;
3535 {
3536   char *p = NULL;
3537
3538   if (ep->X_op != O_constant
3539       && ep->X_op != O_symbol)
3540     {
3541       as_bad (_("expression too complex"));
3542       ep->X_op = O_constant;
3543     }
3544
3545   if (ep->X_op == O_constant)
3546     {
3547       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3548       return;
3549     }
3550
3551   if (mips_pic == NO_PIC)
3552     {
3553       /* If this is a reference to a GP relative symbol, we want
3554            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3555          Otherwise we want
3556            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3557            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3558          If we have an addend, we always use the latter form.
3559
3560          With 64bit address space and a usable $at we want
3561            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3562            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3563            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3564            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3565            dsll32       $reg,0
3566            daddu        $reg,$reg,$at
3567
3568          If $at is already in use, we use an path which is suboptimal
3569          on superscalar processors.
3570            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3571            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3572            dsll         $reg,16
3573            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3574            dsll         $reg,16
3575            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3576        */
3577       if (HAVE_64BIT_ADDRESSES)
3578         {
3579           /* We don't do GP optimization for now because RELAX_ENCODE can't
3580              hold the data for such large chunks.  */
3581
3582           if (*used_at == 0 && ! mips_opts.noat)
3583             {
3584               macro_build (p, counter, ep, "lui", "t,u",
3585                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3586               macro_build (p, counter, ep, "lui", "t,u",
3587                            AT, (int) BFD_RELOC_HI16_S);
3588               macro_build (p, counter, ep, "daddiu", "t,r,j",
3589                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3590               macro_build (p, counter, ep, "daddiu", "t,r,j",
3591                            AT, AT, (int) BFD_RELOC_LO16);
3592               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3593                            "d,w,<", reg, reg, 0);
3594               macro_build (p, counter, (expressionS *) NULL, "daddu",
3595                            "d,v,t", reg, reg, AT);
3596               *used_at = 1;
3597             }
3598           else
3599             {
3600               macro_build (p, counter, ep, "lui", "t,u",
3601                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3602               macro_build (p, counter, ep, "daddiu", "t,r,j",
3603                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3604               macro_build (p, counter, (expressionS *) NULL, "dsll",
3605                            "d,w,<", reg, reg, 16);
3606               macro_build (p, counter, ep, "daddiu", "t,r,j",
3607                            reg, reg, (int) BFD_RELOC_HI16_S);
3608               macro_build (p, counter, (expressionS *) NULL, "dsll",
3609                            "d,w,<", reg, reg, 16);
3610               macro_build (p, counter, ep, "daddiu", "t,r,j",
3611                            reg, reg, (int) BFD_RELOC_LO16);
3612             }
3613         }
3614       else
3615         {
3616           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3617               && ! nopic_need_relax (ep->X_add_symbol, 1))
3618             {
3619               frag_grow (20);
3620               macro_build ((char *) NULL, counter, ep,
3621                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3622                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3623               p = frag_var (rs_machine_dependent, 8, 0,
3624                             RELAX_ENCODE (4, 8, 0, 4, 0,
3625                                           mips_opts.warn_about_macros),
3626                             ep->X_add_symbol, 0, NULL);
3627             }
3628           macro_build_lui (p, counter, ep, reg);
3629           if (p != NULL)
3630             p += 4;
3631           macro_build (p, counter, ep,
3632                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3633                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3634         }
3635     }
3636   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3637     {
3638       expressionS ex;
3639
3640       /* If this is a reference to an external symbol, we want
3641            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3642          Otherwise we want
3643            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3644            nop
3645            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3646          If we have NewABI, we want
3647            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
3648          If there is a constant, it must be added in after.  */
3649       ex.X_add_number = ep->X_add_number;
3650       ep->X_add_number = 0;
3651       frag_grow (20);
3652       if (HAVE_NEWABI)
3653         {
3654           macro_build ((char *) NULL, counter, ep,
3655                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3656                        (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3657         }
3658       else
3659         {
3660           macro_build ((char *) NULL, counter, ep,
3661                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3662                        reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3663           macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3664           p = frag_var (rs_machine_dependent, 4, 0,
3665                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3666                         ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3667           macro_build (p, counter, ep,
3668                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3669                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3670         }
3671
3672       if (ex.X_add_number != 0)
3673         {
3674           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3675             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3676           ex.X_op = O_constant;
3677           macro_build ((char *) NULL, counter, &ex,
3678                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3679                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3680         }
3681     }
3682   else if (mips_pic == SVR4_PIC)
3683     {
3684       expressionS ex;
3685       int off;
3686
3687       /* This is the large GOT case.  If this is a reference to an
3688          external symbol, we want
3689            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3690            addu         $reg,$reg,$gp
3691            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3692          Otherwise, for a reference to a local symbol, we want
3693            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3694            nop
3695            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3696          If we have NewABI, we want
3697            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3698            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3699          If there is a constant, it must be added in after.  */
3700       ex.X_add_number = ep->X_add_number;
3701       ep->X_add_number = 0;
3702       if (HAVE_NEWABI)
3703         {
3704           macro_build ((char *) NULL, counter, ep,
3705                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3706                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3707           macro_build (p, counter, ep,
3708                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3709                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3710         }
3711       else
3712         {
3713           if (reg_needs_delay (mips_gp_register))
3714             off = 4;
3715           else
3716             off = 0;
3717           frag_grow (32);
3718           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3719                        (int) BFD_RELOC_MIPS_GOT_HI16);
3720           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3721                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3722                        reg, mips_gp_register);
3723           macro_build ((char *) NULL, counter, ep,
3724                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3725                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3726           p = frag_var (rs_machine_dependent, 12 + off, 0,
3727                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3728                                       mips_opts.warn_about_macros),
3729                         ep->X_add_symbol, 0, NULL);
3730           if (off > 0)
3731             {
3732               /* We need a nop before loading from $gp.  This special
3733                  check is required because the lui which starts the main
3734                  instruction stream does not refer to $gp, and so will not
3735                  insert the nop which may be required.  */
3736               macro_build (p, counter, (expressionS *) NULL, "nop", "");
3737                 p += 4;
3738             }
3739           macro_build (p, counter, ep,
3740                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3741                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3742           p += 4;
3743           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3744           p += 4;
3745           macro_build (p, counter, ep,
3746                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3747                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3748         }
3749
3750       if (ex.X_add_number != 0)
3751         {
3752           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3753             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3754           ex.X_op = O_constant;
3755           macro_build ((char *) NULL, counter, &ex,
3756                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3757                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3758         }
3759     }
3760   else if (mips_pic == EMBEDDED_PIC)
3761     {
3762       /* We always do
3763            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3764        */
3765       macro_build ((char *) NULL, counter, ep,
3766                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3767                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3768     }
3769   else
3770     abort ();
3771 }
3772
3773 /* Move the contents of register SOURCE into register DEST.  */
3774
3775 static void
3776 move_register (counter, dest, source)
3777      int *counter;
3778      int dest;
3779      int source;
3780 {
3781   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3782                HAVE_32BIT_GPRS ? "addu" : "daddu",
3783                "d,v,t", dest, source, 0);
3784 }
3785
3786 /*
3787  *                      Build macros
3788  *   This routine implements the seemingly endless macro or synthesized
3789  * instructions and addressing modes in the mips assembly language. Many
3790  * of these macros are simple and are similar to each other. These could
3791  * probably be handled by some kind of table or grammer aproach instead of
3792  * this verbose method. Others are not simple macros but are more like
3793  * optimizing code generation.
3794  *   One interesting optimization is when several store macros appear
3795  * consecutivly that would load AT with the upper half of the same address.
3796  * The ensuing load upper instructions are ommited. This implies some kind
3797  * of global optimization. We currently only optimize within a single macro.
3798  *   For many of the load and store macros if the address is specified as a
3799  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3800  * first load register 'at' with zero and use it as the base register. The
3801  * mips assembler simply uses register $zero. Just one tiny optimization
3802  * we're missing.
3803  */
3804 static void
3805 macro (ip)
3806      struct mips_cl_insn *ip;
3807 {
3808   register int treg, sreg, dreg, breg;
3809   int tempreg;
3810   int mask;
3811   int icnt = 0;
3812   int used_at = 0;
3813   expressionS expr1;
3814   const char *s;
3815   const char *s2;
3816   const char *fmt;
3817   int likely = 0;
3818   int dbl = 0;
3819   int coproc = 0;
3820   int lr = 0;
3821   int imm = 0;
3822   offsetT maxnum;
3823   int off;
3824   bfd_reloc_code_real_type r;
3825   int hold_mips_optimize;
3826
3827   assert (! mips_opts.mips16);
3828
3829   treg = (ip->insn_opcode >> 16) & 0x1f;
3830   dreg = (ip->insn_opcode >> 11) & 0x1f;
3831   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3832   mask = ip->insn_mo->mask;
3833
3834   expr1.X_op = O_constant;
3835   expr1.X_op_symbol = NULL;
3836   expr1.X_add_symbol = NULL;
3837   expr1.X_add_number = 1;
3838
3839   switch (mask)
3840     {
3841     case M_DABS:
3842       dbl = 1;
3843     case M_ABS:
3844       /* bgez $a0,.+12
3845          move v0,$a0
3846          sub v0,$zero,$a0
3847          */
3848
3849       mips_emit_delays (true);
3850       ++mips_opts.noreorder;
3851       mips_any_noreorder = 1;
3852
3853       expr1.X_add_number = 8;
3854       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3855       if (dreg == sreg)
3856         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3857                      0);
3858       else
3859         move_register (&icnt, dreg, sreg);
3860       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3861                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3862
3863       --mips_opts.noreorder;
3864       return;
3865
3866     case M_ADD_I:
3867       s = "addi";
3868       s2 = "add";
3869       goto do_addi;
3870     case M_ADDU_I:
3871       s = "addiu";
3872       s2 = "addu";
3873       goto do_addi;
3874     case M_DADD_I:
3875       dbl = 1;
3876       s = "daddi";
3877       s2 = "dadd";
3878       goto do_addi;
3879     case M_DADDU_I:
3880       dbl = 1;
3881       s = "daddiu";
3882       s2 = "daddu";
3883     do_addi:
3884       if (imm_expr.X_op == O_constant
3885           && imm_expr.X_add_number >= -0x8000
3886           && imm_expr.X_add_number < 0x8000)
3887         {
3888           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3889                        (int) BFD_RELOC_LO16);
3890           return;
3891         }
3892       load_register (&icnt, AT, &imm_expr, dbl);
3893       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3894                    treg, sreg, AT);
3895       break;
3896
3897     case M_AND_I:
3898       s = "andi";
3899       s2 = "and";
3900       goto do_bit;
3901     case M_OR_I:
3902       s = "ori";
3903       s2 = "or";
3904       goto do_bit;
3905     case M_NOR_I:
3906       s = "";
3907       s2 = "nor";
3908       goto do_bit;
3909     case M_XOR_I:
3910       s = "xori";
3911       s2 = "xor";
3912     do_bit:
3913       if (imm_expr.X_op == O_constant
3914           && imm_expr.X_add_number >= 0
3915           && imm_expr.X_add_number < 0x10000)
3916         {
3917           if (mask != M_NOR_I)
3918             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3919                          sreg, (int) BFD_RELOC_LO16);
3920           else
3921             {
3922               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3923                            treg, sreg, (int) BFD_RELOC_LO16);
3924               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3925                            "d,v,t", treg, treg, 0);
3926             }
3927           return;
3928         }
3929
3930       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3931       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3932                    treg, sreg, AT);
3933       break;
3934
3935     case M_BEQ_I:
3936       s = "beq";
3937       goto beq_i;
3938     case M_BEQL_I:
3939       s = "beql";
3940       likely = 1;
3941       goto beq_i;
3942     case M_BNE_I:
3943       s = "bne";
3944       goto beq_i;
3945     case M_BNEL_I:
3946       s = "bnel";
3947       likely = 1;
3948     beq_i:
3949       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3950         {
3951           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3952                        0);
3953           return;
3954         }
3955       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3956       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3957       break;
3958
3959     case M_BGEL:
3960       likely = 1;
3961     case M_BGE:
3962       if (treg == 0)
3963         {
3964           macro_build ((char *) NULL, &icnt, &offset_expr,
3965                        likely ? "bgezl" : "bgez", "s,p", sreg);
3966           return;
3967         }
3968       if (sreg == 0)
3969         {
3970           macro_build ((char *) NULL, &icnt, &offset_expr,
3971                        likely ? "blezl" : "blez", "s,p", treg);
3972           return;
3973         }
3974       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
3975                    AT, sreg, treg);
3976       macro_build ((char *) NULL, &icnt, &offset_expr,
3977                    likely ? "beql" : "beq", "s,t,p", AT, 0);
3978       break;
3979
3980     case M_BGTL_I:
3981       likely = 1;
3982     case M_BGT_I:
3983       /* check for > max integer */
3984       maxnum = 0x7fffffff;
3985       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3986         {
3987           maxnum <<= 16;
3988           maxnum |= 0xffff;
3989           maxnum <<= 16;
3990           maxnum |= 0xffff;
3991         }
3992       if (imm_expr.X_op == O_constant
3993           && imm_expr.X_add_number >= maxnum
3994           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3995         {
3996         do_false:
3997           /* result is always false */
3998           if (! likely)
3999             {
4000               if (warn_nops)
4001                 as_warn (_("Branch %s is always false (nop)"),
4002                          ip->insn_mo->name);
4003               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4004                            "", 0);
4005             }
4006           else
4007             {
4008               if (warn_nops)
4009                 as_warn (_("Branch likely %s is always false"),
4010                          ip->insn_mo->name);
4011               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4012                            "s,t,p", 0, 0);
4013             }
4014           return;
4015         }
4016       if (imm_expr.X_op != O_constant)
4017         as_bad (_("Unsupported large constant"));
4018       ++imm_expr.X_add_number;
4019       /* FALLTHROUGH */
4020     case M_BGE_I:
4021     case M_BGEL_I:
4022       if (mask == M_BGEL_I)
4023         likely = 1;
4024       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4025         {
4026           macro_build ((char *) NULL, &icnt, &offset_expr,
4027                        likely ? "bgezl" : "bgez", "s,p", sreg);
4028           return;
4029         }
4030       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4031         {
4032           macro_build ((char *) NULL, &icnt, &offset_expr,
4033                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4034           return;
4035         }
4036       maxnum = 0x7fffffff;
4037       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4038         {
4039           maxnum <<= 16;
4040           maxnum |= 0xffff;
4041           maxnum <<= 16;
4042           maxnum |= 0xffff;
4043         }
4044       maxnum = - maxnum - 1;
4045       if (imm_expr.X_op == O_constant
4046           && imm_expr.X_add_number <= maxnum
4047           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4048         {
4049         do_true:
4050           /* result is always true */
4051           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4052           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4053           return;
4054         }
4055       set_at (&icnt, sreg, 0);
4056       macro_build ((char *) NULL, &icnt, &offset_expr,
4057                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4058       break;
4059
4060     case M_BGEUL:
4061       likely = 1;
4062     case M_BGEU:
4063       if (treg == 0)
4064         goto do_true;
4065       if (sreg == 0)
4066         {
4067           macro_build ((char *) NULL, &icnt, &offset_expr,
4068                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4069           return;
4070         }
4071       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4072                    "d,v,t", AT, sreg, treg);
4073       macro_build ((char *) NULL, &icnt, &offset_expr,
4074                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4075       break;
4076
4077     case M_BGTUL_I:
4078       likely = 1;
4079     case M_BGTU_I:
4080       if (sreg == 0
4081           || (HAVE_32BIT_GPRS
4082               && imm_expr.X_op == O_constant
4083               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4084         goto do_false;
4085       if (imm_expr.X_op != O_constant)
4086         as_bad (_("Unsupported large constant"));
4087       ++imm_expr.X_add_number;
4088       /* FALLTHROUGH */
4089     case M_BGEU_I:
4090     case M_BGEUL_I:
4091       if (mask == M_BGEUL_I)
4092         likely = 1;
4093       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4094         goto do_true;
4095       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4096         {
4097           macro_build ((char *) NULL, &icnt, &offset_expr,
4098                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4099           return;
4100         }
4101       set_at (&icnt, sreg, 1);
4102       macro_build ((char *) NULL, &icnt, &offset_expr,
4103                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4104       break;
4105
4106     case M_BGTL:
4107       likely = 1;
4108     case M_BGT:
4109       if (treg == 0)
4110         {
4111           macro_build ((char *) NULL, &icnt, &offset_expr,
4112                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4113           return;
4114         }
4115       if (sreg == 0)
4116         {
4117           macro_build ((char *) NULL, &icnt, &offset_expr,
4118                        likely ? "bltzl" : "bltz", "s,p", treg);
4119           return;
4120         }
4121       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4122                    AT, treg, sreg);
4123       macro_build ((char *) NULL, &icnt, &offset_expr,
4124                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4125       break;
4126
4127     case M_BGTUL:
4128       likely = 1;
4129     case M_BGTU:
4130       if (treg == 0)
4131         {
4132           macro_build ((char *) NULL, &icnt, &offset_expr,
4133                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4134           return;
4135         }
4136       if (sreg == 0)
4137         goto do_false;
4138       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4139                    "d,v,t", AT, treg, sreg);
4140       macro_build ((char *) NULL, &icnt, &offset_expr,
4141                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4142       break;
4143
4144     case M_BLEL:
4145       likely = 1;
4146     case M_BLE:
4147       if (treg == 0)
4148         {
4149           macro_build ((char *) NULL, &icnt, &offset_expr,
4150                        likely ? "blezl" : "blez", "s,p", sreg);
4151           return;
4152         }
4153       if (sreg == 0)
4154         {
4155           macro_build ((char *) NULL, &icnt, &offset_expr,
4156                        likely ? "bgezl" : "bgez", "s,p", treg);
4157           return;
4158         }
4159       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4160                    AT, treg, sreg);
4161       macro_build ((char *) NULL, &icnt, &offset_expr,
4162                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4163       break;
4164
4165     case M_BLEL_I:
4166       likely = 1;
4167     case M_BLE_I:
4168       maxnum = 0x7fffffff;
4169       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4170         {
4171           maxnum <<= 16;
4172           maxnum |= 0xffff;
4173           maxnum <<= 16;
4174           maxnum |= 0xffff;
4175         }
4176       if (imm_expr.X_op == O_constant
4177           && imm_expr.X_add_number >= maxnum
4178           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4179         goto do_true;
4180       if (imm_expr.X_op != O_constant)
4181         as_bad (_("Unsupported large constant"));
4182       ++imm_expr.X_add_number;
4183       /* FALLTHROUGH */
4184     case M_BLT_I:
4185     case M_BLTL_I:
4186       if (mask == M_BLTL_I)
4187         likely = 1;
4188       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4189         {
4190           macro_build ((char *) NULL, &icnt, &offset_expr,
4191                        likely ? "bltzl" : "bltz", "s,p", sreg);
4192           return;
4193         }
4194       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4195         {
4196           macro_build ((char *) NULL, &icnt, &offset_expr,
4197                        likely ? "blezl" : "blez", "s,p", sreg);
4198           return;
4199         }
4200       set_at (&icnt, sreg, 0);
4201       macro_build ((char *) NULL, &icnt, &offset_expr,
4202                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4203       break;
4204
4205     case M_BLEUL:
4206       likely = 1;
4207     case M_BLEU:
4208       if (treg == 0)
4209         {
4210           macro_build ((char *) NULL, &icnt, &offset_expr,
4211                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4212           return;
4213         }
4214       if (sreg == 0)
4215         goto do_true;
4216       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4217                    "d,v,t", AT, treg, sreg);
4218       macro_build ((char *) NULL, &icnt, &offset_expr,
4219                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4220       break;
4221
4222     case M_BLEUL_I:
4223       likely = 1;
4224     case M_BLEU_I:
4225       if (sreg == 0
4226           || (HAVE_32BIT_GPRS
4227               && imm_expr.X_op == O_constant
4228               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4229         goto do_true;
4230       if (imm_expr.X_op != O_constant)
4231         as_bad (_("Unsupported large constant"));
4232       ++imm_expr.X_add_number;
4233       /* FALLTHROUGH */
4234     case M_BLTU_I:
4235     case M_BLTUL_I:
4236       if (mask == M_BLTUL_I)
4237         likely = 1;
4238       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4239         goto do_false;
4240       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4241         {
4242           macro_build ((char *) NULL, &icnt, &offset_expr,
4243                        likely ? "beql" : "beq",
4244                        "s,t,p", sreg, 0);
4245           return;
4246         }
4247       set_at (&icnt, sreg, 1);
4248       macro_build ((char *) NULL, &icnt, &offset_expr,
4249                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4250       break;
4251
4252     case M_BLTL:
4253       likely = 1;
4254     case M_BLT:
4255       if (treg == 0)
4256         {
4257           macro_build ((char *) NULL, &icnt, &offset_expr,
4258                        likely ? "bltzl" : "bltz", "s,p", sreg);
4259           return;
4260         }
4261       if (sreg == 0)
4262         {
4263           macro_build ((char *) NULL, &icnt, &offset_expr,
4264                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4265           return;
4266         }
4267       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4268                    AT, sreg, treg);
4269       macro_build ((char *) NULL, &icnt, &offset_expr,
4270                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4271       break;
4272
4273     case M_BLTUL:
4274       likely = 1;
4275     case M_BLTU:
4276       if (treg == 0)
4277         goto do_false;
4278       if (sreg == 0)
4279         {
4280           macro_build ((char *) NULL, &icnt, &offset_expr,
4281                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4282           return;
4283         }
4284       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4285                    "d,v,t", AT, sreg,
4286                    treg);
4287       macro_build ((char *) NULL, &icnt, &offset_expr,
4288                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4289       break;
4290
4291     case M_DDIV_3:
4292       dbl = 1;
4293     case M_DIV_3:
4294       s = "mflo";
4295       goto do_div3;
4296     case M_DREM_3:
4297       dbl = 1;
4298     case M_REM_3:
4299       s = "mfhi";
4300     do_div3:
4301       if (treg == 0)
4302         {
4303           as_warn (_("Divide by zero."));
4304           if (mips_trap)
4305             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4306                          "s,t,q", 0, 0, 7);
4307           else
4308             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4309                          "c", 7);
4310           return;
4311         }
4312
4313       mips_emit_delays (true);
4314       ++mips_opts.noreorder;
4315       mips_any_noreorder = 1;
4316       if (mips_trap)
4317         {
4318           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4319                        "s,t,q", treg, 0, 7);
4320           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4321                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4322         }
4323       else
4324         {
4325           expr1.X_add_number = 8;
4326           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4327           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4328                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4329           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4330                        "c", 7);
4331         }
4332       expr1.X_add_number = -1;
4333       macro_build ((char *) NULL, &icnt, &expr1,
4334                    dbl ? "daddiu" : "addiu",
4335                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4336       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4337       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4338       if (dbl)
4339         {
4340           expr1.X_add_number = 1;
4341           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4342                        (int) BFD_RELOC_LO16);
4343           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4344                        "d,w,<", AT, AT, 31);
4345         }
4346       else
4347         {
4348           expr1.X_add_number = 0x80000000;
4349           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4350                        (int) BFD_RELOC_HI16);
4351         }
4352       if (mips_trap)
4353         {
4354           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4355                        "s,t,q", sreg, AT, 6);
4356           /* We want to close the noreorder block as soon as possible, so
4357              that later insns are available for delay slot filling.  */
4358           --mips_opts.noreorder;
4359         }
4360       else
4361         {
4362           expr1.X_add_number = 8;
4363           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4364           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4365                        0);
4366
4367           /* We want to close the noreorder block as soon as possible, so
4368              that later insns are available for delay slot filling.  */
4369           --mips_opts.noreorder;
4370
4371           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4372                        "c", 6);
4373         }
4374       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4375       break;
4376
4377     case M_DIV_3I:
4378       s = "div";
4379       s2 = "mflo";
4380       goto do_divi;
4381     case M_DIVU_3I:
4382       s = "divu";
4383       s2 = "mflo";
4384       goto do_divi;
4385     case M_REM_3I:
4386       s = "div";
4387       s2 = "mfhi";
4388       goto do_divi;
4389     case M_REMU_3I:
4390       s = "divu";
4391       s2 = "mfhi";
4392       goto do_divi;
4393     case M_DDIV_3I:
4394       dbl = 1;
4395       s = "ddiv";
4396       s2 = "mflo";
4397       goto do_divi;
4398     case M_DDIVU_3I:
4399       dbl = 1;
4400       s = "ddivu";
4401       s2 = "mflo";
4402       goto do_divi;
4403     case M_DREM_3I:
4404       dbl = 1;
4405       s = "ddiv";
4406       s2 = "mfhi";
4407       goto do_divi;
4408     case M_DREMU_3I:
4409       dbl = 1;
4410       s = "ddivu";
4411       s2 = "mfhi";
4412     do_divi:
4413       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4414         {
4415           as_warn (_("Divide by zero."));
4416           if (mips_trap)
4417             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4418                          "s,t,q", 0, 0, 7);
4419           else
4420             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4421                          "c", 7);
4422           return;
4423         }
4424       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4425         {
4426           if (strcmp (s2, "mflo") == 0)
4427             move_register (&icnt, dreg, sreg);
4428           else
4429             move_register (&icnt, dreg, 0);
4430           return;
4431         }
4432       if (imm_expr.X_op == O_constant
4433           && imm_expr.X_add_number == -1
4434           && s[strlen (s) - 1] != 'u')
4435         {
4436           if (strcmp (s2, "mflo") == 0)
4437             {
4438               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4439                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4440             }
4441           else
4442             move_register (&icnt, dreg, 0);
4443           return;
4444         }
4445
4446       load_register (&icnt, AT, &imm_expr, dbl);
4447       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4448                    sreg, AT);
4449       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4450       break;
4451
4452     case M_DIVU_3:
4453       s = "divu";
4454       s2 = "mflo";
4455       goto do_divu3;
4456     case M_REMU_3:
4457       s = "divu";
4458       s2 = "mfhi";
4459       goto do_divu3;
4460     case M_DDIVU_3:
4461       s = "ddivu";
4462       s2 = "mflo";
4463       goto do_divu3;
4464     case M_DREMU_3:
4465       s = "ddivu";
4466       s2 = "mfhi";
4467     do_divu3:
4468       mips_emit_delays (true);
4469       ++mips_opts.noreorder;
4470       mips_any_noreorder = 1;
4471       if (mips_trap)
4472         {
4473           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4474                        "s,t,q", treg, 0, 7);
4475           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4476                        sreg, treg);
4477           /* We want to close the noreorder block as soon as possible, so
4478              that later insns are available for delay slot filling.  */
4479           --mips_opts.noreorder;
4480         }
4481       else
4482         {
4483           expr1.X_add_number = 8;
4484           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4485           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4486                        sreg, treg);
4487
4488           /* We want to close the noreorder block as soon as possible, so
4489              that later insns are available for delay slot filling.  */
4490           --mips_opts.noreorder;
4491           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4492                        "c", 7);
4493         }
4494       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4495       return;
4496
4497     case M_DLA_AB:
4498       dbl = 1;
4499     case M_LA_AB:
4500       /* Load the address of a symbol into a register.  If breg is not
4501          zero, we then add a base register to it.  */
4502
4503       if (dbl && HAVE_32BIT_GPRS)
4504         as_warn (_("dla used to load 32-bit register"));
4505
4506       if (! dbl && HAVE_64BIT_OBJECTS)
4507         as_warn (_("la used to load 64-bit address"));
4508
4509       if (offset_expr.X_op == O_constant
4510           && offset_expr.X_add_number >= -0x8000
4511           && offset_expr.X_add_number < 0x8000)
4512         {
4513           macro_build ((char *) NULL, &icnt, &offset_expr,
4514                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4515                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4516           return;
4517         }
4518
4519       if (treg == breg)
4520         {
4521           tempreg = AT;
4522           used_at = 1;
4523         }
4524       else
4525         {
4526           tempreg = treg;
4527           used_at = 0;
4528         }
4529
4530       /* When generating embedded PIC code, we permit expressions of
4531          the form
4532            la   $treg,foo-bar
4533            la   $treg,foo-bar($breg)
4534          where bar is an address in the current section.  These are used
4535          when getting the addresses of functions.  We don't permit
4536          X_add_number to be non-zero, because if the symbol is
4537          external the relaxing code needs to know that any addend is
4538          purely the offset to X_op_symbol.  */
4539       if (mips_pic == EMBEDDED_PIC
4540           && offset_expr.X_op == O_subtract
4541           && (symbol_constant_p (offset_expr.X_op_symbol)
4542               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4543               : (symbol_equated_p (offset_expr.X_op_symbol)
4544                  && (S_GET_SEGMENT
4545                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4546                       ->X_add_symbol)
4547                      == now_seg)))
4548           && (offset_expr.X_add_number == 0
4549               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4550         {
4551           if (breg == 0)
4552             {
4553               tempreg = treg;
4554               used_at = 0;
4555               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4556                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4557             }
4558           else
4559             {
4560               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4561                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4562               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4563                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4564                            "d,v,t", tempreg, tempreg, breg);
4565             }
4566           macro_build ((char *) NULL, &icnt, &offset_expr,
4567                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4568                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4569           if (! used_at)
4570             return;
4571           break;
4572         }
4573
4574       if (offset_expr.X_op != O_symbol
4575           && offset_expr.X_op != O_constant)
4576         {
4577           as_bad (_("expression too complex"));
4578           offset_expr.X_op = O_constant;
4579         }
4580
4581       if (offset_expr.X_op == O_constant)
4582         load_register (&icnt, tempreg, &offset_expr,
4583                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4584                         ? (dbl || HAVE_64BIT_ADDRESSES)
4585                         : HAVE_64BIT_ADDRESSES));
4586       else if (mips_pic == NO_PIC)
4587         {
4588           /* If this is a reference to a GP relative symbol, we want
4589                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4590              Otherwise we want
4591                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4592                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4593              If we have a constant, we need two instructions anyhow,
4594              so we may as well always use the latter form.
4595
4596             With 64bit address space and a usable $at we want
4597               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4598               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4599               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4600               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4601               dsll32    $tempreg,0
4602               daddu     $tempreg,$tempreg,$at
4603
4604             If $at is already in use, we use an path which is suboptimal
4605             on superscalar processors.
4606               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4607               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4608               dsll      $tempreg,16
4609               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4610               dsll      $tempreg,16
4611               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4612           */
4613           char *p = NULL;
4614           if (HAVE_64BIT_ADDRESSES)
4615             {
4616               /* We don't do GP optimization for now because RELAX_ENCODE can't
4617                  hold the data for such large chunks.  */
4618
4619               if (used_at == 0 && ! mips_opts.noat)
4620                 {
4621                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4622                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4623                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4624                                AT, (int) BFD_RELOC_HI16_S);
4625                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4626                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4627                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4628                                AT, AT, (int) BFD_RELOC_LO16);
4629                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4630                                "d,w,<", tempreg, tempreg, 0);
4631                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4632                                "d,v,t", tempreg, tempreg, AT);
4633                   used_at = 1;
4634                 }
4635               else
4636                 {
4637                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4638                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4639                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4640                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4641                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4642                                tempreg, tempreg, 16);
4643                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4644                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4645                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4646                                tempreg, tempreg, 16);
4647                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4648                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4649                 }
4650             }
4651           else
4652             {
4653               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4654                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4655                 {
4656                   frag_grow (20);
4657                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4658                                "t,r,j", tempreg, mips_gp_register,
4659                                (int) BFD_RELOC_GPREL16);
4660                   p = frag_var (rs_machine_dependent, 8, 0,
4661                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4662                                               mips_opts.warn_about_macros),
4663                                 offset_expr.X_add_symbol, 0, NULL);
4664                 }
4665               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4666               if (p != NULL)
4667                 p += 4;
4668               macro_build (p, &icnt, &offset_expr, "addiu",
4669                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4670             }
4671         }
4672       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4673         {
4674           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4675
4676           /* If this is a reference to an external symbol, and there
4677              is no constant, we want
4678                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4679              or if tempreg is PIC_CALL_REG
4680                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4681              For a local symbol, we want
4682                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4683                nop
4684                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4685
4686              If we have a small constant, and this is a reference to
4687              an external symbol, we want
4688                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4689                nop
4690                addiu    $tempreg,$tempreg,<constant>
4691              For a local symbol, we want the same instruction
4692              sequence, but we output a BFD_RELOC_LO16 reloc on the
4693              addiu instruction.
4694
4695              If we have a large constant, and this is a reference to
4696              an external symbol, we want
4697                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4698                lui      $at,<hiconstant>
4699                addiu    $at,$at,<loconstant>
4700                addu     $tempreg,$tempreg,$at
4701              For a local symbol, we want the same instruction
4702              sequence, but we output a BFD_RELOC_LO16 reloc on the
4703              addiu instruction.
4704
4705              For NewABI, we want for local or external data addresses
4706                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4707              For a local function symbol, we want
4708                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
4709                nop
4710                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4711            */
4712
4713           expr1.X_add_number = offset_expr.X_add_number;
4714           offset_expr.X_add_number = 0;
4715           frag_grow (32);
4716           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4717             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4718           else if (HAVE_NEWABI)
4719             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4720           macro_build ((char *) NULL, &icnt, &offset_expr,
4721                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4722                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4723           if (expr1.X_add_number == 0)
4724             {
4725               int off;
4726               char *p;
4727
4728               if (breg == 0)
4729                 off = 0;
4730               else
4731                 {
4732                   /* We're going to put in an addu instruction using
4733                      tempreg, so we may as well insert the nop right
4734                      now.  */
4735                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4736                                "nop", "");
4737                   off = 4;
4738                 }
4739               p = frag_var (rs_machine_dependent, 8 - off, 0,
4740                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4741                                           (breg == 0
4742                                            ? mips_opts.warn_about_macros
4743                                            : 0)),
4744                             offset_expr.X_add_symbol, 0, NULL);
4745               if (breg == 0)
4746                 {
4747                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4748                   p += 4;
4749                 }
4750               macro_build (p, &icnt, &expr1,
4751                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4752                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4753               /* FIXME: If breg == 0, and the next instruction uses
4754                  $tempreg, then if this variant case is used an extra
4755                  nop will be generated.  */
4756             }
4757           else if (expr1.X_add_number >= -0x8000
4758                    && expr1.X_add_number < 0x8000)
4759             {
4760               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4761                            "nop", "");
4762               macro_build ((char *) NULL, &icnt, &expr1,
4763                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4764                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4765               frag_var (rs_machine_dependent, 0, 0,
4766                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4767                         offset_expr.X_add_symbol, 0, NULL);
4768             }
4769           else
4770             {
4771               int off1;
4772
4773               /* If we are going to add in a base register, and the
4774                  target register and the base register are the same,
4775                  then we are using AT as a temporary register.  Since
4776                  we want to load the constant into AT, we add our
4777                  current AT (from the global offset table) and the
4778                  register into the register now, and pretend we were
4779                  not using a base register.  */
4780               if (breg != treg)
4781                 off1 = 0;
4782               else
4783                 {
4784                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4785                                "nop", "");
4786                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4787                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4788                                "d,v,t", treg, AT, breg);
4789                   breg = 0;
4790                   tempreg = treg;
4791                   off1 = -8;
4792                 }
4793
4794               /* Set mips_optimize around the lui instruction to avoid
4795                  inserting an unnecessary nop after the lw.  */
4796               hold_mips_optimize = mips_optimize;
4797               mips_optimize = 2;
4798               macro_build_lui (NULL, &icnt, &expr1, AT);
4799               mips_optimize = hold_mips_optimize;
4800
4801               macro_build ((char *) NULL, &icnt, &expr1,
4802                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4803                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4804               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4805                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4806                            "d,v,t", tempreg, tempreg, AT);
4807               frag_var (rs_machine_dependent, 0, 0,
4808                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4809                         offset_expr.X_add_symbol, 0, NULL);
4810               used_at = 1;
4811             }
4812         }
4813       else if (mips_pic == SVR4_PIC)
4814         {
4815           int gpdel;
4816           char *p;
4817           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4818           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4819           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4820
4821           /* This is the large GOT case.  If this is a reference to an
4822              external symbol, and there is no constant, we want
4823                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4824                addu     $tempreg,$tempreg,$gp
4825                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4826              or if tempreg is PIC_CALL_REG
4827                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
4828                addu     $tempreg,$tempreg,$gp
4829                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4830              For a local symbol, we want
4831                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4832                nop
4833                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4834
4835              If we have a small constant, and this is a reference to
4836              an external symbol, we want
4837                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4838                addu     $tempreg,$tempreg,$gp
4839                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4840                nop
4841                addiu    $tempreg,$tempreg,<constant>
4842              For a local symbol, we want
4843                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4844                nop
4845                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4846
4847              If we have a large constant, and this is a reference to
4848              an external symbol, we want
4849                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4850                addu     $tempreg,$tempreg,$gp
4851                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4852                lui      $at,<hiconstant>
4853                addiu    $at,$at,<loconstant>
4854                addu     $tempreg,$tempreg,$at
4855              For a local symbol, we want
4856                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4857                lui      $at,<hiconstant>
4858                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
4859                addu     $tempreg,$tempreg,$at
4860
4861              For NewABI, we want for local data addresses
4862               lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4863            */
4864
4865           expr1.X_add_number = offset_expr.X_add_number;
4866           offset_expr.X_add_number = 0;
4867           frag_grow (52);
4868           if (reg_needs_delay (mips_gp_register))
4869             gpdel = 4;
4870           else
4871             gpdel = 0;
4872           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4873             {
4874               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4875               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4876             }
4877           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4878                        tempreg, lui_reloc_type);
4879           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4880                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4881                        "d,v,t", tempreg, tempreg, mips_gp_register);
4882           macro_build ((char *) NULL, &icnt, &offset_expr,
4883                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4884                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
4885           if (expr1.X_add_number == 0)
4886             {
4887               int off;
4888
4889               if (breg == 0)
4890                 off = 0;
4891               else
4892                 {
4893                   /* We're going to put in an addu instruction using
4894                      tempreg, so we may as well insert the nop right
4895                      now.  */
4896                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4897                                "nop", "");
4898                   off = 4;
4899                 }
4900
4901               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4902                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4903                                           8 + gpdel, 0,
4904                                           (breg == 0
4905                                            ? mips_opts.warn_about_macros
4906                                            : 0)),
4907                             offset_expr.X_add_symbol, 0, NULL);
4908             }
4909           else if (expr1.X_add_number >= -0x8000
4910                    && expr1.X_add_number < 0x8000)
4911             {
4912               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4913                            "nop", "");
4914               macro_build ((char *) NULL, &icnt, &expr1,
4915                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4916                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4917
4918               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4919                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4920                                           (breg == 0
4921                                            ? mips_opts.warn_about_macros
4922                                            : 0)),
4923                             offset_expr.X_add_symbol, 0, NULL);
4924             }
4925           else
4926             {
4927               int adj, dreg;
4928
4929               /* If we are going to add in a base register, and the
4930                  target register and the base register are the same,
4931                  then we are using AT as a temporary register.  Since
4932                  we want to load the constant into AT, we add our
4933                  current AT (from the global offset table) and the
4934                  register into the register now, and pretend we were
4935                  not using a base register.  */
4936               if (breg != treg)
4937                 {
4938                   adj = 0;
4939                   dreg = tempreg;
4940                 }
4941               else
4942                 {
4943                   assert (tempreg == AT);
4944                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4945                                "nop", "");
4946                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4947                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4948                                "d,v,t", treg, AT, breg);
4949                   dreg = treg;
4950                   adj = 8;
4951                 }
4952
4953               /* Set mips_optimize around the lui instruction to avoid
4954                  inserting an unnecessary nop after the lw.  */
4955               hold_mips_optimize = mips_optimize;
4956               mips_optimize = 2;
4957               macro_build_lui (NULL, &icnt, &expr1, AT);
4958               mips_optimize = hold_mips_optimize;
4959
4960               macro_build ((char *) NULL, &icnt, &expr1,
4961                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4962                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4963               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4964                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4965                            "d,v,t", dreg, dreg, AT);
4966
4967               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4968                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4969                                           8 + gpdel, 0,
4970                                           (breg == 0
4971                                            ? mips_opts.warn_about_macros
4972                                            : 0)),
4973                             offset_expr.X_add_symbol, 0, NULL);
4974
4975               used_at = 1;
4976             }
4977
4978           if (gpdel > 0)
4979             {
4980               /* This is needed because this instruction uses $gp, but
4981                  the first instruction on the main stream does not.  */
4982               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4983               p += 4;
4984             }
4985
4986           if (HAVE_NEWABI)
4987             local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4988           macro_build (p, &icnt, &offset_expr,
4989                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4990                        "t,o(b)", tempreg,
4991                        local_reloc_type,
4992                        mips_gp_register);
4993           p += 4;
4994           if (expr1.X_add_number == 0 && HAVE_NEWABI)
4995             {
4996               /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
4997             }
4998          else
4999            if (expr1.X_add_number >= -0x8000
5000               && expr1.X_add_number < 0x8000)
5001             {
5002               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5003               p += 4;
5004               macro_build (p, &icnt, &expr1,
5005                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5006                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5007               /* FIXME: If add_number is 0, and there was no base
5008                  register, the external symbol case ended with a load,
5009                  so if the symbol turns out to not be external, and
5010                  the next instruction uses tempreg, an unnecessary nop
5011                  will be inserted.  */
5012             }
5013           else
5014             {
5015               if (breg == treg)
5016                 {
5017                   /* We must add in the base register now, as in the
5018                      external symbol case.  */
5019                   assert (tempreg == AT);
5020                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5021                   p += 4;
5022                   macro_build (p, &icnt, (expressionS *) NULL,
5023                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5024                                "d,v,t", treg, AT, breg);
5025                   p += 4;
5026                   tempreg = treg;
5027                   /* We set breg to 0 because we have arranged to add
5028                      it in in both cases.  */
5029                   breg = 0;
5030                 }
5031
5032               macro_build_lui (p, &icnt, &expr1, AT);
5033               p += 4;
5034               macro_build (p, &icnt, &expr1,
5035                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5036                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5037               p += 4;
5038               macro_build (p, &icnt, (expressionS *) NULL,
5039                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5040                            "d,v,t", tempreg, tempreg, AT);
5041               p += 4;
5042             }
5043         }
5044       else if (mips_pic == EMBEDDED_PIC)
5045         {
5046           /* We use
5047                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5048              */
5049           macro_build ((char *) NULL, &icnt, &offset_expr,
5050                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5051                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5052         }
5053       else
5054         abort ();
5055
5056       if (breg != 0)
5057         {
5058           char *s;
5059
5060           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5061             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5062           else
5063             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5064
5065           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5066                        "d,v,t", treg, tempreg, breg);
5067         }
5068
5069       if (! used_at)
5070         return;
5071
5072       break;
5073
5074     case M_J_A:
5075       /* The j instruction may not be used in PIC code, since it
5076          requires an absolute address.  We convert it to a b
5077          instruction.  */
5078       if (mips_pic == NO_PIC)
5079         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5080       else
5081         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5082       return;
5083
5084       /* The jal instructions must be handled as macros because when
5085          generating PIC code they expand to multi-instruction
5086          sequences.  Normally they are simple instructions.  */
5087     case M_JAL_1:
5088       dreg = RA;
5089       /* Fall through.  */
5090     case M_JAL_2:
5091       if (mips_pic == NO_PIC
5092           || mips_pic == EMBEDDED_PIC)
5093         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5094                      "d,s", dreg, sreg);
5095       else if (mips_pic == SVR4_PIC)
5096         {
5097           if (sreg != PIC_CALL_REG)
5098             as_warn (_("MIPS PIC call to register other than $25"));
5099
5100           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5101                        "d,s", dreg, sreg);
5102           if (! HAVE_NEWABI)
5103             {
5104               if (mips_cprestore_offset < 0)
5105                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5106               else
5107                 {
5108                   if (! mips_frame_reg_valid)
5109                     {
5110                       as_warn (_("No .frame pseudo-op used in PIC code"));
5111                       /* Quiet this warning.  */
5112                       mips_frame_reg_valid = 1;
5113                     }
5114                   if (! mips_cprestore_valid)
5115                     {
5116                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5117                       /* Quiet this warning.  */
5118                       mips_cprestore_valid = 1;
5119                     }
5120                   expr1.X_add_number = mips_cprestore_offset;
5121                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5122                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5123                                                 mips_gp_register, mips_frame_reg);
5124                 }
5125             }
5126         }
5127       else
5128         abort ();
5129
5130       return;
5131
5132     case M_JAL_A:
5133       if (mips_pic == NO_PIC)
5134         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5135       else if (mips_pic == SVR4_PIC)
5136         {
5137           char *p;
5138
5139           /* If this is a reference to an external symbol, and we are
5140              using a small GOT, we want
5141                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5142                nop
5143                jalr     $ra,$25
5144                nop
5145                lw       $gp,cprestore($sp)
5146              The cprestore value is set using the .cprestore
5147              pseudo-op.  If we are using a big GOT, we want
5148                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5149                addu     $25,$25,$gp
5150                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5151                nop
5152                jalr     $ra,$25
5153                nop
5154                lw       $gp,cprestore($sp)
5155              If the symbol is not external, we want
5156                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5157                nop
5158                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5159                jalr     $ra,$25
5160                nop
5161                lw $gp,cprestore($sp)
5162              For NewABI, we want
5163                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5164                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5165            */
5166           if (HAVE_NEWABI)
5167             {
5168               macro_build ((char *) NULL, &icnt, &offset_expr,
5169                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5170                            "t,o(b)", PIC_CALL_REG,
5171                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5172               macro_build_jalr (icnt, &offset_expr);
5173             }
5174           else
5175             {
5176               frag_grow (40);
5177               if (! mips_big_got)
5178                 {
5179                   macro_build ((char *) NULL, &icnt, &offset_expr,
5180                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5181                                "t,o(b)", PIC_CALL_REG,
5182                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5183                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5184                                "nop", "");
5185                   p = frag_var (rs_machine_dependent, 4, 0,
5186                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5187                                 offset_expr.X_add_symbol, 0, NULL);
5188                 }
5189               else
5190                 {
5191                   int gpdel;
5192
5193                   if (reg_needs_delay (mips_gp_register))
5194                     gpdel = 4;
5195                   else
5196                     gpdel = 0;
5197                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5198                                "t,u", PIC_CALL_REG,
5199                                (int) BFD_RELOC_MIPS_CALL_HI16);
5200                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5201                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5202                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5203                                mips_gp_register);
5204                   macro_build ((char *) NULL, &icnt, &offset_expr,
5205                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5206                                "t,o(b)", PIC_CALL_REG,
5207                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5208                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5209                                "nop", "");
5210                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5211                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5212                                               8 + gpdel, 0, 0),
5213                                 offset_expr.X_add_symbol, 0, NULL);
5214                   if (gpdel > 0)
5215                     {
5216                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5217                       p += 4;
5218                     }
5219                   macro_build (p, &icnt, &offset_expr,
5220                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5221                                "t,o(b)", PIC_CALL_REG,
5222                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5223                   p += 4;
5224                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5225                   p += 4;
5226                 }
5227               macro_build (p, &icnt, &offset_expr,
5228                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5229                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5230                            (int) BFD_RELOC_LO16);
5231               macro_build_jalr (icnt, &offset_expr);
5232
5233               if (mips_cprestore_offset < 0)
5234                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5235               else
5236                 {
5237                   if (! mips_frame_reg_valid)
5238                     {
5239                       as_warn (_("No .frame pseudo-op used in PIC code"));
5240                       /* Quiet this warning.  */
5241                       mips_frame_reg_valid = 1;
5242                     }
5243                   if (! mips_cprestore_valid)
5244                     {
5245                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5246                       /* Quiet this warning.  */
5247                       mips_cprestore_valid = 1;
5248                     }
5249                   if (mips_opts.noreorder)
5250                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5251                                  "nop", "");
5252                   expr1.X_add_number = mips_cprestore_offset;
5253                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5254                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5255                                                 mips_gp_register, mips_frame_reg);
5256                 }
5257             }
5258         }
5259       else if (mips_pic == EMBEDDED_PIC)
5260         {
5261           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5262           /* The linker may expand the call to a longer sequence which
5263              uses $at, so we must break rather than return.  */
5264           break;
5265         }
5266       else
5267         abort ();
5268
5269       return;
5270
5271     case M_LB_AB:
5272       s = "lb";
5273       goto ld;
5274     case M_LBU_AB:
5275       s = "lbu";
5276       goto ld;
5277     case M_LH_AB:
5278       s = "lh";
5279       goto ld;
5280     case M_LHU_AB:
5281       s = "lhu";
5282       goto ld;
5283     case M_LW_AB:
5284       s = "lw";
5285       goto ld;
5286     case M_LWC0_AB:
5287       s = "lwc0";
5288       /* Itbl support may require additional care here.  */
5289       coproc = 1;
5290       goto ld;
5291     case M_LWC1_AB:
5292       s = "lwc1";
5293       /* Itbl support may require additional care here.  */
5294       coproc = 1;
5295       goto ld;
5296     case M_LWC2_AB:
5297       s = "lwc2";
5298       /* Itbl support may require additional care here.  */
5299       coproc = 1;
5300       goto ld;
5301     case M_LWC3_AB:
5302       s = "lwc3";
5303       /* Itbl support may require additional care here.  */
5304       coproc = 1;
5305       goto ld;
5306     case M_LWL_AB:
5307       s = "lwl";
5308       lr = 1;
5309       goto ld;
5310     case M_LWR_AB:
5311       s = "lwr";
5312       lr = 1;
5313       goto ld;
5314     case M_LDC1_AB:
5315       if (mips_arch == CPU_R4650)
5316         {
5317           as_bad (_("opcode not supported on this processor"));
5318           return;
5319         }
5320       s = "ldc1";
5321       /* Itbl support may require additional care here.  */
5322       coproc = 1;
5323       goto ld;
5324     case M_LDC2_AB:
5325       s = "ldc2";
5326       /* Itbl support may require additional care here.  */
5327       coproc = 1;
5328       goto ld;
5329     case M_LDC3_AB:
5330       s = "ldc3";
5331       /* Itbl support may require additional care here.  */
5332       coproc = 1;
5333       goto ld;
5334     case M_LDL_AB:
5335       s = "ldl";
5336       lr = 1;
5337       goto ld;
5338     case M_LDR_AB:
5339       s = "ldr";
5340       lr = 1;
5341       goto ld;
5342     case M_LL_AB:
5343       s = "ll";
5344       goto ld;
5345     case M_LLD_AB:
5346       s = "lld";
5347       goto ld;
5348     case M_LWU_AB:
5349       s = "lwu";
5350     ld:
5351       if (breg == treg || coproc || lr)
5352         {
5353           tempreg = AT;
5354           used_at = 1;
5355         }
5356       else
5357         {
5358           tempreg = treg;
5359           used_at = 0;
5360         }
5361       goto ld_st;
5362     case M_SB_AB:
5363       s = "sb";
5364       goto st;
5365     case M_SH_AB:
5366       s = "sh";
5367       goto st;
5368     case M_SW_AB:
5369       s = "sw";
5370       goto st;
5371     case M_SWC0_AB:
5372       s = "swc0";
5373       /* Itbl support may require additional care here.  */
5374       coproc = 1;
5375       goto st;
5376     case M_SWC1_AB:
5377       s = "swc1";
5378       /* Itbl support may require additional care here.  */
5379       coproc = 1;
5380       goto st;
5381     case M_SWC2_AB:
5382       s = "swc2";
5383       /* Itbl support may require additional care here.  */
5384       coproc = 1;
5385       goto st;
5386     case M_SWC3_AB:
5387       s = "swc3";
5388       /* Itbl support may require additional care here.  */
5389       coproc = 1;
5390       goto st;
5391     case M_SWL_AB:
5392       s = "swl";
5393       goto st;
5394     case M_SWR_AB:
5395       s = "swr";
5396       goto st;
5397     case M_SC_AB:
5398       s = "sc";
5399       goto st;
5400     case M_SCD_AB:
5401       s = "scd";
5402       goto st;
5403     case M_SDC1_AB:
5404       if (mips_arch == CPU_R4650)
5405         {
5406           as_bad (_("opcode not supported on this processor"));
5407           return;
5408         }
5409       s = "sdc1";
5410       coproc = 1;
5411       /* Itbl support may require additional care here.  */
5412       goto st;
5413     case M_SDC2_AB:
5414       s = "sdc2";
5415       /* Itbl support may require additional care here.  */
5416       coproc = 1;
5417       goto st;
5418     case M_SDC3_AB:
5419       s = "sdc3";
5420       /* Itbl support may require additional care here.  */
5421       coproc = 1;
5422       goto st;
5423     case M_SDL_AB:
5424       s = "sdl";
5425       goto st;
5426     case M_SDR_AB:
5427       s = "sdr";
5428     st:
5429       tempreg = AT;
5430       used_at = 1;
5431     ld_st:
5432       /* Itbl support may require additional care here.  */
5433       if (mask == M_LWC1_AB
5434           || mask == M_SWC1_AB
5435           || mask == M_LDC1_AB
5436           || mask == M_SDC1_AB
5437           || mask == M_L_DAB
5438           || mask == M_S_DAB)
5439         fmt = "T,o(b)";
5440       else if (coproc)
5441         fmt = "E,o(b)";
5442       else
5443         fmt = "t,o(b)";
5444
5445       /* For embedded PIC, we allow loads where the offset is calculated
5446          by subtracting a symbol in the current segment from an unknown
5447          symbol, relative to a base register, e.g.:
5448                 <op>    $treg, <sym>-<localsym>($breg)
5449          This is used by the compiler for switch statements.  */
5450       if (mips_pic == EMBEDDED_PIC
5451           && offset_expr.X_op == O_subtract
5452           && (symbol_constant_p (offset_expr.X_op_symbol)
5453               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5454               : (symbol_equated_p (offset_expr.X_op_symbol)
5455                  && (S_GET_SEGMENT
5456                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5457                       ->X_add_symbol)
5458                      == now_seg)))
5459           && breg != 0
5460           && (offset_expr.X_add_number == 0
5461               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5462         {
5463           /* For this case, we output the instructions:
5464                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5465                 addiu   $tempreg,$tempreg,$breg
5466                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5467              If the relocation would fit entirely in 16 bits, it would be
5468              nice to emit:
5469                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5470              instead, but that seems quite difficult.  */
5471           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5472                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5473           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5474                        ((bfd_arch_bits_per_address (stdoutput) == 32
5475                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5476                         ? "addu" : "daddu"),
5477                        "d,v,t", tempreg, tempreg, breg);
5478           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5479                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5480           if (! used_at)
5481             return;
5482           break;
5483         }
5484
5485       if (offset_expr.X_op != O_constant
5486           && offset_expr.X_op != O_symbol)
5487         {
5488           as_bad (_("expression too complex"));
5489           offset_expr.X_op = O_constant;
5490         }
5491
5492       /* A constant expression in PIC code can be handled just as it
5493          is in non PIC code.  */
5494       if (mips_pic == NO_PIC
5495           || offset_expr.X_op == O_constant)
5496         {
5497           char *p;
5498
5499           /* If this is a reference to a GP relative symbol, and there
5500              is no base register, we want
5501                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5502              Otherwise, if there is no base register, we want
5503                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5504                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5505              If we have a constant, we need two instructions anyhow,
5506              so we always use the latter form.
5507
5508              If we have a base register, and this is a reference to a
5509              GP relative symbol, we want
5510                addu     $tempreg,$breg,$gp
5511                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5512              Otherwise we want
5513                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5514                addu     $tempreg,$tempreg,$breg
5515                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5516              With a constant we always use the latter case.
5517
5518              With 64bit address space and no base register and $at usable,
5519              we want
5520                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5521                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5522                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5523                dsll32   $tempreg,0
5524                daddu    $tempreg,$at
5525                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5526              If we have a base register, we want
5527                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5528                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5529                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5530                daddu    $at,$breg
5531                dsll32   $tempreg,0
5532                daddu    $tempreg,$at
5533                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5534
5535              Without $at we can't generate the optimal path for superscalar
5536              processors here since this would require two temporary registers.
5537                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5538                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5539                dsll     $tempreg,16
5540                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5541                dsll     $tempreg,16
5542                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5543              If we have a base register, we want
5544                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5545                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5546                dsll     $tempreg,16
5547                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5548                dsll     $tempreg,16
5549                daddu    $tempreg,$tempreg,$breg
5550                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5551
5552              If we have 64-bit addresses, as an optimization, for
5553              addresses which are 32-bit constants (e.g. kseg0/kseg1
5554              addresses) we fall back to the 32-bit address generation
5555              mechanism since it is more efficient.  Note that due to
5556              the signed offset used by memory operations, the 32-bit
5557              range is shifted down by 32768 here.  This code should
5558              probably attempt to generate 64-bit constants more
5559              efficiently in general.
5560            */
5561           if (HAVE_64BIT_ADDRESSES
5562               && !(offset_expr.X_op == O_constant
5563                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5564             {
5565               p = NULL;
5566
5567               /* We don't do GP optimization for now because RELAX_ENCODE can't
5568                  hold the data for such large chunks.  */
5569
5570               if (used_at == 0 && ! mips_opts.noat)
5571                 {
5572                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5573                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5574                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5575                                AT, (int) BFD_RELOC_HI16_S);
5576                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5577                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5578                   if (breg != 0)
5579                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5580                                  "d,v,t", AT, AT, breg);
5581                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5582                                "d,w,<", tempreg, tempreg, 0);
5583                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5584                                "d,v,t", tempreg, tempreg, AT);
5585                   macro_build (p, &icnt, &offset_expr, s,
5586                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5587                   used_at = 1;
5588                 }
5589               else
5590                 {
5591                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5592                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5593                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5594                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5595                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5596                                "d,w,<", tempreg, tempreg, 16);
5597                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5598                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5599                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5600                                "d,w,<", tempreg, tempreg, 16);
5601                   if (breg != 0)
5602                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5603                                  "d,v,t", tempreg, tempreg, breg);
5604                   macro_build (p, &icnt, &offset_expr, s,
5605                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5606                 }
5607
5608               return;
5609             }
5610
5611           if (breg == 0)
5612             {
5613               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5614                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5615                 p = NULL;
5616               else
5617                 {
5618                   frag_grow (20);
5619                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5620                                treg, (int) BFD_RELOC_GPREL16,
5621                                mips_gp_register);
5622                   p = frag_var (rs_machine_dependent, 8, 0,
5623                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5624                                               (mips_opts.warn_about_macros
5625                                                || (used_at
5626                                                    && mips_opts.noat))),
5627                                 offset_expr.X_add_symbol, 0, NULL);
5628                   used_at = 0;
5629                 }
5630               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5631               if (p != NULL)
5632                 p += 4;
5633               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5634                            (int) BFD_RELOC_LO16, tempreg);
5635             }
5636           else
5637             {
5638               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5639                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5640                 p = NULL;
5641               else
5642                 {
5643                   frag_grow (28);
5644                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5645                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5646                                "d,v,t", tempreg, breg, mips_gp_register);
5647                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5648                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5649                   p = frag_var (rs_machine_dependent, 12, 0,
5650                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5651                                 offset_expr.X_add_symbol, 0, NULL);
5652                 }
5653               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5654               if (p != NULL)
5655                 p += 4;
5656               macro_build (p, &icnt, (expressionS *) NULL,
5657                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5658                            "d,v,t", tempreg, tempreg, breg);
5659               if (p != NULL)
5660                 p += 4;
5661               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5662                            (int) BFD_RELOC_LO16, tempreg);
5663             }
5664         }
5665       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5666         {
5667           char *p;
5668           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5669
5670           /* If this is a reference to an external symbol, we want
5671                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5672                nop
5673                <op>     $treg,0($tempreg)
5674              Otherwise we want
5675                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5676                nop
5677                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5678                <op>     $treg,0($tempreg)
5679              If we have NewABI, we want
5680                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
5681              If there is a base register, we add it to $tempreg before
5682              the <op>.  If there is a constant, we stick it in the
5683              <op> instruction.  We don't handle constants larger than
5684              16 bits, because we have no way to load the upper 16 bits
5685              (actually, we could handle them for the subset of cases
5686              in which we are not using $at).  */
5687           assert (offset_expr.X_op == O_symbol);
5688           expr1.X_add_number = offset_expr.X_add_number;
5689           offset_expr.X_add_number = 0;
5690           if (HAVE_NEWABI)
5691             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5692           if (expr1.X_add_number < -0x8000
5693               || expr1.X_add_number >= 0x8000)
5694             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5695           frag_grow (20);
5696           macro_build ((char *) NULL, &icnt, &offset_expr,
5697                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5698                        (int) lw_reloc_type, mips_gp_register);
5699           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5700           p = frag_var (rs_machine_dependent, 4, 0,
5701                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5702                         offset_expr.X_add_symbol, 0, NULL);
5703           macro_build (p, &icnt, &offset_expr,
5704                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5705                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5706           if (breg != 0)
5707             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5708                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5709                          "d,v,t", tempreg, tempreg, breg);
5710           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5711                        (int) BFD_RELOC_LO16, tempreg);
5712         }
5713       else if (mips_pic == SVR4_PIC)
5714         {
5715           int gpdel;
5716           char *p;
5717
5718           /* If this is a reference to an external symbol, we want
5719                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5720                addu     $tempreg,$tempreg,$gp
5721                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5722                <op>     $treg,0($tempreg)
5723              Otherwise we want
5724                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5725                nop
5726                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5727                <op>     $treg,0($tempreg)
5728              If there is a base register, we add it to $tempreg before
5729              the <op>.  If there is a constant, we stick it in the
5730              <op> instruction.  We don't handle constants larger than
5731              16 bits, because we have no way to load the upper 16 bits
5732              (actually, we could handle them for the subset of cases
5733              in which we are not using $at).
5734
5735              For NewABI, we want
5736                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
5737                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5738                <op>     $treg,0($tempreg)
5739            */
5740           assert (offset_expr.X_op == O_symbol);
5741           expr1.X_add_number = offset_expr.X_add_number;
5742           offset_expr.X_add_number = 0;
5743           if (expr1.X_add_number < -0x8000
5744               || expr1.X_add_number >= 0x8000)
5745             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5746           if (HAVE_NEWABI)
5747             {
5748               macro_build ((char *) NULL, &icnt, &offset_expr,
5749                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5750                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5751                            mips_gp_register);
5752               macro_build ((char *) NULL, &icnt, &offset_expr,
5753                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5754                            "t,r,j", tempreg, tempreg,
5755                            BFD_RELOC_MIPS_GOT_OFST);
5756               if (breg != 0)
5757                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5758                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5759                              "d,v,t", tempreg, tempreg, breg);
5760               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5761                            (int) BFD_RELOC_LO16, tempreg);
5762
5763               if (! used_at)
5764                 return;
5765
5766               break;
5767             }
5768           if (reg_needs_delay (mips_gp_register))
5769             gpdel = 4;
5770           else
5771             gpdel = 0;
5772           frag_grow (36);
5773           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5774                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5775           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5776                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5777                        "d,v,t", tempreg, tempreg, mips_gp_register);
5778           macro_build ((char *) NULL, &icnt, &offset_expr,
5779                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5780                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5781                        tempreg);
5782           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5783                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5784                         offset_expr.X_add_symbol, 0, NULL);
5785           if (gpdel > 0)
5786             {
5787               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5788               p += 4;
5789             }
5790           macro_build (p, &icnt, &offset_expr,
5791                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5792                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5793                        mips_gp_register);
5794           p += 4;
5795           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5796           p += 4;
5797           macro_build (p, &icnt, &offset_expr,
5798                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5799                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5800           if (breg != 0)
5801             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5802                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5803                          "d,v,t", tempreg, tempreg, breg);
5804           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5805                        (int) BFD_RELOC_LO16, tempreg);
5806         }
5807       else if (mips_pic == EMBEDDED_PIC)
5808         {
5809           /* If there is no base register, we want
5810                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5811              If there is a base register, we want
5812                addu     $tempreg,$breg,$gp
5813                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5814              */
5815           assert (offset_expr.X_op == O_symbol);
5816           if (breg == 0)
5817             {
5818               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5819                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
5820               used_at = 0;
5821             }
5822           else
5823             {
5824               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5825                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5826                            "d,v,t", tempreg, breg, mips_gp_register);
5827               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5828                            treg, (int) BFD_RELOC_GPREL16, tempreg);
5829             }
5830         }
5831       else
5832         abort ();
5833
5834       if (! used_at)
5835         return;
5836
5837       break;
5838
5839     case M_LI:
5840     case M_LI_S:
5841       load_register (&icnt, treg, &imm_expr, 0);
5842       return;
5843
5844     case M_DLI:
5845       load_register (&icnt, treg, &imm_expr, 1);
5846       return;
5847
5848     case M_LI_SS:
5849       if (imm_expr.X_op == O_constant)
5850         {
5851           load_register (&icnt, AT, &imm_expr, 0);
5852           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5853                        "mtc1", "t,G", AT, treg);
5854           break;
5855         }
5856       else
5857         {
5858           assert (offset_expr.X_op == O_symbol
5859                   && strcmp (segment_name (S_GET_SEGMENT
5860                                            (offset_expr.X_add_symbol)),
5861                              ".lit4") == 0
5862                   && offset_expr.X_add_number == 0);
5863           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5864                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
5865           return;
5866         }
5867
5868     case M_LI_D:
5869       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
5870          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
5871          order 32 bits of the value and the low order 32 bits are either
5872          zero or in OFFSET_EXPR.  */
5873       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5874         {
5875           if (HAVE_64BIT_GPRS)
5876             load_register (&icnt, treg, &imm_expr, 1);
5877           else
5878             {
5879               int hreg, lreg;
5880
5881               if (target_big_endian)
5882                 {
5883                   hreg = treg;
5884                   lreg = treg + 1;
5885                 }
5886               else
5887                 {
5888                   hreg = treg + 1;
5889                   lreg = treg;
5890                 }
5891
5892               if (hreg <= 31)
5893                 load_register (&icnt, hreg, &imm_expr, 0);
5894               if (lreg <= 31)
5895                 {
5896                   if (offset_expr.X_op == O_absent)
5897                     move_register (&icnt, lreg, 0);
5898                   else
5899                     {
5900                       assert (offset_expr.X_op == O_constant);
5901                       load_register (&icnt, lreg, &offset_expr, 0);
5902                     }
5903                 }
5904             }
5905           return;
5906         }
5907
5908       /* We know that sym is in the .rdata section.  First we get the
5909          upper 16 bits of the address.  */
5910       if (mips_pic == NO_PIC)
5911         {
5912           macro_build_lui (NULL, &icnt, &offset_expr, AT);
5913         }
5914       else if (mips_pic == SVR4_PIC)
5915         {
5916           macro_build ((char *) NULL, &icnt, &offset_expr,
5917                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5918                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5919                        mips_gp_register);
5920         }
5921       else if (mips_pic == EMBEDDED_PIC)
5922         {
5923           /* For embedded PIC we pick up the entire address off $gp in
5924              a single instruction.  */
5925           macro_build ((char *) NULL, &icnt, &offset_expr,
5926                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
5927                        mips_gp_register, (int) BFD_RELOC_GPREL16);
5928           offset_expr.X_op = O_constant;
5929           offset_expr.X_add_number = 0;
5930         }
5931       else
5932         abort ();
5933
5934       /* Now we load the register(s).  */
5935       if (HAVE_64BIT_GPRS)
5936         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5937                      treg, (int) BFD_RELOC_LO16, AT);
5938       else
5939         {
5940           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5941                        treg, (int) BFD_RELOC_LO16, AT);
5942           if (treg != RA)
5943             {
5944               /* FIXME: How in the world do we deal with the possible
5945                  overflow here?  */
5946               offset_expr.X_add_number += 4;
5947               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5948                            treg + 1, (int) BFD_RELOC_LO16, AT);
5949             }
5950         }
5951
5952       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5953          does not become a variant frag.  */
5954       frag_wane (frag_now);
5955       frag_new (0);
5956
5957       break;
5958
5959     case M_LI_DD:
5960       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
5961          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5962          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
5963          the value and the low order 32 bits are either zero or in
5964          OFFSET_EXPR.  */
5965       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5966         {
5967           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5968           if (HAVE_64BIT_FPRS)
5969             {
5970               assert (HAVE_64BIT_GPRS);
5971               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5972                            "dmtc1", "t,S", AT, treg);
5973             }
5974           else
5975             {
5976               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5977                            "mtc1", "t,G", AT, treg + 1);
5978               if (offset_expr.X_op == O_absent)
5979                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5980                              "mtc1", "t,G", 0, treg);
5981               else
5982                 {
5983                   assert (offset_expr.X_op == O_constant);
5984                   load_register (&icnt, AT, &offset_expr, 0);
5985                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5986                                "mtc1", "t,G", AT, treg);
5987                 }
5988             }
5989           break;
5990         }
5991
5992       assert (offset_expr.X_op == O_symbol
5993               && offset_expr.X_add_number == 0);
5994       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5995       if (strcmp (s, ".lit8") == 0)
5996         {
5997           if (mips_opts.isa != ISA_MIPS1)
5998             {
5999               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6000                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6001                            mips_gp_register);
6002               return;
6003             }
6004           breg = mips_gp_register;
6005           r = BFD_RELOC_MIPS_LITERAL;
6006           goto dob;
6007         }
6008       else
6009         {
6010           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6011           if (mips_pic == SVR4_PIC)
6012             macro_build ((char *) NULL, &icnt, &offset_expr,
6013                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6014                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6015                          mips_gp_register);
6016           else
6017             {
6018               /* FIXME: This won't work for a 64 bit address.  */
6019               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6020             }
6021
6022           if (mips_opts.isa != ISA_MIPS1)
6023             {
6024               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6025                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6026
6027               /* To avoid confusion in tc_gen_reloc, we must ensure
6028                  that this does not become a variant frag.  */
6029               frag_wane (frag_now);
6030               frag_new (0);
6031
6032               break;
6033             }
6034           breg = AT;
6035           r = BFD_RELOC_LO16;
6036           goto dob;
6037         }
6038
6039     case M_L_DOB:
6040       if (mips_arch == CPU_R4650)
6041         {
6042           as_bad (_("opcode not supported on this processor"));
6043           return;
6044         }
6045       /* Even on a big endian machine $fn comes before $fn+1.  We have
6046          to adjust when loading from memory.  */
6047       r = BFD_RELOC_LO16;
6048     dob:
6049       assert (mips_opts.isa == ISA_MIPS1);
6050       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6051                    target_big_endian ? treg + 1 : treg,
6052                    (int) r, breg);
6053       /* FIXME: A possible overflow which I don't know how to deal
6054          with.  */
6055       offset_expr.X_add_number += 4;
6056       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6057                    target_big_endian ? treg : treg + 1,
6058                    (int) r, breg);
6059
6060       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6061          does not become a variant frag.  */
6062       frag_wane (frag_now);
6063       frag_new (0);
6064
6065       if (breg != AT)
6066         return;
6067       break;
6068
6069     case M_L_DAB:
6070       /*
6071        * The MIPS assembler seems to check for X_add_number not
6072        * being double aligned and generating:
6073        *        lui     at,%hi(foo+1)
6074        *        addu    at,at,v1
6075        *        addiu   at,at,%lo(foo+1)
6076        *        lwc1    f2,0(at)
6077        *        lwc1    f3,4(at)
6078        * But, the resulting address is the same after relocation so why
6079        * generate the extra instruction?
6080        */
6081       if (mips_arch == CPU_R4650)
6082         {
6083           as_bad (_("opcode not supported on this processor"));
6084           return;
6085         }
6086       /* Itbl support may require additional care here.  */
6087       coproc = 1;
6088       if (mips_opts.isa != ISA_MIPS1)
6089         {
6090           s = "ldc1";
6091           goto ld;
6092         }
6093
6094       s = "lwc1";
6095       fmt = "T,o(b)";
6096       goto ldd_std;
6097
6098     case M_S_DAB:
6099       if (mips_arch == CPU_R4650)
6100         {
6101           as_bad (_("opcode not supported on this processor"));
6102           return;
6103         }
6104
6105       if (mips_opts.isa != ISA_MIPS1)
6106         {
6107           s = "sdc1";
6108           goto st;
6109         }
6110
6111       s = "swc1";
6112       fmt = "T,o(b)";
6113       /* Itbl support may require additional care here.  */
6114       coproc = 1;
6115       goto ldd_std;
6116
6117     case M_LD_AB:
6118       if (HAVE_64BIT_GPRS)
6119         {
6120           s = "ld";
6121           goto ld;
6122         }
6123
6124       s = "lw";
6125       fmt = "t,o(b)";
6126       goto ldd_std;
6127
6128     case M_SD_AB:
6129       if (HAVE_64BIT_GPRS)
6130         {
6131           s = "sd";
6132           goto st;
6133         }
6134
6135       s = "sw";
6136       fmt = "t,o(b)";
6137
6138     ldd_std:
6139       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6140          loads for the case of doing a pair of loads to simulate an 'ld'.
6141          This is not currently done by the compiler, and assembly coders
6142          writing embedded-pic code can cope.  */
6143
6144       if (offset_expr.X_op != O_symbol
6145           && offset_expr.X_op != O_constant)
6146         {
6147           as_bad (_("expression too complex"));
6148           offset_expr.X_op = O_constant;
6149         }
6150
6151       /* Even on a big endian machine $fn comes before $fn+1.  We have
6152          to adjust when loading from memory.  We set coproc if we must
6153          load $fn+1 first.  */
6154       /* Itbl support may require additional care here.  */
6155       if (! target_big_endian)
6156         coproc = 0;
6157
6158       if (mips_pic == NO_PIC
6159           || offset_expr.X_op == O_constant)
6160         {
6161           char *p;
6162
6163           /* If this is a reference to a GP relative symbol, we want
6164                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6165                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6166              If we have a base register, we use this
6167                addu     $at,$breg,$gp
6168                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6169                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6170              If this is not a GP relative symbol, we want
6171                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6172                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6173                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6174              If there is a base register, we add it to $at after the
6175              lui instruction.  If there is a constant, we always use
6176              the last case.  */
6177           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6178               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6179             {
6180               p = NULL;
6181               used_at = 1;
6182             }
6183           else
6184             {
6185               int off;
6186
6187               if (breg == 0)
6188                 {
6189                   frag_grow (28);
6190                   tempreg = mips_gp_register;
6191                   off = 0;
6192                   used_at = 0;
6193                 }
6194               else
6195                 {
6196                   frag_grow (36);
6197                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6198                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6199                                "d,v,t", AT, breg, mips_gp_register);
6200                   tempreg = AT;
6201                   off = 4;
6202                   used_at = 1;
6203                 }
6204
6205               /* Itbl support may require additional care here.  */
6206               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6207                            coproc ? treg + 1 : treg,
6208                            (int) BFD_RELOC_GPREL16, tempreg);
6209               offset_expr.X_add_number += 4;
6210
6211               /* Set mips_optimize to 2 to avoid inserting an
6212                  undesired nop.  */
6213               hold_mips_optimize = mips_optimize;
6214               mips_optimize = 2;
6215               /* Itbl support may require additional care here.  */
6216               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6217                            coproc ? treg : treg + 1,
6218                            (int) BFD_RELOC_GPREL16, tempreg);
6219               mips_optimize = hold_mips_optimize;
6220
6221               p = frag_var (rs_machine_dependent, 12 + off, 0,
6222                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6223                                           used_at && mips_opts.noat),
6224                             offset_expr.X_add_symbol, 0, NULL);
6225
6226               /* We just generated two relocs.  When tc_gen_reloc
6227                  handles this case, it will skip the first reloc and
6228                  handle the second.  The second reloc already has an
6229                  extra addend of 4, which we added above.  We must
6230                  subtract it out, and then subtract another 4 to make
6231                  the first reloc come out right.  The second reloc
6232                  will come out right because we are going to add 4 to
6233                  offset_expr when we build its instruction below.
6234
6235                  If we have a symbol, then we don't want to include
6236                  the offset, because it will wind up being included
6237                  when we generate the reloc.  */
6238
6239               if (offset_expr.X_op == O_constant)
6240                 offset_expr.X_add_number -= 8;
6241               else
6242                 {
6243                   offset_expr.X_add_number = -4;
6244                   offset_expr.X_op = O_constant;
6245                 }
6246             }
6247           macro_build_lui (p, &icnt, &offset_expr, AT);
6248           if (p != NULL)
6249             p += 4;
6250           if (breg != 0)
6251             {
6252               macro_build (p, &icnt, (expressionS *) NULL,
6253                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6254                            "d,v,t", AT, breg, AT);
6255               if (p != NULL)
6256                 p += 4;
6257             }
6258           /* Itbl support may require additional care here.  */
6259           macro_build (p, &icnt, &offset_expr, s, fmt,
6260                        coproc ? treg + 1 : treg,
6261                        (int) BFD_RELOC_LO16, AT);
6262           if (p != NULL)
6263             p += 4;
6264           /* FIXME: How do we handle overflow here?  */
6265           offset_expr.X_add_number += 4;
6266           /* Itbl support may require additional care here.  */
6267           macro_build (p, &icnt, &offset_expr, s, fmt,
6268                        coproc ? treg : treg + 1,
6269                        (int) BFD_RELOC_LO16, AT);
6270         }
6271       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6272         {
6273           int off;
6274
6275           /* If this is a reference to an external symbol, we want
6276                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6277                nop
6278                <op>     $treg,0($at)
6279                <op>     $treg+1,4($at)
6280              Otherwise we want
6281                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6282                nop
6283                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6284                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6285              If there is a base register we add it to $at before the
6286              lwc1 instructions.  If there is a constant we include it
6287              in the lwc1 instructions.  */
6288           used_at = 1;
6289           expr1.X_add_number = offset_expr.X_add_number;
6290           offset_expr.X_add_number = 0;
6291           if (expr1.X_add_number < -0x8000
6292               || expr1.X_add_number >= 0x8000 - 4)
6293             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6294           if (breg == 0)
6295             off = 0;
6296           else
6297             off = 4;
6298           frag_grow (24 + off);
6299           macro_build ((char *) NULL, &icnt, &offset_expr,
6300                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6301                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6302           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6303           if (breg != 0)
6304             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6305                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6306                          "d,v,t", AT, breg, AT);
6307           /* Itbl support may require additional care here.  */
6308           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6309                        coproc ? treg + 1 : treg,
6310                        (int) BFD_RELOC_LO16, AT);
6311           expr1.X_add_number += 4;
6312
6313           /* Set mips_optimize to 2 to avoid inserting an undesired
6314              nop.  */
6315           hold_mips_optimize = mips_optimize;
6316           mips_optimize = 2;
6317           /* Itbl support may require additional care here.  */
6318           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6319                        coproc ? treg : treg + 1,
6320                        (int) BFD_RELOC_LO16, AT);
6321           mips_optimize = hold_mips_optimize;
6322
6323           (void) frag_var (rs_machine_dependent, 0, 0,
6324                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6325                            offset_expr.X_add_symbol, 0, NULL);
6326         }
6327       else if (mips_pic == SVR4_PIC)
6328         {
6329           int gpdel, off;
6330           char *p;
6331
6332           /* If this is a reference to an external symbol, we want
6333                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6334                addu     $at,$at,$gp
6335                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6336                nop
6337                <op>     $treg,0($at)
6338                <op>     $treg+1,4($at)
6339              Otherwise we want
6340                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6341                nop
6342                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6343                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6344              If there is a base register we add it to $at before the
6345              lwc1 instructions.  If there is a constant we include it
6346              in the lwc1 instructions.  */
6347           used_at = 1;
6348           expr1.X_add_number = offset_expr.X_add_number;
6349           offset_expr.X_add_number = 0;
6350           if (expr1.X_add_number < -0x8000
6351               || expr1.X_add_number >= 0x8000 - 4)
6352             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6353           if (reg_needs_delay (mips_gp_register))
6354             gpdel = 4;
6355           else
6356             gpdel = 0;
6357           if (breg == 0)
6358             off = 0;
6359           else
6360             off = 4;
6361           frag_grow (56);
6362           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6363                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6364           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6365                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6366                        "d,v,t", AT, AT, mips_gp_register);
6367           macro_build ((char *) NULL, &icnt, &offset_expr,
6368                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6369                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6370           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6371           if (breg != 0)
6372             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6373                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6374                          "d,v,t", AT, breg, AT);
6375           /* Itbl support may require additional care here.  */
6376           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6377                        coproc ? treg + 1 : treg,
6378                        (int) BFD_RELOC_LO16, AT);
6379           expr1.X_add_number += 4;
6380
6381           /* Set mips_optimize to 2 to avoid inserting an undesired
6382              nop.  */
6383           hold_mips_optimize = mips_optimize;
6384           mips_optimize = 2;
6385           /* Itbl support may require additional care here.  */
6386           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6387                        coproc ? treg : treg + 1,
6388                        (int) BFD_RELOC_LO16, AT);
6389           mips_optimize = hold_mips_optimize;
6390           expr1.X_add_number -= 4;
6391
6392           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6393                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6394                                       8 + gpdel + off, 1, 0),
6395                         offset_expr.X_add_symbol, 0, NULL);
6396           if (gpdel > 0)
6397             {
6398               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6399               p += 4;
6400             }
6401           macro_build (p, &icnt, &offset_expr,
6402                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6403                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6404                        mips_gp_register);
6405           p += 4;
6406           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6407           p += 4;
6408           if (breg != 0)
6409             {
6410               macro_build (p, &icnt, (expressionS *) NULL,
6411                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6412                            "d,v,t", AT, breg, AT);
6413               p += 4;
6414             }
6415           /* Itbl support may require additional care here.  */
6416           macro_build (p, &icnt, &expr1, s, fmt,
6417                        coproc ? treg + 1 : treg,
6418                        (int) BFD_RELOC_LO16, AT);
6419           p += 4;
6420           expr1.X_add_number += 4;
6421
6422           /* Set mips_optimize to 2 to avoid inserting an undesired
6423              nop.  */
6424           hold_mips_optimize = mips_optimize;
6425           mips_optimize = 2;
6426           /* Itbl support may require additional care here.  */
6427           macro_build (p, &icnt, &expr1, s, fmt,
6428                        coproc ? treg : treg + 1,
6429                        (int) BFD_RELOC_LO16, AT);
6430           mips_optimize = hold_mips_optimize;
6431         }
6432       else if (mips_pic == EMBEDDED_PIC)
6433         {
6434           /* If there is no base register, we use
6435                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6436                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6437              If we have a base register, we use
6438                addu     $at,$breg,$gp
6439                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6440                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6441              */
6442           if (breg == 0)
6443             {
6444               tempreg = mips_gp_register;
6445               used_at = 0;
6446             }
6447           else
6448             {
6449               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6450                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6451                            "d,v,t", AT, breg, mips_gp_register);
6452               tempreg = AT;
6453               used_at = 1;
6454             }
6455
6456           /* Itbl support may require additional care here.  */
6457           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6458                        coproc ? treg + 1 : treg,
6459                        (int) BFD_RELOC_GPREL16, tempreg);
6460           offset_expr.X_add_number += 4;
6461           /* Itbl support may require additional care here.  */
6462           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6463                        coproc ? treg : treg + 1,
6464                        (int) BFD_RELOC_GPREL16, tempreg);
6465         }
6466       else
6467         abort ();
6468
6469       if (! used_at)
6470         return;
6471
6472       break;
6473
6474     case M_LD_OB:
6475       s = "lw";
6476       goto sd_ob;
6477     case M_SD_OB:
6478       s = "sw";
6479     sd_ob:
6480       assert (HAVE_32BIT_ADDRESSES);
6481       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6482                    (int) BFD_RELOC_LO16, breg);
6483       offset_expr.X_add_number += 4;
6484       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6485                    (int) BFD_RELOC_LO16, breg);
6486       return;
6487
6488    /* New code added to support COPZ instructions.
6489       This code builds table entries out of the macros in mip_opcodes.
6490       R4000 uses interlocks to handle coproc delays.
6491       Other chips (like the R3000) require nops to be inserted for delays.
6492
6493       FIXME: Currently, we require that the user handle delays.
6494       In order to fill delay slots for non-interlocked chips,
6495       we must have a way to specify delays based on the coprocessor.
6496       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6497       What are the side-effects of the cop instruction?
6498       What cache support might we have and what are its effects?
6499       Both coprocessor & memory require delays. how long???
6500       What registers are read/set/modified?
6501
6502       If an itbl is provided to interpret cop instructions,
6503       this knowledge can be encoded in the itbl spec.  */
6504
6505     case M_COP0:
6506       s = "c0";
6507       goto copz;
6508     case M_COP1:
6509       s = "c1";
6510       goto copz;
6511     case M_COP2:
6512       s = "c2";
6513       goto copz;
6514     case M_COP3:
6515       s = "c3";
6516     copz:
6517       /* For now we just do C (same as Cz).  The parameter will be
6518          stored in insn_opcode by mips_ip.  */
6519       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6520                    ip->insn_opcode);
6521       return;
6522
6523     case M_MOVE:
6524       move_register (&icnt, dreg, sreg);
6525       return;
6526
6527 #ifdef LOSING_COMPILER
6528     default:
6529       /* Try and see if this is a new itbl instruction.
6530          This code builds table entries out of the macros in mip_opcodes.
6531          FIXME: For now we just assemble the expression and pass it's
6532          value along as a 32-bit immediate.
6533          We may want to have the assembler assemble this value,
6534          so that we gain the assembler's knowledge of delay slots,
6535          symbols, etc.
6536          Would it be more efficient to use mask (id) here? */
6537       if (itbl_have_entries
6538           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6539         {
6540           s = ip->insn_mo->name;
6541           s2 = "cop3";
6542           coproc = ITBL_DECODE_PNUM (immed_expr);;
6543           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6544           return;
6545         }
6546       macro2 (ip);
6547       return;
6548     }
6549   if (mips_opts.noat)
6550     as_warn (_("Macro used $at after \".set noat\""));
6551 }
6552
6553 static void
6554 macro2 (ip)
6555      struct mips_cl_insn *ip;
6556 {
6557   register int treg, sreg, dreg, breg;
6558   int tempreg;
6559   int mask;
6560   int icnt = 0;
6561   int used_at;
6562   expressionS expr1;
6563   const char *s;
6564   const char *s2;
6565   const char *fmt;
6566   int likely = 0;
6567   int dbl = 0;
6568   int coproc = 0;
6569   int lr = 0;
6570   int imm = 0;
6571   int off;
6572   offsetT maxnum;
6573   bfd_reloc_code_real_type r;
6574   char *p;
6575
6576   treg = (ip->insn_opcode >> 16) & 0x1f;
6577   dreg = (ip->insn_opcode >> 11) & 0x1f;
6578   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6579   mask = ip->insn_mo->mask;
6580
6581   expr1.X_op = O_constant;
6582   expr1.X_op_symbol = NULL;
6583   expr1.X_add_symbol = NULL;
6584   expr1.X_add_number = 1;
6585
6586   switch (mask)
6587     {
6588 #endif /* LOSING_COMPILER */
6589
6590     case M_DMUL:
6591       dbl = 1;
6592     case M_MUL:
6593       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6594                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6595       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6596                    dreg);
6597       return;
6598
6599     case M_DMUL_I:
6600       dbl = 1;
6601     case M_MUL_I:
6602       /* The MIPS assembler some times generates shifts and adds.  I'm
6603          not trying to be that fancy. GCC should do this for us
6604          anyway.  */
6605       load_register (&icnt, AT, &imm_expr, dbl);
6606       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6607                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6608       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6609                    dreg);
6610       break;
6611
6612     case M_DMULO_I:
6613       dbl = 1;
6614     case M_MULO_I:
6615       imm = 1;
6616       goto do_mulo;
6617
6618     case M_DMULO:
6619       dbl = 1;
6620     case M_MULO:
6621     do_mulo:
6622       mips_emit_delays (true);
6623       ++mips_opts.noreorder;
6624       mips_any_noreorder = 1;
6625       if (imm)
6626         load_register (&icnt, AT, &imm_expr, dbl);
6627       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6628                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6629       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6630                    dreg);
6631       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6632                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6633       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6634                    AT);
6635       if (mips_trap)
6636         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6637                      "s,t,q", dreg, AT, 6);
6638       else
6639         {
6640           expr1.X_add_number = 8;
6641           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6642                        AT);
6643           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6644                        0);
6645           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6646                        "c", 6);
6647         }
6648       --mips_opts.noreorder;
6649       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6650       break;
6651
6652     case M_DMULOU_I:
6653       dbl = 1;
6654     case M_MULOU_I:
6655       imm = 1;
6656       goto do_mulou;
6657
6658     case M_DMULOU:
6659       dbl = 1;
6660     case M_MULOU:
6661     do_mulou:
6662       mips_emit_delays (true);
6663       ++mips_opts.noreorder;
6664       mips_any_noreorder = 1;
6665       if (imm)
6666         load_register (&icnt, AT, &imm_expr, dbl);
6667       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6668                    dbl ? "dmultu" : "multu",
6669                    "s,t", sreg, imm ? AT : treg);
6670       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6671                    AT);
6672       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6673                    dreg);
6674       if (mips_trap)
6675         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6676                      "s,t,q", AT, 0, 6);
6677       else
6678         {
6679           expr1.X_add_number = 8;
6680           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6681           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6682                        0);
6683           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6684                        "c", 6);
6685         }
6686       --mips_opts.noreorder;
6687       break;
6688
6689     case M_DROL:
6690       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6691                    "d,v,t", AT, 0, treg);
6692       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6693                    "d,t,s", AT, sreg, AT);
6694       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6695                    "d,t,s", dreg, sreg, treg);
6696       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6697                    "d,v,t", dreg, dreg, AT);
6698       break;
6699
6700     case M_ROL:
6701       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6702                    "d,v,t", AT, 0, treg);
6703       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6704                    "d,t,s", AT, sreg, AT);
6705       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6706                    "d,t,s", dreg, sreg, treg);
6707       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6708                    "d,v,t", dreg, dreg, AT);
6709       break;
6710
6711     case M_DROL_I:
6712       {
6713         unsigned int rot;
6714
6715         if (imm_expr.X_op != O_constant)
6716           as_bad (_("rotate count too large"));
6717         rot = imm_expr.X_add_number & 0x3f;
6718         if (rot == 0)
6719           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6720                        "d,w,<", dreg, sreg, 0);
6721         else
6722           {
6723             char *l, *r;
6724
6725             l = (rot < 0x20) ? "dsll" : "dsll32";
6726             r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6727             rot &= 0x1f;
6728             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6729                          "d,w,<", AT, sreg, rot);
6730             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6731                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6732             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6733                          "d,v,t", dreg, dreg, AT);
6734           }
6735       }
6736       break;
6737
6738     case M_ROL_I:
6739       {
6740         unsigned int rot;
6741
6742         if (imm_expr.X_op != O_constant)
6743           as_bad (_("rotate count too large"));
6744         rot = imm_expr.X_add_number & 0x1f;
6745         if (rot == 0)
6746           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6747                        "d,w,<", dreg, sreg, 0);
6748         else
6749           {
6750             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6751                          "d,w,<", AT, sreg, rot);
6752             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6753                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6754             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6755                          "d,v,t", dreg, dreg, AT);
6756           }
6757       }
6758       break;
6759
6760     case M_DROR:
6761       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6762                    "d,v,t", AT, 0, treg);
6763       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6764                    "d,t,s", AT, sreg, AT);
6765       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6766                    "d,t,s", dreg, sreg, treg);
6767       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6768                    "d,v,t", dreg, dreg, AT);
6769       break;
6770
6771     case M_ROR:
6772       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6773                    "d,v,t", AT, 0, treg);
6774       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6775                    "d,t,s", AT, sreg, AT);
6776       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6777                    "d,t,s", dreg, sreg, treg);
6778       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6779                    "d,v,t", dreg, dreg, AT);
6780       break;
6781
6782     case M_DROR_I:
6783       {
6784         unsigned int rot;
6785
6786         if (imm_expr.X_op != O_constant)
6787           as_bad (_("rotate count too large"));
6788         rot = imm_expr.X_add_number & 0x3f;
6789         if (rot == 0)
6790           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6791                        "d,w,<", dreg, sreg, 0);
6792         else
6793           {
6794             char *l, *r;
6795
6796             r = (rot < 0x20) ? "dsrl" : "dsrl32";
6797             l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6798             rot &= 0x1f;
6799             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6800                          "d,w,<", AT, sreg, rot);
6801             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6802                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6803             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6804                          "d,v,t", dreg, dreg, AT);
6805           }
6806       }
6807       break;
6808
6809     case M_ROR_I:
6810       {
6811         unsigned int rot;
6812
6813         if (imm_expr.X_op != O_constant)
6814           as_bad (_("rotate count too large"));
6815         rot = imm_expr.X_add_number & 0x1f;
6816         if (rot == 0)
6817           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6818                        "d,w,<", dreg, sreg, 0);
6819         else
6820           {
6821             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6822                          "d,w,<", AT, sreg, rot);
6823             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6824                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6825             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6826                          "d,v,t", dreg, dreg, AT);
6827           }
6828       }
6829       break;
6830
6831     case M_S_DOB:
6832       if (mips_arch == CPU_R4650)
6833         {
6834           as_bad (_("opcode not supported on this processor"));
6835           return;
6836         }
6837       assert (mips_opts.isa == ISA_MIPS1);
6838       /* Even on a big endian machine $fn comes before $fn+1.  We have
6839          to adjust when storing to memory.  */
6840       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6841                    target_big_endian ? treg + 1 : treg,
6842                    (int) BFD_RELOC_LO16, breg);
6843       offset_expr.X_add_number += 4;
6844       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6845                    target_big_endian ? treg : treg + 1,
6846                    (int) BFD_RELOC_LO16, breg);
6847       return;
6848
6849     case M_SEQ:
6850       if (sreg == 0)
6851         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6852                      treg, (int) BFD_RELOC_LO16);
6853       else if (treg == 0)
6854         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6855                      sreg, (int) BFD_RELOC_LO16);
6856       else
6857         {
6858           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6859                        "d,v,t", dreg, sreg, treg);
6860           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6861                        dreg, (int) BFD_RELOC_LO16);
6862         }
6863       return;
6864
6865     case M_SEQ_I:
6866       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6867         {
6868           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6869                        sreg, (int) BFD_RELOC_LO16);
6870           return;
6871         }
6872       if (sreg == 0)
6873         {
6874           as_warn (_("Instruction %s: result is always false"),
6875                    ip->insn_mo->name);
6876           move_register (&icnt, dreg, 0);
6877           return;
6878         }
6879       if (imm_expr.X_op == O_constant
6880           && imm_expr.X_add_number >= 0
6881           && imm_expr.X_add_number < 0x10000)
6882         {
6883           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6884                        sreg, (int) BFD_RELOC_LO16);
6885           used_at = 0;
6886         }
6887       else if (imm_expr.X_op == O_constant
6888                && imm_expr.X_add_number > -0x8000
6889                && imm_expr.X_add_number < 0)
6890         {
6891           imm_expr.X_add_number = -imm_expr.X_add_number;
6892           macro_build ((char *) NULL, &icnt, &imm_expr,
6893                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6894                        "t,r,j", dreg, sreg,
6895                        (int) BFD_RELOC_LO16);
6896           used_at = 0;
6897         }
6898       else
6899         {
6900           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6901           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6902                        "d,v,t", dreg, sreg, AT);
6903           used_at = 1;
6904         }
6905       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6906                    (int) BFD_RELOC_LO16);
6907       if (used_at)
6908         break;
6909       return;
6910
6911     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
6912       s = "slt";
6913       goto sge;
6914     case M_SGEU:
6915       s = "sltu";
6916     sge:
6917       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6918                    dreg, sreg, treg);
6919       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6920                    (int) BFD_RELOC_LO16);
6921       return;
6922
6923     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
6924     case M_SGEU_I:
6925       if (imm_expr.X_op == O_constant
6926           && imm_expr.X_add_number >= -0x8000
6927           && imm_expr.X_add_number < 0x8000)
6928         {
6929           macro_build ((char *) NULL, &icnt, &imm_expr,
6930                        mask == M_SGE_I ? "slti" : "sltiu",
6931                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6932           used_at = 0;
6933         }
6934       else
6935         {
6936           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6937           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6938                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6939                        AT);
6940           used_at = 1;
6941         }
6942       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6943                    (int) BFD_RELOC_LO16);
6944       if (used_at)
6945         break;
6946       return;
6947
6948     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
6949       s = "slt";
6950       goto sgt;
6951     case M_SGTU:
6952       s = "sltu";
6953     sgt:
6954       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6955                    dreg, treg, sreg);
6956       return;
6957
6958     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
6959       s = "slt";
6960       goto sgti;
6961     case M_SGTU_I:
6962       s = "sltu";
6963     sgti:
6964       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6965       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6966                    dreg, AT, sreg);
6967       break;
6968
6969     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
6970       s = "slt";
6971       goto sle;
6972     case M_SLEU:
6973       s = "sltu";
6974     sle:
6975       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6976                    dreg, treg, sreg);
6977       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6978                    (int) BFD_RELOC_LO16);
6979       return;
6980
6981     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6982       s = "slt";
6983       goto slei;
6984     case M_SLEU_I:
6985       s = "sltu";
6986     slei:
6987       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6988       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6989                    dreg, AT, sreg);
6990       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6991                    (int) BFD_RELOC_LO16);
6992       break;
6993
6994     case M_SLT_I:
6995       if (imm_expr.X_op == O_constant
6996           && imm_expr.X_add_number >= -0x8000
6997           && imm_expr.X_add_number < 0x8000)
6998         {
6999           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7000                        dreg, sreg, (int) BFD_RELOC_LO16);
7001           return;
7002         }
7003       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7004       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7005                    dreg, sreg, AT);
7006       break;
7007
7008     case M_SLTU_I:
7009       if (imm_expr.X_op == O_constant
7010           && imm_expr.X_add_number >= -0x8000
7011           && imm_expr.X_add_number < 0x8000)
7012         {
7013           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7014                        dreg, sreg, (int) BFD_RELOC_LO16);
7015           return;
7016         }
7017       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7018       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7019                    "d,v,t", dreg, sreg, AT);
7020       break;
7021
7022     case M_SNE:
7023       if (sreg == 0)
7024         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7025                      "d,v,t", dreg, 0, treg);
7026       else if (treg == 0)
7027         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7028                      "d,v,t", dreg, 0, sreg);
7029       else
7030         {
7031           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7032                        "d,v,t", dreg, sreg, treg);
7033           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7034                        "d,v,t", dreg, 0, dreg);
7035         }
7036       return;
7037
7038     case M_SNE_I:
7039       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7040         {
7041           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7042                        "d,v,t", dreg, 0, sreg);
7043           return;
7044         }
7045       if (sreg == 0)
7046         {
7047           as_warn (_("Instruction %s: result is always true"),
7048                    ip->insn_mo->name);
7049           macro_build ((char *) NULL, &icnt, &expr1,
7050                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7051                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7052           return;
7053         }
7054       if (imm_expr.X_op == O_constant
7055           && imm_expr.X_add_number >= 0
7056           && imm_expr.X_add_number < 0x10000)
7057         {
7058           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7059                        dreg, sreg, (int) BFD_RELOC_LO16);
7060           used_at = 0;
7061         }
7062       else if (imm_expr.X_op == O_constant
7063                && imm_expr.X_add_number > -0x8000
7064                && imm_expr.X_add_number < 0)
7065         {
7066           imm_expr.X_add_number = -imm_expr.X_add_number;
7067           macro_build ((char *) NULL, &icnt, &imm_expr,
7068                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7069                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7070           used_at = 0;
7071         }
7072       else
7073         {
7074           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7075           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7076                        "d,v,t", dreg, sreg, AT);
7077           used_at = 1;
7078         }
7079       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7080                    "d,v,t", dreg, 0, dreg);
7081       if (used_at)
7082         break;
7083       return;
7084
7085     case M_DSUB_I:
7086       dbl = 1;
7087     case M_SUB_I:
7088       if (imm_expr.X_op == O_constant
7089           && imm_expr.X_add_number > -0x8000
7090           && imm_expr.X_add_number <= 0x8000)
7091         {
7092           imm_expr.X_add_number = -imm_expr.X_add_number;
7093           macro_build ((char *) NULL, &icnt, &imm_expr,
7094                        dbl ? "daddi" : "addi",
7095                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7096           return;
7097         }
7098       load_register (&icnt, AT, &imm_expr, dbl);
7099       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7100                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7101       break;
7102
7103     case M_DSUBU_I:
7104       dbl = 1;
7105     case M_SUBU_I:
7106       if (imm_expr.X_op == O_constant
7107           && imm_expr.X_add_number > -0x8000
7108           && imm_expr.X_add_number <= 0x8000)
7109         {
7110           imm_expr.X_add_number = -imm_expr.X_add_number;
7111           macro_build ((char *) NULL, &icnt, &imm_expr,
7112                        dbl ? "daddiu" : "addiu",
7113                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7114           return;
7115         }
7116       load_register (&icnt, AT, &imm_expr, dbl);
7117       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7118                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7119       break;
7120
7121     case M_TEQ_I:
7122       s = "teq";
7123       goto trap;
7124     case M_TGE_I:
7125       s = "tge";
7126       goto trap;
7127     case M_TGEU_I:
7128       s = "tgeu";
7129       goto trap;
7130     case M_TLT_I:
7131       s = "tlt";
7132       goto trap;
7133     case M_TLTU_I:
7134       s = "tltu";
7135       goto trap;
7136     case M_TNE_I:
7137       s = "tne";
7138     trap:
7139       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7140       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7141                    AT);
7142       break;
7143
7144     case M_TRUNCWS:
7145     case M_TRUNCWD:
7146       assert (mips_opts.isa == ISA_MIPS1);
7147       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7148       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7149
7150       /*
7151        * Is the double cfc1 instruction a bug in the mips assembler;
7152        * or is there a reason for it?
7153        */
7154       mips_emit_delays (true);
7155       ++mips_opts.noreorder;
7156       mips_any_noreorder = 1;
7157       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7158                    treg, RA);
7159       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7160                    treg, RA);
7161       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7162       expr1.X_add_number = 3;
7163       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7164                    (int) BFD_RELOC_LO16);
7165       expr1.X_add_number = 2;
7166       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7167                      (int) BFD_RELOC_LO16);
7168       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7169                    AT, RA);
7170       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7171       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7172               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7173       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7174                    treg, RA);
7175       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7176       --mips_opts.noreorder;
7177       break;
7178
7179     case M_ULH:
7180       s = "lb";
7181       goto ulh;
7182     case M_ULHU:
7183       s = "lbu";
7184     ulh:
7185       if (offset_expr.X_add_number >= 0x7fff)
7186         as_bad (_("operand overflow"));
7187       /* avoid load delay */
7188       if (! target_big_endian)
7189         ++offset_expr.X_add_number;
7190       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7191                    (int) BFD_RELOC_LO16, breg);
7192       if (! target_big_endian)
7193         --offset_expr.X_add_number;
7194       else
7195         ++offset_expr.X_add_number;
7196       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7197                    (int) BFD_RELOC_LO16, breg);
7198       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7199                    treg, treg, 8);
7200       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7201                    treg, treg, AT);
7202       break;
7203
7204     case M_ULD:
7205       s = "ldl";
7206       s2 = "ldr";
7207       off = 7;
7208       goto ulw;
7209     case M_ULW:
7210       s = "lwl";
7211       s2 = "lwr";
7212       off = 3;
7213     ulw:
7214       if (offset_expr.X_add_number >= 0x8000 - off)
7215         as_bad (_("operand overflow"));
7216       if (! target_big_endian)
7217         offset_expr.X_add_number += off;
7218       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7219                    (int) BFD_RELOC_LO16, breg);
7220       if (! target_big_endian)
7221         offset_expr.X_add_number -= off;
7222       else
7223         offset_expr.X_add_number += off;
7224       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7225                    (int) BFD_RELOC_LO16, breg);
7226       return;
7227
7228     case M_ULD_A:
7229       s = "ldl";
7230       s2 = "ldr";
7231       off = 7;
7232       goto ulwa;
7233     case M_ULW_A:
7234       s = "lwl";
7235       s2 = "lwr";
7236       off = 3;
7237     ulwa:
7238       used_at = 1;
7239       load_address (&icnt, AT, &offset_expr, &used_at);
7240       if (breg != 0)
7241         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7242                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7243                      "d,v,t", AT, AT, breg);
7244       if (! target_big_endian)
7245         expr1.X_add_number = off;
7246       else
7247         expr1.X_add_number = 0;
7248       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7249                    (int) BFD_RELOC_LO16, AT);
7250       if (! target_big_endian)
7251         expr1.X_add_number = 0;
7252       else
7253         expr1.X_add_number = off;
7254       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7255                    (int) BFD_RELOC_LO16, AT);
7256       break;
7257
7258     case M_ULH_A:
7259     case M_ULHU_A:
7260       used_at = 1;
7261       load_address (&icnt, AT, &offset_expr, &used_at);
7262       if (breg != 0)
7263         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7264                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7265                      "d,v,t", AT, AT, breg);
7266       if (target_big_endian)
7267         expr1.X_add_number = 0;
7268       macro_build ((char *) NULL, &icnt, &expr1,
7269                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7270                    (int) BFD_RELOC_LO16, AT);
7271       if (target_big_endian)
7272         expr1.X_add_number = 1;
7273       else
7274         expr1.X_add_number = 0;
7275       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7276                    (int) BFD_RELOC_LO16, AT);
7277       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7278                    treg, treg, 8);
7279       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7280                    treg, treg, AT);
7281       break;
7282
7283     case M_USH:
7284       if (offset_expr.X_add_number >= 0x7fff)
7285         as_bad (_("operand overflow"));
7286       if (target_big_endian)
7287         ++offset_expr.X_add_number;
7288       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7289                    (int) BFD_RELOC_LO16, breg);
7290       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7291                    AT, treg, 8);
7292       if (target_big_endian)
7293         --offset_expr.X_add_number;
7294       else
7295         ++offset_expr.X_add_number;
7296       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7297                    (int) BFD_RELOC_LO16, breg);
7298       break;
7299
7300     case M_USD:
7301       s = "sdl";
7302       s2 = "sdr";
7303       off = 7;
7304       goto usw;
7305     case M_USW:
7306       s = "swl";
7307       s2 = "swr";
7308       off = 3;
7309     usw:
7310       if (offset_expr.X_add_number >= 0x8000 - off)
7311         as_bad (_("operand overflow"));
7312       if (! target_big_endian)
7313         offset_expr.X_add_number += off;
7314       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7315                    (int) BFD_RELOC_LO16, breg);
7316       if (! target_big_endian)
7317         offset_expr.X_add_number -= off;
7318       else
7319         offset_expr.X_add_number += off;
7320       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7321                    (int) BFD_RELOC_LO16, breg);
7322       return;
7323
7324     case M_USD_A:
7325       s = "sdl";
7326       s2 = "sdr";
7327       off = 7;
7328       goto uswa;
7329     case M_USW_A:
7330       s = "swl";
7331       s2 = "swr";
7332       off = 3;
7333     uswa:
7334       used_at = 1;
7335       load_address (&icnt, AT, &offset_expr, &used_at);
7336       if (breg != 0)
7337         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7338                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7339                      "d,v,t", AT, AT, breg);
7340       if (! target_big_endian)
7341         expr1.X_add_number = off;
7342       else
7343         expr1.X_add_number = 0;
7344       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7345                    (int) BFD_RELOC_LO16, AT);
7346       if (! target_big_endian)
7347         expr1.X_add_number = 0;
7348       else
7349         expr1.X_add_number = off;
7350       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7351                    (int) BFD_RELOC_LO16, AT);
7352       break;
7353
7354     case M_USH_A:
7355       used_at = 1;
7356       load_address (&icnt, AT, &offset_expr, &used_at);
7357       if (breg != 0)
7358         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7359                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7360                      "d,v,t", AT, AT, breg);
7361       if (! target_big_endian)
7362         expr1.X_add_number = 0;
7363       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7364                    (int) BFD_RELOC_LO16, AT);
7365       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7366                    treg, treg, 8);
7367       if (! target_big_endian)
7368         expr1.X_add_number = 1;
7369       else
7370         expr1.X_add_number = 0;
7371       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7372                    (int) BFD_RELOC_LO16, AT);
7373       if (! target_big_endian)
7374         expr1.X_add_number = 0;
7375       else
7376         expr1.X_add_number = 1;
7377       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7378                    (int) BFD_RELOC_LO16, AT);
7379       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7380                    treg, treg, 8);
7381       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7382                    treg, treg, AT);
7383       break;
7384
7385     default:
7386       /* FIXME: Check if this is one of the itbl macros, since they
7387          are added dynamically.  */
7388       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7389       break;
7390     }
7391   if (mips_opts.noat)
7392     as_warn (_("Macro used $at after \".set noat\""));
7393 }
7394
7395 /* Implement macros in mips16 mode.  */
7396
7397 static void
7398 mips16_macro (ip)
7399      struct mips_cl_insn *ip;
7400 {
7401   int mask;
7402   int xreg, yreg, zreg, tmp;
7403   int icnt;
7404   expressionS expr1;
7405   int dbl;
7406   const char *s, *s2, *s3;
7407
7408   mask = ip->insn_mo->mask;
7409
7410   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7411   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7412   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7413
7414   icnt = 0;
7415
7416   expr1.X_op = O_constant;
7417   expr1.X_op_symbol = NULL;
7418   expr1.X_add_symbol = NULL;
7419   expr1.X_add_number = 1;
7420
7421   dbl = 0;
7422
7423   switch (mask)
7424     {
7425     default:
7426       internalError ();
7427
7428     case M_DDIV_3:
7429       dbl = 1;
7430     case M_DIV_3:
7431       s = "mflo";
7432       goto do_div3;
7433     case M_DREM_3:
7434       dbl = 1;
7435     case M_REM_3:
7436       s = "mfhi";
7437     do_div3:
7438       mips_emit_delays (true);
7439       ++mips_opts.noreorder;
7440       mips_any_noreorder = 1;
7441       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7442                    dbl ? "ddiv" : "div",
7443                    "0,x,y", xreg, yreg);
7444       expr1.X_add_number = 2;
7445       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7446       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7447                    7);
7448
7449       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7450          since that causes an overflow.  We should do that as well,
7451          but I don't see how to do the comparisons without a temporary
7452          register.  */
7453       --mips_opts.noreorder;
7454       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7455       break;
7456
7457     case M_DIVU_3:
7458       s = "divu";
7459       s2 = "mflo";
7460       goto do_divu3;
7461     case M_REMU_3:
7462       s = "divu";
7463       s2 = "mfhi";
7464       goto do_divu3;
7465     case M_DDIVU_3:
7466       s = "ddivu";
7467       s2 = "mflo";
7468       goto do_divu3;
7469     case M_DREMU_3:
7470       s = "ddivu";
7471       s2 = "mfhi";
7472     do_divu3:
7473       mips_emit_delays (true);
7474       ++mips_opts.noreorder;
7475       mips_any_noreorder = 1;
7476       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7477                    xreg, yreg);
7478       expr1.X_add_number = 2;
7479       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7480       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7481                    "6", 7);
7482       --mips_opts.noreorder;
7483       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7484       break;
7485
7486     case M_DMUL:
7487       dbl = 1;
7488     case M_MUL:
7489       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7490                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7491       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7492                    zreg);
7493       return;
7494
7495     case M_DSUBU_I:
7496       dbl = 1;
7497       goto do_subu;
7498     case M_SUBU_I:
7499     do_subu:
7500       if (imm_expr.X_op != O_constant)
7501         as_bad (_("Unsupported large constant"));
7502       imm_expr.X_add_number = -imm_expr.X_add_number;
7503       macro_build ((char *) NULL, &icnt, &imm_expr,
7504                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7505       break;
7506
7507     case M_SUBU_I_2:
7508       if (imm_expr.X_op != O_constant)
7509         as_bad (_("Unsupported large constant"));
7510       imm_expr.X_add_number = -imm_expr.X_add_number;
7511       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7512                    "x,k", xreg);
7513       break;
7514
7515     case M_DSUBU_I_2:
7516       if (imm_expr.X_op != O_constant)
7517         as_bad (_("Unsupported large constant"));
7518       imm_expr.X_add_number = -imm_expr.X_add_number;
7519       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7520                    "y,j", yreg);
7521       break;
7522
7523     case M_BEQ:
7524       s = "cmp";
7525       s2 = "bteqz";
7526       goto do_branch;
7527     case M_BNE:
7528       s = "cmp";
7529       s2 = "btnez";
7530       goto do_branch;
7531     case M_BLT:
7532       s = "slt";
7533       s2 = "btnez";
7534       goto do_branch;
7535     case M_BLTU:
7536       s = "sltu";
7537       s2 = "btnez";
7538       goto do_branch;
7539     case M_BLE:
7540       s = "slt";
7541       s2 = "bteqz";
7542       goto do_reverse_branch;
7543     case M_BLEU:
7544       s = "sltu";
7545       s2 = "bteqz";
7546       goto do_reverse_branch;
7547     case M_BGE:
7548       s = "slt";
7549       s2 = "bteqz";
7550       goto do_branch;
7551     case M_BGEU:
7552       s = "sltu";
7553       s2 = "bteqz";
7554       goto do_branch;
7555     case M_BGT:
7556       s = "slt";
7557       s2 = "btnez";
7558       goto do_reverse_branch;
7559     case M_BGTU:
7560       s = "sltu";
7561       s2 = "btnez";
7562
7563     do_reverse_branch:
7564       tmp = xreg;
7565       xreg = yreg;
7566       yreg = tmp;
7567
7568     do_branch:
7569       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7570                    xreg, yreg);
7571       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7572       break;
7573
7574     case M_BEQ_I:
7575       s = "cmpi";
7576       s2 = "bteqz";
7577       s3 = "x,U";
7578       goto do_branch_i;
7579     case M_BNE_I:
7580       s = "cmpi";
7581       s2 = "btnez";
7582       s3 = "x,U";
7583       goto do_branch_i;
7584     case M_BLT_I:
7585       s = "slti";
7586       s2 = "btnez";
7587       s3 = "x,8";
7588       goto do_branch_i;
7589     case M_BLTU_I:
7590       s = "sltiu";
7591       s2 = "btnez";
7592       s3 = "x,8";
7593       goto do_branch_i;
7594     case M_BLE_I:
7595       s = "slti";
7596       s2 = "btnez";
7597       s3 = "x,8";
7598       goto do_addone_branch_i;
7599     case M_BLEU_I:
7600       s = "sltiu";
7601       s2 = "btnez";
7602       s3 = "x,8";
7603       goto do_addone_branch_i;
7604     case M_BGE_I:
7605       s = "slti";
7606       s2 = "bteqz";
7607       s3 = "x,8";
7608       goto do_branch_i;
7609     case M_BGEU_I:
7610       s = "sltiu";
7611       s2 = "bteqz";
7612       s3 = "x,8";
7613       goto do_branch_i;
7614     case M_BGT_I:
7615       s = "slti";
7616       s2 = "bteqz";
7617       s3 = "x,8";
7618       goto do_addone_branch_i;
7619     case M_BGTU_I:
7620       s = "sltiu";
7621       s2 = "bteqz";
7622       s3 = "x,8";
7623
7624     do_addone_branch_i:
7625       if (imm_expr.X_op != O_constant)
7626         as_bad (_("Unsupported large constant"));
7627       ++imm_expr.X_add_number;
7628
7629     do_branch_i:
7630       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7631       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7632       break;
7633
7634     case M_ABS:
7635       expr1.X_add_number = 0;
7636       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7637       if (xreg != yreg)
7638         move_register (&icnt, xreg, yreg);
7639       expr1.X_add_number = 2;
7640       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7641       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7642                    "neg", "x,w", xreg, xreg);
7643     }
7644 }
7645
7646 /* For consistency checking, verify that all bits are specified either
7647    by the match/mask part of the instruction definition, or by the
7648    operand list.  */
7649 static int
7650 validate_mips_insn (opc)
7651      const struct mips_opcode *opc;
7652 {
7653   const char *p = opc->args;
7654   char c;
7655   unsigned long used_bits = opc->mask;
7656
7657   if ((used_bits & opc->match) != opc->match)
7658     {
7659       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7660               opc->name, opc->args);
7661       return 0;
7662     }
7663 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7664   while (*p)
7665     switch (c = *p++)
7666       {
7667       case ',': break;
7668       case '(': break;
7669       case ')': break;
7670       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7671       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7672       case 'A': break;
7673       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7674       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7675       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7676       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7677       case 'F': break;
7678       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7679       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7680       case 'I': break;
7681       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7682       case 'L': break;
7683       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7684       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7685       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
7686       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
7687                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7688       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7689       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7690       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7691       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7692       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7693       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7694       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7695       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7696       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7697       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7698       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7699       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7700       case 'f': break;
7701       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7702       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7703       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7704       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7705       case 'l': break;
7706       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7707       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7708       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7709       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7710       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7711       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7712       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7713       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7714       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7715       case 'x': break;
7716       case 'z': break;
7717       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7718       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7719                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7720       default:
7721         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7722                 c, opc->name, opc->args);
7723         return 0;
7724       }
7725 #undef USE_BITS
7726   if (used_bits != 0xffffffff)
7727     {
7728       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7729               ~used_bits & 0xffffffff, opc->name, opc->args);
7730       return 0;
7731     }
7732   return 1;
7733 }
7734
7735 /* This routine assembles an instruction into its binary format.  As a
7736    side effect, it sets one of the global variables imm_reloc or
7737    offset_reloc to the type of relocation to do if one of the operands
7738    is an address expression.  */
7739
7740 static void
7741 mips_ip (str, ip)
7742      char *str;
7743      struct mips_cl_insn *ip;
7744 {
7745   char *s;
7746   const char *args;
7747   char c = 0;
7748   struct mips_opcode *insn;
7749   char *argsStart;
7750   unsigned int regno;
7751   unsigned int lastregno = 0;
7752   char *s_reset;
7753   char save_c = 0;
7754
7755   insn_error = NULL;
7756
7757   /* If the instruction contains a '.', we first try to match an instruction
7758      including the '.'.  Then we try again without the '.'.  */
7759   insn = NULL;
7760   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7761     continue;
7762
7763   /* If we stopped on whitespace, then replace the whitespace with null for
7764      the call to hash_find.  Save the character we replaced just in case we
7765      have to re-parse the instruction.  */
7766   if (ISSPACE (*s))
7767     {
7768       save_c = *s;
7769       *s++ = '\0';
7770     }
7771
7772   insn = (struct mips_opcode *) hash_find (op_hash, str);
7773
7774   /* If we didn't find the instruction in the opcode table, try again, but
7775      this time with just the instruction up to, but not including the
7776      first '.'.  */
7777   if (insn == NULL)
7778     {
7779       /* Restore the character we overwrite above (if any).  */
7780       if (save_c)
7781         *(--s) = save_c;
7782
7783       /* Scan up to the first '.' or whitespace.  */
7784       for (s = str;
7785            *s != '\0' && *s != '.' && !ISSPACE (*s);
7786            ++s)
7787         continue;
7788
7789       /* If we did not find a '.', then we can quit now.  */
7790       if (*s != '.')
7791         {
7792           insn_error = "unrecognized opcode";
7793           return;
7794         }
7795
7796       /* Lookup the instruction in the hash table.  */
7797       *s++ = '\0';
7798       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7799         {
7800           insn_error = "unrecognized opcode";
7801           return;
7802         }
7803     }
7804
7805   argsStart = s;
7806   for (;;)
7807     {
7808       boolean ok;
7809
7810       assert (strcmp (insn->name, str) == 0);
7811
7812       if (OPCODE_IS_MEMBER (insn,
7813                             (mips_opts.isa
7814                              | (mips_opts.mips16 ? INSN_MIPS16 : 0)
7815                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7816                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7817                             mips_arch))
7818         ok = true;
7819       else
7820         ok = false;
7821
7822       if (insn->pinfo != INSN_MACRO)
7823         {
7824           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7825             ok = false;
7826         }
7827
7828       if (! ok)
7829         {
7830           if (insn + 1 < &mips_opcodes[NUMOPCODES]
7831               && strcmp (insn->name, insn[1].name) == 0)
7832             {
7833               ++insn;
7834               continue;
7835             }
7836           else
7837             {
7838               if (!insn_error)
7839                 {
7840                   static char buf[100];
7841                   if (mips_arch_info->is_isa)
7842                     sprintf (buf,
7843                              _("opcode not supported at this ISA level (%s)"),
7844                              mips_cpu_info_from_isa (mips_opts.isa)->name);
7845                   else
7846                     sprintf (buf,
7847                              _("opcode not supported on this processor: %s (%s)"),
7848                              mips_arch_info->name,
7849                              mips_cpu_info_from_isa (mips_opts.isa)->name);
7850                   insn_error = buf;
7851                 }
7852               if (save_c)
7853                 *(--s) = save_c;
7854               return;
7855             }
7856         }
7857
7858       ip->insn_mo = insn;
7859       ip->insn_opcode = insn->match;
7860       insn_error = NULL;
7861       for (args = insn->args;; ++args)
7862         {
7863           int is_mdmx;
7864
7865           s += strspn (s, " \t");
7866           is_mdmx = 0;
7867           switch (*args)
7868             {
7869             case '\0':          /* end of args */
7870               if (*s == '\0')
7871                 return;
7872               break;
7873
7874             case ',':
7875               if (*s++ == *args)
7876                 continue;
7877               s--;
7878               switch (*++args)
7879                 {
7880                 case 'r':
7881                 case 'v':
7882                   ip->insn_opcode |= lastregno << OP_SH_RS;
7883                   continue;
7884
7885                 case 'w':
7886                   ip->insn_opcode |= lastregno << OP_SH_RT;
7887                   continue;
7888
7889                 case 'W':
7890                   ip->insn_opcode |= lastregno << OP_SH_FT;
7891                   continue;
7892
7893                 case 'V':
7894                   ip->insn_opcode |= lastregno << OP_SH_FS;
7895                   continue;
7896                 }
7897               break;
7898
7899             case '(':
7900               /* Handle optional base register.
7901                  Either the base register is omitted or
7902                  we must have a left paren.  */
7903               /* This is dependent on the next operand specifier
7904                  is a base register specification.  */
7905               assert (args[1] == 'b' || args[1] == '5'
7906                       || args[1] == '-' || args[1] == '4');
7907               if (*s == '\0')
7908                 return;
7909
7910             case ')':           /* these must match exactly */
7911               if (*s++ == *args)
7912                 continue;
7913               break;
7914
7915             case '<':           /* must be at least one digit */
7916               /*
7917                * According to the manual, if the shift amount is greater
7918                * than 31 or less than 0, then the shift amount should be
7919                * mod 32.  In reality the mips assembler issues an error.
7920                * We issue a warning and mask out all but the low 5 bits.
7921                */
7922               my_getExpression (&imm_expr, s);
7923               check_absolute_expr (ip, &imm_expr);
7924               if ((unsigned long) imm_expr.X_add_number > 31)
7925                 {
7926                   as_warn (_("Improper shift amount (%lu)"),
7927                            (unsigned long) imm_expr.X_add_number);
7928                   imm_expr.X_add_number &= OP_MASK_SHAMT;
7929                 }
7930               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7931               imm_expr.X_op = O_absent;
7932               s = expr_end;
7933               continue;
7934
7935             case '>':           /* shift amount minus 32 */
7936               my_getExpression (&imm_expr, s);
7937               check_absolute_expr (ip, &imm_expr);
7938               if ((unsigned long) imm_expr.X_add_number < 32
7939                   || (unsigned long) imm_expr.X_add_number > 63)
7940                 break;
7941               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7942               imm_expr.X_op = O_absent;
7943               s = expr_end;
7944               continue;
7945
7946             case 'k':           /* cache code */
7947             case 'h':           /* prefx code */
7948               my_getExpression (&imm_expr, s);
7949               check_absolute_expr (ip, &imm_expr);
7950               if ((unsigned long) imm_expr.X_add_number > 31)
7951                 {
7952                   as_warn (_("Invalid value for `%s' (%lu)"),
7953                            ip->insn_mo->name,
7954                            (unsigned long) imm_expr.X_add_number);
7955                   imm_expr.X_add_number &= 0x1f;
7956                 }
7957               if (*args == 'k')
7958                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7959               else
7960                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7961               imm_expr.X_op = O_absent;
7962               s = expr_end;
7963               continue;
7964
7965             case 'c':           /* break code */
7966               my_getExpression (&imm_expr, s);
7967               check_absolute_expr (ip, &imm_expr);
7968               if ((unsigned long) imm_expr.X_add_number > 1023)
7969                 {
7970                   as_warn (_("Illegal break code (%lu)"),
7971                            (unsigned long) imm_expr.X_add_number);
7972                   imm_expr.X_add_number &= OP_MASK_CODE;
7973                 }
7974               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7975               imm_expr.X_op = O_absent;
7976               s = expr_end;
7977               continue;
7978
7979             case 'q':           /* lower break code */
7980               my_getExpression (&imm_expr, s);
7981               check_absolute_expr (ip, &imm_expr);
7982               if ((unsigned long) imm_expr.X_add_number > 1023)
7983                 {
7984                   as_warn (_("Illegal lower break code (%lu)"),
7985                            (unsigned long) imm_expr.X_add_number);
7986                   imm_expr.X_add_number &= OP_MASK_CODE2;
7987                 }
7988               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7989               imm_expr.X_op = O_absent;
7990               s = expr_end;
7991               continue;
7992
7993             case 'B':           /* 20-bit syscall/break code.  */
7994               my_getExpression (&imm_expr, s);
7995               check_absolute_expr (ip, &imm_expr);
7996               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
7997                 as_warn (_("Illegal 20-bit code (%lu)"),
7998                          (unsigned long) imm_expr.X_add_number);
7999               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8000               imm_expr.X_op = O_absent;
8001               s = expr_end;
8002               continue;
8003
8004             case 'C':           /* Coprocessor code */
8005               my_getExpression (&imm_expr, s);
8006               check_absolute_expr (ip, &imm_expr);
8007               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8008                 {
8009                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8010                            (unsigned long) imm_expr.X_add_number);
8011                   imm_expr.X_add_number &= ((1 << 25) - 1);
8012                 }
8013               ip->insn_opcode |= imm_expr.X_add_number;
8014               imm_expr.X_op = O_absent;
8015               s = expr_end;
8016               continue;
8017
8018             case 'J':           /* 19-bit wait code.  */
8019               my_getExpression (&imm_expr, s);
8020               check_absolute_expr (ip, &imm_expr);
8021               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8022                 as_warn (_("Illegal 19-bit code (%lu)"),
8023                          (unsigned long) imm_expr.X_add_number);
8024               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8025               imm_expr.X_op = O_absent;
8026               s = expr_end;
8027               continue;
8028
8029             case 'P':           /* Performance register */
8030               my_getExpression (&imm_expr, s);
8031               check_absolute_expr (ip, &imm_expr);
8032               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8033                 {
8034                   as_warn (_("Invalid performance register (%lu)"),
8035                            (unsigned long) imm_expr.X_add_number);
8036                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8037                 }
8038               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8039               imm_expr.X_op = O_absent;
8040               s = expr_end;
8041               continue;
8042
8043             case 'b':           /* base register */
8044             case 'd':           /* destination register */
8045             case 's':           /* source register */
8046             case 't':           /* target register */
8047             case 'r':           /* both target and source */
8048             case 'v':           /* both dest and source */
8049             case 'w':           /* both dest and target */
8050             case 'E':           /* coprocessor target register */
8051             case 'G':           /* coprocessor destination register */
8052             case 'x':           /* ignore register name */
8053             case 'z':           /* must be zero register */
8054             case 'U':           /* destination register (clo/clz).  */
8055               s_reset = s;
8056               if (s[0] == '$')
8057                 {
8058
8059                   if (ISDIGIT (s[1]))
8060                     {
8061                       ++s;
8062                       regno = 0;
8063                       do
8064                         {
8065                           regno *= 10;
8066                           regno += *s - '0';
8067                           ++s;
8068                         }
8069                       while (ISDIGIT (*s));
8070                       if (regno > 31)
8071                         as_bad (_("Invalid register number (%d)"), regno);
8072                     }
8073                   else if (*args == 'E' || *args == 'G')
8074                     goto notreg;
8075                   else
8076                     {
8077                       if (s[1] == 'r' && s[2] == 'a')
8078                         {
8079                           s += 3;
8080                           regno = RA;
8081                         }
8082                       else if (s[1] == 'f' && s[2] == 'p')
8083                         {
8084                           s += 3;
8085                           regno = FP;
8086                         }
8087                       else if (s[1] == 's' && s[2] == 'p')
8088                         {
8089                           s += 3;
8090                           regno = SP;
8091                         }
8092                       else if (s[1] == 'g' && s[2] == 'p')
8093                         {
8094                           s += 3;
8095                           regno = GP;
8096                         }
8097                       else if (s[1] == 'a' && s[2] == 't')
8098                         {
8099                           s += 3;
8100                           regno = AT;
8101                         }
8102                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8103                         {
8104                           s += 4;
8105                           regno = KT0;
8106                         }
8107                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8108                         {
8109                           s += 4;
8110                           regno = KT1;
8111                         }
8112                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8113                         {
8114                           s += 5;
8115                           regno = ZERO;
8116                         }
8117                       else if (itbl_have_entries)
8118                         {
8119                           char *p, *n;
8120                           unsigned long r;
8121
8122                           p = s + 1;    /* advance past '$' */
8123                           n = itbl_get_field (&p);  /* n is name */
8124
8125                           /* See if this is a register defined in an
8126                              itbl entry.  */
8127                           if (itbl_get_reg_val (n, &r))
8128                             {
8129                               /* Get_field advances to the start of
8130                                  the next field, so we need to back
8131                                  rack to the end of the last field.  */
8132                               if (p)
8133                                 s = p - 1;
8134                               else
8135                                 s = strchr (s, '\0');
8136                               regno = r;
8137                             }
8138                           else
8139                             goto notreg;
8140                         }
8141                       else
8142                         goto notreg;
8143                     }
8144                   if (regno == AT
8145                       && ! mips_opts.noat
8146                       && *args != 'E'
8147                       && *args != 'G')
8148                     as_warn (_("Used $at without \".set noat\""));
8149                   c = *args;
8150                   if (*s == ' ')
8151                     ++s;
8152                   if (args[1] != *s)
8153                     {
8154                       if (c == 'r' || c == 'v' || c == 'w')
8155                         {
8156                           regno = lastregno;
8157                           s = s_reset;
8158                           ++args;
8159                         }
8160                     }
8161                   /* 'z' only matches $0.  */
8162                   if (c == 'z' && regno != 0)
8163                     break;
8164
8165         /* Now that we have assembled one operand, we use the args string
8166          * to figure out where it goes in the instruction.  */
8167                   switch (c)
8168                     {
8169                     case 'r':
8170                     case 's':
8171                     case 'v':
8172                     case 'b':
8173                       ip->insn_opcode |= regno << OP_SH_RS;
8174                       break;
8175                     case 'd':
8176                     case 'G':
8177                       ip->insn_opcode |= regno << OP_SH_RD;
8178                       break;
8179                     case 'U':
8180                       ip->insn_opcode |= regno << OP_SH_RD;
8181                       ip->insn_opcode |= regno << OP_SH_RT;
8182                       break;
8183                     case 'w':
8184                     case 't':
8185                     case 'E':
8186                       ip->insn_opcode |= regno << OP_SH_RT;
8187                       break;
8188                     case 'x':
8189                       /* This case exists because on the r3000 trunc
8190                          expands into a macro which requires a gp
8191                          register.  On the r6000 or r4000 it is
8192                          assembled into a single instruction which
8193                          ignores the register.  Thus the insn version
8194                          is MIPS_ISA2 and uses 'x', and the macro
8195                          version is MIPS_ISA1 and uses 't'.  */
8196                       break;
8197                     case 'z':
8198                       /* This case is for the div instruction, which
8199                          acts differently if the destination argument
8200                          is $0.  This only matches $0, and is checked
8201                          outside the switch.  */
8202                       break;
8203                     case 'D':
8204                       /* Itbl operand; not yet implemented. FIXME ?? */
8205                       break;
8206                       /* What about all other operands like 'i', which
8207                          can be specified in the opcode table? */
8208                     }
8209                   lastregno = regno;
8210                   continue;
8211                 }
8212             notreg:
8213               switch (*args++)
8214                 {
8215                 case 'r':
8216                 case 'v':
8217                   ip->insn_opcode |= lastregno << OP_SH_RS;
8218                   continue;
8219                 case 'w':
8220                   ip->insn_opcode |= lastregno << OP_SH_RT;
8221                   continue;
8222                 }
8223               break;
8224
8225             case 'O':           /* MDMX alignment immediate constant.  */
8226               my_getExpression (&imm_expr, s);
8227               check_absolute_expr (ip, &imm_expr);
8228               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8229                 {
8230                   as_warn ("Improper align amount (%ld), using low bits",
8231                            (long) imm_expr.X_add_number);
8232                   imm_expr.X_add_number &= OP_MASK_ALN;
8233                 }
8234               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8235               imm_expr.X_op = O_absent;
8236               s = expr_end;
8237               continue;
8238
8239             case 'Q':           /* MDMX vector, element sel, or const.  */
8240               if (s[0] != '$')
8241                 {
8242                   /* MDMX Immediate.  */
8243                   my_getExpression (&imm_expr, s);
8244                   check_absolute_expr (ip, &imm_expr);
8245                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8246                     {
8247                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8248                                (long) imm_expr.X_add_number);
8249                       imm_expr.X_add_number &= OP_MASK_FT;
8250                     }
8251                   imm_expr.X_add_number &= OP_MASK_FT;
8252                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8253                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8254                   else
8255                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8256                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8257                   imm_expr.X_op = O_absent;
8258                   s = expr_end;
8259                   continue;
8260                 }
8261               /* Not MDMX Immediate.  Fall through.  */
8262             case 'X':           /* MDMX destination register.  */
8263             case 'Y':           /* MDMX source register.  */
8264             case 'Z':           /* MDMX target register.  */
8265               is_mdmx = 1;
8266             case 'D':           /* floating point destination register */
8267             case 'S':           /* floating point source register */
8268             case 'T':           /* floating point target register */
8269             case 'R':           /* floating point source register */
8270             case 'V':
8271             case 'W':
8272               s_reset = s;
8273               /* Accept $fN for FP and MDMX register numbers, and in
8274                  addition accept $vN for MDMX register numbers.  */
8275               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8276                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8277                       && ISDIGIT (s[2])))
8278                 {
8279                   s += 2;
8280                   regno = 0;
8281                   do
8282                     {
8283                       regno *= 10;
8284                       regno += *s - '0';
8285                       ++s;
8286                     }
8287                   while (ISDIGIT (*s));
8288
8289                   if (regno > 31)
8290                     as_bad (_("Invalid float register number (%d)"), regno);
8291
8292                   if ((regno & 1) != 0
8293                       && HAVE_32BIT_FPRS
8294                       && ! (strcmp (str, "mtc1") == 0
8295                             || strcmp (str, "mfc1") == 0
8296                             || strcmp (str, "lwc1") == 0
8297                             || strcmp (str, "swc1") == 0
8298                             || strcmp (str, "l.s") == 0
8299                             || strcmp (str, "s.s") == 0))
8300                     as_warn (_("Float register should be even, was %d"),
8301                              regno);
8302
8303                   c = *args;
8304                   if (*s == ' ')
8305                     ++s;
8306                   if (args[1] != *s)
8307                     {
8308                       if (c == 'V' || c == 'W')
8309                         {
8310                           regno = lastregno;
8311                           s = s_reset;
8312                           ++args;
8313                         }
8314                     }
8315                   switch (c)
8316                     {
8317                     case 'D':
8318                     case 'X':
8319                       ip->insn_opcode |= regno << OP_SH_FD;
8320                       break;
8321                     case 'V':
8322                     case 'S':
8323                     case 'Y':
8324                       ip->insn_opcode |= regno << OP_SH_FS;
8325                       break;
8326                     case 'Q':
8327                       /* This is like 'Z', but also needs to fix the MDMX
8328                          vector/scalar select bits.  Note that the
8329                          scalar immediate case is handled above.  */
8330                       if (*s == '[')
8331                         {
8332                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8333                           int max_el = (is_qh ? 3 : 7);
8334                           s++;
8335                           my_getExpression(&imm_expr, s);
8336                           check_absolute_expr (ip, &imm_expr);
8337                           s = expr_end;
8338                           if (imm_expr.X_add_number > max_el)
8339                             as_bad(_("Bad element selector %ld"),
8340                                    (long) imm_expr.X_add_number);
8341                           imm_expr.X_add_number &= max_el;
8342                           ip->insn_opcode |= (imm_expr.X_add_number
8343                                               << (OP_SH_VSEL +
8344                                                   (is_qh ? 2 : 1)));
8345                           if (*s != ']')
8346                             as_warn(_("Expecting ']' found '%s'"), s);
8347                           else
8348                             s++;
8349                         }
8350                       else
8351                         {
8352                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8353                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8354                                                 << OP_SH_VSEL);
8355                           else
8356                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8357                                                 OP_SH_VSEL);
8358                         }
8359                       /* Fall through */
8360                     case 'W':
8361                     case 'T':
8362                     case 'Z':
8363                       ip->insn_opcode |= regno << OP_SH_FT;
8364                       break;
8365                     case 'R':
8366                       ip->insn_opcode |= regno << OP_SH_FR;
8367                       break;
8368                     }
8369                   lastregno = regno;
8370                   continue;
8371                 }
8372
8373               switch (*args++)
8374                 {
8375                 case 'V':
8376                   ip->insn_opcode |= lastregno << OP_SH_FS;
8377                   continue;
8378                 case 'W':
8379                   ip->insn_opcode |= lastregno << OP_SH_FT;
8380                   continue;
8381                 }
8382               break;
8383
8384             case 'I':
8385               my_getExpression (&imm_expr, s);
8386               if (imm_expr.X_op != O_big
8387                   && imm_expr.X_op != O_constant)
8388                 insn_error = _("absolute expression required");
8389               s = expr_end;
8390               continue;
8391
8392             case 'A':
8393               my_getExpression (&offset_expr, s);
8394               *imm_reloc = BFD_RELOC_32;
8395               s = expr_end;
8396               continue;
8397
8398             case 'F':
8399             case 'L':
8400             case 'f':
8401             case 'l':
8402               {
8403                 int f64;
8404                 int using_gprs;
8405                 char *save_in;
8406                 char *err;
8407                 unsigned char temp[8];
8408                 int len;
8409                 unsigned int length;
8410                 segT seg;
8411                 subsegT subseg;
8412                 char *p;
8413
8414                 /* These only appear as the last operand in an
8415                    instruction, and every instruction that accepts
8416                    them in any variant accepts them in all variants.
8417                    This means we don't have to worry about backing out
8418                    any changes if the instruction does not match.
8419
8420                    The difference between them is the size of the
8421                    floating point constant and where it goes.  For 'F'
8422                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8423                    is 32 bits.  Where the constant is placed is based
8424                    on how the MIPS assembler does things:
8425                     F -- .rdata
8426                     L -- .lit8
8427                     f -- immediate value
8428                     l -- .lit4
8429
8430                     The .lit4 and .lit8 sections are only used if
8431                     permitted by the -G argument.
8432
8433                     When generating embedded PIC code, we use the
8434                     .lit8 section but not the .lit4 section (we can do
8435                     .lit4 inline easily; we need to put .lit8
8436                     somewhere in the data segment, and using .lit8
8437                     permits the linker to eventually combine identical
8438                     .lit8 entries).
8439
8440                     The code below needs to know whether the target register
8441                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8442                     'F' are used with GPR-based instructions and 'l' and
8443                     'L' are used with FPR-based instructions.  */
8444
8445                 f64 = *args == 'F' || *args == 'L';
8446                 using_gprs = *args == 'F' || *args == 'f';
8447
8448                 save_in = input_line_pointer;
8449                 input_line_pointer = s;
8450                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8451                 length = len;
8452                 s = input_line_pointer;
8453                 input_line_pointer = save_in;
8454                 if (err != NULL && *err != '\0')
8455                   {
8456                     as_bad (_("Bad floating point constant: %s"), err);
8457                     memset (temp, '\0', sizeof temp);
8458                     length = f64 ? 8 : 4;
8459                   }
8460
8461                 assert (length == (unsigned) (f64 ? 8 : 4));
8462
8463                 if (*args == 'f'
8464                     || (*args == 'l'
8465                         && (! USE_GLOBAL_POINTER_OPT
8466                             || mips_pic == EMBEDDED_PIC
8467                             || g_switch_value < 4
8468                             || (temp[0] == 0 && temp[1] == 0)
8469                             || (temp[2] == 0 && temp[3] == 0))))
8470                   {
8471                     imm_expr.X_op = O_constant;
8472                     if (! target_big_endian)
8473                       imm_expr.X_add_number = bfd_getl32 (temp);
8474                     else
8475                       imm_expr.X_add_number = bfd_getb32 (temp);
8476                   }
8477                 else if (length > 4
8478                          && ! mips_disable_float_construction
8479                          /* Constants can only be constructed in GPRs and
8480                             copied to FPRs if the GPRs are at least as wide
8481                             as the FPRs.  Force the constant into memory if
8482                             we are using 64-bit FPRs but the GPRs are only
8483                             32 bits wide.  */
8484                          && (using_gprs
8485                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8486                          && ((temp[0] == 0 && temp[1] == 0)
8487                              || (temp[2] == 0 && temp[3] == 0))
8488                          && ((temp[4] == 0 && temp[5] == 0)
8489                              || (temp[6] == 0 && temp[7] == 0)))
8490                   {
8491                     /* The value is simple enough to load with a couple of
8492                        instructions.  If using 32-bit registers, set
8493                        imm_expr to the high order 32 bits and offset_expr to
8494                        the low order 32 bits.  Otherwise, set imm_expr to
8495                        the entire 64 bit constant.  */
8496                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8497                       {
8498                         imm_expr.X_op = O_constant;
8499                         offset_expr.X_op = O_constant;
8500                         if (! target_big_endian)
8501                           {
8502                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8503                             offset_expr.X_add_number = bfd_getl32 (temp);
8504                           }
8505                         else
8506                           {
8507                             imm_expr.X_add_number = bfd_getb32 (temp);
8508                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8509                           }
8510                         if (offset_expr.X_add_number == 0)
8511                           offset_expr.X_op = O_absent;
8512                       }
8513                     else if (sizeof (imm_expr.X_add_number) > 4)
8514                       {
8515                         imm_expr.X_op = O_constant;
8516                         if (! target_big_endian)
8517                           imm_expr.X_add_number = bfd_getl64 (temp);
8518                         else
8519                           imm_expr.X_add_number = bfd_getb64 (temp);
8520                       }
8521                     else
8522                       {
8523                         imm_expr.X_op = O_big;
8524                         imm_expr.X_add_number = 4;
8525                         if (! target_big_endian)
8526                           {
8527                             generic_bignum[0] = bfd_getl16 (temp);
8528                             generic_bignum[1] = bfd_getl16 (temp + 2);
8529                             generic_bignum[2] = bfd_getl16 (temp + 4);
8530                             generic_bignum[3] = bfd_getl16 (temp + 6);
8531                           }
8532                         else
8533                           {
8534                             generic_bignum[0] = bfd_getb16 (temp + 6);
8535                             generic_bignum[1] = bfd_getb16 (temp + 4);
8536                             generic_bignum[2] = bfd_getb16 (temp + 2);
8537                             generic_bignum[3] = bfd_getb16 (temp);
8538                           }
8539                       }
8540                   }
8541                 else
8542                   {
8543                     const char *newname;
8544                     segT new_seg;
8545
8546                     /* Switch to the right section.  */
8547                     seg = now_seg;
8548                     subseg = now_subseg;
8549                     switch (*args)
8550                       {
8551                       default: /* unused default case avoids warnings.  */
8552                       case 'L':
8553                         newname = RDATA_SECTION_NAME;
8554                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8555                             || mips_pic == EMBEDDED_PIC)
8556                           newname = ".lit8";
8557                         break;
8558                       case 'F':
8559                         if (mips_pic == EMBEDDED_PIC)
8560                           newname = ".lit8";
8561                         else
8562                           newname = RDATA_SECTION_NAME;
8563                         break;
8564                       case 'l':
8565                         assert (!USE_GLOBAL_POINTER_OPT
8566                                 || g_switch_value >= 4);
8567                         newname = ".lit4";
8568                         break;
8569                       }
8570                     new_seg = subseg_new (newname, (subsegT) 0);
8571                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8572                       bfd_set_section_flags (stdoutput, new_seg,
8573                                              (SEC_ALLOC
8574                                               | SEC_LOAD
8575                                               | SEC_READONLY
8576                                               | SEC_DATA));
8577                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8578                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8579                         && strcmp (TARGET_OS, "elf") != 0)
8580                       record_alignment (new_seg, 4);
8581                     else
8582                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8583                     if (seg == now_seg)
8584                       as_bad (_("Can't use floating point insn in this section"));
8585
8586                     /* Set the argument to the current address in the
8587                        section.  */
8588                     offset_expr.X_op = O_symbol;
8589                     offset_expr.X_add_symbol =
8590                       symbol_new ("L0\001", now_seg,
8591                                   (valueT) frag_now_fix (), frag_now);
8592                     offset_expr.X_add_number = 0;
8593
8594                     /* Put the floating point number into the section.  */
8595                     p = frag_more ((int) length);
8596                     memcpy (p, temp, length);
8597
8598                     /* Switch back to the original section.  */
8599                     subseg_set (seg, subseg);
8600                   }
8601               }
8602               continue;
8603
8604             case 'i':           /* 16 bit unsigned immediate */
8605             case 'j':           /* 16 bit signed immediate */
8606               *imm_reloc = BFD_RELOC_LO16;
8607               c = my_getSmallExpression (&imm_expr, s);
8608               if (c != S_EX_NONE)
8609                 {
8610                   if (c != S_EX_LO)
8611                     {
8612                       if (c == S_EX_HI)
8613                         {
8614                           *imm_reloc = BFD_RELOC_HI16_S;
8615                           imm_unmatched_hi = true;
8616                         }
8617 #ifdef OBJ_ELF
8618                       else if (c == S_EX_HIGHEST)
8619                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8620                       else if (c == S_EX_HIGHER)
8621                         *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8622                       else if (c == S_EX_GP_REL)
8623                         {
8624                           /* This occurs in NewABI only.  */
8625                           c = my_getSmallExpression (&imm_expr, s);
8626                           if (c != S_EX_NEG)
8627                             as_bad (_("bad composition of relocations"));
8628                           else
8629                             {
8630                               c = my_getSmallExpression (&imm_expr, s);
8631                               if (c != S_EX_LO)
8632                                 as_bad (_("bad composition of relocations"));
8633                               else
8634                                 {
8635                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8636                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8637                                   imm_reloc[2] = BFD_RELOC_LO16;
8638                                 }
8639                             }
8640                         }
8641 #endif
8642                       else
8643                         *imm_reloc = BFD_RELOC_HI16;
8644                     }
8645                   else if (imm_expr.X_op == O_constant)
8646                     imm_expr.X_add_number &= 0xffff;
8647                 }
8648               if (*args == 'i')
8649                 {
8650                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8651                       || ((imm_expr.X_add_number < 0
8652                            || imm_expr.X_add_number >= 0x10000)
8653                           && imm_expr.X_op == O_constant))
8654                     {
8655                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8656                           !strcmp (insn->name, insn[1].name))
8657                         break;
8658                       if (imm_expr.X_op == O_constant
8659                           || imm_expr.X_op == O_big)
8660                         as_bad (_("16 bit expression not in range 0..65535"));
8661                     }
8662                 }
8663               else
8664                 {
8665                   int more;
8666                   offsetT max;
8667
8668                   /* The upper bound should be 0x8000, but
8669                      unfortunately the MIPS assembler accepts numbers
8670                      from 0x8000 to 0xffff and sign extends them, and
8671                      we want to be compatible.  We only permit this
8672                      extended range for an instruction which does not
8673                      provide any further alternates, since those
8674                      alternates may handle other cases.  People should
8675                      use the numbers they mean, rather than relying on
8676                      a mysterious sign extension.  */
8677                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8678                           strcmp (insn->name, insn[1].name) == 0);
8679                   if (more)
8680                     max = 0x8000;
8681                   else
8682                     max = 0x10000;
8683                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8684                       || ((imm_expr.X_add_number < -0x8000
8685                            || imm_expr.X_add_number >= max)
8686                           && imm_expr.X_op == O_constant)
8687                       || (more
8688                           && imm_expr.X_add_number < 0
8689                           && HAVE_64BIT_GPRS
8690                           && imm_expr.X_unsigned
8691                           && sizeof (imm_expr.X_add_number) <= 4))
8692                     {
8693                       if (more)
8694                         break;
8695                       if (imm_expr.X_op == O_constant
8696                           || imm_expr.X_op == O_big)
8697                         as_bad (_("16 bit expression not in range -32768..32767"));
8698                     }
8699                 }
8700               s = expr_end;
8701               continue;
8702
8703             case 'o':           /* 16 bit offset */
8704               c = my_getSmallExpression (&offset_expr, s);
8705
8706               /* If this value won't fit into a 16 bit offset, then go
8707                  find a macro that will generate the 32 bit offset
8708                  code pattern.  */
8709               if (c == S_EX_NONE
8710                   && (offset_expr.X_op != O_constant
8711                       || offset_expr.X_add_number >= 0x8000
8712                       || offset_expr.X_add_number < -0x8000))
8713                 break;
8714
8715               if (c == S_EX_HI)
8716                 {
8717                   if (offset_expr.X_op != O_constant)
8718                     break;
8719                   offset_expr.X_add_number =
8720                     (offset_expr.X_add_number >> 16) & 0xffff;
8721                 }
8722               *offset_reloc = BFD_RELOC_LO16;
8723               s = expr_end;
8724               continue;
8725
8726             case 'p':           /* pc relative offset */
8727               if (mips_pic == EMBEDDED_PIC)
8728                 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8729               else
8730                 *offset_reloc = BFD_RELOC_16_PCREL;
8731               my_getExpression (&offset_expr, s);
8732               s = expr_end;
8733               continue;
8734
8735             case 'u':           /* upper 16 bits */
8736               c = my_getSmallExpression (&imm_expr, s);
8737               *imm_reloc = BFD_RELOC_LO16;
8738               if (c != S_EX_NONE)
8739                 {
8740                   if (c != S_EX_LO)
8741                     {
8742                       if (c == S_EX_HI)
8743                         {
8744                           *imm_reloc = BFD_RELOC_HI16_S;
8745                           imm_unmatched_hi = true;
8746                         }
8747 #ifdef OBJ_ELF
8748                       else if (c == S_EX_HIGHEST)
8749                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8750                       else if (c == S_EX_GP_REL)
8751                         {
8752                           /* This occurs in NewABI only.  */
8753                           c = my_getSmallExpression (&imm_expr, s);
8754                           if (c != S_EX_NEG)
8755                             as_bad (_("bad composition of relocations"));
8756                           else
8757                             {
8758                               c = my_getSmallExpression (&imm_expr, s);
8759                               if (c != S_EX_HI)
8760                                 as_bad (_("bad composition of relocations"));
8761                               else
8762                                 {
8763                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8764                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8765                                   imm_reloc[2] = BFD_RELOC_HI16_S;
8766                                 }
8767                             }
8768                         }
8769 #endif
8770                       else
8771                         *imm_reloc = BFD_RELOC_HI16;
8772                     }
8773                   else if (imm_expr.X_op == O_constant)
8774                     imm_expr.X_add_number &= 0xffff;
8775                 }
8776               else if (imm_expr.X_op == O_constant
8777                        && (imm_expr.X_add_number < 0
8778                            || imm_expr.X_add_number >= 0x10000))
8779                 as_bad (_("lui expression not in range 0..65535"));
8780               s = expr_end;
8781               continue;
8782
8783             case 'a':           /* 26 bit address */
8784               my_getExpression (&offset_expr, s);
8785               s = expr_end;
8786               *offset_reloc = BFD_RELOC_MIPS_JMP;
8787               continue;
8788
8789             case 'N':           /* 3 bit branch condition code */
8790             case 'M':           /* 3 bit compare condition code */
8791               if (strncmp (s, "$fcc", 4) != 0)
8792                 break;
8793               s += 4;
8794               regno = 0;
8795               do
8796                 {
8797                   regno *= 10;
8798                   regno += *s - '0';
8799                   ++s;
8800                 }
8801               while (ISDIGIT (*s));
8802               if (regno > 7)
8803                 as_bad (_("invalid condition code register $fcc%d"), regno);
8804               if (*args == 'N')
8805                 ip->insn_opcode |= regno << OP_SH_BCC;
8806               else
8807                 ip->insn_opcode |= regno << OP_SH_CCC;
8808               continue;
8809
8810             case 'H':
8811               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8812                 s += 2;
8813               if (ISDIGIT (*s))
8814                 {
8815                   c = 0;
8816                   do
8817                     {
8818                       c *= 10;
8819                       c += *s - '0';
8820                       ++s;
8821                     }
8822                   while (ISDIGIT (*s));
8823                 }
8824               else
8825                 c = 8; /* Invalid sel value.  */
8826
8827               if (c > 7)
8828                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8829               ip->insn_opcode |= c;
8830               continue;
8831
8832             default:
8833               as_bad (_("bad char = '%c'\n"), *args);
8834               internalError ();
8835             }
8836           break;
8837         }
8838       /* Args don't match.  */
8839       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8840           !strcmp (insn->name, insn[1].name))
8841         {
8842           ++insn;
8843           s = argsStart;
8844           insn_error = _("illegal operands");
8845           continue;
8846         }
8847       if (save_c)
8848         *(--s) = save_c;
8849       insn_error = _("illegal operands");
8850       return;
8851     }
8852 }
8853
8854 /* This routine assembles an instruction into its binary format when
8855    assembling for the mips16.  As a side effect, it sets one of the
8856    global variables imm_reloc or offset_reloc to the type of
8857    relocation to do if one of the operands is an address expression.
8858    It also sets mips16_small and mips16_ext if the user explicitly
8859    requested a small or extended instruction.  */
8860
8861 static void
8862 mips16_ip (str, ip)
8863      char *str;
8864      struct mips_cl_insn *ip;
8865 {
8866   char *s;
8867   const char *args;
8868   struct mips_opcode *insn;
8869   char *argsstart;
8870   unsigned int regno;
8871   unsigned int lastregno = 0;
8872   char *s_reset;
8873
8874   insn_error = NULL;
8875
8876   mips16_small = false;
8877   mips16_ext = false;
8878
8879   for (s = str; ISLOWER (*s); ++s)
8880     ;
8881   switch (*s)
8882     {
8883     case '\0':
8884       break;
8885
8886     case ' ':
8887       *s++ = '\0';
8888       break;
8889
8890     case '.':
8891       if (s[1] == 't' && s[2] == ' ')
8892         {
8893           *s = '\0';
8894           mips16_small = true;
8895           s += 3;
8896           break;
8897         }
8898       else if (s[1] == 'e' && s[2] == ' ')
8899         {
8900           *s = '\0';
8901           mips16_ext = true;
8902           s += 3;
8903           break;
8904         }
8905       /* Fall through.  */
8906     default:
8907       insn_error = _("unknown opcode");
8908       return;
8909     }
8910
8911   if (mips_opts.noautoextend && ! mips16_ext)
8912     mips16_small = true;
8913
8914   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8915     {
8916       insn_error = _("unrecognized opcode");
8917       return;
8918     }
8919
8920   argsstart = s;
8921   for (;;)
8922     {
8923       assert (strcmp (insn->name, str) == 0);
8924
8925       ip->insn_mo = insn;
8926       ip->insn_opcode = insn->match;
8927       ip->use_extend = false;
8928       imm_expr.X_op = O_absent;
8929       imm_reloc[0] = BFD_RELOC_UNUSED;
8930       imm_reloc[1] = BFD_RELOC_UNUSED;
8931       imm_reloc[2] = BFD_RELOC_UNUSED;
8932       offset_expr.X_op = O_absent;
8933       offset_reloc[0] = BFD_RELOC_UNUSED;
8934       offset_reloc[1] = BFD_RELOC_UNUSED;
8935       offset_reloc[2] = BFD_RELOC_UNUSED;
8936       for (args = insn->args; 1; ++args)
8937         {
8938           int c;
8939
8940           if (*s == ' ')
8941             ++s;
8942
8943           /* In this switch statement we call break if we did not find
8944              a match, continue if we did find a match, or return if we
8945              are done.  */
8946
8947           c = *args;
8948           switch (c)
8949             {
8950             case '\0':
8951               if (*s == '\0')
8952                 {
8953                   /* Stuff the immediate value in now, if we can.  */
8954                   if (imm_expr.X_op == O_constant
8955                       && *imm_reloc > BFD_RELOC_UNUSED
8956                       && insn->pinfo != INSN_MACRO)
8957                     {
8958                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8959                                     imm_expr.X_add_number, true, mips16_small,
8960                                     mips16_ext, &ip->insn_opcode,
8961                                     &ip->use_extend, &ip->extend);
8962                       imm_expr.X_op = O_absent;
8963                       *imm_reloc = BFD_RELOC_UNUSED;
8964                     }
8965
8966                   return;
8967                 }
8968               break;
8969
8970             case ',':
8971               if (*s++ == c)
8972                 continue;
8973               s--;
8974               switch (*++args)
8975                 {
8976                 case 'v':
8977                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8978                   continue;
8979                 case 'w':
8980                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8981                   continue;
8982                 }
8983               break;
8984
8985             case '(':
8986             case ')':
8987               if (*s++ == c)
8988                 continue;
8989               break;
8990
8991             case 'v':
8992             case 'w':
8993               if (s[0] != '$')
8994                 {
8995                   if (c == 'v')
8996                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8997                   else
8998                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8999                   ++args;
9000                   continue;
9001                 }
9002               /* Fall through.  */
9003             case 'x':
9004             case 'y':
9005             case 'z':
9006             case 'Z':
9007             case '0':
9008             case 'S':
9009             case 'R':
9010             case 'X':
9011             case 'Y':
9012               if (s[0] != '$')
9013                 break;
9014               s_reset = s;
9015               if (ISDIGIT (s[1]))
9016                 {
9017                   ++s;
9018                   regno = 0;
9019                   do
9020                     {
9021                       regno *= 10;
9022                       regno += *s - '0';
9023                       ++s;
9024                     }
9025                   while (ISDIGIT (*s));
9026                   if (regno > 31)
9027                     {
9028                       as_bad (_("invalid register number (%d)"), regno);
9029                       regno = 2;
9030                     }
9031                 }
9032               else
9033                 {
9034                   if (s[1] == 'r' && s[2] == 'a')
9035                     {
9036                       s += 3;
9037                       regno = RA;
9038                     }
9039                   else if (s[1] == 'f' && s[2] == 'p')
9040                     {
9041                       s += 3;
9042                       regno = FP;
9043                     }
9044                   else if (s[1] == 's' && s[2] == 'p')
9045                     {
9046                       s += 3;
9047                       regno = SP;
9048                     }
9049                   else if (s[1] == 'g' && s[2] == 'p')
9050                     {
9051                       s += 3;
9052                       regno = GP;
9053                     }
9054                   else if (s[1] == 'a' && s[2] == 't')
9055                     {
9056                       s += 3;
9057                       regno = AT;
9058                     }
9059                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9060                     {
9061                       s += 4;
9062                       regno = KT0;
9063                     }
9064                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9065                     {
9066                       s += 4;
9067                       regno = KT1;
9068                     }
9069                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9070                     {
9071                       s += 5;
9072                       regno = ZERO;
9073                     }
9074                   else
9075                     break;
9076                 }
9077
9078               if (*s == ' ')
9079                 ++s;
9080               if (args[1] != *s)
9081                 {
9082                   if (c == 'v' || c == 'w')
9083                     {
9084                       regno = mips16_to_32_reg_map[lastregno];
9085                       s = s_reset;
9086                       ++args;
9087                     }
9088                 }
9089
9090               switch (c)
9091                 {
9092                 case 'x':
9093                 case 'y':
9094                 case 'z':
9095                 case 'v':
9096                 case 'w':
9097                 case 'Z':
9098                   regno = mips32_to_16_reg_map[regno];
9099                   break;
9100
9101                 case '0':
9102                   if (regno != 0)
9103                     regno = ILLEGAL_REG;
9104                   break;
9105
9106                 case 'S':
9107                   if (regno != SP)
9108                     regno = ILLEGAL_REG;
9109                   break;
9110
9111                 case 'R':
9112                   if (regno != RA)
9113                     regno = ILLEGAL_REG;
9114                   break;
9115
9116                 case 'X':
9117                 case 'Y':
9118                   if (regno == AT && ! mips_opts.noat)
9119                     as_warn (_("used $at without \".set noat\""));
9120                   break;
9121
9122                 default:
9123                   internalError ();
9124                 }
9125
9126               if (regno == ILLEGAL_REG)
9127                 break;
9128
9129               switch (c)
9130                 {
9131                 case 'x':
9132                 case 'v':
9133                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9134                   break;
9135                 case 'y':
9136                 case 'w':
9137                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9138                   break;
9139                 case 'z':
9140                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9141                   break;
9142                 case 'Z':
9143                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9144                 case '0':
9145                 case 'S':
9146                 case 'R':
9147                   break;
9148                 case 'X':
9149                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9150                   break;
9151                 case 'Y':
9152                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9153                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9154                   break;
9155                 default:
9156                   internalError ();
9157                 }
9158
9159               lastregno = regno;
9160               continue;
9161
9162             case 'P':
9163               if (strncmp (s, "$pc", 3) == 0)
9164                 {
9165                   s += 3;
9166                   continue;
9167                 }
9168               break;
9169
9170             case '<':
9171             case '>':
9172             case '[':
9173             case ']':
9174             case '4':
9175             case '5':
9176             case 'H':
9177             case 'W':
9178             case 'D':
9179             case 'j':
9180             case '8':
9181             case 'V':
9182             case 'C':
9183             case 'U':
9184             case 'k':
9185             case 'K':
9186               if (s[0] == '%'
9187                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9188                 {
9189                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9190                      and generate the appropriate reloc.  If the text
9191                      inside %gprel is not a symbol name with an
9192                      optional offset, then we generate a normal reloc
9193                      and will probably fail later.  */
9194                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9195                   if (imm_expr.X_op == O_symbol)
9196                     {
9197                       mips16_ext = true;
9198                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9199                       s = expr_end;
9200                       ip->use_extend = true;
9201                       ip->extend = 0;
9202                       continue;
9203                     }
9204                 }
9205               else
9206                 {
9207                   /* Just pick up a normal expression.  */
9208                   my_getExpression (&imm_expr, s);
9209                 }
9210
9211               if (imm_expr.X_op == O_register)
9212                 {
9213                   /* What we thought was an expression turned out to
9214                      be a register.  */
9215
9216                   if (s[0] == '(' && args[1] == '(')
9217                     {
9218                       /* It looks like the expression was omitted
9219                          before a register indirection, which means
9220                          that the expression is implicitly zero.  We
9221                          still set up imm_expr, so that we handle
9222                          explicit extensions correctly.  */
9223                       imm_expr.X_op = O_constant;
9224                       imm_expr.X_add_number = 0;
9225                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9226                       continue;
9227                     }
9228
9229                   break;
9230                 }
9231
9232               /* We need to relax this instruction.  */
9233               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9234               s = expr_end;
9235               continue;
9236
9237             case 'p':
9238             case 'q':
9239             case 'A':
9240             case 'B':
9241             case 'E':
9242               /* We use offset_reloc rather than imm_reloc for the PC
9243                  relative operands.  This lets macros with both
9244                  immediate and address operands work correctly.  */
9245               my_getExpression (&offset_expr, s);
9246
9247               if (offset_expr.X_op == O_register)
9248                 break;
9249
9250               /* We need to relax this instruction.  */
9251               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9252               s = expr_end;
9253               continue;
9254
9255             case '6':           /* break code */
9256               my_getExpression (&imm_expr, s);
9257               check_absolute_expr (ip, &imm_expr);
9258               if ((unsigned long) imm_expr.X_add_number > 63)
9259                 {
9260                   as_warn (_("Invalid value for `%s' (%lu)"),
9261                            ip->insn_mo->name,
9262                            (unsigned long) imm_expr.X_add_number);
9263                   imm_expr.X_add_number &= 0x3f;
9264                 }
9265               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9266               imm_expr.X_op = O_absent;
9267               s = expr_end;
9268               continue;
9269
9270             case 'a':           /* 26 bit address */
9271               my_getExpression (&offset_expr, s);
9272               s = expr_end;
9273               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9274               ip->insn_opcode <<= 16;
9275               continue;
9276
9277             case 'l':           /* register list for entry macro */
9278             case 'L':           /* register list for exit macro */
9279               {
9280                 int mask;
9281
9282                 if (c == 'l')
9283                   mask = 0;
9284                 else
9285                   mask = 7 << 3;
9286                 while (*s != '\0')
9287                   {
9288                     int freg, reg1, reg2;
9289
9290                     while (*s == ' ' || *s == ',')
9291                       ++s;
9292                     if (*s != '$')
9293                       {
9294                         as_bad (_("can't parse register list"));
9295                         break;
9296                       }
9297                     ++s;
9298                     if (*s != 'f')
9299                       freg = 0;
9300                     else
9301                       {
9302                         freg = 1;
9303                         ++s;
9304                       }
9305                     reg1 = 0;
9306                     while (ISDIGIT (*s))
9307                       {
9308                         reg1 *= 10;
9309                         reg1 += *s - '0';
9310                         ++s;
9311                       }
9312                     if (*s == ' ')
9313                       ++s;
9314                     if (*s != '-')
9315                       reg2 = reg1;
9316                     else
9317                       {
9318                         ++s;
9319                         if (*s != '$')
9320                           break;
9321                         ++s;
9322                         if (freg)
9323                           {
9324                             if (*s == 'f')
9325                               ++s;
9326                             else
9327                               {
9328                                 as_bad (_("invalid register list"));
9329                                 break;
9330                               }
9331                           }
9332                         reg2 = 0;
9333                         while (ISDIGIT (*s))
9334                           {
9335                             reg2 *= 10;
9336                             reg2 += *s - '0';
9337                             ++s;
9338                           }
9339                       }
9340                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9341                       {
9342                         mask &= ~ (7 << 3);
9343                         mask |= 5 << 3;
9344                       }
9345                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9346                       {
9347                         mask &= ~ (7 << 3);
9348                         mask |= 6 << 3;
9349                       }
9350                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9351                       mask |= (reg2 - 3) << 3;
9352                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9353                       mask |= (reg2 - 15) << 1;
9354                     else if (reg1 == RA && reg2 == RA)
9355                       mask |= 1;
9356                     else
9357                       {
9358                         as_bad (_("invalid register list"));
9359                         break;
9360                       }
9361                   }
9362                 /* The mask is filled in in the opcode table for the
9363                    benefit of the disassembler.  We remove it before
9364                    applying the actual mask.  */
9365                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9366                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9367               }
9368             continue;
9369
9370             case 'e':           /* extend code */
9371               my_getExpression (&imm_expr, s);
9372               check_absolute_expr (ip, &imm_expr);
9373               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9374                 {
9375                   as_warn (_("Invalid value for `%s' (%lu)"),
9376                            ip->insn_mo->name,
9377                            (unsigned long) imm_expr.X_add_number);
9378                   imm_expr.X_add_number &= 0x7ff;
9379                 }
9380               ip->insn_opcode |= imm_expr.X_add_number;
9381               imm_expr.X_op = O_absent;
9382               s = expr_end;
9383               continue;
9384
9385             default:
9386               internalError ();
9387             }
9388           break;
9389         }
9390
9391       /* Args don't match.  */
9392       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9393           strcmp (insn->name, insn[1].name) == 0)
9394         {
9395           ++insn;
9396           s = argsstart;
9397           continue;
9398         }
9399
9400       insn_error = _("illegal operands");
9401
9402       return;
9403     }
9404 }
9405
9406 /* This structure holds information we know about a mips16 immediate
9407    argument type.  */
9408
9409 struct mips16_immed_operand
9410 {
9411   /* The type code used in the argument string in the opcode table.  */
9412   int type;
9413   /* The number of bits in the short form of the opcode.  */
9414   int nbits;
9415   /* The number of bits in the extended form of the opcode.  */
9416   int extbits;
9417   /* The amount by which the short form is shifted when it is used;
9418      for example, the sw instruction has a shift count of 2.  */
9419   int shift;
9420   /* The amount by which the short form is shifted when it is stored
9421      into the instruction code.  */
9422   int op_shift;
9423   /* Non-zero if the short form is unsigned.  */
9424   int unsp;
9425   /* Non-zero if the extended form is unsigned.  */
9426   int extu;
9427   /* Non-zero if the value is PC relative.  */
9428   int pcrel;
9429 };
9430
9431 /* The mips16 immediate operand types.  */
9432
9433 static const struct mips16_immed_operand mips16_immed_operands[] =
9434 {
9435   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9436   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9437   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9438   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9439   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9440   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9441   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9442   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9443   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9444   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9445   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9446   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9447   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9448   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9449   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9450   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9451   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9452   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9453   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9454   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9455   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9456 };
9457
9458 #define MIPS16_NUM_IMMED \
9459   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9460
9461 /* Handle a mips16 instruction with an immediate value.  This or's the
9462    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9463    whether an extended value is needed; if one is needed, it sets
9464    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9465    If SMALL is true, an unextended opcode was explicitly requested.
9466    If EXT is true, an extended opcode was explicitly requested.  If
9467    WARN is true, warn if EXT does not match reality.  */
9468
9469 static void
9470 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9471               extend)
9472      char *file;
9473      unsigned int line;
9474      int type;
9475      offsetT val;
9476      boolean warn;
9477      boolean small;
9478      boolean ext;
9479      unsigned long *insn;
9480      boolean *use_extend;
9481      unsigned short *extend;
9482 {
9483   register const struct mips16_immed_operand *op;
9484   int mintiny, maxtiny;
9485   boolean needext;
9486
9487   op = mips16_immed_operands;
9488   while (op->type != type)
9489     {
9490       ++op;
9491       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9492     }
9493
9494   if (op->unsp)
9495     {
9496       if (type == '<' || type == '>' || type == '[' || type == ']')
9497         {
9498           mintiny = 1;
9499           maxtiny = 1 << op->nbits;
9500         }
9501       else
9502         {
9503           mintiny = 0;
9504           maxtiny = (1 << op->nbits) - 1;
9505         }
9506     }
9507   else
9508     {
9509       mintiny = - (1 << (op->nbits - 1));
9510       maxtiny = (1 << (op->nbits - 1)) - 1;
9511     }
9512
9513   /* Branch offsets have an implicit 0 in the lowest bit.  */
9514   if (type == 'p' || type == 'q')
9515     val /= 2;
9516
9517   if ((val & ((1 << op->shift) - 1)) != 0
9518       || val < (mintiny << op->shift)
9519       || val > (maxtiny << op->shift))
9520     needext = true;
9521   else
9522     needext = false;
9523
9524   if (warn && ext && ! needext)
9525     as_warn_where (file, line,
9526                    _("extended operand requested but not required"));
9527   if (small && needext)
9528     as_bad_where (file, line, _("invalid unextended operand value"));
9529
9530   if (small || (! ext && ! needext))
9531     {
9532       int insnval;
9533
9534       *use_extend = false;
9535       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9536       insnval <<= op->op_shift;
9537       *insn |= insnval;
9538     }
9539   else
9540     {
9541       long minext, maxext;
9542       int extval;
9543
9544       if (op->extu)
9545         {
9546           minext = 0;
9547           maxext = (1 << op->extbits) - 1;
9548         }
9549       else
9550         {
9551           minext = - (1 << (op->extbits - 1));
9552           maxext = (1 << (op->extbits - 1)) - 1;
9553         }
9554       if (val < minext || val > maxext)
9555         as_bad_where (file, line,
9556                       _("operand value out of range for instruction"));
9557
9558       *use_extend = true;
9559       if (op->extbits == 16)
9560         {
9561           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9562           val &= 0x1f;
9563         }
9564       else if (op->extbits == 15)
9565         {
9566           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9567           val &= 0xf;
9568         }
9569       else
9570         {
9571           extval = ((val & 0x1f) << 6) | (val & 0x20);
9572           val = 0;
9573         }
9574
9575       *extend = (unsigned short) extval;
9576       *insn |= val;
9577     }
9578 }
9579 \f
9580 static struct percent_op_match
9581 {
9582    const char *str;
9583    const enum small_ex_type type;
9584 } percent_op[] =
9585 {
9586   {"%lo", S_EX_LO},
9587 #ifdef OBJ_ELF
9588   {"%call_hi", S_EX_CALL_HI},
9589   {"%call_lo", S_EX_CALL_LO},
9590   {"%call16", S_EX_CALL16},
9591   {"%got_disp", S_EX_GOT_DISP},
9592   {"%got_page", S_EX_GOT_PAGE},
9593   {"%got_ofst", S_EX_GOT_OFST},
9594   {"%got_hi", S_EX_GOT_HI},
9595   {"%got_lo", S_EX_GOT_LO},
9596   {"%got", S_EX_GOT},
9597   {"%gp_rel", S_EX_GP_REL},
9598   {"%half", S_EX_HALF},
9599   {"%highest", S_EX_HIGHEST},
9600   {"%higher", S_EX_HIGHER},
9601   {"%neg", S_EX_NEG},
9602 #endif
9603   {"%hi", S_EX_HI}
9604 };
9605
9606 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9607    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9608    can be nested, this is handled by blanking the innermost, parsing the
9609    rest by subsequent calls.  */
9610
9611 static int
9612 my_getSmallParser (str, len, nestlevel)
9613      char **str;
9614      unsigned int *len;
9615      int *nestlevel;
9616 {
9617   *len = 0;
9618   *str += strspn (*str, " \t");
9619   /* Check for expression in parentheses.  */
9620   if (**str == '(')
9621     {
9622       char *b = *str + 1 + strspn (*str + 1, " \t");
9623       char *e;
9624
9625       /* Check for base register.  */
9626       if (b[0] == '$')
9627         {
9628           if (strchr (b, ')')
9629               && (e = b + strcspn (b, ") \t"))
9630               && e - b > 1 && e - b < 4)
9631             {
9632               if ((e - b == 3
9633                    && ((b[1] == 'f' && b[2] == 'p')
9634                        || (b[1] == 's' && b[2] == 'p')
9635                        || (b[1] == 'g' && b[2] == 'p')
9636                        || (b[1] == 'a' && b[2] == 't')
9637                        || (ISDIGIT (b[1])
9638                            && ISDIGIT (b[2]))))
9639                   || (ISDIGIT (b[1])))
9640                 {
9641                   *len = strcspn (*str, ")") + 1;
9642                   return S_EX_REGISTER;
9643                 }
9644             }
9645         }
9646       /* Check for percent_op (in parentheses).  */
9647       else if (b[0] == '%')
9648         {
9649           *str = b;
9650           return my_getPercentOp (str, len, nestlevel);
9651         }
9652
9653       /* Some other expression in the parentheses, which can contain
9654          parentheses itself. Attempt to find the matching one.  */
9655       {
9656         int pcnt = 1;
9657         char *s;
9658
9659         *len = 1;
9660         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9661           {
9662             if (*s == '(')
9663               ++pcnt;
9664             else if (*s == ')')
9665               --pcnt;
9666           }
9667       }
9668     }
9669   /* Check for percent_op (outside of parentheses).  */
9670   else if (*str[0] == '%')
9671     return my_getPercentOp (str, len, nestlevel);
9672
9673   /* Any other expression.  */
9674   return S_EX_NONE;
9675 }
9676
9677 static int
9678 my_getPercentOp (str, len, nestlevel)
9679      char **str;
9680      unsigned int *len;
9681      int *nestlevel;
9682 {
9683   char *tmp = *str + 1;
9684   unsigned int i = 0;
9685
9686   while (ISALPHA (*tmp) || *tmp == '_')
9687     {
9688       *tmp = TOLOWER (*tmp);
9689       tmp++;
9690     }
9691   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9692     {
9693       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9694         i++;
9695       else
9696         {
9697           int type = percent_op[i].type;
9698
9699           /* Only %hi and %lo are allowed for OldABI.  */
9700           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9701             return S_EX_NONE;
9702
9703           *len = strlen (percent_op[i].str);
9704           ++(*nestlevel);
9705           return type;
9706         }
9707     }
9708   return S_EX_NONE;
9709 }
9710
9711 static int
9712 my_getSmallExpression (ep, str)
9713      expressionS *ep;
9714      char *str;
9715 {
9716   static char *oldstr = NULL;
9717   int c = S_EX_NONE;
9718   int oldc;
9719   int nestlevel = -1;
9720   unsigned int len;
9721
9722   /* Don't update oldstr if the last call had nested percent_op's. We need
9723      it to parse the outer ones later.  */
9724   if (! oldstr)
9725     oldstr = str;
9726
9727   do
9728     {
9729       oldc = c;
9730       c = my_getSmallParser (&str, &len, &nestlevel);
9731       if (c != S_EX_NONE && c != S_EX_REGISTER)
9732         str += len;
9733     }
9734   while (c != S_EX_NONE && c != S_EX_REGISTER);
9735
9736   if (nestlevel >= 0)
9737     {
9738       /* A percent_op was encountered.  Don't try to get an expression if
9739          it is already blanked out.  */
9740       if (*(str + strspn (str + 1, " )")) != ')')
9741         {
9742           char save;
9743
9744           /* Let my_getExpression() stop at the closing parenthesis.  */
9745           save = *(str + len);
9746           *(str + len) = '\0';
9747           my_getExpression (ep, str);
9748           *(str + len) = save;
9749         }
9750       if (nestlevel > 0)
9751         {
9752           /* Blank out including the % sign and the proper matching
9753              parenthesis.  */
9754           int pcnt = 1;
9755           char *s = strrchr (oldstr, '%');
9756           char *end;
9757
9758           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9759             {
9760               if (*end == '(')
9761                 ++pcnt;
9762               else if (*end == ')')
9763                 --pcnt;
9764             }
9765
9766           memset (s, ' ', end - s);
9767           str = oldstr;
9768         }
9769       else
9770         expr_end = str + len;
9771
9772       c = oldc;
9773     }
9774   else if (c == S_EX_NONE)
9775     {
9776       my_getExpression (ep, str);
9777     }
9778   else if (c == S_EX_REGISTER)
9779     {
9780       ep->X_op = O_constant;
9781       expr_end = str;
9782       ep->X_add_symbol = NULL;
9783       ep->X_op_symbol = NULL;
9784       ep->X_add_number = 0;
9785     }
9786   else
9787     {
9788       as_fatal (_("internal error"));
9789     }
9790
9791   if (nestlevel <= 0)
9792     /* All percent_op's have been handled.  */
9793     oldstr = NULL;
9794
9795   return c;
9796 }
9797
9798 static void
9799 my_getExpression (ep, str)
9800      expressionS *ep;
9801      char *str;
9802 {
9803   char *save_in;
9804   valueT val;
9805
9806   save_in = input_line_pointer;
9807   input_line_pointer = str;
9808   expression (ep);
9809   expr_end = input_line_pointer;
9810   input_line_pointer = save_in;
9811
9812   /* If we are in mips16 mode, and this is an expression based on `.',
9813      then we bump the value of the symbol by 1 since that is how other
9814      text symbols are handled.  We don't bother to handle complex
9815      expressions, just `.' plus or minus a constant.  */
9816   if (mips_opts.mips16
9817       && ep->X_op == O_symbol
9818       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9819       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9820       && symbol_get_frag (ep->X_add_symbol) == frag_now
9821       && symbol_constant_p (ep->X_add_symbol)
9822       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9823     S_SET_VALUE (ep->X_add_symbol, val + 1);
9824 }
9825
9826 /* Turn a string in input_line_pointer into a floating point constant
9827    of type TYPE, and store the appropriate bytes in *LITP.  The number
9828    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
9829    returned, or NULL on OK.  */
9830
9831 char *
9832 md_atof (type, litP, sizeP)
9833      int type;
9834      char *litP;
9835      int *sizeP;
9836 {
9837   int prec;
9838   LITTLENUM_TYPE words[4];
9839   char *t;
9840   int i;
9841
9842   switch (type)
9843     {
9844     case 'f':
9845       prec = 2;
9846       break;
9847
9848     case 'd':
9849       prec = 4;
9850       break;
9851
9852     default:
9853       *sizeP = 0;
9854       return _("bad call to md_atof");
9855     }
9856
9857   t = atof_ieee (input_line_pointer, type, words);
9858   if (t)
9859     input_line_pointer = t;
9860
9861   *sizeP = prec * 2;
9862
9863   if (! target_big_endian)
9864     {
9865       for (i = prec - 1; i >= 0; i--)
9866         {
9867           md_number_to_chars (litP, (valueT) words[i], 2);
9868           litP += 2;
9869         }
9870     }
9871   else
9872     {
9873       for (i = 0; i < prec; i++)
9874         {
9875           md_number_to_chars (litP, (valueT) words[i], 2);
9876           litP += 2;
9877         }
9878     }
9879
9880   return NULL;
9881 }
9882
9883 void
9884 md_number_to_chars (buf, val, n)
9885      char *buf;
9886      valueT val;
9887      int n;
9888 {
9889   if (target_big_endian)
9890     number_to_chars_bigendian (buf, val, n);
9891   else
9892     number_to_chars_littleendian (buf, val, n);
9893 }
9894 \f
9895 #ifdef OBJ_ELF
9896 static int support_64bit_objects(void)
9897 {
9898   const char **list, **l;
9899
9900   list = bfd_target_list ();
9901   for (l = list; *l != NULL; l++)
9902 #ifdef TE_TMIPS
9903     /* This is traditional mips */
9904     if (strcmp (*l, "elf64-tradbigmips") == 0
9905         || strcmp (*l, "elf64-tradlittlemips") == 0)
9906 #else
9907     if (strcmp (*l, "elf64-bigmips") == 0
9908         || strcmp (*l, "elf64-littlemips") == 0)
9909 #endif
9910       break;
9911   free (list);
9912   return (*l != NULL);
9913 }
9914 #endif /* OBJ_ELF */
9915
9916 const char *md_shortopts = "nO::g::G:";
9917
9918 struct option md_longopts[] =
9919 {
9920 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9921   {"mips0", no_argument, NULL, OPTION_MIPS1},
9922   {"mips1", no_argument, NULL, OPTION_MIPS1},
9923 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9924   {"mips2", no_argument, NULL, OPTION_MIPS2},
9925 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9926   {"mips3", no_argument, NULL, OPTION_MIPS3},
9927 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9928   {"mips4", no_argument, NULL, OPTION_MIPS4},
9929 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9930   {"mips5", no_argument, NULL, OPTION_MIPS5},
9931 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9932   {"mips32", no_argument, NULL, OPTION_MIPS32},
9933 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9934   {"mips64", no_argument, NULL, OPTION_MIPS64},
9935 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9936   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9937 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9938   {"trap", no_argument, NULL, OPTION_TRAP},
9939   {"no-break", no_argument, NULL, OPTION_TRAP},
9940 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9941   {"break", no_argument, NULL, OPTION_BREAK},
9942   {"no-trap", no_argument, NULL, OPTION_BREAK},
9943 #define OPTION_EB (OPTION_MD_BASE + 11)
9944   {"EB", no_argument, NULL, OPTION_EB},
9945 #define OPTION_EL (OPTION_MD_BASE + 12)
9946   {"EL", no_argument, NULL, OPTION_EL},
9947 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9948   {"mips16", no_argument, NULL, OPTION_MIPS16},
9949 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9950   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9951 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9952   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9953 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
9954   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9955   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9956 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9957   {"mfp32", no_argument, NULL, OPTION_FP32},
9958 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9959   {"mgp32", no_argument, NULL, OPTION_GP32},
9960 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9961   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9962 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9963   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9964 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9965   {"march", required_argument, NULL, OPTION_MARCH},
9966 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9967   {"mtune", required_argument, NULL, OPTION_MTUNE},
9968 #define OPTION_FP64 (OPTION_MD_BASE + 23)
9969   {"mfp64", no_argument, NULL, OPTION_FP64},
9970 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9971   {"m4650", no_argument, NULL, OPTION_M4650},
9972 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9973   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9974 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9975   {"m4010", no_argument, NULL, OPTION_M4010},
9976 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9977   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9978 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9979   {"m4100", no_argument, NULL, OPTION_M4100},
9980 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9981   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9982 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9983   {"m3900", no_argument, NULL, OPTION_M3900},
9984 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9985   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9986 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9987   {"mgp64", no_argument, NULL, OPTION_GP64},
9988 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9989   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9990 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9991   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
9992 #define OPTION_MDMX (OPTION_MD_BASE + 35)
9993   {"mdmx", no_argument, NULL, OPTION_MDMX},
9994 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
9995   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
9996 #ifdef OBJ_ELF
9997 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 37)
9998 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9999   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10000   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10001 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10002   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10003 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10004   {"xgot",        no_argument, NULL, OPTION_XGOT},
10005 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10006   {"mabi", required_argument, NULL, OPTION_MABI},
10007 #define OPTION_32          (OPTION_ELF_BASE + 4)
10008   {"32",          no_argument, NULL, OPTION_32},
10009 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10010   {"n32",         no_argument, NULL, OPTION_N32},
10011 #define OPTION_64          (OPTION_ELF_BASE + 6)
10012   {"64",          no_argument, NULL, OPTION_64},
10013 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10014   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10015 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10016   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10017 #endif /* OBJ_ELF */
10018   {NULL, no_argument, NULL, 0}
10019 };
10020 size_t md_longopts_size = sizeof (md_longopts);
10021
10022 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10023    NEW_VALUE.  Warn if another value was already specified.  Note:
10024    we have to defer parsing the -march and -mtune arguments in order
10025    to handle 'from-abi' correctly, since the ABI might be specified
10026    in a later argument.  */
10027
10028 static void
10029 mips_set_option_string (string_ptr, new_value)
10030      const char **string_ptr, *new_value;
10031 {
10032   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10033     as_warn (_("A different %s was already specified, is now %s"),
10034              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10035              new_value);
10036
10037   *string_ptr = new_value;
10038 }
10039
10040 int
10041 md_parse_option (c, arg)
10042      int c;
10043      char *arg;
10044 {
10045   switch (c)
10046     {
10047     case OPTION_CONSTRUCT_FLOATS:
10048       mips_disable_float_construction = 0;
10049       break;
10050
10051     case OPTION_NO_CONSTRUCT_FLOATS:
10052       mips_disable_float_construction = 1;
10053       break;
10054
10055     case OPTION_TRAP:
10056       mips_trap = 1;
10057       break;
10058
10059     case OPTION_BREAK:
10060       mips_trap = 0;
10061       break;
10062
10063     case OPTION_EB:
10064       target_big_endian = 1;
10065       break;
10066
10067     case OPTION_EL:
10068       target_big_endian = 0;
10069       break;
10070
10071     case 'n':
10072       warn_nops = 1;
10073       break;
10074
10075     case 'O':
10076       if (arg && arg[1] == '0')
10077         mips_optimize = 1;
10078       else
10079         mips_optimize = 2;
10080       break;
10081
10082     case 'g':
10083       if (arg == NULL)
10084         mips_debug = 2;
10085       else
10086         mips_debug = atoi (arg);
10087       /* When the MIPS assembler sees -g or -g2, it does not do
10088          optimizations which limit full symbolic debugging.  We take
10089          that to be equivalent to -O0.  */
10090       if (mips_debug == 2)
10091         mips_optimize = 1;
10092       break;
10093
10094     case OPTION_MIPS1:
10095       file_mips_isa = ISA_MIPS1;
10096       break;
10097
10098     case OPTION_MIPS2:
10099       file_mips_isa = ISA_MIPS2;
10100       break;
10101
10102     case OPTION_MIPS3:
10103       file_mips_isa = ISA_MIPS3;
10104       break;
10105
10106     case OPTION_MIPS4:
10107       file_mips_isa = ISA_MIPS4;
10108       break;
10109
10110     case OPTION_MIPS5:
10111       file_mips_isa = ISA_MIPS5;
10112       break;
10113
10114     case OPTION_MIPS32:
10115       file_mips_isa = ISA_MIPS32;
10116       break;
10117
10118     case OPTION_MIPS64:
10119       file_mips_isa = ISA_MIPS64;
10120       break;
10121
10122     case OPTION_MTUNE:
10123       mips_set_option_string (&mips_tune_string, arg);
10124       break;
10125
10126     case OPTION_MARCH:
10127       mips_set_option_string (&mips_arch_string, arg);
10128       break;
10129
10130     case OPTION_M4650:
10131       mips_set_option_string (&mips_arch_string, "4650");
10132       mips_set_option_string (&mips_tune_string, "4650");
10133       break;
10134
10135     case OPTION_NO_M4650:
10136       break;
10137
10138     case OPTION_M4010:
10139       mips_set_option_string (&mips_arch_string, "4010");
10140       mips_set_option_string (&mips_tune_string, "4010");
10141       break;
10142
10143     case OPTION_NO_M4010:
10144       break;
10145
10146     case OPTION_M4100:
10147       mips_set_option_string (&mips_arch_string, "4100");
10148       mips_set_option_string (&mips_tune_string, "4100");
10149       break;
10150
10151     case OPTION_NO_M4100:
10152       break;
10153
10154     case OPTION_M3900:
10155       mips_set_option_string (&mips_arch_string, "3900");
10156       mips_set_option_string (&mips_tune_string, "3900");
10157       break;
10158
10159     case OPTION_NO_M3900:
10160       break;
10161
10162     case OPTION_MDMX:
10163       mips_opts.ase_mdmx = 1;
10164       break;
10165
10166     case OPTION_NO_MDMX:
10167       mips_opts.ase_mdmx = 0;
10168       break;
10169
10170     case OPTION_MIPS16:
10171       mips_opts.mips16 = 1;
10172       mips_no_prev_insn (false);
10173       break;
10174
10175     case OPTION_NO_MIPS16:
10176       mips_opts.mips16 = 0;
10177       mips_no_prev_insn (false);
10178       break;
10179
10180     case OPTION_MIPS3D:
10181       mips_opts.ase_mips3d = 1;
10182       break;
10183
10184     case OPTION_NO_MIPS3D:
10185       mips_opts.ase_mips3d = 0;
10186       break;
10187
10188     case OPTION_MEMBEDDED_PIC:
10189       mips_pic = EMBEDDED_PIC;
10190       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10191         {
10192           as_bad (_("-G may not be used with embedded PIC code"));
10193           return 0;
10194         }
10195       g_switch_value = 0x7fffffff;
10196       break;
10197
10198 #ifdef OBJ_ELF
10199       /* When generating ELF code, we permit -KPIC and -call_shared to
10200          select SVR4_PIC, and -non_shared to select no PIC.  This is
10201          intended to be compatible with Irix 5.  */
10202     case OPTION_CALL_SHARED:
10203       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10204         {
10205           as_bad (_("-call_shared is supported only for ELF format"));
10206           return 0;
10207         }
10208       mips_pic = SVR4_PIC;
10209       if (g_switch_seen && g_switch_value != 0)
10210         {
10211           as_bad (_("-G may not be used with SVR4 PIC code"));
10212           return 0;
10213         }
10214       g_switch_value = 0;
10215       break;
10216
10217     case OPTION_NON_SHARED:
10218       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10219         {
10220           as_bad (_("-non_shared is supported only for ELF format"));
10221           return 0;
10222         }
10223       mips_pic = NO_PIC;
10224       break;
10225
10226       /* The -xgot option tells the assembler to use 32 offsets when
10227          accessing the got in SVR4_PIC mode.  It is for Irix
10228          compatibility.  */
10229     case OPTION_XGOT:
10230       mips_big_got = 1;
10231       break;
10232 #endif /* OBJ_ELF */
10233
10234     case 'G':
10235       if (! USE_GLOBAL_POINTER_OPT)
10236         {
10237           as_bad (_("-G is not supported for this configuration"));
10238           return 0;
10239         }
10240       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10241         {
10242           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10243           return 0;
10244         }
10245       else
10246         g_switch_value = atoi (arg);
10247       g_switch_seen = 1;
10248       break;
10249
10250 #ifdef OBJ_ELF
10251       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10252          and -mabi=64.  */
10253     case OPTION_32:
10254       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10255         {
10256           as_bad (_("-32 is supported for ELF format only"));
10257           return 0;
10258         }
10259       mips_abi = O32_ABI;
10260       break;
10261
10262     case OPTION_N32:
10263       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10264         {
10265           as_bad (_("-n32 is supported for ELF format only"));
10266           return 0;
10267         }
10268       mips_abi = N32_ABI;
10269       break;
10270
10271     case OPTION_64:
10272       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10273         {
10274           as_bad (_("-64 is supported for ELF format only"));
10275           return 0;
10276         }
10277       mips_abi = N64_ABI;
10278       if (! support_64bit_objects())
10279         as_fatal (_("No compiled in support for 64 bit object file format"));
10280       break;
10281 #endif /* OBJ_ELF */
10282
10283     case OPTION_GP32:
10284       file_mips_gp32 = 1;
10285       break;
10286
10287     case OPTION_GP64:
10288       file_mips_gp32 = 0;
10289       break;
10290
10291     case OPTION_FP32:
10292       file_mips_fp32 = 1;
10293       break;
10294
10295     case OPTION_FP64:
10296       file_mips_fp32 = 0;
10297       break;
10298
10299 #ifdef OBJ_ELF
10300     case OPTION_MABI:
10301       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10302         {
10303           as_bad (_("-mabi is supported for ELF format only"));
10304           return 0;
10305         }
10306       if (strcmp (arg, "32") == 0)
10307         mips_abi = O32_ABI;
10308       else if (strcmp (arg, "o64") == 0)
10309         mips_abi = O64_ABI;
10310       else if (strcmp (arg, "n32") == 0)
10311         mips_abi = N32_ABI;
10312       else if (strcmp (arg, "64") == 0)
10313         {
10314           mips_abi = N64_ABI;
10315           if (! support_64bit_objects())
10316             as_fatal (_("No compiled in support for 64 bit object file "
10317                         "format"));
10318         }
10319       else if (strcmp (arg, "eabi") == 0)
10320         mips_abi = EABI_ABI;
10321       else
10322         {
10323           as_fatal (_("invalid abi -mabi=%s"), arg);
10324           return 0;
10325         }
10326       break;
10327 #endif /* OBJ_ELF */
10328
10329     case OPTION_M7000_HILO_FIX:
10330       mips_7000_hilo_fix = true;
10331       break;
10332
10333     case OPTION_MNO_7000_HILO_FIX:
10334       mips_7000_hilo_fix = false;
10335       break;
10336
10337 #ifdef OBJ_ELF
10338     case OPTION_MDEBUG:
10339       mips_flag_mdebug = true;
10340       break;
10341
10342     case OPTION_NO_MDEBUG:
10343       mips_flag_mdebug = false;
10344       break;
10345 #endif /* OBJ_ELF */
10346
10347     default:
10348       return 0;
10349     }
10350
10351   return 1;
10352 }
10353 \f
10354 /* Set up globals to generate code for the ISA or processor
10355    described by INFO.  */
10356
10357 static void
10358 mips_set_architecture (info)
10359      const struct mips_cpu_info *info;
10360 {
10361   if (info != 0)
10362     {
10363       mips_arch_info = info;
10364       mips_arch = info->cpu;
10365       mips_opts.isa = info->isa;
10366     }
10367 }
10368
10369
10370 /* Likewise for tuning.  */
10371
10372 static void
10373 mips_set_tune (info)
10374      const struct mips_cpu_info *info;
10375 {
10376   if (info != 0)
10377     {
10378       mips_tune_info = info;
10379       mips_tune = info->cpu;
10380     }
10381 }
10382
10383
10384 void
10385 mips_after_parse_args ()
10386 {
10387   /* GP relative stuff not working for PE */
10388   if (strncmp (TARGET_OS, "pe", 2) == 0
10389       && g_switch_value != 0)
10390     {
10391       if (g_switch_seen)
10392         as_bad (_("-G not supported in this configuration."));
10393       g_switch_value = 0;
10394     }
10395
10396   /* The following code determines the architecture and register size.
10397      Similar code was added to GCC 3.3 (see override_options() in
10398      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10399      as much as possible.  */
10400
10401   if (mips_arch_string != 0)
10402     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10403
10404   if (mips_tune_string != 0)
10405     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10406
10407   if (file_mips_isa != ISA_UNKNOWN)
10408     {
10409       /* Handle -mipsN.  At this point, file_mips_isa contains the
10410          ISA level specified by -mipsN, while mips_opts.isa contains
10411          the -march selection (if any).  */
10412       if (mips_arch_info != 0)
10413         {
10414           /* -march takes precedence over -mipsN, since it is more descriptive.
10415              There's no harm in specifying both as long as the ISA levels
10416              are the same.  */
10417           if (file_mips_isa != mips_opts.isa)
10418             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10419                     mips_cpu_info_from_isa (file_mips_isa)->name,
10420                     mips_cpu_info_from_isa (mips_opts.isa)->name);
10421         }
10422       else
10423         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10424     }
10425
10426   if (mips_arch_info == 0)
10427     mips_set_architecture (mips_parse_cpu ("default CPU",
10428                                            MIPS_CPU_STRING_DEFAULT));
10429
10430   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10431     as_bad ("-march=%s is not compatible with the selected ABI",
10432             mips_arch_info->name);
10433
10434   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
10435   if (mips_tune_info == 0)
10436     mips_set_tune (mips_arch_info);
10437
10438   if (file_mips_gp32 >= 0)
10439     {
10440       /* The user specified the size of the integer registers.  Make sure
10441          it agrees with the ABI and ISA.  */
10442       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10443         as_bad (_("-mgp64 used with a 32-bit processor"));
10444       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10445         as_bad (_("-mgp32 used with a 64-bit ABI"));
10446       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10447         as_bad (_("-mgp64 used with a 32-bit ABI"));
10448     }
10449   else
10450     {
10451       /* Infer the integer register size from the ABI and processor.
10452          Restrict ourselves to 32-bit registers if that's all the
10453          processor has, or if the ABI cannot handle 64-bit registers.  */
10454       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10455                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10456     }
10457
10458   /* ??? GAS treats single-float processors as though they had 64-bit
10459      float registers (although it complains when double-precision
10460      instructions are used).  As things stand, saying they have 32-bit
10461      registers would lead to spurious "register must be even" messages.
10462      So here we assume float registers are always the same size as
10463      integer ones, unless the user says otherwise.  */
10464   if (file_mips_fp32 < 0)
10465     file_mips_fp32 = file_mips_gp32;
10466
10467   /* End of GCC-shared inference code.  */
10468
10469   /* ??? When do we want this flag to be set?   Who uses it?  */
10470   if (file_mips_gp32 == 1
10471       && mips_abi == NO_ABI
10472       && ISA_HAS_64BIT_REGS (mips_opts.isa))
10473     mips_32bitmode = 1;
10474
10475   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10476     as_bad (_("trap exception not supported at ISA 1"));
10477
10478   /* If the selected architecture includes support for ASEs, enable
10479      generation of code for them.  */
10480   if (mips_opts.mips16 == -1)
10481     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10482   if (mips_opts.ase_mips3d == -1)
10483     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10484   if (mips_opts.ase_mdmx == -1)
10485     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10486
10487   file_mips_isa = mips_opts.isa;
10488   file_ase_mips16 = mips_opts.mips16;
10489   file_ase_mips3d = mips_opts.ase_mips3d;
10490   file_ase_mdmx = mips_opts.ase_mdmx;
10491   mips_opts.gp32 = file_mips_gp32;
10492   mips_opts.fp32 = file_mips_fp32;
10493
10494   if (mips_flag_mdebug < 0)
10495     {
10496 #ifdef OBJ_MAYBE_ECOFF
10497       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10498         mips_flag_mdebug = 1;
10499       else
10500 #endif /* OBJ_MAYBE_ECOFF */
10501         mips_flag_mdebug = 0;
10502     }
10503 }
10504 \f
10505 void
10506 mips_init_after_args ()
10507 {
10508   /* initialize opcodes */
10509   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10510   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10511 }
10512
10513 long
10514 md_pcrel_from (fixP)
10515      fixS *fixP;
10516 {
10517   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10518       && fixP->fx_addsy != (symbolS *) NULL
10519       && ! S_IS_DEFINED (fixP->fx_addsy))
10520     {
10521       /* This makes a branch to an undefined symbol be a branch to the
10522          current location.  */
10523       if (mips_pic == EMBEDDED_PIC)
10524         return 4;
10525       else
10526         return 1;
10527     }
10528
10529   /* Return the address of the delay slot.  */
10530   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10531 }
10532
10533 /* This is called before the symbol table is processed.  In order to
10534    work with gcc when using mips-tfile, we must keep all local labels.
10535    However, in other cases, we want to discard them.  If we were
10536    called with -g, but we didn't see any debugging information, it may
10537    mean that gcc is smuggling debugging information through to
10538    mips-tfile, in which case we must generate all local labels.  */
10539
10540 void
10541 mips_frob_file_before_adjust ()
10542 {
10543 #ifndef NO_ECOFF_DEBUGGING
10544   if (ECOFF_DEBUGGING
10545       && mips_debug != 0
10546       && ! ecoff_debugging_seen)
10547     flag_keep_locals = 1;
10548 #endif
10549 }
10550
10551 /* Sort any unmatched HI16_S relocs so that they immediately precede
10552    the corresponding LO reloc.  This is called before md_apply_fix3 and
10553    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10554    explicit use of the %hi modifier.  */
10555
10556 void
10557 mips_frob_file ()
10558 {
10559   struct mips_hi_fixup *l;
10560
10561   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10562     {
10563       segment_info_type *seginfo;
10564       int pass;
10565
10566       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10567
10568       /* Check quickly whether the next fixup happens to be a matching
10569          %lo.  */
10570       if (l->fixp->fx_next != NULL
10571           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10572           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10573           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10574         continue;
10575
10576       /* Look through the fixups for this segment for a matching %lo.
10577          When we find one, move the %hi just in front of it.  We do
10578          this in two passes.  In the first pass, we try to find a
10579          unique %lo.  In the second pass, we permit multiple %hi
10580          relocs for a single %lo (this is a GNU extension).  */
10581       seginfo = seg_info (l->seg);
10582       for (pass = 0; pass < 2; pass++)
10583         {
10584           fixS *f, *prev;
10585
10586           prev = NULL;
10587           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10588             {
10589               /* Check whether this is a %lo fixup which matches l->fixp.  */
10590               if (f->fx_r_type == BFD_RELOC_LO16
10591                   && f->fx_addsy == l->fixp->fx_addsy
10592                   && f->fx_offset == l->fixp->fx_offset
10593                   && (pass == 1
10594                       || prev == NULL
10595                       || prev->fx_r_type != BFD_RELOC_HI16_S
10596                       || prev->fx_addsy != f->fx_addsy
10597                       || prev->fx_offset !=  f->fx_offset))
10598                 {
10599                   fixS **pf;
10600
10601                   /* Move l->fixp before f.  */
10602                   for (pf = &seginfo->fix_root;
10603                        *pf != l->fixp;
10604                        pf = &(*pf)->fx_next)
10605                     assert (*pf != NULL);
10606
10607                   *pf = l->fixp->fx_next;
10608
10609                   l->fixp->fx_next = f;
10610                   if (prev == NULL)
10611                     seginfo->fix_root = l->fixp;
10612                   else
10613                     prev->fx_next = l->fixp;
10614
10615                   break;
10616                 }
10617
10618               prev = f;
10619             }
10620
10621           if (f != NULL)
10622             break;
10623
10624 #if 0 /* GCC code motion plus incomplete dead code elimination
10625          can leave a %hi without a %lo.  */
10626           if (pass == 1)
10627             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10628                            _("Unmatched %%hi reloc"));
10629 #endif
10630         }
10631     }
10632 }
10633
10634 /* When generating embedded PIC code we need to use a special
10635    relocation to represent the difference of two symbols in the .text
10636    section (switch tables use a difference of this sort).  See
10637    include/coff/mips.h for details.  This macro checks whether this
10638    fixup requires the special reloc.  */
10639 #define SWITCH_TABLE(fixp) \
10640   ((fixp)->fx_r_type == BFD_RELOC_32 \
10641    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10642    && (fixp)->fx_addsy != NULL \
10643    && (fixp)->fx_subsy != NULL \
10644    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10645    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10646
10647 /* When generating embedded PIC code we must keep all PC relative
10648    relocations, in case the linker has to relax a call.  We also need
10649    to keep relocations for switch table entries.
10650
10651    We may have combined relocations without symbols in the N32/N64 ABI.
10652    We have to prevent gas from dropping them.  */
10653
10654 int
10655 mips_force_relocation (fixp)
10656      fixS *fixp;
10657 {
10658   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10659       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10660       || S_FORCE_RELOC (fixp->fx_addsy))
10661     return 1;
10662
10663   if (HAVE_NEWABI
10664       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10665       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10666           || fixp->fx_r_type == BFD_RELOC_HI16_S
10667           || fixp->fx_r_type == BFD_RELOC_LO16))
10668     return 1;
10669
10670   return (mips_pic == EMBEDDED_PIC
10671           && (fixp->fx_pcrel
10672               || SWITCH_TABLE (fixp)
10673               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10674               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10675 }
10676
10677 #ifdef OBJ_ELF
10678 static int
10679 mips_need_elf_addend_fixup (fixP)
10680      fixS *fixP;
10681 {
10682   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10683     return 1;
10684   if (mips_pic == EMBEDDED_PIC
10685       && S_IS_WEAK (fixP->fx_addsy))
10686     return 1;
10687   if (mips_pic != EMBEDDED_PIC
10688       && (S_IS_WEAK (fixP->fx_addsy)
10689           || S_IS_EXTERNAL (fixP->fx_addsy))
10690       && !S_IS_COMMON (fixP->fx_addsy))
10691     return 1;
10692   if (symbol_used_in_reloc_p (fixP->fx_addsy)
10693       && (((bfd_get_section_flags (stdoutput,
10694                                    S_GET_SEGMENT (fixP->fx_addsy))
10695             & SEC_LINK_ONCE) != 0)
10696           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10697                        ".gnu.linkonce",
10698                        sizeof (".gnu.linkonce") - 1)))
10699     return 1;
10700   return 0;
10701 }
10702 #endif
10703
10704 /* Apply a fixup to the object file.  */
10705
10706 void
10707 md_apply_fix3 (fixP, valP, seg)
10708      fixS *fixP;
10709      valueT *valP;
10710      segT seg ATTRIBUTE_UNUSED;
10711 {
10712   bfd_byte *buf;
10713   long insn;
10714   valueT value;
10715   static int previous_fx_r_type = 0;
10716
10717   /* FIXME: Maybe just return for all reloc types not listed below?
10718      Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
10719   if (fixP->fx_r_type == BFD_RELOC_8)
10720       return;
10721
10722   assert (fixP->fx_size == 4
10723           || fixP->fx_r_type == BFD_RELOC_16
10724           || fixP->fx_r_type == BFD_RELOC_32
10725           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10726           || fixP->fx_r_type == BFD_RELOC_HI16_S
10727           || fixP->fx_r_type == BFD_RELOC_LO16
10728           || fixP->fx_r_type == BFD_RELOC_GPREL16
10729           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10730           || fixP->fx_r_type == BFD_RELOC_GPREL32
10731           || fixP->fx_r_type == BFD_RELOC_64
10732           || fixP->fx_r_type == BFD_RELOC_CTOR
10733           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10734           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10735           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10736           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10737           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10738           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10739           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10740           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10741           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
10742
10743   value = *valP;
10744
10745   /* If we aren't adjusting this fixup to be against the section
10746      symbol, we need to adjust the value.  */
10747 #ifdef OBJ_ELF
10748   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10749     {
10750       if (mips_need_elf_addend_fixup (fixP))
10751         {
10752           reloc_howto_type *howto;
10753           valueT symval = S_GET_VALUE (fixP->fx_addsy);
10754
10755           value -= symval;
10756
10757           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10758           if (value != 0 && howto->partial_inplace && ! fixP->fx_pcrel)
10759             {
10760               /* In this case, the bfd_install_relocation routine will
10761                  incorrectly add the symbol value back in.  We just want
10762                  the addend to appear in the object file.  */
10763               value -= symval;
10764
10765               /* Make sure the addend is still non-zero.  If it became zero
10766                  after the last operation, set it to a spurious value and
10767                  subtract the same value from the object file's contents.  */
10768               if (value == 0)
10769                 {
10770                   value = 8;
10771
10772                   /* The in-place addends for LO16 relocations are signed;
10773                      leave the matching HI16 in-place addends as zero.  */
10774                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10775                     {
10776                       bfd_vma contents, mask, field;
10777
10778                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
10779                                                + fixP->fx_where,
10780                                                fixP->fx_size * 8,
10781                                                target_big_endian);
10782
10783                       /* MASK has bits set where the relocation should go.
10784                          FIELD is -value, shifted into the appropriate place
10785                          for this relocation.  */
10786                       mask = 1 << (howto->bitsize - 1);
10787                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10788                       field = (-value >> howto->rightshift) << howto->bitpos;
10789
10790                       bfd_put_bits ((field & mask) | (contents & ~mask),
10791                                     fixP->fx_frag->fr_literal + fixP->fx_where,
10792                                     fixP->fx_size * 8,
10793                                     target_big_endian);
10794                     }
10795                 }
10796             }
10797         }
10798
10799       /* This code was generated using trial and error and so is
10800          fragile and not trustworthy.  If you change it, you should
10801          rerun the elf-rel, elf-rel2, and empic testcases and ensure
10802          they still pass.  */
10803       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10804         {
10805           value += fixP->fx_frag->fr_address + fixP->fx_where;
10806
10807           /* BFD's REL handling, for MIPS, is _very_ weird.
10808              This gives the right results, but it can't possibly
10809              be the way things are supposed to work.  */
10810           if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10811                && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10812               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10813             value += fixP->fx_frag->fr_address + fixP->fx_where;
10814         }
10815     }
10816 #endif
10817
10818   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
10819
10820   /* We are not done if this is a composite relocation to set up gp.  */
10821   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
10822       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10823            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
10824                && (fixP->fx_r_type == BFD_RELOC_HI16_S
10825                    || fixP->fx_r_type == BFD_RELOC_LO16))))
10826     fixP->fx_done = 1;
10827   previous_fx_r_type = fixP->fx_r_type;
10828
10829   switch (fixP->fx_r_type)
10830     {
10831     case BFD_RELOC_MIPS_JMP:
10832     case BFD_RELOC_MIPS_SHIFT5:
10833     case BFD_RELOC_MIPS_SHIFT6:
10834     case BFD_RELOC_MIPS_GOT_DISP:
10835     case BFD_RELOC_MIPS_GOT_PAGE:
10836     case BFD_RELOC_MIPS_GOT_OFST:
10837     case BFD_RELOC_MIPS_SUB:
10838     case BFD_RELOC_MIPS_INSERT_A:
10839     case BFD_RELOC_MIPS_INSERT_B:
10840     case BFD_RELOC_MIPS_DELETE:
10841     case BFD_RELOC_MIPS_HIGHEST:
10842     case BFD_RELOC_MIPS_HIGHER:
10843     case BFD_RELOC_MIPS_SCN_DISP:
10844     case BFD_RELOC_MIPS_REL16:
10845     case BFD_RELOC_MIPS_RELGOT:
10846     case BFD_RELOC_MIPS_JALR:
10847     case BFD_RELOC_HI16:
10848     case BFD_RELOC_HI16_S:
10849     case BFD_RELOC_GPREL16:
10850     case BFD_RELOC_MIPS_LITERAL:
10851     case BFD_RELOC_MIPS_CALL16:
10852     case BFD_RELOC_MIPS_GOT16:
10853     case BFD_RELOC_GPREL32:
10854     case BFD_RELOC_MIPS_GOT_HI16:
10855     case BFD_RELOC_MIPS_GOT_LO16:
10856     case BFD_RELOC_MIPS_CALL_HI16:
10857     case BFD_RELOC_MIPS_CALL_LO16:
10858     case BFD_RELOC_MIPS16_GPREL:
10859       if (fixP->fx_pcrel)
10860         as_bad_where (fixP->fx_file, fixP->fx_line,
10861                       _("Invalid PC relative reloc"));
10862       /* Nothing needed to do. The value comes from the reloc entry */
10863       break;
10864
10865     case BFD_RELOC_MIPS16_JMP:
10866       /* We currently always generate a reloc against a symbol, which
10867          means that we don't want an addend even if the symbol is
10868          defined.  */
10869       fixP->fx_addnumber = 0;
10870       break;
10871
10872     case BFD_RELOC_PCREL_HI16_S:
10873       /* The addend for this is tricky if it is internal, so we just
10874          do everything here rather than in bfd_install_relocation.  */
10875       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10876           && !fixP->fx_done
10877           && value != 0)
10878         break;
10879       if (fixP->fx_addsy
10880           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10881         {
10882           /* For an external symbol adjust by the address to make it
10883              pcrel_offset.  We use the address of the RELLO reloc
10884              which follows this one.  */
10885           value += (fixP->fx_next->fx_frag->fr_address
10886                     + fixP->fx_next->fx_where);
10887         }
10888       value = ((value + 0x8000) >> 16) & 0xffff;
10889       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10890       if (target_big_endian)
10891         buf += 2;
10892       md_number_to_chars ((char *) buf, value, 2);
10893       break;
10894
10895     case BFD_RELOC_PCREL_LO16:
10896       /* The addend for this is tricky if it is internal, so we just
10897          do everything here rather than in bfd_install_relocation.  */
10898       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10899           && !fixP->fx_done
10900           && value != 0)
10901         break;
10902       if (fixP->fx_addsy
10903           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10904         value += fixP->fx_frag->fr_address + fixP->fx_where;
10905       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10906       if (target_big_endian)
10907         buf += 2;
10908       md_number_to_chars ((char *) buf, value, 2);
10909       break;
10910
10911     case BFD_RELOC_64:
10912       /* This is handled like BFD_RELOC_32, but we output a sign
10913          extended value if we are only 32 bits.  */
10914       if (fixP->fx_done
10915           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10916         {
10917           if (8 <= sizeof (valueT))
10918             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10919                                 value, 8);
10920           else
10921             {
10922               long w1, w2;
10923               long hiv;
10924
10925               w1 = w2 = fixP->fx_where;
10926               if (target_big_endian)
10927                 w1 += 4;
10928               else
10929                 w2 += 4;
10930               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10931               if ((value & 0x80000000) != 0)
10932                 hiv = 0xffffffff;
10933               else
10934                 hiv = 0;
10935               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10936             }
10937         }
10938       break;
10939
10940     case BFD_RELOC_RVA:
10941     case BFD_RELOC_32:
10942       /* If we are deleting this reloc entry, we must fill in the
10943          value now.  This can happen if we have a .word which is not
10944          resolved when it appears but is later defined.  We also need
10945          to fill in the value if this is an embedded PIC switch table
10946          entry.  */
10947       if (fixP->fx_done
10948           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10949         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10950                             value, 4);
10951       break;
10952
10953     case BFD_RELOC_16:
10954       /* If we are deleting this reloc entry, we must fill in the
10955          value now.  */
10956       assert (fixP->fx_size == 2);
10957       if (fixP->fx_done)
10958         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10959                             value, 2);
10960       break;
10961
10962     case BFD_RELOC_LO16:
10963       /* When handling an embedded PIC switch statement, we can wind
10964          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
10965       if (fixP->fx_done)
10966         {
10967           if (value + 0x8000 > 0xffff)
10968             as_bad_where (fixP->fx_file, fixP->fx_line,
10969                           _("relocation overflow"));
10970           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10971           if (target_big_endian)
10972             buf += 2;
10973           md_number_to_chars ((char *) buf, value, 2);
10974         }
10975       break;
10976
10977     case BFD_RELOC_16_PCREL_S2:
10978       if ((value & 0x3) != 0)
10979         as_bad_where (fixP->fx_file, fixP->fx_line,
10980                       _("Branch to odd address (%lx)"), (long) value);
10981
10982       /* Fall through.  */
10983
10984     case BFD_RELOC_16_PCREL:
10985       /*
10986        * We need to save the bits in the instruction since fixup_segment()
10987        * might be deleting the relocation entry (i.e., a branch within
10988        * the current segment).
10989        */
10990       if (!fixP->fx_done && value != 0)
10991         break;
10992       /* If 'value' is zero, the remaining reloc code won't actually
10993          do the store, so it must be done here.  This is probably
10994          a bug somewhere.  */
10995       if (!fixP->fx_done
10996           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
10997               || fixP->fx_addsy == NULL                 /* ??? */
10998               || ! S_IS_DEFINED (fixP->fx_addsy)))
10999         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11000
11001       value = (offsetT) value >> 2;
11002
11003       /* update old instruction data */
11004       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11005       if (target_big_endian)
11006         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11007       else
11008         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11009
11010       if (value + 0x8000 <= 0xffff)
11011         insn |= value & 0xffff;
11012       else
11013         {
11014           /* The branch offset is too large.  If this is an
11015              unconditional branch, and we are not generating PIC code,
11016              we can convert it to an absolute jump instruction.  */
11017           if (mips_pic == NO_PIC
11018               && fixP->fx_done
11019               && fixP->fx_frag->fr_address >= text_section->vma
11020               && (fixP->fx_frag->fr_address
11021                   < text_section->vma + text_section->_raw_size)
11022               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11023                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11024                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11025             {
11026               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11027                 insn = 0x0c000000;      /* jal */
11028               else
11029                 insn = 0x08000000;      /* j */
11030               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11031               fixP->fx_done = 0;
11032               fixP->fx_addsy = section_symbol (text_section);
11033               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11034             }
11035           else
11036             {
11037               /* FIXME.  It would be possible in principle to handle
11038                  conditional branches which overflow.  They could be
11039                  transformed into a branch around a jump.  This would
11040                  require setting up variant frags for each different
11041                  branch type.  The native MIPS assembler attempts to
11042                  handle these cases, but it appears to do it
11043                  incorrectly.  */
11044               as_bad_where (fixP->fx_file, fixP->fx_line,
11045                             _("Branch out of range"));
11046             }
11047         }
11048
11049       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11050       break;
11051
11052     case BFD_RELOC_VTABLE_INHERIT:
11053       fixP->fx_done = 0;
11054       if (fixP->fx_addsy
11055           && !S_IS_DEFINED (fixP->fx_addsy)
11056           && !S_IS_WEAK (fixP->fx_addsy))
11057         S_SET_WEAK (fixP->fx_addsy);
11058       break;
11059
11060     case BFD_RELOC_VTABLE_ENTRY:
11061       fixP->fx_done = 0;
11062       break;
11063
11064     default:
11065       internalError ();
11066     }
11067 }
11068
11069 #if 0
11070 void
11071 printInsn (oc)
11072      unsigned long oc;
11073 {
11074   const struct mips_opcode *p;
11075   int treg, sreg, dreg, shamt;
11076   short imm;
11077   const char *args;
11078   int i;
11079
11080   for (i = 0; i < NUMOPCODES; ++i)
11081     {
11082       p = &mips_opcodes[i];
11083       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11084         {
11085           printf ("%08lx %s\t", oc, p->name);
11086           treg = (oc >> 16) & 0x1f;
11087           sreg = (oc >> 21) & 0x1f;
11088           dreg = (oc >> 11) & 0x1f;
11089           shamt = (oc >> 6) & 0x1f;
11090           imm = oc;
11091           for (args = p->args;; ++args)
11092             {
11093               switch (*args)
11094                 {
11095                 case '\0':
11096                   printf ("\n");
11097                   break;
11098
11099                 case ',':
11100                 case '(':
11101                 case ')':
11102                   printf ("%c", *args);
11103                   continue;
11104
11105                 case 'r':
11106                   assert (treg == sreg);
11107                   printf ("$%d,$%d", treg, sreg);
11108                   continue;
11109
11110                 case 'd':
11111                 case 'G':
11112                   printf ("$%d", dreg);
11113                   continue;
11114
11115                 case 't':
11116                 case 'E':
11117                   printf ("$%d", treg);
11118                   continue;
11119
11120                 case 'k':
11121                   printf ("0x%x", treg);
11122                   continue;
11123
11124                 case 'b':
11125                 case 's':
11126                   printf ("$%d", sreg);
11127                   continue;
11128
11129                 case 'a':
11130                   printf ("0x%08lx", oc & 0x1ffffff);
11131                   continue;
11132
11133                 case 'i':
11134                 case 'j':
11135                 case 'o':
11136                 case 'u':
11137                   printf ("%d", imm);
11138                   continue;
11139
11140                 case '<':
11141                 case '>':
11142                   printf ("$%d", shamt);
11143                   continue;
11144
11145                 default:
11146                   internalError ();
11147                 }
11148               break;
11149             }
11150           return;
11151         }
11152     }
11153   printf (_("%08lx  UNDEFINED\n"), oc);
11154 }
11155 #endif
11156
11157 static symbolS *
11158 get_symbol ()
11159 {
11160   int c;
11161   char *name;
11162   symbolS *p;
11163
11164   name = input_line_pointer;
11165   c = get_symbol_end ();
11166   p = (symbolS *) symbol_find_or_make (name);
11167   *input_line_pointer = c;
11168   return p;
11169 }
11170
11171 /* Align the current frag to a given power of two.  The MIPS assembler
11172    also automatically adjusts any preceding label.  */
11173
11174 static void
11175 mips_align (to, fill, label)
11176      int to;
11177      int fill;
11178      symbolS *label;
11179 {
11180   mips_emit_delays (false);
11181   frag_align (to, fill, 0);
11182   record_alignment (now_seg, to);
11183   if (label != NULL)
11184     {
11185       assert (S_GET_SEGMENT (label) == now_seg);
11186       symbol_set_frag (label, frag_now);
11187       S_SET_VALUE (label, (valueT) frag_now_fix ());
11188     }
11189 }
11190
11191 /* Align to a given power of two.  .align 0 turns off the automatic
11192    alignment used by the data creating pseudo-ops.  */
11193
11194 static void
11195 s_align (x)
11196      int x ATTRIBUTE_UNUSED;
11197 {
11198   register int temp;
11199   register long temp_fill;
11200   long max_alignment = 15;
11201
11202   /*
11203
11204     o  Note that the assembler pulls down any immediately preceeding label
11205        to the aligned address.
11206     o  It's not documented but auto alignment is reinstated by
11207        a .align pseudo instruction.
11208     o  Note also that after auto alignment is turned off the mips assembler
11209        issues an error on attempt to assemble an improperly aligned data item.
11210        We don't.
11211
11212     */
11213
11214   temp = get_absolute_expression ();
11215   if (temp > max_alignment)
11216     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11217   else if (temp < 0)
11218     {
11219       as_warn (_("Alignment negative: 0 assumed."));
11220       temp = 0;
11221     }
11222   if (*input_line_pointer == ',')
11223     {
11224       ++input_line_pointer;
11225       temp_fill = get_absolute_expression ();
11226     }
11227   else
11228     temp_fill = 0;
11229   if (temp)
11230     {
11231       auto_align = 1;
11232       mips_align (temp, (int) temp_fill,
11233                   insn_labels != NULL ? insn_labels->label : NULL);
11234     }
11235   else
11236     {
11237       auto_align = 0;
11238     }
11239
11240   demand_empty_rest_of_line ();
11241 }
11242
11243 void
11244 mips_flush_pending_output ()
11245 {
11246   mips_emit_delays (false);
11247   mips_clear_insn_labels ();
11248 }
11249
11250 static void
11251 s_change_sec (sec)
11252      int sec;
11253 {
11254   segT seg;
11255
11256   /* When generating embedded PIC code, we only use the .text, .lit8,
11257      .sdata and .sbss sections.  We change the .data and .rdata
11258      pseudo-ops to use .sdata.  */
11259   if (mips_pic == EMBEDDED_PIC
11260       && (sec == 'd' || sec == 'r'))
11261     sec = 's';
11262
11263 #ifdef OBJ_ELF
11264   /* The ELF backend needs to know that we are changing sections, so
11265      that .previous works correctly.  We could do something like check
11266      for an obj_section_change_hook macro, but that might be confusing
11267      as it would not be appropriate to use it in the section changing
11268      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11269      This should be cleaner, somehow.  */
11270   obj_elf_section_change_hook ();
11271 #endif
11272
11273   mips_emit_delays (false);
11274   switch (sec)
11275     {
11276     case 't':
11277       s_text (0);
11278       break;
11279     case 'd':
11280       s_data (0);
11281       break;
11282     case 'b':
11283       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11284       demand_empty_rest_of_line ();
11285       break;
11286
11287     case 'r':
11288       if (USE_GLOBAL_POINTER_OPT)
11289         {
11290           seg = subseg_new (RDATA_SECTION_NAME,
11291                             (subsegT) get_absolute_expression ());
11292           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11293             {
11294               bfd_set_section_flags (stdoutput, seg,
11295                                      (SEC_ALLOC
11296                                       | SEC_LOAD
11297                                       | SEC_READONLY
11298                                       | SEC_RELOC
11299                                       | SEC_DATA));
11300               if (strcmp (TARGET_OS, "elf") != 0)
11301                 record_alignment (seg, 4);
11302             }
11303           demand_empty_rest_of_line ();
11304         }
11305       else
11306         {
11307           as_bad (_("No read only data section in this object file format"));
11308           demand_empty_rest_of_line ();
11309           return;
11310         }
11311       break;
11312
11313     case 's':
11314       if (USE_GLOBAL_POINTER_OPT)
11315         {
11316           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11317           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11318             {
11319               bfd_set_section_flags (stdoutput, seg,
11320                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11321                                      | SEC_DATA);
11322               if (strcmp (TARGET_OS, "elf") != 0)
11323                 record_alignment (seg, 4);
11324             }
11325           demand_empty_rest_of_line ();
11326           break;
11327         }
11328       else
11329         {
11330           as_bad (_("Global pointers not supported; recompile -G 0"));
11331           demand_empty_rest_of_line ();
11332           return;
11333         }
11334     }
11335
11336   auto_align = 1;
11337 }
11338   
11339 void
11340 s_change_section (ignore)
11341      int ignore ATTRIBUTE_UNUSED;
11342 {
11343 #ifdef OBJ_ELF
11344   char *section_name;
11345   char c;
11346   char next_c;
11347   int section_type;
11348   int section_flag;
11349   int section_entry_size;
11350   int section_alignment;
11351   
11352   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11353     return;
11354
11355   section_name = input_line_pointer;
11356   c = get_symbol_end ();
11357   next_c = *(input_line_pointer + 1);
11358
11359   /* Do we have .section Name<,"flags">?  */
11360   if (c != ',' || (c == ',' && next_c == '"'))
11361     {
11362       /* just after name is now '\0'.  */
11363       *input_line_pointer = c;
11364       input_line_pointer = section_name;
11365       obj_elf_section (ignore);
11366       return;
11367     }
11368   input_line_pointer++;
11369
11370   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11371   if (c == ',')
11372     section_type = get_absolute_expression ();
11373   else
11374     section_type = 0;
11375   if (*input_line_pointer++ == ',')
11376     section_flag = get_absolute_expression ();
11377   else
11378     section_flag = 0;
11379   if (*input_line_pointer++ == ',')
11380     section_entry_size = get_absolute_expression ();
11381   else
11382     section_entry_size = 0;
11383   if (*input_line_pointer++ == ',')
11384     section_alignment = get_absolute_expression ();
11385   else
11386     section_alignment = 0;
11387
11388   obj_elf_change_section (section_name, section_type, section_flag,
11389                           section_entry_size, 0, 0, 0);
11390 #endif /* OBJ_ELF */
11391 }
11392
11393 void
11394 mips_enable_auto_align ()
11395 {
11396   auto_align = 1;
11397 }
11398
11399 static void
11400 s_cons (log_size)
11401      int log_size;
11402 {
11403   symbolS *label;
11404
11405   label = insn_labels != NULL ? insn_labels->label : NULL;
11406   mips_emit_delays (false);
11407   if (log_size > 0 && auto_align)
11408     mips_align (log_size, 0, label);
11409   mips_clear_insn_labels ();
11410   cons (1 << log_size);
11411 }
11412
11413 static void
11414 s_float_cons (type)
11415      int type;
11416 {
11417   symbolS *label;
11418
11419   label = insn_labels != NULL ? insn_labels->label : NULL;
11420
11421   mips_emit_delays (false);
11422
11423   if (auto_align)
11424     {
11425       if (type == 'd')
11426         mips_align (3, 0, label);
11427       else
11428         mips_align (2, 0, label);
11429     }
11430
11431   mips_clear_insn_labels ();
11432
11433   float_cons (type);
11434 }
11435
11436 /* Handle .globl.  We need to override it because on Irix 5 you are
11437    permitted to say
11438        .globl foo .text
11439    where foo is an undefined symbol, to mean that foo should be
11440    considered to be the address of a function.  */
11441
11442 static void
11443 s_mips_globl (x)
11444      int x ATTRIBUTE_UNUSED;
11445 {
11446   char *name;
11447   int c;
11448   symbolS *symbolP;
11449   flagword flag;
11450
11451   name = input_line_pointer;
11452   c = get_symbol_end ();
11453   symbolP = symbol_find_or_make (name);
11454   *input_line_pointer = c;
11455   SKIP_WHITESPACE ();
11456
11457   /* On Irix 5, every global symbol that is not explicitly labelled as
11458      being a function is apparently labelled as being an object.  */
11459   flag = BSF_OBJECT;
11460
11461   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11462     {
11463       char *secname;
11464       asection *sec;
11465
11466       secname = input_line_pointer;
11467       c = get_symbol_end ();
11468       sec = bfd_get_section_by_name (stdoutput, secname);
11469       if (sec == NULL)
11470         as_bad (_("%s: no such section"), secname);
11471       *input_line_pointer = c;
11472
11473       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11474         flag = BSF_FUNCTION;
11475     }
11476
11477   symbol_get_bfdsym (symbolP)->flags |= flag;
11478
11479   S_SET_EXTERNAL (symbolP);
11480   demand_empty_rest_of_line ();
11481 }
11482
11483 static void
11484 s_option (x)
11485      int x ATTRIBUTE_UNUSED;
11486 {
11487   char *opt;
11488   char c;
11489
11490   opt = input_line_pointer;
11491   c = get_symbol_end ();
11492
11493   if (*opt == 'O')
11494     {
11495       /* FIXME: What does this mean?  */
11496     }
11497   else if (strncmp (opt, "pic", 3) == 0)
11498     {
11499       int i;
11500
11501       i = atoi (opt + 3);
11502       if (i == 0)
11503         mips_pic = NO_PIC;
11504       else if (i == 2)
11505         mips_pic = SVR4_PIC;
11506       else
11507         as_bad (_(".option pic%d not supported"), i);
11508
11509       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11510         {
11511           if (g_switch_seen && g_switch_value != 0)
11512             as_warn (_("-G may not be used with SVR4 PIC code"));
11513           g_switch_value = 0;
11514           bfd_set_gp_size (stdoutput, 0);
11515         }
11516     }
11517   else
11518     as_warn (_("Unrecognized option \"%s\""), opt);
11519
11520   *input_line_pointer = c;
11521   demand_empty_rest_of_line ();
11522 }
11523
11524 /* This structure is used to hold a stack of .set values.  */
11525
11526 struct mips_option_stack
11527 {
11528   struct mips_option_stack *next;
11529   struct mips_set_options options;
11530 };
11531
11532 static struct mips_option_stack *mips_opts_stack;
11533
11534 /* Handle the .set pseudo-op.  */
11535
11536 static void
11537 s_mipsset (x)
11538      int x ATTRIBUTE_UNUSED;
11539 {
11540   char *name = input_line_pointer, ch;
11541
11542   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11543     ++input_line_pointer;
11544   ch = *input_line_pointer;
11545   *input_line_pointer = '\0';
11546
11547   if (strcmp (name, "reorder") == 0)
11548     {
11549       if (mips_opts.noreorder && prev_nop_frag != NULL)
11550         {
11551           /* If we still have pending nops, we can discard them.  The
11552              usual nop handling will insert any that are still
11553              needed.  */
11554           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11555                                     * (mips_opts.mips16 ? 2 : 4));
11556           prev_nop_frag = NULL;
11557         }
11558       mips_opts.noreorder = 0;
11559     }
11560   else if (strcmp (name, "noreorder") == 0)
11561     {
11562       mips_emit_delays (true);
11563       mips_opts.noreorder = 1;
11564       mips_any_noreorder = 1;
11565     }
11566   else if (strcmp (name, "at") == 0)
11567     {
11568       mips_opts.noat = 0;
11569     }
11570   else if (strcmp (name, "noat") == 0)
11571     {
11572       mips_opts.noat = 1;
11573     }
11574   else if (strcmp (name, "macro") == 0)
11575     {
11576       mips_opts.warn_about_macros = 0;
11577     }
11578   else if (strcmp (name, "nomacro") == 0)
11579     {
11580       if (mips_opts.noreorder == 0)
11581         as_bad (_("`noreorder' must be set before `nomacro'"));
11582       mips_opts.warn_about_macros = 1;
11583     }
11584   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11585     {
11586       mips_opts.nomove = 0;
11587     }
11588   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11589     {
11590       mips_opts.nomove = 1;
11591     }
11592   else if (strcmp (name, "bopt") == 0)
11593     {
11594       mips_opts.nobopt = 0;
11595     }
11596   else if (strcmp (name, "nobopt") == 0)
11597     {
11598       mips_opts.nobopt = 1;
11599     }
11600   else if (strcmp (name, "mips16") == 0
11601            || strcmp (name, "MIPS-16") == 0)
11602     mips_opts.mips16 = 1;
11603   else if (strcmp (name, "nomips16") == 0
11604            || strcmp (name, "noMIPS-16") == 0)
11605     mips_opts.mips16 = 0;
11606   else if (strcmp (name, "mips3d") == 0)
11607     mips_opts.ase_mips3d = 1;
11608   else if (strcmp (name, "nomips3d") == 0)
11609     mips_opts.ase_mips3d = 0;
11610   else if (strcmp (name, "mdmx") == 0)
11611     mips_opts.ase_mdmx = 1;
11612   else if (strcmp (name, "nomdmx") == 0)
11613     mips_opts.ase_mdmx = 0;
11614   else if (strncmp (name, "mips", 4) == 0)
11615     {
11616       int isa;
11617
11618       /* Permit the user to change the ISA on the fly.  Needless to
11619          say, misuse can cause serious problems.  */
11620       isa = atoi (name + 4);
11621       switch (isa)
11622         {
11623         case  0:
11624           mips_opts.gp32 = file_mips_gp32;
11625           mips_opts.fp32 = file_mips_fp32;
11626           break;
11627         case  1:
11628         case  2:
11629         case 32:
11630           mips_opts.gp32 = 1;
11631           mips_opts.fp32 = 1;
11632           break;
11633         case  3:
11634         case  4:
11635         case  5:
11636         case 64:
11637           mips_opts.gp32 = 0;
11638           mips_opts.fp32 = 0;
11639           break;
11640         default:
11641           as_bad (_("unknown ISA level %s"), name + 4);
11642           break;
11643         }
11644
11645       switch (isa)
11646         {
11647         case  0: mips_opts.isa = file_mips_isa;   break;
11648         case  1: mips_opts.isa = ISA_MIPS1;       break;
11649         case  2: mips_opts.isa = ISA_MIPS2;       break;
11650         case  3: mips_opts.isa = ISA_MIPS3;       break;
11651         case  4: mips_opts.isa = ISA_MIPS4;       break;
11652         case  5: mips_opts.isa = ISA_MIPS5;       break;
11653         case 32: mips_opts.isa = ISA_MIPS32;      break;
11654         case 64: mips_opts.isa = ISA_MIPS64;      break;
11655         default: as_bad (_("unknown ISA level %s"), name + 4); break;
11656         }
11657     }
11658   else if (strcmp (name, "autoextend") == 0)
11659     mips_opts.noautoextend = 0;
11660   else if (strcmp (name, "noautoextend") == 0)
11661     mips_opts.noautoextend = 1;
11662   else if (strcmp (name, "push") == 0)
11663     {
11664       struct mips_option_stack *s;
11665
11666       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11667       s->next = mips_opts_stack;
11668       s->options = mips_opts;
11669       mips_opts_stack = s;
11670     }
11671   else if (strcmp (name, "pop") == 0)
11672     {
11673       struct mips_option_stack *s;
11674
11675       s = mips_opts_stack;
11676       if (s == NULL)
11677         as_bad (_(".set pop with no .set push"));
11678       else
11679         {
11680           /* If we're changing the reorder mode we need to handle
11681              delay slots correctly.  */
11682           if (s->options.noreorder && ! mips_opts.noreorder)
11683             mips_emit_delays (true);
11684           else if (! s->options.noreorder && mips_opts.noreorder)
11685             {
11686               if (prev_nop_frag != NULL)
11687                 {
11688                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11689                                             * (mips_opts.mips16 ? 2 : 4));
11690                   prev_nop_frag = NULL;
11691                 }
11692             }
11693
11694           mips_opts = s->options;
11695           mips_opts_stack = s->next;
11696           free (s);
11697         }
11698     }
11699   else
11700     {
11701       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11702     }
11703   *input_line_pointer = ch;
11704   demand_empty_rest_of_line ();
11705 }
11706
11707 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
11708    .option pic2.  It means to generate SVR4 PIC calls.  */
11709
11710 static void
11711 s_abicalls (ignore)
11712      int ignore ATTRIBUTE_UNUSED;
11713 {
11714   mips_pic = SVR4_PIC;
11715   if (USE_GLOBAL_POINTER_OPT)
11716     {
11717       if (g_switch_seen && g_switch_value != 0)
11718         as_warn (_("-G may not be used with SVR4 PIC code"));
11719       g_switch_value = 0;
11720     }
11721   bfd_set_gp_size (stdoutput, 0);
11722   demand_empty_rest_of_line ();
11723 }
11724
11725 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
11726    PIC code.  It sets the $gp register for the function based on the
11727    function address, which is in the register named in the argument.
11728    This uses a relocation against _gp_disp, which is handled specially
11729    by the linker.  The result is:
11730         lui     $gp,%hi(_gp_disp)
11731         addiu   $gp,$gp,%lo(_gp_disp)
11732         addu    $gp,$gp,.cpload argument
11733    The .cpload argument is normally $25 == $t9.  */
11734
11735 static void
11736 s_cpload (ignore)
11737      int ignore ATTRIBUTE_UNUSED;
11738 {
11739   expressionS ex;
11740   int icnt = 0;
11741
11742   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11743      .cpload is ignored.  */
11744   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11745     {
11746       s_ignore (0);
11747       return;
11748     }
11749
11750   /* .cpload should be in a .set noreorder section.  */
11751   if (mips_opts.noreorder == 0)
11752     as_warn (_(".cpload not in noreorder section"));
11753
11754   ex.X_op = O_symbol;
11755   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11756   ex.X_op_symbol = NULL;
11757   ex.X_add_number = 0;
11758
11759   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
11760   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11761
11762   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11763   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11764                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
11765
11766   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11767                mips_gp_register, mips_gp_register, tc_get_register (0));
11768
11769   demand_empty_rest_of_line ();
11770 }
11771
11772 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
11773      .cpsetup $reg1, offset|$reg2, label
11774
11775    If offset is given, this results in:
11776      sd         $gp, offset($sp)
11777      lui        $gp, %hi(%neg(%gp_rel(label)))
11778      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11779      daddu      $gp, $gp, $reg1
11780
11781    If $reg2 is given, this results in:
11782      daddu      $reg2, $gp, $0
11783      lui        $gp, %hi(%neg(%gp_rel(label)))
11784      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11785      daddu      $gp, $gp, $reg1
11786    $reg1 is normally $25 == $t9.  */
11787 static void
11788 s_cpsetup (ignore)
11789      int ignore ATTRIBUTE_UNUSED;
11790 {
11791   expressionS ex_off;
11792   expressionS ex_sym;
11793   int reg1;
11794   int icnt = 0;
11795   char *f;
11796
11797   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11798      We also need NewABI support.  */
11799   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11800     {
11801       s_ignore (0);
11802       return;
11803     }
11804
11805   reg1 = tc_get_register (0);
11806   SKIP_WHITESPACE ();
11807   if (*input_line_pointer != ',')
11808     {
11809       as_bad (_("missing argument separator ',' for .cpsetup"));
11810       return;
11811     }
11812   else
11813     ++input_line_pointer;
11814   SKIP_WHITESPACE ();
11815   if (*input_line_pointer == '$')
11816     {
11817       mips_cpreturn_register = tc_get_register (0);
11818       mips_cpreturn_offset = -1;
11819     }
11820   else
11821     {
11822       mips_cpreturn_offset = get_absolute_expression ();
11823       mips_cpreturn_register = -1;
11824     }
11825   SKIP_WHITESPACE ();
11826   if (*input_line_pointer != ',')
11827     {
11828       as_bad (_("missing argument separator ',' for .cpsetup"));
11829       return;
11830     }
11831   else
11832     ++input_line_pointer;
11833   SKIP_WHITESPACE ();
11834   expression (&ex_sym);
11835
11836   if (mips_cpreturn_register == -1)
11837     {
11838       ex_off.X_op = O_constant;
11839       ex_off.X_add_symbol = NULL;
11840       ex_off.X_op_symbol = NULL;
11841       ex_off.X_add_number = mips_cpreturn_offset;
11842
11843       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11844                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11845     }
11846   else
11847     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11848                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11849
11850   /* Ensure there's room for the next two instructions, so that `f'
11851      doesn't end up with an address in the wrong frag.  */
11852   frag_grow (8);
11853   f = frag_more (0);
11854   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11855                (int) BFD_RELOC_GPREL16);
11856   fix_new (frag_now, f - frag_now->fr_literal,
11857            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11858   fix_new (frag_now, f - frag_now->fr_literal,
11859            0, NULL, 0, 0, BFD_RELOC_HI16_S);
11860
11861   f = frag_more (0);
11862   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11863                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11864   fix_new (frag_now, f - frag_now->fr_literal,
11865            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11866   fix_new (frag_now, f - frag_now->fr_literal,
11867            0, NULL, 0, 0, BFD_RELOC_LO16);
11868
11869   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11870                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11871                mips_gp_register, mips_gp_register, reg1);
11872
11873   demand_empty_rest_of_line ();
11874 }
11875
11876 static void
11877 s_cplocal (ignore)
11878      int ignore ATTRIBUTE_UNUSED;
11879 {
11880   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11881    .cplocal is ignored.  */
11882   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11883     {
11884       s_ignore (0);
11885       return;
11886     }
11887
11888   mips_gp_register = tc_get_register (0);
11889   demand_empty_rest_of_line ();
11890 }
11891
11892 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
11893    offset from $sp.  The offset is remembered, and after making a PIC
11894    call $gp is restored from that location.  */
11895
11896 static void
11897 s_cprestore (ignore)
11898      int ignore ATTRIBUTE_UNUSED;
11899 {
11900   expressionS ex;
11901   int icnt = 0;
11902
11903   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11904      .cprestore is ignored.  */
11905   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11906     {
11907       s_ignore (0);
11908       return;
11909     }
11910
11911   mips_cprestore_offset = get_absolute_expression ();
11912   mips_cprestore_valid = 1;
11913
11914   ex.X_op = O_constant;
11915   ex.X_add_symbol = NULL;
11916   ex.X_op_symbol = NULL;
11917   ex.X_add_number = mips_cprestore_offset;
11918
11919   macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
11920                                 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11921                                 mips_gp_register, SP);
11922
11923   demand_empty_rest_of_line ();
11924 }
11925
11926 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11927    was given in the preceeding .gpsetup, it results in:
11928      ld         $gp, offset($sp)
11929
11930    If a register $reg2 was given there, it results in:
11931      daddiu     $gp, $gp, $reg2
11932  */
11933 static void
11934 s_cpreturn (ignore)
11935      int ignore ATTRIBUTE_UNUSED;
11936 {
11937   expressionS ex;
11938   int icnt = 0;
11939
11940   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11941      We also need NewABI support.  */
11942   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11943     {
11944       s_ignore (0);
11945       return;
11946     }
11947
11948   if (mips_cpreturn_register == -1)
11949     {
11950       ex.X_op = O_constant;
11951       ex.X_add_symbol = NULL;
11952       ex.X_op_symbol = NULL;
11953       ex.X_add_number = mips_cpreturn_offset;
11954
11955       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11956                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11957     }
11958   else
11959     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11960                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11961
11962   demand_empty_rest_of_line ();
11963 }
11964
11965 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
11966    code.  It sets the offset to use in gp_rel relocations.  */
11967
11968 static void
11969 s_gpvalue (ignore)
11970      int ignore ATTRIBUTE_UNUSED;
11971 {
11972   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11973      We also need NewABI support.  */
11974   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11975     {
11976       s_ignore (0);
11977       return;
11978     }
11979
11980   mips_gprel_offset = get_absolute_expression ();
11981
11982   demand_empty_rest_of_line ();
11983 }
11984
11985 /* Handle the .gpword pseudo-op.  This is used when generating PIC
11986    code.  It generates a 32 bit GP relative reloc.  */
11987
11988 static void
11989 s_gpword (ignore)
11990      int ignore ATTRIBUTE_UNUSED;
11991 {
11992   symbolS *label;
11993   expressionS ex;
11994   char *p;
11995
11996   /* When not generating PIC code, this is treated as .word.  */
11997   if (mips_pic != SVR4_PIC)
11998     {
11999       s_cons (2);
12000       return;
12001     }
12002
12003   label = insn_labels != NULL ? insn_labels->label : NULL;
12004   mips_emit_delays (true);
12005   if (auto_align)
12006     mips_align (2, 0, label);
12007   mips_clear_insn_labels ();
12008
12009   expression (&ex);
12010
12011   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12012     {
12013       as_bad (_("Unsupported use of .gpword"));
12014       ignore_rest_of_line ();
12015     }
12016
12017   p = frag_more (4);
12018   md_number_to_chars (p, (valueT) 0, 4);
12019   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
12020                BFD_RELOC_GPREL32);
12021
12022   demand_empty_rest_of_line ();
12023 }
12024
12025 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12026    tables in SVR4 PIC code.  */
12027
12028 static void
12029 s_cpadd (ignore)
12030      int ignore ATTRIBUTE_UNUSED;
12031 {
12032   int icnt = 0;
12033   int reg;
12034
12035   /* This is ignored when not generating SVR4 PIC code or if this is NewABI
12036      code.  */
12037   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12038     {
12039       s_ignore (0);
12040       return;
12041     }
12042
12043   /* Add $gp to the register named as an argument.  */
12044   reg = tc_get_register (0);
12045   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12046                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12047                "d,v,t", reg, reg, mips_gp_register);
12048
12049   demand_empty_rest_of_line ();
12050 }
12051
12052 /* Handle the .insn pseudo-op.  This marks instruction labels in
12053    mips16 mode.  This permits the linker to handle them specially,
12054    such as generating jalx instructions when needed.  We also make
12055    them odd for the duration of the assembly, in order to generate the
12056    right sort of code.  We will make them even in the adjust_symtab
12057    routine, while leaving them marked.  This is convenient for the
12058    debugger and the disassembler.  The linker knows to make them odd
12059    again.  */
12060
12061 static void
12062 s_insn (ignore)
12063      int ignore ATTRIBUTE_UNUSED;
12064 {
12065   mips16_mark_labels ();
12066
12067   demand_empty_rest_of_line ();
12068 }
12069
12070 /* Handle a .stabn directive.  We need these in order to mark a label
12071    as being a mips16 text label correctly.  Sometimes the compiler
12072    will emit a label, followed by a .stabn, and then switch sections.
12073    If the label and .stabn are in mips16 mode, then the label is
12074    really a mips16 text label.  */
12075
12076 static void
12077 s_mips_stab (type)
12078      int type;
12079 {
12080   if (type == 'n')
12081     mips16_mark_labels ();
12082
12083   s_stab (type);
12084 }
12085
12086 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12087  */
12088
12089 static void
12090 s_mips_weakext (ignore)
12091      int ignore ATTRIBUTE_UNUSED;
12092 {
12093   char *name;
12094   int c;
12095   symbolS *symbolP;
12096   expressionS exp;
12097
12098   name = input_line_pointer;
12099   c = get_symbol_end ();
12100   symbolP = symbol_find_or_make (name);
12101   S_SET_WEAK (symbolP);
12102   *input_line_pointer = c;
12103
12104   SKIP_WHITESPACE ();
12105
12106   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12107     {
12108       if (S_IS_DEFINED (symbolP))
12109         {
12110           as_bad ("ignoring attempt to redefine symbol %s",
12111                   S_GET_NAME (symbolP));
12112           ignore_rest_of_line ();
12113           return;
12114         }
12115
12116       if (*input_line_pointer == ',')
12117         {
12118           ++input_line_pointer;
12119           SKIP_WHITESPACE ();
12120         }
12121
12122       expression (&exp);
12123       if (exp.X_op != O_symbol)
12124         {
12125           as_bad ("bad .weakext directive");
12126           ignore_rest_of_line ();
12127           return;
12128         }
12129       symbol_set_value_expression (symbolP, &exp);
12130     }
12131
12132   demand_empty_rest_of_line ();
12133 }
12134
12135 /* Parse a register string into a number.  Called from the ECOFF code
12136    to parse .frame.  The argument is non-zero if this is the frame
12137    register, so that we can record it in mips_frame_reg.  */
12138
12139 int
12140 tc_get_register (frame)
12141      int frame;
12142 {
12143   int reg;
12144
12145   SKIP_WHITESPACE ();
12146   if (*input_line_pointer++ != '$')
12147     {
12148       as_warn (_("expected `$'"));
12149       reg = ZERO;
12150     }
12151   else if (ISDIGIT (*input_line_pointer))
12152     {
12153       reg = get_absolute_expression ();
12154       if (reg < 0 || reg >= 32)
12155         {
12156           as_warn (_("Bad register number"));
12157           reg = ZERO;
12158         }
12159     }
12160   else
12161     {
12162       if (strncmp (input_line_pointer, "ra", 2) == 0)
12163         {
12164           reg = RA;
12165           input_line_pointer += 2;
12166         }
12167       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12168         {
12169           reg = FP;
12170           input_line_pointer += 2;
12171         }
12172       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12173         {
12174           reg = SP;
12175           input_line_pointer += 2;
12176         }
12177       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12178         {
12179           reg = GP;
12180           input_line_pointer += 2;
12181         }
12182       else if (strncmp (input_line_pointer, "at", 2) == 0)
12183         {
12184           reg = AT;
12185           input_line_pointer += 2;
12186         }
12187       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12188         {
12189           reg = KT0;
12190           input_line_pointer += 3;
12191         }
12192       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12193         {
12194           reg = KT1;
12195           input_line_pointer += 3;
12196         }
12197       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12198         {
12199           reg = ZERO;
12200           input_line_pointer += 4;
12201         }
12202       else
12203         {
12204           as_warn (_("Unrecognized register name"));
12205           reg = ZERO;
12206           while (ISALNUM(*input_line_pointer))
12207            input_line_pointer++;
12208         }
12209     }
12210   if (frame)
12211     {
12212       mips_frame_reg = reg != 0 ? reg : SP;
12213       mips_frame_reg_valid = 1;
12214       mips_cprestore_valid = 0;
12215     }
12216   return reg;
12217 }
12218
12219 valueT
12220 md_section_align (seg, addr)
12221      asection *seg;
12222      valueT addr;
12223 {
12224   int align = bfd_get_section_alignment (stdoutput, seg);
12225
12226 #ifdef OBJ_ELF
12227   /* We don't need to align ELF sections to the full alignment.
12228      However, Irix 5 may prefer that we align them at least to a 16
12229      byte boundary.  We don't bother to align the sections if we are
12230      targeted for an embedded system.  */
12231   if (strcmp (TARGET_OS, "elf") == 0)
12232     return addr;
12233   if (align > 4)
12234     align = 4;
12235 #endif
12236
12237   return ((addr + (1 << align) - 1) & (-1 << align));
12238 }
12239
12240 /* Utility routine, called from above as well.  If called while the
12241    input file is still being read, it's only an approximation.  (For
12242    example, a symbol may later become defined which appeared to be
12243    undefined earlier.)  */
12244
12245 static int
12246 nopic_need_relax (sym, before_relaxing)
12247      symbolS *sym;
12248      int before_relaxing;
12249 {
12250   if (sym == 0)
12251     return 0;
12252
12253   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12254     {
12255       const char *symname;
12256       int change;
12257
12258       /* Find out whether this symbol can be referenced off the $gp
12259          register.  It can be if it is smaller than the -G size or if
12260          it is in the .sdata or .sbss section.  Certain symbols can
12261          not be referenced off the $gp, although it appears as though
12262          they can.  */
12263       symname = S_GET_NAME (sym);
12264       if (symname != (const char *) NULL
12265           && (strcmp (symname, "eprol") == 0
12266               || strcmp (symname, "etext") == 0
12267               || strcmp (symname, "_gp") == 0
12268               || strcmp (symname, "edata") == 0
12269               || strcmp (symname, "_fbss") == 0
12270               || strcmp (symname, "_fdata") == 0
12271               || strcmp (symname, "_ftext") == 0
12272               || strcmp (symname, "end") == 0
12273               || strcmp (symname, "_gp_disp") == 0))
12274         change = 1;
12275       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12276                && (0
12277 #ifndef NO_ECOFF_DEBUGGING
12278                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12279                        && (symbol_get_obj (sym)->ecoff_extern_size
12280                            <= g_switch_value))
12281 #endif
12282                    /* We must defer this decision until after the whole
12283                       file has been read, since there might be a .extern
12284                       after the first use of this symbol.  */
12285                    || (before_relaxing
12286 #ifndef NO_ECOFF_DEBUGGING
12287                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12288 #endif
12289                        && S_GET_VALUE (sym) == 0)
12290                    || (S_GET_VALUE (sym) != 0
12291                        && S_GET_VALUE (sym) <= g_switch_value)))
12292         change = 0;
12293       else
12294         {
12295           const char *segname;
12296
12297           segname = segment_name (S_GET_SEGMENT (sym));
12298           assert (strcmp (segname, ".lit8") != 0
12299                   && strcmp (segname, ".lit4") != 0);
12300           change = (strcmp (segname, ".sdata") != 0
12301                     && strcmp (segname, ".sbss") != 0
12302                     && strncmp (segname, ".sdata.", 7) != 0
12303                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12304         }
12305       return change;
12306     }
12307   else
12308     /* We are not optimizing for the $gp register.  */
12309     return 1;
12310 }
12311
12312 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12313    extended opcode.  SEC is the section the frag is in.  */
12314
12315 static int
12316 mips16_extended_frag (fragp, sec, stretch)
12317      fragS *fragp;
12318      asection *sec;
12319      long stretch;
12320 {
12321   int type;
12322   register const struct mips16_immed_operand *op;
12323   offsetT val;
12324   int mintiny, maxtiny;
12325   segT symsec;
12326   fragS *sym_frag;
12327
12328   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12329     return 0;
12330   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12331     return 1;
12332
12333   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12334   op = mips16_immed_operands;
12335   while (op->type != type)
12336     {
12337       ++op;
12338       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12339     }
12340
12341   if (op->unsp)
12342     {
12343       if (type == '<' || type == '>' || type == '[' || type == ']')
12344         {
12345           mintiny = 1;
12346           maxtiny = 1 << op->nbits;
12347         }
12348       else
12349         {
12350           mintiny = 0;
12351           maxtiny = (1 << op->nbits) - 1;
12352         }
12353     }
12354   else
12355     {
12356       mintiny = - (1 << (op->nbits - 1));
12357       maxtiny = (1 << (op->nbits - 1)) - 1;
12358     }
12359
12360   sym_frag = symbol_get_frag (fragp->fr_symbol);
12361   val = S_GET_VALUE (fragp->fr_symbol);
12362   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12363
12364   if (op->pcrel)
12365     {
12366       addressT addr;
12367
12368       /* We won't have the section when we are called from
12369          mips_relax_frag.  However, we will always have been called
12370          from md_estimate_size_before_relax first.  If this is a
12371          branch to a different section, we mark it as such.  If SEC is
12372          NULL, and the frag is not marked, then it must be a branch to
12373          the same section.  */
12374       if (sec == NULL)
12375         {
12376           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12377             return 1;
12378         }
12379       else
12380         {
12381           /* Must have been called from md_estimate_size_before_relax.  */
12382           if (symsec != sec)
12383             {
12384               fragp->fr_subtype =
12385                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12386
12387               /* FIXME: We should support this, and let the linker
12388                  catch branches and loads that are out of range.  */
12389               as_bad_where (fragp->fr_file, fragp->fr_line,
12390                             _("unsupported PC relative reference to different section"));
12391
12392               return 1;
12393             }
12394           if (fragp != sym_frag && sym_frag->fr_address == 0)
12395             /* Assume non-extended on the first relaxation pass.
12396                The address we have calculated will be bogus if this is
12397                a forward branch to another frag, as the forward frag
12398                will have fr_address == 0.  */
12399             return 0;
12400         }
12401
12402       /* In this case, we know for sure that the symbol fragment is in
12403          the same section.  If the relax_marker of the symbol fragment
12404          differs from the relax_marker of this fragment, we have not
12405          yet adjusted the symbol fragment fr_address.  We want to add
12406          in STRETCH in order to get a better estimate of the address.
12407          This particularly matters because of the shift bits.  */
12408       if (stretch != 0
12409           && sym_frag->relax_marker != fragp->relax_marker)
12410         {
12411           fragS *f;
12412
12413           /* Adjust stretch for any alignment frag.  Note that if have
12414              been expanding the earlier code, the symbol may be
12415              defined in what appears to be an earlier frag.  FIXME:
12416              This doesn't handle the fr_subtype field, which specifies
12417              a maximum number of bytes to skip when doing an
12418              alignment.  */
12419           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12420             {
12421               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12422                 {
12423                   if (stretch < 0)
12424                     stretch = - ((- stretch)
12425                                  & ~ ((1 << (int) f->fr_offset) - 1));
12426                   else
12427                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12428                   if (stretch == 0)
12429                     break;
12430                 }
12431             }
12432           if (f != NULL)
12433             val += stretch;
12434         }
12435
12436       addr = fragp->fr_address + fragp->fr_fix;
12437
12438       /* The base address rules are complicated.  The base address of
12439          a branch is the following instruction.  The base address of a
12440          PC relative load or add is the instruction itself, but if it
12441          is in a delay slot (in which case it can not be extended) use
12442          the address of the instruction whose delay slot it is in.  */
12443       if (type == 'p' || type == 'q')
12444         {
12445           addr += 2;
12446
12447           /* If we are currently assuming that this frag should be
12448              extended, then, the current address is two bytes
12449              higher.  */
12450           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12451             addr += 2;
12452
12453           /* Ignore the low bit in the target, since it will be set
12454              for a text label.  */
12455           if ((val & 1) != 0)
12456             --val;
12457         }
12458       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12459         addr -= 4;
12460       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12461         addr -= 2;
12462
12463       val -= addr & ~ ((1 << op->shift) - 1);
12464
12465       /* Branch offsets have an implicit 0 in the lowest bit.  */
12466       if (type == 'p' || type == 'q')
12467         val /= 2;
12468
12469       /* If any of the shifted bits are set, we must use an extended
12470          opcode.  If the address depends on the size of this
12471          instruction, this can lead to a loop, so we arrange to always
12472          use an extended opcode.  We only check this when we are in
12473          the main relaxation loop, when SEC is NULL.  */
12474       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12475         {
12476           fragp->fr_subtype =
12477             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12478           return 1;
12479         }
12480
12481       /* If we are about to mark a frag as extended because the value
12482          is precisely maxtiny + 1, then there is a chance of an
12483          infinite loop as in the following code:
12484              la $4,foo
12485              .skip      1020
12486              .align     2
12487            foo:
12488          In this case when the la is extended, foo is 0x3fc bytes
12489          away, so the la can be shrunk, but then foo is 0x400 away, so
12490          the la must be extended.  To avoid this loop, we mark the
12491          frag as extended if it was small, and is about to become
12492          extended with a value of maxtiny + 1.  */
12493       if (val == ((maxtiny + 1) << op->shift)
12494           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12495           && sec == NULL)
12496         {
12497           fragp->fr_subtype =
12498             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12499           return 1;
12500         }
12501     }
12502   else if (symsec != absolute_section && sec != NULL)
12503     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12504
12505   if ((val & ((1 << op->shift) - 1)) != 0
12506       || val < (mintiny << op->shift)
12507       || val > (maxtiny << op->shift))
12508     return 1;
12509   else
12510     return 0;
12511 }
12512
12513 /* Estimate the size of a frag before relaxing.  Unless this is the
12514    mips16, we are not really relaxing here, and the final size is
12515    encoded in the subtype information.  For the mips16, we have to
12516    decide whether we are using an extended opcode or not.  */
12517
12518 int
12519 md_estimate_size_before_relax (fragp, segtype)
12520      fragS *fragp;
12521      asection *segtype;
12522 {
12523   int change = 0;
12524   boolean linkonce = false;
12525
12526   if (RELAX_MIPS16_P (fragp->fr_subtype))
12527     /* We don't want to modify the EXTENDED bit here; it might get us
12528        into infinite loops.  We change it only in mips_relax_frag().  */
12529     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12530
12531   if (mips_pic == NO_PIC)
12532     {
12533       change = nopic_need_relax (fragp->fr_symbol, 0);
12534     }
12535   else if (mips_pic == SVR4_PIC)
12536     {
12537       symbolS *sym;
12538       asection *symsec;
12539
12540       sym = fragp->fr_symbol;
12541
12542       /* Handle the case of a symbol equated to another symbol.  */
12543       while (symbol_equated_reloc_p (sym))
12544         {
12545           symbolS *n;
12546
12547           /* It's possible to get a loop here in a badly written
12548              program.  */
12549           n = symbol_get_value_expression (sym)->X_add_symbol;
12550           if (n == sym)
12551             break;
12552           sym = n;
12553         }
12554
12555       symsec = S_GET_SEGMENT (sym);
12556
12557       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12558       if (symsec != segtype && ! S_IS_LOCAL (sym))
12559         {
12560           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12561               != 0)
12562             linkonce = true;
12563
12564           /* The GNU toolchain uses an extension for ELF: a section
12565              beginning with the magic string .gnu.linkonce is a linkonce
12566              section.  */
12567           if (strncmp (segment_name (symsec), ".gnu.linkonce",
12568                        sizeof ".gnu.linkonce" - 1) == 0)
12569             linkonce = true;
12570         }
12571
12572       /* This must duplicate the test in adjust_reloc_syms.  */
12573       change = (symsec != &bfd_und_section
12574                 && symsec != &bfd_abs_section
12575                 && ! bfd_is_com_section (symsec)
12576                 && !linkonce
12577 #ifdef OBJ_ELF
12578                 /* A global or weak symbol is treated as external.  */
12579                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12580                     || (! S_IS_WEAK (sym)
12581                         && (! S_IS_EXTERNAL (sym)
12582                             || mips_pic == EMBEDDED_PIC)))
12583 #endif
12584                 );
12585     }
12586   else
12587     abort ();
12588
12589   if (change)
12590     {
12591       /* Record the offset to the first reloc in the fr_opcode field.
12592          This lets md_convert_frag and tc_gen_reloc know that the code
12593          must be expanded.  */
12594       fragp->fr_opcode = (fragp->fr_literal
12595                           + fragp->fr_fix
12596                           - RELAX_OLD (fragp->fr_subtype)
12597                           + RELAX_RELOC1 (fragp->fr_subtype));
12598       /* FIXME: This really needs as_warn_where.  */
12599       if (RELAX_WARN (fragp->fr_subtype))
12600         as_warn (_("AT used after \".set noat\" or macro used after "
12601                    "\".set nomacro\""));
12602
12603       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12604     }
12605
12606   return 0;
12607 }
12608
12609 /* This is called to see whether a reloc against a defined symbol
12610    should be converted into a reloc against a section.  Don't adjust
12611    MIPS16 jump relocations, so we don't have to worry about the format
12612    of the offset in the .o file.  Don't adjust relocations against
12613    mips16 symbols, so that the linker can find them if it needs to set
12614    up a stub.  */
12615
12616 int
12617 mips_fix_adjustable (fixp)
12618      fixS *fixp;
12619 {
12620   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12621     return 0;
12622
12623   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12624       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12625     return 0;
12626
12627   if (fixp->fx_addsy == NULL)
12628     return 1;
12629
12630 #ifdef OBJ_ELF
12631   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12632       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12633       && fixp->fx_subsy == NULL)
12634     return 0;
12635 #endif
12636
12637   return 1;
12638 }
12639
12640 /* Translate internal representation of relocation info to BFD target
12641    format.  */
12642
12643 arelent **
12644 tc_gen_reloc (section, fixp)
12645      asection *section ATTRIBUTE_UNUSED;
12646      fixS *fixp;
12647 {
12648   static arelent *retval[4];
12649   arelent *reloc;
12650   bfd_reloc_code_real_type code;
12651
12652   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12653   retval[1] = NULL;
12654
12655   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12656   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12657   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12658
12659   if (mips_pic == EMBEDDED_PIC
12660       && SWITCH_TABLE (fixp))
12661     {
12662       /* For a switch table entry we use a special reloc.  The addend
12663          is actually the difference between the reloc address and the
12664          subtrahend.  */
12665       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12666       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12667         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12668       fixp->fx_r_type = BFD_RELOC_GPREL32;
12669     }
12670   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12671     {
12672       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12673         reloc->addend = fixp->fx_addnumber;
12674       else
12675         {
12676           /* We use a special addend for an internal RELLO reloc.  */
12677           if (symbol_section_p (fixp->fx_addsy))
12678             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12679           else
12680             reloc->addend = fixp->fx_addnumber + reloc->address;
12681         }
12682     }
12683   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12684     {
12685       assert (fixp->fx_next != NULL
12686               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12687
12688       /* The reloc is relative to the RELLO; adjust the addend
12689          accordingly.  */
12690       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12691         reloc->addend = fixp->fx_next->fx_addnumber;
12692       else
12693         {
12694           /* We use a special addend for an internal RELHI reloc.  */
12695           if (symbol_section_p (fixp->fx_addsy))
12696             reloc->addend = (fixp->fx_next->fx_frag->fr_address
12697                              + fixp->fx_next->fx_where
12698                              - S_GET_VALUE (fixp->fx_subsy));
12699           else
12700             reloc->addend = (fixp->fx_addnumber
12701                              + fixp->fx_next->fx_frag->fr_address
12702                              + fixp->fx_next->fx_where);
12703         }
12704     }
12705   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12706     reloc->addend = fixp->fx_addnumber;
12707   else
12708     {
12709       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12710         /* A gruesome hack which is a result of the gruesome gas reloc
12711            handling.  */
12712         reloc->addend = reloc->address;
12713       else
12714         reloc->addend = -reloc->address;
12715     }
12716
12717   /* If this is a variant frag, we may need to adjust the existing
12718      reloc and generate a new one.  */
12719   if (fixp->fx_frag->fr_opcode != NULL
12720       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
12721            && ! HAVE_NEWABI)
12722           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12723           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12724           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12725           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12726           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12727           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12728     )
12729     {
12730       arelent *reloc2;
12731
12732       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12733
12734       /* If this is not the last reloc in this frag, then we have two
12735          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12736          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
12737          the second one handle all of them.  */
12738       if (fixp->fx_next != NULL
12739           && fixp->fx_frag == fixp->fx_next->fx_frag)
12740         {
12741           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12742                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12743                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12744                       && (fixp->fx_next->fx_r_type
12745                           == BFD_RELOC_MIPS_GOT_LO16))
12746                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12747                       && (fixp->fx_next->fx_r_type
12748                           == BFD_RELOC_MIPS_CALL_LO16)));
12749           retval[0] = NULL;
12750           return retval;
12751         }
12752
12753       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12754       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12755       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12756       retval[2] = NULL;
12757       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12758       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12759       reloc2->address = (reloc->address
12760                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12761                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12762       reloc2->addend = fixp->fx_addnumber;
12763       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12764       assert (reloc2->howto != NULL);
12765
12766       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12767         {
12768           arelent *reloc3;
12769
12770           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12771           retval[3] = NULL;
12772           *reloc3 = *reloc2;
12773           reloc3->address += 4;
12774         }
12775
12776       if (mips_pic == NO_PIC)
12777         {
12778           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12779           fixp->fx_r_type = BFD_RELOC_HI16_S;
12780         }
12781       else if (mips_pic == SVR4_PIC)
12782         {
12783           switch (fixp->fx_r_type)
12784             {
12785             default:
12786               abort ();
12787             case BFD_RELOC_MIPS_GOT16:
12788               break;
12789             case BFD_RELOC_MIPS_GOT_LO16:
12790             case BFD_RELOC_MIPS_CALL_LO16:
12791               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12792               break;
12793             case BFD_RELOC_MIPS_CALL16:
12794               if (HAVE_NEWABI)
12795                 {
12796                   /* BFD_RELOC_MIPS_GOT16;*/
12797                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
12798                   reloc2->howto = bfd_reloc_type_lookup
12799                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
12800                 }
12801               else
12802                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12803               break;
12804             }
12805         }
12806       else
12807         abort ();
12808
12809       /* newabi uses R_MIPS_GOT_DISP for local symbols */
12810       if (HAVE_NEWABI && BFD_RELOC_MIPS_GOT_LO16)
12811         {
12812           fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
12813           retval[1] = NULL;
12814         }
12815     }
12816
12817   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12818      entry to be used in the relocation's section offset.  */
12819   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12820     {
12821       reloc->address = reloc->addend;
12822       reloc->addend = 0;
12823     }
12824
12825   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12826      fixup_segment converted a non-PC relative reloc into a PC
12827      relative reloc.  In such a case, we need to convert the reloc
12828      code.  */
12829   code = fixp->fx_r_type;
12830   if (fixp->fx_pcrel)
12831     {
12832       switch (code)
12833         {
12834         case BFD_RELOC_8:
12835           code = BFD_RELOC_8_PCREL;
12836           break;
12837         case BFD_RELOC_16:
12838           code = BFD_RELOC_16_PCREL;
12839           break;
12840         case BFD_RELOC_32:
12841           code = BFD_RELOC_32_PCREL;
12842           break;
12843         case BFD_RELOC_64:
12844           code = BFD_RELOC_64_PCREL;
12845           break;
12846         case BFD_RELOC_8_PCREL:
12847         case BFD_RELOC_16_PCREL:
12848         case BFD_RELOC_32_PCREL:
12849         case BFD_RELOC_64_PCREL:
12850         case BFD_RELOC_16_PCREL_S2:
12851         case BFD_RELOC_PCREL_HI16_S:
12852         case BFD_RELOC_PCREL_LO16:
12853           break;
12854         default:
12855           as_bad_where (fixp->fx_file, fixp->fx_line,
12856                         _("Cannot make %s relocation PC relative"),
12857                         bfd_get_reloc_code_name (code));
12858         }
12859     }
12860
12861 #ifdef OBJ_ELF
12862   /* md_apply_fix3 has a double-subtraction hack to get
12863      bfd_install_relocation to behave nicely.  GPREL relocations are
12864      handled correctly without this hack, so undo it here.  We can't
12865      stop md_apply_fix3 from subtracting twice in the first place since
12866      the fake addend is required for variant frags above.  */
12867   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12868       && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
12869       && reloc->addend != 0
12870       && mips_need_elf_addend_fixup (fixp))
12871     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12872 #endif
12873
12874   /* To support a PC relative reloc when generating embedded PIC code
12875      for ECOFF, we use a Cygnus extension.  We check for that here to
12876      make sure that we don't let such a reloc escape normally.  */
12877   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12878        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12879       && code == BFD_RELOC_16_PCREL_S2
12880       && mips_pic != EMBEDDED_PIC)
12881     reloc->howto = NULL;
12882   else
12883     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12884
12885   if (reloc->howto == NULL)
12886     {
12887       as_bad_where (fixp->fx_file, fixp->fx_line,
12888                     _("Can not represent %s relocation in this object file format"),
12889                     bfd_get_reloc_code_name (code));
12890       retval[0] = NULL;
12891     }
12892
12893   return retval;
12894 }
12895
12896 /* Relax a machine dependent frag.  This returns the amount by which
12897    the current size of the frag should change.  */
12898
12899 int
12900 mips_relax_frag (fragp, stretch)
12901      fragS *fragp;
12902      long stretch;
12903 {
12904   if (! RELAX_MIPS16_P (fragp->fr_subtype))
12905     return 0;
12906
12907   if (mips16_extended_frag (fragp, NULL, stretch))
12908     {
12909       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12910         return 0;
12911       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12912       return 2;
12913     }
12914   else
12915     {
12916       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12917         return 0;
12918       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12919       return -2;
12920     }
12921
12922   return 0;
12923 }
12924
12925 /* Convert a machine dependent frag.  */
12926
12927 void
12928 md_convert_frag (abfd, asec, fragp)
12929      bfd *abfd ATTRIBUTE_UNUSED;
12930      segT asec;
12931      fragS *fragp;
12932 {
12933   int old, new;
12934   char *fixptr;
12935
12936   if (RELAX_MIPS16_P (fragp->fr_subtype))
12937     {
12938       int type;
12939       register const struct mips16_immed_operand *op;
12940       boolean small, ext;
12941       offsetT val;
12942       bfd_byte *buf;
12943       unsigned long insn;
12944       boolean use_extend;
12945       unsigned short extend;
12946
12947       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12948       op = mips16_immed_operands;
12949       while (op->type != type)
12950         ++op;
12951
12952       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12953         {
12954           small = false;
12955           ext = true;
12956         }
12957       else
12958         {
12959           small = true;
12960           ext = false;
12961         }
12962
12963       resolve_symbol_value (fragp->fr_symbol);
12964       val = S_GET_VALUE (fragp->fr_symbol);
12965       if (op->pcrel)
12966         {
12967           addressT addr;
12968
12969           addr = fragp->fr_address + fragp->fr_fix;
12970
12971           /* The rules for the base address of a PC relative reloc are
12972              complicated; see mips16_extended_frag.  */
12973           if (type == 'p' || type == 'q')
12974             {
12975               addr += 2;
12976               if (ext)
12977                 addr += 2;
12978               /* Ignore the low bit in the target, since it will be
12979                  set for a text label.  */
12980               if ((val & 1) != 0)
12981                 --val;
12982             }
12983           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12984             addr -= 4;
12985           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12986             addr -= 2;
12987
12988           addr &= ~ (addressT) ((1 << op->shift) - 1);
12989           val -= addr;
12990
12991           /* Make sure the section winds up with the alignment we have
12992              assumed.  */
12993           if (op->shift > 0)
12994             record_alignment (asec, op->shift);
12995         }
12996
12997       if (ext
12998           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12999               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13000         as_warn_where (fragp->fr_file, fragp->fr_line,
13001                        _("extended instruction in delay slot"));
13002
13003       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13004
13005       if (target_big_endian)
13006         insn = bfd_getb16 (buf);
13007       else
13008         insn = bfd_getl16 (buf);
13009
13010       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13011                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13012                     small, ext, &insn, &use_extend, &extend);
13013
13014       if (use_extend)
13015         {
13016           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13017           fragp->fr_fix += 2;
13018           buf += 2;
13019         }
13020
13021       md_number_to_chars ((char *) buf, insn, 2);
13022       fragp->fr_fix += 2;
13023       buf += 2;
13024     }
13025   else
13026     {
13027       if (fragp->fr_opcode == NULL)
13028         return;
13029
13030       old = RELAX_OLD (fragp->fr_subtype);
13031       new = RELAX_NEW (fragp->fr_subtype);
13032       fixptr = fragp->fr_literal + fragp->fr_fix;
13033
13034       if (new > 0)
13035         memcpy (fixptr - old, fixptr, new);
13036
13037       fragp->fr_fix += new - old;
13038     }
13039 }
13040
13041 #ifdef OBJ_ELF
13042
13043 /* This function is called after the relocs have been generated.
13044    We've been storing mips16 text labels as odd.  Here we convert them
13045    back to even for the convenience of the debugger.  */
13046
13047 void
13048 mips_frob_file_after_relocs ()
13049 {
13050   asymbol **syms;
13051   unsigned int count, i;
13052
13053   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13054     return;
13055
13056   syms = bfd_get_outsymbols (stdoutput);
13057   count = bfd_get_symcount (stdoutput);
13058   for (i = 0; i < count; i++, syms++)
13059     {
13060       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13061           && ((*syms)->value & 1) != 0)
13062         {
13063           (*syms)->value &= ~1;
13064           /* If the symbol has an odd size, it was probably computed
13065              incorrectly, so adjust that as well.  */
13066           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13067             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13068         }
13069     }
13070 }
13071
13072 #endif
13073
13074 /* This function is called whenever a label is defined.  It is used
13075    when handling branch delays; if a branch has a label, we assume we
13076    can not move it.  */
13077
13078 void
13079 mips_define_label (sym)
13080      symbolS *sym;
13081 {
13082   struct insn_label_list *l;
13083
13084   if (free_insn_labels == NULL)
13085     l = (struct insn_label_list *) xmalloc (sizeof *l);
13086   else
13087     {
13088       l = free_insn_labels;
13089       free_insn_labels = l->next;
13090     }
13091
13092   l->label = sym;
13093   l->next = insn_labels;
13094   insn_labels = l;
13095 }
13096 \f
13097 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13098
13099 /* Some special processing for a MIPS ELF file.  */
13100
13101 void
13102 mips_elf_final_processing ()
13103 {
13104   /* Write out the register information.  */
13105   if (mips_abi != N64_ABI)
13106     {
13107       Elf32_RegInfo s;
13108
13109       s.ri_gprmask = mips_gprmask;
13110       s.ri_cprmask[0] = mips_cprmask[0];
13111       s.ri_cprmask[1] = mips_cprmask[1];
13112       s.ri_cprmask[2] = mips_cprmask[2];
13113       s.ri_cprmask[3] = mips_cprmask[3];
13114       /* The gp_value field is set by the MIPS ELF backend.  */
13115
13116       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13117                                        ((Elf32_External_RegInfo *)
13118                                         mips_regmask_frag));
13119     }
13120   else
13121     {
13122       Elf64_Internal_RegInfo s;
13123
13124       s.ri_gprmask = mips_gprmask;
13125       s.ri_pad = 0;
13126       s.ri_cprmask[0] = mips_cprmask[0];
13127       s.ri_cprmask[1] = mips_cprmask[1];
13128       s.ri_cprmask[2] = mips_cprmask[2];
13129       s.ri_cprmask[3] = mips_cprmask[3];
13130       /* The gp_value field is set by the MIPS ELF backend.  */
13131
13132       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13133                                        ((Elf64_External_RegInfo *)
13134                                         mips_regmask_frag));
13135     }
13136
13137   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13138      sort of BFD interface for this.  */
13139   if (mips_any_noreorder)
13140     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13141   if (mips_pic != NO_PIC)
13142     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13143
13144   /* Set MIPS ELF flags for ASEs.  */
13145   if (file_ase_mips16)
13146     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13147 #if 0 /* XXX FIXME */
13148   if (file_ase_mips3d)
13149     elf_elfheader (stdoutput)->e_flags |= ???;
13150 #endif
13151   if (file_ase_mdmx)
13152     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13153
13154   /* Set the MIPS ELF ABI flags.  */
13155   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13156     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13157   else if (mips_abi == O64_ABI)
13158     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13159   else if (mips_abi == EABI_ABI)
13160     {
13161       if (!file_mips_gp32)
13162         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13163       else
13164         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13165     }
13166   else if (mips_abi == N32_ABI)
13167     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13168
13169   /* Nothing to do for N64_ABI.  */
13170
13171   if (mips_32bitmode)
13172     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13173 }
13174
13175 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13176 \f
13177 typedef struct proc {
13178   symbolS *isym;
13179   unsigned long reg_mask;
13180   unsigned long reg_offset;
13181   unsigned long fpreg_mask;
13182   unsigned long fpreg_offset;
13183   unsigned long frame_offset;
13184   unsigned long frame_reg;
13185   unsigned long pc_reg;
13186 } procS;
13187
13188 static procS cur_proc;
13189 static procS *cur_proc_ptr;
13190 static int numprocs;
13191
13192 /* Fill in an rs_align_code fragment.  */
13193
13194 void
13195 mips_handle_align (fragp)
13196      fragS *fragp;
13197 {
13198   if (fragp->fr_type != rs_align_code)
13199     return;
13200
13201   if (mips_opts.mips16)
13202     {
13203       static const unsigned char be_nop[] = { 0x65, 0x00 };
13204       static const unsigned char le_nop[] = { 0x00, 0x65 };
13205
13206       int bytes;
13207       char *p;
13208
13209       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13210       p = fragp->fr_literal + fragp->fr_fix;
13211
13212       if (bytes & 1)
13213         {
13214           *p++ = 0;
13215           fragp->fr_fix++;
13216         }
13217
13218       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13219       fragp->fr_var = 2;
13220     }
13221
13222   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13223 }
13224
13225 static void
13226 md_obj_begin ()
13227 {
13228 }
13229
13230 static void
13231 md_obj_end ()
13232 {
13233   /* check for premature end, nesting errors, etc */
13234   if (cur_proc_ptr)
13235     as_warn (_("missing .end at end of assembly"));
13236 }
13237
13238 static long
13239 get_number ()
13240 {
13241   int negative = 0;
13242   long val = 0;
13243
13244   if (*input_line_pointer == '-')
13245     {
13246       ++input_line_pointer;
13247       negative = 1;
13248     }
13249   if (!ISDIGIT (*input_line_pointer))
13250     as_bad (_("expected simple number"));
13251   if (input_line_pointer[0] == '0')
13252     {
13253       if (input_line_pointer[1] == 'x')
13254         {
13255           input_line_pointer += 2;
13256           while (ISXDIGIT (*input_line_pointer))
13257             {
13258               val <<= 4;
13259               val |= hex_value (*input_line_pointer++);
13260             }
13261           return negative ? -val : val;
13262         }
13263       else
13264         {
13265           ++input_line_pointer;
13266           while (ISDIGIT (*input_line_pointer))
13267             {
13268               val <<= 3;
13269               val |= *input_line_pointer++ - '0';
13270             }
13271           return negative ? -val : val;
13272         }
13273     }
13274   if (!ISDIGIT (*input_line_pointer))
13275     {
13276       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13277               *input_line_pointer, *input_line_pointer);
13278       as_warn (_("invalid number"));
13279       return -1;
13280     }
13281   while (ISDIGIT (*input_line_pointer))
13282     {
13283       val *= 10;
13284       val += *input_line_pointer++ - '0';
13285     }
13286   return negative ? -val : val;
13287 }
13288
13289 /* The .file directive; just like the usual .file directive, but there
13290    is an initial number which is the ECOFF file index.  In the non-ECOFF
13291    case .file implies DWARF-2.  */
13292
13293 static void
13294 s_mips_file (x)
13295      int x ATTRIBUTE_UNUSED;
13296 {
13297   static int first_file_directive = 0;
13298
13299   if (ECOFF_DEBUGGING)
13300     {
13301       get_number ();
13302       s_app_file (0);
13303     }
13304   else
13305     {
13306       char *filename;
13307
13308       filename = dwarf2_directive_file (0);
13309
13310       /* Versions of GCC up to 3.1 start files with a ".file"
13311          directive even for stabs output.  Make sure that this
13312          ".file" is handled.  Note that you need a version of GCC
13313          after 3.1 in order to support DWARF-2 on MIPS.  */
13314       if (filename != NULL && ! first_file_directive)
13315         {
13316           (void) new_logical_line (filename, -1);
13317           s_app_file_string (filename);
13318         }
13319       first_file_directive = 1;
13320     }
13321 }
13322
13323 /* The .loc directive, implying DWARF-2.  */
13324
13325 static void
13326 s_mips_loc (x)
13327      int x ATTRIBUTE_UNUSED;
13328 {
13329   if (!ECOFF_DEBUGGING)
13330     dwarf2_directive_loc (0);
13331 }
13332
13333 /* The .end directive.  */
13334
13335 static void
13336 s_mips_end (x)
13337      int x ATTRIBUTE_UNUSED;
13338 {
13339   symbolS *p;
13340   int maybe_text;
13341
13342   /* Following functions need their own .frame and .cprestore directives.  */
13343   mips_frame_reg_valid = 0;
13344   mips_cprestore_valid = 0;
13345
13346   if (!is_end_of_line[(unsigned char) *input_line_pointer])
13347     {
13348       p = get_symbol ();
13349       demand_empty_rest_of_line ();
13350     }
13351   else
13352     p = NULL;
13353
13354 #ifdef BFD_ASSEMBLER
13355   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13356     maybe_text = 1;
13357   else
13358     maybe_text = 0;
13359 #else
13360   if (now_seg != data_section && now_seg != bss_section)
13361     maybe_text = 1;
13362   else
13363     maybe_text = 0;
13364 #endif
13365
13366   if (!maybe_text)
13367     as_warn (_(".end not in text section"));
13368
13369   if (!cur_proc_ptr)
13370     {
13371       as_warn (_(".end directive without a preceding .ent directive."));
13372       demand_empty_rest_of_line ();
13373       return;
13374     }
13375
13376   if (p != NULL)
13377     {
13378       assert (S_GET_NAME (p));
13379       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13380         as_warn (_(".end symbol does not match .ent symbol."));
13381
13382       if (debug_type == DEBUG_STABS)
13383         stabs_generate_asm_endfunc (S_GET_NAME (p),
13384                                     S_GET_NAME (p));
13385     }
13386   else
13387     as_warn (_(".end directive missing or unknown symbol"));
13388
13389 #ifdef OBJ_ELF
13390   /* Generate a .pdr section.  */
13391   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13392     {
13393       segT saved_seg = now_seg;
13394       subsegT saved_subseg = now_subseg;
13395       valueT dot;
13396       expressionS exp;
13397       char *fragp;
13398
13399       dot = frag_now_fix ();
13400
13401 #ifdef md_flush_pending_output
13402       md_flush_pending_output ();
13403 #endif
13404
13405       assert (pdr_seg);
13406       subseg_set (pdr_seg, 0);
13407
13408       /* Write the symbol.  */
13409       exp.X_op = O_symbol;
13410       exp.X_add_symbol = p;
13411       exp.X_add_number = 0;
13412       emit_expr (&exp, 4);
13413
13414       fragp = frag_more (7 * 4);
13415
13416       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
13417       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
13418       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13419       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13420       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13421       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13422       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
13423
13424       subseg_set (saved_seg, saved_subseg);
13425     }
13426 #endif /* OBJ_ELF */
13427
13428   cur_proc_ptr = NULL;
13429 }
13430
13431 /* The .aent and .ent directives.  */
13432
13433 static void
13434 s_mips_ent (aent)
13435      int aent;
13436 {
13437   symbolS *symbolP;
13438   int maybe_text;
13439
13440   symbolP = get_symbol ();
13441   if (*input_line_pointer == ',')
13442     ++input_line_pointer;
13443   SKIP_WHITESPACE ();
13444   if (ISDIGIT (*input_line_pointer)
13445       || *input_line_pointer == '-')
13446     get_number ();
13447
13448 #ifdef BFD_ASSEMBLER
13449   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13450     maybe_text = 1;
13451   else
13452     maybe_text = 0;
13453 #else
13454   if (now_seg != data_section && now_seg != bss_section)
13455     maybe_text = 1;
13456   else
13457     maybe_text = 0;
13458 #endif
13459
13460   if (!maybe_text)
13461     as_warn (_(".ent or .aent not in text section."));
13462
13463   if (!aent && cur_proc_ptr)
13464     as_warn (_("missing .end"));
13465
13466   if (!aent)
13467     {
13468       /* This function needs its own .frame and .cprestore directives.  */
13469       mips_frame_reg_valid = 0;
13470       mips_cprestore_valid = 0;
13471
13472       cur_proc_ptr = &cur_proc;
13473       memset (cur_proc_ptr, '\0', sizeof (procS));
13474
13475       cur_proc_ptr->isym = symbolP;
13476
13477       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13478
13479       ++numprocs;
13480
13481       if (debug_type == DEBUG_STABS)
13482         stabs_generate_asm_func (S_GET_NAME (symbolP),
13483                                  S_GET_NAME (symbolP));
13484     }
13485
13486   demand_empty_rest_of_line ();
13487 }
13488
13489 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13490    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13491    s_mips_frame is used so that we can set the PDR information correctly.
13492    We can't use the ecoff routines because they make reference to the ecoff
13493    symbol table (in the mdebug section).  */
13494
13495 static void
13496 s_mips_frame (ignore)
13497      int ignore ATTRIBUTE_UNUSED;
13498 {
13499 #ifdef OBJ_ELF
13500   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13501     {
13502       long val;
13503
13504       if (cur_proc_ptr == (procS *) NULL)
13505         {
13506           as_warn (_(".frame outside of .ent"));
13507           demand_empty_rest_of_line ();
13508           return;
13509         }
13510
13511       cur_proc_ptr->frame_reg = tc_get_register (1);
13512
13513       SKIP_WHITESPACE ();
13514       if (*input_line_pointer++ != ','
13515           || get_absolute_expression_and_terminator (&val) != ',')
13516         {
13517           as_warn (_("Bad .frame directive"));
13518           --input_line_pointer;
13519           demand_empty_rest_of_line ();
13520           return;
13521         }
13522
13523       cur_proc_ptr->frame_offset = val;
13524       cur_proc_ptr->pc_reg = tc_get_register (0);
13525
13526       demand_empty_rest_of_line ();
13527     }
13528   else
13529 #endif /* OBJ_ELF */
13530     s_ignore (ignore);
13531 }
13532
13533 /* The .fmask and .mask directives. If the mdebug section is present
13534    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13535    embedded targets, s_mips_mask is used so that we can set the PDR
13536    information correctly. We can't use the ecoff routines because they
13537    make reference to the ecoff symbol table (in the mdebug section).  */
13538
13539 static void
13540 s_mips_mask (reg_type)
13541      char reg_type;
13542 {
13543 #ifdef OBJ_ELF
13544   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13545     {
13546       long mask, off;
13547
13548       if (cur_proc_ptr == (procS *) NULL)
13549         {
13550           as_warn (_(".mask/.fmask outside of .ent"));
13551           demand_empty_rest_of_line ();
13552           return;
13553         }
13554
13555       if (get_absolute_expression_and_terminator (&mask) != ',')
13556         {
13557           as_warn (_("Bad .mask/.fmask directive"));
13558           --input_line_pointer;
13559           demand_empty_rest_of_line ();
13560           return;
13561         }
13562
13563       off = get_absolute_expression ();
13564
13565       if (reg_type == 'F')
13566         {
13567           cur_proc_ptr->fpreg_mask = mask;
13568           cur_proc_ptr->fpreg_offset = off;
13569         }
13570       else
13571         {
13572           cur_proc_ptr->reg_mask = mask;
13573           cur_proc_ptr->reg_offset = off;
13574         }
13575
13576       demand_empty_rest_of_line ();
13577     }
13578   else
13579 #endif /* OBJ_ELF */
13580     s_ignore (reg_type);
13581 }
13582
13583 /* The .loc directive.  */
13584
13585 #if 0
13586 static void
13587 s_loc (x)
13588      int x;
13589 {
13590   symbolS *symbolP;
13591   int lineno;
13592   int addroff;
13593
13594   assert (now_seg == text_section);
13595
13596   lineno = get_number ();
13597   addroff = frag_now_fix ();
13598
13599   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13600   S_SET_TYPE (symbolP, N_SLINE);
13601   S_SET_OTHER (symbolP, 0);
13602   S_SET_DESC (symbolP, lineno);
13603   symbolP->sy_segment = now_seg;
13604 }
13605 #endif
13606
13607 /* A table describing all the processors gas knows about.  Names are
13608    matched in the order listed.
13609
13610    To ease comparison, please keep this table in the same order as
13611    gcc's mips_cpu_info_table[].  */
13612 static const struct mips_cpu_info mips_cpu_info_table[] =
13613 {
13614   /* Entries for generic ISAs */
13615   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
13616   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
13617   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
13618   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
13619   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
13620   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
13621   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
13622
13623   /* MIPS I */
13624   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
13625   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
13626   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
13627
13628   /* MIPS II */
13629   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
13630
13631   /* MIPS III */
13632   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
13633   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
13634   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
13635   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
13636   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
13637   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
13638   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
13639   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
13640   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
13641
13642   /* MIPS IV */
13643   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
13644   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
13645   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
13646   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
13647   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
13648   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
13649   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
13650   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
13651   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
13652   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
13653
13654   /* MIPS 32 */
13655   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
13656   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
13657   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
13658
13659   /* MIPS 64 */
13660   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
13661   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
13662
13663   /* Broadcom SB-1 CPU core */
13664   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
13665
13666   /* End marker */
13667   { NULL, 0, 0, 0 }
13668 };
13669
13670
13671 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13672    with a final "000" replaced by "k".  Ignore case.
13673
13674    Note: this function is shared between GCC and GAS.  */
13675
13676 static boolean
13677 mips_strict_matching_cpu_name_p (canonical, given)
13678      const char *canonical, *given;
13679 {
13680   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13681     given++, canonical++;
13682
13683   return ((*given == 0 && *canonical == 0)
13684           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13685 }
13686
13687
13688 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13689    CPU name.  We've traditionally allowed a lot of variation here.
13690
13691    Note: this function is shared between GCC and GAS.  */
13692
13693 static boolean
13694 mips_matching_cpu_name_p (canonical, given)
13695      const char *canonical, *given;
13696 {
13697   /* First see if the name matches exactly, or with a final "000"
13698      turned into "k".  */
13699   if (mips_strict_matching_cpu_name_p (canonical, given))
13700     return true;
13701
13702   /* If not, try comparing based on numerical designation alone.
13703      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
13704   if (TOLOWER (*given) == 'r')
13705     given++;
13706   if (!ISDIGIT (*given))
13707     return false;
13708
13709   /* Skip over some well-known prefixes in the canonical name,
13710      hoping to find a number there too.  */
13711   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13712     canonical += 2;
13713   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13714     canonical += 2;
13715   else if (TOLOWER (canonical[0]) == 'r')
13716     canonical += 1;
13717
13718   return mips_strict_matching_cpu_name_p (canonical, given);
13719 }
13720
13721
13722 /* Parse an option that takes the name of a processor as its argument.
13723    OPTION is the name of the option and CPU_STRING is the argument.
13724    Return the corresponding processor enumeration if the CPU_STRING is
13725    recognized, otherwise report an error and return null.
13726
13727    A similar function exists in GCC.  */
13728
13729 static const struct mips_cpu_info *
13730 mips_parse_cpu (option, cpu_string)
13731      const char *option, *cpu_string;
13732 {
13733   const struct mips_cpu_info *p;
13734
13735   /* 'from-abi' selects the most compatible architecture for the given
13736      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
13737      EABIs, we have to decide whether we're using the 32-bit or 64-bit
13738      version.  Look first at the -mgp options, if given, otherwise base
13739      the choice on MIPS_DEFAULT_64BIT.
13740
13741      Treat NO_ABI like the EABIs.  One reason to do this is that the
13742      plain 'mips' and 'mips64' configs have 'from-abi' as their default
13743      architecture.  This code picks MIPS I for 'mips' and MIPS III for
13744      'mips64', just as we did in the days before 'from-abi'.  */
13745   if (strcasecmp (cpu_string, "from-abi") == 0)
13746     {
13747       if (ABI_NEEDS_32BIT_REGS (mips_abi))
13748         return mips_cpu_info_from_isa (ISA_MIPS1);
13749
13750       if (ABI_NEEDS_64BIT_REGS (mips_abi))
13751         return mips_cpu_info_from_isa (ISA_MIPS3);
13752
13753       if (file_mips_gp32 >= 0)
13754         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13755
13756       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13757                                      ? ISA_MIPS3
13758                                      : ISA_MIPS1);
13759     }
13760
13761   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
13762   if (strcasecmp (cpu_string, "default") == 0)
13763     return 0;
13764
13765   for (p = mips_cpu_info_table; p->name != 0; p++)
13766     if (mips_matching_cpu_name_p (p->name, cpu_string))
13767       return p;
13768
13769   as_bad ("Bad value (%s) for %s", cpu_string, option);
13770   return 0;
13771 }
13772
13773 /* Return the canonical processor information for ISA (a member of the
13774    ISA_MIPS* enumeration).  */
13775
13776 static const struct mips_cpu_info *
13777 mips_cpu_info_from_isa (isa)
13778      int isa;
13779 {
13780   int i;
13781
13782   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13783     if (mips_cpu_info_table[i].is_isa
13784         && isa == mips_cpu_info_table[i].isa)
13785       return (&mips_cpu_info_table[i]);
13786
13787   return NULL;
13788 }
13789 \f
13790 static void
13791 show (stream, string, col_p, first_p)
13792      FILE *stream;
13793      const char *string;
13794      int *col_p;
13795      int *first_p;
13796 {
13797   if (*first_p)
13798     {
13799       fprintf (stream, "%24s", "");
13800       *col_p = 24;
13801     }
13802   else
13803     {
13804       fprintf (stream, ", ");
13805       *col_p += 2;
13806     }
13807
13808   if (*col_p + strlen (string) > 72)
13809     {
13810       fprintf (stream, "\n%24s", "");
13811       *col_p = 24;
13812     }
13813
13814   fprintf (stream, "%s", string);
13815   *col_p += strlen (string);
13816
13817   *first_p = 0;
13818 }
13819
13820 void
13821 md_show_usage (stream)
13822      FILE *stream;
13823 {
13824   int column, first;
13825   size_t i;
13826
13827   fprintf (stream, _("\
13828 MIPS options:\n\
13829 -membedded-pic          generate embedded position independent code\n\
13830 -EB                     generate big endian output\n\
13831 -EL                     generate little endian output\n\
13832 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
13833 -G NUM                  allow referencing objects up to NUM bytes\n\
13834                         implicitly with the gp register [default 8]\n"));
13835   fprintf (stream, _("\
13836 -mips1                  generate MIPS ISA I instructions\n\
13837 -mips2                  generate MIPS ISA II instructions\n\
13838 -mips3                  generate MIPS ISA III instructions\n\
13839 -mips4                  generate MIPS ISA IV instructions\n\
13840 -mips5                  generate MIPS ISA V instructions\n\
13841 -mips32                 generate MIPS32 ISA instructions\n\
13842 -mips64                 generate MIPS64 ISA instructions\n\
13843 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
13844
13845   first = 1;
13846
13847   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13848     show (stream, mips_cpu_info_table[i].name, &column, &first);
13849   show (stream, "from-abi", &column, &first);
13850   fputc ('\n', stream);
13851
13852   fprintf (stream, _("\
13853 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
13854 -no-mCPU                don't generate code specific to CPU.\n\
13855                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
13856
13857   first = 1;
13858
13859   show (stream, "3900", &column, &first);
13860   show (stream, "4010", &column, &first);
13861   show (stream, "4100", &column, &first);
13862   show (stream, "4650", &column, &first);
13863   fputc ('\n', stream);
13864
13865   fprintf (stream, _("\
13866 -mips16                 generate mips16 instructions\n\
13867 -no-mips16              do not generate mips16 instructions\n"));
13868   fprintf (stream, _("\
13869 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
13870 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
13871 -O0                     remove unneeded NOPs, do not swap branches\n\
13872 -O                      remove unneeded NOPs and swap branches\n\
13873 -n                      warn about NOPs generated from macros\n\
13874 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
13875 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
13876 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
13877 #ifdef OBJ_ELF
13878   fprintf (stream, _("\
13879 -KPIC, -call_shared     generate SVR4 position independent code\n\
13880 -non_shared             do not generate position independent code\n\
13881 -xgot                   assume a 32 bit GOT\n\
13882 -mabi=ABI               create ABI conformant object file for:\n"));
13883
13884   first = 1;
13885
13886   show (stream, "32", &column, &first);
13887   show (stream, "o64", &column, &first);
13888   show (stream, "n32", &column, &first);
13889   show (stream, "64", &column, &first);
13890   show (stream, "eabi", &column, &first);
13891
13892   fputc ('\n', stream);
13893
13894   fprintf (stream, _("\
13895 -32                     create o32 ABI object file (default)\n\
13896 -n32                    create n32 ABI object file\n\
13897 -64                     create 64 ABI object file\n"));
13898 #endif
13899 }